GeezerVison is showing github.com — fix(opencode): recover prompts dropped by an early Stop (#43359) · patroza/t3code@cc61cdb
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2/Adapters packages/provider-opencode/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -924,7 +924,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { | 924 | 924 | }); | 925 | 925 | yield* Deferred.succeed(release, undefined); | 926 | 926 | yield* Fiber.join(started); | 927 | | - assert.equal((yield* Fiber.join(terminal))?.status, "interrupted"); | | 927 | + assert.include(yield* Fiber.join(terminal), { status: "interrupted", promptDropped: true }); | 928 | 928 | assert.lengthOf(prompts, 0); | 929 | 929 | }), | 930 | 930 | ); | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/OpenCodeRuntime.ts +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -219,8 +219,12 @@ export interface OpenCodeInventory { | 219 | 219 |
| 220 | 220 | export type OpenCodeSlashCommand = Pick<Command, "name" | "description" | "source" | "hints">; | 221 | 221 |
| 222 | | -export const loadOpenCodeCommands = (client: OpencodeClient) => | 223 | | - runOpenCodeSdk("command.list", (signal) => client.command.list(undefined, { signal })).pipe( | | 222 | +export const loadOpenCodeCommands = (client: OpencodeClient, requestSignal?: AbortSignal) => | | 223 | + runOpenCodeSdk("command.list", (signal) => | | 224 | + client.command.list(undefined, { | | 225 | + signal: requestSignal === undefined ? signal : AbortSignal.any([signal, requestSignal]), | | 226 | + }), | | 227 | + ).pipe( | 224 | 228 | Effect.map((result): ReadonlyArray<OpenCodeSlashCommand> => | 225 | 229 | (result.data ?? []).map(({ name, description, source, hints }) => ({ | 226 | 230 | name, | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/adapter.test.ts +25-3Lines changed: 25 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4420,13 +4420,25 @@ describe("OpenCodeAdapterV2", () => { | 4420 | 4420 | ), | 4421 | 4421 | ); | 4422 | 4422 |
| 4423 | | - it.effect("interrupts an initial prompt while its SDK request is pending", () => | | 4423 | + it.effect.each(["preparing", "submitted"])("stops a pending prompt (%s)", (stage) => | 4424 | 4424 | Effect.gen(function* () { | 4425 | 4425 | const nativeEvents = asyncEventStream(); | 4426 | 4426 | const promptStarted = promiseGate<void>(); | 4427 | 4427 | const abortCalled = promiseGate<void>(); | 4428 | 4428 | let abortCalls = 0; | | 4429 | + let promptCalls = 0; | 4429 | 4430 | const client = { | | 4431 | + command: { | | 4432 | + list: async (_input: unknown, options?: { readonly signal?: AbortSignal }) => { | | 4433 | + promptStarted.resolve(); | | 4434 | + await new Promise<void>((resolve) => { | | 4435 | + const signal = options?.signal; | | 4436 | + if (signal?.aborted) resolve(); | | 4437 | + else signal?.addEventListener("abort", () => resolve(), { once: true }); | | 4438 | + }); | | 4439 | + return { data: [] }; | | 4440 | + }, | | 4441 | + }, | 4430 | 4442 | event: { | 4431 | 4443 | subscribe: async (_input: unknown, options: { signal?: AbortSignal }) => { | 4432 | 4444 | options.signal?.addEventListener("abort", () => nativeEvents.close(), { once: true }); | @@ -4441,6 +4453,7 @@ describe("OpenCodeAdapterV2", () => { | 4441 | 4453 | data: { id: "native-opencode-initial-stop", time: { created: 1, updated: 1 } }, | 4442 | 4454 | }), | 4443 | 4455 | promptAsync: async (_input: unknown, options?: { readonly signal?: AbortSignal }) => { | | 4456 | + promptCalls += 1; | 4444 | 4457 | promptStarted.resolve(); | 4445 | 4458 | await new Promise<void>((_resolve, reject) => { | 4446 | 4459 | const signal = options?.signal; | @@ -4455,7 +4468,10 @@ describe("OpenCodeAdapterV2", () => { | 4455 | 4468 | status: async () => ({ | 4456 | 4469 | data: { "native-opencode-initial-stop": { type: "idle" as const } }, | 4457 | 4470 | }), | 4458 | | - messages: async () => ({ data: [] }), | | 4471 | + message: async () => ({ | | 4472 | + response: { status: 404 }, | | 4473 | + error: { name: "NotFoundError" }, | | 4474 | + }), | 4459 | 4475 | children: async () => ({ data: [] }), | 4460 | 4476 | abort: async () => { | 4461 | 4477 | abortCalls += 1; | @@ -4543,7 +4559,7 @@ describe("OpenCodeAdapterV2", () => { | 4543 | 4559 | createdBy: "user", | 4544 | 4560 | creationSource: "web", | 4545 | 4561 | messageId: MessageId.make("message-opencode-initial-stop"), | 4546 | | - text: "hello", | | 4562 | + text: stage === "preparing" ? "/hello" : "hello", | 4547 | 4563 | attachments: [], | 4548 | 4564 | }, | 4549 | 4565 | modelSelection, | @@ -4573,8 +4589,14 @@ describe("OpenCodeAdapterV2", () => { | 4573 | 4589 | const events = Array.from(yield* Fiber.join(terminalEvents)); | 4574 | 4590 | const terminals = events.filter((event) => event.type === "turn.terminal"); | 4575 | 4591 | assert.equal(abortCalls, 1); | | 4592 | + assert.equal(promptCalls, stage === "preparing" ? 0 : 1); | 4576 | 4593 | assert.lengthOf(terminals, 1); | 4577 | 4594 | assert.equal(terminals[0]?.status, "interrupted"); | | 4595 | + assert.equal(terminals[0]?.promptDropped, stage === "preparing" ? true : undefined); | | 4596 | + const finalTurn = events | | 4597 | + .filter((event) => event.type === "provider_turn.updated") | | 4598 | + .findLast((event) => event.providerTurn.status === "interrupted"); | | 4599 | + assert.equal(finalTurn?.providerTurn.promptDropped, stage === "preparing" ? true : undefined); | 4578 | 4600 | assert.isNull(terminals[0]?.failure ?? null); | 4579 | 4601 | assert.isFalse( | 4580 | 4602 | events.some( | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/adapter.ts +28-10Lines changed: 28 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -310,6 +310,7 @@ interface ActiveOpenCodeTurn { | 310 | 310 | readonly providerTurn: OrchestrationV2ProviderTurn; | 311 | 311 | nextItemOrdinal: number; | 312 | 312 | nativeUserMessageId: string | null; | | 313 | + unsentRootPromptId: string | null; | 313 | 314 | admissionMessageId: string | null; | 314 | 315 | readonly priorWakePromptIds: Set<string>; | 315 | 316 | priorWakeSettled: Deferred.Deferred<void> | null; | @@ -1275,6 +1276,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 1275 | 1276 | turn: ActiveOpenCodeTurn, | 1276 | 1277 | status: OrchestrationV2ProviderTurn["status"], | 1277 | 1278 | completedAt: DateTime.Utc | null, | | 1279 | + promptDropped = false, | 1278 | 1280 | ) => { | 1279 | 1281 | const providerTurn: OrchestrationV2ProviderTurn = { | 1280 | 1282 | ...turn.providerTurn, | @@ -1284,6 +1286,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 1284 | 1286 | : providerRef(turn.nativeUserMessageId, "weak"), | 1285 | 1287 | status, | 1286 | 1288 | completedAt, | | 1289 | + ...(promptDropped ? { promptDropped: true } : {}), | 1287 | 1290 | ...(completedAt === null | 1288 | 1291 | ? {} | 1289 | 1292 | : { | @@ -2312,7 +2315,9 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 2312 | 2315 | ), | 2313 | 2316 | ).pipe(Effect.exit); | 2314 | 2317 | } | 2315 | | - yield* emitProviderTurn(state, turn, status, completedAt); | | 2318 | + // OpenCode can save a submitted prompt after acknowledging Stop. | | 2319 | + const promptDropped = status === "interrupted" && turn.unsentRootPromptId !== null; | | 2320 | + yield* emitProviderTurn(state, turn, status, completedAt, promptDropped); | 2316 | 2321 | const threadDisposition = terminal?.threadDisposition ?? "reusable"; | 2317 | 2322 | yield* updateProviderThread(state, { | 2318 | 2323 | status: turn.isRoot ? "active" : threadDisposition === "broken" ? "error" : "idle", | @@ -2382,6 +2387,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 2382 | 2387 | runOrdinal: turn.runOrdinal, | 2383 | 2388 | status, | 2384 | 2389 | failure: null, | | 2390 | + ...(promptDropped ? { promptDropped: true as const } : {}), | 2385 | 2391 | threadDisposition, | 2386 | 2392 | }, | 2387 | 2393 | ); | @@ -2528,6 +2534,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 2528 | 2534 | providerTurn, | 2529 | 2535 | nextItemOrdinal: 1, | 2530 | 2536 | nativeUserMessageId: message.id, | | 2537 | + unsentRootPromptId: null, | 2531 | 2538 | admissionMessageId: null, | 2532 | 2539 | priorWakePromptIds: new Set(), | 2533 | 2540 | priorWakeSettled: null, | @@ -3537,6 +3544,16 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 3537 | 3544 | abortController: AbortController, | 3538 | 3545 | messageText: string, | 3539 | 3546 | ) { | | 3547 | + const sendIfLive = | | 3548 | + <A>(send: (signal: AbortSignal) => Promise<A>) => | | 3549 | + async (signal: AbortSignal) => { | | 3550 | + const requestSignal = AbortSignal.any([signal, abortController.signal]); | | 3551 | + if (requestSignal.aborted || !turnIsLive(state, turn)) return; | | 3552 | + if (turn.unsentRootPromptId === inputPayload.messageID) { | | 3553 | + turn.unsentRootPromptId = null; | | 3554 | + } | | 3555 | + return send(requestSignal); | | 3556 | + }; | 3540 | 3557 | const { variant, ...payloadWithoutVariant } = inputPayload; | 3541 | 3558 | const selectedVariant = yield* OpenCodeRuntime.resolveOpenCodeVariant( | 3542 | 3559 | client, | @@ -3551,16 +3568,16 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 3551 | 3568 | }; | 3552 | 3569 | const match = messageText.trim().match(/^\/(\S+)(?:\s+([\s\S]*))?$/); | 3553 | 3570 | const command = match | 3554 | | - ? (yield* OpenCodeRuntime.loadOpenCodeCommands(client).pipe( | | 3571 | + ? (yield* OpenCodeRuntime.loadOpenCodeCommands(client, abortController.signal).pipe( | 3555 | 3572 | Effect.timeout("10 seconds"), | 3556 | 3573 | Effect.orElseSucceed(() => []), | 3557 | 3574 | )).find((entry) => entry.name === match[1]) | 3558 | 3575 | : undefined; | 3559 | 3576 | if (!command) { | 3560 | | - return yield* sdkCall("session.promptAsync", payload, (signal) => | 3561 | | - client.session.promptAsync(payload, { | 3562 | | - signal: AbortSignal.any([signal, abortController.signal]), | 3563 | | - }), | | 3577 | + return yield* sdkCall( | | 3578 | + "session.promptAsync", | | 3579 | + payload, | | 3580 | + sendIfLive((signal) => client.session.promptAsync(payload, { signal })), | 3564 | 3581 | ).pipe(Effect.asVoid); | 3565 | 3582 | } | 3566 | 3583 | const receipt = Deferred.makeUnsafe<void>(); | @@ -3582,10 +3599,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 3582 | 3599 | ...(payload.variant ? { variant: payload.variant } : {}), | 3583 | 3600 | parts: payload.parts?.filter((part) => part.type === "file") ?? [], | 3584 | 3601 | }; | 3585 | | - const request = yield* sdkCall("session.command", commandPayload, (signal) => | 3586 | | - client.session.command(commandPayload, { | 3587 | | - signal: AbortSignal.any([signal, abortController.signal]), | 3588 | | - }), | | 3602 | + const request = yield* sdkCall( | | 3603 | + "session.command", | | 3604 | + commandPayload, | | 3605 | + sendIfLive((signal) => client.session.command(commandPayload, { signal })), | 3589 | 3606 | ).pipe( | 3590 | 3607 | Effect.asVoid, | 3591 | 3608 | // OpenCode answers only when generation ends; once admitted, the event stream owns the outcome. | @@ -3804,6 +3821,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 3804 | 3821 | providerTurn, | 3805 | 3822 | nextItemOrdinal: turnInput.providerTurnOrdinal * 100 + 1, | 3806 | 3823 | nativeUserMessageId: null, | | 3824 | + unsentRootPromptId: isCompaction ? null : admissionMessageId, | 3807 | 3825 | admissionMessageId, | 3808 | 3826 | priorWakePromptIds: new Set(), | 3809 | 3827 | priorWakeSettled: null, | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/v2/adapter.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2406,6 +2406,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 2406 | 2406 | ) { | 2407 | 2407 | const turn = state.active; | 2408 | 2408 | if (turn === undefined) return; | | 2409 | + const promptDropped = terminal.status === "interrupted" && turn.unsent; | 2409 | 2410 | state.active = undefined; | 2410 | 2411 | for (const inboxID of turn.steers) state.strandedSteers.add(inboxID); | 2411 | 2412 | for (const entry of pending.values()) { | @@ -2472,6 +2473,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 2472 | 2473 | ...turn.providerTurn, | 2473 | 2474 | status: terminal.status, | 2474 | 2475 | completedAt, | | 2476 | + ...(promptDropped ? { promptDropped: true } : {}), | 2475 | 2477 | turnTokenUsage: turnTokenUsage(turn, terminal.status), | 2476 | 2478 | ...(lastStep === undefined | 2477 | 2479 | ? {} | @@ -2525,6 +2527,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 2525 | 2527 | providerTurnId: turn.providerTurn.id, | 2526 | 2528 | runOrdinal: turn.input.runOrdinal, | 2527 | 2529 | threadDisposition, | | 2530 | + ...(promptDropped ? { promptDropped: true as const } : {}), | 2528 | 2531 | ...retainedRequestsOf(turn), | 2529 | 2532 | }; | 2530 | 2533 | yield* emit( | |
|