BTerminal/docs/progress.md

15 KiB

BTerminal v2 — Progress Log

Earlier sessions (2026-03-05 to 2026-03-06 early): see progress-archive.md

Session: 2026-03-06 (continued) — Multi-Machine Architecture Design

Multi-Machine Support Architecture

  • Designed full multi-machine architecture in docs/multi-machine.md (303 lines)
  • Three-layer model: BTerminal (controller) + bterminal-relay (remote binary) + unified frontend
  • WebSocket NDJSON protocol: RelayCommand/RelayEvent envelope wrapping existing sidecar format
  • Authentication: pre-shared token + TLS, rate limiting, lockout
  • Autonomous relay model: agents keep running when controller disconnects
  • Reconnection with exponential backoff (1s-30s), state_sync on reconnect
  • 4-phase implementation plan: A (extract bterminal-core crate), B (relay binary), C (RemoteManager), D (frontend)
  • Updated TODO.md and docs/task_plan.md to reference the design

Session: 2026-03-06 (continued) — Multi-Machine Implementation (Phases A-D)

Phase A: bterminal-core crate extraction

  • Created Cargo workspace at v2/ level (v2/Cargo.toml, workspace members: src-tauri, bterminal-core, bterminal-relay)
  • Extracted PtyManager into v2/bterminal-core/src/pty.rs
  • Extracted SidecarManager into v2/bterminal-core/src/sidecar.rs
  • Created EventSink trait (v2/bterminal-core/src/event.rs) to abstract event emission
  • TauriEventSink (v2/src-tauri/src/event_sink.rs) implements EventSink for Tauri AppHandle
  • src-tauri/src/pty.rs and sidecar.rs now thin re-export wrappers
  • Cargo.lock moved from src-tauri/ to workspace root (v2/)

