mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-29 05:11:19 -08:00
Add the Review Loop: unattended review-and-fix rounds in the desktop app and in loop phases
AI Assisted
This commit is contained in:
1 parent
d6a28b5dc8
commit
57567d7a0b
71 files changed
+11331
-443
No files matched your search
@@ -10,6 +10,13 @@
|
||||
//! 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)
|
||||
//! ROUND <json> write <json> as `roundOutcome` the same way; when it names a
|
||||
//! `report` id, first add that doc (`kind: report`) to `docs`.
|
||||
//! Says `ROUND written` (`ROUND unavailable` when it cannot)
|
||||
//! FILE <path> <text> write <text> to <path> under the working directory (`\n`
|
||||
//! in the text is a line break), for a round's "fixes". Says
|
||||
//! `FILE written <path>` (`FILE refused <path>` for a path that
|
||||
//! leaves the folder)
|
||||
//! ENV <name> print the variable as `ENV <name>=<value>`
|
||||
//! HANG say `HANGING`, then go silent for good (no more lines read)
|
||||
//! <other> echoed back as `echo: <line>`
|
||||
@@ -43,6 +50,10 @@ pub enum Command {
|
||||
Env(String),
|
||||
/// `REPORT <json>`: the phase report the loop waits for, as the JSON text.
|
||||
Report(String),
|
||||
/// `ROUND <json>`: a Review Loop round's report, as the JSON text.
|
||||
Round(String),
|
||||
/// `FILE <path> <text>`: a file the agent "fixed", relative to its folder.
|
||||
File(String, String),
|
||||
/// `HANG`: go silent, to model an agent that stops answering.
|
||||
Hang,
|
||||
Echo(String),
|
||||
@@ -65,6 +76,47 @@ 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;
|
||||
};
|
||||
update_state(home, sid, |state| state["loopOutcome"] = outcome)
|
||||
}
|
||||
|
||||
/// Write the JSON as the `roundOutcome` of the session named by the environment.
|
||||
fn write_round(json: &str) -> bool {
|
||||
let (Ok(home), Ok(sid)) = (
|
||||
std::env::var("PLAN2CODE_CONSOLE_HOME"),
|
||||
std::env::var("PLAN2CODE_CONSOLE_SID"),
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
write_round_to(std::path::Path::new(&home), &sid, json)
|
||||
}
|
||||
|
||||
/// `write_round` with the session named. A named `report` is added to `docs`
|
||||
/// first, as the console would hold it, so the app finds the doc it checks for.
|
||||
fn write_round_to(home: &std::path::Path, sid: &str, json: &str) -> bool {
|
||||
let Ok(outcome) = serde_json::from_str::<serde_json::Value>(json) else {
|
||||
return false;
|
||||
};
|
||||
update_state(home, sid, |state| {
|
||||
if let Some(report) = outcome.get("report").and_then(|value| value.as_str()) {
|
||||
let doc =
|
||||
serde_json::json!({ "id": report, "kind": "report", "title": "Review round" });
|
||||
match state.get_mut("docs").and_then(|docs| docs.as_array_mut()) {
|
||||
Some(docs) => docs.push(doc),
|
||||
None => state["docs"] = serde_json::json!([doc]),
|
||||
}
|
||||
}
|
||||
state["roundOutcome"] = outcome;
|
||||
})
|
||||
}
|
||||
|
||||
/// Read `<home>/sessions/<sid>/state.json` (a new object when it is missing or
|
||||
/// not one), let `change` edit it, and write it back through a temp file and a
|
||||
/// rename, so a reader never sees half a file.
|
||||
fn update_state(
|
||||
home: &std::path::Path,
|
||||
sid: &str,
|
||||
change: impl FnOnce(&mut serde_json::Value),
|
||||
) -> bool {
|
||||
let dir = home.join("sessions").join(sid);
|
||||
let file = dir.join("state.json");
|
||||
let mut state = std::fs::read_to_string(&file)
|
||||
@@ -73,7 +125,7 @@ fn write_report_to(home: &std::path::Path, sid: &str, json: &str) -> bool {
|
||||
.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;
|
||||
change(&mut state);
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let temp = dir.join("state.json.tmp");
|
||||
let Ok(text) = serde_json::to_string(&state) else {
|
||||
@@ -109,12 +161,45 @@ pub fn parse(line: &str) -> Command {
|
||||
return Command::Report(json.to_string());
|
||||
}
|
||||
}
|
||||
if let Some(rest) = line.strip_prefix("ROUND ") {
|
||||
let json = rest.trim();
|
||||
if serde_json::from_str::<serde_json::Value>(json).is_ok() {
|
||||
return Command::Round(json.to_string());
|
||||
}
|
||||
}
|
||||
if let Some(rest) = line.strip_prefix("FILE ") {
|
||||
let rest = rest.trim_start();
|
||||
if let Some((path, text)) = rest.split_once(char::is_whitespace) {
|
||||
return Command::File(path.to_string(), text.trim_start().to_string());
|
||||
}
|
||||
}
|
||||
if line == "HANG" {
|
||||
return Command::Hang;
|
||||
}
|
||||
Command::Echo(line.to_string())
|
||||
}
|
||||
|
||||
/// Write `text` to `relative` under the working directory. Refuses a path that
|
||||
/// is absolute or climbs out of it. `\n` in `text` becomes a line break.
|
||||
fn write_file(relative: &str, text: &str) -> bool {
|
||||
use std::path::{Component, Path};
|
||||
let path = Path::new(relative);
|
||||
let inside = !relative.is_empty()
|
||||
&& path
|
||||
.components()
|
||||
.all(|part| matches!(part, Component::Normal(_) | Component::CurDir));
|
||||
if !inside {
|
||||
return false;
|
||||
}
|
||||
if let Some(parent) = path
|
||||
.parent()
|
||||
.filter(|parent| !parent.as_os_str().is_empty())
|
||||
{
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
std::fs::write(path, text.replace("\\n", "\n")).is_ok()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
say("FAKE-AGENT ready");
|
||||
// What the app launched it with: the launch prompt is one of these, so a
|
||||
@@ -169,6 +254,20 @@ fn main() {
|
||||
say("REPORT unavailable")
|
||||
}
|
||||
}
|
||||
Command::Round(json) => {
|
||||
if write_round(&json) {
|
||||
say("ROUND written")
|
||||
} else {
|
||||
say("ROUND unavailable")
|
||||
}
|
||||
}
|
||||
Command::File(path, text) => {
|
||||
if write_file(&path, &text) {
|
||||
say(&format!("FILE written {path}"))
|
||||
} else {
|
||||
say(&format!("FILE refused {path}"))
|
||||
}
|
||||
}
|
||||
Command::Hang => {
|
||||
say("HANGING");
|
||||
loop {
|
||||
@@ -230,6 +329,87 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&home);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_round_and_file() {
|
||||
assert_eq!(
|
||||
parse("ROUND {\"round\":1}"),
|
||||
Command::Round("{\"round\":1}".into())
|
||||
);
|
||||
assert_eq!(parse("ROUND {nope"), Command::Echo("ROUND {nope".into()));
|
||||
assert_eq!(
|
||||
parse("FILE src/a.rs fixed it\\nline two"),
|
||||
Command::File("src/a.rs".into(), "fixed it\\nline two".into())
|
||||
);
|
||||
assert_eq!(
|
||||
parse("FILE onlypath"),
|
||||
Command::Echo("FILE onlypath".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_round_lands_with_its_report_doc_beside_what_was_there() {
|
||||
let home = std::env::temp_dir().join(format!("fake-agent-round-{}", std::process::id()));
|
||||
let dir = home.join("sessions").join("s5");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let read = || -> serde_json::Value {
|
||||
serde_json::from_str(&std::fs::read_to_string(dir.join("state.json")).unwrap()).unwrap()
|
||||
};
|
||||
// A report id adds the doc to a state that had none.
|
||||
assert!(write_round_to(
|
||||
&home,
|
||||
"s5",
|
||||
"{\"round\":1,\"role\":\"both\",\"report\":\"rep\"}"
|
||||
));
|
||||
let state = read();
|
||||
assert_eq!(state["roundOutcome"]["round"], 1);
|
||||
assert_eq!(state["docs"][0]["id"], "rep");
|
||||
assert_eq!(state["docs"][0]["kind"], "report");
|
||||
assert_eq!(state["docs"][0]["title"], "Review round");
|
||||
assert_eq!(state["schema"], 1);
|
||||
// Existing docs stay, and the new one is appended.
|
||||
std::fs::write(
|
||||
dir.join("state.json"),
|
||||
"{\"schema\":1,\"docs\":[{\"id\":\"phasefile\"}]}",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(write_round_to(
|
||||
&home,
|
||||
"s5",
|
||||
"{\"round\":2,\"report\":\"r2\"}"
|
||||
));
|
||||
let state = read();
|
||||
assert_eq!(state["docs"][0]["id"], "phasefile");
|
||||
assert_eq!(state["docs"][1]["id"], "r2");
|
||||
// No report id, no doc; a fixer's outcome adds none.
|
||||
std::fs::write(dir.join("state.json"), "{\"schema\":1}").unwrap();
|
||||
assert!(write_round_to(
|
||||
&home,
|
||||
"s5",
|
||||
"{\"round\":3,\"role\":\"fixer\"}"
|
||||
));
|
||||
let state = read();
|
||||
assert!(state.get("docs").is_none());
|
||||
assert_eq!(state["roundOutcome"]["role"], "fixer");
|
||||
// The loop's own report key is untouched by a round.
|
||||
assert!(state.get("loopOutcome").is_none());
|
||||
assert!(!write_round_to(&home, "s5", "not json"));
|
||||
let _ = std::fs::remove_dir_all(&home);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_command_writes_under_the_working_folder_only() {
|
||||
let base = format!("fake-agent-file-{}", std::process::id());
|
||||
assert!(write_file(&format!("{base}/sub/a.txt"), "one\\ntwo"));
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(format!("{base}/sub/a.txt")).unwrap(),
|
||||
"one\ntwo"
|
||||
);
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
assert!(!write_file("../escape.txt", "x"));
|
||||
assert!(!write_file("/abs.txt", "x"));
|
||||
assert!(!write_file("", "x"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_report_with_bad_json_is_an_echo() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user