@indigoai-us/hq-cloud 6.16.4 → 6.16.6

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 (34) hide show
  1. package/dist/bin/sync-runner-events.d.ts +10 -0
  2. package/dist/bin/sync-runner-events.d.ts.map +1 -1
  3. package/dist/bin/sync-runner-events.js +34 -0
  4. package/dist/bin/sync-runner-events.js.map +1 -1
  5. package/dist/bin/sync-runner-events.test.js +65 -1
  6. package/dist/bin/sync-runner-events.test.js.map +1 -1
  7. package/dist/bin/sync-runner-heap-telemetry.d.ts +313 -45
  8. package/dist/bin/sync-runner-heap-telemetry.d.ts.map +1 -1
  9. package/dist/bin/sync-runner-heap-telemetry.js +503 -114
  10. package/dist/bin/sync-runner-heap-telemetry.js.map +1 -1
  11. package/dist/bin/sync-runner-heap-telemetry.test.js +436 -11
  12. package/dist/bin/sync-runner-heap-telemetry.test.js.map +1 -1
  13. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  14. package/dist/bin/sync-runner-watch-loop.js +69 -9
  15. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  16. package/dist/bin/sync-runner-watch-loop.test.js +13 -1
  17. package/dist/bin/sync-runner-watch-loop.test.js.map +1 -1
  18. package/dist/bin/sync-runner.d.ts +27 -1
  19. package/dist/bin/sync-runner.d.ts.map +1 -1
  20. package/dist/bin/sync-runner.js +10 -1
  21. package/dist/bin/sync-runner.js.map +1 -1
  22. package/dist/sync/event-sync.js +2 -2
  23. package/dist/sync/event-sync.js.map +1 -1
  24. package/dist/sync/state-store.d.ts +8 -0
  25. package/dist/sync/state-store.d.ts.map +1 -1
  26. package/dist/sync/state-store.js +37 -0
  27. package/dist/sync/state-store.js.map +1 -1
  28. package/dist/watcher.d.ts +49 -9
  29. package/dist/watcher.d.ts.map +1 -1
  30. package/dist/watcher.js +425 -158
  31. package/dist/watcher.js.map +1 -1
  32. package/dist/watcher.test.js +349 -23
  33. package/dist/watcher.test.js.map +1 -1
  34. package/package.json +1 -1
@@ -4,7 +4,7 @@ import * as os from "os";
4
4
  import * as path from "path";
5
5
  import { createHash } from "node:crypto";
6
6
  import { watch } from "chokidar";
7
- import { FakeClock, WatchPushDriver, TreeWatcher, ChokidarWatchBudget, createWatchPathFilter, toChokidarIgnored, PushEventEmitter, PUBLISH_OUTCOME_JOURNALD_CEILING_BYTES, PUBLISH_OUTCOME_SUCCESS_SAMPLE_CAP, resolveEventDebounceConfig, summarizePublishBatchOutcome, DEFAULT_EVENT_DEBOUNCE_MS, DEFAULT_EVENT_MAX_WAIT_MS, DEFAULT_EVENT_SMALL_BATCH_DEBOUNCE_MS, DEFAULT_EVENT_SMALL_BATCH_MAX_PATHS, DEFAULT_EVENT_SMALL_BATCH_MAX_BYTES, DEFAULT_PUBLISH_MIN_INTERVAL_MS, EVENT_DEBOUNCE_MS_ENV, EVENT_MAX_WAIT_MS_ENV, EVENT_SMALL_BATCH_DEBOUNCE_MS_ENV, EVENT_SMALL_BATCH_MAX_PATHS_ENV, EVENT_SMALL_BATCH_MAX_BYTES_ENV, DEFAULT_TREE_WATCHER_MAX_WATCHED_PATHS, MAX_TREE_WATCHER_MAX_WATCHED_PATHS, TREE_WATCHER_MAX_WATCHED_PATHS_ENV, resolveMaxWatchedPaths, PUBLISH_MIN_INTERVAL_MS_ENV, resolvePublishMinIntervalMs, replayDurablePublishedContentHashRecordsForTest, } from "./watcher.js";
7
+ import { FakeClock, WatchPushDriver, TreeWatcher, ChokidarWatchBudget, createWatchPathFilter, toChokidarIgnored, PushEventEmitter, PUBLISH_OUTCOME_JOURNALD_CEILING_BYTES, PUBLISH_OUTCOME_SUCCESS_SAMPLE_CAP, resolveEventDebounceConfig, summarizePublishBatchOutcome, DEFAULT_EVENT_DEBOUNCE_MS, DEFAULT_EVENT_MAX_WAIT_MS, DEFAULT_EVENT_SMALL_BATCH_DEBOUNCE_MS, DEFAULT_EVENT_SMALL_BATCH_MAX_PATHS, DEFAULT_EVENT_SMALL_BATCH_MAX_BYTES, DEFAULT_PUBLISH_MIN_INTERVAL_MS, EVENT_DEBOUNCE_MS_ENV, EVENT_MAX_WAIT_MS_ENV, EVENT_SMALL_BATCH_DEBOUNCE_MS_ENV, EVENT_SMALL_BATCH_MAX_PATHS_ENV, EVENT_SMALL_BATCH_MAX_BYTES_ENV, DEFAULT_TREE_WATCHER_MAX_WATCHED_PATHS, MAX_TREE_WATCHER_MAX_WATCHED_PATHS, TREE_WATCHER_MAX_WATCHED_PATHS_ENV, resolveMaxWatchedPaths, PUBLISH_MIN_INTERVAL_MS_ENV, resolvePublishMinIntervalMs, DurablePublishedContentHashes, DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS, replayDurablePublishedContentHashRecordsForTest } from "./watcher.js";
8
8
  import { StaticFlagProvider } from "./sync/feature-flags.js";
