Commit Graph

4607 Commits

Author SHA1 Message Date
Mohamed Boudra
9aacc26eb6 feat: add 60-second timeout to Agent Control MCP wait operations
Implement hard-coded 60-second timeout for agent wait operations in three tools:
- create_agent (when background=false and initialPrompt provided)
- send_agent_prompt (when background=false)
- wait_for_agent (always)

When timeout occurs, returns gracefully with message "Awaiting the agent timed out, await again" instead of throwing an error. Agent continues running after timeout.

Added AGENT_WAIT_TIMEOUT_MS constant and waitForAgentWithTimeout helper function to handle timeout logic with proper cleanup and signal forwarding.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 20:53:01 +07:00
Mohamed Boudra
5f645d6b7a perf: use shallow selectors in useAggregatedAgents to reduce re-renders
Replace greedy sessions selector with granular useShallow selectors
that only extract agent Maps and methods. This prevents HomeScreen
from re-rendering on every agent stream update.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 20:47:10 +07:00
Mohamed Boudra
56e2af2308 refactor: move mode validation into AgentSession implementations
Move mode validation from AgentManager into the individual AgentSession
implementations (ClaudeAgentSession and CodexAgentSession) where it belongs.

Each session validates modes in:
- Constructor (when config.modeId is provided)
- setMode() method (when mode is changed)

This follows the principle that the AgentManager is a coordinator while
the AgentSession is the authority on what modes it supports.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 20:17:25 +07:00
Mohamed Boudra
7b649eb77f feat: add centralized mode validation for agent providers
Prevents server crashes when invalid modes are passed to agents by
validating mode IDs before attempting to set them on the SDK.

- Export VALID_CLAUDE_MODES and VALID_CODEX_MODES from provider files
- Add validateMode() function in agent-manager.ts
- Apply validation in createAgent(), resumeAgent(), and setAgentMode()
- Throws clear error listing valid modes when validation fails

This fixes the crash when 'read-only' (Codex mode) was passed to
a Claude agent.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 20:03:38 +07:00
Mohamed Boudra
b7eed8409b fix: handle promise rejections in claude-agent fire-and-forget patterns
Fixed two fire-and-forget promise patterns that could cause unhandled rejections:
- interruptActiveTurn() in requestCancel closure - now catches errors and logs warning
- forwardPromptEvents() in stream method - now catches errors and logs to console

Both now properly handle promise rejections to prevent Node.js process crashes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 20:02:51 +07:00
Mohamed Boudra
2699e198d6 fix: always fetch agent timeline on screen open
Previously, opening an agent screen would skip fetching timeline history
if stream state already existed in the cache. This caused issues when:
- Opening a running agent after reconnection
- Returning to an agent with stale/incomplete cache

Now we always fetch fresh timeline on open, ensuring:
- Complete history is shown regardless of client state
- Reconnection scenarios work correctly
- Agent state is always up-to-date

Uses a ref to track initialization per mount to prevent infinite loops.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 19:14:37 +07:00
Mohamed Boudra
644c654274 perf: prevent HomeScreen rerenders during agent streaming
- Add top-level agentLastActivity Map to store (prevents cascade rerenders)
- Remove lastActivityAt updates from agent_stream handler (only status changes update timestamps)
- useAggregatedAgents now uses agent.lastActivityAt directly
- Remove investigation logging from previous debug session

The root cause was that every stream event updated agent.lastActivityAt,
causing useAggregatedAgents to recalculate, which caused HomeScreen to
rerender even when not visible. Now timestamps only update on meaningful
events (agent status changes), not individual stream tokens.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 17:34:07 +07:00
Mohamed Boudra
4c1ba8f1b1 chore: remove investigation logging from performance work
Remove debug logging and render counters added during performance investigation.
All performance issues have been resolved by:
- Moving drafts to top-level store
- Moving agentLastActivity to top-level store
- Using granular Zustand selectors throughout
- Eliminating useDaemonSession hook

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 17:33:23 +07:00
Mohamed Boudra
21d43bc249 perf: eliminate cascading rerenders with granular Zustand selectors
- Delete useDaemonSession hook that selected entire session object
- Move drafts to top-level store (not nested in sessions)
- Refactor all components to use granular useSessionStore selectors
- Fix infinite loop bugs from selector fallbacks (use stable constants)

Before: 15-20 component rerenders per keystroke
After: ~1-2 rerenders per keystroke

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 16:37:23 +07:00
Mohamed Boudra
74c82a312e perf: move drafts to top-level store to prevent session rerenders
Previously, drafts were nested inside each session object, which caused
the entire session to be recreated on every keystroke. This triggered
rerenders in all components subscribed to the session.

