@indigoai-us/hq-cloud 6.18.8 → 6.18.10-alpha.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.
Files changed (77) hide show
  1. package/dist/bin/sync-runner-args.test.d.ts +2 -0
  2. package/dist/bin/sync-runner-args.test.d.ts.map +1 -0
  3. package/dist/bin/sync-runner-args.test.js +82 -0
  4. package/dist/bin/sync-runner-args.test.js.map +1 -0
  5. package/dist/bin/sync-runner-company.d.ts +1 -0
  6. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  7. package/dist/bin/sync-runner-company.js +3 -0
  8. package/dist/bin/sync-runner-company.js.map +1 -1
  9. package/dist/bin/sync-runner-company.test.js +31 -0
  10. package/dist/bin/sync-runner-company.test.js.map +1 -1
  11. package/dist/bin/sync-runner-watch-loop.d.ts +2 -0
  12. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  13. package/dist/bin/sync-runner-watch-loop.js +103 -25
  14. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  15. package/dist/bin/sync-runner-watch-loop.test.js +71 -0
  16. package/dist/bin/sync-runner-watch-loop.test.js.map +1 -1
  17. package/dist/bin/sync-runner-watch-routes.d.ts +2 -1
  18. package/dist/bin/sync-runner-watch-routes.d.ts.map +1 -1
  19. package/dist/bin/sync-runner-watch-routes.js +14 -3
  20. package/dist/bin/sync-runner-watch-routes.js.map +1 -1
  21. package/dist/bin/sync-runner-watch-routes.test.js +15 -0
  22. package/dist/bin/sync-runner-watch-routes.test.js.map +1 -1
  23. package/dist/bin/sync-runner.d.ts +67 -0
  24. package/dist/bin/sync-runner.d.ts.map +1 -1
  25. package/dist/bin/sync-runner.js +52 -1
  26. package/dist/bin/sync-runner.js.map +1 -1
  27. package/dist/bin/sync-runner.test.js +314 -4
  28. package/dist/bin/sync-runner.test.js.map +1 -1
  29. package/dist/cli/share.d.ts.map +1 -1
  30. package/dist/cli/share.js +2 -0
  31. package/dist/cli/share.js.map +1 -1
  32. package/dist/cli/share.test.js +76 -0
  33. package/dist/cli/share.test.js.map +1 -1
  34. package/dist/cli/sync.d.ts +13 -2
  35. package/dist/cli/sync.d.ts.map +1 -1
  36. package/dist/cli/sync.js +81 -4
  37. package/dist/cli/sync.js.map +1 -1
  38. package/dist/cli/sync.test.js +228 -0
  39. package/dist/cli/sync.test.js.map +1 -1
  40. package/dist/journal.d.ts +9 -0
  41. package/dist/journal.d.ts.map +1 -1
  42. package/dist/journal.js +21 -2
  43. package/dist/journal.js.map +1 -1
  44. package/dist/journal.test.js +16 -1
  45. package/dist/journal.test.js.map +1 -1
  46. package/dist/sync/content-hash.d.ts +4 -0
  47. package/dist/sync/content-hash.d.ts.map +1 -0
  48. package/dist/sync/content-hash.js +11 -0
  49. package/dist/sync/content-hash.js.map +1 -0
  50. package/dist/sync/content-hash.test.d.ts +2 -0
  51. package/dist/sync/content-hash.test.d.ts.map +1 -0
  52. package/dist/sync/content-hash.test.js +16 -0
  53. package/dist/sync/content-hash.test.js.map +1 -0
  54. package/dist/sync/event-sync.d.ts.map +1 -1
  55. package/dist/sync/event-sync.js +58 -1
  56. package/dist/sync/event-sync.js.map +1 -1
  57. package/dist/sync/event-sync.test.js +46 -0
  58. package/dist/sync/event-sync.test.js.map +1 -1
  59. package/dist/sync/logger.test.js +3 -0
  60. package/dist/sync/logger.test.js.map +1 -1
  61. package/dist/sync/metrics.test.js +3 -0
  62. package/dist/sync/metrics.test.js.map +1 -1
  63. package/dist/sync/push-receiver.d.ts +52 -0
  64. package/dist/sync/push-receiver.d.ts.map +1 -1
  65. package/dist/sync/push-receiver.js +413 -50
  66. package/dist/sync/push-receiver.js.map +1 -1
  67. package/dist/sync/push-receiver.test.js +629 -40
  68. package/dist/sync/push-receiver.test.js.map +1 -1
  69. package/dist/sync/unchanged-push-events.d.ts +16 -0
  70. package/dist/sync/unchanged-push-events.d.ts.map +1 -0
  71. package/dist/sync/unchanged-push-events.js +51 -0
  72. package/dist/sync/unchanged-push-events.js.map +1 -0
  73. package/dist/sync/unchanged-push-events.test.d.ts +2 -0
  74. package/dist/sync/unchanged-push-events.test.d.ts.map +1 -0
  75. package/dist/sync/unchanged-push-events.test.js +76 -0
  76. package/dist/sync/unchanged-push-events.test.js.map +1 -0
  77. package/package.json +1 -1
