|
| 1 | +import { createBlobReader } from "@intx/types/runtime"; |
1 | 2 | import { describe, expect, test } from "bun:test"; |
| 3 | +import type { Task } from "../agent/tasks.js"; |
2 | 4 | import { createFleetMailbox } from "./agent-fleet.js"; |
3 | 5 | import { |
4 | 6 | buildFleetDryContinuationPrompt, |
5 | 7 | collectUncollectedTerminals, |
6 | 8 | driveOpenTasksAfterFleetDry, |
7 | 9 | FLEET_DRY_CONTINUATION_PREFIX, |
8 | 10 | FLEET_DRY_REPORT_CHARS, |
| 11 | + fleetDrySpillKey, |
9 | 12 | shouldDriveOpenTasks, |
10 | 13 | type FleetDryMailbox, |
11 | 14 | type FleetDryMailboxRecord, |
12 | 15 | } from "./fleet-dry-drive.js"; |
13 | 16 | import { createSubAgentSessionStore } from "./session-store.js"; |
14 | | -import type { Task } from "../agent/tasks.js"; |
15 | 17 |
|
16 | 18 | const openTask: Task = { id: "t1", title: "keep going", status: "todo" }; |
17 | 19 |
|
| 20 | +function peekMailbox( |
| 21 | + records: Map<string, FleetDryMailboxRecord>, |
| 22 | +): FleetDryMailbox { |
| 23 | + return { |
| 24 | + ids: () => [...records.keys()], |
| 25 | + peek: (id) => records.get(id), |
| 26 | + take: (id) => records.get(id), |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function fakeBlobStore() { |
| 31 | + const blobs = new Map<string, { bytes: Uint8Array; contentType: string }>(); |
| 32 | + return { |
| 33 | + blobs, |
| 34 | + writeBlob: (key: string, bytes: Uint8Array, contentType: string) => { |
| 35 | + blobs.set(key, { bytes, contentType }); |
| 36 | + }, |
| 37 | + readBlob: async (key: string) => { |
| 38 | + const entry = blobs.get(key); |
| 39 | + if (entry === undefined) throw new Error(`Blob not found: ${key}`); |
| 40 | + return entry.bytes; |
| 41 | + }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +function reportsJSONFromPrompt(prompt: string): unknown { |
| 46 | + const header = |
| 47 | + "Collected worker reports (already collected — do not call wait_agents for these agent_ids):\n"; |
| 48 | + const start = prompt.indexOf(header); |
| 49 | + expect(start).toBeGreaterThanOrEqual(0); |
| 50 | + const jsonStart = start + header.length; |
| 51 | + const jsonEnd = prompt.indexOf("\n", jsonStart); |
| 52 | + return JSON.parse( |
| 53 | + prompt.slice(jsonStart, jsonEnd === -1 ? undefined : jsonEnd), |
| 54 | + ); |
| 55 | +} |
| 56 | + |
18 | 57 | describe("shouldDriveOpenTasks", () => { |
19 | 58 | test("is true only on wentDry && open tasks && !parentProcessing", () => { |
20 | 59 | expect( |
@@ -250,21 +289,94 @@ describe("collectUncollectedTerminals", () => { |
250 | 289 | ]); |
251 | 290 | }); |
252 | 291 |
|
253 | | - test("clips oversized reports", () => { |
| 292 | + test("clips oversized reports with an honest not-retrievable notice when no writer is provided", () => { |
| 293 | + const original = "x".repeat(FLEET_DRY_REPORT_CHARS + 40); |
254 | 294 | const records = new Map<string, FleetDryMailboxRecord>([ |
255 | | - [ |
256 | | - "big", |
257 | | - { status: "done", report: "x".repeat(FLEET_DRY_REPORT_CHARS + 40) }, |
258 | | - ], |
| 295 | + ["big", { status: "done", report: original }], |
259 | 296 | ]); |
260 | | - const mailbox: FleetDryMailbox = { |
261 | | - ids: () => [...records.keys()], |
262 | | - peek: (id) => records.get(id), |
263 | | - take: (id) => records.get(id), |
264 | | - }; |
265 | | - const reports = collectUncollectedTerminals(mailbox, [], true); |
266 | | - expect(reports[0]?.report?.length).toBe(FLEET_DRY_REPORT_CHARS); |
267 | | - expect(reports[0]?.report?.endsWith("…")).toBe(true); |
| 297 | + const reports = collectUncollectedTerminals(peekMailbox(records), [], true); |
| 298 | + const clipped = reports[0]?.report ?? ""; |
| 299 | + expect(clipped.length).toBeLessThanOrEqual(FLEET_DRY_REPORT_CHARS); |
| 300 | + expect(clipped.length).toBeLessThan(original.length); |
| 301 | + expect(clipped).toContain("[output truncated"); |
| 302 | + expect(clipped).toContain("NOT retrievable"); |
| 303 | + expect(clipped).not.toContain("tool-output:///"); |
| 304 | + expect(clipped.endsWith("…") && !clipped.includes("truncated")).toBe(false); |
| 305 | + }); |
| 306 | + |
| 307 | + test("spills oversized reports to a tool-output URI when a writer is provided", async () => { |
| 308 | + const original = `head-${"x".repeat(FLEET_DRY_REPORT_CHARS)}TAIL-MARKER`; |
| 309 | + const records = new Map<string, FleetDryMailboxRecord>([ |
| 310 | + ["big", { status: "done", report: original }], |
| 311 | + ]); |
| 312 | + const store = fakeBlobStore(); |
| 313 | + const reports = collectUncollectedTerminals( |
| 314 | + peekMailbox(records), |
| 315 | + [], |
| 316 | + true, |
| 317 | + store.writeBlob, |
| 318 | + ); |
| 319 | + const clipped = reports[0]?.report ?? ""; |
| 320 | + expect(clipped.length).toBeLessThanOrEqual(FLEET_DRY_REPORT_CHARS); |
| 321 | + expect(clipped).not.toContain("TAIL-MARKER"); |
| 322 | + expect(clipped).toContain("[output truncated"); |
| 323 | + expect(clipped).not.toContain("NOT retrievable"); |
| 324 | + const key = fleetDrySpillKey("big", "report"); |
| 325 | + const uri = `tool-output:///${key}`; |
| 326 | + expect(clipped).toContain(uri); |
| 327 | + expect(clipped).toContain("read_file"); |
| 328 | + const entry = store.blobs.get(key); |
| 329 | + expect(entry).toBeDefined(); |
| 330 | + expect(new TextDecoder().decode(entry?.bytes ?? new Uint8Array())).toBe( |
| 331 | + original, |
| 332 | + ); |
| 333 | + |
| 334 | + const recovered = new TextDecoder().decode( |
| 335 | + await createBlobReader(store).read(uri), |
| 336 | + ); |
| 337 | + expect(recovered).toBe(original); |
| 338 | + |
| 339 | + const parsed = reportsJSONFromPrompt( |
| 340 | + buildFleetDryContinuationPrompt([openTask], reports), |
| 341 | + ); |
| 342 | + expect(parsed).toEqual(reports); |
| 343 | + }); |
| 344 | + |
| 345 | + test("leaves under-budget reports unchanged even when a writer is provided", () => { |
| 346 | + const report = "short enough"; |
| 347 | + const records = new Map<string, FleetDryMailboxRecord>([ |
| 348 | + ["w1", { status: "done", report }], |
| 349 | + ]); |
| 350 | + const store = fakeBlobStore(); |
| 351 | + const reports = collectUncollectedTerminals( |
| 352 | + peekMailbox(records), |
| 353 | + [], |
| 354 | + true, |
| 355 | + store.writeBlob, |
| 356 | + ); |
| 357 | + expect(reports[0]?.report).toBe(report); |
| 358 | + expect(store.blobs.size).toBe(0); |
| 359 | + }); |
| 360 | + |
| 361 | + test("spills oversized error fields under a distinct key", () => { |
| 362 | + const original = "e".repeat(FLEET_DRY_REPORT_CHARS + 20); |
| 363 | + const records = new Map<string, FleetDryMailboxRecord>([ |
| 364 | + ["boom", { status: "failed", error: original }], |
| 365 | + ]); |
| 366 | + const store = fakeBlobStore(); |
| 367 | + const reports = collectUncollectedTerminals( |
| 368 | + peekMailbox(records), |
| 369 | + [], |
| 370 | + true, |
| 371 | + store.writeBlob, |
| 372 | + ); |
| 373 | + const clipped = reports[0]?.error ?? ""; |
| 374 | + const key = fleetDrySpillKey("boom", "error"); |
| 375 | + expect(clipped).toContain(`tool-output:///${key}`); |
| 376 | + expect(store.blobs.has(fleetDrySpillKey("boom", "report"))).toBe(false); |
| 377 | + expect( |
| 378 | + new TextDecoder().decode(store.blobs.get(key)?.bytes ?? new Uint8Array()), |
| 379 | + ).toBe(original); |
268 | 380 | }); |
269 | 381 |
|
270 | 382 | test("consume false peeks without take", () => { |
@@ -329,6 +441,42 @@ describe("driveOpenTasksAfterFleetDry", () => { |
329 | 441 | expect(records.get("w1")?.collected).toBe(true); |
330 | 442 | }); |
331 | 443 |
|
| 444 | + test("dry+open continuation JSON includes a spill URI for oversized reports", () => { |
| 445 | + const original = `head-${"x".repeat(FLEET_DRY_REPORT_CHARS)}TAIL-MARKER`; |
| 446 | + const records = new Map<string, FleetDryMailboxRecord>([ |
| 447 | + ["big", { status: "done", report: original }], |
| 448 | + ]); |
| 449 | + const store = fakeBlobStore(); |
| 450 | + const sent: string[] = []; |
| 451 | + const driven = driveOpenTasksAfterFleetDry({ |
| 452 | + previousRunning: 1, |
| 453 | + running: 0, |
| 454 | + openTasks: [openTask], |
| 455 | + parentProcessing: false, |
| 456 | + mailbox: peekMailbox(records), |
| 457 | + lanes: [], |
| 458 | + writeBlob: store.writeBlob, |
| 459 | + beginSystemContinuation: (prompt) => { |
| 460 | + sent.push(prompt); |
| 461 | + }, |
| 462 | + send: () => undefined, |
| 463 | + }); |
| 464 | + expect(driven).toBe(true); |
| 465 | + const parsed = reportsJSONFromPrompt(sent[0] ?? ""); |
| 466 | + expect(Array.isArray(parsed)).toBe(true); |
| 467 | + const report = (parsed as { report?: string }[])[0]?.report ?? ""; |
| 468 | + expect(report).toContain( |
| 469 | + `tool-output:///${fleetDrySpillKey("big", "report")}`, |
| 470 | + ); |
| 471 | + expect(report).not.toContain("TAIL-MARKER"); |
| 472 | + expect( |
| 473 | + new TextDecoder().decode( |
| 474 | + store.blobs.get(fleetDrySpillKey("big", "report"))?.bytes ?? |
| 475 | + new Uint8Array(), |
| 476 | + ), |
| 477 | + ).toBe(original); |
| 478 | + }); |
| 479 | + |
332 | 480 | test("dry+terminal, live+open, and parentProcessing only skip", () => { |
333 | 481 | const noop = { |
334 | 482 | mailbox: undefined, |
|
0 commit comments