Repository navigation
Conversation
…#805) run_agent_loop gains an optional async-capable transform_context hook, applied to every provider request payload through a single seam (_apply_transform_context). The hook receives a fresh copy plus the cancellation signal, never mutates durable history, and lets best-effort consumers shape or compact the request without touching loop semantics. CodingSession binds the hook to a request-scoped compaction: when a mid-run payload crosses auto_compact_token_threshold (boundary checks only fire before a prompt and after a turn, so one long tool run can grow unchecked - and local OpenAI-compatible servers truncate silently instead of raising), the compacted prefix is replaced by a summary for that request. The summary is generated once per over-threshold episode and reused; the keep-recent boundary walks back to the earliest dangling tool call so a call is never split from its results; failures keep the original context. Durable CompactionEntry materialization stays with the existing boundary paths. context_window.last_applicable_provider_usage is publicized for the session-side measurement (provider-reported usage first, estimator fallback for silent truncation). Adversarial coverage: payload-vs-durable isolation, hostile input mutation, sync/async parity, per-request application, signal plumbing, payload reordering, exception propagation, list aliasing, mid-run firing, summary failure survival, cancellation, boundary-walk tool-call grouping invariant. Refs huggingface#805
This branch has not been deployed
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.
Closes #805
Summary
run_agent_loopgains an optionaltransform_contexthook, applied to every provider request payload through a single seam (_apply_transform_context). It receives a fresh copy of the payload plus the cancellation signal, may be sync or async, never mutates durable loop history, and lets a consumer shape or compact the request without touching loop semantics. Exceptions propagate — best-effort consumers catch their own failures and return the original context. This follows the existing optional-callable pattern in the loop (get_steering_messages,before_tool_call,after_tool_call), extended with awaitable support since compaction must summarize.AgentHarnessConfigexposes the hook and passes it through to the loop.CodingSessionbinds the hook to a request-scoped mid-run compaction: boundary checks (_try_auto_compact) only fire before a prompt and after a turn, so one long run of tool calls can grow pastauto_compact_token_thresholdunchecked — and local OpenAI-compatible servers truncate the context silently instead of raising an overflow error, so the existing recovery path never sees it. When a request payload crosses the threshold mid-run, the compacted prefix is replaced by a summary for that request.Design decisions
CompactionEntrycannot be written mid-run. The summary replaces only the payload for that request; durable materialization stays with the existing boundary paths, which still compact normally after the turn. The summary is generated once per over-threshold episode and reused for subsequent requests of the same run; the cache drops as soon as a request measures under threshold again.usermessage (the prompt), so there is no user anchor to keep to mid-run. Instead the keep-recent boundary walks back to the earliest dangling tool call (_plan_payload_compaction) until the split is clean; when no clean boundary exists the payload is sent unchanged.last_applicable_provider_usageis publicized fromcontext_windowfor this.auto_compact_enabled; any summary failure returns the original payload and the turn continues (boundary-path behaviour).What's unchanged
CompactionEntryformat, overflow handling, and the repair path (repair_tool_history) are untouched. The hook is a no-op unless a consumer setstransform_context.Validation
uv run pytest tests/test_loop_transform_context.py tests/test_midturn_compaction.py— 14 passed. Covers: payload-vs-durable isolation, hostile input mutation, sync/async parity, per-request application, signal plumbing, payload reordering, exception propagation, list aliasing, mid-run firing across a two-tool run (summary present in the final request, compacted prefix absent, tool-call grouping intact), summarizer-down survival, cancellation, under-threshold no-op, and the boundary-walk invariant.test_tui_app.py::test_tui_tree_labels_filter_timestamps_and_clear[asyncio], pre-existing onmain(fails identically without this branch).uv run mypy src/tau_agent/loop.py src/tau_agent/harness.py src/tau_coding/session.py— clean.uv run ruff check+ruff format --check— clean.upstream/main@2ff567d.Refs #781 (composition: the same
compaction_start/endevents and plan machinery remain the durable layer this hook defers to).