Skip to content

Commit 9f2bada

Browse files
committed
fix(opencode): stop cancelled text generation sessions
1 parent 09e8de9 commit 9f2bada

2 files changed

Lines changed: 160 additions & 16 deletions

File tree

‎apps/server/src/textGeneration/OpenCodeTextGeneration.test.ts‎

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { OpenCodeSettings, ProviderInstanceId, TextGenerationError } from "@t3tools/contracts";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { it } from "@effect/vitest";
4+
import * as Cause from "effect/Cause";
45
import * as Duration from "effect/Duration";
56
import * as Effect from "effect/Effect";
7+
import * as Exit from "effect/Exit";
8+
import * as Fiber from "effect/Fiber";
69
import * as Layer from "effect/Layer";
710
import * as Schema from "effect/Schema";
811
import * as TestClock from "effect/testing/TestClock";
@@ -23,6 +26,10 @@ const runtimeMock = {
2326
authHeaders: [] as Array<string | null>,
2427
closeCalls: [] as string[],
2528
sessionCreateCalls: 0,
29+
abortCalls: [] as string[],
30+
onSessionCreate: undefined as ((signal?: AbortSignal) => Promise<never>) | undefined,
31+
onPrompt: undefined as ((signal?: AbortSignal) => Promise<never>) | undefined,
32+
onAbort: undefined as ((signal?: AbortSignal) => Promise<never>) | undefined,
2633
connectionError: undefined as Error | undefined,
2734
sessionCreateError: undefined as unknown,
2835
sessionResult: undefined as { data?: { id: string } } | undefined,
@@ -38,6 +45,10 @@ const runtimeMock = {
3845
this.state.authHeaders.length = 0;
3946
this.state.closeCalls.length = 0;
4047
this.state.sessionCreateCalls = 0;
48+
this.state.abortCalls.length = 0;
49+
this.state.onSessionCreate = undefined;
50+
this.state.onPrompt = undefined;
51+
this.state.onAbort = undefined;
4152
this.state.connectionError = undefined;
4253
this.state.sessionCreateError = undefined;
4354
this.state.sessionResult = undefined;
@@ -94,19 +105,34 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntime.OpenCodeRuntimeShape = {
94105
createOpenCodeSdkClient: ({ baseUrl, serverPassword }) =>
95106
({
96107
session: {
97-
create: async () => {
108+
create: async (_input: unknown, options?: { readonly signal?: AbortSignal }) => {
98109
runtimeMock.state.sessionCreateCalls += 1;
110+
if (runtimeMock.state.onSessionCreate) {
111+
return runtimeMock.state.onSessionCreate(options?.signal);
112+
}
99113
if (runtimeMock.state.sessionCreateError !== undefined) {
100114
throw runtimeMock.state.sessionCreateError;
101115
}
102116
return runtimeMock.state.sessionResult ?? { data: { id: `${baseUrl}/session` } };
103117
},
104-
prompt: async (input: { readonly parts: ReadonlyArray<unknown> }) => {
118+
abort: async (
119+
input: { readonly sessionID: string },
120+
options?: { readonly signal?: AbortSignal },
121+
) => {
122+
runtimeMock.state.abortCalls.push(input.sessionID);
123+
if (runtimeMock.state.onAbort) return runtimeMock.state.onAbort(options?.signal);
124+
return { data: true };
125+
},
126+
prompt: async (
127+
input: { readonly parts: ReadonlyArray<unknown> },
128+
options?: { readonly signal?: AbortSignal },
129+
) => {
105130
runtimeMock.state.promptUrls.push(baseUrl);
106131
runtimeMock.state.promptParts.push(input.parts);
107132
runtimeMock.state.authHeaders.push(
108133
serverPassword ? `Basic ${btoa(`opencode:${serverPassword}`)}` : null,
109134
);
135+
if (runtimeMock.state.onPrompt) return runtimeMock.state.onPrompt(options?.signal);
110136
if (runtimeMock.state.promptRequestError !== undefined) {
111137
throw runtimeMock.state.promptRequestError;
112138
}
@@ -235,6 +261,101 @@ const advanceIdleClock = Effect.gen(function* () {
235261
});
236262

237263
it.layer(OpenCodeTextGenerationTestLayer)("OpenCodeTextGeneration", (it) => {
264+
it.effect.each(["local", "external"] as const)(
265+
"cancels an in-flight session creation on the %s server",
266+
(server) =>
267+
withOpenCodeTextGeneration(
268+
server === "local" ? DEFAULT_OPENCODE_SETTINGS : EXISTING_SERVER_OPENCODE_SETTINGS,
269+
(textGeneration) =>
270+
Effect.gen(function* () {
271+
const started = Promise.withResolvers<AbortSignal | undefined>();
272+
runtimeMock.state.onSessionCreate = (signal) => {
273+
started.resolve(signal);
274+
return new Promise<never>(() => {});
275+
};
276+
const fiber = yield* textGeneration
277+
.generateCommitMessage(DEFAULT_COMMIT_MESSAGE_INPUT)
278+
.pipe(Effect.forkChild);
279+
const signal = yield* Effect.promise(() => started.promise);
280+
yield* Fiber.interrupt(fiber);
281+
282+
expect(signal?.aborted).toBe(true);
283+
expect(runtimeMock.state.promptUrls).toEqual([]);
284+
expect(runtimeMock.state.abortCalls).toEqual([]);
285+
}),
286+
),
287+
);
288+
289+
it.effect.each(["local", "external"] as const)(
290+
"cancels the prompt and stops only its session on the %s server",
291+
(server) =>
292+
withOpenCodeTextGeneration(
293+
server === "local" ? DEFAULT_OPENCODE_SETTINGS : EXISTING_SERVER_OPENCODE_SETTINGS,
294+
(textGeneration) =>
295+
Effect.gen(function* () {
296+
const started = Promise.withResolvers<AbortSignal | undefined>();
297+
runtimeMock.state.onPrompt = (signal) => {
298+
started.resolve(signal);
299+
return new Promise<never>(() => {});
300+
};
301+
const fiber = yield* textGeneration
302+
.generateCommitMessage(DEFAULT_COMMIT_MESSAGE_INPUT)
303+
.pipe(Effect.forkChild);
304+
const signal = yield* Effect.promise(() => started.promise);
305+
yield* Fiber.interrupt(fiber);
306+
307+
expect(signal?.aborted).toBe(true);
308+
expect(runtimeMock.state.abortCalls).toEqual([
309+
`${runtimeMock.state.promptUrls[0]}/session`,
310+
]);
311+
expect(runtimeMock.state.closeCalls).toEqual([]);
312+
313+
runtimeMock.state.onPrompt = undefined;
314+
const result = yield* textGeneration.generateCommitMessage(
315+
DEFAULT_COMMIT_MESSAGE_INPUT,
316+
);
317+
expect(result.subject).toBe("Improve OpenCode reuse");
318+
expect(runtimeMock.state.abortCalls).toHaveLength(1);
319+
}),
320+
),
321+
);
322+
323+
it.effect.each(["failure", "timeout"] as const)(
324+
"preserves cancellation when the session abort ends in %s",
325+
(outcome) =>
326+
withOpenCodeTextGeneration(EXISTING_SERVER_OPENCODE_SETTINGS, (textGeneration) =>
327+
Effect.gen(function* () {
328+
const promptStarted = Promise.withResolvers<void>();
329+
const abortStarted = Promise.withResolvers<AbortSignal | undefined>();
330+
runtimeMock.state.onPrompt = () => {
331+
promptStarted.resolve();
332+
return new Promise<never>(() => {});
333+
};
334+
runtimeMock.state.onAbort = (signal) => {
335+
abortStarted.resolve(signal);
336+
return outcome === "failure"
337+
? Promise.reject(new Error("Server disconnected"))
338+
: new Promise<never>(() => {});
339+
};
340+
const fiber = yield* textGeneration
341+
.generateCommitMessage(DEFAULT_COMMIT_MESSAGE_INPUT)
342+
.pipe(Effect.forkChild);
343+
yield* Effect.promise(() => promptStarted.promise);
344+
const cancellation = yield* Fiber.interrupt(fiber).pipe(Effect.forkChild);
345+
const abortSignal = yield* Effect.promise(() => abortStarted.promise);
346+
if (outcome === "timeout") {
347+
yield* TestClock.adjust("10 seconds");
348+
}
349+
yield* Fiber.join(cancellation);
350+
351+
const exit = yield* Fiber.await(fiber);
352+
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
353+
expect(runtimeMock.state.abortCalls).toEqual(["http://127.0.0.1:9999/session"]);
354+
if (outcome === "timeout") expect(abortSignal?.aborted).toBe(true);
355+
}),
356+
),
357+
);
358+
238359
it.effect("excludes generic files from thread title generation", () =>
239360
withOpenCodeTextGeneration(DEFAULT_OPENCODE_SETTINGS, (textGeneration) =>
240361
Effect.gen(function* () {

‎apps/server/src/textGeneration/OpenCodeTextGeneration.ts‎

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,14 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
212212
...(server.serverPassword !== undefined ? { serverPassword: server.serverPassword } : {}),
213213
});
214214
const session = yield* Effect.tryPromise({
215-
try: () =>
216-
client.session.create({
217-
title: `T3 Code ${input.operation}`,
218-
permission: [{ permission: "*", pattern: "*", action: "deny" }],
219-
}),
215+
try: (signal) =>
216+
client.session.create(
217+
{
218+
title: `T3 Code ${input.operation}`,
219+
permission: [{ permission: "*", pattern: "*", action: "deny" }],
220+
},
221+
{ signal },
222+
),
220223
catch: (cause) =>
221224
new OpenCodeTextGenerationSessionRequestError({
222225
operation: input.operation,
@@ -241,20 +244,40 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
241244
};
242245

243246
const result = yield* Effect.tryPromise({
244-
try: () =>
245-
client.session.prompt({
246-
sessionID: session.data.id,
247-
model: parsedModel,
248-
...(selectedAgent ? { agent: selectedAgent } : {}),
249-
...(selectedVariant ? { variant: selectedVariant } : {}),
250-
parts: [{ type: "text", text: input.prompt }, ...fileParts],
251-
}),
247+
try: (signal) =>
248+
client.session.prompt(
249+
{
250+
sessionID: session.data.id,
251+
model: parsedModel,
252+
...(selectedAgent ? { agent: selectedAgent } : {}),
253+
...(selectedVariant ? { variant: selectedVariant } : {}),
254+
parts: [{ type: "text", text: input.prompt }, ...fileParts],
255+
},
256+
{ signal },
257+
),
252258
catch: (cause) =>
253259
new OpenCodeTextGenerationPromptRequestError({
254260
...promptContext,
255261
cause,
256262
}),
257-
});
263+
}).pipe(
264+
// Disconnecting the HTTP request does not stop work in a shared or external server.
265+
Effect.onInterrupt(() =>
266+
OpenCodeRuntime.runOpenCodeSdk("session.abort", (signal) =>
267+
client.session.abort({ sessionID: session.data.id }, { signal }),
268+
).pipe(
269+
Effect.interruptible,
270+
Effect.timeout("10 seconds"),
271+
Effect.catch((cause) =>
272+
Effect.logWarning("Failed to stop cancelled OpenCode text generation.", {
273+
...promptContext,
274+
cause,
275+
}),
276+
),
277+
Effect.asVoid,
278+
),
279+
),
280+
);
258281
const promptFailure = getOpenCodePromptFailure(result.data?.info?.error);
259282
if (promptFailure) {
260283
return yield* new OpenCodeTextGenerationPromptResponseError({

0 commit comments

Comments
 (0)