Skip to content

Commit 80401f6

Browse files
committed
fix(opencode): settle image jobs through preview and transport failures
1 parent e701863 commit 80401f6

4 files changed

Lines changed: 311 additions & 61 deletions

File tree

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

Lines changed: 206 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ const resumed = (
326326
readonly resolveExtractedTextPath?: (path: string) => Effect.Effect<string | undefined>;
327327
readonly fileSystem?: FileSystem.FileSystem;
328328
readonly onPrompt?: (input: unknown) => void;
329+
readonly borrowers?: { current: number };
329330
},
330331
) =>
331332
Effect.gen(function* () {
@@ -352,6 +353,7 @@ const resumed = (
352353
],
353354
{
354355
...(options?.version === undefined ? {} : { version: options.version }),
356+
...(options?.borrowers === undefined ? {} : { borrowers: options.borrowers }),
355357
...(options?.fileSystem === undefined ? {} : { fileSystem: options.fileSystem }),
356358
...(options?.onPrompt === undefined ? {} : { onPrompt: options.onPrompt }),
357359
...(options?.external === undefined ? {} : { external: options.external }),
@@ -3000,7 +3002,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30003002
}).pipe(Effect.scoped),
30013003
);
30023004

3003-
it.effect.each(["history", "list"] as const)(
3005+
it.effect.each(["history", "list", "late-preview"] as const)(
30043006
"recovers native image job outcomes from %s after reconnect",
30053007
(source) =>
30063008
Effect.gen(function* () {
@@ -3026,14 +3028,14 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30263028
},
30273029
],
30283030
});
3029-
if (source === "list")
3031+
if (source !== "history")
30303032
frames.splice(2, 0, out("rpc.call", "<any>"), reply("rpc.call", { output: null }));
30313033
return frames;
30323034
})(),
30333035
out("message.list", { sessionID: SESSION, type: "synthetic", limit: "100" }),
30343036
reply("message.list", {
30353037
data:
3036-
source === "list"
3038+
source !== "history"
30373039
? []
30383040
: [
30393041
{
@@ -3052,7 +3054,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30523054
],
30533055
cursor: {},
30543056
}),
3055-
...(source === "list"
3057+
...(source !== "history"
30563058
? [
30573059
out("imageJobs", { sessionID: SESSION, kind: "image" }),
30583060
reply("imageJobs", {
@@ -3061,7 +3063,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30613063
kind: "image",
30623064
id: "image_gap",
30633065
sessionID: SESSION,
3064-
status: "error",
3066+
status: source === "late-preview" ? "completed" : "error",
30653067
path: "/tmp/image",
30663068
model: "test/image",
30673069
time: { started: 2, completed: 3 },
@@ -3070,15 +3072,45 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30703072
}),
30713073
]
30723074
: []),
3075+
...(source === "late-preview"
3076+
? [
3077+
event("session.inbox.enqueued", {
3078+
sessionID: SESSION,
3079+
inboxID: "msg_image_late",
3080+
item: {
3081+
type: "synthetic",
3082+
delivery: "steer",
3083+
payload: {
3084+
description: "Image generation",
3085+
text: "Image request finished. Saved image: /tmp/image.png (test/image).",
3086+
metadata: {
3087+
source: "experimental_image_generate",
3088+
jobID: "image_gap",
3089+
state: "completed",
3090+
path: "/tmp/image",
3091+
},
3092+
},
3093+
},
3094+
}),
3095+
out("image.read", {
3096+
path: "/api/fs/read/image.png",
3097+
"location[directory]": "/tmp",
3098+
}),
3099+
reply("image.read", { imageBase64: "iVBORw0KGgo=" }),
3100+
]
3101+
: []),
30733102
],
3074-
source === "list" ? { version: "2.2.6" } : undefined,
3103+
{ ...(source !== "history" ? { version: "2.2.6" as const } : {}), external: true },
30753104
);
30763105
const events = yield* runtime.events.pipe(
30773106
Stream.takeUntil(
30783107
(e) =>
30793108
e.type === "turn_item.updated" &&
30803109
e.turnItem.nativeItemRef?.nativeId === "call-image-gap" &&
3081-
e.turnItem.status === "failed",
3110+
(source === "late-preview"
3111+
? e.turnItem.type === "dynamic_tool" &&
3112+
toolOutputImages(e.turnItem.output).length > 0
3113+
: e.turnItem.status === "failed"),
30823114
),
30833115
Stream.runCollect,
30843116
Effect.forkScoped,
@@ -3087,7 +3119,11 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30873119
const collected = yield* Fiber.join(events);
30883120
assert.include(
30893121
JSON.stringify(collected.at(-1)),
3090-
source === "list" ? "Image generation error" : "Recovered image failure",
3122+
source === "late-preview"
3123+
? "Saved image: /tmp/image.png"
3124+
: source !== "history"
3125+
? "Image generation error"
3126+
: "Recovered image failure",
30913127
);
30923128
assert.isTrue(
30933129
collected.some(
@@ -3099,6 +3135,168 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
30993135
}).pipe(Effect.scoped),
31003136
);
31013137

