Skip to content

Commit 1b1826b

Browse files
committed
fix(opencode): preserve pending steers when goal refresh fails
1 parent b12405f commit 1b1826b

2 files changed

Lines changed: 95 additions & 51 deletions

File tree

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

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,39 +2386,88 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
23862386
event.type === "turn_item.updated" ? [`${event.turnItem.type}:${event.turnItem.status}`] : [],
23872387
);
23882388

2389-
it.effect("recovers a steer's missed delivery from history after reconnect", () =>
2390-
Effect.gen(function* () {
2391-
const steerId = `msg_t3_steer_${SESSION}:message:opencode2-adapter:steer`;
2392-
const { runtime, thread } = yield* resumed([
2393-
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
2394-
promptAccepted,
2395-
out("session.prompt", "<any>"),
2396-
replyData("session.prompt", {
2397-
id: steerId,
2398-
sessionID: SESSION,
2399-
time: { created: 2 },
2400-
type: "user",
2401-
payload: { text: "steered" },
2402-
delivery: "steer",
2403-
}),
2404-
...reconnected({}, "succeeded", {}, steerId),
2405-
event("session.execution.interrupted", { sessionID: SESSION, reason: "user" }),
2406-
]);
2407-
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2408-
yield* runtime.startTurn(turnInput(thread));
2409-
yield* runtime.steerTurn({
2410-
providerThread: thread,
2411-
providerTurnId: yield* providerTurnId,
2412-
threadId,
2413-
runId: turnInput(thread).runId,
2414-
message: {
2415-
...turnInput(thread).message,
2416-
messageId: MessageId.make("message:opencode2-adapter:steer"),
2417-
text: "steered",
2418-
},
2419-
});
2420-
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
2421-
}).pipe(Effect.scoped),
2389+
it.effect.each(["delivered", "pending"] as const)(
2390+
"reconciles a %s steer after reconnect without losing its execution",
2391+
(delivery) =>
2392+
Effect.gen(function* () {
2393+
const steerId = `msg_t3_steer_${SESSION}:message:opencode2-adapter:steer`;
2394+
const reconnect = reconnected(
2395+
{},
2396+
"succeeded",
2397+
{},
2398+
delivery === "delivered" ? steerId : undefined,
2399+
);
2400+
const { runtime, thread } = yield* resumed(
2401+
[
2402+
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
2403+
promptAccepted,
2404+
...(delivery === "pending"
2405+
? [
2406+
event("rpc.opencode.goal.changed", {
2407+
sessionID: SESSION,
2408+
goal: nativeGoal("active"),
2409+
}),
2410+
]
2411+
: []),
2412+
out("session.prompt", "<any>"),
2413+
replyData("session.prompt", {
2414+
id: steerId,
2415+
sessionID: SESSION,
2416+
time: { created: 2 },
2417+
type: "user",
2418+
payload: { text: "steered" },
2419+
delivery: "steer",
2420+
}),
2421+
...reconnect.slice(0, 2),
2422+
...(delivery === "pending"
2423+
? [out("rpc.call", "<any>"), reply("rpc.call", { output: { unreadable: true } })]
2424+
: []),
2425+
...reconnect.slice(2),
2426+
...(delivery === "pending"
2427+
? [
2428+
event("session.execution.started", { sessionID: SESSION }),
2429+
event("session.inbox.delivered", { sessionID: SESSION, inboxID: steerId }),
2430+
event("session.text.ended", {
2431+
sessionID: SESSION,
2432+
assistantMessageID: "msg_delayed_steer",
2433+
ordinal: 0,
2434+
text: "Steer processed",
2435+
}),
2436+
event("session.execution.succeeded", { sessionID: SESSION }),
2437+
]
2438+
: [event("session.execution.interrupted", { sessionID: SESSION, reason: "user" })]),
2439+
],
2440+
delivery === "pending" ? { version: "2.2.5" } : undefined,
2441+
);
2442+
const events = yield* runtime.events.pipe(
2443+
Stream.takeUntil((e) => e.type === "turn.terminal"),
2444+
Stream.runCollect,
2445+
Effect.forkScoped,
2446+
);
2447+
yield* runtime.startTurn(turnInput(thread));
2448+
yield* runtime.steerTurn({
2449+
providerThread: thread,
2450+
providerTurnId: yield* providerTurnId,
2451+
threadId,
2452+
runId: turnInput(thread).runId,
2453+
message: {
2454+
...turnInput(thread).message,
2455+
messageId: MessageId.make("message:opencode2-adapter:steer"),
2456+
text: "steered",
2457+
},
2458+
});
2459+
const collected = yield* Fiber.join(events);
2460+
assert.deepInclude(collected.at(-1), { type: "turn.terminal", status: "completed" });
2461+
if (delivery === "pending")
2462+
assert.isTrue(
2463+
collected.some(
2464+
(e) =>
2465+
e.type === "turn_item.updated" &&
2466+
e.turnItem.type === "assistant_message" &&
2467+
e.turnItem.text === "Steer processed",
2468+
),
2469+
);
2470+
}).pipe(Effect.scoped),
24222471
);
24232472

24242473
it.effect("does not end a turn from an earlier turn's history when its prompt is not there", () =>

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ interface ThreadState {
292292
providerThread: OrchestrationV2ProviderThread;
293293
publishedBackgroundWork?: boolean;
294294
goalRevision: number;
295+
goalFresh: boolean;
295296
readonly providerTurns: Map<string, OrchestrationV2ProviderTurn>;
296297
active: ActiveTurn | undefined;
297298
model: ModelRef | undefined;
@@ -715,7 +716,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
715716
let connection = initial.connection;
716717
const supportsNativeGoals = () => compareSemverVersions(connection.version, "2.2.5") >= 0;
717718
const hasActiveGoal = (state: ThreadState) =>
718-
supportsNativeGoals() && state.providerThread.goal?.status === "active";
719+
supportsNativeGoals() && state.goalFresh && state.providerThread.goal?.status === "active";
719720
let client = connection.client;
720721
const sessionScope = yield* Effect.scope;
721722
// Project config changes model limits; key by directory because this runtime serves all instance threads.
@@ -771,6 +772,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
771772
sessionId,
772773
providerThread,
773774
goalRevision: 0,
775+
goalFresh: false,
774776
providerTurns: new Map(),
775777
active: undefined,
776778
model: undefined,
@@ -868,6 +870,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
868870
goal: typeof NativeGoal.Type,
869871
) {
870872
state.goalRevision += 1;
873+
state.goalFresh = true;
871874
state.providerThread = {
872875
...state.providerThread,
873876
goal:
@@ -2651,9 +2654,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
26512654
});
26522655

26532656
// Restarts retain the previous session.outcome; without an idle item, the lost execution was interrupted.
2654-
const reconcile = Effect.fn("OpenCode2Adapter.reconcile")(function* (
2655-
freshGoals: ReadonlySet<ThreadState>,
2656-
) {
2657+
const reconcile = Effect.fn("OpenCode2Adapter.reconcile")(function* () {
26572658
// Read deepest sessions first; subagent turns end before their waiting parents.
26582659
const running = [...threads]
26592660
.filter(([, state]) => state.active !== undefined)
@@ -2696,8 +2697,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
26962697
}),
26972698
}
26982699
: { status: "interrupted" };
2699-
if (supportsNativeGoals() && !freshGoals.has(state)) yield* finishTurn(state, terminal);
2700-
else yield* endExecution(state, turn, terminal);
2700+
yield* endExecution(state, turn, terminal);
27012701
}
27022702
for (const state of background) {
27032703
for (const call of runningCalls(state)) {
@@ -2754,24 +2754,19 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27542754
currentScope = scope;
27552755
yield* Scope.close(previous, Exit.void);
27562756
// A restarted server forgot T3's MCP servers; the next turn adds them again.
2757-
for (const state of threads.values()) state.mcp = undefined;
2758-
const freshGoals = new Set<ThreadState>();
2757+
for (const state of threads.values()) {
2758+
state.mcp = undefined;
2759+
state.goalFresh = false;
2760+
state.goalRevision += 1;
2761+
}
27592762
if (supportsNativeGoals()) {
27602763
yield* Effect.forEach(
27612764
[...threads.values()],
2762-
(state) =>
2763-
readGoal(state).pipe(
2764-
Effect.tap(() =>
2765-
Effect.sync(() => {
2766-
freshGoals.add(state);
2767-
}),
2768-
),
2769-
Effect.ignore({ log: true }),
2770-
),
2765+
(state) => readGoal(state).pipe(Effect.ignore({ log: true })),
27712766
{ concurrency: 4, discard: true },
27722767
).pipe(Effect.timeout(INVENTORY_TIMEOUT), Effect.ignore({ log: true }));
27732768
}
2774-
yield* lock.withPermit(reconcile(freshGoals)).pipe(Effect.timeout(RECONCILE_TIMEOUT));
2769+
yield* lock.withPermit(reconcile()).pipe(Effect.timeout(RECONCILE_TIMEOUT));
27752770
return stream;
27762771
}).pipe(
27772772
Effect.retry({ times: RECONNECT_ATTEMPTS - 1, schedule: Schedule.spaced(RECONNECT_DELAY) }),

0 commit comments

Comments
 (0)