@@ -17,7 +17,8 @@ import { TreeWatcher, resolveEventDebounceConfig, systemClock, } from "../watche
17
17
  import { recoverHqRootLocalState } from "../hq-root-recovery.js";
18
18
  import { clearLocalDeleteIntent, getStateDir, markLocalDeleteIntent, PERSONAL_VAULT_JOURNAL_SLUG, openJournalStoreSession, openJournalStoreSessionAsync, listJournalSlugs, listJournals, maintainAreaJournalSlice, disposeScopedJournalStores, releaseScopedJournalStore, releaseCachedAreaJournalMigrations, journalCacheCensusSizes, emitJournalCacheMetric, journalRows, readJournalScoped, writeJournal, } from "../journal.js";
19
19
  import { isPersonalVaultDeletionExcluded } from "../personal-vault.js";
20
- import { NoopPushReceiver, } from "../sync/push-receiver.js";
20
+ import { NoopPushReceiver, ReceiverDispatchError, isReceiverTransportFailure, } from "../sync/push-receiver.js";
21
+ import { CONTENT_HASH_PATTERN } from "../sync/push-event.js";
21
22
  import { resolveEventSync, startEventSync as defaultStartEventSync, } from "../sync/event-sync.js";
22
23
  import { resolveAreaStorageEnabled, resolveEffectiveParallelSyncRollout, resolveParallelSyncRollout, } from "../sync/feature-flags.js";
23
24
  import { resolveAreaLayoutAuthority } from "../sync/area-ledger-migration.js";
@@ -30,9 +31,11 @@ import { createHqFlagsCapSource, FullPassCapController, } from "../sync/full-pas
30
31
  import { DEFAULT_REALTIME_AUTHORIZATION_TIMEOUT_MS } from "../sync/realtime-rollout.js";
31
32
  import { RealtimeTelemetrySpan, writeRealtimeTelemetryToStderr, } from "../sync/realtime-telemetry.js";
32
33
  import { InlineSyncWorkerAdapter, } from "../sync/sync-work.js";
33
- import { buildRoutePullArgv, buildScopedPushArgv, buildScopedDrainPullArgv, buildTargetedPullArgv, isWatchRouteSelected, routeChangeToTarget, routeKey, } from "./sync-runner-watch-routes.js";
34
+ import { buildRoutePullArgv, buildScopedPushArgv, buildScopedDrainPullArgv, buildTargetedPullArgv, isWatchRouteSelected, routeChangeToTarget, routeKey, scopePathForRoute, } from "./sync-runner-watch-routes.js";
34
35
  import { resolveSkipCompanies } from "./sync-runner-planning.js";
35
36
  const FULL_RECONCILE_EVERY_TICKS = 10;
37
+ /** Maximum time shutdown waits for event-sync startup before abandoning it. */
38
+ export const EVENT_SYNC_STARTUP_SHUTDOWN_WAIT_MS = 5_000;
36
39
  /** A deferred full chore must get the next admission after this long. */
37
40
  export const CHORE_STARVATION_CAP_MS = 15 * 60 * 1000;
