use serde::{Deserialize, Serialize};
use crate::engine::rules::TerminalShape;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ProcessRef {
pub pid: u32,
pub started_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TerminalRef {
pub adapter: String,
pub process: ProcessRef,
pub shape: TerminalShape,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tab_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub profile: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exe: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum Note {
Shell(ShellNote),
Agent(AgentNote),
}
impl Note {
pub fn process(&self) -> &ProcessRef {
match self {
Note::Shell(note) => ¬e.process,
Note::Agent(note) => ¬e.process,
}
}
pub fn terminal(&self) -> Option<&TerminalRef> {
match self {
Note::Shell(note) => note.terminal.as_ref(),
Note::Agent(note) => note.terminal.as_ref(),
}
}
pub fn session(&self) -> &str {
match self {
Note::Shell(note) => ¬e.session,
Note::Agent(note) => ¬e.session,
}
}
pub fn offered(&self) -> bool {
match self {
Note::Shell(note) => note.offered,
Note::Agent(note) => note.offered,
}
}
pub fn set_offered(&mut self) {
match self {
Note::Shell(note) => note.offered = true,
Note::Agent(note) => note.offered = true,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShellNote {
pub process: ProcessRef,
pub shell: String,
pub cwd: String,
pub terminal: Option<TerminalRef>,
pub session: String,
#[serde(default)]
pub offered: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub adopted_ms: Option<u64>,
}
impl ShellNote {
pub fn adopted(&self) -> bool {
self.adopted_ms.is_some()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AgentNote {
pub process: ProcessRef,
pub agent: String,
pub conversation: String,
pub cwd: String,
pub shell: Option<ProcessRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub shell_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub shell_exe: Option<String>,
pub terminal: Option<TerminalRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub transcript: Option<String>,
pub session: String,
#[serde(default)]
pub offered: bool,
}
#[cfg(test)]
mod tests {
use super::*;
fn terminal() -> TerminalRef {
TerminalRef {
adapter: "windows-terminal".into(),
process: ProcessRef {
pid: 1000,
started_ms: 1_700_000_000_000,
},
shape: TerminalShape::Tabs,
tab_id: Some("00000000-0000-4000-8000-00000000000a".into()),
profile: None,
exe: Some(r"C:\tools\terminal\WindowsTerminal.exe".into()),
}
}
#[test]
fn notes_survive_a_round_trip_through_json() {
let notes = [
Note::Shell(ShellNote {
process: ProcessRef {
pid: 2000,
started_ms: 1_700_000_100_000,
},
shell: "powershell".into(),
cwd: r"C:\Users\demo\code\api-server".into(),
terminal: Some(terminal()),
session: "boot-1".into(),
offered: false,
adopted_ms: None,
}),
Note::Agent(AgentNote {
process: ProcessRef {
pid: 3000,
started_ms: 1_700_000_200_000,
},
agent: "claude".into(),
conversation: "00000000-0000-4000-8000-000000000001".into(),
cwd: "/home/demo/code/mobile-app".into(),
shell: Some(ProcessRef {
pid: 2000,
started_ms: 1_700_000_100_000,
}),
shell_name: Some("powershell".into()),
shell_exe: None,
terminal: Some(terminal()),
transcript: None,
session: "boot-1".into(),
offered: true,
}),
];
for note in notes {
let json = serde_json::to_string(¬e).unwrap();
assert_eq!(serde_json::from_str::<Note>(&json).unwrap(), note);
}
}
#[test]
fn the_kind_is_written_as_a_tag() {
let json = serde_json::to_value(Note::Shell(ShellNote {
process: ProcessRef {
pid: 1,
started_ms: 1,
},
shell: "bash".into(),
cwd: "/tmp".into(),
terminal: None,
session: "s".into(),
offered: false,
adopted_ms: None,
}))
.unwrap();
assert_eq!(json["kind"], "shell");
}
#[test]
fn fields_from_newer_versions_are_ignored() {
let json = r#"{"kind":"shell","process":{"pid":1,"started_ms":1},"shell":"zsh",
"cwd":"/tmp","terminal":null,"session":"s","added_later":42}"#;
let note: Note = serde_json::from_str(json).unwrap();
assert!(matches!(
note,
Note::Shell(ShellNote { offered: false, .. })
));
}
}