Now drafts are stored at the top level of the Zustand store, keyed by
agentId (not serverId:agentId since agent IDs are globally unique).
This means draft updates only trigger rerenders in components that
subscribe to drafts specifically.

Changes:
- Move drafts from SessionState to SessionStoreState (top-level)
- Add createModalDraft for future use by the create agent modal
- Update getDraftInput/saveDraftInput to work with agentId only
- Update session-context to clear drafts using new API
- Remove investigation console.log statements

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 15:46:45 +07:00
Mohamed Boudra
d5f8091d66 refactor: simplify modal mounting with lazy wrapper and add read-only git env 2025-12-03 14:09:56 +07:00
Mohamed Boudra
f4f7790d07 fix: improve terminal init error handling and websocket debug logging 2025-12-03 13:26:28 +07:00
Mohamed Boudra
43d2794e2d chore: remove plan.md 2025-12-03 12:43:41 +07:00
Mohamed Boudra
e346b4c026 Replace reconnecting badge with pull-to-refresh
UX improvements:
- Remove misleading "Reconnecting..." badge overlay
- Implement native pull-to-refresh gesture on agent list
- Add manual refresh capability via pull gesture

Technical changes:
- Add refreshSession() method to session context
- Add refreshAll() to useAggregatedAgents hook
- Integrate RefreshControl in AgentList component
- Use isRevalidating state for refresh indicator
- Apply theme colors to RefreshControl for consistency

Benefits:
- Familiar iOS/Android UX pattern
- No UI overlays blocking content
- Clear user action (pull) → clear feedback (refresh)
- Accurate state representation (syncing vs reconnecting)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 02:57:38 +00:00
Mohamed Boudra
348cb9d637 Fix loading state bugs and server crash
Backend fixes:
- Fix server crash when logging large session_state messages (186 agents)
- Use safe logging that extracts only message/stack properties
- Prevents TypeError in util.inspect on circular refs or deep objects

Frontend loading state fixes:
- Add reactive WebSocket update to prevent stale session.ws references
- Add timeout/retry wrapper for session_state loading (3 retries × 15s)
- Add hasEverReceivedSessionState flag to track cached data across reconnects
- Implement data-aware loading (isInitialLoad vs isRevalidating)
- Show reconnecting badge instead of hiding agents on reconnect

Debug improvements:
- Add detailed connection state logging in useAggregatedAgents
- Add connection status logging in AgentInputArea
- Track connecting reasons for easier debugging

Fixes:
- Agent screen no longer shows false "Offline" toast on reconnect
- Home screen shows loading immediately (no empty state flash)
- Home screen keeps agents visible during reconnect with badge
- Stuck loading has timeout protection with graceful degradation
- Server no longer crashes when sending session_state with many agents

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 02:45:16 +00:00
Mohamed Boudra
5ddaea7352 Complete Zustand migration: fix component access to session APIs
Finalizes the pure Zustand refactor by ensuring all components properly access both session state and imperative methods through useDaemonSession. Moves CLAUDE.md to root and ignores local overrides.

Changes:
- Fix useDaemonSession to return stable combined state + methods object
- Update components to use useDaemonSession instead of direct context
- Fix realtime context to work with session state only
- Move CLAUDE.md to root, ignore CLAUDE.local.md for local config
- Add proper null checks and type safety throughout

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 17:42:49 +00:00
Mohamed Boudra
473e11d25f Fix stale closure bug in agent-input-area message handlers
Replace direct `agentId` prop usage with `agentIdRef.current` to prevent
messages from being sent to the wrong agent during navigation.

Fixed 3 instances:
- handleSendMessage: Line 383
- handleSendQueuedNow: Line 869
- handleCancelAgent: Line 834

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 17:17:54 +00:00
Mohamed Boudra
e8aa08159a Fix useDaemonSession to return combined state + APIs
Problem: After migrating to pure Zustand, useDaemonSession returned only
SessionState from the store, but components need both state AND imperative
APIs (like sendAgentMessage, createAgent, etc.).

Solution: Create DaemonSession type that combines:
- SessionState (from Zustand store)
- Imperative APIs (from SessionContext)
- Wrapped store actions (getDraftInput, setFocusedAgentId, etc.)

Changes:
- Added DaemonSession type to use-daemon-session.ts
- useDaemonSession now merges sessionState + context + store actions
- Wraps store actions to bind serverId automatically
- Updated prop types in components to use DaemonSession
- Fixed use-session-directory.ts to use SessionState

This restores component compatibility while keeping pure Zustand architecture.

