Skip to content
TypeScript

fix(opencode): OpenCode 1.x text generation deletes its scratch session (#28294)

Victor Vogel5d0de82dcb43
diff --git a/packages/provider-opencode/src/server/textGeneration.test.ts b/packages/provider-opencode/src/server/textGeneration.test.ts
index 9b9c2525190..634956f6b55 100644
--- a/packages/provider-opencode/src/server/textGeneration.test.ts
+++ b/packages/provider-opencode/src/server/textGeneration.test.ts
@@ -28,7 +28,7 @@ const runtimeMock = {
authHeaders: [] as Array<string | null>,
closeCalls: [] as string[],
sessionCreateCalls: 0,
- abortCalls: [] as string[],
+ sessionCalls: [] as string[],
onSessionCreate: undefined as ((signal?: AbortSignal) => Promise<never>) | undefined,
onPrompt: undefined as
| ((signal?: AbortSignal) => Promise<{ data: { parts: ReadonlyArray<unknown> } }>)
@@ -49,7 +49,7 @@ const runtimeMock = {
this.state.authHeaders.length = 0;
this.state.closeCalls.length = 0;
this.state.sessionCreateCalls = 0;
- this.state.abortCalls.length = 0;
+ this.state.sessionCalls.length = 0;
this.state.onSessionCreate = undefined;
this.state.onPrompt = undefined;
this.state.onAbort = undefined;
@@ -121,10 +121,14 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntime.OpenCodeRuntime["Service"] = {
input: { readonly sessionID: string },
options?: { readonly signal?: AbortSignal },
) => {
- runtimeMock.state.abortCalls.push(input.sessionID);
+ runtimeMock.state.sessionCalls.push(`abort ${input.sessionID}`);
if (runtimeMock.state.onAbort) return runtimeMock.state.onAbort(options?.signal);
return { data: true };
},
+ delete: async (input: { readonly sessionID: string }) => {
+ runtimeMock.state.sessionCalls.push(`delete ${input.sessionID}`);
+ return { data: true };
+ },
prompt: async (
input: { readonly parts: ReadonlyArray<unknown> },
options?: { readonly signal?: AbortSignal },
@@ -262,7 +266,7 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
expect(signal?.aborted).toBe(true);
expect(runtimeMock.state.promptUrls).toEqual([]);
- expect(runtimeMock.state.abortCalls).toEqual([]);
+ expect(runtimeMock.state.sessionCalls).toEqual([]);
}),
),
);
@@ -298,7 +302,10 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
expect(signal?.aborted).toBe(true);
expect(otherSignal?.aborted).toBe(false);
- expect(runtimeMock.state.abortCalls).toEqual(["cancelled-session"]);
+ expect(runtimeMock.state.sessionCalls).toEqual([
+ "abort cancelled-session",
+ "delete cancelled-session",
+ ]);
expect(runtimeMock.state.closeCalls).toEqual([]);
otherReply.resolve({
@@ -312,7 +319,11 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
"http://127.0.0.1:4301",
"http://127.0.0.1:4301",
]);
- expect(runtimeMock.state.abortCalls).toEqual(["cancelled-session"]);
+ expect(runtimeMock.state.sessionCalls).toEqual([
+ "abort cancelled-session",
+ "delete cancelled-session",
+ "delete other-session",
+ ]);
expect(runtimeMock.state.closeCalls).toEqual([]);
}),
),
@@ -397,6 +408,22 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
textPartCount: 1,
});
expect(error.cause).not.toHaveProperty("cause");
+ expect(runtimeMock.state.sessionCalls).toEqual(["delete http://127.0.0.1:4301/session"]);
+ }),
+ ),
+ );
+
+ it.effect("deletes the session when the prompt request rejects", () =>
+ withOpenCodeTextGeneration(DEFAULT_OPENCODE_SETTINGS, (textGeneration) =>
+ Effect.gen(function* () {
+ runtimeMock.state.promptRequestError = new Error("prompt transport failed");
+
+ const error = yield* textGeneration
+ .generateCommitMessage(DEFAULT_COMMIT_MESSAGE_INPUT)
+ .pipe(Effect.flip);
+
+ expect(error.detail).toBe("OpenCode session.prompt request failed.");
+ expect(runtimeMock.state.sessionCalls).toEqual(["delete http://127.0.0.1:4301/session"]);
}),
),
);
@@ -484,6 +511,10 @@ it.layer(layerOpenCodeTextGenerationExistingServerTest)(
});
expect(runtimeMock.state.startCalls).toEqual([]);
+ expect(runtimeMock.state.sessionCalls).toEqual([
+ "delete http://127.0.0.1:9999/session",
+ "delete http://127.0.0.1:9999/session",
+ ]);
expect(runtimeMock.state.promptUrls).toEqual([
"http://127.0.0.1:9999",
"http://127.0.0.1:9999",
diff --git a/packages/provider-opencode/src/server/textGeneration.ts b/packages/provider-opencode/src/server/textGeneration.ts
index eed819e44d6..7e350dc9b68 100644
--- a/packages/provider-opencode/src/server/textGeneration.ts
+++ b/packages/provider-opencode/src/server/textGeneration.ts
@@ -211,6 +211,11 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
cwd: input.cwd,
});
}
+ yield* Effect.addFinalizer(() =>
+ OpenCodeRuntime.runOpenCodeSdk("session.delete", (signal) =>
+ client.session.delete({ sessionID: session.data.id }, { signal }),
+ ).pipe(Effect.timeout("5 seconds"), Effect.ignore({ log: "Warn" })),
+ );
const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent");
const selectedVariant = getModelSelectionStringOptionValue(input.modelSelection, "variant");
const promptContext = {
@@ -275,6 +280,7 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
}
return rawText;
},
+ Effect.scoped,
Effect.catchTags({
OpenCodeTextGenerationSessionRequestError: (cause) =>
Effect.fail(