Skip to content

Commit e7171ae

Browse files
committed
Overlay branch overlay/opencode-textgen on top of upstream-target/2026-10-22 via Merge
# Conflicts: # packages/provider-opencode/src/server/textGeneration.test.ts # packages/provider-opencode/src/server/textGeneration.ts * packages/provider-opencode/src/server/textGeneration.test.ts * packages/provider-opencode/src/server/textGeneration.ts • 5f536c935c Overlay branch overlay/opencode-textgen on top of upstream-target/2026-10-22 via Merge |\ | • 6062167 Overlay branch overlay/opencode-textgen on top of origin/main via Merge | |\ | | • c6a0ac0 Overlay branch overlay/opencode-textgen on top of origin/main via Merge | | |\ | | | • d5e383b fix(textGeneration): delete opencode helper sessions after one-shot calls | | |/ • | | 6f75bcb feat: ChatGPT Business usage limits offer a copyable admin request (pingdotgg#23361) • | | 002e941 fix(mobile): preserve new-thread drafts and native header layout (pingdotgg#23366) • | | efa5381 fix(mobile): the iOS composer reopens where you left a long draft (pingdotgg#23096) • | | a49d18c fix(source-control): clones retry over HTTPS when the SSH key is refused (pingdotgg#23170)
2 parents 6f75bcb + 6062167 commit e7171ae

2 files changed

Lines changed: 115 additions & 49 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const runtimeMock = {
3434
| ((signal?: AbortSignal) => Promise<{ data: { parts: ReadonlyArray<unknown> } }>)
3535
| undefined,
3636
onAbort: undefined as ((signal?: AbortSignal) => Promise<never>) | undefined,
37+
sessionDeleteCalls: [] as string[],
3738
connectionError: undefined as Error | undefined,
3839
sessionCreateError: undefined as unknown,
3940
sessionResult: undefined as { data?: { id: string } } | undefined,
@@ -53,6 +54,7 @@ const runtimeMock = {
5354
this.state.onSessionCreate = undefined;
5455
this.state.onPrompt = undefined;
5556
this.state.onAbort = undefined;
57+
this.state.sessionDeleteCalls.length = 0;
5658
this.state.connectionError = undefined;
5759
this.state.sessionCreateError = undefined;
5860
this.state.sessionResult = undefined;
@@ -154,6 +156,10 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntime.OpenCodeRuntime["Service"] = {
154156
}
155157
);
156158
},
159+
delete: async (input: { readonly sessionID: string }) => {
160+
runtimeMock.state.sessionDeleteCalls.push(input.sessionID);
161+
return { data: true };
162+
},
157163
},
158164
}) as unknown as ReturnType<
159165
OpenCodeRuntime.OpenCodeRuntime["Service"]["createOpenCodeSdkClient"]
@@ -308,6 +314,7 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
308314
expect(signal?.aborted).toBe(true);
309315
expect(otherSignal?.aborted).toBe(false);
310316
expect(runtimeMock.state.abortCalls).toEqual(["cancelled-session"]);
317+
expect(runtimeMock.state.sessionDeleteCalls).toEqual(["cancelled-session"]);
311318
expect(runtimeMock.state.closeCalls).toEqual([]);
312319

313320
otherReply.resolve({
@@ -322,11 +329,56 @@ it.layer(layerOpenCodeTextGenerationTest)("OpenCodeTextGeneration", (it) => {
322329
"http://127.0.0.1:4301",
323330
]);
324331
expect(runtimeMock.state.abortCalls).toEqual(["cancelled-session"]);
332+
expect(runtimeMock.state.sessionDeleteCalls).toEqual([
333+
"cancelled-session",
334+
"other-session",
335+
]);
325336
expect(runtimeMock.state.closeCalls).toEqual([]);
326337
}),
327338
),
328339
);
329340