9
9
  import { StateStore, StateStoreLockError } from "./sync/state-store.js";
10
10
  import { RealtimeUnavailableError, } from "./sync/push-transport.js";
@@ -168,6 +168,28 @@ describe("TreeWatcher pending backlog caps", () => {
168
168
  fs.rmSync(dir, { recursive: true, force: true });
169
169
  }
170
170
  });
171
+ it("canonicalizes a legacy journal key before adding its warm-up deletion to the batch", () => {
172
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-watcher-warmup-posix-key-"));
173
+ try {
174
+ const capture = vi.fn(() => []);
175
+ const watcher = new TreeWatcher({
176
+ hqRoot: dir,
177
+ pathFilter: () => true,
178
+ captureLocalDeleteSnapshots: capture,
179
+ });
180
+ const result = watcher.collectWarmupCatchup(Date.now(), [
181
+ "projects\\forecast\\x.csv",
182
+ ]);
183
+ const absolute = path.join(dir, "projects", "forecast", "x.csv");
184
+ expect([...result.batch.paths.entries()]).toEqual([
185
+ [absolute, "projects/forecast/x.csv"],
186
+ ]);
187
+ expect(capture).toHaveBeenCalledWith("projects/forecast/x.csv", "unlink");
188
+ }
189
+ finally {
190
+ fs.rmSync(dir, { recursive: true, force: true });
191
+ }
192
+ });
171
193
  it("recovers a warm-up path even when the event backlog dropped it", () => {
172
194
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-watcher-warmup-overflow-"));
173
195
  try {
@@ -1423,6 +1445,275 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
1423
1445
  expect(replayDurablePublishedContentHashRecordsForTest(records, true))
1424
1446
  .toEqual(replayDurablePublishedContentHashRecordsForTest(records, false));
1425
1447
  });
1448
+ it("replays legacy records followed by a batch with the exact single-record LRU state", () => {
1449
+ const batchEntries = [
1450
+ { kind: "delete", relativePath: "alpha" },
1451
+ { kind: "set", relativePath: "gamma", contentHash: "sha256:gamma", maxPaths: 2 },
1452
+ { kind: "set", relativePath: "beta", contentHash: "sha256:beta-new", maxPaths: 2 },
1453
+ ];
1454
+ const legacyThenBatch = [
1455
+ { sequence: 1, type: 0x5031, flags: 0, payload: { relativePath: "alpha", contentHash: "sha256:alpha", maxPaths: 2 } },
1456
+ { sequence: 2, type: 0x5031, flags: 0, payload: { relativePath: "beta", contentHash: "sha256:beta", maxPaths: 2 } },
1457
+ { sequence: 3, type: 0x5033, flags: 0, payload: { entries: batchEntries } },
1458
+ ];
1459
+ const equivalentSingles = [
1460
+ ...legacyThenBatch.slice(0, 2),
1461
+ ...batchEntries.map((entry, index) => ({
1462
+ sequence: index + 3,
1463
+ type: entry.kind === "set" ? 0x5031 : 0x5032,
1464
+ flags: 0,
1465
+ payload: entry.kind === "set"
1466
+ ? { relativePath: entry.relativePath, contentHash: entry.contentHash, maxPaths: entry.maxPaths }
1467
+ : { relativePath: entry.relativePath },
1468
+ })),
1469
+ ];
1470
+ const batched = replayDurablePublishedContentHashRecordsForTest(legacyThenBatch);
1471
+ expect(JSON.stringify(batched)).toBe(JSON.stringify(replayDurablePublishedContentHashRecordsForTest(equivalentSingles)));
1472
+ expect(batched).toEqual(replayDurablePublishedContentHashRecordsForTest(legacyThenBatch, false));
1473
+ });
1474
+ it("coalesces pull marks into one append and persists only the final mutation for a path", async () => {
1475
+ const clock = new FakeClock();
1476
+ const appendGenerated = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync");
1477
+ const stateDir = path.join(dir, "coalesced-state");
1478
+ const emitter = makeEmitter({ clock, lastPublishedContentHashStateDir: stateDir });
1479
+ const finalContent = "final revision\n";
1480
+ const finalHash = `sha256:${createHash("sha256").update(finalContent).digest("hex")}`;
1481
+ try {
1482
+ for (let index = 0; index < 128; index++) {
1483
+ emitter.markContentPublished(`path-${index}.md`, `sha256:${index}`);
1484
+ }
1485
+ emitter.markContentPublished("same.md", "sha256:obsolete");
1486
+ emitter.markContentPublished("same.md", finalHash);
1487
+ clock.advance(DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS);
1488
+ await flushImmediate();
1489
+ expect(appendGenerated).toHaveBeenCalledTimes(1);
1490
+ await emitter.dispose();
1491
+ const samePath = path.join(dir, "same.md");
1492
+ fs.writeFileSync(samePath, finalContent);
1493
+ const published = [];
1494
+ await makeEmitter({ published, lastPublishedContentHashStateDir: stateDir }).emitForBatch({
1495
+ paths: new Map([[samePath, "same.md"]]),
1496
+ });
1497
+ expect(published).toEqual([]);
1498
+ }
1499
+ finally {
1500
+ appendGenerated.mockRestore();
1501
+ }
1502
+ });
1503
+ it("drops a stale guarded mutation at flush time without dropping the rest of its batch", async () => {
1504
+ const clock = new FakeClock();
1505
+ const hashes = new DurablePublishedContentHashes(path.join(dir, "predicate-state"), "test", 4, {}, clock);
1506
+ let shouldPersist = true;
1507
+ const stale = hashes.setIfCurrent("stale.md", "sha256:stale", 4, () => shouldPersist);
1508
+ const kept = hashes.set("kept.md", "sha256:kept", 4, false);
1509
+ shouldPersist = false;
1510
+ await hashes.dispose();
1511
+ await expect(stale).resolves.toBe(false);
1512
+ await expect(kept).resolves.toBeUndefined();
1513
+ expect(hashes.get("stale.md")).toBeUndefined();
1514
+ expect(hashes.get("kept.md")).toBe("sha256:kept");
1515
+ });
1516
+ it("flushes pending durable pull marks while disposing", async () => {
1517
+ const appendGenerated = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync");
1518
+ const emitter = makeEmitter({ lastPublishedContentHashStateDir: path.join(dir, "dispose-state") });
1519
+ try {
1520
+ emitter.markContentPublished("pending.md", "sha256:pending");
1521
+ await emitter.dispose();
1522
+ expect(appendGenerated).toHaveBeenCalledTimes(1);
1523
+ }
1524
+ finally {
1525
+ appendGenerated.mockRestore();
1526
+ }
1527
+ });
1528
+ it("reports and retries a failed durable hash append as one physical batch", async () => {
1529
+ const clock = new FakeClock();
1530
+ const onError = vi.fn();
1531
+ const appendGenerated = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
1532
+ .mockRejectedValueOnce(new StateStoreLockError("timed out acquiring append.lock"));
1533
+ const emitter = makeEmitter({
1534
+ clock,
1535
+ onError,
1536
+ lastPublishedContentHashStateDir: path.join(dir, "batch-lock-timeout-state"),
1537
+ });
1538
+ try {
1539
+ for (let index = 0; index < 128; index += 1) {
1540
+ emitter.markContentPublished(`batch-${index}.md`, `sha256:${index}`);
1541
+ }
1542
+ clock.advance(DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS);
1543
+ await flushImmediate();
1544
+ expect(onError).toHaveBeenCalledTimes(1);
1545
+ expect(onError.mock.calls[0]?.[0]).toMatchObject({ name: "StateStoreLockError" });
1546
+ expect((onError.mock.calls[0]?.[0]).message).toContain("128 mutations");
1547
+ expect(clock.pendingTimerCount()).toBe(1);
1548
+ }
1549
+ finally {
1550
+ await emitter.dispose();
1551
+ appendGenerated.mockRestore();
1552
+ }
1553
+ });
1554
+ it("does not revive a superseded mutation while its failed batch is retrying", async () => {
1555
+ const clock = new FakeClock();
1556
+ let releaseRetry;
1557
+ const retryGate = new Promise((resolve) => { releaseRetry = resolve; });
1558
+ let retryEntered;
1559
+ const retryStarted = new Promise((resolve) => { retryEntered = resolve; });
1560
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
1561
+ const appendGenerated = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
1562
+ .mockRejectedValueOnce(new StateStoreLockError("timed out acquiring append.lock"))
1563
+ .mockImplementationOnce(async function (type, preparePayload, flags) {
1564
+ retryEntered();
1565
+ await retryGate;
1566
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
1567
+ });
1568
+ const hashes = new DurablePublishedContentHashes(path.join(dir, "superseded-batch-retry-state"), "test", 4, {}, clock);
1569
+ try {
1570
+ const stale = hashes.setIfCurrent("same.md", "sha256:stale", 4, () => true);
1571
+ await expect(stale).resolves.toBe(false);
1572
+ expect(clock.pendingTimerCount()).toBe(1);
1573
+ clock.advance(250);
1574
+ await retryStarted;
1575
+ const newer = hashes.set("same.md", "sha256:newer", 4, false);
1576
+ releaseRetry();
1577
+ await flushImmediate();
1578
+ clock.advance(DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS);
1579
+ await expect(newer).resolves.toBeUndefined();
1580
+ expect(hashes.get("same.md")).toBe("sha256:newer");
1581
+ }
1582
+ finally {
1583
+ await hashes.dispose();
1584
+ appendGenerated.mockRestore();
1585
+ }
1586
+ });
1587
+ it("rechecks a guarded mutation when its failed batch retries", async () => {
1588
+ const clock = new FakeClock();
1589
+ let shouldPersist = true;
1590
+ const appendGenerated = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
1591
+ .mockRejectedValueOnce(new StateStoreLockError("timed out acquiring append.lock"));
1592
+ const hashes = new DurablePublishedContentHashes(path.join(dir, "guarded-batch-retry-state"), "test", 4, {}, clock);
1593
+ try {
1594
+ const stale = hashes.setIfCurrent("stale.md", "sha256:stale", 4, () => shouldPersist);
1595
+ await expect(stale).resolves.toBe(false);
1596
+ shouldPersist = false;
1597
+ clock.advance(250);
1598
+ await flushImmediate();
1599
+ expect(hashes.get("stale.md")).toBeUndefined();
1600
+ expect(appendGenerated).toHaveBeenCalledTimes(2);
1601
+ }
1602
+ finally {
1603
+ await hashes.dispose();
1604
+ appendGenerated.mockRestore();
1605
+ }
1606
+ });
1607
+ it("drains an active publish into the durable memo before disposing", async () => {
1608
+ let releasePublish;
1609
+ const publishGate = new Promise((resolve) => { releasePublish = resolve; });
1610
+ let publishEntered;
1611
+ const publishStarted = new Promise((resolve) => { publishEntered = resolve; });
1612
+ const published = [];
1613
+ const stateDir = path.join(dir, "dispose-publish-drain-state");
1614
+ const changed = path.join(dir, "dispose-publish-drain.md");
1615
+ fs.writeFileSync(changed, "drain me\n");
1616
+ const emitter = makeEmitter({
1617
+ lastPublishedContentHashStateDir: stateDir,
1618
+ transport: {
1619
+ start: async () => { },
1620
+ dispose: async () => { },
1621
+ connected: true,
1622
+ publish: async (event) => {
1623
+ publishEntered();
1624
+ await publishGate;
1625
+ published.push(event);
1626
+ },
1627
+ },
1628
+ });
1629
+ const publishing = emitter.emitForBatch({ paths: new Map([[changed, "dispose-publish-drain.md"]]) });
1630
+ await publishStarted;
1631
+ let disposed = false;
1632
+ const disposing = emitter.dispose().then(() => { disposed = true; });
1633
+ await Promise.resolve();
1634
+ expect(disposed).toBe(false);
1635
+ releasePublish();
1636
+ await Promise.all([publishing, disposing]);
1637
+ const afterRestart = [];
1638
+ await makeEmitter({
1639
+ published: afterRestart,
1640
+ lastPublishedContentHashStateDir: stateDir,
1641
+ }).emitForBatch({ paths: new Map([[changed, "dispose-publish-drain.md"]]) });
1642
+ expect(published).toHaveLength(1);
1643
+ expect(afterRestart).toEqual([]);
1644
+ });
1645
+ it("leaves the durable memo open when a publish outlives the shutdown drain", async () => {
1646
+ const clock = new FakeClock();
1647
+ const onError = vi.fn();
1648
+ let releasePublish;
1649
+ const publishGate = new Promise((resolve) => { releasePublish = resolve; });
1650
+ let publishEntered;
1651
+ const publishStarted = new Promise((resolve) => { publishEntered = resolve; });
1652
+ const stateDir = path.join(dir, "dispose-timeout-state");
1653
+ const changed = path.join(dir, "dispose-timeout.md");
1654
+ fs.writeFileSync(changed, "late publish\n");
1655
+ const emitter = makeEmitter({
1656
+ clock,
1657
+ onError,
1658
+ lastPublishedContentHashStateDir: stateDir,
1659
+ transport: {
1660
+ start: async () => { },
1661
+ dispose: async () => { },
1662
+ connected: true,
1663
+ publish: async () => {
1664
+ publishEntered();
1665
+ await publishGate;
1666
+ },
1667
+ },
1668
+ });
1669
+ const publishing = emitter.emitForBatch({ paths: new Map([[changed, "dispose-timeout.md"]]) });
1670
+ await publishStarted;
1671
+ const disposing = emitter.dispose();
1672
+ clock.advance(60_000);
1673
+ await disposing;
1674
+ expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("timed out draining active publish") }), {});
1675
+ releasePublish();
1676
+ await publishing;
1677
+ await flushImmediate();
1678
+ const afterRestart = [];
1679
+ await makeEmitter({
1680
+ published: afterRestart,
1681
+ lastPublishedContentHashStateDir: stateDir,
1682
+ }).emitForBatch({ paths: new Map([[changed, "dispose-timeout.md"]]) });
1683
+ expect(afterRestart).toEqual([]);
1684
+ });
1685
+ it("returns one disposal barrier until the final durable batch is appended", async () => {
1686
+ let releaseAppend;
1687
+ const appendGate = new Promise((resolve) => { releaseAppend = resolve; });
1688
+ let appendEntered;
1689
+ const appendStarted = new Promise((resolve) => { appendEntered = resolve; });
1690
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
1691
+ const appendGenerated = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
1692
+ .mockImplementationOnce(async function (type, preparePayload, flags) {
1693
+ appendEntered();
1694
+ await appendGate;
1695
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
1696
+ });
1697
+ const emitter = makeEmitter({ lastPublishedContentHashStateDir: path.join(dir, "concurrent-dispose-state") });
1698
+ try {
1699
+ emitter.markContentPublished("pending.md", "sha256:pending");
1700
+ const firstDispose = emitter.dispose();
1701
+ await appendStarted;
1702
+ let secondDisposed = false;
1703
+ const secondDispose = emitter.dispose();
1704
+ expect(secondDispose).toBe(firstDispose);
1705
+ void secondDispose.then(() => { secondDisposed = true; });
1706
+ await Promise.resolve();
1707
+ expect(secondDisposed).toBe(false);
1708
+ releaseAppend();
1709
+ await Promise.all([firstDispose, secondDispose]);
1710
+ expect(secondDisposed).toBe(true);
1711
+ expect(appendGenerated).toHaveBeenCalledTimes(1);
1712
+ }
1713
+ finally {
1714
+ appendGenerated.mockRestore();
1715
+ }
1716
+ });
1426
1717
  it("replays __proto__ hash records with the same state as the legacy reducer", async () => {
1427
1718
  const records = [
1428
1719
  { sequence: 1, type: 0x5031, flags: 0, payload: { relativePath: "__proto__", contentHash: "sha256:proto", maxPaths: 4 } },
@@ -1596,6 +1887,38 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
1596
1887
  expect(published[0]).not.toHaveProperty("mtime");
1597
1888
  expect(published[0]).not.toHaveProperty("sizeBytes");
1598
1889
  });
1890
+ it("canonicalizes backslash event keys while leaving POSIX keys byte-identical", async () => {
1891
+ const published = [];
1892
+ const legacyPath = path.join(dir, "legacy.csv");
1893
+ const posixPath = path.join(dir, "already-posix.csv");
1894
+ fs.writeFileSync(legacyPath, "legacy\n");
1895
+ fs.writeFileSync(posixPath, "posix\n");
1896
+ const emitter = makeEmitter({ published });
1897
+ await emitter.emitForBatch({
1898
+ paths: new Map([[legacyPath, "projects\\forecast\\x.csv"]]),
1899
+ });
1900
+ await emitter.emitForBatch({
1901
+ paths: new Map([[posixPath, "projects/forecast/already-posix.csv"]]),
1902
+ });
1903
+ expect(published.map((event) => event.relativePath)).toEqual([
1904
+ "projects/forecast/x.csv",
1905
+ "projects/forecast/already-posix.csv",
1906
+ ]);
1907
+ });
1908
+ it("canonicalizes a backslash tombstone event key", async () => {
1909
+ const published = [];
1910
+ const missing = path.join(dir, "deleted.csv");
1911
+ const emitter = makeEmitter({ published });
1912
+ await emitter.emitForBatch({
1913
+ paths: new Map([[missing, "projects\\forecast\\deleted.csv"]]),
1914
+ });
1915
+ expect(published).toEqual([
1916
+ expect.objectContaining({
1917
+ kind: "delete",
1918
+ relativePath: "projects/forecast/deleted.csv",
1919
+ }),
1920
+ ]);
1921
+ });
1599
1922
  it("publishes N unchanged watcher announcements exactly once", async () => {
1600
1923
  const published = [];
1601
1924
  const unchanged = path.join(dir, "unchanged-session-log.jsonl");
@@ -2218,9 +2541,11 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2218
2541
  it("retries a durable publish-state lock timeout without rejecting the watcher announce", async () => {
2219
2542
  const clock = new FakeClock();
2220
2543
  const onError = vi.fn();
2221
- const appendAsync = vi.spyOn(StateStore.prototype, "appendAsync")
2222
- .mockResolvedValueOnce({});
2223
- const appendIfAsync = vi.spyOn(StateStore.prototype, "appendIfAsync")
2544
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
2545
+ const appendGeneratedIfAsync = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
2546
+ .mockImplementationOnce(function (type, preparePayload, flags) {
2547
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
2548
+ })
2224
2549
  .mockRejectedValueOnce(new StateStoreLockError("timed out acquiring append.lock"));
2225
2550
  const published = [];
2226
2551
  const changed = path.join(dir, "lock-timeout.md");
@@ -2234,23 +2559,24 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2234
2559
  }).emitForBatch({ paths: new Map([[changed, "lock-timeout.md"]]) })).resolves.toBeUndefined();
2235
2560
  expect(published).toHaveLength(1);
2236
2561
  expect(onError).toHaveBeenCalledWith(expect.objectContaining({ name: "StateStoreLockError" }), { relativePath: "lock-timeout.md" });
2237
- expect(appendAsync).toHaveBeenCalledTimes(1); // invalidate before the guarded durable set
2238
- const conditionalAppendsBeforeRetry = appendIfAsync.mock.calls.length;
2562
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(2); // invalidate before the guarded durable set
2563
+ const appendsBeforeRetry = appendGeneratedIfAsync.mock.calls.length;
2239
2564
  clock.advance(250);
2240
2565
  await Promise.resolve();
2241
2566
  await Promise.resolve();
2242
- expect(appendIfAsync).toHaveBeenCalledTimes(conditionalAppendsBeforeRetry + 1);
2567
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(appendsBeforeRetry + 1);
2243
2568
  }
2244
2569
  finally {
2245
- appendIfAsync.mockRestore();
2246
- appendAsync.mockRestore();
2570
+ appendGeneratedIfAsync.mockRestore();
2247
2571
  }
2248
2572
  });
