Skip to content

Commit a62a0d5

Browse files
Bewinxedclaude
andcommitted
An OpenCode session's first prompt in a fresh location waits until Code Mode's catalog has CawCo's tools
setup-on-ask r78 failed on OpenCode only: Caw's first step in a fresh location was answered "Unknown tool 'cawco.project_setup'" by OpenCode itself (its stored tool output), and the call never reached the gateway (no tool_call_keys row; folder_write and the rest, later steps, have theirs). OpenCode takes a server's tools into its Code Mode catalog on a debounce after the server connects (anomalyco/opencode#54794, its fix #54942 unmerged at 2.0.32): the catalog took CawCo's tools 195 ms after the connect, and the step came first. The agent waited for "connected" alone. waitForMcp now waits CATALOG_SETTLE_MS more, once per location on each server. source-burst leg 6: four OpenCode sessions, each in a fresh location, their start and first message back to back as the hub starts Caw; each first step calls a CawCo tool. Each answer must reach the model whole, and each first model request come CATALOG_SETTLE_MS or more after OpenCode logged CawCo's server connected there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent daf1cb1 commit a62a0d5

2 files changed

Lines changed: 221 additions & 1 deletion

File tree

‎packages/agent/src/harnesses/opencode-client.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,39 @@ export const MCP_CONNECT_TIMEOUT_MS = 60_000;
235235
/** CawCo's own MCP server, as every server's launch config names it (`mcp.servers.cawco`). */
236236
export const CAWCO_MCP_SERVER = "cawco";
237237

238+
/**
239+
* How long a location's first prompt waits once its MCP servers are seen
240+
* connected there. OpenCode takes a connected server's tools into its Code
241+
* Mode catalog on a debounce ("its catalog update waited in the 100ms
242+
* debounce", anomalyco/opencode#54942, the fix for #54794, unmerged at
243+
* 2.0.32), and a step before then runs on a catalog without them: Caw's
244+
* first step in a fresh location was answered "Unknown tool
245+
* 'cawco.project_setup'" by OpenCode itself, its catalog taking CawCo's tools
246+
* 195 ms after the server connected (setup-on-ask r78). OpenCode says when
247+
* the catalog changes in no event or route, so the debounce is waited out.
248+
* Ours: the debounce ten times over.
249+
*/
250+
export const CATALOG_SETTLE_MS = 1000;
251+
252+
/** The locations each server's catalog has had its settle in ({@link CATALOG_SETTLE_MS}). */
253+
const catalogSettled = new WeakMap<OpencodeClient, Set<string>>();
254+
255+
/** Waits out a location's catalog refresh, once per location on each server. */
256+
const settleCatalog = async (
257+
client: OpencodeClient,
258+
directory: string,
259+
signal: AbortSignal
260+
): Promise<void> => {
261+
const settled = catalogSettled.get(client) ?? new Set<string>();
262+
catalogSettled.set(client, settled);
263+
if (settled.has(directory)) {
264+
return;
265+
}
266+
await Bun.sleep(CATALOG_SETTLE_MS);
267+
signal.throwIfAborted();
268+
settled.add(directory);
269+
};
270+
238271
/** An MCP server that had not connected when a location took its first prompt, and why. */
239272
export interface McpNotConnected {
240273
name: string;
@@ -264,6 +297,10 @@ export interface McpNotConnected {
264297
* that time, the last list the server gave decides, as the deadline did:
265298
* CawCo's server connected settles with the others still `pending` said as
266299
* still connecting, anything else refuses.
300+
*
301+
* Connected is not yet in the catalog: the location's first wait on each
302+
* server then waits {@link CATALOG_SETTLE_MS} more, for OpenCode's catalog
303+
* to take the servers' tools.
267304
*/
268305
export const waitForMcp = async (
269306
client: OpencodeClient,
@@ -336,10 +373,12 @@ export const waitForMcp = async (
336373
);
337374
switch (settled.kind) {
338375
case "answered":
376+
await settleCatalog(client, directory, signal);
339377
return settled.value;
340378
case "unanswered": {
341379
const cawco = last ? cawcoOf(last) : undefined;
342380
if (last && cawco?.status === "connected") {
381+
await settleCatalog(client, directory, signal);
343382
return notConnected(last);
344383
}
345384
throw new Error(

‎scripts/probe-kit/specs/source-burst.ts‎

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@
4747
import { readFileSync } from "node:fs";
4848
import { mkdir, writeFile } from "node:fs/promises";
4949
import { join } from "node:path";
50-
import { MACHINE, type Reply, type Seen, until } from "../../scratch-fleet";
50+
import { CATALOG_SETTLE_MS } from "../../../packages/agent/src/harnesses/opencode-client";
51+
import {
52+
MACHINE,
53+
MODEL,
54+
type Reply,
55+
type Seen,
56+
until,
57+
} from "../../scratch-fleet";
5158
import type { Kit, Spec } from "../kit";
5259
import { type Relay, relay } from "../relay";
5360

@@ -147,7 +154,71 @@ const once = (request: Seen): Reply => {
147154
};
148155
};
149156

157+
/** How a leg-6 session's prompt names it: the tag and its location's number. */
158+
const OPENCODE_TAG = "PROBE-SOURCE-BURST-OPENCODE";
159+
const OPENCODE_LANE = new RegExp(`${OPENCODE_TAG}-(\\d+)`);
160+
/**
161+
* Fresh OpenCode locations leg 6 starts a session in. Ours: each first step
162+
* races OpenCode's catalog refresh once (anomalyco/opencode#54794); one
163+
* location lost that race in setup-on-ask r78, and one alone wins it often.
164+
*/
165+
const OPENCODE_LOCATIONS = 4;
166+
/** OpenCode's log line for CawCo's server connected in a location: its time and the location. */
167+
const CAWCO_CONNECTED =
168+
/^timestamp=(\S+) .*message="mcp connected" server=cawco .*directory=(\S+)/;
169+
/** OpenCode's Code Mode tool, which runs MCP tools from a script. */
170+
const CODE_MODE = "execute";
171+
/** Leg 6, per location: the tool its first step called, and the request after it. */
172+
const leg6 = new Map<
173+
string,
174+
{ answer?: Seen; called: string; firstAt: number }
175+
>();
176+
177+
/** A leg-6 session's title: its location's, so each answer names its own. */
178+
const opencodeTitle = (lane: string): string => `Titled from OpenCode ${lane}`;
179+
180+
/**
181+
* A leg-6 session, on OpenCode: its first step calls cawco's set_title as
182+
* OpenCode offers it (its MCP name, or its Code Mode catalog), at once, and
183+
* the request after the call is kept.
184+
*/
185+
const fromOpencode = (request: Seen, lane: string): Reply => {
186+
const mine = leg6.get(lane);
187+
if (mine) {
188+
if (request.afterTool) {
189+
mine.answer ??= request;
190+
}
191+
return text("ok");
192+
}
193+
const input = { title: opencodeTitle(lane) };
194+
const direct = request.toolNames.find((name) =>
195+
["cawco_set_title", SET_TITLE].includes(name)
196+
);
197+
if (direct) {
198+
leg6.set(lane, { called: direct, firstAt: request.at });
199+
return { everyMs: 5, words: [], tool: { name: direct, input } };
200+
}
201+
if (request.toolNames.includes(CODE_MODE)) {
202+
leg6.set(lane, { called: CODE_MODE, firstAt: request.at });
203+
return {
204+
everyMs: 5,
205+
words: [],
206+
tool: {
207+
name: CODE_MODE,
208+
input: {
209+
code: `return await tools.cawco.set_title(${JSON.stringify(input)});`,
210+
},
211+
},
212+
};
213+
}
214+
return text(`set_title is not offered: ${request.toolNames.join(", ")}`);
215+
};
216+
150217
const respond = (request: Seen): Reply => {
218+
const lane = request.tools ? OPENCODE_LANE.exec(request.all)?.[1] : undefined;
219+
if (lane !== undefined) {
220+
return fromOpencode(request, lane);
221+
}
151222
if (request.tools && request.last.includes(LOST_TAG)) {
152223
leg4.turns.push(request);
153224
return text("ok");
@@ -283,6 +354,115 @@ const loseSend = async (at: Legs): Promise<void> => {
283354
);
284355
};
285356

357+
/**
358+
* Leg 6: OpenCode sessions, each in a location of its own that no session
359+
* has used, whose first step calls a CawCo tool at once through the
360+
* machine's gateway: each has CawCo's tools on that first step, and the
361+
* hub's answer reaches its model whole. In setup-on-ask r78, Caw's first
362+
* step in a fresh location was answered "Unknown tool
363+
* 'cawco.project_setup'" by OpenCode itself: its Code Mode catalog took
364+
* CawCo's tools 195 ms after the server connected, and the step came first.
365+
* Which comes first is a race, which a lightly loaded machine always wins,
366+
* so what decides it is read too: each location's first model request comes
367+
* at least the agent's settle after OpenCode logged CawCo's server connected
368+
* there (agent opencode-client.ts `CATALOG_SETTLE_MS`).
369+
*/
370+
const opencodeAnswer = async (at: Legs): Promise<void> => {
371+
const lanes = Array.from({ length: OPENCODE_LOCATIONS }, (_, index) =>
372+
String(index)
373+
);
374+
const outcomes: string[] = [];
375+
for (const lane of lanes) {
376+
const place = join(at.src, `opencode-${lane}`);
377+
// biome-ignore lint/performance/noAwaitInLoops: one fresh location at a time, each its own race
378+
await mkdir(place, { recursive: true });
379+
// Its start and its first message back to back, as the hub starts Caw
380+
// with the message that woke him: the message goes right behind the
381+
// start, and the location's first prompt as soon as it is ready.
382+
const session = crypto.randomUUID();
383+
await at.fleet.post({
384+
verb: "spawn",
385+
machineId: MACHINE,
386+
instanceId: session,
387+
requestId: crypto.randomUUID(),
388+
payload: {
389+
instanceId: session,
390+
cwd: place,
391+
harness: "opencode",
392+
model: MODEL.opencode,
393+
title: `OpenCode ${lane}`,
394+
permissionMode: "bypassPermissions",
395+
projectId: at.projectId,
396+
},
397+
});
398+
await at.fleet.send(session, `${OPENCODE_TAG}-${lane}: set your title.`);
399+
const answered = await until(
400+
`OpenCode ${lane}'s first call is answered`,
401+
() => leg6.get(lane)?.answer,
402+
Boolean,
403+
120_000
404+
).catch(() => undefined);
405+
const named = at.fleet.query<{ title: string | null }>(
406+
"SELECT title FROM instances WHERE id = ?",
407+
session
408+
)[0]?.title;
409+
const said = answered?.body.indexOf("Named this session") ?? -1;
410+
const unknown = answered?.body.indexOf("Unknown tool") ?? -1;
411+
const carried =
412+
unknown >= 0
413+
? answered?.body.slice(unknown, unknown + 90)
414+
: `no answer in ${answered?.body.slice(-300)}`;
415+
outcomes.push(
416+
said >= 0 && named === opencodeTitle(lane)
417+
? `${lane}: answered`
418+
: `${lane}: ${answered ? carried : "never answered"}; title ${named ?? "none"}`
419+
);
420+
}
421+
at.check(
422+
`an OpenCode session's first step in a location no session used has CawCo's tools, and its call's answer reaches its model whole (${OPENCODE_LOCATIONS} locations)`,
423+
outcomes.every((one) => one.endsWith(": answered")),
424+
outcomes.join(" | ").slice(0, 1500)
425+
);
426+
const connected = await cawcoConnectedAt(at.fleet.home);
427+
const gaps = lanes.map((lane) => {
428+
const since = connected.get(join(at.src, `opencode-${lane}`));
429+
const first = leg6.get(lane)?.firstAt;
430+
return since === undefined || first === undefined
431+
? undefined
432+
: first - since;
433+
});
434+
at.check(
435+
`each fresh location's first prompt waits out OpenCode's catalog refresh: its first model request comes ${CATALOG_SETTLE_MS} ms or more after CawCo's server connected there`,
436+
gaps.every((gap) => gap !== undefined && gap >= CATALOG_SETTLE_MS),
437+
lanes
438+
.map(
439+
(lane, index) =>
440+
`${lane}: ${gaps[index] === undefined ? "no connect or no request seen" : `${gaps[index]} ms`}`
441+
)
442+
.join(" | ")
443+
);
444+
};
445+
446+
/** When OpenCode logged CawCo's MCP server connected, by the location's directory (the last time each). */
447+
const cawcoConnectedAt = async (home: string): Promise<Map<string, number>> => {
448+
const dir = join(home, ".local", "share", "opencode", "log");
449+
const files = await Array.fromAsync(
450+
new Bun.Glob("*.log").scan({ cwd: dir })
451+
).catch(() => [] as string[]);
452+
const connected = new Map<string, number>();
453+
for (const file of files) {
454+
// biome-ignore lint/performance/noAwaitInLoops: a few log files, read in turn
455+
const log = await Bun.file(join(dir, file)).text();
456+
for (const line of log.split("\n")) {
457+
const found = CAWCO_CONNECTED.exec(line);
458+
if (found?.[1] && found[2]) {
459+
connected.set(found[2], Date.parse(found[1]));
460+
}
461+
}
462+
}
463+
return connected;
464+
};
465+
286466
/** Leg 5. */
287467
const startThroughCut = async (at: Legs): Promise<void> => {
288468
// 5. A start posted 1 s into a 3 s cut of the second machine's link.
@@ -446,6 +626,7 @@ const spec: Spec = {
446626
await handOnce(legs, parent);
447627
await loseSend(legs);
448628
await startThroughCut(legs);
629+
await opencodeAnswer(legs);
449630
check(
450631
"no request through the second machine's link is cut for waiting on the hub: the relay, standing in for that link, has no idle timeout of its own",
451632
between.idleCuts.length === 0,

0 commit comments

Comments
 (0)