@indigoai-us/hq-cloud 6.16.27 → 6.16.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/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 +94 -47
  29. package/dist/cli/sync.test.js.map +1 -1
  30. package/dist/lib/conflict-file.d.ts +50 -18
  31. package/dist/lib/conflict-file.d.ts.map +1 -1
  32. package/dist/lib/conflict-file.js +63 -34
  33. package/dist/lib/conflict-file.js.map +1 -1
  34. package/dist/lib/conflict-index.d.ts.map +1 -1
  35. package/dist/lib/conflict-index.js +6 -0
  36. package/dist/lib/conflict-index.js.map +1 -1
  37. package/dist/lib/conflict-reconcile.d.ts +106 -0
  38. package/dist/lib/conflict-reconcile.d.ts.map +1 -0
  39. package/dist/lib/conflict-reconcile.js +412 -0
  40. package/dist/lib/conflict-reconcile.js.map +1 -0
  41. package/dist/lib/conflict-reconcile.test.d.ts +7 -0
  42. package/dist/lib/conflict-reconcile.test.d.ts.map +1 -0
  43. package/dist/lib/conflict-reconcile.test.js +246 -0
  44. package/dist/lib/conflict-reconcile.test.js.map +1 -0
  45. package/dist/lib/conflict.test.d.ts +3 -2
  46. package/dist/lib/conflict.test.d.ts.map +1 -1
  47. package/dist/lib/conflict.test.js +60 -3
  48. package/dist/lib/conflict.test.js.map +1 -1
  49. package/dist/types.d.ts +23 -5
  50. package/dist/types.d.ts.map +1 -1
  51. 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");
@@ -1162,7 +1197,7 @@ describe("sync", () => {
1162
1197
  fs.mkdirSync(companyDocs, { recursive: true });
1163
1198
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1164
1199
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1165
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1200
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1166
1201
  ]);
1167
1202
  fs.writeFileSync(journalPath, JSON.stringify({
1168
1203
  version: "1",
@@ -1186,12 +1221,9 @@ describe("sync", () => {
1186
1221
  expect(result.conflictPaths).toEqual(["docs/handoff.md"]);
1187
1222
  // The cloud body is the working copy now.
1188
1223
  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");
1224
+ // …and the local body is not lost — it is parked out of the tree under
1225
+ // `.hq/conflict-backups/`, never as a sibling twin.
1226
+ expect(fs.readFileSync(sidecarIn(companyDocs), "utf-8")).toBe("local version");
1195
1227
  });
1196
1228
  it("conflict keep clears the fetched sidecar before rename for Windows compatibility", async () => {
1197
1229
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
@@ -1199,7 +1231,7 @@ describe("sync", () => {
1199
1231
  const originalPath = path.join(companyDocs, "handoff.md");
1200
1232
  fs.writeFileSync(originalPath, "local version");
1201
1233
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1202
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1234
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1203
1235
  ]);
1204
1236
  fs.writeFileSync(journalPath, JSON.stringify({
1205
1237
  version: "1",
@@ -1219,7 +1251,7 @@ describe("sync", () => {
1219
1251
  const realRenameSync = fs.renameSync;
1220
1252
  const renameSpy = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
1221
1253
  if (String(from) === originalPath &&
1222
- String(to).includes(".conflict-") &&
1254
+ String(to).includes("conflict-backups") &&
1223
1255
  fs.existsSync(to)) {
1224
1256
  throw errnoError("EEXIST");
1225
1257
  }
@@ -1244,7 +1276,7 @@ describe("sync", () => {
1244
1276
  fs.mkdirSync(companyDocs, { recursive: true });
1245
1277
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1246
1278
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1247
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1279
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1248
1280
  ]);
1249
1281
  fs.writeFileSync(journalPath, JSON.stringify({
1250
1282
  version: "1",
@@ -1277,7 +1309,7 @@ describe("sync", () => {
1277
1309
  const originalPath = path.join(companyDocs, "handoff.md");
1278
1310
  fs.writeFileSync(originalPath, "local version");
1279
1311
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1280
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1312
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1281
1313
  ]);
1282
1314
  fs.writeFileSync(journalPath, JSON.stringify({
1283
1315
  version: "1",
@@ -1295,7 +1327,7 @@ describe("sync", () => {
1295
1327
  // WindowsRenameBlockedError immediately (no retry).
1296
1328
  const realRenameSync = fs.renameSync;
1297
1329
  const renameSpy = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
1298
- if (String(from) === originalPath && String(to).includes(".conflict-")) {
1330
+ if (String(from) === originalPath && String(to).includes("conflict-backups")) {
1299
1331
  throw errnoError("EPERM");
1300
1332
  }
1301
1333
  return realRenameSync(from, to);
@@ -1333,7 +1365,7 @@ describe("sync", () => {
1333
1365
  const originalPath = path.join(companyDocs, "handoff.md");
1334
1366
  fs.writeFileSync(originalPath, "local version");
1335
1367
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1336
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1368
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1337
1369
  ]);
1338
1370
  fs.writeFileSync(journalPath, JSON.stringify({
1339
1371
  version: "1",
@@ -1349,7 +1381,7 @@ describe("sync", () => {
1349
1381
  }));
1350
1382
  const realRenameSync = fs.renameSync;
1351
1383
  const renameSpy = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
1352
- if (String(from) === originalPath && String(to).includes(".conflict-")) {
1384
+ if (String(from) === originalPath && String(to).includes("conflict-backups")) {
1353
1385
  throw errnoError("EACCES");
1354
1386
  }
1355
1387
  return realRenameSync(from, to);
@@ -1392,7 +1424,7 @@ describe("sync", () => {
1392
1424
  fs.mkdirSync(companyDocs, { recursive: true });
1393
1425
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1394
1426
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1395
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1427
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1396
1428
  ]);
1397
1429
  fs.writeFileSync(journalPath, JSON.stringify({
1398
1430
  version: "1",
@@ -1428,7 +1460,7 @@ describe("sync", () => {
1428
1460
  fs.mkdirSync(companyDocs, { recursive: true });
1429
1461
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1430
1462
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1431
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1463
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1432
1464
  ]);
1433
1465
  fs.writeFileSync(journalPath, JSON.stringify({
1434
1466
  version: "1",
@@ -1453,6 +1485,7 @@ describe("sync", () => {
1453
1485
  const entry = index.conflicts[0];
1454
1486
  expect(entry.originalPath).toBe("companies/acme/docs/handoff.md");
1455
1487
  expect(entry.preserved).toBe("local");
1488
+ expect(entry.decision).toMatchObject({ winner: "remote", reason: "mtime" });
1456
1489
  // The sidecar it points at holds the LOCAL body — the side the operator
1457
1490
  // may want back. Reading the recorded path is the contract /resolve-conflicts
1458
1491
  // relies on, so assert through it rather than re-globbing the directory.
@@ -1466,7 +1499,7 @@ describe("sync", () => {
1466
1499
  fs.mkdirSync(companyDocs, { recursive: true });
1467
1500
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1468
1501
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1469
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1502
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1470
1503
  ]);
1471
1504
  fs.writeFileSync(journalPath, JSON.stringify({
1472
1505
  version: "1",
@@ -1483,6 +1516,10 @@ describe("sync", () => {
1483
1516
  const resolveSpy = vi
1484
1517
  .spyOn(conflictModule, "resolveConflict")
1485
1518
  .mockResolvedValue("skip");
1519
+ // An UNSET strategy only prompts when a human is on a TTY; off a TTY it is
1520
+ // the version-aware winner. Model the interactive terminal here.
1521
+ const ttyDescriptor = Object.getOwnPropertyDescriptor(process.stdin, "isTTY");
1522
+ Object.defineProperty(process.stdin, "isTTY", { value: true, configurable: true });
1486
1523
  try {
1487
1524
  await sync({
1488
1525
  company: "acme",
@@ -1491,9 +1528,15 @@ describe("sync", () => {
1491
1528
  });
1492
1529
  }
1493
1530
  finally {
1531
+ if (ttyDescriptor)
1532
+ Object.defineProperty(process.stdin, "isTTY", ttyDescriptor);
1533
+ else
1534
+ delete process.stdin.isTTY;
1494
1535
  resolveSpy.mockRestore();
1495
1536
  }
1496
1537
  expect(fs.readFileSync(path.join(companyDocs, "handoff.md"), "utf-8")).toBe("local version");
1538
+ // The cloud body is still parked for inspection — skip never adopts it.
1539
+ expect(fs.readFileSync(sidecarIn(companyDocs), "utf-8")).toBe("mock file content");
1497
1540
  });
1498
1541
  it("a genuine download clears a stale localDiverges flag (entry is now truly local==remote)", async () => {
1499
1542
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
@@ -1722,16 +1765,15 @@ describe("sync", () => {
1722
1765
  readlinkSpy.mockRestore();
1723
1766
  }
1724
1767
  });
1725
- it("still writes a `.conflict-*` mirror when remote genuinely diverges from local", async () => {
1768
+ it("still preserves the losing body (out of tree) when remote genuinely diverges from local", async () => {
1726
1769
  // 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).
1770
+ // differ from local, it remains a real conflict — counted, decided, and the
1771
+ // losing body parked under `.hq/conflict-backups/` (never a sibling twin).
1730
1772
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
1731
1773
  fs.mkdirSync(companyDocs, { recursive: true });
1732
1774
  fs.writeFileSync(path.join(companyDocs, "handoff.md"), "local version");
1733
1775
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1734
- { key: "docs/handoff.md", size: 42, lastModified: new Date(), etag: '"divergent"' },
1776
+ { key: "docs/handoff.md", size: 42, lastModified: new Date(Date.now() + 60_000), etag: '"divergent"' },
1735
1777
  ]);
1736
1778
  // Default downloadFile mock writes "mock file content" — differs from local.
1737
1779
  fs.writeFileSync(journalPath, JSON.stringify({
@@ -1852,9 +1894,10 @@ describe("sync", () => {
1852
1894
  const st = fs.statSync(localPath);
1853
1895
  const crypto = await import("node:crypto");
1854
1896
  const localHash = crypto.createHash("sha256").update(localContent).digest("hex");
1855
- // Remote object is SMALLER (an older, truncated copy) with a CHANGED etag.
1897
+ // Remote object is SMALLER (an older, truncated copy) with a CHANGED etag
1898
+ // and an OLDER lastModified — the regressed object really is older.
1856
1899
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
1857
- { key: "docs/handoff.md", size: 12, lastModified: new Date(), etag: '"regressed-etag"' },
1900
+ { key: "docs/handoff.md", size: 12, lastModified: new Date(Date.now() - 60_000), etag: '"regressed-etag"' },
1858
1901
  ]);
1859
1902
  // The convergence probe fetches remote bytes; default mock writes
1860
1903
  // "mock file content" — genuinely different from local, so this is a real
@@ -1885,16 +1928,17 @@ describe("sync", () => {
1885
1928
  // Surfaced as a conflict, NOT a silent download.
1886
1929
  expect(result.conflicts).toBe(1);
1887
1930
  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);
1931
+ // The version-aware winner keeps the NEWER local bytes live (the regressed
1932
+ // remote is older), so nothing is downloaded over them…
1933
+ expect(result.filesDownloaded).toBe(0);
1934
+ expect(fs.readFileSync(localPath, "utf-8")).toBe(localContent);
1935
+ // …the regressed cloud body is parked out of tree, and no sibling twin is
1936
+ // littered beside the file.
1937
+ expect(fs.readFileSync(sidecarIn(path.dirname(localPath)), "utf-8")).toBe("mock file content");
1894
1938
  const litter = fs
1895
1939
  .readdirSync(companyDocs)
1896
1940
  .filter((f) => f.includes(".conflict-"));
1897
- expect(litter).toHaveLength(1);
1941
+ expect(litter).toHaveLength(0);
1898
1942
  });
1899
1943
  it("same-size same-mtime local edit plus remote edit is a conflict", async () => {
1900
1944
  const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
@@ -2130,7 +2174,7 @@ describe("sync", () => {
2130
2174
  const localContent = "in-progress local work that must survive\n";
2131
2175
  fs.writeFileSync(localPath, localContent);
2132
2176
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
2133
- { key: "docs/handoff.md", size: 17, lastModified: new Date(), etag: '"cloud-etag"' },
2177
+ { key: "docs/handoff.md", size: 17, lastModified: new Date(Date.now() + 60_000), etag: '"cloud-etag"' },
2134
2178
  ]);
2135
2179
  // Journal has NO entry for this key — the whole point of the regression.
2136
2180
  fs.writeFileSync(journalPath, JSON.stringify({ version: "1", lastSync: new Date().toISOString(), files: {} }));
@@ -2166,7 +2210,7 @@ describe("sync", () => {
2166
2210
  // Byte-identical to what the downloadFile mock writes.
2167
2211
  fs.writeFileSync(localPath, "mock file content");
2168
2212
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
2169
- { key: "docs/handoff.md", size: 17, lastModified: new Date(), etag: '"cloud-etag"' },
2213
+ { key: "docs/handoff.md", size: 17, lastModified: new Date(Date.now() + 60_000), etag: '"cloud-etag"' },
2170
2214
  ]);
2171
2215
  fs.writeFileSync(journalPath, JSON.stringify({ version: "1", lastSync: new Date().toISOString(), files: {} }));
2172
2216
  const result = await sync({
@@ -2873,11 +2917,12 @@ describe("sync", () => {
2873
2917
  },
2874
2918
  },
2875
2919
  }));
2920
+ // A pushed BEFORE B's local /handoff, so the remote object is older.
2876
2921
  vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
2877
2922
  {
2878
2923
  key: "workspace/threads/handoff.json",
2879
2924
  size: 50,
2880
- lastModified: new Date(),
2925
+ lastModified: new Date(Date.now() - 60_000),
2881
2926
  etag: '"remote-from-A"',
2882
2927
  },
2883
2928
  ]);
@@ -2890,12 +2935,12 @@ describe("sync", () => {
2890
2935
  });
2891
2936
  expect(result.conflicts).toBe(1);
2892
2937
  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"));
2938
+ // B's newer local pointer stays LIVE — the version-aware winner prefers
2939
+ // the newer body — and A's stale remote is parked out of tree.
2940
+ const kept = JSON.parse(fs.readFileSync(path.join(threadsLocal, "handoff.json"), "utf-8"));
2897
2941
  expect(kept.message).toBe("newer local from B");
2898
2942
  expect(kept.thread_path).toBe("workspace/threads/T-machineB.json");
2943
+ expect(fs.readFileSync(sidecarIn(threadsLocal), "utf-8")).toBe("mock file content");
2899
2944
  });
2900
2945
  it("personalMode: downloads companies/{local-slug}/... keys by default when slug NOT in teamSyncedSlugs", async () => {
2901
2946
  // Local (non-cloud) companies sync to the personal vault by default.
@@ -3712,11 +3757,13 @@ describe("sync", () => {
3712
3757
  vaultConfig: mockConfig,
3713
3758
  hqRoot: tmpDir,
3714
3759
  });
3715
- // Conflict surfaced; local edits preserved; nothing tombstoned away.
3760
+ // Conflict surfaced; the newer local edits stay live (the remote object is
3761
+ // older); the remote body is parked out of tree; nothing tombstoned away.
3716
3762
  expect(result.conflicts).toBeGreaterThanOrEqual(1);
3717
3763
  expect(result.conflictPaths).toContain("docs/edited.md");
3718
3764
  expect(fs.existsSync(editedPath)).toBe(true);
3719
- expect(fs.readFileSync(sidecarIn(path.dirname(editedPath)), "utf-8")).toBe("my local edits");
3765
+ expect(fs.readFileSync(editedPath, "utf-8")).toBe("my local edits");
3766
+ expect(fs.readFileSync(sidecarIn(path.dirname(editedPath)), "utf-8")).toBe("mock file content");
3720
3767
  expect(result.filesTombstoned).toBe(0);
3721
3768
  });
3722
3769
  it("delete-resync: suppression is per-key and the fetch is company-scoped (no cross-tenant bleed)", async () => {