Skip to content

feat: add token budget circuit breaker and session log to agent loop#245

Merged
l50 merged 3 commits into
mainfrom
worktree-session-context-mgmt
Apr 29, 2026
Merged

feat: add token budget circuit breaker and session log to agent loop#245
l50 merged 3 commits into
mainfrom
worktree-session-context-mgmt

Conversation

@l50
Copy link
Copy Markdown
Contributor

@l50 l50 commented Apr 29, 2026

Key Changes:

  • Introduced token budget circuit breaker to halt agent loop when cumulative input/output token limits are exceeded
  • Added append-only JSONL session log for crash recovery and debugging
  • Enhanced context compaction with proactive and reactive trimming and semantic recap
  • Exposed new configuration options for budget, logging, and prompt cache through environment variables

Added:

  • Token budget enforcement (BudgetConfig) that ends loops with BudgetExceeded reason when input, output, or total token budgets are reached; configurable via env vars
  • Append-only JSONL session log (SessionLog and SessionLogConfig) that records all conversation events, tool results, usage stats, and outcomes for each agent loop in a crash-tolerant manner
  • Proactive context compaction logic, including semantic recap of trimmed messages, with configurability for compaction cadence and thresholds
  • New public exports for session logging and budget configs in agent loop modules and library root

Changed:

  • Agent loop runner now checks token budgets each iteration and exits early if limits are exceeded
  • Context compaction upgraded to support both proactive (threshold-based) and reactive (hard ceiling) trimming, with detailed logging and semantic recaps of dropped content
  • Anthropic provider now attaches prompt-cache breakpoints to system/tool blocks when enabled, improving cache hit rates and efficiency
  • Configuration loading for agent loop now layers environment variable overrides on top of defaults for agent, context, budget, and session log settings
  • All agent loop terminal paths (success, error, budget, assistance) now consistently log outcomes to the session log if enabled

Removed:

  • Previous static context trimming that lacked proactive compaction and semantic recaps
  • Unused or redundant context compaction code paths in favor of unified maybe_compact and semantic recap mechanism

…nd token budget circuit breaker

**Added:**

- Session logging framework for agent loops, writing append-only JSONL logs per task via new `SessionLog` and `SessionLogConfig` types
- Support for replaying message history from session logs for crash recovery and debugging
- Proactive conversation context compaction with configurable thresholds and cadence to manage token usage, including semantic recap of trimmed messages
- Per-loop token budget circuit breaker (`BudgetConfig`) with env-var overrides, terminating loops that exceed configured input, output, or total token limits
- New `LoopEndReason::BudgetExceeded` variant for reporting budget-related terminations
- Env-var driven configuration for agent loop, context, budget, and session log parameters

**Changed:**

- Anthropic provider now attaches prompt cache breakpoints to system and tool definitions when enabled, improving caching effectiveness
- Agent loop runner tracks session log events for every major step (messages, usage, compaction, terminal outcome) for observability
- Conversation compaction logic now distinguishes between proactive (threshold-based) and reactive (hard ceiling) triggers, with improved summary markers
- All configuration objects extended to support env-var overrides for fine-grained tuning without code changes
- Tests updated and expanded to cover new compaction, budget, and session log behaviors

**Removed:**

- Legacy conversation trimming logic replaced with new proactive compaction flow
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 83.57571% with 192 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.10%. Comparing base (d750941) to head (4713213).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
ares-llm/src/agent_loop/runner.rs 58.74% 118 Missing ⚠️
ares-llm/src/agent_loop/context.rs 88.04% 22 Missing ⚠️
ares-cli/src/orchestrator/dispatcher/submission.rs 0.00% 20 Missing ⚠️
ares-llm/src/agent_loop/session_log.rs 95.22% 13 Missing ⚠️
ares-llm/src/agent_loop/config.rs 98.01% 6 Missing ⚠️
ares-cli/src/worker/blue_task_loop.rs 0.00% 5 Missing ⚠️
ares-cli/src/orchestrator/blue/investigation.rs 0.00% 3 Missing ⚠️
ares-cli/src/orchestrator/llm_runner.rs 0.00% 3 Missing ⚠️
ares-cli/src/orchestrator/blue/callbacks.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #245      +/-   ##
==========================================
+ Coverage   74.94%   75.10%   +0.16%     
==========================================
  Files         382      383       +1     
  Lines       80418    81461    +1043     
==========================================
+ Hits        60268    61183     +915     
- Misses      20150    20278     +128     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

l50 added 2 commits April 29, 2026 16:05
**Changed:**

- Reformatted multiline function calls and match arms for improved readability in `context.rs`, `runner.rs`, and `session_log.rs`
- Standardized closure formatting for iterator methods in `session_log.rs`
- Corrected typo in log message ("unparseable" to "unparsable") in `session_log.rs`
…ion log

**Added:**

- Added tests for environment variable parsing functions to ensure correct defaulting and fallback logic in `config.rs`
- Introduced tests for `describe_reason` covering all `LoopEndReason` variants in `runner.rs`
- Added tests for session log compaction, message replay, unparsable line handling, and path sanitization in `session_log.rs`
@l50 l50 merged commit ff10d2b into main Apr 29, 2026
11 checks passed
@l50 l50 deleted the worktree-session-context-mgmt branch April 29, 2026 22:29
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