diff --git a/CHANGELOG.md b/CHANGELOG.md index 8da1910..d1e459d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,16 @@ This changelog is automatically generated by AI when releases are published. ## [Unreleased] +## [3.19.0] - 2026-12-05 + +### Added + +- **Stored transcripts keep each tool call's id.** Every tool call in a transcript autter saves now carries the id the agent gave it: the `id` of a Claude Code (`toolu_...`) or Droid (`call_...`) `tool_use` block, or the `call_id` of a Codex call. These are the same ids the agents' hooks report, so the Autter PR page can point a changed line at the exact tool call that wrote it instead of guessing by time. Transcripts stored before this have no ids and still parse as before. + +### Changed + +- **Codex freeform `apply_patch` calls are kept.** Codex `custom_tool_call` items were dropped from stored transcripts; they now appear as tool calls, with their `call_id`. + ## [3.18.0] - 2026-11-24 ### Added diff --git a/Cargo.lock b/Cargo.lock index 892c1a7..d01ec5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,7 +258,7 @@ checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "autter" -version = "3.18.0" +version = "3.19.0" dependencies = [ "autter", "base64", diff --git a/Cargo.toml b/Cargo.toml index 57bca39..302d973 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ resolver = "3" [package] name = "autter" -version = "3.18.0" +version = "3.19.0" edition = "2024" default-run = "autter" diff --git a/src/authorship/cas_bridge.rs b/src/authorship/cas_bridge.rs index 763a144..63c8c54 100644 --- a/src/authorship/cas_bridge.rs +++ b/src/authorship/cas_bridge.rs @@ -436,29 +436,35 @@ fn messages_from_event(event: &Value) -> Vec { .map(str::to_string) .or(outer_timestamp); - if event - .get("type") - .and_then(Value::as_str) - .is_some_and(|t| t == "function_call") - { + // Codex tool calls: `function_call` (JSON `arguments`) and + // `custom_tool_call` (freeform `input`, e.g. `apply_patch`). Both name the + // call with `call_id`, which is the `tool_use_id` Codex's hooks send. + // (`id` on these items is the response item's own id, not the call's.) + let event_type = event.get("type").and_then(Value::as_str); + if matches!(event_type, Some("function_call" | "custom_tool_call")) { let name = event .get("name") .and_then(Value::as_str) .unwrap_or("unknown") .to_string(); - let input = event - .get("arguments") - .and_then(|v| { - if let Some(raw) = v.as_str() { - serde_json::from_str(raw).ok() - } else { - Some(v.clone()) - } - }) - .unwrap_or(Value::Null); + let input = if event_type == Some("function_call") { + event + .get("arguments") + .and_then(|v| { + if let Some(raw) = v.as_str() { + serde_json::from_str(raw).ok() + } else { + Some(v.clone()) + } + }) + .unwrap_or(Value::Null) + } else { + event.get("input").cloned().unwrap_or(Value::Null) + }; return vec![Message::ToolUse { name, input, + id: non_empty_str(&event, "call_id"), timestamp, }]; } @@ -514,9 +520,12 @@ fn block_to_messages(role: Role, block: &Value, timestamp: &Option) -> V .unwrap_or("unknown") .to_string(); let input = block.get("input").cloned().unwrap_or(Value::Null); + // Claude's `toolu_…` id (Droid: `call_…`), the same id the + // agent's PostToolUse hook sends as `tool_use_id`. vec![Message::ToolUse { name, input, + id: non_empty_str(block, "id"), timestamp: timestamp.clone(), }] } @@ -583,6 +592,16 @@ fn event_role(event: &Value) -> Role { } } +/// A string field as written (ids must match the hook's byte for byte), or +/// `None` when it is missing, not a string, or blank. +fn non_empty_str(value: &Value, key: &str) -> Option { + value + .get(key) + .and_then(Value::as_str) + .filter(|s| !s.trim().is_empty()) + .map(str::to_string) +} + /// Build a user/assistant text message, dropping empty text. fn text_message(role: Role, text: String, timestamp: Option) -> Option { if text.trim().is_empty() { @@ -664,14 +683,66 @@ mod tests { "type": "assistant", "message": { "content": [ { "type": "text", "text": "I'll create it." }, - { "type": "tool_use", "name": "Write", "input": { "file_path": "hello.py" } } + { "type": "tool_use", "id": "toolu_017eJUTKxEGKFWWtVD54v9vx", "name": "Write", "input": { "file_path": "hello.py" } } ], "model": "claude-sonnet-4" }, "timestamp": "2025-01-01T12:00:02Z" }); let msgs = messages_from_event(&event); assert_eq!(msgs.len(), 2); assert!(matches!(&msgs[0], Message::Assistant { text, .. } if text == "I'll create it.")); - assert!(matches!(&msgs[1], Message::ToolUse { name, .. } if name == "Write")); + assert_eq!( + msgs[1], + Message::ToolUse { + name: "Write".to_string(), + input: serde_json::json!({ "file_path": "hello.py" }), + id: Some("toolu_017eJUTKxEGKFWWtVD54v9vx".to_string()), + timestamp: Some("2025-01-01T12:00:02Z".to_string()), + } + ); + } + + #[test] + fn droid_tool_use_block_keeps_call_id() { + let event = serde_json::json!({ + "type": "message", + "id": "3931b94d-4807-4231-a40c-854b90f1ba03", + "timestamp": "2026-01-28T16:57:15.899Z", + "message": { "role": "assistant", "content": [ + { "type": "tool_use", "id": "call_4LkL0djh4nDHl8xGUQeH4Kvh", "name": "Read", + "input": { "file_path": "/p/index.ts" } } + ] } + }); + let msgs = messages_from_event(&event); + assert!(matches!( + &msgs[0], + Message::ToolUse { name, id, .. } + if name == "Read" && id.as_deref() == Some("call_4LkL0djh4nDHl8xGUQeH4Kvh") + )); + } + + #[test] + fn cursor_tool_use_block_without_id_has_none() { + // Cursor's agent transcripts carry no tool-call id. + let event = serde_json::json!({ + "role": "assistant", + "message": { "content": [ + { "type": "tool_use", "name": "Read", "input": { "path": "/p/a.txt" } } + ] } + }); + let msgs = messages_from_event(&event); + assert!(matches!(&msgs[0], Message::ToolUse { id: None, .. })); + } + + #[test] + fn blank_tool_use_id_is_dropped() { + let event = serde_json::json!({ + "type": "assistant", + "message": { "content": [ + { "type": "tool_use", "id": " ", "name": "Write", "input": {} } + ] } + }); + let msgs = messages_from_event(&event); + assert!(matches!(&msgs[0], Message::ToolUse { id: None, .. })); } #[test] @@ -740,11 +811,116 @@ mod tests { "payload": { "type": "function_call", "name": "apply_patch", - "arguments": "{\"patch\":\"*** Begin Patch\"}" + "arguments": "{\"patch\":\"*** Begin Patch\"}", + "call_id": "call_abc123" } }); let msgs = messages_from_event(&event); - assert!(matches!(&msgs[0], Message::ToolUse { name, .. } if name == "apply_patch")); + assert_eq!( + msgs, + vec![Message::ToolUse { + name: "apply_patch".to_string(), + input: serde_json::json!({ "patch": "*** Begin Patch" }), + id: Some("call_abc123".to_string()), + timestamp: Some("2026-02-11T05:53:33.420Z".to_string()), + }] + ); + } + + #[test] + fn codex_function_call_uses_call_id_not_item_id() { + let event = serde_json::json!({ + "type": "response_item", + "payload": { + "type": "function_call", + "id": "fc_68a1b2c3", + "name": "shell", + "arguments": "{\"command\":[\"ls\"]}", + "call_id": "call_def456" + } + }); + let msgs = messages_from_event(&event); + assert!(matches!( + &msgs[0], + Message::ToolUse { id, .. } if id.as_deref() == Some("call_def456") + )); + } + + #[test] + fn codex_custom_tool_call_maps_to_tool_use_with_call_id() { + let event = serde_json::json!({ + "timestamp": "2026-02-11T05:53:34.000Z", + "type": "response_item", + "payload": { + "type": "custom_tool_call", + "status": "completed", + "call_id": "call_patch789", + "name": "apply_patch", + "input": "*** Begin Patch\n*** Update File: src/main.rs\n*** End Patch" + } + }); + let msgs = messages_from_event(&event); + assert_eq!( + msgs, + vec![Message::ToolUse { + name: "apply_patch".to_string(), + input: Value::String( + "*** Begin Patch\n*** Update File: src/main.rs\n*** End Patch".to_string() + ), + id: Some("call_patch789".to_string()), + timestamp: Some("2026-02-11T05:53:34.000Z".to_string()), + }] + ); + } + + #[test] + fn transcript_file_keeps_tool_use_ids_through_redaction() { + // The path both the upload queue and the local cache take: parse, + // serialize, redact, parse again. `id` is on the redaction denylist, + // so even a secret-looking id must come out unchanged. + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("session.jsonl"); + let secret_looking_id = "toolu_sk_test_4eC39HqLyjWDarjtT1zdp7dc"; + let lines = [ + serde_json::json!({ + "type": "user", + "message": { "content": "Write hello.py" }, + "timestamp": "2025-01-01T12:00:00Z" + }), + serde_json::json!({ + "type": "assistant", + "message": { "content": [ + { "type": "tool_use", "id": secret_looking_id, "name": "Write", + "input": { "file_path": "hello.py" } } + ] }, + "timestamp": "2025-01-01T12:00:02Z" + }), + serde_json::json!({ + "timestamp": "2026-02-11T05:53:33.420Z", + "type": "response_item", + "payload": { "type": "function_call", "name": "shell", + "arguments": "{}", "call_id": "call_abc123" } + }), + ]; + let raw: Vec = lines.iter().map(|l| l.to_string()).collect(); + std::fs::write(&path, raw.join("\n")).unwrap(); + + let messages = messages_from_transcript_file(path.to_str().unwrap()) + .unwrap() + .unwrap(); + let ids: Vec> = messages + .iter() + .filter_map(|m| match m { + Message::ToolUse { id, .. } => Some(id.as_deref()), + _ => None, + }) + .collect(); + assert_eq!(ids, vec![Some(secret_looking_id), Some("call_abc123")]); + + // The CAS object the daemon uploads as-is carries `id`. + let payload = serde_json::to_value(CasMessagesObject { messages }).unwrap(); + assert_eq!(payload["messages"][1]["id"], secret_looking_id); + assert_eq!(payload["messages"][2]["id"], "call_abc123"); } #[test] diff --git a/src/authorship/transcript.rs b/src/authorship/transcript.rs index b2340d4..8ae5a26 100644 --- a/src/authorship/transcript.rs +++ b/src/authorship/transcript.rs @@ -28,6 +28,13 @@ pub enum Message { ToolUse { name: String, input: serde_json::Value, + /// The agent's own id for this tool call (Claude's `tool_use` block + /// `id`, Codex's `call_id`). Checkpoints record the same id from the + /// hook input (`tool_use_id`), so a line can point at the exact call + /// that wrote it. Absent in transcripts stored before ids were kept, + /// and for agents whose transcripts carry no id. + #[serde(default, skip_serializing_if = "Option::is_none")] + id: Option, #[serde(skip_serializing_if = "Option::is_none")] timestamp: Option, }, @@ -57,10 +64,11 @@ impl Message { } /// Create a tool use message - pub fn tool_use(name: String, input: serde_json::Value) -> Self { + pub fn tool_use(name: String, input: serde_json::Value, id: Option) -> Self { Message::ToolUse { name, input, + id, timestamp: None, } } @@ -229,15 +237,21 @@ mod tests { #[test] fn test_message_tool_use() { let input = json!({"param": "value"}); - let msg = Message::tool_use("read_file".to_string(), input.clone()); + let msg = Message::tool_use( + "read_file".to_string(), + input.clone(), + Some("toolu_01".to_string()), + ); match msg { Message::ToolUse { name, input: tool_input, + id, timestamp, } => { assert_eq!(name, "read_file"); assert_eq!(tool_input, input); + assert_eq!(id.as_deref(), Some("toolu_01")); assert_eq!(timestamp, None); } _ => panic!("Expected ToolUse message"), @@ -258,7 +272,7 @@ mod tests { let plan_msg = Message::plan("Plan text".to_string(), None); assert_eq!(plan_msg.text(), Some(&"Plan text".to_string())); - let tool_msg = Message::tool_use("tool".to_string(), json!({})); + let tool_msg = Message::tool_use("tool".to_string(), json!({}), None); assert_eq!(tool_msg.text(), None); } @@ -267,7 +281,7 @@ mod tests { let user_msg = Message::user("text".to_string(), None); assert!(!user_msg.is_tool_use()); - let tool_msg = Message::tool_use("tool".to_string(), json!({})); + let tool_msg = Message::tool_use("tool".to_string(), json!({}), None); assert!(tool_msg.is_tool_use()); } @@ -312,7 +326,7 @@ mod tests { fn test_ai_transcript_without_tool_use() { let mut transcript = AiTranscript::new(); transcript.add_message(Message::user("user msg".to_string(), None)); - transcript.add_message(Message::tool_use("tool".to_string(), json!({}))); + transcript.add_message(Message::tool_use("tool".to_string(), json!({}), None)); transcript.add_message(Message::assistant("assistant msg".to_string(), None)); let filtered = transcript.without_tool_use(); @@ -467,4 +481,46 @@ mod tests { assert_eq!(t1, t2); assert_ne!(t1, t3); } + + #[test] + fn test_tool_use_id_round_trips() { + let msg = Message::ToolUse { + name: "Write".to_string(), + input: json!({"file_path": "hello.py"}), + id: Some("toolu_017eJUTKxEGKFWWtVD54v9vx".to_string()), + timestamp: Some("2025-01-01T12:00:02Z".to_string()), + }; + let value = serde_json::to_value(&msg).unwrap(); + assert_eq!(value["type"], "tool_use"); + assert_eq!(value["id"], "toolu_017eJUTKxEGKFWWtVD54v9vx"); + + let back: Message = serde_json::from_value(value).unwrap(); + assert_eq!(back, msg); + } + + #[test] + fn test_tool_use_without_id_omits_field() { + let msg = Message::tool_use("tool".to_string(), json!({}), None); + let value = serde_json::to_value(&msg).unwrap(); + assert!(value.get("id").is_none()); + } + + #[test] + fn test_stored_tool_use_without_id_still_parses() { + // Transcripts stored before tool-use ids were kept have no `id`. + let json = r#"{"messages":[ + {"type":"user","text":"Write hello.py"}, + {"type":"tool_use","name":"Write","input":{"file_path":"hello.py"},"timestamp":"2025-01-01T12:00:02Z"} + ]}"#; + let transcript: AiTranscript = serde_json::from_str(json).unwrap(); + assert_eq!( + transcript.messages[1], + Message::ToolUse { + name: "Write".to_string(), + input: json!({"file_path": "hello.py"}), + id: None, + timestamp: Some("2025-01-01T12:00:02Z".to_string()), + } + ); + } }