Still TODO: Fix remaining components that use SessionContext directly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 14:27:57 +00:00
Mohamed Boudra
f5ec3bffb7 Update agent lastActivityAt on stream events
Problem: lastActivityAt was only updated when agent_state messages arrived
(which only happens on agent lifecycle changes), not when the agent was
actively streaming output. This meant the agent directory showed stale
"last activity" times.

Solution: Update agent.lastActivityAt whenever agent_stream events arrive,
using the event's timestamp. Now lastActivityAt tracks real-time activity.

Updates happen on:
1. agent_state message → lastActivityAt = updatedAt (from server)
2. agent_stream message → lastActivityAt = event.timestamp (real-time!)

This ensures the agent directory always shows fresh activity times.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 14:16:50 +00:00
Mohamed Boudra
8b87b0a4e2 Remove agent directory syncing, make it pure derived state
Problem: Agent directory was still being synced from SessionProvider to
Zustand store, violating the "SessionProvider is just a message handler"
principle and creating unnecessary state duplication.

Solution: Make agent directory pure derived state:
- Removed agentDirectory from SessionStoreState
- Removed setAgentDirectory/clearAgentDirectory actions
- Changed getAgentDirectory to compute on-demand from session.agents
- Removed buildAgentDirectoryEntries and syncing from SessionProvider
- Updated useAggregatedAgents to derive from sessions directly

How it works now:
- Agent directory is computed on-demand from session.agents Map
- lastActivityAt automatically updates when agents update via WebSocket
- No syncing, no stale state, no overhead
- Single source of truth: session.agents

Benefits:
- Eliminated redundant state (agentDirectory was duplicate of agents)
- No sync overhead or complexity
- Always fresh data (derived on read)
- Simpler mental model (agents is the only source)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 14:15:32 +00:00
Mohamed Boudra
f5c6bce54f Migrate SessionContext to pure Zustand, eliminate hybrid overhead
Problem: SessionContext used useSyncedSessionState which synced every state
update from React useState to Zustand, creating double overhead and complexity.

Solution: Complete migration to pure Zustand architecture:

- session-store.ts is now the single source of truth
  - Moved ALL state directly into Zustand (no React useState)
  - Added per-session state: messages, agents, commands, permissions, etc.
  - Added Zustand actions for all state updates (setMessages, setAgents, etc.)
  - Exported all types (MessageEntry, Agent, Command, etc.)

- session-context.tsx is now a pure WebSocket message handler
  - Removed useSyncedSessionState hook entirely
  - Removed all useState calls for session state
  - Removed sync machinery (syncSessionField, syncSessionPartial)
  - WebSocket handlers now call Zustand setters directly
  - Context only provides imperative APIs (ws, audioPlayer, actions)
  - No state values in context

Architecture change:
  Before: WebSocket → React useState → sync → Zustand (double overhead)
  After:  WebSocket → Zustand directly (single source of truth)

Benefits:
- Eliminated double overhead from React → Zustand syncing
- Single state update per WebSocket message
- Better re-render optimization via Zustand selectors
- Cleaner separation: SessionProvider = message handler, SessionStore = state
- Reduced complexity: removed 400+ lines of hybrid sync code

Backward compatibility:
- useDaemonSession() already reads from Zustand, no changes needed
- Type exports re-exported from session-store for compatibility
- Components work exactly the same

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 14:08:56 +00:00
Mohamed Boudra
32e111e1bf fix: remove unused focusedAgentId orchestrator logic and timestamp thrashing
This commit implements a critical performance optimization by removing
unused orchestrator logic that was causing thousands of unnecessary
store updates per minute:

1. Removed orchestratorFocusedAgentId state and auto-selection logic
   - The orchestrator auto-selected "most recently active agent"
   - Nothing in the codebase actually reads focusedAgentId
   - Simplified focusedAgentId to just focusedAgentOverride (user selection only)

2. Stopped updating timestamps on every stream event
   - Previously updated lastActivityAt/updatedAt on every agent_stream event
   - These events occur 15+ times per second during streaming
   - Created 2700+ store updates per minute from a single agent

3. Removed focusedAgentId from store sync
   - No longer syncs focusedAgentId changes to the store
   - Reduces cascading updates throughout the app

Expected impact:
- 95%+ reduction in store updates during agent streaming
- Home screen no longer re-renders during agent activity
- Improved UI responsiveness and reduced battery usage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 13:14:49 +00:00
Mohamed Boudra
6615b23c8b fix: apply import.meta transform to ALL platforms (web + native)
The previous fix only applied unstable_transformImportMeta to the native
platform, but Zustand 5's import.meta.env usage also breaks on web.

