Skip to content

Fix context accounting, terminal hangs, and sub-agent workspaces - #7

Merged
enowdev merged 4 commits into
enowdev:mainfrom
nvdorman:fix/context-usage-accounting
Jul 31, 2026
Merged

enowdev merged 4 commits into
enowdev:mainfrom
nvdorman:fix/context-usage-accounting

Conversation

@nvdorman

@nvdorman nvdorman commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • resolve compaction limits from active-model metadata and include tool schemas in request-size estimates
  • normalise per-call context size by provider instead of double-counting cached prompt tokens
  • preserve cache-read/write counters separately for billing and analytics
  • prevent persistent terminal sessions from inheriting non-POSIX shells such as fish
  • reset dashboard activity timing per tool/turn state and make Stop interrupt the backend
  • inherit the parent project for background workers and honour isolated worktrees
  • add regression coverage for context/cache semantics, compaction estimation, shell completion, and sub-agent workspace inheritance/isolation

Reproductions

Cached context gauge

In session ses_52aed4a75b7997bc6d6d, an OpenAI-compatible usage record reported input_tokens=145878 and cached_tokens=145408. OpenAI defines cached tokens as a subset of prompt tokens, but the gauge added both and showed roughly 279k/256k (100%). The real context was about 145.9k/256k (~57%). Provider adapters now expose a normalised context size: OpenAI/Gemini use total prompt tokens, while Anthropic includes its separately reported cache read/write tokens.

Terminal hang

In session ses_206f9da8, $SHELL=/bin/fish rejected the POSIX $? completion sentinel, so completed commands waited until the 300-second timeout.

Background sub-agent workspace

In parent session ses_52aed4a75b7997bc6d6d, task task_e5b480030e8ab5c79713 created child ses_73594af85e6d0c9e0274 in the global Antares workspace instead of /home/nvdorman/captcha-solver, making the first file tools fail as outside the workspace. Background delegation now shares the synchronous workspace/isolation path.

Verification

  • go test ./internal/llm ./internal/agent
  • go test ./internal/agent ./internal/worktree ./internal/tools ./internal/server
  • make check
  • bun run build

@nvdorman
nvdorman force-pushed the fix/context-usage-accounting branch from 338dfc7 to 0e3611a Compare July 31, 2026 13:15
@nvdorman nvdorman changed the title Fix context usage accounting and compaction Improve compaction context estimation Jul 31, 2026
@nvdorman nvdorman changed the title Improve compaction context estimation Fix context estimation and terminal hangs Jul 31, 2026
@nvdorman nvdorman changed the title Fix context estimation and terminal hangs Fix context estimation, terminal hangs, and sub-agent workspaces Jul 31, 2026
@nvdorman nvdorman changed the title Fix context estimation, terminal hangs, and sub-agent workspaces Fix context accounting, terminal hangs, and sub-agent workspaces Jul 31, 2026
@enowdev
enowdev merged commit a0ba64e into enowdev:main Jul 31, 2026
@enowdev

enowdev commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Merged — thank you, @nvdorman. This is a well-reasoned fix with real reproductions and solid test coverage, and I reviewed it carefully before integrating.

The per-provider context normalization is exactly right: OpenAI/Gemini/Codex report cached tokens as a subset of the prompt (so they must not be added), while Anthropic reports cache read/creation separately (so they must be summed) — verified across all four adapters. The ContextSize() helper with its conservative InputTokens fallback is the clean way to express that. The estimateRequestTokens change to count tool schemas is a genuinely good catch — large tool packs really can delay compaction until the provider rejects the request. The sub-agent workspace refactor also tightens write confinement on the isolation-failure path, and the fish-shell fix is spot on.

Integration note: this landed alongside a parallel in-progress fix for the same runaway-gauge symptom, so I merged by hand rather than a straight fast-forward. Your ContextSize() value won on the usage emit (it's the more provider-aware one), the tools parameter was kept on maybeCompact, and it was reconciled with a per-model context-window lookup (so e.g. glm-5.2 reports its real 1M window). Everything builds and the full llm/agent/tools/worktree/server suites pass. 🙏

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.

2 participants