← Back to PRs

#2352: feat(cli): CLI feature parity phase 2 - usage tracking and streaming (AI-assisted)

by rmorse open 2026-01-26 19:41 View on GitHub →
app: web-ui gateway agents size: XL
## Summary This PR continues CLI backend improvements from #1921, adding accurate token usage tracking and real-time streaming support. ### Concurrency Hardening Fixes race conditions in CLI transcript writes: - Uses `{ flag: 'wx' }` for atomic file creation (TOCTOU fix) - Adds session-level locking for concurrent-safe writes - New async API: `appendMessageToTranscriptAsync`, `appendAssistantMessageToTranscriptAsync` - Tests for partial failure (orphaned user message) and concurrent writes ### Token Usage Tracking > ⚠️ **WIP:** Token display is improved but not fully resolved - some edge cases remain Fixes incorrect token display in UI (showed 558k/200k when actual was ~120k): - Adds `usage` parameter to `appendMessageToTranscript` functions - Passes `result.meta.agentMeta?.usage` when persisting assistant messages - Creates `CliSessionManager` class with SDK-aligned API for future use - Transcript entries now contain actual input/output/cache token counts ### Configurable Usage Fields Enables per-backend token field parsing: - Adds `usageFields` config to `CliBackendConfig` - Handles different API response formats (Anthropic vs OpenAI field names) - Correctly parses `cache_creation_input_tokens` (previously missed) - Maintains backwards compatibility via fallback defaults ### Streaming NDJSON Support > ⚠️ **TODO:** Non-streaming path (`streaming: false`) is currently broken - needs fix before merge Adds real-time output for CLI backends: - New `cli-runner/streaming.ts` module with readline-based NDJSON parsing - Emits events as they arrive instead of waiting for full response - Config options: `streaming?: boolean`, `streamingEventTypes?: string[]` - Event mapping for Claude CLI (`text`, `tool_use`, `result`) and Codex CLI (`item.*`, `turn.*`) - Debug logging throughout pipeline for production diagnostics ### Reply Directives for Streaming Matches embedded flow's text processing: - Applies `parseReplyDirectives` to streaming text events - Extracts media URLs, cleans directives, computes delta from cleaned text ## Test plan - [x] Unit tests for CliSessionManager (17 tests) - [x] Unit tests for session-utils.fs usage parameter - [x] Unit tests for CLI streaming module - [x] Unit tests for agent-runner-execution CLI persistence - [x] Tested locally with claude-cli backend - tokens display improved - [x] Verified streaming events emit in real-time - [ ] **TODO:** Fix and test non-streaming path - [ ] **TODO:** Verify token display edge cases resolved ## AI-assisted This PR was developed with AI assistance (Claude). The code has been tested locally with a live Clawdbot instance. I understand what all the code does.

Most Similar PRs