diff --git a/CHANGELOG.md b/CHANGELOG.md index db42f83..be61907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Streamed responses (plan 19.7 item 6, streaming half): Lenshift dialect `openai_stream` in Rust and Python takes a streamed `/v1/chat/completions` body (Server-Sent Events) exactly as sent, assembles the fragments into one assistant message and parses it with the `openai` rules (`spec/LENSHIFT.md`). A stream without `finish_reason` `stop`, `tool_calls` or `function_call` rejects every call as "truncated tool call"; malformed streams (conflicting id or name, second choice, fragment without index, data after `[DONE]`, non-JSON chunk) are rejected whole. 23 dialect fixtures, six of them the real Ollama, llama.cpp and SGLang stream captures byte for byte. Both dialect fixture runners now also compare every digest a fixture records. - Lenshift `openai` cross-language fix: Rust accepted an already-decoded `arguments` object and reported a non-string value as "arguments is not an object"; it now rejects every non-string `arguments` as "arguments is not valid JSON", as Python and `spec/LENSHIFT.md` do. Only Ollama's own `/api/chat` sends decoded objects (capture `05e`); all 29 OpenAI-compatible captured calls send strings. Fixtures `openai/arguments_decoded_object` and `openai/arguments_not_string` fail on the old Rust code. - Session state limits (plan 19.7 item 6b): host-set `max_traces` (1024), `max_messages_per_trace`, `max_requests_per_trace` and `max_inputs_per_trace` (4096 each) bound the pipeline's per-trace state in Rust and Python. A message or request over a limit is refused at the replay and duplicate stage with the new `ErrorCode` `session_limit_exceeded` (`session limit exceeded: `), after the replay and duplicate checks and before any runtime contact; `register_input` over its limit fails like a duplicate `input_id`. New host-only `Pipeline.close_trace` drops a trace's ledger ids, inputs, approval entries, event sequence and events and refuses later messages, requests, inputs and approval continuations with the new `session_closed` (`session closed: `). Unit tests with the same scenarios in both SDKs (the 0.3 corpus stays frozen). Python: the trace flag `_pending` is renamed `_awaiting`. +- Refused calls use their request id (cross-language fix): Python `run_turn` did not record the `request_id` of a malformed or over-the-turn-limit call, Rust did, so a later call reusing that id was a duplicate in Rust and accepted in Python. Python now records it like Rust (within the session state limits); `spec/CORE.md` states the rule. Same test in both SDKs; it fails on the old Python. ## Unreleased (0.2, CrossAxis lane) diff --git a/python/interplane/crossveil.py b/python/interplane/crossveil.py index 42701ad..6c2376f 100644 --- a/python/interplane/crossveil.py +++ b/python/interplane/crossveil.py @@ -356,6 +356,13 @@ class Pipeline: return ErrorCode.SESSION_LIMIT_EXCEEDED, msg return None + def _note_refused_request(self, trace_id: str, request_id: str) -> None: + """Record a request id for a call that is already refused (``run_turn``'s malformed and + over-the-turn-limit calls), unless that would break a session limit. Its result cites the + id, so a later call may not reuse it (``request_id`` is unique within a trace).""" + if self._request_refusal(trace_id) is None and self._can_hold(trace_id): + self.ledger.admit_request_id(trace_id, request_id) + def close_trace(self, trace_id: str) -> None: """Host-only: drop everything the pipeline keeps for the trace (ledger ids, inputs, approval entries, event sequence, events) and refuse any later message, request, input or @@ -901,12 +908,14 @@ class Pipeline: for ordinal, (index, kind, item) in enumerate(calls): if kind == "rejected": rid = request_id_for(getattr(item, "call_id", None), trace_id, turn, index) + self._note_refused_request(trace_id, rid) self._emit(trace_id, "tool_request", rid, turn) rej = self._rejected( rid, ErrorCode.MALFORMED_TOOL_CALL, trace_id, item.get("message"), turn ) pairs.append((None, *rej)) elif ordinal >= self.limits.max_requests_per_turn: + self._note_refused_request(trace_id, item.request_id) self._emit(trace_id, "tool_request", item.request_id, turn) rej = self._rejected( item.request_id, diff --git a/python/tests/test_session_limits.py b/python/tests/test_session_limits.py index 6a00756..d0b9681 100644 --- a/python/tests/test_session_limits.py +++ b/python/tests/test_session_limits.py @@ -218,3 +218,21 @@ def test_close_trace(): assert out.results[0].error.message == "session closed: t" assert not p.ledger.holds("t") and p.inputs("t") == [] assert all(e.extra["trace_id"] != "t" for e in p.events) + + +def test_refused_call_uses_its_request_id(): + """A malformed call's result cites its request id, so no later call in the trace may reuse it + (same in both SDKs).""" + + def call(cid, args): + return {"id": cid, "type": "function", "function": {"name": "read_file", "arguments": args}} + + def turn(*calls): + return {"role": "assistant", "content": None, "tool_calls": list(calls)} + + p = pipe() + out = p.run_turn("openai", "m", turn(call("c8", "{bad"), call("c8", '{"path": "/tmp/a"}')), "t", 0) + assert [r.error.code for r in out.results] == ["malformed_tool_call", "duplicate_request_id"] + out = p.run_turn("openai", "m", turn(call("c8", '{"path": "/tmp/a"}')), "t", 1) + assert out.results[0].error.code == "duplicate_request_id" + assert out.results[0].error.message == "duplicate request_id: c8" diff --git a/rust/crates/interplane-crossveil/tests/session_limits.rs b/rust/crates/interplane-crossveil/tests/session_limits.rs index 6a84841..ebf2ba7 100644 --- a/rust/crates/interplane-crossveil/tests/session_limits.rs +++ b/rust/crates/interplane-crossveil/tests/session_limits.rs @@ -276,3 +276,24 @@ fn close_trace() { assert!(!p.ledger.holds("t") && p.inputs("t").is_empty()); assert_eq!(p.events.len(), 2 * n_u, "the closed trace added no events"); } + +/// A malformed call's result cites its request id, so no later call in the trace may reuse it +/// (same in both SDKs). +#[test] +fn refused_call_uses_its_request_id() { + let mut rt = MockRuntime::new(); + let mut p = pipe(&mut rt, Limits::default()); + let bad = json!({"id": "c8", "type": "function", "function": {"name": "read_file", "arguments": "{bad"}}); + let good = call("c8", "read_file", json!({"path": "/tmp/a"})); + let out = p.run_turn("openai", "m", &turn(vec![bad, good.clone()]), "t", 0); + let codes: Vec<&str> = out + .results + .iter() + .map(|r| r.error.as_ref().expect("error").code.as_str()) + .collect(); + assert_eq!(codes, ["malformed_tool_call", "duplicate_request_id"]); + let out = p.run_turn("openai", "m", &turn(vec![good]), "t", 1); + let e = out.results[0].error.as_ref().expect("error"); + assert_eq!(e.code.as_str(), "duplicate_request_id"); + assert_eq!(e.message, "duplicate request_id: c8"); +} diff --git a/spec/CORE.md b/spec/CORE.md index b172b83..c477e55 100644 --- a/spec/CORE.md +++ b/spec/CORE.md @@ -25,7 +25,7 @@ when the extension exists, proves origin, never authority. - `trace_id`: one per model/runtime exchange (a task). Generated by whoever opens the exchange. - `message_id`: unique within a trace. A repeated `message_id` within a trace is a **replay** and is rejected with `replayed_message`. -- `request_id`: one per tool request, unique within a trace. A repeated `request_id` is rejected with `duplicate_request_id`. Results, decisions and capability requests cite it. +- `request_id`: one per tool request, unique within a trace. A repeated `request_id` is rejected with `duplicate_request_id`. A call refused in `run_turn` before admission (malformed, or over the per-turn limit) still uses its id, because its result cites it; a later call in the trace with that id is a duplicate. Results, decisions and capability requests cite it. - `parent_id`: the `message_id` this message answers, or null. ## Limits (defaults, runtime-overridable, never raised by a model)