Skip to content

Commit 92daf4d

Browse files
fix(server): OpenCode 2 keeps checking a failed prompt whose run starts late
A prompt whose request failed may still have reached OpenCode, so the next turn checks `session.active` first. The previous commit cleared that mark on any execution start, including the late start of that same run, so the next turn skipped the check and prompted into the run still going. The mark now records whether its run had started when it was marked. A run not yet started clears nothing with its own start; only a start after it (a new execution) clears the mark. A reconnect counts the marked run as started, since by then it is running or over and its start may have been lost in the gap. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c857ba9 commit 92daf4d

2 files changed

Lines changed: 68 additions & 14 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,40 @@ describe("OpenCode2 adapter", () => {
14461446
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
14471447
);
14481448

1449+
it.effect("still checks the server when a failed prompt's execution starts late", () =>
1450+
Effect.gen(function* () {
1451+
const offered = yield* Deferred.make<void>();
1452+
const { runtime, thread } = yield* resumed([
1453+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
1454+
// The request failed, but the server took the prompt: its execution
1455+
// starts only after the turn already failed.
1456+
reply("session.prompt", {
1457+
status: 502,
1458+
body: { _tag: "UnknownError", message: "bad gateway" },
1459+
}),
1460+
event("session.execution.started", { sessionID: SESSION }),
1461+
// That run is still going, so the next turn stops it and fails.
1462+
out("session.active"),
1463+
reply("session.active", { data: { [SESSION]: { type: "running" } } }),
1464+
out("session.interrupt", { sessionID: SESSION }),
1465+
reply("session.interrupt", { interrupted: true }),
1466+
]).pipe(
1467+
Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
1468+
offer: () => Deferred.succeed(offered, undefined).pipe(Effect.asVoid),
1469+
take: Effect.never,
1470+
}),
1471+
);
1472+
const ended = yield* terminals(runtime, 2);
1473+
yield* runtime.startTurn(turnInput(thread)).pipe(Effect.ignore);
1474+
// The late start has been read.
1475+
yield* Deferred.await(offered);
1476+
yield* runtime.startTurn(secondTurn(thread));
1477+
const [first, second] = yield* Fiber.join(ended);
1478+
assert.equal(first?.status, "failed");
1479+
assert.equal(second?.failure?.message, OPENCODE_2_STILL_STOPPING);
1480+
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
1481+
);
1482+
14491483
it.effect("prompts again without a check after the server refused a prompt", () =>
14501484
Effect.gen(function* () {
14511485
const { runtime, thread } = yield* resumed([

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ interface ThreadState {
334334
* so the next execution end belongs to that run; it clears this and ends no turn.
335335
*/
336336
unsettled: boolean;
337+
/**
338+
* Whether the run `unsettled` waits on had started when it was marked. A
339+
* prompt whose request failed may start its run late; that start is the
340+
* run's own, and only a start after it means its end was lost.
341+
*/
342+
unsettledStarted: boolean;
337343
/** The session's location, where its agents' path rules are read. */
338344
directory: string;
339345
/**
@@ -903,6 +909,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
903909
const emit = (event: ProviderAdapter.ProviderAdapterV2Event) =>
904910
Queue.offer(events, event).pipe(Effect.asVoid);
905911
const ownerOf = (sessionId: string) => childOwners.get(sessionId) ?? threads.get(sessionId);
912+
/** Marks a run T3 ended while OpenCode may still run it (see `unsettled`). */
913+
const markUnsettled = (state: ThreadState) => {
914+
state.unsettled = true;
915+
state.unsettledStarted = busy.has(state.sessionId);
916+
};
906917

907918
const newThreadState = (
908919
sessionId: string,
@@ -916,6 +927,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
916927
active: undefined,
917928
model: undefined,
918929
unsettled: false,
930+
unsettledStarted: false,
919931
directory,
920932
agent: "build",
921933
rules: undefined,
@@ -1780,7 +1792,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
17801792
reason,
17811793
});
17821794
if (state.active === undefined) return;
1783-
state.unsettled = true;
1795+
markUnsettled(state);
17841796
yield* finishTurn(state, {
17851797
status: "failed",
17861798
failure: makeProviderFailure({
@@ -1877,7 +1889,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
18771889
const [permissions, forms] = listed.value;
18781890
if (permissions.length === 0 && forms.length === 0) return;
18791891
// The stopped run's end is not the next turn's.
1880-
state.unsettled = true;
1892+
markUnsettled(state);
18811893
for (const request of permissions) {
18821894
yield* stopStaleRequest(state.sessionId, { type: "permission", id: request.id });
18831895
}
@@ -2062,7 +2074,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
20622074
// goes on. A subagent's session has no Stop end to skip.
20632075
const asker = threads.get(form.sessionID) ?? state;
20642076
if (!cancelled) return yield* abandonRequest(asker, "form cancel failed");
2065-
if (asker.subagent === undefined) asker.unsettled = true;
2077+
if (asker.subagent === undefined) markUnsettled(asker);
20662078
yield* finishTurn(asker, {
20672079
status: "failed",
20682080
failure: makeProviderFailure({
@@ -2294,7 +2306,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
22942306
delivered.every((entry) => state.stoppedChildren.has(entry.childId));
22952307
for (const entry of delivered) state.stoppedChildren.delete(entry.childId);
22962308
if (stopped) {
2297-
state.unsettled = true;
2309+
markUnsettled(state);
22982310
yield* client.session
22992311
.interrupt({ sessionID: Session.ID.make(state.sessionId) })
23002312
.pipe(Effect.timeout(INTERRUPT_TIMEOUT), Effect.ignore({ log: true }));
@@ -2421,13 +2433,17 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
24212433
event.type === "session.execution.started" ||
24222434
event.type === "unreadable.execution.started"
24232435
) {
2424-
busy.add(sessionId);
24252436
// A session runs one execution at a time, each opening with `started`
2426-
// on this ordered stream, so the run `unsettled` waits on is over: an
2427-
// end not seen by now was lost with a dropped stream, and the next
2428-
// end is this execution's own.
2437+
// on this ordered stream. Once the run `unsettled` waits on has
2438+
// started, a later start means that run is over: an end not seen by
2439+
// now was lost with a dropped stream, and the next end is this
2440+
// execution's own. A run not yet started when it was marked starts here.
24292441
const state = threads.get(sessionId);
2430-
if (state !== undefined) state.unsettled = false;
2442+
if (state?.unsettled === true) {
2443+
if (state.unsettledStarted) state.unsettled = false;
2444+
else state.unsettledStarted = true;
2445+
}
2446+
busy.add(sessionId);
24312447
} else if (event.type === "unreadable.execution.ended" || executionEnd(event.type)) {
24322448
busy.delete(sessionId);
24332449
}
@@ -2708,10 +2724,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27082724
const background = [...threads.values()].filter(
27092725
(state) => state.subagent === undefined && hasBackground(state),
27102726
);
2711-
if (running.length === 0 && background.length === 0 && busy.size === 0) return;
2727+
const marked = [...threads.values()].some((state) => state.unsettled);
2728+
if (running.length === 0 && background.length === 0 && busy.size === 0 && !marked) return;
27122729
const active = yield* client.session.active();
27132730
// An execution that ended while the stream was down never said so.
27142731
for (const sessionId of busy) if (!(sessionId in active)) busy.delete(sessionId);
2732+
// A run `unsettled` waits on is running now or over: any start from here
2733+
// is a new execution's, even if the run's own start was lost in the gap.
2734+
for (const state of threads.values()) if (state.unsettled) state.unsettledStarted = true;
27152735
for (const [sessionId, state] of running) {
27162736
const turn = state.active;
27172737
if (turn === undefined) continue;
@@ -3432,7 +3452,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
34323452
const running = state.wakes.some((wake) => wake.running);
34333453
for (const wake of state.wakes.splice(0)) wake.dropped = true;
34343454
if (running) {
3435-
state.unsettled = true;
3455+
markUnsettled(state);
34363456
yield* client.session
34373457
.interrupt({ sessionID: Session.ID.make(state.sessionId) })
34383458
.pipe(Effect.timeout(INTERRUPT_TIMEOUT), Effect.ignore({ log: true }));
@@ -3794,7 +3814,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
37943814
? Effect.gen(function* () {
37953815
// Without a clear rejection the server may have taken the
37963816
// prompt, so the next turn checks before it prompts again.
3797-
if (!CLEAR_PROMPT_REJECTIONS.has(cause._tag)) state.unsettled = true;
3817+
if (!CLEAR_PROMPT_REJECTIONS.has(cause._tag)) markUnsettled(state);
37983818
yield* finishTurn(state, {
37993819
status: "failed",
38003820
failure: makeProviderFailure({ cause, class: "provider_error" }),
@@ -3902,13 +3922,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
39023922
),
39033923
);
39043924
if (reply._tag === "None") {
3905-
state.unsettled = true;
3925+
markUnsettled(state);
39063926
return yield* lock.withPermit(finishTurn(state, { status: "interrupted" }));
39073927
}
39083928
// Between the executions a late steer spans nothing runs yet. The
39093929
// steers are taken back, and a run they already started is skipped.
39103930
if (!reply.value.interrupted && state.active === turn && turn.heldEnd !== undefined) {
3911-
state.unsettled = true;
3931+
markUnsettled(state);
39123932
yield* lock.withPermit(finishTurn(state, { status: "interrupted" }));
39133933
return yield* cancelStrandedSteers(state);
39143934
}

0 commit comments

Comments
 (0)