Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

A builder never continues a session another builder saved (#471)

BFlat1e8f6616730e
diff --git a/crates/ai/src/rebuild/mod.rs b/crates/ai/src/rebuild/mod.rs
index ffdfed34..0baf28dd 100644
--- a/crates/ai/src/rebuild/mod.rs
+++ b/crates/ai/src/rebuild/mod.rs
@@ -618,6 +618,12 @@ impl Worker {
// A coding agent resumes its own session; our loop resumes its kept transcript.
let resumable = api.as_ref().is_none_or(crate::agent::ApiConversation::resumed);
let api_loop = api.is_some();
+ // A session another coding agent saved can't be continued by this one (#469).
+ if !api_loop && ws.manifest.agent_session_by.as_deref() != Some(entry.config.model.as_str()) {
+ ws.manifest.agent_session = ws.manifest.agent_session_for(&entry.config.model).map(str::to_string);
+ ws.manifest.agent_session_by = Some(entry.config.model.clone());
+ ws.save()?;
+ }
// A coding agent on this computer whose session the project keeps a copy of (#451).
let keeper = (!api_loop && !remote).then(|| opts.adapters.for_agent(&entry.config.model));
if let Some(a) = &keeper {
@@ -642,7 +648,7 @@ impl Worker {
let mut prompt = match &opts.message {
// An agent with a saved session gets the follow-up; one without (an API loop) starts
// from the brief with the request added.
- Some(m) if !first && resumable => follow_up(m, kind) + &now(),
+ Some(m) if !first && has_history => follow_up(m, kind) + &now(),
// Carrying on after a stop: the same, told to finish what it was doing.
None if carrying_on && !first && has_history => continue_prompt(kind) + &now(),
_ => {
diff --git a/crates/ai/src/rebuild/question.rs b/crates/ai/src/rebuild/question.rs
index 1acac89e..5ed8a182 100644
--- a/crates/ai/src/rebuild/question.rs
+++ b/crates/ai/src/rebuild/question.rs
@@ -84,7 +84,7 @@ pub fn ask(
};
let (mut conv, mut from): (Box<dyn Conversation>, AnsweredFrom) = match entry.config.kind {
BackendKind::AgentCli => {
- let session = ws.manifest.agent_session.clone();
+ let session = ws.manifest.agent_session_for(&entry.config.model).map(str::to_string);
if let Some(id) = session.as_deref() {
bring_back_session(ws, &entry, id);
}
diff --git a/crates/ai/src/rebuild/tests.rs b/crates/ai/src/rebuild/tests.rs
index 4f56a463..3348cdb2 100644
--- a/crates/ai/src/rebuild/tests.rs
+++ b/crates/ai/src/rebuild/tests.rs
@@ -968,6 +968,31 @@ mod picking_up_a_build {
assert!(!told.contains("Their request"), "not the first-build brief again: {told}");
}
+ /// #469: after the builder changed, the session the old one saved isn't handed to the new
+ /// one, which starts from the brief; the same agent still carries on.
+ #[test]
+ fn another_agents_session_is_not_continued() {
+ let b = bench();
+ let mut ws = worked_on(&b);
+ ws.manifest.agent_session_by = Some("claude-code".into());
+ ws.save().unwrap();
+ let (ok, feed) = try_it(&b, &ws);
+ assert!(ok, "{feed:?}");
+ let told = std::fs::read_to_string(&b.prompts).unwrap();
+ assert!(told.contains("Their request") && !told.contains("Pick up where you left off"), "{told}");
+ let m = Workspace::open(&ws.root).unwrap().manifest;
+ assert_eq!((m.agent_session, m.agent_session_by.as_deref()), (None, Some("stand-in")));
+
+ let b = bench();
+ let mut ws = worked_on(&b);
+ ws.manifest.agent_session_by = Some("stand-in".into());
+ ws.save().unwrap();
+ let (ok, feed) = try_it(&b, &ws);
+ assert!(ok, "{feed:?}");
+ assert!(std::fs::read_to_string(&b.prompts).unwrap().contains("Pick up where you left off"));
+ assert_eq!(Workspace::open(&ws.root).unwrap().manifest.agent_session.as_deref(), Some("session-1"));
+ }
+
#[test]
fn a_new_project_still_starts_from_the_top() {
let b = bench();
diff --git a/crates/engine/src/workspace.rs b/crates/engine/src/workspace.rs
index 0c56cc74..d6efb482 100644
--- a/crates/engine/src/workspace.rs
+++ b/crates/engine/src/workspace.rs
@@ -378,6 +378,10 @@ pub struct Manifest {
/// The coding agent's session, so a later request continues the same conversation.
#[serde(default)]
pub agent_session: Option<String>,
+ /// The coding agent that saved `agent_session` (`claude-code`, `codex`…); none: saved before
+ /// this was kept.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub agent_session_by: Option<String>,
/// Kinds of agent action the person allowed for this workspace ("bash:npm", "web_fetch"…).
#[serde(default)]
pub allowed_actions: Vec<String>,
@@ -428,6 +432,7 @@ impl Manifest {
updated_ms: now,
checks: Vec::new(),
agent_session: None,
+ agent_session_by: None,
allowed_actions: Vec::new(),
last_reply: String::new(),
asset_budget_usd: ASSET_BUDGET_USD,
@@ -440,6 +445,12 @@ impl Manifest {
}
}
+ /// The saved session, if `agent` (`claude-code`, `codex`…) saved it: another agent can't
+ /// continue it (#469). One saved before the agent was kept counts.
+ pub fn agent_session_for(&self, agent: &str) -> Option<&str> {
+ self.agent_session.as_deref().filter(|_| self.agent_session_by.as_deref().is_none_or(|a| a == agent))
+ }
+
/// Take the fields the person owns from `other` (what's on disk).
fn take_person_fields(&mut self, other: &Manifest) {
self.asset_budget_usd = other.asset_budget_usd;
@@ -1387,6 +1398,20 @@ mod tests {
}
}
+ /// #469: a saved session is only for the agent that saved it.
+ #[test]
+ fn a_session_is_only_for_the_agent_that_saved_it() {
+ let mut m = Manifest::new("x", "A thing", TargetRef::None);
+ assert_eq!(m.agent_session_for("codex"), None);
+ m.agent_session = Some("s1".into());
+ assert_eq!(m.agent_session_for("codex"), Some("s1"), "one saved before the agent was kept counts");
+ m.agent_session_by = Some("claude-code".into());
+ assert_eq!(m.agent_session_for("claude-code"), Some("s1"));
+ assert_eq!(m.agent_session_for("codex"), None);
+ let text = serde_json::to_string(&m).unwrap();
+ assert_eq!(serde_json::from_str::<Manifest>(&text).unwrap().agent_session_by.as_deref(), Some("claude-code"));
+ }
+
/// A rename survives a stale save, a copy takes the build but not the conversation (nor its
/// kept session), and a delete leaves the person's own files.
#[test]
diff --git a/crates/ui-egui/src/demo.rs b/crates/ui-egui/src/demo.rs
index 07f70c16..2a1e0f25 100644
--- a/crates/ui-egui/src/demo.rs
+++ b/crates/ui-egui/src/demo.rs
@@ -229,6 +229,7 @@ fn workspace(slug: &str, name: &str, request: &str, target: TargetRef, status: S
updated_ms: at,
checks: Vec::new(),
agent_session: None,
+ agent_session_by: None,
allowed_actions: Vec::new(),
last_reply: String::new(),
asset_budget_usd: craft_anything_engine::workspace::ASSET_BUDGET_USD,
diff --git a/docs/schemas/craft.schema.json b/docs/schemas/craft.schema.json
index 63a74a57..40c26670 100644
--- a/docs/schemas/craft.schema.json
+++ b/docs/schemas/craft.schema.json
@@ -12,6 +12,13 @@
],
"default": null
},
+ "agent_session_by": {
+ "description": "The coding agent that saved `agent_session` (`claude-code`, `codex`…); none: saved before\nthis was kept.",
+ "type": [
+ "string",
+ "null"
+ ]
+ },
"allowed_actions": {
"description": "Kinds of agent action the person allowed for this workspace (\"bash:npm\", \"web_fetch\"…).",
"type": "array",