3138+
it.effect("settles a recovered image outcome when preview I/O is interrupted", () =>
3139+
Effect.gen(function* () {
3140+
const reading = yield* Deferred.make<void>();
3141+
const interrupted = yield* Deferred.make<void>();
3142+
const fs = yield* FileSystem.FileSystem;
3143+
const { runtime, thread } = yield* resumed(
3144+
[
3145+
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3146+
promptAccepted,
3147+
...reconnected({}, "succeeded", {
3148+
content: [
3149+
{
3150+
type: "tool",
3151+
id: "call-image-slow",
3152+
name: "experimental_image_generate",
3153+
executed: false,
3154+
state: {
3155+
status: "completed",
3156+
input: { prompt: "A tree" },
3157+
content: [{ type: "text", text: "Launched" }],
3158+
metadata: { jobID: "image_slow", path: "/tmp/image", model: "test/image" },
3159+
},
3160+
time: { created: 2, ran: 2, completed: 2 },
3161+
},
3162+
],
3163+
}),
3164+
out("message.list", { sessionID: SESSION, type: "synthetic", limit: "100" }),
3165+
reply("message.list", {
3166+
data: [
3167+
{
3168+
id: "msg_image_slow",
3169+
type: "synthetic",
3170+
time: { created: 3 },
3171+
text: "Image request finished. Saved image: /tmp/image.png (test/image).",
3172+
description: "Image generation",
3173+
metadata: {
3174+
source: "experimental_image_generate",
3175+
jobID: "image_slow",
3176+
state: "completed",
3177+
path: "/tmp/image",
3178+
},
3179+
},
3180+
],
3181+
cursor: {},
3182+
}),
3183+
event("session.model.selected", {
3184+
sessionID: SESSION,
3185+
model: { providerID: "opencode", id: "big-pickle", variant: "recovery-observed" },
3186+
}),
3187+
],
3188+
{
3189+
fileSystem: {
3190+
...fs,
3191+
stat: (path) =>
3192+
path === "/tmp/image.png"
3193+
? Deferred.succeed(reading, undefined).pipe(
3194+
Effect.andThen(Effect.never),
3195+
Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined)),
3196+
)
3197+
: fs.stat(path),
3198+
},
3199+
},
3200+
);
3201+
const events = yield* runtime.events.pipe(
3202+
Stream.takeUntil(
3203+
(e) =>
3204+
e.type === "provider_thread.updated" &&
3205+
e.providerThread.nativeMetadata?.modelSelection?.options?.some(
3206+
(option) => option.value === "recovery-observed",
3207+
) === true,
3208+
),
3209+
Stream.runCollect,
3210+
Effect.forkScoped,
3211+
);
3212+
yield* runtime.startTurn(turnInput(thread));
3213+
yield* Deferred.await(reading);
3214+
yield* TestClock.adjust("5 seconds");
3215+
yield* Deferred.await(interrupted);
3216+
const collected = yield* Fiber.join(events);
3217+
assert.isTrue(
3218+
collected.some(
3219+
(e) =>
3220+
e.type === "turn_item.updated" &&
3221+
e.turnItem.nativeItemRef?.nativeId === "call-image-slow" &&
3222+
e.turnItem.status === "completed",
3223+
),
3224+
);
3225+
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
3226+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
3227+
);
3228+
3229+
it.effect("settles native image tasks when reconnect attempts fail", () =>
3230+
Effect.gen(function* () {
3231+
const attempts = yield* Effect.forEach([0, 1, 2, 3, 4], () => Deferred.make<void>());
3232+
let count = 0;
3233+
let failures = 0;
3234+
const borrowers = {
3235+
get current() {
3236+
return count;
3237+
},
3238+
set current(next: number) {
3239+
if (next < count && count === 2) {
3240+
const attempt = attempts[failures++];
3241+
if (attempt !== undefined) Deferred.doneUnsafe(attempt, Effect.void);
3242+
}
3243+
count = next;
3244+
},
3245+
};
3246+
const tool = {
3247+
sessionID: SESSION,
3248+
assistantMessageID: "msg_assistant",
3249+
id: "call-image-lost",
3250+
};
3251+
const { runtime, thread } = yield* resumed(
3252+
[
3253+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3254+
promptAccepted,
3255+
event("session.tool.input.started", { ...tool, name: "experimental_image_generate" }),
3256+
event(
3257+
"session.tool.success",
3258+
{
3259+
...tool,
3260+
content: [{ type: "text", text: "Launched" }],
3261+
metadata: { jobID: "image_lost", path: "/tmp/image", model: "test/image" },
3262+
executed: false,
3263+
},
3264+
undefined,
3265+
2,
3266+
),
3267+
event("session.execution.succeeded", { sessionID: SESSION }),
3268+
{ type: "runtime_exit", status: "success" },
3269+
],
3270+
{ borrowers },
3271+
);
3272+
const events = yield* runtime.events.pipe(Stream.runCollect, Effect.forkScoped);
3273+
yield* runtime.startTurn(turnInput(thread));
3274+
yield* Deferred.await(attempts[0]!);
3275+
for (const attempt of attempts.slice(1)) {
3276+
yield* TestClock.adjust("2 seconds");
3277+
yield* Deferred.await(attempt);
3278+
}
3279+
const collected = yield* Fiber.join(events);
3280+
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
3281+
assert.isTrue(
3282+
collected.some(
3283+
(e) =>
3284+
e.type === "turn_item.updated" &&
3285+
e.turnItem.nativeItemRef?.nativeId === tool.id &&
3286+
e.turnItem.status === "failed",
3287+
),
3288+
);
3289+
assert.deepEqual(
3290+
collected
3291+
.flatMap((e) =>
3292+
e.type === "provider_thread.updated" ? [e.providerThread.pendingBackgroundTasks] : [],
3293+
)
3294+
.at(-1),
3295+
[],
3296+
);
3297+
}).pipe(Effect.scoped),
3298+
);
3299+
31023300
it.effect("projects successful native todo updates into one checklist", () =>
31033301
Effect.gen(function* () {
31043302
const updates = [

‎packages/provider-opencode/src/server/v2/OpenCode2Client.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ describe("OpenCode2Client native image jobs", () => {
198198
}),
199199
);
200200

201+
it.effect("rejects remote previews above the shared image limit", () =>
202+
Effect.gen(function* () {
203+
const transport = HttpClient.make((request) =>
204+
Effect.succeed(
205+
HttpClientResponse.fromWeb(request, new Response(new Uint8Array(11 * 1024 * 1024))),
206+
),
207+
);
208+
const service = yield* OpenCode2Client.make.pipe(
209+
Effect.provideService(HttpClient.HttpClient, transport),
210+
);
211+
const api = yield* service.connect({ baseUrl: "http://remote.test", password: "secret" });
212+
const result = yield* api.readImage("/work/image.png", "/work").pipe(Effect.result);
213+
assert.equal(result._tag, "Failure");
214+
}),
215+
);
216+
201217
it.effect("reads scoped image jobs without accepting other sessions or job kinds", () =>
202218
Effect.gen(function* () {
203219
const job = {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Path from "effect/Path";
2+
import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts";
23
import { OpenCode, type OpenCodeClient } from "@opencode/client/effect";
34
import { OpenCodeEvent } from "@opencode/protocol/groups/event";
45
import * as Context from "effect/Context";
@@ -56,6 +57,15 @@ export class OpenCode2ImageError extends Schema.TaggedError<OpenCode2ImageError>
5657
{ cause: Schema.Defect() },
5758
) {}
5859

60+
const IMAGE_MIME: Readonly<Record<string, string>> = {
61+
".png": "image/png",
62+
".jpg": "image/jpeg",
63+
".jpeg": "image/jpeg",
64+
".webp": "image/webp",
65+
".gif": "image/gif",
66+
};
67+
export const imagePreviewMime = (extension: string) => IMAGE_MIME[extension.toLowerCase()];
68+
5969
const NativeImageJob = Schema.Struct({
6070
kind: Schema.Literal("image"),
6171
id: Schema.String,
@@ -260,15 +270,7 @@ export const make = Effect.gen(function* () {
260270
),
261271
readImage: Effect.fn("OpenCode2Client.readImage")(
262272
function* (path: string, directory: string) {
263-
const mime = (
264-
{
265-
".png": "image/png",
266-
".jpg": "image/jpeg",
267-
".jpeg": "image/jpeg",
268-
".webp": "image/webp",
269-
".gif": "image/gif",
270-
} as Record<string, string>
271-
)[paths.extname(path).toLowerCase()];
273+
const mime = imagePreviewMime(paths.extname(path));
272274
if (mime === undefined)
273275
return yield* new OpenCode2ImageError({ cause: "Unsupported image format" });
274276
const relative = paths.relative(directory, path);
@@ -285,7 +287,7 @@ export const make = Effect.gen(function* () {
285287
})
286288
.pipe(Effect.flatMap(HttpClientResponse.filterStatusOk));
287289
const bytes = yield* response.arrayBuffer;
288-
if (bytes.byteLength > 20 * 1024 * 1024)
290+
if (bytes.byteLength > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)
289291
return yield* new OpenCode2ImageError({ cause: "Image exceeds preview limit" });
290292
return { uri: `data:${mime};base64,${Buffer.from(bytes).toString("base64")}`, mime };
291293
},

0 commit comments

Comments
 (0)