@indigoai-us/hq-cloud 6.16.27 → 6.16.29

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 (62) hide show
  1. package/dist/cli/conflict-winner.test.d.ts +7 -0
  2. package/dist/cli/conflict-winner.test.d.ts.map +1 -0
  3. package/dist/cli/conflict-winner.test.js +98 -0
  4. package/dist/cli/conflict-winner.test.js.map +1 -0
  5. package/dist/cli/conflict.d.ts +72 -0
  6. package/dist/cli/conflict.d.ts.map +1 -1
  7. package/dist/cli/conflict.js +107 -0
  8. package/dist/cli/conflict.js.map +1 -1
  9. package/dist/cli/doctor.d.ts +12 -0
  10. package/dist/cli/doctor.d.ts.map +1 -1
  11. package/dist/cli/doctor.js +29 -0
  12. package/dist/cli/doctor.js.map +1 -1
  13. package/dist/cli/doctor.test.js +55 -0
  14. package/dist/cli/doctor.test.js.map +1 -1
  15. package/dist/cli/index.d.ts +4 -2
  16. package/dist/cli/index.d.ts.map +1 -1
  17. package/dist/cli/index.js +4 -1
  18. package/dist/cli/index.js.map +1 -1
  19. package/dist/cli/share.d.ts.map +1 -1
  20. package/dist/cli/share.js +364 -101
  21. package/dist/cli/share.js.map +1 -1
  22. package/dist/cli/share.test.js +85 -15
  23. package/dist/cli/share.test.js.map +1 -1
  24. package/dist/cli/sync.d.ts +3 -1
  25. package/dist/cli/sync.d.ts.map +1 -1
  26. package/dist/cli/sync.js +315 -276
  27. package/dist/cli/sync.js.map +1 -1
  28. package/dist/cli/sync.test.js +109 -47
  29. package/dist/cli/sync.test.js.map +1 -1
  30. package/dist/journal-store.test.js +59 -0
  31. package/dist/journal-store.test.js.map +1 -1
  32. package/dist/journal.d.ts +8 -0
  33. package/dist/journal.d.ts.map +1 -1
  34. package/dist/journal.js +19 -5
  35. package/dist/journal.js.map +1 -1
  36. package/dist/lib/conflict-file.d.ts +50 -18
  37. package/dist/lib/conflict-file.d.ts.map +1 -1
  38. package/dist/lib/conflict-file.js +63 -34
  39. package/dist/lib/conflict-file.js.map +1 -1
  40. package/dist/lib/conflict-index.d.ts.map +1 -1
  41. package/dist/lib/conflict-index.js +6 -0
  42. package/dist/lib/conflict-index.js.map +1 -1
  43. package/dist/lib/conflict-reconcile.d.ts +106 -0
  44. package/dist/lib/conflict-reconcile.d.ts.map +1 -0
  45. package/dist/lib/conflict-reconcile.js +412 -0
  46. package/dist/lib/conflict-reconcile.js.map +1 -0
  47. package/dist/lib/conflict-reconcile.test.d.ts +7 -0
  48. package/dist/lib/conflict-reconcile.test.d.ts.map +1 -0
  49. package/dist/lib/conflict-reconcile.test.js +246 -0
  50. package/dist/lib/conflict-reconcile.test.js.map +1 -0
  51. package/dist/lib/conflict.test.d.ts +3 -2
  52. package/dist/lib/conflict.test.d.ts.map +1 -1
  53. package/dist/lib/conflict.test.js +60 -3
  54. package/dist/lib/conflict.test.js.map +1 -1
  55. package/dist/sync/area-ledger.d.ts.map +1 -1
  56. package/dist/sync/area-ledger.js +50 -7
  57. package/dist/sync/area-ledger.js.map +1 -1
  58. package/dist/sync/area-ledger.test.js +90 -0
  59. package/dist/sync/area-ledger.test.js.map +1 -1
  60. package/dist/types.d.ts +23 -5
  61. package/dist/types.d.ts.map +1 -1
  62. package/package.json +1 -1