341+
it.effect("deletes the helper session after a successful title generation", () =>
342+
withOpenCodeTextGeneration(DEFAULT_OPENCODE_SETTINGS, (textGeneration) =>
343+
Effect.gen(function* () {
344+
runtimeMock.state.sessionResult = { data: { id: "ses_helper_1" } };
345+
runtimeMock.state.promptResult = {
346+
data: {
347+
parts: [{ type: "text", text: '{"title":"Review uploaded report"}' }],
348+
},
349+
};
350+
351+
const title = yield* textGeneration.generateThreadTitle({
352+
cwd: process.cwd(),
353+
message: "Review these attachments.",
354+
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
355+
});
356+
357+
expect(title.title).toBe("Review uploaded report");
358+
expect(runtimeMock.state.sessionDeleteCalls).toEqual(["ses_helper_1"]);
359+
}),
360+
),
361+
);
362+
363+
it.effect("deletes the helper session when the prompt fails", () =>
364+
withOpenCodeTextGeneration(DEFAULT_OPENCODE_SETTINGS, (textGeneration) =>
365+
Effect.gen(function* () {
366+
runtimeMock.state.sessionResult = { data: { id: "ses_helper_2" } };
367+
runtimeMock.state.promptRequestError = new Error("provider exploded");
368+
369+
yield* textGeneration
370+
.generateThreadTitle({
371+
cwd: process.cwd(),
372+
message: "Review these attachments.",
373+
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
374+
})
375+
.pipe(Effect.flip);
376+
377+
expect(runtimeMock.state.sessionDeleteCalls).toEqual(["ses_helper_2"]);
378+
}),
379+
),
380+
);
381+
330382
it.effect("excludes generic files from thread title generation", () =>
331383
withOpenCodeTextGeneration(DEFAULT_OPENCODE_SETTINGS, (textGeneration) =>
332384
Effect.gen(function* () {

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

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -207,69 +207,83 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
207207
cwd: input.cwd,
208208
});
209209
}
210+
const sessionId = session.data.id;
210211
const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent");
211212
const selectedVariant = getModelSelectionStringOptionValue(input.modelSelection, "variant");
212213
const promptContext = {
213214
operation: input.operation,
214215
cwd: input.cwd,
215-
sessionId: session.data.id,
216+
sessionId,
216217
providerId: parsedModel.providerID,
217218
modelId: parsedModel.modelID,
218219
};
219220

220-
const result = yield* Effect.tryPromise({
221-
try: (signal) =>
222-
client.session.prompt(
223-
{
224-
sessionID: session.data.id,
225-
model: parsedModel,
226-
...(selectedAgent ? { agent: selectedAgent } : {}),
227-
...(selectedVariant ? { variant: selectedVariant } : {}),
228-
parts: [{ type: "text", text: input.prompt }, ...fileParts],
229-
},
230-
{ signal },
231-
),
232-
catch: (cause) =>
233-
new OpenCodeTextGenerationPromptRequestError({
234-
...promptContext,
235-
cause,
236-
}),
237-
}).pipe(
238-
// Disconnecting the HTTP request does not stop work in a shared or external server.
239-
Effect.onInterrupt(() =>
240-
OpenCodeRuntime.runOpenCodeSdk("session.abort", (signal) =>
241-
client.session.abort({ sessionID: session.data.id }, { signal }),
242-
).pipe(
243-
Effect.interruptible,
244-
Effect.timeout("10 seconds"),
245-
Effect.catch(() =>
246-
Effect.logWarning(
247-
"Failed to stop cancelled OpenCode text generation.",
248-
promptContext,
221+
// The helper session exists only to satisfy session.prompt; opencode
222+
// persists it in the user's session store, so delete it on every exit
223+
// path (prime's --no-session is the parity point).
224+
return yield* Effect.gen(function* () {
225+
const result = yield* Effect.tryPromise({
226+
try: (signal) =>
227+
client.session.prompt(
228+
{
229+
sessionID: sessionId,
230+
model: parsedModel,
231+
...(selectedAgent ? { agent: selectedAgent } : {}),
232+
...(selectedVariant ? { variant: selectedVariant } : {}),
233+
parts: [{ type: "text", text: input.prompt }, ...fileParts],
234+
},
235+
{ signal },
236+
),
237+
catch: (cause) =>
238+
new OpenCodeTextGenerationPromptRequestError({
239+
...promptContext,
240+
cause,
241+
}),
242+
}).pipe(
243+
// Disconnecting the HTTP request does not stop work in a shared or external server.
244+
Effect.onInterrupt(() =>
245+
OpenCodeRuntime.runOpenCodeSdk("session.abort", (signal) =>
246+
client.session.abort({ sessionID: sessionId }, { signal }),
247+
).pipe(
248+
Effect.interruptible,
249+
Effect.timeout("10 seconds"),
250+
Effect.catch(() =>
251+
Effect.logWarning(
252+
"Failed to stop cancelled OpenCode text generation.",
253+
promptContext,
254+
),
249255
),
256+
Effect.asVoid,
250257
),
251-
Effect.asVoid,
252258
),
259+
);
260+
const promptFailure = getOpenCodePromptFailure(result.data?.info?.error);
261+
if (promptFailure) {
262+
return yield* new OpenCodeTextGenerationPromptResponseError({
263+
...promptContext,
264+
...(promptFailure.name ? { providerErrorName: promptFailure.name } : {}),
265+
providerMessage: promptFailure.message,
266+
});
267+
}
268+
const responseParts = result.data?.parts ?? [];
269+
const rawText = getOpenCodeTextResponse(responseParts);
270+
if (rawText.length === 0) {
271+
return yield* new OpenCodeTextGenerationEmptyOutputError({
272+
...promptContext,
273+
responsePartCount: responseParts.length,
274+
textPartCount: responseParts.filter(isOpenCodeTextPart).length,
275+
});
276+
}
277+
return rawText;
278+
}).pipe(
279+
// Runs after the abort above, so an interrupted prompt is stopped before
280+
// its session is removed. Finalizers are uninterruptible, so bound it.
281+
Effect.ensuring(
282+
OpenCodeRuntime.runOpenCodeSdk("session.delete", (signal) =>
283+
client.session.delete({ sessionID: sessionId }, { signal }),
284+
).pipe(Effect.interruptible, Effect.timeout("10 seconds"), Effect.ignore),
253285
),
254286
);
255-
const promptFailure = getOpenCodePromptFailure(result.data?.info?.error);
256-
if (promptFailure) {
257-
return yield* new OpenCodeTextGenerationPromptResponseError({
258-
...promptContext,
259-
...(promptFailure.name ? { providerErrorName: promptFailure.name } : {}),
260-
providerMessage: promptFailure.message,
261-
});
262-
}
263-
const responseParts = result.data?.parts ?? [];
264-
const rawText = getOpenCodeTextResponse(responseParts);
265-
if (rawText.length === 0) {
266-
return yield* new OpenCodeTextGenerationEmptyOutputError({
267-
...promptContext,
268-
responsePartCount: responseParts.length,
269-
textPartCount: responseParts.filter(isOpenCodeTextPart).length,
270-
});
271-
}
272-
return rawText;
273287
},
274288
Effect.catchTags({
275289
OpenCodeTextGenerationSessionRequestError: (cause) =>

0 commit comments

Comments
 (0)