Skip to content

fix(llm): fencing a long text gives other requests their turn - #390

Merged
IvanJS17 merged 3 commits into
mainfrom
fix/long-fenced-text-does-not-hold-the-process
Nov 28, 2026
Merged

IvanJS17 merged 3 commits into
mainfrom
fix/long-fenced-text-does-not-hold-the-process

Conversation

@IvanJS17

@IvanJS17 IvanJS17 commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fencing a long text no longer holds the API process. neutralizeFenceAsync does the same work as neutralizeFence in slices of at most 100,000 units and gives the event loop a turn between slices; the output is byte for byte the same.

What changed

  • promptFence.ts: one shared core (fast reject, matcher loop, edit application) driven in one go by neutralizeFence and in slices by neutralizeFenceAsync; fencedAsync; a reserved name whose fold starts with / or a blank is now refused like one that contains <.
  • contextBuilders.ts: spotlightAsync, spotlightWorkflowAsync. The tool handlers (document reads, search and page results, delegate results, live Word text, workflow bodies) use them.
  • Architecture tests: a tool handler imports no synchronous fence builder under any name (one listed exception with its bound); every call of an asynchronous fence builder is awaited.
  • Ledger row extended.

Why

Fencing costs in proportion to the text, and a document read can hand the model up to 16 MiB of it. The work on a long text is now split into slices so that other requests keep advancing while it runs. Nothing is cut or rejected: the model receives the same text as before.

Testing

Differential tests (existing corpora plus tokens, surrogate pairs and multi-unit folds placed on slice boundaries, with small and real slice sizes), turn-count tests for the scan and for the application of edits, precondition tests with an exhaustive code-space check, read-path tests through the tool handler (document and workflow reads), the guard tests with samples. Mutants of each rule fail the matching test. Focused set: 2905 passed; build and test typecheck clean.

Measured on a shared, loaded host with 16 MiB inputs through the real builder: the longest stretch without a turn of the event loop is 3 to 7 ms for ordinary text and typically 20 to 65 ms for the slowest texts, with single stretches of 100 to 140 ms seen on the loaded host (before: one stretch of 1.3 to 2.9 s). Total time is near the synchronous form. A 100-character text costs about 1.5 µs more per call when it contains < and about the same when it does not.

Not changed here: the caller's first use of a very large result and its serialization are still done in one go (up to about 0.3 s for a 16 to 21 MB result, as before), and a call whose request has ended runs to its end, now without holding the process.

Deploy notes

No migration, no setting. What the model receives does not change.

The fence neutralization has an asynchronous form that does the same
work in slices and lets the event loop run between them; the result is
the same string. The tool handlers, whose texts can be long, use it. A
reserved tag name that starts with '/' or a blank is refused, like one
that contains '<'.
…edit steps

A slice is 100,000 units. Every call of an asynchronous fence builder
is awaited, a tool handler imports no synchronous builder under any
name, and the application of edits takes its turns: each has a test.
…does-not-hold-the-process

# Conflicts:
#	docs/UPSTREAM_COMPATIBILITY_LEDGER.md
@IvanJS17
IvanJS17 merged commit 920c5f7 into main Nov 28, 2026
13 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