GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(opencode): recover prompts dropped by an early Stop (#43359) · patroza/t3code@cc61cdb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/orchestration-v2/Adapters
    • OpenCode2AdapterV2.test.ts
  • packages/provider-opencode/src/server
    • OpenCodeRuntime.ts
    • adapter.test.ts
    • adapter.ts
    • v2
      • adapter.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

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 numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
924924
});
925925
yield* Deferred.succeed(release, undefined);
926926
yield* Fiber.join(started);
927-
assert.equal((yield* Fiber.join(terminal))?.status, "interrupted");
927+
assert.include(yield* Fiber.join(terminal), { status: "interrupted", promptDropped: true });
928928
assert.lengthOf(prompts, 0);
929929
}),
930930
);
Collapse file

‎packages/provider-opencode/src/server/OpenCodeRuntime.ts‎

Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/OpenCodeRuntime.ts
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,12 @@ export interface OpenCodeInventory {
219219

220220
export type OpenCodeSlashCommand = Pick<Command, "name" | "description" | "source" | "hints">;
221221

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(
224228
Effect.map((result): ReadonlyArray<OpenCodeSlashCommand> =>
225229
(result.data ?? []).map(({ name, description, source, hints }) => ({
226230
name,
Collapse file

‎packages/provider-opencode/src/server/adapter.test.ts‎

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 numberDiff line numberDiff line change
@@ -4420,13 +4420,25 @@ describe("OpenCodeAdapterV2", () => {
44204420
),
44214421
);
44224422

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) =>
44244424
Effect.gen(function* () {
44254425
const nativeEvents = asyncEventStream();
44264426
const promptStarted = promiseGate<void>();
44274427
const abortCalled = promiseGate<void>();
44284428
let abortCalls = 0;
4429+
let promptCalls = 0;
44294430
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+
},
44304442
event: {
44314443
subscribe: async (_input: unknown, options: { signal?: AbortSignal }) => {
44324444
options.signal?.addEventListener("abort", () => nativeEvents.close(), { once: true });
@@ -4441,6 +4453,7 @@ describe("OpenCodeAdapterV2", () => {
44414453
data: { id: "native-opencode-initial-stop", time: { created: 1, updated: 1 } },
44424454
}),
44434455
promptAsync: async (_input: unknown, options?: { readonly signal?: AbortSignal }) => {
4456+
promptCalls += 1;
44444457
promptStarted.resolve();
44454458
await new Promise<void>((_resolve, reject) => {
44464459
const signal = options?.signal;
@@ -4455,7 +4468,10 @@ describe("OpenCodeAdapterV2", () => {
44554468
status: async () => ({
44564469
data: { "native-opencode-initial-stop": { type: "idle" as const } },
44574470
}),
4458-
messages: async () => ({ data: [] }),
4471+
message: async () => ({
4472+
response: { status: 404 },
4473+
error: { name: "NotFoundError" },
4474+
}),
44594475
children: async () => ({ data: [] }),
44604476
abort: async () => {
44614477
abortCalls += 1;
@@ -4543,7 +4559,7 @@ describe("OpenCodeAdapterV2", () => {
45434559
createdBy: "user",
45444560
creationSource: "web",
45454561
messageId: MessageId.make("message-opencode-initial-stop"),
4546-
text: "hello",
4562+
text: stage === "preparing" ? "/hello" : "hello",
45474563
attachments: [],
45484564
},
45494565
modelSelection,
@@ -4573,8 +4589,14 @@ describe("OpenCodeAdapterV2", () => {
45734589
const events = Array.from(yield* Fiber.join(terminalEvents));
45744590
const terminals = events.filter((event) => event.type === "turn.terminal");
45754591
assert.equal(abortCalls, 1);
4592+
assert.equal(promptCalls, stage === "preparing" ? 0 : 1);
45764593
assert.lengthOf(terminals, 1);
45774594
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);
45784600
assert.isNull(terminals[0]?.failure ?? null);
45794601
assert.isFalse(
45804602
events.some(
Collapse file

‎packages/provider-opencode/src/server/adapter.ts‎

Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/adapter.ts
+28-10Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ interface ActiveOpenCodeTurn {
310310
readonly providerTurn: OrchestrationV2ProviderTurn;
311311
nextItemOrdinal: number;
312312
nativeUserMessageId: string | null;
313+
unsentRootPromptId: string | null;
313314
admissionMessageId: string | null;
314315
readonly priorWakePromptIds: Set<string>;
315316
priorWakeSettled: Deferred.Deferred<void> | null;
@@ -1275,6 +1276,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
12751276
turn: ActiveOpenCodeTurn,
12761277
status: OrchestrationV2ProviderTurn["status"],
12771278
completedAt: DateTime.Utc | null,
1279+
promptDropped = false,
12781280
) => {
12791281
const providerTurn: OrchestrationV2ProviderTurn = {
12801282
...turn.providerTurn,
@@ -1284,6 +1286,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
12841286
: providerRef(turn.nativeUserMessageId, "weak"),
12851287
status,
12861288
completedAt,
1289+
...(promptDropped ? { promptDropped: true } : {}),
12871290
...(completedAt === null
12881291
? {}
12891292
: {
@@ -2312,7 +2315,9 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
23122315
),
23132316
).pipe(Effect.exit);
23142317
}
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);
23162321
const threadDisposition = terminal?.threadDisposition ?? "reusable";
23172322
yield* updateProviderThread(state, {
23182323
status: turn.isRoot ? "active" : threadDisposition === "broken" ? "error" : "idle",
@@ -2382,6 +2387,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
23822387
runOrdinal: turn.runOrdinal,
23832388
status,
23842389
failure: null,
2390+
...(promptDropped ? { promptDropped: true as const } : {}),
23852391
threadDisposition,
23862392
},
23872393
);
@@ -2528,6 +2534,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
25282534
providerTurn,
25292535
nextItemOrdinal: 1,
25302536
nativeUserMessageId: message.id,
2537+
unsentRootPromptId: null,
25312538
admissionMessageId: null,
25322539
priorWakePromptIds: new Set(),
25332540
priorWakeSettled: null,
@@ -3537,6 +3544,16 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
35373544
abortController: AbortController,
35383545
messageText: string,
35393546
) {
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+
};
35403557
const { variant, ...payloadWithoutVariant } = inputPayload;
35413558
const selectedVariant = yield* OpenCodeRuntime.resolveOpenCodeVariant(
35423559
client,
@@ -3551,16 +3568,16 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
35513568
};
35523569
const match = messageText.trim().match(/^\/(\S+)(?:\s+([\s\S]*))?$/);
35533570
const command = match
3554-
? (yield* OpenCodeRuntime.loadOpenCodeCommands(client).pipe(
3571+
? (yield* OpenCodeRuntime.loadOpenCodeCommands(client, abortController.signal).pipe(
35553572
Effect.timeout("10 seconds"),
35563573
Effect.orElseSucceed(() => []),
35573574
)).find((entry) => entry.name === match[1])
35583575
: undefined;
35593576
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 })),
35643581
).pipe(Effect.asVoid);
35653582
}
35663583
const receipt = Deferred.makeUnsafe<void>();
@@ -3582,10 +3599,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
35823599
...(payload.variant ? { variant: payload.variant } : {}),
35833600
parts: payload.parts?.filter((part) => part.type === "file") ?? [],
35843601
};
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 })),
35893606
).pipe(
35903607
Effect.asVoid,
35913608
// 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
38043821
providerTurn,
38053822
nextItemOrdinal: turnInput.providerTurnOrdinal * 100 + 1,
38063823
nativeUserMessageId: null,
3824+
unsentRootPromptId: isCompaction ? null : admissionMessageId,
38073825
admissionMessageId,
38083826
priorWakePromptIds: new Set(),
38093827
priorWakeSettled: null,
Collapse file

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/v2/adapter.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,6 +2406,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
24062406
) {
24072407
const turn = state.active;
24082408
if (turn === undefined) return;
2409+
const promptDropped = terminal.status === "interrupted" && turn.unsent;
24092410
state.active = undefined;
24102411
for (const inboxID of turn.steers) state.strandedSteers.add(inboxID);
24112412
for (const entry of pending.values()) {
@@ -2472,6 +2473,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
24722473
...turn.providerTurn,
24732474
status: terminal.status,
24742475
completedAt,
2476+
...(promptDropped ? { promptDropped: true } : {}),
24752477
turnTokenUsage: turnTokenUsage(turn, terminal.status),
24762478
...(lastStep === undefined
24772479
? {}
@@ -2525,6 +2527,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
25252527
providerTurnId: turn.providerTurn.id,
25262528
runOrdinal: turn.input.runOrdinal,
25272529
threadDisposition,
2530+
...(promptDropped ? { promptDropped: true as const } : {}),
25282531
...retainedRequestsOf(turn),
25292532
};
25302533
yield* emit(

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.