38
41
  /**
@@ -1197,6 +1200,33 @@ export function advanceMonotonicDeadline(previousDueAt, now, intervalMs) {
1197
1200
  nextDueAt = now + intervalMs;
1198
1201
  return { nextDueAt, delayMs: intervalMs };
1199
1202
  }
1203
+ async function withScopeContentHashesFile(route, events, scopedPaths, run) {
1204
+ const targetPaths = new Set(scopedPaths
1205
+ .map((relativePath) => scopePathForRoute(route, relativePath))
1206
+ .filter((relativePath) => relativePath !== null));
1207
+ const contentHashes = Object.create(null);
1208
+ for (const event of events) {
1209
+ if (event.kind !== "upsert" ||
1210
+ !event.contentHash ||
1211
+ !CONTENT_HASH_PATTERN.test(event.contentHash))
1212
+ continue;
1213
+ const scopePath = scopePathForRoute(route, event.relativePath);
1214
+ if (scopePath !== null && targetPaths.has(scopePath)) {
1215
+ contentHashes[scopePath] = event.contentHash;
1216
+ }
1217
+ }
1218
+ if (Object.keys(contentHashes).length === 0)
1219
+ return run();
1220
+ const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "hq-sync-pull-content-hashes-"));
1221
+ const contentHashesFile = path.join(temporaryDirectory, "hashes.json");
1222
+ try {
1223
+ fs.writeFileSync(contentHashesFile, JSON.stringify(contentHashes), { mode: 0o600 });
1224
+ return await run(contentHashesFile);
1225
+ }
1226
+ finally {
1227
+ fs.rmSync(temporaryDirectory, { recursive: true, force: true });
1228
+ }
1229
+ }
1200
1230
  /**
1201
1231
  * Select journal entries covered by one watcher delete. Exact file unlinks
1202
1232
  * deliberately avoid enumeration; only directory unlinks expand descendants.
@@ -1266,6 +1296,22 @@ function isBulkIntentOverride() {
1266
1296
  function passExitCode(outcome) {
1267
1297
  return typeof outcome === "number" ? outcome : outcome.exitCode;
1268
1298
  }
1299
+ function targetedReceiverDispatchError(outcome, accepted, authRequiredExitCode) {
1300
+ const exitCode = passExitCode(outcome);
1301
+ const result = typeof outcome === "number" ? undefined : outcome.result;
1302
+ const diagnostics = [
1303
+ ...(result?.errors ?? []).map(({ company, message }) => `${company}: ${message}`),
1304
+ ...(result?.transient ?? []).map(({ company, message }) => `${company}: ${message}`),
1305
+ ...(result?.companyFailures ?? []).map(({ company, message }) => `${company}: ${message}`),
1306
+ ];
1307
+ const failureReason = `targeted pull failed with exit code ${exitCode}, accepted=${accepted}` +
1308
+ (diagnostics.length === 0 ? "" : `: ${[...new Set(diagnostics)].join("; ")}`);
1309
+ const isTransportFailure = exitCode === authRequiredExitCode ||
1310
+ exitCode === TRANSIENT_NETWORK_EXIT ||
1311
+ (result?.transient?.length ?? 0) > 0 ||
1312
+ isReceiverTransportFailure(failureReason);
1313
+ return new ReceiverDispatchError(isTransportFailure ? "transport" : "path-specific", failureReason);
1314
+ }
1269
1315
  /**
1270
1316
  * Exit code for one scoped/catch-up route. A 404 company route returns
1271
1317
  * UNRESOLVED_COMPANY_EXIT from the runner; that must not poison the rest of
@@ -1408,6 +1454,30 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
1408
1454
  }
1409
1455
  const PENDING_APPLIED_UPSERT_MAX_PATHS = 1_024;
1410
1456
  let eventSync = null;
1457
+ let eventSyncStartup = Promise.resolve();
1458
+ let eventSyncShutdownDeadlineMs = null;
1459
+ const boundEventSyncShutdownWork = (work, abandonMessage) => {
1460
+ const deadlineMs = eventSyncShutdownDeadlineMs;
1461
+ if (deadlineMs === null) {
1462
+ return work.then(() => undefined);
1463
+ }
1464
+ let timeout;
1465
+ const deadline = new Promise((resolve) => {
1466
+ timeout = setTimeout(() => {
1467
+ process.stderr.write(`${abandonMessage}\n`);
1468
+ resolve();
1469
+ }, Math.max(0, deadlineMs - Date.now()));
1470
+ });
1471
+ return Promise.race([work.then(() => undefined), deadline]).finally(() => {
1472
+ if (timeout !== undefined)
1473
+ clearTimeout(timeout);
1474
+ });
1475
+ };
1476
+ const disposeEventSyncHandles = (handles, kind) => boundEventSyncShutdownWork(Promise.resolve()
1477
+ .then(() => handles.dispose())
1478
+ .catch((err) => {
1479
+ process.stderr.write(`event-sync ${kind} disposal failed during shutdown: ${describeError(err)}\n`);
1480
+ }), `event-sync ${kind} disposal still pending at shutdown; abandoning`);
1411
1481
  let eventSyncLive = false;
1412
1482
  // Set once the heap sampler starts; cleared on every shutdown path so the
1413
1483
  // unref'd timer can neither hold the process open nor survive into a respawn.
@@ -3339,10 +3409,12 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3339
3409
  // Submit every route in this lane before awaiting any result. In
3340
3410
  // particular, a later route's fast receipt must never wait for an
3341
3411
  // earlier route's slow pull to finish.
3342
- const completed = await Promise.all([...eventsByRoute.values()].map(({ route, events }) => runRealtimePass("receiver", "pull", events.map((event) => event.relativePath), lane, (slice, telemetry, coordinatorFenced) => runGuarded(buildTargetedPullArgv(route, passArgv, slice.paths.map((path) => path.canonicalPath)), undefined, lane, "realtime", lane === "fast", slice.signal, false, telemetry, coordinatorFenced))));
3412
+ const completed = await Promise.all([...eventsByRoute.values()].map(({ route, events }) => {
3413
+ return runRealtimePass("receiver", "pull", events.map((event) => event.relativePath), lane, (slice, telemetry, coordinatorFenced) => withScopeContentHashesFile(route, events, slice.paths.map((workPath) => workPath.canonicalPath), (contentHashesFile) => runGuarded(buildTargetedPullArgv(route, passArgv, slice.paths.map((workPath) => workPath.canonicalPath), contentHashesFile), undefined, lane, "realtime", lane === "fast", slice.signal, false, telemetry, coordinatorFenced)));
3414
+ }));
3343
3415
  for (const execution of completed) {
3344
3416
  if (!execution.accepted || passExitCode(execution.result) !== 0) {
3345
- throw new Error(`targeted pull failed with exit code ${passExitCode(execution.result)}`);
3417
+ throw targetedReceiverDispatchError(execution.result, execution.accepted, runtime.authRequiredPassExit);
3346
3418
  }
3347
3419
  }
3348
3420
  };
@@ -3377,7 +3449,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3377
3449
  }
3378
3450
  const lanes = partitionByKnownSizes(events, receiverEventSize);
3379
3451
  const drainLane = async (lane, laneEvents) => {
3380
- const pathsByRoute = new Map();
3452
+ const eventsByRoute = new Map();
3381
3453
  for (const event of laneEvents) {
3382
3454
  const route = routeChangeToTarget(event.relativePath);
3383
3455
  // The receiver is an independent ingress from the local watcher.
@@ -3387,15 +3459,15 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3387
3459
  if (!route || !isWatchRouteSelected(route, watchRouteSelection))
3388
3460
  continue;
3389
3461
  const key = routeKey(route);
3390
- const group = pathsByRoute.get(key) ?? { route, paths: [] };
3391
- group.paths.push(event.relativePath);
3392
- pathsByRoute.set(key, group);
3462
+ const group = eventsByRoute.get(key) ?? { route, events: [] };
3463
+ group.events.push(event);
3464
+ eventsByRoute.set(key, group);
3393
3465
  }
3394
- for (const { route, paths } of pathsByRoute.values()) {
3395
- const targetedArgv = buildTargetedPullArgv(route, passArgv, paths);
3396
- const result = await runGuarded(targetedArgv, undefined, lane, "realtime");
3466
+ for (const { route, events: routeEvents } of eventsByRoute.values()) {
3467
+ const paths = routeEvents.map((event) => event.relativePath);
3468
+ const result = await withScopeContentHashesFile(route, routeEvents, paths, (contentHashesFile) => runGuarded(buildTargetedPullArgv(route, passArgv, paths, contentHashesFile), undefined, lane, "realtime"));
3397
3469
  if (passExitCode(result) !== 0) {
3398
- throw new Error(`targeted pull failed with exit code ${passExitCode(result)}`);
3470
+ throw targetedReceiverDispatchError(result, true, runtime.authRequiredPassExit);
3399
3471
  }
3400
3472
  }
3401
3473
  };
@@ -3485,7 +3557,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3485
3557
  }
3486
3558
  if (resolveEventSync(process.env.HQ_SYNC_EVENT_SYNC)) {
3487
3559
  const startES = deps.startEventSync ?? defaultStartEventSync;
3488
- void (async () => {
3560
+ eventSyncStartup = (async () => {
3489
3561
  const eventSyncClient = new VaultClient({
3490
3562
  apiUrl: runtime.apiUrl,
3491
3563
  authToken: getAccessToken,
@@ -3518,7 +3590,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3518
3590
  if (!handles)
3519
3591
  return;
3520
3592
  if (stopped) {
3521
- void handles.dispose();
3593
+ await disposeEventSyncHandles(handles, "late");
3522
3594
  return;
3523
3595
  }
3524
3596
  eventSync = handles;
@@ -3539,11 +3611,13 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3539
3611
  });
3540
3612
  }
3541
3613
  }
3542
- let coordinatorShutdown = null;
3614
+ let shutdownPromise = null;
3543
3615
  const shutdown = () => {
3544
3616
  if (stopped)
3545
- return coordinatorShutdown ?? Promise.resolve();
3617
+ return shutdownPromise ?? Promise.resolve();
3546
3618
  stopped = true;
3619
+ eventSyncShutdownDeadlineMs =
3620
+ Date.now() + EVENT_SYNC_STARTUP_SHUTDOWN_WAIT_MS;
3547
3621
  resolveStoppedQueue();
3548
3622
  try {
3549
3623
  stopHeapSampling?.();
@@ -3556,20 +3630,24 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3556
3630
  // Reject queued coordinator work before tearing down ingress. Active
3557
3631
  // work receives its abort signal; receiver receipts remain unacknowledged
3558
3632
  // unless their durable pull has already completed.
3559
- coordinatorShutdown = coordinator?.shutdown() ?? Promise.resolve();
3633
+ const coordinatorDisposal = coordinator?.shutdown() ?? Promise.resolve();
3634
+ const eventSyncToDispose = eventSync;
3635
+ eventSync = null;
3636
+ const eventSyncDisposal = eventSyncToDispose
3637
+ ? disposeEventSyncHandles(eventSyncToDispose, "live")
3638
+ : Promise.resolve();
3639
+ const boundedEventSyncStartup = boundEventSyncShutdownWork(eventSyncStartup, "event-sync startup still pending at shutdown; abandoning");
3640
+ shutdownPromise = Promise.all([
3641
+ coordinatorDisposal,
3642
+ boundedEventSyncStartup,
3643
+ eventSyncDisposal,
3644
+ ]).then(() => undefined);
3560
3645
  try {
3561
3646
  void receiver?.dispose();
3562
3647
  }
3563
3648
  catch {
3564
3649
  /* ignore */
3565
3650
  }
3566
- try {
3567
- void eventSync?.dispose();
3568
- }
3569
- catch {
3570
- /* ignore */
3571
- }
3572
- eventSync = null;
3573
3651
  try {
3574
3652
  void realtimeState.scheduler?.dispose();
3575
3653
  }
@@ -3584,7 +3662,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3584
3662
  /* ignore */
3585
3663
  }
3586
3664
  resolveStopped?.();
3587
- return coordinatorShutdown;
3665
+ return shutdownPromise;
3588
3666
  };
3589
3667
  const onShutdownSignal = deps.onShutdownSignal ??
3590
3668
  ((handler) => {