Commit Graph

166 Commits

Author SHA1 Message Date
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
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
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
898b8ce5a1 fix: lazy agent loading and type safety improvements
1. Lazy agent loading - fix race condition where prompts sent before initialization completes:
   - Add ensureAgentLoaded() helper that deduplicates initialization requests
   - Update handleSendAgentMessage/Audio to await agent initialization before streaming
   - Fix status updates being sent to client after initialization

2. Type safety - remove unsafe 'as any' casts for agent status:
   - Create AGENT_LIFECYCLE_STATUSES constant as single source of truth
   - Export AgentStatusSchema from messages.ts for reuse
   - Update registry schema to validate lastStatus against AgentStatusSchema
   - Add .default("closed") to handle missing status values from legacy files
   - Remove (record.lastStatus as any) cast in buildStoredAgentPayload

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 22:44:03 +00:00
Mohamed Boudra
7c1517d830 feat(server): add Playwright MCP with headless mode to spawned Claude agents
Spawned agents now get Playwright browser automation capabilities by default.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 05:08:56 +00:00
Mohamed Boudra
53d91a319e fix(app): use pre-parsed props for Codex file edit diffs
ToolCall component had parsedEditEntries, parsedReadEntries, and
parsedCommandDetails props defined but never used them. Now prefers
these pre-parsed props when available, enabling Codex apply_patch
diffs to render correctly in the expanded view.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 01:04:33 +00:00
Mohamed Boudra
4001b7dc0a refactor: remove heuristic parsing, use pure type-based tool rendering
- Remove all heuristic parsing code from message.tsx (hasCommandDetails,
  commandSection, editSections, readSections, hasStructuredContent, etc.)
- Replace with simple raw JSON fallback for tools without structured results
- Fix server-side support for Claude SDK's old_string/new_string params
  (in addition to old_str/new_str)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 19:38:42 +00:00
Mohamed Boudra
38b08fec0c feat: add structured tool result types for better rendering
Server-side:
- Add StructuredToolResult discriminated union type with command, file_write,
  file_edit, file_read, and generic variants
- Implement buildStructuredToolResult in claude-agent.ts to detect tool types
  and emit properly structured results
- Update codex-agent.ts to emit structured command results

Client-side:
- Add type guard and extraction functions for structured results
- Render tool calls based on result.type when available:
  - command: show command, output, exit code
  - file_write/file_edit: show diff viewer with proper +/- format
  - file_read: show file content
  - generic: show raw JSON
- Fall back to heuristic parsing for backwards compatibility

This fixes file writes showing as "Command: success message" - they now
properly show as diffs with +line additions.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 19:24:52 +00:00
Mohamed Boudra
a7df671144 fix(app): deduplicate tool call expansion sections
Command output was being shown three times: as "Command", as "Read Result",
and as raw JSON "Result". Now:
- Skip "Read Result" when command output is already shown
- Skip raw JSON "Result" when we have structured content to display

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 19:05:41 +00:00
Mohamed Boudra
e80989ea78 perf: remove raw field from timeline items to reduce payload size
Remove the `raw` field that was duplicating provider data in timeline
items, reducing WebSocket payload sizes by 64-85%:
- session_state: ~320KB → ~48KB
- agent_stream_snapshot: ~320KB → ~114KB

Changes:
- Remove raw from AgentTimelineItem, AgentStreamEvent, AgentPermissionRequest
- Remove raw assignments from claude-agent.ts and codex-agent.ts
- Remove provider_event handling from stream.ts (only used for Codex raw)
- Update tests to reflect new behavior

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 18:20:33 +00:00
Mohamed Boudra
ae4678bca8 fix: eliminate duplicate WebSocket messages during agent history priming
- Skip dispatching individual agent_stream events when replaying history
  (the snapshot is sent after priming anyway, so individual events were wasted)
- Add WebSocket message logging on client (type, size, id) for debugging
- Change default daemon URL from dev to localhost
- Clean up unused imports in session.ts

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 17:44:07 +00:00
Mohamed Boudra
1b019c4688 chore: update android scripts and add dev runner with IPC restart support 2025-11-28 11:39:31 +01:00
Mohamed Boudra
2c84447ec2 fix(app): resolve create agent dictation confirm handler timing issue
Fixed a state management bug where clicking the checkmark in create agent dictation mode would not trigger processing. The issue was that setIsDictationProcessing(true) was called after stopping the recorder, causing a race condition where the UI never showed the processing state.

