@indigoai-us/hq-cloud 6.16.5 → 6.16.7

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.
@@ -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";
@@ -1445,6 +1445,275 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
1445
1445
  expect(replayDurablePublishedContentHashRecordsForTest(records, true))
1446
1446
  .toEqual(replayDurablePublishedContentHashRecordsForTest(records, false));
1447
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
+ });
1448
1717
  it("replays __proto__ hash records with the same state as the legacy reducer", async () => {
1449
1718
  const records = [
1450
1719
  { sequence: 1, type: 0x5031, flags: 0, payload: { relativePath: "__proto__", contentHash: "sha256:proto", maxPaths: 4 } },
@@ -1618,37 +1887,57 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
1618
1887
  expect(published[0]).not.toHaveProperty("mtime");
1619
1888
  expect(published[0]).not.toHaveProperty("sizeBytes");
1620
1889
  });
1621
- it("canonicalizes backslash event keys while leaving POSIX keys byte-identical", async () => {
1890
+ it("canonicalizes native-separator event keys and leaves POSIX keys byte-identical", async () => {
1622
1891
  const published = [];
1892
+ const onError = vi.fn();
1623
1893
  const legacyPath = path.join(dir, "legacy.csv");
1624
1894
  const posixPath = path.join(dir, "already-posix.csv");
1625
1895
  fs.writeFileSync(legacyPath, "legacy\n");
1626
1896
  fs.writeFileSync(posixPath, "posix\n");
1627
- const emitter = makeEmitter({ published });
1897
+ const emitter = makeEmitter({ published, onError });
1898
+ const windowsKey = "projects\\forecast\\x.csv";
1628
1899
  await emitter.emitForBatch({
1629
- paths: new Map([[legacyPath, "projects\\forecast\\x.csv"]]),
1900
+ paths: new Map([[legacyPath, windowsKey]]),
1630
1901
  });
1631
1902
  await emitter.emitForBatch({
1632
1903
  paths: new Map([[posixPath, "projects/forecast/already-posix.csv"]]),
1633
1904
  });
1634
- expect(published.map((event) => event.relativePath)).toEqual([
1635
- "projects/forecast/x.csv",
1636
- "projects/forecast/already-posix.csv",
1637
- ]);
1905
+ if (path.sep === "\\") {
1906
+ expect(published.map((event) => event.relativePath)).toEqual([
1907
+ "projects/forecast/x.csv",
1908
+ "projects/forecast/already-posix.csv",
1909
+ ]);
1910
+ expect(onError).not.toHaveBeenCalled();
1911
+ }
1912
+ else {
1913
+ // POSIX: `\\` is a filename character. Do not remap onto a nested key.
1914
+ expect(published.map((event) => event.relativePath)).toEqual([
1915
+ "projects/forecast/already-posix.csv",
1916
+ ]);
1917
+ expect(onError).toHaveBeenCalled();
1918
+ }
1638
1919
  });
1639
- it("canonicalizes a backslash tombstone event key", async () => {
1920
+ it("canonicalizes a native-separator tombstone only when `\\` is path.sep", async () => {
1640
1921
  const published = [];
1922
+ const onError = vi.fn();
1641
1923
  const missing = path.join(dir, "deleted.csv");
1642
- const emitter = makeEmitter({ published });
1924
+ const emitter = makeEmitter({ published, onError });
1643
1925
  await emitter.emitForBatch({
1644
1926
  paths: new Map([[missing, "projects\\forecast\\deleted.csv"]]),
1645
1927
  });
1646
- expect(published).toEqual([
1647
- expect.objectContaining({
1648
- kind: "delete",
1649
- relativePath: "projects/forecast/deleted.csv",
1650
- }),
1651
- ]);
1928
+ if (path.sep === "\\") {
1929
+ expect(published).toEqual([
1930
+ expect.objectContaining({
1931
+ kind: "delete",
1932
+ relativePath: "projects/forecast/deleted.csv",
1933
+ }),
1934
+ ]);
1935
+ expect(onError).not.toHaveBeenCalled();
1936
+ }
1937
+ else {
1938
+ expect(published).toEqual([]);
1939
+ expect(onError).toHaveBeenCalled();
1940
+ }
1652
1941
  });
1653
1942
  it("publishes N unchanged watcher announcements exactly once", async () => {
1654
1943
  const published = [];
@@ -2272,9 +2561,11 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2272
2561
  it("retries a durable publish-state lock timeout without rejecting the watcher announce", async () => {
2273
2562
  const clock = new FakeClock();
2274
2563
  const onError = vi.fn();
2275
- const appendAsync = vi.spyOn(StateStore.prototype, "appendAsync")
2276
- .mockResolvedValueOnce({});
2277
- const appendIfAsync = vi.spyOn(StateStore.prototype, "appendIfAsync")
2564
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
2565
+ const appendGeneratedIfAsync = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
2566
+ .mockImplementationOnce(function (type, preparePayload, flags) {
2567
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
2568
+ })
2278
2569
  .mockRejectedValueOnce(new StateStoreLockError("timed out acquiring append.lock"));
2279
2570
  const published = [];
2280
2571
  const changed = path.join(dir, "lock-timeout.md");
@@ -2288,23 +2579,24 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2288
2579
  }).emitForBatch({ paths: new Map([[changed, "lock-timeout.md"]]) })).resolves.toBeUndefined();
2289
2580
  expect(published).toHaveLength(1);
2290
2581
  expect(onError).toHaveBeenCalledWith(expect.objectContaining({ name: "StateStoreLockError" }), { relativePath: "lock-timeout.md" });
2291
- expect(appendAsync).toHaveBeenCalledTimes(1); // invalidate before the guarded durable set
2292
- const conditionalAppendsBeforeRetry = appendIfAsync.mock.calls.length;
2582
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(2); // invalidate before the guarded durable set
2583
+ const appendsBeforeRetry = appendGeneratedIfAsync.mock.calls.length;
2293
2584
  clock.advance(250);
2294
2585
  await Promise.resolve();
2295
2586
  await Promise.resolve();
2296
- expect(appendIfAsync).toHaveBeenCalledTimes(conditionalAppendsBeforeRetry + 1);
2587
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(appendsBeforeRetry + 1);
2297
2588
  }
2298
2589
  finally {
2299
- appendIfAsync.mockRestore();
2300
- appendAsync.mockRestore();
2590
+ appendGeneratedIfAsync.mockRestore();
2301
2591
  }
2302
2592
  });
2303
2593
  it("cancels a stale durable-state retry after persisting a newer hash", async () => {
2304
2594
  const clock = new FakeClock();
2305
- const appendAsync = vi.spyOn(StateStore.prototype, "appendAsync")
2306
- .mockResolvedValueOnce({});
2307
- const appendIfAsync = vi.spyOn(StateStore.prototype, "appendIfAsync")
2595
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
2596
+ const appendGeneratedIfAsync = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
2597
+ .mockImplementationOnce(function (type, preparePayload, flags) {
2598
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
2599
+ })
2308
2600
  .mockRejectedValueOnce(new StateStoreLockError("timed out acquiring append.lock"));
2309
2601
  const stateDir = path.join(dir, "superseded-retry-state");
2310
2602
  const changed = path.join(dir, "superseded-retry.md");
@@ -2317,10 +2609,10 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2317
2609
  fs.writeFileSync(changed, "B\n");
2318
2610
  await emitter.emitForBatch({ paths: new Map([[changed, "superseded-retry.md"]]) });
2319
2611
  expect(clock.pendingTimerCount()).toBe(0);
2320
- const conditionalAppendsAfterNewerSuccess = appendIfAsync.mock.calls.length;
2612
+ const appendsAfterNewerSuccess = appendGeneratedIfAsync.mock.calls.length;
2321
2613
  clock.advance(10_000);
2322
2614
  await Promise.resolve();
2323
- expect(appendIfAsync).toHaveBeenCalledTimes(conditionalAppendsAfterNewerSuccess);
2615
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(appendsAfterNewerSuccess);
2324
2616
  const afterRestart = [];
2325
2617
  await makeEmitter({
2326
2618
  published: afterRestart,
@@ -2329,16 +2621,17 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2329
2621
  expect(afterRestart).toEqual([]);
2330
2622
  }
2331
2623
  finally {
2332
- appendIfAsync.mockRestore();
2333
- appendAsync.mockRestore();
2624
+ appendGeneratedIfAsync.mockRestore();
2334
2625
  }
2335
2626
  });
2336
2627
  it("drops permanent durable-state persistence errors without scheduling retries", async () => {
2337
2628
  const clock = new FakeClock();
2338
2629
  const onError = vi.fn();
2339
- const appendAsync = vi.spyOn(StateStore.prototype, "appendAsync")
2340
- .mockResolvedValueOnce({});
2341
- const appendIfAsync = vi.spyOn(StateStore.prototype, "appendIfAsync")
2630
+ const originalAppendGeneratedIfAsync = StateStore.prototype.appendGeneratedIfAsync;
2631
+ const appendGeneratedIfAsync = vi.spyOn(StateStore.prototype, "appendGeneratedIfAsync")
2632
+ .mockImplementationOnce(function (type, preparePayload, flags) {
2633
+ return originalAppendGeneratedIfAsync.call(this, type, preparePayload, flags);
2634
+ })
2342
2635
  .mockRejectedValue(new Error("EACCES: state directory is unwritable"));
2343
2636
  const changed = path.join(dir, "permanent-state-error.md");
2344
2637
  fs.writeFileSync(changed, "cannot persist\n");
@@ -2351,15 +2644,14 @@ describe("PushEventEmitter — directory and delete tombstone handling", () => {
2351
2644
  expect(onError).toHaveBeenCalledTimes(1);
2352
2645
  expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("EACCES") }), { relativePath: "permanent-state-error.md" });
2353
2646
  expect(clock.pendingTimerCount()).toBe(0);
2354
- const conditionalAppendsAfterFailure = appendIfAsync.mock.calls.length;
2647
+ const appendsAfterFailure = appendGeneratedIfAsync.mock.calls.length;
2355
2648
  clock.advance(60_000);
2356
2649
  await Promise.resolve();
2357
- expect(appendIfAsync).toHaveBeenCalledTimes(conditionalAppendsAfterFailure);
2650
+ expect(appendGeneratedIfAsync).toHaveBeenCalledTimes(appendsAfterFailure);
2358
2651
  expect(onError).toHaveBeenCalledTimes(1);
2359
2652
  }
2360
2653
  finally {
2361
- appendIfAsync.mockRestore();
2362
- appendAsync.mockRestore();
2654
+ appendGeneratedIfAsync.mockRestore();
2363
2655
  }
2364
2656
  });
2365
2657
  it("emits push telemetry only when publication fails", async () => {