TUI Gateway: pre-compress idle sessions to reduce next-turn latency - #38956
TUI Gateway: pre-compress idle sessions to reduce next-turn latency#38956mchen0571 wants to merge 2 commits into
Conversation
bce83fa to
2625d2a
Compare
|
Rebased this PR onto the latest Local verification run after the rebase:
Current note: GitHub still shows no check runs/status contexts for this fork PR, so CI appears to be awaiting maintainer workflow approval or not configured for this path. |
teknium1
left a comment
There was a problem hiding this comment.
Thanks for the careful opt-in design and the snapshot/version guards. The feature is not present on current main, but the current session machinery has moved substantially and exposes two blocking issues.
Problems
tui_gateway/server.py:2160/2227start the profile-sensitive idle worker without the per-sessionHERMES_HOMEoverride. Current prompt execution installs that override attui_gateway/server.py:8935-8937, and resume recordsprofile_homeattui_gateway/server.py:5899-5903. The worker must preserve that profile scope.tests/test_tui_gateway_server.py:3074asserts that a prompt is rejected during compaction. Currentprompt.submitdeliberately queues busy input through_handle_busy_submit(tui_gateway/server.py:8447-8452); retaining the rejection would regress the no-drop busy-input behavior.
Suggested changes
- Scope the idle worker to
session["profile_home"]and add a named-profile integration regression. - Integrate compaction with the current queued-input path, cancelling/defering compaction safely rather than rejecting the prompt.
Automated hermes-sweeper review.
| intentionally quiet by default: success should make the next turn faster, | ||
| not generate user-visible chatter. | ||
| """ | ||
| cfg = _load_idle_compression_config() |
There was a problem hiding this comment.
This daemon path never installs session["profile_home"] as the HERMES_HOME override. Current prompt execution does so at tui_gateway/server.py:8935-8937; without equivalent scope here, a resumed named-profile session can read the default profile's compression.idle settings and compact outside its profile context.
There was a problem hiding this comment.
Fixed in 0d23b55. Both idle scheduling and the actual worker install the live session's profile_home through the HERMES_HOME ContextVar and restore it in finally; there is no process-global os.environ mutation. Added real-thread regression coverage for default→named and named→default profile isolation.
| {"id": "1", "method": "prompt.submit", "params": {"session_id": "sid", "text": "hi"}} | ||
| ) | ||
|
|
||
| assert resp["error"]["code"] == 4009 |
There was a problem hiding this comment.
Please do not lock in a rejection here. Current main routes a busy prompt.submit through _handle_busy_submit (tui_gateway/server.py:8447-8452) so input is queued instead of dropped; idle compaction needs to preserve that contract while safely deferring or cancelling the compaction.
There was a problem hiding this comment.
Fixed in 0d23b55. idle_compression_running now follows the existing busy-submit path: queue, steer, and interrupt modes all accept and queue input without steering into or interrupting the compactor. The started compaction completes, synchronizes the continuation session key, then drains queued input exactly once. Added lossless/busy-mode tests plus finalize/replacement Event-barrier race coverage.
- isolate named profiles with thread-scoped HERMES_HOME overrides - queue busy input losslessly across queue, steer, and interrupt modes - preserve live registry identity without long-lived sid fields - serialize session-key handoff with finalize and compensate stale lease, notifier, YOLO, and worker state after blocked transfers - keep LLM, sync, drain, and worker start operations outside registry and history locks Signed-off-by: cm <cm@local>
2625d2a to
0d23b55
Compare
|
Thanks @mchen0571 — this was a carefully engineered take on the idle-session compression problem: opt-in design, snapshot/version + replacement-identity fences, per-session handoff lock, real-thread named-profile regression tests in both directions, and the lossless busy-input queueing rework after review. The trigger half has since landed via #69360: The surviving delta — true background pre-compression so the first post-idle turn doesn't pay compaction latency — is a real idea, but this branch predates the per-session compression lock, the deferred engine-notify contract (#69324), lock-skip feedback (#69870), and the quiet-status hooks (#69859), all of which any background compressor must now compose with; the two review blockers (profile-scope gap, busy-input rejection test regressing Closing as superseded by #69360, with credit for the design work. |
PR: TUI Gateway: pre-compress idle sessions to reduce next-turn latency
Summary
Adds opt-in idle pre-compression for TUI Gateway sessions. When enabled, the Gateway schedules a quiet background compaction pass after a session has been idle long enough, reducing the chance that the next user prompt blocks on context compression.
Why
Long-running sessions often hit context compression on the next user turn. That puts compression latency on the user's critical path. If the session is already idle after a long response, the Gateway can safely use that idle window to compact context ahead of the next prompt.
Maintainer feedback addressed
Named-profile isolation
Both idle scheduling and the actual worker execution install the live session's
profile_homethrough the HERMES_HOME ContextVar and restore it infinally. No process-globalos.environmutation is used. Regression coverage runs the worker in a real thread against temporary default/named profile homes in both directions.Busy input remains lossless
idle_compression_runningparticipates in the existing busy-input path. Aprompt.submitreceived during idle compaction is accepted and queued for allqueue,steer, andinterruptmodes; it is never steered into or used to interrupt the compactor. The already-started compaction completes, its continuation session key is synchronized, and the queued user turn drains exactly once afterward.Details
compression.idleconfig.compression.threshold * 0.9when no explicit idle threshold is configured.compression.enabled; disabling global compression also disables idle compression.sidfields on create/deferred session records.session_keyafter_compress_contextrotates the agent continuation session, with atomic live/finalized commit and deterministic Event-barrier race coverage._sidteardown contract.Tests
Latest local validation after rebasing onto
NousResearch/main:Scope
This remains an opt-in TUI Gateway feature. It does not change the default compression behavior, production configuration, or non-TUI Gateway services.