Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(agents): extract snapshot-tested Claude and Codex transcript folds (#7362)

Eric Hayes2ec69694490d
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 753cf4de34..f0148c3e1f 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -2765,6 +2765,16 @@
"crates/remote_env_var",
"crates/workspace-hack"
],
+ "claude_fold": [
+ "crates/agent_runtime_protocol",
+ "crates/cowlike",
+ "crates/folds/agent_fold",
+ "crates/folds/claude_fold",
+ "crates/macro_user_id",
+ "crates/macro_uuid",
+ "crates/non_empty",
+ "crates/workspace-hack"
+ ],
"codex_cloud_agents": [
"crates/activity",
"crates/agent",
@@ -2964,6 +2974,16 @@
"crates/unfurl",
"crates/workspace-hack"
],
+ "codex_fold": [
+ "crates/agent_runtime_protocol",
+ "crates/cowlike",
+ "crates/folds/agent_fold",
+ "crates/folds/codex_fold",
+ "crates/macro_user_id",
+ "crates/macro_uuid",
+ "crates/non_empty",
+ "crates/workspace-hack"
+ ],
"coding_agent_worker": [
"crates/activity",
"crates/agent",
@@ -3009,6 +3029,8 @@
"crates/fig_engine",
"crates/filter_ast",
"crates/folds/agent_fold",
+ "crates/folds/claude_fold",
+ "crates/folds/codex_fold",
"crates/foreign_entity",
"crates/frecency",
"crates/genai_telemetry",
diff --git a/Cargo.lock b/Cargo.lock
index ebb9b4deb9..f6d4d8594f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4119,6 +4119,16 @@ dependencies = [
"zeroize",
]
+[[package]]
+name = "claude_fold"
+version = "0.1.0"
+dependencies = [
+ "agent_fold",
+ "insta",
+ "serde_json",
+ "workspace-hack",
+]
+
[[package]]
name = "clipboard-win"
version = "5.4.1"
@@ -4213,11 +4223,22 @@ dependencies = [
"zeroize",
]
+[[package]]
+name = "codex_fold"
+version = "0.1.0"
+dependencies = [
+ "agent_fold",
+ "insta",
+ "serde_json",
+ "workspace-hack",
+]
+
[[package]]
name = "coding_agent_worker"
version = "0.1.0"
dependencies = [
"agent-client-protocol",
+ "agent_fold",
"agent_runtime_protocol",
"agent_session",
"agent_trigger",
@@ -4227,6 +4248,8 @@ dependencies = [
"channels",
"chrono",
"clap",
+ "claude_fold",
+ "codex_fold",
"crossterm 0.28.1",
"dashmap",
"eventsource-stream",
diff --git a/Cargo.toml b/Cargo.toml
index 6b406f36b3..e5f24f95cf 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,7 +4,7 @@ resolver = "2"
members = [
"crates/routines",
"crates/activity",
- "crates/agent", "crates/agent_changes", "crates/agent_egress", "crates/folds/agent_fold",
+ "crates/agent", "crates/agent_changes", "crates/agent_egress", "crates/folds/agent_fold", "crates/folds/claude_fold", "crates/folds/codex_fold",
"crates/agent_harness",
"crates/agent_preview",
"crates/agent_inmem",
diff --git a/crates/coding_agent_worker/Cargo.toml b/crates/coding_agent_worker/Cargo.toml
index d4bfa31499..93fb29e5cf 100644
--- a/crates/coding_agent_worker/Cargo.toml
+++ b/crates/coding_agent_worker/Cargo.toml
@@ -9,6 +9,9 @@ name = "macrod"
path = "src/main.rs"
[dependencies]
+agent_fold = { path = "../folds/agent_fold", default-features = false }
+claude_fold = { path = "../folds/claude_fold" }
+codex_fold = { path = "../folds/codex_fold" }
messages = { path = "../messages", features = ["broker"] }
# Direct crate imports are the whole typegen story: the protocol envelope,
# the trigger event vocabulary, and the delivery signature all come from the
diff --git a/crates/coding_agent_worker/src/herdr.rs b/crates/coding_agent_worker/src/herdr.rs
index 5f4b6a32b9..4c95bd8012 100644
--- a/crates/coding_agent_worker/src/herdr.rs
+++ b/crates/coding_agent_worker/src/herdr.rs
@@ -16,9 +16,7 @@
//! [`acp_agent`]).
pub(crate) mod acp_agent;
-mod claude_log;
mod cli;
-mod codex_log;
mod hub;
mod tap;
pub(crate) mod wire;
diff --git a/crates/coding_agent_worker/src/herdr/acp_agent.rs b/crates/coding_agent_worker/src/herdr/acp_agent.rs
index 8c08aee129..737463d5eb 100644
--- a/crates/coding_agent_worker/src/herdr/acp_agent.rs
+++ b/crates/coding_agent_worker/src/herdr/acp_agent.rs
@@ -30,10 +30,11 @@ use tokio::sync::{mpsc, oneshot};
use tokio_util::sync::CancellationToken;
use super::HerdrSession;
-use super::claude_log::{ClaudeLog, LogEvent};
use super::cli::{HerdrCli, HerdrError};
-use super::codex_log::CodexLog;
use super::hub::title_from;
+use agent_fold::domain::transcript::{Fold, LogEvent};
+use claude_fold::ClaudeLog;
+use codex_fold::CodexLog;
/// The subcommand macrod's harness config names to run this adapter.
pub(crate) const SUBCOMMAND: &str = "herdr-acp";
diff --git a/crates/folds/README.md b/crates/folds/README.md
index 535783187d..b600ca552f 100644
--- a/crates/folds/README.md
+++ b/crates/folds/README.md
@@ -4,7 +4,13 @@ This directory contains the pure, deterministic folds used to turn agent
transcripts into messages for Macro. Keep filesystem tailing, process control,
transport, credentials, and session ownership in their adapters.
+- `claude_fold`: Claude Code JSONL to ACP updates and turn facts.
+- `codex_fold`: Codex rollout JSONL to ACP updates and turn facts.
- `agent_fold`: ACP protocol frames to Macro messages and lifecycle events.
Package names remain stable; only their workspace paths live under `folds/`.
Run each package's tests from the repository root.
+
+Native folds implement `agent_fold::domain::transcript::Fold`. Their fixtures are
+synthetic transcripts with pinned output snapshots. Runtime adapters consume the
+same events; there is no separate parsing implementation in macrod.
diff --git a/crates/folds/agent_fold/src/domain/mod.rs b/crates/folds/agent_fold/src/domain/mod.rs
index 91f3d96458..86ba024488 100644
--- a/crates/folds/agent_fold/src/domain/mod.rs
+++ b/crates/folds/agent_fold/src/domain/mod.rs
@@ -28,3 +28,6 @@ pub mod speculation;
#[cfg(test)]
mod test;
+
+/// Native transcript normalization contract.
+pub mod transcript;
diff --git a/crates/folds/agent_fold/src/domain/transcript.rs b/crates/folds/agent_fold/src/domain/transcript.rs
new file mode 100644
index 0000000000..591a1decd4
--- /dev/null
+++ b/crates/folds/agent_fold/src/domain/transcript.rs
@@ -0,0 +1,32 @@
+//! Pure native-transcript normalization. File framing and session ownership
+//! belong to the caller; each fold consumes complete JSON records in order.
+use serde_json::Value;
+
+/// Normalize one native transcript record into ACP updates and turn facts.
+/// Unknown records produce no events. Keep one instance per native session.
+pub trait Fold {
+ /// Consume one complete JSONL record, retaining provider deduplication state.
+ fn entry(&mut self, line: &str) -> Vec<LogEvent>;
+}
+
+/// Longest tool output forwarded to Macro, in characters.
+const TOOL_OUTPUT_LIMIT: usize = 4_000;
+
+/// What one transcript line means for the ACP session.
+#[derive(Debug, Clone, PartialEq, serde::Serialize)]
+pub enum LogEvent {
+ /// A `session/update` payload to send.
+ Update(Value),
+ /// The turn is over, with this ACP stop reason.
+ TurnEnded(&'static str),
+}
+
+/// Bound tool output without splitting UTF-8 characters.
+pub fn truncate(text: &str) -> String {
+ if text.chars().count() <= TOOL_OUTPUT_LIMIT {
+ return text.to_owned();
+ }
+ let mut kept: String = text.chars().take(TOOL_OUTPUT_LIMIT).collect();
+ kept.push_str("\n… [truncated]");
+ kept
+}
diff --git a/crates/folds/claude_fold/Cargo.toml b/crates/folds/claude_fold/Cargo.toml
new file mode 100644
index 0000000000..206240b7fb
--- /dev/null
+++ b/crates/folds/claude_fold/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "claude_fold"
+version = "0.1.0"
+edition = "2024"
+publish = false
+
+[dependencies]
+agent_fold = { path = "../agent_fold", default-features = false }
+serde_json = { workspace = true }
+workspace-hack = { version = "0.1", path = "../../workspace-hack" }
+
+[dev-dependencies]
+agent_fold = { path = "../agent_fold", default-features = false, features = ["test-utils"] }
+insta = { version = "1.48.0", features = ["json"] }
diff --git a/crates/folds/claude_fold/fixtures/conversation.jsonl b/crates/folds/claude_fold/fixtures/conversation.jsonl
new file mode 100644
index 0000000000..c83a700492
--- /dev/null
+++ b/crates/folds/claude_fold/fixtures/conversation.jsonl
@@ -0,0 +1,9 @@
+{"type":"user","uuid":"u1","message":{"content":"Read the file."}}
+{"type":"assistant","uuid":"a1","message":{"content":[{"type":"thinking","thinking":"I will inspect it."},{"type":"tool_use","id":"t1","name":"Read","input":{"file_path":"/repo/a.txt"}}],"stop_reason":"tool_use"}}
+{"type":"user","uuid":"u2","message":{"content":[{"type":"tool_result","tool_use_id":"t1","is_error":true,"content":"File missing"}]}}
+{"type":"assistant","uuid":"a2","message":{"content":[{"type":"text","text":"The file is missing."}],"stop_reason":"end_turn"}}
+{"type":"assistant","uuid":"a2","message":{"content":[{"type":"text","text":"The file is missing."}],"stop_reason":"end_turn"}}
+{"type":"user","uuid":"u3","message":{"content":[{"type":"text","text":"Thanks."}]}}
+{"type":"assistant","uuid":"a3","isSidechain":true,"message":{"content":[{"type":"text","text":"Hidden child."}],"stop_reason":"end_turn"}}
+{"type":"assistant","uuid":"a4","message":{"content":[{"type":"text","text":"You are welcome."}],"stop_reason":"max_tokens"}}
+{"type":"future_record","uuid":"f1"}
diff --git a/crates/coding_agent_worker/src/herdr/claude_log.rs b/crates/folds/claude_fold/src/lib.rs
similarity index 85%
rename from crates/coding_agent_worker/src/herdr/claude_log.rs
rename to crates/folds/claude_fold/src/lib.rs
index 7c811c5110..a9542c0103 100644
--- a/crates/coding_agent_worker/src/herdr/claude_log.rs
+++ b/crates/folds/claude_fold/src/lib.rs
@@ -7,6 +7,9 @@
//! the assistant message that stops for a reason other than a tool call ends
//! the turn.
+#![deny(missing_docs)]
+
+use agent_fold::domain::transcript::{Fold, LogEvent, truncate};
use std::collections::HashSet;
use serde_json::{Value, json};
@@ -14,28 +17,16 @@ use serde_json::{Value, json};
#[cfg(test)]
mod test;
-/// Longest tool output forwarded to Macro, in characters.
-const TOOL_OUTPUT_LIMIT: usize = 4_000;
-
-/// What one transcript line means for the ACP session.
-#[derive(Debug, Clone, PartialEq)]
-pub(crate) enum LogEvent {
- /// A `session/update` payload to send.
- Update(Value),
- /// The turn is over, with this ACP stop reason.
- TurnEnded(&'static str),
-}
-
/// Transcript reader state: lines are appended once, but a reader that
/// reopens the file must not replay them.
#[derive(Debug, Default)]
-pub(crate) struct ClaudeLog {
+pub struct ClaudeLog {
seen: HashSet<String>,
}
-impl ClaudeLog {
+impl Fold for ClaudeLog {
/// Translate one transcript line.
- pub(crate) fn entry(&mut self, line: &str) -> Vec<LogEvent> {
+ fn entry(&mut self, line: &str) -> Vec<LogEvent> {
let Ok(entry) = serde_json::from_str::<Value>(line) else {
return Vec::new();
};
@@ -52,7 +43,7 @@ impl ClaudeLog {
};
match entry.get("type").and_then(Value::as_str) {
Some("assistant") => assistant(message),
- Some("user") => tool_results(message),
+ Some("user") => user_message(message),
_ => Vec::new(),
}
}
@@ -89,6 +80,7 @@ fn assistant(message: &Value) -> Vec<LogEvent> {
"kind": tool_kind(name),
"status": "in_progress",
"rawInput": input,
+ "_meta": {"claudeCode": {"toolName": name}},
}))
}
_ => None,
@@ -106,6 +98,25 @@ fn assistant(message: &Value) -> Vec<LogEvent> {
events
}
+fn user_message(message: &Value) -> Vec<LogEvent> {
+ let mut events = tool_results(message);
+ let text = match message.get("content") {
+ Some(Value::String(text)) => text.clone(),
+ _ => blocks(message)
+ .filter(|block| block["type"] == "text")
+ .filter_map(|block| block["text"].as_str())
+ .collect::<Vec<_>>()
+ .join("\n"),
+ };
+ if !text.is_empty() {
+ events.push(LogEvent::Update(json!({
+ "sessionUpdate": "user_message_chunk",
+ "content": {"type": "text", "text": text},
+ })));
+ }
+ events
+}
+
fn tool_results(message: &Value) -> Vec<LogEvent> {
blocks(message)
.filter(|block| block.get("type").and_then(Value::as_str) == Some("tool_result"))
@@ -141,15 +152,6 @@ fn result_text(content: Option<&Value>) -> String {
}
}
-pub(crate) fn truncate(text: &str) -> String {
- if text.chars().count() <= TOOL_OUTPUT_LIMIT {
- return text.to_owned();
- }
- let mut kept: String = text.chars().take(TOOL_OUTPUT_LIMIT).collect();
- kept.push_str("\n… [truncated]");
- kept
-}
-
fn tool_title(name: &str, input: &Value) -> String {
let field = |key| input.get(key).and_then(Value::as_str);
let detail = match name {
diff --git a/crates/folds/claude_fold/src/snapshots/claude_fold__test__api_messages_snapshot.snap b/crates/folds/claude_fold/src/snapshots/claude_fold__test__api_messages_snapshot.snap
new file mode 100644
index 0000000000..7df151ed9c
--- /dev/null
+++ b/crates/folds/claude_fold/src/snapshots/claude_fold__test__api_messages_snapshot.snap
@@ -0,0 +1,89 @@
+---
+source: crates/folds/claude_fold/src/test.rs
+expression: messages
+---
+[
+ {
+ "turn": 0,
+ "author": {
+ "kind": "user",
+ "userId": null
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "text",
+ "text": "Read the file."
+ }
+ ],
+ "stop": null,
+ "pending": false
+ },
+ {
+ "turn": 0,
+ "author": {
+ "kind": "agent"
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "thought",
+ "text": "I will inspect it."
+ },
+ {
+ "kind": "tool_use",
+ "id": "t1",
+ "name": {
+ "kind": "native",
+ "name": "Read"
+ },
+ "status": "failed",
+ "detail": {
+ "kind": "read",
+ "paths": []
+ }
+ },
+ {
+ "kind": "text",
+ "text": "The file is missing."
+ }
+ ],
+ "stop": {
+ "kind": "end_turn"
+ },
+ "pending": false
+ },
+ {
+ "turn": 1,
+ "author": {
+ "kind": "user",
+ "userId": null
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "text",
+ "text": "Thanks."
+ }
+ ],
+ "stop": null,
+ "pending": false
+ },
+ {
+ "turn": 1,
+ "author": {
+ "kind": "agent"
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "text",
+ "text": "You are welcome."
+ }
+ ],
+ "stop": {
+ "kind": "end_turn"
+ },
+ "pending": false
+ }
+]
diff --git a/crates/folds/claude_fold/src/snapshots/claude_fold__test__conversation_snapshot.snap b/crates/folds/claude_fold/src/snapshots/claude_fold__test__conversation_snapshot.snap
new file mode 100644
index 0000000000..e88e0f0843
--- /dev/null
+++ b/crates/folds/claude_fold/src/snapshots/claude_fold__test__conversation_snapshot.snap
@@ -0,0 +1,90 @@
+---
+source: crates/folds/claude_fold/src/test.rs
+expression: events
+---
+[
+ {
+ "Update": {
+ "sessionUpdate": "user_message_chunk",
+ "content": {
+ "type": "text",
+ "text": "Read the file."
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "agent_thought_chunk",
+ "content": {
+ "type": "text",
+ "text": "I will inspect it."
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "tool_call",
+ "toolCallId": "t1",
+ "title": "Read /repo/a.txt",
+ "kind": "read",
+ "status": "in_progress",
+ "rawInput": {
+ "file_path": "/repo/a.txt"
+ },
+ "_meta": {
+ "claudeCode": {
+ "toolName": "Read"
+ }
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "tool_call_update",
+ "toolCallId": "t1",
+ "status": "failed",
+ "content": [
+ {
+ "type": "content",
+ "content": {
+ "type": "text",
+ "text": "File missing"
+ }
+ }
+ ]
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "agent_message_chunk",
+ "content": {
+ "type": "text",
+ "text": "The file is missing."
+ }
+ }
+ },
+ {
+ "TurnEnded": "end_turn"
+ },
+ {
+ "Update": {
+ "sessionUpdate": "user_message_chunk",
+ "content": {
+ "type": "text",
+ "text": "Thanks."
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "agent_message_chunk",
+ "content": {
+ "type": "text",
+ "text": "You are welcome."
+ }
+ }
+ },
+ {
+ "TurnEnded": "max_tokens"
+ }
+]
diff --git a/crates/coding_agent_worker/src/herdr/claude_log/test.rs b/crates/folds/claude_fold/src/test.rs
similarity index 65%
rename from crates/coding_agent_worker/src/herdr/claude_log/test.rs
rename to crates/folds/claude_fold/src/test.rs
index e3a9985b16..368cf5bfcb 100644
--- a/crates/coding_agent_worker/src/herdr/claude_log/test.rs
+++ b/crates/folds/claude_fold/src/test.rs
@@ -28,6 +28,7 @@ fn a_tool_turn_streams_calls_results_and_its_end() {
"kind": "edit",
"status": "in_progress",
"rawInput": {"file_path": "/r/todo.html", "content": "<html>"},
+ "_meta": {"claudeCode": {"toolName": "Write"}},
})),
]
);
@@ -84,10 +85,9 @@ fn failed_tools_and_block_results_are_reported() {
}
#[test]
-fn prompts_bookkeeping_and_sidechains_are_skipped() {
+fn bookkeeping_and_sidechains_are_skipped() {
let mut log = ClaudeLog::default();
for entry in [
- json!({"type": "user", "uuid": "a", "message": {"content": "build a todo app"}}),
json!({"type": "attachment", "uuid": "b"}),
json!({"type": "ai-title", "aiTitle": "Todo app"}),
json!({"type": "assistant", "uuid": "c", "isSidechain": true,
@@ -110,3 +110,54 @@ fn bash_calls_are_titled_by_their_description() {
assert_eq!(tool_title("Bash", &json!({"command": "ls"})), "Bash ls");
assert_eq!(tool_title("TodoWrite", &json!({})), "TodoWrite");
}
+
+#[test]
+fn conversation_snapshot() {
+ let mut fold = ClaudeLog::default();
+ let events: Vec<_> = include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| fold.entry(line))
+ .collect();
+ insta::assert_json_snapshot!(events);
+}
+
+#[test]
+fn malformed_records_do_not_poison_following_records() {
+ let mut fold = ClaudeLog::default();
+ assert!(fold.entry("{").is_empty());
+ let actual: Vec<_> = include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| fold.entry(line))
+ .collect();
+ let mut fresh = ClaudeLog::default();
+ let expected: Vec<_> = include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| fresh.entry(line))
+ .collect();
+ assert_eq!(actual, expected);
+}
+
+#[test]
+fn api_messages_snapshot() {
+ let mut native = ClaudeLog::default();
+ let mut wire = String::new();
+ for event in include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| native.entry(line))
+ {
+ let (method, params) = match event {
+ LogEvent::Update(update) => {
+ ("session/update", json!({"sessionId":"s", "update":update}))
+ }
+ LogEvent::TurnEnded(stop) => (
+ "_session/turn_complete",
+ json!({"sessionId":"s", "outcome":{"kind":if stop == "cancelled" {"cancelled"} else {"finished"}}}),
+ ),
+ };
+ wire.push_str(&json!({"direction":"to_server", "content":{"type":"acp", "jsonrpc":"2.0", "method":method, "params":params}}).to_string());
+ wire.push('\n');
+ }
+ let messages = agent_fold::domain::fold::fold(agent_fold::testing::parse_log(&wire));
+ assert!(messages.len() >= 3, "native turns must reach API messages");
+ insta::assert_json_snapshot!(messages);
+}
diff --git a/crates/folds/codex_fold/Cargo.toml b/crates/folds/codex_fold/Cargo.toml
new file mode 100644
index 0000000000..f4e520b631
--- /dev/null
+++ b/crates/folds/codex_fold/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "codex_fold"
+version = "0.1.0"
+edition = "2024"
+publish = false
+
+[dependencies]
+agent_fold = { path = "../agent_fold", default-features = false }
+serde_json = { workspace = true }
+workspace-hack = { version = "0.1", path = "../../workspace-hack" }
+
+[dev-dependencies]
+agent_fold = { path = "../agent_fold", default-features = false, features = ["test-utils"] }
+insta = { version = "1.48.0", features = ["json"] }
diff --git a/crates/folds/codex_fold/fixtures/conversation.jsonl b/crates/folds/codex_fold/fixtures/conversation.jsonl
new file mode 100644
index 0000000000..ef3cc9ddba
--- /dev/null
+++ b/crates/folds/codex_fold/fixtures/conversation.jsonl
@@ -0,0 +1,12 @@
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"UserMessage","id":"u1","content":[{"type":"Text","text":"Read the file."}]}}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"Reasoning","id":"r1","summary_text":["I will inspect it."]}}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"CommandExecution","id":"t1","command":["bash","-lc","cat a.txt"],"parsed_cmd":[{"type":"read"}],"status":"completed","exit_code":1,"aggregated_output":"File missing"}}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"FileChange","id":"f1","changes":{"/repo/a.txt":{"type":"add"}},"status":"completed"}}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"McpToolCall","id":"m1","server":"macro","tool":"search","arguments":{"query":"file"},"status":"failed"}}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"WebSearch","id":"w1","query":"file format"}}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"AgentMessage","id":"a1","content":[{"type":"Text","text":"The file is missing."}]}}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"AgentMessage","id":"a1","content":[{"type":"Text","text":"The file is missing."}]}}}
+{"type":"event_msg","payload":{"type":"task_complete"}}
+{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"UserMessage","id":"u2","content":[{"text":"Stop."}]}}}
+{"type":"event_msg","payload":{"type":"turn_aborted"}}
+{"type":"future_record"}
diff --git a/crates/coding_agent_worker/src/herdr/codex_log.rs b/crates/folds/codex_fold/src/lib.rs
similarity index 95%
rename from crates/coding_agent_worker/src/herdr/codex_log.rs
rename to crates/folds/codex_fold/src/lib.rs
index 0905a1ae9f..54b422dbbe 100644
--- a/crates/coding_agent_worker/src/herdr/codex_log.rs
+++ b/crates/folds/codex_fold/src/lib.rs
@@ -6,12 +6,13 @@
//! messages, reasoning, commands, file changes, tool calls) and a
//! `task_complete` or `turn_aborted` when a turn ends.
+#![deny(missing_docs)]
+
+use agent_fold::domain::transcript::{Fold, LogEvent, truncate};
use std::collections::HashSet;
use serde_json::{Value, json};
-use super::claude_log::{LogEvent, truncate};
-
#[cfg(test)]
mod test;
@@ -19,13 +20,13 @@ const TITLE_LIMIT: usize = 80;
/// Rollout reader state, so a reopened file never replays an item.
#[derive(Debug, Default)]
-pub(crate) struct CodexLog {
+pub struct CodexLog {
seen: HashSet<String>,
}
-impl CodexLog {
+impl Fold for CodexLog {
/// Translate one rollout line.
- pub(crate) fn entry(&mut self, line: &str) -> Vec<LogEvent> {
+ fn entry(&mut self, line: &str) -> Vec<LogEvent> {
let Ok(entry) = serde_json::from_str::<Value>(line) else {
return Vec::new();
};
@@ -74,11 +75,11 @@ fn item_update(item: &Value) -> Option<Value> {
let id = item.get("id").and_then(Value::as_str).unwrap_or_default();
let field = |name: &str| item.get(name).and_then(Value::as_str);
match item.get("type").and_then(Value::as_str)? {
- "AgentMessage" => {
+ "AgentMessage" | "UserMessage" => {
let text = texts(item.get("content"), "text");
(!text.is_empty()).then(|| {
json!({
- "sessionUpdate": "agent_message_chunk",
+ "sessionUpdate": if item["type"] == "UserMessage" { "user_message_chunk" } else { "agent_message_chunk" },
"content": {"type": "text", "text": text},
})
})
diff --git a/crates/folds/codex_fold/src/snapshots/codex_fold__test__api_messages_snapshot.snap b/crates/folds/codex_fold/src/snapshots/codex_fold__test__api_messages_snapshot.snap
new file mode 100644
index 0000000000..83ed840a4c
--- /dev/null
+++ b/crates/folds/codex_fold/src/snapshots/codex_fold__test__api_messages_snapshot.snap
@@ -0,0 +1,135 @@
+---
+source: crates/folds/codex_fold/src/test.rs
+expression: messages
+---
+[
+ {
+ "turn": 0,
+ "author": {
+ "kind": "user",
+ "userId": null
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "text",
+ "text": "Read the file."
+ }
+ ],
+ "stop": null,
+ "pending": false
+ },
+ {
+ "turn": 0,
+ "author": {
+ "kind": "agent"
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "thought",
+ "text": "I will inspect it."
+ },
+ {
+ "kind": "tool_use",
+ "id": "t1",
+ "name": {
+ "kind": "native",
+ "name": "Run cat a.txt"
+ },
+ "status": "failed",
+ "detail": {
+ "kind": "read",
+ "paths": []
+ }
+ },
+ {
+ "kind": "tool_use",
+ "id": "f1",
+ "name": {
+ "kind": "native",
+ "name": "Edit /repo/a.txt"
+ },
+ "status": "completed",
+ "detail": {
+ "kind": "edit",
+ "diffs": []
+ }
+ },
+ {
+ "kind": "tool_use",
+ "id": "m1",
+ "name": {
+ "kind": "native",
+ "name": "Call macro.search"
+ },
+ "status": "failed",
+ "detail": {
+ "kind": "other",
+ "acpKind": "other",
+ "output": null,
+ "input": {
+ "query": "file"
+ },
+ "result": null,
+ "error": null
+ }
+ },
+ {
+ "kind": "tool_use",
+ "id": "w1",
+ "name": {
+ "kind": "native",
+ "name": "Search file format"
+ },
+ "status": "completed",
+ "detail": {
+ "kind": "search",
+ "paths": [],
+ "output": null
+ }
+ },
+ {
+ "kind": "text",
+ "text": "The file is missing."
+ }
+ ],
+ "stop": {
+ "kind": "end_turn"
+ },
+ "pending": false
+ },
+ {
+ "turn": 1,
+ "author": {
+ "kind": "user",
+ "userId": null
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "text",
+ "text": "Stop."
+ }
+ ],
+ "stop": null,
+ "pending": false
+ },
+ {
+ "turn": 1,
+ "author": {
+ "kind": "agent"
+ },
+ "requestId": null,
+ "parts": [
+ {
+ "kind": "text",
+ "text": ""
+ }
+ ],
+ "stop": {
+ "kind": "cancelled"
+ },
+ "pending": false
+ }
+]
diff --git a/crates/folds/codex_fold/src/snapshots/codex_fold__test__conversation_snapshot.snap b/crates/folds/codex_fold/src/snapshots/codex_fold__test__conversation_snapshot.snap
new file mode 100644
index 0000000000..e85ff32ce5
--- /dev/null
+++ b/crates/folds/codex_fold/src/snapshots/codex_fold__test__conversation_snapshot.snap
@@ -0,0 +1,105 @@
+---
+source: crates/folds/codex_fold/src/test.rs
+expression: events
+---
+[
+ {
+ "Update": {
+ "sessionUpdate": "user_message_chunk",
+ "content": {
+ "type": "text",
+ "text": "Read the file."
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "agent_thought_chunk",
+ "content": {
+ "type": "text",
+ "text": "I will inspect it."
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "tool_call",
+ "toolCallId": "t1",
+ "title": "Run cat a.txt",
+ "kind": "read",
+ "status": "failed",
+ "rawInput": {
+ "command": "cat a.txt"
+ },
+ "content": [
+ {
+ "type": "content",
+ "content": {
+ "type": "text",
+ "text": "File missing"
+ }
+ }
+ ]
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "tool_call",
+ "toolCallId": "f1",
+ "title": "Edit /repo/a.txt",
+ "kind": "edit",
+ "status": "completed",
+ "rawInput": {
+ "paths": [
+ "/repo/a.txt"
+ ]
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "tool_call",
+ "toolCallId": "m1",
+ "title": "Call macro.search",
+ "kind": "other",
+ "status": "failed",
+ "rawInput": {
+ "query": "file"
+ }
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "tool_call",
+ "toolCallId": "w1",
+ "title": "Search file format",
+ "kind": "search",
+ "status": "completed",
+ "rawInput": null
+ }
+ },
+ {
+ "Update": {
+ "sessionUpdate": "agent_message_chunk",
+ "content": {
+ "type": "text",
+ "text": "The file is missing."
+ }
+ }
+ },
+ {
+ "TurnEnded": "end_turn"
+ },
+ {
+ "Update": {
+ "sessionUpdate": "user_message_chunk",
+ "content": {
+ "type": "text",
+ "text": "Stop."
+ }
+ }
+ },
+ {
+ "TurnEnded": "cancelled"
+ }
+]
diff --git a/crates/coding_agent_worker/src/herdr/codex_log/test.rs b/crates/folds/codex_fold/src/test.rs
similarity index 69%
rename from crates/coding_agent_worker/src/herdr/codex_log/test.rs
rename to crates/folds/codex_fold/src/test.rs
index 7775836525..55a829ffbc 100644
--- a/crates/coding_agent_worker/src/herdr/codex_log/test.rs
+++ b/crates/folds/codex_fold/src/test.rs
@@ -12,12 +12,6 @@ fn completed(item: Value) -> String {
#[test]
fn a_command_turn_streams_messages_commands_and_its_end() {
let mut log = CodexLog::default();
- assert!(
- log.entry(&completed(
- json!({"type": "UserMessage", "id": "u1", "content": [{"type": "text", "text": "hi"}]})
- ))
- .is_empty()
- );
assert_eq!(
log.entry(&completed(json!({
"type": "AgentMessage", "id": "m1", "phase": "commentary",
@@ -121,3 +115,54 @@ fn bookkeeping_is_ignored() {
assert!(log.entry(&line).is_empty());
}
}
+
+#[test]
+fn conversation_snapshot() {
+ let mut fold = CodexLog::default();
+ let events: Vec<_> = include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| fold.entry(line))
+ .collect();
+ insta::assert_json_snapshot!(events);
+}
+
+#[test]
+fn malformed_records_do_not_poison_following_records() {
+ let mut fold = CodexLog::default();
+ assert!(fold.entry("{").is_empty());
+ let actual: Vec<_> = include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| fold.entry(line))
+ .collect();
+ let mut fresh = CodexLog::default();
+ let expected: Vec<_> = include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| fresh.entry(line))
+ .collect();
+ assert_eq!(actual, expected);
+}
+
+#[test]
+fn api_messages_snapshot() {
+ let mut native = CodexLog::default();
+ let mut wire = String::new();
+ for event in include_str!("../fixtures/conversation.jsonl")
+ .lines()
+ .flat_map(|line| native.entry(line))
+ {
+ let (method, params) = match event {
+ LogEvent::Update(update) => {
+ ("session/update", json!({"sessionId":"s", "update":update}))
+ }
+ LogEvent::TurnEnded(stop) => (
+ "_session/turn_complete",
+ json!({"sessionId":"s", "outcome":{"kind":if stop == "cancelled" {"cancelled"} else {"finished"}}}),
+ ),
+ };
+ wire.push_str(&json!({"direction":"to_server", "content":{"type":"acp", "jsonrpc":"2.0", "method":method, "params":params}}).to_string());
+ wire.push('\n');
+ }
+ let messages = agent_fold::domain::fold::fold(agent_fold::testing::parse_log(&wire));
+ assert!(messages.len() >= 3, "native turns must reach API messages");
+ insta::assert_json_snapshot!(messages);
+}