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.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
fix(gateway): restore Hermes history continuity for Chat reasoning aliases (#190)
澄潭bc3dcc56791d
diff --git a/crates/gateway/src/adapters/ingress.rs b/crates/gateway/src/adapters/ingress.rsindex be17d8b7..c75fe6ac 100644--- a/crates/gateway/src/adapters/ingress.rs+++ b/crates/gateway/src/adapters/ingress.rs@@ -7,22 +7,37 @@ use std::collections::BTreeSet;use serde_json::{Map, Value};use crate::content_ref::{ContentRef, JsonValueExt};+use crate::replay::ReplayStore;use crate::server::core_runtime::model_ir::*;use crate::server::request_plan::IngressProtocol;#[path = "responses_options.rs"]mod responses_options;+mod chat_reasoning;+#[derive(Default)]-struct DecodeContext {+struct DecodeContext<'a> {native_only: std::cell::Cell<bool>,+ replay: Option<&'a ReplayStore>,}pub fn decode_ingress_request(protocol: IngressProtocol,body: &Value,) -> Result<ModelRequestIRV1, ModelIrError> {- let context = &DecodeContext::default();+ decode_ingress_request_with_replay(protocol, body, None)+}++pub(crate) fn decode_ingress_request_with_replay(+ protocol: IngressProtocol,+ body: &Value,+ replay: Option<&ReplayStore>,+) -> Result<ModelRequestIRV1, ModelIrError> {+ let context = &DecodeContext {+ replay,+ ..DecodeContext::default()+ };let mut request = match protocol {IngressProtocol::Responses => decode_responses(context, body),IngressProtocol::ChatCompletions => decode_chat(context, body),@@ -322,10 +337,12 @@ fn decode_chat_message("tool_calls","tool_call_id","reasoning_content",+ "reasoning",],"chat message",)?;let role = required_string(object, "role")?;+ let reasoning = chat_reasoning::canonical_reasoning(context, object, &role);if role == "system" || role == "developer" {ensure_absent(object,@@ -383,7 +400,7 @@ fn decode_chat_message(.map(|value| decode_chat_content(context, value)).transpose()?.unwrap_or_default();- if let Some(reasoning) = object.get("reasoning_content") {+ if let Some(reasoning) = reasoning {if role != "assistant" || (!reasoning.is_string() && reasoning.content_ref().is_none()) {return Err(ModelIrError::InvalidField("reasoning_content"));}diff --git a/crates/gateway/src/adapters/ingress/chat_reasoning.rs b/crates/gateway/src/adapters/ingress/chat_reasoning.rsnew file mode 100644index 00000000..f8b83fc6--- /dev/null+++ b/crates/gateway/src/adapters/ingress/chat_reasoning.rs@@ -0,0 +1,95 @@+//! Chat reasoning aliases share one canonical state; native copies remain untouched.++use std::io::{Read, Write};++use serde_json::{Map, Value};+use sha2::{Digest, Sha256};++use super::DecodeContext;+use crate::content_ref::JsonValueExt;+use crate::replay::ReplayStore;++pub(super) fn canonical_reasoning<'a>(+ context: &DecodeContext,+ object: &'a Map<String, Value>,+ role: &str,+) -> Option<&'a Value> {+ let content = object.get("reasoning_content");+ let Some(alias) = object.get("reasoning") else {+ return content;+ };+ // Unknown shapes and conflicting carriers remain native payload. Never+ // choose one and silently discard potentially different historical state.+ let alias_is_text = alias.is_string()+ || (alias.content_ref().is_some()+ && (content.is_some() || text_digest(alias, context.replay).is_some()));+ if role != "assistant"+ || !alias_is_text+ || content.is_some_and(|content| !same_text(content, alias, context.replay))+ {+ context.native_only.set(true);+ return content;+ }+ content.or(Some(alias))+}++fn same_text(left: &Value, right: &Value, replay: Option<&ReplayStore>) -> bool {+ if let (Some(left), Some(right)) = (left.as_str(), right.as_str()) {+ return left == right;+ }+ // Replay locations are not content identities: equal strings can occupy+ // different ranges. Resolve their bytes with bounded memory before admitting+ // an alias. Failed or unavailable backing never establishes equivalence.+ match (text_digest(left, replay), text_digest(right, replay)) {+ (Some(left), Some(right)) => left == right,+ _ => false,+ }+}++fn text_digest(value: &Value, replay: Option<&ReplayStore>) -> Option<(u64, [u8; 32])> {+ let Some(reference) = value.content_ref() else {+ let mut writer = TextDigest::default();+ // Whole-JSON backing contains quotes and escapes. Stream inline text+ // in the same representation instead of materializing another string.+ serde_json::to_writer(&mut writer, value.as_str()?).ok()?;+ return Some((writer.bytes, writer.digest.finalize().into()));+ };+ let mut reader = replay?.reader(&reference).ok()?;+ // Only string backing produced by the ingress JSON owner is a text alias;+ // a whole-JSON reference can otherwise carry an opaque object or array.+ let mut first = [0];+ reader.read_exact(&mut first).ok()?;+ if first[0] != b'"' {+ return None;+ }+ let mut digest = Sha256::new();+ digest.update(first);+ let mut buffer = [0; 4096];+ loop {+ let read = reader.read(&mut buffer).ok()?;+ if read == 0 {+ break;+ }+ digest.update(&buffer[..read]);+ }+ reader.verify_terminal().ok()?;+ Some((reference.byte_len(), digest.finalize().into()))+}++#[derive(Default)]+struct TextDigest {+ bytes: u64,+ digest: Sha256,+}++impl Write for TextDigest {+ fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {+ self.digest.update(bytes);+ self.bytes += bytes.len() as u64;+ Ok(bytes.len())+ }++ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+}diff --git a/crates/gateway/src/adapters/mod.rs b/crates/gateway/src/adapters/mod.rsindex c40b8a17..4332dd60 100644--- a/crates/gateway/src/adapters/mod.rs+++ b/crates/gateway/src/adapters/mod.rs@@ -15,6 +15,7 @@ pub(crate) use continuation::{with_active_response_delivery,};pub use ingress::decode_ingress_request;+pub(crate) use ingress::decode_ingress_request_with_replay;pub use request::{PreparedNativeRequest, PreparedNativeTemplate, project_candidate_request,project_candidate_request_template, sequential_attempt_body,diff --git a/crates/gateway/src/adapters/request/native.rs b/crates/gateway/src/adapters/request/native.rsindex c297f594..ce589372 100644--- a/crates/gateway/src/adapters/request/native.rs+++ b/crates/gateway/src/adapters/request/native.rs@@ -282,6 +282,9 @@ fn clean_prefix(body: &mut Value, protocol: IngressProtocol, end: usize) {}if let Some(object) = item.as_object_mut() {object.remove("reasoning_content");+ if protocol == IngressProtocol::ChatCompletions {+ object.remove("reasoning");+ }}if let Some(parts) = item.get_mut("content").and_then(Value::as_array_mut) {let original_len = parts.len();diff --git a/crates/gateway/src/adapters/tests.rs b/crates/gateway/src/adapters/tests.rsindex 9eb46bf9..4cab462e 100644--- a/crates/gateway/src/adapters/tests.rs+++ b/crates/gateway/src/adapters/tests.rs@@ -21,6 +21,9 @@ mod native_responses;#[path = "tests/payload_tolerance.rs"]mod payload_tolerance;+#[path = "tests/chat_reasoning.rs"]+mod chat_reasoning;+#[path = "tests/structured_output.rs"]mod structured_output;diff --git a/crates/gateway/src/adapters/tests/chat_reasoning.rs b/crates/gateway/src/adapters/tests/chat_reasoning.rsnew file mode 100644index 00000000..94e4ff73--- /dev/null+++ b/crates/gateway/src/adapters/tests/chat_reasoning.rs@@ -0,0 +1,220 @@+use super::*;+use crate::context_hold::visible_history_with_replay;++fn body() -> serde_json::Value {+ json!({"model":"alias","messages":[+ {"role":"system","content":"Use the available tools."},+ {"role":"user","content":"Read the value."},+ {"role":"assistant","content":null,"reasoning_content":"Read before answering.",+ "tool_calls":[{"id":"call-1","type":"function", "function":{"name":"read","arguments":"{}"}}]},+ {"role":"tool","tool_call_id":"call-1","content":"42"}+ ]})+}++#[test]+fn chat_reasoning_aliases_share_canonical_history_and_preserve_native_payload() {+ let original = body();+ let canonical = decode_ingress_request(IngressProtocol::ChatCompletions, &original).unwrap();+ let expected = visible_history_with_replay(&canonical, &[7; 32], None)+ .unwrap()+ .measure(None)+ .unwrap();+ for duplicate in [false, true] {+ let mut body = original.clone();+ body["messages"][2]["reasoning"] = body["messages"][2]["reasoning_content"].clone();+ if !duplicate {+ body["messages"][2]+ .as_object_mut()+ .unwrap()+ .remove("reasoning_content");+ }+ let request = decode_ingress_request(IngressProtocol::ChatCompletions, &body).unwrap();+ assert!(!request.native_only);+ assert_eq!(request.messages, canonical.messages);+ assert_eq!(+ visible_history_with_replay(&request, &[7; 32], None)+ .unwrap()+ .measure(None)+ .unwrap()+ .complete_digest,+ expected.complete_digest+ );+ let profile = exact_state_profile(+ IngressProtocol::ChatCompletions,+ IngressProtocol::ChatCompletions,+ );+ assert_eq!(+ project_candidate_request(&request, &profile).unwrap().body["messages"],+ body["messages"]+ );+ body["messages"]+ .as_array_mut()+ .unwrap()+ .push(json!({"role":"assistant","content":"42",+ "reasoning":"new thinking","reasoning_content":"new thinking"}));+ let request = decode_ingress_request(IngressProtocol::ChatCompletions, &body).unwrap();+ let cleaned =+ project_candidate_request_template_with_cleanup(&request, &profile, Some(2)).unwrap();+ let cleaned: serde_json::Value = serde_json::from_slice(&cleaned.bytes).unwrap();+ assert!(cleaned["messages"][2].get("reasoning").is_none());+ assert!(cleaned["messages"][2].get("reasoning_content").is_none());+ assert_eq!(cleaned["messages"][3], body["messages"][3]);+ assert_eq!(cleaned["messages"][4], body["messages"][4]);+ assert_eq!(request.native_body.as_ref(), Some(&body));+ }+}++#[test]+fn conflicting_or_opaque_chat_reasoning_still_prevents_history_reuse() {+ for alias in [+ json!("different thinking"),+ json!({"encrypted":"opaque"}),+ json!(null),+ json!(["thinking"]),+ ] {+ let mut body = body();+ body["messages"][2]["reasoning"] = alias;+ let request = decode_ingress_request(IngressProtocol::ChatCompletions, &body).unwrap();+ assert!(request.native_only);+ assert!(visible_history_with_replay(&request, &[7; 32], None).is_none());+ let profile = exact_state_profile(+ IngressProtocol::ChatCompletions,+ IngressProtocol::ChatCompletions,+ );+ assert_eq!(+ project_candidate_request(&request, &profile).unwrap().body["messages"],+ body["messages"]+ );+ assert!(+ project_candidate_request_template_with_cleanup(&request, &profile, Some(2)).is_err()+ );+ }+ for index in [0, 1, 3] {+ let mut body = body();+ body["messages"][index]["reasoning"] = json!("unexpected role");+ let request = decode_ingress_request(IngressProtocol::ChatCompletions, &body).unwrap();+ assert!(request.native_only);+ }+ let mut body = body();+ body["messages"][2]["reasoning"] = body["messages"][2]["reasoning_content"].clone();+ body["messages"][2]["unknown_state"] = json!("must not be ignored");+ let request = decode_ingress_request(IngressProtocol::ChatCompletions, &body).unwrap();+ assert!(visible_history_with_replay(&request, &[7; 32], None).is_none());+}++#[test]+fn chat_alias_normalization_does_not_hide_tool_history_changes() {+ let original = body();+ let request = decode_ingress_request(IngressProtocol::ChatCompletions, &original).unwrap();+ let previous = visible_history_with_replay(&request, &[7; 32], None)+ .unwrap()+ .measure(None)+ .unwrap();+ let mut continuation = original.clone();+ continuation["messages"][2]["reasoning"] =+ continuation["messages"][2]["reasoning_content"].clone();+ continuation["messages"]+ .as_array_mut()+ .unwrap()+ .push(json!({"role":"user","content":"Add eight."}));+ for mutation in 0..5 {+ let mut body = continuation.clone();+ match mutation {+ 1 => {+ body["messages"][2]["tool_calls"][0]["function"]["arguments"] =+ json!("{\"other\":true}")+ }+ 2 => body["messages"][3]["tool_call_id"] = json!("different-call"),+ 3 => body["messages"][3]["content"] = json!("43"),+ 4 => body["messages"][2]["reasoning_content"] = json!("changed thinking"),+ _ => {}+ }+ let request = decode_ingress_request(IngressProtocol::ChatCompletions, &body).unwrap();+ let digest = visible_history_with_replay(&request, &[7; 32], None)+ .and_then(|history| history.measure(Some(previous.message_count)))+ .and_then(|digests| digests.prefix_digest);+ assert_eq!(digest == Some(previous.complete_digest), mutation == 0);+ }+}++#[test]+fn replay_backed_chat_aliases_compare_bytes_instead_of_reference_locations() {+ let root = std::env::temp_dir().join(format!(+ "hiroute-chat-alias-{}-{}",+ std::process::id(),+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap()+ .as_nanos()+ ));+ let manager = ReplayManager::open(ReplayConfig {+ root: root.clone(),+ memory_threshold_bytes: 4096,+ record_bytes: 4096,+ orphan_ttl: std::time::Duration::from_secs(60),+ })+ .unwrap();+ let tree = BudgetTree::new(8 * 1024 * 1024, 8 * 1024 * 1024).unwrap();+ let budget = tree.stream(8 * 1024 * 1024).unwrap();+ let store = manager.begin_request(budget.clone()).unwrap();+ let text = "推理 🧠 \"quoted\"\n".repeat(4096);+ let first = store+ .store_content(&serde_json::to_vec(&text).unwrap())+ .unwrap();+ let second = store+ .store_content(&serde_json::to_vec(&text).unwrap())+ .unwrap();+ let different = store+ .store_content(&serde_json::to_vec(&text.replace("推", "理")).unwrap())+ .unwrap();+ assert_ne!(first, second);+ store+ .prevalidate(&[first.clone(), second.clone(), different.clone()])+ .unwrap();+ for (alias, matches) in [+ (second.json_marker(), true),+ (json!(text), true),+ (different.json_marker(), false),+ ] {+ let mut body = body();+ body["messages"][2]["reasoning_content"] = first.json_marker();+ body["messages"][2]["reasoning"] = alias;+ let request = decode_ingress_request_with_replay(+ IngressProtocol::ChatCompletions,+ &body,+ Some(&store),+ )+ .unwrap();+ assert_eq!(!request.native_only, matches);+ if matches {+ assert!(+ visible_history_with_replay(&request, &[7; 32], Some(&store))+ .unwrap()+ .measure(None)+ .is_some()+ );+ let profile = exact_state_profile(+ IngressProtocol::ChatCompletions,+ IngressProtocol::ChatCompletions,+ );+ let template = project_candidate_request_template(&request, &profile).unwrap();+ let mut reader =+ sequential_attempt_body(template, store.clone(), &budget, 257).unwrap();+ let mut bytes = Vec::new();+ while let Some(chunk) = reader.next_chunk().unwrap() {+ bytes.extend_from_slice(chunk.bytes());+ }+ let output: serde_json::Value = serde_json::from_slice(&bytes).unwrap();+ assert_eq!(output["messages"][2]["reasoning"], text);+ assert_eq!(output["messages"][2]["reasoning_content"], text);+ }+ assert!(+ decode_ingress_request(IngressProtocol::ChatCompletions, &body)+ .unwrap()+ .native_only+ );+ }+ drop(store);+ drop(manager);+ fs_err_remove_dir(&root);+}diff --git a/crates/gateway/src/content_ref/ingress.rs b/crates/gateway/src/content_ref/ingress.rsindex 944eacbf..3d0eca07 100644--- a/crates/gateway/src/content_ref/ingress.rs+++ b/crates/gateway/src/content_ref/ingress.rs@@ -910,6 +910,10 @@ fn whole_json_field(protocol: IngressProtocol, field: &str, depth: usize, value:// validate and retain summary before the sealed Plan replaces effort.// Other protocols still use backing for their native reasoning shapes.|| (depth == 1 && field == "reasoning" && protocol != IngressProtocol::Responses)+ || (protocol == IngressProtocol::ChatCompletions+ && depth > 1+ && field == "reasoning"+ && value.is_string())|| (field == "reasoning_content" && !value.is_null())}diff --git a/crates/gateway/src/core_runtime.rs b/crates/gateway/src/core_runtime.rsindex 596419a9..04759120 100644--- a/crates/gateway/src/core_runtime.rs+++ b/crates/gateway/src/core_runtime.rs@@ -555,20 +555,25 @@ 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(protocol, &document).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 mut canonical_request = match adapters::decode_ingress_request_with_replay(+ protocol,+ &document,+ Some(&replay),+ )+ .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,diff --git a/docs/code-map/decision-foundation.md b/docs/code-map/decision-foundation.mdindex 1d9d24e3..eb2325de 100644--- a/docs/code-map/decision-foundation.md+++ b/docs/code-map/decision-foundation.md@@ -97,6 +97,9 @@ distinguishes continuing, shortened, otherwise rewritten and unavailable checkporecords the opening cause on completion; [timeline projection](../../crates/observation/src/query_v2/routing_timeline/projection.rs)prefers it over legacy ledger inference. [Boundary regressions](../../crates/observation/src/store/tests/plan_quality/timeline_boundaries.rs)cover persistence, unknown evidence and old canonical bytes.+The [ingress adapters](../../crates/gateway/src/adapters/ingress.rs) normalize+supported protocol history into the same representation before prefix checks;+Chat reasoning aliases share one state while their native fields remain intact.The planner always prefers a valid held candidate within the newly selected group,including an in-group backup; retired follow-up fields survive only at serializeddiff --git a/issue-spec/specs/request-routing/spec.md b/issue-spec/specs/request-routing/spec.mdindex 933e6fb6..c4b0f712 100644--- a/issue-spec/specs/request-routing/spec.md+++ b/issue-spec/specs/request-routing/spec.md@@ -68,6 +68,33 @@ hosted-execution extensions, auth, or destination overrides cannot bypass admissCross-protocol candidates fail explicitly when equivalence is unavailable; theycannot silently drop images, tool semantics, reasoning, or ordinary extensions.+Protocol adapters SHALL derive continuity from their supported typed history,+using the existing common history representation and prefix comparison. In Chat+assistant history, textual `reasoning` is an alias of `reasoning_content`. A sole+alias or two byte-equivalent carriers represent one canonical reasoning state;+native forwarding retains the original fields. Equivalence MUST compare actual+bytes, including Replay-backed text, rather than reference locations. Conflicting+carriers, non-text aliases, aliases on other roles and other unknown historical+semantics remain native-only and cannot authorize history reuse, conversion or+cleanup. Authorized prefix cleanup removes both recognized carriers only within+the selected prefix. Alias normalization does not bypass session/authority scope,+tool identity, message order or content-change checks.++#### Scenario: Hermes replays duplicate reasoning through a local Chat endpoint++- **WHEN** a Hermes tool continuation or subsequent user turn supplies the same+ native session and continuous typed history, with identical `reasoning` and+ `reasoning_content` text+- **THEN** the tool continuation inherits its own decision, the next real user+ input obtains the complete attributable assessment target for the three-question+ protocol, and original native request fields remain intact.++#### Scenario: Reasoning aliases cannot conceal changed history++- **WHEN** aliases conflict, backing cannot establish equality, tool arguments,+ results or IDs change, or a different native session supplies equal messages+- **THEN** no unproven continuity or assessment target is inherited.+Request projection uses actual adapter capability, not a blanket requirement forExact provider metadata on reasoning/refusal/usage/streaming. Optional unknown factsmay affect estimates/catalog declaration, not provider admission. Auth, exactdiff --git a/tools/e2e-harness/tests/p0_gateway_runtime/decision_branches.rs b/tools/e2e-harness/tests/p0_gateway_runtime/decision_branches.rsindex 26ced1c4..c607a58d 100644--- a/tools/e2e-harness/tests/p0_gateway_runtime/decision_branches.rs+++ b/tools/e2e-harness/tests/p0_gateway_runtime/decision_branches.rs@@ -2,6 +2,9 @@ use super::*;use hiroute_gateway::server::publication::ModelRouteV2;use serde_json::{Value, json};+#[path = "decision_branches/chat_history.rs"]+mod chat_history;+fn reply(body: &'static [u8]) -> ProviderReply {ProviderReply::Complete {status: 200,diff --git a/tools/e2e-harness/tests/p0_gateway_runtime/decision_branches/chat_history.rs b/tools/e2e-harness/tests/p0_gateway_runtime/decision_branches/chat_history.rsnew file mode 100644index 00000000..5ff3e7b2--- /dev/null+++ b/tools/e2e-harness/tests/p0_gateway_runtime/decision_branches/chat_history.rs@@ -0,0 +1,122 @@+use super::*;++#[test]+fn hermes_chat_aliases_keep_tool_attribution_and_request_all_three_questions() {+ use hiroute_gateway::server::request_plan::IngressProtocol;++ for reasoning_bytes in [32, 32 * 1024] {+ let plain = br#"{"answers":{"q0":{"type":"score","probabilities":{"0":0.9,"1":0.1}}}}"#;+ let chat_tool = br#"{"id":"chat-tool","model":"runtime-native-model-1","choices":[{"index":0,"message":{"role":"assistant","content":null,"tool_calls":[{"id":"native-call","type":"function","function":{"name":"lookup","arguments":"{}"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}"#;+ let chat_ok = br#"{"id":"chat-ok","model":"runtime-native-model-1","choices":[{"index":0,"message":{"role":"assistant","content":"42"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}"#;+ let regular = NativeProvider::start(vec![+ reply(chat_tool),+ reply(chat_ok),+ reply(chat_ok),+ reply(chat_ok),+ reply(chat_ok),+ ]);+ let primary = NativeProvider::start(vec![]);+ let decider = NativeProvider::start(vec![+ reply(plain),+ reply(c_answer(true, false)),+ reply(plain),+ reply(plain),+ ]);+ let fixture =+ RuntimeFixture::launch_builtin_branches(&[®ular, &primary, &decider], true);+ let mut publication = fixture.publication();+ for (candidate, provider) in publication.aliases[0]+ .candidates+ .iter_mut()+ .zip([®ular, &primary])+ {+ *candidate = hiroute_e2e::gateway_fixture::sealed_native_candidate(+ candidate.local_id,+ &candidate.stable_target_key,+ &candidate.credential_refs,+ provider.authority(),+ &candidate.upstream_model_id,+ &[+ (IngressProtocol::Responses, IngressProtocol::ChatCompletions),+ (+ IngressProtocol::ChatCompletions,+ IngressProtocol::ChatCompletions,+ ),+ (IngressProtocol::Messages, IngressProtocol::ChatCompletions),+ ],+ );+ }+ publication+ .grants+ .iter_mut()+ .find(|grant| grant.protocol == IngressProtocol::ChatCompletions)+ .unwrap()+ .consumer = Some(hiroute_domain::AgentModelConsumerV1::Hermes);+ enable_c_in_publication(&mut publication);+ fixture.publish(&mut publication);+ let send = |history: &Vec<Value>, session| {+ let response = request(+ fixture.address,+ "POST",+ "/v1/chat/completions",+ &[+ ("X-HiRoute-Token", CHAT_TOKEN),+ ("x-hermes-session-id", session),+ ],+ &serde_json::to_vec(&json!({"model":MODEL,"stream":false,"messages":history,+ "tools":[{"type":"function","function":{"name":"lookup","parameters":{"type":"object"}}}]}))+ .unwrap(),+ );+ assert_eq!(+ response.status,+ 200,+ "history length {}, session {session}: {}",+ history.len(),+ String::from_utf8_lossy(&response.body)+ );+ let body: Value = serde_json::from_slice(&response.body).unwrap();+ body["choices"][0]["message"].clone()+ };+ let thinking = "r".repeat(reasoning_bytes);+ let mut history = vec![json!({"role":"user","content":"Read the value with lookup."})];+ let mut assistant = send(&history, "hermes-main");+ assert_eq!(assistant["tool_calls"][0]["function"]["name"], "lookup");+ assistant["reasoning_content"] = json!(thinking);+ assistant["reasoning"] = json!(thinking);+ let call_id = assistant["tool_calls"][0]["id"].clone();+ history.push(assistant);+ history.push(json!({"role":"tool","tool_call_id":call_id,"content":"42"}));+ let mut assistant = send(&history, "hermes-main");+ assert_eq!(+ decider.calls(),+ 1,+ "tool continuation inherits its own opening decision"+ );+ assistant["reasoning_content"] = json!(thinking);+ assistant["reasoning"] = json!(thinking);+ history.push(assistant);+ history.push(json!({"role":"user","content":"Add eight to that value."}));+ send(&history, "hermes-main");+ assert_eq!(decider.calls(), 2);+ // Neither another native session nor a rewritten main history may borrow+ // the complete assessment target from the original conversation.+ send(&history, "hermes-other");+ history[2]["content"] = json!("43");+ send(&history, "hermes-main");+ assert_eq!(+ (regular.calls(), primary.calls(), decider.calls()),+ (5, 0, 4)+ );+ for (index, request) in decider.requests().iter().enumerate() {+ let offset = request.windows(4).position(|w| w == b"\r\n\r\n").unwrap() + 4;+ let body: Value = serde_json::from_slice(&request[offset..]).unwrap();+ assert_eq!(body["questions"].get("q2").is_some(), index == 1);+ assert_eq!(body["questions"].get("q1").is_some(), index == 1);+ }+ let facts = wait_execution_facts(&fixture, 5);+ assert!(facts.iter().any(|fact| {+ fact.pointer("/fact/complexity/routing_observation/applicability_state")+ == Some(&json!("valid"))+ }));+ }+}