Repository navigation
perf(agent): isolate prompt rendering from event loop - #2328
Closed
liuhao-labs wants to merge 5 commits into
Closed
liuhao-labs wants to merge 5 commits into
liuhao-labs wants to merge 5 commits into
Conversation
3 tasks done
zhuyijie88
reviewed
Aug 31, 2026
3 tasks done
liuhao-labs
force-pushed
the
codex/2285-tokenizer-worker
branch
from
August 31, 2026 04:56
254200a to
bbaff65
Compare
Keep full-history chat-template rendering as the source of truth while moving it to a bounded adapter-owned worker. Add cancellation-safe cleanup and bounded queue, latency, utilization, prompt-length, and event-loop metrics. Related to THUDM#2285 Signed-off-by: Hao Liu <liuaho6@gmail.com>
Track cleanup-owned admission cancellations explicitly so Python 3.10 keeps client cancellation distinct from worker shutdown. Shield permit acquisition and settle it before releasing a successful handoff. Signed-off-by: Hao Liu <liuaho6@gmail.com>
Release an admission permit from the acquisition task completion callback when cancellation races with a successful handoff. Avoid waiting through repeated client cancellations and cover the occupied-permit case. Signed-off-by: Hao Liu <liuaho6@gmail.com>
Make the repeated-cancellation regression distinguish callback cleanup from the prior waiting loop by holding the acquisition task in its cancellation handler. Signed-off-by: Hao Liu <liuaho6@gmail.com>
Signed-off-by: Hao Liu <liuaho6@gmail.com>
liuhao-labs
force-pushed
the
codex/2285-tokenizer-worker
branch
from
October 10, 2026 15:31
256bb42 to
87c2fdb
Compare
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
Long full-history prompt rendering blocks the agent adapter's aiohttp event loop. Move the existing
_session_prompt_ids()call to one adapter-owned worker thread, retaining exact upstream rendering, saved sampled-token prefixes, and safe fallback for changed histories/tool schemas.The worker bounds submitted running/queued work. Canceled requests retain their executor capacity until physical completion, discard queued jobs, return promptly during admission cancellation, and avoid starting SGLang or recording a trajectory after cancellation. App cleanup drains the worker. Session prefix lists/template options are snapshotted before offload; the live session is used for generation and records only flushed turns.
Bounded recent-window metrics at
GET /debug/adapter_metricsinclude prompt length, total prompt-render latency, queue wait, worker utilization, event-loop lag, cancellations and discarded work, with P50/P95/P99 distributions.Related to #2285.
Scope
This improves event-loop responsiveness and observability. It preserves upstream full-history rendering and canonical-boundary checks, including original sampled prompt/response token prefixes introduced by #2457. It does not remove the cumulative O(N²) tokenization cost or claim general incremental tokenization. Sampler score-centering/top-p/R3 metadata, output-token validation, and protocol behavior are unchanged.
Validation
PYTHONPATH=. python -m pytest -q tests/test_agent tests/test_grouped_rewards.py: 169 passed, 2 skipped against main58b08786.git diff --checkpassed. Sunabako 8-GPU/35-minute E2E registration, container init/cidfile cleanup, proxy/mount setup and artifacts remain unchanged. GPU E2E execution is not claimed locally.