Skip to content

Commit 289078a

Browse files
fix(opencode): wait for a cold directory's scan before caching its skills (pingdotgg#31074)
1 parent 52f0988 commit 289078a

4 files changed

Lines changed: 111 additions & 20 deletions

File tree

‎packages/provider-opencode/src/server/driver.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ import * as ProviderMaintenance from "@t3tools/provider-core/server/maintenanceR
3737
import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
3838
import { OpenCodeDriver, isLocalOpenCodeServerUrl, openCodeUpdateFor } from "./driver.ts";
3939
import * as OpenCodeServerLedger from "./OpenCodeServerLedger.ts";
40-
import { OPENCODE_2_RESPONSES, replayOpenCodeServer } from "./probeResponses.fixture.ts";
40+
import {
41+
OPENCODE_2_RESPONSES,
42+
OPENCODE_2_WORKSPACE_RESPONSES,
43+
replayOpenCodeServer,
44+
} from "./probeResponses.fixture.ts";
4145
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
4246

4347
it("only probes a locally configured OpenCode server's installed CLI", () => {
@@ -413,6 +417,45 @@ it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
413417
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
414418
);
415419

420+
it.effect.each(["/api/debug/location", "/api/event"])(
421+
"lists workspace skills from an external OpenCode 2 server whose %s never answers",
422+
(stalled) =>
423+
Effect.gen(function* () {
424+
const answers: Record<string, string> = {
425+
"/api/info": OPENCODE_2_RESPONSES["/api/info"].body,
426+
"/api/debug/location": "[]",
427+
"/api/command": OPENCODE_2_WORKSPACE_RESPONSES["/api/command"].body,
428+
"/api/skill": OPENCODE_2_WORKSPACE_RESPONSES["/api/skill"].body,
429+
};
430+
const http = HttpClient.make((request) => {
431+
const path = new URL(request.url).pathname;
432+
if (path === stalled) return Effect.never;
433+
if (path === "/api/event")
434+
return Effect.succeed(
435+
HttpClientResponse.fromWeb(
436+
request,
437+
new Response(new ReadableStream(), {
438+
headers: { "content-type": "text/event-stream" },
439+
}),
440+
),
441+
);
442+
const body = answers[path];
443+
return body === undefined
444+
? Effect.die(`Unexpected request: ${path}`)
445+
: Effect.succeed(HttpClientResponse.fromWeb(request, Response.json(JSON.parse(body))));
446+
});
447+
const instance = yield* create({ serverUrl: "http://127.0.0.1:4096" }, http);
448+
449+
const snapshot = yield* instance.snapshotForCwd!("/work").pipe(Effect.forkChild);
450+
yield* TestClock.adjust("30 seconds");
451+
const { skills } = yield* Fiber.join(snapshot);
452+
assert.include(
453+
skills.map((skill) => skill.name),
454+
"plum",
455+
);
456+
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
457+
);
458+
416459
it.effect.each(["probe", "error", "empty"] as const)(
417460
"recovers discovery without a manual refresh after %s",
418461
(initialCatalog) =>

‎packages/provider-opencode/src/server/driver.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,17 @@ export const OpenCodeDriver: ProviderDriver<
379379
const listOpenCode2Workspace = (cwd: string) =>
380380
openCode2Server.withConnection(({ client, events }) =>
381381
Effect.gen(function* () {
382-
const location = { directory: cwd };
382+
// OpenCode names a location by its resolved path (no trailing slash or `..`).
383+
const directory = pathService.resolve(cwd);
384+
const location = { directory };
383385
const scanned = yield* Deferred.make<void>();
384386
const pending = new Set(["command.updated", "skill.updated"]);
385387
const stream = yield* events.pipe(Effect.option);
386388
if (stream._tag === "Some") {
387389
yield* stream.value.pipe(
388390
Stream.runForEach((event) =>
389391
"location" in event &&
390-
event.location?.directory === cwd &&
392+
event.location?.directory === directory &&
391393
pending.delete(event.type) &&
392394
pending.size === 0
393395
? Deferred.succeed(scanned, undefined)
@@ -406,6 +408,14 @@ export const OpenCodeDriver: ProviderDriver<
406408
{ concurrency: "unbounded" },
407409
),
408410
Deferred.await(scanned),
411+
// Without events no scan can be awaited, so trust the first read as before.
412+
stream._tag === "None"
413+
? Effect.succeed(true)
414+
: client.debug.location.list().pipe(
415+
Effect.map((loaded) => loaded.some((entry) => entry.directory === directory)),
416+
Effect.timeout("2 seconds"),
417+
Effect.orElseSucceed(() => true),
418+
),
409419
);
410420
}).pipe(Effect.scoped),
411421
);

‎packages/provider-opencode/src/server/status.test.ts‎

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,17 +1113,50 @@ const scanned: OpenCode2Workspace = {
11131113
],
11141114
};
11151115

1116-
it.effect("reads a fresh OpenCode 2 directory again once its scan has ended", () =>
1116+
it.effect(
1117+
"reads a loaded OpenCode 2 directory listing no commands again once its scan has ended",
1118+
() =>
1119+
Effect.gen(function* () {
1120+
const reads: Array<OpenCode2Workspace> = [{ commands: [], skills: [] }, scanned];
1121+
const scanEnded = yield* Deferred.make<void>();
1122+
const load = yield* loadOpenCode2Workspace(
1123+
Effect.sync(() => reads.shift()!),
1124+
Deferred.await(scanEnded),
1125+
Effect.succeed(true),
1126+
).pipe(Effect.forkChild);
1127+
yield* Effect.yieldNow;
1128+
NodeAssert.equal(reads.length, 1);
1129+
yield* Deferred.succeed(scanEnded, undefined);
1130+
NodeAssert.deepEqual(yield* Fiber.join(load), scanned);
1131+
}),
1132+
);
1133+
1134+
it.effect("waits for the scan of a cold OpenCode 2 directory that first lists only built-ins", () =>
11171135
Effect.gen(function* () {
1118-
const reads: Array<OpenCode2Workspace> = [{ commands: [], skills: [] }, scanned];
1119-
const scanEnded = yield* Deferred.make<void>();
1120-
const load = yield* loadOpenCode2Workspace(
1121-
Effect.sync(() => reads.shift()!),
1122-
Deferred.await(scanEnded),
1123-
).pipe(Effect.forkChild);
1124-
yield* Effect.yieldNow;
1125-
NodeAssert.equal(reads.length, 1);
1126-
yield* Deferred.succeed(scanEnded, undefined);
1127-
NodeAssert.deepEqual(yield* Fiber.join(load), scanned);
1136+
const calls: Array<string> = [];
1137+
const reads: Array<OpenCode2Workspace> = [
1138+
{ commands: builtinCommands, skills: builtinSkills },
1139+
scanned,
1140+
];
1141+
const workspace = yield* loadOpenCode2Workspace(
1142+
Effect.sync(() => (calls.push("read"), reads.shift()!)),
1143+
Effect.sync(() => void calls.push("scanned")),
1144+
Effect.sync(() => (calls.push("loaded"), false)),
1145+
);
1146+
NodeAssert.deepEqual(workspace, scanned);
1147+
NodeAssert.deepEqual(calls, ["loaded", "read", "scanned", "read"]);
1148+
}),
1149+
);
1150+
1151+
it.effect("reads an already loaded OpenCode 2 directory once without waiting for a scan", () =>
1152+
Effect.gen(function* () {
1153+
const calls: Array<string> = [];
1154+
const workspace = yield* loadOpenCode2Workspace(
1155+
Effect.sync(() => (calls.push("read"), scanned)),
1156+
Effect.sync(() => void calls.push("scanned")),
1157+
Effect.succeed(true),
1158+
);
1159+
NodeAssert.deepEqual(workspace, scanned);
1160+
NodeAssert.deepEqual(calls, ["read"]);
11281161
}),
11291162
);

‎packages/provider-opencode/src/server/status.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,20 @@ export interface OpenCode2Workspace {
527527
}
528528

529529
/**
530-
* A server scans a directory it has not served yet in the background: its first read lists
531-
* no commands at all (not even the built-ins), and `command.updated` plus `skill.updated`
532-
* for the directory end the scan. `scanned` must be listening before `read` runs, since
533-
* the read is what starts the scan.
530+
* Reading a location the server has not loaded boots its scan, and only reads after the scan's
531+
* `command.updated` plus `skill.updated` are complete; `scanned` must listen before `read`. A
532+
* loaded location listing no commands is still scanning. OpenCode exposes no readiness signal,
533+
* so one another caller booted moments earlier can still read partial.
534534
*/
535535
export const loadOpenCode2Workspace = Effect.fn("OpencodeProviderStatus.loadOpenCode2Workspace")(
536-
function* <E>(read: Effect.Effect<OpenCode2Workspace, E>, scanned: Effect.Effect<void>) {
536+
function* <E>(
537+
read: Effect.Effect<OpenCode2Workspace, E>,
538+
scanned: Effect.Effect<void>,
539+
loaded: Effect.Effect<boolean>,
540+
) {
541+
const wasLoaded = yield* loaded;
537542
const first = yield* read;
538-
if (first.commands.length > 0) return first;
543+
if (wasLoaded && first.commands.length > 0) return first;
539544
yield* scanned.pipe(Effect.timeoutOption("10 seconds"));
540545
return yield* read;
541546
},

0 commit comments

Comments
 (0)