Skip to content

feat(agent-loop): aggressive prune tier at 60% context (#3) - #221

Closed
yogthos wants to merge 1 commit into
feat/improve-1-compaction-circuit-breakerfrom
feat/improve-2-aggressive-prune-tier
Closed

feat(agent-loop): aggressive prune tier at 60% context (#3)#221
yogthos wants to merge 1 commit into
feat/improve-1-compaction-circuit-breakerfrom
feat/improve-2-aggressive-prune-tier

Conversation

@yogthos

@yogthos yogthos commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Phase 2 of IMPROVEMENTS_PLAN. Stacked on #220 (base = circuit-breaker branch).

Problem

cap_oversized_tool_results used a flat 3000-token cap at every context level. Near the limit, one uncapped tool result could push the next request over before the reactive 75% post-response fold fires.

Fix

Tiered cap via a pure tiered_result_cap(estimate, ctx_max): above AGGRESSIVE_CAP_THRESHOLD = 0.60 the per-result cap tightens to AGGRESSIVE_RESULT_CAP_TOKENS = 1000; below, it stays 3000. 60% sits under the 75% fold trigger so the tighter cap has room to work first. Wired at the pre-send cap site in run_loop.

Unit test covers normal (<60%), the strict 60% boundary, aggressive (>60%), and a div-by-zero guard. 2136 pass at -D warnings.

Review note: this PR targets the #220 branch (stacked). Merge #220 first; this will retarget to main automatically.

Next: #4 snip feedback loop (builds on this), then #2 file restore, then #5 report enrichment.

…PLAN #3)

The per-result cap (cap_oversized_tool_results) was a flat 3000 tokens
regardless of context pressure. Near the limit, a single uncapped tool
result could push the NEXT request over before the reactive 75%
post-response fold fires.

Add a tiered cap: above AGGRESSIVE_CAP_THRESHOLD (60% estimated context)
the per-result cap tightens to AGGRESSIVE_RESULT_CAP_TOKENS (1000) via a
pure `tiered_result_cap(estimate, ctx_max)` helper; below it stays at
3000. The 60% threshold sits below the 75% fold trigger so the tighter
cap has room to work first. Wired at the pre-send cap site in run_loop.

Unit test for the tiering (normal below 60%, strict boundary at 60%,
aggressive above). 2136 pass at -D warnings.

Stacked on the circuit-breaker branch (PR #220).
@yogthos

yogthos commented May 29, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #225 (merged). This PR was auto-closed by GitHub when its stacked base branch was deleted on merge of #220; #225 carries the identical content and is part of the merged stack.

allen-munsch pushed a commit to allen-munsch/dirge that referenced this pull request May 29, 2026
The pre-send snip (cap_oversized_tool_results) freed tokens but never
reported how many, so the post-response fold always fired at 75% even
when the snip had just bought plenty of headroom.

- Add `cap_oversized_tool_results_counted` — wraps the unchanged capper
  and reports tokens freed (measured with the same estimator the fold
  decision uses). Existing callers stay on the Vec-returning fn.
- run_loop tracks `snip_tokens_freed` from the (now tiered) cap site.
- In the post-usage Fold path, a pure `snip_bought_enough(freed,
  ctx_max, aggressive)` skips a NORMAL fold when the snip freed
  > SNIP_SUFFICIENT_FRACTION (10%) of the window. Aggressive /
  force-summary folds still fire. The credit resets after each
  post-usage decision so a stale snip can't suppress a later fold.

Tests: snip_bought_enough gating (normal vs aggressive, <10%, div-0
guard) and cap_counted freed-token accuracy. 2138 pass at -D warnings.

Stacked on the aggressive-prune branch (PR dirge-code#221).
allen-munsch pushed a commit to allen-munsch/dirge that referenced this pull request Jun 3, 2026
The pre-send snip (cap_oversized_tool_results) freed tokens but never
reported how many, so the post-response fold always fired at 75% even
when the snip had just bought plenty of headroom.

- Add `cap_oversized_tool_results_counted` — wraps the unchanged capper
  and reports tokens freed (measured with the same estimator the fold
  decision uses). Existing callers stay on the Vec-returning fn.
- run_loop tracks `snip_tokens_freed` from the (now tiered) cap site.
- In the post-usage Fold path, a pure `snip_bought_enough(freed,
  ctx_max, aggressive)` skips a NORMAL fold when the snip freed
  > SNIP_SUFFICIENT_FRACTION (10%) of the window. Aggressive /
  force-summary folds still fire. The credit resets after each
  post-usage decision so a stale snip can't suppress a later fold.

Tests: snip_bought_enough gating (normal vs aggressive, <10%, div-0
guard) and cap_counted freed-token accuracy. 2138 pass at -D warnings.

Stacked on the aggressive-prune branch (PR dirge-code#221).
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