fix: check the Assistant config inside its lock and pass bare stream events through (OM-179)

ensureAssistantMode now checks whether opencode.json exists after taking the file lock, so a conversion that ran in between cannot leave it acting on a stale result.

The browser event stream again delivers a message frame that carries a typed event without the directory envelope, as it did before the client migration, instead of dropping it silently.
This commit is contained in:
Chris Scott committed 2026-11-05 16:43:56 -05:00
1 parent 052b501963
commit 8b6ed7a861
3 files changed
+20 -4

No files matched your search

+1 -2
View File
@@ -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
@@ -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 })
@@ -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 }