Files
loki/scripts/test/loki-prose-gate.ts
CatoandClaude Opus 5 e5f8e8ea49 fix(loki): let it think, and fetch the thing you asked about (#668)
Asked "what do you think about heidi", Loki answered:

    ...`.

    Everything is clean and strictly compliant.
    </think>An opinion or assessment is: Not in your data.

…and then listed every record containing "Heidi", including two unrelated
people who happen to be named Heidi. Four defects, stacked.

## 1. A model's private reasoning was persisted as the answer

There is a literal `</think>` in the database. This repo already knew the
trick — TWICE, in `frontier/propose.ts` and `orchestration/dod-gate.ts` — and
both copies exist to make JSON parse. The one path a HUMAN reads had none.

`stripReasoning` now lives once, at the model seam next to
`stripToolCallLines`, and is applied at all four seams (tool loop, groq
buffered, groq streamed, gateway). The two old copies call it.

`lastIndexOf` rather than a regex is load-bearing: the leak had a CLOSING tag
and no opening one, because the head of the reasoning was lost upstream. A
`<think>[\s\S]*?</think>` pattern matches nothing there.

While streaming, nothing can know text is reasoning until the tag arrives — so
the gate now RETRACTS what it showed the moment the tag proves what it was.

## 2. It was forbidden to have a view

`buildContract` says every claim must cite a record (1) and anything without
one gets exactly "Not in your data." (3). An opinion is not a record, so the
model looked one up, failed, and refused. It was obeying the contract exactly.

ai-kit's OTHER builder, `buildAssistantRules`, already carries the equivalent
carve-out as its rule 5 ("general knowledge … is not covered by rules 1–2").
The typed-record builder never grew one — so the WEAKER contract is the usable
one and the stronger contract forbids reasoning.

Rules 7–10 are added as a NARROWING, inserted INSIDE the contract section:
the contract opens with "this overrides every formatting instruction below",
so a narrowing placed after the records would be outranked by the rule it
narrows. Both grounded paths share one assembly point, because the production
refusal came through the gateway while the loop carried the same defect.

The durable fix belongs upstream in `buildContract`; noted in the module.

## 3. Retrieval fetched the wrong subject

This was the one that made the answer useless, and it survived the prompt fix.

`peopleFacts(userId, message)` searches by the message. `documentFacts(userId,
message, limit)` searches by the message. `projectFacts(userId)` ignored it:
projects came back in table order and were then head-sliced. With ~30
registered projects that is a lottery, and the `Heidi` PROJECT lost it — while
the people search matched two contacts by name. Retrieval chose the subject of
the answer, and chose wrong. No prompt rule can recover a record that was
never fetched.

Projects the message NAMES are now ranked ahead of the slice. Pure, in its own
module because `sources.ts` imports `@/db`, which throws at load without a
connection string — and this repo's unit suite is env-independent.

## 4. A citation the verifier could not see

Found only by running it: nemotron cited `【F16】` in fullwidth brackets. Both
readers are ASCII-only, so the verifier's unknown-citation check silently had
nothing to check, and the renderer printed the handle verbatim as plumbing.
Normalised at the seam, before verification.

## Verified

Against REAL production records through the read-only tunnel, same question:

    Heidi is an active project — status active [F16]. Its recorded description
    is "Learn to understand and text in Zurich Swiss German.
    heidi.orangecat.ch" [F16]. The latest development log notes the creation of
    a /check page … and a PR #4 that has not yet been merged [F16].

No reasoning tags, no refusal, ASCII citations, 0 grounding violations.

Three new pure suites, each proven by MUTATION: removing the judgment clause
fails 7/10 checks; a no-op `stripReasoning` fails 3. The prose-gate test
replays the exact production bytes.


Claude-Session: https://claude.ai/code/session_018SCNNZ3cqMFzQtuGe6e1V9

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-12 02:28:36 -07:00

214 lines
6.6 KiB
TypeScript

/**
* The streamed-prose gate: what an operator is allowed to see while a model is
* still writing.
*
* This is the one new piece of the streaming path that can put plumbing on
* someone's screen. The buffered path has always stripped `TOOL:` / `ARGS:`
* lines before anyone saw them (`stripToolCallLines`); a stream that forwarded
* bytes as they arrived would show the operator a tool call being narrated and
* then delete it a moment later — and on a weak model, narrating calls in prose
* is the COMMON case, not the edge case.
*
* Pure: no provider, no socket, no model.
*/
import {
createProseGate,
normaliseCitations,
parseTextToolCalls,
stripReasoning,
stripToolCallLines,
} from "@/lib/agent/llm";
let failures = 0;
function check(name: string, actual: unknown, expected: unknown) {
const a = JSON.stringify(actual);
const e = JSON.stringify(expected);
if (a === e) {
console.log(` ✓ ${name}`);
} else {
console.error(` ✗ ${name}\n expected: ${e}\n actual: ${a}`);
failures++;
}
}
/**
* Feed `chunks` through the gate and return what the operator is left looking
* at — modelling the real client, which CLEARS its preview on `reset` (see
* `useLokiStream`). Accumulating and ignoring reset would make a retraction
* look like a leak that was never taken back.
*/
function run(chunks: string[]): string {
let out = "";
const gate = createProseGate(
(t) => {
out += t;
},
() => {
out = "";
},
);
for (const chunk of chunks) gate.push(chunk);
gate.end();
return out;
}
console.log("loki prose gate");
check("plain prose is released", run(["Hello, ", "world."]), "Hello, world.\n");
check(
"a line is only released once a newline proves it finished",
(() => {
let out = "";
const gate = createProseGate((t) => {
out += t;
});
// `TOO` could still become `TOOL:` — releasing it here is the bug.
gate.push("TOO");
return out;
})(),
"",
);
check(
"a narrated tool call never reaches the operator",
run(["Let me check.\n", "TOOL: search_people\n", 'ARGS: {"query": "Elena"}\n']),
"Let me check.\n",
);
check(
"the gate stays shut for the multi-line ARGS object that follows",
run(["TOOL: search_people\n", "ARGS:\n", "```json\n", '{\n "query": "Elena"\n}\n', "```\n"]),
"",
);
check(
"shape variants models actually emit are all caught",
run(["- **TOOL:** list_projects\n", "> ARGS = {}\n"]),
"",
);
check(
"a tool call split across chunk boundaries is still caught",
run(["TO", "OL: sea", "rch_people\n"]),
"",
);
check(
"the final line is released even with no trailing newline",
run(["The answer is 42."]),
"The answer is 42.\n",
);
// The gate's whole purpose is to agree with the buffered path about what counts
// as prose. If these two ever disagree, the preview shows something the saved
// message does not contain — which is the failure the swap-on-`message` rule in
// lib/loki/stream.ts exists to catch, and this test exists to prevent.
const narrated = 'Checking now.\nTOOL: search_people\nARGS: {"query": "Elena"}';
check(
"gate agrees with stripToolCallLines on the same text",
run([narrated]).trim(),
stripToolCallLines(narrated),
);
// And the call it hid must still be the call the loop runs — hiding it from the
// screen must not hide it from the parser.
check(
"the hidden call is still parsed and executed",
parseTextToolCalls(narrated, ["search_people"]).map((c) => [c.name, c.args]),
[["search_people", { query: "Elena" }]],
);
// ── Reasoning leak ─────────────────────────────────────────────────────────
// Observed in production on gemini-flash via the gateway. The PERSISTED answer
// began mid-thought and carried a bare closing tag:
//
// ...`.
//
// Everything is clean and strictly compliant.
// </think>An opinion or assessment is: Not in your data.
//
// Note there is no OPENING tag — the head of the reasoning was lost before it
// reached us, which is exactly why a `<think>[\s\S]*?</think>` regex would have
// matched nothing and left the whole thing on screen.
const LEAKED = [
"...`.",
"",
"Everything is clean and strictly compliant.",
"</think>An opinion or assessment is: Not in your data.",
].join("\n");
check(
"a dangling close tag still drops the reasoning before it",
stripReasoning(LEAKED),
"An opinion or assessment is: Not in your data.",
);
check(
"a well-formed reasoning block is dropped whole",
stripReasoning("<think>weighing the options</think>The answer is 42."),
"The answer is 42.",
);
check("text with no reasoning is untouched", stripReasoning("Just an answer."), "Just an answer.");
check(
"only the LAST close tag counts, so a quoted tag mid-reasoning cannot end it early",
stripReasoning("<think>a</think>b</think>real answer"),
"real answer",
);
check(
"reasoning never streams to the screen",
run(["<think>\n", "I should check the records.\n", "</think>\n", "Heidi looks early.\n"]),
"Heidi looks early.\n",
);
check(
"a leak with no opening tag is RETRACTED once the close tag proves what it was",
run(["Everything is clean.\n", "</think>Heidi looks early.\n"]),
"Heidi looks early.\n",
);
check(
"prose before an opening tag is still owed to the reader",
run(["Here goes. <think>\n", "thinking\n", "</think>\n", "Done.\n"]),
"Here goes. \nDone.\n",
);
// ── Citation brackets ──────────────────────────────────────────────────────
// Observed live on nemotron: 【F16】 rather than [F16]. Both readers of a
// citation are ASCII-only, so a fullwidth bracket is invisible to the VERIFIER
// (its unknown-citation check silently has nothing to check) and unrecognised
// by the RENDERER (the handle prints verbatim in the prose).
check(
"a fullwidth citation is normalised to the shape both readers expect",
normaliseCitations("Heidi is active【F16】."),
"Heidi is active[F16].",
);
check(
"a multi-id fullwidth citation keeps every id",
normaliseCitations("Both hold【F1, D2】here."),
"Both hold[F1,D2]here.",
);
check(
"an ASCII citation is left exactly as it is",
normaliseCitations("Already fine [F8] here."),
"Already fine [F8] here.",
);
check(
"fullwidth brackets around non-citations are not touched",
normaliseCitations("The label 【important】 stays."),
"The label 【important】 stays.",
);
if (failures > 0) {
console.error(`\n${failures} check(s) failed`);
process.exit(1);
}
console.log("✓ loki prose gate: all checks passed");