Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

feat(cli): context usage progress bar with token state persistence - #523

Merged
Chronostasys merged 65 commits into
masterfrom
feat/context-usage-bar
Apr 26, 2026
Merged

Chronostasys merged 65 commits into
masterfrom
feat/context-usage-bar

Conversation

@Chronostasys

@Chronostasys Chronostasys commented Apr 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Replace the text-based 📊 12.5K/128K (10%) context indicator with a segmented progress bar showing prompt fill ratio, output token reservation, and compression threshold marker.

Visual

Wide terminal:   42% ████████████░░░░░░░░▒▒ 8.4K/120K out:8K
Narrow terminal:  42% ████░░░░░░░░
  • █ filled = prompt tokens used (green→yellow→red by ratio)
  • ▒ dim = output token reservation
  • ░ empty = unused context space
  • ┊ red marker = compression trigger threshold

Key changes

Data pipeline

  • TokenUsageSnapshot / CLITokenUsage gain MaxOutputTokens field
  • Agent passes MaxOutputTokens through all progress event paths (CLI, remote, WS, interactive)

Token state persistence & restoration

  • Agent already persists via SaveTokenState → DB
  • New TokenStateLoader reads from DB on CLI startup → bar shows immediately on launch
  • New get_token_state RPC for remote CLI mode
  • New GetTokenState method on AgentBackend interface
  • Both LocalBackend and RemoteBackend implement it

Real-time per-iteration updates

  • callLLM sends notifyProgress after updateTokenUsage so each iteration pushes fresh token data
  • handleProgressMsg caches token data on every progress event (not just turn end)
  • cacheTokenUsage() helper deduplicates 4 caching sites

Performance (anti-freeze)

  • Cached rendering: ctxBarCache + ctxBarCacheKey — only recompute when data changes
  • Pre-created ctxBarStyles package-level vars — zero per-frame allocation
  • Batched bar rendering: 3 Render() calls instead of barWidth individual ones
  • cachedCompressRatio and cachedMaxOutputTokens cached during progress events, no per-frame settings lookup

Server-side fixes (remote mode)

  • get_settings RPC injects config defaults for max_context_tokens, compression_threshold, etc.
  • CLI remote values cache fallback for max_context_tokens changed from "0" to "200000"

Other changes in this PR (by xiaohouzhen):

  • compression_threshold setting exposed in UI (i18n ZH/EN/JA)
  • compression_threshold default changed from 0.7 → 0.9
  • sandbox_mode default changed from docker → none
  • SetCompressionThreshold added to backend interface + RPC

Files changed

Area Files
Agent data progress.go, engine_run.go, engine_wire.go, interactive.go
Agent backend backend.go, backend_local.go, backend_remote.go
CLI display cli_view.go (bar rendering + caching), cli_model.go, cli_types.go
CLI caching cli_helpers.go (cacheTokenUsage helper + resolveCompressRatio), cli_update_handlers.go, cli_message.go
CLI startup cli.go, main.go
Server RPC rpc_table.go (get_settings defaults + get_token_state + set_compression_threshold)
Settings setting_handlers.go, i18n.go (compression_threshold ZH/EN/JA)
Config config.go (defaults)
Web compat web.go
Tests main_test.go, server_test.go, cli_test.go

Chronostasys and others added 13 commits April 25, 2026 21:11
Replace the text-based '📊 12.5K/128K (10%)' context indicator in the
TUI status bar with a segmented progress bar showing:

- Filled portion (prompt tokens used) with color gradient (green/yellow/red)
- Output token reservation (dim segment on right)
- Compression threshold marker (75% of promptBudget)
- Numeric label (prompt/budget + output reservation)

Key changes:

Data pipeline:
- TokenUsageSnapshot/CLITokenUsage gain MaxOutputTokens field
- Agent passes MaxOutputTokens through all progress event paths
  (CLI, remote CLI, WS, interactive SubAgent, ToCLIProgressPayload)

Token state persistence & restoration:
- Agent already persists token counts via SaveTokenState → DB
- New TokenStateLoader callback reads from DB on CLI startup
- New get_token_state RPC for remote CLI mode
- New GetTokenState method on AgentBackend interface
- Both LocalBackend and RemoteBackend implement it
- CLI restores lastTokenUsage + cachedMaxContextTokens on startup

Real-time updates:
- handleProgressMsg now caches lastTokenUsage + cachedMaxOutputTokens
  (previously only cached on turn end and reconnect)
