mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 20:45:33 -08:00
Add the desktop app loop driver with commit, notify, report and tools modules, and update the quit prompt, task dialog and meter
AI Assisted
This commit is contained in:
1 parent
8db8df5791
commit
1f80fa6a7c
24 files changed
+9850
-64
No files matched your search
@@ -7,6 +7,10 @@
|
||||
//! EXIT <c> exit with code c
|
||||
//! SIZE print the terminal size once
|
||||
//! PID print the agent's process id as `PID <n>`
|
||||
//! REPORT <json> write <json> as `loopOutcome` into the session's state.json
|
||||
//! (PLAN2CODE_CONSOLE_HOME and PLAN2CODE_CONSOLE_SID name it), say
|
||||
//! `REPORT written` (`REPORT unavailable` when either is missing)
|
||||
//! HANG say `HANGING`, then go silent for good (no more lines read)
|
||||
//! <other> echoed back as `echo: <line>`
|
||||
//!
|
||||
//! A change in terminal size is reported on its own as `SIZE <cols>x<rows>`.
|
||||
@@ -36,9 +40,47 @@ pub enum Command {
|
||||
Pid,
|
||||
/// `ENV NAME`: print the variable's value, for tests of what an agent is given.
|
||||
Env(String),
|
||||
/// `REPORT <json>`: the phase report the loop waits for, as the JSON text.
|
||||
Report(String),
|
||||
/// `HANG`: go silent, to model an agent that stops answering.
|
||||
Hang,
|
||||
Echo(String),
|
||||
}
|
||||
|
||||
/// Write `json` as the `loopOutcome` of the session named by the environment.
|
||||
/// Returns false when the session cannot be named.
|
||||
fn write_report(json: &str) -> bool {
|
||||
let (Ok(home), Ok(sid)) = (
|
||||
std::env::var("PLAN2CODE_CONSOLE_HOME"),
|
||||
std::env::var("PLAN2CODE_CONSOLE_SID"),
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
write_report_to(std::path::Path::new(&home), &sid, json)
|
||||
}
|
||||
|
||||
/// `write_report` with the session named: `<home>/sessions/<sid>/state.json`.
|
||||
fn write_report_to(home: &std::path::Path, sid: &str, json: &str) -> bool {
|
||||
let Ok(outcome) = serde_json::from_str::<serde_json::Value>(json) else {
|
||||
return false;
|
||||
};
|
||||
let dir = home.join("sessions").join(sid);
|
||||
let file = dir.join("state.json");
|
||||
let mut state = std::fs::read_to_string(&file)
|
||||
.ok()
|
||||
.and_then(|text| serde_json::from_str::<serde_json::Value>(&text).ok())
|
||||
.filter(|value| value.is_object())
|
||||
// The console's own file always carries its schema number; a new one does too.
|
||||
.unwrap_or_else(|| serde_json::json!({ "schema": 1 }));
|
||||
state["loopOutcome"] = outcome;
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let temp = dir.join("state.json.tmp");
|
||||
let Ok(text) = serde_json::to_string(&state) else {
|
||||
return false;
|
||||
};
|
||||
std::fs::write(&temp, text).is_ok() && std::fs::rename(&temp, &file).is_ok()
|
||||
}
|
||||
|
||||
pub fn parse(line: &str) -> Command {
|
||||
let line = line.trim();
|
||||
if let Some(rest) = line.strip_prefix("STEP ") {
|
||||
@@ -60,6 +102,15 @@ pub fn parse(line: &str) -> Command {
|
||||
if let Some(name) = line.strip_prefix("ENV ") {
|
||||
return Command::Env(name.trim().to_string());
|
||||
}
|
||||
if let Some(rest) = line.strip_prefix("REPORT ") {
|
||||
let json = rest.trim();
|
||||
if serde_json::from_str::<serde_json::Value>(json).is_ok() {
|
||||
return Command::Report(json.to_string());
|
||||
}
|
||||
}
|
||||
if line == "HANG" {
|
||||
return Command::Hang;
|
||||
}
|
||||
Command::Echo(line.to_string())
|
||||
}
|
||||
|
||||
@@ -106,6 +157,19 @@ fn main() {
|
||||
"ENV {name}={}",
|
||||
std::env::var(&name).unwrap_or_default()
|
||||
)),
|
||||
Command::Report(json) => {
|
||||
if write_report(&json) {
|
||||
say("REPORT written")
|
||||
} else {
|
||||
say("REPORT unavailable")
|
||||
}
|
||||
}
|
||||
Command::Hang => {
|
||||
say("HANGING");
|
||||
loop {
|
||||
thread::sleep(Duration::from_secs(3600));
|
||||
}
|
||||
}
|
||||
Command::Echo(text) => say(&format!("echo: {text}")),
|
||||
}
|
||||
}
|
||||
@@ -128,6 +192,47 @@ mod tests {
|
||||
assert_eq!(parse("hello there"), Command::Echo("hello there".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_report_and_hang() {
|
||||
assert_eq!(
|
||||
parse("REPORT {\"phase\":2}"),
|
||||
Command::Report("{\"phase\":2}".into())
|
||||
);
|
||||
assert_eq!(parse("HANG\r\n"), Command::Hang);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_report_lands_in_the_session_file_beside_what_was_there() {
|
||||
let home = std::env::temp_dir().join(format!("fake-agent-report-{}", std::process::id()));
|
||||
let dir = home.join("sessions").join("s9");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
// No file yet: a new one with the schema number.
|
||||
assert!(write_report_to(&home, "s9", "{\"phase\":2}"));
|
||||
let read = || -> serde_json::Value {
|
||||
serde_json::from_str(&std::fs::read_to_string(dir.join("state.json")).unwrap()).unwrap()
|
||||
};
|
||||
assert_eq!(read()["loopOutcome"]["phase"], 2);
|
||||
assert_eq!(read()["schema"], 1);
|
||||
// An existing object keeps its keys.
|
||||
std::fs::write(dir.join("state.json"), "{\"schema\":1,\"title\":\"t\"}").unwrap();
|
||||
assert!(write_report_to(&home, "s9", "{\"phase\":3}"));
|
||||
assert_eq!(read()["title"], "t");
|
||||
assert_eq!(read()["loopOutcome"]["phase"], 3);
|
||||
// A garbage file is replaced, not trusted.
|
||||
std::fs::write(dir.join("state.json"), "not json").unwrap();
|
||||
assert!(write_report_to(&home, "s9", "{\"phase\":4}"));
|
||||
assert_eq!(read()["loopOutcome"]["phase"], 4);
|
||||
let _ = std::fs::remove_dir_all(&home);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_report_with_bad_json_is_an_echo() {
|
||||
assert_eq!(
|
||||
parse("REPORT {not json"),
|
||||
Command::Echo("REPORT {not json".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bad_number_is_just_an_echo() {
|
||||
assert_eq!(parse("STEP x"), Command::Echo("STEP x".into()));
|
||||
|
||||
Reference in new issue
Block a user