@@ -161,9 +161,22 @@ function journalAt(journalPath) {
161
161
  * test reaches for the preserved side.
162
162
  */
163
163
  function sidecarIn(dir) {
164
- const found = fs.readdirSync(dir).filter((f) => f.includes(".conflict-"));
164
+ // Loser bodies are parked OUT of the tree under
165
+ // `<hqRoot>/.hq/conflict-backups/<relative dir>/`; walk up from `dir` to the
166
+ // root that owns the backup dir. A sibling `.conflict-` twin beside the live
167
+ // file is never acceptable any more.
168
+ let root = dir;
169
+ while (!fs.existsSync(path.join(root, ".hq", "conflict-backups"))) {
170
+ const parent = path.dirname(root);
171
+ if (parent === root)
172
+ throw new Error(`no .hq/conflict-backups above ${dir}`);
173
+ root = parent;
174
+ }
175
+ const backupDir = path.join(root, ".hq", "conflict-backups", path.relative(root, dir));
176
+ const found = fs.existsSync(backupDir) ? fs.readdirSync(backupDir) : [];
165
177
  expect(found).toHaveLength(1);
166
- return path.join(dir, found[0]);
178
+ expect(fs.readdirSync(dir).filter((f) => f.includes(".conflict-"))).toEqual([]);
179
+ return path.join(backupDir, found[0]);
167
180
  }
168
181
  const mockConfig = {
169
182
  apiUrl: "https://vault-api.test",
@@ -981,10 +994,15 @@ describe("sync", () => {
981
994
  const result = await sync({ vaultConfig: mockConfig, hqRoot: tmpDir });
982
995
  expect(result.filesDownloaded).toBe(2);
983
996
  });
984
- it("detects conflicts with local changes and adopts the cloud copy on --on-conflict keep", async () => {
997
+ it("detects conflicts with local changes and keeps the NEWER local body live on --on-conflict keep (version-aware)", async () => {
985
998
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
986
999
  fs.mkdirSync(companyDocs, { recursive: true });
987
1000
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1001
+ // The cloud object predates the local edit: with no frontmatter version on
1002
+ // either side the newer mtime decides, so the local body must stay live.
1003
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1004
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() - 60_000), etag: '"older-cloud"' },
1005
+ ]);
988
1006
  fs.writeFileSync(journalPath, JSON.stringify({
989
1007
  version: "1",
990
1008
  lastSync: new Date().toISOString(),
@@ -997,18 +1015,35 @@ describe("sync", () => {
997
1015
  },
998
1016
  },
999
1017
  }));
1018
+ const events = [];
1000
1019
  const result = await sync({
1001
1020
  company: "acme",
1002
1021
  onConflict: "keep",
1003
1022
  vaultConfig: mockConfig,
1004
1023
  hqRoot: tmpDir,
1024
+ onEvent: (e) => events.push(e),
1005
1025
  });
1006
1026
  expect(result.conflicts).toBe(1);
1007
1027
  expect(result.conflictPaths).toEqual(["docs/handoff.md"]);
1008
- // `keep` adopts the cloud body at the original name…
1009
- expect(fs.readFileSync(path.join(companyDocs, "handoff.md"), "utf-8")).toBe("mock file content");
1010
- // …and the local one survives beside it.
1011
- expect(fs.readFileSync(sidecarIn(companyDocs), "utf-8")).toBe("local version");
1028
+ expect(result.filesDownloaded).toBe(0);
1029
+ // The newer local body stays at the original name…
1030
+ expect(fs.readFileSync(path.join(companyDocs, "handoff.md"), "utf-8")).toBe("local version");
1031
+ // …the cloud body is parked out of tree for inspection, and no sibling
1032
+ // twin is written.
1033
+ expect(fs.readFileSync(sidecarIn(companyDocs), "utf-8")).toBe("mock file content");
1034
+ // The decision is surfaced on the event and recorded in the index.
1035
+ const conflictEvent = events.find((e) => e.type === "conflict");
1036
+ expect(conflictEvent?.decision).toMatchObject({ winner: "local", reason: "mtime" });
1037
+ const index = JSON.parse(fs.readFileSync(path.join(tmpDir, ".hq-conflicts", "index.json"), "utf-8"));
1038
+ expect(index.conflicts).toHaveLength(1);
1039
+ expect(index.conflicts[0]).toMatchObject({
1040
+ originalPath: "companies/acme/docs/handoff.md",
1041
+ preserved: "remote",
1042
+ decision: { winner: "local", reason: "mtime" },
1043
+ });
1044
+ // Held with the honesty flag so the push leg publishes it under a fence.
1045
+ const entry = journalAt(journalPath).files["docs/handoff.md"];
1046
+ expect(entry.localDiverges).toBe(true);
1012
1047
  });
1013
1048
  it("rechecks the current local body when executing a planned conflict", async () => {
1014
1049
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
@@ -1086,18 +1121,33 @@ describe("sync", () => {
1086
1121
  const resolveSpy = vi
1087
1122
  .spyOn(conflictModule, "resolveConflict")
1088
1123
  .mockResolvedValue("skip");
1124
+ // An UNSET strategy only prompts when a human is on a TTY; off a TTY it is
1125
+ // the version-aware winner, which never consults the prompt and decides by
1126
+ // mtime instead — so without this the outcome depended on how many seconds
1127
+ // separated the S3 mock's `new Date()` from the local write above. Model the
1128
+ // interactive terminal so the `skip` arm is what runs.
1129
+ const ttyDescriptor = Object.getOwnPropertyDescriptor(process.stdin, "isTTY");
1130
+ Object.defineProperty(process.stdin, "isTTY", { value: true, configurable: true });
1089
1131
  let result;
1132
+ let prompted = 0;
1090
1133
  try {
1091
1134
  result = await sync({
1092
1135
  company: "acme",
1093
1136
  vaultConfig: mockConfig,
1094
1137
  hqRoot: tmpDir,
1095
1138
  });
1139
+ prompted = resolveSpy.mock.calls.length;
1096
1140
  }
1097
1141
  finally {
1142
+ if (ttyDescriptor)
1143
+ Object.defineProperty(process.stdin, "isTTY", ttyDescriptor);
1144
+ else
1145
+ delete process.stdin.isTTY;
1098
1146
  resolveSpy.mockRestore();
1099
1147
  }
1100
1148
  expect(result.conflicts).toBe(1);
1149
+ // The prompt stand-in decided — not a wall-clock mtime comparison.
1150
+ expect(prompted).toBe(1);
1101
1151
  const journal = journalAt(journalPath);
1102
1152
  // remoteEtag still recorded so the conflict can't re-fire (#137 invariant)…
1103
1153
  expect(journal.files["docs/handoff.md"].remoteEtag).toBeTruthy();
@@ -1162,7 +1212,7 @@ describe("sync", () => {
1162
1212
  fs.mkdirSync(companyDocs, { recursive: true });
1163
1213
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1164
1214
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1165
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1215
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1166
1216
  ]);
1167
1217
  fs.writeFileSync(journalPath, JSON.stringify({
1168
1218
  version: "1",
@@ -1186,12 +1236,9 @@ describe("sync", () => {
1186
1236
  expect(result.conflictPaths).toEqual(["docs/handoff.md"]);
1187
1237
  // The cloud body is the working copy now.
1188
1238
  expect(fs.readFileSync(path.join(companyDocs, "handoff.md"), "utf-8")).toBe("mock file content");
1189
- // …and the local body is not lost — it is sitting beside it.
1190
- const sidecars = fs
1191
- .readdirSync(companyDocs)
1192
- .filter((f) => f.includes(".conflict-"));
1193
- expect(sidecars).toHaveLength(1);
1194
- expect(fs.readFileSync(path.join(companyDocs, sidecars[0]), "utf-8")).toBe("local version");
1239
+ // …and the local body is not lost — it is parked out of the tree under
1240
+ // `.hq/conflict-backups/`, never as a sibling twin.
1241
+ expect(fs.readFileSync(sidecarIn(companyDocs), "utf-8")).toBe("local version");
1195
1242
  });
1196
1243
  it("conflict keep clears the fetched sidecar before rename for Windows compatibility", async () => {
1197
1244
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
@@ -1199,7 +1246,7 @@ describe("sync", () => {
1199
1246
  const originalPath = path.join(companyDocs, "handoff.md");
1200
1247
  fs.writeFileSync(originalPath, "local version");
1201
1248
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1202
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1249
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1203
1250
  ]);
1204
1251
  fs.writeFileSync(journalPath, JSON.stringify({
1205
1252
  version: "1",
@@ -1219,7 +1266,7 @@ describe("sync", () => {
1219
1266
  const realRenameSync = fs.renameSync;
1220
1267
  const renameSpy = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
1221
1268
  if (String(from) === originalPath &&
1222
- String(to).includes(".conflict-") &&
1269
+ String(to).includes("conflict-backups") &&
1223
1270
  fs.existsSync(to)) {
1224
1271
  throw errnoError("EEXIST");
1225
1272
  }
@@ -1244,7 +1291,7 @@ describe("sync", () => {
1244
1291
  fs.mkdirSync(companyDocs, { recursive: true });
1245
1292
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1246
1293
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1247
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1294
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1248
1295
  ]);
1249
1296
  fs.writeFileSync(journalPath, JSON.stringify({
1250
1297
  version: "1",
@@ -1277,7 +1324,7 @@ describe("sync", () => {
1277
1324
  const originalPath = path.join(companyDocs, "handoff.md");
1278
1325
  fs.writeFileSync(originalPath, "local version");
1279
1326
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1280
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1327
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1281
1328
  ]);
1282
1329
  fs.writeFileSync(journalPath, JSON.stringify({
1283
1330
  version: "1",
@@ -1295,7 +1342,7 @@ describe("sync", () => {
1295
1342
  // WindowsRenameBlockedError immediately (no retry).
1296
1343
  const realRenameSync = fs.renameSync;
1297
1344
  const renameSpy = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
1298
- if (String(from) === originalPath && String(to).includes(".conflict-")) {
1345
+ if (String(from) === originalPath && String(to).includes("conflict-backups")) {
1299
1346
  throw errnoError("EPERM");
1300
1347
  }
1301
1348
  return realRenameSync(from, to);
@@ -1333,7 +1380,7 @@ describe("sync", () => {
1333
1380
  const originalPath = path.join(companyDocs, "handoff.md");
1334
1381
  fs.writeFileSync(originalPath, "local version");
1335
1382
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1336
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1383
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1337
1384
  ]);
1338
1385
  fs.writeFileSync(journalPath, JSON.stringify({
1339
1386
  version: "1",
@@ -1349,7 +1396,7 @@ describe("sync", () => {
1349
1396
  }));
1350
1397
  const realRenameSync = fs.renameSync;
1351
1398
  const renameSpy = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
1352
- if (String(from) === originalPath && String(to).includes(".conflict-")) {
1399
+ if (String(from) === originalPath && String(to).includes("conflict-backups")) {
1353
1400
  throw errnoError("EACCES");
1354
1401
  }
1355
1402
  return realRenameSync(from, to);
@@ -1392,7 +1439,7 @@ describe("sync", () => {
1392
1439
  fs.mkdirSync(companyDocs, { recursive: true });
1393
1440
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1394
1441
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1395
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1442
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1396
1443
  ]);
1397
1444
  fs.writeFileSync(journalPath, JSON.stringify({
1398
1445
  version: "1",
@@ -1428,7 +1475,7 @@ describe("sync", () => {
1428
1475
  fs.mkdirSync(companyDocs, { recursive: true });
1429
1476
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1430
1477
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1431
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1478
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1432
1479
  ]);
1433
1480
  fs.writeFileSync(journalPath, JSON.stringify({
1434
1481
  version: "1",
@@ -1453,6 +1500,7 @@ describe("sync", () => {
1453
1500
  const entry = index.conflicts[0];
1454
1501
  expect(entry.originalPath).toBe("companies/acme/docs/handoff.md");
1455
1502
  expect(entry.preserved).toBe("local");
1503
+ expect(entry.decision).toMatchObject({ winner: "remote", reason: "mtime" });
1456
1504
  // The sidecar it points at holds the LOCAL body — the side the operator
1457
1505
  // may want back. Reading the recorded path is the contract /resolve-conflicts
1458
1506
  // relies on, so assert through it rather than re-globbing the directory.
@@ -1466,7 +1514,7 @@ describe("sync", () => {
1466
1514
  fs.mkdirSync(companyDocs, { recursive: true });
1467
1515
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1468
1516
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1469
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1517
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1470
1518
  ]);
1471
1519
  fs.writeFileSync(journalPath, JSON.stringify({
1472
1520
  version: "1",
@@ -1483,6 +1531,10 @@ describe("sync", () => {
1483
1531
  const resolveSpy = vi
1484
1532
  .spyOn(conflictModule, "resolveConflict")
1485
1533
  .mockResolvedValue("skip");
1534
+ // An UNSET strategy only prompts when a human is on a TTY; off a TTY it is
1535
+ // the version-aware winner. Model the interactive terminal here.
1536
+ const ttyDescriptor = Object.getOwnPropertyDescriptor(process.stdin, "isTTY");
1537
+ Object.defineProperty(process.stdin, "isTTY", { value: true, configurable: true });
1486
1538
  try {
1487
1539
  await sync({
1488
1540
  company: "acme",
@@ -1491,9 +1543,15 @@ describe("sync", () => {
1491
1543
  });
1492
1544
  }
1493
1545
  finally {
1546
+ if (ttyDescriptor)
1547
+ Object.defineProperty(process.stdin, "isTTY", ttyDescriptor);
1548
+ else
1549
+ delete process.stdin.isTTY;
1494
1550
  resolveSpy.mockRestore();
1495
1551
  }
1496
1552
  expect(fs.readFileSync(path.join(companyDocs, "handoff.md"), "utf-8")).toBe("local version");
1553
+ // The cloud body is still parked for inspection — skip never adopts it.
1554
+ expect(fs.readFileSync(sidecarIn(companyDocs), "utf-8")).toBe("mock file content");
1497
1555
  });
1498
1556
  it("a genuine download clears a stale localDiverges flag (entry is now truly local==remote)", async () => {
1499
1557
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
@@ -1722,16 +1780,15 @@ describe("sync", () => {
1722
1780
  readlinkSpy.mockRestore();
1723
1781
  }
1724
1782
  });
1725
- it("still writes a `.conflict-*` mirror when remote genuinely diverges from local", async () => {
1783
+ it("still preserves the losing body (out of tree) when remote genuinely diverges from local", async () => {
1726
1784
  // Guard the other side of the convergence branch: when the probe bytes
1727
- // differ from local, it remains a real conflict — counted, kept, and the
1728
- // fetched remote is retained as the inspection mirror (reused, not
1729
- // re-downloaded).
1785
+ // differ from local, it remains a real conflict — counted, decided, and the
1786
+ // losing body parked under `.hq/conflict-backups/` (never a sibling twin).
1730
1787
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
1731
1788
  fs.mkdirSync(companyDocs, { recursive: true });
1732
1789
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1733
1790
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1734
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1791
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1735
1792
  ]);
1736
1793
  // Default downloadFile mock writes "mock file content" — differs from local.
1737
1794
  fs.writeFileSync(journalPath, JSON.stringify({
@@ -1852,9 +1909,10 @@ describe("sync", () => {
1852
1909
  const st = fs.statSync(localPath);
1853
1910
  const crypto = await import("node:crypto");
1854
1911
  const localHash = crypto.createHash("sha256").update(localContent).digest("hex");
1855
- // Remote object is SMALLER (an older, truncated copy) with a CHANGED etag.
1912
+ // Remote object is SMALLER (an older, truncated copy) with a CHANGED etag
1913
+ // and an OLDER lastModified — the regressed object really is older.
1856
1914
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1857
- { key: "docs/handoff.md", size: 12, lastModified: new Date(), etag: '"regressed-etag"' },
1915
+ { key: "docs/handoff.md", size: 12, lastModified: new Date(Date.now() - 60_000), etag: '"regressed-etag"' },
1858
1916
  ]);
1859
1917
  // The convergence probe fetches remote bytes; default mock writes
1860
1918
  // "mock file content" — genuinely different from local, so this is a real
@@ -1885,16 +1943,17 @@ describe("sync", () => {
1885
1943
  // Surfaced as a conflict, NOT a silent download.
1886
1944
  expect(result.conflicts).toBe(1);
1887
1945
  expect(result.conflictPaths).toEqual(["docs/handoff.md"]);
1888
- expect(result.filesDownloaded).toBe(1);
1889
- // The newer local bytes are not lost — the rollback is survivable because
1890
- // `keep` renames them aside rather than discarding them. The guard's job is
1891
- // to route this through conflict resolution instead of a silent download;
1892
- // which side ends up at the original name is the strategy's call.
1893
- expect(fs.readFileSync(sidecarIn(path.dirname(localPath)), "utf-8")).toBe(localContent);
1946
+ // The version-aware winner keeps the NEWER local bytes live (the regressed
1947
+ // remote is older), so nothing is downloaded over them…
1948
+ expect(result.filesDownloaded).toBe(0);
1949
+ expect(fs.readFileSync(localPath, "utf-8")).toBe(localContent);
1950
+ // …the regressed cloud body is parked out of tree, and no sibling twin is
1951
+ // littered beside the file.
1952
+ expect(fs.readFileSync(sidecarIn(path.dirname(localPath)), "utf-8")).toBe("mock file content");
1894
1953
  const litter = fs
1895
1954
  .readdirSync(companyDocs)
1896
1955
  .filter((f) => f.includes(".conflict-"));
1897
- expect(litter).toHaveLength(1);
1956
+ expect(litter).toHaveLength(0);
1898
1957
  });
1899
1958
  it("same-size same-mtime local edit plus remote edit is a conflict", async () => {
1900
1959
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
@@ -2130,7 +2189,7 @@ describe("sync", () => {
2130
2189
  const localContent = "in-progress local work that must survive\n";
2131
2190
  fs.writeFileSync(localPath, localContent);
2132
2191
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
2133
- { key: "docs/handoff.md", size: 17, lastModified: new Date(), etag: '"cloud-etag"' },
2192
+ { key: "docs/handoff.md", size: 17, lastModified: new Date(Date.now() + 60_000), etag: '"cloud-etag"' },
2134
2193
  ]);
2135
2194
  // Journal has NO entry for this key — the whole point of the regression.
2136
2195
  fs.writeFileSync(journalPath, JSON.stringify({ version: "1", lastSync: new Date().toISOString(), files: {} }));
@@ -2166,7 +2225,7 @@ describe("sync", () => {
2166
2225
  // Byte-identical to what the downloadFile mock writes.
2167
2226
  fs.writeFileSync(localPath, "mock file content");
2168
2227
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
2169
- { key: "docs/handoff.md", size: 17, lastModified: new Date(), etag: '"cloud-etag"' },
2228
+ { key: "docs/handoff.md", size: 17, lastModified: new Date(Date.now() + 60_000), etag: '"cloud-etag"' },
2170
2229
  ]);
2171
2230
  fs.writeFileSync(journalPath, JSON.stringify({ version: "1", lastSync: new Date().toISOString(), files: {} }));
2172
2231
  const result = await sync({
@@ -2873,11 +2932,12 @@ describe("sync", () => {
2873
2932
  },
2874
2933
  },
2875
2934
  }));
2935
+ // A pushed BEFORE B's local /handoff, so the remote object is older.
2876
2936
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
2877
2937
  {
2878
2938
  key: "workspace/threads/handoff.json",
2879
2939
  size: 50,
2880
- lastModified: new Date(),
2940
+ lastModified: new Date(Date.now() - 60_000),
2881
2941
  etag: '"remote-from-A"',
2882
2942
  },
2883
2943
  ]);
@@ -2890,12 +2950,12 @@ describe("sync", () => {
2890
2950
  });
2891
2951
  expect(result.conflicts).toBe(1);
2892
2952
  expect(result.conflictPaths).toEqual(["workspace/threads/handoff.json"]);
2893
- // B's newer local pointer is preserved verbatim — renamed aside rather than
2894
- // destroyed. DEV-1778 was about the pointer being LOST; a recoverable
2895
- // sidecar satisfies that, and the machine can still resume from it.
2896
- const kept = JSON.parse(fs.readFileSync(sidecarIn(threadsLocal), "utf-8"));
2953
+ // B's newer local pointer stays LIVE — the version-aware winner prefers
2954
+ // the newer body — and A's stale remote is parked out of tree.
2955
+ const kept = JSON.parse(fs.readFileSync(path.join(threadsLocal, "handoff.json"), "utf-8"));
2897
2956
  expect(kept.message).toBe("newer local from B");
2898
2957
  expect(kept.thread_path).toBe("workspace/threads/T-machineB.json");
2958
+ expect(fs.readFileSync(sidecarIn(threadsLocal), "utf-8")).toBe("mock file content");
2899
2959
  });
2900
2960
  it("personalMode: downloads companies/{local-slug}/... keys by default when slug NOT in teamSyncedSlugs", async () => {
2901
2961
  // Local (non-cloud) companies sync to the personal vault by default.
@@ -3712,11 +3772,13 @@ describe("sync", () => {
3712
3772
  vaultConfig: mockConfig,
3713
3773
  hqRoot: tmpDir,
3714
3774
  });
3715
- // Conflict surfaced; local edits preserved; nothing tombstoned away.
3775
+ // Conflict surfaced; the newer local edits stay live (the remote object is
3776
+ // older); the remote body is parked out of tree; nothing tombstoned away.
3716
3777
  expect(result.conflicts).toBeGreaterThanOrEqual(1);
3717
3778
  expect(result.conflictPaths).toContain("docs/edited.md");
3718
3779
  expect(fs.existsSync(editedPath)).toBe(true);
3719
- expect(fs.readFileSync(sidecarIn(path.dirname(editedPath)), "utf-8")).toBe("my local edits");
3780
+ expect(fs.readFileSync(editedPath, "utf-8")).toBe("my local edits");
3781
+ expect(fs.readFileSync(sidecarIn(path.dirname(editedPath)), "utf-8")).toBe("mock file content");
3720
3782
  expect(result.filesTombstoned).toBe(0);
3721
3783
  });
3722
3784
  it("delete-resync: suppression is per-key and the fetch is company-scoped (no cross-tenant bleed)", async () => {