From 24627031cfe9519cef7f2839e09510949166ab03 Mon Sep 17 00:00:00 2001 From: Pedram Amini Date: Mon, 18 May 2026 07:37:04 -0700 Subject: [PATCH] fix(cli): persist CLI-created agent to disk before responding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `maestro:remoteCreateSession` handler relied on the renderer's 2-second debounced persistence to flush the new session, so a CLI consumer that ran `create-agent` and immediately followed up with `list agents` / `send` / `show agent` hit the disk-backed CLI storage before the flush fired, surfacing as AGENT_NOT_FOUND. Force an immediate `sessions:setMany` write before sending the create_session_result, so the CLI contract "create returned success → the agent is on disk" holds. The subsequent debounced flush is idempotent — it just rewrites the same row. Closes #1013 --- .../hooks/remote/useAppRemoteEventListeners.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/renderer/hooks/remote/useAppRemoteEventListeners.ts b/src/renderer/hooks/remote/useAppRemoteEventListeners.ts index bb49396436..cabc29504f 100644 --- a/src/renderer/hooks/remote/useAppRemoteEventListeners.ts +++ b/src/renderer/hooks/remote/useAppRemoteEventListeners.ts @@ -1015,6 +1015,20 @@ export function useAppRemoteEventListeners(deps: UseAppRemoteEventListenersDeps) isRemote: false, }); + // Persist the new agent to disk synchronously before responding. The + // renderer's debounced persistence path (useDebouncedPersistence) is + // driven by React render cycles and a 2s timer, so a CLI consumer that + // runs `create-agent` and then immediately `list agents` / `send` would + // otherwise hit the disk-backed CLI storage layer before the in-memory + // session has been flushed — surfacing as `AGENT_NOT_FOUND` (issue #1013). + // `setMany` is incremental and idempotent: the debounced flush that + // follows simply rewrites the same row. + try { + await window.maestro.sessions.setMany([newSession], []); + } catch (persistErr) { + logger.error('[Remote] Failed to persist new CLI-created session:', undefined, persistErr); + } + window.maestro.process.sendRemoteCreateSessionResponse(responseChannel, { sessionId: newId, });