Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(workstream): preserve OpenCode tool failure results
Samir Hanna Verza7ef9358181ec
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e65cba04..3c3ab11e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,9 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed+- Kept OpenCode V1 failed tool results in the managed workstream ledger,+ including their visible error text and error status, instead of importing+ only the call and silently dropping the failure reason (#PR).- Refused empty and whitespace-only `--workspace` and `--project` flags at theshared CLI scope-resolution choke point (`resolve_scope`,`resolve_scope_noting_marker_project`, `resolve_scope_with_marker`, anddiff --git a/crates/ai-memory-workstream/src/transcript.rs b/crates/ai-memory-workstream/src/transcript.rsindex 822dd481..2e03827b 100644--- a/crates/ai-memory-workstream/src/transcript.rs+++ b/crates/ai-memory-workstream/src/transcript.rs@@ -3494,11 +3494,19 @@ fn parse_opencode(None,json!({"tool": name}),);- if let Some(output) = state- .get("output")- .map(value_text)- .filter(|value| !value.is_empty())- {+ let status = state.get("status").and_then(Value::as_str);+ // V1 terminal failures store their visible result in `error`, not+ // `output`; only that state's string field is conversation text.+ // https://github.com/anomalyco/opencode/blob/c63a93995f1e5c28dd73cc032dcb224780f208ff/packages/schema/src/v1/session.ts#L277-L302+ let output = if status == Some("error") {+ state+ .get("error")+ .and_then(Value::as_str)+ .map(str::to_owned)+ } else {+ state.get("output").map(value_text)+ };+ if let Some(output) = output.filter(|value| !value.is_empty()) {push_event(events,AgentKind::OpenCode,@@ -3509,7 +3517,11 @@ fn parse_opencode(Some("tool"),&output,None,- json!({"status": state.get("status").and_then(Value::as_str)}),+ if status == Some("error") {+ json!({"status": status, "is_error": true})+ } else {+ json!({"status": status})+ },);}}@@ -8668,6 +8680,105 @@ mod tests {assert_eq!(second.events[0].content, "second");}+ #[test]+ fn opencode_failed_tool_results_keep_the_visible_error() {+ let home = tempfile::tempdir().unwrap();+ let db = opencode_db(home.path(), None);+ fs::create_dir_all(db.parent().unwrap()).unwrap();+ let connection = Connection::open(&db).unwrap();+ connection+ .execute_batch(+ "CREATE TABLE message(id TEXT PRIMARY KEY, session_id TEXT, data TEXT);\n\+ CREATE TABLE part(id TEXT PRIMARY KEY, message_id TEXT, session_id TEXT, \+ time_updated INTEGER, data TEXT);",+ )+ .unwrap();+ connection+ .execute(+ "INSERT INTO message VALUES ('m1', 's1', ?1)",+ [json!({"role":"assistant"}).to_string()],+ )+ .unwrap();+ // OpenCode's terminal error state has `error`, whereas completed+ // tools have `output`. Other fields are not visible error text.+ let states = [+ (+ "completed",+ json!({"status":"completed", "output":"success", "error":"not a completed result"}),+ ),+ (+ "failed",+ json!({"status":"error", "error":"Permission denied"}),+ ),+ (+ "pending",+ json!({"status":"pending", "error":"not a terminal result"}),+ ),+ (+ "running",+ json!({"status":"running", "error":"not a terminal result"}),+ ),+ (+ "malformed",+ json!({"status":"error", "error":{"private":"not visible text"}}),+ ),+ ("legacy", json!({"output":"legacy output"})),+ ];+ for (id, mut state) in states {+ state["input"] = json!({"command":"test-command"});+ connection+ .execute(+ "INSERT INTO part VALUES (?1, 'm1', 's1', 1, ?2)",+ params![+ id,+ json!({"type":"tool", "tool":"bash", "state":state}).to_string()+ ],+ )+ .unwrap();+ }+ let before = fs::read(&db).unwrap();+ let transcript = export_opencode(home.path(), None, "s1", None).unwrap();+ let results: Vec<_> = transcript+ .events+ .iter()+ .filter(|event| event.kind == WorkstreamEventKind::ToolResult)+ .collect();+ assert_eq!(+ results+ .iter()+ .map(|event| event.content.as_str())+ .collect::<Vec<_>>(),+ ["success", "Permission denied", "legacy output"]+ );+ let failed = results[1];+ assert_eq!(failed.role.as_deref(), Some("tool"));+ assert_eq!(failed.source_record_id.as_deref(), Some("failed"));+ assert_eq!(failed.metadata, json!({"status":"error", "is_error":true}));+ assert_eq!(results[0].metadata, json!({"status":"completed"}));+ assert_eq!(results[2].metadata, json!({"status":null}));+ assert!(transcript.losses.is_empty());+ let replay = export_opencode(home.path(), None, "s1", None).unwrap();+ assert_eq!(+ transcript+ .events+ .iter()+ .map(|event| &event.event_id)+ .collect::<Vec<_>>(),+ replay+ .events+ .iter()+ .map(|event| &event.event_id)+ .collect::<Vec<_>>()+ );+ assert!(+ export_opencode(home.path(), None, "s1", transcript.source_cursor.as_deref())+ .unwrap()+ .events+ .is_empty()+ );+ assert_eq!(fs::read(&db).unwrap(), before);+ }+/// Beta store fixture: `session_v2` + `session_message` with the shapes/// observed on beta-18999 (user text, assistant text/tool/reasoning).fn opencode2_fixture(home: &Path) {diff --git a/docs/managed-workstreams.md b/docs/managed-workstreams.mdindex f8224ee7..7842a6f9 100644--- a/docs/managed-workstreams.md+++ b/docs/managed-workstreams.md@@ -562,6 +562,10 @@ another shell, pass `--workstream-id <uuid>` explicitly. Search results preservethe source harness, role, event sequence, and content. Historical tool activityis labelled completed evidence and must never be replayed as a pending call.+OpenCode V1 tool failures contribute their visible `state.error` text as a+result, with `status: "error"` and `is_error: true` in the ledger metadata.+Successful calls keep their ordinary `state.output` result.+Search and tail also return optional `source_record_id` and `metadata` fields.The text CLI prints the source record and correlation metadata as escaped JSON;`--json` preserves these fields for consumers. Older server responses without