2249
2573
  it("cancels a stale durable-state retry after persisting a newer hash", async () => {
2250
2574
  const clock = new FakeClock();
2251
- const appendAsync = vi.spyOn(StateStore.prototype, "appendAsync")
2252
- .mockResolvedValueOnce({});
2253
- const appendIfAsync = vi.spyOn(StateStore.prototype, "appendIfAsync")
2575
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
2576
+ const appendGeneratedIfAsync = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
2577
+ .mockImplementationOnce(function (type, preparePayload, flags) {
2578
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
2579
+ })
2254
2580
  .mockRejectedValueOnce(new StateStoreLockError("timed out acquiring append.lock"));
2255
2581
  const stateDir = path.join(dir, "superseded-retry-state");
2256
2582
  const changed = path.join(dir, "superseded-retry.md");
@@ -2263,10 +2589,10 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2263
2589
  fs.writeFileSync(changed, "B\n");
2264
2590
  await emitter.emitForBatch({ paths: new Map([[changed, "superseded-retry.md"]]) });
2265
2591
  expect(clock.pendingTimerCount()).toBe(0);
2266
- const conditionalAppendsAfterNewerSuccess = appendIfAsync.mock.calls.length;
2592
+ const appendsAfterNewerSuccess = appendGeneratedIfAsync.mock.calls.length;
2267
2593
  clock.advance(10_000);
2268
2594
  await Promise.resolve();
2269
- expect(appendIfAsync).toHaveBeenCalledTimes(conditionalAppendsAfterNewerSuccess);
2595
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(appendsAfterNewerSuccess);
2270
2596
  const afterRestart = [];
2271
2597
  await makeEmitter({
2272
2598
  published: afterRestart,
@@ -2275,16 +2601,17 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2275
2601
  expect(afterRestart).toEqual([]);
2276
2602
  }
2277
2603
  finally {
2278
- appendIfAsync.mockRestore();
2279
- appendAsync.mockRestore();
2604
+ appendGeneratedIfAsync.mockRestore();
2280
2605
  }
2281
2606
  });
2282
2607
  it("drops permanent durable-state persistence errors without scheduling retries", async () => {
2283
2608
  const clock = new FakeClock();
2284
2609
  const onError = vi.fn();
2285
- const appendAsync = vi.spyOn(StateStore.prototype, "appendAsync")
2286
- .mockResolvedValueOnce({});
2287
- const appendIfAsync = vi.spyOn(StateStore.prototype, "appendIfAsync")
2610
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
2611
+ const appendGeneratedIfAsync = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
2612
+ .mockImplementationOnce(function (type, preparePayload, flags) {
2613
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
2614
+ })
2288
2615
  .mockRejectedValue(new Error("EACCES: state directory is unwritable"));
2289
2616
  const changed = path.join(dir, "permanent-state-error.md");
2290
2617
  fs.writeFileSync(changed, "cannot persist\n");
@@ -2297,15 +2624,14 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2297
2624
  expect(onError).toHaveBeenCalledTimes(1);
2298
2625
  expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("EACCES") }), { relativePath: "permanent-state-error.md" });
2299
2626
  expect(clock.pendingTimerCount()).toBe(0);
2300
- const conditionalAppendsAfterFailure = appendIfAsync.mock.calls.length;
2627
+ const appendsAfterFailure = appendGeneratedIfAsync.mock.calls.length;
2301
2628
  clock.advance(60_000);
2302
2629
  await Promise.resolve();
2303
- expect(appendIfAsync).toHaveBeenCalledTimes(conditionalAppendsAfterFailure);
2630
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(appendsAfterFailure);
2304
2631
  expect(onError).toHaveBeenCalledTimes(1);
2305
2632
  }
2306
2633
  finally {
2307
- appendIfAsync.mockRestore();
2308
- appendAsync.mockRestore();
2634
+ appendGeneratedIfAsync.mockRestore();
2309
2635
  }
2310
2636
  });
2311
2637
  it("emits push telemetry only when publication fails", async () => {