Skip to content

Mid-run request transform for threshold compaction (#805) - #817

Open
hr1juldey wants to merge 1 commit into
huggingface:mainfrom
hr1juldey:feat/805-mid-turn-context-transform
Open

hr1juldey wants to merge 1 commit into
huggingface:mainfrom
hr1juldey:feat/805-mid-turn-context-transform

Conversation

@hr1juldey

Copy link
Copy Markdown

Closes #805

Summary

  • run_agent_loop gains an optional transform_context hook, 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.
  • AgentHarnessConfig exposes the hook and passes it through to the loop.
  • CodingSession binds 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 past auto_compact_token_threshold unchecked — 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

  • Request-scoped, not durable. The running turn's messages are not session entries yet, so a durable CompactionEntry cannot 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.
  • Never split a tool call from its results. A run contains exactly one user message (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.
  • Measurement precedence: provider-reported usage from the last applicable response when available (truthful), estimator otherwise (the only signal on silent truncation). last_applicable_provider_usage is publicized from context_window for this.
  • Honours cancellation and auto_compact_enabled; any summary failure returns the original payload and the turn continues (boundary-path behaviour).

What's unchanged

  • Boundary compaction semantics, CompactionEntry format, overflow handling, and the repair path (repair_tool_history) are untouched. The hook is a no-op unless a consumer sets transform_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.
  • Full suite: 2259 passed, 1 failed — test_tui_app.py::test_tui_tree_labels_filter_timestamps_and_clear[asyncio], pre-existing on main (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.
  • Rebased on upstream/main @ 2ff567d.

Refs #781 (composition: the same compaction_start/end events and plan machinery remain the durable layer this hook defers to).

…#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

No deployments
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.

Auto-compaction never runs inside a long agent run

1 participant