Moving the transform to the top level ensures it applies to both web and
native platforms, fixing the "Cannot use 'import.meta' outside a module"
error across all platforms.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 11:25:18 +00:00
Mohamed Boudra
7896bdd42c revert: remove Phase 3 complexity - server handles persistence 2025-12-02 11:06:57 +00:00
Mohamed Boudra
3208624c5e perf: cap messages and optimize persistence (Phase 3) 2025-12-02 10:50:11 +00:00
Mohamed Boudra
0ba5e481f4 fix: Phase 2 review issues 2025-12-02 10:33:49 +00:00
Mohamed Boudra
dfe4321051 Ensure agent titles persist synchronously during creation 2025-12-02 10:13:59 +00:00
Mohamed Boudra
6e259c37db refactor: SessionProvider to use Zustand setters directly (Phase 2b) 2025-12-02 10:13:25 +00:00
Mohamed Boudra
fa1d7dd295 feat: migrate to real Zustand store (Phase 2) 2025-12-02 09:55:59 +00:00
Mohamed Boudra
b278b43fac feat: extract lightweight agent directory (Phase 1) 2025-12-02 09:28:52 +00:00
Mohamed Boudra
34e0e21657 chore: add session perf instrumentation 2025-12-02 09:20:02 +00:00
Mohamed Boudra
755b314e51 Use FlatList for home agent list 2025-12-02 08:17:53 +00:00
Mohamed Boudra
dc26bf713e feat(dictation): add resilient rpc retries and status toasts 2025-12-02 08:12:02 +00:00
Mohamed Boudra
71b238c0cd feat(dictation): migrate transcription to session rpc hook 2025-12-02 04:30:38 +00:00
Mohamed Boudra
5b920a2b13 Harden session RPC hook 2025-12-02 04:07:14 +00:00
Mohamed Boudra
9079695f46 Ensure Codex tool call IDs unique 2025-12-02 04:00:30 +00:00
Mohamed Boudra
b36f870ee9 Add RPC helper for agent initialization 2025-12-02 03:57:36 +00:00
Mohamed Boudra
6244988d79 fix: prevent crash from accessing released AudioRecorder ref
- use recorderState.isRecording instead of recorderRef.current.isRecording
- avoid touching native shared object after it's been released
- fixes production crash: 'cannot cast to AudioRecorder (received Integer)'

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 13:15:23 +00:00
Mohamed Boudra
27ec2b5c67 fix: share dictation hook across surfaces
- add shared useDictation hook to prevent double-stop races
- refactor modal + agent chat to consume shared API
- improve web recorder errors with secure-context messaging

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 12:25:58 +00:00
Mohamed Boudra
ad804cb1fe Require explicit titles for MCP create_agent 2025-12-01 11:37:02 +00:00
Mohamed Boudra
cb2ee687e9 Merge pull request #1 from boudra/refactor/agent-snapshot-removal
fix: remove stale raw field expectation from test

The 'raw' field was removed from timeline entries in commit e80989e
(Nov 28) to reduce payload sizes by 64-85%. This test was never updated
and has been broken since then. Remove the stale expectation to align
with the current implementation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 11:04:08 +08:00
Mohamed Boudra
fa84d4c0c9 docs: document paseo env overrides 2025-11-30 02:49:55 +00:00
Mohamed Boudra
b8d942cb6b feat: add configurable paseo home and port 2025-11-30 02:46:44 +00:00
Mohamed Boudra
cc34ea722a Preserve registry creation timestamps 2025-11-30 02:26:26 +00:00
Mohamed Boudra
f38287ff4c Fix concurrent agent initialization in sessions 2025-11-30 02:26:19 +00:00
Mohamed Boudra
51d31bb76d Remove unused restorePersistedAgents function
This function was never called after implementing lazy-loading.
Agents are now loaded on-demand via ensureAgentLoaded when the UI
needs them, not eagerly at startup.

Keeping the unused function was confusing - it suggested eager
restoration was still the intended pattern.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 02:16:14 +00:00
Mohamed Boudra
03b4799bc6 test: harden TerminalManager non-zero exit assertion 2025-11-30 01:58:04 +00:00
Mohamed Boudra
218bcb59db test: gate Claude SDK stream tests behind env 2025-11-30 01:57:43 +00:00
Mohamed Boudra
3030287d14 test: skip Claude ack test when Anthropic key missing 2025-11-30 01:56:30 +00:00
Mohamed Boudra
1adee92dd1 Actually remove test skipping logic (fix agent hallucination)
The previous agent claimed it removed the test skipping logic but
actually didn't - it hallucinated the fix. The file still had:
- const claudeIntegrationEnabled check
- describeClaudeIntegration conditional
- Warning message about skipping tests

This commit ACTUALLY removes all of it. Tests now run unconditionally.

All 14 tests pass ✓

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 01:46:30 +00:00