mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 04:45:33 -08:00
Hold loop reports to the app's criteria at post time and keep phase prompts out of New Session
AI Assisted
This commit is contained in:
1 parent
33f1783979
commit
61f27f81ab
7 files changed
+497
-35
No files matched your search
@@ -116,6 +116,10 @@ pub fn parse(line: &str) -> Command {
|
||||
|
||||
fn main() {
|
||||
say("FAKE-AGENT ready");
|
||||
// What the app launched it with: the launch prompt is one of these, so a
|
||||
// test can read back which prompt a generation was started on.
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
say(&format!("ARGS {}", args.join(" ")));
|
||||
if let Some(size) = size_text() {
|
||||
say(&size);
|
||||
}
|
||||
|
||||
@@ -95,7 +95,8 @@ fn append_replay(replay: &mut Vec<u8>, bytes: &[u8], limit: usize) {
|
||||
struct Inner {
|
||||
state: AgentState,
|
||||
running: Option<Running>,
|
||||
/// What a Restart starts again: the choice and the brief.
|
||||
/// What a Restart starts again: the choice and its brief (the prompt a
|
||||
/// restart runs again, never a transient first prompt).
|
||||
last_start: Option<(StartChoice, Option<String>)>,
|
||||
size: (u16, u16),
|
||||
brief_note: Option<String>,
|
||||
@@ -194,12 +195,10 @@ impl AgentManager {
|
||||
mode: PermissionMode,
|
||||
brief: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let mut inner = lock(&self.shared.inner);
|
||||
if inner.state == AgentState::Running {
|
||||
return Err("an agent is already running for this project".to_string());
|
||||
}
|
||||
inner.brief_note = None;
|
||||
let previous = inner.last_start.replace((
|
||||
let prompt = brief
|
||||
.clone()
|
||||
.unwrap_or_else(|| kind.default_prompt().to_string());
|
||||
self.begin(
|
||||
StartChoice {
|
||||
kind,
|
||||
model,
|
||||
@@ -207,8 +206,50 @@ impl AgentManager {
|
||||
mode,
|
||||
},
|
||||
brief,
|
||||
));
|
||||
match self.shared.spawn(&mut inner) {
|
||||
prompt,
|
||||
)
|
||||
}
|
||||
|
||||
/// Like `start`, but `prompt` is this launch's first prompt alone: a later
|
||||
/// Restart still opens the placeholder brief (the dashboard), it does not
|
||||
/// run `prompt` again. The loop's own agent starts take this path, so a
|
||||
/// phase prompt never becomes the brief a New Session repeats.
|
||||
pub fn start_transient(
|
||||
&self,
|
||||
kind: Agent,
|
||||
model: Option<String>,
|
||||
effort: Option<String>,
|
||||
mode: PermissionMode,
|
||||
prompt: String,
|
||||
) -> Result<(), String> {
|
||||
self.begin(
|
||||
StartChoice {
|
||||
kind,
|
||||
model,
|
||||
effort,
|
||||
mode,
|
||||
},
|
||||
None,
|
||||
prompt,
|
||||
)
|
||||
}
|
||||
|
||||
/// `start` and `start_transient` behind one door: remember `choice` with
|
||||
/// `restart_brief` (what a later Restart runs again), then spawn the agent
|
||||
/// on `first_prompt`, which is not always the same string.
|
||||
fn begin(
|
||||
&self,
|
||||
choice: StartChoice,
|
||||
restart_brief: Option<String>,
|
||||
first_prompt: String,
|
||||
) -> Result<(), String> {
|
||||
let mut inner = lock(&self.shared.inner);
|
||||
if inner.state == AgentState::Running {
|
||||
return Err("an agent is already running for this project".to_string());
|
||||
}
|
||||
inner.brief_note = None;
|
||||
let previous = inner.last_start.replace((choice, restart_brief));
|
||||
match self.shared.spawn(&mut inner, first_prompt) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) => {
|
||||
// A start that never ran must not take away what Restart would
|
||||
@@ -225,14 +266,15 @@ impl AgentManager {
|
||||
/// Restart's brief note; the app still forgets the console session, so
|
||||
/// the new agent's `open` makes a new one. An agent that ended, or never
|
||||
/// ran, starts directly. Needs a launch choice from an earlier start.
|
||||
/// `prompt` belongs to this launch alone: the stored brief stays what it
|
||||
/// was, so a later New Session still opens the dashboard, not this
|
||||
/// prompt again.
|
||||
pub fn start_fresh(&self, prompt: String) -> Result<(), String> {
|
||||
let mut inner = lock(&self.shared.inner);
|
||||
let (choice, _) = inner
|
||||
.last_start
|
||||
.clone()
|
||||
.ok_or("no agent has been started")?;
|
||||
if inner.last_start.is_none() {
|
||||
return Err("no agent has been started".to_string());
|
||||
}
|
||||
inner.brief_note = None;
|
||||
inner.last_start = Some((choice, Some(prompt)));
|
||||
self.shared.forget();
|
||||
if inner.state == AgentState::Running {
|
||||
self.shared.generation.fetch_add(1, Ordering::SeqCst);
|
||||
@@ -240,7 +282,7 @@ impl AgentManager {
|
||||
let _ = old.host.kill();
|
||||
}
|
||||
}
|
||||
match self.shared.spawn(&mut inner) {
|
||||
match self.shared.spawn(&mut inner, prompt) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) => {
|
||||
inner.state = AgentState::Ended {
|
||||
@@ -381,13 +423,15 @@ impl Shared {
|
||||
(self.sink)(AgentEvent::State(self.snapshot(inner)));
|
||||
}
|
||||
|
||||
/// Start the agent in `inner.last_start` and watch for it to end.
|
||||
fn spawn(self: &Arc<Self>, inner: &mut Inner) -> Result<(), String> {
|
||||
let (choice, brief) = inner
|
||||
/// Start the agent in `inner.last_start` on `prompt` and watch for it to
|
||||
/// end. `prompt` is this launch's first prompt: the stored brief for a
|
||||
/// start or a Restart, or a transient one (`start_fresh`,
|
||||
/// `start_transient`) that a Restart must not repeat.
|
||||
fn spawn(self: &Arc<Self>, inner: &mut Inner, prompt: String) -> Result<(), String> {
|
||||
let (choice, _) = inner
|
||||
.last_start
|
||||
.clone()
|
||||
.ok_or("no agent has been started")?;
|
||||
let prompt = brief.unwrap_or_else(|| choice.kind.default_prompt().to_string());
|
||||
let mut spec = agent_launch_spec(
|
||||
&choice.kind,
|
||||
&self.folder,
|
||||
@@ -467,7 +511,16 @@ impl Shared {
|
||||
}
|
||||
let hook = self.before_fresh.lock().ok().and_then(|slot| slot.clone());
|
||||
inner.brief_note = hook.and_then(|run| run());
|
||||
match self.spawn(inner) {
|
||||
let prompt = inner
|
||||
.last_start
|
||||
.as_ref()
|
||||
.map(|(choice, brief)| {
|
||||
brief
|
||||
.clone()
|
||||
.unwrap_or_else(|| choice.kind.default_prompt().to_string())
|
||||
})
|
||||
.ok_or("no agent has been started")?;
|
||||
match self.spawn(inner, prompt) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) => {
|
||||
inner.state = AgentState::Ended {
|
||||
|
||||
@@ -573,6 +573,23 @@ fn suggestion_warning(review: &ReviewPart) -> Option<String> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The `loopExpect` patch: the setup's judging criteria, stamped into the
|
||||
/// phase's session so the console refuses a report that misses one at post
|
||||
/// time, while the agent can still fix it. `fix` is "smart" or the level
|
||||
/// list; an empty list means findings are reported, not fixed.
|
||||
fn expect_patch(setup: &LoopSetup) -> Value {
|
||||
json!({
|
||||
"loopExpect": {
|
||||
"review": setup.review,
|
||||
"fix": match setup.fix_mode {
|
||||
FixMode::Smart => json!("smart"),
|
||||
FixMode::Levels => json!(setup.fix.iter().map(|l| l.word()).collect::<Vec<_>>()),
|
||||
},
|
||||
"guess": setup.on_question == OnQuestion::Guess,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether to take the skill's report at its word, cross-checked against the
|
||||
/// spec's own marks (`phase_status` is the phase's entry from `specs.mjs
|
||||
/// status`). The quick check runs after an Approve, not here.
|
||||
@@ -1310,14 +1327,16 @@ pub fn note_page(slot: &LoopSlot, workflow: &str, sid: &str) {
|
||||
}
|
||||
|
||||
/// Start a fresh agent on `prompt`: on the agent's stored launch choice when it
|
||||
/// has one, else on `launch` (an agent the loop stopped has forgotten it).
|
||||
/// has one, else on `launch` (an agent the loop stopped has forgotten it). The
|
||||
/// prompt is transient either way: a later New Session opens the dashboard, it
|
||||
/// does not run the phase's (or Fix it's or Finalize's) prompt again.
|
||||
pub fn start_with(agent: &AgentManager, launch: &Launch, prompt: String) -> Result<(), String> {
|
||||
if agent.launch_choice().is_some() {
|
||||
agent.start_fresh(prompt)
|
||||
} else {
|
||||
agent.forget_session();
|
||||
let (kind, model, effort, mode) = launch.clone();
|
||||
agent.start(kind, model, effort, mode, Some(prompt))
|
||||
agent.start_transient(kind, model, effort, mode, prompt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1504,6 +1523,7 @@ fn tick(ctx: &Ctx) -> bool {
|
||||
if state["loopOutcome"].is_object() {
|
||||
return handle_outcome(ctx, &snap, &state["loopOutcome"]);
|
||||
}
|
||||
ensure_expect(ctx, &snap, &sid, state);
|
||||
if let Some(go) = crash_or_outcome(ctx, &snap, &sid) {
|
||||
return go;
|
||||
}
|
||||
@@ -1544,6 +1564,32 @@ fn crash_or_outcome(ctx: &Ctx, snap: &Snapshot, sid: &str) -> Option<bool> {
|
||||
Some(fail(ctx, snap, StopCause::Crash, reason))
|
||||
}
|
||||
|
||||
/// Whether `e` is a whole `loopExpect` stamp: the three fields the console
|
||||
/// enforces, each the right kind. Anything less counts as no stamp, so a
|
||||
/// corrupt or partial one is replaced rather than left to fail every post.
|
||||
fn valid_expect(e: &Value) -> bool {
|
||||
let fix_ok = e["fix"].as_str() == Some("smart")
|
||||
|| e["fix"].as_array().is_some_and(|a| {
|
||||
a.iter()
|
||||
.all(|l| ["critical", "warning", "suggestion"].contains(&l.as_str().unwrap_or("")))
|
||||
});
|
||||
e.is_object() && e["review"].is_boolean() && e["guess"].is_boolean() && fix_ok
|
||||
}
|
||||
|
||||
/// The judging criteria (`loopExpect`), posted into the session so the
|
||||
/// console refuses a report that misses one at post time, while the agent can
|
||||
/// still fix and repost, instead of the phase stopping on the verdict later.
|
||||
/// It lands only once the agent has locked the session with `open --loop`,
|
||||
/// and is reposted on every look while the stored stamp is missing or not the
|
||||
/// full shape (a fresh session, an unlock that dropped it, a corrupt stamp).
|
||||
/// A post that does not land only weakens this early check and is retried.
|
||||
fn ensure_expect(ctx: &Ctx, snap: &Snapshot, sid: &str, state: &Value) {
|
||||
if !state["loop"]["driving"].as_bool().unwrap_or(false) || valid_expect(&state["loopExpect"]) {
|
||||
return;
|
||||
}
|
||||
let _ = loop_tools::console_post(&ctx.console_dir, sid, &expect_patch(&snap.setup));
|
||||
}
|
||||
|
||||
/// Count (or clear) the looks that could not read the session.
|
||||
fn note_read_failure(ctx: &Ctx, failed: bool) {
|
||||
let mut guard = lock(&ctx.slot);
|
||||
@@ -2955,6 +3001,45 @@ mod tests {
|
||||
assert_eq!(LoopSetup::default().on_question, OnQuestion::Guess);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_expect_patch_carries_the_setup() {
|
||||
assert_eq!(
|
||||
expect_patch(&setup()),
|
||||
json!({ "loopExpect": { "review": true, "fix": ["critical", "warning", "suggestion"], "guess": false } })
|
||||
);
|
||||
let mut smart = setup();
|
||||
smart.fix_mode = FixMode::Smart;
|
||||
smart.on_question = OnQuestion::Guess;
|
||||
assert_eq!(expect_patch(&smart)["loopExpect"]["fix"], json!("smart"));
|
||||
assert_eq!(expect_patch(&smart)["loopExpect"]["guess"], json!(true));
|
||||
let mut no_review = setup();
|
||||
no_review.review = false;
|
||||
no_review.fix = vec![FixLevel::Critical];
|
||||
assert_eq!(
|
||||
expect_patch(&no_review)["loopExpect"],
|
||||
json!({ "review": false, "fix": ["critical"], "guess": false })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stamp_counts_only_when_whole() {
|
||||
assert!(valid_expect(
|
||||
&json!({ "review": true, "fix": "smart", "guess": false })
|
||||
));
|
||||
assert!(valid_expect(
|
||||
&json!({ "review": true, "fix": ["critical"], "guess": true })
|
||||
));
|
||||
for bad in [
|
||||
json!(5),
|
||||
json!({ "review": "yes", "fix": "smart", "guess": false }),
|
||||
json!({ "review": true, "guess": false }),
|
||||
json!({ "review": true, "fix": ["major"], "guess": false }),
|
||||
json!({ "review": true, "fix": "everything", "guess": false }),
|
||||
] {
|
||||
assert!(!valid_expect(&bad), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
fn phase(n: u32) -> PhaseRecord {
|
||||
PhaseRecord {
|
||||
n,
|
||||
@@ -3933,7 +4018,7 @@ second line"
|
||||
console_dir: PathBuf::from("."),
|
||||
home: report_home,
|
||||
launch: (
|
||||
Agent::Builtin(crate::agents::AgentKind::Claude),
|
||||
Agent::Builtin(crate::agents::builtin_entry("claude").unwrap()),
|
||||
None,
|
||||
None,
|
||||
PermissionMode::Bypass,
|
||||
@@ -5874,4 +5959,88 @@ second line"
|
||||
"/plan2code-4-finalize specs/x/overview.md --web"
|
||||
);
|
||||
}
|
||||
|
||||
/// The `ARGS` lines the fake agent has printed, one per generation, in the
|
||||
/// order they started: the prompts each launch ran with.
|
||||
fn launched_prompts(seen: &Arc<Mutex<String>>) -> Vec<String> {
|
||||
lock(seen)
|
||||
.lines()
|
||||
.filter_map(|line| {
|
||||
line.trim()
|
||||
.strip_prefix("ARGS")
|
||||
.map(|rest| rest.trim().to_string())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A plain folder to run the fake agent in: no git or specs needed, the
|
||||
/// manager only spawns the program there.
|
||||
fn agent_folder(name: &str) -> PathBuf {
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("plan2code-agent-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
fn wait_for_generations(seen: &Arc<Mutex<String>>, count: usize) {
|
||||
wait_until("the agent generations", || {
|
||||
launched_prompts(seen).len() >= count
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_session_opens_the_dashboard_not_the_phase_prompt_again() {
|
||||
let folder = agent_folder("restart");
|
||||
let (agent, seen) = fake_agent_manager(&folder);
|
||||
// The loop's cold start, then the usual fresh start: both transient.
|
||||
start_with(&agent, &fake_agent_launch(), "phase one prompt".into()).unwrap();
|
||||
wait_for_generations(&seen, 1);
|
||||
start_with(&agent, &fake_agent_launch(), "phase two prompt".into()).unwrap();
|
||||
wait_for_generations(&seen, 2);
|
||||
// New Session starts on the placeholder brief, not the last prompt.
|
||||
agent.restart().unwrap();
|
||||
wait_for_generations(&seen, 3);
|
||||
assert_eq!(
|
||||
launched_prompts(&seen),
|
||||
vec![
|
||||
"phase one prompt".to_string(),
|
||||
"phase two prompt".to_string(),
|
||||
String::new()
|
||||
],
|
||||
"a transient prompt must never become what Restart starts again"
|
||||
);
|
||||
agent.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_real_brief_survives_a_transient_prompt() {
|
||||
let folder = agent_folder("brief");
|
||||
let (agent, seen) = fake_agent_manager(&folder);
|
||||
let (kind, model, effort, mode) = fake_agent_launch();
|
||||
agent
|
||||
.start(
|
||||
kind,
|
||||
model,
|
||||
effort,
|
||||
mode,
|
||||
Some("the original brief".to_string()),
|
||||
)
|
||||
.unwrap();
|
||||
wait_for_generations(&seen, 1);
|
||||
agent.start_fresh("a phase prompt".into()).unwrap();
|
||||
wait_for_generations(&seen, 2);
|
||||
agent.restart().unwrap();
|
||||
wait_for_generations(&seen, 3);
|
||||
assert_eq!(
|
||||
launched_prompts(&seen),
|
||||
vec![
|
||||
"the original brief".to_string(),
|
||||
"a phase prompt".to_string(),
|
||||
"the original brief".to_string()
|
||||
],
|
||||
"Restart starts the stored brief again"
|
||||
);
|
||||
agent.stop();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user