Skip to content
Python

fix(runtime): reject reused tool call IDs before dispatch

Rohit P444d209191ee
diff --git a/docs/harness/guides/tools.md b/docs/harness/guides/tools.md
index 49f6158f..ee65c69c 100644
--- a/docs/harness/guides/tools.md
+++ b/docs/harness/guides/tools.md
@@ -39,7 +39,7 @@ turn ID and call ID from either channel. Do not wait inside a callback for the U
the start event, and do not let a late start reset a completed entry. This also
works when reading saved events after the run has finished.
-Chat Completions history currently rejects reused provider call IDs before the
-next model request. A tool may already have completed when this happens; a failed
-turn does not mean its tool actions were undone. The correlation fields help your
-UI preserve that distinction.
+A provider response that reuses a call ID from retained model context is rejected
+before dispatching that new call. Earlier completed actions remain completed; a
+failed later turn does not undo them. The correlation fields help your UI
+preserve that distinction.
diff --git a/harness/runtime/src/model.rs b/harness/runtime/src/model.rs
index a1b9c27d..77cff84d 100644
--- a/harness/runtime/src/model.rs
+++ b/harness/runtime/src/model.rs
@@ -965,6 +965,26 @@ pub fn validate_output(output: &[Value]) -> Result<()> {
Ok(())
}
+/// A provider ID must not alias an earlier invocation in retained model context. Check
+/// before authorizing tools, not on the subsequent history replay after an
+/// action has already happened. Never interpret a repeated ID as a retry grant.
+pub(crate) fn validate_new_call_ids(history: &[Value], output: &[Value]) -> Result<()> {
+ let prior: std::collections::HashSet<_> = history
+ .iter()
+ .filter(|item| item["type"] == "function_call")
+ .filter_map(|item| item["call_id"].as_str())
+ .collect();
+ if output.iter().any(|item| {
+ item["type"] == "function_call"
+ && item["call_id"]
+ .as_str()
+ .is_some_and(|id| prior.contains(id))
+ }) {
+ return Err(crate::provider::Error::new(crate::provider::Failure::InvalidResponse).into());
+ }
+ Ok(())
+}
+
#[cfg(test)]
mod inbox_fixture_tests {
use super::*;
diff --git a/harness/runtime/src/runtime.rs b/harness/runtime/src/runtime.rs
index 68eebe57..016e55f5 100644
--- a/harness/runtime/src/runtime.rs
+++ b/harness/runtime/src/runtime.rs
@@ -780,6 +780,15 @@ impl Runtime {
}
}
}
+ if result.as_ref().is_some_and(|value| value.is_ok()) {
+ if let Some(response) = &completed {
+ if let Err(error) =
+ crate::model::validate_new_call_ids(history, &response.output)
+ {
+ result = Some(Err(error));
+ }
+ }
+ }
if let Some(u) = completed
.as_ref()
.and_then(|c: &crate::provider::Completion| c.usage.as_ref())
diff --git a/harness/runtime/src/runtime_budget_tests.rs b/harness/runtime/src/runtime_budget_tests.rs
index 3b24f82c..a7e16248 100644
--- a/harness/runtime/src/runtime_budget_tests.rs
+++ b/harness/runtime/src/runtime_budget_tests.rs
@@ -172,6 +172,56 @@ fn read_history_does_not_disguise_storage_or_session_failures() {
assert!(!result.to_string().contains("history excerpt"));
}
+#[tokio::test]
+async fn reused_provider_call_id_is_rejected_before_a_second_tool_action() {
+ let (_tmp, rt, sid) = fixture(model(|_, _| plan(0), 1), None).await;
+ rt.update(&sid, "test_tools_bound", json!({}), |s| {
+ s.schema_digest = Some("duplicate-id-test".into());
+ s.permissions.mode = Mode::Bypass;
+ Ok(())
+ })
+ .unwrap();
+ let error = rt
+ .drive(&sid, Default::default(), vec![crate::planning::schema()])
+ .await
+ .unwrap_err();
+ assert_eq!(
+ error.downcast_ref::<crate::provider::Error>().unwrap().kind,
+ crate::provider::Failure::InvalidResponse
+ );
+ let events = rt.store.lock().unwrap().events(&sid, 0).unwrap();
+ assert_eq!(
+ rt.store
+ .lock()
+ .unwrap()
+ .get(&sid)
+ .unwrap()
+ .plan
+ .unwrap()
+ .revision,
+ 1
+ );
+ assert_eq!(
+ events
+ .iter()
+ .filter(|event| event.kind == "plan_updated")
+ .count(),
+ 1
+ );
+ assert_eq!(
+ events
+ .iter()
+ .filter(|event| event.kind == "model_attempt_started")
+ .count(),
+ 2
+ );
+ assert!(events
+ .iter()
+ .any(|event| event.kind == "model_attempt_finished"
+ && event.data["status"] == "failed"
+ && event.data["kind"] == "invalid_response"));
+}
+
#[tokio::test]
async fn a_turn_can_finish_after_more_than_twenty_model_calls() {
let (_tmp, rt, sid) = fixture(
diff --git a/harness/typescript-sdk/test/openrouter-streaming.test.mjs b/harness/typescript-sdk/test/openrouter-streaming.test.mjs
index db0a77a7..c1b65647 100644
--- a/harness/typescript-sdk/test/openrouter-streaming.test.mjs
+++ b/harness/typescript-sdk/test/openrouter-streaming.test.mjs
@@ -198,45 +198,29 @@ test(
},
);
test(
- "Repeated provider call IDs remain distinguishable when strict history validation fails",
+ "Repeated provider call IDs are rejected before another tool can execute",
options,
async (t) => {
const { session, state } = await setup(t, "tool");
- const keys = [];
- for (let turn = 0; turn < 2; turn++) {
- const run = await session.send(`Show the preview for turn ${turn + 1}.`);
- const pending = await run.result();
- assert.equal(pending.status, "interrupted");
- await session.send({
- type: "approval",
- interactionId: pending.interruptions[0].id,
- decision: "once",
- });
- // Existing strict history validation rejects replay of the second
- // duplicate provider ID before sending another model request. The tool
- // has already executed: its UI entry must still belong to this turn,
- // and no callback should be replayed while handling the failed run.
- assert.equal((await run.result()).status, turn === 0 ? "completed" : "failed");
- const callback = state.callbackContexts[turn];
- assert.equal(callback.callId, "call-preview");
- assert.ok(callback.turnId);
- const key = JSON.stringify([callback.sessionId, callback.turnId, callback.callId]);
- keys.push(key);
- const kinds = [];
- // The terminal turn is replayed only after its callback already ran.
- for await (const item of run.events()) {
- if (["tool_started", "tool_finished"].includes(item.event.type)) {
- assert.equal(item.summary.callId, item.event.data.call_id);
- assert.equal(JSON.stringify([item.sessionId, item.event.turn_id, item.summary.callId]), key);
- kinds.push(item.event.type);
- }
- }
- assert.deepEqual(kinds, ["tool_started", "tool_finished"]);
- }
- assert.equal(state.effects, 2);
- assert.equal(state.requests, 3);
- assert.deepEqual(state.callbackIds, ["call-preview", "call-preview"]);
- assert.notEqual(keys[0], keys[1]);
+ const first = await session.send("Show the first preview.");
+ const pending = await first.result();
+ assert.equal(pending.status, "interrupted");
+ await session.send({
+ type: "approval", interactionId: pending.interruptions[0].id, decision: "once",
+ });
+ assert.equal((await first.result()).status, "completed");
+ assert.equal(state.effects, 1);
+ const second = await session.send("Show another preview.");
+ const result = await second.result();
+ assert.equal(result.status, "failed");
+ assert.deepEqual(result.interruptions, []);
+ const events = [];
+ for await (const item of second.events()) events.push(item.event);
+ assert.ok(events.some((event) => event.type === "model_attempt_finished" && event.data.status === "failed"));
+ assert.equal(events.filter((event) => event.type === "tool_started").length, 0);
+ assert.equal(state.effects, 1, "Do not dispatch an ambiguous invocation or replay the first one");
+ assert.equal(state.requests, 3, "Invalid output must not automatically retry");
+ assert.deepEqual(state.callbackIds, ["call-preview"]);
},
);
for (const mode of ["late-content", "conflicting-reason", "choice-error"])