Skip to content

Commit 28b4cee

Browse files
fix(opencode): a Stop OpenCode 2 never answers offers Force stop (pingdotgg#34840)
1 parent bfec275 commit 28b4cee

2 files changed

Lines changed: 66 additions & 27 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
22022202
.interruptTurn({ providerThread: thread, providerTurnId: yield* providerTurnId })
22032203
.pipe(Effect.forkScoped);
22042204
yield* TestClock.adjust("11 seconds");
2205-
yield* Fiber.join(interrupt);
2205+
yield* Fiber.join(interrupt).pipe(Effect.flip);
22062206
});
22072207
const terminals = (runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime, count: number) =>
22082208
runtime.events.pipe(
@@ -2217,23 +2217,57 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
22172217
Effect.forkScoped,
22182218
);
22192219

2220+
// regression: #30193 an unanswered Stop showed the turn interrupted while its tool kept running
2221+
it.effect("reports a Stop OpenCode never answers as an interrupt timeout", () =>
2222+
Effect.gen(function* () {
2223+
const { runtime, thread } = yield* resumed(stopTimedOut);
2224+
yield* runtime.startTurn(turnInput(thread));
2225+
const interrupt = yield* runtime
2226+
.interruptTurn({ providerThread: thread, providerTurnId: yield* providerTurnId })
2227+
.pipe(Effect.exit, Effect.forkScoped);
2228+
yield* TestClock.adjust("11 seconds");
2229+
const exit = yield* Fiber.join(interrupt);
2230+
assert.propertyVal(
2231+
Exit.isFailure(exit) ? Cause.squash(exit.cause) : { _tag: "interrupt succeeded" },
2232+
"_tag",
2233+
"ProviderAdapterInterruptTimeoutError",
2234+
);
2235+
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
2236+
);
2237+
22202238
it.effect("never lets a timed-out Stop's late end finish the next turn", () =>
22212239
Effect.gen(function* () {
2222-
const { runtime, thread } = yield* resumed([
2223-
...stopTimedOut,
2224-
out("session.active"),
2225-
reply("session.active", { data: {} }),
2226-
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2227-
promptAccepted,
2228-
event("session.execution.succeeded", { sessionID: SESSION }),
2229-
event("session.execution.started", { sessionID: SESSION }),
2230-
event("session.execution.failed", {
2231-
sessionID: SESSION,
2232-
error: { type: "provider", message: "second turn failed" },
2233-
}),
2234-
]);
2235-
const ended = yield* terminals(runtime, 2);
2240+
const gate = makeProviderReplayGate(["late-end"]);
2241+
const { runtime, thread } = yield* resumed(
2242+
[
2243+
...stopTimedOut,
2244+
event("session.execution.succeeded", { sessionID: SESSION }, "late-end"),
2245+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2246+
promptAccepted,
2247+
event("session.execution.started", { sessionID: SESSION }),
2248+
event("session.execution.failed", {
2249+
sessionID: SESSION,
2250+
error: { type: "provider", message: "second turn failed" },
2251+
}),
2252+
],
2253+
{ replayGate: gate },
2254+
);
2255+
const lateEnd = yield* Deferred.make<void>();
2256+
const ended = yield* runtime.events.pipe(
2257+
Stream.filter(
2258+
(
2259+
event,
2260+
): event is Extract<ProviderAdapter.ProviderAdapterV2Event, { type: "turn.terminal" }> =>
2261+
event.type === "turn.terminal",
2262+
),
2263+
Stream.tap(() => Deferred.succeed(lateEnd, undefined)),
2264+
Stream.take(2),
2265+
Stream.runCollect,
2266+
Effect.forkScoped,
2267+
);
22362268
yield* stopFirstTurn(runtime, thread);
2269+
gate.release("late-end");
2270+
yield* Deferred.await(lateEnd);
22372271
yield* runtime.startTurn(secondTurn(thread));
22382272
const [first, second] = yield* Fiber.join(ended);
22392273
assert.equal(first?.status, "interrupted");
@@ -2305,15 +2339,15 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
23052339
...backgroundLaunch(CHILD),
23062340
out("session.interrupt", { sessionID: SESSION }),
23072341
reply("session.interrupt", "<hang>"),
2308-
out("session.active"),
2309-
replyData("session.active", { [SESSION]: { type: "running" } }),
23102342
out("session.interrupt", { sessionID: SESSION }),
23112343
reply("session.interrupt", { interrupted: true }),
23122344
event("session.usage.updated", { sessionID: SESSION }, "drop"),
23132345
{ type: "runtime_exit", status: "success" } as const,
23142346
out("event.subscribe"),
23152347
out("session.active"),
23162348
replyData("session.active", { [CHILD]: { type: "running" } }),
2349+
out("message.list", "<any>"),
2350+
reply("message.list", { data: [], cursor: {} }),
23172351
out("permission.list", { sessionID: CHILD }),
23182352
replyData("permission.list", []),
23192353
out("session.form.list", { sessionID: CHILD }),
@@ -2375,23 +2409,22 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
23752409
);
23762410
yield* runtime.startTurn(withLineage(thread));
23772411
yield* Deferred.await(attached);
2378-
const interrupt = yield* runtime
2379-
.interruptTurn({ providerThread: thread, providerTurnId: yield* providerTurnId })
2380-
.pipe(Effect.forkScoped);
2412+
const stop = { providerThread: thread, providerTurnId: yield* providerTurnId };
2413+
const interrupt = yield* runtime.interruptTurn(stop).pipe(Effect.flip, Effect.forkScoped);
23812414
yield* TestClock.adjust("11 seconds");
23822415
yield* Fiber.join(interrupt);
2383-
yield* runtime.startTurn({ ...secondTurn(thread), appThread: withLineage(thread).appThread });
2416+
yield* runtime.interruptTurn(stop);
23842417
gate.release("drop");
23852418
yield* runtime.startTurn({
23862419
...continuationTurn(thread, yield* Deferred.await(offered)),
2387-
runOrdinal: 3,
2388-
providerTurnOrdinal: 3,
2420+
runOrdinal: 2,
2421+
providerTurnOrdinal: 2,
23892422
});
23902423
gate.release("reply");
23912424
const events = [...(yield* Fiber.join(collected))];
23922425
assert.deepEqual(
23932426
events.flatMap((event) => (event.type === "turn.terminal" ? [event.status] : [])),
2394-
["interrupted", "failed", "completed"],
2427+
["interrupted", "completed"],
23952428
);
23962429
assert.include(
23972430
events.flatMap((event) =>

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ const claudeUsageLimit = (message: string | undefined) =>
583583
? { resetAt: /\bResets at (\d{4}-\d\d-\d\dT[\d:.]+Z)\.?\s*$/.exec(message)?.[1] ?? null }
584584
: undefined;
585585

586-
const INTERRUPT_TIMEOUT = "10 seconds";
586+
const INTERRUPT_TIMEOUT = 10_000;
587587
const INSTRUCTIONS_KEY = "t3-code";
588588
const RECONNECT_ATTEMPTS = 5;
589589
const RECONNECT_DELAY = "2 seconds";
@@ -4815,8 +4815,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
48154815
),
48164816
);
48174817
if (reply._tag === "None") {
4818-
markUnsettled(state);
4819-
return yield* lock.withPermit(finishTurn(state, { status: "interrupted" }));
4818+
if (state.active !== turn) return;
4819+
// The turn stays open: its tools may still run, and the user decides whether to Force stop.
4820+
return yield* new ProviderAdapter.ProviderAdapterInterruptTimeoutError({
4821+
driver: OPENCODE_PROVIDER,
4822+
providerThreadId: interruptInput.providerThread.id,
4823+
providerTurnId: interruptInput.providerTurnId,
4824+
timeoutMs: INTERRUPT_TIMEOUT,
4825+
});
48204826
}
48214827
// Stop between steer executions retracts pending steers and skips any run they already started.
48224828
if (!reply.value.interrupted && state.active === turn && turn.heldEnd !== undefined) {

0 commit comments

Comments
 (0)