Skip to content

Commit 3a3ab2e

Browse files
fix(opencode): preserve Stop status when discarding image steers
1 parent 8dfecea commit 3a3ab2e

2 files changed

Lines changed: 138 additions & 92 deletions

File tree

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

Lines changed: 132 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,98 +3453,139 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
34533453
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(IdAllocator.layer, NodeServices.layer))),
34543454
);
34553455

3456-
it.effect("rejects an image steer stopped during attachment resolution without sending it", () =>
3457-
Effect.gen(function* () {
3458-
const fileSystem = yield* FileSystem.FileSystem;
3459-
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
3460-
prefix: "t3-oc2-stopped-image-",
3461-
});
3462-
const image = ChatImageAttachment.make({
3463-
type: "image",
3464-
id: ChatAttachmentId.make("thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc"),
3465-
name: "screen.png",
3466-
mimeType: "image/png",
3467-
sizeBytes: 4,
3468-
});
3469-
const path = resolveAttachmentPath({ attachmentsDir, attachment: image })!;
3470-
yield* fileSystem.writeFileString(path, "data");
3471-
const resolving = yield* Deferred.make<void>();
3472-
const release = yield* Deferred.make<void>();
3473-
const prompts: Array<unknown> = [];
3474-
const { runtime, thread } = yield* resumed(
3475-
[
3476-
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3477-
promptAccepted,
3478-
event("session.execution.started", { sessionID: SESSION }),
3479-
out("session.interrupt", { sessionID: SESSION }),
3480-
reply("session.interrupt", { interrupted: true }),
3481-
event("session.execution.interrupted", { sessionID: SESSION }),
3482-
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3483-
promptAccepted,
3484-
event("session.execution.started", { sessionID: SESSION }),
3485-
event("session.execution.succeeded", { sessionID: SESSION }),
3486-
],
3487-
{
3488-
attachmentsDir,
3489-
onPrompt: (input) => prompts.push(input),
3490-
fileSystem: {
3491-
...fileSystem,
3492-
exists: (file) =>
3493-
file === path
3494-
? Effect.gen(function* () {
3495-
yield* Deferred.succeed(resolving, undefined);
3496-
yield* Deferred.await(release);
3497-
return yield* fileSystem.exists(file);
3498-
})
3499-
: fileSystem.exists(file),
3500-
},
3501-
},
3502-
);
3503-
const stopped = yield* Deferred.make<void>();
3504-
const completed = yield* Deferred.make<void>();
3505-
const terminals: Array<string> = [];
3506-
yield* runtime.events.pipe(
3507-
Stream.tap((event) => {
3508-
if (event.type !== "turn.terminal") return Effect.void;
3509-
terminals.push(event.status);
3510-
return Deferred.succeed(terminals.length === 1 ? stopped : completed, undefined);
3511-
}),
3512-
Stream.runDrain,
3513-
Effect.forkScoped,
3514-
);
3515-
yield* runtime.startTurn(turnInput(thread));
3516-
const steer = yield* runtime
3517-
.steerTurn({
3518-
threadId,
3519-
runId: RunId.make("run:opencode2-adapter"),
3520-
providerThread: thread,
3521-
providerTurnId: yield* providerTurnId,
3522-
message: {
3523-
...turnInput(thread).message,
3524-
messageId: MessageId.make("message:opencode2-adapter:steer"),
3525-
text: "Do not send this image after Stop.",
3526-
attachments: [image],
3456+
it.effect.each([false, true])(
3457+
"rejects an image steer stopped during attachment resolution (held end: %s)",
3458+
(heldEnd) =>
3459+
Effect.gen(function* () {
3460+
const fileSystem = yield* FileSystem.FileSystem;
3461+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
3462+
prefix: "t3-oc2-stopped-image-",
3463+
});
3464+
const image = ChatImageAttachment.make({
3465+
type: "image",
3466+
id: ChatAttachmentId.make(
3467+
"thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc",
3468+
),
3469+
name: "screen.png",
3470+
mimeType: "image/png",
3471+
sizeBytes: 4,
3472+
});
3473+
const path = resolveAttachmentPath({ attachmentsDir, attachment: image })!;
3474+
yield* fileSystem.writeFileString(path, "data");
3475+
const resolving = yield* Deferred.make<void>();
3476+
const release = yield* Deferred.make<void>();
3477+
const gate = makeProviderReplayGate(["execution-end", "interrupt-reply"]);
3478+
const executionEnded = yield* Deferred.make<void>();
3479+
const prompts: Array<unknown> = [];
3480+
const { runtime, thread } = yield* resumed(
3481+
[
3482+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3483+
promptAccepted,
3484+
event("session.execution.started", { sessionID: SESSION }),
3485+
...(heldEnd
3486+
? [
3487+
event("session.execution.succeeded", { sessionID: SESSION }, "execution-end"),
3488+
event("session.text.ended", {
3489+
sessionID: SESSION,
3490+
assistantMessageID: "msg_finished_before_stop",
3491+
ordinal: 0,
3492+
text: "Finished before Stop.",
3493+
}),
3494+
]
3495+
: []),
3496+
out("session.interrupt", { sessionID: SESSION }),
3497+
...(heldEnd
3498+
? [event("session.usage.updated", { sessionID: SESSION }, "interrupt-reply")]
3499+
: []),
3500+
reply("session.interrupt", { interrupted: !heldEnd }),
3501+
...(heldEnd ? [] : [event("session.execution.interrupted", { sessionID: SESSION })]),
3502+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3503+
promptAccepted,
3504+
event("session.execution.started", { sessionID: SESSION }),
3505+
event("session.execution.succeeded", { sessionID: SESSION }),
3506+
],
3507+
{
3508+
attachmentsDir,
3509+
replayGate: gate,
3510+
onPrompt: (input) => prompts.push(input),
3511+
fileSystem: {
3512+
...fileSystem,
3513+
exists: (file) =>
3514+
file === path
3515+
? Effect.gen(function* () {
3516+
yield* Deferred.succeed(resolving, undefined);
3517+
yield* Deferred.await(release);
3518+
return yield* fileSystem.exists(file);
3519+
})
3520+
: fileSystem.exists(file),
3521+
},
35273522
},
3528-
})
3529-
.pipe(Effect.exit, Effect.forkScoped);
3530-
yield* Deferred.await(resolving);
3531-
yield* runtime.interruptTurn({
3532-
providerThread: thread,
3533-
providerTurnId: yield* providerTurnId,
3534-
});
3535-
yield* Deferred.await(stopped);
3536-
yield* Deferred.succeed(release, undefined);
3537-
const result = yield* Fiber.join(steer);
3538-
assert.lengthOf(prompts, 1);
3539-
assert.isTrue(Exit.isFailure(result));
3540-
if (Exit.isFailure(result)) {
3541-
assert.deepInclude(Cause.squash(result.cause), { _tag: "ProviderAdapterProtocolError" });
3542-
}
3543-
yield* runtime.startTurn(secondTurn(thread));
3544-
yield* Deferred.await(completed);
3545-
assert.lengthOf(prompts, 2);
3546-
assert.deepEqual(terminals, ["interrupted", "completed"]);
3547-
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
3523+
);
3524+
const stopped = yield* Deferred.make<void>();
3525+
const completed = yield* Deferred.make<void>();
3526+
const terminals: Array<string> = [];
3527+
yield* runtime.events.pipe(
3528+
Stream.tap((event) => {
3529+
if (
3530+
event.type === "turn_item.updated" &&
3531+
event.turnItem.type === "assistant_message" &&
3532+
event.turnItem.text === "Finished before Stop."
3533+
) {
3534+
return Deferred.succeed(executionEnded, undefined);
3535+
}
3536+
if (event.type !== "turn.terminal") return Effect.void;
3537+
terminals.push(event.status);
3538+
return Deferred.succeed(terminals.length === 1 ? stopped : completed, undefined);
3539+
}),
3540+
Stream.runDrain,
3541+
Effect.forkScoped,
3542+
);
3543+
yield* runtime.startTurn(turnInput(thread));
3544+
const steer = yield* runtime
3545+
.steerTurn({
3546+
threadId,
3547+
runId: RunId.make("run:opencode2-adapter"),
3548+
providerThread: thread,
3549+
providerTurnId: yield* providerTurnId,
3550+
message: {
3551+
...turnInput(thread).message,
3552+
messageId: MessageId.make("message:opencode2-adapter:steer"),
3553+
text: "Do not send this image after Stop.",
3554+
attachments: [image],
3555+
},
3556+
})
3557+
.pipe(Effect.exit, Effect.forkScoped);
3558+
yield* Deferred.await(resolving);
3559+
if (heldEnd) {
3560+
gate.release("execution-end");
3561+
yield* Deferred.await(executionEnded);
3562+
}
3563+
const interrupt = yield* runtime
3564+
.interruptTurn({
3565+
providerThread: thread,
3566+
providerTurnId: yield* providerTurnId,
3567+
})
3568+
.pipe(Effect.forkScoped);
3569+
if (heldEnd) {
3570+
assert.isTrue(yield* Effect.promise(() => gate.waitForReached("interrupt-reply")));
3571+
} else {
3572+
yield* Deferred.await(stopped);
3573+
}
3574+
yield* Deferred.succeed(release, undefined);
3575+
const result = yield* Fiber.join(steer);
3576+
assert.lengthOf(prompts, 1);
3577+
assert.isTrue(Exit.isFailure(result));
3578+
if (Exit.isFailure(result)) {
3579+
assert.deepInclude(Cause.squash(result.cause), { _tag: "ProviderAdapterProtocolError" });
3580+
}
3581+
yield* Deferred.await(stopped);
3582+
gate.release("interrupt-reply");
3583+
yield* Fiber.join(interrupt);
3584+
yield* runtime.startTurn(secondTurn(thread));
3585+
yield* Deferred.await(completed);
3586+
assert.lengthOf(prompts, 2);
3587+
assert.deepEqual(terminals, ["interrupted", "completed"]);
3588+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
35483589
);
35493590

35503591
it.effect("reads user and assistant text from the session's message list", () =>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3615,7 +3615,12 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
36153615
turn.steers.delete(inboxID);
36163616
// Stop may have stranded this steer, but it never reached OpenCode.
36173617
state.strandedSteers.delete(inboxID);
3618-
if (state.active === turn) yield* endIfSettled(state, turn);
3618+
if (state.active === turn) {
3619+
if (turn.interrupted && turn.heldEnd !== undefined) {
3620+
turn.heldEnd = { status: "interrupted" };
3621+
}
3622+
yield* endIfSettled(state, turn);
3623+
}
36193624
}),
36203625
);
36213626
return yield* new ProviderAdapter.ProviderAdapterProtocolError({

0 commit comments

Comments
 (0)