Phase B: bterminal-relay binary

  • New Rust binary at v2/bterminal-relay/ with WebSocket server (tokio-tungstenite)
  • Token auth via Authorization: Bearer header on WebSocket upgrade
  • CLI flags: --port (default 9750), --token (required), --insecure (allow ws://)
  • Routes RelayCommand types (pty_create/write/resize/close, agent_query/stop, sidecar_restart, ping)
  • Forwards RelayEvent types (pty_data/exit, sidecar_message/exited, error, pong, ready)
  • Rate limiting: 10 failed auth attempts triggers 5-minute lockout
  • Per-connection isolated PtyManager + SidecarManager instances

Phase C: RemoteManager in controller

  • New v2/src-tauri/src/remote.rs module — RemoteManager struct
  • WebSocket client connections to relay instances (tokio-tungstenite)
  • RemoteMachine struct: id, label, url, token, status (Connected/Connecting/Disconnected/Error)
  • Machine lifecycle: add_machine, remove_machine, connect, disconnect
  • 12 new Tauri commands: remote_add_machine, remote_remove_machine, remote_connect, remote_disconnect, remote_list_machines, remote_pty_spawn/write/resize/kill, remote_agent_query/stop, remote_sidecar_restart
  • Heartbeat ping every 15s to detect stale connections

Phase D: Frontend integration

  • v2/src/lib/adapters/remote-bridge.ts — IPC adapter for machine management + remote events
  • v2/src/lib/stores/machines.svelte.ts — Svelte 5 store for remote machine state
  • Layout store: added remoteMachineId?: string to Pane interface
  • agent-bridge.ts: routes to remote_agent_query/stop when pane has remoteMachineId
  • pty-bridge.ts: routes to remote_pty_spawn/write/resize/kill when pane has remoteMachineId
  • SettingsDialog: new "Remote Machines" section (add/remove/connect/disconnect UI)
  • SessionList sidebar: auto-groups remote panes by machine label

Verification

  • cargo check --workspace: clean (0 errors)
  • vitest: 114/114 tests passing
  • svelte-check: clean (0 errors)

New dependencies added

  • bterminal-core: serde, serde_json, log, portable-pty, uuid (extracted from src-tauri)
  • bterminal-relay: tokio, tokio-tungstenite, clap, env_logger, futures-util
  • src-tauri: tokio-tungstenite, tokio, futures-util, uuid (added for RemoteManager)

Session: 2026-03-06 (continued) — Relay Hardening & Reconnection

Relay Command Response Propagation

  • Shared event channel between EventSink and command response sender (sink_tx clone in bterminal-relay)
  • send_error() helper function: all command failures now emit RelayEvent with commandId + error message instead of just logging
  • ping command: now sends pong response via event channel (was a no-op)
  • pty_create: returns pty_created event with session ID and commandId for correlation
  • All error paths (pty_write, pty_resize, pty_close, agent_query, agent_stop, sidecar_restart) use send_error()

RemoteManager Reconnection

  • Exponential backoff reconnection in remote.rs: spawns async tokio task on disconnect
  • Backoff schedule: 1s, 2s, 4s, 8s, 16s, 30s (capped)
  • attempt_tcp_probe() function: TCP-only connect probe (5s timeout, default port 9750) — avoids allocating per-connection resources on relay
  • Emits remote-machine-reconnecting (with backoffSecs) and remote-machine-reconnect-ready Tauri events
  • Cancellation: stops if machine removed (not in HashMap) or manually reconnected (status != disconnected)
  • Fixed scoping: disconnection cleanup uses inner block to release mutex before emitting event

RemoteManager PTY Creation Confirmation

  • Handles pty_created event type from relay: emits remote-pty-created Tauri event with machineId, ptyId, commandId

Session: 2026-03-06 (continued) — Reconnection Hardening

TCP Probe Refactor

  • Replaced attempt_ws_connect() with attempt_tcp_probe() in remote.rs: TCP-only connect (no WS upgrade), 5s timeout, default port 9750
  • Avoids allocating per-connection resources (PtyManager, SidecarManager) on the relay during reconnection probes
  • Probe no longer needs auth token — only checks TCP reachability

Frontend Reconnection Listeners

  • Added onRemoteMachineReconnecting() listener in remote-bridge.ts: receives machineId + backoffSecs
  • Added onRemoteMachineReconnectReady() listener in remote-bridge.ts: receives machineId when probe succeeds
  • machines.svelte.ts: reconnecting handler sets machine status to 'reconnecting', shows toast with backoff duration
  • machines.svelte.ts: reconnect-ready handler auto-calls connectMachine() to re-establish full WebSocket connection
  • Updated docs/multi-machine.md to reflect TCP probe and frontend listener changes

Session: 2026-03-06 (continued) — Sidecar Env Var Bug Fix

CLAUDE* Environment Variable Leak (critical fix)

  • Diagnosed silent hang in agent sessions when BTerminal launched from Claude Code terminal
  • Root cause: Claude Code sets ~8 CLAUDE* env vars for nesting/sandbox detection
  • Fixed both sidecar runners to filter out all keys starting with 'CLAUDE'

Session: 2026-03-06 (continued) — Sidecar SDK Migration

Migration from CLI Spawning to Agent SDK

  • Diagnosed root cause: claude CLI v2.1.69 hangs with piped stdio (bug #6775)
  • Migrated both runners to @anthropic-ai/claude-agent-sdk query() function
  • Added build:sidecar script (esbuild bundle, SDK included)
  • SDK options: permissionMode configurable, allowDangerouslySkipPermissions conditional

Bug Found and Fixed

  • AgentPane onDestroy killed running sessions on layout remounts (fixed: moved to TilingGrid onClose)

Session: 2026-03-06 (continued) — Permission Mode, AgentPane Bug Fix, SDK Bundling

Permission Mode Passthrough

  • permission_mode field flows Rust -> sidecar -> SDK, defaults to 'bypassPermissions'

AgentPane onDestroy Bug Fix

  • Stop-on-close moved from AgentPane onDestroy to TilingGrid onClose handler

SDK Bundling Fix

  • SDK bundled into agent-runner.mjs (no external dependency at runtime)

Session: 2026-03-07 — Unified Sidecar Bundle

Sidecar Resolution Simplification

  • Consolidated to single pre-built bundle (dist/agent-runner.mjs) running on both Deno and Node.js
  • resolve_sidecar_command() checks runtime availability upfront, prefers Deno
  • agent-runner-deno.ts retained in repo but not used by SidecarManager

Session: 2026-03-07 (continued) — Rust-Side CLAUDE* Env Var Stripping

Dual-Layer Env Var Stripping

  • Rust SidecarManager uses env_clear() + envs(clean_env) before spawn (primary defense)
  • JS-side stripping retained as defense-in-depth

Session: 2026-03-07 (continued) — Claude CLI Path Detection

pathToClaudeCodeExecutable for SDK

  • Added findClaudeCli() to agent-runner.ts (Node.js): checks ~/.local/bin/claude, ~/.claude/local/claude, /usr/local/bin/claude, /usr/bin/claude, then falls back to which claude/where claude
  • Added findClaudeCli() to agent-runner-deno.ts (Deno): same candidate paths, uses Deno.statSync() + Deno.Command("which")
  • Both runners now pass pathToClaudeCodeExecutable to SDK query() options
  • Early error: if Claude CLI not found, agent_error emitted immediately instead of cryptic SDK failure
  • CLI path resolved once at sidecar startup, logged for debugging

Session: 2026-03-07 (continued) — Claude Profiles & Skill Discovery

Claude Profile / Account Switching (switcher-claude integration)

  • New Tauri commands: claude_list_profiles(), claude_list_skills(), claude_read_skill(), pick_directory()
  • claude_list_profiles() reads ~/.config/switcher/profiles/ for profile directories with profile.toml metadata
  • Config dir resolution: ~/.config/switcher-claude/{name}/ or fallback ~/.claude/
  • extract_toml_value() simple TOML parser for profile metadata (email, subscription_type, display_name)
  • Always includes "default" profile if no switcher profiles found

Skill Discovery & Autocomplete

  • claude_list_skills() reads ~/.claude/skills/ directory (directories with SKILL.md or standalone .md files)
  • Description extracted from first non-heading, non-empty line (max 120 chars)
  • claude_read_skill(path) reads full skill file content
  • New frontend adapter: v2/src/lib/adapters/claude-bridge.ts (ClaudeProfile, ClaudeSkill interfaces)

AgentPane Session Toolbar

  • Working directory input (cwdInput) — editable text field, replaces fixed cwd prop
  • Profile/account selector dropdown (shown when >1 profile available)
  • Selected profile's config_dir passed as claude_config_dir in AgentQueryOptions
  • Skill autocomplete menu: type / to trigger, arrow keys navigate, Tab/Enter select, Escape dismiss
  • expandSkillPrompt(): reads skill content via readSkill(), prepends to prompt with optional user args

Extended AgentQueryOptions (full stack passthrough)

  • New fields in Rust AgentQueryOptions struct: setting_sources, system_prompt, model, claude_config_dir, additional_directories
  • Sidecar QueryMessage interface updated with matching fields
  • Both sidecar runners (agent-runner.ts, agent-runner-deno.ts) pass new fields to SDK query()
  • CLAUDE_CONFIG_DIR injected into cleanEnv when claudeConfigDir provided (multi-account support)
  • settingSources defaults to ['user', 'project'] (loads CLAUDE.md and project settings)
  • Frontend AgentQueryOptions interface updated in agent-bridge.ts

Session: 2026-03-07 (continued) — v3 Mission Control Planning

v3 Architecture Planning

  • Created docs/v3-task_plan.md — core concept, user requirements, architecture questions
  • Created docs/v3-findings.md — codebase reuse analysis (what to keep/replace/drop)
  • Created docs/v3-progress.md — v3-specific progress log
  • Launched 3 adversarial architecture agents (Architect, Devil's Advocate, UX+Perf Specialist)
  • Collect adversarial agent findings
  • Produce final architecture plan
  • Create v3 implementation phases

Session: 2026-03-07 (continued) — v3 Mission Control MVP Implementation (Phases 1-5)

Phase 1: Data Model + Config

  • Created v2/src/lib/types/groups.ts (ProjectConfig, GroupConfig, GroupsFile interfaces)
  • Created v2/src-tauri/src/groups.rs (Rust structs + load/save groups.json + default_groups())
  • Added groups_load, groups_save Tauri commands to lib.rs
  • SQLite migrations in session.rs: project_id column, agent_messages table, project_agent_state table
  • Created v2/src/lib/adapters/groups-bridge.ts (IPC wrapper)
  • Created v2/src/lib/stores/workspace.svelte.ts (replaces layout.svelte.ts for v3, Svelte 5 runes)
  • Added --group CLI argument parsing in main.rs
  • 24 vitest tests for workspace store + 7 cargo tests for groups

Phase 2: Project Box Shell

  • Created 12 new Workspace components in v2/src/lib/components/Workspace/
  • GlobalTabBar, ProjectGrid, ProjectBox, ProjectHeader, CommandPalette, DocsTab, ContextTab, SettingsTab
  • Rewrote App.svelte (no sidebar, no TilingGrid — GlobalTabBar + tab content + StatusBar)

Phase 3: Claude Session Integration

  • Created ClaudeSession.svelte wrapping AgentPane per-project

Phase 4: Terminal Tabs

  • Created TerminalTabs.svelte with shell/SSH/agent tab types

Phase 5: Team Agents Panel

  • Created TeamAgentsPanel.svelte + AgentCard.svelte

Bug Fix

  • Fixed AgentPane Svelte 5 event modifier: on:click -> onclick

Verification

  • All 138 vitest + 36 cargo tests pass, vite build succeeds

Session: 2026-03-07 (continued) — v3 Phases 6-10 Completion

Phase 6: Session Continuity

  • Added persistSessionForProject() to agent-dispatcher (saves state + messages to SQLite on complete)
  • Added registerSessionProject() + sessionProjectMap for session->project persistence routing
  • ClaudeSession restoreMessagesFromRecords() restores cached messages on mount
  • Added getAgentSession() export to agents store

Phase 7: Workspace Teardown

  • Added clearAllAgentSessions() to agents store
  • switchGroup() calls clearAllAgentSessions() + resets terminal tabs
  • Updated workspace.test.ts with clearAllAgentSessions mock

Phase 10: Dead Component Removal + Polish

  • Deleted 7 dead v2 components (~1,836 lines): TilingGrid, PaneContainer, PaneHeader, SessionList, SshSessionList, SshDialog, SettingsDialog
  • Removed empty directories: Layout/, Sidebar/, Settings/, SSH/
  • Rewrote StatusBar for workspace store (group name, project count, "BTerminal v3")
  • Fixed subagent routing: project-scoped sessions skip layout pane (render in TeamAgentsPanel)
  • Updated v3-task_plan.md to mark all 10 phases complete

Verification

  • All 138 vitest + 36 cargo tests pass, vite build succeeds

Session: 2026-03-07 (continued) — Multi-Theme System

Theme System Generalization (7 Editor Themes)

  • Generalized CatppuccinFlavor to ThemeId union type (11 themes)
  • Added 7 editor themes: VSCode Dark+, Atom One Dark, Monokai, Dracula, Nord, Solarized Dark, GitHub Dark
  • Added ThemePalette, ThemeMeta, THEME_LIST types; deprecated old Catppuccin-only types
  • Theme store: getCurrentTheme()/setTheme() with deprecated wrappers for backwards compat
  • SettingsTab: optgroup-based theme selector, fixed input overflow with min-width:0
  • All themes map to same --ctp-* CSS vars — zero component changes needed

Verification

  • All 138 vitest + 35 cargo tests pass

Next Steps

  • Real-world relay testing (2 machines)
  • TLS/certificate pinning for relay connections
  • E2E testing with Playwright/WebDriver (when display server available)
  • Test agent teams with CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1