Skip to content

Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.

fix: preserve native tool IDs across task continuations (#11)

澄潭6dd7e3ecbcb0
diff --git a/apps/desktop/src/features/AgentTasks.tsx b/apps/desktop/src/features/AgentTasks.tsx
index 76dd36ad..6d20a117 100644
--- a/apps/desktop/src/features/AgentTasks.tsx
+++ b/apps/desktop/src/features/AgentTasks.tsx
@@ -106,7 +106,7 @@ export function AgentTasks({
}, [active, read.status, selected?.taskId, selected?.runId]);
useEffect(() => {
- if (!active || !onReadTask || !selected?.detailsLoaded || !['queued', 'running', 'cancelling'].includes(selected.status)) return;
+ if (!active || !onReadTask || !selected?.detailsLoaded) return;
let disposed = false;
let timer: number | undefined;
const poll = async (current: AgentTask) => {
@@ -116,21 +116,22 @@ export function AgentTasks({
if (disposed) return;
setTasks(items => items.map(item => sameRun(item, current) ? hydrated : item));
setError('');
- if (['queued', 'running', 'cancelling'].includes(hydrated.status)) {
- timer = window.setTimeout(() => void poll(hydrated), 1800);
- }
+ // A completed run does not mean the task cannot be continued elsewhere.
+ timer = window.setTimeout(() => void poll(hydrated),
+ ['queued', 'running', 'cancelling'].includes(hydrated.status) ? 1800 : 4500);
} catch {
if (disposed) return;
setError(text('任务状态暂时无法刷新,将继续重试。', 'Task status could not be refreshed. Retrying.'));
timer = window.setTimeout(() => void poll(current), 4500);
}
};
- timer = window.setTimeout(() => void poll(selected), 1800);
+ timer = window.setTimeout(() => void poll(selected),
+ ['queued', 'running', 'cancelling'].includes(selected.status) ? 1800 : 4500);
return () => {
disposed = true;
if (timer !== undefined) window.clearTimeout(timer);
};
- }, [active, onReadTask, selected?.taskId, selected?.runId, selected?.detailsLoaded, selected?.status]);
+ }, [active, onReadTask, selected]);
const statusLabel = (status: AgentTask['status']) => ({
running: text('执行中', 'Running'),
queued: text('准备中', 'Preparing'),
diff --git a/apps/desktop/src/features/worker-task-client.ts b/apps/desktop/src/features/worker-task-client.ts
index 1cf8b101..51bd483b 100644
--- a/apps/desktop/src/features/worker-task-client.ts
+++ b/apps/desktop/src/features/worker-task-client.ts
@@ -221,6 +221,7 @@ export async function readWorkerTask(
locator: WorkerTaskLocator,
presentationFor: (planId: string) => WorkerTaskPresentation,
invokeWorker: WorkerInvoke = nativeInvoke,
+ previous?: AgentTask,
): Promise<AgentTask> {
const envelope = await invokeWorker<WorkerEnvelope<WorkerTaskStatus>>('worker_task_status', {
input: {
@@ -231,6 +232,17 @@ export async function readWorkerTask(
},
});
const task = envelopeData(envelope).task;
+ // Completed result content is immutable. Preserve already loaded pages while
+ // refreshing cleanup/latest-run facts; a Continue must fetch its own result.
+ if (previous?.detailsLoaded && previous.taskId === task.task_id
+ && previous.runId === task.run.run_id && previous.resultAvailable === task.run.result_available
+ && previous.contentAvailability === task.content_availability
+ && previous.status === workerTaskState(task.run.state)
+ && ['complete', 'failed', 'cancelled'].includes(previous.status)) {
+ return { ...projectTask(task, presentationFor, true, undefined, envelope.next_actions),
+ result: previous.result, resultAvailable: previous.resultAvailable,
+ resultIncomplete: previous.resultIncomplete, resultNextOffset: previous.resultNextOffset };
+ }
const result = await readResult(task.run.run_id, task.run.result_available, null, invokeWorker);
return projectTask(task, presentationFor, true, result, envelope.next_actions);
}
diff --git a/apps/desktop/src/product/DesktopApp.tsx b/apps/desktop/src/product/DesktopApp.tsx
index a299db3a..745e7510 100644
--- a/apps/desktop/src/product/DesktopApp.tsx
+++ b/apps/desktop/src/product/DesktopApp.tsx
@@ -414,8 +414,10 @@ export function DesktopApp() {
async function hydrateTask(task: AgentTask) {
return readWorkerTask(
- { taskId: task.taskId, runId: task.runId },
+ { taskId: task.taskId },
taskPresentation,
+ undefined,
+ task,
);
}
diff --git a/apps/desktop/tests/worker-task-client.test.mjs b/apps/desktop/tests/worker-task-client.test.mjs
index 2886cf74..3b2459fc 100644
--- a/apps/desktop/tests/worker-task-client.test.mjs
+++ b/apps/desktop/tests/worker-task-client.test.mjs
@@ -138,6 +138,45 @@ test('task list title truncation counts Unicode scalars', () => {
assert.equal(workerTaskDisplayTitle(title), `${'界'.repeat(59)}😀…`);
});
+test('refresh discovers an external Continue and never keeps the previous result', async () => {
+ const oldView = view('continued', { run: { ...view('continued').run, state: 'succeeded', result_available: true } });
+ const old = await readWorkerTask({ taskId: 'continued' }, presentation, async command =>
+ command === 'worker_task_status'
+ ? envelope({ task: oldView })
+ : envelope({ text: 'old result', next_offset: null, incomplete: false }));
+ const next = view('continued', { latest_run_id: 'run/new', run: { ...view('continued').run, run_id: 'run/new' } });
+ const refreshed = await readWorkerTask({ taskId: old.taskId }, presentation, async (command, args) => {
+ assert.equal(command, 'worker_task_status');
+ assert.equal(args.input.run_id, null);
+ return envelope({ task: next });
+ }, old);
+ assert.equal(refreshed.runId, 'run/new');
+ assert.equal(refreshed.status, 'running');
+ assert.notEqual(refreshed.result, 'old result');
+ assert.equal(refreshed.resultAvailable, false);
+});
+
+test('terminal refresh preserves loaded pages but refreshes cleanup and clears withdrawn content', async () => {
+ const task = view('finished', { run: { ...view('finished').run, state: 'succeeded', result_available: true } });
+ const previous = await readWorkerTask({ taskId: 'finished' }, presentation, async command =>
+ command === 'worker_task_status' ? envelope({ task })
+ : envelope({ text: 'page one', next_offset: 10, incomplete: true }));
+ previous.result = 'page one plus page two';
+ previous.resultNextOffset = 20;
+ const refreshed = await readWorkerTask({ taskId: 'finished' }, presentation, async command => {
+ assert.equal(command, 'worker_task_status');
+ return envelope({ task: { ...task, run: { ...task.run, cleanup: 'complete' } } });
+ }, previous);
+ assert.equal(refreshed.result, previous.result);
+ assert.equal(refreshed.resultNextOffset, 20);
+ const withdrawn = await readWorkerTask({ taskId: 'finished' }, presentation, async command => {
+ assert.equal(command, 'worker_task_status');
+ return envelope({ task: { ...task, content_availability: 'unavailable', run: { ...task.run, result_available: false } } });
+ }, refreshed);
+ assert.equal(withdrawn.resultAvailable, false);
+ assert.notEqual(withdrawn.result, previous.result);
+});
+
test('same-name tasks remain separate and a hidden title falls back to task identity', async () => {
const pager = new WorkerTaskPager(presentation, async () => envelope({
schema: 'hiroute.delegation-list/v1',
diff --git a/crates/daemon/src/delegation/profile/mod.rs b/crates/daemon/src/delegation/profile/mod.rs
index 246cfbee..158c0749 100644
--- a/crates/daemon/src/delegation/profile/mod.rs
+++ b/crates/daemon/src/delegation/profile/mod.rs
@@ -128,6 +128,9 @@ impl CandidateWorkerProfile {
};
let mut env = BTreeMap::new();
// This is a new child environment, never merged with std::env::vars().
+ // Tool discovery is not authentication. Keep an explicit command search
+ // path while leaving ambient model credentials and configuration behind.
+ env.insert("PATH".into(), Zeroizing::new(worker_path(&input)?));
env.insert(
"HOME".into(),
Zeroizing::new(path_string(&private_root.join("home"))?),
@@ -344,5 +347,46 @@ fn path_string(path: &Path) -> Result<String, DelegationErrorV1> {
.ok_or(DelegationErrorV1::InvalidArguments)
}
+fn worker_path(input: &ProfileInput<'_>) -> Result<String, DelegationErrorV1> {
+ let mut paths = Vec::new();
+ for executable in [
+ Some(input.harness_binary),
+ input.node_binary,
+ Some(input.adapter),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ if let Some(parent) = executable.parent() {
+ paths.push(parent.to_owned());
+ }
+ }
+ if let Some(path) = std::env::var_os("PATH") {
+ paths.extend(std::env::split_paths(&path).filter(|path| path.is_absolute()));
+ }
+ #[cfg(unix)]
+ paths.extend(
+ [
+ "/opt/homebrew/bin",
+ "/usr/local/bin",
+ "/usr/bin",
+ "/bin",
+ "/usr/sbin",
+ "/sbin",
+ ]
+ .map(PathBuf::from),
+ );
+ let mut unique = Vec::new();
+ for path in paths {
+ if !unique.contains(&path) {
+ unique.push(path);
+ }
+ }
+ std::env::join_paths(unique)
+ .map_err(|_| DelegationErrorV1::InvalidArguments)?
+ .into_string()
+ .map_err(|_| DelegationErrorV1::InvalidArguments)
+}
+
#[cfg(test)]
mod tests;
diff --git a/crates/daemon/src/delegation/profile/tests.rs b/crates/daemon/src/delegation/profile/tests.rs
index d57bfdd5..3b58c39e 100644
--- a/crates/daemon/src/delegation/profile/tests.rs
+++ b/crates/daemon/src/delegation/profile/tests.rs
@@ -1,5 +1,36 @@
use super::*;
+#[cfg(unix)]
+#[test]
+fn isolated_worker_profiles_can_resolve_standard_tools() {
+ for harness in [WorkerHarnessV1::ClaudeCode, WorkerHarnessV1::CodexCli] {
+ let profile = request(harness, "private-token");
+ let path = profile.env.get("PATH").expect("worker PATH");
+ let entries: Vec<_> = std::env::split_paths(path.as_str()).collect();
+ assert!(entries.iter().all(|entry| entry.is_absolute()));
+ assert!(entries.contains(&std::path::PathBuf::from("/trusted")));
+ assert_eq!(
+ entries
+ .iter()
+ .collect::<std::collections::BTreeSet<_>>()
+ .len(),
+ entries.len()
+ );
+ let output = std::process::Command::new("/bin/sh")
+ .env_clear()
+ .envs(
+ profile
+ .env
+ .iter()
+ .map(|(key, value)| (key.as_str(), value.as_str())),
+ )
+ .args(["-c", "test -n \"$PATH\" && command -v sh && command -v env"])
+ .output()
+ .unwrap();
+ assert!(output.status.success(), "{:?}", output);
+ }
+}
+
fn request(harness: WorkerHarnessV1, token: &str) -> CandidateWorkerProfile {
request_with_alias(harness, token, "hiroute/1234567890abcdef")
}
diff --git a/crates/gateway/src/adapters/continuation.rs b/crates/gateway/src/adapters/continuation.rs
index 98d67608..20c7ea9d 100644
--- a/crates/gateway/src/adapters/continuation.rs
+++ b/crates/gateway/src/adapters/continuation.rs
@@ -1,500 +1,356 @@
-//! Trusted Tool continuation issuance and downstream acceptance bridge.
-
-use std::fmt;
+//! Stateless tool IDs and request-local delivery bookkeeping.
+use super::ProtocolAdapterError;
+use crate::server::core_runtime::model_ir::{ExactProviderPathV1, ModelIrError};
+use crate::server::request_plan::IngressProtocol;
+use sha2::{Digest, Sha256};
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
-use std::sync::{Arc, OnceLock};
+use std::sync::{Arc, Mutex};
use std::time::Instant;
-use sha2::{Digest, Sha256};
-
-use crate::ports::{
- ToolContinuationAuthority, ToolContinuationIssuanceV1, ToolContinuationScopeV1,
-};
-use crate::server::core_runtime::model_ir::{
- ExactProviderPathV1, ModelIrError, ToolIdMapEntryV1, ToolKindV1,
-};
-
-use super::ProtocolAdapterError;
-
-const HMAC_BLOCK_BYTES: usize = 64;
-const TOOL_ID_PREFIX: &str = "hiroute_tool_v1_";
-const TOOL_ID_DIGEST_BYTES: usize = 32;
-const TOOL_ID_DIGEST_HEX_BYTES: usize = TOOL_ID_DIGEST_BYTES * 2;
-const TOOL_ID_BYTES: usize = TOOL_ID_PREFIX.len() + TOOL_ID_DIGEST_HEX_BYTES;
-const TOOL_ID_DOMAIN: &[u8] = b"hiroute/tool-continuation-logical-id/v2";
-
-static PROCESS_TOOL_ID_KEY: OnceLock<[u8; 32]> = OnceLock::new();
-
-#[derive(Clone)]
-pub(crate) struct ActiveToolContinuation {
- authority: Arc<dyn ToolContinuationAuthority>,
- issuance: ToolContinuationIssuanceV1,
- provider_states: Option<crate::provider_state::ActiveProviderStates>,
- accepted_count: Arc<AtomicUsize>,
-}
-
-impl ActiveToolContinuation {
- pub(crate) fn new(
- authority: Arc<dyn ToolContinuationAuthority>,
- issuance: ToolContinuationIssuanceV1,
- ) -> Self {
- Self {
- authority,
- issuance,
- provider_states: None,
- accepted_count: Arc::new(AtomicUsize::new(0)),
- }
- }
-
- pub(crate) fn with_provider_states(
- mut self,
- states: crate::provider_state::ActiveProviderStates,
- ) -> Self {
- self.provider_states = Some(states);
- self
+/// CPA-style deterministic adaptation, only across protocols. A digest avoids
+/// the collisions introduced by replacing arbitrary characters with '_'.
+pub(crate) fn project_tool_id(
+ id: &str,
+ from: IngressProtocol,
+ to: IngressProtocol,
+) -> Result<String, ModelIrError> {
+ if id.trim().is_empty() {
+ return Err(ModelIrError::InvalidField("tool id"));
}
-
- pub(crate) fn scanner(&self) -> AcceptedToolContinuationScanner {
- AcceptedToolContinuationScanner {
- authority: Arc::clone(&self.authority),
- issuance: self.issuance.clone(),
- carry: Vec::with_capacity(TOOL_ID_BYTES.saturating_sub(1)),
- provider_states: self.provider_states.as_ref().map(|states| states.scanner()),
- accepted_count: Arc::clone(&self.accepted_count),
- }
+ if from == to {
+ return Ok(id.to_owned());
}
-
- pub(crate) fn accepted_count(&self) -> usize {
- self.accepted_count.load(Ordering::Acquire)
+ let invalid = to == IngressProtocol::Messages
+ && !id
+ .bytes()
+ .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-');
+ let too_long = to == IngressProtocol::Responses && id.len() > 64;
+ if !invalid && !too_long {
+ return Ok(id.to_owned());
}
-
- /// Returns a request-bound, projection-only capability for off-path
- /// canonical decoding. It can reproduce this issuance's opaque logical
- /// IDs, but carries no authority port and therefore cannot record,
- /// activate, resolve, or abort continuation mappings.
- pub(crate) fn logical_id_projection(
- &self,
- ) -> Result<ToolLogicalIdProjection, ProtocolAdapterError> {
- let key = PROCESS_TOOL_ID_KEY
- .get()
- .copied()
- .ok_or(ModelIrError::ToolContinuationUnavailable)?;
- Ok(ToolLogicalIdProjection {
- key,
- scope: self.issuance.scope().clone(),
+ let prefix: String = id
+ .chars()
+ .take(31)
+ .map(|c| {
+ if c.is_ascii_alphanumeric() || c == '_' || c == '-' {
+ c
+ } else {
+ '_'
+ }
})
- }
-
- pub(crate) fn abort_pending(&self) {
- self.authority.abort_pending(&self.issuance);
- }
+ .collect();
+ let digest = Sha256::digest(id.as_bytes());
+ let suffix: String = digest[..16].iter().map(|b| format!("{b:02x}")).collect();
+ Ok(format!("{prefix}_{suffix}"))
}
-/// Non-escalating capability used only to reconstruct the same request-bound
-/// logical Tool identity in an asynchronous canonical observation decoder.
-/// The process key remains private and is redacted from Debug output.
-#[derive(Clone)]
-pub(crate) struct ToolLogicalIdProjection {
- key: [u8; 32],
- scope: ToolContinuationScopeV1,
+#[derive(Clone, Debug)]
+pub(crate) struct ToolIdProjection {
+ downstream: IngressProtocol,
}
-
-impl ToolLogicalIdProjection {
- // Keep every identity component explicit at this authority boundary; an
- // omitted kind/namespace/name would permit cross-tool ID aliasing.
- #[allow(clippy::too_many_arguments)]
+impl ToolIdProjection {
+ pub(crate) fn new(downstream: IngressProtocol) -> Self {
+ Self { downstream }
+ }
pub(crate) fn project(
&self,
- response_id: &str,
- index: u32,
native_id: &str,
- kind: ToolKindV1,
- namespace: Option<&str>,
- name: &str,
owner: &ExactProviderPathV1,
) -> Result<String, ProtocolAdapterError> {
- let physical_binding = serde_json::to_vec(owner)
- .map_err(|error| ProtocolAdapterError::Serialization(error.to_string()))?;
- let mut hmac = ToolIdHmac::new(&self.key, TOOL_ID_DOMAIN);
- hmac.update(self.scope.authority_id.as_bytes());
- hmac.update(&self.scope.authority_epoch.to_be_bytes());
- hmac.update(self.scope.grant_id.as_bytes());
- hmac.update(&self.scope.grant_generation.to_be_bytes());
- hmac.update(self.scope.served_model_id.as_bytes());
- let route = serde_json::to_vec(&self.scope.route)
- .map_err(|error| ProtocolAdapterError::Serialization(error.to_string()))?;
- hmac.update(&route);
- hmac.update(response_id.as_bytes());
- hmac.update(&index.to_be_bytes());
- hmac.update(native_id.as_bytes());
- hmac.update(match kind {
- ToolKindV1::Function => b"function",
- ToolKindV1::Custom => b"custom",
- });
- match namespace {
- Some(namespace) => {
- hmac.update(b"namespace-some");
- hmac.update(namespace.as_bytes());
- }
- None => hmac.update(b"namespace-none"),
- }
- hmac.update(name.as_bytes());
- hmac.update(&physical_binding);
- Ok(format!("{TOOL_ID_PREFIX}{:x}", hmac.finish()))
- }
-
- #[cfg(test)]
- pub(crate) fn for_test(key: [u8; 32], scope: ToolContinuationScopeV1) -> Self {
- Self { key, scope }
- }
-}
-
-impl fmt::Debug for ToolLogicalIdProjection {
- fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
- formatter
- .debug_struct("ToolLogicalIdProjection")
- .field("scope", &self.scope)
- .field("key", &"[REDACTED]")
- .finish()
+ Ok(project_tool_id(
+ native_id,
+ owner.upstream_protocol,
+ self.downstream,
+ )?)
}
}
-tokio::task_local! {
- static ACTIVE_TOOL_CONTINUATION: ActiveToolContinuation;
-}
-
-pub(crate) async fn with_active_tool_continuation<F>(
- continuation: ActiveToolContinuation,
- future: F,
-) -> F::Output
-where
- F: Future,
-{
- ACTIVE_TOOL_CONTINUATION.scope(continuation, future).await
+#[derive(Clone)]
+pub(crate) struct ActiveResponseDelivery {
+ projection: ToolIdProjection,
+ provider_states: crate::provider_state::ActiveProviderStates,
+ pending_tools: Arc<Mutex<Vec<Vec<u8>>>>,
+ accepted_count: Arc<AtomicUsize>,
}
-
-/// Installs one non-exportable process-boot secret. A restart therefore loses
-/// both the authority entries and the ability to reproduce old logical IDs.
-pub(crate) fn install_process_tool_id_codec() -> Result<(), ()> {
- if PROCESS_TOOL_ID_KEY.get().is_some() {
- return Ok(());
+impl ActiveResponseDelivery {
+ pub(crate) fn new(
+ downstream: IngressProtocol,
+ provider_states: crate::provider_state::ActiveProviderStates,
+ ) -> Self {
+ Self {
+ projection: ToolIdProjection::new(downstream),
+ provider_states,
+ pending_tools: Default::default(),
+ accepted_count: Default::default(),
+ }
+ }
+ pub(crate) fn scanner(&self) -> AcceptedResponseDeliveryScanner {
+ AcceptedResponseDeliveryScanner {
+ active: self.clone(),
+ carry: Vec::new(),
+ provider_states: self.provider_states.scanner(),
+ }
+ }
+ pub(crate) fn accepted_count(&self) -> usize {
+ self.accepted_count.load(Ordering::Acquire)
}
- let mut key = [0_u8; 32];
- getrandom::fill(&mut key).map_err(|_| ())?;
- if key.iter().all(|byte| *byte == 0) {
- return Err(());
+ pub(crate) fn tool_id_projection(&self) -> ToolIdProjection {
+ self.projection.clone()
}
- let _ = PROCESS_TOOL_ID_KEY.set(key);
- Ok(())
}
-
-pub(super) fn issue_logical_tool_id(
- response_id: &str,
- index: u32,
+tokio::task_local! { static ACTIVE_RESPONSE_DELIVERY: ActiveResponseDelivery; }
+pub(crate) async fn with_active_response_delivery<F: Future>(
+ active: ActiveResponseDelivery,
+ future: F,
+) -> F::Output {
+ ACTIVE_RESPONSE_DELIVERY.scope(active, future).await
+}
+pub(super) fn project_delivered_tool_id(
native_id: &str,
- kind: ToolKindV1,
- namespace: Option<&str>,
- name: &str,
owner: &ExactProviderPathV1,
) -> Result<String, ProtocolAdapterError> {
- let active = ACTIVE_TOOL_CONTINUATION.try_with(Clone::clone).ok();
- let logical_id = match (PROCESS_TOOL_ID_KEY.get(), active.as_ref()) {
- (Some(_), Some(active)) => active.logical_id_projection()?.project(
- response_id,
- index,
- native_id,
- kind,
- namespace,
- name,
- owner,
- )?,
- // Direct adapter golden tests intentionally do not install production
- // process authority and retain their stable historical IDs.
- (None, None) => format!("hiroute_tool_{index}"),
- // A production runtime without both pieces of trusted request context
- // must not fall back to a client-guessable continuation identity.
- (Some(_), None) | (None, Some(_)) => {
- return Err(ModelIrError::ToolContinuationUnavailable.into());
- }
+ let active = ACTIVE_RESPONSE_DELIVERY.try_with(Clone::clone).ok();
+ let Some(active) = active else {
+ return Ok(native_id.to_owned());
+ };
+ let id = active.projection.project(native_id, owner)?;
+ let field = match active.projection.downstream {
+ IngressProtocol::Responses => "call_id",
+ _ => "id",
};
- if let Some(active) = active {
- active
- .authority
- .record_pending(
- &active.issuance,
- ToolIdMapEntryV1 {
- logical_id: logical_id.clone(),
- native_id: native_id.into(),
- kind,
- name: name.into(),
- namespace: namespace.map(str::to_owned),
- owner: owner.clone(),
- },
- Instant::now(),
- )
- .map_err(|_| ModelIrError::ToolContinuationUnavailable)?;
+ let pattern = format!(
+ "{}:{}",
+ serde_json::to_string(field).unwrap(),
+ serde_json::to_string(&id)
+ .map_err(|e| ProtocolAdapterError::Serialization(e.to_string()))?
+ )
+ .into_bytes();
+ let mut pending = active
+ .pending_tools
+ .lock()
+ .unwrap_or_else(|e| e.into_inner());
+ if !pending.contains(&pattern) {
+ if pending.len() >= 128
+ || pending.iter().map(Vec::len).sum::<usize>() + pattern.len() > 256 * 1024
+ {
+ return Err(ModelIrError::BufferLimit(256 * 1024).into());
+ }
+ pending.push(pattern);
}
- Ok(logical_id)
+ Ok(id)
}
-
pub(super) fn record_provider_state(
value: &serde_json::Value,
owner: &ExactProviderPathV1,
) -> Result<(), ProtocolAdapterError> {
- ACTIVE_TOOL_CONTINUATION
+ ACTIVE_RESPONSE_DELIVERY
+ .try_with(|active| active.provider_states.record(value, owner))
+ .unwrap_or(Ok(()))
+ .map_err(Into::into)
+}
+pub(super) fn record_provider_state_at_acceptance(
+ state: &str,
+ owner: &ExactProviderPathV1,
+ closing_event: &[u8],
+) -> Result<(), ProtocolAdapterError> {
+ ACTIVE_RESPONSE_DELIVERY
.try_with(|active| {
active
.provider_states
- .as_ref()
- .map_or(Ok(()), |states| states.record(value, owner))
+ .record_at_acceptance(state, owner, closing_event.to_vec())
})
.unwrap_or(Ok(()))
.map_err(Into::into)
}
-
-struct ToolIdHmac {
- inner: Sha256,
- outer_pad: [u8; HMAC_BLOCK_BYTES],
-}
-
-impl ToolIdHmac {
- fn new(key: &[u8; 32], domain: &[u8]) -> Self {
- let mut key_block = [0_u8; HMAC_BLOCK_BYTES];
- key_block[..key.len()].copy_from_slice(key);
- let mut inner_pad = [0x36_u8; HMAC_BLOCK_BYTES];
- let mut outer_pad = [0x5c_u8; HMAC_BLOCK_BYTES];
- for ((inner, outer), key) in inner_pad
- .iter_mut()
- .zip(outer_pad.iter_mut())
- .zip(key_block)
- {
- *inner ^= key;
- *outer ^= key;
- }
- let mut inner = Sha256::new();
- inner.update(inner_pad);
- inner.update((domain.len() as u64).to_be_bytes());
- inner.update(domain);
- Self { inner, outer_pad }
- }
-
- fn update(&mut self, bytes: &[u8]) {
- self.inner.update((bytes.len() as u64).to_be_bytes());
- self.inner.update(bytes);
- }
-
- fn finish(self) -> impl std::fmt::LowerHex {
- let inner = self.inner.finalize();
- let mut outer = Sha256::new();
- outer.update(self.outer_pad);
- outer.update(inner);
- outer.finalize()
- }
-}
-
-/// Incremental O(n) scanner over bytes already accepted by the downstream
-/// transport. It recognizes only opaque IDs minted by this process, and the
-/// authority independently verifies that each ID is pending for this exact
-/// request issuance.
-pub(crate) struct AcceptedToolContinuationScanner {
- authority: Arc<dyn ToolContinuationAuthority>,
- issuance: ToolContinuationIssuanceV1,
+/// Delivery facts only; no later request queries these patterns.
+pub(crate) struct AcceptedResponseDeliveryScanner {
+ active: ActiveResponseDelivery,
carry: Vec<u8>,
- provider_states: Option<crate::provider_state::AcceptedProviderStateScanner>,
- accepted_count: Arc<AtomicUsize>,
+ provider_states: crate::provider_state::AcceptedProviderStateScanner,
}
-
-impl AcceptedToolContinuationScanner {
+impl AcceptedResponseDeliveryScanner {
pub(crate) fn accept_bytes(&mut self, bytes: &[u8], now: Instant) {
- if let Some(states) = &mut self.provider_states {
- states.accept_bytes(bytes, now);
- }
- let boundary_bytes = bytes.len().min(TOOL_ID_BYTES.saturating_sub(1));
- let mut boundary = Vec::with_capacity(self.carry.len().saturating_add(boundary_bytes));
- boundary.extend_from_slice(&self.carry);
- boundary.extend_from_slice(&bytes[..boundary_bytes]);
- self.accept_complete_ids(&boundary, now);
- self.accept_complete_ids(bytes, now);
-
- if bytes.len() >= TOOL_ID_BYTES.saturating_sub(1) {
- self.carry.clear();
- self.carry
- .extend_from_slice(&bytes[bytes.len() - (TOOL_ID_BYTES - 1)..]);
- } else {
- boundary.extend_from_slice(&bytes[boundary_bytes..]);
- let retained = TOOL_ID_BYTES.saturating_sub(1).min(boundary.len());
- self.carry.clear();
- self.carry
- .extend_from_slice(&boundary[boundary.len() - retained..]);
- }
- }
-
- fn accept_complete_ids(&self, bytes: &[u8], now: Instant) {
- let prefix = TOOL_ID_PREFIX.as_bytes();
- let mut offset: usize = 0;
- while offset.saturating_add(TOOL_ID_BYTES) <= bytes.len() {
- let Some(relative) = bytes[offset..]
- .windows(prefix.len())
- .position(|window| window == prefix)
- else {
- break;
- };
- let start = offset + relative;
- let end = start + TOOL_ID_BYTES;
- if end > bytes.len() {
- break;
+ self.provider_states.accept_bytes(bytes, now);
+ let mut pending = self
+ .active
+ .pending_tools
+ .lock()
+ .unwrap_or_else(|e| e.into_inner());
+ let keep = pending
+ .iter()
+ .map(|p| p.len().saturating_sub(1))
+ .max()
+ .unwrap_or(0);
+ let mut boundary = self.carry.clone();
+ boundary.extend_from_slice(&bytes[..bytes.len().min(keep)]);
+ pending.retain(|pattern| {
+ let accepted = bytes.windows(pattern.len()).any(|w| w == pattern)
+ || boundary.windows(pattern.len()).any(|w| w == pattern);
+ if accepted {
+ self.active.accepted_count.fetch_add(1, Ordering::AcqRel);
}
- let candidate = &bytes[start..end];
- if candidate[prefix.len()..].iter().all(u8::is_ascii_hexdigit)
- && let Ok(logical_id) = std::str::from_utf8(candidate)
- && self.authority.accept(&self.issuance, logical_id, now)
- {
- self.accepted_count.fetch_add(1, Ordering::AcqRel);
- }
- offset = start.saturating_add(1);
+ !accepted
+ });
+ if bytes.len() >= keep {
+ self.carry = bytes[bytes.len() - keep..].to_vec();
+ } else {
+ self.carry.extend_from_slice(bytes);
+ let drop = self.carry.len().saturating_sub(keep);
+ self.carry.drain(..drop);
}
}
}
-
#[cfg(test)]
mod tests {
- use std::time::Duration;
-
use super::*;
- use crate::ports::{InMemoryToolContinuationAuthority, ToolContinuationScopeV1};
- use crate::server::request_plan::IngressProtocol;
+ use crate::server::core_runtime::adapters::{
+ NativeResponseDecoder, decode_ingress_request, project_candidate_request,
+ };
+ use crate::server::core_runtime::profiles::{CandidateProtocolProfile, fixed_reasoning};
+ use serde_json::json;
- fn scope() -> ToolContinuationScopeV1 {
- ToolContinuationScopeV1 {
- authority_id: "authority".into(),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "agent".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 2,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
+ #[test]
+ fn repeated_native_ids_preserve_each_result_without_history_pairing() {
+ let document = json!({"model":"alias","input":[
+ {"type":"function_call","call_id":"same","name":"lookup","arguments":"{}"},
+ {"type":"function_call_output","call_id":"same","output":"first"},
+ {"type":"function_call","call_id":"same","name":"lookup","arguments":"{}"},
+ {"type":"function_call_output","call_id":"same","output":"second"}
+ ]});
+ let request = decode_ingress_request(IngressProtocol::Responses, &document).unwrap();
+ for target in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let profile = CandidateProtocolProfile::exact_portable_path(
+ IngressProtocol::Responses,
+ target,
+ "candidate",
+ fixed_reasoning("fixed"),
+ );
+ let wire = project_candidate_request(&request, &profile).unwrap().body;
+ if target == IngressProtocol::Responses {
+ assert_eq!(wire["input"], document["input"]);
+ } else {
+ assert_eq!(wire["messages"][1]["content"][0]["content"], "first");
+ assert_eq!(wire["messages"][3]["content"][0]["content"], "second");
+ assert_eq!(wire["messages"][3]["content"][0]["tool_use_id"], "same");
+ }
}
+ // The wire item defines its kind; a same-ID call does not override it.
+ let result_only = json!({"model":"alias","input":[
+ {"type":"function_call","call_id":"same","namespace":"group","name":"lookup","arguments":"{}"},
+ {"type":"custom_tool_call_output","call_id":"same","output":"native output"},
+ {"type":"function_call_output","call_id":"unknown","output":"standalone"}
+ ]});
+ let request = decode_ingress_request(IngressProtocol::Responses, &result_only).unwrap();
+ let profile = CandidateProtocolProfile::exact_portable_path(
+ IngressProtocol::Responses,
+ IngressProtocol::Responses,
+ "candidate",
+ fixed_reasoning("fixed"),
+ );
+ assert_eq!(
+ project_candidate_request(&request, &profile).unwrap().body["input"],
+ result_only["input"]
+ );
}
- fn owner() -> ExactProviderPathV1 {
- ExactProviderPathV1 {
- provider_id: "provider".into(),
- endpoint_id: "endpoint".into(),
- entitlement_id: "entitlement".into(),
- connector_id: "connector".into(),
- connector_revision: "1".into(),
- capability_id: "capability".into(),
- capability_revision: "1".into(),
- model_configuration_id: "configuration".into(),
- native_model: "native".into(),
- upstream_protocol: IngressProtocol::Responses,
- adapter_revision: "1".into(),
- serializer_revision: "1".into(),
- decoder_revision: "1".into(),
+ #[test]
+ fn tool_history_pairs_without_owner_and_rejects_projected_collisions() {
+ let id = "call.with-punctuation";
+ let projected_id =
+ project_tool_id(id, IngressProtocol::Responses, IngressProtocol::Messages).unwrap();
+ let mut document = json!({"model":"alias","input":[
+ {"type":"function_call","call_id":id,"name":"lookup","arguments":"{}"},
+ {"type":"function_call_output","call_id":id,"output":"done"}
+ ]});
+ let request = decode_ingress_request(IngressProtocol::Responses, &document).unwrap();
+ for model in ["first-candidate", "different-candidate"] {
+ let profile = CandidateProtocolProfile::exact_portable_path(
+ IngressProtocol::Responses,
+ IngressProtocol::Messages,
+ model,
+ fixed_reasoning("fixed"),
+ );
+ let wire = project_candidate_request(&request, &profile).unwrap().body;
+ assert_eq!(wire["messages"][0]["content"][0]["id"], projected_id);
+ assert_eq!(
+ wire["messages"][1]["content"][0]["tool_use_id"],
+ projected_id
+ );
}
+ document["input"].as_array_mut().unwrap().push(json!({
+ "type":"function_call", "call_id":projected_id, "name":"lookup", "arguments":"{}"
+ }));
+ let request = decode_ingress_request(IngressProtocol::Responses, &document).unwrap();
+ let profile = CandidateProtocolProfile::exact_portable_path(
+ IngressProtocol::Responses,
+ IngressProtocol::Messages,
+ "candidate",
+ fixed_reasoning("fixed"),
+ );
+ assert_eq!(
+ project_candidate_request(&request, &profile).unwrap_err(),
+ ProtocolAdapterError::ModelIr(ModelIrError::ToolContinuationConflict)
+ );
}
#[test]
- fn logical_ids_are_opaque_stable_and_owner_affine() {
- let key = [7_u8; 32];
- let projection = ToolLogicalIdProjection::for_test(key, scope());
- let first = projection
- .project(
- "response",
- 1,
- "native",
- ToolKindV1::Function,
- None,
- "weather",
- &owner(),
- )
- .unwrap();
- let repeated = projection
- .project(
- "response",
- 1,
- "native",
- ToolKindV1::Function,
- None,
- "weather",
- &owner(),
- )
- .unwrap();
- let changed = projection
- .project(
- "response",
- 1,
- "native-2",
- ToolKindV1::Function,
- None,
- "weather",
- &owner(),
- )
- .unwrap();
- let namespaced = projection
- .project(
- "response",
- 1,
- "native",
- ToolKindV1::Function,
- Some("group"),
- "weather",
- &owner(),
- )
- .unwrap();
- let mut other_scope = scope();
- other_scope.grant_id = "other-grant".into();
- let other_scope = ToolLogicalIdProjection::for_test(key, other_scope)
- .project(
- "response",
- 1,
- "native",
- ToolKindV1::Function,
- None,
- "weather",
- &owner(),
- )
+ fn response_long_id_is_stateless_and_returned_history_keeps_the_pair() {
+ let id = "native".repeat(20);
+ let profile = CandidateProtocolProfile::exact_portable_path(
+ IngressProtocol::Responses,
+ IngressProtocol::ChatCompletions,
+ "candidate",
+ fixed_reasoning("fixed"),
+ );
+ let body = json!({"id":"response","model":"candidate","choices":[{
+ "index":0,"message":{"role":"assistant","content":null,"tool_calls":[{
+ "id":id,"type":"function","function":{"name":"lookup","arguments":"{}"}
+ }]},"finish_reason":"tool_calls"
+ }]});
+ let mut decoder = NativeResponseDecoder::new(&profile, 200, false).unwrap();
+ decoder
+ .feed(&serde_json::to_vec(&body).unwrap(), true)
.unwrap();
- assert_eq!(first, repeated);
- assert_ne!(first, changed);
- assert_ne!(first, namespaced);
- assert_ne!(first, other_scope);
- assert!(first.starts_with(TOOL_ID_PREFIX));
- assert_eq!(first.len(), TOOL_ID_BYTES);
+ let response = decoder.finish().unwrap();
+ let wire_id = &response.response.tool_id_map[0].logical_id;
+ assert_eq!(wire_id.len(), 64);
+ let request = decode_ingress_request(IngressProtocol::Responses, &json!({"model":"alias",
+ "tools":[{"type":"function","name":"lookup","parameters":{"type":"object"}}],
+ "input":[{"type":"function_call","call_id":wire_id,"name":"lookup","arguments":"{}"},
+ {"type":"function_call_output","call_id":wire_id,"output":"done"}]
+ })).unwrap();
+ let wire = project_candidate_request(&request, &profile).unwrap().body;
+ assert_eq!(wire["messages"][0]["tool_calls"][0]["id"], *wire_id);
+ assert_eq!(wire["messages"][1]["tool_call_id"], *wire_id);
}
-
#[test]
- fn accepted_scanner_activates_only_complete_fragmented_pending_ids() {
- let authority: Arc<dyn ToolContinuationAuthority> =
- Arc::new(InMemoryToolContinuationAuthority::new(4, Duration::from_secs(1)).unwrap());
- let issuance = authority.begin(scope()).unwrap();
- let logical_id = format!("{TOOL_ID_PREFIX}{}", "a".repeat(TOOL_ID_DIGEST_HEX_BYTES));
- let mapping = ToolIdMapEntryV1 {
- logical_id: logical_id.clone(),
- native_id: "provider-native".into(),
- kind: ToolKindV1::Function,
- name: "weather".into(),
- namespace: None,
- owner: owner(),
- };
- let now = Instant::now();
- authority
- .record_pending(&issuance, mapping.clone(), now)
- .unwrap();
- let active = ActiveToolContinuation::new(Arc::clone(&authority), issuance.clone());
- let mut scanner = active.scanner();
- let wire = format!("data: {{\"call_id\":\"{logical_id}\"}}\n\n");
- for chunk in wire.as_bytes().chunks(3) {
- scanner.accept_bytes(chunk, now);
+ fn native_id_and_stateless_cross_protocol_adaptation() {
+ use IngressProtocol::*;
+ for protocol in [Responses, Messages, ChatCompletions] {
+ assert_eq!(
+ project_tool_id("native-call-1", protocol, protocol).unwrap(),
+ "native-call-1"
+ );
+ assert_eq!(
+ project_tool_id("native-call-1", protocol, Responses).unwrap(),
+ "native-call-1"
+ );
}
+ let long = "x".repeat(100);
+ let shortened = project_tool_id(&long, Messages, Responses).unwrap();
+ assert_eq!(shortened.len(), 64);
+ assert_eq!(
+ project_tool_id(&shortened, Messages, Responses).unwrap(),
+ shortened
+ );
+ assert_ne!(
+ project_tool_id("call.a", Responses, Messages).unwrap(),
+ project_tool_id("call a", Responses, Messages).unwrap()
+ );
assert_eq!(
- authority
- .resolve(issuance.scope(), &[logical_id], now)
- .unwrap(),
- vec![mapping]
+ project_tool_id("call.a", Responses, Responses).unwrap(),
+ "call.a"
);
}
}
diff --git a/crates/gateway/src/adapters/cpa_responses_tests.rs b/crates/gateway/src/adapters/cpa_responses_tests.rs
index b5eb5fec..bd271efd 100644
--- a/crates/gateway/src/adapters/cpa_responses_tests.rs
+++ b/crates/gateway/src/adapters/cpa_responses_tests.rs
@@ -168,7 +168,6 @@ fn assert_replay_to_owner(input: Vec<Value>) {
&json!({"model":"hiroute-fixture","input":input}),
&IngressRequestBindings {
provider_state_owner: Some(profile.exact_provider_path().unwrap()),
- tool_id_map: Vec::new(),
},
)
.unwrap();
@@ -300,7 +299,6 @@ fn native_input_status_preserves_function_output_lifecycle_only() {
&json!({"model":"alias","input":[item]}),
&IngressRequestBindings {
provider_state_owner: Some(profile.exact_provider_path().unwrap()),
- tool_id_map: Vec::new(),
},
);
if status == "completed" {
@@ -337,7 +335,6 @@ fn native_input_status_preserves_function_output_lifecycle_only() {
&json!({"model":"alias","input":[item]}),
&IngressRequestBindings {
provider_state_owner: Some(profile.exact_provider_path().unwrap()),
- tool_id_map: Vec::new(),
},
)
.unwrap();
@@ -362,7 +359,6 @@ fn native_input_status_preserves_function_output_lifecycle_only() {
}]}),
&IngressRequestBindings {
provider_state_owner: Some(profile.exact_provider_path().unwrap()),
- tool_id_map: Vec::new(),
},
);
assert!(decoded.is_err(), "accepted {status}");
diff --git a/crates/gateway/src/adapters/ingress.rs b/crates/gateway/src/adapters/ingress.rs
index d33fa02f..c308f5e9 100644
--- a/crates/gateway/src/adapters/ingress.rs
+++ b/crates/gateway/src/adapters/ingress.rs
@@ -4,11 +4,8 @@ use responses::{decode_responses_input, decode_responses_tools};
#[path = "ingress/continuation.rs"]
mod continuation;
-pub use continuation::{
- IngressRequestBindings, decode_ingress_request_with_state_and_tool_resolver,
- decode_ingress_request_with_tool_resolver,
-};
-use continuation::{bind_tool_continuations, validate_bindings};
+pub use continuation::IngressRequestBindings;
+use continuation::validate_bindings;
use std::collections::BTreeSet;
@@ -41,7 +38,7 @@ pub fn decode_ingress_request_with_bindings(
}) {
return Err(ModelIrError::ProviderStateNotPortable);
}
- let mut request = match protocol {
+ let request = match protocol {
IngressProtocol::Responses => {
decode_responses(body, bindings.provider_state_owner.as_ref())
}
@@ -50,9 +47,6 @@ pub fn decode_ingress_request_with_bindings(
}
IngressProtocol::Messages => decode_messages(body, bindings.provider_state_owner.as_ref()),
}?;
- if !bindings.tool_id_map.is_empty() {
- bind_tool_continuations(&mut request, bindings.tool_id_map.clone())?;
- }
Ok(request)
}
@@ -244,7 +238,6 @@ fn decode_responses(
.unwrap_or_else(RequestedReasoningControl::absent),
requested_max_output_tokens: optional_u64(object, "max_output_tokens")?,
provider_state,
- tool_id_map: Vec::new(),
})
}
@@ -313,7 +306,6 @@ fn decode_chat(
requested_reasoning,
requested_max_output_tokens: compatible_max_output(object)?,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
})
}
@@ -377,7 +369,6 @@ fn decode_chat_message(
content: vec![ContentPart::ToolResult {
logical_id: required_string(object, "tool_call_id")?,
tool_kind: ToolKindV1::Function,
- namespace: None,
output: decode_tool_output(
object
.get("content")
@@ -560,7 +551,6 @@ fn decode_messages(
requested_reasoning,
requested_max_output_tokens: optional_u64(object, "max_tokens")?,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
})
}
@@ -661,7 +651,6 @@ fn decode_messages_content(
Ok(ContentPart::ToolResult {
logical_id: required_string(object, "tool_use_id")?,
tool_kind: ToolKindV1::Function,
- namespace: None,
output: decode_tool_output(
object
.get("content")
diff --git a/crates/gateway/src/adapters/ingress/continuation.rs b/crates/gateway/src/adapters/ingress/continuation.rs
index 9afdd521..3ed3bf88 100644
--- a/crates/gateway/src/adapters/ingress/continuation.rs
+++ b/crates/gateway/src/adapters/ingress/continuation.rs
@@ -1,140 +1,9 @@
-use std::collections::BTreeSet;
+use crate::server::core_runtime::model_ir::{ExactProviderPathV1, ModelIrError};
-use serde_json::Value;
-
-use crate::server::core_runtime::model_ir::{
- ContentPart, ExactProviderPathV1, ModelIrError, ModelRequestIRV1, ToolIdMapEntryV1,
-};
-use crate::server::request_plan::IngressProtocol;
-
-use super::{decode_ingress_request, decode_ingress_request_with_bindings};
-
-/// Request-owned continuation facts recovered from the accepted logical
-/// request scope. Raw client JSON is never allowed to invent physical owner
-/// identity or native Tool IDs.
+/// Only opaque provider state needs trusted, source-specific ownership.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct IngressRequestBindings {
pub provider_state_owner: Option<ExactProviderPathV1>,
- pub tool_id_map: Vec<ToolIdMapEntryV1>,
-}
-
-/// Canonical ingress decode with request-scoped trusted Tool authority. The
-/// caller sees either a fully bound request or an error; an unbound
-/// continuation IR never escapes this adapter seam.
-pub fn decode_ingress_request_with_tool_resolver<F>(
- protocol: IngressProtocol,
- body: &Value,
- resolve: F,
-) -> Result<ModelRequestIRV1, ModelIrError>
-where
- F: FnOnce(&[String]) -> Result<Vec<ToolIdMapEntryV1>, ModelIrError>,
-{
- decode_ingress_request_with_state_and_tool_resolver(protocol, body, None, resolve)
-}
-
-pub fn decode_ingress_request_with_state_and_tool_resolver<F>(
- protocol: IngressProtocol,
- body: &Value,
- provider_state_owner: Option<ExactProviderPathV1>,
- resolve: F,
-) -> Result<ModelRequestIRV1, ModelIrError>
-where
- F: FnOnce(&[String]) -> Result<Vec<ToolIdMapEntryV1>, ModelIrError>,
-{
- let initial = IngressRequestBindings {
- provider_state_owner: provider_state_owner.clone(),
- tool_id_map: Vec::new(),
- };
- let mut request = if provider_state_owner.is_some() {
- decode_ingress_request_with_bindings(protocol, body, &initial)?
- } else {
- decode_ingress_request(protocol, body)?
- };
- let logical_ids = request.continuation_logical_ids()?;
- if logical_ids.is_empty() {
- return Ok(request);
- }
- let tool_id_map = resolve(&logical_ids)?;
- let bindings = IngressRequestBindings {
- provider_state_owner,
- tool_id_map,
- };
- validate_bindings(&bindings)?;
- let resolved_ids = bindings
- .tool_id_map
- .iter()
- .map(|mapping| mapping.logical_id.as_str())
- .collect::<BTreeSet<_>>();
- if bindings.tool_id_map.len() != logical_ids.len()
- || resolved_ids.len() != logical_ids.len()
- || logical_ids
- .iter()
- .any(|logical_id| !resolved_ids.contains(logical_id.as_str()))
- {
- return Err(ModelIrError::ToolContinuationUnavailable);
- }
- bind_tool_continuations(&mut request, bindings.tool_id_map)?;
- Ok(request)
-}
-
-pub(super) fn bind_tool_continuations(
- request: &mut ModelRequestIRV1,
- mappings: Vec<ToolIdMapEntryV1>,
-) -> Result<(), ModelIrError> {
- let by_logical_id = mappings
- .iter()
- .map(|mapping| (mapping.logical_id.as_str(), mapping))
- .collect::<std::collections::BTreeMap<_, _>>();
- for part in request
- .instructions
- .iter_mut()
- .flat_map(|instruction| instruction.content.iter_mut())
- .chain(
- request
- .messages
- .iter_mut()
- .flat_map(|message| message.content.iter_mut()),
- )
- {
- match part {
- ContentPart::ToolCall {
- logical_id,
- tool_kind,
- namespace,
- name,
- ..
- } => {
- let mapping = by_logical_id
- .get(logical_id.as_str())
- .ok_or(ModelIrError::ToolContinuationUnavailable)?;
- if mapping.kind != *tool_kind
- || mapping.name != *name
- || mapping.namespace != *namespace
- {
- return Err(ModelIrError::ToolContinuationConflict);
- }
- }
- ContentPart::ToolResult {
- logical_id,
- tool_kind,
- namespace,
- ..
- } => {
- let mapping = by_logical_id
- .get(logical_id.as_str())
- .ok_or(ModelIrError::ToolContinuationUnavailable)?;
- if mapping.kind != *tool_kind {
- return Err(ModelIrError::ToolContinuationConflict);
- }
- *namespace = mapping.namespace.clone();
- }
- ContentPart::Text { .. }
- | ContentPart::Image { .. }
- | ContentPart::ProviderState { .. } => {}
- }
- }
- request.tool_id_map = mappings;
- Ok(())
}
pub(super) fn validate_bindings(bindings: &IngressRequestBindings) -> Result<(), ModelIrError> {
@@ -145,25 +14,5 @@ pub(super) fn validate_bindings(bindings: &IngressRequestBindings) -> Result<(),
{
return Err(ModelIrError::ProviderStateOwnershipRequired);
}
- let mut seen = Vec::new();
- for binding in &bindings.tool_id_map {
- if binding.logical_id.trim().is_empty()
- || binding.native_id.trim().is_empty()
- || binding.name.trim().is_empty()
- || binding
- .namespace
- .as_ref()
- .is_some_and(|namespace| namespace.trim().is_empty())
- || !binding.owner.is_complete()
- || seen.iter().any(|(logical_id, owner)| {
- logical_id == &binding.logical_id && owner == &binding.owner
- })
- {
- return Err(ModelIrError::ToolIdBindingRequired(
- binding.logical_id.clone(),
- ));
- }
- seen.push((binding.logical_id.clone(), binding.owner.clone()));
- }
Ok(())
}
diff --git a/crates/gateway/src/adapters/mod.rs b/crates/gateway/src/adapters/mod.rs
index 4aca108f..d85bc595 100644
--- a/crates/gateway/src/adapters/mod.rs
+++ b/crates/gateway/src/adapters/mod.rs
@@ -9,12 +9,11 @@ mod tool_projection;
pub(crate) use tool_projection::{ChatToolIdentity, ChatToolProjection};
pub(crate) use continuation::{
- AcceptedToolContinuationScanner, ActiveToolContinuation, ToolLogicalIdProjection,
- install_process_tool_id_codec, with_active_tool_continuation,
+ AcceptedResponseDeliveryScanner, ActiveResponseDelivery, ToolIdProjection,
+ with_active_response_delivery,
};
pub use ingress::{
IngressRequestBindings, decode_ingress_request, decode_ingress_request_with_bindings,
- decode_ingress_request_with_state_and_tool_resolver, decode_ingress_request_with_tool_resolver,
};
pub use request::{
PreparedNativeRequest, PreparedNativeTemplate, project_candidate_request,
diff --git a/crates/gateway/src/adapters/namespace_tests.rs b/crates/gateway/src/adapters/namespace_tests.rs
index ce46e8fa..d0761918 100644
--- a/crates/gateway/src/adapters/namespace_tests.rs
+++ b/crates/gateway/src/adapters/namespace_tests.rs
@@ -1,7 +1,6 @@
use serde_json::{Value, json};
use super::*;
-use crate::ports::ToolContinuationScopeV1;
use crate::server::core_runtime::model_ir::{ContentPart, ResponseBlock, ToolKindV1};
use crate::server::core_runtime::profiles::{
CandidateProtocolProfile, ClientProtocolProfile, fixed_reasoning,
@@ -16,20 +15,7 @@ fn responses_namespaced_tool_calls_roundtrip_trusted_namespace_and_native_ids()
"physical",
fixed_reasoning("fixed"),
);
- let projection = ToolLogicalIdProjection::for_test(
- [13_u8; 32],
- ToolContinuationScopeV1 {
- authority_id: "authority".into(),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "agent".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 2,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
- },
- );
+ let projection = ToolIdProjection::new(IngressProtocol::Responses);
let mut decoder =
NativeResponseDecoder::new_for_observation(&profile, 200, false, projection, None).unwrap();
decoder
@@ -117,18 +103,12 @@ fn responses_namespaced_tool_calls_roundtrip_trusted_namespace_and_native_ids()
{"type":"function_call_output","call_id":calls[1].0,"output":"done-b"}
]
});
- let request =
- decode_ingress_request_with_tool_resolver(IngressProtocol::Responses, &continued, |ids| {
- assert_eq!(ids.len(), 2);
- Ok(decoded.response.tool_id_map.clone())
- })
- .unwrap();
+ let request = decode_ingress_request(IngressProtocol::Responses, &continued).unwrap();
assert_eq!(
request.messages[1].content[0],
ContentPart::ToolResult {
logical_id: calls[0].0.clone(),
tool_kind: crate::server::core_runtime::model_ir::ToolKindV1::Function,
- namespace: Some("group-a".into()),
output: crate::server::core_runtime::model_ir::ToolOutput::Text("done-a".into()),
status: crate::server::core_runtime::model_ir::ToolResultStatusV1::Failed,
}
@@ -151,20 +131,7 @@ fn responses_namespaced_stream_keeps_one_identity_through_added_delta_done_and_c
"physical",
fixed_reasoning("fixed"),
);
- let projection = ToolLogicalIdProjection::for_test(
- [19_u8; 32],
- ToolContinuationScopeV1 {
- authority_id: "authority".into(),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "agent".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 2,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
- },
- );
+ let projection = ToolIdProjection::new(IngressProtocol::Responses);
let mut decoder =
NativeResponseDecoder::new_for_observation(&profile, 200, true, projection, None).unwrap();
let status = decoder
@@ -223,7 +190,7 @@ data: {"type":"response.completed","sequence_number":5,"response":{"id":"respons
}
});
assert_eq!(started_id, finished_id);
- assert!(started_id.unwrap().starts_with("hiroute_tool_v1_"));
+ assert_eq!(started_id.unwrap(), "native");
assert_eq!(decoded.response.tool_id_map.len(), 1);
assert_eq!(
decoded.response.tool_id_map[0].namespace.as_deref(),
@@ -250,12 +217,7 @@ data: {"type":"response.completed","sequence_number":5,"response":{"id":"respons
for snapshot in tool_snapshots {
assert_eq!(snapshot.data["item"]["namespace"], "generic-group");
assert_eq!(snapshot.data["item"]["name"], "generic-child");
- assert!(
- snapshot.data["item"]["call_id"]
- .as_str()
- .unwrap()
- .starts_with("hiroute_tool_v1_")
- );
+ assert_eq!(snapshot.data["item"]["call_id"], "native");
}
assert_eq!(
rendered.last().unwrap().event.as_deref(),
@@ -286,20 +248,7 @@ fn chat_projection_reverses_namespaced_function_and_custom_calls_for_responses_c
"physical",
fixed_reasoning("fixed"),
);
- let tool_id_projection = ToolLogicalIdProjection::for_test(
- [23_u8; 32],
- ToolContinuationScopeV1 {
- authority_id: "authority".into(),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "agent".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 2,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
- },
- );
+ let tool_id_projection = ToolIdProjection::new(IngressProtocol::Responses);
let mut decoder = NativeResponseDecoder::new_for_observation(
&profile,
200,
@@ -389,11 +338,7 @@ fn chat_projection_reverses_namespaced_function_and_custom_calls_for_responses_c
{"type":"custom","name":"shell","description":"run a command"}
]
});
- let continued =
- decode_ingress_request_with_tool_resolver(IngressProtocol::Responses, &continued, |_| {
- Ok(decoded.response.tool_id_map.clone())
- })
- .unwrap();
+ let continued = decode_ingress_request(IngressProtocol::Responses, &continued).unwrap();
let projected = project_candidate_request(&continued, &profile).unwrap();
assert_eq!(projected.body["messages"].as_array().unwrap().len(), 3);
assert_eq!(
diff --git a/crates/gateway/src/adapters/request.rs b/crates/gateway/src/adapters/request.rs
index 5c92d47f..b66ac542 100644
--- a/crates/gateway/src/adapters/request.rs
+++ b/crates/gateway/src/adapters/request.rs
@@ -223,14 +223,7 @@ fn serialize_responses(
};
let mut item = json!({
"type": match tool_kind { ToolKindV1::Function => "function_call", ToolKindV1::Custom => "custom_tool_call" },
- "call_id": native_tool_call_id(
- request,
- profile,
- logical_id,
- *tool_kind,
- namespace.as_deref(),
- name,
- )?,
+ "call_id": native_tool_id(request, profile, logical_id)?,
"name": name,
});
item[match tool_kind {
@@ -251,7 +244,6 @@ fn serialize_responses(
ContentPart::ToolResult {
logical_id,
tool_kind,
- namespace,
output,
..
} => {
@@ -269,13 +261,7 @@ fn serialize_responses(
false,
json!({
"type": match tool_kind { ToolKindV1::Function => "function_call_output", ToolKindV1::Custom => "custom_tool_call_output" },
- "call_id": native_tool_result_id(
- request,
- profile,
- logical_id,
- *tool_kind,
- namespace.as_deref(),
- )?,
+ "call_id": native_tool_id(request, profile, logical_id)?,
"output": render_tool_output(output)?,
}),
)?);
@@ -495,17 +481,13 @@ fn serialize_chat(
.expect("emitted name came from this projection");
let chat_arguments = ChatToolProjection::chat_arguments(identity, arguments)?;
tool_calls.push(json!({
- "id": native_tool_call_id(request, profile, logical_id, *tool_kind, namespace.as_deref(), name)?,
+ "id": native_tool_id(request, profile, logical_id)?,
"type": "function",
"function": {"name": emitted_name, "arguments": compact_json(&chat_arguments)?},
}));
}
ContentPart::ToolResult {
- logical_id,
- tool_kind,
- namespace,
- output,
- ..
+ logical_id, output, ..
} => {
if message.content.len() != 1 || message.role != MessageRole::User {
return Err(ProtocolAdapterError::ClientUnrepresentable(
@@ -516,13 +498,7 @@ fn serialize_chat(
object.insert("role".into(), Value::String("tool".into()));
object.insert(
"tool_call_id".into(),
- Value::String(native_tool_result_id(
- request,
- profile,
- logical_id,
- *tool_kind,
- namespace.as_deref(),
- )?),
+ Value::String(native_tool_id(request, profile, logical_id)?),
);
object.insert("content".into(), render_tool_output(output)?);
if let Some(name) = &message.name {
@@ -664,7 +640,7 @@ fn serialize_messages(
reject_namespace(namespace, IngressProtocol::Messages)?;
content.push(json!({
"type": "tool_use",
- "id": native_tool_call_id(request, profile, logical_id, *tool_kind, None, name)?,
+ "id": native_tool_id(request, profile, logical_id)?,
"name": name,
"input": arguments.wire_value(),
}));
@@ -672,7 +648,6 @@ fn serialize_messages(
ContentPart::ToolResult {
logical_id,
tool_kind,
- namespace,
output,
status,
} => {
@@ -681,10 +656,9 @@ fn serialize_messages(
"Messages cannot express custom tool results".into(),
));
}
- reject_namespace(namespace, IngressProtocol::Messages)?;
let mut tool_result = json!({
"type": "tool_result",
- "tool_use_id": native_tool_result_id(request, profile, logical_id, *tool_kind, None)?,
+ "tool_use_id": native_tool_id(request, profile, logical_id)?,
"content": render_tool_output(output)?,
});
if *status == ToolResultStatusV1::Failed {
@@ -727,6 +701,7 @@ fn validate_message_shapes(
request: &ModelRequestIRV1,
target: IngressProtocol,
) -> Result<(), ProtocolAdapterError> {
+ let mut projected = std::collections::BTreeMap::new();
if (!request.responses_annotations.is_empty()
|| !request.responses_search_history.is_empty()
|| request.web_search.is_some()
@@ -794,6 +769,22 @@ fn validate_message_shapes(
let mut native_items = 0_usize;
let mut message_parts = 0_usize;
for part in &message.content {
+ if request.ingress_protocol != target
+ && let ContentPart::ToolCall { logical_id, .. }
+ | ContentPart::ToolResult { logical_id, .. } = part
+ {
+ let target_id = super::continuation::project_tool_id(
+ logical_id,
+ request.ingress_protocol,
+ target,
+ )?;
+ if projected
+ .insert(target_id, logical_id)
+ .is_some_and(|original| original != logical_id)
+ {
+ return Err(ModelIrError::ToolContinuationConflict.into());
+ }
+ }
match part {
ContentPart::Text { .. } => message_parts += 1,
ContentPart::Image { .. } => {
@@ -1031,68 +1022,13 @@ fn ensure_state_owner(
fn native_tool_id(
request: &ModelRequestIRV1,
profile: &CandidateProtocolProfile,
- logical_id: &str,
+ id: &str,
) -> Result<String, ProtocolAdapterError> {
- let owner = profile.exact_provider_path()?;
- let mut matches = request
- .tool_id_map
- .iter()
- .filter(|binding| binding.logical_id == logical_id && binding.owner == owner);
- let binding = matches
- .next()
- .ok_or_else(|| ModelIrError::ToolIdBindingRequired(logical_id.into()))?;
- if binding.native_id.trim().is_empty() || matches.next().is_some() {
- return Err(ModelIrError::ToolIdBindingRequired(logical_id.into()).into());
- }
- Ok(binding.native_id.clone())
-}
-
-fn native_tool_call_id(
- request: &ModelRequestIRV1,
- profile: &CandidateProtocolProfile,
- logical_id: &str,
- kind: ToolKindV1,
- namespace: Option<&str>,
- name: &str,
-) -> Result<String, ProtocolAdapterError> {
- let binding = native_tool_binding(request, profile, logical_id)?;
- if binding.kind != kind || binding.name != name || binding.namespace.as_deref() != namespace {
- return Err(ModelIrError::ToolContinuationConflict.into());
- }
- Ok(binding.native_id.clone())
-}
-
-fn native_tool_result_id(
- request: &ModelRequestIRV1,
- profile: &CandidateProtocolProfile,
- logical_id: &str,
- kind: ToolKindV1,
- namespace: Option<&str>,
-) -> Result<String, ProtocolAdapterError> {
- let binding = native_tool_binding(request, profile, logical_id)?;
- if binding.kind != kind || binding.namespace.as_deref() != namespace {
- return Err(ModelIrError::ToolContinuationConflict.into());
- }
- Ok(binding.native_id.clone())
-}
-
-fn native_tool_binding<'a>(
- request: &'a ModelRequestIRV1,
- profile: &CandidateProtocolProfile,
- logical_id: &str,
-) -> Result<&'a ToolIdMapEntryV1, ProtocolAdapterError> {
- let owner = profile.exact_provider_path()?;
- let mut matches = request
- .tool_id_map
- .iter()
- .filter(|binding| binding.logical_id == logical_id && binding.owner == owner);
- let binding = matches
- .next()
- .ok_or_else(|| ModelIrError::ToolIdBindingRequired(logical_id.into()))?;
- if binding.native_id.trim().is_empty() || matches.next().is_some() {
- return Err(ModelIrError::ToolIdBindingRequired(logical_id.into()).into());
- }
- Ok(binding)
+ Ok(super::continuation::project_tool_id(
+ id,
+ request.ingress_protocol,
+ profile.capability.upstream_protocol,
+ )?)
}
fn reject_namespace(
diff --git a/crates/gateway/src/adapters/response/decoder.rs b/crates/gateway/src/adapters/response/decoder.rs
index 303797b2..e4cec098 100644
--- a/crates/gateway/src/adapters/response/decoder.rs
+++ b/crates/gateway/src/adapters/response/decoder.rs
@@ -56,7 +56,15 @@ impl NativeResponseDecoder {
status: u16,
streaming: bool,
) -> Result<Self, ProtocolAdapterError> {
- Self::new_with_projections(profile, status, streaming, None, None)
+ Self::new_with_projections(
+ profile,
+ status,
+ streaming,
+ Some(super::super::continuation::ToolIdProjection::new(
+ profile.ingress_protocol,
+ )),
+ None,
+ )
}
pub(crate) fn new_for_attempt(
@@ -75,7 +83,7 @@ impl NativeResponseDecoder {
profile: &CandidateProtocolProfile,
status: u16,
streaming: bool,
- tool_id_projection: super::super::continuation::ToolLogicalIdProjection,
+ tool_id_projection: super::super::continuation::ToolIdProjection,
chat_tool_projection: Option<ChatToolProjection>,
) -> Result<Self, ProtocolAdapterError> {
Self::new_with_projections(
@@ -91,7 +99,7 @@ impl NativeResponseDecoder {
profile: &CandidateProtocolProfile,
status: u16,
streaming: bool,
- tool_id_projection: Option<super::super::continuation::ToolLogicalIdProjection>,
+ tool_id_projection: Option<super::super::continuation::ToolIdProjection>,
chat_tool_projection: Option<ChatToolProjection>,
) -> Result<Self, ProtocolAdapterError> {
if profile.capability.upstream_protocol != profile.connector.upstream_protocol
diff --git a/crates/gateway/src/adapters/response/mod.rs b/crates/gateway/src/adapters/response/mod.rs
index 0a513944..00e3ee57 100644
--- a/crates/gateway/src/adapters/response/mod.rs
+++ b/crates/gateway/src/adapters/response/mod.rs
@@ -17,24 +17,10 @@ mod wire;
mod native_passthrough_tests;
#[cfg(test)]
-pub(super) fn test_tool_projection() -> super::continuation::ToolLogicalIdProjection {
+pub(super) fn test_tool_projection() -> super::continuation::ToolIdProjection {
// Production installs the process key before requests. Golden response tests must supply
// their own request-bound projection so parallel runtime tests cannot change their result.
- super::continuation::install_process_tool_id_codec().unwrap();
- super::continuation::ToolLogicalIdProjection::for_test(
- [7_u8; 32],
- crate::ports::ToolContinuationScopeV1 {
- authority_id: "test-authority".into(),
- authority_epoch: 1,
- grant_id: "test-grant".into(),
- grant_generation: 1,
- served_model_id: "alias".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 1,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"response-test"),
- },
- },
- )
+ super::continuation::ToolIdProjection::new(IngressProtocol::Responses)
}
pub use decoder::{DecodedNativeResponse, NativeResponseDecoder, ResponseDecodeStatus};
@@ -111,7 +97,7 @@ enum NativeBlockKind {
#[derive(Clone, Debug)]
struct DecoderCore {
owner: ExactProviderPathV1,
- tool_id_projection: Option<super::continuation::ToolLogicalIdProjection>,
+ tool_id_projection: Option<super::continuation::ToolIdProjection>,
provider_state_emission: NativeProviderStateEmission,
accumulator: ResponseAccumulator,
native_blocks: BTreeMap<(NativeBlockKind, u32), u32>,
@@ -131,7 +117,7 @@ impl DecoderCore {
fn new(
owner: ExactProviderPathV1,
provider_state_emission: NativeProviderStateEmission,
- tool_id_projection: Option<super::continuation::ToolLogicalIdProjection>,
+ tool_id_projection: Option<super::continuation::ToolIdProjection>,
) -> Self {
Self {
owner,
@@ -1068,32 +1054,22 @@ impl DecoderCore {
}) {
return Err(ModelIrError::MissingToolIdentity(native_id).into());
}
- let response_id = self.accumulator.response_id.as_deref().ok_or_else(|| {
- ModelIrError::InvalidResponseLifecycle(
+ if self.accumulator.response_id.is_none() {
+ return Err(ModelIrError::InvalidResponseLifecycle(
"Tool call arrived before native response identity".into(),
)
- })?;
+ .into());
+ }
let logical_id = if let Some(projection) = &self.tool_id_projection {
- projection.project(
- response_id,
- index,
- &native_id,
- kind,
- namespace.as_deref(),
- &name,
- &self.owner,
- )?
+ projection.project(&native_id, &self.owner)?
} else {
- super::continuation::issue_logical_tool_id(
- response_id,
- index,
- &native_id,
- kind,
- namespace.as_deref(),
- &name,
- &self.owner,
- )?
+ super::continuation::project_delivered_tool_id(&native_id, &self.owner)?
};
+ if self.accumulator.blocks.values().any(|block| {
+ matches!(block, MutableResponseBlock::ToolCall { logical_id: current, .. } if current == &logical_id)
+ }) {
+ return Err(ModelIrError::ToolContinuationConflict.into());
+ }
let item_id = self.native_item_id(native_index);
self.emit(
ModelEvent::ToolCallStarted {
diff --git a/crates/gateway/src/adapters/response/native_passthrough.rs b/crates/gateway/src/adapters/response/native_passthrough.rs
index 93bcf47a..acb545ac 100644
--- a/crates/gateway/src/adapters/response/native_passthrough.rs
+++ b/crates/gateway/src/adapters/response/native_passthrough.rs
@@ -19,7 +19,7 @@ use crate::server::core_runtime::profiles::CandidateProtocolProfile;
use crate::server::request_plan::IngressProtocol;
use super::super::continuation::{
- ToolLogicalIdProjection, issue_logical_tool_id, record_provider_state,
+ ToolIdProjection, project_delivered_tool_id, record_provider_state,
};
use super::super::{ChatToolIdentity, ChatToolProjection, ProtocolAdapterError};
@@ -28,6 +28,8 @@ mod helpers;
use helpers::*;
#[path = "native_passthrough_evidence.rs"]
mod evidence;
+#[path = "native_passthrough_state.rs"]
+mod provider_state;
const MAX_NATIVE_BODY_BYTES: usize = 16 * 1024 * 1024;
const MAX_SSE_EVENT_BYTES: usize = 256 * 1024;
@@ -73,7 +75,7 @@ impl NativeResponseProjector {
streaming,
served_model_alias,
chat_projection,
- ToolAuthority::Active,
+ ToolIdDelivery::Active,
budget,
)
}
@@ -83,7 +85,7 @@ impl NativeResponseProjector {
streaming: bool,
served_model_alias: String,
chat_projection: Option<ChatToolProjection>,
- tool_projection: ToolLogicalIdProjection,
+ tool_projection: ToolIdProjection,
) -> Result<Self, ProtocolAdapterError> {
// Capture already reserves its own bounded input/output copies. This
// local framer budget is off-path and cannot grant execution authority.
@@ -97,7 +99,7 @@ impl NativeResponseProjector {
streaming,
served_model_alias,
chat_projection,
- ToolAuthority::Projection(tool_projection),
+ ToolIdDelivery::Projection(tool_projection),
budget,
)
}
@@ -107,7 +109,7 @@ impl NativeResponseProjector {
streaming: bool,
served_model_alias: String,
chat_projection: Option<ChatToolProjection>,
- authority: ToolAuthority,
+ authority: ToolIdDelivery,
budget: StreamBudget,
) -> Result<Self, ProtocolAdapterError> {
if profile.ingress_protocol != profile.capability.upstream_protocol
@@ -158,6 +160,7 @@ impl NativeResponseProjector {
response_deltas: BTreeMap::new(),
response_items_uncertain: false,
reasoning_state: BTreeMap::new(),
+ messages_signatures: BTreeMap::new(),
responses_done_seen: false,
usage: ModelUsage::default(),
usage_input_overflow: false,
@@ -243,30 +246,20 @@ impl NativeResponseProjector {
}
#[derive(Clone)]
-enum ToolAuthority {
+enum ToolIdDelivery {
Active,
- Projection(ToolLogicalIdProjection),
+ Projection(ToolIdProjection),
}
-impl ToolAuthority {
- #[allow(clippy::too_many_arguments)]
- fn issue(
+impl ToolIdDelivery {
+ fn project(
&self,
- response_id: &str,
- index: u32,
native_id: &str,
- kind: ToolKindV1,
- namespace: Option<&str>,
- name: &str,
owner: &ExactProviderPathV1,
) -> Result<String, ProtocolAdapterError> {
match self {
- Self::Active => {
- issue_logical_tool_id(response_id, index, native_id, kind, namespace, name, owner)
- }
- Self::Projection(projection) => {
- projection.project(response_id, index, native_id, kind, namespace, name, owner)
- }
+ Self::Active => project_delivered_tool_id(native_id, owner),
+ Self::Projection(projection) => projection.project(native_id, owner),
}
}
@@ -295,7 +288,7 @@ pub(super) struct ProjectionState {
pub(super) protocol: IngressProtocol,
alias: String,
owner: ExactProviderPathV1,
- authority: ToolAuthority,
+ authority: ToolIdDelivery,
chat_projection: Option<ChatToolProjection>,
response_id: Option<String>,
native_model: Option<String>,
@@ -305,6 +298,7 @@ pub(super) struct ProjectionState {
response_deltas: BTreeMap<evidence::ResponseDeltaKey, evidence::ResponseDeltaEvidence>,
response_items_uncertain: bool,
reasoning_state: BTreeMap<u32, [u8; 32]>,
+ messages_signatures: BTreeMap<u32, String>,
responses_done_seen: bool,
usage: ModelUsage,
usage_input_overflow: bool,
@@ -342,6 +336,7 @@ impl ProjectionState {
&mut self,
event_type: Option<&str>,
data: &[u8],
+ raw_event: &[u8],
) -> Result<(Option<Vec<u8>>, ProjectionMetadata), ProtocolAdapterError> {
if self.protocol == IngressProtocol::Responses && data == b"[DONE]" {
if event_type.is_some() || self.terminal.is_none() || self.responses_done_seen {
@@ -391,9 +386,14 @@ impl ProjectionState {
Err(error) => return Err(ModelIrError::InvalidJson(error.to_string()).into()),
};
let object = value.as_object_mut().expect("checked JSON object");
+ let tools_before = self.tools.len();
let mut metadata = match self.protocol {
IngressProtocol::Responses => self.project_responses_sse(event_type, object)?,
- IngressProtocol::Messages => self.project_messages_sse(event_type, object)?,
+ IngressProtocol::Messages => {
+ let metadata = self.project_messages_sse(event_type, object)?;
+ self.observe_messages_state(object, raw_event)?;
+ metadata
+ }
IngressProtocol::ChatCompletions => self.project_chat_sse(object)?,
};
if after_terminal {
@@ -404,7 +404,10 @@ impl ProjectionState {
let changed = value
!= serde_json::from_slice::<Value>(data)
.map_err(|error| ModelIrError::InvalidJson(error.to_string()))?;
- let rewritten = changed
+ // Tool delivery bookkeeping matches the serialized ID field only after
+ // transport acceptance. Normalize this known frame's JSON syntax even
+ // when the native ID itself is unchanged (including escaped IDs).
+ let rewritten = (changed || self.tools.len() != tools_before)
.then(|| serde_json::to_vec(&value))
.transpose()
.map_err(|error| ProtocolAdapterError::Serialization(error.to_string()))?;
@@ -692,6 +695,14 @@ impl ProjectionState {
block,
)?;
}
+ if block.get("type").and_then(Value::as_str) == Some("thinking")
+ && let Some(signature) = block
+ .get("signature")
+ .filter(|value| value.as_str().is_some_and(|s| !s.is_empty()))
+ && self.authority.records_state()
+ {
+ record_provider_state(signature, &self.owner)?;
+ }
}
}
let terminal = object
@@ -983,19 +994,17 @@ impl ProjectionState {
.budget
.reserve(MemoryRole::SemanticState, retained)
.map_err(|_| ModelIrError::BufferLimit(retained))?;
- let response_id = self
- .response_id
+ self.response_id
.as_deref()
.ok_or_else(|| ModelIrError::MissingToolIdentity("response id".into()))?;
- let logical_id = self.authority.issue(
- response_id,
- index,
- native_id,
- kind,
- namespace,
- name,
- &self.owner,
- )?;
+ let logical_id = self.authority.project(native_id, &self.owner)?;
+ if self
+ .tools
+ .values()
+ .any(|tool| tool.logical_id == logical_id)
+ {
+ return Err(ModelIrError::ToolContinuationConflict.into());
+ }
self.tools.insert(
index,
NativeToolIdentity {
@@ -1010,33 +1019,6 @@ impl ProjectionState {
Ok(logical_id)
}
- fn observe_reasoning_state(
- &mut self,
- index: u32,
- value: &Value,
- ) -> Result<(), ProtocolAdapterError> {
- let digest: [u8; 32] = Sha256::digest(
- serde_json::to_vec(&value)
- .map_err(|error| ProtocolAdapterError::Serialization(error.to_string()))?,
- )
- .into();
- if self.reasoning_state.get(&index) == Some(&digest) {
- return Ok(());
- }
- if !self.reasoning_state.contains_key(&index) {
- let charge = self
- .budget
- .reserve(MemoryRole::SemanticState, 128)
- .map_err(|_| ModelIrError::BufferLimit(128))?;
- self.retained.push(charge);
- }
- if self.authority.records_state() {
- record_provider_state(value, &self.owner)?;
- }
- self.reasoning_state.insert(index, digest);
- Ok(())
- }
-
fn observe_usage(&mut self, protocol: IngressProtocol, value: Option<&Value>) {
let Some(object) = value.and_then(Value::as_object) else {
return;
diff --git a/crates/gateway/src/adapters/response/native_passthrough_sse.rs b/crates/gateway/src/adapters/response/native_passthrough_sse.rs
index e7b8c033..4bb0d2d1 100644
--- a/crates/gateway/src/adapters/response/native_passthrough_sse.rs
+++ b/crates/gateway/src/adapters/response/native_passthrough_sse.rs
@@ -135,13 +135,17 @@ impl SseVisitor for ProjectionVisitor<'_> {
},
None => None,
};
- let (rewritten, mut metadata) = match self.state.project_sse(event_type, data.as_ref()) {
- Ok(projected) => projected,
- Err(error) => {
- self.protocol_error = Some(error);
- return Err(SseError::FramerFailed);
- }
- };
+ let (rewritten, mut metadata) =
+ match self
+ .state
+ .project_sse(event_type, data.as_ref(), event.raw())
+ {
+ Ok(projected) => projected,
+ Err(error) => {
+ self.protocol_error = Some(error);
+ return Err(SseError::FramerFailed);
+ }
+ };
metadata.source_bytes = event.raw().len();
self.metadata.push_back(metadata);
if let Some(data) = rewritten {
diff --git a/crates/gateway/src/adapters/response/native_passthrough_state.rs b/crates/gateway/src/adapters/response/native_passthrough_state.rs
new file mode 100644
index 00000000..fb3a50ce
--- /dev/null
+++ b/crates/gateway/src/adapters/response/native_passthrough_state.rs
@@ -0,0 +1,105 @@
+//! Provenance observation for opaque state in native passthrough responses.
+use super::super::super::continuation::record_provider_state_at_acceptance;
+use super::*;
+
+impl ProjectionState {
+ pub(super) fn observe_reasoning_state(
+ &mut self,
+ index: u32,
+ value: &Value,
+ ) -> Result<(), ProtocolAdapterError> {
+ let digest: [u8; 32] = Sha256::digest(
+ serde_json::to_vec(&value)
+ .map_err(|error| ProtocolAdapterError::Serialization(error.to_string()))?,
+ )
+ .into();
+ if self.reasoning_state.get(&index) == Some(&digest) {
+ return Ok(());
+ }
+ if !self.reasoning_state.contains_key(&index) {
+ let charge = self
+ .budget
+ .reserve(MemoryRole::SemanticState, 128)
+ .map_err(|_| ModelIrError::BufferLimit(128))?;
+ self.retained.push(charge);
+ }
+ if self.authority.records_state() {
+ record_provider_state(value, &self.owner)?;
+ }
+ self.reasoning_state.insert(index, digest);
+ Ok(())
+ }
+
+ pub(super) fn observe_messages_state(
+ &mut self,
+ object: &Map<String, Value>,
+ raw_event: &[u8],
+ ) -> Result<(), ProtocolAdapterError> {
+ // Off-path observation must never issue continuation authority.
+ if !self.authority.records_state() {
+ return Ok(());
+ }
+ match object.get("type").and_then(Value::as_str) {
+ Some("content_block_start") if object["content_block"]["type"] == "thinking" => {
+ let index = u32_field(object, "index")?;
+ if self.messages_signatures.len() >= 128
+ || self.messages_signatures.contains_key(&index)
+ {
+ return Err(ModelIrError::InvalidField("thinking.index").into());
+ }
+ self.messages_signatures.insert(index, String::new());
+ if let Some(value) = object["content_block"]
+ .get("signature")
+ .and_then(Value::as_str)
+ {
+ self.append_messages_signature(index, value)?;
+ }
+ }
+ Some("content_block_delta") if object["delta"]["type"] == "signature_delta" => {
+ let index = u32_field(object, "index")?;
+ let value = object["delta"]["signature"]
+ .as_str()
+ .ok_or(ModelIrError::InvalidField("signature"))?;
+ self.append_messages_signature(index, value)?;
+ }
+ Some("content_block_stop") => {
+ let index = u32_field(object, "index")?;
+ if let Some(signature) = self.messages_signatures.remove(&index)
+ && !signature.is_empty()
+ {
+ record_provider_state_at_acceptance(&signature, &self.owner, raw_event)?;
+ }
+ }
+ _ => {}
+ }
+ Ok(())
+ }
+
+ fn append_messages_signature(
+ &mut self,
+ index: u32,
+ delta: &str,
+ ) -> Result<(), ProtocolAdapterError> {
+ let signature = self
+ .messages_signatures
+ .get_mut(&index)
+ .ok_or(ModelIrError::InvalidField("signature.index"))?;
+ if delta.is_empty() {
+ return Ok(());
+ }
+ if signature.len().saturating_add(delta.len()) > MAX_SSE_EVENT_BYTES {
+ return Err(ModelIrError::BufferLimit(MAX_SSE_EVENT_BYTES).into());
+ }
+ let charge = self
+ .budget
+ .reserve(
+ MemoryRole::SemanticState,
+ delta.len() + 2 * std::mem::size_of::<Reservation>(),
+ )
+ .map_err(|_| ModelIrError::BufferLimit(MAX_SSE_EVENT_BYTES))?;
+ signature.reserve_exact(delta.len());
+ signature.push_str(delta);
+ self.retained.push(charge);
+ Ok(())
+ }
+}
diff --git a/crates/gateway/src/adapters/response/native_passthrough_tests.rs b/crates/gateway/src/adapters/response/native_passthrough_tests.rs
index a73dd117..6a01f5a7 100644
--- a/crates/gateway/src/adapters/response/native_passthrough_tests.rs
+++ b/crates/gateway/src/adapters/response/native_passthrough_tests.rs
@@ -1,12 +1,10 @@
-use hiroute_domain::ModelRequestRouteV2;
use hiroute_gateway_core::runtime::body::BudgetTree;
use serde_json::{Value, json};
use super::native_passthrough::{
NativeProjectedUnit, NativeResponseProjector, NativeTerminalOutcome,
};
-use crate::ports::ToolContinuationScopeV1;
-use crate::server::core_runtime::adapters::ToolLogicalIdProjection;
+use crate::server::core_runtime::adapters::ToolIdProjection;
use crate::server::core_runtime::profiles::{CandidateProtocolProfile, fixed_reasoning};
use crate::server::request_plan::IngressProtocol;
@@ -19,30 +17,13 @@ fn profile(protocol: IngressProtocol) -> CandidateProtocolProfile {
)
}
-fn projection() -> ToolLogicalIdProjection {
- ToolLogicalIdProjection::for_test(
- [7_u8; 32],
- ToolContinuationScopeV1 {
- authority_id: "authority".into(),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "alias".into(),
- route: ModelRequestRouteV2::Plan {
- revision: 2,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
- },
- )
-}
-
fn projector(protocol: IngressProtocol, streaming: bool) -> NativeResponseProjector {
NativeResponseProjector::new_for_observation(
&profile(protocol),
streaming,
"alias".into(),
None,
- projection(),
+ ToolIdProjection::new(protocol),
)
.unwrap()
}
@@ -159,12 +140,7 @@ fn responses_stream_preserves_unknown_wire_and_rewrites_only_owned_paths() {
assert_eq!(created["vendor"]["model"], "nested-untouched");
let tool = unit_json(&units[2]);
assert_eq!(tool["item"]["id"], "item-native");
- assert!(
- tool["item"]["call_id"]
- .as_str()
- .unwrap()
- .starts_with("hiroute_tool_v1_")
- );
+ assert_eq!(tool["item"]["call_id"], "call-native");
let terminal = unit_json(&units[3]);
assert_eq!(terminal["response"]["model"], "alias");
assert_eq!(terminal["response"]["output"][0]["id"], "item-native");
@@ -262,7 +238,7 @@ fn responses_hosted_search_identity_is_trusted_in_native_stream_and_snapshot() {
.as_str()
.unwrap()
.to_owned();
- assert!(logical.starts_with("hiroute_tool_v1_"));
+ assert_eq!(logical, "native-search");
assert_eq!(unit_json(&units[2])["item_id"], logical);
assert_eq!(unit_json(&units[3])["item"]["id"], logical);
assert_eq!(unit_json(&units[4])["response"]["output"][0]["id"], logical);
@@ -1014,11 +990,9 @@ fn reasoning_and_tool_argument_deltas_emit_before_terminal() {
"opaque-signature"
);
assert_eq!(unit_json(&messages_units[2])["delta"]["thinking"], "plan");
- assert!(
- unit_json(&messages_units[3])["content_block"]["id"]
- .as_str()
- .unwrap()
- .starts_with("hiroute_tool_v1_")
+ assert_eq!(
+ unit_json(&messages_units[3])["content_block"]["id"],
+ "native-tool"
);
assert_eq!(
unit_json(&messages_units[4])["delta"]["partial_json"],
@@ -1050,11 +1024,9 @@ fn reasoning_and_tool_argument_deltas_emit_before_terminal() {
chat_value["choices"][0]["delta"]["reasoning_content"],
"plan"
);
- assert!(
- chat_value["choices"][0]["delta"]["tool_calls"][0]["id"]
- .as_str()
- .unwrap()
- .starts_with("hiroute_tool_v1_")
+ assert_eq!(
+ chat_value["choices"][0]["delta"]["tool_calls"][0]["id"],
+ "native-chat-tool"
);
assert!(chat_units[0].semantic);
assert_eq!(chat_units[0].terminal, None);
diff --git a/crates/gateway/src/adapters/response/protocols.rs b/crates/gateway/src/adapters/response/protocols.rs
index 4b4b654a..afc09c3a 100644
--- a/crates/gateway/src/adapters/response/protocols.rs
+++ b/crates/gateway/src/adapters/response/protocols.rs
@@ -55,7 +55,7 @@ impl ProtocolState {
state_emission: NativeProviderStateEmission,
terminal_refusal_buffer: usize,
terminal_refusal_blocks: u32,
- tool_id_projection: Option<super::super::continuation::ToolLogicalIdProjection>,
+ tool_id_projection: Option<super::super::continuation::ToolIdProjection>,
chat_tool_projection: Option<ChatToolProjection>,
) -> Self {
match protocol {
diff --git a/crates/gateway/src/adapters/response/search.rs b/crates/gateway/src/adapters/response/search.rs
index 233843d6..5c9ad6c2 100644
--- a/crates/gateway/src/adapters/response/search.rs
+++ b/crates/gateway/src/adapters/response/search.rs
@@ -232,31 +232,14 @@ impl DecoderCore {
if !fresh {
return Err(invalid_block(index, "duplicate search"));
}
- let response_id = self
- .accumulator
+ self.accumulator
.response_id
.as_deref()
.ok_or_else(|| invalid_block(index, "search before response"))?;
item.id = if let Some(projection) = &self.tool_id_projection {
- projection.project(
- response_id,
- index,
- &native_id,
- crate::server::core_runtime::model_ir::ToolKindV1::Function,
- None,
- "web_search",
- &self.owner,
- )?
+ projection.project(&native_id, &self.owner)?
} else {
- super::super::continuation::issue_logical_tool_id(
- response_id,
- index,
- &native_id,
- crate::server::core_runtime::model_ir::ToolKindV1::Function,
- None,
- "web_search",
- &self.owner,
- )?
+ super::super::continuation::project_delivered_tool_id(&native_id, &self.owner)?
};
} else {
match self.accumulator.blocks.get(&index) {
diff --git a/crates/gateway/src/adapters/response/search_tests.rs b/crates/gateway/src/adapters/response/search_tests.rs
index 867ac580..3a871471 100644
--- a/crates/gateway/src/adapters/response/search_tests.rs
+++ b/crates/gateway/src/adapters/response/search_tests.rs
@@ -79,18 +79,21 @@ fn search_stream_replays_with_trusted_owner_and_original_action() {
&body,
&IngressRequestBindings {
provider_state_owner: None,
- tool_id_map: decoded.response.tool_id_map.clone(),
},
)
.unwrap();
let native = project_candidate_request(&request, &candidate).unwrap();
assert_eq!(native.body["input"][0], events()[5]["item"]);
- let mut unbound = request.clone();
- unbound.tool_id_map.clear();
- assert!(project_candidate_request(&unbound, &candidate).is_err());
- let mut wrong_owner = request;
- wrong_owner.tool_id_map[0].owner.native_model = "other".into();
- assert!(project_candidate_request(&wrong_owner, &candidate).is_err());
+ let another = CandidateProtocolProfile::exact_portable_path(
+ IngressProtocol::Responses,
+ IngressProtocol::Responses,
+ "another-model",
+ fixed_reasoning("fixed"),
+ );
+ assert_eq!(
+ project_candidate_request(&request, &another).unwrap().body["input"],
+ native.body["input"]
+ );
assert!(matches!(
&decoded.response.blocks[0],
ResponseBlock::WebSearch { .. }
diff --git a/crates/gateway/src/adapters/responses_ingress.rs b/crates/gateway/src/adapters/responses_ingress.rs
index 258cb276..44d880cb 100644
--- a/crates/gateway/src/adapters/responses_ingress.rs
+++ b/crates/gateway/src/adapters/responses_ingress.rs
@@ -82,7 +82,6 @@ pub(super) fn decode_responses_input(
content: vec![ContentPart::ToolResult {
logical_id: required_string(object, "call_id")?,
tool_kind: ToolKindV1::Function,
- namespace: None,
output: decode_tool_output(
object
.get("output")
@@ -143,7 +142,6 @@ pub(super) fn decode_responses_input(
content: vec![ContentPart::ToolResult {
logical_id: required_string(object, "call_id")?,
tool_kind: ToolKindV1::Custom,
- namespace: None,
output: decode_tool_output(
object
.get("output")
diff --git a/crates/gateway/src/adapters/responses_options.rs b/crates/gateway/src/adapters/responses_options.rs
index e1d7db2a..d320bfa2 100644
--- a/crates/gateway/src/adapters/responses_options.rs
+++ b/crates/gateway/src/adapters/responses_options.rs
@@ -392,7 +392,7 @@ mod tests {
assert_eq!(duplicate.responses_tool_order.len(), 1);
}
#[test]
- fn acp_tool_item_ids_do_not_replace_trusted_call_bindings() {
+ fn acp_tool_item_ids_remain_distinct_from_call_ids() {
use super::super::super::project_candidate_request;
use super::super::decode_ingress_request;
use crate::server::core_runtime::profiles::{CandidateProtocolProfile, fixed_reasoning};
@@ -400,32 +400,20 @@ mod tests {
{"type":"function_call","id":"client-item-call","call_id":"logical","name":"lookup","arguments":"{}"},
{"type":"function_call_output","id":"client-item-result","call_id":"logical","output":"done"}
]});
- let mut request = decode_ingress_request(IngressProtocol::Responses, &document).unwrap();
+ let request = decode_ingress_request(IngressProtocol::Responses, &document).unwrap();
let profile = CandidateProtocolProfile::exact_portable_path(
IngressProtocol::Responses,
IngressProtocol::Responses,
"physical",
fixed_reasoning("fixed"),
);
- assert!(project_candidate_request(&request, &profile).is_err());
- request.tool_id_map = vec![ToolIdMapEntryV1 {
- logical_id: "logical".into(),
- native_id: "trusted-native-call".into(),
- kind: ToolKindV1::Function,
- name: "lookup".into(),
- namespace: None,
- owner: profile.exact_provider_path().unwrap(),
- }];
let projected = project_candidate_request(&request, &profile).unwrap();
for index in 0..2 {
assert_eq!(
projected.body["input"][index]["id"],
document["input"][index]["id"]
);
- assert_eq!(
- projected.body["input"][index]["call_id"],
- "trusted-native-call"
- );
+ assert_eq!(projected.body["input"][index]["call_id"], "logical");
}
}
#[test]
@@ -489,7 +477,7 @@ mod tests {
#[test]
fn installed_codex_request_matches_same_protocol_projection_except_owned_rewrites() {
use super::super::super::project_candidate_request;
- use super::super::decode_ingress_request_with_state_and_tool_resolver;
+ use super::super::{IngressRequestBindings, decode_ingress_request_with_bindings};
use crate::server::core_runtime::profiles::{
CandidateProtocolProfile, Fidelity, NativeProviderStateEmission, StateAffinity,
fixed_reasoning,
@@ -533,19 +521,12 @@ mod tests {
profile.capability.request.state_affinity = StateAffinity::ExactOwner;
profile.capability.response.provider_state = Fidelity::Exact;
profile.capability.response.state_affinity = StateAffinity::ExactOwner;
- let bindings = vec![ToolIdMapEntryV1 {
- logical_id: "native-call".into(),
- native_id: "native-call".into(),
- kind: ToolKindV1::Function,
- name: "lookup".into(),
- namespace: Some("tools".into()),
- owner: profile.exact_provider_path().unwrap(),
- }];
- let request = decode_ingress_request_with_state_and_tool_resolver(
+ let request = decode_ingress_request_with_bindings(
IngressProtocol::Responses,
&native,
- profile.exact_provider_path().ok(),
- |_| Ok(bindings.clone()),
+ &IngressRequestBindings {
+ provider_state_owner: profile.exact_provider_path().ok(),
+ },
)
.unwrap();
let projected = project_candidate_request(&request, &profile).unwrap().body;
@@ -600,8 +581,7 @@ mod tests {
#[test]
fn reasoning_history_preserves_native_fields_but_requires_opaque_owner() {
use super::super::super::project_candidate_request;
- use super::super::decode_ingress_request_with_state_and_tool_resolver;
- use crate::server::core_runtime::model_ir::ToolIdMapEntryV1;
+ use super::super::{IngressRequestBindings, decode_ingress_request_with_bindings};
use crate::server::core_runtime::profiles::{
CandidateProtocolProfile, Fidelity, NativeProviderStateEmission, StateAffinity,
fixed_reasoning,
@@ -623,11 +603,12 @@ mod tests {
profile.capability.request.state_affinity = StateAffinity::ExactOwner;
profile.capability.response.provider_state = Fidelity::Exact;
profile.capability.response.state_affinity = StateAffinity::ExactOwner;
- let request = decode_ingress_request_with_state_and_tool_resolver(
+ let request = decode_ingress_request_with_bindings(
IngressProtocol::Responses,
&native,
- profile.exact_provider_path().ok(),
- |_| Ok(Vec::<ToolIdMapEntryV1>::new()),
+ &IngressRequestBindings {
+ provider_state_owner: profile.exact_provider_path().ok(),
+ },
)
.unwrap();
let projected = project_candidate_request(&request, &profile).unwrap().body;
@@ -645,11 +626,12 @@ mod tests {
json!({"type":"reasoning","summary":[],"content":[{"type":"reasoning_text","text":"native reasoning"}],"provider_extension":{"mode":1},"encrypted_content":"state"}),
] {
let native = json!({"model":"alias","input":[reasoning]});
- let request = decode_ingress_request_with_state_and_tool_resolver(
+ let request = decode_ingress_request_with_bindings(
IngressProtocol::Responses,
&native,
- profile.exact_provider_path().ok(),
- |_| Ok(Vec::<ToolIdMapEntryV1>::new()),
+ &IngressRequestBindings {
+ provider_state_owner: profile.exact_provider_path().ok(),
+ },
)
.unwrap();
assert_eq!(
@@ -658,11 +640,12 @@ mod tests {
);
}
assert!(
- decode_ingress_request_with_state_and_tool_resolver(
+ decode_ingress_request_with_bindings(
IngressProtocol::Responses,
&native,
- None,
- |_| Ok(Vec::<ToolIdMapEntryV1>::new()),
+ &IngressRequestBindings {
+ provider_state_owner: None
+ },
)
.is_err()
);
@@ -671,8 +654,7 @@ mod tests {
#[test]
fn summary_only_reasoning_preserves_native_encrypted_content_shape() {
use super::super::super::project_candidate_request;
- use super::super::decode_ingress_request_with_state_and_tool_resolver;
- use crate::server::core_runtime::model_ir::ToolIdMapEntryV1;
+ use super::super::{IngressRequestBindings, decode_ingress_request_with_bindings};
use crate::server::core_runtime::profiles::{CandidateProtocolProfile, fixed_reasoning};
let profile = CandidateProtocolProfile::exact_portable_path(
@@ -700,11 +682,12 @@ mod tests {
reasoning["encrypted_content"] = value;
}
let native = json!({"model":"alias","input":[reasoning]});
- let request = decode_ingress_request_with_state_and_tool_resolver(
+ let request = decode_ingress_request_with_bindings(
IngressProtocol::Responses,
&native,
- None,
- |_| Ok(Vec::<ToolIdMapEntryV1>::new()),
+ &IngressRequestBindings {
+ provider_state_owner: None,
+ },
)
.unwrap();
assert!(request.messages[0].content.is_empty());
@@ -717,11 +700,12 @@ mod tests {
"type":"reasoning","summary":[],"encrypted_content":7
}]});
assert!(
- decode_ingress_request_with_state_and_tool_resolver(
+ decode_ingress_request_with_bindings(
IngressProtocol::Responses,
&invalid,
- None,
- |_| Ok(Vec::<ToolIdMapEntryV1>::new()),
+ &IngressRequestBindings {
+ provider_state_owner: None
+ },
)
.is_err()
);
diff --git a/crates/gateway/src/adapters/tests.rs b/crates/gateway/src/adapters/tests.rs
index 5cfedb97..8d5bf067 100644
--- a/crates/gateway/src/adapters/tests.rs
+++ b/crates/gateway/src/adapters/tests.rs
@@ -4,11 +4,10 @@ use super::*;
use crate::content_ref::{
JsonValueExt, compact_ingress_document, externalize_model_request, model_content_refs,
};
-use crate::ports::ToolContinuationScopeV1;
use crate::replay::{ReplayConfig, ReplayManager};
use crate::server::core_runtime::model_ir::{
CanonicalTool, ContentPart, ImageSource, MessageRole, ModelIrError, RequestedReasoningControl,
- ToolChoice, ToolIdMapEntryV1, ToolKindV1, ToolResultStatusV1,
+ ToolChoice, ToolKindV1, ToolResultStatusV1,
};
use crate::server::core_runtime::profiles::{
CandidateProtocolProfile, ClientProtocolProfile, CriticalFact, Fidelity,
@@ -177,15 +176,7 @@ fn messages_ingress_accepts_claude_failed_tool_result() {
"physical",
fixed_reasoning("fixed"),
);
- let mut continued = request.clone();
- continued.tool_id_map = vec![ToolIdMapEntryV1 {
- logical_id: "hiroute_tool_v1_fixture".into(),
- native_id: "native-tool-id".into(),
- kind: ToolKindV1::Function,
- name: "Read".into(),
- namespace: None,
- owner: profile.exact_provider_path().unwrap(),
- }];
+ let continued = request.clone();
let projected = project_candidate_request(&continued, &profile).unwrap();
if upstream == IngressProtocol::Messages {
assert_eq!(
@@ -598,7 +589,6 @@ fn messages_thinking_signature_replays_as_responses_reasoning_item_in_order() {
}),
&IngressRequestBindings {
provider_state_owner: Some(owner),
- tool_id_map: Vec::new(),
},
)
.unwrap();
@@ -886,30 +876,9 @@ fn observation_decoder_projects_request_bound_tool_id_without_ambient_authority(
fixed_reasoning("fixed"),
);
let owner = profile.exact_provider_path().unwrap();
- let projection = ToolLogicalIdProjection::for_test(
- [7_u8; 32],
- ToolContinuationScopeV1 {
- authority_id: "authority".into(),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "agent".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 2,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
- },
- );
+ let projection = ToolIdProjection::new(IngressProtocol::Responses);
let expected = projection
- .project(
- "capture-response",
- 0,
- "provider-native-secret",
- ToolKindV1::Function,
- None,
- "weather",
- &owner,
- )
+ .project("provider-native-secret", &owner)
.unwrap();
let decoded = std::thread::spawn(move || {
let mut decoder =
@@ -939,7 +908,7 @@ fn observation_decoder_projects_request_bound_tool_id_without_ambient_authority(
.expect("Tool start event");
assert_eq!(logical_id, &expected);
assert_eq!(native_id, "provider-native-secret");
- assert!(logical_id.starts_with("hiroute_tool_v1_"));
+ assert_eq!(logical_id, native_id);
}
fn assert_nonstream_only_profile(upstream: IngressProtocol) {
@@ -1137,14 +1106,6 @@ fn replay_sequential_encoder_streams_large_json_in_raw_and_string_positions() {
fixed_reasoning("fixed"),
);
let mut request = canonical.clone();
- request.tool_id_map = vec![ToolIdMapEntryV1 {
- logical_id: "logical".into(),
- native_id: format!("native-{target:?}"),
- kind: ToolKindV1::Function,
- name: "weather".into(),
- namespace: None,
- owner: profile.exact_provider_path().expect("provider path"),
- }];
let expected = project_candidate_request(&request, &profile)
.expect("inline JSON projection")
.bytes;
@@ -1221,14 +1182,6 @@ fn replay_sequential_encoder_preserves_externalized_custom_input_wrappers() {
fixed_reasoning("fixed"),
);
let mut request = canonical.clone();
- request.tool_id_map = vec![ToolIdMapEntryV1 {
- logical_id: "logical".into(),
- native_id: format!("native-{target:?}"),
- kind: ToolKindV1::Custom,
- name: "shell".into(),
- namespace: None,
- owner: profile.exact_provider_path().expect("provider path"),
- }];
let expected = project_candidate_request(&request, &profile)
.expect("inline custom projection")
.bytes;
@@ -1288,14 +1241,6 @@ fn replay_externalization_keeps_tool_identity_keys_comparable_for_chat_projectio
"physical-chat",
fixed_reasoning("fixed"),
);
- let mapping = ToolIdMapEntryV1 {
- logical_id: "logical".into(),
- native_id: "native".into(),
- kind: ToolKindV1::Function,
- name: "lookup".into(),
- namespace: Some("records".into()),
- owner: profile.exact_provider_path().expect("provider path"),
- };
let document = json!({
"model":"alias",
"input":[
@@ -1308,10 +1253,7 @@ fn replay_externalization_keeps_tool_identity_keys_comparable_for_chat_projectio
]}],
"tool_choice":{"type":"function","name":"lookup"}
});
- let inline =
- decode_ingress_request_with_tool_resolver(IngressProtocol::Responses, &document, |_| {
- Ok(vec![mapping.clone()])
- })
+ let inline = decode_ingress_request(IngressProtocol::Responses, &document)
.expect("decode inline namespaced continuation");
let expected = project_candidate_request(&inline, &profile)
.expect("inline Chat projection")
@@ -1339,10 +1281,7 @@ fn replay_externalization_keeps_tool_identity_keys_comparable_for_chat_projectio
let mut compacted = document;
compact_ingress_document(IngressProtocol::Responses, &mut compacted, &store)
.expect("compact ingress payloads");
- let mut request =
- decode_ingress_request_with_tool_resolver(IngressProtocol::Responses, &compacted, |_| {
- Ok(vec![mapping])
- })
+ let mut request = decode_ingress_request(IngressProtocol::Responses, &compacted)
.expect("decode compacted namespaced continuation");
externalize_model_request(&mut request, &store, 16).expect("externalize request payloads");
let ContentPart::ToolCall {
diff --git a/crates/gateway/src/adapters/tests/native_responses.rs b/crates/gateway/src/adapters/tests/native_responses.rs
index cefdb22b..f97e487c 100644
--- a/crates/gateway/src/adapters/tests/native_responses.rs
+++ b/crates/gateway/src/adapters/tests/native_responses.rs
@@ -40,7 +40,7 @@ fn responses_to_messages_keeps_top_level_instructions_and_tool_history_but_rejec
"glm-5.3",
fixed_reasoning("fixed"),
);
- let mut request = decode_ingress_request(
+ let request = decode_ingress_request(
IngressProtocol::Responses,
&json!({
"model": "route", "stream": true, "instructions": "top-level policy",
@@ -57,28 +57,20 @@ fn responses_to_messages_keeps_top_level_instructions_and_tool_history_but_rejec
)
.unwrap();
assert!(!request.requirements().mid_conversation_instructions);
- request.tool_id_map.push(ToolIdMapEntryV1 {
- logical_id: "logical-call".into(),
- native_id: "native-call".into(),
- kind: ToolKindV1::Function,
- name: "lookup".into(),
- namespace: None,
- owner: profile.exact_provider_path().unwrap(),
- });
let body = project_candidate_request(&request, &profile).unwrap().body;
assert_eq!(body["model"], "glm-5.3");
assert_eq!(body["system"], "top-level policy");
assert_eq!(body["stream"], true);
assert_eq!(body["messages"][0]["content"][0]["text"], "first turn");
assert_eq!(body["messages"][1]["content"][0]["text"], "checking");
- assert_eq!(body["messages"][2]["content"][0]["id"], "native-call");
+ assert_eq!(body["messages"][2]["content"][0]["id"], "logical-call");
assert_eq!(
body["messages"][2]["content"][0]["input"],
json!({"key":"v"})
);
assert_eq!(
body["messages"][3]["content"][0]["tool_use_id"],
- "native-call"
+ "logical-call"
);
assert_eq!(body["messages"][4]["content"][0]["text"], "next turn");
assert_eq!(
@@ -121,7 +113,7 @@ fn codex_input_developer_prelude_is_initial_but_not_messages_representable() {
"tools": [{"type":"function","name":"lookup","parameters":{"type":"object"}}],
"tool_choice": "auto", "parallel_tool_calls": false
});
- let mut request = decode_ingress_request(IngressProtocol::Responses, &document).unwrap();
+ let request = decode_ingress_request(IngressProtocol::Responses, &document).unwrap();
let requirements = request.requirements();
assert!(requirements.initial_instructions);
assert!(!requirements.mid_conversation_instructions);
@@ -133,17 +125,9 @@ fn codex_input_developer_prelude_is_initial_but_not_messages_representable() {
"physical",
fixed_reasoning("fixed"),
);
- request.tool_id_map.push(ToolIdMapEntryV1 {
- logical_id: "logical-call".into(),
- native_id: "native-call".into(),
- kind: ToolKindV1::Function,
- name: "lookup".into(),
- namespace: None,
- owner: native.exact_provider_path().unwrap(),
- });
let projected = project_candidate_request(&request, &native).unwrap().body;
assert_eq!(projected["input"][0]["role"], "developer");
- assert_eq!(projected["input"][3]["call_id"], "native-call");
+ assert_eq!(projected["input"][3]["call_id"], "logical-call");
assert_eq!(projected["input"][5]["content"][0]["text"], "next turn");
assert_eq!(projected["stream"], true);
@@ -244,16 +228,6 @@ fn responses_multitool_continuation_replays_externalized_reasoning_state() {
let opaque_second = "second-state".repeat(130);
let bindings = IngressRequestBindings {
provider_state_owner: Some(owner.clone()),
- tool_id_map: ["call-1", "call-2"]
- .map(|id| ToolIdMapEntryV1 {
- logical_id: id.into(),
- native_id: id.into(),
- kind: ToolKindV1::Function,
- name: "shell".into(),
- namespace: None,
- owner: owner.clone(),
- })
- .into(),
};
let mut request = decode_ingress_request_with_bindings(
IngressProtocol::Responses,
diff --git a/crates/gateway/src/adapters/tool_projection.rs b/crates/gateway/src/adapters/tool_projection.rs
index 12cd859f..b0db5adf 100644
--- a/crates/gateway/src/adapters/tool_projection.rs
+++ b/crates/gateway/src/adapters/tool_projection.rs
@@ -353,7 +353,6 @@ mod tests {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
responses_options: None,
responses_item_ids: Default::default(),
responses_item_statuses: Default::default(),
diff --git a/crates/gateway/src/agent_turn_history/projection.rs b/crates/gateway/src/agent_turn_history/projection.rs
index 2c12bfde..61613326 100644
--- a/crates/gateway/src/agent_turn_history/projection.rs
+++ b/crates/gateway/src/agent_turn_history/projection.rs
@@ -1,4 +1,3 @@
-use std::collections::BTreeMap;
use std::io::Read;
use super::{AgentTurnHistoryError, ToolStatus, VisibleContentPart};
@@ -11,7 +10,7 @@ use crate::server::core_runtime::model_ir::{
pub(super) struct AnalyzedRequest {
pub(super) message_count: usize,
pub(super) turns: Vec<AnalyzedTurn>,
- pub(super) tool_results: BTreeMap<String, ToolStatus>,
+ pub(super) tool_results: Vec<(usize, String, ToolStatus)>,
}
pub(super) struct AnalyzedTurn {
@@ -77,22 +76,23 @@ pub(super) fn project_user(
.collect()
}
-fn project_tool_results(request: &ModelRequestIRV1) -> BTreeMap<String, ToolStatus> {
- let mut results = BTreeMap::new();
- for message in &request.messages {
+fn project_tool_results(request: &ModelRequestIRV1) -> Vec<(usize, String, ToolStatus)> {
+ let mut results = Vec::new();
+ for (message_index, message) in request.messages.iter().enumerate() {
for part in &message.content {
if let ContentPart::ToolResult {
logical_id, status, ..
} = part
{
- results.insert(
+ results.push((
+ message_index,
logical_id.clone(),
match status {
ToolResultStatusV1::Completed => ToolStatus::Completed,
ToolResultStatusV1::Failed => ToolStatus::Failed,
ToolResultStatusV1::Unknown => ToolStatus::Unknown,
},
- );
+ ));
}
}
}
diff --git a/crates/gateway/src/agent_turn_history/store.rs b/crates/gateway/src/agent_turn_history/store.rs
index 4c0a8393..eebedb20 100644
--- a/crates/gateway/src/agent_turn_history/store.rs
+++ b/crates/gateway/src/agent_turn_history/store.rs
@@ -90,6 +90,7 @@ impl Drop for SnapshotPin {
#[derive(Clone)]
struct TranscriptCheckpoint {
active_user_index: usize,
+ message_count: usize,
}
struct ActiveTurn {
@@ -255,6 +256,7 @@ impl AgentTurnHistoryStore {
};
let checkpoint = TranscriptCheckpoint {
active_user_index: latest.user_index,
+ message_count: analyzed.message_count,
};
let mut entry = SessionEntry {
entry_token,
@@ -339,11 +341,16 @@ impl AgentTurnHistoryStore {
if entry.active.finalized {
output::reopen_finalized(entry, &latest.user);
}
- output::apply_tool_results(&mut entry.active, &analyzed.tool_results);
+ output::apply_tool_results(
+ &mut entry.active,
+ &analyzed.tool_results,
+ entry.checkpoint.message_count,
+ );
entry.active.output.next_step();
entry.active.last_request_status = AgentTurnStatus::Unknown;
entry.checkpoint = TranscriptCheckpoint {
active_user_index: latest.user_index,
+ message_count: analyzed.message_count,
};
let ticket = AgentTurnTicket {
key: key.clone(),
@@ -399,7 +406,13 @@ impl AgentTurnHistoryStore {
.entries
.get_mut(&key)
.ok_or(AgentTurnHistoryError::Integrity)?;
- output::apply_tool_results(&mut entry.active, &analyzed.tool_results);
+ if context.history_continues {
+ output::apply_tool_results(
+ &mut entry.active,
+ &analyzed.tool_results,
+ entry.checkpoint.message_count,
+ );
+ }
if let Some(next_segment_id) = next_segment_id {
if appended_user && entry.active.last_request_status == AgentTurnStatus::Unknown {
entry.active.last_request_status = AgentTurnStatus::Interrupted;
@@ -448,6 +461,7 @@ impl AgentTurnHistoryStore {
};
entry.checkpoint = TranscriptCheckpoint {
active_user_index: latest.user_index,
+ message_count: analyzed.message_count,
};
let ticket = AgentTurnTicket {
key: key.clone(),
diff --git a/crates/gateway/src/agent_turn_history/store/output.rs b/crates/gateway/src/agent_turn_history/store/output.rs
index 74141c33..6449c37d 100644
--- a/crates/gateway/src/agent_turn_history/store/output.rs
+++ b/crates/gateway/src/agent_turn_history/store/output.rs
@@ -8,7 +8,7 @@ use crate::server::core_runtime::model_ir::{ModelEvent, ModelStreamEventV1, WebS
pub(super) struct AcceptedOutput {
step: Option<usize>,
blocks: BTreeMap<u32, usize>,
- tools: BTreeMap<String, (usize, usize)>,
+ tools: BTreeMap<String, Option<(usize, usize)>>,
}
impl AcceptedOutput {
@@ -28,13 +28,25 @@ impl AcceptedOutput {
}
}
-pub(super) fn apply_tool_results(active: &mut ActiveTurn, results: &BTreeMap<String, ToolStatus>) {
- for (id, status) in results {
- if let Some(&(step, part)) = active.output.tools.get(id)
- && let Some(VisibleContentPart::ToolActivity { status: target, .. }) = active
- .steps
- .get_mut(step)
- .and_then(|step| step.get_mut(part))
+pub(super) fn apply_tool_results(
+ active: &mut ActiveTurn,
+ results: &[(usize, String, ToolStatus)],
+ previous_message_count: usize,
+) {
+ for (message_index, id, status) in results {
+ if *message_index < previous_message_count {
+ continue;
+ }
+ // Keep an ambiguous ID unknown for the rest of this turn: a late result
+ // from either old call must never be attributed to a later reuse.
+ let Some(Some((step, part))) = active.output.tools.get(id).copied() else {
+ continue;
+ };
+ active.output.tools.remove(id);
+ if let Some(VisibleContentPart::ToolActivity { status: target, .. }) = active
+ .steps
+ .get_mut(step)
+ .and_then(|step| step.get_mut(part))
{
*target = *status;
}
@@ -157,10 +169,17 @@ impl AgentTurnHistoryStore {
_ => active.capture_partial = true,
}
if let Some(id) = tool_id {
+ // Ambiguous concurrent reuse affects observation only, never forwarding.
active
.output
.tools
- .insert(id.clone(), (step_index, part_index));
+ .entry(id.clone())
+ .and_modify(|position| {
+ if *position != Some((step_index, part_index)) {
+ *position = None;
+ }
+ })
+ .or_insert(Some((step_index, part_index)));
}
let old = entry.accounted_bytes;
entry.accounted_bytes = entry_bytes(&ticket.key, entry);
diff --git a/crates/gateway/src/agent_turn_history/store_tests.rs b/crates/gateway/src/agent_turn_history/store_tests.rs
index eaca3bee..491ee20c 100644
--- a/crates/gateway/src/agent_turn_history/store_tests.rs
+++ b/crates/gateway/src/agent_turn_history/store_tests.rs
@@ -1,4 +1,5 @@
use super::*;
+use crate::agent_turn_history::ToolStatus;
use hiroute_gateway_core::runtime::body::BudgetTree;
use serde_json::json;
use std::path::PathBuf;
@@ -75,7 +76,6 @@ fn request(messages: Vec<CanonicalMessage>) -> ModelRequestIRV1 {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
responses_options: None,
responses_item_ids: Default::default(),
responses_item_statuses: Default::default(),
@@ -110,7 +110,6 @@ fn tool_result(id: &str, failed: bool) -> CanonicalMessage {
content: vec![ContentPart::ToolResult {
logical_id: id.into(),
tool_kind: ToolKindV1::Function,
- namespace: Some("functions".into()),
output: ToolOutput::Text("must not be projected".into()),
status: if failed {
ToolResultStatusV1::Failed
@@ -351,6 +350,201 @@ fn tool_continuation_freezes_branch_and_next_user_starts_one_new_turn() {
assert_eq!(history.visible_conversation.len(), 2);
}
+#[test]
+fn reused_tool_id_updates_only_the_new_occurrence_from_appended_results() {
+ let (_directory, replay) = replay();
+ let store = AgentTurnHistoryStore::new(2 * 1024 * 1024, Duration::from_secs(60));
+ let key = store.scope_key("workspace", "reused-tool").unwrap();
+ let now = Instant::now();
+ let mut messages = vec![text(MessageRole::User, "Run tools")];
+ let (ticket, history) = new_turn(
+ store
+ .begin(
+ key.clone(),
+ plan(1),
+ &request(messages.clone()),
+ &replay,
+ now,
+ )
+ .unwrap(),
+ );
+ drop(history);
+ store.commit_decision(&ticket, decision("simple")).unwrap();
+ accepted_output::tool(&store, &ticket, "same", "first");
+ let finish = |ticket: &AgentTurnTicket| {
+ store
+ .finish_request(
+ ticket,
+ "request".into(),
+ AgentTurnStatus::Completed,
+ vec![execution("model", "profile", "simple", "request")],
+ false,
+ now,
+ )
+ .unwrap();
+ };
+ let resume = |messages: &[CanonicalMessage]| match store
+ .begin(
+ key.clone(),
+ plan(1),
+ &request(messages.to_vec()),
+ &replay,
+ now,
+ )
+ .unwrap()
+ {
+ AgentTurnBegin::Continuation { ticket, .. } => ticket,
+ _ => panic!("expected continuation"),
+ };
+ let statuses = || {
+ let inner = store.inner.lock();
+ inner.entries[&key]
+ .active
+ .steps
+ .iter()
+ .flatten()
+ .filter_map(|part| match part {
+ VisibleContentPart::ToolActivity { status, .. } => Some(*status),
+ _ => None,
+ })
+ .collect::<Vec<_>>()
+ };
+ finish(&ticket);
+ messages.extend([tool_call("same", "first"), tool_result("same", true)]);
+ let second = resume(&messages);
+ accepted_output::tool(&store, &second, "same", "second");
+ finish(&second);
+ assert_eq!(statuses(), vec![ToolStatus::Failed, ToolStatus::Unknown]);
+
+ // Retrying the same transcript must not apply its old failed result to second.
+ let retry = resume(&messages);
+ assert_eq!(statuses(), vec![ToolStatus::Failed, ToolStatus::Unknown]);
+ finish(&retry);
+ messages.extend([tool_call("same", "second"), tool_result("same", false)]);
+ let third = resume(&messages);
+ assert_eq!(statuses(), vec![ToolStatus::Failed, ToolStatus::Completed]);
+ accepted_output::tool(&store, &third, "ambiguous", "third");
+ finish(&third);
+ let fourth = resume(&messages);
+ accepted_output::tool(&store, &fourth, "ambiguous", "fourth");
+ finish(&fourth);
+ messages.extend([
+ tool_call("ambiguous", "fourth"),
+ tool_result("ambiguous", false),
+ ]);
+ let fifth = resume(&messages);
+ assert_eq!(
+ statuses(),
+ vec![
+ ToolStatus::Failed,
+ ToolStatus::Completed,
+ ToolStatus::Unknown,
+ ToolStatus::Unknown
+ ]
+ );
+ accepted_output::tool(&store, &fifth, "ambiguous", "fifth");
+ finish(&fifth);
+ messages.extend([
+ tool_call("ambiguous", "fifth"),
+ tool_result("ambiguous", true),
+ ]);
+ let sixth = resume(&messages);
+ assert_eq!(
+ statuses(),
+ vec![
+ ToolStatus::Failed,
+ ToolStatus::Completed,
+ ToolStatus::Unknown,
+ ToolStatus::Unknown,
+ ToolStatus::Unknown
+ ]
+ );
+ finish(&sixth);
+}
+
+#[test]
+fn rebuilt_context_does_not_attribute_shifted_old_result_to_reused_id() {
+ let (_directory, replay) = replay();
+ let store = AgentTurnHistoryStore::new(2 * 1024 * 1024, Duration::from_secs(60));
+ let key = store.scope_key("workspace", "shifted-tool-result").unwrap();
+ let now = Instant::now();
+ let user = text(MessageRole::User, "Run tools");
+ let (first, history) = new_turn(
+ store
+ .begin(
+ key.clone(),
+ plan(1),
+ &request(vec![user.clone()]),
+ &replay,
+ now,
+ )
+ .unwrap(),
+ );
+ drop(history);
+ store.commit_decision(&first, decision("simple")).unwrap();
+ accepted_output::tool(&store, &first, "same", "first");
+ let finish = |ticket: &AgentTurnTicket| {
+ store
+ .finish_request(
+ ticket,
+ "request".into(),
+ AgentTurnStatus::Completed,
+ vec![execution("model", "profile", "simple", "request")],
+ false,
+ now,
+ )
+ .unwrap();
+ };
+ finish(&first);
+ let old_messages = vec![
+ user.clone(),
+ tool_call("same", "first"),
+ tool_result("same", true),
+ ];
+ let AgentTurnBegin::Continuation { ticket: second, .. } = store
+ .begin(
+ key.clone(),
+ plan(1),
+ &request(old_messages.clone()),
+ &replay,
+ now,
+ )
+ .unwrap()
+ else {
+ panic!("expected continuation");
+ };
+ accepted_output::tool(&store, &second, "same", "second");
+ finish(&second);
+
+ let mut rebuilt = vec![text(MessageRole::Developer, "New instructions")];
+ rebuilt.extend(old_messages);
+ let (_, history) = new_turn(
+ store
+ .begin_with_context(
+ key,
+ plan(1),
+ &request(rebuilt),
+ &replay,
+ ContextDecisionFacts {
+ history_continues: false,
+ has_hold_preference: false,
+ },
+ now,
+ )
+ .unwrap(),
+ );
+ let statuses = history.visible_conversation[0]
+ .steps
+ .iter()
+ .flatten()
+ .filter_map(|part| match part {
+ VisibleContentPart::ToolActivity { status, .. } => Some(*status),
+ _ => None,
+ })
+ .collect::<Vec<_>>();
+ assert_eq!(statuses, vec![ToolStatus::Failed, ToolStatus::Unknown]);
+}
+
#[test]
fn context_boundary_reclassifies_same_user_and_preserves_unknown_execution() {
let (_directory, replay) = replay();
@@ -423,7 +617,13 @@ fn context_boundary_reclassifies_same_user_and_preserves_unknown_execution() {
);
let encoded = serde_json::to_string(&history.visible_conversation).unwrap();
assert!(encoded.contains("functions.run_tests"));
- assert!(encoded.contains("failed"));
+ assert_eq!(
+ history.visible_conversation[0].steps,
+ vec![vec![VisibleContentPart::ToolActivity {
+ tool: "functions.run_tests".into(),
+ status: ToolStatus::Unknown,
+ }]]
+ );
assert_eq!(history.assessment_from, Some(0));
assert!(!history.history_partial);
drop(history);
diff --git a/crates/gateway/src/composition/ports.rs b/crates/gateway/src/composition/ports.rs
index 6d344f49..d1bbfd95 100644
--- a/crates/gateway/src/composition/ports.rs
+++ b/crates/gateway/src/composition/ports.rs
@@ -330,26 +330,16 @@ fn project_candidate_facts_template(
match adapters::project_candidate_request_template(request, profile) {
Ok(projected) => Ok(projected),
Err(
- error @ adapters::ProtocolAdapterError::ModelIr(
- ModelIrError::ProviderStateNotPortable | ModelIrError::ToolIdBindingRequired(_),
- ),
+ error @ adapters::ProtocolAdapterError::ModelIr(ModelIrError::ProviderStateNotPortable),
) => {
let exact_owner = profile.exact_provider_path()?;
- // Size a non-executable candidate without granting it continuation
- // authority. The original Tool IDs and opaque-state owners stay in
- // Planner input; its state gate excludes mismatching candidates.
+ // Size opaque state without changing Planner's original owner facts.
let mut sizing_request = request.clone();
let mut mismatched = false;
for owner in sizing_request
- .tool_id_map
+ .provider_state
.iter_mut()
- .map(|entry| &mut entry.owner)
- .chain(
- sizing_request
- .provider_state
- .iter_mut()
- .map(|state| &mut state.owner),
- )
+ .map(|state| &mut state.owner)
.chain(
sizing_request
.instructions
@@ -788,7 +778,6 @@ mod sizing_tests {
&json!({"model":"route","input":[{"type":"reasoning","summary":[],"encrypted_content":"state"}]}),
&adapters::IngressRequestBindings {
provider_state_owner: Some(owner.exact_provider_path().unwrap()),
- tool_id_map: Vec::new(),
},
).unwrap();
let before = serde_json::to_value(&request).unwrap();
diff --git a/crates/gateway/src/content_ref.rs b/crates/gateway/src/content_ref.rs
index ecdd125a..6a56c2ee 100644
--- a/crates/gateway/src/content_ref.rs
+++ b/crates/gateway/src/content_ref.rs
@@ -358,7 +358,7 @@ fn part_field_count(parts: &[ContentPart]) -> Result<usize, ReplayError> {
count
.checked_add(match part {
ContentPart::ToolCall { namespace, .. } => 2 + usize::from(namespace.is_some()),
- ContentPart::ToolResult { namespace, .. } => 1 + usize::from(namespace.is_some()),
+ ContentPart::ToolResult { .. } => 1,
ContentPart::Text { .. }
| ContentPart::Image { .. }
| ContentPart::ProviderState { .. } => 1,
diff --git a/crates/gateway/src/context_hold/history.rs b/crates/gateway/src/context_hold/history.rs
index fc9d051d..f635cc7d 100644
--- a/crates/gateway/src/context_hold/history.rs
+++ b/crates/gateway/src/context_hold/history.rs
@@ -338,7 +338,6 @@ impl Encoder {
ContentPart::ToolResult {
logical_id,
tool_kind,
- namespace,
output,
status,
} => {
@@ -348,7 +347,6 @@ impl Encoder {
ToolKindV1::Custom => b"custom",
});
self.bytes(logical_id.as_bytes());
- self.optional_string(namespace.as_deref());
self.tag(match status {
ToolResultStatusV1::Completed => b"completed",
ToolResultStatusV1::Failed => b"failed",
@@ -564,7 +562,6 @@ mod tests {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
responses_options: None,
responses_item_ids: Default::default(),
responses_item_statuses: Default::default(),
@@ -733,6 +730,28 @@ mod tests {
);
}
+ #[test]
+ fn later_reused_id_does_not_change_earlier_result_fingerprint() {
+ use crate::server::core_runtime::adapters::decode_ingress_request;
+ use serde_json::json;
+ let mut wire = json!({"model":"alias","input":[
+ {"type":"function_call_output","call_id":"same","output":"first"}
+ ]});
+ let first = decode_ingress_request(IngressProtocol::Responses, &wire).unwrap();
+ wire["input"].as_array_mut().unwrap().extend([
+ json!({"type":"function_call","call_id":"same","namespace":"new-group","name":"lookup","arguments":"{}"}),
+ json!({"type":"function_call_output","call_id":"same","output":"second"}),
+ ]);
+ let continued = decode_ingress_request(IngressProtocol::Responses, &wire).unwrap();
+ let key = [19; 32];
+ assert_eq!(
+ visible_history(&continued, &key)
+ .unwrap()
+ .digest_at(first.messages.len()),
+ Some(visible_history(&first, &key).unwrap().complete_digest())
+ );
+ }
+
#[test]
fn namespace_schema_order_and_supported_responses_options_rebuild_context() {
use crate::server::core_runtime::adapters::decode_ingress_request;
diff --git a/crates/gateway/src/context_hold/identity.rs b/crates/gateway/src/context_hold/identity.rs
index 46a89ebe..a9f9d015 100644
--- a/crates/gateway/src/context_hold/identity.rs
+++ b/crates/gateway/src/context_hold/identity.rs
@@ -340,7 +340,6 @@ mod tests {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
responses_options: None,
responses_item_ids: Default::default(),
responses_item_statuses: Default::default(),
diff --git a/crates/gateway/src/context_hold/store_tests.rs b/crates/gateway/src/context_hold/store_tests.rs
index 4b0243b9..56db067a 100644
--- a/crates/gateway/src/context_hold/store_tests.rs
+++ b/crates/gateway/src/context_hold/store_tests.rs
@@ -50,7 +50,6 @@ fn history_request(instruction: u8, messages: &[u8]) -> ModelRequestIRV1 {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
responses_options: None,
responses_item_ids: Default::default(),
responses_item_statuses: Default::default(),
diff --git a/crates/gateway/src/core_runtime.rs b/crates/gateway/src/core_runtime.rs
index 7a85c236..bde22b39 100644
--- a/crates/gateway/src/core_runtime.rs
+++ b/crates/gateway/src/core_runtime.rs
@@ -45,9 +45,7 @@ use crate::content_ref::{
scan_ingress_document,
};
use crate::context_hold::{ContextHoldStore, ContextRequest, HoldCompletion, begin_context};
-use crate::ports::{
- InMemoryToolContinuationAuthority, ToolContinuationAuthority, ToolContinuationScopeV1,
-};
+use crate::provider_state::ProviderStateScopeV1;
use crate::replay::{ReplayError, ReplayManager, ReplayReader, ReplayStore};
use crate::runtime::{
ProductionProvider, ProductionReplaySeed, ProductionReplaySeedEnvelope, ProductionRouteContext,
@@ -82,7 +80,6 @@ pub struct ProductionGatewayRuntime {
observation: Arc<GatewayObservation>,
context_holds: Arc<ContextHoldStore>,
agent_turn_history: Arc<AgentTurnHistoryStore>,
- tool_continuations: Arc<dyn ToolContinuationAuthority>,
replay: Option<ReplayManager>,
provider_states: Arc<crate::provider_state::ProviderStateStore>,
executable_sha256: Option<Arc<str>>,
@@ -105,44 +102,16 @@ impl ProductionGatewayRuntime {
ports: ProductionPorts,
planner_inputs: Arc<dyn PlannerInputAuthority>,
) -> Self {
- Self::compose_with_planner_and_continuations(
- ports,
- planner_inputs,
- Arc::new(InMemoryToolContinuationAuthority::default()),
- )
- }
-
- pub fn compose_with_planner_and_continuations(
- ports: ProductionPorts,
- planner_inputs: Arc<dyn PlannerInputAuthority>,
- tool_continuations: Arc<dyn ToolContinuationAuthority>,
- ) -> Self {
- Self::compose_with_planner_observation_and_continuations(
+ Self::compose_with_planner_and_observation(
ports,
planner_inputs,
Arc::new(GatewayObservation::from_environment()),
- tool_continuations,
)
}
-
pub fn compose_with_planner_and_observation(
- ports: ProductionPorts,
- planner_inputs: Arc<dyn PlannerInputAuthority>,
- observation: Arc<GatewayObservation>,
- ) -> Self {
- Self::compose_with_planner_observation_and_continuations(
- ports,
- planner_inputs,
- observation,
- Arc::new(InMemoryToolContinuationAuthority::default()),
- )
- }
-
- fn compose_with_planner_observation_and_continuations(
mut ports: ProductionPorts,
planner_inputs: Arc<dyn PlannerInputAuthority>,
observation: Arc<GatewayObservation>,
- tool_continuations: Arc<dyn ToolContinuationAuthority>,
) -> Self {
ports.credentials = Arc::new(ObservedCredentialResolver::new(Arc::clone(
&ports.credentials,
@@ -174,7 +143,6 @@ impl ProductionGatewayRuntime {
observation,
context_holds: Arc::new(ContextHoldStore::default()),
agent_turn_history: Arc::new(AgentTurnHistoryStore::default()),
- tool_continuations,
replay: ReplayManager::from_environment().ok(),
provider_states: Arc::new(crate::provider_state::ProviderStateStore::default()),
executable_sha256: None,
@@ -530,7 +498,30 @@ impl ProductionGatewayRuntime {
return write_typed_error_phase(session, status, code, "planner").await;
}
};
- let provider_state_owner = if protocol == IngressProtocol::Responses {
+ // Native Messages state already has an exact single-source binding.
+ // Responses ciphertext, including its Messages signature projection,
+ // must instead resolve the source actually accepted downstream.
+ let native_messages_owner = if protocol == IngressProtocol::Messages {
+ match provider_state_owner(&authorized, protocol) {
+ Ok(owner) => {
+ owner.filter(|owner| owner.upstream_protocol == IngressProtocol::Messages)
+ }
+ Err(_) => {
+ return write_typed_error_phase(
+ session,
+ StatusCode::INTERNAL_SERVER_ERROR,
+ "PROVIDER_STATE_AUTHORITY_UNAVAILABLE",
+ "continuation_authority",
+ )
+ .await;
+ }
+ }
+ } else {
+ None
+ };
+ let provider_state_owner = if protocol == IngressProtocol::Responses
+ || (protocol == IngressProtocol::Messages && native_messages_owner.is_none())
+ {
match self
.provider_states
.resolve(&continuation_scope, &document, Instant::now())
@@ -552,7 +543,11 @@ impl ProductionGatewayRuntime {
}
}
} else {
- match provider_state_owner(&authorized, protocol) {
+ match if protocol == IngressProtocol::Messages {
+ Ok(native_messages_owner)
+ } else {
+ provider_state_owner(&authorized, protocol)
+ } {
Ok(owner) => owner,
Err(_) => {
return write_typed_error_phase(
@@ -576,43 +571,27 @@ impl ProductionGatewayRuntime {
return write_typed_error_phase(session, status, code, "canonical_request").await;
}
let parse_started = Instant::now();
- let mut canonical_request =
- match adapters::decode_ingress_request_with_state_and_tool_resolver(
- protocol,
- &document,
+ let mut canonical_request = match adapters::decode_ingress_request_with_bindings(
+ protocol,
+ &document,
+ &adapters::IngressRequestBindings {
provider_state_owner,
- |logical_ids| {
- self.tool_continuations
- .resolve(&continuation_scope, logical_ids, Instant::now())
- .map_err(|_| model_ir::ModelIrError::ToolContinuationUnavailable)
- },
- )
- .map(Box::new)
- {
- Ok(request) => request,
- Err(
- model_ir::ModelIrError::ToolContinuationUnavailable
- | model_ir::ModelIrError::ToolContinuationConflict,
- ) => {
- return write_typed_error_phase(
- session,
- StatusCode::BAD_REQUEST,
- "TOOL_CONTINUATION_UNAVAILABLE",
- "continuation_authority",
- )
- .await;
- }
- Err(error) => {
- let error = adapters::ProtocolAdapterError::from(error);
- return write_typed_error_phase(
- session,
- StatusCode::BAD_REQUEST,
- error.code(),
- "canonical_request",
- )
- .await;
- }
- };
+ },
+ )
+ .map(Box::new)
+ {
+ Ok(request) => request,
+ Err(error) => {
+ let error = adapters::ProtocolAdapterError::from(error);
+ return write_typed_error_phase(
+ session,
+ StatusCode::BAD_REQUEST,
+ error.code(),
+ "canonical_request",
+ )
+ .await;
+ }
+ };
let parse_elapsed = parse_started.elapsed();
let (identity, hold_ticket) = begin_context(
&self.context_holds,
@@ -1147,31 +1126,16 @@ impl ProductionGatewayRuntime {
}
let frozen_candidates: Arc<[DecisionCandidateAuthority]> = frozen_candidates.into();
let route_binding = frozen_candidates[0].binding;
- let continuation_issuance =
- match self.tool_continuations.begin(continuation_scope.clone()) {
- Ok(issuance) => issuance,
- Err(_) => {
- return write_typed_error_phase(
- session,
- StatusCode::SERVICE_UNAVAILABLE,
- "TOOL_CONTINUATION_UNAVAILABLE",
- "continuation_authority",
- )
- .await;
- }
- };
- let active_continuation = adapters::ActiveToolContinuation::new(
- Arc::clone(&self.tool_continuations),
- continuation_issuance,
- )
- .with_provider_states(crate::provider_state::ActiveProviderStates::new(
- Arc::clone(&self.provider_states),
- continuation_scope,
- ));
- if let Ok(projection) = active_continuation.logical_id_projection() {
- request_observation.bind_tool_id_projection(projection);
- }
- let _continuation_guard = ToolContinuationRequestGuard(active_continuation.clone());
+ let active_continuation = adapters::ActiveResponseDelivery::new(
+ protocol,
+ crate::provider_state::ActiveProviderStates::new(
+ Arc::clone(&self.provider_states),
+ continuation_scope,
+ protocol,
+ ),
+ );
+ request_observation.bind_tool_id_projection(active_continuation.tool_id_projection());
+ let response_delivery = active_continuation.clone();
let admission = BoundRequestAdmission {
route_binding,
accepted_response_body_plan: authorized.accepted_response_plan().body_plan.clone(),
@@ -1217,7 +1181,7 @@ impl ProductionGatewayRuntime {
budget,
seed,
));
- let execution = adapters::with_active_tool_continuation(active_continuation, execution);
+ let execution = adapters::with_active_response_delivery(active_continuation, execution);
let execution = Box::pin(with_active_request(request_observation.clone(), execution));
let result = execution.await;
let response_started = core_session.response_started;
@@ -1257,7 +1221,7 @@ impl ProductionGatewayRuntime {
request_observation.metadata().request_id.clone(),
status,
executions,
- _continuation_guard.0.accepted_count() == 0,
+ response_delivery.accepted_count() == 0,
Instant::now(),
) {
if let Some(completed) = completed {
@@ -1414,14 +1378,6 @@ impl Drop for AgentTurnRequestGuard {
}
}
-struct ToolContinuationRequestGuard(adapters::ActiveToolContinuation);
-
-impl Drop for ToolContinuationRequestGuard {
- fn drop(&mut self) {
- self.0.abort_pending();
- }
-}
-
struct ReplayBodySession<'a> {
inner: &'a mut dyn GatewaySession,
request_head: GatewayRequestHead,
@@ -1430,7 +1386,7 @@ struct ReplayBodySession<'a> {
response_capture: AcceptedResponseCapture,
response_started: bool,
runtime_state_authority: crate::runtime::RuntimeStateAuthoritySignal,
- continuation_scanner: adapters::AcceptedToolContinuationScanner,
+ continuation_scanner: adapters::AcceptedResponseDeliveryScanner,
}
#[async_trait]
@@ -1496,9 +1452,9 @@ impl GatewaySession for ReplayBodySession<'_> {
}
}
-fn tool_continuation_scope(authorized: &AuthorizedRequestPlan) -> ToolContinuationScopeV1 {
+fn tool_continuation_scope(authorized: &AuthorizedRequestPlan) -> ProviderStateScopeV1 {
let receipt = authorized.receipt();
- ToolContinuationScopeV1 {
+ ProviderStateScopeV1 {
authority_id: receipt.authority_id.to_string(),
authority_epoch: receipt.authority_epoch,
grant_id: receipt.grant_id.to_string(),
diff --git a/crates/gateway/src/core_runtime/acceptance_tests.rs b/crates/gateway/src/core_runtime/acceptance_tests.rs
index 53240f4d..09e45a69 100644
--- a/crates/gateway/src/core_runtime/acceptance_tests.rs
+++ b/crates/gateway/src/core_runtime/acceptance_tests.rs
@@ -1,5 +1,4 @@
use std::sync::Arc;
-use std::time::{Duration, Instant};
use async_trait::async_trait;
use bytes::Bytes;
@@ -10,10 +9,7 @@ use hiroute_gateway_core::transport::{
use http::{HeaderMap, Method};
use super::*;
-use crate::ports::{
- InMemoryToolContinuationAuthority, ToolContinuationAuthority, ToolContinuationScopeV1,
-};
-use crate::server::core_runtime::model_ir::{ToolIdMapEntryV1, ToolKindV1};
+use crate::provider_state::ProviderStateScopeV1;
use crate::server::core_runtime::profiles::{CandidateProtocolProfile, fixed_reasoning};
struct FailSecondBodyWrite {
@@ -54,9 +50,7 @@ impl GatewaySession for FailSecondBodyWrite {
#[tokio::test]
async fn production_response_sink_keeps_only_the_tool_unit_accepted_before_reset() {
- let authority: Arc<dyn ToolContinuationAuthority> =
- Arc::new(InMemoryToolContinuationAuthority::new(4, Duration::from_secs(30)).unwrap());
- let scope = ToolContinuationScopeV1 {
+ let scope = ProviderStateScopeV1 {
authority_id: "authority:runtime-test".into(),
authority_epoch: 1,
grant_id: "grant:runtime-test".into(),
@@ -67,39 +61,39 @@ async fn production_response_sink_keeps_only_the_tool_unit_accepted_before_reset
semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"runtime-test-plan"),
},
};
- let issuance = authority.begin(scope.clone()).unwrap();
+ let first_id = "native-first".to_owned();
+ let second_id = "native-second".to_owned();
+ let active = adapters::ActiveResponseDelivery::new(
+ IngressProtocol::Responses,
+ crate::provider_state::ActiveProviderStates::new(
+ Arc::new(crate::provider_state::ProviderStateStore::default()),
+ scope,
+ IngressProtocol::Responses,
+ ),
+ );
let profile = CandidateProtocolProfile::exact_portable_path(
IngressProtocol::Responses,
IngressProtocol::Responses,
"native-runtime-test",
fixed_reasoning("fixed"),
);
- let owner = profile.exact_provider_path().unwrap();
- let first_id = format!("hiroute_tool_v1_{}", "a".repeat(64));
- let second_id = format!("hiroute_tool_v1_{}", "b".repeat(64));
- let first = ToolIdMapEntryV1 {
- logical_id: first_id.clone(),
- native_id: "native-first".into(),
- kind: ToolKindV1::Function,
- namespace: Some("first-group".into()),
- name: "shared".into(),
- owner: owner.clone(),
- };
- let second = ToolIdMapEntryV1 {
- logical_id: second_id.clone(),
- native_id: "native-second".into(),
- kind: ToolKindV1::Function,
- namespace: Some("second-group".into()),
- name: "shared".into(),
- owner,
- };
- let now = Instant::now();
- authority
- .record_pending(&issuance, first.clone(), now)
- .unwrap();
- authority.record_pending(&issuance, second, now).unwrap();
- let active = adapters::ActiveToolContinuation::new(Arc::clone(&authority), issuance.clone());
- let guard = ToolContinuationRequestGuard(active.clone());
+ let tool_frames = adapters::with_active_response_delivery(active.clone(), async {
+ let mut frames = Vec::new();
+ for id in [&first_id, &second_id] {
+ let mut projector = adapters::NativeResponseProjector::new_for_attempt(
+ &profile, true, "runtime-test".into(), None, budget(),
+ ).unwrap();
+ projector.feed(b"data: {\"type\":\"response.created\",\"response\":{\"id\":\"response\",\"model\":\"native-runtime-test\"}}\n\n", false).unwrap();
+ let item = serde_json::json!({"type":"response.output_item.added","output_index":0,
+ "item":{"type":"function_call","call_id":id,"id":"item","name":"weather","arguments":"","status":"in_progress"}});
+ // Native whitespace and escaped IDs must not hide a delivered tool.
+ let encoded = serde_json::to_string(&item).unwrap().replace(":", ": ").replace("native-f", "native-\\u0066");
+ let units = projector.feed(format!("data: {encoded}\n\n").as_bytes(), false).unwrap();
+ assert_eq!(units.len(), 1);
+ frames.push(units[0].bytes.clone());
+ }
+ frames
+ }).await;
let observation = observation::accepted_request_for_runtime_test();
let budget = budget();
let mut downstream = FailSecondBodyWrite {
@@ -118,10 +112,7 @@ async fn production_response_sink_keeps_only_the_tool_unit_accepted_before_reset
};
session
- .write_response_body(
- Bytes::from(format!("data: {{\"call_id\":\"{first_id}\"}}\n\n")),
- false,
- )
+ .write_response_body(Bytes::from(tool_frames[0].clone()), false)
.await
.unwrap();
assert!(observation.has_accepted_attempt());
@@ -131,10 +122,7 @@ async fn production_response_sink_keeps_only_the_tool_unit_accepted_before_reset
);
let error = session
- .write_response_body(
- Bytes::from(format!("data: {{\"call_id\":\"{second_id}\"}}\n\n")),
- true,
- )
+ .write_response_body(Bytes::from(tool_frames[1].clone()), true)
.await
.unwrap_err();
assert!(matches!(error, TransportError::Io(_)));
@@ -143,13 +131,7 @@ async fn production_response_sink_keeps_only_the_tool_unit_accepted_before_reset
1
);
drop(session);
- drop(guard);
-
- assert_eq!(
- authority.resolve(&scope, &[first_id], now).unwrap(),
- vec![first]
- );
- assert!(authority.resolve(&scope, &[second_id], now).is_err());
+ assert_eq!(active.accepted_count(), 1);
assert_eq!(downstream.accepted.len(), 1);
}
diff --git a/crates/gateway/src/core_runtime/classification.rs b/crates/gateway/src/core_runtime/classification.rs
index 26742143..2a00c93e 100644
--- a/crates/gateway/src/core_runtime/classification.rs
+++ b/crates/gateway/src/core_runtime/classification.rs
@@ -605,7 +605,6 @@ fn diagnostic_request() -> ModelRequestIRV1 {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
}
}
diff --git a/crates/gateway/src/core_runtime/classification/protocol.rs b/crates/gateway/src/core_runtime/classification/protocol.rs
index 3cf9e9bb..55ad4ca2 100644
--- a/crates/gateway/src/core_runtime/classification/protocol.rs
+++ b/crates/gateway/src/core_runtime/classification/protocol.rs
@@ -196,7 +196,6 @@ mod tests {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
responses_options: None,
responses_item_ids: Default::default(),
responses_item_statuses: Default::default(),
diff --git a/crates/gateway/src/model_ir/error.rs b/crates/gateway/src/model_ir/error.rs
index e4dabdd6..dc15ded2 100644
--- a/crates/gateway/src/model_ir/error.rs
+++ b/crates/gateway/src/model_ir/error.rs
@@ -22,8 +22,6 @@ pub enum ModelIrError {
ProviderStateOwnershipRequired,
#[error("Tool call has no exact logical/native ID binding: {0}")]
ToolIdBindingRequired(String),
- #[error("Tool continuation authority is unavailable")]
- ToolContinuationUnavailable,
#[error("Tool continuation identity is ambiguous or conflicting")]
ToolContinuationConflict,
#[error("native response lifecycle is invalid: {0}")]
diff --git a/crates/gateway/src/model_ir/request.rs b/crates/gateway/src/model_ir/request.rs
index 72281fd7..882cf0cb 100644
--- a/crates/gateway/src/model_ir/request.rs
+++ b/crates/gateway/src/model_ir/request.rs
@@ -6,8 +6,6 @@ use serde_json::Value;
use crate::content_ref::ContentValue;
use crate::server::request_plan::IngressProtocol;
-use super::ModelIrError;
-
pub const MODEL_REQUEST_IR_SCHEMA: &str = "hiroute.model-request-ir/v1";
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -44,7 +42,6 @@ pub struct ModelRequestIRV1 {
/// Recorded for audit only. P0 projection always uses the candidate cap.
pub requested_max_output_tokens: Option<u64>,
pub provider_state: Vec<OpaqueProviderState>,
- pub tool_id_map: Vec<ToolIdMapEntryV1>,
/// Native Responses transport controls are retained, never silently dropped on conversion.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub responses_options: Option<ResponsesRequestOptionsV1>,
@@ -233,52 +230,6 @@ impl ModelRequestIRV1 {
requirements.image_media_types = media_types.into_iter().collect();
requirements
}
-
- /// Returns every client-visible Tool identity that requires trusted
- /// continuation recovery. One historical call and one matching result are
- /// the only unambiguous repeated use of the same logical ID.
- pub fn continuation_logical_ids(&self) -> Result<Vec<String>, ModelIrError> {
- let mut identities = std::collections::BTreeMap::<String, (bool, bool)>::new();
- for search in self.responses_search_history.values() {
- if identities
- .insert(search.id.clone(), (true, false))
- .is_some()
- {
- return Err(ModelIrError::ToolContinuationConflict);
- }
- }
- for part in self
- .instructions
- .iter()
- .flat_map(|instruction| instruction.content.iter())
- .chain(
- self.messages
- .iter()
- .flat_map(|message| message.content.iter()),
- )
- {
- let (logical_id, is_call) = match part {
- ContentPart::ToolCall { logical_id, .. } => (logical_id, true),
- ContentPart::ToolResult { logical_id, .. } => (logical_id, false),
- ContentPart::Text { .. }
- | ContentPart::Image { .. }
- | ContentPart::ProviderState { .. } => continue,
- };
- if logical_id.trim().is_empty() {
- return Err(ModelIrError::ToolContinuationConflict);
- }
- let presence = identities.entry(logical_id.clone()).or_default();
- let duplicate = if is_call {
- std::mem::replace(&mut presence.0, true)
- } else {
- std::mem::replace(&mut presence.1, true)
- };
- if duplicate {
- return Err(ModelIrError::ToolContinuationConflict);
- }
- }
- Ok(identities.into_keys().collect())
- }
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
@@ -333,8 +284,6 @@ pub enum ContentPart {
ToolResult {
logical_id: String,
tool_kind: ToolKindV1,
- #[serde(default, skip_serializing_if = "Option::is_none")]
- namespace: Option<ContentValue>,
output: ToolOutput,
#[serde(default, skip_serializing_if = "ToolResultStatusV1::is_unknown")]
status: ToolResultStatusV1,
diff --git a/crates/gateway/src/observation/content/request.rs b/crates/gateway/src/observation/content/request.rs
index 52386cdc..52b7cdff 100644
--- a/crates/gateway/src/observation/content/request.rs
+++ b/crates/gateway/src/observation/content/request.rs
@@ -264,7 +264,6 @@ impl RequestObservation {
}
ContentPart::ToolResult {
logical_id,
- namespace,
output,
status,
..
@@ -289,18 +288,6 @@ impl RequestObservation {
);
part_ordinal = part_ordinal.saturating_add(1);
}
- if let Some(namespace) = namespace {
- self.append_value(
- replay,
- message_ordinal,
- part_ordinal,
- role,
- "tool_result_namespace",
- "text/plain; charset=utf-8",
- namespace,
- )?;
- part_ordinal = part_ordinal.saturating_add(1);
- }
match output {
ToolOutput::Text(text) => self.append_value(
replay,
diff --git a/crates/gateway/src/observation/provider/capture.rs b/crates/gateway/src/observation/provider/capture.rs
index 3c7cc33c..c254f77a 100644
--- a/crates/gateway/src/observation/provider/capture.rs
+++ b/crates/gateway/src/observation/provider/capture.rs
@@ -85,7 +85,7 @@ enum CaptureJob {
id: u64,
request: RequestObservation,
profile: Arc<CandidateProtocolProfile>,
- tool_id_projection: crate::server::core_runtime::adapters::ToolLogicalIdProjection,
+ tool_id_projection: crate::server::core_runtime::adapters::ToolIdProjection,
chat_tool_projection: Option<crate::server::core_runtime::adapters::ChatToolProjection>,
streaming: bool,
alias: String,
@@ -185,7 +185,7 @@ impl CanonicalCaptureProducer {
&self,
request: RequestObservation,
profile: Arc<CandidateProtocolProfile>,
- tool_id_projection: crate::server::core_runtime::adapters::ToolLogicalIdProjection,
+ tool_id_projection: crate::server::core_runtime::adapters::ToolIdProjection,
chat_tool_projection: Option<&crate::server::core_runtime::adapters::ChatToolProjection>,
streaming: bool,
alias: String,
diff --git a/crates/gateway/src/observation/provider/capture/tracker.rs b/crates/gateway/src/observation/provider/capture/tracker.rs
index c471f708..025e20f2 100644
--- a/crates/gateway/src/observation/provider/capture/tracker.rs
+++ b/crates/gateway/src/observation/provider/capture/tracker.rs
@@ -4,7 +4,7 @@ use std::sync::Arc;
use crate::server::core_runtime::adapters::{
ChatToolProjection, ClientResponseRenderer, IncrementalClientSseRenderer,
NativeResponseDecoder, NativeResponseProjector, RenderedClientResponse, ResponseDecodeStatus,
- ToolLogicalIdProjection,
+ ToolIdProjection,
};
use crate::server::core_runtime::model_ir::{ModelEvent, ModelStreamEventV1};
use crate::server::core_runtime::profiles::{CandidateProtocolProfile, ClientProtocolProfile};
@@ -20,7 +20,7 @@ pub(super) struct CanonicalResponseTracker {
ingress: IngressProtocol,
alias: String,
streaming: bool,
- tool_id_projection: ToolLogicalIdProjection,
+ tool_id_projection: ToolIdProjection,
chat_tool_projection: Option<ChatToolProjection>,
decoder: Option<NativeResponseDecoder>,
renderer: Option<IncrementalClientSseRenderer>,
@@ -41,7 +41,7 @@ struct CanonicalWireUnit {
impl CanonicalResponseTracker {
pub(super) fn new(
profile: Arc<CandidateProtocolProfile>,
- tool_id_projection: ToolLogicalIdProjection,
+ tool_id_projection: ToolIdProjection,
chat_tool_projection: Option<ChatToolProjection>,
streaming: bool,
alias: String,
@@ -345,29 +345,14 @@ fn captures_conversation_content(event: &ModelEvent) -> bool {
#[cfg(test)]
mod tests {
- use hiroute_domain::ModelRequestRouteV2;
use serde_json::json;
use super::*;
- use crate::ports::ToolContinuationScopeV1;
- use crate::server::core_runtime::adapters::ToolLogicalIdProjection;
+ use crate::server::core_runtime::adapters::ToolIdProjection;
use crate::server::core_runtime::profiles::fixed_reasoning;
- fn projection() -> ToolLogicalIdProjection {
- ToolLogicalIdProjection::for_test(
- [3_u8; 32],
- ToolContinuationScopeV1 {
- authority_id: "authority".into(),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "alias".into(),
- route: ModelRequestRouteV2::Plan {
- revision: 1,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
- },
- )
+ fn projection() -> ToolIdProjection {
+ ToolIdProjection::new(IngressProtocol::Responses)
}
#[test]
diff --git a/crates/gateway/src/observation/request.rs b/crates/gateway/src/observation/request.rs
index 06adcad2..213ce985 100644
--- a/crates/gateway/src/observation/request.rs
+++ b/crates/gateway/src/observation/request.rs
@@ -18,7 +18,7 @@ use crate::agent_turn_history::{AgentTurnStatus, CompletedAgentTurn, ExecutionAt
use crate::ports::{
CasOutcome, ProbeLeaseOutcome, RuntimeHealth, RuntimeStateEntry, RuntimeStateKey,
};
-use crate::server::core_runtime::adapters::ToolLogicalIdProjection;
+use crate::server::core_runtime::adapters::ToolIdProjection;
use crate::server::core_runtime::classification::BoundAssessment;
use crate::server::core_runtime::profiles::CandidateProtocolProfile;
@@ -105,7 +105,7 @@ pub(super) struct RequestObservationState {
pub(super) accepted_wire_usage_recorded: bool,
pub(super) accepted_attempt_finished: bool,
pub(super) response_capture: Option<CanonicalCaptureHandle>,
- pub(super) tool_id_projection: Option<ToolLogicalIdProjection>,
+ pub(super) tool_id_projection: Option<ToolIdProjection>,
pub(super) response_part_ordinal: u32,
}
@@ -318,7 +318,7 @@ impl RequestObservation {
/// request scope, and observation remains unable to mutate authority.
pub(in crate::server::core_runtime) fn bind_tool_id_projection(
&self,
- projection: ToolLogicalIdProjection,
+ projection: ToolIdProjection,
) {
let mut state = self.lock_state();
if state.tool_id_projection.is_none() {
diff --git a/crates/gateway/src/planning/eligibility.rs b/crates/gateway/src/planning/eligibility.rs
index 55022905..0084ffd1 100644
--- a/crates/gateway/src/planning/eligibility.rs
+++ b/crates/gateway/src/planning/eligibility.rs
@@ -308,12 +308,7 @@ fn state_gate(
{
return Err(ExclusionReasonCodeV1::OpaqueStateUnportable);
}
- if state_owners(request).any(|owner| owner != &exact_owner)
- || request
- .tool_id_map
- .iter()
- .any(|entry| entry.owner != exact_owner)
- {
+ if state_owners(request).any(|owner| owner != &exact_owner) {
return Err(ExclusionReasonCodeV1::ProviderStateAffinityMismatch);
}
match profile.capability.native_provider_state {
diff --git a/crates/gateway/src/planning/tests.rs b/crates/gateway/src/planning/tests.rs
index 70a30e2e..4b7ca465 100644
--- a/crates/gateway/src/planning/tests.rs
+++ b/crates/gateway/src/planning/tests.rs
@@ -40,7 +40,6 @@ fn request(protocol: IngressProtocol, text: &str) -> ModelRequestIRV1 {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
}
}
@@ -389,7 +388,6 @@ fn planner_user_phrase_and_continuations_have_fixed_precedence() {
content: vec![ContentPart::ToolResult {
logical_id: "call-1".into(),
tool_kind: ToolKindV1::Function,
- namespace: None,
output: ToolOutput::Text("opaque Tool output".into()),
status: ToolResultStatusV1::Unknown,
}],
diff --git a/crates/gateway/src/ports/mod.rs b/crates/gateway/src/ports/mod.rs
index c12ad25d..31fad21e 100644
--- a/crates/gateway/src/ports/mod.rs
+++ b/crates/gateway/src/ports/mod.rs
@@ -3,9 +3,7 @@
mod credential;
mod runtime_state;
mod scope;
-mod tool_continuation;
pub use credential::*;
pub use runtime_state::*;
pub use scope::*;
-pub use tool_continuation::*;
diff --git a/crates/gateway/src/ports/tool_continuation.rs b/crates/gateway/src/ports/tool_continuation.rs
deleted file mode 100644
index 7eb33f67..00000000
--- a/crates/gateway/src/ports/tool_continuation.rs
+++ /dev/null
@@ -1,495 +0,0 @@
-//! Bounded, process-local authority for accepted Tool-call continuation.
-
-use std::collections::{BTreeMap, VecDeque};
-use std::sync::Mutex;
-use std::sync::atomic::{AtomicU64, Ordering};
-use std::time::{Duration, Instant};
-
-use thiserror::Error;
-
-use crate::server::core_runtime::model_ir::ToolIdMapEntryV1;
-
-pub const DEFAULT_TOOL_CONTINUATION_CAPACITY: usize = 16_384;
-pub const DEFAULT_TOOL_CONTINUATION_TTL: Duration = Duration::from_secs(60 * 60);
-const MAX_TOOL_CONTINUATION_CAPACITY: usize = 1_000_000;
-const MAX_TOOL_CONTINUATION_TTL: Duration = Duration::from_secs(24 * 60 * 60);
-
-/// Exact logical authority scope. Deliberately excludes ingress protocol so a
-/// continuation may be losslessly projected through another supported client
-/// protocol, while Workspace epoch, Grant generation, and AgentPlan semantic
-/// identity remain hard boundaries.
-#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
-pub struct ToolContinuationScopeV1 {
- pub authority_id: String,
- pub authority_epoch: u64,
- pub grant_id: String,
- pub grant_generation: u64,
- pub served_model_id: String,
- pub route: hiroute_domain::ModelRequestRouteV2,
-}
-
-impl ToolContinuationScopeV1 {
- pub fn is_complete(&self) -> bool {
- self.authority_epoch > 0
- && self.grant_generation > 0
- && self.route.validate().is_ok()
- && [
- self.authority_id.as_str(),
- self.grant_id.as_str(),
- self.served_model_id.as_str(),
- ]
- .into_iter()
- .all(|value| !value.trim().is_empty())
- }
-}
-
-/// Request-owned issuance identity. It is never serialized or accepted from
-/// the client.
-#[derive(Clone, Debug, Eq, PartialEq)]
-pub struct ToolContinuationIssuanceV1 {
- sequence: u64,
- scope: ToolContinuationScopeV1,
-}
-
-impl ToolContinuationIssuanceV1 {
- pub fn scope(&self) -> &ToolContinuationScopeV1 {
- &self.scope
- }
-}
-
-#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
-pub enum ToolContinuationError {
- #[error("Tool continuation authority is unavailable")]
- Unavailable,
- #[error("Tool continuation identity conflicts with existing authority")]
- Conflict,
- #[error("Tool continuation authority capacity is exhausted")]
- Capacity,
- #[error("Tool continuation authority configuration is invalid")]
- InvalidConfiguration,
-}
-
-/// Trusted local port. A mapping starts pending while native response bytes
-/// are decoded and becomes resolvable only after the corresponding logical ID
-/// is accepted by the downstream transport.
-pub trait ToolContinuationAuthority: Send + Sync {
- fn begin(
- &self,
- scope: ToolContinuationScopeV1,
- ) -> Result<ToolContinuationIssuanceV1, ToolContinuationError>;
-
- fn record_pending(
- &self,
- issuance: &ToolContinuationIssuanceV1,
- mapping: ToolIdMapEntryV1,
- now: Instant,
- ) -> Result<(), ToolContinuationError>;
-
- fn accept(&self, issuance: &ToolContinuationIssuanceV1, logical_id: &str, now: Instant)
- -> bool;
-
- fn abort_pending(&self, issuance: &ToolContinuationIssuanceV1);
-
- fn resolve(
- &self,
- scope: &ToolContinuationScopeV1,
- logical_ids: &[String],
- now: Instant,
- ) -> Result<Vec<ToolIdMapEntryV1>, ToolContinuationError>;
-}
-
-pub struct InMemoryToolContinuationAuthority {
- capacity: usize,
- ttl: Duration,
- next_issuance: AtomicU64,
- state: Mutex<AuthorityState>,
-}
-
-#[derive(Default)]
-struct AuthorityState {
- entries: BTreeMap<String, StoredMapping>,
- insertion_order: VecDeque<(u64, String)>,
- next_entry: u64,
-}
-
-struct StoredMapping {
- scope: ToolContinuationScopeV1,
- mapping: ToolIdMapEntryV1,
- state: MappingState,
- expires_at: Option<Instant>,
- entry_sequence: u64,
-}
-
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
-enum MappingState {
- Pending(u64),
- Active,
- Conflicted,
-}
-
-impl Default for InMemoryToolContinuationAuthority {
- fn default() -> Self {
- Self::new(
- DEFAULT_TOOL_CONTINUATION_CAPACITY,
- DEFAULT_TOOL_CONTINUATION_TTL,
- )
- .expect("static Tool continuation limits are valid")
- }
-}
-
-impl InMemoryToolContinuationAuthority {
- pub fn new(capacity: usize, ttl: Duration) -> Result<Self, ToolContinuationError> {
- if capacity == 0
- || capacity > MAX_TOOL_CONTINUATION_CAPACITY
- || ttl.is_zero()
- || ttl > MAX_TOOL_CONTINUATION_TTL
- {
- return Err(ToolContinuationError::InvalidConfiguration);
- }
- Ok(Self {
- capacity,
- ttl,
- next_issuance: AtomicU64::new(1),
- state: Mutex::new(AuthorityState::default()),
- })
- }
-
- pub fn from_environment() -> Result<Self, ToolContinuationError> {
- let capacity = environment_usize(
- "HIROUTE_TOOL_CONTINUATION_CAPACITY",
- DEFAULT_TOOL_CONTINUATION_CAPACITY,
- )?;
- let ttl_ms = environment_u64(
- "HIROUTE_TOOL_CONTINUATION_TTL_MS",
- u64::try_from(DEFAULT_TOOL_CONTINUATION_TTL.as_millis())
- .map_err(|_| ToolContinuationError::InvalidConfiguration)?,
- )?;
- Self::new(capacity, Duration::from_millis(ttl_ms))
- }
-
- fn lock(&self) -> std::sync::MutexGuard<'_, AuthorityState> {
- self.state
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- }
-
- fn prune_expired(state: &mut AuthorityState, now: Instant) {
- state
- .entries
- .retain(|_, entry| entry.expires_at.is_none_or(|expires_at| now < expires_at));
- Self::compact_order(state);
- }
-
- fn compact_order(state: &mut AuthorityState) {
- let entries = &state.entries;
- state.insertion_order.retain(|(sequence, logical_id)| {
- entries
- .get(logical_id)
- .is_some_and(|entry| entry.entry_sequence == *sequence)
- });
- }
-
- fn reserve_slot(
- &self,
- state: &mut AuthorityState,
- now: Instant,
- ) -> Result<(), ToolContinuationError> {
- Self::prune_expired(state, now);
- let candidates = state.insertion_order.len();
- for _ in 0..candidates {
- if state.entries.len() < self.capacity {
- return Ok(());
- }
- let Some((sequence, logical_id)) = state.insertion_order.pop_front() else {
- break;
- };
- let removable = state.entries.get(&logical_id).is_some_and(|entry| {
- entry.entry_sequence == sequence && !matches!(entry.state, MappingState::Pending(_))
- });
- if removable {
- state.entries.remove(&logical_id);
- } else if state
- .entries
- .get(&logical_id)
- .is_some_and(|entry| entry.entry_sequence == sequence)
- {
- state.insertion_order.push_back((sequence, logical_id));
- }
- }
- (state.entries.len() < self.capacity)
- .then_some(())
- .ok_or(ToolContinuationError::Capacity)
- }
-
- fn mark_conflicted(&self, entry: &mut StoredMapping, now: Instant) {
- entry.state = MappingState::Conflicted;
- entry.expires_at = Some(now.checked_add(self.ttl).unwrap_or(now));
- }
-}
-
-impl ToolContinuationAuthority for InMemoryToolContinuationAuthority {
- fn begin(
- &self,
- scope: ToolContinuationScopeV1,
- ) -> Result<ToolContinuationIssuanceV1, ToolContinuationError> {
- if !scope.is_complete() {
- return Err(ToolContinuationError::Unavailable);
- }
- let sequence = self.next_issuance.fetch_add(1, Ordering::Relaxed);
- if sequence == 0 {
- return Err(ToolContinuationError::Unavailable);
- }
- Ok(ToolContinuationIssuanceV1 { sequence, scope })
- }
-
- fn record_pending(
- &self,
- issuance: &ToolContinuationIssuanceV1,
- mapping: ToolIdMapEntryV1,
- now: Instant,
- ) -> Result<(), ToolContinuationError> {
- if mapping.logical_id.trim().is_empty()
- || mapping.native_id.trim().is_empty()
- || !mapping.owner.is_complete()
- {
- return Err(ToolContinuationError::Unavailable);
- }
- let mut state = self.lock();
- Self::prune_expired(&mut state, now);
- if let Some(current) = state.entries.get_mut(&mapping.logical_id) {
- let exact = current.scope == issuance.scope && current.mapping == mapping;
- match current.state {
- MappingState::Pending(sequence) if exact && sequence == issuance.sequence => {
- return Ok(());
- }
- MappingState::Active if exact => return Ok(()),
- MappingState::Pending(_) | MappingState::Active | MappingState::Conflicted => {
- self.mark_conflicted(current, now);
- return Err(ToolContinuationError::Conflict);
- }
- }
- }
- self.reserve_slot(&mut state, now)?;
- state.next_entry = state.next_entry.wrapping_add(1);
- if state.next_entry == 0 {
- state.next_entry = 1;
- }
- let entry_sequence = state.next_entry;
- state
- .insertion_order
- .push_back((entry_sequence, mapping.logical_id.clone()));
- state.entries.insert(
- mapping.logical_id.clone(),
- StoredMapping {
- scope: issuance.scope.clone(),
- mapping,
- state: MappingState::Pending(issuance.sequence),
- expires_at: None,
- entry_sequence,
- },
- );
- Ok(())
- }
-
- fn accept(
- &self,
- issuance: &ToolContinuationIssuanceV1,
- logical_id: &str,
- now: Instant,
- ) -> bool {
- let mut state = self.lock();
- Self::prune_expired(&mut state, now);
- let Some(entry) = state.entries.get_mut(logical_id) else {
- return false;
- };
- match entry.state {
- MappingState::Pending(sequence)
- if sequence == issuance.sequence && entry.scope == issuance.scope =>
- {
- entry.state = MappingState::Active;
- entry.expires_at = Some(now.checked_add(self.ttl).unwrap_or(now));
- true
- }
- MappingState::Active if entry.scope == issuance.scope => true,
- MappingState::Pending(_) | MappingState::Active | MappingState::Conflicted => false,
- }
- }
-
- fn abort_pending(&self, issuance: &ToolContinuationIssuanceV1) {
- let mut state = self.lock();
- state.entries.retain(|_, entry| {
- !matches!(entry.state, MappingState::Pending(sequence) if sequence == issuance.sequence)
- });
- Self::compact_order(&mut state);
- }
-
- fn resolve(
- &self,
- scope: &ToolContinuationScopeV1,
- logical_ids: &[String],
- now: Instant,
- ) -> Result<Vec<ToolIdMapEntryV1>, ToolContinuationError> {
- if !scope.is_complete() {
- return Err(ToolContinuationError::Unavailable);
- }
- let mut state = self.lock();
- Self::prune_expired(&mut state, now);
- logical_ids
- .iter()
- .map(|logical_id| {
- let entry = state
- .entries
- .get(logical_id)
- .ok_or(ToolContinuationError::Unavailable)?;
- if entry.scope != *scope || entry.state != MappingState::Active {
- return Err(ToolContinuationError::Unavailable);
- }
- Ok(entry.mapping.clone())
- })
- .collect()
- }
-}
-
-fn environment_usize(name: &str, default: usize) -> Result<usize, ToolContinuationError> {
- match std::env::var(name) {
- Ok(value) => value
- .parse::<usize>()
- .ok()
- .filter(|value| *value > 0)
- .ok_or(ToolContinuationError::InvalidConfiguration),
- Err(std::env::VarError::NotPresent) => Ok(default),
- Err(std::env::VarError::NotUnicode(_)) => Err(ToolContinuationError::InvalidConfiguration),
- }
-}
-
-fn environment_u64(name: &str, default: u64) -> Result<u64, ToolContinuationError> {
- match std::env::var(name) {
- Ok(value) => value
- .parse::<u64>()
- .ok()
- .filter(|value| *value > 0)
- .ok_or(ToolContinuationError::InvalidConfiguration),
- Err(std::env::VarError::NotPresent) => Ok(default),
- Err(std::env::VarError::NotUnicode(_)) => Err(ToolContinuationError::InvalidConfiguration),
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::server::core_runtime::model_ir::ExactProviderPathV1;
- use crate::server::request_plan::IngressProtocol;
-
- fn scope(name: &str) -> ToolContinuationScopeV1 {
- ToolContinuationScopeV1 {
- authority_id: format!("authority-{name}"),
- authority_epoch: 1,
- grant_id: "grant".into(),
- grant_generation: 1,
- served_model_id: "agent".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 7,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"plan"),
- },
- }
- }
-
- fn mapping(logical_id: &str, native_id: &str) -> ToolIdMapEntryV1 {
- ToolIdMapEntryV1 {
- logical_id: logical_id.into(),
- native_id: native_id.into(),
- kind: crate::server::core_runtime::model_ir::ToolKindV1::Function,
- name: "tool".into(),
- namespace: None,
- owner: ExactProviderPathV1 {
- provider_id: "provider".into(),
- endpoint_id: "endpoint".into(),
- entitlement_id: "entitlement".into(),
- connector_id: "connector".into(),
- connector_revision: "1".into(),
- capability_id: "capability".into(),
- capability_revision: "1".into(),
- model_configuration_id: "configuration".into(),
- native_model: "native".into(),
- upstream_protocol: IngressProtocol::Responses,
- adapter_revision: "1".into(),
- serializer_revision: "1".into(),
- decoder_revision: "1".into(),
- },
- }
- }
-
- #[test]
- fn pending_mapping_requires_downstream_acceptance_and_exact_scope() {
- let authority = InMemoryToolContinuationAuthority::new(4, Duration::from_secs(1)).unwrap();
- let now = Instant::now();
- let issuance = authority.begin(scope("a")).unwrap();
- let entry = mapping("hiroute_tool_v1_a", "provider-a");
- authority
- .record_pending(&issuance, entry.clone(), now)
- .unwrap();
- assert_eq!(
- authority.resolve(
- issuance.scope(),
- std::slice::from_ref(&entry.logical_id),
- now
- ),
- Err(ToolContinuationError::Unavailable)
- );
- assert!(authority.accept(&issuance, &entry.logical_id, now));
- assert_eq!(
- authority
- .resolve(
- issuance.scope(),
- std::slice::from_ref(&entry.logical_id),
- now,
- )
- .unwrap(),
- vec![entry.clone()]
- );
- assert_eq!(
- authority.resolve(&scope("b"), &[entry.logical_id], now),
- Err(ToolContinuationError::Unavailable)
- );
- }
-
- #[test]
- fn expiration_abort_and_conflict_fail_closed() {
- let authority =
- InMemoryToolContinuationAuthority::new(2, Duration::from_millis(5)).unwrap();
- let now = Instant::now();
- let issuance = authority.begin(scope("a")).unwrap();
- let expired = mapping("hiroute_tool_v1_expired", "provider-a");
- authority
- .record_pending(&issuance, expired.clone(), now)
- .unwrap();
- assert!(authority.accept(&issuance, &expired.logical_id, now));
- assert_eq!(
- authority.resolve(
- issuance.scope(),
- &[expired.logical_id],
- now + Duration::from_millis(6)
- ),
- Err(ToolContinuationError::Unavailable)
- );
-
- let pending = mapping("hiroute_tool_v1_pending", "provider-b");
- authority
- .record_pending(&issuance, pending.clone(), now)
- .unwrap();
- authority.abort_pending(&issuance);
- assert!(!authority.accept(&issuance, &pending.logical_id, now));
-
- let first = authority.begin(scope("a")).unwrap();
- let second = authority.begin(scope("b")).unwrap();
- let shared = mapping("hiroute_tool_v1_conflict", "provider-c");
- authority
- .record_pending(&first, shared.clone(), now)
- .unwrap();
- assert_eq!(
- authority.record_pending(&second, shared.clone(), now),
- Err(ToolContinuationError::Conflict)
- );
- assert!(!authority.accept(&first, &shared.logical_id, now));
- }
-}
diff --git a/crates/gateway/src/provider_state.rs b/crates/gateway/src/provider_state.rs
index d56a3798..1d1ec781 100644
--- a/crates/gateway/src/provider_state.rs
+++ b/crates/gateway/src/provider_state.rs
@@ -7,14 +7,24 @@ use std::time::{Duration, Instant};
use serde_json::Value;
use sha2::{Digest, Sha256};
-use crate::ports::ToolContinuationScopeV1;
use crate::server::core_runtime::model_ir::{ExactProviderPathV1, ModelIrError};
+use crate::server::request_plan::IngressProtocol;
const CAPACITY: usize = 4096;
const IDLE_TTL: Duration = Duration::from_secs(3600);
const MAX_PATTERN: usize = 256 * 1024;
const MAX_PENDING_BYTES: usize = 4 * 1024 * 1024;
-type Key = (ToolContinuationScopeV1, [u8; 32]);
+/// Authorization scope for opaque provider state, never for ordinary tool IDs.
+#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
+pub(crate) struct ProviderStateScopeV1 {
+ pub authority_id: String,
+ pub authority_epoch: u64,
+ pub grant_id: String,
+ pub grant_generation: u64,
+ pub served_model_id: String,
+ pub route: hiroute_domain::ModelRequestRouteV2,
+}
+type Key = (ProviderStateScopeV1, [u8; 32]);
#[derive(Default)]
pub(crate) struct ProviderStateStore(Mutex<BTreeMap<Key, Entry>>);
@@ -25,7 +35,7 @@ struct Entry {
}
impl ProviderStateStore {
- fn accept(&self, scope: &ToolContinuationScopeV1, pending: &Pending, now: Instant) {
+ fn accept(&self, scope: &ProviderStateScopeV1, pending: &Pending, now: Instant) {
let mut entries = self.0.lock().unwrap_or_else(|e| e.into_inner());
entries.retain(|_, entry| entry.expires > now);
let key = (scope.clone(), pending.digest);
@@ -54,7 +64,7 @@ impl ProviderStateStore {
pub(crate) fn resolve(
&self,
- scope: &ToolContinuationScopeV1,
+ scope: &ProviderStateScopeV1,
document: &Value,
now: Instant,
) -> Result<Option<ExactProviderPathV1>, ModelIrError> {
@@ -62,28 +72,45 @@ impl ProviderStateStore {
entries.retain(|_, entry| entry.expires > now);
let mut owner = None;
let mut resolved_keys = Vec::new();
- if let Some(input) = document.get("input").and_then(Value::as_array) {
- for item in input.iter().filter(|item| item["type"] == "reasoning") {
- let state = match item.get("encrypted_content") {
- None | Some(Value::Null) => continue,
- Some(Value::String(state)) if state.is_empty() => continue,
- Some(Value::String(state)) => state,
- Some(_) => return Err(ModelIrError::InvalidField("encrypted_content")),
- };
- let key = (scope.clone(), digest(state));
- let entry = entries
- .get(&key)
- .ok_or(ModelIrError::ProviderStateOwnershipRequired)?;
- let next = entry
- .owner
- .as_ref()
- .ok_or(ModelIrError::ProviderStateNotPortable)?;
- if owner.as_ref().is_some_and(|current| current != next) {
- return Err(ModelIrError::ProviderStateNotPortable);
- }
- owner = Some(next.clone());
- resolved_keys.push(key);
+ let states = document
+ .get("input")
+ .and_then(Value::as_array)
+ .into_iter()
+ .flatten()
+ .filter(|item| item["type"] == "reasoning")
+ .map(|item| item.get("encrypted_content"))
+ .chain(
+ document
+ .get("messages")
+ .and_then(Value::as_array)
+ .into_iter()
+ .flatten()
+ .filter(|message| message["role"] == "assistant")
+ .filter_map(|message| message.get("content").and_then(Value::as_array))
+ .flatten()
+ .filter(|block| block["type"] == "thinking")
+ .map(|block| block.get("signature")),
+ );
+ for value in states {
+ let state = match value {
+ None | Some(Value::Null) => continue,
+ Some(Value::String(state)) if state.is_empty() => continue,
+ Some(Value::String(state)) => state,
+ Some(_) => return Err(ModelIrError::InvalidField("encrypted_content")),
+ };
+ let key = (scope.clone(), digest(state));
+ let entry = entries
+ .get(&key)
+ .ok_or(ModelIrError::ProviderStateOwnershipRequired)?;
+ let next = entry
+ .owner
+ .as_ref()
+ .ok_or(ModelIrError::ProviderStateNotPortable)?;
+ if owner.as_ref().is_some_and(|current| current != next) {
+ return Err(ModelIrError::ProviderStateNotPortable);
}
+ owner = Some(next.clone());
+ resolved_keys.push(key);
}
// Renew only after the entire replay passes ownership validation.
for key in resolved_keys {
@@ -109,16 +136,22 @@ struct Pending {
#[derive(Clone)]
pub(crate) struct ActiveProviderStates {
store: Arc<ProviderStateStore>,
- scope: ToolContinuationScopeV1,
+ scope: ProviderStateScopeV1,
pending: Arc<Mutex<Vec<Pending>>>,
+ downstream: IngressProtocol,
}
impl ActiveProviderStates {
- pub(crate) fn new(store: Arc<ProviderStateStore>, scope: ToolContinuationScopeV1) -> Self {
+ pub(crate) fn new(
+ store: Arc<ProviderStateStore>,
+ scope: ProviderStateScopeV1,
+ downstream: IngressProtocol,
+ ) -> Self {
Self {
store,
scope,
pending: Arc::new(Mutex::new(Vec::new())),
+ downstream,
}
}
@@ -131,14 +164,32 @@ impl ActiveProviderStates {
.as_str()
.filter(|s| !s.is_empty())
.ok_or(ModelIrError::InvalidField("encrypted_content"))?;
- // This exact key/value fragment is written by the native Responses renderer.
+ // Responses ciphertext is surfaced as a thinking signature for Messages.
+ // Bind the actual downstream representation, not the upstream field name.
// Quotes inside user text are escaped and cannot activate this fragment.
+ let field = match self.downstream {
+ IngressProtocol::Responses => "encrypted_content",
+ IngressProtocol::Messages => "signature",
+ _ => return Err(ModelIrError::ProviderStateNotPortable),
+ };
let pattern = format!(
- "\"encrypted_content\":{}",
+ "\"{field}\":{}",
serde_json::to_string(state)
.map_err(|_| ModelIrError::InvalidField("encrypted_content"))?
)
.into_bytes();
+ self.record_at_acceptance(state, owner, pattern)
+ }
+
+ pub(crate) fn record_at_acceptance(
+ &self,
+ state: &str,
+ owner: &ExactProviderPathV1,
+ pattern: Vec<u8>,
+ ) -> Result<(), ModelIrError> {
+ if state.is_empty() || state.len() > MAX_PATTERN || pattern.is_empty() {
+ return Err(ModelIrError::InvalidField("provider_state"));
+ }
if pattern.len() > MAX_PATTERN {
return Err(ModelIrError::BufferLimit(MAX_PATTERN));
}
@@ -216,8 +267,8 @@ mod tests {
use crate::server::request_plan::IngressProtocol;
use serde_json::json;
- fn scope() -> ToolContinuationScopeV1 {
- ToolContinuationScopeV1 {
+ fn scope() -> ProviderStateScopeV1 {
+ ProviderStateScopeV1 {
authority_id: "a".into(),
authority_epoch: 1,
grant_id: "g".into(),
@@ -245,10 +296,65 @@ mod tests {
json!({"input":[{"type":"reasoning","encrypted_content":state}]})
}
+ #[test]
+ fn fragmented_native_signature_requires_accepted_matching_block_close() {
+ let store = Arc::new(ProviderStateStore::default());
+ let active = ActiveProviderStates::new(store.clone(), scope(), IngressProtocol::Messages);
+ let now = Instant::now();
+ let replay = json!({"messages":[{"role":"assistant","content":[
+ {"type":"thinking","thinking":"","signature":"combined-signature"}
+ ]}]});
+ let closing = br#"{ "index": 0, "type": "content_block_stop" }"#;
+ active
+ .record_at_acceptance("combined-signature", &owner("luna"), closing.to_vec())
+ .unwrap();
+ let mut scanner = active.scanner();
+ scanner.accept_bytes(br#"{"signature":"combined-signature"}"#, now);
+ scanner.accept_bytes(br#"{ "index": 1, "type": "content_block_stop" }"#, now);
+ assert!(store.resolve(&scope(), &replay, now).is_err());
+ for part in closing.chunks(2) {
+ scanner.accept_bytes(part, now);
+ }
+ assert_eq!(
+ store.resolve(&scope(), &replay, now).unwrap(),
+ Some(owner("luna"))
+ );
+ }
+
+ #[test]
+ fn messages_signature_binds_only_accepted_responses_ciphertext() {
+ let store = Arc::new(ProviderStateStore::default());
+ let active = ActiveProviderStates::new(store.clone(), scope(), IngressProtocol::Messages);
+ let now = Instant::now();
+ let replay = json!({"messages":[{"role":"assistant","content":[
+ {"type":"thinking","thinking":"","signature":"luna-state"},
+ {"type":"tool_use","id":"tool-one","name":"Bash","input":{"command":"pwd"}}
+ ]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"tool-one","content":"/work"}]}]});
+ active.record(&json!("luna-state"), &owner("luna")).unwrap();
+ assert!(store.resolve(&scope(), &replay, now).is_err());
+ let mut scanner = active.scanner();
+ scanner.accept_bytes(br#"{"encrypted_content":"luna-state"}"#, now);
+ assert!(store.resolve(&scope(), &replay, now).is_err());
+ let escaped = json!({"text":"\"signature\":\"luna-state\""}).to_string();
+ scanner.accept_bytes(escaped.as_bytes(), now);
+ assert!(store.resolve(&scope(), &replay, now).is_err());
+ for part in br#"data: {"type":"content_block_delta","delta":{"type":"signature_delta","signature":"luna-state"}}"#.chunks(3) {
+ scanner.accept_bytes(part, now);
+ }
+ assert_eq!(
+ store.resolve(&scope(), &replay, now).unwrap(),
+ Some(owner("luna"))
+ );
+ let mut foreign = scope();
+ foreign.grant_generation += 1;
+ assert!(store.resolve(&foreign, &replay, now).is_err());
+ assert!(store.resolve(&scope(), &replay, now + IDLE_TTL).is_err());
+ }
+
#[test]
fn only_accepted_ciphertext_resolves_and_scope_expiry_are_enforced() {
let store = Arc::new(ProviderStateStore::default());
- let active = ActiveProviderStates::new(store.clone(), scope());
+ let active = ActiveProviderStates::new(store.clone(), scope(), IngressProtocol::Responses);
let now = Instant::now();
let state = "fixture-\\\"ciphertext";
active.record(&json!(state), &owner("luna")).unwrap();
@@ -282,7 +388,8 @@ mod tests {
let store = Arc::new(ProviderStateStore::default());
let now = Instant::now();
for (value, model) in [("one", "luna"), ("two", "terra")] {
- let active = ActiveProviderStates::new(store.clone(), scope());
+ let active =
+ ActiveProviderStates::new(store.clone(), scope(), IngressProtocol::Responses);
active.record(&json!(value), &owner(model)).unwrap();
active.scanner().accept_bytes(
json!({"encrypted_content":value}).to_string().as_bytes(),
@@ -294,7 +401,7 @@ mod tests {
store.resolve(&scope(), &mixed, now),
Err(ModelIrError::ProviderStateNotPortable)
);
- let active = ActiveProviderStates::new(store.clone(), scope());
+ let active = ActiveProviderStates::new(store.clone(), scope(), IngressProtocol::Responses);
active.record(&json!("one"), &owner("terra")).unwrap();
active
.scanner()
@@ -304,7 +411,7 @@ mod tests {
Err(ModelIrError::ProviderStateNotPortable)
);
assert!(
- ActiveProviderStates::new(store, scope())
+ ActiveProviderStates::new(store, scope(), IngressProtocol::Responses)
.record(&json!("x".repeat(MAX_PATTERN)), &owner("luna"))
.is_err()
);
@@ -313,7 +420,7 @@ mod tests {
#[test]
fn active_replay_renews_idle_deadline_but_eventually_expires() {
let store = Arc::new(ProviderStateStore::default());
- let active = ActiveProviderStates::new(store.clone(), scope());
+ let active = ActiveProviderStates::new(store.clone(), scope(), IngressProtocol::Responses);
let now = Instant::now();
active.record(&json!("state"), &owner("luna")).unwrap();
active
@@ -351,7 +458,8 @@ mod tests {
let store = Arc::new(ProviderStateStore::default());
let now = Instant::now();
for (state, model) in [("state", "luna"), ("terra-state", "terra")] {
- let active = ActiveProviderStates::new(store.clone(), scope());
+ let active =
+ ActiveProviderStates::new(store.clone(), scope(), IngressProtocol::Responses);
active.record(&json!(state), &owner(model)).unwrap();
active.scanner().accept_bytes(
json!({"encrypted_content":state}).to_string().as_bytes(),
diff --git a/crates/gateway/src/server.rs b/crates/gateway/src/server.rs
index 07476728..62ff6cca 100644
--- a/crates/gateway/src/server.rs
+++ b/crates/gateway/src/server.rs
@@ -35,7 +35,6 @@ use self::publication::{
use self::test_control::{
E2eControlEndpoint, E2eControlError, E2eControlHandle, E2eControlOptions,
};
-use crate::ports::InMemoryToolContinuationAuthority;
use async_trait::async_trait;
use bytes::Bytes;
use hiroute_gateway_core::transport::pingora::GatewayHttpApp;
@@ -377,15 +376,7 @@ impl GatewayLauncher {
Some(path) => Arc::new(FilePlannerInputAuthority::open(path)?),
None => Arc::new(PublicationPlannerInputAuthority),
};
- let tool_continuations = Arc::new(
- InMemoryToolContinuationAuthority::from_environment()
- .map_err(|_| PortError::Rejected)?,
- );
- let runtime = ProductionGatewayRuntime::compose_with_planner_and_continuations(
- ports,
- planner,
- tool_continuations,
- );
+ let runtime = ProductionGatewayRuntime::compose_with_planner(ports, planner);
let runtime = match launch_identity.executable_sha256() {
Some(digest) => runtime.with_executable_sha256(Arc::clone(digest)),
None => runtime,
@@ -453,11 +444,7 @@ impl GatewayLauncher {
GatewayMode::Production {
runtime,
test_control,
- } => {
- self::core_runtime::adapters::install_process_tool_id_codec()
- .map_err(|_| GatewayLauncherError::ContinuationKeyUnavailable)?;
- (ListenerLifecycle::Production(runtime), test_control)
- }
+ } => (ListenerLifecycle::Production(runtime), test_control),
};
#[cfg(not(feature = "e2e-test-control"))]
let lifecycle = match mode {
@@ -467,11 +454,7 @@ impl GatewayLauncher {
readiness_path: fixture.readiness.path,
executable_sha256: launch_identity.executable_sha256,
}),
- GatewayMode::Production { runtime } => {
- self::core_runtime::adapters::install_process_tool_id_codec()
- .map_err(|_| GatewayLauncherError::ContinuationKeyUnavailable)?;
- ListenerLifecycle::Production(runtime)
- }
+ GatewayMode::Production { runtime } => ListenerLifecycle::Production(runtime),
};
let mut server = match run_style {
ListenerRunStyle::Standalone => pingora_core::server::Server::new(None)
diff --git a/e2e/matrix/p0-gateway-coverage.json b/e2e/matrix/p0-gateway-coverage.json
index 32431561..4b9c4dc4 100644
--- a/e2e/matrix/p0-gateway-coverage.json
+++ b/e2e/matrix/p0-gateway-coverage.json
@@ -63,13 +63,13 @@
"protocol.stream_usage_terminal",
"protocol.namespace_native_acceptance",
"protocol.namespace_upstream_rejection",
- "protocol.continuation_owner_affinity",
- "protocol.continuation_preconnect_refusal"
+ "protocol.continuation_request_authentication",
+ "protocol.continuation_restart_native_history"
],
"execution": "real_hirouted_native_provider",
"id": "protocol.tool_and_sse",
"source": "tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs",
- "symbol": "continuation::production_tool_continuation_restores_native_id_and_fails_closed_outside_scope",
+ "symbol": "continuation::production_native_tool_history_survives_restart_with_request_authentication",
"test_target": "p0_gateway_protocol"
},
{
@@ -301,7 +301,7 @@
"test_target": "p0_gateway_privacy"
}
],
- "registry_digest": "sha256:957d559bfa1a1d63e97e5bdc20d32865e261031ddf6c9e27e4b36be614281b8a",
+ "registry_digest": "sha256:061cda488951676f3f8a60b38467ba345ee519249f999cb21f75aac9ab793d62",
"rows": [
{
"id": "publication.incompatible_or_gap",
diff --git a/tools/e2e-harness/src/p0/coverage.rs b/tools/e2e-harness/src/p0/coverage.rs
index 3ef64f9f..af3663f5 100644
--- a/tools/e2e-harness/src/p0/coverage.rs
+++ b/tools/e2e-harness/src/p0/coverage.rs
@@ -169,7 +169,7 @@ pub const RECEIPTS: &[FrozenReceipt] = &[
runtime_receipt!(
"protocol.tool_and_sse",
"p0_gateway_protocol",
- "continuation::production_tool_continuation_restores_native_id_and_fails_closed_outside_scope",
+ "continuation::production_native_tool_history_survives_restart_with_request_authentication",
"tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs",
[
"protocol.tool_round_trip",
@@ -178,8 +178,8 @@ pub const RECEIPTS: &[FrozenReceipt] = &[
"protocol.stream_usage_terminal",
"protocol.namespace_native_acceptance",
"protocol.namespace_upstream_rejection",
- "protocol.continuation_owner_affinity",
- "protocol.continuation_preconnect_refusal",
+ "protocol.continuation_request_authentication",
+ "protocol.continuation_restart_native_history",
]
),
receipt!(
diff --git a/tools/e2e-harness/tests/p0_gateway_observation.rs b/tools/e2e-harness/tests/p0_gateway_observation.rs
index eac60b26..63172ec5 100644
--- a/tools/e2e-harness/tests/p0_gateway_observation.rs
+++ b/tools/e2e-harness/tests/p0_gateway_observation.rs
@@ -50,13 +50,7 @@ fn real_hirouted_emits_request_route_attempt_commit_usage_and_accepted_only_cont
assert_eq!(response.status, 200);
assert!(String::from_utf8_lossy(&response.body).contains("accepted-response-22008"));
let downstream_logical_id = response_tool_logical_id(&response.body);
- assert!(downstream_logical_id.starts_with("hiroute_tool_v1_"));
- assert!(
- !response
- .body
- .windows("call-22008".len())
- .any(|value| value == b"call-22008")
- );
+ assert_eq!(downstream_logical_id, "call-22008");
assert_eq!(rejected.calls(), 1);
assert_eq!(accepted.calls(), 1);
wait_replay_empty(&fixture.replay_root);
@@ -343,7 +337,9 @@ fn real_hirouted_emits_request_route_attempt_commit_usage_and_accepted_only_cont
assert_eq!(observed_tool["namespace"], "weather-services");
assert!(observed_tool.get("native_id").is_none());
assert!(observed_tool.get("owner").is_none());
- assert!(!response_content.contains("call-22008"));
+ // The public call ID is preserved; provider-only item IDs remain off the
+ // canonical observation path.
+ assert!(response_content.contains("call-22008"));
assert!(!response_content.contains("fc-native-22008"));
assert!(!response_content.contains("rejected-attempt-secret-22008"));
assert!(content.iter().all(|event| {
@@ -710,13 +706,7 @@ fn real_hirouted_isolates_slow_fail_and_panic_sinks_and_reports_each_gap() {
let started = Instant::now();
let first_response = fixture.request();
assert_eq!(first_response.status, 200);
- assert!(response_tool_logical_id(&first_response.body).starts_with("hiroute_tool_v1_"));
- assert!(
- !first_response
- .body
- .windows("call-22008".len())
- .any(|value| value == b"call-22008")
- );
+ assert_eq!(response_tool_logical_id(&first_response.body), "call-22008");
assert!(
started.elapsed() < Duration::from_millis(750),
"a one-second sink must not backpressure the model response"
@@ -745,7 +735,10 @@ fn real_hirouted_isolates_slow_fail_and_panic_sinks_and_reports_each_gap() {
// cooldown-isolation assertion.
let second_response = fixture.request();
assert_eq!(second_response.status, 200);
- assert!(response_tool_logical_id(&second_response.body).starts_with("hiroute_tool_v1_"));
+ assert_eq!(
+ response_tool_logical_id(&second_response.body),
+ "call-22008"
+ );
assert_eq!(first.calls(), 1, "cooldown must survive observation faults");
assert_eq!(second.calls(), 2);
}
diff --git a/tools/e2e-harness/tests/p0_gateway_planner.rs b/tools/e2e-harness/tests/p0_gateway_planner.rs
index f7b709d2..eaa08f01 100644
--- a/tools/e2e-harness/tests/p0_gateway_planner.rs
+++ b/tools/e2e-harness/tests/p0_gateway_planner.rs
@@ -119,7 +119,6 @@ fn corpus_request(case: &CorpusCase) -> ModelRequestIRV1 {
content: vec![ContentPart::ToolResult {
logical_id: "tool-1".into(),
tool_kind: ToolKindV1::Function,
- namespace: None,
output: ToolOutput::Text("opaque output is excluded".into()),
status: ToolResultStatusV1::Unknown,
}],
@@ -170,7 +169,6 @@ fn corpus_request(case: &CorpusCase) -> ModelRequestIRV1 {
requested_reasoning: RequestedReasoningControl::absent(),
requested_max_output_tokens: None,
provider_state: Vec::new(),
- tool_id_map: Vec::new(),
}
}
diff --git a/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs b/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs
index 5bb37d77..449a3251 100644
--- a/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs
+++ b/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs
@@ -8,13 +8,9 @@ use std::time::{Duration, Instant};
use hiroute_e2e::gateway_fixture::{
TestTlsListener, TestTlsStream, sealed_native_candidate, write_dial_config,
};
-use hiroute_gateway::ports::{
- InMemoryToolContinuationAuthority, ToolContinuationAuthority, ToolContinuationScopeV1,
-};
use hiroute_gateway::server::core_runtime::adapters::{
- decode_ingress_request_with_tool_resolver, project_candidate_request,
+ decode_ingress_request, project_candidate_request,
};
-use hiroute_gateway::server::core_runtime::model_ir::{ToolIdMapEntryV1, ToolKindV1};
use hiroute_gateway::server::publication::{
AliasComplexityClassifierV1, AliasGroupIdV1, AliasModelGroupV1, AliasPlanV1,
AliasRequestOwnedRouteV1, AliasRoutingV1, GatewayPublicationSnapshotV3, GrantV1, token_sha256,
@@ -46,7 +42,7 @@ const NAMESPACE_REJECTED: &[u8] =
const SSE_FRAGMENT_SIZES: &[usize] = &[1, 2, 7, 3, 11, 1, 17, 5, 2, 29, 13];
#[test]
-fn production_tool_continuation_restores_native_id_and_fails_closed_outside_scope() {
+fn production_native_tool_history_survives_restart_with_request_authentication() {
let mut receipt = hiroute_e2e::p0_runtime_execution_receipt!("protocol.tool_and_sse");
let _serial = process_test_lock();
let directory = tempfile::tempdir().unwrap();
@@ -54,6 +50,7 @@ fn production_tool_continuation_restores_native_id_and_fails_closed_outside_scop
ProviderReply::Sse(FIRST_PROVIDER_STREAM),
ProviderReply::Json(SECOND_PROVIDER_JSON),
ProviderReply::Json(NAMESPACE_ONLY_PROVIDER_JSON),
+ ProviderReply::Json(SECOND_PROVIDER_JSON),
]);
let rejecting_provider = ContinuationProvider::start(vec![ProviderReply::JsonStatus {
status: 400,
@@ -149,7 +146,6 @@ fn production_tool_continuation_restores_native_id_and_fails_closed_outside_scop
&credentials_path,
directory.path(),
&[
- ("HIROUTE_TOOL_CONTINUATION_TTL_MS", "2000"),
("HIROUTE_REPLAY_ROOT", replay_root.as_str()),
("HIROUTE_REPLAY_MEMORY_THRESHOLD", "1024"),
("HIROUTE_REPLAY_RECORD_BYTES", "256"),
@@ -208,11 +204,7 @@ fn production_tool_continuation_restores_native_id_and_fails_closed_outside_scop
assert_eq!(projected["input"][0]["namespace"], "weather-services");
assert_eq!(projected["input"][0]["arguments"], large_arguments);
assert_eq!(projected["input"][1]["output"], large_result);
- assert!(
- !http_body(&attempts[1])
- .windows(logical_id.len())
- .any(|window| window == logical_id.as_bytes())
- );
+ assert_eq!(logical_id, "provider-weather-7");
receipt.mark_assertion("protocol.native_tool_identity");
receipt.mark_assertion("protocol.tool_round_trip");
@@ -281,69 +273,11 @@ fn production_tool_continuation_restores_native_id_and_fails_closed_outside_scop
assert_eq!(rejected_document["tools"][2]["name"], "archive-services");
receipt.mark_assertion("protocol.namespace_upstream_rejection");
- let providers = ContinuationProviders {
- continuation: &provider,
- rejecting: &rejecting_provider,
- forbidden: &forbidden_provider,
- };
- assert_rejected_without_attempt(
- address,
- "continuation-token",
- &json!({
- "model":"continuation",
- "input":[{
- "type":"function_call_output",
- "call_id":"hiroute_tool_v1_ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
- "output":"forged"
- }]
- }),
- &providers,
- 3,
- 1,
- );
- assert_rejected_without_attempt(
- address,
- "continuation-token",
- &json!({
- "model":"continuation",
- "input":[{
- "type":"function_call_output",
- "call_id":"provider-weather-7",
- "output":"client-forged-native-id"
- }]
- }),
- &providers,
- 3,
- 1,
- );
- assert_rejected_without_attempt(
- address,
- "continuation-token",
- &continuation_request("other-plan", &logical_id, &large_arguments, &large_result),
- &providers,
- 3,
- 1,
- );
- assert_rejected_without_attempt(address, "other-token", &continuation, &providers, 3, 1);
- receipt.mark_assertion("protocol.continuation_owner_affinity");
- assert_rejected_without_attempt(
- address,
- "continuation-token",
- &conflicting_request(&logical_id),
- &providers,
- 3,
- 1,
- );
-
- std::thread::sleep(Duration::from_millis(2_100));
- assert_rejected_without_attempt(
- address,
- "continuation-token",
- &continuation,
- &providers,
- 3,
- 1,
- );
+ let unauthorized = request(address, "invalid-token", &continuation);
+ assert_eq!(unauthorized.status, 401);
+ assert_eq!(provider.calls(), 3);
+ assert_eq!(forbidden_provider.calls(), 0);
+ receipt.mark_assertion("protocol.continuation_request_authentication");
process.stop();
let mut restarted = Hirouted::spawn_with_environment(
@@ -354,102 +288,50 @@ fn production_tool_continuation_restores_native_id_and_fails_closed_outside_scop
&credentials_path,
directory.path(),
&[
- ("HIROUTE_TOOL_CONTINUATION_TTL_MS", "2000"),
("HIROUTE_REPLAY_ROOT", replay_root.as_str()),
("HIROUTE_REPLAY_MEMORY_THRESHOLD", "1024"),
("HIROUTE_REPLAY_RECORD_BYTES", "256"),
],
);
restarted.wait_ready();
- assert_rejected_without_attempt(
- address,
- "continuation-token",
- &continuation,
- &providers,
- 3,
- 1,
+ let mut repeated = continuation.clone();
+ repeated["input"].as_array_mut().unwrap().extend([
+ json!({"type":"function_call","call_id":logical_id,"namespace":"weather-services","name":"weather","arguments":"{}"}),
+ json!({"type":"function_call_output","call_id":logical_id,"output":"second occurrence"}),
+ ]);
+ let resumed = request(address, "continuation-token", &repeated);
+ assert_eq!(
+ resumed.status,
+ 200,
+ "{}",
+ String::from_utf8_lossy(&resumed.body)
);
+ wait_for_calls(&provider, 4);
+ let resumed_requests = provider.requests();
+ let resumed_body: Value = serde_json::from_slice(http_body(&resumed_requests[3])).unwrap();
+ assert_eq!(resumed_body["input"][0]["call_id"], "provider-weather-7");
+ assert_eq!(resumed_body["input"][1]["call_id"], "provider-weather-7");
+ assert_eq!(resumed_body["input"][1]["output"], large_result);
+ assert_eq!(resumed_body["input"][2]["call_id"], "provider-weather-7");
+ assert_eq!(resumed_body["input"][3]["output"], "second occurrence");
restarted.stop();
- receipt.mark_assertion("protocol.continuation_preconnect_refusal");
+ receipt.mark_assertion("protocol.continuation_restart_native_history");
receipt.finish();
}
#[test]
fn continuation_binding_projects_exact_native_id_across_three_by_three_corpus() {
- const LOGICAL_ID: &str =
- "hiroute_tool_v1_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
for ingress in PROTOCOLS {
for upstream in PROTOCOLS {
- let mut document = request_fixture(ingress);
- replace_string(&mut document, "call_weather", LOGICAL_ID);
- let profile = candidate_profile(ingress, upstream);
- let mapping = ToolIdMapEntryV1 {
- logical_id: LOGICAL_ID.into(),
- native_id: format!("native-{ingress:?}-{upstream:?}"),
- kind: ToolKindV1::Function,
- name: "weather".into(),
- namespace: None,
- owner: profile.exact_provider_path().unwrap(),
- };
- let authority =
- InMemoryToolContinuationAuthority::new(4, Duration::from_secs(1)).unwrap();
- let scope = ToolContinuationScopeV1 {
- authority_id: "corpus-authority".into(),
- authority_epoch: 1,
- grant_id: "corpus-grant".into(),
- grant_generation: 1,
- served_model_id: "agent/research".into(),
- route: hiroute_domain::ModelRequestRouteV2::Plan {
- revision: 1,
- semantic_digest: hiroute_domain::CanonicalDigest::of_bytes(b"corpus"),
- },
- };
- let issuance = authority.begin(scope.clone()).unwrap();
- let now = Instant::now();
- authority
- .record_pending(&issuance, mapping.clone(), now)
- .unwrap();
- assert!(authority.accept(&issuance, LOGICAL_ID, now));
- let request = decode_ingress_request_with_tool_resolver(
- ingress,
- &document,
- |logical_ids| authority.resolve(&scope, logical_ids, now).map_err(|_| {
- hiroute_gateway::server::core_runtime::model_ir::ModelIrError::ToolContinuationUnavailable
- }),
- )
- .unwrap();
- let projected = project_candidate_request(&request, &profile).unwrap();
+ let document = request_fixture(ingress);
+ let request = decode_ingress_request(ingress, &document).unwrap();
+ let projected =
+ project_candidate_request(&request, &candidate_profile(ingress, upstream)).unwrap();
assert!(
- projected
- .bytes
- .windows(mapping.native_id.len())
- .any(|window| window == mapping.native_id.as_bytes()),
- "ingress={ingress:?} upstream={upstream:?}"
+ String::from_utf8_lossy(&projected.bytes).contains("call_weather"),
+ "{ingress:?} -> {upstream:?}"
);
- assert!(
- !projected
- .bytes
- .windows(LOGICAL_ID.len())
- .any(|window| window == LOGICAL_ID.as_bytes())
- );
- }
- }
-}
-
-fn replace_string(value: &mut Value, from: &str, to: &str) {
- match value {
- Value::String(value) if value == from => *value = to.into(),
- Value::Array(values) => {
- for value in values {
- replace_string(value, from, to);
- }
- }
- Value::Object(values) => {
- for value in values.values_mut() {
- replace_string(value, from, to);
- }
}
- Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {}
}
}
@@ -537,63 +419,6 @@ fn continuation_request(model: &str, logical_id: &str, arguments: &str, output:
})
}
-fn conflicting_request(logical_id: &str) -> Value {
- json!({
- "model": "continuation",
- "input": [
- {
- "type":"function_call",
- "call_id":logical_id,
- "namespace":"weather-services",
- "name":"weather",
- "arguments":"{\"city\":\"Paris\"}"
- },
- {
- "type":"function_call",
- "call_id":logical_id,
- "namespace":"archive-services",
- "name":"weather",
- "arguments":"{}"
- }
- ]
- })
-}
-
-struct ContinuationProviders<'a> {
- continuation: &'a ContinuationProvider,
- rejecting: &'a ContinuationProvider,
- forbidden: &'a ContinuationProvider,
-}
-
-fn assert_rejected_without_attempt(
- address: SocketAddr,
- token: &str,
- document: &Value,
- providers: &ContinuationProviders<'_>,
- expected_attempts: usize,
- expected_rejections: usize,
-) {
- let provider_connections = providers.continuation.connections();
- let rejecting_connections = providers.rejecting.connections();
- let forbidden_connections = providers.forbidden.connections();
- let response = request(address, token, document);
- assert_eq!(
- response.status,
- 400,
- "{}",
- String::from_utf8_lossy(&response.body)
- );
- let error: Value = serde_json::from_slice(&response.body).unwrap();
- assert_eq!(error["code"], "TOOL_CONTINUATION_UNAVAILABLE");
- assert_eq!(error["phase"], "continuation_authority");
- assert_eq!(providers.continuation.calls(), expected_attempts);
- assert_eq!(providers.continuation.connections(), provider_connections);
- assert_eq!(providers.rejecting.calls(), expected_rejections);
- assert_eq!(providers.rejecting.connections(), rejecting_connections);
- assert_eq!(providers.forbidden.calls(), 0);
- assert_eq!(providers.forbidden.connections(), forbidden_connections);
-}
-
fn assert_fragmented_tool_stream(body: &[u8], fragments_written: usize) -> String {
assert_eq!(
fragments_written,
@@ -612,8 +437,7 @@ fn assert_fragmented_tool_stream(body: &[u8], fragments_written: usize) -> Strin
.and_then(Value::as_str)
.expect("downstream Tool-call logical ID")
.to_owned();
- assert!(logical_id.starts_with("hiroute_tool_v1_"));
- assert_ne!(logical_id, "provider-weather-7");
+ assert_eq!(logical_id, "provider-weather-7");
assert_eq!(
events,
vec![
@@ -655,14 +479,10 @@ fn decode_downstream_sse(body: &[u8]) -> Vec<(String, Value)> {
.and_then(|line| line.strip_prefix("event: "))
.expect("SSE event name");
let data = lines
- .next()
- .and_then(|line| line.strip_prefix("data: "))
- .expect("SSE event data");
- assert!(
- lines.next().is_none(),
- "SSE event has only name and JSON data"
- );
- let value: Value = serde_json::from_str(data).expect("SSE JSON data");
+ .map(|line| line.strip_prefix("data: ").expect("SSE JSON data line"))
+ .collect::<Vec<_>>()
+ .join("\n");
+ let value: Value = serde_json::from_str(&data).expect("SSE JSON data");
assert_eq!(value["type"], name, "SSE name and JSON type agree");
(name.to_owned(), value)
})
@@ -670,14 +490,23 @@ fn decode_downstream_sse(body: &[u8]) -> Vec<(String, Value)> {
}
fn request(address: SocketAddr, token: &str, document: &Value) -> WireResponse {
+ request_at_path(address, token, document, "/v1/responses")
+}
+
+fn request_at_path(address: SocketAddr, token: &str, document: &Value, path: &str) -> WireResponse {
let body = serialize_document_with_model_first(document);
+ let auth = if path == "/v1/messages" {
+ format!("Authorization: Bearer {token}")
+ } else {
+ format!("X-HiRoute-Token: {token}")
+ };
let mut stream = TcpStream::connect_timeout(&address, Duration::from_secs(2)).unwrap();
stream
.set_read_timeout(Some(Duration::from_secs(15)))
.unwrap();
write!(
stream,
- "POST /v1/responses HTTP/1.1\r\nHost: {address}\r\nX-HiRoute-Token: {token}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
+ "POST {path} HTTP/1.1\r\nHost: {address}\r\n{auth}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
)
.unwrap();
diff --git a/tools/e2e-harness/tests/p0_gateway_protocol/matrix.rs b/tools/e2e-harness/tests/p0_gateway_protocol/matrix.rs
index 1485f591..10c4665e 100644
--- a/tools/e2e-harness/tests/p0_gateway_protocol/matrix.rs
+++ b/tools/e2e-harness/tests/p0_gateway_protocol/matrix.rs
@@ -5,8 +5,7 @@ use hiroute_gateway::server::core_runtime::adapters::{
ResponseDecodeStatus, decode_ingress_request, project_candidate_request,
};
use hiroute_gateway::server::core_runtime::model_ir::{
- ContentPart, ModelEvent, ModelIrError, RequestedReasoningDisposition, ToolIdMapEntryV1,
- ToolKindV1, ToolResultStatusV1,
+ ContentPart, ModelEvent, ModelIrError, RequestedReasoningDisposition, ToolResultStatusV1,
};
use hiroute_gateway::server::core_runtime::profiles::{
CandidateProtocolProfile, ClientProtocolProfile, CriticalFact, NativeProviderStateEmission,
@@ -59,8 +58,8 @@ fn protocol_request_matrix_preserves_shared_content_and_exact_status_semantics()
let projected = requests
.iter()
.map(|request| {
- let mut profile = candidate_profile(request.ingress_protocol, target);
- bind_tools_for_profile(request, &mut profile)
+ let profile = candidate_profile(request.ingress_protocol, target);
+ project_candidate_request(request, &profile).unwrap()
})
.collect::<Vec<_>>();
for request in &projected[1..] {
@@ -103,7 +102,6 @@ fn protocol_context_and_capability_boundaries_fail_before_connect() {
"physical",
reasoning_profile(IngressProtocol::Responses),
);
- bind_request_to_profile(&mut request, &profile);
let baseline = project_candidate_request(&request, &profile).unwrap();
let input_n = baseline.context.target_serialized_input_upper_bound;
let total_n = baseline.context.required_total;
@@ -289,10 +287,32 @@ fn protocol_native_nonstream_and_sse_fragmentation_share_one_ledger() {
.iter()
.any(|event| { matches!(event.event, ModelEvent::FinishReason { .. }) })
);
+ // Different providers choose different IDs. Check preservation first,
+ // then compare the remaining semantics using block identity locally.
+ let mut comparable = whole.response.semantic_ledger();
+ for event in &mut comparable.events {
+ use hiroute_gateway::server::core_runtime::model_ir::{
+ ResponseBlock, SemanticLedgerEvent,
+ };
+ if let SemanticLedgerEvent::Block(ResponseBlock::ToolCall {
+ index, logical_id, ..
+ }) = event
+ {
+ assert!(
+ whole
+ .response
+ .tool_id_map
+ .iter()
+ .any(|entry| entry.native_id == *logical_id
+ && entry.logical_id == *logical_id)
+ );
+ *logical_id = format!("fixture-call-{index}");
+ }
+ }
if let Some(expected) = &expected {
- assert_eq!(&whole.response.semantic_ledger(), expected);
+ assert_eq!(&comparable, expected);
} else {
- expected = Some(whole.response.semantic_ledger());
+ expected = Some(comparable);
}
}
}
@@ -547,48 +567,6 @@ fn protocol_sse_decoder_applies_bounded_backpressure_without_rescanning() {
);
}
-fn bind_tools_for_profile(
- request: &hiroute_gateway::server::core_runtime::model_ir::ModelRequestIRV1,
- profile: &mut CandidateProtocolProfile,
-) -> hiroute_gateway::server::core_runtime::adapters::PreparedNativeRequest {
- let mut request = request.clone();
- bind_request_to_profile(&mut request, profile);
- project_candidate_request(&request, profile).unwrap()
-}
-
-fn bind_request_to_profile(
- request: &mut hiroute_gateway::server::core_runtime::model_ir::ModelRequestIRV1,
- profile: &CandidateProtocolProfile,
-) {
- let owner = profile.exact_provider_path().unwrap();
- let mut logical_ids = Vec::new();
- for part in request
- .messages
- .iter()
- .flat_map(|message| message.content.iter())
- {
- let logical_id = match part {
- ContentPart::ToolCall { logical_id, .. }
- | ContentPart::ToolResult { logical_id, .. } => logical_id,
- _ => continue,
- };
- if !logical_ids.contains(logical_id) {
- logical_ids.push(logical_id.clone());
- }
- }
- request.tool_id_map = logical_ids
- .into_iter()
- .map(|logical_id| ToolIdMapEntryV1 {
- native_id: format!("native-{logical_id}"),
- logical_id,
- kind: ToolKindV1::Function,
- name: "weather".into(),
- namespace: None,
- owner: owner.clone(),
- })
- .collect();
-}
-
fn reject_before_connect(
request: &hiroute_gateway::server::core_runtime::model_ir::ModelRequestIRV1,
profile: &CandidateProtocolProfile,
diff --git a/tools/e2e-harness/tests/p0_gateway_protocol/native_fidelity.rs b/tools/e2e-harness/tests/p0_gateway_protocol/native_fidelity.rs
index aa689690..a486edde 100644
--- a/tools/e2e-harness/tests/p0_gateway_protocol/native_fidelity.rs
+++ b/tools/e2e-harness/tests/p0_gateway_protocol/native_fidelity.rs
@@ -1,11 +1,9 @@
use hiroute_gateway::server::core_runtime::adapters::{
ClientResponseRenderer, IncrementalClientSseRenderer, NativeResponseDecoder,
- ProtocolAdapterError, RenderedClientResponse, ResponseDecodeStatus,
- decode_ingress_request_with_tool_resolver, project_candidate_request,
-};
-use hiroute_gateway::server::core_runtime::model_ir::{
- ModelEvent, ModelIrError, ToolIdMapEntryV1, ToolKindV1,
+ ProtocolAdapterError, RenderedClientResponse, ResponseDecodeStatus, decode_ingress_request,
+ project_candidate_request,
};
+use hiroute_gateway::server::core_runtime::model_ir::{ModelEvent, ModelIrError};
use hiroute_gateway::server::core_runtime::profiles::{
CandidateProtocolProfile, ClientProtocolProfile, fixed_reasoning,
};
@@ -45,19 +43,7 @@ fn native_codex_request_has_only_owned_same_protocol_rewrites() {
"physical-model",
fixed_reasoning("fixed"),
);
- let bindings = vec![ToolIdMapEntryV1 {
- logical_id: "native-call".into(),
- native_id: "native-call".into(),
- kind: ToolKindV1::Function,
- name: "lookup".into(),
- namespace: Some("tools".into()),
- owner: profile.exact_provider_path().unwrap(),
- }];
- let request =
- decode_ingress_request_with_tool_resolver(IngressProtocol::Responses, &native, |_| {
- Ok(bindings.clone())
- })
- .unwrap();
+ let request = decode_ingress_request(IngressProtocol::Responses, &native).unwrap();
let projected = project_candidate_request(&request, &profile).unwrap().body;
let mut expected = native;
expected["model"] = json!("physical-model");
diff --git a/tools/e2e-harness/tests/p0_gateway_protocol/provider_state.rs b/tools/e2e-harness/tests/p0_gateway_protocol/provider_state.rs
index 93074884..e9fdd30a 100644
--- a/tools/e2e-harness/tests/p0_gateway_protocol/provider_state.rs
+++ b/tools/e2e-harness/tests/p0_gateway_protocol/provider_state.rs
@@ -20,11 +20,68 @@ data: {"type":"response.completed","response":{"id":"native-first","model":"cont
#[test]
fn production_responses_continuation_keeps_actual_owner_in_multi_candidate_route() {
+ production_continuation(IngressProtocol::Responses, IngressProtocol::Responses);
+}
+
+#[test]
+fn production_messages_signature_keeps_responses_owner_in_multi_candidate_route() {
+ production_continuation(IngressProtocol::Messages, IngressProtocol::Responses);
+}
+
+#[test]
+fn production_native_messages_fragmented_signature_and_tool_result_keep_actual_owner() {
+ production_continuation(IngressProtocol::Messages, IngressProtocol::Messages);
+}
+
+const FIRST_MESSAGES: &[u8] = br#"event: message_start
+data: {"type":"message_start","message":{"id":"native-msg","model":"continuation-native-2","role":"assistant","content":[],"usage":{"input_tokens":4,"output_tokens":0}}}
+
+event: content_block_start
+data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":"","signature":""}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"fixture-"}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"opaque-final"}}
+
+event: content_block_stop
+data: { "index": 0,
+data: "type": "content_block_stop" }
+
+event: content_block_start
+data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"physical-tool","name":"Bash","input":{}}}
+
+event: content_block_delta
+data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"command\":\"pwd\"}"}}
+
+event: content_block_stop
+data: {"type":"content_block_stop","index":1}
+
+event: message_delta
+data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":8}}
+
+event: message_stop
+data: {"type":"message_stop"}
+
+"#;
+
+const SECOND_MESSAGES: &[u8] = br#"{"id":"native-done","model":"continuation-native-2","type":"message","role":"assistant","content":[{"type":"text","text":"done"}],"stop_reason":"end_turn","usage":{"input_tokens":8,"output_tokens":1}}"#;
+
+fn production_continuation(ingress: IngressProtocol, upstream: IngressProtocol) {
let _serial = process_test_lock();
let directory = tempfile::tempdir().unwrap();
let provider = ContinuationProvider::start(vec![
- ProviderReply::Sse(FIRST),
- ProviderReply::Json(SECOND_PROVIDER_JSON),
+ ProviderReply::Sse(if upstream == IngressProtocol::Messages {
+ FIRST_MESSAGES
+ } else {
+ FIRST
+ }),
+ ProviderReply::Json(if upstream == IngressProtocol::Messages {
+ SECOND_MESSAGES
+ } else {
+ SECOND_PROVIDER_JSON
+ }),
]);
let forbidden = ContinuationProvider::start(Vec::new());
let mut publication = snapshot(
@@ -33,10 +90,38 @@ fn production_responses_continuation_keeps_actual_owner_in_multi_candidate_route
forbidden.authority(),
);
publication.aliases[0].candidates.remove(0); // Two different native Responses models.
+ if upstream == IngressProtocol::Messages {
+ publication.aliases[0].candidates = vec![
+ sealed_native_candidate(
+ 2,
+ "continuation-target",
+ &["continuation-credential".into()],
+ provider.authority(),
+ "continuation-native-2",
+ &[(ingress, upstream)],
+ ),
+ sealed_native_candidate(
+ 3,
+ "forbidden-fallback-target",
+ &["forbidden-credential".into()],
+ forbidden.authority(),
+ "continuation-native-3",
+ &[(ingress, upstream)],
+ ),
+ ];
+ }
+ publication.aliases[0].protocols = vec![ingress];
+ for grant in &mut publication.grants {
+ grant.protocol = ingress;
+ if ingress == IngressProtocol::Messages {
+ grant.routes.retain(|alias, _| alias == "continuation");
+ }
+ }
for binding in &mut publication.aliases[0].candidates {
for profile in &mut binding.protocol_profiles {
let mut native: CandidateProtocolProfile =
serde_json::from_value(serde_json::to_value(&*profile).unwrap()).unwrap();
+ native.ingress_protocol = ingress;
native.capability.native_provider_state = NativeProviderStateEmission::ExactOwnerAffine;
native.capability.request.provider_state = Fidelity::Exact;
native.capability.request.state_affinity = StateAffinity::ExactOwner;
@@ -85,6 +170,11 @@ fn production_responses_continuation_keeps_actual_owner_in_multi_candidate_route
directory.path(),
);
process.wait_ready();
+ if ingress == IngressProtocol::Messages {
+ assert_messages_continuation(address, &provider, &forbidden, upstream);
+ process.stop();
+ return;
+ }
let initial = json!({"type":"message","role":"user","content":"first"});
let metadata = json!({"session_id":"native-continuation-fixture"});
let first = request(
@@ -158,3 +248,89 @@ fn production_responses_continuation_keeps_actual_owner_in_multi_candidate_route
assert_eq!(provider.calls(), 2);
process.stop();
}
+
+fn assert_messages_continuation(
+ address: SocketAddr,
+ provider: &ContinuationProvider,
+ forbidden: &ContinuationProvider,
+ upstream: IngressProtocol,
+) {
+ let initial = json!({"role":"user","content":"first"});
+ let first = request_at_path(
+ address,
+ "continuation-token",
+ &json!({
+ "model":"continuation","stream":true,"max_tokens":256,"messages":[initial.clone()],
+ "tools":[{"name":"Bash","input_schema":{"type":"object","properties":{"command":{"type":"string"}}}}]
+ }),
+ "/v1/messages",
+ );
+ assert_eq!(
+ first.status,
+ 200,
+ "{}",
+ String::from_utf8_lossy(&first.body)
+ );
+ let events = decode_downstream_sse(&first.body);
+ let signatures: Vec<_> = events
+ .iter()
+ .filter_map(|(_, value)| value["delta"]["signature"].as_str())
+ .collect();
+ assert_eq!(signatures.concat(), "fixture-opaque-final", "{events:#?}");
+ let mut next = json!({"model":"continuation","stream":false,"max_tokens":256,"messages":[
+ initial,
+ {"role":"assistant","content":[{"type":"thinking","thinking":"","signature":signatures.concat()}]},
+ {"role":"user","content":"continue"}
+ ]});
+ if upstream == IngressProtocol::Messages {
+ let tool = events
+ .iter()
+ .find_map(|(_, event)| {
+ let block = &event["content_block"];
+ (block["type"] == "tool_use").then_some(block)
+ })
+ .expect("delivered tool use");
+ next["messages"][1]["content"]
+ .as_array_mut()
+ .unwrap()
+ .push(tool.clone());
+ next["messages"][2]["content"] =
+ json!([{"type":"tool_result","tool_use_id":tool["id"],"content":"/work"}]);
+ }
+ let second = request_at_path(address, "continuation-token", &next, "/v1/messages");
+ assert_eq!(
+ second.status,
+ 200,
+ "{}",
+ String::from_utf8_lossy(&second.body)
+ );
+ wait_for_calls(provider, 2);
+ let bodies = provider.requests();
+ let forwarded: Value = serde_json::from_slice(http_body(&bodies[1])).unwrap();
+ assert_eq!(forwarded["model"], "continuation-native-2");
+ if upstream == IngressProtocol::Messages {
+ assert_eq!(
+ forwarded["messages"][1]["content"][0]["signature"],
+ "fixture-opaque-final"
+ );
+ assert_eq!(
+ forwarded["messages"][2]["content"][0]["tool_use_id"],
+ "physical-tool"
+ );
+ } else {
+ assert!(
+ forwarded["input"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .any(|item| item["type"] == "reasoning"
+ && item["encrypted_content"] == "fixture-opaque-final")
+ );
+ }
+ assert_eq!(forbidden.connections(), 0);
+ let mut altered = next;
+ altered["messages"][1]["content"][0]["signature"] = json!("unknown");
+ let rejected = request_at_path(address, "continuation-token", &altered, "/v1/messages");
+ assert_eq!(rejected.status, 400);
+ assert_eq!(provider.calls(), 2);
+}
diff --git a/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs b/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs
index ceda469a..fbf59316 100644
--- a/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs
+++ b/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs
@@ -5,7 +5,7 @@ use hiroute_gateway::server::core_runtime::adapters::{
};
use hiroute_gateway::server::core_runtime::model_ir::{
FinishReason, ModelEvent, ModelIrError, ModelStreamEventV1, OpaqueProviderState, ResponseBlock,
- ResponseBlockKind, ToolIdMapEntryV1, ToolKindV1,
+ ResponseBlockKind,
};
use hiroute_gateway::server::core_runtime::profiles::{
CandidateProtocolProfile, ClientProtocolProfile, Fidelity,
@@ -332,7 +332,6 @@ fn protocol_reviewer_exact_owner_is_required_for_provider_state_and_tool_continu
&body,
&IngressRequestBindings {
provider_state_owner: Some(owner.clone()),
- tool_id_map: Vec::new(),
},
)
.unwrap();
@@ -462,19 +461,10 @@ fn protocol_reviewer_json_tool_result_uses_exact_native_binding_for_all_targets(
format!("physical-{target:?}"),
fixed_reasoning("fixed"),
);
- let owner = profile.exact_provider_path().unwrap();
- let mut request = canonical.clone();
- request.tool_id_map = vec![ToolIdMapEntryV1 {
- logical_id: "logical_weather".into(),
- native_id: format!("native-{target:?}"),
- kind: ToolKindV1::Function,
- name: "weather".into(),
- namespace: None,
- owner,
- }];
+ let request = canonical.clone();
let body = project_candidate_request(&request, &profile).unwrap().body;
let (native_id, native_wire, output) = native_tool_result(target, &body);
- assert_eq!(native_id, format!("native-{target:?}"));
+ assert_eq!(native_id, "logical_weather");
assert_eq!(
native_wire,
r#"{"nested":{"a":1,"z":2},"temperature":21,"z":1}"#
@@ -483,15 +473,6 @@ fn protocol_reviewer_json_tool_result_uses_exact_native_binding_for_all_targets(
output,
json!({"z":1,"temperature":21,"nested":{"z":2,"a":1}})
);
-
- let mut unbound = request;
- unbound.tool_id_map.clear();
- assert_eq!(
- project_candidate_request(&unbound, &profile)
- .unwrap_err()
- .code(),
- "PROTOCOL_SEMANTICS_UNSUPPORTED"
- );
}
let profile = CandidateProtocolProfile::exact_portable_path(
@@ -503,14 +484,6 @@ fn protocol_reviewer_json_tool_result_uses_exact_native_binding_for_all_targets(
let mut missing_declaration = canonical;
missing_declaration.tools.clear();
missing_declaration.responses_tool_order.clear();
- missing_declaration.tool_id_map = vec![ToolIdMapEntryV1 {
- logical_id: "logical_weather".into(),
- native_id: "native-chat-without-declaration".into(),
- kind: ToolKindV1::Function,
- name: "weather".into(),
- namespace: None,
- owner: profile.exact_provider_path().unwrap(),
- }];
assert_eq!(
project_candidate_request(&missing_declaration, &profile)
.unwrap_err()
@@ -755,7 +728,7 @@ fn protocol_reviewer_native_tool_ids_are_physical_and_refusal_is_typed_three_by_
);
assert!(decoded.response.blocks.iter().any(|block| matches!(
block,
- ResponseBlock::ToolCall { logical_id, .. } if logical_id == "hiroute_tool_2"
+ ResponseBlock::ToolCall { logical_id, .. } if native_ids.last().unwrap().contains(logical_id)
)));
}
assert_ne!(native_ids[0], native_ids[1]);
@@ -772,15 +745,14 @@ fn protocol_reviewer_native_tool_ids_are_physical_and_refusal_is_typed_three_by_
decode_fragmented_for_profile(&profile, false, &native_nonstream(source), &[3, 1]);
let binding = decoded.response.tool_id_map[0].clone();
assert_eq!(profile.exact_provider_path().unwrap(), binding.owner);
- let mut continuation = decode_ingress_request(
+ let continuation = decode_ingress_request(
IngressProtocol::Responses,
&json!({
"model":"agent/research",
- "input":[{"type":"function_call_output","call_id":"hiroute_tool_2","output":{"ok":true}}]
+ "input":[{"type":"function_call_output","call_id":binding.native_id,"output":{"ok":true}}]
}),
)
.unwrap();
- continuation.tool_id_map = vec![binding.clone()];
let projected = project_candidate_request(&continuation, &profile).unwrap();
assert_eq!(
native_tool_result(source, &projected.body).0,