agent-yes 1.299.0 → 1.300.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +13 -2
  2. package/default.config.yaml +12 -0
  3. package/dist/{SUPPORTED_CLIS-BIlQQJa6.js → SUPPORTED_CLIS-BUn4zN_B.js} +3 -3
  4. package/dist/{SUPPORTED_CLIS-Ct9reIJH.js → SUPPORTED_CLIS-DWZDaH9f.js} +2 -2
  5. package/dist/{agentShare-uzFuaLUH.js → agentShare-DpJmCvJ2.js} +3 -3
  6. package/dist/{callback-DRrURxmb.js → callback-BW73zdcS.js} +3 -3
  7. package/dist/{callback-BxTZx3xJ.js → callback-DQrYImyf.js} +3 -3
  8. package/dist/cli.js +5 -5
  9. package/dist/{cmdPs-BGIWbeBM.js → cmdPs-DBt-LD-H.js} +3 -3
  10. package/dist/index.js +2 -2
  11. package/dist/{notifyDaemon-EEbnpoYp.js → notifyDaemon-BkuOBb_g.js} +3 -3
  12. package/dist/{parentPingLoop-C1XXpKK7.js → parentPingLoop-d0GZjqY9.js} +2 -2
  13. package/dist/{remotes-DrYe_nfw.js → remotes-B4v0Nowo.js} +64 -7
  14. package/dist/remotes-DnMQWaqQ.js +5 -0
  15. package/dist/{rustBinary-B7rq3nuz.js → rustBinary-CVjRaDmT.js} +2 -2
  16. package/dist/{rustBinary--pof-MTj.js → rustBinary-DdWIf051.js} +2 -2
  17. package/dist/{schedule-BFUe9-Mf.js → schedule-C0HWIN7Z.js} +4 -4
  18. package/dist/{serve-Q4dnZc5l.js → serve-DTQomE4j.js} +42 -28
  19. package/dist/{setup-aEuGSFjB.js → setup-BwZ3UlCM.js} +2 -2
  20. package/dist/{shareCmd-DkITELaM.js → shareCmd-Cx2kxthJ.js} +3 -3
  21. package/dist/subcommands-R1eJF51D.js +13 -0
  22. package/dist/{subcommands-CUWPMI_r.js → subcommands-sr1CIEeP.js} +766 -81
  23. package/dist/{terminal-CUn1_oPd.js → terminal-CHPcyJuv.js} +2 -2
  24. package/dist/{trayApp-BHwAJFzl.js → trayApp-CjUWDytD.js} +2 -2
  25. package/dist/trayApp-K6-U3syh.js +5 -0
  26. package/dist/{ts-oIxnyQbE.js → ts-tta7Z887.js} +3 -3
  27. package/dist/{versionChecker-DOgDaagD.js → versionChecker-qFzGUoUo.js} +2 -2
  28. package/dist/{widget-CuagzQwz.js → widget-BOUSiXVj.js} +3 -3
  29. package/dist/{ws-DcJBGzQP.js → ws-BV2o8Zce.js} +2 -2
  30. package/dist/{ws-ID7-zDiJ.js → ws-xxrH3pXD.js} +3 -3
  31. package/package.json +1 -1
  32. package/ts/composerGuard.spec.ts +279 -0
  33. package/ts/composerGuard.ts +366 -0
  34. package/ts/globalPidIndex.ts +7 -0
  35. package/ts/messageLog.ts +2 -0
  36. package/ts/needsInput.bun.spec.ts +36 -0
  37. package/ts/needsInput.spec.ts +36 -0
  38. package/ts/remotes.bun.spec.ts +125 -0
  39. package/ts/remotes.spec.ts +125 -0
  40. package/ts/remotes.ts +95 -5
  41. package/ts/serve.bun.spec.ts +50 -0
  42. package/ts/serve.spec.ts +57 -1
  43. package/ts/serve.ts +50 -9
  44. package/ts/subcommands.spec.ts +530 -50
  45. package/ts/subcommands.ts +657 -94
  46. package/ts/workDir.bun.spec.ts +90 -0
  47. package/ts/workDir.spec.ts +90 -0
  48. package/ts/workDir.ts +75 -0
  49. package/dist/remotes-BDMnvGI6.js +0 -5
  50. package/dist/subcommands-CuRZt3X6.js +0 -13
  51. package/dist/trayApp-ucuB_j5l.js +0 -5
@@ -200,14 +200,19 @@ describe("subcommands.isSubcommand", () => {
200
200
  expect(isSubcommand("send", false)).toBe(true);
201
201
  });
202
202
 
203
- it("gates `share` / `connect` on the manager entry — they are prompt words", async () => {
203
+ it("gates the prompt-word verbs so `cy read …` / `cy connect …` are prompt text", async () => {
204
204
  const { isSubcommand } = await loadModule();
205
- // `cy connect the frontend to the API` / `cy share this design` must reach
206
- // the agent; the commands live on `ay share` / `ay connect`.
207
- for (const sub of ["share", "connect"]) {
205
+ // `read` / `share` / `connect` are ordinary English verbs: `cy read ts/cli.ts
206
+ // and explain it` and `cy connect the frontend to the API` must reach claude.
207
+ // The commands live on the manager entry (`ay read`, `ay share`, `ay connect`).
208
+ for (const sub of ["read", "share", "connect"]) {
208
209
  expect(isSubcommand(sub, true)).toBe(true);
209
210
  expect(isSubcommand(sub, false)).toBe(false);
210
211
  }
212
+ // Their siblings are not prompt openers, so they stay universal.
213
+ expect(isSubcommand("cat", false)).toBe(true);
214
+ expect(isSubcommand("tail", false)).toBe(true);
215
+ expect(isSubcommand("head", false)).toBe(true);
211
216
  });
212
217
  });
213
218
 
@@ -305,13 +310,18 @@ describe("subcommands.cmdHelp", () => {
305
310
  expect(await capture()).toContain("ay notify watch --unread"); // Management entry
306
311
  });
307
312
 
308
- it("labels `ay share` / `ay connect` as ay-only for cli-bound aliases", async () => {
309
- // Both lines stay listed (sharing is how the console is reached at all) but
310
- // must not read as invokable through `cy`, where those words are prompt text.
313
+ it("labels the ay-only lines for cli-bound aliases", async () => {
314
+ // `read` / `share` / `connect` stay listed (pagination flags are worth
315
+ // knowing; `ay share` is how the console is reached at all) but must not read
316
+ // as invokable through `cy`, where those words are prompt text.
311
317
  const alias = await capture(false);
318
+ expect(alias).toContain("ay read <keyword>");
319
+ expect(alias).toContain("`cy read …` is a prompt");
312
320
  expect(alias).toContain("ay share");
313
321
  expect(alias).toContain("`cy share`/`cy connect` are prompts");
314
- expect(await capture(true)).not.toContain("are prompts");
322
+ const manager = await capture(true);
323
+ expect(manager).not.toContain("is a prompt");
324
+ expect(manager).not.toContain("are prompts");
315
325
  });
316
326
 
317
327
  it("stays plain for a human shell (no AGENT_YES_PID)", async () => {
@@ -531,6 +541,18 @@ describe("subcommands.matchKeyword", () => {
531
541
  expect(matchKeyword(baseRecord, "different-project")).toBe(false);
532
542
  });
533
543
 
544
+ it("matches the effective work dir too (lane spawned in one dir, working in another)", async () => {
545
+ const { matchKeyword } = await loadModule();
546
+ const r = {
547
+ ...baseRecord,
548
+ workdir: "/repo/alpha/tree/feat-x",
549
+ workdir_source: "observed" as const,
550
+ };
551
+ expect(matchKeyword(r, "feat-x")).toBe(true);
552
+ expect(matchKeyword(r, "agent-yes")).toBe(true); // spawn dir still matches
553
+ expect(matchKeyword(baseRecord, "feat-x")).toBe(false);
554
+ });
555
+
534
556
  it("matches by exact cli name", async () => {
535
557
  const { matchKeyword } = await loadModule();
536
558
  expect(matchKeyword(baseRecord, "claude")).toBe(true);
@@ -1098,8 +1120,8 @@ describe("subcommands.cmdSend writes bytes to FIFO", () => {
1098
1120
  "hello-fifo",
1099
1121
  "--force",
1100
1122
  ]);
1101
- expect(code).toBe(0);
1102
- expect(stdout.join("")).toMatch(/sent to pid/);
1123
+ expect(code).toBe(1); // Bytes written, but no destination log proves submission.
1124
+ expect(stdout.join("")).toMatch(/NOT SUBMITTED/);
1103
1125
  } finally {
1104
1126
  process.stdout.write = orig;
1105
1127
  if (savedAyPid !== undefined) process.env.AGENT_YES_PID = savedAyPid;
@@ -1409,10 +1431,15 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1409
1431
  // has actually landed — a genuine idle→busy transition, and one that
1410
1432
  // provably happens AFTER submitAndConfirm's pre-write snapshot.
1411
1433
  const reacted = onKeystroke().then((got) => {
1412
- if (got) appendFileSync(log, BUSY);
1434
+ if (got) appendFileSync(log, "some leftover unsent text\r\n" + BUSY + "❯ \r\n");
1413
1435
  return got;
1414
1436
  });
1415
- const { confirmed, screen } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1437
+ const { confirmed, screen } = await submitAndConfirm(
1438
+ rec({ log_file: log }),
1439
+ fifo,
1440
+ "\r",
1441
+ "some leftover unsent text",
1442
+ );
1416
1443
  expect(await reacted).toBe(true); // the fifo really carried the Enter
1417
1444
  expect(confirmed).toBe(true);
1418
1445
  expect(screen.join("\n")).toContain("esc to interrupt");
@@ -1436,7 +1463,86 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1436
1463
  await writeFile(log, BUSY);
1437
1464
  const { submitAndConfirm } = await loadModule();
1438
1465
  await withFifo(async (fifo) => {
1439
- const { confirmed } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1466
+ const { confirmed } = await submitAndConfirm(
1467
+ rec({ log_file: log }),
1468
+ fifo,
1469
+ "\r",
1470
+ "some leftover unsent text",
1471
+ );
1472
+ expect(confirmed).toBe(false);
1473
+ });
1474
+ } finally {
1475
+ await rm(dir, { recursive: true, force: true }).catch(() => null);
1476
+ }
1477
+ },
1478
+ 10_000,
1479
+ );
1480
+
1481
+ // Codex review (2ecc427): counting copies in a 40-row window cannot confirm a
1482
+ // repeated raw message: the old "continue" scrolls out as the new one lands,
1483
+ // and the count stays 1. Leaving the visible composer is the evidence.
1484
+ it.skipIf(!itUnix)(
1485
+ "confirms a repeated raw message whose earlier copy scrolls out of the window",
1486
+ async () => {
1487
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1488
+ try {
1489
+ const log = path.join(dir, "a.log");
1490
+ const RULE = "─".repeat(20);
1491
+ await writeFile(log, `❯ continue\r\nok\r\n${RULE}\r\n❯ continue\r\n${RULE}\r\n`);
1492
+ const { submitAndConfirm } = await loadModule();
1493
+ await withFifo(async (fifo, onKeystroke) => {
1494
+ const reacted = onKeystroke().then((got) => {
1495
+ if (got) {
1496
+ const output = Array.from({ length: 50 }, (_, i) => `output line ${i}`).join("\r\n");
1497
+ appendFileSync(
1498
+ log,
1499
+ `\x1b[2J\x1b[H❯ continue\r\n${output}\r\n${RULE}\r\n❯ \r\n${RULE}\r\n`,
1500
+ );
1501
+ }
1502
+ return got;
1503
+ });
1504
+ const { confirmed, submission } = await submitAndConfirm(
1505
+ rec({ log_file: log }),
1506
+ fifo,
1507
+ "\r",
1508
+ "continue",
1509
+ );
1510
+ expect(await reacted).toBe(true);
1511
+ expect(submission).toBe("submitted");
1512
+ expect(confirmed).toBe(true);
1513
+ });
1514
+ } finally {
1515
+ await rm(dir, { recursive: true, force: true }).catch(() => null);
1516
+ }
1517
+ },
1518
+ 10_000,
1519
+ );
1520
+
1521
+ it.skipIf(!itUnix)(
1522
+ "does not take a vanished composer (a menu took the screen) for delivery",
1523
+ async () => {
1524
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1525
+ try {
1526
+ const log = path.join(dir, "a.log");
1527
+ const RULE = "─".repeat(20);
1528
+ await writeFile(log, `${RULE}\r\n❯ continue\r\n${RULE}\r\n`);
1529
+ const { submitAndConfirm } = await loadModule();
1530
+ await withFifo(async (fifo, onKeystroke) => {
1531
+ const reacted = onKeystroke().then((got) => {
1532
+ if (got)
1533
+ appendFileSync(
1534
+ log,
1535
+ `\x1b[2J\x1b[HDo you trust this folder?\r\n 1. Yes\r\n 2. No\r\n`,
1536
+ );
1537
+ return got;
1538
+ });
1539
+ const { confirmed } = await submitAndConfirm(
1540
+ rec({ log_file: log }),
1541
+ fifo,
1542
+ "\r",
1543
+ "continue",
1544
+ );
1545
+ expect(await reacted).toBe(true);
1440
1546
  expect(confirmed).toBe(false);
1441
1547
  });
1442
1548
  } finally {
@@ -1446,25 +1552,296 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1446
1552
  10_000,
1447
1553
  );
1448
1554
 
1449
- it.skipIf(!itUnix)("confirms via log growth even without a recognized busy marker", async () => {
1450
- const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1555
+ it.skipIf(!itUnix)(
1556
+ "confirms a cleared composer after a response without a busy marker",
1557
+ async () => {
1558
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1559
+ try {
1560
+ const log = path.join(dir, "a.log");
1561
+ await writeFile(log, "❯ \r\n");
1562
+ const { submitAndConfirm } = await loadModule();
1563
+ await withFifo(async (fifo, onKeystroke) => {
1564
+ // The CLI starts responding once the Enter lands. Keyed off the actual
1565
+ // keystroke so the growth cannot be folded into `sizeBefore`.
1566
+ const reacted = onKeystroke().then((got) => {
1567
+ if (got)
1568
+ appendFileSync(
1569
+ log,
1570
+ "some leftover unsent text\r\nsome real response text appears here\r\n❯ \r\n",
1571
+ );
1572
+ return got;
1573
+ });
1574
+ const { confirmed } = await submitAndConfirm(
1575
+ rec({ log_file: log }),
1576
+ fifo,
1577
+ "\r",
1578
+ "some leftover unsent text",
1579
+ );
1580
+ expect(await reacted).toBe(true);
1581
+ expect(confirmed).toBe(true);
1582
+ });
1583
+ } finally {
1584
+ await rm(dir, { recursive: true, force: true }).catch(() => null);
1585
+ }
1586
+ },
1587
+ );
1588
+
1589
+ it.skipIf(!itUnix)(
1590
+ "rejects growing logs while the message remains in the composer",
1591
+ async () => {
1592
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-redraw-"));
1593
+ try {
1594
+ const log = path.join(dir, "a.log");
1595
+ await writeFile(log, IDLE);
1596
+ const { submitAndConfirm } = await loadModule();
1597
+ await withFifo(async (fifo, onKeystroke) => {
1598
+ const reaction = onKeystroke().then((got) => {
1599
+ if (got) appendFileSync(log, BUSY + IDLE);
1600
+ return got;
1601
+ });
1602
+ const result = await submitAndConfirm(
1603
+ rec({ log_file: log }),
1604
+ fifo,
1605
+ "\r",
1606
+ "some leftover unsent text",
1607
+ );
1608
+ expect(await reaction).toBe(true);
1609
+ expect(result.confirmed).toBe(false);
1610
+ expect(result.submission).toBe("not-submitted");
1611
+ });
1612
+ } finally {
1613
+ await rm(dir, { recursive: true, force: true });
1614
+ }
1615
+ },
1616
+ 10_000,
1617
+ );
1618
+
1619
+ it.skipIf(!itUnix)("distinguishes a queued receipt from submission", async () => {
1620
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-queued-"));
1451
1621
  try {
1452
1622
  const log = path.join(dir, "a.log");
1453
- await writeFile(log, "❯ \r\n");
1623
+ await writeFile(log, IDLE);
1454
1624
  const { submitAndConfirm } = await loadModule();
1455
1625
  await withFifo(async (fifo, onKeystroke) => {
1456
- // The CLI starts responding once the Enter lands. Keyed off the actual
1457
- // keystroke so the growth cannot be folded into `sizeBefore`.
1458
- const reacted = onKeystroke().then((got) => {
1459
- if (got) appendFileSync(log, "some real response text appears here\r\n");
1626
+ const reaction = onKeystroke().then((got) => {
1627
+ if (got) appendFileSync(log, "❯ Press up to edit queued messages\r\n");
1460
1628
  return got;
1461
1629
  });
1462
- const { confirmed } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1463
- expect(await reacted).toBe(true);
1464
- expect(confirmed).toBe(true);
1630
+ const result = await submitAndConfirm(
1631
+ rec({ log_file: log }),
1632
+ fifo,
1633
+ "\r",
1634
+ "some leftover unsent text",
1635
+ );
1636
+ expect(await reaction).toBe(true);
1637
+ expect(result.confirmed).toBe(false);
1638
+ expect(result.submission).toBe("queued");
1465
1639
  });
1466
1640
  } finally {
1467
- await rm(dir, { recursive: true, force: true }).catch(() => null);
1641
+ await rm(dir, { recursive: true, force: true });
1642
+ }
1643
+ });
1644
+
1645
+ it("requires our identity to move out of the composer into the transcript", async () => {
1646
+ const { submissionState, inspectSubmission } = await loadModule();
1647
+ const marker = "<ay-msg abc12345";
1648
+ expect(submissionState([`❯ ${marker}`, "────────"], marker)).toBe("not-submitted");
1649
+ expect(inspectSubmission(["❯", ` ${marker}`, "────────"], marker).retry).toBe(true);
1650
+ expect(submissionState([marker, "❯", "────────"], marker)).toBe("submitted");
1651
+ expect(
1652
+ submissionState([marker, "❯ Press up to edit queued messages", "────────"], marker),
1653
+ ).toBe("queued");
1654
+ expect(submissionState(["❯", "────────"], marker)).toBe("not-submitted");
1655
+ expect(submissionState([], marker)).toBe("not-submitted");
1656
+ expect(inspectSubmission(["❯ unrelated draft"], marker).retry).toBe(false);
1657
+ expect(inspectSubmission(["Codex", "› Ask Codex to do anything"], "x")).toMatchObject({
1658
+ submission: "not-submitted",
1659
+ retry: false,
1660
+ });
1661
+ });
1662
+
1663
+ // Codex review (2ecc427): the last prompt-looking row was taken for the prompt,
1664
+ // so a quoted `› line` inside our unsent message put its nonce in "transcript".
1665
+ it("does not take a quoted prompt glyph inside our unsent message for the prompt", async () => {
1666
+ const { inspectSubmission } = await loadModule();
1667
+ const marker = "<ay-msg abc12345";
1668
+ const screen = ["older history", "────────", `❯ ${marker}`, " › quoted line", "────────"];
1669
+ expect(inspectSubmission(screen, marker)).toMatchObject({
1670
+ transcriptMatches: 0,
1671
+ retry: true,
1672
+ inComposer: true,
1673
+ composerVisible: true,
1674
+ });
1675
+ });
1676
+
1677
+ it("recognizes captured Codex idle/working and Claude suggestion layouts only with our transcript identity", async () => {
1678
+ const { submissionState, inspectSubmission } = await loadModule();
1679
+ const marker = "<ay-msg abc12345";
1680
+ // Layouts from the reviewer's captured fleet screens, with synthetic message identity.
1681
+ const screens = [
1682
+ [
1683
+ "• Ran tests",
1684
+ "",
1685
+ "› Ask Codex to do anything",
1686
+ " GPT-6-Astra low · ~/ws/x · Read brief",
1687
+ " ? for shortcuts ⚠ 2 warnings · f2 to view",
1688
+ ],
1689
+ [
1690
+ "• Working (10m 54s • esc to interrupt)",
1691
+ " └ Tip: Start a fresh idea with /new",
1692
+ "› Ask Codex to do anything",
1693
+ " GPT-6-Astra low · ~/ws/x · Read brief",
1694
+ " ? for shortcuts",
1695
+ ],
1696
+ [
1697
+ "✻ Churned for 15s",
1698
+ "────────────",
1699
+ "❯ push it and open the PR",
1700
+ "────────────",
1701
+ " ⏵⏵ auto mode on",
1702
+ ],
1703
+ ];
1704
+ for (const screen of screens) {
1705
+ expect(submissionState([marker, ...screen], marker)).toBe("submitted");
1706
+ expect(submissionState(screen, marker)).toBe("not-submitted");
1707
+ expect(inspectSubmission(screen, marker).retry).toBe(false);
1708
+ }
1709
+ });
1710
+
1711
+ it.skipIf(!itUnix)(
1712
+ "sends zero extra Enters for unrelated composer text, placeholders and dialogs",
1713
+ async () => {
1714
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-no-retry-"));
1715
+ try {
1716
+ const log = path.join(dir, "a.log");
1717
+ const { submitAndConfirm } = await loadModule();
1718
+ for (const screen of [
1719
+ "❯ push it and open the PR",
1720
+ "› Ask Codex to do anything",
1721
+ "Allow this command?\r\n❯ 1. Yes",
1722
+ ]) {
1723
+ await writeFile(log, screen + "\r\n");
1724
+ await withFifo(async (fifo) => {
1725
+ const result = await submitAndConfirm(
1726
+ rec({ log_file: log }),
1727
+ fifo,
1728
+ "\r",
1729
+ "<ay-msg abc12345",
1730
+ );
1731
+ expect(result.confirmed).toBe(false);
1732
+ const fs = await import("fs");
1733
+ const fd = fs.openSync(fifo, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
1734
+ try {
1735
+ const bytes = Buffer.alloc(16);
1736
+ const count = fs.readSync(fd, bytes, 0, bytes.length, null);
1737
+ expect(bytes.subarray(0, count).toString()).toBe("\r");
1738
+ } finally {
1739
+ fs.closeSync(fd);
1740
+ }
1741
+ });
1742
+ }
1743
+ } finally {
1744
+ await rm(dir, { recursive: true, force: true });
1745
+ }
1746
+ },
1747
+ );
1748
+
1749
+ it.skipIf(!itUnix)(
1750
+ "retries a swallowed Enter only for our composer and confirms its transcript",
1751
+ async () => {
1752
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-own-retry-"));
1753
+ try {
1754
+ const log = path.join(dir, "a.log");
1755
+ const marker = "<ay-msg abc12345";
1756
+ await writeFile(log, `❯ ${marker} hello\r\n`);
1757
+ const { submitAndConfirm } = await loadModule();
1758
+ await withFifo(async (fifo, onKeystroke) => {
1759
+ const reaction = (async () => {
1760
+ expect(await onKeystroke()).toBe(true); // first Enter is swallowed
1761
+ expect(await onKeystroke()).toBe(true); // only our message gets retried
1762
+ appendFileSync(
1763
+ log,
1764
+ `\r\n${marker} hello\r\n❯ push it and open the PR\r\n────────────\r\n`,
1765
+ );
1766
+ })();
1767
+ const result = await submitAndConfirm(rec({ log_file: log }), fifo, "\r", marker);
1768
+ await reaction;
1769
+ expect(result.confirmed).toBe(true);
1770
+ expect(result.submission).toBe("submitted");
1771
+ });
1772
+ } finally {
1773
+ await rm(dir, { recursive: true, force: true });
1774
+ }
1775
+ },
1776
+ 10_000,
1777
+ );
1778
+
1779
+ it("owns only a newly appeared Claude collapsed paste, never an existing or unreadable baseline", async () => {
1780
+ const { ownedCollapsedPaste } = await loadModule();
1781
+ const token = "[Pasted text #2 +12 lines]";
1782
+ const after = [`❯ ${token}`];
1783
+ expect(ownedCollapsedPaste("claude", ["❯"], after)).toBe(token);
1784
+ expect(ownedCollapsedPaste("claude", after, after)).toBeNull();
1785
+ expect(ownedCollapsedPaste("claude", [token, "❯"], after)).toBeNull();
1786
+ expect(ownedCollapsedPaste("claude", null, after)).toBeNull();
1787
+ expect(ownedCollapsedPaste("codex", ["❯"], after)).toBeNull();
1788
+ expect(ownedCollapsedPaste("claude", ["❯"], ["❯ unrelated draft"])).toBeNull();
1789
+ });
1790
+
1791
+ it.skipIf(!itUnix)(
1792
+ "retries our newly collapsed paste until its nonce appears in the transcript",
1793
+ async () => {
1794
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-collapsed-"));
1795
+ try {
1796
+ const log = path.join(dir, "a.log");
1797
+ const marker = "<ay-msg abc12345";
1798
+ await writeFile(log, "❯ [Pasted text #2 +12 lines]\r\n");
1799
+ const { submitAndConfirm } = await loadModule();
1800
+ await withFifo(async (fifo, onKeystroke) => {
1801
+ const reaction = (async () => {
1802
+ expect(await onKeystroke()).toBe(true);
1803
+ expect(await onKeystroke()).toBe(true);
1804
+ appendFileSync(log, `\r\n${marker} hello\r\n❯\r\n────────────\r\n`);
1805
+ })();
1806
+ const result = await submitAndConfirm(rec({ log_file: log }), fifo, "\r", marker, ["❯"]);
1807
+ await reaction;
1808
+ expect(result.confirmed).toBe(true);
1809
+ });
1810
+ } finally {
1811
+ await rm(dir, { recursive: true, force: true });
1812
+ }
1813
+ },
1814
+ 10_000,
1815
+ );
1816
+
1817
+ it.skipIf(!itUnix)("does not retry a collapsed paste that existed before our write", async () => {
1818
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-existing-paste-"));
1819
+ try {
1820
+ const log = path.join(dir, "a.log");
1821
+ const screen = ["❯ [Pasted text #2 +12 lines]"];
1822
+ await writeFile(log, screen.join("\r\n"));
1823
+ const { submitAndConfirm } = await loadModule();
1824
+ await withFifo(async (fifo) => {
1825
+ const result = await submitAndConfirm(
1826
+ rec({ log_file: log }),
1827
+ fifo,
1828
+ "\r",
1829
+ "<ay-msg abc12345",
1830
+ screen,
1831
+ );
1832
+ expect(result.confirmed).toBe(false);
1833
+ const fs = await import("fs");
1834
+ const fd = fs.openSync(fifo, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
1835
+ try {
1836
+ const bytes = Buffer.alloc(16);
1837
+ const count = fs.readSync(fd, bytes, 0, bytes.length, null);
1838
+ expect(bytes.subarray(0, count).toString()).toBe("\r");
1839
+ } finally {
1840
+ fs.closeSync(fd);
1841
+ }
1842
+ });
1843
+ } finally {
1844
+ await rm(dir, { recursive: true, force: true });
1468
1845
  }
1469
1846
  });
1470
1847
 
@@ -1477,7 +1854,12 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1477
1854
  await writeFile(log, IDLE); // never touched again — nothing to confirm
1478
1855
  const { submitAndConfirm } = await loadModule();
1479
1856
  await withFifo(async (fifo) => {
1480
- const { confirmed, screen } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1857
+ const { confirmed, screen } = await submitAndConfirm(
1858
+ rec({ log_file: log }),
1859
+ fifo,
1860
+ "\r",
1861
+ "some leftover unsent text",
1862
+ );
1481
1863
  expect(confirmed).toBe(false);
1482
1864
  expect(screen.join("\n")).toContain("leftover unsent text");
1483
1865
  });
@@ -1495,7 +1877,10 @@ describe("subcommands.cmdSend end-to-end submit-confirm wiring", () => {
1495
1877
 
1496
1878
  // A background drain so cmdSend's writes to the FIFO never block, mirroring
1497
1879
  // the "delivers a message to a real FIFO" setup above.
1498
- async function withDrainedFifo(fn: (fifo: string) => Promise<void>) {
1880
+ async function withDrainedFifo(
1881
+ fn: (fifo: string) => Promise<void>,
1882
+ onBytes?: (bytes: string) => void,
1883
+ ) {
1499
1884
  const { spawnSync } = await import("child_process");
1500
1885
  const dir = await mkdtemp(path.join(tmpdir(), "ay-send-e2e-"));
1501
1886
  const fifo = path.join(dir, "test.fifo");
@@ -1513,7 +1898,9 @@ describe("subcommands.cmdSend end-to-end submit-confirm wiring", () => {
1513
1898
  const rdwrFd = fs.openSync(fifo, fs.constants.O_RDWR | fs.constants.O_NONBLOCK);
1514
1899
  const drain = setInterval(() => {
1515
1900
  try {
1516
- fs.readSync(rdwrFd, Buffer.alloc(4096), 0, 4096, null);
1901
+ const bytes = Buffer.alloc(4096);
1902
+ const count = fs.readSync(rdwrFd, bytes, 0, bytes.length, null);
1903
+ if (count) onBytes?.(bytes.subarray(0, count).toString());
1517
1904
  } catch {
1518
1905
  /* EAGAIN or similar — nothing pending, ignore */
1519
1906
  }
@@ -1577,34 +1964,90 @@ describe("subcommands.cmdSend end-to-end submit-confirm wiring", () => {
1577
1964
  "exits 0 and reports 'sent' when a working marker appears after the Enter (genuine confirm)",
1578
1965
  async () => {
1579
1966
  const dir = await mkdtemp(path.join(tmpdir(), "ay-send-e2e-log-"));
1580
- // A STATIC pre-existing busy marker cannot confirm — that's the #157
1581
- // wasAlreadyWorking guard (see the "already on screen before sending" test).
1582
- // Confirmation needs a real idle→working transition (or >=8 bytes of growth)
1583
- // AFTER the submit. Model that: the log is idle until we send, then a steady
1584
- // trickle of the working marker lands within submitAndConfirm's window, so it
1585
- // confirms deterministically without fragile single-shot timing.
1586
1967
  const log = path.join(dir, "a.log");
1587
- await writeFile(log, "❯ \r\n"); // idle until the submitted Enter lands
1588
- let working = false;
1589
- const respond = setInterval(() => {
1590
- if (working) appendFileSync(log, BUSY);
1591
- }, 40);
1592
- respond.unref();
1968
+ await writeFile(log, "❯ hello\r\n");
1593
1969
  try {
1594
- await withDrainedFifo(async (fifo) => {
1595
- working = true; // the submitted Enter kicks the agent into working
1596
- const { code, stdout } = await send(fifo, log, "hello");
1597
- expect(code).toBe(0);
1598
- expect(stdout).toMatch(/^sent to pid/);
1599
- expect(stdout).not.toMatch(/NOT confirmed/);
1600
- });
1970
+ await withDrainedFifo(
1971
+ async (fifo) => {
1972
+ const { code, stdout } = await send(fifo, log, "hello");
1973
+ expect(code).toBe(0);
1974
+ expect(stdout).toMatch(/^sent to pid/);
1975
+ expect(stdout).not.toMatch(/NOT confirmed/);
1976
+ },
1977
+ (bytes) => {
1978
+ if (bytes.includes("\r")) appendFileSync(log, "\r\nhello\r\n" + BUSY + "❯ \r\n");
1979
+ },
1980
+ );
1601
1981
  } finally {
1602
- clearInterval(respond);
1603
1982
  await rm(dir, { recursive: true, force: true }).catch(() => null);
1604
1983
  }
1605
1984
  },
1606
1985
  );
1607
1986
 
1987
+ it.skipIf(!itUnix)("captures collapsed-paste ownership before writing the body", async () => {
1988
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-collapse-wiring-"));
1989
+ const log = path.join(dir, "a.log");
1990
+ await writeFile(log, "❯\r\n");
1991
+ let enters = 0;
1992
+ try {
1993
+ await withDrainedFifo(
1994
+ async (fifo) => {
1995
+ const { code } = await send(fifo, log, "collapsed-message");
1996
+ expect(code).toBe(0);
1997
+ expect(enters).toBe(2);
1998
+ },
1999
+ (bytes) => {
2000
+ if (bytes.includes("collapsed-message"))
2001
+ appendFileSync(log, "\x1b[2J\x1b[H❯ [Pasted text #3 +20 lines]\r\n");
2002
+ if (bytes.includes("\r") && ++enters === 2)
2003
+ appendFileSync(log, "collapsed-message\r\n❯\r\n");
2004
+ },
2005
+ );
2006
+ } finally {
2007
+ await rm(dir, { recursive: true, force: true });
2008
+ }
2009
+ });
2010
+
2011
+ it.skipIf(!itUnix)("reports QUEUED with its distinct exit 4", async () => {
2012
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-queue-e2e-"));
2013
+ const log = path.join(dir, "a.log");
2014
+ await writeFile(log, "❯ waiting\r\n");
2015
+ try {
2016
+ await withDrainedFifo(
2017
+ async (fifo) => {
2018
+ const errs: string[] = [];
2019
+ const origErr = process.stderr.write.bind(process.stderr);
2020
+ process.stderr.write = ((c: string | Uint8Array) => (
2021
+ errs.push(String(c)), true
2022
+ )) as typeof process.stderr.write;
2023
+ let res: { code: number | null; stdout: string };
2024
+ try {
2025
+ res = await send(fifo, log, "queued-receipt-test");
2026
+ } finally {
2027
+ process.stderr.write = origErr;
2028
+ }
2029
+ const { code, stdout } = res;
2030
+ expect(code).toBe(4);
2031
+ expect(stdout).toMatch(/^QUEUED to pid/);
2032
+ // The receipt says so, so a lane does not read rc=4 as a failure and resend.
2033
+ expect(errs.join("")).toContain("Queued, do not resend.");
2034
+ const { readMailbox } = await import("./messageLog.ts");
2035
+ const receipt = (await readMailbox(process.cwd(), "outbox")).findLast(
2036
+ (r) => r.body === "queued-receipt-test",
2037
+ );
2038
+ expect(receipt?.confirmed).toBe(false);
2039
+ expect(receipt?.submission).toBe("queued");
2040
+ },
2041
+ (bytes) => {
2042
+ if (bytes.includes("\r"))
2043
+ appendFileSync(log, "queued-receipt-test\r\n❯ Press up to edit queued messages\r\n");
2044
+ },
2045
+ );
2046
+ } finally {
2047
+ await rm(dir, { recursive: true, force: true });
2048
+ }
2049
+ });
2050
+
1608
2051
  it.skipIf(!itUnix)(
1609
2052
  "exits non-zero and reports the leftover screen when submission can't be confirmed",
1610
2053
  async () => {
@@ -3236,7 +3679,7 @@ describe("subcommands.cmdSend double-envelope warning", () => {
3236
3679
  '<ay-msg deadbeef from claude #900001 @ /repo/beta — reply: ay send 900001 "...">hi</ay-msg deadbeef>',
3237
3680
  "--force",
3238
3681
  ]);
3239
- expect(code).toBe(0);
3682
+ expect(code).toBe(1); // This FIFO fixture has no destination log.
3240
3683
  expect(stderr.join("")).toMatch(/DOUBLE wrapper/);
3241
3684
  } finally {
3242
3685
  process.stderr.write = origErr;
@@ -3319,7 +3762,7 @@ describe("subcommands.cmdSend double-envelope warning", () => {
3319
3762
  "plain body, quoting <ay-msg deadbeef …> mid-text is fine",
3320
3763
  "--force",
3321
3764
  ]);
3322
- expect(code).toBe(0);
3765
+ expect(code).toBe(1); // This FIFO fixture has no destination log.
3323
3766
  expect(stderr.join("")).not.toMatch(/DOUBLE wrapper/);
3324
3767
  } finally {
3325
3768
  process.stderr.write = origErr;
@@ -3393,7 +3836,7 @@ describe("subcommands.cmdSend double-envelope warning", () => {
3393
3836
  "hello from crm",
3394
3837
  "--force",
3395
3838
  ]);
3396
- expect(code).toBe(0);
3839
+ expect(code).toBe(1); // This FIFO fixture has no destination log.
3397
3840
  } finally {
3398
3841
  process.stdout.write = origOut;
3399
3842
  if (savedAyPid === undefined) delete process.env.AGENT_YES_PID;
@@ -4172,3 +4615,40 @@ describe("subcommands.cmdSend unreachable exit status", () => {
4172
4615
  },
4173
4616
  );
4174
4617
  });
4618
+
4619
+ // The drainer is spawned detached from `ay send`; a launcher that cannot start
4620
+ // is silent (spawn's ENOENT is swallowed), so the parked message simply never
4621
+ // moves. Pin the argv shape per runtime so the Windows regression (spawning the
4622
+ // .js script bare) cannot come back.
4623
+ describe("drainerLauncher", () => {
4624
+ it("re-runs the current script under bun, on every platform", async () => {
4625
+ const { drainerLauncher } = await loadModule();
4626
+ expect(
4627
+ drainerLauncher("/usr/local/bin/bun", "/repo/alpha/dist/cli.js", "/usr/local/bin/ay"),
4628
+ ).toEqual(["/usr/local/bin/bun", "/repo/alpha/dist/cli.js"]);
4629
+ expect(
4630
+ drainerLauncher(
4631
+ "C:\\Users\\alice\\.bun\\bin\\bun.exe",
4632
+ "C:\\repo\\alpha\\dist\\cli.js",
4633
+ null,
4634
+ ),
4635
+ ).toEqual(["C:\\Users\\alice\\.bun\\bin\\bun.exe", "C:\\repo\\alpha\\dist\\cli.js"]);
4636
+ });
4637
+
4638
+ it("falls back to the ay on PATH only when there is no script, and to nothing when there is neither", async () => {
4639
+ const { drainerLauncher } = await loadModule();
4640
+ expect(drainerLauncher("/usr/local/bin/bun", undefined, "/usr/local/bin/ay")).toEqual([
4641
+ "/usr/local/bin/bun",
4642
+ "/usr/local/bin/ay",
4643
+ ]);
4644
+ expect(drainerLauncher("/usr/local/bin/bun", undefined, null)).toBeNull();
4645
+ });
4646
+
4647
+ it("re-runs a compiled single-file binary as itself", async () => {
4648
+ const { drainerLauncher } = await loadModule();
4649
+ expect(drainerLauncher("/opt/ay/ay", "/$bunfs/root/cli", "/usr/local/bin/ay")).toEqual([
4650
+ "/opt/ay/ay",
4651
+ ]);
4652
+ expect(drainerLauncher("C:\\tools\\ay.exe", undefined, null)).toEqual(["C:\\tools\\ay.exe"]);
4653
+ });
4654
+ });