- callLLM sends notifyProgress after updateTokenUsage so each
  iteration pushes fresh token data to CLI

Server-side fixes (remote mode):
- get_settings RPC now injects config defaults for max_context_tokens,
  max_iterations, max_concurrency, context_mode when not in user_settings
- CLI remote values cache fallback for max_context_tokens changed from
  '0' to '200000' (matches config default)
- 将硬编码的 0.75 压缩阈值改为通过 CompressionThreshold 配置
- 默认值从 0.7 提高到 0.9(更宽松的压缩策略)
- 更新 CLAUDE.md 补充缺失的开发命令和架构细节

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- AgentBackend 接口增加 SetCompressionThreshold 方法
- LocalBackend 和 RemoteBackend 均实现该方法
- server RPC 增加 set_compression_threshold 处理器
- serverapp/setting_handlers 增加 compression_threshold 运行时处理
- cmd/xbot-cli/setting_handlers 增加 compression_threshold 配置处理
- channel/i18n.go SettingsSchema 增加 sandbox_mode、compression_threshold(ZH/EN/JA)
- remote values cache 增加 compression_threshold 默认值 0.9
- local GetCurrentValues 增加 compression_threshold 读取

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
当 config.json 未填写 sandbox.mode 时,
代码 fallback 硬编码为 docker,与 UI 默认值 none 不一致。
改为 none,避免用户未配置时意外启用容器隔离。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 工具运行指示器从 pulse 动画改为静态 ●
- thinking phase 去掉 spinner + 变化动词
- compressing/retrying 保留文字但去掉 orbit spinner
- typing 等待态去掉 spinner + 动词
- SubAgent 树图标从 wave 动画改为静态 ●

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Commit c18db2e removed thinking spinner and verbs from renderProgressBlock
and renderProgressStatus, but the tests still expected 'Thinking' text
and Progress headers.

- TestCLIModelRenderProgressStatus/thinking: expect '#0' not 'Thinking'
- TestCLIModelRenderProgressStatusNil: check non-empty (spinner still shown)
- TestCLIModelRenderProgressBlockThinking: expect empty (spinner removed)
Revert commit c18db2e's spinner removal from renderProgressBlock:
- Restore thinking phase spinner + pickVerb animation
- Restore compressing/retrying orbit spinner
- Restore typing wait state spinner + verb
- Restore tool pulse animation (pulseFrames)
- Restore SubAgent tree wave animation (waveFrames)

Fix lint issues in our own code:
- cli_view.go: use tagged switch on rune (QF1002)
- cli_view.go: remove empty branch (SA9003)
- cli_message.go: remove empty else-if branch (SA9003)

