From 1b792454c568fe319370f5e0398f04e2c8ebd9d6 Mon Sep 17 00:00:00 2001 From: wsp Date: Tue, 17 Nov 2026 10:46:11 +0800 Subject: [PATCH] fix(context): Estimate composition on demand Compute current context composition when the panel opens so compressed and forked sessions no longer depend on the latest request snapshot. Reuse each estimate for numeric breakdowns and content previews. Stop writing usage-context.json and per-request contextComposition. Keep legacy reads, usage accounting and runtime recovery data intact. Expose the additive query through Desktop and CLI Peer adapters, and show unavailable states when current context cannot be reconstructed. Cover compression, forks, read-only inspection, legacy payloads and UI refresh behavior with focused regression tests. --- .../session-runtime-usage-report-design.md | 1763 ----------------- src/apps/cli/src/dispatch/mod.rs | 1 + src/apps/cli/src/management.rs | 1 + src/apps/cli/src/modes/chat/selection.rs | 1 + .../cli/src/peer_host/commands/session.rs | 24 + src/apps/desktop/AGENTS.md | 4 + src/apps/desktop/src/api/session_api.rs | 25 + .../src/runtime/session_application.rs | 2 + .../src/agentic/coordination/coordinator.rs | 42 + .../core/src/agentic/execution/AGENTS.md | 10 + .../src/agentic/execution/execution_engine.rs | 94 +- .../execution_engine/context_estimate.rs | 383 ++++ .../src/agentic/execution/round_executor.rs | 11 +- .../core/src/agentic/execution/types.rs | 1 - .../core/src/agentic/insights/collector.rs | 1 + .../core/src/agentic/persistence/manager.rs | 1 + .../persistence/manager/usage_context.rs | 4 +- .../core/src/agentic/session/context_store.rs | 1 - .../src/agentic/session/session_manager.rs | 142 ++ .../session_manager/context_estimate.rs | 116 ++ .../assembly/core/src/product_runtime.rs | 123 +- .../core/src/service/session_usage/service.rs | 1 + .../contracts/core-types/src/session_usage.rs | 32 +- .../session_usage_contracts.rs | 26 + .../contracts/runtime-ports/src/agent_api.rs | 14 + .../session_operation_ports.rs | 1 + .../app-server/tests/agent_kernel.rs | 1 + .../src/flow_chat/components/ChatInput.tsx | 6 +- .../ChatInputWorkspaceStrip.test.tsx | 18 +- .../components/ChatInputWorkspaceStrip.tsx | 4 +- .../components/usage/ContextUsagePopover.tsx | 13 +- .../usage/SessionUsageComponents.test.tsx | 8 +- .../components/usage/SessionUsagePanel.tsx | 7 +- .../components/usage/SessionUsageSummary.tsx | 2 +- .../components/usage/useContextPreview.ts | 6 +- .../openSessionContextPreview.test.ts | 40 +- .../services/openSessionContextPreview.ts | 45 +- .../services/usageReportService.test.ts | 1 + .../flow_chat/services/usageReportService.ts | 3 +- .../api/service-api/SessionAPI.test.ts | 10 + .../api/service-api/SessionAPI.ts | 11 + src/web-ui/src/locales/en-US/flow-chat.json | 14 +- src/web-ui/src/locales/zh-CN/flow-chat.json | 14 +- src/web-ui/src/locales/zh-TW/flow-chat.json | 14 +- 44 files changed, 1082 insertions(+), 1959 deletions(-) delete mode 100644 docs/features/session-runtime-usage-report-design.md create mode 100644 src/crates/assembly/core/src/agentic/execution/execution_engine/context_estimate.rs create mode 100644 src/crates/assembly/core/src/agentic/session/session_manager/context_estimate.rs diff --git a/docs/features/session-runtime-usage-report-design.md b/docs/features/session-runtime-usage-report-design.md deleted file mode 100644 index 991c7d3e8b..0000000000 --- a/docs/features/session-runtime-usage-report-design.md +++ /dev/null @@ -1,1763 +0,0 @@ -# Session Runtime Usage Report Design - -> Status: P0 and P1 implemented; P2 Desktop analysis surface hardening mostly implemented as of 2026-05-11 -> Scope: `/usage`, Desktop Flow Chat usage reports, CLI usage reports, session runtime metrics, Chat-bottom usage entry -> Non-goal: this document does not prescribe exact code edits or final UI copy. - -## Background - -### Current side-panel scope - -The side panel displays session/project identity, context -composition, and cumulative token shares. The former Usage details chapter and -its Overview, Models, Tools, Files, Errors, and Slowest tabs are removed. Chat -cards retain their report entry and compact summaries, but no longer offer jumps -to those removed tabs. Report data and Markdown export remain intact. Saved -panel tab hints are accepted and show the summary. Appearance parts now describe -the remaining UI; overrides for removed detail parts follow the existing -structured incompatibility diagnostics without rewriting saved packages. Detailed -tab and navigation descriptions in the earlier implementation stages below are -historical, not the current side-panel UI. - -The session title is centered independently of the copy action. The generation -timestamp is omitted from the panel header; report and export timestamps remain intact. - -The system-prompt, tool-definition, rules, skills, MCP/dynamic-tools and subagent -legend rows expose an external-link button on hover or keyboard focus. Each opens -its own read-only preview tab. Tool definitions use a searchable, expandable -`tool-definitions` document reader, showing tool descriptions and parameter types, -required flags, defaults, enums, nested structures and schema constraints. It -projects the existing captured definition array without altering persisted -sessions; the Markdown envelope and calling instructions remain compatible with -older captures. Other categories use the standard `markdown-editor`. The executing host replaces a single -session-local `usage-context.json` snapshot when preparing a request, using the -same typed sources as the category estimates. It includes prompt/reminder text, -native tool schemas and the model-visible Skill/GetToolSpec results; it does not -reload today's catalog or workspace files to reconstruct earlier content. - -An explicit `contextPreviewCategory` request returns only that category in -`contextPreview`, including its capture time. Empty captured categories open an -empty-content message; absent snapshots/categories return unavailable. A snapshot -whose owning turn has been replaced or reverted is not exposed. This is the -latest prepared request, not a historical request selected from the chart. -The older `includeSystemPromptPreview` / `systemPromptPreview` cached-system-message -path remains readable as a fallback for older hosts and sessions. Ordinary usage -reports, persisted `/usage` results, and Markdown exports remain content-free. -Dispatch projections refuse preview instead of reading the controller's cache. -Tab identity and late responses remain scoped to the originating device, session, -and category; no filesystem editor path is created, and the standard Markdown -editor stays read-only in rich and source modes. Retained tool-definition Markdown -tabs also render through the structured reader and retain their tab identity. - -### Context composition and token shares - -The side panel separates the latest recorded parent request's context composition -from cumulative session token usage. The context donut has eight categories: -system prompt (including runtime context), tool definitions, rules (workspace/user -instructions), skills, MCP/dynamic tools, subagent definitions, summarized -conversation, and conversation. Typed runtime message/reminder provenance assigns -categories; user-authored text is never classified by matching prompt headings. -Counts use the runtime token estimator, are labelled estimates, and are not -provider billing counts or measured context-window occupancy. - -Numeric-only composition snapshots travel in the existing opaque usage-record -`token_details.openbitfunModelRequest.contextComposition` field. The additive -optional report `context` field projects the newest in-scope parent request; -subagent records do not replace it. A newer request without a snapshot does not -reuse stale composition. Old records and external hosts without this telemetry -show an explicit empty state, while existing report totals remain usable. -The same runtime/report path runs on the executing host for local, SSH, Peer, -and detached jobs; no controller filesystem reads or new commands are required. - -The leading project, cumulative token, and turn facts use the home overview's -rounded outline and inset vertical dividers, with centered values above labels. -The Token Usage section precedes context composition. Its continuous horizontal bar groups -cumulative tokens into input and output, with total tokens as the denominator. -With recorded cache counts, the input group splits into cached and remaining -parts in two shades of blue; output stays purple. Group labels above the bar show -input/output totals and shares. Cache hit rate appears in parentheses immediately -after the input share, with the cached/input calculation in its tooltip. The child legend -shows percentages of input, with counts and the denominator in tooltips. Parent -hover/focus highlights both input parts; a child highlights its own segment and -parent label. For complete telemetry, remaining input is labelled uncached. Partial -telemetry still plots recorded cache reads, but labels the remainder as remaining -input, including unknown cache usage. The inline hit percentage uses an explicit -lower-bound marker (≥) over all input tokens and a scope explanation in its tooltip; -it does not extrapolate the reported-call hit rate to unreported calls. Missing -or invalid cache counts keep a single input segment. Invalid or inconsistent -totals never produce invented segments. -Child widths are normalized within each parent group so they fill that group -instead of applying the overall share a second time. Nonzero groups and segments -have small visibility minimums; labels retain the actual percentages and zero -segments are omitted. - -The token-level hit rate uses summed cache reads divided by summed total input -over the same records, not an average of per-request percentages or a share of -input plus output. This follows the [OpenAI caching guide](https://developers.openai.com/api/docs/guides/prompt-caching). -The adapter already normalizes [Anthropic usage](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) -by adding ordinary input, cache reads and cache writes; only reads enter the hit -numerator. The report's partial-coverage rate covers reported records only, so it -is not presented as the overall input hit rate or used to infer uncached input. - -Long OpenBitFun sessions can include model streaming, tool execution, Git operations, file writes, Skills, MCP calls, context compression, subagents, retries, user confirmation waits, and file diffs. The chat transcript shows what happened, but it does not yet answer the user's operational questions: - -- What recorded runtime spans are available for the session, and where are they approximate? -- Which models contributed token usage when a session used multiple models? -- Which tools, files, or retries dominated the session? -- Did context compression reduce or increase token pressure? -- Can this summary be read later in the conversation, not only in a temporary popover? - -Claude Code's `/usage` is the closest product reference. Anthropic describes it as a command that helps users understand Claude Code usage in the context of session and context-window management. GitHub Copilot cloud agent exposes a session overview/log where users can track progress, token usage, session count, and session length. OpenAI Agents SDK is the useful engineering reference: its usage API tracks requests and tokens per run/request, while tracing models agent work as spans for model generations, tool calls, handoffs, guardrails, and custom events. - -References: - -- [Claude Code session management and `/usage`](https://claude.com/blog/using-claude-code-session-management-and-1m-context) -- [GitHub Copilot agent session tracking](https://docs.github.com/en/copilot/how-tos/use-copilot-agents/cloud-agent/track-copilot-sessions) -- [OpenAI Agents SDK usage](https://openai.github.io/openai-agents-python/usage/) -- [OpenAI Agents SDK tracing](https://openai.github.io/openai-agents-python/tracing/) - -External reference check: as of 2026-05-11, these sources support a narrow usage-report scope. Claude Code frames `/usage` around understanding session usage and managing context; GitHub Copilot exposes session progress, token usage, session count, session length, and logs; OpenAI Agents SDK separates per-run/request token usage from deeper span tracing. Treat these as product references, not compatibility requirements. OpenBitFun's boundary is intentionally narrower than a full tracing dashboard: it reports the current session's recorded runtime facts, their coverage, and safe navigation points back to the transcript or existing diff surfaces. - -## Product Direction - -OpenBitFun should support a Claude-like `/usage` command and a richer Desktop runtime report using the same underlying data. - -The key product decision is: - -> `/usage` creates a durable, readable session report in the current conversation, while Desktop also exposes the same action from one compact button in the Chat input footer. - -The implemented report is intentionally a persisted-data report. It does not claim pure model streaming throughput, first-token latency, token-per-second speed, or live per-model timing unless a stable runtime span contract provides those fields. - -This avoids making usage insight feel hidden behind a temporary button. Users should be able to run `/usage`, scroll back to the report, search it, export it with the session, and compare it with file changes. - -## Closed Product Scope - -The closed product is a current-session runtime report. It answers: - -- How long the session spans and how much recorded work happened inside it. -- Which token, model, tool, file, error, compression, and slow-work facts were recorded. -- Which facts are complete, partial, or unavailable, with user-facing reasons near the affected values. -- Which transcript turn or existing diff surface can be opened for verification. -- Which report metadata should be copyable or exportable without exposing unnecessary path detail by default. - -The product is complete when `/usage` can generate a durable, model-invisible report from the current session, the Chat-bottom entry exposes the same action, the detail panel makes recorded facts understandable, and partial data is clearly labeled. Anything that changes runtime policy, recommends workflow changes, aggregates across sessions, or shows live status belongs outside this closed scope. - -## Independent Third-Party Review - -Verdict: the direction is reasonable, but the current draft needs a few product and telemetry boundaries before it is safe to implement. The strongest decisions are: - -- Use one structured report contract and surface-specific renderers instead of separate CLI/Desktop counters. -- Store Desktop `/usage` as user-visible but model-invisible local output. -- Start with existing persisted data, then improve accuracy with additive runtime spans. -- Exclude charts, cross-session summaries, and automated optimization advice from the closed product scope; keep the Chat-bottom action as a compact entry point backed by the same session-level contract. - -The main remaining risk is not that the feature is too ambitious. The risk is that "usage reporting" accidentally becomes a second runtime control plane for budget, scheduler, retry, artifact, or context mutation behavior. The report should be an observability projection. It may consume runtime facts, but it must not decide scheduling, retries, context compaction, permissions, or runtime governance. - -The current implementation resolves the important boundaries as follows: - -- Standard `/usage` reports the current session only. -- Overlapping recorded spans use union accounting where supported, and the UI labels approximations. -- `/usage` requires an idle session and returns friendly local feedback while a turn is active. -- Cached tokens, reasoning tokens, local models, model aliases, and provider-specific token details are marked partial or unavailable when the source cannot prove them. -- Durable/exportable reports use bounded labels, workspace-relative paths where possible, and copy/export path redaction by default. - -## Relationship to Adjacent Runtime Plans - -This document should remain the user-facing reporting layer for session runtime facts. It should not duplicate the owner responsibilities already planned elsewhere: - -| Area | Owner document / module | Usage report responsibility | Must not do | -| --- | --- | --- | --- | -| Budget, truncation, retry classification, output spill | `agent-runtime-budget-governance-design.md` and runtime budget modules | Show summarized facts and coverage when those events exist | Recompute budget policy, trigger compaction, retry model calls, or own spill decisions | -| Context mutation and health | `context-reliability-architecture.md` | Report context mutation timing, token before/after, and lossy/partial markers | Infer context quality from transcript prose or create a second health model | -| Subagent scheduling and gateway pressure | `docs/agent-runtime-subagent-scheduling-plan.md` | Report queued/running/retry/wait timing from scheduler events | Implement queueing, permits, retry backoff, or effective concurrency | -| Deep Review policy and evidence | `deep-review-design.md` and Deep Review services | Display reviewer/runtime contribution when linked to the session | Re-plan reviewer roles, strategy, retry budget, or evidence collection | - -If two documents describe the same runtime fact, the implementation should pick one behavioral owner and let `/usage` consume the typed event or persisted summary from that owner. - -## User Experience Shape - -### CLI - -In CLI chat mode, `/usage` should render a compact terminal-friendly report. - -Current implemented shape: - -```text -Session usage - -Session span: 2h 14m -Recorded turn time: 18m 42s -Tool call time: 4m 36s -Compressions: 2 - -Tokens -Input: 183,420 -Output: 21,908 -Cached: not reported -Total: 205,328 - -Models -gpt-5.4: 8 req, 183,420 input, 21,908 output - -Tools -ExecCommand: 19 calls, 3m 13s, 2 errors -Write/Edit: 7 calls, 1m 08s -``` - -CLI may allow a one-line mode later, but the implemented command is a full report only. - -### Desktop Chat Markdown Report - -In Desktop Flow Chat, `/usage` should add a local, non-model-visible Markdown report into the chat stream. - -The report should be durable and exportable: - -- It persists with the session. -- It is searchable like other chat content. -- It is not sent back to the model by default. -- It can contain links/actions to open the detailed runtime panel or relevant diffs. - -Current implemented Markdown shape: - -```markdown -## Session Usage - -| Metric | Value | -|---|---| -| Session span | 2h 14m | -| Recorded turn time | 18m 42s | -| Model round time | 11m 28s | -| Tool call time | 4m 36s | - -### Tokens -| Type | Tokens | -|---|---:| -| Input | 183,420 | -| Output | 21,908 | -| Cached | not reported | -| Total | 205,328 | - -### Models -| Model | Calls | Input | Output | Total | -|---|---:|---:|---:|---:| -| gpt-5.4 | 8 | 183,420 | 21,908 | 205,328 | - -### Slowest Work -1. ExecCommand `pnpm run build:web` - 1m 42s -2. Context compression - 28s -3. ExecCommand `git fetch origin main` - 19s -``` - -The detailed visual report exists alongside the Markdown snapshot. It uses the structured DTO when present and falls back to the Markdown snapshot for historical/local-only reports. - -### Desktop Chat-Bottom Usage Entry - -The implemented Flow Chat entry is a compact action in the Chat input footer (`ChatInputWorkspaceStrip`) that generates `/usage` in the current chat. It intentionally avoids title/header placement and live timing values. - -The Chat-bottom entry should never compete with the title/header row. It must preserve the input footer's workspace/branch controls, model selector, and send affordances. Its job is to start report generation, not to become a live status display. - -## Current Reusable Capabilities - -OpenBitFun can reuse several existing surfaces. - -### Current code anchors - -| Capability | Current anchor | -| --- | --- | -| Agentic event definitions | `src/crates/contracts/events/src/agentic.rs` | -| Token usage persistence and aggregation | `src/crates/assembly/core/src/service/token_usage/{types.rs,service.rs,subscriber.rs}` | -| Model stream timing currently logged/held during execution | `src/crates/assembly/core/src/agentic/execution/{round_executor.rs,stream_processor.rs}` | -| Context compression events and tool-like UI item | `src/crates/assembly/core/src/agentic/execution/execution_engine.rs`, `src/web-ui/src/flow_chat/tool-cards/ContextCompressionDisplay.tsx` | -| Tool lifecycle and total duration | `src/crates/assembly/core/src/agentic/tools/pipeline/{tool_pipeline.rs,state_manager.rs}` | -| CLI slash command handling | `src/apps/cli/src/modes/chat.rs` | -| CLI session/tool persistence | `src/apps/cli/src/session.rs`, `src/apps/cli/src/agent/core_adapter.rs` | -| Desktop token/compression event routing | `src/web-ui/src/flow_chat/services/flow-chat-manager/EventHandlerModule.ts` | -| Flow Chat Chat-bottom usage entry | `src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.tsx` | -| Session file badge and diff affordances | `src/web-ui/src/flow_chat/components/modern/{SessionFilesBadge.tsx,SessionFileModificationsBar.tsx}` | -| Operation-level file diff and summary entry | `src/web-ui/src/flow_chat/tool-cards/FileOperationToolCard.tsx` | - -### Existing events and runtime data - -- `AgenticEvent::DialogTurnCompleted` already has turn duration, round count, tool count, success, finish reason, and partial recovery metadata. -- `AgenticEvent::TokenUsageUpdated` already carries session, turn, model, input/output/total tokens, max context tokens, and subagent marker. -- `AgenticEvent::ContextCompressionStarted/Completed/Failed` already exposes trigger, before/after tokens, ratio, duration, summary status, and subagent parent info. -- `AgenticEvent::ModelRoundStarted/Completed` can define model round boundaries. -- `ToolEventData::Completed` carries tool duration. -- `ToolEventData::{Queued, Waiting, Started, Progress, Streaming, Failed, Cancelled}` already gives tool lifecycle states. - -### Existing token usage service - -`TokenUsageService` already persists records with: - -- model id -- session id -- turn id -- input tokens -- output tokens -- cached tokens -- total tokens -- subagent flag - -It also provides summary aggregation by model and session. - -### Existing UI surfaces - -- Flow Chat already listens to token usage and context compression events. -- Context compression is already rendered as a tool-like card. -- `SessionFilesBadge`, `SessionFileModificationsBar`, and file operation tool cards already connect chat with file diffs. -- The Chat input footer already hosts compact session-level controls, including workspace/branch context and the usage action. - -### Existing CLI surfaces - -- CLI chat mode already recognizes slash commands. -- `/sessions` opens the session browser; `/resume`, `/continue`, and `/history` are aliases. -- `/status` shows current runtime facts and the latest primary-model request observed by the TUI. -- CLI session messages and tool cards already persist tool call count and tool duration. - -## Original Gaps and Current Implementation Status - -The subsections below preserve the original gap analysis so later reviews can -see why each work item existed. The current code review on 2026-05-11 checked -the plan against `origin/main..HEAD` and found P0/P1 implemented, with P2 -hardening implemented except for real long-session smoke checks and items now -outside the closed scope. "Done" means code and automated coverage exist in -this branch; "Partial" means the foundation exists, but the remaining work -below still needs product or technical signoff before the item should be -considered complete. - -| Area | Current status | Code evidence | Remaining work | -| --- | --- | --- | --- | -| Shared report service | Done | `src/crates/assembly/core/src/service/session_usage/{service.rs,types.rs,render.rs}` and `SessionAPI.getSessionUsageReport` | Keep the API contract stable while adding future report fields. | -| Durable local report message | Done | `DialogTurnKind::LocalCommand`, `localCommandKind: 'usage_report'`, `modelVisible: false` | Keep usage report snapshots model-invisible through future history, export, and transcript changes. | -| CLI `/usage` coverage | Done for interactive CLI | CLI `usage_*` coverage and the shared renderer | Top-level `openbitfun usage --session` is outside the closed product scope. | -| Model timing | Mostly done | Optional event and persisted fields for duration, provider/model identity, first chunk, visible output, stream duration, attempts, failure category, and token details | Throughput/TPS and provider-latency claims are outside the closed product scope. | -| Tool phase timing/classification | Mostly done | Optional terminal tool duration fields and `session_usage::classifier` coverage | Scheduler, budget, and backoff facts still depend on owning modules emitting typed facts. | -| File correlation and diff links | Mostly done | Snapshot summaries, `UsageFileRow.operationIds`, representative model/tool/error anchors, `SessionUsagePanel` diff actions, file-row turn jumps, tool-input-only file-row tool anchors, and panel-local long-session row caps | Exact per-call deep anchors for every aggregate row are outside the closed product scope. | -| Token reporting boundary | Done | Token-focused locale guard coverage and DTO fields limited to runtime/session facts | Keep `/usage` centered on current-session observability. | -| i18n and theme | Mostly done | Flow Chat locale alignment coverage, semantic style guard coverage, quick-action localization coverage, detail-panel tab keyboard semantics, and manual preview checks across Light, Slate, Dark, Midnight, Ink Charm, Ink Night, Cyber, and Tokyo Night | Final real-App long-session smoke and keyboard/focus pass are still needed before final UX signoff. | -| Scope and workspace identity | Done for current session | Request carries workspace path/remote identity and DTO exposes `UsageWorkspace` | Hidden subagent and visible side-session aggregation are outside the closed product scope. | -| Redaction/export policy | Mostly done | Bounded labels, privacy flags, workspace-relative display, copied metadata, and copy/export path redaction preference | Broader export formats stay outside the closed product scope. | - -### 1. Shared session usage report service - -Missing: - -- A single API that returns a `SessionUsageReport` for CLI, Desktop, and later server use. -- A shared formatter that can produce Markdown and terminal text from the same structured data. - -Required change: - -- Use the existing platform-agnostic session usage owners to aggregate persisted session turns, runtime events or runtime journal records, token usage records, and snapshot/file stats. -- Keep product logic platform-agnostic. Desktop and CLI should call the same report service through adapters. - -### 2. Durable local report message - -Missing: - -- A session item kind for local command output that is visible to the user but not injected into future model context. - -Required change: - -- Add a non-model-visible `LocalCommand` or `UsageReport` dialog turn/item kind. -- The Markdown report should be persisted and exportable, but `DialogTurnKind::is_model_visible()` should keep it out of model input by default. - -Risk if skipped: - -- If `/usage` is stored as a normal assistant message, later model calls may ingest the report, increasing token usage and creating self-referential context noise. - -### 3. CLI event coverage - -Missing: - -- CLI `AgentEvent` does not currently surface token usage, model round timing, or context compression as first-class events. -- CLI `/status` reports the latest observed request rather than cumulative session usage; `/history` - is an alias for the session browser and is not a usage-report entrypoint. - -Required change: - -- Extend CLI adapter events or let `/usage` query the shared report service after the fact. -- Add `/usage` to CLI command handling and `/help`. -- Prefer querying the report service for final numbers over maintaining a separate CLI-only counter. - -### 4. Model timing and throughput - -Missing: - -- Model round start/completion events do not currently expose duration or model id in a way that is enough for reliable throughput metrics. -- `first_chunk_ms`, `first_visible_output_ms`, `send_to_stream_ms`, and `stream_processing_ms` exist in runtime logs/objects, but they are not stable persisted report fields. - -Required change: - -- Add structured model timing to report data, either through `ModelRoundCompleted` fields or a new runtime span/journal. -- Capture model id per round, request attempt count, first token latency, stream duration, output tokens, and failure category. - -### 5. Tool timing breakdown - -Missing: - -- Tool completion has total duration, but queue wait, preflight, confirmation wait, and execution time are currently logged rather than emitted as stable report fields. -- Failed and cancelled tool events do not consistently include duration. - -Required change: - -- Extend tool lifecycle report metadata or create `RuntimeSpan` records for tool phases. -- Include `duration_ms` on failed/cancelled tool terminal events when available. -- Classify tool categories: `git`, `terminal`, `file_write`, `file_read`, `skill`, `mcp`, `browser`, `context`, `review`, `other`. - -### 6. Git command classification - -Missing: - -- Git now runs through ExecCommand; persisted legacy sessions may still contain dedicated Git or Bash calls. - -Required change: - -- Classify a terminal call as Git when the normalized command starts with `git` or PowerShell/cmd wrappers run `git`. -- Keep the original tool name and command for detail view, but aggregate under `Git` for report readability. - -### 7. Skill and script attribution - -Missing: - -- Skills may appear as Skill tool calls, shell scripts, or prompt-loaded context. - -Required change: - -- Attribute explicit `Skill` tool invocations by skill command/name. -- Attribute shell-executed known skill scripts only when the tool metadata proves it, not by fuzzy command guessing. -- Treat passive skill-loading context as token/context overhead, not script execution time. - -### 8. File change statistics by scope - -Missing: - -- File operation cards can already request operation summaries/diffs, but the usage report needs stable aggregate counts for operation, turn, session, and git scopes. - -Required change: - -- Reuse snapshot operation metadata for operation-scoped file stats. -- Add report fields for files changed, additions, deletions, and top changed files. -- Link report rows to existing operation/turn/session/git diff opening paths. - -### 9. Token reporting boundary - -Missing: - -- TokenUsageService stores tokens, but not every provider reports the same token detail categories. -- Cache, reasoning, audio/image, local-model, and gateway-mediated token details may be unavailable or partial. - -Required change: - -- `/usage` reports token counts and token coverage only when the source data can support them. -- Keep unavailable token categories visible as partial coverage instead of showing misleading zeros. -- Keep token detail categories provider-agnostic in the DTO; provider-specific values can be optional structured fields, not prose. -- Do not let token reporting become a recommendation, quota, or policy surface. - -### 10. Internationalization and theme integration - -Missing: - -- Report strings, metric labels, table headers, compact chip labels, empty/error states, and tooltip text need locale coverage. -- Theme variables must be used for charts, status chips, badges, and report tables. - -Required change: - -- Use locale keys in `src/web-ui/src/locales/*/flow-chat.json` or a new localized report namespace. -- Render Desktop reports from structured data through the frontend i18n layer, not from hard-coded backend prose. -- CLI can start with English if the CLI has no locale pipeline, but the report DTO should not make localization impossible later. -- Use `@openbitfun/ui` and semantic theme tokens for colors; do not hard-code status colors except through semantic variables. - -### 11. Report scope and workspace identity - -Missing: - -- The current draft mostly keys reports by `sessionId`, but persisted session ids are only meaningful within a workspace/runtime identity. -- The closed product reports only the active chat session. Hidden subagent sessions, visible `/btw` side sessions, and Deep Review child sessions are not silently aggregated. -- Remote sessions need `remote_connection_id` / `remote_ssh_host` context, and snapshot data may be unavailable for remote workspaces. - -Required change: - -- Every report request and API adapter should include the same workspace identity fields used by session persistence: workspace path plus remote identity when present. -- Default scope is "current user-visible session". -- Hidden subagent usage and visible side sessions such as `/btw` should not be silently folded into the parent report. -- Add coverage keys such as `workspace_identity`, `subagent_scope`, and `remote_snapshot_stats`. - -### 12. Time accounting and overlapping spans - -Missing: - -- The report examples show percentages, but they do not define the denominator. -- Parent and child work can overlap: subagents, tool queues, retries, streaming, and UI waits can make summed resource time exceed wall time. -- P0 turn durations and tool durations can overlap, so "active runtime" can only be a lower-bound or approximate metric until spans are complete. - -Required change: - -- Define `wallMs` as session elapsed time from first reportable turn start to report generation or session end. -- Define `activeMs` as the union of known active spans when spans exist. In P0, label it approximate and derive it from available turn durations. -- Percentages should use `activeMs` as the denominator and should not double count overlapping child spans. -- If the UI later wants "resource time" that sums parallel work, expose it as a separate field such as `resourceMs`, not as a percentage of wall time. - -### 13. Active-session and repeated-report behavior - -Missing: - -- The draft does not say what happens if the user runs `/usage` while a turn is streaming or tools are executing. -- It does not define whether repeated `/usage` commands update the previous report or append new reports. - -Required change: - -- P0 may restrict durable Desktop insertion to idle sessions if the current state machine cannot safely insert local output during an active turn. -- If `/usage` is allowed during active work, it must be a point-in-time snapshot with `generatedAt`, `inProgress=true`, and coverage notes for open spans. It must not mutate the active model round or queued user input. -- Repeated `/usage` should append a new report in P0. Updating an earlier report is a separate UX decision because it changes transcript durability and export semantics. - -### 14. Cached token and provider token-detail propagation - -Missing: - -- `TokenUsageService` stores `cached_tokens`, but current `TokenUsageUpdated` events do not expose cached token counts and the subscriber records cached tokens as `0`. -- Providers can expose different token detail categories: cache read/write, reasoning, audio/image tokens, ephemeral cache tiers, or local-model estimates. - -Required change: - -- Do not render "Cached" as an authoritative P0 metric unless the source actually records it. -- Add coverage metadata for `cached_tokens` and `token_detail_breakdown`. -- Later span/event enrichment should carry provider token details as structured optional fields, not as provider-specific prose. - -### 15. Privacy, redaction, and durable export policy - -Missing: - -- The report is durable and exportable, so command labels, file paths, error examples, model ids, remote host names, and operation links can become part of long-lived session history. -- Detailed tool params and tool results may contain secrets, prompts, file contents, command output, or private paths. - -Required change: - -- Report DTOs must not include raw prompts, full command output, tool params, tool results, file contents, environment variables, or secret-bearing payloads. -- Tool and error examples should use sanitized labels with bounded length and explicit redaction. -- File paths should follow the same visibility policy as existing transcript and diff surfaces. Prefer workspace-relative paths where possible; keep absolute or remote paths only behind existing detail views. -- Export behavior should include local reports by default because they are user-visible, but the report must be safe enough to export under the same rules as chat history. - -### 16. Report versioning and migration - -Missing: - -- A local report item becomes persisted session history. Future schema changes need a migration path. -- Generated Markdown can become stale when the underlying session receives more turns after the report was inserted. - -Required change: - -- Add `schemaVersion`, `reportId`, `generatedAt`, and optionally `generatedFromAppVersion` to structured report metadata. -- Treat persisted Markdown as a historical snapshot, not a live view. Regeneration should create a new report unless a later UX explicitly supports updating. -- Old local report items should deserialize as generic user-visible, model-invisible local output even if the usage-specific subtype is retired. - -## Proposed Data Shape - -The report service should return structured data first. Text rendering is a view concern. - -This is the design target, not a promise that every optional field is implemented. The current implementation intentionally omits per-model speed/latency fields such as `firstTokenMsP50`, `outputTokensPerSecond`, and `effectiveTokensPerSecond` until runtime spans can link those values reliably. - -```ts -type SessionUsageReport = { - schemaVersion: number; - reportId: string; - sessionId: string; - generatedAt: number; - generatedFromAppVersion?: string; - workspace: { - workspacePath?: string; - remoteConnectionId?: string; - remoteSshHost?: string; - workspaceKind?: 'local' | 'remote' | 'unknown'; - }; - scope: { - kind: 'current_session' | 'parent_with_hidden_subagents' | 'custom'; - includesHiddenSubagents: boolean; - includesVisibleSideSessions: boolean; - includedSessionIds: string[]; - inProgress: boolean; - generatedDuringActiveTurn: boolean; - }; - coverage: { - level: 'partial' | 'complete'; - missing: Array< - | 'model_round_timing' - | 'tool_phase_timing' - | 'cached_tokens' - | 'token_detail_breakdown' - | 'subagent_scope' - | 'remote_snapshot_stats' - | 'file_line_stats' - | string - >; - }; - time: { - wallMs: number; - activeMs: number; - activeMsIsApproximate: boolean; - accounting: { - denominator: 'active_union_ms' | 'turn_duration_estimate'; - overlappingChildSpansCountedOnce: boolean; - }; - userIdleMs?: number; - modelMs: number; - toolMs: number; - compressionMs: number; - queueMs?: number; - retryBackoffMs?: number; - resourceMs?: number; - }; - tokens: { - source: 'provider_reported' | 'estimated' | 'mixed' | 'unavailable'; - cacheCoverage: 'available' | 'unavailable' | 'partial'; - input: number; - output: number; - cached?: number; - total: number; - maxContextTokens?: number; - inputDetails?: Record; - outputDetails?: Record; - }; - models: Array<{ - providerId?: string; - modelId: string; - modelAlias?: string; - requestCount: number; - inputTokens: number; - outputTokens: number; - cachedTokens?: number; - totalTokens: number; - firstTokenMsP50?: number; - outputTokensPerSecond?: number; - effectiveTokensPerSecond?: number; - errorCount: number; - }>; - tools: Array<{ - category: string; - name: string; - displayLabel: string; - callCount: number; - successCount: number; - errorCount: number; - totalDurationMs: number; - p95DurationMs?: number; - queueWaitMs?: number; - redacted: boolean; - }>; - files: { - changedFiles: number; - additions?: number; - deletions?: number; - topFiles: Array<{ - path: string; - additions?: number; - deletions?: number; - operationIds?: string[]; - turnIds?: string[]; - scope?: 'operation' | 'turn' | 'session' | 'git'; - redacted?: boolean; - }>; - }; - compression: { - count: number; - failedCount: number; - tokensBefore: number; - tokensAfter: number; - totalDurationMs: number; - }; - errors: Array<{ - category: string; - count: number; - examples: Array<{ turnId?: string; toolCallId?: string; message: string }>; - }>; - slowest: Array<{ - kind: 'model' | 'tool' | 'compression' | 'subagent'; - label: string; - durationMs: number; - turnId?: string; - toolCallId?: string; - coverage?: 'complete' | 'partial'; - }>; - privacy: { - redactionApplied: boolean; - omittedDetailKinds: string[]; - }; -}; -``` - -Exact field names can change during implementation. The important boundary is that the report is structured and renderers are surface-specific. - -## Interaction Requirements - -### `/usage` command - -- CLI: `/usage` renders the terminal report. -- Desktop: `/usage` inserts a Markdown usage report into Flow Chat. -- The command should be local-only and should not call the model. -- The report should clearly indicate when data is partial because older sessions lack runtime spans. -- The report should offer a clear action to open the detailed report panel when Desktop supports it. -- The report is a point-in-time snapshot. It is not expected to update after more turns run. -- Running `/usage` multiple times should append multiple reports in P0. -- If `/usage` is invoked during an active turn, the implementation must either reject it with a friendly local-only message or insert a clearly marked in-progress snapshot without touching the active model/tool state. -- A standard session report should not silently include visible side sessions or hidden subagents. - -### Chat-bottom usage entry - -- The Chat-bottom usage entry is a convenience, not the source of truth. -- It should remain action-only at constrained widths; live metrics are outside the closed product scope. -- It must not introduce horizontal overflow. -- It must support keyboard focus, screen-reader labels, and tooltip summaries. -- It should hide itself automatically when no active session or no reportable data exists. - -### Detailed report panel - -The panel is not required for the first `/usage` milestone, but the data shape should support it. - -Recommended tabs: - -- Overview -- Models -- Tools -- Files -- Errors -- Slowest - -Timeline is outside the closed product scope. Current P2 uses the Slowest tab -plus representative transcript links instead of a full trace timeline. - -## Milestone Execution Plan - -Implementation should be delivered through at most three mergeable milestones. Each milestone must leave CLI, Desktop, and existing session replay behavior usable, even if later metrics are still partial. The detailed tasks below remain the task inventory; this section defines the delivery order, release gates, and rollback boundaries. - -Current milestone progress, verified against the current branch on 2026-05-11: - -| Milestone | Status | Current evidence | Remaining work | -| --- | --- | --- | --- | -| P0: Safe `/usage` foundation | Complete | Shared report service/renderers, model-invisible local report items, CLI interactive `/usage`, Desktop report cards, repeated-report exclusion, and old-session/cache-unavailable fixtures are covered by Rust and Web UI tests. | Keep future changes within the same local-only and model-invisible contract. | -| P1: Runtime spans and file correlation | Complete for the approved P1 scope | Optional model/tool runtime facts persist and aggregate; local usage reports are excluded from the next report span; snapshot-backed and recognized tool-input file rows are surfaced; missing model identity uses legacy-session copy instead of implementation labels. | Scheduler/budget/context/artifact facts remain projections only when their owning modules emit typed facts. Hidden subagents remain excluded by default. | -| P2: Desktop analysis surface | Almost complete | Chat-bottom entry, detail panel tabs, accessible tab keyboard semantics, copyable metadata, file diff actions, slow-span turn jumps, representative model/tool/error anchors, file-row turn/tool-input anchors, panel-local long-list caps, user-confirmed Markdown copy path redaction, i18n coverage, semantic style tests, duplicate-localized-header guard, and manual preview checks across all built-in themes are present. | Real-App long-session smoke remains before final UX signoff. | - -### Milestone P0: Safe `/usage` Foundation - -Goal: ship a Claude-like `/usage` command that is useful with existing data only and cannot affect model execution. - -Included task groups: - -| Order | Work item | Detailed tasks | Output | -| --- | --- | --- | --- | -| P0.0 | Baseline and scope lock | Task 0 | Clean branch proof and narrow change set | -| P0.1 | Shared contract and boundaries | Task 1 | Provider-agnostic `SessionUsageReport` DTO with scope, workspace identity, accounting, redaction, and coverage metadata | -| P0.2 | Durable local report item | Task 2 | User-visible, model-invisible local command/report item | -| P0.3 | Read-only aggregation | Task 3 | Report service using token, turn, tool, compression, and cached snapshot summaries | -| P0.4 | Text renderers | Task 4 | Deterministic terminal and Markdown renderers | -| P0.5 | CLI command | Task 5 | Interactive CLI `/usage` output | -| P0.6 | Desktop command | Task 6 | Desktop `/usage` local Markdown report insertion | -| P0.7 | Presentation baseline | Task 7 subset | Locale keys and theme-safe empty/error/partial states for Desktop report text | -| P0.8 | Contract fixtures | Tasks 1, 3, 4, 6 | Fixtures for old sessions, missing cache data, remote workspace without snapshot stats, repeated reports, and active-session rejection/snapshot behavior | - -Functional guardrails: - -- `/usage` must never call the model, enqueue an agent turn, mutate runtime scheduling, or trigger context compression. -- The Desktop report must be stored as a local command/report item that is visible to the user but excluded from model-visible history. -- P0 must not introduce live header UI, charts, cross-session summaries, or new runtime span persistence. -- Missing data must be represented by coverage metadata and partial-data copy, not by misleading zero values. -- Old sessions must still deserialize and replay; sessions without token/snapshot data should still produce a partial report instead of failing. -- P0 must not show cached-token counts as real if current events only record them as `0`. -- P0 report requests must be scoped by workspace identity as well as session id. -- P0 percentages must state whether they use approximate turn-duration accounting or complete span-union accounting. -- P0 output must use sanitized labels and bounded examples; no raw prompts, tool params, tool results, command output, file contents, or environment values. - -Risk and drift controls: - -| Risk or drift | Mitigation | Stop condition | -| --- | --- | --- | -| Report enters future model context | Add regression tests around model-visible history assembly before wiring CLI/Desktop commands | Any test shows report Markdown in model input | -| `ChatInput.tsx` becomes a feature dump | Keep command parsing behind a small command helper or service boundary when adding `/usage` | More than one local-command branch is added directly to the component | -| Aggregation scans become slow on long sessions | Use persisted records and cached snapshot summaries only; do not compute full diffs during `/usage` | Report generation needs workspace-wide or full-diff reads | -| P0 appears more accurate than it is | Render partial coverage notes next to affected sections | A metric cannot explain whether it is complete or approximate | -| CLI and Desktop diverge | Both call the same report service and only differ at renderer/adaptor boundaries | Same fixture produces different counts | -| Workspace/session id ambiguity | Require workspace identity in report API requests and service lookup | Same session id can read data from another workspace | -| Cached token metric is fake | Add `cached_tokens` coverage and hide/unavailable-state when only zero-filled records exist | Report labels cached tokens as known while source cannot measure them | -| Running `/usage` disturbs active turn | Reject during active turn or insert only a local point-in-time item outside the active model round; exclude local usage-report turns from aggregation and session activity ordering | `/usage` changes queued input, model state, active turn persistence, report scope, or session recency | - -Required verification before merging P0: - -- `cargo check -p openbitfun-core --no-default-features --features agent-runtime` -- `cargo test -p openbitfun-core --no-default-features --features agent-runtime --lib session_usage -- --nocapture` -- Focused CLI command tests or manual CLI smoke if no existing helper test harness exists. -- `pnpm run lint:web` -- `pnpm run type-check:web` -- `pnpm --dir src/web-ui run test:run` -- Manual or automated proof that Desktop `/usage` does not call the model send path. -- Fixture proof that cache-unavailable, remote-snapshot-unavailable, and old-session reports render as partial rather than zero/empty success. -- Regression proof that repeated `/usage` creates separate historical snapshots while the previous report is excluded from the next report's scope and timing. - -Rollback boundary: - -- P0 can be disabled by hiding `/usage` command registration in CLI/Desktop while leaving DTO and read-only service code in place. -- The local report item type must remain backward-compatible once persisted; if it needs removal, migrate it as a generic local system/report item instead of deleting session records. - -P0 implementation status (2026-05-11): complete. The current branch has the -shared DTO/service/renderer path, interactive CLI `/usage`, Desktop local -report card insertion, and regression coverage for old sessions, unavailable -cache fields, repeated usage reports, workspace identity, and model-invisible -local report turns. The original rollback boundary still applies: disable the -command entry points first if product rollback is needed, and keep persisted -local command records backward-compatible. - -### Milestone P1: Accurate Runtime Spans and File Correlation - -Goal: make P0 reports more accurate by enriching runtime span data and linking file-change summaries without changing tool/model behavior. - -Included task groups: - -| Order | Work item | Detailed tasks | Output | -| --- | --- | --- | --- | -| P1.0 | Model span enrichment | Task 8 | Optional model timing fields for first chunk, first visible output, stream duration, attempts, and failure category | -| P1.1 | Tool phase spans | Task 9 | Queue, preflight, confirmation, execution, total, failed, and cancelled timing summaries | -| P1.2 | Conservative classification | Task 9 | Report-only tool categories, including Git classification with false-positive tests | -| P1.3 | File-change correlation | Task 10 | Changed file counts, additions/deletions when cached, and metadata links to existing diff scopes | -| P1.4 | Coverage upgrade | Tasks 3, 8, 9, 10 | Aggregator prefers precise spans and falls back to P0 data for old sessions | -| P1.5 | Runtime-fact consumption | Tasks 8, 9, 10 | Consume scheduler, budget, context mutation, and artifact facts when their owning modules emit them; do not reimplement those owners | - -Functional guardrails: - -- Span fields must be optional or additive so existing Desktop, server, websocket, and CLI consumers continue to work. -- Instrumentation must not add sleeps, awaits, locks, retries, or scheduling changes to model streaming or tool execution paths. -- No per-token persistence is allowed; store terminal summaries or bounded span records only. -- File correlation must reuse existing snapshot summaries and diff open paths. It must not change operation, turn, session, or git diff semantics. -- Command classification must be conservative. A terminal command is `git` only when the normalized executable is clearly Git. -- Scheduler, budget, retry, context mutation, and artifact fields must be projections from their owning runtime modules. The usage report must not create a parallel state machine. -- Parallel child spans should be parented or linked so the report can show both user-perceived elapsed time and optional resource time without double-counting percentages. - -Risk and drift controls: - -| Risk or drift | Mitigation | Stop condition | -| --- | --- | --- | -| Event schema breaks old clients | Add optional fields or new event variants with adapter compatibility tests | Existing event consumers require code changes unrelated to reporting | -| Timing instrumentation affects runtime | Capture already measured timestamps at terminal state transitions | Any measurable behavior change in model/tool lifecycle tests | -| File links open the wrong diff scope | Store explicit scope metadata and route through existing snapshot APIs | Operation-scoped links resolve to cumulative session diff | -| Tool category becomes policy logic | Keep classifiers inside report service or report-only module | Classification affects permissions, scheduling, or confirmation | -| Old sessions lose report usefulness | Preserve P0 fallback path and mark coverage partial | Old-session fixtures fail report generation | -| Subagent or retry time is double-counted | Store parent/child span relationships and compute percentages from span union | Parent active time plus child active time exceeds denominator in percentage sections | -| Usage reporting duplicates budget/scheduler facts | Consume typed events from owning modules only | New `/usage` code owns retry, queue, budget, or context mutation decisions | - -Required verification before merging P1: - -- `cargo check --workspace` -- `cargo test --workspace` -- Event adapter serialization tests for optional model/tool timing fields. -- Report aggregation tests covering complete spans, partial spans, old sessions, and file summaries. -- Regression tests for Git command classification false positives. - -Rollback boundary: - -- P1 can be rolled back by ignoring new span fields in aggregation while leaving additive event fields in place. -- If an event field proves risky, keep the DTO coverage key and revert only the producer path, so `/usage` continues to work with P0 data. - -Executable implementation plan: - -P1 must move `/usage` from P0 approximation toward factual runtime accounting without changing the report command contract. The implementation order below is test-first and split by ownership boundary so each step can be reviewed independently. - -1. Persist runtime facts already owned by the runtime. - - Files: `src/crates/assembly/core/src/service/session/types.rs`, `src/crates/assembly/core/src/agentic/session/session_manager.rs`, `src/crates/assembly/core/src/agentic/coordination/coordinator.rs`, and the tool/model execution call sites that construct persisted session items. - - Add optional fields to persisted model rounds for provider/model identity, first chunk latency, first visible output latency, stream duration, attempt count, failure category, token details, and total duration. - - Add optional tool phase durations to persisted tool items: queue wait, preflight, confirmation wait, and execution. - - Acceptance: old session JSON still deserializes, new session JSON round-trips with these fields, and missing fields never fail report generation. - -2. Consume persisted facts in the usage service. - - File: `src/crates/assembly/core/src/service/session_usage/service.rs`. - - Prefer persisted model/tool duration fields when present; fall back to existing start/end or result durations only when facts are missing. - - Compute active time as a union of known active intervals so overlapping spans do not double-count the denominator. - - Exclude `local_command` usage-report turns from scope, wall/active time, model/tool/file/error rows, and slowest spans so generating a report cannot affect the next report. - - Use a localized "model not recorded" label for persisted model spans that have timing but no model identity, instead of exposing implementation terms such as `model round 0`. - - Mark `ModelRoundTiming` and `ToolPhaseTiming` coverage available only from actual recorded facts, not from guessed fallback data. - - Acceptance: model rows can exist from runtime span facts even when token records are absent, tool rows expose phase subtotals, slowest spans include model rounds and tools, and the coverage panel explains missing facts conservatively. - -3. Keep file-change correlation conservative. - - File: `src/crates/assembly/core/src/service/session_usage/service.rs`. - - Keep snapshot operations as the highest-trust source for file rows, including remote sessions when cached snapshot summaries are present, then use tool-call metadata as a fallback only for recognized edit/write/delete operations. - - Preserve operation ids and turn indexes for later UI navigation, but do not invent line counts when no snapshot or diff fact exists. - - Acceptance: remote sessions with cached snapshot summaries show file/line rows; remote sessions that only have tool metadata show edited files with unknown line counts instead of "unavailable"; files without trustworthy evidence remain omitted. - -4. Surface the new facts without adding noise. - - Files: `src/web-ui/src/flow_chat/components/usage/*`, `src/web-ui/src/flow_chat/store/FlowChatStore.ts`, `src/web-ui/src/infrastructure/api/service-api/AgentAPI.ts`, and `src/web-ui/src/locales/*/flow-chat.json`. - - Show model duration only when at least one model duration is recorded. - - Keep error and coverage explanations visible through concise hover text plus detail-page descriptions. - - Avoid new claims such as exact throughput or file-line changes unless the backend has the underlying fact. - -5. Verify and review consistency. - - Required checks: `pnpm run lint:web`, `pnpm run type-check:web`, `pnpm --dir src/web-ui run test:run`, `cargo check --workspace`, and `cargo test --workspace`. - - Review pass: compare backend DTOs, TypeScript types, visible copy, and coverage explanations for the same semantics; list any remaining approximate or inferred fields explicitly. - -P1 red/green test plan: - -- Rust service tests: - - model span facts create model rows and slow-span rows without token records; - - local usage-report turns are excluded from report scope, timing, model/tool/file/error rows, and slowest spans; - - missing model identity renders as localized "model not recorded" copy rather than `model round N`; - - active time uses interval union instead of summing overlapping turns; - - tool phase timings are summed by tool and enable `ToolPhaseTiming` coverage; - - file rows prefer snapshot operations for local and remote sessions, and use tool-call metadata only as fallback. -- Rust persistence tests: - - legacy persisted model/tool JSON without new fields still deserializes; - - persisted model/tool JSON with P1 fields round-trips. -- Web UI tests: - - model duration column appears only when duration facts exist; - - missing timing facts still use coverage/error explanations instead of absolute claims. - -P1 residual-risk checklist: - -- Hallucination risk: any field derived from a fallback must be labeled approximate or unavailable, never exact. -- Drift risk: frontend labels must match backend `accounting`, `denominator`, and coverage states. -- Privacy risk: file paths must continue to use existing redaction/path-label behavior. -- Compatibility risk: optional fields must not invalidate old persisted sessions or remote-session reports. -- Rollback risk: ignoring new optional fields must leave the P0 report usable. - -P1 implementation review note (2026-05-11): - -- Third-party review result: the P1 data contract is additive and optional; old persisted turns, model rounds, and tool items still deserialize, while new facts round-trip through Rust and Web UI session persistence. -- Product-risk review result: visible copy now distinguishes recorded runtime from provider latency, model duration columns stay hidden until at least one model row has timing, and unavailable file/error facts have short hover text plus detail-panel explanations. -- Configuration-side review result: built-in Commit/Create PR quick actions display localized defaults, but unchanged localized defaults are normalized back to canonical storage values when saved so language switching is not pinned to one locale. -- Known boundary: hidden subagent totals remain excluded from the standard report until parent linkage and scheduler/event aggregation are reliable enough for default inclusion. -- Known boundary: legacy start/end timing can make the report useful but still approximate; `accounting` and help text must remain the source of truth for precision. -- Known boundary: remote-session file rows use snapshot summaries when available and recognized file-edit tool inputs otherwise; line counts are still unavailable without snapshot facts. -- Current consistency update: Chat-bottom usage is the only Desktop entry point; report generation appends a local visible report card but that local command is excluded from future usage aggregation and does not update session activity ordering. -- Verification evidence for this review: `pnpm run lint:web`, `pnpm run type-check:web`, `pnpm --dir src/web-ui run test:run`, `cargo check --workspace`, and `cargo test --workspace`. - -### Milestone P2: Responsive Desktop Analysis Surface - -Goal: add the interactive Desktop analysis panel and keep a compact Chat-bottom entry point after the report contract is stable. The implemented entry is a lightweight `/usage` trigger in the Chat input footer; title/header placement and live timing values are outside the closed product scope. - -Included task groups: - -| Order | Work item | Detailed tasks | Output | -| --- | --- | --- | --- | -| P2.0 | Chat-bottom action contract | Task 11 | Entry point that can generate the current session report | -| P2.1 | Responsive Chat-bottom entry | Task 11 | `ChatInputWorkspaceStrip` usage action as a stable icon/text trigger | -| P2.2 | Detailed report panel | Task 12 | Overview, Models, Tools, Files, Errors, and Slowest tabs using the shared DTO | -| P2.3 | Diff and transcript links | Tasks 10, 12 | Snapshot-backed file rows open existing diff viewers; slow-span rows can jump to known turns; model/tool/error aggregate rows expose representative anchors; file rows can jump to transcript turns; tool-input-only file rows can request tool-card focus when a single stable tool item id exists | -| P2.4 | i18n, theme, accessibility hardening | Tasks 7, 11, 12 | Locale-safe labels, semantic colors, keyboard access, tooltips, and screen-reader labels | - -Functional guardrails: - -- The Chat-bottom entry is an optional entry point, not the only way to access `/usage`. -- The implemented Chat-bottom entry does not show live metrics or model/tool percentages. -- The title/header must stay free of usage controls in the current implementation. -- The Chat input footer must preserve existing workspace, branch, model, attachment, and send controls at small widths. -- Entry rendering must use priority collapse instead of viewport-scaled fonts or clipped text. -- The panel must not render raw prompts, full command output, file contents, or secret-bearing tool payloads. -- P2 must not add large charting libraries; use existing components, simple bars, tables, or capped lists. - -Risk and drift controls: - -| Risk or drift | Mitigation | Stop condition | -| --- | --- | --- | -| Small windows become cluttered | Use container-aware priority collapse and icon-only fallback | Chat footer controls overlap or disappear in narrow desktop widths | -| Live summary creeps back into the footer | Keep the entry action-only | Streaming causes visible layout jitter | -| Panel becomes a debugger replacement | Keep default view summary-first and deep links back to existing transcript/diff surfaces | Panel starts duplicating raw tool output or full diffs | -| i18n text overflows | Test `en-US`, `zh-CN`, and `zh-TW`; prefer card/list fallback over wide tables | Any required label clips in supported locales | -| Theme contrast regresses | Use semantic tokens and light/dark checks | New colors bypass theme tokens | - -Required verification before merging P2: - -- `pnpm run lint:web` -- `pnpm run type-check:web` -- `pnpm --dir src/web-ui run test:run` -- Component/layout tests for the usage trigger in wide and narrow Chat footer states. -- Locale smoke checks for `en-US`, `zh-CN`, and `zh-TW`. -- Manual preview checks across all built-in themes: Light, Slate, Dark, Midnight, Ink Charm, Ink Night, Cyber, and Tokyo Night. -- Manual proof that existing file diff buttons and report-linked diff buttons open the same scopes. - -Rollback boundary: - -- P2 can be disabled by hiding the Chat-bottom entry and panel route/action while keeping `/usage` Markdown reports available. -- If the panel has performance issues on long sessions, keep P2.0/P2.1 and disable only the detailed tab content behind a feature flag or capability switch. - -P2 implementation progress note (2026-05-11): - -- P2.0/P2.1 entry placement matches current code: the usage action lives in `ChatInputWorkspaceStrip` at the Chat bottom, not in `FlowChatHeader` or the window title/header area. -- P2.2 is implemented as a single detail-panel module today: `SessionUsagePanel.tsx`, `SessionUsagePanel.scss`, `sessionUsagePanelTypes.ts`, and `openSessionUsageReport.ts`. The panel includes Overview, Models, Tools, Files, Errors, and Slowest tabs. Splitting tab bodies into separate files is a maintenance choice, not product scope. -- P2.3 is partially implemented. File rows open the existing snapshot diff viewer through `snapshotAPI.getOperationDiff` and `createDiffEditorTab`; no new diff renderer or mutation path is introduced. Slowest rows can jump to known turns through the existing Flow Chat pin-to-top event. Model, tool, and error aggregate rows now carry optional representative anchors from core and route through the existing Flow Chat focus event. File-row turn indexes also use the focus event for transcript jumps, and tool-input-only file rows request tool-card focus only when a single stable tool item id is available. -- The file diff action is intentionally enabled only for trustworthy snapshot-backed rows with a visible path and session id. Redacted rows, tool-input-only rows, and unavailable rows show a disabled placeholder with an explanation instead of attempting a best-effort open. -- Exact per-call deep anchors are outside the closed product scope; aggregate rows link to representative sources instead of becoming a full occurrence explorer. -- Remaining P2 hardening: complete real-App long-session smoke for scroll/jump feel. Panel-local long-session row caps, Markdown copy/export path redaction, detail-panel tab keyboard semantics, representative aggregate anchors, file-row turn/tool-input anchors, duplicate-localized-table-header guards, and built-in-theme preview checks are implemented in this branch. Do not touch Flow Chat's global virtual list or scroll layout for this work unless a separate navigation design is approved. -- Verification evidence for current P2 slices: `SessionUsageComponents` covers detail tabs, file diff action, slowest turn jumps, copyable metadata, unavailable help, token-only copy, i18n behavior, duplicate localized table headers, and semantic color usage; manual preview checks covered Light, Slate, Dark, Midnight, Ink Charm, Ink Night, Cyber, and Tokyo Night. Broader web/Rust verification is tracked in the P1 review note above. - -### Final Merge-Ready Execution Plan - -This plan was re-reviewed on 2026-05-11 after refreshing `origin/main`. The -current branch is based on latest `origin/main`, and main has recent Flow Chat -scroll-position, follow-output jitter, settings/usage, and theme work. The -remaining usage-report work must therefore stay local to the usage-report -surface. - -Compatibility guardrails for the final hardening batch: - -- Run `git fetch origin main:refs/remotes/origin/main` before starting. -- Run `git merge-base --is-ancestor origin/main HEAD`; stop and rebase if it fails. -- Run `git diff --name-only origin/main..HEAD` and confirm the batch does not touch unrelated mainline areas. -- Do not modify `src/web-ui/src/flow_chat/components/modern/VirtualMessageList.tsx`, `src/web-ui/src/flow_chat/utils/flowChatScrollLayout.ts`, theme preset files, tray files, installer files, or settings basics files for P2 hardening unless a separate design review approves that scope. -- Keep every final P2 change reversible by hiding the usage detail action or disabling one panel tab while leaving `/usage` Markdown reports available. - -Next executable stage: - -| Stage | Recommendation | Files allowed by default | Verification | Stop condition | -| --- | --- | --- | --- | --- | -| Final smoke and merge readiness | Run real Desktop long-session smoke, fix only usage-local defects, then prepare commit/PR | Usage panel/card styles, usage component tests, locale files, and this document | `pnpm run lint:web`, `pnpm run type-check:web`, `pnpm --dir src/web-ui run test:run`, `cargo check --workspace`, `cargo test --workspace`, real-App long-session smoke | Any fix requires Flow Chat global scroll/session-switch changes or shared theme token changes. | - -Detailed executable checklist: - -1. Launch the real Desktop app from this branch. -2. Open or create a long session with enough model, tool, file, and error rows to exercise every detail tab. -3. Generate `/usage` from the Chat-bottom entry and from the slash command if available. -4. Check the report card, detail panel tabs, file table sticky action column, copy/export redaction toggle, representative transcript jumps, file diff actions, and keyboard focus order. -5. Confirm that report generation does not trigger a model request, does not appear in the next report span, and does not make session switching or follow-output feel slower. -6. Fix only usage-local issues found during the smoke pass. -7. Re-run the full frontend and Rust verification commands. -8. Rebase onto latest `origin/main`, split into a small commit set, and update the PR description around the closed product scope. - -### Explicitly Out Of Closed Scope - -The following work is intentionally not queued as follow-up for this usage-report product: - -- Cross-session or project-level summaries. -- Export formats beyond existing conversation export behavior. -- Automated recommendations such as context optimization advice or retry-loop diagnosis. -- Live status metrics in the Chat footer or title/header. -- Exact per-call occurrence browsing for every aggregate row. - -If any of these become important later, they should start from a new product question instead of being treated as incomplete work in this milestone. - -## Fine-Grained Execution Breakdown - -This section turns the milestone plan into implementation-sized tasks. Each task has an explicit blast radius, risk list, mitigation, and functional guardrails. Later detailed task plans can split these further, but implementation should preserve the P0 to P2 order unless a task is deliberately descoped. - -### Global guardrails - -- `/usage` must never trigger a model request. -- `/usage` output must not be included in future model context unless a user explicitly quotes or references it in a later prompt. -- P0 reports tokens and available timing only. P0 does not introduce charts, cross-session summaries, or live header UI. -- Runtime metrics collection must be append-only or summary-only; do not add per-token persistence. -- Shared report logic belongs in its current platform-agnostic Rust owner. Desktop, server, and CLI keep only their entry adapters. -- Desktop UI must use existing i18n, semantic theme tokens, and `@openbitfun/ui` primitives. -- Every report field that can be incomplete must carry coverage metadata instead of silently showing `0`. -- Existing file diff behavior must not change while adding report links. -- Report APIs must be scoped by workspace identity and remote identity, not only by session id. -- Reports must define their scope: current session, hidden subagents included/excluded, visible side sessions included/excluded, and whether the session was active when generated. -- Time percentages must state the denominator and must not double-count overlapping child spans. -- Durable report content must be redacted and bounded. Do not persist raw prompts, tool params, full tool output, file contents, secrets, or environment values in the report DTO or Markdown. -- Reporting must consume budget/scheduler/context/artifact facts from their owning modules rather than implementing another control plane. - -### Task 0: Baseline and scope lock - -Goal: start implementation from a known branch state and prevent unrelated files from entering the feature. - -Files: - -- Read: `session-runtime-usage-report-design.md` -- Read: `AGENTS.md` -- Read: `src/web-ui/AGENTS.md` -- Read: `src/crates/assembly/core/AGENTS.md` - -Steps: - -1. Confirm the branch is based on the latest remote main: - - Run `git fetch --no-tags origin main`. - - Run `git merge-base --is-ancestor origin/main HEAD`. - - Run `git rev-list --left-right --count origin/main...HEAD`. -2. Confirm the intended working set: - - Run `git status --short --branch`. - - Keep unrelated untracked docs and scratch files unstaged unless the user explicitly asks otherwise. -3. Create a narrow branch or backup branch before code changes if the checkout is dirty. - -Functional guardrails: - -- Do not rebase, push, or force-push during implementation unless the user asks for that exact git operation. -- Do not stage root-level architecture docs unless the current task explicitly edits them. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Implementation starts from stale main | Require ancestry proof before code edits | -| Unrelated docs enter the PR | Use explicit path staging and `git diff -- ` review | -| Old tracking branch shows ahead/behind after rebase | Treat tracking divergence as publish state only; do not "fix" it without a push request | - -Verification: - -- `git merge-base --is-ancestor origin/main HEAD` exits `0`. -- `git diff --check origin/main..HEAD` has no new whitespace issues from this feature. - -### Task 1: Shared report DTO and coverage model - -Goal: define the stable structured contract used by CLI, Desktop, and future server/API surfaces. - -Files: - -- Create: `src/crates/assembly/core/src/service/session_usage/types.rs` -- Create: `src/crates/assembly/core/src/service/session_usage/mod.rs` -- Modify: `src/crates/assembly/core/src/service/mod.rs` -- Test: `src/crates/assembly/core/src/service/session_usage/types.rs` or nearby module tests - -Steps: - -1. Add `SessionUsageReport`, `UsageCoverage`, `UsageTimeBreakdown`, `UsageTokenBreakdown`, `UsageModelBreakdown`, `UsageToolBreakdown`, `UsageFileBreakdown`, `UsageCompressionBreakdown`, `UsageErrorBreakdown`, and `UsageSlowSpan` structs. -2. Add `CoverageLevel::{Complete, Partial}` plus a stable list of missing-data keys such as `model_round_timing`, `tool_phase_timing`, and `file_line_stats`. -3. Use milliseconds and token counts in DTOs; keep formatting out of DTOs. -4. Make optional fields explicit for data that is not reliable in P0. -5. Add `schema_version`, `report_id`, `generated_at`, workspace identity, report scope, in-progress marker, and redaction metadata. -6. Define time accounting fields so renderers know whether percentages use span-union accounting or P0 turn-duration estimates. -7. Add token source and cache coverage fields so cached tokens are not shown as known when the event source cannot measure them. -8. Add serde round-trip tests with missing optional fields. - -Functional guardrails: - -- The DTO must be provider-agnostic. -- Do not include raw prompts, full command output, file contents, secrets, or tool result payloads. -- Do not encode localized prose in the DTO. -- Do not treat model aliases such as `fast` as the authoritative model id when a resolved provider/model id is available. -- Do not assume session ids are globally unique across local and remote workspaces. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| DTO becomes UI-specific | Use numeric fields and enum identifiers, not rendered text | -| DTO leaks sensitive transcript data | Store labels and identifiers only; detail links reuse existing transcript permissions | -| Future fields break older persisted reports | Prefer optional fields and serde defaults | -| Cache coverage looks complete when it is not | Make cache coverage explicit and assert unavailable cache does not render as measured `0` | -| Scope ambiguity hides or double-counts child work | Record report scope and included session ids | - -Verification: - -- `cargo test -p openbitfun-core --no-default-features --features agent-runtime --lib session_usage -- --nocapture` once tests exist. -- `cargo check -p openbitfun-core --no-default-features --features agent-runtime`. -- DTO tests for workspace identity, report scope, in-progress reports, cache-unavailable coverage, and redaction metadata. - -### Task 2: Non-model-visible local report item - -Goal: give Desktop `/usage` a durable chat representation without polluting future model context. - -Files: - -- Modify: `src/crates/assembly/core/src/service/session/types.rs` -- Modify: `src/crates/assembly/core/src/agentic/session/session_manager.rs` -- Modify: `src/web-ui/src/flow_chat/types/flow-chat.ts` -- Modify: `src/web-ui/src/flow_chat/store/FlowChatStore.ts` -- Test: session serialization/deserialization tests near existing session tests -- Test: web-ui Flow Chat store tests - -Steps: - -1. Add a local-only session item or dialog turn kind such as `DialogTurnKind::LocalCommand` with a report subtype such as `usage_report`. -2. Ensure `DialogTurnKind::is_model_visible()` returns `false` for local command/report turns. -3. Persist the report Markdown and structured report id/metadata in the session, including `generatedAt`, `schemaVersion`, report scope, and whether it was generated while the session was active. -4. Ensure older sessions deserialize with the existing default `UserDialog`. -5. Ensure export/search can include local reports, while model message assembly excludes them. -6. Treat each generated report as a historical snapshot. Do not mutate an older report in P0 when the user runs `/usage` again. - -Functional guardrails: - -- Do not store `/usage` as a normal assistant message. -- Do not change visibility semantics for existing user, assistant, tool, or compaction messages. -- Do not hide existing system/tool diagnostics from the user. -- Do not insert a local report into the currently streaming model round. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Report enters model context | Add a regression test that builds model-visible history after `/usage` and asserts the report is absent | -| Existing persisted sessions fail to load | Keep serde defaults and add old-shape fixture tests | -| Search/export unexpectedly omit the report | Treat local reports as user-visible but model-invisible | -| Re-running `/usage` rewrites history | Append a new report item in P0 and preserve previous report timestamps | - -Verification: - -- Rust session tests for `is_model_visible()`. -- Web UI store tests for insert, render, persist, reload, and exclude-from-model-context behavior. -- Tests for repeated local reports and active-turn insertion/rejection behavior. - -### Task 3: P0 report aggregation from existing data - -Goal: produce a useful `/usage` report without changing runtime event production. - -Files: - -- Create: `src/crates/assembly/core/src/service/session_usage/service.rs` -- Modify: `src/crates/assembly/core/src/service/session_usage/mod.rs` -- Modify: `src/crates/assembly/core/src/service/mod.rs` -- Read/reuse: `src/crates/assembly/core/src/service/token_usage/service.rs` -- Read/reuse: `src/crates/assembly/core/src/service/session/types.rs` -- Read/reuse: `src/crates/assembly/core/src/service/snapshot/service.rs` -- Test: `src/crates/assembly/core/src/service/session_usage/service.rs` - -Steps: - -1. Aggregate token records for the current session. Preserve subagent markers only as coverage metadata; do not silently fold hidden subagent usage into the default report. -2. Aggregate persisted dialog turn durations for wall and active lower-bound estimates. -3. Aggregate tool result `duration_ms` from persisted model round items. -4. Identify context compression items by existing `ContextCompression` tool records and compression event metadata when available. -5. Aggregate file stats from snapshot operation summaries where operation ids are present. -6. Resolve workspace identity before reading session, token, or snapshot data. -7. Mark cached tokens as unavailable when the source records only subscriber-filled zeroes. -8. Mark remote snapshot stats as unavailable when snapshot tracking is skipped for remote workspaces. -9. Mark missing coverage keys for model round timing, tool phase timing, cached token detail, subagent scope, and remote snapshot stats in P0. - -Functional guardrails: - -- P0 aggregation must be read-only. -- Absence of a data source must produce partial coverage, not a failed report. -- If a metric is unavailable, omit it or label it unavailable; do not substitute `0` unless the true value is known to be zero. -- Aggregation must not scan the workspace, compute large diffs, or read full file contents. -- Aggregation must not infer hidden subagent linkage from display names or fuzzy text matching. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Double-counting subagent tokens | Include `is_subagent` in model breakdown and document whether totals include subagents | -| Turn duration and tool duration overlap | Label active runtime as approximate until span data exists | -| Snapshot stats unavailable for old sessions | Mark `file_line_stats` missing and still report changed file count if available | -| Remote workspace snapshot data is absent by design | Mark `remote_snapshot_stats` missing and avoid file-line totals unless a remote-safe source exists | -| Cached tokens are silently zero-filled | Add a source capability check and render cache as unavailable | -| Scope is guessed from names | Use explicit parent/session linkage only; otherwise mark `subagent_scope` partial | - -Verification: - -- Unit tests with complete data. -- Unit tests with missing token records. -- Unit tests with missing snapshot stats. -- Unit tests proving not-reported metrics are partial, not zeroed. -- Unit tests for remote workspace coverage, cache-unavailable coverage, and scope/include-subagent behavior. - -### Task 4: Shared text renderers - -Goal: render the same report as CLI terminal text and Desktop Markdown without duplicating business logic. - -Files: - -- Create: `src/crates/assembly/core/src/service/session_usage/render.rs` -- Modify: `src/crates/assembly/core/src/service/session_usage/mod.rs` -- Test: renderer snapshot or exact-output tests in Rust - -Steps: - -1. Add `render_usage_report_terminal(report)` for CLI. -2. Add `render_usage_report_markdown(report)` for Desktop P0. -3. Keep rendering deterministic: stable sort models/tools by duration or token count, then by label. -4. Include a partial-data note when `coverage.level == Partial`. -5. Render token counts and token coverage only; omit unrelated account, plan, or quota language. -6. Render scope, generated time, and in-progress state in a compact way when they affect interpretation. -7. Render unavailable cached tokens as unavailable/partial, not as `0`, unless the source proves the true value is zero. -8. Bound and redact slowest-work labels, error examples, and path displays. - -Functional guardrails: - -- Renderers must not query storage or mutate sessions. -- Markdown should be plain Markdown with tables and short lists; no HTML or app-specific directives in P0. -- Terminal output must not rely on color for meaning. -- Renderers must not leak raw tool input/output fields that were intentionally omitted from the DTO. -- Renderers must explain approximate time accounting when percentages are shown. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| CLI and Desktop summaries diverge | Both renderers consume one DTO | -| Wide Markdown tables are unreadable | Keep tables narrow in P0 and move wide details to later UI cards | -| Report text becomes localized in Rust too early | Keep Rust renderer English for CLI/P0, let Desktop component-localized rendering replace Markdown later if needed | -| Redacted fields become confusing | Use short labels such as `redacted path` or `details omitted` and link to existing transcript detail when available | -| Approximate time reads as exact | Add a compact note near affected time fields | - -Verification: - -- Exact-output tests for terminal and Markdown renderers. -- Tests for partial coverage note. -- Tests for stable sort ordering. -- Tests for cache-unavailable rendering, in-progress rendering, redaction, and approximate time notes. - -### Task 5: CLI `/usage` - -Goal: add Claude-like interactive CLI usage reporting. - -Files: - -- Modify: `src/apps/cli/src/modes/chat.rs` -- Modify: `src/apps/cli/src/agent/core_adapter.rs` only if the report needs additional session identity wiring -- Modify: `src/apps/cli/src/session.rs` only if local command entries are also persisted in CLI sessions -- Test: CLI command handling tests if available; otherwise add focused unit tests around command dispatch helpers - -Steps: - -1. Add `/usage` to `/help`. -2. In command handling, call the shared report service for the current session id. -3. Render terminal text through `render_usage_report_terminal`. -4. Add the output as a system/local CLI message without calling the model. -5. Return a clear partial-data message if the report service cannot find a source. -6. If the existing command handler is synchronous, isolate async report loading behind a small command-dispatch boundary instead of blocking the TUI event loop with storage work. - -Functional guardrails: - -- Do not make `/usage` asynchronous model work. -- Do not overload `/sessions` or its aliases with usage-report behavior. -- Do not require Desktop-only state for CLI reports. -- Do not make the CLI command depend on a Tauri API or Desktop workspace state. -- Do not print sensitive raw tool details that Desktop would redact. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| CLI lacks enough persisted data | Use coverage metadata and report available basics | -| Command blocks TUI | Keep aggregation bounded and avoid scanning full workspaces | -| `/usage` conflicts with future top-level `openbitfun usage` | Keep interactive command implementation isolated behind a reusable service | -| Sync command path grows ad hoc async blocking | Add a small command dispatcher/helper rather than embedding runtime/blocking logic in the match arm | - -Verification: - -- CLI `/help` includes `/usage`. -- `/usage` output appears in chat without a model request. -- Existing `/sessions` aliases, `/new` and `/clear` fresh-session behavior, and normal message send - behavior still work. -- CLI output redacts the same sensitive detail categories as Desktop P0. - -### Task 6: Desktop `/usage` command and local Markdown insertion - -Goal: insert a durable, model-invisible usage report in Flow Chat when the user types `/usage`. - -Files: - -- Modify: `src/web-ui/src/flow_chat/components/ChatInput.tsx` -- Modify or create: `src/web-ui/src/flow_chat/services/usageReportService.ts` -- Modify: `src/web-ui/src/infrastructure/api/service-api/AgentAPI.ts` or a new usage API adapter -- Modify: `src/web-ui/src/flow_chat/services/flow-chat-manager/EventHandlerModule.ts` only if local report insertion belongs in manager flow -- Test: `src/web-ui/src/flow_chat/components/ChatInput.test.tsx` or command helper tests -- Test: Flow Chat manager/store tests for local report insertion - -Steps: - -1. Add local `/usage` command recognition before model submit. -2. Call the usage report API for the active session. -3. Insert a local Markdown report item using the non-model-visible type from Task 2. -4. Show a user-friendly error if the report cannot be generated. -5. Ensure the report can be copied/exported like other visible chat content. -6. Pass workspace path and remote identity through the same adapter shape used by session persistence. -7. Define active-turn behavior before wiring: reject with local feedback, insert after current turn, or insert a marked point-in-time snapshot. P0 should choose the simplest safe option. -8. Add `/usage` to slash suggestions only where it can execute safely; do not queue it as a future model message while the session is processing. - -Functional guardrails: - -- Do not add more feature-specific branching to `ChatInput.tsx` than necessary; prefer a small command registry/helper if the existing shape supports it. -- Do not call Tauri directly from UI components; go through infrastructure API adapters. -- Do not send `/usage` to the current model. -- Do not let `/usage` participate in queued user input semantics. -- Do not store the typed `/usage` command as the user prompt for a model turn. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| ChatInput grows more coupled | Extract command handling into a small helper if more than one local command path is touched | -| Desktop and CLI render different numbers | Both call shared report service | -| User sees a blank or overly technical failure | Add localized empty/error states with retry/open-details actions | -| `/usage` gets queued during active generation | Treat it as a local command with explicit active-session behavior, separate from model queueing | -| Remote sessions read the wrong store | Carry remote identity through the report API and test remote/local request shapes | - -Verification: - -- Web UI test that typing `/usage` does not call send-message/model APIs. -- Web UI test that a local report appears in the session. -- Web UI test that the local report is marked model-invisible. -- Web UI test for active-session behavior and for repeated reports. -- Adapter test or mock proof that workspace path and remote identity are passed to the backend. - -### Task 7: Internationalization and report presentation guardrails - -Goal: make Desktop report output friendly across locales, themes, and small surfaces. - -Files: - -- Modify: `src/web-ui/src/locales/en-US/flow-chat.json` -- Modify: `src/web-ui/src/locales/zh-CN/flow-chat.json` -- Modify: `src/web-ui/src/locales/zh-TW/flow-chat.json` -- Modify or create: Desktop report rendering component if Markdown is rendered through a structured card in later phases -- Test: locale key coverage tests if available - -Steps: - -1. Add locale keys for report title, time labels, token labels, model/table labels, partial data notes, error states, and actions. -2. Use locale-aware duration and number formatting in Desktop components. -3. Keep CLI English until CLI locale support is deliberately added. -4. Define report color semantics through existing theme tokens. -5. For Markdown reports, avoid embedding hard-coded backend-localized prose in the DTO. - -Functional guardrails: - -- Do not add English strings directly to React components except test ids or internal identifiers. -- Do not introduce hard-coded colors for warnings, errors, model segments, or token types. -- Do not make CJK locales rely on narrow English table labels. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Report tables overflow in Chinese or Traditional Chinese | Use compact labels and allow card/list fallback in later UI | -| Theme contrast regression | Use semantic tokens and screenshot/manual checks in light/dark | -| Backend-generated Markdown cannot localize | Treat Rust Markdown renderer as CLI/P0 fallback; Desktop can render from DTO with i18n later | - -Verification: - -- Locale smoke check for `en-US`, `zh-CN`, and `zh-TW`. -- Theme smoke check in dark and light mode. -- Text overflow check at narrow widths. - -### Task 8: Model round span enrichment - -Goal: make model speed and wait-time metrics accurate after the minimal report is stable. - -Files: - -- Modify: `src/crates/contracts/events/src/agentic.rs` -- Modify: `src/crates/contracts/events/src/frontend_projection.rs` -- Modify: `src/crates/assembly/core/src/agentic/execution/round_executor.rs` -- Modify: `src/crates/assembly/core/src/agentic/execution/stream_processor.rs` -- Deferred: add Server/WebSocket delivery only when the server has a real usage-report event consumer; do not prebuild an unused transport adapter. -- Test: Rust event serialization and stream/round executor tests - -Steps: - -1. Extend model round completion metadata or add a runtime span event with provider id, resolved model id, display/model alias, duration, first chunk latency, first visible output latency, stream duration, and attempt count. -2. Preserve existing `ModelRoundStarted/Completed` event consumers by adding optional fields or a separate event. -3. Record failure category and partial recovery state when available. -4. Propagate provider token details when available: cached tokens, cache write/read, reasoning tokens, multimodal tokens, and provider-specific detail keys. -5. Update report aggregation to use precise model span data when present. -6. Keep P0 fallback logic for old sessions. - -Functional guardrails: - -- Do not break existing event names or required fields consumed by Desktop, server, or CLI. -- Do not emit per-token events. -- Do not change model retry behavior as part of metrics instrumentation. -- Do not derive user-facing optimization conclusions from token details in this task. -- Do not use display aliases such as `fast` for model-level aggregation when the resolved model id is known. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Event schema breaks clients | Add optional fields or a new event variant with adapter compatibility tests | -| Timing changes affect execution | Use already measured timestamps; no sleeps, locks, or awaits added to hot stream loops | -| TPS appears wrong with missing output tokens | Only compute TPS when both duration and output tokens are present | -| Model aliases merge unrelated providers | Store display alias separately from provider/resolved model id | -| Token details vary by provider | Preserve detail keys as optional structured metadata and mark coverage partial when absent | - -Verification: - -- Existing stream processor tests still pass. -- Event projection tests cover optional fields. -- Report tests prefer span timing when available and fall back when absent. -- Tests for cache/reasoning token detail propagation when provided and absence handling when not provided. - -### Task 9: Tool phase span enrichment and classification - -Goal: explain tool-heavy sessions without relying on logs. - -Files: - -- Modify: `src/crates/contracts/events/src/agentic.rs` -- Modify: `src/crates/contracts/events/src/frontend_projection.rs` -- Modify: `src/crates/assembly/core/src/agentic/tools/pipeline/tool_pipeline.rs` -- Modify: `src/crates/assembly/core/src/agentic/tools/pipeline/state_manager.rs` -- Test: tool pipeline/state manager tests - -Steps: - -1. Persist or emit queue wait, preflight, confirmation wait, execution, and total duration for completed tools. -2. Include best-effort total duration for failed and cancelled tools. -3. Add a report-only classifier for tool categories. -4. Classify legacy dedicated Git tool calls as `git`. -5. Classify ExecCommand and legacy terminal calls as `git` only when the normalized command clearly invokes Git. -6. Classify file operations by tool name and snapshot operation metadata. -7. When SubagentScheduler or budget governance events exist, consume their queued/running/retry/backoff summaries as runtime facts instead of inferring them from tool names. - -Functional guardrails: - -- Do not change tool scheduling, confirmation, permissions, or cancellation behavior. -- Do not parse arbitrary command text for sensitive details beyond category detection. -- Do not treat every terminal command containing the string `git` as a Git operation. -- Do not classify tool mutability or retry safety in the usage report; consume those facts from the tool/runtime owner if they exist. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Metrics changes perturb tool pipeline | Capture existing measured durations at terminal state transitions only | -| Misclassified commands mislead users | Use conservative classifiers and expose original tool name in detail | -| Failed tools lack start time | Mark phase timing partial while still counting failure | -| Report code becomes scheduler/retry policy | Keep queue/backoff/retry fields as consumed event facts, not report-owned decisions | - -Verification: - -- Unit tests for legacy Git tool classification. -- Unit tests for ExecCommand Git command classification and false positives. -- Existing tool lifecycle tests still pass. - -### Task 10: File-change report integration - -Goal: connect usage reports with existing file diff affordances without changing diff semantics. - -Files: - -- Modify: `src/crates/assembly/core/src/service/session_usage/service.rs` -- Read/reuse: `src/crates/assembly/core/src/service/snapshot/service.rs` -- Read/reuse: `src/web-ui/src/infrastructure/api/service-api/SnapshotAPI.ts` -- Modify later UI: `src/web-ui/src/flow_chat/tool-cards/FileOperationToolCard.tsx` only for report link integration -- Test: report aggregation tests with snapshot operation summaries - -Steps: - -1. Aggregate changed files from snapshot session stats and operation summaries. -2. Include additions/deletions only when available from snapshot diff summaries. -3. Preserve operation, turn, session, and git diff scopes as separate concepts. -4. Add report link metadata that can open existing diff viewers later. -5. Keep file paths normalized for display, but do not rewrite stored paths. -6. For remote workspaces where snapshot tracking is skipped or unavailable, report file stats as partial and avoid implying no files changed. -7. Prefer workspace-relative display paths; mark paths redacted when a safe relative display cannot be produced. - -Functional guardrails: - -- Do not change `get_operation_diff` semantics. -- Do not replace operation-scoped diff with cumulative session diff. -- Do not open or compute large diffs while generating a lightweight `/usage` report unless cached summaries exist. -- Do not read file bodies solely to enrich `/usage`. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Report generation becomes slow on large sessions | Use cached operation summaries and counts, not full diff content | -| Scope confusion returns | Store `operationIds` and `turnIds` separately from session/git scopes | -| Path normalization breaks remote workspaces | Use existing path utilities and keep original path in metadata | -| Remote sessions look like they changed no files | Render `remote_snapshot_stats` partial instead of zeroing changed-file counts | -| Absolute paths leak private workspace layout | Prefer workspace-relative paths and bounded labels | - -Verification: - -- Tests for operation-level additions/deletions. -- Tests for old sessions without operation ids. -- Manual check that existing file diff buttons still open the same diff. -- Tests for remote/no-snapshot coverage and path display redaction. - -### Task 11: Responsive Chat-bottom usage entry - -Goal: add one compact Chat-bottom entry that generates the current session usage report without displaying live metrics. Live summaries and header entries are outside the closed product scope. - -Files: - -- Modify: `src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.tsx` -- Modify: `src/web-ui/src/flow_chat/components/ChatInput.tsx` only for command/action wiring that already belongs to the input surface -- Existing: `src/web-ui/src/flow_chat/components/usage/SessionRuntimeStatusEntry.tsx` remains a lightweight/tested action component, but the production Chat-bottom entry is owned by `ChatInputWorkspaceStrip` -- Test: layout/component tests near existing Chat input/footer tests - -Steps: - -1. Add or keep a Chat-bottom usage action that triggers the same report generation path as `/usage`. -2. Implement a stable icon/text button with an icon-only narrow fallback. -3. Preserve Chat input footer workspace, branch, model, attachment, and send-control layout priority. -4. Add tooltip and accessible label that describe the action, not live report values. -5. Hide the entry when no active session exists. -6. Keep live values out of the entry. -7. Do not add a duplicate title/header entry while the Chat-bottom action is the product-approved entry point. - -Functional guardrails: - -- Do not make the status entry the only way to access usage details. -- Do not use viewport-scaled font sizes. -- Do not allow the entry to push core Chat input controls offscreen. -- Do not show high-frequency timing changes in a way that causes constant reflow. -- Do not show model/tool percentages in the Chat footer. -- Do not add a title/header affordance unless a separate product/design review reopens that placement. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Small windows become cluttered | Use container-query or measured available-space collapse | -| The Chat footer becomes too dynamic while streaming | Keep the implemented entry action-only | -| Accessibility suffers in icon-only mode | Provide aria-label and tooltip with text summary | -| Users confuse live action and historical report | Label report generated time and keep the entry action-only | - -Verification: - -- Component/layout tests for visible text and icon-only narrow states. -- Playwright or manual screenshot checks for small desktop windows and the Chat input footer. -- Theme checks in dark and light mode. - -### Task 12: Detailed report panel - -Goal: provide interactive analysis without making `/usage` depend on a heavy UI. - -Files: - -- Current implementation: `src/web-ui/src/flow_chat/components/usage/SessionUsagePanel.tsx` -- Current implementation: `src/web-ui/src/flow_chat/components/usage/SessionUsagePanel.scss` -- Current implementation: `src/web-ui/src/flow_chat/components/usage/sessionUsagePanelTypes.ts` -- Current implementation: `src/web-ui/src/flow_chat/components/usage/openSessionUsageReport.ts` -- Deferred split, only if needed: `SessionUsageOverview.tsx`, `SessionUsageModels.tsx`, `SessionUsageTools.tsx`, and `SessionUsageFiles.tsx` -- Test: focused component tests for panel tabs and empty states - -Steps: - -1. Open the panel from the Markdown/report-card action and Chat-bottom usage entry. -2. Add tabs: Overview, Models, Tools, Files, Errors, Slowest. -3. Use panel-local capped lists for slowest spans and file rows first; consider virtualization only in a separate design if caps are insufficient. -4. Link file rows to existing diff open paths. -5. Show partial coverage explanations close to affected metrics. -6. Keep detail expansion routed through existing transcript/diff permissions instead of embedding raw details in the panel. - -Functional guardrails: - -- Do not put large charting libraries in P1/P2. -- Do not render full command output, prompts, or file contents inside the report panel. -- Do not change existing transcript tool-card behavior. -- Do not let the panel become the only place where partial-data caveats are visible; Markdown and CLI still need caveats. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| Panel becomes a second debugger UI | Keep the default view summary-first and hide raw detail behind existing transcript links | -| Large sessions cause UI jank | Cap rows inside the usage panel first; do not touch Flow Chat global virtualization without a separate design | -| File links open wrong scope | Include explicit scope metadata and route through existing diff helpers | -| Panel exposes more sensitive detail than transcript | Reuse existing detail surfaces and redaction policy | - -Verification: - -- Component tests for partial and complete reports. -- Manual checks on long sessions. -- Existing Flow Chat rendering tests still pass. - -### Task 13: Closed product scope guardrails - -Goal: keep `/usage` focused on current-session observability and prevent scope drift into cross-session summaries, recommendations, or live runtime control. - -Files: - -- Harden: `src/crates/assembly/core/src/service/session_usage/render.rs` -- Harden: `src/apps/cli/src/main.rs` -- Update: `session-runtime-usage-report-design.md` -- Test: current-session report and top-level CLI scope tests - -Steps: - -1. Keep the DTO limited to current-session runtime facts: tokens, timing, models, tools, files, errors, coverage, privacy, and navigation metadata. -2. Separate cache read, cache write, input, output, and reasoning token categories only when providers expose them reliably. -3. Keep cross-session aggregation, live footer metrics, and automated recommendations out of the closed product scope. -4. Keep top-level `openbitfun usage --session ` out of scope until workspace-scoped persisted session lookup is designed. - -Functional guardrails: - -- Do not show charts, cross-session trends, live runtime percentages, optimization recommendations, or scheduler actions in this report. -- Do not imply token counts are a quota or policy decision. -- Do not backfill or rewrite historical reports when newer runtime facts become available. -- Preserve current-session scope markers and partial-coverage copy. - -Risks and mitigations: - -| Risk | Mitigation | -| --- | --- | -| User reads the report as an instruction to change workflow | Keep the copy descriptive and avoid recommendations | -| Provider token semantics drift | Keep token detail fields optional and covered by partial-data markers | -| Scope expands beyond the current session too early | Keep cross-session views out of this milestone | -| Historical reports become inconsistent after runtime schema updates | Treat persisted reports as snapshots and regenerate a new report when needed | - -Verification: - -- Tests assert token reporting is present. -- CLI test asserts top-level `usage` is not registered. - -## Product Risks and Mitigations - -| Risk | Impact | Mitigation | -| --- | --- | --- | -| Usage report pollutes future model context | Higher token usage, confusing self-reference | Store as non-model-visible local command output | -| Metrics look authoritative when data is partial | User mistrust | Include coverage state and "partial data" notes | -| Token counts are mistaken for a workflow instruction | User changes behavior based on partial data | Keep the report descriptive, show coverage, and avoid recommendations | -| Chat footer becomes noisy or breaks small windows | Worse chat UX | Responsive priority collapse and tooltip-only narrow mode | -| Report exposes sensitive command/file details | Privacy concern | Default to aggregate labels; detailed command/file rows follow existing transcript visibility rules | -| Runtime tracing adds overhead | Slower sessions | Persist request/tool terminal summaries, not per-token events | -| i18n tables become unreadable in CJK locales | Poor localization | Use responsive table/cards and locale-aware number/duration formatting | -| Theme colors fail in dark/light/high-contrast modes | Accessibility issue | Use semantic theme tokens and contrast tests | -| CLI and Desktop diverge | Confusing reports | Use shared DTO and separate renderers | -| Old sessions lack span data | Empty or misleading report | Partial report with clear unavailable fields | -| Session scope is ambiguous | Double-counting or hiding subagent/side-session work | Include explicit report scope, included session ids, and subagent coverage metadata | -| Overlapping spans are summed as percentages | Percentages exceed reality and users distrust the report | Use active span union as denominator; expose resource time separately if needed | -| Cached tokens are zero-filled | False optimization signal | Hide or mark cache metrics unavailable until events carry real cache counts | -| Report reads the wrong workspace/session store | Privacy or correctness issue | Require workspace path plus remote identity in report API requests | -| Active `/usage` mutates running state | Lost queued input or corrupted turn persistence | Reject during active turn or insert only a local point-in-time snapshot outside the active round | -| Durable report leaks sensitive details | Exported chat exposes commands, paths, errors, or secrets | Use bounded labels, workspace-relative paths, redaction metadata, and no raw tool params/results | -| Usage report duplicates runtime control logic | Conflicting budget/scheduler/retry behavior | Treat report as projection only; consume typed facts from owning modules | - -## Verification Strategy - -Minimum checks for implementation milestones: - -- Rust unit tests for report aggregation and partial coverage. -- CLI tests for `/usage` command output. -- Web UI tests for Markdown insertion, non-model-visible report item behavior, and responsive Chat-footer collapse. -- Locale smoke tests for `en-US`, `zh-CN`, and `zh-TW`. -- Theme screenshot/manual checks for dark and light modes. -- Regression check that running `/usage` does not trigger a model request. -- Fixture tests for old sessions, missing token records, cache-unavailable records, remote workspaces without snapshot stats, hidden subagent exclusion, repeated reports, and active-session behavior. -- Time-accounting tests where parent and child spans overlap, proving percentages use the intended denominator. -- Redaction tests covering command labels, file paths, error examples, and omitted tool params/results. -- Workspace identity tests proving a report cannot read a same-id session from another local or remote workspace. - -For frontend changes, use the normal web verification: - -```bash -pnpm run lint:web -pnpm run type-check:web -pnpm --dir src/web-ui run test:run -``` - -For shared Rust aggregation: - -```bash -cargo check --workspace -cargo test --workspace -``` - -## Closed Product Decisions - -No remaining product decision blocks closure. The current implementation and this document define the closed scope as: - -- Desktop renders `/usage` as a dedicated local report card backed by structured metadata and Markdown fallback. -- CLI supports interactive chat `/usage`; top-level `openbitfun usage --session ` is outside the closed product scope. -- The compact usage entry lives in the Chat input footer as a lightweight `/usage` trigger; it does not display live model/tool percentages and no longer appears in the title/header area. -- Unavailable cache, tool timing, and file metrics include user-facing reasons in hover/help text. -- Model timing is labeled as recorded model-round time; per-model duration columns appear only when at least one model row has recorded duration facts. -- The detail panel shows generated time, session ID, and project path as separate rows with copy controls for long values. -- Idle gap is computed as wall time minus the union of recorded active turn spans when those spans are available. -- Report generation itself is a user-visible local command card, but it is excluded from report scope, timing, model/tool/file/error rows, and session activity ordering. -- `/usage` requires an idle session and returns local feedback while a turn is active. -- Cached tokens are shown as unavailable when the source cannot prove a value; unknown cache metrics are never shown as `0`. -- Hidden subagents, visible side sessions, full trace timelines, exact per-call occurrence browsing, live status metrics, and cross-session summaries are outside the closed product scope. -- Path redaction for Markdown copy/export defaults on, can be user-confirmed by checkbox, and remembers the last preference. -- The detail panel stays consolidated unless normal maintenance proves a split is needed. - -## Recommended First Cut - -Historical first cut, now complete in the current branch, started with Milestone P0: - -1. Lock the report contract first: schema version, workspace identity, report scope, coverage keys, time accounting semantics, token source/cache coverage, and redaction policy. -2. Add shared `SessionUsageReport` aggregation using existing persisted data. -3. Add `/usage` in CLI and Desktop, with explicit idle/active behavior. -4. Render Desktop output as durable Markdown, stored as non-model-visible local command output. -5. Add explicit messaging that recorded runtime spans are approximate and may differ from pure model streaming throughput. -6. Keep one compact Chat-bottom entry point until live runtime values have a separate, reliable span contract and placement review. - -This delivered immediate user value while avoiding risky runtime rewrites. The -remaining work is now concentrated in P2 hardening, not the P0/P1 report -contract. diff --git a/src/apps/cli/src/dispatch/mod.rs b/src/apps/cli/src/dispatch/mod.rs index fb003f63be..53b5ffa122 100644 --- a/src/apps/cli/src/dispatch/mod.rs +++ b/src/apps/cli/src/dispatch/mod.rs @@ -421,6 +421,7 @@ async fn query_in_store( remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: false, }, ) diff --git a/src/apps/cli/src/management.rs b/src/apps/cli/src/management.rs index 506483bb0b..8a07d8c086 100644 --- a/src/apps/cli/src/management.rs +++ b/src/apps/cli/src/management.rs @@ -572,6 +572,7 @@ pub(crate) async fn print_usage_report(session_id: Option<&str>) -> Result<()> { remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: true, }) .await diff --git a/src/apps/cli/src/modes/chat/selection.rs b/src/apps/cli/src/modes/chat/selection.rs index d9cccac4d7..3cdca3ba87 100644 --- a/src/apps/cli/src/modes/chat/selection.rs +++ b/src/apps/cli/src/modes/chat/selection.rs @@ -316,6 +316,7 @@ impl ChatMode { remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: true, }) .await diff --git a/src/apps/cli/src/peer_host/commands/session.rs b/src/apps/cli/src/peer_host/commands/session.rs index 35f58ffe10..f32e3af60d 100644 --- a/src/apps/cli/src/peer_host/commands/session.rs +++ b/src/apps/cli/src/peer_host/commands/session.rs @@ -1128,6 +1128,7 @@ pub(crate) async fn get_session_usage_report( include_hidden_subagents: options.include_hidden_subagents, include_system_prompt_preview: options.include_system_prompt_preview, context_preview_category: options.context_preview_category, + context_estimate: options.context_estimate, }) .await .map_err(|error| { @@ -1147,6 +1148,7 @@ struct UsageReportOptions { include_hidden_subagents: bool, include_system_prompt_preview: bool, context_preview_category: Option, + context_estimate: Option, } /// Reads the Desktop `GetSessionUsageReportRequest` option fields with the @@ -1166,6 +1168,13 @@ fn usage_report_options(request: &Value) -> Result { include_system_prompt_preview: optional_bool(request, "includeSystemPromptPreview") .unwrap_or(false), context_preview_category, + context_estimate: ["contextEstimate", "context_estimate"] + .iter() + .find_map(|key| request.get(*key)) + .filter(|value| !value.is_null()) + .map(|value| serde_json::from_value(value.clone())) + .transpose() + .map_err(|error| format!("Invalid context estimate options: {error}"))?, }) } @@ -1226,6 +1235,7 @@ mod tests { include_hidden_subagents: true, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, } ); let snake = json!({ @@ -1239,6 +1249,7 @@ mod tests { include_hidden_subagents: false, include_system_prompt_preview: true, context_preview_category: Some(UsageContextCategory::ToolDefinitions), + context_estimate: None, } ); // A category a newer controller knows maps to Unknown instead of failing. @@ -1249,6 +1260,19 @@ mod tests { .context_preview_category, Some(UsageContextCategory::Unknown) ); + for key in ["contextEstimate", "context_estimate"] { + let options = super::usage_report_options( + &json!({key: {"includeContents": true, "contextOnly": true}}), + ) + .unwrap(); + assert_eq!( + options.context_estimate, + Some(openbitfun_core_types::UsageContextEstimateRequest { + include_contents: true, + context_only: true, + }) + ); + } assert!(super::usage_report_options(&json!({ "context_preview_category": 3 })).is_err()); } diff --git a/src/apps/desktop/AGENTS.md b/src/apps/desktop/AGENTS.md index 369333630c..86bd5b5193 100644 --- a/src/apps/desktop/AGENTS.md +++ b/src/apps/desktop/AGENTS.md @@ -463,3 +463,7 @@ Use `-- --include-ignored` with `latexmk`, pdfLaTeX, and XeLaTeX on PATH to exercise a multi-file paper build, error diagnostics, and temporary output. The POSIX build test checks the remote command in a local shell. Verify live SSH, container, and peer routing separately. + +For additive on-demand context-estimate requests and legacy payload defaults: +`cargo test --locked -p openbitfun-desktop --no-default-features --lib context_estimate_request_tests`. +This verifies the Desktop boundary, not a live Peer or remote workspace. diff --git a/src/apps/desktop/src/api/session_api.rs b/src/apps/desktop/src/api/session_api.rs index 86c884318a..b3ef554e27 100644 --- a/src/apps/desktop/src/api/session_api.rs +++ b/src/apps/desktop/src/api/session_api.rs @@ -242,6 +242,8 @@ pub struct GetSessionUsageReportRequest { pub include_system_prompt_preview: bool, #[serde(default, skip_serializing_if = "Option::is_none")] pub context_preview_category: Option, + #[serde(default)] + pub context_estimate: Option, } fn default_include_hidden_subagents() -> bool { @@ -567,6 +569,7 @@ pub async fn get_session_usage_report( request.include_hidden_subagents, request.include_system_prompt_preview, request.context_preview_category, + request.context_estimate, ) .await .map_err(|error| { @@ -970,3 +973,25 @@ mod catalog_request_tests { assert_eq!(serde_json::to_value(request).unwrap(), current); } } + +#[cfg(test)] +mod context_estimate_request_tests { + use super::*; + + #[test] + fn current_context_estimate_preserves_legacy_request_defaults() { + let legacy = serde_json::json!({"session_id": "session-1", "workspace_id": "workspace-1"}); + let request: GetSessionUsageReportRequest = serde_json::from_value(legacy.clone()).unwrap(); + assert!(request.context_estimate.is_none()); + assert!(request.include_hidden_subagents); + let mut current = legacy; + current["context_estimate"] = + serde_json::json!({"includeContents": true, "contextOnly": true}); + let request: GetSessionUsageReportRequest = serde_json::from_value(current).unwrap(); + let restored: GetSessionUsageReportRequest = + serde_json::from_value(serde_json::to_value(request).unwrap()).unwrap(); + let options = restored.context_estimate.unwrap(); + assert!(options.include_contents); + assert!(options.context_only); + } +} diff --git a/src/apps/desktop/src/runtime/session_application.rs b/src/apps/desktop/src/runtime/session_application.rs index 7b64ff54a8..8ba66e6565 100644 --- a/src/apps/desktop/src/runtime/session_application.rs +++ b/src/apps/desktop/src/runtime/session_application.rs @@ -606,6 +606,7 @@ impl DesktopSessionApplication { include_hidden_subagents: bool, include_system_prompt_preview: bool, context_preview_category: Option, + context_estimate: Option, ) -> DesktopSessionApplicationResult { let scope = self.resolved_scope(request).await?; let storage_path = self.storage_path(&scope); @@ -620,6 +621,7 @@ impl DesktopSessionApplication { include_hidden_subagents, include_system_prompt_preview, context_preview_category, + context_estimate, }) .await .map_err(|error| DesktopSessionApplicationError::Runtime(error.into_message()))?; diff --git a/src/crates/assembly/core/src/agentic/coordination/coordinator.rs b/src/crates/assembly/core/src/agentic/coordination/coordinator.rs index 4db21d58cb..63d7561e23 100644 --- a/src/crates/assembly/core/src/agentic/coordination/coordinator.rs +++ b/src/crates/assembly/core/src/agentic/coordination/coordinator.rs @@ -2861,6 +2861,48 @@ Update the persona files and delete BOOTSTRAP.md as soon as bootstrap is complet .await } + /// Resolve prompt sources after the caller has released the session read permit. + pub(crate) async fn estimate_session_context( + &self, + inputs: crate::agentic::session::session_manager::ContextEstimateInputs, + options: &openbitfun_core_types::UsageContextEstimateRequest, + ) -> OpenBitFunResult { + let workspace = Self::build_workspace_binding(&inputs.session.config) + .await + .ok_or_else(|| { + OpenBitFunError::validation( + "Session workspace is unavailable for context inspection", + ) + })?; + let services = Self::build_workspace_services(&Some(workspace.clone())).await?; + let restrictions = runtime_tool_restrictions_for_session_lifetime( + miniapp_agent_run_tool_restrictions( + inputs.turn_metadata.as_ref(), + inputs.session.created_by.as_deref(), + ), + self.session_manager + .is_transient_session(&inputs.session.session_id), + ); + let mut context_vars = HashMap::new(); + copy_turn_control_context(&mut context_vars, inputs.turn_metadata.as_ref()); + Self::copy_output_schema_context(&mut context_vars, inputs.turn_metadata.as_ref()); + if metadata_bool(inputs.turn_metadata.as_ref(), "acp_transport") == Some(true) { + context_vars.insert("acp_transport".into(), "true".into()); + } + self.execution_engine + .estimate_current_context( + inputs, + workspace, + services, + self.terminal_port.get(), + self.remote_exec_port.get(), + restrictions, + context_vars, + options, + ) + .await + } + pub async fn retry_model_pool_member(&self, model_id: &str) -> OpenBitFunResult<()> { self.execution_engine .model_pools diff --git a/src/crates/assembly/core/src/agentic/execution/AGENTS.md b/src/crates/assembly/core/src/agentic/execution/AGENTS.md index 0c680841b6..4787a5b4c9 100644 --- a/src/crates/assembly/core/src/agentic/execution/AGENTS.md +++ b/src/crates/assembly/core/src/agentic/execution/AGENTS.md @@ -432,3 +432,13 @@ cargo test --locked -p openbitfun-core --no-default-features --features agent-ru This delayed loopback HTTP fixture separates response headers, metadata and model output. It does not exercise a real provider or remote host. + +For on-demand context composition, compression/fork recovery without display +snapshots, and request-ledger independence: + +```bash +cargo test --locked -p openbitfun-core --no-default-features --features agent-runtime,git --lib -- current_context_estimate context_composition account_attempt_ledger_keeps_failed_usage_actual_accounts_and_idempotent_events +``` + +These local fixtures cover read-only persistence and typed estimates, not live +remote workspace, Peer, relay, or detached transports. diff --git a/src/crates/assembly/core/src/agentic/execution/execution_engine.rs b/src/crates/assembly/core/src/agentic/execution/execution_engine.rs index f8ea8bf67d..6c6b9f5be6 100644 --- a/src/crates/assembly/core/src/agentic/execution/execution_engine.rs +++ b/src/crates/assembly/core/src/agentic/execution/execution_engine.rs @@ -2,6 +2,8 @@ //! //! Executes complete dialog turns, managing loops of multiple model rounds +mod context_estimate; + use super::context_budget::CompressionTriggerBudget; use super::model_exchange_trace::{ prepare_model_exchange_trace_for_workspace, ModelExchangeTraceOperation, @@ -544,7 +546,6 @@ struct TurnPromptScaffoldInput<'a> { } struct FinalizeRoundInput<'a> { - prompt_reminders: &'a PrependedPromptReminders, permission_constraints: openbitfun_runtime_ports::PermissionConstraintLayer, context_window: usize, tool_definitions: Option>, @@ -2167,54 +2168,6 @@ impl ExecutionEngine { ) } - async fn persist_usage_context_preview( - &self, - context: &ExecutionContext, - messages: &[Message], - reminders: &PrependedPromptReminders, - tools: Option<&[ToolDefinition]>, - ) { - let Some(storage_path) = self - .session_manager - .effective_session_storage_path(&context.session_id) - .await - else { - return; - }; - let result = async { - let contents = super::context_composition::context_preview_contents( - messages.iter().filter(|message| { - !Self::skip_message_for_model_send(message) - && !Self::is_stale_interrupted_continue(message, &context.dialog_turn_id) - }), - reminders, - tools, - ) - .map_err(|error| OpenBitFunError::parse(error.to_string()))?; - self.session_manager - .persistence_manager() - .save_usage_context_snapshot( - &storage_path, - &crate::agentic::persistence::manager::SessionUsageContextSnapshot { - schema_version: 1, - session_id: context.session_id.clone(), - turn_id: context.dialog_turn_id.clone(), - turn_index: context.turn_index, - captured_at: chrono::Utc::now().timestamp_millis().max(0) as u64, - contents, - }, - ) - .await - } - .await; - if let Err(error) = result { - warn!( - "Failed to persist usage context preview: session_id={}, error={}", - context.session_id, error - ); - } - } - /// True if this message would contribute at least one image to the model (before pruning). fn message_bears_images(msg: &Message) -> bool { if Self::skip_message_for_model_send(msg) { @@ -2317,13 +2270,6 @@ impl ExecutionEngine { .await?; final_ai_messages.push(AIMessage::user(render_system_reminder(input.reminder_text))); final_ai_messages.push(AIMessage::user(Self::FINALIZE_USER_FOLLOWUP.to_string())); - self.persist_usage_context_preview( - input.context, - input.messages, - input.prompt_reminders, - input.tool_definitions.as_deref(), - ) - .await; let model_exchange_trace_dir = self .session_manager @@ -2337,23 +2283,6 @@ impl ExecutionEngine { ) .await; let round_context = RoundContext { - context_composition: Some({ - let mut composition = Self::context_composition_for_send( - input.messages, - input.prompt_reminders, - input.tool_definitions.as_deref(), - input.context_window, - &input.context.dialog_turn_id, - input.primary_model_facts.supports_image_inputs, - ); - composition.conversation += TokenCounter::estimate_message_tokens(&AIMessage::user( - render_system_reminder(input.reminder_text), - )) as u64 - + TokenCounter::estimate_message_tokens(&AIMessage::user( - Self::FINALIZE_USER_FOLLOWUP.to_string(), - )) as u64; - composition - }), session_id: input.context.session_id.clone(), subagent_parent_info: input.context.subagent_parent_info.clone(), permission_delegation: input.context.permission_delegation.clone(), @@ -4706,7 +4635,6 @@ impl ExecutionEngine { .persistent_model_exchange_trace_dir(&context.session_id) .await; let mut round_context = RoundContext { - context_composition: None, session_id: context.session_id.clone(), subagent_parent_info: context.subagent_parent_info.clone(), permission_delegation: context.permission_delegation.clone(), @@ -4793,14 +4721,6 @@ impl ExecutionEngine { &model_fingerprint, selected_pool.is_some(), ); - round_context.context_composition = Some(Self::context_composition_for_send( - &request_messages, - &turn_prompt_scaffold.prepended_prompt_reminders, - tool_definitions.as_deref(), - context_window, - &context.dialog_turn_id, - primary_supports_image_understanding, - )); let ai_messages = Self::build_ai_messages_for_send( &request_messages, &ai_client.config.format, @@ -4812,14 +4732,6 @@ impl ExecutionEngine { ) .await?; - self.persist_usage_context_preview( - &context, - &request_messages, - &turn_prompt_scaffold.prepended_prompt_reminders, - tool_definitions.as_deref(), - ) - .await; - let round_lifecycle = active_round_lifecycle.get_or_insert_with(ModelRoundLifecycle::new); round_lifecycle.routing = routing_decision; @@ -5648,7 +5560,6 @@ impl ExecutionEngine { ); let final_round_result = self .run_finalize_round(FinalizeRoundInput { - prompt_reminders: &turn_prompt_scaffold.prepended_prompt_reminders, permission_constraints: tool_policy.permission_constraints.clone(), ai_client: ai_client.clone(), account_client_source: account_client_source.clone(), @@ -5683,7 +5594,6 @@ impl ExecutionEngine { ); let retry_result = self .run_finalize_round(FinalizeRoundInput { - prompt_reminders: &turn_prompt_scaffold.prepended_prompt_reminders, permission_constraints: tool_policy.permission_constraints.clone(), ai_client: ai_client.clone(), account_client_source: account_client_source.clone(), diff --git a/src/crates/assembly/core/src/agentic/execution/execution_engine/context_estimate.rs b/src/crates/assembly/core/src/agentic/execution/execution_engine/context_estimate.rs new file mode 100644 index 0000000000..af0cc006b5 --- /dev/null +++ b/src/crates/assembly/core/src/agentic/execution/execution_engine/context_estimate.rs @@ -0,0 +1,383 @@ +//! On-demand inspection uses the request projection, without model execution or writes. +use super::*; +use crate::agentic::session::session_manager::ContextEstimateInputs; +use openbitfun_core_types::{UsageContextEstimate, UsageContextEstimateRequest}; + +impl ExecutionEngine { + #[allow(clippy::too_many_arguments)] + pub(crate) async fn estimate_current_context( + &self, + inputs: ContextEstimateInputs, + workspace: crate::agentic::WorkspaceBinding, + workspace_services: Option, + terminal_port: Option<&Arc>, + remote_exec_port: Option<&Arc>, + restrictions: ToolRuntimeRestrictions, + mut vars: HashMap, + options: &UsageContextEstimateRequest, + ) -> OpenBitFunResult { + let session = &inputs.session; + if session.config.agent_route_owner != openbitfun_core_types::SessionAgentRouteOwner::Local + || session.agent_type.starts_with("acp:") + { + return Err(OpenBitFunError::NotImplemented( + "The external agent does not expose its current model context".into(), + )); + } + if session.kind != openbitfun_core_types::SessionKind::Standard { + return Err(OpenBitFunError::NotImplemented( + "Delegated session context requires its execution-scoped tool restrictions".into(), + )); + } + let registry = get_agent_registry(); + // Match the remote-aware registry path; never discover controller-local project agents. + registry + .load_custom_agents( + (!workspace.is_remote()) + .then_some(workspace.workspace_id.as_deref()) + .flatten(), + ) + .await; + let agent = registry + .get_agent(&session.agent_type, workspace.workspace_id.as_deref()) + .ok_or_else(|| { + OpenBitFunError::NotFound(format!("Agent not found: {}", session.agent_type)) + })?; + let ai_config = SessionManager::load_ai_config_for_session(&session.config) + .await? + .ok_or_else(|| { + OpenBitFunError::service( + "Model configuration is unavailable for context inspection", + ) + })?; + let (model_id, pool_window) = + if session.config.model_binding_policy == SessionModelBindingPolicy::Pool { + let pool_id = session.config.model_pool_id.as_deref().ok_or_else(|| { + OpenBitFunError::validation("Pool session has no pool selection") + })?; + // preview does not reserve a member or advance the pool cursor. + let (id, _) = self + .model_pools + .preview( + &session.session_id, + session.config.model_pool_selection_revision, + pool_id, + session.config.enable_tools, + ImageInputRequirement::None, + ) + .await?; + ( + id, + Some( + self.model_pools + .context_budget(pool_id) + .await? + .context_window as usize, + ), + ) + } else { + let (id, _) = self + .resolve_model_id_for_turn( + session, + &session.agent_type, + Some(&workspace), + inputs.turn_index, + None, + None, + ImageInputRequirement::None, + ) + .await?; + (id, None) + }; + let model = ai_config + .models + .iter() + .find(|model| model.id == model_id && model.enabled) + .ok_or_else(|| OpenBitFunError::NotFound(format!("Model not found: {model_id}")))?; + // Resolve default limits with the same pure conversion used by the client factory. + // Do not create a client, resolve credentials, or contact a model provider. + let model_config = crate::util::types::AIConfig::try_from(model.clone()) + .map_err(|error| OpenBitFunError::validation(error.to_string()))?; + let supports_images = model + .capabilities + .contains(&ModelCapability::ImageUnderstanding) + || matches!(model.category, ModelCategory::Multimodal); + let model_facts = PrimaryModelFacts::new( + &model_id, + &model_config.model, + &model_config.format, + supports_images, + ); + let context_window = pool_window.unwrap_or( + (model_config.context_window as usize).min(session.config.max_context_tokens), + ); + let policy = registry + .get_agent_tool_policy(&session.agent_type, workspace.workspace_id.as_deref()) + .await; + vars.extend([ + ( + "enable_deferred_tool_loading".to_string(), + ai_config.enable_deferred_tool_loading.to_string(), + ), + ("turn_index".to_string(), inputs.turn_index.to_string()), + ]); + if session.config.tool_presentation_mode.is_ptc() { + vars.insert("tool_presentation_mode".into(), "ptc".into()); + vars.insert( + "ptc_language".into(), + session + .config + .tool_presentation_mode + .language() + .as_str() + .into(), + ); + } + + let tool_context = tool_context_runtime::build_tool_description_context( + &session.agent_type, + Some(&workspace), + workspace_services.as_ref(), + Some(&session.session_id), + terminal_port, + remote_exec_port, + Some(&model_facts), + &vars, + &restrictions, + ); + let manifest = if session.config.enable_tools { + Some( + resolve_tool_manifest( + &policy.allowed_tools, + &policy.exposure_overrides, + &tool_context, + ) + .await, + ) + } else { + None + }; + let current_sections = match &manifest { + Some(manifest) => Self::build_tool_listing_sections(manifest, &tool_context).await, + None => ToolListingSections::default(), + }; + let needs = manifest + .as_ref() + .map(|manifest| { + RuntimeContextNeeds::from_tool_names(manifest.allowed_tool_names.iter()) + }) + .unwrap_or_default(); + let user_policy = agent.user_context_policy(); + let prompt_context = build_prompt_context_for_workspace( + &workspace, + workspace.workspace_id.as_deref(), + &session.session_id, + Some(model_config.model.clone()), + Some(supports_images), + current_sections.clone(), + needs, + &user_policy, + ) + .await + .ok_or_else(|| OpenBitFunError::service("Prompt context is unavailable"))?; + let now = chrono::Utc::now().timestamp_millis().max(0) as u64; + let mut system_identity = agent.system_prompt_cache_identity(Some(&model_config.model)); + system_identity + .scope_key + .push_str("|genui-profile:disabled"); + let system_prompt = match inputs + .prompt_cache + .system_prompt + .as_ref() + .filter(|cached| cached.is_usable(&system_identity, inputs.cache_ttl, now)) + { + Some(cached) => cached.text.content.clone(), + None => agent.get_system_prompt(Some(&prompt_context)).await?, + }; + let mut user_identity = agent.user_context_cache_identity(); + if let Some(remote) = &prompt_context.remote_execution { + user_identity.scope_key.push_str(&format!( + "|remote:{}", + remote.connection_display_name.replace('|', "/") + )); + } + let user_context = match inputs + .prompt_cache + .user_context + .as_ref() + .filter(|cached| cached.is_usable(&user_identity, inputs.cache_ttl, now)) + { + Some(cached) => Some(cached.text.content.clone()), + None => { + let (content, complete) = Self::build_user_context_for_cache_miss( + Some(&workspace), + workspace_services.as_ref(), + prompt_context.clone(), + &user_policy, + ) + .await; + if !complete { + return Err(OpenBitFunError::service( + "Workspace instructions are unavailable; context estimate was not produced", + )); + } + content + } + }; + let baseline = inputs + .listing_baseline + .as_ref() + .map(build_skill_agent_tool_listing_sections_from_snapshot); + let (skill_listing, agent_listing) = + skill_agent_listing_reminders(baseline.as_ref().or(Some(¤t_sections))); + let reminders = PrependedPromptReminders { + runtime_context: PromptBuilder::new(prompt_context) + .build_runtime_context_reminder() + .await, + user_context, + skill_listing, + agent_listing, + deferred_tool_listing: deferred_tool_listing_reminder( + ¤t_sections, + baseline.as_ref(), + ), + ptc_tool_calling_guide: current_sections.ptc_tool_calling_guide.clone(), + }; + let mut messages = inputs.messages; + messages.retain(|message| message.role != MessageRole::System); + messages.insert(0, Message::system(system_prompt)); + let tools = manifest + .as_ref() + .map(|manifest| manifest.tool_definitions.as_slice()); + Self::estimate_context_view( + &messages, + &reminders, + tools, + context_window, + &inputs.turn_id, + supports_images, + options.include_contents, + now, + ) + } + + #[allow(clippy::too_many_arguments)] + fn estimate_context_view( + messages: &[Message], + reminders: &PrependedPromptReminders, + tools: Option<&[ToolDefinition]>, + context_window: usize, + turn_id: &str, + supports_images: bool, + include_contents: bool, + captured_at: u64, + ) -> OpenBitFunResult { + let composition = Self::context_composition_for_send( + messages, + reminders, + tools, + context_window, + turn_id, + supports_images, + ); + let contents = if include_contents { + super::super::context_composition::context_preview_contents( + messages.iter().filter(|message| { + !Self::skip_message_for_model_send(message) + && !Self::is_stale_interrupted_continue(message, turn_id) + }), + reminders, + tools, + ) + .map_err(|error| OpenBitFunError::parse(error.to_string()))? + } else { + Default::default() + }; + Ok(UsageContextEstimate { + captured_at, + composition: Some(composition), + contents, + unavailable_reason: None, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn current_context_estimate_tracks_compression_without_request_records() { + let system = Message::system("System instructions".into()); + let old = Message::user("Long conversation content ".repeat(10_000)); + let reminders = PrependedPromptReminders::default(); + let before = ExecutionEngine::estimate_context_view( + &[system.clone(), old], + &reminders, + None, + 128_000, + "turn-1", + false, + true, + 1, + ) + .unwrap(); + let mut summary = Message::user("Compressed conversation".into()); + summary.metadata.semantic_kind = Some(MessageSemanticKind::CompressionSummary); + let after = ExecutionEngine::estimate_context_view( + &[system, summary], + &reminders, + None, + 128_000, + "turn-1", + false, + true, + 2, + ) + .unwrap(); + assert!(before.composition.as_ref().unwrap().conversation > 10_000); + let counts = after.composition.unwrap(); + assert_eq!(counts.conversation, 0); + assert!(counts.summarized_conversation > 0); + assert_eq!(before.contents, after.contents); + } + + #[test] + fn current_context_estimate_preview_is_opt_in_and_matches_numeric_sources() { + let mut rules = Message::user("Current rule".into()); + rules.metadata.internal_reminder_kind = Some(InternalReminderKind::ConditionalInstructions); + let reminders = PrependedPromptReminders::default(); + let numeric = ExecutionEngine::estimate_context_view( + std::slice::from_ref(&rules), + &reminders, + None, + 128_000, + "turn-1", + false, + false, + 1, + ) + .unwrap(); + assert!(numeric.contents.is_empty()); + let preview = ExecutionEngine::estimate_context_view( + &[rules.clone()], + &reminders, + None, + 128_000, + "turn-1", + false, + true, + 1, + ) + .unwrap(); + assert_eq!(numeric.composition, preview.composition); + assert_eq!( + preview.composition.unwrap().rules, + rules.estimate_tokens_with_reasoning(true) as u64 + ); + assert_eq!( + preview.contents[&openbitfun_core_types::UsageContextCategory::Rules], + "Current rule" + ); + } +} diff --git a/src/crates/assembly/core/src/agentic/execution/round_executor.rs b/src/crates/assembly/core/src/agentic/execution/round_executor.rs index b900ca050c..59fe97595e 100644 --- a/src/crates/assembly/core/src/agentic/execution/round_executor.rs +++ b/src/crates/assembly/core/src/agentic/execution/round_executor.rs @@ -1021,7 +1021,7 @@ impl RoundExecutor { .map_err(|error| OpenBitFunError::AIClient(error.to_string()).with_channel_bound_replay())?; let mut usage_context = openbitfun_services_core::token_usage::types::ModelRequestUsageContext { - context_composition: context.context_composition.clone(), + context_composition: None, version: 1, round_id: round_id.clone(), attempt_id: attempt_id.clone(), @@ -2994,7 +2994,6 @@ pub(super) mod tests { pub(in crate::agentic::execution) fn test_round_context() -> RoundContext { RoundContext { - context_composition: None, session_id: "session-1".to_string(), subagent_parent_info: None, permission_delegation: None, @@ -6717,12 +6716,6 @@ pub(super) mod tests { let source = test_account_source(vec![primary.client(), backup.client()], false); let mut context = test_round_context(); context.account_client_source = Some(source); - let composition = openbitfun_core_types::UsageContextBreakdown { - system_prompt: 3, - conversation: 7, - ..Default::default() - }; - context.context_composition = Some(composition.clone()); let mut executor = test_round_executor(); executor.usage_service = Some(service.clone()); let result = executor @@ -6751,7 +6744,7 @@ pub(super) mod tests { .request_context() .unwrap() .context_composition - == Some(composition.clone()))); + .is_none())); let failed = records .iter() .find(|record| record.request_context().unwrap().attempt_index == 1) diff --git a/src/crates/assembly/core/src/agentic/execution/types.rs b/src/crates/assembly/core/src/agentic/execution/types.rs index 0fabeca4be..9ff4c81c29 100644 --- a/src/crates/assembly/core/src/agentic/execution/types.rs +++ b/src/crates/assembly/core/src/agentic/execution/types.rs @@ -56,7 +56,6 @@ pub struct ExecutionContext { /// Round context #[derive(Debug, Clone)] pub struct RoundContext { - pub context_composition: Option, pub session_id: String, pub subagent_parent_info: Option, pub permission_delegation: Option, diff --git a/src/crates/assembly/core/src/agentic/insights/collector.rs b/src/crates/assembly/core/src/agentic/insights/collector.rs index 6d52850c4a..d4482ff7b0 100644 --- a/src/crates/assembly/core/src/agentic/insights/collector.rs +++ b/src/crates/assembly/core/src/agentic/insights/collector.rs @@ -623,6 +623,7 @@ fn compute_active_duration_millis( remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: false, }, turns, diff --git a/src/crates/assembly/core/src/agentic/persistence/manager.rs b/src/crates/assembly/core/src/agentic/persistence/manager.rs index 3466c59930..5dedfbcb21 100644 --- a/src/crates/assembly/core/src/agentic/persistence/manager.rs +++ b/src/crates/assembly/core/src/agentic/persistence/manager.rs @@ -9,6 +9,7 @@ pub(crate) use external_history::external_history_catalog; mod session_catalog; mod turn_window_index; mod usage_context; +#[cfg(test)] pub(crate) use usage_context::SessionUsageContextSnapshot; use crate::agentic::core::{ diff --git a/src/crates/assembly/core/src/agentic/persistence/manager/usage_context.rs b/src/crates/assembly/core/src/agentic/persistence/manager/usage_context.rs index cdf45f430f..041d10b1c5 100644 --- a/src/crates/assembly/core/src/agentic/persistence/manager/usage_context.rs +++ b/src/crates/assembly/core/src/agentic/persistence/manager/usage_context.rs @@ -5,7 +5,8 @@ use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::path::Path; -/// Latest prepared request only. Kept separately from numeric usage records and exports. +/// Legacy prepared-request preview. Read for older report consumers only. +/// Current context inspection is computed on demand and never writes this file. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub(crate) struct SessionUsageContextSnapshot { @@ -19,6 +20,7 @@ pub(crate) struct SessionUsageContextSnapshot { } impl PersistenceManager { + #[cfg(test)] pub(crate) async fn save_usage_context_snapshot( &self, storage_path: &Path, diff --git a/src/crates/assembly/core/src/agentic/session/context_store.rs b/src/crates/assembly/core/src/agentic/session/context_store.rs index 3a1c6b9e95..8da24c3ab2 100644 --- a/src/crates/assembly/core/src/agentic/session/context_store.rs +++ b/src/crates/assembly/core/src/agentic/session/context_store.rs @@ -139,7 +139,6 @@ impl SessionContextStore { debug!("Deleted session context cache: session_id={}", session_id); } - #[cfg(test)] pub(crate) fn has_session(&self, session_id: &str) -> bool { self.session_contexts.contains_key(session_id) } diff --git a/src/crates/assembly/core/src/agentic/session/session_manager.rs b/src/crates/assembly/core/src/agentic/session/session_manager.rs index 7bd3a26eec..131a588e27 100644 --- a/src/crates/assembly/core/src/agentic/session/session_manager.rs +++ b/src/crates/assembly/core/src/agentic/session/session_manager.rs @@ -2,6 +2,9 @@ //! //! Responsible for session CRUD, lifecycle management, and resource association +mod context_estimate; +pub(crate) use context_estimate::ContextEstimateInputs; + use crate::agentic::agents::get_agent_registry; use crate::agentic::context_normalization::normalize_incomplete_tool_calls; use crate::agentic::core::{ @@ -12187,6 +12190,145 @@ mod tests { ) } + #[tokio::test] + async fn current_context_estimate_reads_compression_forks_and_legacy_snapshots_without_writes() + { + use openbitfun_services_core::session::{SessionBranchBoundary, SessionBranchRequest}; + let workspace = TestWorkspace::new(); + let persistence = Arc::new(PersistenceManager::new(workspace.path_manager()).unwrap()); + let manager = test_manager(persistence.clone()); + let session = manager + .create_session( + "Estimate".into(), + "Standard".into(), + SessionConfig { + workspace_path: Some(workspace.path().to_string_lossy().into_owned()), + ..Default::default() + }, + ) + .await + .unwrap(); + let storage = manager + .effective_session_storage_path(&session.session_id) + .await + .unwrap(); + let turn = DialogTurnData::new( + "turn-estimate".into(), + 0, + session.session_id.clone(), + crate::service::session::UserMessageData { + id: "input".into(), + content: "Uncompressed history".into(), + timestamp: 1, + metadata: None, + }, + ); + persistence.save_dialog_turn(&storage, &turn).await.unwrap(); + let mut summary = Message::user("Compressed summary".into()); + summary.metadata.semantic_kind = + Some(crate::agentic::core::MessageSemanticKind::CompressionSummary); + persistence + .save_turn_context_snapshot(&storage, &session.session_id, 0, &[summary.clone()]) + .await + .unwrap(); + manager + .context_store + .replace_context(&session.session_id, vec![summary.clone()]); + let live = manager + .read_context_estimate_inputs(&storage, &session.session_id) + .await + .unwrap(); + assert_eq!(live.messages.len(), 1); + assert_eq!(live.messages[0].id, summary.id); + + let fork = persistence + .branch_session( + &storage, + &SessionBranchRequest { + source_session_id: session.session_id.clone(), + source_turn_id: turn.turn_id.clone(), + boundary: SessionBranchBoundary::ThroughTurn, + }, + ) + .await + .unwrap(); + assert!(manager.get_session(&fork.session_id).is_none()); + // Corrupt display-only legacy data must not affect a current-context query. + let root = if persistence.is_resolved_sessions_dir(&storage) { + storage.clone() + } else { + persistence.path_manager().project_sessions_dir(&storage) + }; + let dir = root.join(&fork.session_id); + std::fs::write( + dir.join("usage-context.json"), + b"legacy unreadable display snapshot", + ) + .unwrap(); + fn files(path: &Path) -> std::collections::BTreeMap> { + let mut result = std::collections::BTreeMap::new(); + for entry in std::fs::read_dir(path).unwrap() { + let path = entry.unwrap().path(); + if path.is_dir() { + result.extend(files(&path)); + } else { + result.insert(path.clone(), std::fs::read(path).unwrap()); + } + } + result + } + let before = files(&dir); + let copied = manager + .read_context_estimate_inputs(&storage, &fork.session_id) + .await + .unwrap(); + assert_eq!(copied.messages.len(), 1); + assert_eq!(copied.messages[0].id, summary.id); + assert!( + manager.get_session(&fork.session_id).is_none(), + "inspection must not restore a session" + ); + assert_eq!( + before, + files(&dir), + "inspection must not rewrite persisted data" + ); + // Legacy sessions with no context snapshot fall back to model-visible turns. + persistence + .delete_turn_context_snapshots_from(&storage, &fork.session_id, 0) + .await + .unwrap(); + let legacy = manager + .read_context_estimate_inputs(&storage, &fork.session_id) + .await + .unwrap(); + assert!(legacy + .messages + .iter() + .any(|message| matches!(&message.content, + MessageContent::Text(text) if text.contains("Uncompressed history")))); + // A staged revert must exclude both retained snapshots and in-memory history. + persistence + .save_session_revert_state( + &storage, + &session.session_id, + &SessionRevertState { + schema_version: SESSION_REVERT_SCHEMA_VERSION, + boundary_turn: 0, + original_turn_end: 1, + phase: SessionRevertPhase::Staged, + workspace_checkpoint: Vec::new(), + }, + ) + .await + .unwrap(); + let reverted = manager + .read_context_estimate_inputs(&storage, &session.session_id) + .await + .unwrap(); + assert!(reverted.messages.is_empty()); + } + #[tokio::test] async fn current_context_usage_is_persisted_by_the_session_owner() { let workspace = TestWorkspace::new(); diff --git a/src/crates/assembly/core/src/agentic/session/session_manager/context_estimate.rs b/src/crates/assembly/core/src/agentic/session/session_manager/context_estimate.rs new file mode 100644 index 0000000000..46fdc31e6a --- /dev/null +++ b/src/crates/assembly/core/src/agentic/session/session_manager/context_estimate.rs @@ -0,0 +1,116 @@ +//! Read existing runtime/recovery facts without restoring or mutating a session. +use super::*; + +pub(crate) struct ContextEstimateInputs { + pub session: Session, + pub messages: Vec, + pub prompt_cache: SessionPromptCache, + pub cache_ttl: Option, + pub listing_baseline: Option, + pub turn_id: String, + pub turn_metadata: Option, + pub turn_index: usize, +} + +impl SessionManager { + /// Caller holds the session mutation permit and has validated storage ownership. + pub(crate) async fn read_context_estimate_inputs( + &self, + storage: &Path, + session_id: &str, + ) -> OpenBitFunResult { + let persistence = &self.persistence_manager; + let session = match self.get_session(session_id) { + Some(session) => session, + None => persistence.load_session_header(storage, session_id).await?, + }; + let boundary = persistence + .load_session_revert_state(storage, session_id) + .await? + .map(|state| state.boundary_turn); + let mut turns = persistence.load_session_turns(storage, session_id).await?; + turns.retain(|turn| boundary.is_none_or(|end| turn.turn_index < end)); + let turn_id = turns + .last() + .map(|turn| turn.turn_id.clone()) + .unwrap_or_default(); + let turn_metadata = turns + .last() + .and_then(|turn| turn.user_message.metadata.clone()); + let turn_index = turns.last().map(|turn| turn.turn_index).unwrap_or(0); + let messages = if boundary.is_none() && self.context_store.has_session(session_id) { + self.context_store.get_context_messages(session_id) + } else { + let snapshot = persistence + .load_latest_turn_context_snapshot_before( + storage, + session_id, + boundary.unwrap_or(usize::MAX), + ) + .await?; + match snapshot { + Some((index, mut messages)) => { + let metadata = persistence + .load_session_metadata(storage, session_id) + .await?; + if Self::listing_baseline_rebuild_turn_index_from_metadata(metadata.as_ref()) + .is_some_and(|cutoff| index < cutoff) + { + messages = Self::strip_listing_diff_internal_reminders(messages).0; + } + let delta: Vec<_> = turns + .iter() + .filter(|turn| turn.turn_index > index) + .cloned() + .collect(); + messages.extend(Self::build_messages_from_turns(&delta)); + messages + } + None => Self::build_messages_from_turns(&turns), + } + }; + // Do not run cache restore/expiry cleanup: inspection must not write files. + let prompt_cache = match self.prompt_cache_store.get_cache(session_id) { + Some(cache) => cache, + None => persistence + .load_prompt_cache(storage, session_id) + .await? + .unwrap_or_default(), + }; + let override_snapshot = match self + .skill_agent_baseline_override_snapshot_store + .get(session_id) + { + Some(snapshot) => Some(snapshot.value().clone()), + None => { + persistence + .load_skill_agent_baseline_override_snapshot(storage, session_id) + .await? + } + }; + let listing_baseline = match override_snapshot { + Some(snapshot) => Some(snapshot), + None => match self + .turn_skill_agent_snapshot_store + .get_snapshot(session_id, 0) + { + Some(snapshot) => Some(snapshot), + None => { + persistence + .load_turn_skill_agent_snapshot(storage, session_id, 0) + .await? + } + }, + }; + Ok(ContextEstimateInputs { + session, + messages, + prompt_cache, + listing_baseline, + turn_id, + turn_metadata, + turn_index, + cache_ttl: self.config.prompt_cache_policy.cache_ttl, + }) + } +} diff --git a/src/crates/assembly/core/src/product_runtime.rs b/src/crates/assembly/core/src/product_runtime.rs index 32dece76e0..1886540f2c 100644 --- a/src/crates/assembly/core/src/product_runtime.rs +++ b/src/crates/assembly/core/src/product_runtime.rs @@ -443,6 +443,25 @@ async fn begin_consistent_persisted_session_read( storage_path: &Path, session_id: &str, expected_workspace: Option<&WorkspaceSessionIdentity>, +) -> OpenBitFunResult { + begin_session_read( + coordinator, + persistence, + storage_path, + session_id, + expected_workspace, + true, + ) + .await +} + +async fn begin_session_read( + coordinator: &ConversationCoordinator, + persistence: &PersistenceManager, + storage_path: &Path, + session_id: &str, + expected_workspace: Option<&WorkspaceSessionIdentity>, + recover_unfinished_revert: bool, ) -> OpenBitFunResult { validate_persisted_session_id(session_id)?; let session_manager = coordinator.get_session_manager(); @@ -467,7 +486,7 @@ async fn begin_consistent_persisted_session_read( .await? { if state.phase != crate::agentic::session::revert::SessionRevertPhase::Staged { - if session_manager.get_session(session_id).is_none() { + if !recover_unfinished_revert || session_manager.get_session(session_id).is_none() { return Err(OpenBitFunError::OutcomeUnknown( "Session facts are unavailable until the unfinished undo transition is restored" .to_string(), @@ -2981,23 +3000,72 @@ impl AgentSessionUsagePort for CoreSessionOperationsPort { request.remote_ssh_host.clone(), ) .await?; - let _read = begin_consistent_persisted_session_read( + let _read = begin_session_read( self.coordinator.as_ref(), self.persistence.as_ref(), &storage_path, &request.session_id, None, + request.context_estimate.is_none(), ) .await .map_err(runtime_port_error)?; - generate_core_session_usage_report( - self.persistence.as_ref(), - self.token_usage_service.as_ref(), - &storage_path, - request, - ) - .await - .map_err(runtime_port_error) + let estimate_options = request.context_estimate.clone(); + let mut report = if estimate_options + .as_ref() + .is_some_and(|options| options.context_only) + { + SessionUsageReport::partial_unavailable( + &request.session_id, + chrono::Utc::now().timestamp_millis(), + ) + } else { + generate_core_session_usage_report( + self.persistence.as_ref(), + self.token_usage_service.as_ref(), + &storage_path, + request.clone(), + ) + .await + .map_err(runtime_port_error)? + }; + let inputs = if estimate_options.is_some() { + Some( + self.coordinator + .get_session_manager() + .read_context_estimate_inputs(&storage_path, &request.session_id) + .await, + ) + } else { + None + }; + // Prompt discovery can involve a remote workspace. Do not hold a session + // mutation permit across that IO or block the running agent for a viewer. + drop(_read); + if let Some(options) = estimate_options { + let estimate = match inputs.expect("requested context inputs") { + Ok(inputs) => { + self.coordinator + .estimate_session_context(inputs, &options) + .await + } + Err(error) => Err(error), + }; + report.context_estimate = Some(match estimate { + Ok(estimate) => estimate, + Err(error) => openbitfun_core_types::UsageContextEstimate { + captured_at: chrono::Utc::now().timestamp_millis().max(0) as u64, + composition: None, + contents: Default::default(), + unavailable_reason: Some(error.to_string()), + }, + }); + report.privacy.prompt_content_included |= report + .context_estimate + .as_ref() + .is_some_and(|estimate| !estimate.contents.is_empty()); + } + Ok(report) } } @@ -5202,6 +5270,7 @@ mod tests { remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: false, }) .await @@ -5231,6 +5300,38 @@ mod tests { .expect("usage task") .expect("usage after staged marker"); assert_eq!(usage.scope.turn_count, 1); + + // Context inspection must not repair an interrupted undo, even for a loaded session. + assert!(session_manager.get_session(session_id).is_some()); + let mut interrupted = persistence + .load_session_revert_state(&storage_path, session_id) + .await + .unwrap() + .unwrap(); + interrupted.phase = crate::agentic::session::revert::SessionRevertPhase::Applying; + persistence + .save_session_revert_state(&storage_path, session_id, &interrupted) + .await + .unwrap(); + let error = super::begin_session_read( + coordinator.as_ref(), + persistence.as_ref(), + &storage_path, + session_id, + None, + false, + ) + .await + .err() + .expect("inspection does not restore undo"); + assert!(matches!(error, OpenBitFunError::OutcomeUnknown(_))); + assert_eq!( + persistence + .load_session_revert_state(&storage_path, session_id) + .await + .unwrap(), + Some(interrupted) + ); } #[tokio::test] @@ -5254,6 +5355,7 @@ mod tests { remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: false, }, ) @@ -5320,6 +5422,7 @@ mod tests { remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: false, }, ) diff --git a/src/crates/assembly/core/src/service/session_usage/service.rs b/src/crates/assembly/core/src/service/session_usage/service.rs index 35d670776f..5d4fa090df 100644 --- a/src/crates/assembly/core/src/service/session_usage/service.rs +++ b/src/crates/assembly/core/src/service/session_usage/service.rs @@ -2970,6 +2970,7 @@ mod tests { remote_ssh_host: remote_connection_id.map(|_| "host.example".to_string()), include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: true, } } diff --git a/src/crates/contracts/core-types/src/session_usage.rs b/src/crates/contracts/core-types/src/session_usage.rs index 4fa1b56b2b..836440880e 100644 --- a/src/crates/contracts/core-types/src/session_usage.rs +++ b/src/crates/contracts/core-types/src/session_usage.rs @@ -30,6 +30,9 @@ pub struct SessionUsageReport { /// Estimated composition of the latest recorded model request, not cumulative usage. #[serde(default, skip_serializing_if = "Option::is_none")] pub context: Option, + /// Opt-in current-context estimate. Absence identifies older hosts. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub context_estimate: Option, /// Latest cached system message, returned only for an explicit preview request. #[serde(default, skip_serializing_if = "Option::is_none")] pub system_prompt_preview: Option, @@ -77,8 +80,32 @@ pub struct UsageContextPreview { pub captured_at: u64, } -/// Numeric-only prompt estimates captured by the executing runtime. -/// Missing snapshots (including legacy hosts) stay absent. +/// Read-only inspection options; preview text requires explicit opt-in. +#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct UsageContextEstimateRequest { + #[serde(default)] + pub include_contents: bool, + /// Skip cumulative accounting when opening the composer overview. + #[serde(default)] + pub context_only: bool, +} + +/// A single query's view of the current effective context, never persisted. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct UsageContextEstimate { + pub captured_at: u64, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub composition: Option, + #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")] + pub contents: std::collections::BTreeMap, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub unavailable_reason: Option, +} + +/// Numeric-only prompt estimates produced by the executing runtime. +/// Shared by current inspection and legacy request snapshots. #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct UsageContextBreakdown { @@ -474,6 +501,7 @@ impl SessionUsageReport { cache_hit_rate: None, }, context: None, + context_estimate: None, system_prompt_preview: None, context_preview: None, models: vec![], diff --git a/src/crates/contracts/core-types/tests/core_type_contracts/session_usage_contracts.rs b/src/crates/contracts/core-types/tests/core_type_contracts/session_usage_contracts.rs index 2bace19b6c..3e836b61f6 100644 --- a/src/crates/contracts/core-types/tests/core_type_contracts/session_usage_contracts.rs +++ b/src/crates/contracts/core-types/tests/core_type_contracts/session_usage_contracts.rs @@ -37,6 +37,7 @@ fn context_composition_keeps_legacy_reports_readable() { assert!(legacy.get("contextPreview").is_none()); let mut restored: SessionUsageReport = serde_json::from_value(legacy.clone()).unwrap(); assert!(restored.context.is_none()); + assert!(restored.context_estimate.is_none()); assert!(restored.system_prompt_preview.is_none()); assert!(restored.context_preview.is_none()); assert_eq!(serde_json::to_value(&restored).unwrap(), legacy); @@ -87,3 +88,28 @@ fn context_preview_round_trips_with_explicit_category_and_capture_time() { report ); } + +#[test] +fn current_context_estimate_is_additive_and_preview_is_optional() { + let mut report = SessionUsageReport::partial_unavailable("session-1", 42); + assert!(serde_json::to_value(&report) + .unwrap() + .get("contextEstimate") + .is_none()); + report.context_estimate = Some(openbitfun_core_types::UsageContextEstimate { + captured_at: 42, + composition: Some(openbitfun_core_types::UsageContextBreakdown { + conversation: 900, + ..Default::default() + }), + contents: Default::default(), + unavailable_reason: None, + }); + let value = serde_json::to_value(&report).unwrap(); + assert_eq!(value["contextEstimate"]["composition"]["conversation"], 900); + assert!(value["contextEstimate"].get("contents").is_none()); + assert_eq!( + serde_json::from_value::(value).unwrap(), + report + ); +} diff --git a/src/crates/contracts/runtime-ports/src/agent_api.rs b/src/crates/contracts/runtime-ports/src/agent_api.rs index a3a310fa9a..4c7ff5f844 100644 --- a/src/crates/contracts/runtime-ports/src/agent_api.rs +++ b/src/crates/contracts/runtime-ports/src/agent_api.rs @@ -489,6 +489,8 @@ pub struct AgentSessionUsageRequest { pub include_system_prompt_preview: bool, #[serde(default, skip_serializing_if = "Option::is_none")] pub context_preview_category: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub context_estimate: Option, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -3617,6 +3619,18 @@ mod tests { serde_json::from_value::(encoded).unwrap(), request ); + assert!(request.context_estimate.is_none()); + request.context_estimate = Some(openbitfun_core_types::UsageContextEstimateRequest { + include_contents: true, + context_only: true, + }); + let current = serde_json::to_value(&request).unwrap(); + assert_eq!(current["contextEstimate"]["includeContents"], true); + assert_eq!(current["contextEstimate"]["contextOnly"], true); + assert_eq!( + serde_json::from_value::(current).unwrap(), + request + ); request.include_system_prompt_preview = true; request.context_preview_category = Some(openbitfun_core_types::UsageContextCategory::McpTools); diff --git a/src/crates/execution/agent-runtime/tests/agent_session_contracts/session_operation_ports.rs b/src/crates/execution/agent-runtime/tests/agent_session_contracts/session_operation_ports.rs index f26ddcd252..39abd6bd6e 100644 --- a/src/crates/execution/agent-runtime/tests/agent_session_contracts/session_operation_ports.rs +++ b/src/crates/execution/agent-runtime/tests/agent_session_contracts/session_operation_ports.rs @@ -152,6 +152,7 @@ async fn runtime_delegates_narrow_session_operations_to_registered_ports() { remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: true, }) .await diff --git a/src/crates/interfaces/app-server/tests/agent_kernel.rs b/src/crates/interfaces/app-server/tests/agent_kernel.rs index a0fa111c69..fb03cfce98 100644 --- a/src/crates/interfaces/app-server/tests/agent_kernel.rs +++ b/src/crates/interfaces/app-server/tests/agent_kernel.rs @@ -957,6 +957,7 @@ async fn phase2_read_models_cover_usage_settlement_references_lineage_and_diff() remote_ssh_host: None, include_system_prompt_preview: false, context_preview_category: None, + context_estimate: None, include_hidden_subagents: false, }, )) diff --git a/src/web-ui/src/flow_chat/components/ChatInput.tsx b/src/web-ui/src/flow_chat/components/ChatInput.tsx index b0eed9d9d2..a73440d855 100644 --- a/src/web-ui/src/flow_chat/components/ChatInput.tsx +++ b/src/web-ui/src/flow_chat/components/ChatInput.tsx @@ -11,7 +11,7 @@ import { expandLargePastes, pickReferencedLargePastes } from '../utils/largePast */ import React, { useRef, useCallback, useEffect, useReducer, useState, useMemo, useSyncExternalStore } from 'react'; -import type { UsageContextBreakdown } from '@/infrastructure/api/service-api/SessionAPI'; +import type { UsageContextEstimate } from '@/infrastructure/api/service-api/SessionAPI'; import type { UsageReportUiParams } from '../session-drivers/types'; import path from 'path-browserify'; import { useTranslation } from 'react-i18next'; @@ -3989,9 +3989,9 @@ export const ChatInput: React.FC = ({ }, [runEffectiveSessionUsageReport]); const loadContextUsageOverview = useCallback(async () => { - let context: UsageContextBreakdown | undefined; + let context: UsageContextEstimate | undefined; await runEffectiveSessionUsageReport(report => { - context = report.context; + context = report.contextEstimate; return true; }); return context; diff --git a/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.test.tsx b/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.test.tsx index 8f73784e22..32ce8ef9dd 100644 --- a/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.test.tsx +++ b/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.test.tsx @@ -434,11 +434,11 @@ describe('ChatInputWorkspaceStrip git refresh behavior', () => { it('opens the overview from the context ring, highlights categories and offers explicit preview and report actions', async () => { const onOpen = vi.fn(); const onCompact = vi.fn(); - const loadContext = vi.fn(async () => ({ + const loadContext = vi.fn(async () => ({ capturedAt: 123, contents: {}, composition: { systemPrompt: 1600, toolDefinitions: 3900, rules: 8300, skills: 1700, mcpTools: 2700, subagentDefinitions: 1100, summarizedConversation: 0, conversation: 114000, contextWindow: 256000, - })); + } })); await act(async () => root.render( { await act(async () => headerActions[0]!.click()); expect(onCompact).toHaveBeenCalledOnce(); expect(document.querySelector('[data-testid="context-usage-popover"]')).toBeNull(); + loadContext.mockResolvedValueOnce({ capturedAt: 456, contents: {}, composition: { + systemPrompt: 1600, toolDefinitions: 3900, rules: 8300, skills: 1700, + mcpTools: 2700, subagentDefinitions: 1100, summarizedConversation: 2000, + conversation: 0, contextWindow: 256000, + } }); await act(async () => trigger.click()); const reopenedOverview = document.querySelector('[data-testid="context-usage-popover"]')!; + expect(loadContext).toHaveBeenCalledTimes(2); + expect(reopenedOverview.querySelector('[data-usage-category="conversation"] dd')?.textContent).toBe('0'); + expect(reopenedOverview.querySelector('[data-usage-category="summarizedConversation"] dd')?.textContent).toBe('2K'); await act(async () => reopenedOverview.querySelector('[data-testid="context-usage-view-report"]')!.click()); expect(onOpen).toHaveBeenCalledOnce(); expect(document.querySelector('[data-testid="context-usage-popover"]')).toBeNull(); @@ -517,7 +525,7 @@ describe('ChatInputWorkspaceStrip git refresh behavior', () => { )); const trigger = container.querySelector('[data-openbitfun-part="usageAction"]')!; await act(async () => trigger.click()); - expect(document.querySelector('[data-testid="context-usage-popover"]')?.textContent).toContain('usage.context.unavailable'); + expect(document.querySelector('[data-testid="context-usage-popover"]')?.textContent).toContain('usage.context.unsupported'); expect(document.querySelector('.context-usage-popover__bar')).toBeNull(); await act(async () => trigger.click()); await act(async () => trigger.click()); @@ -526,10 +534,10 @@ describe('ChatInputWorkspaceStrip git refresh behavior', () => { }); it('keeps legacy category counts without treating the composer default as reported capacity', async () => { - const loadContext = vi.fn(async () => ({ + const loadContext = vi.fn(async () => ({ capturedAt: 123, contents: {}, composition: { systemPrompt: 100, toolDefinitions: 20, rules: 0, skills: 0, mcpTools: 0, subagentDefinitions: 0, summarizedConversation: 40, conversation: 840, - })); + } })); await act(async () => root.render( , diff --git a/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.tsx b/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.tsx index 6750badde8..11d3962386 100644 --- a/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.tsx +++ b/src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.tsx @@ -38,7 +38,7 @@ import { } from './thread-goal/ThreadGoalStripControl'; import { formatCompactTokenCount } from '../utils/tokenUsageDisplay'; import { ContextUsagePopover } from './usage/ContextUsagePopover'; -import type { UsageContextBreakdown } from '@/infrastructure/api/service-api/SessionAPI'; +import type { UsageContextEstimate } from '@/infrastructure/api/service-api/SessionAPI'; import './ChatInputWorkspaceStrip.scss'; export interface ChatInputWorkspaceStripProps { @@ -57,7 +57,7 @@ export interface ChatInputWorkspaceStripProps { sessionKey?: string; anchorRef?: React.RefObject; previewSession?: { sessionId: string; workspaceId?: string }; - loadContext?: () => Promise; + loadContext?: () => Promise; compactDisabled?: boolean; onCompact?: () => void; onOpen: () => void; diff --git a/src/web-ui/src/flow_chat/components/usage/ContextUsagePopover.tsx b/src/web-ui/src/flow_chat/components/usage/ContextUsagePopover.tsx index 3053a84a17..ac4d389dd3 100644 --- a/src/web-ui/src/flow_chat/components/usage/ContextUsagePopover.tsx +++ b/src/web-ui/src/flow_chat/components/usage/ContextUsagePopover.tsx @@ -3,7 +3,7 @@ import { Card, Empty, Icon, IconButton, OverflowText, Tooltip, createOverlayPort import { ChartPie, ExternalLink, Minus, Shrink } from 'lucide-react'; import { useI18n } from '@/infrastructure/i18n'; import { getAppearanceOverlayHost } from '@/infrastructure/appearance/runtime/AppearanceOverlayHost'; -import type { UsageContextBreakdown } from '@/infrastructure/api/service-api/SessionAPI'; +import type { UsageContextBreakdown, UsageContextEstimate } from '@/infrastructure/api/service-api/SessionAPI'; import { useAnchoredPopoverPosition } from '@/shared/utils/useAnchoredPopoverPosition'; import { formatTokenCount, formatUsagePercent } from './usageReportUtils'; import { useUsageSeriesHighlight } from './useUsageSeriesHighlight'; @@ -30,7 +30,7 @@ export function ContextUsagePopover({ triggerRef: RefObject; currentTokens: number; maxTokens: number; - loadContext?: () => Promise; + loadContext?: () => Promise; previewSession?: { sessionId: string; workspaceId?: string }; compactDisabled?: boolean; onCompact?: () => void; @@ -39,10 +39,11 @@ export function ContextUsagePopover({ }) { const { t } = useI18n('flow-chat'); const popoverRef = useRef(null); + const [estimate, setEstimate] = useState(); const { previewLoading, previewContext } = useContextPreview({ - ...previewSession, reportSessionId: previewSession?.sessionId, + ...previewSession, reportSessionId: previewSession?.sessionId, estimate, }); - const [context, setContext] = useState(); + const context = estimate?.composition; const [status, setStatus] = useState<'loading' | 'ready' | 'error' | 'unsupported' | 'updateRequired'>( loadContext ? 'loading' : 'ready', ); @@ -52,7 +53,7 @@ export function ContextUsagePopover({ let current = true; if (loaderRef.current) { void loaderRef.current().then(value => { - if (current) { setContext(value); setStatus('ready'); } + if (current) { setEstimate(value); setStatus(value ? 'ready' : 'unsupported'); } }, (error: unknown) => { if (!current) return; // A peer host without the report is an expected state, not a load failure. @@ -178,7 +179,7 @@ export function ContextUsagePopover({ : status === 'updateRequired' ? t('usage.context.updateRequired') : t('usage.context.unavailable')} - } /> + } description={estimate?.unavailableReason} /> } , diff --git a/src/web-ui/src/flow_chat/components/usage/SessionUsageComponents.test.tsx b/src/web-ui/src/flow_chat/components/usage/SessionUsageComponents.test.tsx index 330c2bfb29..0b26a42e88 100644 --- a/src/web-ui/src/flow_chat/components/usage/SessionUsageComponents.test.tsx +++ b/src/web-ui/src/flow_chat/components/usage/SessionUsageComponents.test.tsx @@ -452,6 +452,7 @@ function usageReport(overrides: Partial = {}): SessionUsageR redactedFields: ['tools', 'files', 'errors'], }, ...overrides, + contextEstimate: overrides.contextEstimate ?? (overrides.context ? { capturedAt: 123, composition: overrides.context } : undefined), }; } @@ -1134,7 +1135,7 @@ describe('Session usage report UI components', () => { act(() => { button.focus(); button.click(); }); expect(button.disabled).toBe(true); expect(promptPreviewMocks.open).toHaveBeenCalledExactlyOnceWith({ - sessionId: 'session-1', reportSessionId: 'session-1', workspaceId: 'workspace-1', title: `usage.context.categories.${category}${category === 'toolDefinitions' ? '' : '.md'}`, category, emptyContent: 'usage.context.previewEmpty', + sessionId: 'session-1', reportSessionId: 'session-1', workspaceId: 'workspace-1', title: `usage.context.categories.${category}${category === 'toolDefinitions' ? '' : '.md'}`, category, emptyContent: 'usage.context.previewEmpty', estimate: undefined, onEstimate: expect.any(Function), }); expect(container.querySelector('.session-usage-panel__donut-segment[data-usage-category="conversation"]')?.hasAttribute('data-usage-muted')).toBe(true); await act(async () => finish('unavailable')); @@ -1520,6 +1521,11 @@ describe('Session usage report UI components', () => { systemPrompt: 0, toolDefinitions: 0, rules: 0, skills: 0, mcpTools: 0, subagentDefinitions: 0, summarizedConversation: 0, conversation: 0, }; + const legacyReport = usageReport({ context: emptyContext }); + delete legacyReport.contextEstimate; + render(); + expect(factValues()).toEqual(['openbitfun', '1.5K', 'Unavailable']); + render(); expect(factValues()).toEqual(['openbitfun', '1.5K', '~0 / Unavailable']); diff --git a/src/web-ui/src/flow_chat/components/usage/SessionUsagePanel.tsx b/src/web-ui/src/flow_chat/components/usage/SessionUsagePanel.tsx index af8e5e77eb..86b8571c08 100644 --- a/src/web-ui/src/flow_chat/components/usage/SessionUsagePanel.tsx +++ b/src/web-ui/src/flow_chat/components/usage/SessionUsagePanel.tsx @@ -2,7 +2,7 @@ import React, { useCallback, useEffect, useState } from 'react'; import { useI18n } from '@/infrastructure/i18n'; import { MarkdownRenderer } from '@/infrastructure/markdown'; import { Empty, Icon, IconButton, LoadingState, PageHeader, ScrollArea, Tooltip } from '@openbitfun/ui'; -import type { SessionUsageReport } from '@/infrastructure/api/service-api/SessionAPI'; +import type { SessionUsageReport, UsageContextEstimate } from '@/infrastructure/api/service-api/SessionAPI'; import { buildSessionUsageExportMarkdown, isUnusedSession, @@ -47,9 +47,12 @@ export const SessionUsagePanel: React.FC = ({ const { t } = useI18n('flow-chat'); const reportTitle = t('usage.panel.report'); const [copied, setCopied] = useState(false); + const [previewEstimate, setPreviewEstimate] = useState(); + useEffect(() => setPreviewEstimate(undefined), [report]); const { previewLoading, previewContext: handlePreviewContext } = useContextPreview({ sessionId: sessionId ?? report?.sessionId, reportSessionId: report?.sessionId, workspaceId: workspaceId ?? report?.workspace.workspaceId, + estimate: previewEstimate, onEstimate: setPreviewEstimate, }); const [redactExportPaths, setRedactExportPaths] = useState(getUsageExportRedactPathsPreference); @@ -142,7 +145,7 @@ export const SessionUsagePanel: React.FC = ({ /> - ); diff --git a/src/web-ui/src/flow_chat/components/usage/SessionUsageSummary.tsx b/src/web-ui/src/flow_chat/components/usage/SessionUsageSummary.tsx index 8a3dd44c92..371824c046 100644 --- a/src/web-ui/src/flow_chat/components/usage/SessionUsageSummary.tsx +++ b/src/web-ui/src/flow_chat/components/usage/SessionUsageSummary.tsx @@ -65,7 +65,7 @@ export function SessionUsageSummary({ report, workspaceName, onPreviewContext, p previewLoading: UsageContextCategory | null; }) { const { t } = useI18n('flow-chat'); - const context = report.context; + const context = report.contextEstimate?.composition; const hasContext = !!context && CONTEXT_CATEGORIES.every(key => isCount(context[key])); const contextTotal = hasContext ? CONTEXT_CATEGORIES.reduce((sum, key) => sum + context[key], 0) : 0; const hasContextTotal = hasContext && Number.isSafeInteger(contextTotal); diff --git a/src/web-ui/src/flow_chat/components/usage/useContextPreview.ts b/src/web-ui/src/flow_chat/components/usage/useContextPreview.ts index 81f49bd4f1..332e1b416f 100644 --- a/src/web-ui/src/flow_chat/components/usage/useContextPreview.ts +++ b/src/web-ui/src/flow_chat/components/usage/useContextPreview.ts @@ -1,12 +1,12 @@ import { useEffect, useRef, useState } from 'react'; import { useI18n } from '@/infrastructure/i18n'; -import type { UsageContextCategory } from '@/infrastructure/api/service-api/SessionAPI'; +import type { UsageContextCategory, UsageContextEstimate } from '@/infrastructure/api/service-api/SessionAPI'; import { openSessionContextPreview } from '../../services/openSessionContextPreview'; import { notificationService } from '@/shared/notification-system'; import { getActiveSurfaceScope, isSurfaceChangedError } from '@/infrastructure/peer-device/deviceSurface'; /** Both usage surfaces open the same session-owned, capability-checked preview. */ -export function useContextPreview(params: { sessionId?: string; reportSessionId?: string; workspaceId?: string }) { +export function useContextPreview(params: { sessionId?: string; reportSessionId?: string; workspaceId?: string; estimate?: UsageContextEstimate; onEstimate?: (estimate: UsageContextEstimate) => void }) { const { t } = useI18n('flow-chat'); const [previewLoading, setPreviewLoading] = useState(null); const requestRef = useRef(null); @@ -27,7 +27,7 @@ export function useContextPreview(params: { sessionId?: string; reportSessionId? const result = await openSessionContextPreview({ sessionId: params.sessionId, reportSessionId: params.reportSessionId, workspaceId: params.workspaceId, title: `${t(`usage.context.categories.${category}`)}${category === 'toolDefinitions' ? '' : '.md'}`, - category, emptyContent: t('usage.context.previewEmpty'), + category, estimate: params.estimate, onEstimate: estimate => { if (isCurrent()) params.onEstimate?.(estimate); }, emptyContent: t('usage.context.previewEmpty'), }); if (isCurrent() && result === 'unavailable') notificationService.warning(t('usage.context.previewUnavailable')); } catch (error) { diff --git a/src/web-ui/src/flow_chat/services/openSessionContextPreview.test.ts b/src/web-ui/src/flow_chat/services/openSessionContextPreview.test.ts index f5d3ccdcc4..3f91e748ab 100644 --- a/src/web-ui/src/flow_chat/services/openSessionContextPreview.test.ts +++ b/src/web-ui/src/flow_chat/services/openSessionContextPreview.test.ts @@ -8,8 +8,8 @@ import { openSessionContextPreview } from './openSessionContextPreview'; const apiMocks = vi.hoisted(() => ({ getSessionUsageReport: vi.fn() })); vi.mock('@/infrastructure/api/service-api/SessionAPI', () => ({ sessionAPI: apiMocks })); -const params = { sessionId: 'preview-session', reportSessionId: 'preview-session', workspaceId: 'workspace-1', title: 'System prompt.md', category: 'systemPrompt' as const, emptyContent: 'No content in the latest request.' }; -const response = () => ({ sessionId: params.sessionId, systemPromptPreview: { content: '# System\n\nExact prompt.\n', cachedAt: 123 } }); +const params = { sessionId: 'preview-session', reportSessionId: 'preview-session', workspaceId: 'workspace-1', title: 'System prompt.md', category: 'systemPrompt' as const, emptyContent: 'No content in the current context.' }; +const response = () => ({ sessionId: params.sessionId, contextEstimate: { contents: { systemPrompt: '# System\n\nExact prompt.\n' }, capturedAt: 123 } }); const tabs = () => useAgentCanvasStore.getState().primaryGroup.tabs; describe('system prompt preview tabs', () => { @@ -28,13 +28,13 @@ describe('system prompt preview tabs', () => { const workspaceId = surface === 'ssh-workspace' ? 'ssh-workspace-id' : params.workspaceId; await expect(openSessionContextPreview({ ...params, workspaceId })).resolves.toBe('opened'); expect(apiMocks.getSessionUsageReport).toHaveBeenCalledExactlyOnceWith({ - sessionId: params.sessionId, workspaceId, includeHiddenSubagents: false, includeSystemPromptPreview: true, contextPreviewCategory: params.category, + sessionId: params.sessionId, workspaceId, includeHiddenSubagents: false, contextEstimate: { includeContents: true, contextOnly: true }, }); expect(tabs()).toHaveLength(1); expect(tabs()[0].content).toMatchObject({ type: 'markdown-editor', title: params.title, - data: { initialContent: response().systemPromptPreview.content, readOnly: true, fileName: 'system-prompt.md' }, - metadata: { sessionId: params.sessionId, cachedAt: 123, resourceScope: { workspaceId } }, + data: { initialContent: response().contextEstimate.contents.systemPrompt, readOnly: true, fileName: 'system-prompt.md' }, + metadata: { sessionId: params.sessionId, capturedAt: 123, resourceScope: { workspaceId } }, }); expect(tabs()[0].content.data.filePath).toBeUndefined(); await openSessionContextPreview({ ...params, workspaceId }); @@ -54,12 +54,12 @@ describe('system prompt preview tabs', () => { }); it.each(['rules', 'skills', 'mcpTools', 'subagentDefinitions'] as const)( - 'opens %s in its own read-only tab without fetching system prompt content', async category => { + 'opens the requested %s from a current-context inspection', async category => { apiMocks.getSessionUsageReport.mockResolvedValue({ sessionId: params.sessionId, - contextPreview: { category, content: `# ${category}\n\nRecorded content.`, capturedAt: 456 } }); + contextEstimate: { contents: { [category]: `# ${category}\n\nRecorded content.` }, capturedAt: 456 } }); await expect(openSessionContextPreview({ ...params, category })).resolves.toBe('opened'); expect(apiMocks.getSessionUsageReport).toHaveBeenLastCalledWith({ - sessionId: params.sessionId, workspaceId: params.workspaceId, includeHiddenSubagents: false, contextPreviewCategory: category, + sessionId: params.sessionId, workspaceId: params.workspaceId, includeHiddenSubagents: false, contextEstimate: { includeContents: true, contextOnly: true }, }); expect(tabs()[0].content.data).toMatchObject({ initialContent: `# ${category}\n\nRecorded content.`, readOnly: true }); expect(tabs()[0].content.data.filePath).toBeUndefined(); @@ -71,9 +71,9 @@ describe('system prompt preview tabs', () => { }, ); - it('prefers the recorded system category over the legacy cache', async () => { + it('uses the current estimate returned by the host', async () => { apiMocks.getSessionUsageReport.mockResolvedValue({ ...response(), - contextPreview: { category: params.category, content: '# Request system content', capturedAt: 456 } }); + contextEstimate: { contents: { systemPrompt: '# Request system content' }, capturedAt: 456 } }); await openSessionContextPreview(params); expect(tabs()[0].content.data.initialContent).toBe('# Request system content'); }); @@ -86,7 +86,7 @@ describe('system prompt preview tabs', () => { }, 'active'); const content = '```json\n[{"name":"Read","description":"Read files","parameters":{"type":"object"}}]\n```'; apiMocks.getSessionUsageReport.mockResolvedValue({ sessionId: params.sessionId, - contextPreview: { category: 'toolDefinitions', content, capturedAt: 456 } }); + contextEstimate: { contents: { toolDefinitions: content }, capturedAt: 456 } }); await expect(openSessionContextPreview({ ...params, title: 'Tool definitions', category: 'toolDefinitions' })).resolves.toBe('opened'); expect(tabs()).toHaveLength(1); expect(tabs()[0].content).toMatchObject({ type: 'tool-definitions', title: 'Tool definitions', data: { content, readOnly: true } }); @@ -96,7 +96,7 @@ describe('system prompt preview tabs', () => { it('distinguishes an empty recorded category from a missing or mismatched preview', async () => { apiMocks.getSessionUsageReport.mockResolvedValue({ sessionId: params.sessionId, - contextPreview: { category: 'rules', content: '', capturedAt: 456 } }); + contextEstimate: { contents: { rules: '' }, capturedAt: 456 } }); await expect(openSessionContextPreview({ ...params, category: 'rules' })).resolves.toBe('opened'); expect(tabs()[0].content.data.initialContent).toBe(params.emptyContent); await expect(openSessionContextPreview({ ...params, category: 'skills' })).resolves.toBe('unavailable'); @@ -143,4 +143,20 @@ describe('system prompt preview tabs', () => { await expect(openSessionContextPreview(params)).rejects.toThrow('Host offline'); expect(tabs()).toHaveLength(0); }); + it('reuses the overview contents without another query after context changes', async () => { + const estimate = response().contextEstimate; + apiMocks.getSessionUsageReport.mockRejectedValue(new Error('must not query again')); + await expect(openSessionContextPreview({ ...params, estimate })).resolves.toBe('opened'); + expect(apiMocks.getSessionUsageReport).not.toHaveBeenCalled(); + expect(tabs()[0].content.data.initialContent).toBe(estimate.contents.systemPrompt); + }); + + it('does not substitute old request snapshots for a current estimate', async () => { + apiMocks.getSessionUsageReport.mockResolvedValue({ sessionId: params.sessionId, + systemPromptPreview: { content: 'old system', cachedAt: 1 }, + contextPreview: { category: 'systemPrompt', content: 'old request', capturedAt: 1 } }); + await expect(openSessionContextPreview(params)).resolves.toBe('unavailable'); + expect(tabs()).toHaveLength(0); + }); + }); diff --git a/src/web-ui/src/flow_chat/services/openSessionContextPreview.ts b/src/web-ui/src/flow_chat/services/openSessionContextPreview.ts index c71ee0bc92..0ccd040df4 100644 --- a/src/web-ui/src/flow_chat/services/openSessionContextPreview.ts +++ b/src/web-ui/src/flow_chat/services/openSessionContextPreview.ts @@ -1,4 +1,4 @@ -import { sessionAPI, type UsageContextCategory } from '@/infrastructure/api/service-api/SessionAPI'; +import { sessionAPI, type UsageContextCategory, type UsageContextEstimate } from '@/infrastructure/api/service-api/SessionAPI'; import { getActiveSurfaceScope } from '@/infrastructure/peer-device/deviceSurface'; import { openAgentCanvasTabForSession, useAgentCanvasStore } from '@/app/components/panels/content-canvas/stores/canvasStore'; import { flowChatStore } from '../store/FlowChatStore'; @@ -21,6 +21,9 @@ export async function openSessionContextPreview(params: { title: string; category: UsageContextCategory; emptyContent: string; + /** Reuse the overview query so numbers and text describe the same instant. */ + estimate?: UsageContextEstimate; + onEstimate?: (estimate: UsageContextEstimate) => void; }): Promise<'opened' | 'unavailable' | 'stale'> { const surface = getActiveSurfaceScope(); const session = flowChatStore.getState().sessions.get(params.sessionId); @@ -29,28 +32,25 @@ export async function openSessionContextPreview(params: { if (!workspaceId || params.sessionId !== params.reportSessionId || resolveSessionDriverId(params.sessionId, session) !== 'local') return 'unavailable'; - let report; - try { - report = await sessionAPI.getSessionUsageReport({ - sessionId: params.sessionId, - workspaceId, - includeHiddenSubagents: false, - contextPreviewCategory: params.category, - ...(params.category === 'systemPrompt' ? { includeSystemPromptPreview: true } : {}), - }); - } catch (error) { - // A peer host without the report (for example an older CLI host) does not - // offer previews either; that is the calm unavailable state, not a failure. - if (classifyPeerHostRefusal(error)) return surface.isCurrent() ? 'unavailable' : 'stale'; - throw error; + let estimate = params.estimate; + if (!estimate) { + try { + const report = await sessionAPI.getSessionUsageReport({ + sessionId: params.sessionId, workspaceId, includeHiddenSubagents: false, + contextEstimate: { includeContents: true, contextOnly: true }, + }); + if (!surface.isCurrent()) return 'stale'; + if (report.sessionId !== params.sessionId) return 'unavailable'; + estimate = report.contextEstimate; + } catch (error) { + if (classifyPeerHostRefusal(error)) return surface.isCurrent() ? 'unavailable' : 'stale'; + throw error; + } } if (!surface.isCurrent()) return 'stale'; - if (report.sessionId !== params.sessionId) return 'unavailable'; - const snapshot = report.contextPreview; - const legacy = !snapshot && params.category === 'systemPrompt' ? report.systemPromptPreview : undefined; - const content = snapshot?.category === params.category && typeof snapshot.content === 'string' - ? snapshot.content : legacy?.content?.trim() ? legacy.content : undefined; - if (content === undefined) return 'unavailable'; + if (estimate) params.onEstimate?.(estimate); + const content = estimate?.contents?.[params.category]; + if (typeof content !== 'string') return 'unavailable'; openAgentCanvasTabForSession(params.sessionId, { type: params.category === 'toolDefinitions' ? 'tool-definitions' : 'markdown-editor', @@ -61,8 +61,7 @@ export async function openSessionContextPreview(params: { duplicateCheckKey: params.category === 'systemPrompt' ? `session-system-prompt-${params.sessionId}` : `session-context-${params.category}-${params.sessionId}`, sessionId: params.sessionId, - capturedAt: snapshot?.capturedAt, - cachedAt: legacy?.cachedAt, + capturedAt: estimate?.capturedAt, resourceScope: { surfaceId: surface.surfaceId, workspaceId }, }, }); diff --git a/src/web-ui/src/flow_chat/services/usageReportService.test.ts b/src/web-ui/src/flow_chat/services/usageReportService.test.ts index e649aac23e..57f19cd587 100644 --- a/src/web-ui/src/flow_chat/services/usageReportService.test.ts +++ b/src/web-ui/src/flow_chat/services/usageReportService.test.ts @@ -224,6 +224,7 @@ describe('runUsageReportCommand', () => { sessionId: 'session-1', workspaceId: 'workspace-1', includeHiddenSubagents: true, + contextEstimate: { includeContents: false, contextOnly: false }, }); }); diff --git a/src/web-ui/src/flow_chat/services/usageReportService.ts b/src/web-ui/src/flow_chat/services/usageReportService.ts index f91f147c4c..b05ca9d381 100644 --- a/src/web-ui/src/flow_chat/services/usageReportService.ts +++ b/src/web-ui/src/flow_chat/services/usageReportService.ts @@ -50,7 +50,7 @@ export interface UsageReportCommandResult { export async function runUsageReportCommand( params: UsageReportCommandParams ): Promise { - // An inline overview only reads the latest recorded request and can be viewed during a turn. + // An inline overview inspects current context without starting a turn. if (params.isProcessing && !params.presentReport) { notificationService.warning(params.busyMessage); return { shown: false, reason: 'busy' }; @@ -82,6 +82,7 @@ export async function runUsageReportCommand( sessionId: params.session.sessionId, workspaceId: requireSessionOwningWorkspaceId(params.session), includeHiddenSubagents: true, + contextEstimate: { includeContents: !!params.presentReport, contextOnly: !!params.presentReport }, }); const report = enrichUsageReportModelIdentity(rawReport, params.session); if (!surface.isCurrent()) return { shown: false }; diff --git a/src/web-ui/src/infrastructure/api/service-api/SessionAPI.test.ts b/src/web-ui/src/infrastructure/api/service-api/SessionAPI.test.ts index 552d050c45..7e46f60c51 100644 --- a/src/web-ui/src/infrastructure/api/service-api/SessionAPI.test.ts +++ b/src/web-ui/src/infrastructure/api/service-api/SessionAPI.test.ts @@ -40,6 +40,16 @@ describe('SessionAPI paged metadata reads', () => { }); }); + it('requests current context explicitly through the existing host transport', async () => { + invokeMock.mockResolvedValue({ contextEstimate: { capturedAt: 1 } }); + await sessionAPI.getSessionUsageReport({ sessionId: 'fork', workspaceId: 'workspace-1', + contextEstimate: { includeContents: true, contextOnly: true } }); + expect(invokeMock).toHaveBeenLastCalledWith('get_session_usage_report', { + request: { session_id: 'fork', workspace_id: 'workspace-1', include_hidden_subagents: true, + context_estimate: { includeContents: true, contextOnly: true } }, + }); + }); + it('does not send a local workspace request to an equal ID on a newly selected peer', async () => { const pending = sessionAPI.listSessionsPage({ workspaceId: 'same-workspace-id', limit: 5 }); activateSurface('peer-with-the-same-workspace'); diff --git a/src/web-ui/src/infrastructure/api/service-api/SessionAPI.ts b/src/web-ui/src/infrastructure/api/service-api/SessionAPI.ts index ac4352f8f9..046bdd4ba6 100644 --- a/src/web-ui/src/infrastructure/api/service-api/SessionAPI.ts +++ b/src/web-ui/src/infrastructure/api/service-api/SessionAPI.ts @@ -166,6 +166,7 @@ export interface SessionUsageReportRequest { includeHiddenSubagents?: boolean; includeSystemPromptPreview?: boolean; contextPreviewCategory?: UsageContextCategory; + contextEstimate?: { includeContents?: boolean; contextOnly?: boolean }; } export type UsageContextCategory = 'systemPrompt' | 'toolDefinitions' | 'rules' | 'skills' | 'mcpTools' | 'subagentDefinitions'; @@ -185,7 +186,16 @@ export interface UsageContextBreakdown { contextWindow?: number; } +/** One read-only inspection of the executing host's current context. */ +export interface UsageContextEstimate { + capturedAt: number; + composition?: UsageContextBreakdown; + contents?: Partial>; + unavailableReason?: string; +} + export interface SessionUsageReport { + contextEstimate?: UsageContextEstimate; context?: UsageContextBreakdown; /** Opt-in view of the latest cached system message, not a historical snapshot. */ systemPromptPreview?: { content: string; cachedAt: number }; @@ -604,6 +614,7 @@ export class SessionAPI { ...await sessionWorkspaceIdRequest(request.workspaceId), include_hidden_subagents: request.includeHiddenSubagents ?? true, ...(request.includeSystemPromptPreview ? { include_system_prompt_preview: true } : {}), + ...(request.contextEstimate ? { context_estimate: request.contextEstimate } : {}), ...(request.contextPreviewCategory ? { context_preview_category: request.contextPreviewCategory } : {}), } })); diff --git a/src/web-ui/src/locales/en-US/flow-chat.json b/src/web-ui/src/locales/en-US/flow-chat.json index 241a0ebbf9..b5e97754ed 100644 --- a/src/web-ui/src/locales/en-US/flow-chat.json +++ b/src/web-ui/src/locales/en-US/flow-chat.json @@ -251,22 +251,22 @@ "tokenCapacity": "{{used}} / {{size}} Tokens", "loading": "Loading context usage…", "loadFailed": "Unable to load context usage. Close and reopen to try again.", - "unsupported": "Context usage is not available on this device.", - "updateRequired": "The OpenBitFun version on this device cannot show context usage yet. Update OpenBitFun on that device to see it.", + "unsupported": "This host does not support current context estimates.", + "updateRequired": "Update OpenBitFun on this host to inspect the current context.", "title": "Context composition", "label": "Context", "utilization": "Context usage", "unused": "Unused", - "capacityUnavailable": "Context capacity was not reported. Only recorded usage by category is shown.", - "exceedsCapacity": "Estimated context usage exceeds the reported capacity. Only recorded usage by category is shown.", + "capacityUnavailable": "Context capacity was not reported. Only estimated usage by category is shown.", + "exceedsCapacity": "Estimated context usage exceeds the reported capacity. Only estimated usage by category is shown.", "usageOfCapacity": "{{used}} / {{size}}", "approximateTokens": "~{{value}}", "estimated": "Estimated", "estimatedTokens": "Estimated tokens", - "estimateHelp": "Estimated from the latest recorded model request. Rules include workspace and user instructions; Skills and subagents include their listings. These are not provider-reported token counts.", - "unavailable": "No context breakdown recorded", + "estimateHelp": "Estimated on demand from the current session context and configured prompt sources. Category counts are estimates, not exact provider-reported token counts.", + "unavailable": "Current context breakdown is unavailable", "previewCategory": "Preview {{category}}", - "previewEmpty": "This category has no content in the latest request.", + "previewEmpty": "This category has no content in the current context.", "previewUnavailable": "This session or host has no preview available for this category.", "previewFailed": "Unable to load the preview. Please try again.", "categories": { diff --git a/src/web-ui/src/locales/zh-CN/flow-chat.json b/src/web-ui/src/locales/zh-CN/flow-chat.json index 5df8c158dd..7ac0b0d228 100644 --- a/src/web-ui/src/locales/zh-CN/flow-chat.json +++ b/src/web-ui/src/locales/zh-CN/flow-chat.json @@ -251,22 +251,22 @@ "tokenCapacity": "{{used}} / {{size}} Tokens", "loading": "正在加载上下文用量…", "loadFailed": "无法加载上下文用量,请收起后重新打开。", - "unsupported": "该设备暂不支持查看上下文用量。", - "updateRequired": "该设备上的 OpenBitFun 版本暂不支持查看上下文用量,更新该设备上的 OpenBitFun 后即可查看。", + "unsupported": "该主机暂不支持当前上下文估算。", + "updateRequired": "请更新该主机上的 OpenBitFun,以查看当前上下文估算。", "title": "上下文构成", "label": "上下文", "utilization": "使用占比", "unused": "未使用", - "capacityUnavailable": "未提供上下文容量,仅显示已记录的用量构成。", - "exceedsCapacity": "上下文估算用量超出已报告的容量,仅显示已记录的用量构成。", + "capacityUnavailable": "未提供上下文容量,仅显示当前估算的用量构成。", + "exceedsCapacity": "上下文估算用量超出已报告的容量,仅显示当前估算的用量构成。", "usageOfCapacity": "{{used}} / {{size}}", "approximateTokens": "~{{value}}", "estimated": "估算", "estimatedTokens": "估算 Token", - "estimateHelp": "基于最近一次已记录的模型请求估算。规则包含项目及用户指令,技能和子代理包含其定义列表;分类数量并非模型提供方上报的精确值。", - "unavailable": "尚未记录上下文分类数据", + "estimateHelp": "按需估算当前会话上下文及其提示词来源。分类数量是估算值,并非模型提供方上报的精确 Token 数。", + "unavailable": "暂时无法估算当前上下文构成", "previewCategory": "预览{{category}}", - "previewEmpty": "最近一次请求中,此分类没有内容。", + "previewEmpty": "当前上下文中,此分类没有内容。", "previewUnavailable": "当前会话或执行主机未提供此分类的内容预览。", "previewFailed": "内容加载失败,请重试。", "categories": { diff --git a/src/web-ui/src/locales/zh-TW/flow-chat.json b/src/web-ui/src/locales/zh-TW/flow-chat.json index 181dc48aa1..a4cf73cde8 100644 --- a/src/web-ui/src/locales/zh-TW/flow-chat.json +++ b/src/web-ui/src/locales/zh-TW/flow-chat.json @@ -251,22 +251,22 @@ "tokenCapacity": "{{used}} / {{size}} Tokens", "loading": "正在載入上下文用量…", "loadFailed": "無法載入上下文用量,請收合後重新開啟。", - "unsupported": "此裝置暫不支援檢視上下文用量。", - "updateRequired": "此裝置上的 OpenBitFun 版本暫不支援檢視上下文用量,更新該裝置上的 OpenBitFun 後即可檢視。", + "unsupported": "此主機暫不支援目前上下文估算。", + "updateRequired": "請更新此主機上的 OpenBitFun,以查看目前上下文估算。", "title": "上下文組成", "label": "上下文", "utilization": "使用占比", "unused": "未使用", - "capacityUnavailable": "未提供上下文容量,僅顯示已記錄的用量組成。", - "exceedsCapacity": "上下文估算用量超出已回報的容量,僅顯示已記錄的用量組成。", + "capacityUnavailable": "未提供上下文容量,僅顯示目前估算的用量組成。", + "exceedsCapacity": "上下文估算用量超出已回報的容量,僅顯示目前估算的用量組成。", "usageOfCapacity": "{{used}} / {{size}}", "approximateTokens": "~{{value}}", "estimated": "估算", "estimatedTokens": "估算 Token", - "estimateHelp": "依據最近一次已記錄的模型請求估算。規則包含專案及使用者指令,技能與子代理包含其定義清單;分類數量並非模型提供方回報的精確值。", - "unavailable": "尚未記錄上下文分類資料", + "estimateHelp": "按需估算目前會話上下文及其提示詞來源。分類數量是估算值,並非模型提供方回報的精確 Token 數。", + "unavailable": "暫時無法估算目前上下文構成", "previewCategory": "預覽{{category}}", - "previewEmpty": "最近一次請求中,此分類沒有內容。", + "previewEmpty": "目前上下文中,此分類沒有內容。", "previewUnavailable": "目前工作階段或執行主機未提供此分類的內容預覽。", "previewFailed": "內容載入失敗,請重試。", "categories": {