|
47 | 47 | import { readFileSync } from "node:fs"; |
48 | 48 | import { mkdir, writeFile } from "node:fs/promises"; |
49 | 49 | 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"; |
51 | 58 | import type { Kit, Spec } from "../kit"; |
52 | 59 | import { type Relay, relay } from "../relay"; |
53 | 60 |
|
@@ -147,7 +154,71 @@ const once = (request: Seen): Reply => { |
147 | 154 | }; |
148 | 155 | }; |
149 | 156 |
|
| 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 | + |
150 | 217 | 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 | + } |
151 | 222 | if (request.tools && request.last.includes(LOST_TAG)) { |
152 | 223 | leg4.turns.push(request); |
153 | 224 | return text("ok"); |
@@ -283,6 +354,115 @@ const loseSend = async (at: Legs): Promise<void> => { |
283 | 354 | ); |
284 | 355 | }; |
285 | 356 |
|
| 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 | + |
286 | 466 | /** Leg 5. */ |
287 | 467 | const startThroughCut = async (at: Legs): Promise<void> => { |
288 | 468 | // 5. A start posted 1 s into a 3 s cut of the second machine's link. |
@@ -446,6 +626,7 @@ const spec: Spec = { |
446 | 626 | await handOnce(legs, parent); |
447 | 627 | await loseSend(legs); |
448 | 628 | await startThroughCut(legs); |
| 629 | + await opencodeAnswer(legs); |
449 | 630 | check( |
450 | 631 | "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", |
451 | 632 | between.idleCuts.length === 0, |
|
0 commit comments