interplane.Get Omega
Read in your language

interplane-mv

INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.

Brought by drakestapleton

branch main Branches Tags History

interplane-mv / spec / LENSHIFT.md

226 lines · 15764 bytes Raw Blame History Rendered
1# Lenshift: model-native representation <-> canonical intent
2
3Lenshift owns representation differences. It never executes, never authorizes, never decides
4whether a capability exists. It produces `LenshiftTurn`:
5
6```
7LenshiftTurn {
8 dialect, dialect_version, parser_version, model,
9 text: string // the model's prose, with tool-call markup and think blocks removed
10 reasoning_digest: Digest|null // sha256 of the reasoning channel; raw reasoning not kept
11 intents: [tool_request] // structurally valid calls, in emission order
12 rejected: [{ index, code, message, source_digest }] // malformed calls, in emission order
13 partial: bool // a tool call was cut off (streaming/truncation)
14}
15```
16
17`rejected` entries also become `rejected` results (`malformed_tool_call`) so the model learns the
18call was not understood. Partial calls are rejected, never guessed.
19
20Every intent's provenance records `dialect`, `parser_version`, `model`, `source_turn`,
21`source_call_id` (if the dialect has ids), `source_digest` (sha256 of the exact dialect-native
22representation of that one call), `raw_name`, `coercion`.
23
24Tool name canonicalization: `raw_name` is split on the **last** `.` into `namespace` + `name` only
25when the prefix matches `^[a-z][a-z0-9_]*$`; otherwise `namespace = null` and `name = raw_name`.
26Lenshift never renames tools.
27
28Request ids: `source_call_id` when the dialect provides one, else `"<trace_id>:t<turn>:c<index>"`
29assigned by the caller. Lenshift itself does not know the trace; it emits `request_id = null` and
30the pipeline fills it. (In the reference implementations the parse function takes the trace and
31turn so ids are filled in one step.)
32
33## Dialect `openai` (version 1)
34
35Input: one assistant message object `{role, content, tool_calls?, reasoning_content?}` as returned
36by an OpenAI-compatible `/v1/chat/completions` (non-streamed, or streamed and assembled by the
37caller; dialect `openai_stream` below assembles a stream).
38
39- `tool_calls[i].function.name` -> raw_name; `tool_calls[i].id` -> source_call_id.
40- `function.arguments` is a JSON string: parse; must be an object. Not JSON, absent, or not a string
41 (an already-decoded object, as Ollama's own `/api/chat` sends, is not the OpenAI-compatible form)
42 -> `malformed_tool_call` "arguments is not valid JSON". JSON but not an object ->
43 `malformed_tool_call` "arguments is not an object".
44- Missing or empty name -> `malformed_tool_call`.
45- `content` -> text (null -> ""). `reasoning_content` / `reasoning` -> reasoning_digest.
46- Legacy `function_call` (single) is accepted as one call with no id.
47- `coercion = "none"`.
48
49Render result -> `{"role":"tool","tool_call_id":<source_call_id or request_id>,"content":<string>}`
50where content is `data` JSON-serialized (canonical) for `ok`, otherwise
51`{"error": {"code":..., "message":...}, "status": ...}` serialized. Never raw secrets.
52
53## Dialect `openai_stream` (version 1)
54
55Input: the body of a streamed OpenAI-compatible `/v1/chat/completions` response (`stream: true`), as
56one string of Server-Sent Events, exactly as the server sent it. Lenshift assembles the fragments
57into one assistant message and parses it with the `openai` rules above. Grounded in the six streamed
58captures under `qualification/captures/qwen35-{ollama,llamacpp,sglang}/h{1,2}-*.json`: Ollama sends
59each tool call whole; llama.cpp sends `id`, `name` and `"{"` first and the arguments in later pieces;
60SGLang does the same but repeats `"id": null, "name": null` in every later piece and sends
61`content: "\n\n"` before the call.
62
63Framing (the `data` subset of the WHATWG Server-Sent Events format):
64- Split on `\n`; a trailing `\r` on a line is removed. A line `data` or `data:<value>` appends
65 `<value>` (one leading space removed) to the event's data lines. Lines starting with `:` and all
66 other fields (`event`, `id`, `retry`) are ignored. A blank line ends an event; its data is the data
67 lines joined with `\n`. An event not ended by a blank line before the end of input is discarded.
68- Event data `[DONE]` ends the stream; any later event is a stream error. Any other data must be a
69 JSON object (strict JSON, as for arguments); otherwise a stream error.
70
71Assembly, chunk by chunk in arrival order:
72- `choices` absent, not an array, or empty (for example a usage chunk): ignored. A choice that is
73 not an object: stream error "stream choice is not an object". A choice with an `index` that is not
74 the integer `0`: stream error "multiple choices are not supported" (an absent `index` counts as 0).
75- `delta.content` and `delta.reasoning_content` / `delta.reasoning` strings are concatenated; `null`
76 is skipped. `delta.role` is ignored.
77- `delta.tool_calls[]` (ignored when not an array): each fragment must be an object with an integer
78 `index` from 0 to 2^64-1 (`1.0` is not an integer), else stream error "tool call fragment has no
79 index". Per index: the first non-empty string `id` and the first non-empty string
80 `function.name` are kept; a later, different non-empty value is a stream error ("conflicting tool
81 call id" / "conflicting tool call name"); `null` and `""` are skipped. `function.arguments`
82 strings are concatenated.
83- `finish_reason`: must be a string or `null`, else stream error "finish_reason is not a string"; the
84 first non-null value is kept; a later, different non-null value is a stream
85 error "conflicting finish_reason".
86
87Assembled message: `{"role": "assistant", "content": <text>, "tool_calls": [...]}` with calls in
88ascending `index`, each `{"type": "function", "function": {"name", "arguments"}}` plus `"id"` when one
89arrived; `"reasoning_content"` is present when reasoning text arrived. A call with no name or no
90arguments keeps the field absent and is rejected by the `openai` rules.
91
92Completeness: the stream is complete only when `finish_reason` is `stop`, `tool_calls` or
93`function_call`. Otherwise (absent, `length`, `content_filter`, anything else) every call of the turn
94is rejected `malformed_tool_call` "truncated tool call" with its own index, digest and id, and
95`partial = true` when there was at least one call. Text is kept. A cut-off stream is never guessed,
96even when a call's arguments already parse.
97
98Stream errors and non-string input: no intents, `text = ""`, one `rejected` entry at index 0
99(`malformed_tool_call`, the error message, `source_digest` = sha256 of the body text, or of its JCS
100form for a non-string), `partial = false`.
101
102Intents carry `dialect = "openai_stream"`, `dialect_version = "1"`; `source_digest` is of the
103assembled call object (JCS), as for `openai`. Render result is the same as `openai`.
104
105## Dialect `qwen35` (version 1)
106
107Grounded in the official `Qwen/Qwen3.5-9B` chat template (sha256 a4aee8af…, lines 53, 105-137).
108
109Input: the assistant turn as text (what the server returned in `content`, possibly with the
110reasoning included inline).
111
112- Reasoning: a leading `<think>...</think>` block (possibly empty) is removed; its content is digested.
113- Tool calls: each `<tool_call>` ... `</tool_call>` block whose body is `<function=NAME>` ... `</function>`
114 with zero or more `<parameter=KEY>` ... `</parameter>` children.
115 - KEY and NAME: trimmed; empty -> `malformed_tool_call`.
116 - Value: the text between the tags with exactly one leading and one trailing newline stripped
117 (the template emits `\n` + value + `\n`). Internal newlines are preserved.
118 - Typing: `coercion = "json_guess"`: if the value parses as JSON and the result is not a string,
119 use the parsed value; otherwise keep the string. (The template serializes non-string arguments
120 with `tojson`; the schema-driven coercion that serving engines do is CrossAxis's job.)
121 - Duplicate KEY within one call -> `malformed_tool_call`.
122- `<tool_call>` without a closing tag, `<function=` without `</function>`, or `<parameter=`
123 without `</parameter>` -> `partial = true` and a `rejected` entry "truncated tool call".
124- A `<tool_call>` body that is bare JSON `{"name":..,"arguments":{..}}` (the Qwen3 Hermes form that
125 older templates and some servers still emit) is accepted with `dialect_version = "hermes_json"`
126 recorded in provenance, so qualification can tell which form the model actually produced.
127- Text after the last `</tool_call>` is kept in `text` (the template forbids it, so provenance
128 notes `trailing_text = true` in extensions; it is not an error).
129- `text` is everything outside think and tool_call blocks, trimmed.
130
131Render result -> the content of a `role: tool` message: `<tool_response>\n<serialized>\n</tool_response>`
132with the same serialization as the openai dialect. (The chat template wraps tool messages itself;
133Lenshift emits the inner text and the caller places it in a `tool` role message.)
134
135Both forms of a Qwen3.5 tool call are real inputs: raw XML when the caller reads the model text,
136and OpenAI `tool_calls` when a serving engine's parser converted it. The `openai` dialect handles
137the second; qualification runs both.
138
139## Dialect `aien_legacy` (version 1)
140
141The textual protocol of `aien-cli` (aien-sovereign-core 7580039). Ground truth, all under
142`crates/aien-cli/src/`: parser `client.rs:350-419` (`parse_tool_call_json`, `extract_tool_calls`),
143prompt-side format `client.rs:100-160`, result reinjection `main.rs:330-340` (the same shape
144repeats at `main.rs:414-421` and `:537-553`). Input: the assistant turn as text.
145
146aien-cli streams reasoning in a separate `reasoning` / `reasoning_content` delta and keeps it out of
147the text it parses (`client.rs:297-325`); it never looks for `<think>` in the text.
148
149- Tool calls: each `<tool_call>` ... `</tool_call>` block, body trimmed, whose body is JSON
150 `{"name": string, "arguments": object}` (Hermes style). aien-cli's regex is
151 `(?s)<tool_call>\s*(.*?)\s*(?:</tool_call>|$)` (`client.rs:383`). Blocks are parsed in order and
152 every one is dispatched (`main.rs:330-340`); there is no per-turn cap.
153- Repairs aien-cli applies, tried in this order only after a strict parse fails (`client.rs:350-379`):
154 1. `bracket_to_brace`: a body ending in `]` has it replaced by `}` (`:355-362`).
155 2. `close_braces`: when `{` outnumbers `}` (counted over every character, strings included), a `"`
156 is appended if the body has an odd number of `"`, then the missing `}` are appended
157 (`:363-377`).
158 3. Otherwise the call is dropped without a trace.
159 There is **no** trailing-comma repair and **no** single-quote repair; those inputs are rejected.
160 A repaired intent carries `provenance.repairs` (a list of the names above); `coercion = "none"`.
161- Missing `arguments` means `{}` (`client.rs:392`). aien-cli forwards any other value unchecked.
162- Name `tool_name` (the placeholder in aien-cli's system prompt) is dropped silently (`:391`).
163- Fenced fallback: only when the `<tool_call>` pass produced no call, aien-cli scans for
164 `` ```(?:json|tool_call)?\s*(\{\s*"name"\s*:\s*"[^"]+".*?\})\s*``` `` (`:399-416`) and parses each
165 match with the same repairs.
166- Result rendering (`main.rs:338`): a **user-role** message
167 `{"role":"user","content":"<tool_response name=\"NAME\">\n<json>\n</tool_response>"}`. The
168 attribute is `name` (the tool name as the model wrote it, unescaped); there is no call id.
169
170Lenshift's rules (differences from aien-cli are marked **deviation**):
171
172- Reasoning: a leading `<think>...</think>` block is removed and digested, as in `qwen35`.
173 Lenshift addition: aien-cli never sees it in text.
174- Blocks are parsed in emission order; `index` counts every block, rejected ones included.
175- Whitespace in trimming, the fence pattern and the think prefix is ASCII (` \t\n\r\v\f`) in both
176 implementations, so they stay byte-identical. aien-cli's `\s`/`trim` also match other Unicode
177 whitespace.
178- **deviation** Unterminated `<tool_call>` (no `</tool_call>`): `partial = true`, `rejected`
179 "truncated tool call", never guessed. aien-cli accepts it to end of text (`client.rs:383`, test
180 at `:448-454`). Repair 2 therefore applies only to a block the model closed itself.
181- **deviation** Rejected with `malformed_tool_call` instead of dropped silently: body not JSON after
182 repairs ("tool call body is not valid JSON"), JSON that is not an object with a non-empty string
183 `name` ("tool call JSON has no name"), name `tool_name` ("placeholder tool name"), `arguments`
184 present but not an object ("arguments is not an object"), illegal tool name ("invalid tool name").
185- **deviation** The fenced fallback runs only when the text contains no `<tool_call>` marker at all,
186 so a malformed or truncated tagged call is never replaced by an unrelated fenced block. Fenced
187 intents carry `provenance.form = "fenced_json"`. Fenced blocks are removed from `text`.
188- `source_digest` is of the exact block (`<tool_call>` through `</tool_call>`, or the whole fence).
189 `source_call_id` is null. `dialect_version = "1"`.
190- `text` is everything outside think, tool_call and matched fence blocks, trimmed.
191
192Render result -> `{"role":"user","content":"<tool_response name=\"NAME\">\n<serialized>\n</tool_response>"}`
193with the shared serialization of the openai dialect. NAME is the intent's `raw_name`; without an
194intent (for example a rejected call) it is `unknown`. aien-cli serializes its own result object with
195`serde_json::to_string`; Lenshift does not reproduce that shape, only the envelope.
196
197Versus `qwen35`'s `hermes_json` variant: the JSON body grammar is the same, but aien_legacy adds the
198two repairs, the placeholder-name rule, the fenced fallback and the user-role rendering, and
199`qwen35` accepts the XML `<function=...>` form which aien-cli does not. They are separate dialects
200with separate parsers.
201
202## Dialect `ajax` — reserved, not implemented
203
204Ajax (announced 2026-10-02 as a Qwen3.5-9B fine-tune for Odysseus) had no published weights,
205model card, license, tokenizer or chat template on 2026-10-04. No parser exists. Adding one must
206require only a new module under `dialects/ajax` and `lenshift/ajax.*`, plus fixtures captured
207from the official artifact: no change to Core. The reference implementations' dialect registry is
208the abstraction test for that.
209
210## Fixtures
211
212`dialects/fixtures/<dialect>/<name>.json`: `{"input": ..., "model": ..., "expected": LenshiftTurn-without-digests, "expected_digests": {...}}`.
213Required openai_stream fixtures: every streamed capture under `qualification/captures` byte for byte
214(with its `sha256_response`); fragmented call; repeated null fragments; two interleaved calls; cut
215mid-arguments; cut after complete arguments; `finish_reason` length; unterminated last event; CRLF
216and comment framing; streamed plain text, complete and cut; assembled arguments not JSON; each stream
217error. Fixtures record `expected_digests`, and both runners compare every recorded digest.
218Required qwen35 fixtures: valid single call; multiple calls; malformed (unclosed function);
219reasoning plus call; plain answer with no tool; unknown tool name (parses fine); partial/truncated
220call; multi-line parameter value; JSON-typed parameter; hermes_json form. Required aien_legacy fixtures: valid single call; multiple calls; reasoning plus call; repaired bracket; repaired missing braces; trailing comma (rejected); single quotes (rejected); unterminated (rejected, partial); fenced json fallback; placeholder name (rejected); missing arguments; arguments not object; plain answer. Required openai fixtures:
221single; multiple; arguments not JSON; arguments not object; missing name; reasoning_content present;
222plain answer; legacy function_call; arguments as a decoded object (rejected); arguments not a string
223(rejected).
224
225Fixture provenance: inputs are constructed from the official template grammar, not captured from a
226live model, until the qualification run under `qualification/` captures real outputs and pins them.