diff --git a/backend/src/services/assistant-mode.ts b/backend/src/services/assistant-mode.ts index 03d6bee8..cbe01d13 100644 --- a/backend/src/services/assistant-mode.ts +++ b/backend/src/services/assistant-mode.ts @@ -1183,8 +1183,6 @@ export async function ensureAssistantMode( const assistantAgentPath = getAssistantDefaultAgentPath(assistantDir) const agentsMdExists = await fileExists(agentsMdPath) - const opencodeJsonExists = await fileExists(opencodeJsonPath) - const overwriteOpenCodeConfig = options?.overwriteOpenCodeConfig ?? false const overwriteAgentsMd = options?.overwriteAgentsMd ?? false @@ -1212,6 +1210,7 @@ export async function ensureAssistantMode( } const opencodeJsonUpdated = await withFileLock(opencodeJsonPath, async () => { + const opencodeJsonExists = await fileExists(opencodeJsonPath) const existingOpenCodeJsonContent = opencodeJsonExists ? await readFileContent(opencodeJsonPath) : undefined const parsedOpenCodeConfig = existingOpenCodeJsonContent === undefined ? undefined diff --git a/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts b/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts index 490b8e48..169f6a24 100644 --- a/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts +++ b/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts @@ -67,6 +67,21 @@ describe('OpenCodeEventStream', () => { expect(onEvent).toHaveBeenNthCalledWith(2, { type: 'credential.updated', data: {} }) }) + it('passes through a bare event without an envelope and drops frames without a type', () => { + const transport = new TestEventStreamTransport() + const stream = new OpenCodeEventStream({ transport }) + const onEvent = vi.fn() + + stream.subscribeGlobalMonitor({ directories: ['/repo'], onEvent }) + transport.openConnection() + transport.connected() + transport.message({ type: 'ssh.host-key-request', properties: { requestId: 'r1' } }) + transport.message({ payload: { data: {} } }) + + expect(onEvent).toHaveBeenCalledTimes(1) + expect(onEvent).toHaveBeenCalledWith({ type: 'ssh.host-key-request', properties: { requestId: 'r1' } }) + }) + it('copies the envelope server id onto remote events only', () => { const transport = new TestEventStreamTransport() const stream = new OpenCodeEventStream({ transport }) diff --git a/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts b/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts index 0a1d05fe..e19d9377 100644 --- a/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts +++ b/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts @@ -534,10 +534,12 @@ function isEventPayload(value: unknown): value is V2Event | SSHHostKeyRequestEve /** * Unwraps a transport envelope into the stream event consumers expect, copying `directory` and - * `serverId` onto it when present. Returns `null` when the envelope or its payload is malformed. + * `serverId` onto it when present. A bare event without an envelope is passed through unchanged; + * anything else returns `null`. */ function flattenEventEnvelope(parsed: unknown): OpenCodeStreamEvent | null { - if (!isRecord(parsed) || !isEventPayload(parsed.payload)) return null + if (!isRecord(parsed)) return null + if (!isEventPayload(parsed.payload)) return isEventPayload(parsed) ? { ...parsed } : null const { payload, directory, serverId } = parsed const event: OpenCodeStreamEvent = { ...payload }