Skip to content

fix(agent,desktop): align context gauge with compaction trigger / 对齐上下文容量条与压缩触发器 - #8329

Merged
SivanCola merged 2 commits into
esengine:main-v2from
xiaotaoya2022-cell:fix/context-gauge-trigger-alignment
Aug 11, 2026
Merged

fix(agent,desktop): align context gauge with compaction trigger / 对齐上下文容量条与压缩触发器#8329
SivanCola merged 2 commits into
esengine:main-v2from
xiaotaoya2022-cell:fix/context-gauge-trigger-alignment

Conversation

@xiaotaoya2022-cell

@xiaotaoya2022-cell xiaotaoya2022-cell commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

What breaks today

The desktop context gauge measured something different from the compaction trigger in both directions:

  1. The backend gauge under-reported. ContextUsedTokens() used the message-only estimator, while the trigger sizes messages, role projection, and tool schemas.
  2. The frontend inflated the fill. It added completion tokens to prompt occupancy, so a large output could make the gauge cross the threshold even though the next request prompt had not.
  3. Live MCP changes could leave the gauge stale. The context-usage cache did not observe tool registration, replacement, removal, or suspension.

Fix

  • Use estimatedVisibleRequestTokens() for both the gauge and compaction trigger.
  • Track a lock-free provider-visible tool schema revision and include the registry identity plus revision in the context-usage cache key.
  • Count prompt tokens only for context occupancy, with latest-attempt and legacy aggregate fallbacks selected independently.
  • Align the shared ContextFillTokens() helper with prompt occupancy.
  • Add deterministic regression coverage for live tool changes and completion-only usage payloads.

Verification

  • go test ./...
  • go test -race ./internal/tool ./internal/agent
  • go vet ./...
  • go run ./tools/repolint
  • pnpm build in desktop/frontend
  • Relevant frontend controller suites: 280 assertions passed
  • Latest main-v2 virtual merge: relevant Go tests, frontend reducer test, frontend build, and repolint passed

Refs: #8191, #8221

Documentation-impact: none - this aligns internal context-capacity measurement with the existing compaction semantics and does not change configuration or user workflows.
Cache-impact: none - the revision is internal bookkeeping only; tool schema bytes, ordering, and provider request construction are unchanged.
Cache-guard: go test ./internal/tool ./internal/agent ./internal/provider covers registry revision changes, live add/remove/suspend invalidation, and prompt-only occupancy fallbacks.

The desktop context gauge measured something different from the
compaction trigger, in both directions:

- The backend gauge (ContextUsedTokens) used the message-only estimator
  while the trigger sizes messages + role projection + tool schemas
  (estimatedVisibleRequestTokens). With a non-empty tool registry the
  gauge under-reported the fill, so a session could display below
  compact_ratio while the trigger had already crossed it (the esengine#8221 test
  passed only because its tool registry was empty).
- The desktop frontend overwrote the backend gauge with the last turn's
  real usage and added completion tokens to the used figure. A long
  output round inflated the displayed fill (e.g. 90%) while the trigger,
  which only counts the next request's prompt, still sat below the
  configured 85% — compaction never fired and the session ran into a real
  overflow.

Both now use one measurement: the gauge calls the trigger's own estimator,
and the frontend counts prompt tokens only. Completion stays in the output
metrics, not the occupancy gauge.

Refs: esengine#8191
@github-actions github-actions Bot added v2 Go rewrite (1.x) — main-v2 branch, active development desktop Wails desktop app (desktop/**) agent Core agent loop (internal/agent, internal/control) labels Aug 11, 2026
Problem:
- Context usage stayed cached after live MCP tool changes.
- Completion-only latest usage could collapse prompt occupancy to zero.
- The PR also tripped formatting and repository budget gates.

Root cause:
- The memoization key omitted the provider-visible tool registry revision.
- Frontend fallback selected context prompt data based on unrelated completion data.

Fix:
- Track tool schema revisions and include registry identity plus revision in the gauge cache key.
- Select prompt and completion fallbacks independently and align ContextFillTokens with prompt occupancy.
- Add deterministic backend and frontend regression coverage while removing budget-breaking comments.

Verification:
- go test ./...
- go test -race ./internal/tool ./internal/agent
- go vet ./...
- pnpm build
- go run ./tools/repolint
@github-actions github-actions Bot added skills Skill system (internal/skill, internal/tool) provider Model providers & selection (internal/provider) labels Aug 11, 2026
@SivanCola SivanCola changed the title fix(agent,desktop): align the context gauge with the compaction trigger fix(agent,desktop): align context gauge with compaction trigger / 对齐上下文容量条与压缩触发器 Aug 11, 2026
@SivanCola
SivanCola enabled auto-merge (squash) August 11, 2026 07:17
@SivanCola
SivanCola merged commit ee2a6a7 into esengine:main-v2 Aug 11, 2026
25 of 28 checks passed
SivanCola added a commit to SivanCola/DeepSeek-Reasonix that referenced this pull request Aug 11, 2026
Problem:
- main-v2 advanced again with esengine#8329 after the catalog integration merge had been fully tested.

Root cause:
- The context gauge and compaction trigger previously measured different provider-visible inputs, and the frontend counted completion tokens as prompt occupancy.

Fix:
- Integrate the latest prompt/tool-schema accounting and frontend prompt-occupancy semantics without conflicts.

Verification:
- go test -count=1 ./internal/agent ./internal/provider ./internal/tool
- go test -race -count=1 ./internal/agent ./internal/provider ./internal/tool
- pnpm exec tsx src/__tests__/use-controller-stream-progress.test.ts
- pnpm build
- go run ./tools/repolint
- gofmt -l internal desktop
- git diff --cached --check
complyue pushed a commit to complyue/DeepSeek-Reasonix that referenced this pull request Aug 11, 2026
…efresh

Keeps the review-endorsed parts of the original four-threshold PR esengine#8256,
rescaled to the single compact_ratio architecture of esengine#8244:
- over-threshold resume guard, asked BEFORE the mutating
  ResumeSessionPageForTab / OpenChannelSessionPageForTab calls via a
  read-only PreviewSession estimate, so cancelling leaves the session
  untouched
- immediate context + balance refresh on compaction_done (the coalesced
  BalanceForTab query is invalidated first so the refresh reads fresh data)

Drops everything tied to the multi-threshold model removed by esengine#8244
(soft/snip/force ratios, the four-thumb slider, thresholdLayout,
SetCompactionThresholds, CLI snip-ratio, the widened compact-ratio range)
and does not reintroduce the ContextPanel maintenance block removed by
esengine#8265. The live capacity visualization already aligns with the single
Compact threshold on current main-v2 (esengine#8329/esengine#8307), so the gauge is
unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent Core agent loop (internal/agent, internal/control) desktop Wails desktop app (desktop/**) provider Model providers & selection (internal/provider) skills Skill system (internal/skill, internal/tool) v2 Go rewrite (1.x) — main-v2 branch, active development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants