agent-yes 1.299.1 → 1.300.2

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 +9 -0
  2. package/default.config.yaml +12 -0
  3. package/dist/{SUPPORTED_CLIS-MftynU62.js → SUPPORTED_CLIS-BAeTuI-p.js} +3 -3
  4. package/dist/{SUPPORTED_CLIS-YZvIGQvw.js → SUPPORTED_CLIS-BZlaxyEH.js} +2 -2
  5. package/dist/{agentShare-dKQQSOD7.js → agentShare-DGQM24wU.js} +3 -3
  6. package/dist/{callback-BEbbWLKe.js → callback-3RsdcuV0.js} +3 -3
  7. package/dist/{callback-DWxTSlly.js → callback-CQadU_zP.js} +3 -3
  8. package/dist/cli.js +5 -5
  9. package/dist/{cmdPs-D7HWttUU.js → cmdPs-CQxFAs9M.js} +3 -3
  10. package/dist/index.js +2 -2
  11. package/dist/{notifyDaemon-B0aU7koS.js → notifyDaemon-CxkiJvOo.js} +3 -3
  12. package/dist/{parentPingLoop-DSiOXQeU.js → parentPingLoop-vZ8I9k5K.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-CKuufNRn.js → rustBinary-B1buVId2.js} +2 -2
  16. package/dist/{rustBinary-DgRg5H6V.js → rustBinary-etb0Jsga.js} +2 -2
  17. package/dist/{schedule-BStFx0_s.js → schedule-CMBHLSlz.js} +4 -4
  18. package/dist/{serve-Cis4oJM-.js → serve-D5ijYGTv.js} +42 -28
  19. package/dist/{setup-B0sg4AV1.js → setup-DMJYufvD.js} +2 -2
  20. package/dist/{shareCmd-DKIvwe0I.js → shareCmd-CNhCKuOK.js} +3 -3
  21. package/dist/{subcommands-CvL_1Nt9.js → subcommands-BncoxUN6.js} +775 -78
  22. package/dist/subcommands-ZEjbTV_7.js +13 -0
  23. package/dist/{terminal-DqsAuqhw.js → terminal-DzbPI4n0.js} +2 -2
  24. package/dist/{trayApp-Dayj9Frx.js → trayApp-BvtgbxMB.js} +2 -2
  25. package/dist/trayApp-CpPsP3_a.js +5 -0
  26. package/dist/{ts-C2osm4MO.js → ts-Gr1eJOUk.js} +3 -3
  27. package/dist/{versionChecker-BmXwOe40.js → versionChecker-BrjeGNf1.js} +2 -2
  28. package/dist/{widget-D5tsB-oi.js → widget-p4Db_OXd.js} +3 -3
  29. package/dist/{ws-B4jdOeB2.js → ws-D_QClddd.js} +3 -3
  30. package/dist/{ws-mz7VJJwO.js → ws-DnKPi3TS.js} +2 -2
  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 +564 -42
  45. package/ts/subcommands.ts +650 -79
  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-DioyG80q.js +0 -13
  51. package/dist/trayApp-C6C7-TSh.js +0 -5
@@ -541,6 +541,18 @@ describe("subcommands.matchKeyword", () => {
541
541
  expect(matchKeyword(baseRecord, "different-project")).toBe(false);
542
542
  });
543
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
+
544
556
  it("matches by exact cli name", async () => {
545
557
  const { matchKeyword } = await loadModule();
546
558
  expect(matchKeyword(baseRecord, "claude")).toBe(true);
@@ -585,6 +597,23 @@ describe("subcommands.matchKeyword", () => {
585
597
  expect(matchKeyword(r, "b2c3")).toBe(false); // not a prefix (mid-string)
586
598
  expect(matchKeyword({ ...baseRecord, agent_id: null }, "a1b2")).toBe(false);
587
599
  });
600
+
601
+ // A vanished agent's id still sits in its sublanes' prompts ("reply: ay send
602
+ // <id>"); an id-shaped keyword must never fall through to that prompt match.
603
+ it("treats an id-shaped keyword as an identity selector (no cwd/prompt match)", async () => {
604
+ const { matchKeyword } = await loadModule();
605
+ const quoting = {
606
+ ...baseRecord,
607
+ agent_id: "0123456789ab",
608
+ prompt: "you were spawned by d3e4f5a6b7c8 — reply: ay send d3e4f5a6b7c8",
609
+ cwd: "/repo/alpha/tree/d3e4f5a6b7c8",
610
+ };
611
+ expect(matchKeyword(quoting, "d3e4f5a6b7c8")).toBe(false);
612
+ expect(matchKeyword(quoting, "0123456789ab")).toBe(true); // its own id
613
+ expect(matchKeyword(quoting, "0123456789AB")).toBe(true);
614
+ // a short hex word is still an ordinary keyword
615
+ expect(matchKeyword(quoting, "d3e4f5")).toBe(true);
616
+ });
588
617
  });
589
618
 
590
619
  describe("subcommands.resolveOne exact-identity precedence", () => {
@@ -664,6 +693,41 @@ describe("subcommands.resolveOne exact-identity precedence", () => {
664
693
  const record = await resolveOne("a1b2c3d4e5f6", opts);
665
694
  expect(record.pid).toBe(111);
666
695
  });
696
+ // 2026-10-09: a parent's wrapper restarted under a new id; sends to the old
697
+ // id keyword-matched every sublane whose prompt quoted it. With exactly one
698
+ // such lane left, the send would have gone to that lane.
699
+ it("refuses a stale id instead of resolving it to a lane that quotes it", async () => {
700
+ const { resolveOne } = await loadModule();
701
+ const { appendGlobalPid } = await import("./globalPidIndex.ts");
702
+ const base = {
703
+ cwd: process.cwd(),
704
+ log_file: null,
705
+ exit_code: null,
706
+ exit_reason: null,
707
+ started_at: Date.now() - 1_000,
708
+ };
709
+ await appendGlobalPid({
710
+ ...base,
711
+ pid: 3333,
712
+ cli: "claude",
713
+ status: "active" as const,
714
+ agent_id: "0123456789ab",
715
+ prompt: "<ay-msg from parent — reply: ay send d3e4f5a6b7c8>",
716
+ });
717
+ await expect(resolveOne("d3e4f5a6b7c8", opts)).rejects.toThrow(/no live agent has id/);
718
+ // the exited parent is named when its record is still in the index
719
+ await appendGlobalPid({
720
+ ...base,
721
+ pid: 4444,
722
+ cli: "claude",
723
+ status: "exited" as const,
724
+ agent_id: "d3e4f5a6b7c8",
725
+ prompt: "cto",
726
+ });
727
+ await expect(resolveOne("d3e4f5a6b7c8", opts)).rejects.toThrow(/pid 4444 exited/);
728
+ // the quoting lane is still reachable by its own id
729
+ expect((await resolveOne("0123456789ab", opts)).pid).toBe(3333);
730
+ });
667
731
  });
668
732
 
669
733
  describe("subcommands.runSubcommand routing", () => {
@@ -1108,8 +1172,8 @@ describe("subcommands.cmdSend writes bytes to FIFO", () => {
1108
1172
  "hello-fifo",
1109
1173
  "--force",
1110
1174
  ]);
1111
- expect(code).toBe(0);
1112
- expect(stdout.join("")).toMatch(/sent to pid/);
1175
+ expect(code).toBe(1); // Bytes written, but no destination log proves submission.
1176
+ expect(stdout.join("")).toMatch(/NOT SUBMITTED/);
1113
1177
  } finally {
1114
1178
  process.stdout.write = orig;
1115
1179
  if (savedAyPid !== undefined) process.env.AGENT_YES_PID = savedAyPid;
@@ -1419,10 +1483,15 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1419
1483
  // has actually landed — a genuine idle→busy transition, and one that
1420
1484
  // provably happens AFTER submitAndConfirm's pre-write snapshot.
1421
1485
  const reacted = onKeystroke().then((got) => {
1422
- if (got) appendFileSync(log, BUSY);
1486
+ if (got) appendFileSync(log, "some leftover unsent text\r\n" + BUSY + "❯ \r\n");
1423
1487
  return got;
1424
1488
  });
1425
- const { confirmed, screen } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1489
+ const { confirmed, screen } = await submitAndConfirm(
1490
+ rec({ log_file: log }),
1491
+ fifo,
1492
+ "\r",
1493
+ "some leftover unsent text",
1494
+ );
1426
1495
  expect(await reacted).toBe(true); // the fifo really carried the Enter
1427
1496
  expect(confirmed).toBe(true);
1428
1497
  expect(screen.join("\n")).toContain("esc to interrupt");
@@ -1446,7 +1515,86 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1446
1515
  await writeFile(log, BUSY);
1447
1516
  const { submitAndConfirm } = await loadModule();
1448
1517
  await withFifo(async (fifo) => {
1449
- const { confirmed } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1518
+ const { confirmed } = await submitAndConfirm(
1519
+ rec({ log_file: log }),
1520
+ fifo,
1521
+ "\r",
1522
+ "some leftover unsent text",
1523
+ );
1524
+ expect(confirmed).toBe(false);
1525
+ });
1526
+ } finally {
1527
+ await rm(dir, { recursive: true, force: true }).catch(() => null);
1528
+ }
1529
+ },
1530
+ 10_000,
1531
+ );
1532
+
1533
+ // Codex review (2ecc427): counting copies in a 40-row window cannot confirm a
1534
+ // repeated raw message: the old "continue" scrolls out as the new one lands,
1535
+ // and the count stays 1. Leaving the visible composer is the evidence.
1536
+ it.skipIf(!itUnix)(
1537
+ "confirms a repeated raw message whose earlier copy scrolls out of the window",
1538
+ async () => {
1539
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1540
+ try {
1541
+ const log = path.join(dir, "a.log");
1542
+ const RULE = "─".repeat(20);
1543
+ await writeFile(log, `❯ continue\r\nok\r\n${RULE}\r\n❯ continue\r\n${RULE}\r\n`);
1544
+ const { submitAndConfirm } = await loadModule();
1545
+ await withFifo(async (fifo, onKeystroke) => {
1546
+ const reacted = onKeystroke().then((got) => {
1547
+ if (got) {
1548
+ const output = Array.from({ length: 50 }, (_, i) => `output line ${i}`).join("\r\n");
1549
+ appendFileSync(
1550
+ log,
1551
+ `\x1b[2J\x1b[H❯ continue\r\n${output}\r\n${RULE}\r\n❯ \r\n${RULE}\r\n`,
1552
+ );
1553
+ }
1554
+ return got;
1555
+ });
1556
+ const { confirmed, submission } = await submitAndConfirm(
1557
+ rec({ log_file: log }),
1558
+ fifo,
1559
+ "\r",
1560
+ "continue",
1561
+ );
1562
+ expect(await reacted).toBe(true);
1563
+ expect(submission).toBe("submitted");
1564
+ expect(confirmed).toBe(true);
1565
+ });
1566
+ } finally {
1567
+ await rm(dir, { recursive: true, force: true }).catch(() => null);
1568
+ }
1569
+ },
1570
+ 10_000,
1571
+ );
1572
+
1573
+ it.skipIf(!itUnix)(
1574
+ "does not take a vanished composer (a menu took the screen) for delivery",
1575
+ async () => {
1576
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1577
+ try {
1578
+ const log = path.join(dir, "a.log");
1579
+ const RULE = "─".repeat(20);
1580
+ await writeFile(log, `${RULE}\r\n❯ continue\r\n${RULE}\r\n`);
1581
+ const { submitAndConfirm } = await loadModule();
1582
+ await withFifo(async (fifo, onKeystroke) => {
1583
+ const reacted = onKeystroke().then((got) => {
1584
+ if (got)
1585
+ appendFileSync(
1586
+ log,
1587
+ `\x1b[2J\x1b[HDo you trust this folder?\r\n 1. Yes\r\n 2. No\r\n`,
1588
+ );
1589
+ return got;
1590
+ });
1591
+ const { confirmed } = await submitAndConfirm(
1592
+ rec({ log_file: log }),
1593
+ fifo,
1594
+ "\r",
1595
+ "continue",
1596
+ );
1597
+ expect(await reacted).toBe(true);
1450
1598
  expect(confirmed).toBe(false);
1451
1599
  });
1452
1600
  } finally {
@@ -1456,25 +1604,296 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1456
1604
  10_000,
1457
1605
  );
1458
1606
 
1459
- it.skipIf(!itUnix)("confirms via log growth even without a recognized busy marker", async () => {
1460
- const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1607
+ it.skipIf(!itUnix)(
1608
+ "confirms a cleared composer after a response without a busy marker",
1609
+ async () => {
1610
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-confirm-log-"));
1611
+ try {
1612
+ const log = path.join(dir, "a.log");
1613
+ await writeFile(log, "❯ \r\n");
1614
+ const { submitAndConfirm } = await loadModule();
1615
+ await withFifo(async (fifo, onKeystroke) => {
1616
+ // The CLI starts responding once the Enter lands. Keyed off the actual
1617
+ // keystroke so the growth cannot be folded into `sizeBefore`.
1618
+ const reacted = onKeystroke().then((got) => {
1619
+ if (got)
1620
+ appendFileSync(
1621
+ log,
1622
+ "some leftover unsent text\r\nsome real response text appears here\r\n❯ \r\n",
1623
+ );
1624
+ return got;
1625
+ });
1626
+ const { confirmed } = await submitAndConfirm(
1627
+ rec({ log_file: log }),
1628
+ fifo,
1629
+ "\r",
1630
+ "some leftover unsent text",
1631
+ );
1632
+ expect(await reacted).toBe(true);
1633
+ expect(confirmed).toBe(true);
1634
+ });
1635
+ } finally {
1636
+ await rm(dir, { recursive: true, force: true }).catch(() => null);
1637
+ }
1638
+ },
1639
+ );
1640
+
1641
+ it.skipIf(!itUnix)(
1642
+ "rejects growing logs while the message remains in the composer",
1643
+ async () => {
1644
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-redraw-"));
1645
+ try {
1646
+ const log = path.join(dir, "a.log");
1647
+ await writeFile(log, IDLE);
1648
+ const { submitAndConfirm } = await loadModule();
1649
+ await withFifo(async (fifo, onKeystroke) => {
1650
+ const reaction = onKeystroke().then((got) => {
1651
+ if (got) appendFileSync(log, BUSY + IDLE);
1652
+ return got;
1653
+ });
1654
+ const result = await submitAndConfirm(
1655
+ rec({ log_file: log }),
1656
+ fifo,
1657
+ "\r",
1658
+ "some leftover unsent text",
1659
+ );
1660
+ expect(await reaction).toBe(true);
1661
+ expect(result.confirmed).toBe(false);
1662
+ expect(result.submission).toBe("not-submitted");
1663
+ });
1664
+ } finally {
1665
+ await rm(dir, { recursive: true, force: true });
1666
+ }
1667
+ },
1668
+ 10_000,
1669
+ );
1670
+
1671
+ it.skipIf(!itUnix)("distinguishes a queued receipt from submission", async () => {
1672
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-queued-"));
1461
1673
  try {
1462
1674
  const log = path.join(dir, "a.log");
1463
- await writeFile(log, "❯ \r\n");
1675
+ await writeFile(log, IDLE);
1464
1676
  const { submitAndConfirm } = await loadModule();
1465
1677
  await withFifo(async (fifo, onKeystroke) => {
1466
- // The CLI starts responding once the Enter lands. Keyed off the actual
1467
- // keystroke so the growth cannot be folded into `sizeBefore`.
1468
- const reacted = onKeystroke().then((got) => {
1469
- if (got) appendFileSync(log, "some real response text appears here\r\n");
1678
+ const reaction = onKeystroke().then((got) => {
1679
+ if (got) appendFileSync(log, "❯ Press up to edit queued messages\r\n");
1470
1680
  return got;
1471
1681
  });
1472
- const { confirmed } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1473
- expect(await reacted).toBe(true);
1474
- expect(confirmed).toBe(true);
1682
+ const result = await submitAndConfirm(
1683
+ rec({ log_file: log }),
1684
+ fifo,
1685
+ "\r",
1686
+ "some leftover unsent text",
1687
+ );
1688
+ expect(await reaction).toBe(true);
1689
+ expect(result.confirmed).toBe(false);
1690
+ expect(result.submission).toBe("queued");
1691
+ });
1692
+ } finally {
1693
+ await rm(dir, { recursive: true, force: true });
1694
+ }
1695
+ });
1696
+
1697
+ it("requires our identity to move out of the composer into the transcript", async () => {
1698
+ const { submissionState, inspectSubmission } = await loadModule();
1699
+ const marker = "<ay-msg abc12345";
1700
+ expect(submissionState([`❯ ${marker}`, "────────"], marker)).toBe("not-submitted");
1701
+ expect(inspectSubmission(["❯", ` ${marker}`, "────────"], marker).retry).toBe(true);
1702
+ expect(submissionState([marker, "❯", "────────"], marker)).toBe("submitted");
1703
+ expect(
1704
+ submissionState([marker, "❯ Press up to edit queued messages", "────────"], marker),
1705
+ ).toBe("queued");
1706
+ expect(submissionState(["❯", "────────"], marker)).toBe("not-submitted");
1707
+ expect(submissionState([], marker)).toBe("not-submitted");
1708
+ expect(inspectSubmission(["❯ unrelated draft"], marker).retry).toBe(false);
1709
+ expect(inspectSubmission(["Codex", "› Ask Codex to do anything"], "x")).toMatchObject({
1710
+ submission: "not-submitted",
1711
+ retry: false,
1712
+ });
1713
+ });
1714
+
1715
+ // Codex review (2ecc427): the last prompt-looking row was taken for the prompt,
1716
+ // so a quoted `› line` inside our unsent message put its nonce in "transcript".
1717
+ it("does not take a quoted prompt glyph inside our unsent message for the prompt", async () => {
1718
+ const { inspectSubmission } = await loadModule();
1719
+ const marker = "<ay-msg abc12345";
1720
+ const screen = ["older history", "────────", `❯ ${marker}`, " › quoted line", "────────"];
1721
+ expect(inspectSubmission(screen, marker)).toMatchObject({
1722
+ transcriptMatches: 0,
1723
+ retry: true,
1724
+ inComposer: true,
1725
+ composerVisible: true,
1726
+ });
1727
+ });
1728
+
1729
+ it("recognizes captured Codex idle/working and Claude suggestion layouts only with our transcript identity", async () => {
1730
+ const { submissionState, inspectSubmission } = await loadModule();
1731
+ const marker = "<ay-msg abc12345";
1732
+ // Layouts from the reviewer's captured fleet screens, with synthetic message identity.
1733
+ const screens = [
1734
+ [
1735
+ "• Ran tests",
1736
+ "",
1737
+ "› Ask Codex to do anything",
1738
+ " GPT-6-Astra low · ~/ws/x · Read brief",
1739
+ " ? for shortcuts ⚠ 2 warnings · f2 to view",
1740
+ ],
1741
+ [
1742
+ "• Working (10m 54s • esc to interrupt)",
1743
+ " └ Tip: Start a fresh idea with /new",
1744
+ "› Ask Codex to do anything",
1745
+ " GPT-6-Astra low · ~/ws/x · Read brief",
1746
+ " ? for shortcuts",
1747
+ ],
1748
+ [
1749
+ "✻ Churned for 15s",
1750
+ "────────────",
1751
+ "❯ push it and open the PR",
1752
+ "────────────",
1753
+ " ⏵⏵ auto mode on",
1754
+ ],
1755
+ ];
1756
+ for (const screen of screens) {
1757
+ expect(submissionState([marker, ...screen], marker)).toBe("submitted");
1758
+ expect(submissionState(screen, marker)).toBe("not-submitted");
1759
+ expect(inspectSubmission(screen, marker).retry).toBe(false);
1760
+ }
1761
+ });
1762
+
1763
+ it.skipIf(!itUnix)(
1764
+ "sends zero extra Enters for unrelated composer text, placeholders and dialogs",
1765
+ async () => {
1766
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-no-retry-"));
1767
+ try {
1768
+ const log = path.join(dir, "a.log");
1769
+ const { submitAndConfirm } = await loadModule();
1770
+ for (const screen of [
1771
+ "❯ push it and open the PR",
1772
+ "› Ask Codex to do anything",
1773
+ "Allow this command?\r\n❯ 1. Yes",
1774
+ ]) {
1775
+ await writeFile(log, screen + "\r\n");
1776
+ await withFifo(async (fifo) => {
1777
+ const result = await submitAndConfirm(
1778
+ rec({ log_file: log }),
1779
+ fifo,
1780
+ "\r",
1781
+ "<ay-msg abc12345",
1782
+ );
1783
+ expect(result.confirmed).toBe(false);
1784
+ const fs = await import("fs");
1785
+ const fd = fs.openSync(fifo, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
1786
+ try {
1787
+ const bytes = Buffer.alloc(16);
1788
+ const count = fs.readSync(fd, bytes, 0, bytes.length, null);
1789
+ expect(bytes.subarray(0, count).toString()).toBe("\r");
1790
+ } finally {
1791
+ fs.closeSync(fd);
1792
+ }
1793
+ });
1794
+ }
1795
+ } finally {
1796
+ await rm(dir, { recursive: true, force: true });
1797
+ }
1798
+ },
1799
+ );
1800
+
1801
+ it.skipIf(!itUnix)(
1802
+ "retries a swallowed Enter only for our composer and confirms its transcript",
1803
+ async () => {
1804
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-own-retry-"));
1805
+ try {
1806
+ const log = path.join(dir, "a.log");
1807
+ const marker = "<ay-msg abc12345";
1808
+ await writeFile(log, `❯ ${marker} hello\r\n`);
1809
+ const { submitAndConfirm } = await loadModule();
1810
+ await withFifo(async (fifo, onKeystroke) => {
1811
+ const reaction = (async () => {
1812
+ expect(await onKeystroke()).toBe(true); // first Enter is swallowed
1813
+ expect(await onKeystroke()).toBe(true); // only our message gets retried
1814
+ appendFileSync(
1815
+ log,
1816
+ `\r\n${marker} hello\r\n❯ push it and open the PR\r\n────────────\r\n`,
1817
+ );
1818
+ })();
1819
+ const result = await submitAndConfirm(rec({ log_file: log }), fifo, "\r", marker);
1820
+ await reaction;
1821
+ expect(result.confirmed).toBe(true);
1822
+ expect(result.submission).toBe("submitted");
1823
+ });
1824
+ } finally {
1825
+ await rm(dir, { recursive: true, force: true });
1826
+ }
1827
+ },
1828
+ 10_000,
1829
+ );
1830
+
1831
+ it("owns only a newly appeared Claude collapsed paste, never an existing or unreadable baseline", async () => {
1832
+ const { ownedCollapsedPaste } = await loadModule();
1833
+ const token = "[Pasted text #2 +12 lines]";
1834
+ const after = [`❯ ${token}`];
1835
+ expect(ownedCollapsedPaste("claude", ["❯"], after)).toBe(token);
1836
+ expect(ownedCollapsedPaste("claude", after, after)).toBeNull();
1837
+ expect(ownedCollapsedPaste("claude", [token, "❯"], after)).toBeNull();
1838
+ expect(ownedCollapsedPaste("claude", null, after)).toBeNull();
1839
+ expect(ownedCollapsedPaste("codex", ["❯"], after)).toBeNull();
1840
+ expect(ownedCollapsedPaste("claude", ["❯"], ["❯ unrelated draft"])).toBeNull();
1841
+ });
1842
+
1843
+ it.skipIf(!itUnix)(
1844
+ "retries our newly collapsed paste until its nonce appears in the transcript",
1845
+ async () => {
1846
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-collapsed-"));
1847
+ try {
1848
+ const log = path.join(dir, "a.log");
1849
+ const marker = "<ay-msg abc12345";
1850
+ await writeFile(log, "❯ [Pasted text #2 +12 lines]\r\n");
1851
+ const { submitAndConfirm } = await loadModule();
1852
+ await withFifo(async (fifo, onKeystroke) => {
1853
+ const reaction = (async () => {
1854
+ expect(await onKeystroke()).toBe(true);
1855
+ expect(await onKeystroke()).toBe(true);
1856
+ appendFileSync(log, `\r\n${marker} hello\r\n❯\r\n────────────\r\n`);
1857
+ })();
1858
+ const result = await submitAndConfirm(rec({ log_file: log }), fifo, "\r", marker, ["❯"]);
1859
+ await reaction;
1860
+ expect(result.confirmed).toBe(true);
1861
+ });
1862
+ } finally {
1863
+ await rm(dir, { recursive: true, force: true });
1864
+ }
1865
+ },
1866
+ 10_000,
1867
+ );
1868
+
1869
+ it.skipIf(!itUnix)("does not retry a collapsed paste that existed before our write", async () => {
1870
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-existing-paste-"));
1871
+ try {
1872
+ const log = path.join(dir, "a.log");
1873
+ const screen = ["❯ [Pasted text #2 +12 lines]"];
1874
+ await writeFile(log, screen.join("\r\n"));
1875
+ const { submitAndConfirm } = await loadModule();
1876
+ await withFifo(async (fifo) => {
1877
+ const result = await submitAndConfirm(
1878
+ rec({ log_file: log }),
1879
+ fifo,
1880
+ "\r",
1881
+ "<ay-msg abc12345",
1882
+ screen,
1883
+ );
1884
+ expect(result.confirmed).toBe(false);
1885
+ const fs = await import("fs");
1886
+ const fd = fs.openSync(fifo, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
1887
+ try {
1888
+ const bytes = Buffer.alloc(16);
1889
+ const count = fs.readSync(fd, bytes, 0, bytes.length, null);
1890
+ expect(bytes.subarray(0, count).toString()).toBe("\r");
1891
+ } finally {
1892
+ fs.closeSync(fd);
1893
+ }
1475
1894
  });
1476
1895
  } finally {
1477
- await rm(dir, { recursive: true, force: true }).catch(() => null);
1896
+ await rm(dir, { recursive: true, force: true });
1478
1897
  }
1479
1898
  });
1480
1899
 
@@ -1487,7 +1906,12 @@ describe("subcommands.submitAndConfirm (ay send swallowed-Enter fix)", () => {
1487
1906
  await writeFile(log, IDLE); // never touched again — nothing to confirm
1488
1907
  const { submitAndConfirm } = await loadModule();
1489
1908
  await withFifo(async (fifo) => {
1490
- const { confirmed, screen } = await submitAndConfirm(rec({ log_file: log }), fifo, "\r");
1909
+ const { confirmed, screen } = await submitAndConfirm(
1910
+ rec({ log_file: log }),
1911
+ fifo,
1912
+ "\r",
1913
+ "some leftover unsent text",
1914
+ );
1491
1915
  expect(confirmed).toBe(false);
1492
1916
  expect(screen.join("\n")).toContain("leftover unsent text");
1493
1917
  });
@@ -1505,7 +1929,10 @@ describe("subcommands.cmdSend end-to-end submit-confirm wiring", () => {
1505
1929
 
1506
1930
  // A background drain so cmdSend's writes to the FIFO never block, mirroring
1507
1931
  // the "delivers a message to a real FIFO" setup above.
1508
- async function withDrainedFifo(fn: (fifo: string) => Promise<void>) {
1932
+ async function withDrainedFifo(
1933
+ fn: (fifo: string) => Promise<void>,
1934
+ onBytes?: (bytes: string) => void,
1935
+ ) {
1509
1936
  const { spawnSync } = await import("child_process");
1510
1937
  const dir = await mkdtemp(path.join(tmpdir(), "ay-send-e2e-"));
1511
1938
  const fifo = path.join(dir, "test.fifo");
@@ -1523,7 +1950,9 @@ describe("subcommands.cmdSend end-to-end submit-confirm wiring", () => {
1523
1950
  const rdwrFd = fs.openSync(fifo, fs.constants.O_RDWR | fs.constants.O_NONBLOCK);
1524
1951
  const drain = setInterval(() => {
1525
1952
  try {
1526
- fs.readSync(rdwrFd, Buffer.alloc(4096), 0, 4096, null);
1953
+ const bytes = Buffer.alloc(4096);
1954
+ const count = fs.readSync(rdwrFd, bytes, 0, bytes.length, null);
1955
+ if (count) onBytes?.(bytes.subarray(0, count).toString());
1527
1956
  } catch {
1528
1957
  /* EAGAIN or similar — nothing pending, ignore */
1529
1958
  }
@@ -1587,34 +2016,90 @@ describe("subcommands.cmdSend end-to-end submit-confirm wiring", () => {
1587
2016
  "exits 0 and reports 'sent' when a working marker appears after the Enter (genuine confirm)",
1588
2017
  async () => {
1589
2018
  const dir = await mkdtemp(path.join(tmpdir(), "ay-send-e2e-log-"));
1590
- // A STATIC pre-existing busy marker cannot confirm — that's the #157
1591
- // wasAlreadyWorking guard (see the "already on screen before sending" test).
1592
- // Confirmation needs a real idle→working transition (or >=8 bytes of growth)
1593
- // AFTER the submit. Model that: the log is idle until we send, then a steady
1594
- // trickle of the working marker lands within submitAndConfirm's window, so it
1595
- // confirms deterministically without fragile single-shot timing.
1596
2019
  const log = path.join(dir, "a.log");
1597
- await writeFile(log, "❯ \r\n"); // idle until the submitted Enter lands
1598
- let working = false;
1599
- const respond = setInterval(() => {
1600
- if (working) appendFileSync(log, BUSY);
1601
- }, 40);
1602
- respond.unref();
2020
+ await writeFile(log, "❯ hello\r\n");
1603
2021
  try {
1604
- await withDrainedFifo(async (fifo) => {
1605
- working = true; // the submitted Enter kicks the agent into working
1606
- const { code, stdout } = await send(fifo, log, "hello");
1607
- expect(code).toBe(0);
1608
- expect(stdout).toMatch(/^sent to pid/);
1609
- expect(stdout).not.toMatch(/NOT confirmed/);
1610
- });
2022
+ await withDrainedFifo(
2023
+ async (fifo) => {
2024
+ const { code, stdout } = await send(fifo, log, "hello");
2025
+ expect(code).toBe(0);
2026
+ expect(stdout).toMatch(/^sent to pid/);
2027
+ expect(stdout).not.toMatch(/NOT confirmed/);
2028
+ },
2029
+ (bytes) => {
2030
+ if (bytes.includes("\r")) appendFileSync(log, "\r\nhello\r\n" + BUSY + "❯ \r\n");
2031
+ },
2032
+ );
1611
2033
  } finally {
1612
- clearInterval(respond);
1613
2034
  await rm(dir, { recursive: true, force: true }).catch(() => null);
1614
2035
  }
1615
2036
  },
1616
2037
  );
1617
2038
 
2039
+ it.skipIf(!itUnix)("captures collapsed-paste ownership before writing the body", async () => {
2040
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-collapse-wiring-"));
2041
+ const log = path.join(dir, "a.log");
2042
+ await writeFile(log, "❯\r\n");
2043
+ let enters = 0;
2044
+ try {
2045
+ await withDrainedFifo(
2046
+ async (fifo) => {
2047
+ const { code } = await send(fifo, log, "collapsed-message");
2048
+ expect(code).toBe(0);
2049
+ expect(enters).toBe(2);
2050
+ },
2051
+ (bytes) => {
2052
+ if (bytes.includes("collapsed-message"))
2053
+ appendFileSync(log, "\x1b[2J\x1b[H❯ [Pasted text #3 +20 lines]\r\n");
2054
+ if (bytes.includes("\r") && ++enters === 2)
2055
+ appendFileSync(log, "collapsed-message\r\n❯\r\n");
2056
+ },
2057
+ );
2058
+ } finally {
2059
+ await rm(dir, { recursive: true, force: true });
2060
+ }
2061
+ });
2062
+
2063
+ it.skipIf(!itUnix)("reports QUEUED with its distinct exit 4", async () => {
2064
+ const dir = await mkdtemp(path.join(tmpdir(), "ay-queue-e2e-"));
2065
+ const log = path.join(dir, "a.log");
2066
+ await writeFile(log, "❯ waiting\r\n");
2067
+ try {
2068
+ await withDrainedFifo(
2069
+ async (fifo) => {
2070
+ const errs: string[] = [];
2071
+ const origErr = process.stderr.write.bind(process.stderr);
2072
+ process.stderr.write = ((c: string | Uint8Array) => (
2073
+ errs.push(String(c)), true
2074
+ )) as typeof process.stderr.write;
2075
+ let res: { code: number | null; stdout: string };
2076
+ try {
2077
+ res = await send(fifo, log, "queued-receipt-test");
2078
+ } finally {
2079
+ process.stderr.write = origErr;
2080
+ }
2081
+ const { code, stdout } = res;
2082
+ expect(code).toBe(4);
2083
+ expect(stdout).toMatch(/^QUEUED to pid/);
2084
+ // The receipt says so, so a lane does not read rc=4 as a failure and resend.
2085
+ expect(errs.join("")).toContain("Queued, do not resend.");
2086
+ const { readMailbox } = await import("./messageLog.ts");
2087
+ const receipt = (await readMailbox(process.cwd(), "outbox")).findLast(
2088
+ (r) => r.body === "queued-receipt-test",
2089
+ );
2090
+ expect(receipt?.confirmed).toBe(false);
2091
+ expect(receipt?.submission).toBe("queued");
2092
+ },
2093
+ (bytes) => {
2094
+ if (bytes.includes("\r"))
2095
+ appendFileSync(log, "queued-receipt-test\r\n❯ Press up to edit queued messages\r\n");
2096
+ },
2097
+ );
2098
+ } finally {
2099
+ await rm(dir, { recursive: true, force: true });
2100
+ }
2101
+ });
2102
+
1618
2103
  it.skipIf(!itUnix)(
1619
2104
  "exits non-zero and reports the leftover screen when submission can't be confirmed",
1620
2105
  async () => {
@@ -3246,7 +3731,7 @@ describe("subcommands.cmdSend double-envelope warning", () => {
3246
3731
  '<ay-msg deadbeef from claude #900001 @ /repo/beta — reply: ay send 900001 "...">hi</ay-msg deadbeef>',
3247
3732
  "--force",
3248
3733
  ]);
3249
- expect(code).toBe(0);
3734
+ expect(code).toBe(1); // This FIFO fixture has no destination log.
3250
3735
  expect(stderr.join("")).toMatch(/DOUBLE wrapper/);
3251
3736
  } finally {
3252
3737
  process.stderr.write = origErr;
@@ -3329,7 +3814,7 @@ describe("subcommands.cmdSend double-envelope warning", () => {
3329
3814
  "plain body, quoting <ay-msg deadbeef …> mid-text is fine",
3330
3815
  "--force",
3331
3816
  ]);
3332
- expect(code).toBe(0);
3817
+ expect(code).toBe(1); // This FIFO fixture has no destination log.
3333
3818
  expect(stderr.join("")).not.toMatch(/DOUBLE wrapper/);
3334
3819
  } finally {
3335
3820
  process.stderr.write = origErr;
@@ -3403,7 +3888,7 @@ describe("subcommands.cmdSend double-envelope warning", () => {
3403
3888
  "hello from crm",
3404
3889
  "--force",
3405
3890
  ]);
3406
- expect(code).toBe(0);
3891
+ expect(code).toBe(1); // This FIFO fixture has no destination log.
3407
3892
  } finally {
3408
3893
  process.stdout.write = origOut;
3409
3894
  if (savedAyPid === undefined) delete process.env.AGENT_YES_PID;
@@ -4182,3 +4667,40 @@ describe("subcommands.cmdSend unreachable exit status", () => {
4182
4667
  },
4183
4668
  );
4184
4669
  });
4670
+
4671
+ // The drainer is spawned detached from `ay send`; a launcher that cannot start
4672
+ // is silent (spawn's ENOENT is swallowed), so the parked message simply never
4673
+ // moves. Pin the argv shape per runtime so the Windows regression (spawning the
4674
+ // .js script bare) cannot come back.
4675
+ describe("drainerLauncher", () => {
4676
+ it("re-runs the current script under bun, on every platform", async () => {
4677
+ const { drainerLauncher } = await loadModule();
4678
+ expect(
4679
+ drainerLauncher("/usr/local/bin/bun", "/repo/alpha/dist/cli.js", "/usr/local/bin/ay"),
4680
+ ).toEqual(["/usr/local/bin/bun", "/repo/alpha/dist/cli.js"]);
4681
+ expect(
4682
+ drainerLauncher(
4683
+ "C:\\Users\\alice\\.bun\\bin\\bun.exe",
4684
+ "C:\\repo\\alpha\\dist\\cli.js",
4685
+ null,
4686
+ ),
4687
+ ).toEqual(["C:\\Users\\alice\\.bun\\bin\\bun.exe", "C:\\repo\\alpha\\dist\\cli.js"]);
4688
+ });
4689
+
4690
+ it("falls back to the ay on PATH only when there is no script, and to nothing when there is neither", async () => {
4691
+ const { drainerLauncher } = await loadModule();
4692
+ expect(drainerLauncher("/usr/local/bin/bun", undefined, "/usr/local/bin/ay")).toEqual([
4693
+ "/usr/local/bin/bun",
4694
+ "/usr/local/bin/ay",
4695
+ ]);
4696
+ expect(drainerLauncher("/usr/local/bin/bun", undefined, null)).toBeNull();
4697
+ });
4698
+
4699
+ it("re-runs a compiled single-file binary as itself", async () => {
4700
+ const { drainerLauncher } = await loadModule();
4701
+ expect(drainerLauncher("/opt/ay/ay", "/$bunfs/root/cli", "/usr/local/bin/ay")).toEqual([
4702
+ "/opt/ay/ay",
4703
+ ]);
4704
+ expect(drainerLauncher("C:\\tools\\ay.exe", undefined, null)).toEqual(["C:\\tools\\ay.exe"]);
4705
+ });
4706
+ });