Changes:
- Move setIsDictationProcessing(true) to execute immediately at the start of the confirm handler
- Add proper cleanup when audioData is null
- Ensure UI shows loading spinner when checkmark is clicked

This aligns the create agent dictation behavior with the working agent chat dictation implementation.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 10:10:31 +00:00
Mohamed Boudra
b97ae99b2b feat(app): add Android product flavors and env-based daemon config 2025-11-28 08:19:41 +01:00
Mohamed Boudra
08dc2ed937 feat(app): add zustand store for session state management 2025-11-27 21:36:03 +01:00
Mohamed Boudra
bf58e81ce8 refactor(app): simplify session directory with direct snapshots and add sessionReady state
Replace subscription-based session accessor pattern with direct snapshot updates in daemon connections context. Add sessionReady flag to connection state to track when initial agent list has been received. Auto-select first ready host in create agent modal. Migrate from app.json to app.config.js. Add android:release script and Playwright testing docs.
2025-11-27 21:06:48 +01:00
Mohamed Boudra
a01c3f9b73 Flatten home screen agent list and sort by activity
The agent list on the home screen now displays all agents in a single flat
list rather than grouped by host. Each row shows a host badge next to the
agent title so users can still identify which host an agent belongs to.
Agents are sorted globally by status (running first) then by most recent
activity, providing a unified view across all hosts.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 20:18:49 +01:00
Mohamed Boudra
135bcf13a8 Fix create agent modal incorrectly showing hosts as offline
The availability check was failing for online hosts due to a timing
issue: connectionStates would show "online" before SessionProvider
registered its session accessor, causing session to be null.

Changed selectedDaemonIsUnavailable to selectedDaemonIsOffline which
only checks connectionStates status (the single source of truth for
connection state), removing the redundant session and ws.isConnected
checks. isTargetDaemonReady still requires session for actual operations.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 20:12:11 +01:00
Mohamed Boudra
0c571f6f43 Add loading indicator to home screen while agents are fetching
useAggregatedAgents now returns { groups, isLoading } where isLoading is true
while the daemon registry loads or while hosts are connecting without a session.
HomeScreen shows an ActivityIndicator during loading, then either the agent
list or empty state. Offline hosts don't block loading—only connecting hosts do.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 20:07:33 +01:00
Mohamed Boudra
69881d3862 Remove connection status banners from home screen
Host health now only displays in Settings. The home screen no longer
shows offline/connecting/error banners for hosts, following the
principle that a stopped host is not an error.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 20:03:22 +01:00
Mohamed Boudra
c88bbd3ee8 Rename "Server" to "Host" in Settings add-host form and restart alert
- Change placeholder text from "My Server" to "My Host"
- Change restart success alert title from "Server reachable" to "Host reachable"

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 19:58:31 +01:00
Mohamed Boudra
e98952bf4c Remove manual connect prompts 2025-11-26 19:47:55 +01:00
Mohamed Boudra
0db0e1ad23 Clarify automatic host reconnection 2025-11-26 19:33:43 +01:00
Mohamed Boudra
393aa44e68 feat: auto reconnect hosts when app resumes 2025-11-26 19:26:27 +01:00
Mohamed Boudra
99b055d2f9 Remove global settings test form 2025-11-26 19:09:19 +01:00
Mohamed Boudra
4f00e07284 Eliminate hidden host defaults 2025-11-26 19:02:47 +01:00
Mohamed Boudra
670c38ab8c Remove legacy host auto-connect state 2025-11-26 18:53:53 +01:00
Mohamed Boudra
737e8904be Remove auto-connect toggle from host settings 2025-11-26 18:42:36 +01:00
Mohamed Boudra
db87f331fb Remove primary host plumbing 2025-11-26 18:30:04 +01:00
Mohamed Boudra
5807f62f1a Remove active host dependency from UI actions 2025-11-26 18:04:40 +01:00
Mohamed Boudra
e6d2998904 Remove primary host concept 2025-11-26 17:51:25 +01:00