@indigoai-us/hq-cloud 5.23.0 → 5.24.0

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.
@@ -23,7 +23,7 @@ vi.mock("../s3.js", () => ({
23
23
  headRemoteFile: vi.fn().mockResolvedValue(null),
24
24
  }));
25
25
 
26
- import { share } from "./share.js";
26
+ import { share, _testing as shareTesting } from "./share.js";
27
27
  import { deleteRemoteFile, headRemoteFile, uploadFile, uploadSymlink } from "../s3.js";
28
28
  import type { EntityContext } from "../types.js";
29
29
 
@@ -915,6 +915,10 @@ describe("share", () => {
915
915
  hqRoot: tmpDir,
916
916
  skipUnchanged: true,
917
917
  propagateDeletes: true,
918
+ // Pin owned-only — this test predates the currency-gated default and
919
+ // asserts the direction-of-origin branch. A separate currency-gated
920
+ // test covers the new default semantics for the same scenario.
921
+ propagateDeletePolicy: "owned-only",
918
922
  });
919
923
 
920
924
  expect(result.filesDeleted).toBe(1);
@@ -965,6 +969,9 @@ describe("share", () => {
965
969
  hqRoot: tmpDir,
966
970
  skipUnchanged: true,
967
971
  propagateDeletes: true,
972
+ // Owned-only — predates currency-gated default; asserts the lstat
973
+ // guard which is independent of the policy branch.
974
+ propagateDeletePolicy: "owned-only",
968
975
  });
969
976
 
970
977
  expect(result.filesDeleted).toBe(0);
@@ -1013,6 +1020,8 @@ describe("share", () => {
1013
1020
  hqRoot: tmpDir,
1014
1021
  skipUnchanged: true,
1015
1022
  propagateDeletes: true,
1023
+ // Owned-only — currency-gated semantics covered separately below.
1024
+ propagateDeletePolicy: "owned-only",
1016
1025
  });
1017
1026
 
1018
1027
  expect(result.filesDeleted).toBe(1);
@@ -1054,6 +1063,9 @@ describe("share", () => {
1054
1063
  hqRoot: tmpDir,
1055
1064
  skipUnchanged: true,
1056
1065
  propagateDeletes: true,
1066
+ // Owned-only — currency-gated emits a separate event variant; this test
1067
+ // pins the legacy progress-with-deleted-flag shape.
1068
+ propagateDeletePolicy: "owned-only",
1057
1069
  onEvent: (e) => events.push(e as { type: string }),
1058
1070
  });
1059
1071
 
@@ -1143,6 +1155,9 @@ describe("share", () => {
1143
1155
  hqRoot: tmpDir,
1144
1156
  skipUnchanged: true,
1145
1157
  propagateDeletes: true,
1158
+ // Owned-only — this test asserts scope containment, independent of
1159
+ // the policy branch. Currency-gated covered separately.
1160
+ propagateDeletePolicy: "owned-only",
1146
1161
  });
1147
1162
 
1148
1163
  expect(result.filesDeleted).toBe(1);
@@ -1190,6 +1205,9 @@ describe("share", () => {
1190
1205
  hqRoot: tmpDir,
1191
1206
  skipUnchanged: true,
1192
1207
  propagateDeletes: true,
1208
+ // Owned-only — pinning so the retry-survival assertion is independent
1209
+ // of currency-gated's HEAD-driven bucketing.
1210
+ propagateDeletePolicy: "owned-only",
1193
1211
  onEvent: (e) => events.push(e as { type: string }),
1194
1212
  });
1195
1213
 
@@ -1227,7 +1245,7 @@ describe("share", () => {
1227
1245
  // (i) `direction === "up"` requirement under the default policy.
1228
1246
  // (ii) `shouldSync` must accept the key — same filter the pull uses.
1229
1247
 
1230
- it("propagateDeletes: under owned-only (default), skips direction:'down' entries", async () => {
1248
+ it("propagateDeletes: under owned-only (legacy pre-5.24 default), skips direction:'down' entries", async () => {
1231
1249
  const companyRoot = path.join(tmpDir, "companies", "acme");
1232
1250
  fs.mkdirSync(companyRoot, { recursive: true });
1233
1251
  // No local files. One journal entry pulled from elsewhere (direction:'down')
@@ -1259,7 +1277,8 @@ describe("share", () => {
1259
1277
  hqRoot: tmpDir,
1260
1278
  skipUnchanged: true,
1261
1279
  propagateDeletes: true,
1262
- // propagateDeletePolicy omitted defaults to "owned-only".
1280
+ // Explicit opt-in `currency-gated` is the new (5.24+) default.
1281
+ propagateDeletePolicy: "owned-only",
1263
1282
  });
1264
1283
 
1265
1284
  // Only the 'up' entry is deleted; the 'down' entry is left alone so a
@@ -1338,6 +1357,10 @@ describe("share", () => {
1338
1357
  hqRoot: tmpDir,
1339
1358
  skipUnchanged: true,
1340
1359
  propagateDeletes: true,
1360
+ // Owned-only — filter symmetry is policy-independent; pinning so the
1361
+ // legacy direction-of-origin gate (not currency-gated's HEAD path)
1362
+ // resolves the "delete vs skip" decision for active/current.md.
1363
+ propagateDeletePolicy: "owned-only",
1341
1364
  });
1342
1365
 
1343
1366
  // legacy/old-layout.md is filter-skipped; only active/current.md is
@@ -1347,6 +1370,420 @@ describe("share", () => {
1347
1370
  expect(deleteRemoteFile).not.toHaveBeenCalledWith(expect.anything(), "legacy/old-layout.md");
1348
1371
  });
1349
1372
 
1373
+ // ── Delete propagation: currency-gated policy (5.24+ default) ──────────────
1374
+ //
1375
+ // The pre-5.24 `owned-only` default refused to delete-propagate any journal
1376
+ // entry whose `direction !== "up"`. That made sense as a safety net against
1377
+ // a behind machine's first push erasing peer uploads — but it had a worse
1378
+ // failure mode in practice: every `/update-hq` writes `core/`, `.claude/`,
1379
+ // `.codex/` with `direction: "down"` (they came from upstream), so any
1380
+ // subsequent local delete during a cleanup or upgrade was silently dropped.
1381
+ // Net effect: remote vault accumulated permanent litter the user could not
1382
+ // clean up without manually invoking `policy: "all"` (which has its own
1383
+ // safety problems).
1384
+ //
1385
+ // `currency-gated` solves this by gating on per-file proof of currency: HEAD
1386
+ // the remote, compare ETag, only propagate when this device has the
1387
+ // latest version. Direction-of-origin becomes irrelevant — a file the
1388
+ // upstream `/update-hq` wrote can be cleanly deleted by the device that
1389
+ // wrote it, as long as no other device modified it in the meantime.
1390
+
1391
+ it("currency-gated: propagates delete when remote ETag matches journal", async () => {
1392
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1393
+ fs.mkdirSync(companyRoot, { recursive: true });
1394
+ // direction:"down" file (e.g. arrived via /update-hq), locally deleted.
1395
+ // Under owned-only this would be stuck on remote forever; currency-gated
1396
+ // checks the ETag and propagates the delete cleanly when match holds.
1397
+ const journalPath = path.join(stateDir, "sync-journal.acme.json");
1398
+ fs.writeFileSync(
1399
+ journalPath,
1400
+ JSON.stringify({
1401
+ version: "1",
1402
+ lastSync: new Date().toISOString(),
1403
+ files: {
1404
+ "core/policies/old.md": {
1405
+ hash: "h", size: 100, syncedAt: new Date().toISOString(),
1406
+ direction: "down",
1407
+ remoteEtag: "abc123",
1408
+ },
1409
+ },
1410
+ }),
1411
+ );
1412
+
1413
+ // HEAD returns the same etag — this device is current for the file.
1414
+ vi.mocked(headRemoteFile).mockResolvedValueOnce({
1415
+ lastModified: new Date(),
1416
+ etag: '"abc123"',
1417
+ size: 100,
1418
+ });
1419
+
1420
+ const result = await share({
1421
+ paths: [companyRoot],
1422
+ company: "acme",
1423
+ vaultConfig: mockConfig,
1424
+ hqRoot: tmpDir,
1425
+ skipUnchanged: true,
1426
+ propagateDeletes: true,
1427
+ propagateDeletePolicy: "currency-gated",
1428
+ });
1429
+
1430
+ expect(result.filesDeleted).toBe(1);
1431
+ expect(deleteRemoteFile).toHaveBeenCalledWith(
1432
+ expect.anything(),
1433
+ "core/policies/old.md",
1434
+ );
1435
+ const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
1436
+ expect(journal.files["core/policies/old.md"]).toBeUndefined();
1437
+ });
1438
+
1439
+ it("currency-gated: refuses delete + emits stale-etag event when remote moved since last sync", async () => {
1440
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1441
+ fs.mkdirSync(companyRoot, { recursive: true });
1442
+ // Another device modified `shared.md` after this device's last sync.
1443
+ // The journal still records the old etag; HEAD returns the new one.
1444
+ const journalPath = path.join(stateDir, "sync-journal.acme.json");
1445
+ fs.writeFileSync(
1446
+ journalPath,
1447
+ JSON.stringify({
1448
+ version: "1",
1449
+ lastSync: new Date().toISOString(),
1450
+ files: {
1451
+ "shared.md": {
1452
+ hash: "h", size: 50, syncedAt: new Date().toISOString(),
1453
+ direction: "down",
1454
+ remoteEtag: "stale-etag",
1455
+ },
1456
+ },
1457
+ }),
1458
+ );
1459
+
1460
+ vi.mocked(headRemoteFile).mockResolvedValueOnce({
1461
+ lastModified: new Date(),
1462
+ etag: '"fresh-etag"',
1463
+ size: 51,
1464
+ });
1465
+
1466
+ const events: Array<{ type: string; path?: string; journalEtag?: string; remoteEtag?: string }> = [];
1467
+ const result = await share({
1468
+ paths: [companyRoot],
1469
+ company: "acme",
1470
+ vaultConfig: mockConfig,
1471
+ hqRoot: tmpDir,
1472
+ skipUnchanged: true,
1473
+ propagateDeletes: true,
1474
+ propagateDeletePolicy: "currency-gated",
1475
+ onEvent: (e) => events.push(e as { type: string }),
1476
+ });
1477
+
1478
+ // No remote DELETE issued, journal entry preserved (pull will re-pull).
1479
+ expect(result.filesDeleted).toBe(0);
1480
+ expect(deleteRemoteFile).not.toHaveBeenCalled();
1481
+ const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
1482
+ expect(journal.files["shared.md"]).toBeDefined();
1483
+
1484
+ // Dedicated event fired so UIs surface the refusal. `reason` field
1485
+ // discriminates "stale-etag" (real drift) from "legacy-no-etag" so
1486
+ // consumers don't have to string-compare placeholder etag values.
1487
+ const refusedEvent = events.find((e) => e.type === "delete-refused-stale-etag");
1488
+ expect(refusedEvent).toMatchObject({
1489
+ type: "delete-refused-stale-etag",
1490
+ path: "shared.md",
1491
+ journalEtag: "stale-etag",
1492
+ remoteEtag: "fresh-etag",
1493
+ reason: "stale-etag",
1494
+ });
1495
+ // Counter exposed on ShareResult so callers don't need to re-count events.
1496
+ expect(result.filesRefusedStale).toBe(1);
1497
+ expect(result.filesTombstoned).toBe(0);
1498
+ });
1499
+
1500
+ it("currency-gated: tombstones journal entry when remote returns 404 (out-of-band cleanup)", async () => {
1501
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1502
+ fs.mkdirSync(companyRoot, { recursive: true });
1503
+ // Remote file was deleted out-of-band (e.g. someone removed it via the
1504
+ // S3 console). Local copy also missing. Currency-gated should drop the
1505
+ // journal entry without issuing a DELETE — the remote is already gone.
1506
+ const journalPath = path.join(stateDir, "sync-journal.acme.json");
1507
+ fs.writeFileSync(
1508
+ journalPath,
1509
+ JSON.stringify({
1510
+ version: "1",
1511
+ lastSync: new Date().toISOString(),
1512
+ files: {
1513
+ "removed-out-of-band.md": {
1514
+ hash: "h", size: 25, syncedAt: new Date().toISOString(),
1515
+ direction: "down",
1516
+ remoteEtag: "doesnt-matter",
1517
+ },
1518
+ },
1519
+ }),
1520
+ );
1521
+
1522
+ vi.mocked(headRemoteFile).mockResolvedValueOnce(null);
1523
+
1524
+ const events: Array<{ type: string; path?: string; deleted?: boolean; message?: string; bytes?: number }> = [];
1525
+ const result = await share({
1526
+ paths: [companyRoot],
1527
+ company: "acme",
1528
+ vaultConfig: mockConfig,
1529
+ hqRoot: tmpDir,
1530
+ skipUnchanged: true,
1531
+ propagateDeletes: true,
1532
+ propagateDeletePolicy: "currency-gated",
1533
+ onEvent: (e) => events.push(e as { type: string }),
1534
+ });
1535
+
1536
+ // No DeleteObject (remote was already gone), but journal converges.
1537
+ expect(deleteRemoteFile).not.toHaveBeenCalled();
1538
+ // filesDeleted counts actual S3 deletes; tombstones aren't counted here.
1539
+ expect(result.filesDeleted).toBe(0);
1540
+ // Tombstones have their own counter so callers can distinguish them
1541
+ // from real deletes without re-counting events.
1542
+ expect(result.filesTombstoned).toBe(1);
1543
+ expect(result.filesRefusedStale).toBe(0);
1544
+ const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
1545
+ expect(journal.files["removed-out-of-band.md"]).toBeUndefined();
1546
+ // Synthetic progress event carries the tombstone marker. Without this,
1547
+ // the tty stream renders tombstones identically to real deletes — operator
1548
+ // can't tell from logs alone that no S3 call was issued.
1549
+ const tombstoneEvent = events.find(
1550
+ (e) => e.type === "progress" && e.deleted === true && e.message?.includes("tombstone"),
1551
+ );
1552
+ expect(tombstoneEvent).toMatchObject({
1553
+ type: "progress",
1554
+ path: "removed-out-of-band.md",
1555
+ bytes: 0,
1556
+ deleted: true,
1557
+ message: expect.stringContaining("tombstone"),
1558
+ });
1559
+ });
1560
+
1561
+ it("currency-gated: refuses delete for legacy journal entry with no recorded remoteEtag", async () => {
1562
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1563
+ fs.mkdirSync(companyRoot, { recursive: true });
1564
+ // Journal entry from before remoteEtag tracking — no etag to compare
1565
+ // against. Refuse the delete in the safe direction; a future sync with
1566
+ // a recorded etag can re-evaluate.
1567
+ const journalPath = path.join(stateDir, "sync-journal.acme.json");
1568
+ fs.writeFileSync(
1569
+ journalPath,
1570
+ JSON.stringify({
1571
+ version: "1",
1572
+ lastSync: new Date().toISOString(),
1573
+ files: {
1574
+ "legacy-no-etag.md": {
1575
+ hash: "h", size: 5, syncedAt: new Date().toISOString(),
1576
+ direction: "down",
1577
+ // No remoteEtag.
1578
+ },
1579
+ },
1580
+ }),
1581
+ );
1582
+
1583
+ const events: Array<{ type: string; journalEtag?: string }> = [];
1584
+ const result = await share({
1585
+ paths: [companyRoot],
1586
+ company: "acme",
1587
+ vaultConfig: mockConfig,
1588
+ hqRoot: tmpDir,
1589
+ skipUnchanged: true,
1590
+ propagateDeletes: true,
1591
+ propagateDeletePolicy: "currency-gated",
1592
+ onEvent: (e) => events.push(e as { type: string }),
1593
+ });
1594
+
1595
+ expect(result.filesDeleted).toBe(0);
1596
+ expect(deleteRemoteFile).not.toHaveBeenCalled();
1597
+ // HEAD must NOT be called — we short-circuit on the missing journal etag.
1598
+ expect(headRemoteFile).not.toHaveBeenCalled();
1599
+ const refused = events.find((e) => e.type === "delete-refused-stale-etag");
1600
+ expect(refused).toMatchObject({
1601
+ journalEtag: "<legacy-no-etag>",
1602
+ reason: "legacy-no-etag",
1603
+ });
1604
+ expect(result.filesRefusedStale).toBe(1);
1605
+ });
1606
+
1607
+ it("currency-gated: real-world /update-hq scenario — direction:'down' delete propagates when current", async () => {
1608
+ // Regression for the exact bug that motivated the 5.24 default flip:
1609
+ // every `/update-hq` writes core/.claude/.codex from upstream and
1610
+ // journals them as direction:"down". When the user later moves or
1611
+ // deletes one locally (e.g. during cleanup, dir-restructure, or the
1612
+ // next upgrade), pre-5.24 `owned-only` silently kept it on remote
1613
+ // forever. Currency-gated propagates the delete cleanly as long as
1614
+ // this device is current for the file.
1615
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1616
+ fs.mkdirSync(companyRoot, { recursive: true });
1617
+ const journalPath = path.join(stateDir, "sync-journal.acme.json");
1618
+ fs.writeFileSync(
1619
+ journalPath,
1620
+ JSON.stringify({
1621
+ version: "1",
1622
+ lastSync: new Date().toISOString(),
1623
+ files: {
1624
+ ".claude/commands/retired-command.md": {
1625
+ hash: "h", size: 200, syncedAt: new Date().toISOString(),
1626
+ direction: "down",
1627
+ remoteEtag: "upstream-etag",
1628
+ },
1629
+ },
1630
+ }),
1631
+ );
1632
+
1633
+ vi.mocked(headRemoteFile).mockResolvedValueOnce({
1634
+ lastModified: new Date(),
1635
+ etag: '"upstream-etag"',
1636
+ size: 200,
1637
+ });
1638
+
1639
+ const result = await share({
1640
+ paths: [companyRoot],
1641
+ company: "acme",
1642
+ vaultConfig: mockConfig,
1643
+ hqRoot: tmpDir,
1644
+ skipUnchanged: true,
1645
+ propagateDeletes: true,
1646
+ // Explicit opt-in. 5.24 ships the currency-gated CODE PATH but keeps
1647
+ // `owned-only` as the default while it soaks; the default flips to
1648
+ // `currency-gated` in 5.25. This test pins the user-facing semantics
1649
+ // (the /update-hq delete-propagation bug) under the new policy
1650
+ // regardless of which default the surrounding release ships.
1651
+ propagateDeletePolicy: "currency-gated",
1652
+ });
1653
+
1654
+ expect(result.filesDeleted).toBe(1);
1655
+ expect(deleteRemoteFile).toHaveBeenCalledWith(
1656
+ expect.anything(),
1657
+ ".claude/commands/retired-command.md",
1658
+ );
1659
+ });
1660
+
1661
+ // ── Conflict-mirror exclusion (ephemeral pattern) ──────────────────────────
1662
+ //
1663
+ // Conflict mirrors (`*.conflict-<ISO>-<machineHash>.<ext>`) are local-only
1664
+ // safety backups written by the pull leg whenever a 3-way merge keeps
1665
+ // local and wants to preserve the remote version for inspection. They
1666
+ // MUST never round-trip to S3. Pre-fix, the push walker uploaded them,
1667
+ // the journal tracked them, and the owned-only delete policy then refused
1668
+ // to clean them up — permanent ratchet of remote litter.
1669
+
1670
+ it("conflict-mirror exclusion: push walker skips local conflict-mirror files", async () => {
1671
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1672
+ fs.mkdirSync(path.join(companyRoot, "skills"), { recursive: true });
1673
+ // Two files: a normal one and a conflict mirror. Only the normal one
1674
+ // should reach S3; the mirror is local-only state.
1675
+ fs.writeFileSync(
1676
+ path.join(companyRoot, "skills", "real.md"),
1677
+ "real content",
1678
+ );
1679
+ fs.writeFileSync(
1680
+ path.join(companyRoot, "skills", "real.md.conflict-2026-05-13T19-40-40Z-e5797a.md"),
1681
+ "conflict mirror content",
1682
+ );
1683
+
1684
+ await share({
1685
+ paths: [companyRoot],
1686
+ company: "acme",
1687
+ vaultConfig: mockConfig,
1688
+ hqRoot: tmpDir,
1689
+ skipUnchanged: true,
1690
+ });
1691
+
1692
+ expect(uploadFile).toHaveBeenCalledTimes(1);
1693
+ expect(uploadFile).toHaveBeenCalledWith(
1694
+ expect.anything(),
1695
+ expect.stringContaining("real.md"),
1696
+ "skills/real.md",
1697
+ );
1698
+ // Spy was called once total — the conflict mirror never reaches uploadFile.
1699
+ // (Asserted by count above; the explicit non-call below is defensive.)
1700
+ const calls = vi.mocked(uploadFile).mock.calls;
1701
+ expect(calls.every((c) => !String(c[1] ?? "").includes("conflict-"))).toBe(true);
1702
+ });
1703
+
1704
+ it("conflict-mirror exclusion: explicit user-supplied conflict-mirror path is also refused", async () => {
1705
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1706
+ fs.mkdirSync(companyRoot, { recursive: true });
1707
+ const mirrorPath = path.join(
1708
+ companyRoot,
1709
+ "CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md",
1710
+ );
1711
+ fs.writeFileSync(mirrorPath, "mirror content");
1712
+
1713
+ await share({
1714
+ paths: [mirrorPath],
1715
+ company: "acme",
1716
+ vaultConfig: mockConfig,
1717
+ hqRoot: tmpDir,
1718
+ skipUnchanged: true,
1719
+ });
1720
+
1721
+ // Explicit caller path matching the ephemeral pattern is filtered the
1722
+ // same as a walker-discovered one. Belt-and-suspenders against any
1723
+ // tooling that hands a conflict mirror to share() directly.
1724
+ expect(uploadFile).not.toHaveBeenCalled();
1725
+ });
1726
+
1727
+ it("conflict-mirror exclusion: journaled mirror with local-missing is NOT swept by delete plan", async () => {
1728
+ const companyRoot = path.join(tmpDir, "companies", "acme");
1729
+ fs.mkdirSync(companyRoot, { recursive: true });
1730
+ // Simulate the existing-litter state: a conflict mirror that leaked
1731
+ // into the journal in a prior buggy version. Locally missing (user
1732
+ // already deleted it). The regular delete plan must NOT issue a
1733
+ // DeleteObject — that's the dedicated reconcile command's job, and
1734
+ // a sync should not accidentally race a user reviewing the mirror.
1735
+ const journalPath = path.join(stateDir, "sync-journal.acme.json");
1736
+ fs.writeFileSync(
1737
+ journalPath,
1738
+ JSON.stringify({
1739
+ version: "1",
1740
+ lastSync: new Date().toISOString(),
1741
+ files: {
1742
+ "CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md": {
1743
+ hash: "h", size: 10, syncedAt: new Date().toISOString(),
1744
+ direction: "up",
1745
+ remoteEtag: "mirror-etag",
1746
+ },
1747
+ "regular.md": {
1748
+ hash: "h", size: 5, syncedAt: new Date().toISOString(),
1749
+ direction: "up",
1750
+ remoteEtag: "regular-etag",
1751
+ },
1752
+ },
1753
+ }),
1754
+ );
1755
+
1756
+ // HEAD needed for the non-mirror entry under currency-gated.
1757
+ vi.mocked(headRemoteFile).mockResolvedValue({
1758
+ lastModified: new Date(),
1759
+ etag: '"regular-etag"',
1760
+ size: 5,
1761
+ });
1762
+
1763
+ const result = await share({
1764
+ paths: [companyRoot],
1765
+ company: "acme",
1766
+ vaultConfig: mockConfig,
1767
+ hqRoot: tmpDir,
1768
+ skipUnchanged: true,
1769
+ propagateDeletes: true,
1770
+ });
1771
+
1772
+ expect(result.filesDeleted).toBe(1);
1773
+ expect(deleteRemoteFile).toHaveBeenCalledTimes(1);
1774
+ expect(deleteRemoteFile).toHaveBeenCalledWith(expect.anything(), "regular.md");
1775
+ expect(deleteRemoteFile).not.toHaveBeenCalledWith(
1776
+ expect.anything(),
1777
+ expect.stringContaining("conflict-"),
1778
+ );
1779
+ // Mirror's journal entry survives — reconcile command (separate skill)
1780
+ // sweeps it once the user explicitly opts in.
1781
+ const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
1782
+ expect(
1783
+ journal.files["CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md"],
1784
+ ).toBeDefined();
1785
+ });
1786
+
1350
1787
  // ── personalMode ───────────────────────────────────────────────────────────
1351
1788
  //
1352
1789
  // The personal vault (slug "personal" in the runner's fanout plan) shares
@@ -1748,3 +2185,140 @@ describe("share", () => {
1748
2185
  });
1749
2186
  });
1750
2187
  });
2188
+
2189
+ // ── Pure-function unit coverage: isEphemeralPath ─────────────────────────────
2190
+ //
2191
+ // EPHEMERAL_PATH_PATTERN is the single source of truth for "this is a conflict
2192
+ // mirror and must never round-trip to S3." Integration tests already cover the
2193
+ // behavior end-to-end (uploadFile is or isn't called), but a direct regex
2194
+ // contract test makes intent unambiguous and catches future drift in the
2195
+ // pattern — much cheaper than reproducing each case through share().
2196
+
2197
+ describe("isEphemeralPath (conflict-mirror pattern contract)", () => {
2198
+ const { isEphemeralPath } = shareTesting;
2199
+
2200
+ it.each([
2201
+ // Canonical: basename and relativeKey both match (the two callsites).
2202
+ [".claude/CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md", true],
2203
+ ["CLAUDE.md.conflict-2026-05-13T19-40-40Z-e5797a.md", true],
2204
+ [".claude/commands/adr.md.conflict-2026-05-13T19-40-41Z-e5797a.md", true],
2205
+ // Longer machine hash (no upper bound on `[a-f0-9]+`).
2206
+ ["foo.conflict-2026-05-19T17-05-56Z-deadbeef.md", true],
2207
+ // Non-markdown extensions also valid (sh scripts, ts files, etc.).
2208
+ ["foo.sh.conflict-2026-05-19T17-05-56Z-abc123.sh", true],
2209
+ ])("matches conflict mirror: %s", (p, expected) => {
2210
+ expect(isEphemeralPath(p)).toBe(expected);
2211
+ });
2212
+
2213
+ it.each([
2214
+ // Normal files: regular markdown, scripts, etc.
2215
+ ["README.md", false],
2216
+ [".claude/CLAUDE.md", false],
2217
+ ["companies/acme/knowledge/note.md", false],
2218
+ // Strings containing the word "conflict" but not the timestamp+hash token.
2219
+ ["conflict-resolution.md", false],
2220
+ ["my-conflict.md", false],
2221
+ ["foo.conflict-handler.md", false],
2222
+ // Date-shaped but missing the trailing dot + extension (real conflicts
2223
+ // always carry a file extension; the trailing `\.` in the pattern is the
2224
+ // safety against bare-substring false positives).
2225
+ ["foo.conflict-2026-05-13T19-40-40Z-abc", false],
2226
+ // Wrong-case or non-hex machine hash.
2227
+ ["foo.conflict-2026-05-13T19-40-40Z-ZZZZZZ.md", false],
2228
+ // Wrong timestamp format (real conflicts use UTC ISO with Z suffix).
2229
+ ["foo.conflict-2026-05-13-abc123.md", false],
2230
+ // Missing leading dot before "conflict" (this protects against legitimate
2231
+ // files that happen to contain the word "conflict" mid-name).
2232
+ ["fooconflict-2026-05-13T19-40-40Z-abc.md", false],
2233
+ ])("rejects non-mirror: %s", (p, expected) => {
2234
+ expect(isEphemeralPath(p)).toBe(expected);
2235
+ });
2236
+ });
2237
+
2238
+ // ── Currency-gated coverage against journal version "2" fixtures ─────────────
2239
+ //
2240
+ // Production journals on disk are at `version: "2"` (verified via jq on the
2241
+ // real-world ~/.hq/sync-journal.personal.json). The currency-gated tests above
2242
+ // use v1 fixtures by historical convention — this block pins the behavior at
2243
+ // v2 explicitly so a future schema change can't silently strand the new policy
2244
+ // on a stale fixture format. The bucket logic ignores `version` (only reads
2245
+ // `journal.files[*]`), so this should pass identically — but we want the
2246
+ // regression test on record.
2247
+
2248
+ describe("currency-gated: journal version 2 fixtures", () => {
2249
+ let tmpDir: string;
2250
+ let stateDir: string;
2251
+ let origDataHome: string | undefined;
2252
+
2253
+ beforeEach(() => {
2254
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-share-test-v2-"));
2255
+ stateDir = path.join(tmpDir, ".hq");
2256
+ fs.mkdirSync(stateDir, { recursive: true });
2257
+ origDataHome = process.env.XDG_DATA_HOME;
2258
+ process.env.XDG_DATA_HOME = stateDir;
2259
+ process.env.HQ_STATE_DIR = stateDir;
2260
+ clearContextCache();
2261
+ setupFetchMock();
2262
+ vi.mocked(uploadFile).mockClear();
2263
+ vi.mocked(uploadSymlink).mockClear();
2264
+ vi.mocked(deleteRemoteFile).mockClear();
2265
+ vi.mocked(headRemoteFile).mockReset();
2266
+ vi.mocked(headRemoteFile).mockResolvedValue(null);
2267
+ });
2268
+
2269
+ afterEach(() => {
2270
+ if (origDataHome === undefined) {
2271
+ delete process.env.XDG_DATA_HOME;
2272
+ } else {
2273
+ process.env.XDG_DATA_HOME = origDataHome;
2274
+ }
2275
+ delete process.env.HQ_STATE_DIR;
2276
+ fs.rmSync(tmpDir, { recursive: true, force: true });
2277
+ });
2278
+
2279
+ it("propagates delete on etag match against a v2-shaped journal", async () => {
2280
+ const companyRoot = path.join(tmpDir, "companies", "acme");
2281
+ fs.mkdirSync(companyRoot, { recursive: true });
2282
+ const journalPath = path.join(stateDir, "sync-journal.acme.json");
2283
+ // v2-shaped fixture — same field set the production journal uses.
2284
+ fs.writeFileSync(
2285
+ journalPath,
2286
+ JSON.stringify({
2287
+ version: "2",
2288
+ lastSync: new Date().toISOString(),
2289
+ files: {
2290
+ "core/policies/old.md": {
2291
+ hash: "h",
2292
+ size: 100,
2293
+ syncedAt: new Date().toISOString(),
2294
+ direction: "down",
2295
+ remoteEtag: "v2-etag",
2296
+ },
2297
+ },
2298
+ pulls: [],
2299
+ }),
2300
+ );
2301
+
2302
+ vi.mocked(headRemoteFile).mockResolvedValueOnce({
2303
+ lastModified: new Date(),
2304
+ etag: '"v2-etag"',
2305
+ size: 100,
2306
+ });
2307
+
2308
+ const result = await share({
2309
+ paths: [companyRoot],
2310
+ company: "acme",
2311
+ vaultConfig: mockConfig,
2312
+ hqRoot: tmpDir,
2313
+ skipUnchanged: true,
2314
+ propagateDeletes: true,
2315
+ propagateDeletePolicy: "currency-gated",
2316
+ });
2317
+
2318
+ expect(result.filesDeleted).toBe(1);
2319
+ expect(deleteRemoteFile).toHaveBeenCalledWith(
2320
+ expect.anything(),
2321
+ "core/policies/old.md",
2322
+ );
2323
+ });
2324
+ });