Repository navigation
fix(llm): text inside a nonce fence cannot carry the nonce or a fence tag in any written form - #388
Merged
Merged
Conversation
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: newneutralizeFence(text, nonce, reservedTags);fenceduses it.backend/src/modules/chat/engine/contextBuilders.ts:neutralizeFenceTokenscalls it (same four reserved tags).backend/src/modules/playbooks/playbooks.draft.ts:fencedcalls 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 infencedis gone).docs/UPSTREAM_COMPATIBILITY_LEDGER.md: one row for the function shared with upstream.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<). 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,dependencyCyclesand the three route suites;npm run buildandnpm run typecheck:testinbackend/.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 build0,typecheck:test0; 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.