diff --git a/agents/pi/admitted.json b/agents/pi/admitted.json index 528f891..614ed64 100644 --- a/agents/pi/admitted.json +++ b/agents/pi/admitted.json @@ -29,7 +29,7 @@ { "kind": "tree", "path": "lib", - "sha256": "fbe13e2031ba2dda14a42634ec1d1cb710bfe5a6f87c3d105e9f71fe2e041e64" + "sha256": "cbb5bf80c669ea3ba0b6fa608181736f88ae61b71502f858b63418f050893349" }, { "kind": "skill", @@ -74,7 +74,7 @@ { "kind": "provider", "path": "agents/pi/near-provider.ts", - "sha256": "2cf2c4d7dd1c2a69e98d7cb8506b58ec7787480ba16257c36f16a023a4ad3ded" + "sha256": "ead5b421867089bf13a0a209bf19ef4d5e65cb32f18634eae83a4482c2f3c630" }, { "kind": "provider", diff --git a/agents/pi/near-provider.ts b/agents/pi/near-provider.ts index 95aaeaf..552eef2 100644 --- a/agents/pi/near-provider.ts +++ b/agents/pi/near-provider.ts @@ -9,77 +9,15 @@ import { createProvider, envApiKeyAuth } from "@earendil-works/pi-ai"; import { openAICompletionsApi } from "@earendil-works/pi-ai/compat"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { providers } from "../../lib/config.mjs"; -import { ATTESTED_OWNERS, chatSignatureFails, peerStatus, untrusted, decrypt, encrypt, verifyNearModel, withKey } from "../../lib/near.mjs"; +import { ATTESTED_OWNERS, chatSignatureFails, untrusted, encrypt, verifyNearModel, withKey } from "../../lib/near.mjs"; import { deviceHeaders } from "../../lib/gift.mjs"; import { saveLeft } from "../../lib/peer.mjs"; +import { prepareNearTextTools } from "../../lib/near-tool-boundary.mjs"; +import { decryptSse } from "../../lib/near-sse.mjs"; import { tcbFlag } from "../../lib/status.mjs"; const FIELDS = ["content", "reasoning_content", "reasoning"]; -// The node appends ": abalone-charge {charge, remaining}" after NEAR's stream. We recompute the charge from -// NEAR's usage and our own price list, and stop the turn if they differ. -// ": abalone-status " from the peer (e.g. "confirming credit" while its next spending window reaches the chain's -// safe head) is shown until the answer starts. -// onNear gets NEAR's own bytes (without the peer's status and charge lines) before [DONE] is released. -// thinkOff: with enable_thinking=false GLM still writes a short thought, then "", into the content (measured on -// NEAR). That prefix is passed on as reasoning, so the answer starts after the marker; with no marker, the buffered -// text is the answer and is sent as content at the end. -function decryptSse(body: ReadableStream, ecdh: crypto.ECDH, price?: { input: number; output: number }, onCharge?: (c: any) => void, onStatus?: (s: string | null) => void, onNear?: (raw: Buffer) => Promise, thinkOff = false): ReadableStream { - const raw: Buffer[] = []; - let pending = "", usage: any = null, pre = "", split = !thinkOff; - const dec = new TextDecoder(), enc = new TextEncoder(); - let held = false, charged = false; - const line = (l: string) => { - // The node's charge line follows NEAR's [DONE]; hold [DONE] back so the client reads (and checks) the charge first. - if ((price || !split) && l === "data: [DONE]") { held = true; return ""; } - if (price && l.startsWith(": abalone-charge ")) { - const c = JSON.parse(l.slice(17)); - if (!usage) throw new Error(`Stopped. NEAR reported no usage for that answer, so its charge (${c.charge} µ$, the most it could have cost) cannot be checked here and nothing more will be sent.`); - const expect = Math.ceil(usage.prompt_tokens * price.input + usage.completion_tokens * price.output); - if (c.charge !== expect) throw new Error(`Stopped. The charge for that answer didn't match what was used, so nothing more will be sent.\ndetails: node charged ${c.charge} µ$, usage ${JSON.stringify(usage)} prices to ${expect} µ$`); - onCharge!(c); - charged = true; - return l; - } - if (l.startsWith(": abalone-status ")) { onStatus?.(peerStatus(l)); return ""; } - if (!l.startsWith("data: {")) return l; - onStatus?.(null); - const d = JSON.parse(l.slice(6)); - usage = d.usage ?? usage; - for (const c of d.choices ?? []) for (const f of FIELDS) if (typeof c.delta?.[f] === "string" && c.delta[f]) c.delta[f] = decrypt(c.delta[f], ecdh); - for (const c of d.choices ?? []) { - if (split || typeof c.delta?.content !== "string" || !c.delta.content) continue; - const start = pre.length; pre += c.delta.content; - const i = pre.indexOf(""); - if (i < 0) { c.delta.reasoning_content = c.delta.content; delete c.delta.content; continue; } - c.delta.reasoning_content = pre.slice(start, Math.max(start, i)); - c.delta.content = pre.slice(i + "".length); - split = true; - } - return "data: " + JSON.stringify(d); - }; - return body.pipeThrough(new TransformStream({ - transform(chunk, ctl) { - if (onNear) raw.push(Buffer.from(chunk)); - pending += dec.decode(chunk, { stream: true }); - const lines = pending.split("\n"); - pending = lines.pop()!; - for (const l of lines) ctl.enqueue(enc.encode(line(l) + "\n")); - }, - async flush(ctl) { - if (pending) ctl.enqueue(enc.encode(line(pending))); - if (!split && pre) ctl.enqueue(enc.encode(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: pre } }] })}\n\n`)); - if (price && !charged) throw new Error("Stopped. The gift node sent no charge for that answer, so nothing more will be sent."); - if (onNear) { - let b = Buffer.concat(raw); - while (b.subarray(0, 17).toString() === ": abalone-status ") b = b.subarray(b.indexOf("\n\n") + 2); - await onNear(b.subarray(0, b.lastIndexOf(": abalone-charge "))); - } - if (held) ctl.enqueue(enc.encode("data: [DONE]\n\n")); - }, - })); -} - // Shown in the status strip once a model is verified. Policy (providers.json): gateway and model TCB are reported, not // gating. The model's out-of-date TCB is red; the gateway's (gw-tcb:) is not, since it sees only ciphertext and metadata. const RED = (s: string) => `\x1b[31m${s}\x1b[39m`; @@ -103,14 +41,12 @@ export async function nearProvider(pi: ExtensionAPI, id: string, gift: Gift | nu const fetchE2ee: typeof fetch = async (input, init) => { const body = JSON.parse(String(init!.body)); + prepareNearTextTools(body); if (!verified.has(body.model)) verified.set(body.model, verifyNearModel(cfg.baseUrl, body.model, getReport, cfg.policy).then((v) => (pi.appendEntry("near-attestation", { ...v.profile, provider: id }), ui?.setStatus(id, strip(v.profile, gift)), v))); // A refusal must reach the user verbatim; the OpenAI client reports a thrown fetch only as "Connection error." const v = await verified.get(body.model)!.catch((e) => e as Error); const plain = crypto.createHash("sha256").update(String(init!.body)).digest("hex"); if (v instanceof Error) return new Response(JSON.stringify({ error: { message: v.message } }), { status: 403, headers: { "content-type": "application/json" } }); - // Tool definitions and tool-call arguments are not covered by NEAR's E2EE, so they never go to NEAR. - delete body.tools; delete body.tool_choice; delete body.parallel_tool_calls; - if (body.messages.some((m: any) => m.tool_calls || m.role === "tool")) throw new Error("✗ refused: tool calls are not sent to NEAR (their arguments are not end-to-end encrypted)"); for (const m of body.messages) { if (Array.isArray(m.content)) { if (m.content.some((p: any) => p.type !== "text")) throw new Error("✗ refused: NEAR E2EE carries text only"); diff --git a/docs/protocols/near-adapter.md b/docs/protocols/near-adapter.md index 3bf195c..11ccb15 100644 --- a/docs/protocols/near-adapter.md +++ b/docs/protocols/near-adapter.md @@ -50,6 +50,46 @@ A refused endpoint identity at the pinned transport is reported as `transport_re distinct from `access_revoked_or_expired`. Every failure is a stable code; upstream text, collateral errors and wire payloads never surface. +## Tool confidentiality and execution boundary + +This integration encrypts message content and reasoning with the attested model +key. It does not implement NEAR's opt-in all-fields tool protocol. Native tool +schemas (including legacy function definitions) are removed before sending; +structured tool history in either format is refused before admission. The +ordinary and coupon adapters also refuse structured tool calls in responses. + +Tools remain available through [the in-content protocol](../../lib/intools.mjs): +the system prompt describes them inside encrypted text, the model returns a tool +request inside encrypted answer text, Abalone executes it locally, and the result +returns as another encrypted message. “Tools off” in the ordinary NEAR status +means native API tools are disabled; it does not mean local tools are disabled. + +| Data or action | Current integration boundary | +| --- | --- | +| Tool descriptions, requests and results in message text | Encrypted to/from the attested model | +| Native request tool schemas | Removed before sending | +| Native tool-call history | Refused | +| Native response calls on the coupon route | Refused | +| Native response calls on the ordinary route | Refused before delivery to Pi | +| Local tool execution | Runs on the client under the selected host/sandbox profile | +| Data sent by a tool to an external service | Disclosed to that destination; inference E2EE does not cover its use there | + +NEAR's [published agent-loop example](https://github.com/nearai/nearai-cloud-verifier/blob/main/py/encrypted_agent_loop_verifier.py) +uses `X-Encrypt-All-Fields: true` with encryption version 2 and describes encryption +of tool names, arguments, definitions, reasoning and search-result output. Its +server-side search loop is described as running in the confidential VM, with the +query sent to Brave over TLS. This is source evidence checked on 2026-10-14, +not a fresh live test or qualification of every model. The example retrieves a +key from an attestation report; that operation alone is not cryptographic +verification of the key. + +Adopting native encrypted tools requires verified key binding, complete field +coverage in both directions, response integrity checks, and refusal of plaintext +or unsupported protocol fallback. Keep the current encrypted-text path until +those properties are qualified. See [integration issue 73](https://github.com/amiller/abalone/issues/73), +[parser issue 56](https://github.com/amiller/abalone/issues/56), and the +[research thread](https://github.com/amiller/awesome-private-inference/issues/23). + ## Bounded session lifetime and expiry An adapter session is a bounded monotonic window set at opening — default 60 seconds, diff --git a/lib/coupon-near.mjs b/lib/coupon-near.mjs index e3b219a..b3933bb 100644 --- a/lib/coupon-near.mjs +++ b/lib/coupon-near.mjs @@ -1,4 +1,5 @@ // Host-side NEAR inference for a delivered coupon credential. This does not admit an issuer. +import { prepareNearTextTools, assertNearTextToolReply } from "./near-tool-boundary.mjs"; import crypto from "node:crypto"; import http from "node:http"; import fs from "node:fs"; @@ -73,8 +74,8 @@ function inferenceBody(bytes, profile, modelPublicKey) { let body; try { body = JSON.parse(bytes); } catch { throw fail("request_refused"); } if (!body || typeof body !== "object" || Array.isArray(body)) throw fail("request_refused"); - // Tools are carried in encrypted text by the existing in-content protocol. - for (const key of ["tools", "tool_choice", "parallel_tool_calls"]) delete body[key]; + // Refusals stay static and sanitized at the credential boundary. + try { prepareNearTextTools(body); } catch { throw fail("request_refused"); } const keys = ["model", "messages", "stream", ...["max_tokens", "temperature", "top_p", "chat_template_kwargs", "stream_options"].filter((key) => Object.hasOwn(body, key))]; if (!exact(body, keys) || body.model !== profile.model || body.stream !== true || !Array.isArray(body.messages) || !body.messages.length || body.messages.length > 256) throw fail("request_refused"); if (body.max_tokens !== undefined && (!Number.isSafeInteger(body.max_tokens) || body.max_tokens < 1 || body.max_tokens > profile.maxTokens)) throw fail("request_refused"); @@ -117,8 +118,8 @@ function plaintextSse(bytes, responseKey) { let value; try { value = JSON.parse(line.slice(6)); } catch { throw fail("provider_response_refused"); } if (value.error || !Array.isArray(value.choices)) throw fail("provider_response_refused"); + try { assertNearTextToolReply(value); } catch { throw fail("provider_response_refused"); } for (const choice of value.choices) { - if (choice.delta?.tool_calls || choice.delta?.function_call) throw fail("provider_response_refused"); for (const field of FIELDS) if (choice.delta?.[field]) { if (typeof choice.delta[field] !== "string" || !/^(?:[0-9a-f]{2})+$/.test(choice.delta[field])) throw fail("provider_response_refused"); try { choice.delta[field] = decrypt(choice.delta[field], responseKey); } catch { throw fail("provider_response_refused"); } diff --git a/lib/intools.mjs b/lib/intools.mjs index 1bc9668..804fbfa 100644 --- a/lib/intools.mjs +++ b/lib/intools.mjs @@ -1,5 +1,5 @@ -// Tools inside the encrypted content. NEAR's E2EE covers message content but not tool definitions or tool-call -// arguments, so abalone sends NEAR no tools (agents/pi/near-provider.ts). Instead the system prompt defines this text +// Tools inside the encrypted content. Our NEAR integration does not implement its opt-in all-fields tool +// encryption, so abalone sends no native tools (agents/pi/near-provider.ts). Instead the system prompt defines this text // protocol: the model writes requests as GLM's native blocks (or our older {json} line), abalone runs them here in the // workspace, and the result goes back as the next user turn, which NEAR's E2EE covers like any other message. import { spawnSync } from "node:child_process"; diff --git a/lib/near-sse.mjs b/lib/near-sse.mjs new file mode 100644 index 0000000..c5eac58 --- /dev/null +++ b/lib/near-sse.mjs @@ -0,0 +1,73 @@ +import { assertNearTextToolReply } from "./near-tool-boundary.mjs"; +import { decrypt, peerStatus } from "./near.mjs"; + +const FIELDS = ["content", "reasoning_content", "reasoning"]; + +// The node appends ": abalone-charge {charge, remaining}" after NEAR's stream. We recompute the charge from +// NEAR's usage and our own price list, and stop the turn if they differ. +// ": abalone-status " from the peer (e.g. "confirming credit" while its next spending window reaches the chain's +// safe head) is shown until the answer starts. +// onNear gets NEAR's own bytes (without the peer's status and charge lines) before [DONE] is released. +// thinkOff: with enable_thinking=false GLM still writes a short thought, then "", into the content (measured on +// NEAR). That prefix is passed on as reasoning, so the answer starts after the marker; with no marker, the buffered +// text is the answer and is sent as content at the end. +export function decryptSse(body, ecdh, price, onCharge, onStatus, onNear, thinkOff = false) { + const raw = []; + let pending = "", usage = null, pre = "", split = !thinkOff; + const dec = new TextDecoder(), enc = new TextEncoder(); + let held = false, charged = false; + const line = (l) => { + if (l.endsWith("\r")) l = l.slice(0, -1); + // SSE permits data: with zero or more spaces. Normalize before inspecting + // JSON so alternative framing cannot bypass native-tool refusal. + if (l.startsWith("data:")) l = "data: " + l.slice(5).trimStart(); + // The node's charge line follows NEAR's [DONE]; hold [DONE] back so the client reads (and checks) the charge first. + if ((price || !split) && l === "data: [DONE]") { held = true; return ""; } + if (price && l.startsWith(": abalone-charge ")) { + const c = JSON.parse(l.slice(17)); + if (!usage) throw new Error(`Stopped. NEAR reported no usage for that answer, so its charge (${c.charge} µ$, the most it could have cost) cannot be checked here and nothing more will be sent.`); + const expect = Math.ceil(usage.prompt_tokens * price.input + usage.completion_tokens * price.output); + if (c.charge !== expect) throw new Error(`Stopped. The charge for that answer didn't match what was used, so nothing more will be sent.\ndetails: node charged ${c.charge} µ$, usage ${JSON.stringify(usage)} prices to ${expect} µ$`); + onCharge(c); + charged = true; + return l; + } + if (l.startsWith(": abalone-status ")) { onStatus?.(peerStatus(l)); return ""; } + if (!l.startsWith("data: ") || l === "data: [DONE]" || l === "data: ") return l; + onStatus?.(null); + const d = JSON.parse(l.slice(6)); + assertNearTextToolReply(d); + usage = d.usage ?? usage; + for (const c of d.choices ?? []) for (const f of FIELDS) if (typeof c.delta?.[f] === "string" && c.delta[f]) c.delta[f] = decrypt(c.delta[f], ecdh); + for (const c of d.choices ?? []) { + if (split || typeof c.delta?.content !== "string" || !c.delta.content) continue; + const start = pre.length; pre += c.delta.content; + const i = pre.indexOf(""); + if (i < 0) { c.delta.reasoning_content = c.delta.content; delete c.delta.content; continue; } + c.delta.reasoning_content = pre.slice(start, Math.max(start, i)); + c.delta.content = pre.slice(i + "".length); + split = true; + } + return "data: " + JSON.stringify(d); + }; + return body.pipeThrough(new TransformStream({ + transform(chunk, ctl) { + if (onNear) raw.push(Buffer.from(chunk)); + pending += dec.decode(chunk, { stream: true }); + const lines = pending.split("\n"); + pending = lines.pop(); + for (const l of lines) ctl.enqueue(enc.encode(line(l) + "\n")); + }, + async flush(ctl) { + if (pending) ctl.enqueue(enc.encode(line(pending))); + if (!split && pre) ctl.enqueue(enc.encode(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: pre } }] })}\n\n`)); + if (price && !charged) throw new Error("Stopped. The gift node sent no charge for that answer, so nothing more will be sent."); + if (onNear) { + let b = Buffer.concat(raw); + while (b.subarray(0, 17).toString() === ": abalone-status ") b = b.subarray(b.indexOf("\n\n") + 2); + await onNear(b.subarray(0, b.lastIndexOf(": abalone-charge "))); + } + if (held) ctl.enqueue(enc.encode("data: [DONE]\n\n")); + }, + })); +} diff --git a/lib/near-tool-boundary.mjs b/lib/near-tool-boundary.mjs new file mode 100644 index 0000000..0b06471 --- /dev/null +++ b/lib/near-tool-boundary.mjs @@ -0,0 +1,20 @@ +// This integration uses encrypted message text for tools, not native API fields. +// Keep request and reply enforcement identical across ordinary and coupon routes. +export function prepareNearTextTools(body) { + if (!Array.isArray(body.messages) || body.messages.some((message) => !message || + ["tool", "function"].includes(message.role) || + ["tool_calls", "function_call", "tool_call_id", "name"].some((field) => Object.hasOwn(message, field)))) { + throw new Error("Stopped. Native tool history is unsupported by this NEAR integration; use tools inside encrypted message text."); + } + for (const field of ["tools", "tool_choice", "parallel_tool_calls", "functions", "function_call"]) delete body[field]; +} + +export function assertNearTextToolReply(value) { + for (const choice of value.choices ?? []) { + if (["tool_calls", "function_call"].includes(choice.finish_reason) || + [choice.delta, choice.message].some((message) => message && + ["tool_calls", "function_call", "nearai_tool_result"].some((field) => message[field] != null))) { + throw new Error("Stopped. NEAR returned an unsupported native tool response; no tool from that response will run."); + } + } +} diff --git a/tests/coupon-near.test.mjs b/tests/coupon-near.test.mjs index e950025..6496370 100644 --- a/tests/coupon-near.test.mjs +++ b/tests/coupon-near.test.mjs @@ -95,7 +95,7 @@ const verdict = (state) => ({ state.bodies?.push(body); assert.deepEqual(Object.keys(body).sort(), state.keys ?? ["max_tokens", "messages", "model", "stream"]); assert.equal(body.model, "selected/model"); assert.equal(body.stream, true); - assert.deepEqual(body.messages.map((m) => ({ role: m.role, content: decrypt(m.content, modelKey) })), [{ role: "user", content: "synthetic qualification content" }]); + assert.deepEqual(body.messages.map((m) => ({ role: m.role, content: decrypt(m.content, modelKey) })), state.messages ?? [{ role: "user", content: "synthetic qualification content" }]); const clientPub = new Headers(init.headers).get("x-client-pub-key"); assert.match(clientPub, /^[0-9a-f]{128}$/); assert.equal(new Headers(init.headers).get("x-model-pub-key"), modelKey.getPublicKey("hex")); @@ -106,12 +106,37 @@ const verdict = (state) => ({ if (mode === "unavailable") return new Response("retry later", { status: 503 }); if (mode === "json") return new Response("{}", { status: 200, headers: { "content-type": "application/json" } }); if (mode === "tampered") return new Response(`data: ${JSON.stringify({ choices: [{ delta: { content: "00".repeat(96) } }] })}\n\ndata: [DONE]\n`, { status: 200, headers: { "content-type": "text/event-stream" } }); + if (mode === "native-tool" || mode === "legacy-function") return new Response(`data: ${JSON.stringify({ choices: [{ delta: mode === "native-tool" + ? { tool_calls: [{ id: "synthetic-call", type: "function", function: { name: "read", arguments: '{"path":"synthetic-secret"}' } }] } + : { function_call: { name: "read", arguments: '{"path":"synthetic-secret"}' } } }] })}\n\ndata: [DONE]\n`, { status: 200, headers: { "content-type": "text/event-stream" } }); if (mode === "cleartext") return new Response(`data: ${JSON.stringify({ choices: [{ delta: { content: `not ciphertext ${options().credential}` } }] })}\n\ndata: [DONE]\n`, { status: 200, headers: { "content-type": "text/event-stream" } }); return new Response(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: encrypt(`useful synthetic answer ${options().credential}`, clientPub) }, finish_reason: null }] })}\n\ndata: ${JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\ndata: [DONE]\n`, { status: 200, headers: { "content-type": "text/event-stream" } }); }, }, }); +test("tool text stays encrypted while native tool fields are removed or refused", async () => { + const canary = "synthetic-tool-argument-canary"; + const state = { chats: 0, closed: false, bodies: [], messages: [{ role: "user", content: `readpath${canary}` }] }; + const adapter = openCouponNearAdapterForTesting({ ...options(), verifiedForTesting: verdict(state) }); + try { + const payload = { ...body(), messages: state.messages, tools: [{ type: "function", function: { name: "read", description: canary, parameters: { type: "object" } } }], tool_choice: "auto", parallel_tool_calls: true, functions: [{ name: canary }], function_call: { name: canary } }; + await (await adapter.fetch(`${options().baseUrl}/chat/completions`, request(payload))).text(); + assert.equal(state.chats, 1); + assert.ok(!JSON.stringify(state.bodies[0]).includes(canary), "sensitive tool text and schema must not be visible on the wire"); + for (const messages of [ + [{ role: "assistant", content: "", tool_calls: [{ function: { name: "read", arguments: canary } }] }], + [{ role: "assistant", content: "", function_call: { name: "read", arguments: canary } }], + [{ role: "tool", content: canary }], + ]) await assert.rejects(adapter.fetch(`${options().baseUrl}/chat/completions`, request({ ...body(), messages })), { code: "request_refused" }); + assert.equal(state.chats, 1, "unsupported tool history must refuse before contacting the provider"); + for (const mode of ["native-tool", "legacy-function"]) { + state.mode = mode; + await assert.rejects(adapter.fetch(`${options().baseUrl}/chat/completions`, request({ ...body(), messages: state.messages })), { code: "provider_response_refused" }); + } + } finally { await adapter.close(); } +}); + test("actual Pi OpenAI request grammar reaches the strict NEAR encrypted adapter", async () => { const { streamSimple } = await import("../vendor/pi/packages/ai/dist/api/openai-completions.js"); const state = { chats: 0, closed: false, mode: "ok", bodies: [], keys: ["chat_template_kwargs", "max_tokens", "messages", "model", "stream", "stream_options"] }; diff --git a/tests/near-sse.test.mjs b/tests/near-sse.test.mjs new file mode 100644 index 0000000..ba8fb4d --- /dev/null +++ b/tests/near-sse.test.mjs @@ -0,0 +1,94 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import crypto from "node:crypto"; +import { decryptSse } from "../lib/near-sse.mjs"; +import { prepareNearTextTools } from "../lib/near-tool-boundary.mjs"; +import { encrypt } from "../lib/near.mjs"; + +const key = crypto.createECDH("secp256k1"); key.generateKeys(); +const seal = (text) => encrypt(text, key.getPublicKey("hex")); +const wire = (text, width = 7) => new ReadableStream({ start(controller) { + const bytes = new TextEncoder().encode(text); + for (let i = 0; i < bytes.length; i += width) controller.enqueue(bytes.slice(i, i + width)); + controller.close(); +} }); +const decode = (text, width) => new Response(decryptSse(wire(text, width), key)).text(); + +for (const prefix of ["data: ", "data:", "data: "]) { + test(`ordinary NEAR decoder refuses native tool arguments (${JSON.stringify(prefix)})`, async () => { + const secret = "synthetic-native-tool-canary"; + for (const delta of [ + { tool_calls: [{ index: 0, function: { arguments: secret } }] }, + { function_call: { name: "read", arguments: secret } }, + { nearai_tool_result: { output: secret } }, + ]) await assert.rejects(decode(`${prefix}${JSON.stringify({ choices: [{ delta }] })}\r\n\r\ndata: [DONE]\r\n`), /unsupported native tool/); + }); +} + +test("ordinary NEAR decoder keeps encrypted text tools and reasoning", async () => { + const text = 'readpathsynthetic.txt'; + const delta = { content: seal(text), reasoning_content: seal("synthetic reasoning"), tool_calls: null }; + const result = await decode(`data: ${JSON.stringify({ choices: [{ delta }] })}\n\ndata: [DONE]\n\n`, 1); + const parsed = JSON.parse(result.split("\n").find((line) => line.startsWith("data: {" )).slice(6)); + assert.equal(parsed.choices[0].delta.content, text); + assert.equal(parsed.choices[0].delta.reasoning_content, "synthetic reasoning"); + assert.ok(result.includes("data: [DONE]")); +}); + +test("ordinary request preparation removes schemas and refuses both history formats", () => { + const secret = "synthetic-schema-canary"; + const body = { messages: [{ role: "user", content: secret }], tools: [{ description: secret }], + tool_choice: "auto", parallel_tool_calls: true, functions: [{ name: secret }], function_call: { name: secret } }; + prepareNearTextTools(body); + assert.deepEqual(body, { messages: [{ role: "user", content: secret }] }); + for (const message of [ + { role: "assistant", content: "", tool_calls: [{ function: { arguments: secret } }] }, + { role: "assistant", content: "", function_call: { arguments: secret } }, + { role: "tool", content: secret }, { role: "function", content: secret }, + { role: "assistant", content: "", tool_call_id: secret }, + { role: "assistant", content: "", name: secret }, + ]) assert.throws(() => prepareNearTextTools({ messages: [message] }), (error) => + error.message.includes("unsupported") && !error.message.includes(secret)); +}); + +test("native finish reasons and non-stream message envelopes cannot bypass refusal", async () => { + for (const choice of [ + { delta: {}, finish_reason: "tool_calls" }, { delta: {}, finish_reason: "function_call" }, + { message: { function_call: { arguments: "synthetic-secret" } } }, + ]) await assert.rejects(decode(`data:${JSON.stringify({ choices: [choice] })}\n\n`), /unsupported native tool/); +}); + +test("gift accounting and raw response verification finish before DONE", async () => { + const raw = `data: ${JSON.stringify({ choices: [{ delta: { content: seal("synthetic answer") } }], usage: { prompt_tokens: 2, completion_tokens: 3 } })}\n\ndata: [DONE]\n\n`; + const charge = { charge: 8, remaining: 92 }; + const bytes = raw + `: abalone-charge ${JSON.stringify(charge)}\n\n`; + const steps = []; + const result = await new Response(decryptSse(wire(bytes), key, { input: 1, output: 2 }, + (value) => { assert.deepEqual(value, charge); steps.push("charge"); }, undefined, + async (observed) => { assert.equal(observed.toString(), raw); steps.push("verified"); })).text(); + assert.deepEqual(steps, ["charge", "verified"]); + assert.ok(result.includes("synthetic answer")); + assert.ok(result.endsWith("data: [DONE]\n\n")); + await assert.rejects(new Response(decryptSse(wire(bytes.replace('"charge":8', '"charge":9')), key, + { input: 1, output: 2 }, () => assert.fail("wrong charge accepted"))).text(), /charge.*didn't match/); +}); + +test("encrypted thinking marker split across chunks preserves tools after the marker", async () => { + const data = ["thinking read"].map((content) => + `data: ${JSON.stringify({ choices: [{ delta: { content: seal(content) } }] })}\n\n`).join("") + "data: [DONE]\n\n"; + const decoded = await new Response(decryptSse(wire(data, 1), key, undefined, undefined, undefined, undefined, true)).text(); + const chunks = decoded.split("\n").filter((line) => line.startsWith("data: {")).map((line) => JSON.parse(line.slice(6))); + assert.equal(chunks.map((chunk) => chunk.choices[0].delta.content ?? "").join(""), "read"); +}); + +test("Pi receives an error rather than an executable native tool request", async () => { + const { streamSimple } = await import("../vendor/pi/packages/ai/dist/api/openai-completions.js"); + const model = { id: "synthetic/model", name: "Synthetic", provider: "nearai", api: "openai-completions", baseUrl: "http://synthetic.invalid/v1", + reasoning: false, input: ["text"], contextWindow: 4096, maxTokens: 128, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } }; + const bad = `data: ${JSON.stringify({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "synthetic-call", type: "function", function: { name: "read", arguments: '{"path":"synthetic.txt"}' } }] }, finish_reason: "tool_calls" }] })}\n\ndata: [DONE]\n\n`; + const response = await streamSimple(model, { messages: [{ role: "user", content: "synthetic", timestamp: Date.now() }] }, + { apiKey: "synthetic", fetch: async () => new Response(decryptSse(wire(bad), key), { headers: { "content-type": "text/event-stream" } }) }).result(); + assert.equal(response.stopReason, "error"); + assert.ok(!response.content.some((part) => part.type === "toolCall")); +}); diff --git a/tests/verify.sh b/tests/verify.sh index 026fe89..8e05e62 100755 --- a/tests/verify.sh +++ b/tests/verify.sh @@ -34,6 +34,7 @@ suites=( "offline|chat-signature (NEAR per-chat signature, recorded reply and forgeries)|node --test tests/chat-signature.test.mjs" "offline|mac-coupon-issuer (Apple issuer binding, synthetic contract only)|node --test tests/mac-coupon-issuer.test.mjs" "offline|coupon-staging (signed service staging admission, exact schema)|node --test tests/coupon-staging.test.mjs" + "offline|near tool boundary (encrypted text and native field refusal)|node --test tests/near-sse.test.mjs" "offline|intools (in-content tool call parser)|node --test tests/intools.test.mjs" "offline|regress (tests/regress fixtures replayed)|tests/regress/run.sh" "offline|report (/report, abalone report)|tests/report.sh" "offline|fetch (/fetch routes, stub arXiv)|tests/fetch.sh" "offline|tor (built-in Tor transport, stub client)|node --test tests/tor.test.mjs"