Skip to content

Commit 9facfc5

Browse files
only21milclaude
andauthored
fix(opencode): apply the 3-minute limit to the whole v2 text generation (#23598)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 200a373 commit 9facfc5

2 files changed

Lines changed: 106 additions & 28 deletions

File tree

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { assert, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { ProviderInstanceId } from "@t3tools/contracts";
4+
import * as Deferred from "effect/Deferred";
45
import * as Effect from "effect/Effect";
6+
import * as Fiber from "effect/Fiber";
57
import * as Layer from "effect/Layer";
8+
import { TestClock } from "effect/testing";
69

710
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
811
import * as OpenCode2AdapterV2Testkit from "../orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts";
@@ -119,4 +122,70 @@ it.layer(layer)("OpenCode2TextGeneration", (it) => {
119122
assert.deepEqual(failure.cause, refusal);
120123
}).pipe(Effect.scoped),
121124
);
125+
126+
it.effect("times out when the prompt request and its interrupt never answer", () =>
127+
Effect.gen(function* () {
128+
const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
129+
const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
130+
const prompted = yield* Deferred.make<void>();
131+
const server = yield* OpenCode2AdapterV2Testkit.replayServer(
132+
{
133+
provider: "opencode",
134+
protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
135+
version: "2.0.18",
136+
scenario: "opencode2_title_generation_stalled",
137+
entries: [
138+
subscribe!,
139+
connected!,
140+
create!,
141+
created!,
142+
{
143+
type: "expect_outbound",
144+
frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
145+
},
146+
{
147+
type: "emit_inbound",
148+
frame: { type: "sdk.response", operation: "session.prompt", data: "<hang>" },
149+
},
150+
{
151+
type: "expect_outbound",
152+
frame: { type: "session.interrupt", input: { sessionID: sessionId } },
153+
},
154+
{
155+
type: "emit_inbound",
156+
frame: { type: "sdk.response", operation: "session.interrupt", data: "<hang>" },
157+
},
158+
{
159+
type: "expect_outbound",
160+
frame: { type: "session.remove", input: { sessionID: sessionId } },
161+
},
162+
{
163+
type: "emit_inbound",
164+
frame: { type: "sdk.response", operation: "session.remove", data: null },
165+
},
166+
{ type: "runtime_exit", status: "success" },
167+
],
168+
},
169+
{ onPrompt: () => Deferred.doneUnsafe(prompted, Effect.void) },
170+
);
171+
const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
172+
Effect.provideService(OpenCode2Server.OpenCode2Server, server),
173+
);
174+
const generation = yield* textGeneration
175+
.generateThreadTitle({
176+
cwd: process.cwd(),
177+
message: "fix the login redirect loop after oauth",
178+
modelSelection: {
179+
instanceId: ProviderInstanceId.make("opencode"),
180+
model: "opencode/big-pickle",
181+
},
182+
})
183+
.pipe(Effect.flip, Effect.forkScoped);
184+
yield* Deferred.await(prompted);
185+
yield* TestClock.adjust("3 minutes");
186+
yield* TestClock.adjust("5 seconds");
187+
const failure = yield* Fiber.join(generation);
188+
assert.equal(failure.detail, "OpenCode did not finish generating in time.");
189+
}).pipe(Effect.scoped),
190+
);
122191
});

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

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,47 @@ const runOnServer = Effect.fn("Opencode2TextGeneration.runOnServer")(function* (
112112
),
113113
Effect.forkScoped,
114114
);
115-
const session = yield* client.session.create({
116-
title: `T3 Code ${input.operation}`,
117-
location: Location.PublicRef.make({ directory: AbsolutePath.make(input.cwd) }),
118-
model: Model.Ref.make({
119-
providerID: Provider.ID.make(parsed.providerID),
120-
id: Model.ID.make(parsed.modelID),
121-
...(variant === undefined ? {} : { variant: Model.VariantID.make(variant) }),
122-
}),
123-
permissions: GENERATION_PERMISSIONS,
124-
});
125-
sessionId = session.id;
126-
yield* Effect.addFinalizer(() =>
127-
client.session
128-
.remove({ sessionID: session.id })
129-
.pipe(Effect.timeout("5 seconds"), Effect.ignore({ log: true })),
130-
);
131-
const images = (input.attachments ?? []).flatMap((attachment) => {
132-
if (attachment.type !== "image") return [];
133-
const path = resolveAttachmentPath(attachment);
134-
return path === null ? [] : [{ uri: `file://${path}`, name: attachment.name }];
135-
});
136-
yield* client.session.prompt({
137-
sessionID: session.id,
138-
text: input.prompt,
139-
...(images.length === 0 ? {} : { files: images }),
115+
// The limit covers creating the session and sending the prompt as well, since
116+
// either request can stall while the event stream stays connected.
117+
const generate = Effect.gen(function* () {
118+
const session = yield* client.session.create({
119+
title: `T3 Code ${input.operation}`,
120+
location: Location.PublicRef.make({ directory: AbsolutePath.make(input.cwd) }),
121+
model: Model.Ref.make({
122+
providerID: Provider.ID.make(parsed.providerID),
123+
id: Model.ID.make(parsed.modelID),
124+
...(variant === undefined ? {} : { variant: Model.VariantID.make(variant) }),
125+
}),
126+
permissions: GENERATION_PERMISSIONS,
127+
});
128+
sessionId = session.id;
129+
yield* Effect.addFinalizer(() =>
130+
client.session
131+
.remove({ sessionID: session.id })
132+
.pipe(Effect.timeout("5 seconds"), Effect.ignore({ log: true })),
133+
);
134+
const images = (input.attachments ?? []).flatMap((attachment) => {
135+
if (attachment.type !== "image") return [];
136+
const path = resolveAttachmentPath(attachment);
137+
return path === null ? [] : [{ uri: `file://${path}`, name: attachment.name }];
138+
});
139+
yield* client.session.prompt({
140+
sessionID: session.id,
141+
text: input.prompt,
142+
...(images.length === 0 ? {} : { files: images }),
143+
});
144+
return yield* Deferred.await(outcome);
140145
});
141-
const result = yield* Deferred.await(outcome).pipe(
146+
const result = yield* generate.pipe(
142147
Effect.timeoutOrElse({
143148
duration: GENERATION_TIMEOUT,
144149
orElse: () =>
145-
client.session.interrupt({ sessionID: Session.ID.make(session.id) }).pipe(
146-
Effect.ignore,
150+
(sessionId === undefined
151+
? Effect.void
152+
: client.session
153+
.interrupt({ sessionID: Session.ID.make(sessionId) })
154+
.pipe(Effect.timeout("5 seconds"), Effect.ignore)
155+
).pipe(
147156
Effect.as<Outcome>({
148157
_tag: "failed",
149158
detail: "OpenCode did not finish generating in time.",

0 commit comments

Comments
 (0)