Skip to content

Commit 5d0de82

Browse files
only21milclaudemaria-rcks
authored
fix(opencode): OpenCode 1.x text generation deletes its scratch session (#28294)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 3d10957 commit 5d0de82

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const runtimeMock = {
2828
authHeaders: [] as Array<string | null>,
2929
closeCalls: [] as string[],
3030
sessionCreateCalls: 0,
31-
abortCalls: [] as string[],
31+
sessionCalls: [] as string[],
3232
onSessionCreate: undefined as ((signal?: AbortSignal) => Promise<never>) | undefined,
3333
onPrompt: undefined as
3434
| ((signal?: AbortSignal) => Promise<{ data: { parts: ReadonlyArray<unknown> } }>)
@@ -49,7 +49,7 @@ const runtimeMock = {
4949
this.state.authHeaders.length = 0;
5050
this.state.closeCalls.length = 0;
5151
this.state.sessionCreateCalls = 0;
52-
this.state.abortCalls.length = 0;
52+
this.state.sessionCalls.length = 0;
5353
this.state.onSessionCreate = undefined;
5454
this.state.onPrompt = undefined;
5555
this.state.onAbort = undefined;
@@ -121,10 +121,14 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntime.OpenCodeRuntime["Service"] = {
121121
input: { readonly sessionID: string },
122122
options?: { readonly signal?: AbortSignal },
123123
) => {
124-
runtimeMock.state.abortCalls.push(input.sessionID);
124+
runtimeMock.state.sessionCalls.push(`abort ${input.sessionID}`);
125125
if (runtimeMock.state.onAbort) return runtimeMock.state.onAbort(options?.signal);
126126
return { data: true };
127127
},
128+
delete: async (input: { readonly sessionID: string }) => {
129+
runtimeMock.state.sessionCalls.push(`delete ${input.sessionID}`);
130+
return { data: true };
131+
},
128132
prompt: async (
129133
input: { readonly parts: ReadonlyArray<unknown> },
130134
options?: { readonly signal?: AbortSignal },
@@ -262,7 +266,7 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
262266
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
263267
expect(signal?.aborted).toBe(true);
264268
expect(runtimeMock.state.promptUrls).toEqual([]);
265-
expect(runtimeMock.state.abortCalls).toEqual([]);
269+
expect(runtimeMock.state.sessionCalls).toEqual([]);
266270
}),
267271
),
268272
);
@@ -298,7 +302,10 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
298302
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
299303
expect(signal?.aborted).toBe(true);
300304
expect(otherSignal?.aborted).toBe(false);
301-
expect(runtimeMock.state.abortCalls).toEqual(["cancelled-session"]);
305+
expect(runtimeMock.state.sessionCalls).toEqual([
306+
"abort cancelled-session",
307+
"delete cancelled-session",
308+
]);
302309
expect(runtimeMock.state.closeCalls).toEqual([]);
303310

304311
otherReply.resolve({
@@ -312,7 +319,11 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
312319
"http://127.0.0.1:4301",
313320
"http://127.0.0.1:4301",
314321
]);
315-
expect(runtimeMock.state.abortCalls).toEqual(["cancelled-session"]);
322+
expect(runtimeMock.state.sessionCalls).toEqual([
323+
"abort cancelled-session",
324+
"delete cancelled-session",
325+
"delete other-session",
326+
]);
316327
expect(runtimeMock.state.closeCalls).toEqual([]);
317328
}),
318329
),
@@ -397,6 +408,22 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
397408
textPartCount: 1,
398409
});
399410
expect(error.cause).not.toHaveProperty("cause");
411+
expect(runtimeMock.state.sessionCalls).toEqual(["delete http://127.0.0.1:4301/session"]);
412+
}),
413+
),
414+
);
415+
416+
it.effect("deletes the session when the prompt request rejects", () =>
417+
withOpenCodeTextGeneration(DEFAULT_OPENCODE_SETTINGS, (textGeneration) =>
418+
Effect.gen(function* () {
419+
runtimeMock.state.promptRequestError = new Error("prompt transport failed");
420+
421+
const error = yield* textGeneration
422+
.generateCommitMessage(DEFAULT_COMMIT_MESSAGE_INPUT)
423+
.pipe(Effect.flip);
424+
425+
expect(error.detail).toBe("OpenCode session.prompt request failed.");
426+
expect(runtimeMock.state.sessionCalls).toEqual(["delete http://127.0.0.1:4301/session"]);
400427
}),
401428
),
402429
);
@@ -484,6 +511,10 @@ it.layer(layerOpenCodeTextGenerationExistingServerTest)(
484511
});
485512

486513
expect(runtimeMock.state.startCalls).toEqual([]);
514+
expect(runtimeMock.state.sessionCalls).toEqual([
515+
"delete http://127.0.0.1:9999/session",
516+
"delete http://127.0.0.1:9999/session",
517+
]);
487518
expect(runtimeMock.state.promptUrls).toEqual([
488519
"http://127.0.0.1:9999",
489520
"http://127.0.0.1:9999",

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
211211
cwd: input.cwd,
212212
});
213213
}
214+
yield* Effect.addFinalizer(() =>
215+
OpenCodeRuntime.runOpenCodeSdk("session.delete", (signal) =>
216+
client.session.delete({ sessionID: session.data.id }, { signal }),
217+
).pipe(Effect.timeout("5 seconds"), Effect.ignore({ log: "Warn" })),
218+
);
214219
const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent");
215220
const selectedVariant = getModelSelectionStringOptionValue(input.modelSelection, "variant");
216221
const promptContext = {
@@ -275,6 +280,7 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
275280
}
276281
return rawText;
277282
},
283+
Effect.scoped,
278284
Effect.catchTags({
279285
OpenCodeTextGenerationSessionRequestError: (cause) =>
280286
Effect.fail(

0 commit comments

Comments
 (0)