Fix test expectations:
- renderProgressStatus thinking test: expect '#0' (status bar never had
  'Thinking' text — that's in renderProgressBlock)
- renderProgressStatusNil: check non-empty instead of 'Thinking'
The token cache (lastTokenUsage) was only being updated in:
1. endAgentTurn (turn completion)
2. handleReconnectProgress (reconnect)
3. The isStreamOnly branch (stream content merges)

The main progress path (handleProgressMsg line 379+) never cached
lastTokenUsage, so the context bar only showed stale values until
the turn completed.

Move token caching to the main progress path (after m.progress assignment),
so every progress event from the agent updates the context bar in real-time.
Each iteration's callLLM → updateTokenUsage → notifyProgress now flows
through to the CLI's renderContextUsage() immediately.
Two bugs fixed:

1. TUI freeze/stutter (critical):
   - renderContextUsage() was called on every View() tick (~100ms)
   - Each call: GetCurrentValues() (possible RPC in remote mode) +
     5 lipgloss.NewStyle() allocations + barWidth individual
     Style.Render() calls (20-30 per frame)
   - Fix: cache rendered string, only recompute when token data or
     terminal width changes (cacheKey comparison). Pre-create styles
     as package-level vars. Batch bar segments into 3 Render() calls
     instead of barWidth. Cache compressRatio like maxOutputTokens.

2. Missing iteration spinner verb:
   - renderProgressStatus had thinking phase verb removed, showing only
     '#3' with no action text. nil progress also had no verb.
   - Fix: restore thinking phase verb (pickVerb) in status bar.
     Restore nil-progress fallback verb.

Also:
- Add cachedCompressRatio field to cliModel (cached during progress
  events, not read from settings every frame)
- Add resolveCompressRatio() helper in cli_helpers.go
- Remove unused strconv import from cli_view.go
- Update tests to match restored behavior
…ve plan file

- Extract cacheTokenUsage() helper to replace 4 duplicate token caching
  blocks across cli_helpers.go, cli_message.go, cli_update_handlers.go
- Fix context bar cache key missing compressRatio — changing compression
  threshold via settings panel now correctly invalidates the bar
- Remove duplicate sandbox_mode definitions in i18n.go (ZH/EN/JA) that
  were added under both Environment and Agent categories
- Remove docs/plans/tui-context-status-bar.md (development artifact)
- Fix nil pointer in cacheTokenUsage call when m.progress is nil
…ogress bar

Bug 1 — Estimated token usage on first iteration:
  initProgress() created StructuredProgress with nil TokenUsage.
  The first notifyThinking() and maybeCompress() ran before callLLM,
  so the CLI received nil (or estimated) token data until the first
  LLM response arrived. Fix: seed TokenUsage from lastPromptTokens
  (restored from DB) in initProgress, so the first progress event
  carries real data.

Bug 2 — No queue indicator during typing:
  Queue indicator (QueuePending) only rendered in ready-status bar
  (not typing/progress mode). The only queue feedback during typing
  was showTempStatus which expires after 3 seconds. Fix: add
  persistent queue indicator to renderProgressStatus, visible
  throughout the agent turn.
Two bugs fixed:

1. Tier model settings (vanguard/balance/swift) were classified as
   'global-scoped' but are actually per-user preferences. They were
   never persisted to user_settings DB, so:
   - Settings panel showed empty after save+reload (remote mode)
   - SubAgent model resolution couldn't read tier config (remote mode)

   Fix: moved tier model keys from cliGlobalScopedSettingKeys to
   cliUserScopedSettingKeys. Added tier sync in refreshRemoteValuesCache.

2. GetLLMForModel used provider-guess matching for tier-resolved models,
   which is unsafe: 'vanguard' → 'gpt-4.1-pro' matched the GLM
   subscription's provider='openai', sending gpt-4.1-pro to the GLM
   endpoint → 400 'model not found'.

   Fix: tier-resolved models now skip provider-guess entirely. They
   only use exact subscription matches. If no match found, fall back
   to parent agent's LLM (correct endpoint + model).
@Chronostasys

Copy link
Copy Markdown
Collaborator Author

修了一部分,但是状态栏几乎除了context以外的功能全没了,包括subagent ,background task等状态提示。需要重新检查并修复相关逻辑

Identified 5 architectural defects and 12 specific issues in config system.
The report proposes a 5-phase fix plan.
Replace 4 scattered scope maps + runtime key list with a single
AllSettingDefs registry. Scope classification, runtime key lists,
and known-key checks are all auto-derived from AllSettingDefs.

Changes:
- New file: channel/setting_keys.go — single source of truth
  for all 28 setting keys (scope + runtime flag)
- channel/cli_helpers.go: remove 4 redundant scope maps,
  remove duplicate scope functions, keep thin wrappers
  that delegate to the unified registry

All existing tests pass. No behavioral change.
saveCLIConfig and saveServerConfig now separate LLM tier models
(vanguard/balance/swift) from credentials (provider/base_url/api_key/model).

When config.json has subscriptions, only tier models are written back.
Credentials belong to the subscription system (DB = single source of truth).

When config.json has NO subscriptions (first-run / legacy), credentials
are still written to config.json as before.

This eliminates the data cycle where:
  loadLLMFromDBSubscription → cfg.LLM → saveCLIConfig → config.json
which caused DB credentials to leak into config.json and create
conflicting copies on every restart.

Updated TestSaveCLIConfigPreservesDiskFields to reflect new behavior.
Replaced the 7-level fallback chain (cache → config exact → config
provider guess → DB cache → DB API load → DB provider guess → default)
with a clean 3-step resolution:

1. Exact match in subscription cached model lists (via buildModelSubscriptionMap)
2. Config.json subscription Model field match (CLI mode only)
3. DB subscription first-run API load (only for uncached subscriptions)
4. Fall back to parent agent's LLM (with its own model, not targetModel)

Removed:
- guessProvider() usage from GetLLMForModel (function kept for tests only)
  Substring-based provider guessing was error-prone (e.g. o1 matching
  non-OpenAI models) and produced silent wrong-endpoint calls.
- Provider-guess matching in all code paths
- Redundant step-by-step logging replaced with clean source-tagged logs

Net: -76 lines of complex fallback logic.
…ig.json

Removed inconsistent zero-value checks from 6 fields:
- AGENT_MAX_ITERATIONS, AGENT_MAX_CONCURRENCY, AGENT_MAX_CONTEXT_TOKENS
- MAX_SUBAGENT_DEPTH, SANDBOX_IDLE_TIMEOUT_MINUTES, SANDBOX_WS_PORT

Previously, these fields only accepted env var values when config.json
had a zero value. Other fields (LLM_PROVIDER, LLM_MODEL, etc.) always
overrode. This inconsistency made behavior unpredictable.

New strategy (documented in code comment):
  Environment variables ALWAYS override config.json values.
  Default value filling in Load() only applies when both config.json
  and env var are unset.

This ensures predictable behavior: setting an env var means override,
regardless of what config.json contains.
Phase 2 Task 1: Replace 5 scattered token tracking fields in runState
(lastPromptTokens, lastCompletionTokens, lastMsgCountAtLLMCall,
hadLLMCall, restoredFromDB) with a single TokenTracker type.

New type: agent/token_tracker.go
- NewTokenTracker() — seeded from previous Run's state
- RecordLLMCall() — records API-returned token counts
- ResetAfterCompress() — resets after context compression
- DetectTruncation() — re-estimates if messages were truncated
- EstimateTotal() — 4-path token estimation (replaces inline if/else)
- SaveState() — conditional persistence via callback

23 unit tests in token_tracker_test.go, all existing tests pass.
Phase 2 Task 3: Replace lastPersistedCount field and 10 inline
persistence code blocks with a PersistenceBridge type.

New type: agent/persist_bridge.go
- NewPersistenceBridge() — wraps *session.TenantSession
- IncrementalPersist() — persist new messages, strip system reminders
- RewriteAfterCompress() — clear + rewrite session after compression
- MarkAllPersisted() — update watermark after synthetic message injection
- ComputeEngineMessages() — extract Run-producing messages for output
- IsPersisted() — check if message at index is already persisted

14 unit tests in persist_bridge_test.go, all existing tests pass.
Replaces ~110 lines of scattered persistence code with 6 bridge calls.
…blocks

Phase 2 Task 2: Replace 3 inline compress→persist→cleanup blocks
with a unified ApplyCompress pipeline function.

New type: agent/compress_pipeline.go
- CompressPipelineParams — all inputs (CM, messages, tracker, persistence, stores)
- CompressPipelineResult — outputs (new messages, token count, compress output)
- ApplyCompress() — 6-step pipeline: compress → accumulate → sync → reset → persist → clean

3 call sites updated:
- handleInputTooLong: ManualCompress path with store cleanup
- handleFinalResponse context_window_exceeded: Compress path, no store cleanup
- runCompression: Compress path with hooks, metrics, progress (extras preserved)

5 unit tests in compress_pipeline_test.go, all existing tests pass.
Net reduction: ~80 lines of duplicate code eliminated.
Add runState.ValidateInvariants() and validateInvariantsAt() helper
that check three invariants at key transition points:
1. persistence watermark <= len(messages)
2. promptTokens > 0 iff hadLLMCall || restoredFromDB
3. msgCountAtCall <= len(messages)

Called at 5 transition points (log.Debug level):
- post_llm_call, post_llm_call_input_too_long
- post_compress, post_compress_window_exceeded
- post_persist
…ler coverage checks

Add channel.MissingRegistryKeys[T any] that works with any
map[string]T handler registry. Simplifies both serverapp and
CLI missing-key-check functions from hand-rolled loops to
single-line delegations.

RPC generics already optimal. Offload/Mask CleanOldEntries
differ in lock strategy and cleanup side-effects — not
suitable for generic unification.
- architecture.md: Add 'Run State Components' section describing
  TokenTracker, CompressPipeline, PersistenceBridge, and
  ValidateInvariants
- agent.md: Update engine_run.go description, add 4 new component
  file entries
- AGENT.md: Replace lastPersistedCount gotcha with PersistenceBridge
  + ApplyCompress + ValidateInvariants description
- Extract executeToolCalls into: initToolProgress, execOneTool,
  updateToolResultProgress, updateToolResultLine, dispatchToolCalls
  (with 3 strategy methods), executeSubAgentOps, snapshotCompletedIteration
- Extract maybeMaskObservations from maybeCompress
- Extract setWaitingUser from processToolResults
- Result: largest function reduced from 291 to ~5 lines (body)
- All functions now under 103 lines
- Extract renderTitleBar, renderInputArea, renderReadyStatus
- Extract layoutSearch, layoutAskUser, layoutPanel, layoutMain
- Add appendStatusHint helper to replace 5 repeated if/else blocks
- Clear layout dispatch: searchMode → askUser → panel → main
- Delete hand-written min/max in agent.go, integration_test.go,
  ultraviolet layout example (Go 1.21 built-in min/max)
- Replace all interface{} with any across codebase (66 locations)
- sync.Once → sync.OnceValue in llm/tokenizer.go (getSortedPrefixes)
- maps.Keys + slices.Sorted replace manual map-to-slice loops
  (middleware.go, card_builder.go)
- slices.DeleteFunc replaces manual filter loops
  (registry.go, cli_helpers.go, cli_message.go, middleware.go)
- Extract toStringValue helper in feishu.go (7 duplicate type
  switch patterns → 1 helper function)
- feishu_settings.go: nested found loop → slices.ContainsFunc
- cli_panel.go: option cycling found loop → slices.IndexFunc
Move the 146-line tool progress formatter from agent.go to its own
file for better organization. No logic changes.
Extract 4 self-contained blocks from processMessage() (~428 → 214 lines)
into agent/agent_process.go:

- injectSystemNotes: builds system notes from bg tasks, interactive
  agents, and groups; appends to last user message
- wireBgNotificationDrain: creates the DrainBgNotifications callback
  that filters by session key
- drainRemainingBgNotifications: drains post-Run bg notifications
- handleCancelledRun: persists un-saved engine messages on cancel
- handleRunOutput: handles all post-Run success paths (waitingUser,
  empty content, assistant message save/send/react)

No behavior changes. All existing tests pass.
…shotIterationChange, handleProgressDone

Phase 6.2: pure extraction refactoring with no behavior change.

- syncProgressTodos(payload): syncs todo items from progress payload (~28 lines)
- snapshotIterationChange(payload, prev): detects iteration changes and snapshots previous iteration (~25 lines)
- handleProgressDone(msg, prev, turnID): handles Phase 'done' — snapshot final iteration, tool summary, reset state, agent synthesis (~96 lines)
- handleProgressMsg reduced from ~256 lines to ~116 lines

All builds, vets, and tests pass clean.
…t.go

- Add sessionKey() helper — replaces 10 inline 'channel + ":" + chatID' constructions
- Add resetSessionState() method — replaces 3 repeated sessionMsgIDs/sessionFinalSent Delete pairs
- Add resolveMemoryProvider() helper — replaces 4 repeated if-empty-default-to-flat patterns
- Add injectCLIUserMessage() method — replaces 2 channelFinder→CLIChannel→InjectUserMessage blocks
- Remove dead duplicate metadata nil check in sendMessage (line was already guaranteed non-nil)
- Net change: +52/-43 lines, improved consistency and reduced error-prone repetition
…ic helper

Convert 25 instances of manual JSON argument parsing across 10 tool files
to use the existing parseToolArgs[T] generic helper. Benefits:
- Consistent error messages (all now use 'parse args: ...')
- Removed unused encoding/json imports from 7 files
- Net -7 lines, improved consistency

Skipped: search_tools.go (custom ToolResult error), session_mcp.go (map[string]any),
and card_tools.go CreateCard (input guard pattern).
…rgs callers

parseToolArgs already returns 'parse args: <error>'. Callers that wrapped
it again as 'invalid parameters: parse args: <error>' were producing
redundant error chains. Simplified to return err directly.

Updated glob_test.go and grep_test.go to match new error message prefix.

19 files affected, net -19 lines.
tipFileCreated and tipEditCompleted each appeared twice as inline literals.
Extracted as package-level constants for DRY and easier future changes.
Replace 6 inline 'channel + ":" + chatID' constructions with a local
sessKey() helper function for consistency and typo prevention.
Replace 5 inline 'channel + ":" + chatID' constructions in engine_wire.go
with calls to the sessionKey() helper (same package). Left 3 variable
assignments as-is to avoid function name shadowing.
…lidation

The run_as+reason co-occurrence check was duplicated 3 times across
edit.go and shell.go. Extracted as validateRunAsReason in permission.go.
- runstate_invariant: TestValidateInvariants with 8 sub-cases covering
  all 3 invariants (persistence watermark, token source, msg count)
- persist_bridge: RewriteAfterCompress nil-session edge cases,
  assertNoSystemPersist filter validation
- compress_pipeline: SyncMessages=nil path, AccumulateUsage=nil path
- llm_factory: GetLLMForModel config sub exact match, no-match,
  empty credentials skip
…path

- TestSaveCLIConfig_WritesLLMCredentialsWhenNoSubscriptions: verifies
  legacy/first-run path writes LLM credentials when no DB subscriptions
- TestSaveCLIConfig_TierModelsAlwaysPersisted: verifies tier model
  mappings (vanguard/balance/swift) are always written back even when
  subscriptions exist
- TestSaveCLIConfig_ParsesExistingFile: verifies merge behavior when
  overwriting a full config with partial updates
- TestBuildModelSubscriptionMap_ConfigSubs: verifies multiple config subs mapped
- TestBuildModelSubscriptionMap_ConfigSubsSkipsEmptyCredentials: verifies empty
  BaseURL/APIKey subs are skipped
- TestBuildModelSubscriptionMap_EmptySenderID: verifies only config subs used
  when no DB svc
- TestConfigSubToLLMSubscription: verifies field mapping completeness
6 positive cases (all subscription-scoped keys) + 6 negative cases
(theme, sandbox_mode, vanguard_model, max_iterations, empty, random).
Table-driven tests for formatErrorForUser, sessionKey, resolveMemoryProvider,
resolveGlobalSkillsDirs, copyMessages, assertNoSystemPersist — 19 subtests total.
chatKey: 5 cases (normal, empty parts, embedded colons)
parseOrDefault: 6 cases (empty, valid, zero, negative, non-numeric, whitespace)
4 cases: valid JSON, invalid JSON, empty object, empty string
6 cases: both empty, both set, asymmetric (both directions),
whitespace-only, mixed whitespace and set
isKnownDotFile: 13 subtests (9 known + 4 unknown)
splitJSONLLines: 5 subtests (empty, single, two lines, trailing newline, multiple)
New file covering 10 test functions:
- LanguageMiddleware: 9 cases (nil svc, various languages, empty, error)
- ProjectContextMiddleware: 9 cases (CWD/WorkDir, AGENT.md, .xbot/context.md, .cursorrules)
- PermissionControlMiddleware: 7 cases (config variations)
- MemoryMiddleware extended: 5 cases (nil provider, error, nil context)
- SenderInfoMiddleware extended: 2 cases (format verification)
- IsPermControlEnabled: 5 cases
- LanguageInstruction: 5 cases
- FormatProjectContext: 2 cases
- LoadProjectContextFile: 4 cases
- BuildSystemPrompt integration: 1 case
getEncodingForModel: 21 cases (Claude/GPT series, prefix matching, case-insensitive, unknown)
serializeToolsToJSON: 5 cases (empty, nil, single, multiple, required params)
estimateToolsTokens: 4 cases (empty, nil, single, monotonicity)
…ackage

Replace 11 instances of fmt.Errorf with identical strings with package-level
sentinel errors (ErrSettingsUnavailable, ErrBgTasksUnavailable,
ErrSubscriptionsUnavailable, ErrNoSessionManager). This enables errors.Is()
matching and eliminates string duplication.
…eaming error path

The streaming error path now properly reads the full response body before
closing, ensuring HTTP connection reuse works correctly.
- Use defer for file Close() to ensure cleanup on all paths
- Check write errors from fmt.Fprintf instead of silently ignoring
- Handle os.MkdirAll errors instead of silently ignoring
slices.Sort uses Go's pdqsort algorithm which is faster than the old
quicksort in sort.Strings. Replaces 12 call sites across 11 files.
…ableFunc

Convert 16 sort.Slice/SliceStable calls to use slices.SortFunc/SortStableFunc
with cmp.Compare. Benefits:
- Uses pdqsort algorithm (faster than old quicksort)
- Type-safe comparison functions (no index-based access)
- Cleaner function signatures with named parameters

Files changed:
- agent/observation_masking.go (2 calls)
- agent/quality.go (1 call)
- agent/middleware.go (1 call)
- llm/tokenizer.go (1 call)
- tools/todo.go, interface.go, logs.go, cron.go, cd.go, event_trigger.go

@Chronostasys Chronostasys left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: Architecture Overhaul + Bug Fixes

Overall Assessment: ✅ Approve with Comments

This is a high-quality refactoring PR that addresses genuine architectural problems. The core design decisions are sound, but I have some concerns worth discussing.


👍 What's Done Well

1. TokenTracker extraction — Clean, well-documented type. The EstimateTotal() method elegantly replaces a 60-line if/else chain with a single call. Good separation of concerns.

2. CompressPipeline — The 3→1 deduplication is textbook correct. The params/result struct pattern is clean and extensible.

3. GetLLMForModel cleanup — Removing the 7-level fallback chain and guessProvider() is the right call. Provider-guess-by-substring was a landmine.

4. setting_keys.go unified registry — Single source of truth for setting definitions. Adding a new key = one entry. Excellent.


⚠️ Concerns

1. PR Scope (Risk)

51 commits, 107 files, +7,136/-3,279 — This is massive for a single PR on a feature branch. Mixing:

  • Bug fixes (tier model persistence, GetLLMForModel)
  • Architecture refactoring (3 new types, function decomposition)
  • Cosmetic modernization (interface{} → any, sort → slices)
  • Test additions (2,379 new test lines)

Risk: Hard to bisect if something breaks. Consider splitting into 2-3 PRs next time.

2. CompressPipelineParams has 12 fields

At the upper limit. Consider grouping into sub-structs (CompressInput, CompressStores) or just passing *runState.

3. GetLLMForModel still has a 15-second API call in the lookup path

During a SubAgent spawn, a 15s blocking call is risky. Consider pre-loading all subscriptions on startup.

4. PersistenceBridge.IncrementalPersist silently returns nil on error

func (b *PersistenceBridge) IncrementalPersist(messages []llm.ChatMessage) error {
    // logs error but always returns nil
}

The error return type is misleading. Either return the error or change the signature.

5. runstate_invariant.go — debug-only checks invisible in production

Using log.Debug means invariant violations are invisible in production (default info level). Consider log.Warn for critical invariants like LastPersistedCount > len(messages).

6. View() decomposition — extracted functions cannot be independently tested

The extracted render methods are unexported methods on cliModel. Consider pure functions with explicit parameters.


🐛 Minor Issues

  1. llm_factory.go: The fromTier branch duplicates exact-match logic. Could simplify by unifying the lookup path.
  2. token_tracker.go: DetectTruncation mutates state AND returns info. Split into IsTruncated() + ApplyTruncation().
  3. config-audit.md: Phase 5 (enable_auto_compress alias removal) is deferred. Worth a tracking issue.

Summary

Architecture changes are well-motivated and implementation quality is high. Bug fixes are targeted and correct. Main risks: PR scope and a few edge cases in new abstractions.

…em to token estimates

Root cause of token count jump (91.5K→127.5K) and premature mask triggering:

1. EstimateTotal() was adding completionTokens (API output tokens) to
   the context size estimate. These are NOT part of the prompt context.
   This inflated maybeCompress estimates by ~36K, causing premature
   observation masking on the next message.

2. initProgress()/updateTokenUsage() set TotalTokens = PromptTokens +
   CompletionTokens. The context bar fallback (formatTokenCount)
   displayed TotalTokens, showing 126.5K instead of 91.5K.

3. formatTokenCount() displayed TotalTokens including output tokens,
   making the jump visible in the status bar.

Fix: TotalTokens now equals PromptTokens only (context fill).
EstimateTotal() uses api_prompt source instead of api+completion.
formatTokenCount() simplified to show prompt tokens as "ctx: X".
Lint fixes:
- Remove unused isActionSettingKey wrapper in channel/cli_helpers.go
- Pass ctx through dispatchConcurrentSubAgents instead of using nil Context
- Discard unused return value of injectSystemNotes (modifies in-place)

Windows test fix:
- TestResolveGlobalSkillsDirs: use filepath.Abs for cross-platform path
  comparison instead of hardcoded Unix paths
@Chronostasys
Chronostasys merged commit 3cbbbab into master Apr 26, 2026
6 checks passed
@Chronostasys
Chronostasys deleted the feat/context-usage-bar branch April 26, 2026 05:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants