@indigoai-us/hq-cloud 6.18.6 → 6.18.8

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 (64) hide show
  1. package/dist/bin/sync-runner-company.d.ts +2 -0
  2. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  3. package/dist/bin/sync-runner-company.js +14 -0
  4. package/dist/bin/sync-runner-company.js.map +1 -1
  5. package/dist/bin/sync-runner-company.test.js +45 -1
  6. package/dist/bin/sync-runner-company.test.js.map +1 -1
  7. package/dist/bin/sync-runner-publish-expansion.test.d.ts +2 -0
  8. package/dist/bin/sync-runner-publish-expansion.test.d.ts.map +1 -0
  9. package/dist/bin/sync-runner-publish-expansion.test.js +54 -0
  10. package/dist/bin/sync-runner-publish-expansion.test.js.map +1 -0
  11. package/dist/bin/sync-runner-watch-loop.d.ts +11 -0
  12. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  13. package/dist/bin/sync-runner-watch-loop.js +58 -1
  14. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  15. package/dist/bin/sync-runner.d.ts +4 -0
  16. package/dist/bin/sync-runner.d.ts.map +1 -1
  17. package/dist/bin/sync-runner.js +1 -0
  18. package/dist/bin/sync-runner.js.map +1 -1
  19. package/dist/cli/share.d.ts +26 -2
  20. package/dist/cli/share.d.ts.map +1 -1
  21. package/dist/cli/share.js +113 -23
  22. package/dist/cli/share.js.map +1 -1
  23. package/dist/cli/share.test.js +320 -9
  24. package/dist/cli/share.test.js.map +1 -1
  25. package/dist/lib/ontology-entity-meta.d.ts +21 -0
  26. package/dist/lib/ontology-entity-meta.d.ts.map +1 -0
  27. package/dist/lib/ontology-entity-meta.js +51 -0
  28. package/dist/lib/ontology-entity-meta.js.map +1 -0
  29. package/dist/lib/ontology-entity-meta.test.d.ts +2 -0
  30. package/dist/lib/ontology-entity-meta.test.d.ts.map +1 -0
  31. package/dist/lib/ontology-entity-meta.test.js +79 -0
  32. package/dist/lib/ontology-entity-meta.test.js.map +1 -0
  33. package/dist/lib/ontology-entity-push.d.ts +9 -4
  34. package/dist/lib/ontology-entity-push.d.ts.map +1 -1
  35. package/dist/lib/ontology-entity-push.js +19 -20
  36. package/dist/lib/ontology-entity-push.js.map +1 -1
  37. package/dist/lib/ontology-entity-push.test.js +7 -0
  38. package/dist/lib/ontology-entity-push.test.js.map +1 -1
  39. package/dist/lib/ontology-outbox.d.ts +52 -0
  40. package/dist/lib/ontology-outbox.d.ts.map +1 -0
  41. package/dist/lib/ontology-outbox.js +118 -0
  42. package/dist/lib/ontology-outbox.js.map +1 -0
  43. package/dist/lib/skill-register-landed-bytes.d.ts +16 -0
  44. package/dist/lib/skill-register-landed-bytes.d.ts.map +1 -0
  45. package/dist/lib/skill-register-landed-bytes.js +78 -0
  46. package/dist/lib/skill-register-landed-bytes.js.map +1 -0
  47. package/dist/lib/skill-register-landed-bytes.test.d.ts +2 -0
  48. package/dist/lib/skill-register-landed-bytes.test.d.ts.map +1 -0
  49. package/dist/lib/skill-register-landed-bytes.test.js +83 -0
  50. package/dist/lib/skill-register-landed-bytes.test.js.map +1 -0
  51. package/dist/private-folder-transport.d.ts.map +1 -1
  52. package/dist/private-folder-transport.js +16 -1
  53. package/dist/private-folder-transport.js.map +1 -1
  54. package/dist/private-folder-transport.test.d.ts +2 -0
  55. package/dist/private-folder-transport.test.d.ts.map +1 -0
  56. package/dist/private-folder-transport.test.js +33 -0
  57. package/dist/private-folder-transport.test.js.map +1 -0
  58. package/dist/s3.d.ts +1 -1
  59. package/dist/s3.d.ts.map +1 -1
  60. package/dist/s3.js +34 -4
  61. package/dist/s3.js.map +1 -1
  62. package/dist/s3.test.js +51 -0
  63. package/dist/s3.test.js.map +1 -1
  64. package/package.json +2 -1
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * Unit tests for hq share command (VLT-5 US-002).
3
3
  */
4
+ import { createHash } from "node:crypto";
4
5
  import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
5
6
  import * as fs from "fs";
6
7
  import * as path from "path";
7
8
  import * as os from "os";
9
+ import crypto from "crypto";
8
10
  import { clearContextCache } from "../context.js";
9
11
  // Re-export node:fs as a mutable module so the large-tree regression can
10
12
  // replace only readdirSync; normal tests retain the real implementation.
@@ -31,7 +33,7 @@ vi.mock("../s3.js", async (importOriginal) => {
31
33
  primeObjectTransport: vi.fn().mockResolvedValue(undefined),
32
34
  primeUploads: vi.fn().mockResolvedValue(undefined),
33
35
  publishedContentHash: actual.publishedContentHash,
34
- setUploadContentHash: actual.setUploadContentHash,
36
+ setUploadContentHash: vi.fn(actual.setUploadContentHash),
35
37
  // Real exports share.ts now imports for the HQ-DESKTOP-54 privilege skip.
36
38
  WINDOWS_SYMLINK_PRIVILEGE_REMEDY: "Enable Windows Developer Mode (Settings > Privacy & security > For developers) or run HQ sync once from an elevated terminal, then sync again.",
37
39
  WindowsSymlinkPrivilegeError: class WindowsSymlinkPrivilegeError extends Error {
@@ -57,6 +59,10 @@ vi.mock("../s3.js", async (importOriginal) => {
57
59
  // prompt lifecycle (createInterface + close) without ever touching stdin.
58
60
  // Default impl never resolves; conflict-prompt tests below override per-test
59
61
  // to script answers.
62
+ vi.mock("../lib/skill-register-landed-bytes.js", async (importOriginal) => {
63
+ const actual = await importOriginal();
64
+ return { ...actual, resolveSkillRegisterLandedBytes: vi.fn().mockResolvedValue(false) };
65
+ });
60
66
  vi.mock("readline", () => ({
61
67
  createInterface: vi.fn(() => ({
62
68
  question: vi.fn(),
@@ -65,10 +71,11 @@ vi.mock("readline", () => ({
65
71
  }));
66
72
  import * as readline from "readline";
67
73
  import { share, ServerOwnedPushPathsError, UnreachablePushPathsError, UnregisteredCompanySkillError, currentUploadBytes, FULL_PUSH_COOPERATIVE_CHECKPOINT_BATCH_SIZE, parseRegisteredCompanySkillUid, setPushCollectCooperativeYieldMsForTesting, _testing as shareTesting, } from "./share.js";
68
- import { deleteRemoteFile, downloadFile, headRemoteFile, primeObjectTransport, primeUploads, uploadFile, uploadSymlink, WindowsSymlinkPrivilegeError, } from "../s3.js";
74
+ import { deleteRemoteFile, downloadFile, headRemoteFile, primeObjectTransport, primeUploads, setUploadContentHash, uploadFile, uploadSymlink, WindowsSymlinkPrivilegeError, } from "../s3.js";
69
75
  import { VaultAuthError, VaultClientError, VaultConflictError, VaultPermissionDeniedError, } from "../vault-client.js";
70
76
  import { CognitoRefreshError } from "../cognito-auth.js";
71
77
  import { RateLimitedError } from "../object-io.js";
78
+ import { resolveSkillRegisterLandedBytes } from "../lib/skill-register-landed-bytes.js";
72
79
  import { AreaLedgerMigration, closeCachedAreaJournalMigration, hashFile, openJournalStateStoreForMigration, readJournal, readJournalScoped, setAreaLedgerMigrationTestHooksForTest, writeJournal, } from "../journal.js";
73
80
  import { ReconcileCursorStore } from "../sync/reconcile-cursor.js";
74
81
  import { AreaResolver } from "../sync/area-resolver.js";
@@ -241,6 +248,107 @@ describe("company skill identity preflight", () => {
241
248
  landed: true,
242
249
  });
243
250
  });
251
+ it("registers the exact upload bytes when the landed-bytes flag is on", async () => {
252
+ const original = "---\nname: Existing\ndescription: Ready\nskill_uid: skl_EXISTING1\ntags: [sales]\n---\n";
253
+ const changedDuringUpload = original.replace("[sales]", "[support]");
254
+ const file = createSkill("same-bytes", original);
255
+ vi.mocked(resolveSkillRegisterLandedBytes).mockResolvedValueOnce(true);
256
+ let uploadedBody;
257
+ vi.mocked(uploadFile).mockImplementationOnce(async (...args) => {
258
+ fs.writeFileSync(file, changedDuringUpload);
259
+ uploadedBody = args[6] ?? fs.readFileSync(file);
260
+ fs.writeFileSync(file, original);
261
+ return { etag: '"upload-etag"' };
262
+ });
263
+ const registrar = vi.fn(async (input) => registered(input, "skl_EXISTING1"));
264
+ await share({
265
+ paths: [file],
266
+ hqRoot,
267
+ entityContext: makeEntityContext(),
268
+ registerCompanySkill: registrar,
269
+ });
270
+ const landed = registrar.mock.calls.find(([input]) => input.landed === true);
271
+ expect(uploadedBody).toBeDefined();
272
+ expect(landed?.[0].content).toBe(uploadedBody.toString("utf8"));
273
+ });
274
+ it("replans a skill edited before landed-byte capture and keeps its upload hash aligned", async () => {
275
+ const original = "---\nname: Existing\ndescription: Ready\nskill_uid: skl_EXISTING1\ntags: [sales]\n---\n";
276
+ const changed = original.replace("[sales]", "[other]");
277
+ const file = createSkill("changed-before-capture", original);
278
+ vi.mocked(resolveSkillRegisterLandedBytes).mockResolvedValueOnce(true);
279
+ vi.mocked(setUploadContentHash).mockClear();
280
+ const realReadFileSync = fs.readFileSync.bind(fs);
281
+ let skillByteReads = 0;
282
+ let changedAfterHash = false;
283
+ const readSpy = vi.spyOn(fs, "readFileSync").mockImplementation(((pathname, options) => {
284
+ if (pathname === file && options === undefined) {
285
+ skillByteReads += 1;
286
+ if (skillByteReads === 2) {
287
+ changedAfterHash = true;
288
+ const before = fs.statSync(file);
289
+ fs.writeFileSync(file, changed);
290
+ fs.utimesSync(file, before.atime, new Date(before.mtimeMs + 2_000));
291
+ }
292
+ }
293
+ return realReadFileSync(pathname, options);
294
+ }));
295
+ let uploadedBytes;
296
+ let uploadedHash;
297
+ let byteReadsWhenUploaded = 0;
298
+ vi.mocked(uploadFile).mockImplementationOnce(async (...args) => {
299
+ uploadedBytes = args[6];
300
+ uploadedHash = vi.mocked(setUploadContentHash).mock.calls.at(-1)?.[1];
301
+ byteReadsWhenUploaded = skillByteReads;
302
+ return { etag: '"upload-etag"' };
303
+ });
304
+ const registrar = vi.fn(async (input) => registered(input, "skl_EXISTING1"));
305
+ let result;
306
+ try {
307
+ result = await share({
308
+ paths: [file],
309
+ hqRoot,
310
+ entityContext: makeEntityContext(),
311
+ registerCompanySkill: registrar,
312
+ });
313
+ }
314
+ finally {
315
+ readSpy.mockRestore();
316
+ }
317
+ const landed = registrar.mock.calls.find(([input]) => input.landed === true);
318
+ expect(changedAfterHash).toBe(true);
319
+ expect(result.filesUploaded).toBe(1);
320
+ expect(uploadFile).toHaveBeenCalledOnce();
321
+ expect(uploadedBytes?.toString("utf8")).toBe(changed);
322
+ expect(uploadedHash).toBe(crypto.createHash("sha256").update(uploadedBytes).digest("hex"));
323
+ expect(headRemoteFile).toHaveBeenCalledTimes(2);
324
+ expect(byteReadsWhenUploaded).toBeGreaterThan(2);
325
+ expect(setUploadContentHash).toHaveBeenCalledOnce();
326
+ expect(landed?.[0]).toMatchObject({ content: changed, landed: true });
327
+ expect(crypto.createHash("sha256").update(landed[0].content).digest("hex")).toBe(uploadedHash);
328
+ expect(readJournal("acme").files["skills/changed-before-capture/SKILL.md"]?.hash)
329
+ .toBe(uploadedHash);
330
+ });
331
+ it("keeps the current upload and registration path when the flag is off", async () => {
332
+ const content = "---\nname: Existing\ndescription: Ready\nskill_uid: skl_EXISTING1\ntags: [sales]\n---\n";
333
+ const file = createSkill("flag-off", content);
334
+ let uploadedBody;
335
+ vi.mocked(uploadFile).mockImplementationOnce(async (...args) => {
336
+ uploadedBody = fs.readFileSync(args[1]);
337
+ return { etag: '"upload-etag"' };
338
+ });
339
+ const registrar = vi.fn(async (input) => registered(input, "skl_EXISTING1"));
340
+ const result = await share({
341
+ paths: [file],
342
+ hqRoot,
343
+ entityContext: makeEntityContext(),
344
+ registerCompanySkill: registrar,
345
+ });
346
+ const landed = registrar.mock.calls.find(([input]) => input.landed === true);
347
+ expect(uploadedBody?.toString("utf8")).toBe(content);
348
+ expect(landed?.[0].content).toBe(uploadedBody?.toString("utf8"));
349
+ expect(vi.mocked(uploadFile).mock.calls.at(-1)).toHaveLength(5);
350
+ expect(result).not.toHaveProperty("skillRegistrationFallbacks");
351
+ });
244
352
  it("fails closed without an authenticated registration seam", async () => {
245
353
  const file = createSkill("manual", "---\nname: Manual\ndescription: Missing id\n---\n");
246
354
  await expect(share({
@@ -971,9 +1079,10 @@ describe("company skill identity preflight", () => {
971
1079
  const copied = "---\nname: Copied\ndescription: Copied skill\nskill_uid: skl_SOURCE1\n---\n\nWork.\n";
972
1080
  const file = createSkill("copied", copied);
973
1081
  const landedCalls = [];
974
- // Model the server: a landed call for the source-stamped copy 409s; a
975
- // non-landed call forks a fresh identity for this path; and a landed call
976
- // that now carries the FORKED identity reconciles successfully.
1082
+ let nonLandedCalls = 0;
1083
+ // Model the server: preflight reuses the source identity, its landed call
1084
+ // 409s, the one recovery fallback forks a fresh identity, and its landed
1085
+ // retry reconciles successfully.
977
1086
  const registrar = vi.fn(async (input) => {
978
1087
  landedCalls.push(input.landed === true);
979
1088
  const uid = parseRegisteredCompanySkillUid(input.content);
@@ -994,9 +1103,13 @@ describe("company skill identity preflight", () => {
994
1103
  // skill on a landed call — the exact 409 behind HQ-DESKTOP-6T/64.
995
1104
  throw new VaultConflictError("The landed skill does not carry the registered identity for this path");
996
1105
  }
997
- return forkedRegistration(input);
1106
+ nonLandedCalls += 1;
1107
+ return nonLandedCalls === 1
1108
+ ? registered(input, "skl_SOURCE1")
1109
+ : forkedRegistration(input);
998
1110
  });
999
1111
  const events = [];
1112
+ vi.mocked(resolveSkillRegisterLandedBytes).mockResolvedValueOnce(true);
1000
1113
  const result = await share({
1001
1114
  paths: [file],
1002
1115
  hqRoot,
@@ -1006,9 +1119,11 @@ describe("company skill identity preflight", () => {
1006
1119
  });
1007
1120
  expect(result.filesUploaded).toBe(1);
1008
1121
  expect(result.aborted).toBe(false);
1009
- // The landed reconcile 409'd, then the non-landed fallback ran (a fork).
1010
- expect(landedCalls).toContain(true);
1011
- expect(landedCalls).toContain(false);
1122
+ expect(result.skillRegistrationFallbacks).toBe(1);
1123
+ // Preflight reserved once, then the landed reconcile 409'd and recovery
1124
+ // performed exactly one non-landed fallback before one landed retry.
1125
+ expect(nonLandedCalls).toBe(2);
1126
+ expect(landedCalls.filter(Boolean)).toHaveLength(2);
1012
1127
  // The forked, re-stamped bytes were written back so the planner re-uploads.
1013
1128
  expect(parseRegisteredCompanySkillUid(fs.readFileSync(file, "utf8"))).toBe("skl_FORKED1");
1014
1129
  // No per-file error (the pass-1 HQ-DESKTOP-6T error) and no unresolved
@@ -2728,6 +2843,9 @@ describe("share", () => {
2728
2843
  const before = fs.lstatSync(abs);
2729
2844
  await new Promise((resolve) => setTimeout(resolve, 5));
2730
2845
  fs.writeFileSync(abs, CONTENT_B); // same length, different bytes
2846
+ // Keep the rewrite and timestamp restoration in distinct ctime ticks on
2847
+ // filesystems whose ctime clock is coarser than the file writes.
2848
+ await new Promise((resolve) => setTimeout(resolve, 10));
2731
2849
  fs.utimesSync(abs, before.atime, before.mtime); // put the old mtime back
2732
2850
  const after = fs.lstatSync(abs);
2733
2851
  // Preconditions. The disguise worked on size, and as closely on mtime as
@@ -3330,6 +3448,7 @@ describe("share", () => {
3330
3448
  vaultConfig: mockConfig,
3331
3449
  hqRoot: tmpDir,
3332
3450
  ontologyEntityPush: true,
3451
+ ontologyMultiWriter: false,
3333
3452
  });
3334
3453
  expect(result.filesUploaded).toBe(1);
3335
3454
  expect(uploadFile).toHaveBeenCalledWith(expect.anything(), file, entityRel, undefined, { ifNoneMatch: "*" });
@@ -3353,6 +3472,7 @@ describe("share", () => {
3353
3472
  vaultConfig: mockConfig,
3354
3473
  hqRoot: tmpDir,
3355
3474
  ontologyEntityPush: true,
3475
+ ontologyMultiWriter: false,
3356
3476
  })).rejects.toThrow(ServerOwnedPushPathsError);
3357
3477
  expect(uploadFile).not.toHaveBeenCalled();
3358
3478
  });
@@ -3370,6 +3490,7 @@ describe("share", () => {
3370
3490
  vaultConfig: mockConfig,
3371
3491
  hqRoot: tmpDir,
3372
3492
  ontologyEntityPush: true,
3493
+ ontologyMultiWriter: false,
3373
3494
  });
3374
3495
  expect(result.filesUploaded).toBe(1);
3375
3496
  expect(uploadFile).toHaveBeenCalledWith(expect.anything(), file, entityRel, undefined, { ifMatch: "synced-etag" });
@@ -3394,6 +3515,7 @@ describe("share", () => {
3394
3515
  hqRoot: tmpDir,
3395
3516
  onConflict: "overwrite",
3396
3517
  ontologyEntityPush: true,
3518
+ ontologyMultiWriter: false,
3397
3519
  onEvent: (e) => events.push(e),
3398
3520
  });
3399
3521
  expect(uploadFile).not.toHaveBeenCalled();
@@ -3420,6 +3542,7 @@ describe("share", () => {
3420
3542
  hqRoot: tmpDir,
3421
3543
  onConflict: "overwrite",
3422
3544
  ontologyEntityPush: true,
3545
+ ontologyMultiWriter: false,
3423
3546
  });
3424
3547
  expect(uploadFile).toHaveBeenCalledTimes(1);
3425
3548
  expect(vi.mocked(uploadFile).mock.calls[0][4]).toEqual({ ifNoneMatch: "*" });
@@ -3439,6 +3562,7 @@ describe("share", () => {
3439
3562
  hqRoot: tmpDir,
3440
3563
  onConflict: "overwrite",
3441
3564
  ontologyEntityPush: true,
3565
+ ontologyMultiWriter: false,
3442
3566
  });
3443
3567
  expect(uploadFile).not.toHaveBeenCalled();
3444
3568
  expect(result.filesUploaded).toBe(0);
@@ -3459,6 +3583,7 @@ describe("share", () => {
3459
3583
  hqRoot: tmpDir,
3460
3584
  onConflict: "overwrite",
3461
3585
  ontologyEntityPush: true,
3586
+ ontologyMultiWriter: false,
3462
3587
  onEvent: (e) => events.push(e),
3463
3588
  });
3464
3589
  expect(uploadFile).not.toHaveBeenCalled();
@@ -3474,6 +3599,7 @@ describe("share", () => {
3474
3599
  it("unset option: reads the flag, and the local override turns the push on", async () => {
3475
3600
  const file = writeEntity();
3476
3601
  process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY = "true";
3602
+ process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY_MULTI_WRITER = "false";
3477
3603
  try {
3478
3604
  const result = await share({
3479
3605
  paths: [file],
@@ -3485,6 +3611,7 @@ describe("share", () => {
3485
3611
  }
3486
3612
  finally {
3487
3613
  delete process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY;
3614
+ delete process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY_MULTI_WRITER;
3488
3615
  }
3489
3616
  });
3490
3617
  it("unset option: a personal-vault push never reads the flag or pushes entities", async () => {
@@ -3507,6 +3634,190 @@ describe("share", () => {
3507
3634
  }
3508
3635
  expect(uploadFile).not.toHaveBeenCalled();
3509
3636
  });
3637
+ describe("several writers (flag checkpoint.ontology.multi-writer)", () => {
3638
+ const sha = (body) => createHash("sha256").update(body).digest("hex");
3639
+ const bodyAt = (lastUpdated, extra = "") => `---\nid: abc\ntype: person\ncanonical_name: Maria Khan\nlast_updated: ${lastUpdated}\n${extra}---\n`;
3640
+ const freshBody = () => bodyAt(new Date(Date.now() - 60_000).toISOString());
3641
+ const outboxFile = () => path.join(tmpDir, ".hq", "ontology-outbox", "acme.json");
3642
+ function writeOutbox(entries, raw) {
3643
+ fs.mkdirSync(path.dirname(outboxFile()), { recursive: true });
3644
+ fs.writeFileSync(outboxFile(), raw ??
3645
+ JSON.stringify({
3646
+ schema: "hq-ontology-outbox.v1",
3647
+ company: "acme",
3648
+ entries: Object.fromEntries(Object.entries(entries).map(([key, hash]) => [key, { sha256: hash, written_at: new Date().toISOString() }])),
3649
+ }));
3650
+ }
3651
+ const multiWriterShare = (file, extra = {}) => share({
3652
+ paths: [file],
3653
+ company: "acme",
3654
+ vaultConfig: mockConfig,
3655
+ hqRoot: tmpDir,
3656
+ ontologyEntityPush: true,
3657
+ ontologyMultiWriter: true,
3658
+ ...extra,
3659
+ });
3660
+ it("C1: a pulled copy with no outbox entry is refused, with or without a journal row", async () => {
3661
+ const body = freshBody();
3662
+ const file = writeEntity(body);
3663
+ writeOutbox({});
3664
+ await expect(multiWriterShare(file)).rejects.toThrow(ServerOwnedPushPathsError);
3665
+ seedEntityJournal("old-etag", sha("---\nolder: body\n---\n"));
3666
+ await expect(multiWriterShare(file, { onConflict: "overwrite" })).rejects.toThrow(ServerOwnedPushPathsError);
3667
+ expect(uploadFile).not.toHaveBeenCalled();
3668
+ });
3669
+ it("C2: bytes that differ from the outbox entry are refused", async () => {
3670
+ const file = writeEntity(freshBody());
3671
+ writeOutbox({ [entityRel]: sha(bodyAt("2026-09-01T00:00:00.000Z")) });
3672
+ await expect(multiWriterShare(file)).rejects.toThrow(ServerOwnedPushPathsError);
3673
+ expect(uploadFile).not.toHaveBeenCalled();
3674
+ });
3675
+ it("an outbox'd fresh create uploads with If-None-Match:*", async () => {
3676
+ const body = freshBody();
3677
+ const file = writeEntity(body);
3678
+ writeOutbox({ [entityRel]: sha(body) });
3679
+ const result = await multiWriterShare(file);
3680
+ expect(result.filesUploaded).toBe(1);
3681
+ expect(uploadFile).toHaveBeenCalledWith(expect.anything(), file, entityRel, undefined, { ifNoneMatch: "*" });
3682
+ });
3683
+ it("an outbox'd edit over the synced remote is fenced on the journal etag", async () => {
3684
+ const body = bodyAt("2026-09-01T00:00:00.000Z");
3685
+ const file = writeEntity(body);
3686
+ writeOutbox({ [entityRel]: sha(body) });
3687
+ seedEntityJournal("synced-etag");
3688
+ vi.mocked(headRemoteFile).mockResolvedValueOnce({
3689
+ lastModified: new Date(Date.now() - 120_000),
3690
+ etag: '"synced-etag"',
3691
+ size: 10,
3692
+ });
3693
+ const result = await multiWriterShare(file);
3694
+ expect(result.filesUploaded).toBe(1);
3695
+ expect(uploadFile).toHaveBeenCalledWith(expect.anything(), file, entityRel, undefined, { ifMatch: "synced-etag" });
3696
+ });
3697
+ it("an outbox'd create older than 24 hours over an absent remote is set aside, not uploaded", async () => {
3698
+ const body = bodyAt(new Date(Date.now() - 3 * 24 * 60 * 60 * 1000).toISOString());
3699
+ const file = writeEntity(body);
3700
+ writeOutbox({ [entityRel]: sha(body) });
3701
+ vi.mocked(headRemoteFile).mockResolvedValueOnce(null);
3702
+ const events = [];
3703
+ const result = await multiWriterShare(file, { onEvent: (e) => events.push(e) });
3704
+ expect(uploadFile).not.toHaveBeenCalled();
3705
+ expect(result.filesUploaded).toBe(0);
3706
+ expect(result.conflictPaths).toEqual([entityRel]);
3707
+ expect(fs.existsSync(file)).toBe(false);
3708
+ expect(fs.readFileSync(sidecarIn(path.dirname(file)), "utf-8")).toBe(body);
3709
+ expect(events).toContainEqual(expect.objectContaining({ type: "conflict", path: entityRel, resolution: "keep" }));
3710
+ });
3711
+ it("an outbox'd create with no readable last_updated over an absent remote is set aside", async () => {
3712
+ const file = writeEntity(entityBody);
3713
+ writeOutbox({ [entityRel]: sha(entityBody) });
3714
+ vi.mocked(headRemoteFile).mockResolvedValueOnce(null);
3715
+ const result = await multiWriterShare(file);
3716
+ expect(uploadFile).not.toHaveBeenCalled();
3717
+ expect(result.conflictPaths).toEqual([entityRel]);
3718
+ });
3719
+ it("a malformed outbox pushes nothing and says so", async () => {
3720
+ const body = freshBody();
3721
+ const file = writeEntity(body);
3722
+ writeOutbox({}, "{not json");
3723
+ const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
3724
+ try {
3725
+ await expect(multiWriterShare(file)).rejects.toThrow(ServerOwnedPushPathsError);
3726
+ expect(stderr.mock.calls.map((c) => String(c[0])).join("")).toContain("could not read the ontology outbox for acme");
3727
+ }
3728
+ finally {
3729
+ stderr.mockRestore();
3730
+ }
3731
+ expect(uploadFile).not.toHaveBeenCalled();
3732
+ });
3733
+ it("an outbox for another company does not open this one", async () => {
3734
+ const body = freshBody();
3735
+ const file = writeEntity(body);
3736
+ writeOutbox({}, JSON.stringify({
3737
+ schema: "hq-ontology-outbox.v1",
3738
+ company: "other",
3739
+ entries: { [entityRel]: { sha256: sha(body), written_at: new Date().toISOString() } },
3740
+ }));
3741
+ const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
3742
+ try {
3743
+ await expect(multiWriterShare(file)).rejects.toThrow(ServerOwnedPushPathsError);
3744
+ }
3745
+ finally {
3746
+ stderr.mockRestore();
3747
+ }
3748
+ expect(uploadFile).not.toHaveBeenCalled();
3749
+ });
3750
+ it("unset option: a failed read of the multi-writer flag keeps entities pull-only", async () => {
3751
+ const body = freshBody();
3752
+ const file = writeEntity(body);
3753
+ process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY = "true";
3754
+ const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
3755
+ const failingConfig = { ...mockConfig, apiUrl: "http://127.0.0.1:9", authToken: "t" };
3756
+ try {
3757
+ await expect(share({ paths: [file], company: "acme", vaultConfig: failingConfig, hqRoot: tmpDir })).rejects.toThrow(ServerOwnedPushPathsError);
3758
+ expect(stderr.mock.calls.map((c) => String(c[0])).join("")).toContain("could not read checkpoint.ontology.multi-writer");
3759
+ }
3760
+ finally {
3761
+ stderr.mockRestore();
3762
+ delete process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY;
3763
+ }
3764
+ expect(uploadFile).not.toHaveBeenCalled();
3765
+ });
3766
+ it("unset option: no flags endpoint for the multi-writer flag keeps entities pull-only", async () => {
3767
+ const body = freshBody();
3768
+ const file = writeEntity(body);
3769
+ writeOutbox({ [entityRel]: sha(body) });
3770
+ process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY = "true";
3771
+ const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
3772
+ const noEndpoint = { ...mockConfig, apiUrl: "", authToken: "t" };
3773
+ try {
3774
+ await expect(share({ paths: [file], company: "acme", vaultConfig: noEndpoint, hqRoot: tmpDir })).rejects.toThrow(ServerOwnedPushPathsError);
3775
+ expect(stderr.mock.calls.map((c) => String(c[0])).join("")).toContain("no flags endpoint or token");
3776
+ }
3777
+ finally {
3778
+ stderr.mockRestore();
3779
+ delete process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY;
3780
+ }
3781
+ expect(uploadFile).not.toHaveBeenCalled();
3782
+ });
3783
+ it("an outbox with one malformed entry pushes nothing", async () => {
3784
+ const body = freshBody();
3785
+ const file = writeEntity(body);
3786
+ writeOutbox({}, JSON.stringify({
3787
+ schema: "hq-ontology-outbox.v1",
3788
+ company: "acme",
3789
+ entries: {
3790
+ [entityRel]: { sha256: sha(body), written_at: new Date().toISOString() },
3791
+ "ontology/entities/person/other.md": { sha256: "not-a-hash", written_at: new Date().toISOString() },
3792
+ },
3793
+ }));
3794
+ const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
3795
+ try {
3796
+ await expect(multiWriterShare(file)).rejects.toThrow(ServerOwnedPushPathsError);
3797
+ expect(stderr.mock.calls.map((c) => String(c[0])).join("")).toContain("malformed entry");
3798
+ }
3799
+ finally {
3800
+ stderr.mockRestore();
3801
+ }
3802
+ expect(uploadFile).not.toHaveBeenCalled();
3803
+ });
3804
+ it("unset option: the multi-writer local override turns the outbox rule on", async () => {
3805
+ const body = freshBody();
3806
+ const file = writeEntity(body);
3807
+ process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY = "true";
3808
+ process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY_MULTI_WRITER = "true";
3809
+ try {
3810
+ await expect(share({ paths: [file], company: "acme", vaultConfig: mockConfig, hqRoot: tmpDir })).rejects.toThrow(ServerOwnedPushPathsError);
3811
+ writeOutbox({ [entityRel]: sha(body) });
3812
+ const result = await share({ paths: [file], company: "acme", vaultConfig: mockConfig, hqRoot: tmpDir });
3813
+ expect(result.filesUploaded).toBe(1);
3814
+ }
3815
+ finally {
3816
+ delete process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY;
3817
+ delete process.env.HQ_FLAG_CHECKPOINT_ONTOLOGY_MULTI_WRITER;
3818
+ }
3819
+ });
3820
+ });
3510
3821
  });
3511
3822
  it("watch push collapses 32k server-owned refusals to one exact-count event while board.json still uploads", async () => {
3512
3823
  const companyRoot = path.join(tmpDir, "companies", "acme");