Skip to content

fix(llm): text inside a nonce fence cannot carry the nonce or a fence tag in any written form - #388

Merged
IvanJS17 merged 3 commits into
mainfrom
fix/fence-nonce-redacted-in-any-form
Nov 28, 2026
Merged

IvanJS17 merged 3 commits into
mainfrom
fix/fence-nonce-redacted-in-any-form

Conversation

@IvanJS17

@IvanJS17 IvanJS17 commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

Text placed inside a nonce fence for a model (documents, file names, workflow instructions, messages a connected app wrote, trainee attempts, client answers) can no longer carry the nonce or a reserved fence tag in any written form: other letter case, invisible characters, Unicode compatibility forms, whitespace after < or </. Ordinary text is returned unchanged. The three separate copies of the neutralization are now one.

What changed

  • backend/src/lib/promptFence.ts: new neutralizeFence(text, nonce, reservedTags); fenced uses it.
  • backend/src/modules/chat/engine/contextBuilders.ts: neutralizeFenceTokens calls it (same four reserved tags).
  • backend/src/modules/playbooks/playbooks.draft.ts: fenced calls it (same three reserved tags).
  • backend/src/__tests__/regexShapes.test.ts: the count of patterns built at run time goes from 13 to 12 (the one in fenced is gone).
  • docs/UPSTREAM_COMPATIBILITY_LEDGER.md: one row for the function shared with upstream.
  • Tests: promptFence.test.ts, chat/engine/__tests__/fenceAnyForm.test.ts, playbooks/__tests__/draftFence.test.ts.

Why

The fence only holds if the text cannot produce the closing tag or the nonce. The text is compared as a reader would see it; only a matched nonce or tag is replaced (the nonce by [redacted-nonce], the < of a tag by &lt;). The cost is linear in the text length and no memory proportional to the text is kept besides the output and the list of edits. Measured on a shared host, against the previous exact-match code: 200,000 characters of ordinary Spanish or ASCII text cost about 0.15 ms before and 1.5 to 2 ms after; with a < or with some characters that fold (º ª … ™ ½, fullwidth digits) about 0.15 ms before and 5 to 6 ms after, and up to about 14 ms when the text is dense in such characters; a document of thousands of distinct ideographs with a < in it about 6 ms. The extracted-text limit is 16 MiB in bytes, so the longest text is 5.59 million units of ordinary Spanish (about 3 bytes per character) and up to 16.8 million units of ASCII. At that limit: ordinary text about 110 to 540 ms and 10 to 25 MB of extra memory (about 10 ms before); inputs that fold heavily (U+FDFA, thousands of distinct characters) 120 to 300 ms at 5.59 million units; the worst inputs found, 16.8 million units of ASCII that is almost all tag openings (complete, or cut just short of a reserved name), about 1.9 s, roughly thirteen times ordinary text of the same byte budget, and about 100 MB of extra memory for the output and the list of edits (the previous code already needed about 90 MB for that output). The cost is linear (three times the input, three times the time). Bounding what a document read hands to the model is a separate change.

Testing

Focused suites for chat, project-chat, tabular, playbooks, mcp, memory, training and promptFence libs, regexShapes, architecture, dependencyCycles and the three route suites; npm run build and npm run typecheck:test in backend/.

The function is compared, in the test file, with a straightforward implementation that builds the whole folded text, over several thousand random strings (mixed, plain, wide-folding and many-distinct-character alphabets; ASCII and non-ASCII nonces and names): no differences. A reserved tag name cannot contain < (a < in the text always starts a new candidate tag); the function now refuses such a name with an error instead of accepting it, and no caller passes one. Coordinator's run on the final tree, a focused subset of the backend suite: 188 files / 2,861 tests passed (5 skipped), npm run build 0, typecheck:test 0; the whole backend suite ran in CI on this head.

Deploy notes

No migration. Text without a token is returned byte for byte, so cached prompts are unaffected. The lookup tables fill lazily, about 0.1 ms per block of 256 code points the first time a block is seen.

… tag in any written form

One implementation in lib/promptFence, used by the chat context builders,
the training prompts and the playbook drafts: the nonce and the reserved tags
are recognised in other letter case, with invisible characters inside, in
Unicode compatibility forms and with whitespace after the opening bracket.
Only the matched token is replaced; ordinary text is returned as it is, and
the cost stays in proportion to the length of the text.
… text

One pass over the text feeds each folded unit to a nonce matcher and a tag
matcher with constant state; a match records an edit on the original
offsets. Nothing the size of the text is built besides the output and the
list of edits, and a character whose fold is known from a table does not go
through the cache. The result is the same as before for every input, which
the tests compare against the earlier implementation kept in the test file.
…ontains '<'

A '<' in the text always starts a new candidate tag, so a reserved name
whose written form contains '<' could not be recognized. The function now
throws for such a name, whatever the text, instead of accepting it. No
caller passes one. For every other name the result is the same as the
earlier implementation kept in the test file.
@IvanJS17
IvanJS17 merged commit 362e51c into main Nov 28, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant