Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/tap-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Identity scopes remain separate:
- Capture attempt: `(run_id, exchange, step)` when the step is known; `(run_id, exchange)` identifies the exchange even for anonymous or unread traffic.
- Graph nodes named `exchange:<n>` and `step:<exchange>.<n>` are scoped to the enclosing line's `run_id`; their bare names are not globally unique.

Record-order waits, continuation history and tool-call matching use `(adapter, thread)`: equal native ids from different adapters do not link or wait on each other. Native ids and emitted edge names remain unchanged. The thread and open-call memory bounds below are shared across all adapters, not allocated per adapter.

The capture identity is independent of native harness sessions and wrapper run attribution. Diagnostic `--shape-file` output has its own format and carries none of `run_id`, `schema_version` or `sequence`.

## Delivery cursor
Expand Down
2 changes: 1 addition & 1 deletion src/exchange/exchanges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Exchanges {
let exchange = self.next_exchange();
let identity = opening.adapter.identity(opening.head);
// Joined before the parser is built, so nothing widens the gap between the id and the place in line
let steps = Steps::join(&self.threads, exchange, &identity);
let steps = Steps::join(&self.threads, exchange, &identity, opening.adapter.name());
let recorder = Recorder::start(self.output.clone(), exchange, opening.adapter.name(), identity.clone());
let parser: Box<dyn ExchangeParser> = match &self.shapes {
Some(log) => Box::new(ShapedParser::new(log.clone(), opening.parser, exchange, opening.head)),
Expand Down
190 changes: 189 additions & 1 deletion src/exchange/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ async fn a_result_in_a_later_request_writes_one_calls_edge_for_its_call() {
}

#[tokio::test]
async fn a_claude_and_a_codex_exchange_write_these_exact_records_and_edges() {
async fn two_exchanges_of_one_adapter_write_these_exact_records_and_edges() {
// It pins every field of the record and edge payloads a thread's two exchanges write
use crate::common::*;
use crate::message::{Block, Message, Output};
Expand Down Expand Up @@ -861,6 +861,177 @@ async fn a_claude_and_a_codex_exchange_write_these_exact_records_and_edges() {
);
}

#[tokio::test]
async fn adapter_scoped_threads_keep_separate_continuation_chains() {
// Equal native thread ids continue only earlier steps from the same adapter.
let claude = Named("claude", on_thread("t1"));
let codex = Named("codex", on_thread("t1"));
let mut capture = Capture::new();

let first_claude = capture.run(&claude, None, closed_reply()).await;
let first_codex = capture.run(&codex, None, closed_reply()).await;
let next_claude = capture.run(&claude, None, closed_reply()).await;
let next_codex = capture.run(&codex, None, closed_reply()).await;

assert!(step_edges(&first_claude).is_empty());
assert!(step_edges(&first_codex).is_empty());
assert_eq!(step_edges(&next_claude), [edge("continues", "step:3.1", "step:1.1")]);
assert_eq!(step_edges(&next_codex), [edge("continues", "step:4.1", "step:2.1")]);
for (name, lines) in [("claude", next_claude), ("codex", next_codex)] {
assert!(lines
.iter()
.all(|line| line["adapter"] == name && line["thread"] == "t1"));
assert_eq!(record_of(&lines)["payload"]["thread"], "t1");
}
}

#[tokio::test]
async fn adapter_scoped_threads_do_not_share_or_overwrite_reply_ids() {
// A foreign reply id is unknown; identical reply ids still resolve to each adapter's own step.
let claude = Named("claude", on_thread("t1"));
let codex = Named("codex", on_thread("t1"));
let reply = || {
vec![
conversation(Value::Null, Value::Null, Vec::new()),
reply_end_of_message(Value::Null, json!("m1")),
]
};
let following = || {
let mut events = reply();
events[0].event.conversation_mut().previous_message_id = Some("m1".into());
events
};
let mut capture = Capture::new();

capture.run(&claude, None, reply()).await;
let foreign = capture.run(&codex, None, following()).await;
let next_claude = capture.run(&claude, None, following()).await;
let next_codex = capture.run(&codex, None, following()).await;

assert!(step_edges(&foreign).is_empty());
assert_eq!(step_edges(&next_claude), [edge("continues", "step:3.1", "step:1.1")]);
assert_eq!(step_edges(&next_codex), [edge("continues", "step:4.1", "step:2.1")]);
}

#[tokio::test]
async fn adapter_scoped_threads_match_and_close_only_their_own_calls() {
// Foreign results consume nothing, and equal call ids can be answered once in each adapter.
use crate::common::{BlockKind, BlockStart};
use crate::message::{Block, Message, Output};
let claude = Named("claude", on_thread("t1"));
let codex = Named("codex", on_thread("t1"));
let calling = || {
vec![
conversation(Value::Null, Value::Null, Vec::new()),
block_start(BlockStart {
index: 0,
kind: BlockKind::ToolCall,
id: json!("c1"),
name: Value::Null,
}),
block_stop(0),
reply_end_of_message(Value::Null, Value::Null),
]
};
let answering = || {
vec![
conversation(
Value::Null,
Value::Null,
vec![Message::user(vec![Block::ToolResult {
call_id: json!("c1"),
is_error: false,
output: Output::Raw(Value::Null),
}])],
),
reply_end_of_message(Value::Null, Value::Null),
]
};
let calls = |lines: &[Value]| -> Vec<Edge> {
step_edges(lines)
.into_iter()
.filter(|(kind, _, _)| kind == "calls")
.collect()
};
let mut capture = Capture::new();

capture.run(&claude, None, calling()).await;
let foreign = capture.run(&codex, None, answering()).await;
capture.run(&codex, None, calling()).await;
let own_claude = capture.run(&claude, None, answering()).await;
let own_codex = capture.run(&codex, None, answering()).await;
let resent = capture.run(&codex, None, answering()).await;

assert!(calls(&foreign).is_empty());
assert_eq!(calls(&own_claude), [edge("calls", "call:c1", "result:c1")]);
assert_eq!(calls(&own_codex), [edge("calls", "call:c1", "result:c1")]);
assert!(calls(&resent).is_empty());
}

#[tokio::test]
async fn adapter_scoped_threads_do_not_wait_for_another_adapter() {
// An unfinished exchange cannot delay a record belonging to another adapter's same native thread id.
let claude = Named("claude", on_thread("t1"));
let codex = Named("codex", on_thread("t1"));
let mut capture = Capture::new();
let earlier = capture.begin(
&claude,
None,
Emits {
events: Vec::new(),
release: None,
},
);

let finished = tokio::time::timeout(Duration::from_millis(500), capture.run(&codex, None, closed_reply())).await;
drop(earlier);

assert!(finished.is_ok());
let lines = finished.unwrap();
assert_eq!(record_of(&lines)["exchange"], 2);
assert!(step_edges(&lines).is_empty());
}

#[tokio::test]
async fn adapter_scoped_threads_still_wait_for_their_own_earlier_exchange() {
// The same adapter still writes its earlier record and edges before the later record.
let who = Named("claude", on_thread("t1"));
let mut capture = Capture::new();
let earlier = capture.begin(
&who,
None,
Emits {
events: closed_reply(),
release: None,
},
);
let later = capture.begin(
&who,
None,
Emits {
events: closed_reply(),
release: None,
},
);

later.end(Side::Response);
let_the_tasks_run().await;
let premature_record =
std::iter::from_fn(|| capture.drained.try_recv().ok()).any(|line| line.entry.event() == "record");
earlier.end(Side::Response);
drop((earlier, later));
let lines = capture.ended(2).await;

assert!(!premature_record);
let record_order: Vec<u64> = lines
.iter()
.filter(|line| line["event"] == "record")
.map(|line| line["exchange"].as_u64().unwrap())
.collect();
assert_eq!(record_order, [1, 2]);
assert_eq!(step_edges(&lines), [edge("continues", "step:2.1", "step:1.1")]);
}

type Edge = (String, String, String);

fn edge(kind: &str, from: &str, to: &str) -> Edge {
Expand Down Expand Up @@ -1230,6 +1401,23 @@ async fn a_stalled_parser_gives_an_incomplete_record_that_counts_the_dropped_inp

struct Identifying(Identity);

struct Named(&'static str, Identifying);

impl Adapter for Named {
fn name(&self) -> &'static str {
self.0
}
fn hosts(&self) -> &[String] {
&[]
}
fn claim(&self, _head: &RequestHead) -> Option<Box<dyn ExchangeParser>> {
None
}
fn identity(&self, head: &RequestHead) -> Identity {
self.1.identity(head)
}
}

impl Adapter for Identifying {
fn name(&self) -> &'static str {
"test"
Expand Down
63 changes: 35 additions & 28 deletions src/graph/calls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::Thread;
use crate::common::{Edge, EdgeKind};
use std::collections::{HashMap, VecDeque};

Expand All @@ -7,29 +8,29 @@ pub const CALL_BOUND: usize = 4096;
/// Remembers the tool calls a reply made until a request carries their result.
#[derive(Default)]
pub struct Calls {
open: HashMap<(String, String), u64>,
order: VecDeque<((String, String), u64)>,
open: HashMap<(Thread, String), u64>,
order: VecDeque<((Thread, String), u64)>,
next: u64,
}

impl Calls {
/// The `calls` edges one step adds: one for each result in its request that answers an open
/// call of the thread, which then closes, so a result resent in a later request adds nothing.
/// The step's own calls are remembered after its results, as the request comes first.
pub fn edges(&mut self, thread: &str, results: &[String], calls: &[String]) -> Vec<Edge> {
pub fn edges(&mut self, thread: &Thread, results: &[String], calls: &[String]) -> Vec<Edge> {
let mut edges = Vec::new();
for id in results.iter().filter(|id| !id.is_empty()) {
if self.open.remove(&(thread.to_string(), id.clone())).is_some() {
if self.open.remove(&(thread.clone(), id.clone())).is_some() {
edges.push(Edge::new(EdgeKind::Calls, format!("call:{id}"), format!("result:{id}")));
}
}
for id in calls.iter().filter(|id| !id.is_empty()) {
self.remember((thread.to_string(), id.clone()));
self.remember((thread.clone(), id.clone()));
}
edges
}

fn remember(&mut self, key: (String, String)) {
fn remember(&mut self, key: (Thread, String)) {
self.next += 1;
self.open.insert(key.clone(), self.next);
self.order.push_back((key, self.next));
Expand Down Expand Up @@ -65,8 +66,8 @@ mod tests {
// The reply makes the call, the next request carries the result
let mut calls = Calls::default();

let reply = calls.edges("t1", &[], &ids(&["c1"]));
let next = calls.edges("t1", &ids(&["c1"]), &[]);
let reply = calls.edges(&Thread::new("test", "t1"), &[], &ids(&["c1"]));
let next = calls.edges(&Thread::new("test", "t1"), &ids(&["c1"]), &[]);

assert!(reply.is_empty());
assert_eq!(pairs(&next), [("call:c1", "result:c1")]);
Expand All @@ -77,73 +78,79 @@ mod tests {
fn a_result_resent_in_a_later_request_gives_no_second_edge() {
// Every request resends the whole conversation, so old results come again
let mut calls = Calls::default();
calls.edges("t1", &[], &ids(&["c1"]));
calls.edges("t1", &ids(&["c1"]), &[]);
calls.edges(&Thread::new("test", "t1"), &[], &ids(&["c1"]));
calls.edges(&Thread::new("test", "t1"), &ids(&["c1"]), &[]);

assert!(calls.edges("t1", &ids(&["c1"]), &[]).is_empty());
assert!(calls.edges(&Thread::new("test", "t1"), &ids(&["c1"]), &[]).is_empty());
}

#[test]
fn a_result_or_a_call_with_no_match_gives_no_edge() {
// An unknown result is ignored, and a call that never gets a result adds nothing
let mut calls = Calls::default();

assert!(calls.edges("t1", &ids(&["gone"]), &ids(&["c1"])).is_empty());
assert!(calls
.edges(&Thread::new("test", "t1"), &ids(&["gone"]), &ids(&["c1"]))
.is_empty());
}

#[test]
fn an_empty_id_is_ignored() {
// An empty id names nothing, so it cannot pair unrelated calls and results
let mut calls = Calls::default();
calls.edges("t1", &[], &ids(&[""]));
calls.edges(&Thread::new("test", "t1"), &[], &ids(&[""]));

assert!(calls.edges("t1", &ids(&[""]), &[]).is_empty());
assert!(calls.edges(&Thread::new("test", "t1"), &ids(&[""]), &[]).is_empty());
}

#[test]
fn a_result_matches_only_a_call_of_its_own_thread() {
// Another thread may have used the same call id
let mut calls = Calls::default();
calls.edges("t1", &[], &ids(&["c1"]));
calls.edges(&Thread::new("test", "t1"), &[], &ids(&["c1"]));

assert!(calls.edges("t2", &ids(&["c1"]), &[]).is_empty());
assert_eq!(calls.edges("t1", &ids(&["c1"]), &[]).len(), 1);
assert!(calls.edges(&Thread::new("test", "t2"), &ids(&["c1"]), &[]).is_empty());
assert_eq!(calls.edges(&Thread::new("test", "t1"), &ids(&["c1"]), &[]).len(), 1);
}

#[test]
fn several_results_in_one_request_each_give_an_edge() {
// A reply may call several tools at once
let mut calls = Calls::default();
calls.edges("t1", &[], &ids(&["c1", "c2"]));
calls.edges(&Thread::new("test", "t1"), &[], &ids(&["c1", "c2"]));

let edges = calls.edges("t1", &ids(&["c1", "c2"]), &[]);
let edges = calls.edges(&Thread::new("test", "t1"), &ids(&["c1", "c2"]), &[]);

assert_eq!(pairs(&edges), [("call:c1", "result:c1"), ("call:c2", "result:c2")]);
}

#[test]
fn the_oldest_open_call_is_forgotten_past_the_bound() {
// It keeps at most CALL_BOUND open calls, so unanswered calls cannot grow without limit
fn the_oldest_open_call_is_forgotten_past_the_global_bound_across_adapters() {
// Both adapters share CALL_BOUND, so a new adapter does not multiply retained calls.
let mut calls = Calls::default();
let thread = |n| Thread::new(if n % 2 == 0 { "claude" } else { "codex" }, "t1");
for n in 0..=CALL_BOUND {
calls.edges("t1", &[], &[format!("c{n}")]);
calls.edges(&thread(n), &[], &[format!("c{n}")]);
}

assert!(calls.edges("t1", &ids(&["c0"]), &[]).is_empty());
assert_eq!(calls.edges("t1", &[format!("c{CALL_BOUND}")], &[]).len(), 1);
assert!(calls.edges(&thread(0), &ids(&["c0"]), &[]).is_empty());
assert_eq!(
calls.edges(&thread(CALL_BOUND), &[format!("c{CALL_BOUND}")], &[]).len(),
1
);
}

#[test]
fn answered_calls_do_not_make_the_order_list_grow_without_limit() {
// A long run of answered calls behind one unanswered call keeps memory bounded
let mut calls = Calls::default();
calls.edges("t1", &[], &ids(&["first"]));
calls.edges(&Thread::new("test", "t1"), &[], &ids(&["first"]));
for n in 0..CALL_BOUND * 4 {
calls.edges("t1", &[], &[format!("c{n}")]);
calls.edges("t1", &[format!("c{n}")], &[]);
calls.edges(&Thread::new("test", "t1"), &[], &[format!("c{n}")]);
calls.edges(&Thread::new("test", "t1"), &[format!("c{n}")], &[]);
}

assert!(calls.order.len() <= 2 * CALL_BOUND + 1);
assert_eq!(calls.edges("t1", &ids(&["first"]), &[]).len(), 1);
assert_eq!(calls.edges(&Thread::new("test", "t1"), &ids(&["first"]), &[]).len(), 1);
}
}
Loading
Loading