The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 21:00 UTC
fix(opencode): wait for a cold directory's scan before caching its skills (#31074)
Julius Marminge committed
Nov 11, 2026 at 15:47 UTC
289078a535c913478779d6be3714c5dbe6b715de
4 files changed
+111
-20
packages/provider-opencode/src/server/driver.test.ts
+44
-1
@@ -37,7 +37,11 @@ import * as ProviderMaintenance from "@t3tools/provider-core/server/maintenanceR
37
import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
38
import { OpenCodeDriver, isLocalOpenCodeServerUrl, openCodeUpdateFor } from "./driver.ts";
39
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";
45
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
46
47
it("only probes a locally configured OpenCode server's installed CLI", () => {
@@ -413,6 +417,45 @@ it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
417
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
418
);
419
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
+
459
it.effect.each(["probe", "error", "empty"] as const)(
460
"recovers discovery without a manual refresh after %s",
461
(initialCatalog) =>
packages/provider-opencode/src/server/driver.ts
+12
-2
@@ -379,7 +379,9 @@ export const OpenCodeDriver: ProviderDriver<
379
const listOpenCode2Workspace = (cwd: string) =>
380
openCode2Server.withConnection(({ client, events }) =>
381
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 };
385
const scanned = yield* Deferred.make<void>();
386
const pending = new Set(["command.updated", "skill.updated"]);
387
const stream = yield* events.pipe(Effect.option);
@@ -387,7 +389,7 @@ export const OpenCodeDriver: ProviderDriver<
389
yield* stream.value.pipe(
390
Stream.runForEach((event) =>
391
"location" in event &&
390
- event.location?.directory === cwd &&
392
+ event.location?.directory === directory &&
393
pending.delete(event.type) &&
394
pending.size === 0
395
? Deferred.succeed(scanned, undefined)
@@ -406,6 +408,14 @@ export const OpenCodeDriver: ProviderDriver<
408
{ concurrency: "unbounded" },
409
),
410
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
+ ),
419
);
420
}).pipe(Effect.scoped),
421
);
packages/provider-opencode/src/server/status.test.ts
+44
-11
@@ -1113,17 +1113,50 @@ const scanned: OpenCode2Workspace = {
1113
],
1114
};
1115
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", () =>
1135
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"]);
1161
}),
1162
);
packages/provider-opencode/src/server/status.ts
+11
-6
@@ -527,15 +527,20 @@ export interface OpenCode2Workspace {
527
}
528
529
/**
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.
534
*/
535
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;
542
const first = yield* read;
538
- if (first.commands.length > 0) return first;
543
+ if (wasLoaded && first.commands.length > 0) return first;
544
yield* scanned.pipe(Effect.timeoutOption("10 seconds"));
545
return yield* read;
546
},