@indigoai-us/hq-cloud 6.16.29 → 6.16.31

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 (43) hide show
  1. package/dist/bin/sync-runner-post-sync-flags.d.ts +22 -11
  2. package/dist/bin/sync-runner-post-sync-flags.d.ts.map +1 -1
  3. package/dist/bin/sync-runner-post-sync-flags.js +31 -44
  4. package/dist/bin/sync-runner-post-sync-flags.js.map +1 -1
  5. package/dist/bin/sync-runner-post-sync-flags.test.js +37 -77
  6. package/dist/bin/sync-runner-post-sync-flags.test.js.map +1 -1
  7. package/dist/bin/sync-runner.d.ts.map +1 -1
  8. package/dist/bin/sync-runner.js +4 -19
  9. package/dist/bin/sync-runner.js.map +1 -1
  10. package/dist/bin/sync-runner.test.js +0 -4
  11. package/dist/bin/sync-runner.test.js.map +1 -1
  12. package/dist/cli/share.test.js +44 -0
  13. package/dist/cli/share.test.js.map +1 -1
  14. package/dist/manifest/build-manifest.d.ts.map +1 -1
  15. package/dist/manifest/build-manifest.js +6 -1
  16. package/dist/manifest/build-manifest.js.map +1 -1
  17. package/dist/manifest/build-manifest.test.js +20 -1
  18. package/dist/manifest/build-manifest.test.js.map +1 -1
  19. package/dist/manifest/contract.d.ts +17 -1
  20. package/dist/manifest/contract.d.ts.map +1 -1
  21. package/dist/manifest/contract.js +23 -2
  22. package/dist/manifest/contract.js.map +1 -1
  23. package/dist/manifest/contract.test.js +43 -1
  24. package/dist/manifest/contract.test.js.map +1 -1
  25. package/dist/sync/state-store.d.ts +7 -0
  26. package/dist/sync/state-store.d.ts.map +1 -1
  27. package/dist/sync/state-store.js +187 -12
  28. package/dist/sync/state-store.js.map +1 -1
  29. package/dist/sync/state-store.test.js +111 -1
  30. package/dist/sync/state-store.test.js.map +1 -1
  31. package/dist/watch-roots.d.ts +11 -0
  32. package/dist/watch-roots.d.ts.map +1 -1
  33. package/dist/watch-roots.js +16 -7
  34. package/dist/watch-roots.js.map +1 -1
  35. package/dist/watch-roots.test.js +97 -0
  36. package/dist/watch-roots.test.js.map +1 -1
  37. package/dist/watcher.d.ts +43 -4
  38. package/dist/watcher.d.ts.map +1 -1
  39. package/dist/watcher.js +180 -36
  40. package/dist/watcher.js.map +1 -1
  41. package/dist/watcher.test.js +292 -1
  42. package/dist/watcher.test.js.map +1 -1
  43. package/package.json +1 -1
@@ -4,7 +4,7 @@ import * as os from "os";
4
4
  import * as path from "path";
5
5
  import { createHash } from "node:crypto";
6
6
  import { watch } from "chokidar";
7
- import { FakeClock, WatchPushDriver, TreeWatcher, ChokidarWatchBudget, createWatchPathFilter, toChokidarIgnored, PushEventEmitter, PUBLISH_OUTCOME_JOURNALD_CEILING_BYTES, PUBLISH_OUTCOME_SUCCESS_SAMPLE_CAP, resolveEventDebounceConfig, summarizePublishBatchOutcome, DEFAULT_EVENT_DEBOUNCE_MS, DEFAULT_EVENT_MAX_WAIT_MS, DEFAULT_EVENT_SMALL_BATCH_DEBOUNCE_MS, DEFAULT_EVENT_SMALL_BATCH_MAX_PATHS, DEFAULT_EVENT_SMALL_BATCH_MAX_BYTES, DEFAULT_PUBLISH_MIN_INTERVAL_MS, EVENT_DEBOUNCE_MS_ENV, EVENT_MAX_WAIT_MS_ENV, EVENT_SMALL_BATCH_DEBOUNCE_MS_ENV, EVENT_SMALL_BATCH_MAX_PATHS_ENV, EVENT_SMALL_BATCH_MAX_BYTES_ENV, DEFAULT_TREE_WATCHER_MAX_WATCHED_PATHS, MAX_TREE_WATCHER_MAX_WATCHED_PATHS, TREE_WATCHER_MAX_WATCHED_PATHS_ENV, resolveMaxWatchedPaths, PUBLISH_MIN_INTERVAL_MS_ENV, resolvePublishMinIntervalMs, DurablePublishedContentHashes, DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS, replayDurablePublishedContentHashRecordsForTest } from "./watcher.js";
7
+ import { FakeClock, WatchPushDriver, TreeWatcher, ChokidarWatchBudget, createWatchPathFilter, toChokidarIgnored, PushEventEmitter, PUBLISH_OUTCOME_JOURNALD_CEILING_BYTES, PUBLISH_OUTCOME_SUCCESS_SAMPLE_CAP, resolveEventDebounceConfig, summarizePublishBatchOutcome, DEFAULT_EVENT_DEBOUNCE_MS, DEFAULT_EVENT_MAX_WAIT_MS, DEFAULT_EVENT_SMALL_BATCH_DEBOUNCE_MS, DEFAULT_EVENT_SMALL_BATCH_MAX_PATHS, DEFAULT_EVENT_SMALL_BATCH_MAX_BYTES, DEFAULT_PUBLISH_MIN_INTERVAL_MS, EVENT_DEBOUNCE_MS_ENV, EVENT_MAX_WAIT_MS_ENV, EVENT_SMALL_BATCH_DEBOUNCE_MS_ENV, EVENT_SMALL_BATCH_MAX_PATHS_ENV, EVENT_SMALL_BATCH_MAX_BYTES_ENV, DEFAULT_TREE_WATCHER_MAX_WATCHED_PATHS, MAX_TREE_WATCHER_MAX_WATCHED_PATHS, TREE_WATCHER_MAX_WATCHED_PATHS_ENV, resolveMaxWatchedPaths, PUBLISH_MIN_INTERVAL_MS_ENV, resolvePublishMinIntervalMs, DurablePublishedContentHashes, DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS, replayDurablePublishedContentHashRecordsForTest, startScopedRecursiveWatch, selectTreeWatchBackend } from "./watcher.js";
8
8
  import { StaticFlagProvider } from "./sync/feature-flags.js";
9
9
  import { StateStore, StateStoreLockError } from "./sync/state-store.js";
10
10
  import { RealtimeUnavailableError, } from "./sync/push-transport.js";
@@ -3051,4 +3051,295 @@ describe("resolvePublishMinIntervalMs", () => {
3051
3051
  expect(resolvePublishMinIntervalMs({ [PUBLISH_MIN_INTERVAL_MS_ENV]: "1234.9" })).toBe(1234);
3052
3052
  });
3053
3053
  });
3054
+ /**
3055
+ * Linux folder-only backend.
3056
+ *
3057
+ * On Linux there is no recursive `fs.watch`, and chokidar's answer was one
3058
+ * inotify watch per directory PLUS one per file. On a real HQ root that was
3059
+ * ~74k directories and ~878k files — ~950k watches, ~16 KB each — while the
3060
+ * safety cap counted only the directories and so never fired. A directory
3061
+ * watch already reports content changes to the files directly inside it, so
3062
+ * the per-file watches bought nothing. These tests pin the replacement: the
3063
+ * scoped native backend driven with `recursiveWatches: false`, taking exactly
3064
+ * one shallow watch per in-scope directory and none per file.
3065
+ */
3066
+ describe("Linux folder-only native backend (startScopedRecursiveWatch, recursiveWatches: false)", () => {
3067
+ let root;
3068
+ const isLinux = process.platform === "linux";
3069
+ beforeEach(() => {
3070
+ root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "folder-watch-")));
3071
+ });
3072
+ afterEach(() => {
3073
+ fs.rmSync(root, { recursive: true, force: true });
3074
+ });
3075
+ /** In-scope tree: root, docs, docs/deep, notes — 4 dirs, 8 files, plus an excluded node_modules. */
3076
+ function seedTree() {
3077
+ const dirs = [
3078
+ path.join(root, "docs"),
3079
+ path.join(root, "docs", "deep"),
3080
+ path.join(root, "notes"),
3081
+ ];
3082
+ for (const d of dirs)
3083
+ fs.mkdirSync(d, { recursive: true });
3084
+ const files = [];
3085
+ for (const d of [root, ...dirs]) {
3086
+ for (const n of ["one.md", "two.md"]) {
3087
+ const f = path.join(d, n);
3088
+ fs.writeFileSync(f, "x");
3089
+ files.push(f);
3090
+ }
3091
+ }
3092
+ const nm = path.join(root, "docs", "node_modules", "pkg");
3093
+ fs.mkdirSync(nm, { recursive: true });
3094
+ fs.writeFileSync(path.join(nm, "index.js"), "1");
3095
+ return { inScopeDirs: [root, ...dirs], files };
3096
+ }
3097
+ /** Count this process's live inotify watch descriptors (Linux only). */
3098
+ function inotifyWatchDescriptors() {
3099
+ let total = 0;
3100
+ for (const fd of fs.readdirSync("/proc/self/fdinfo")) {
3101
+ try {
3102
+ const info = fs.readFileSync(path.join("/proc/self/fdinfo", fd), "utf8");
3103
+ total += (info.match(/^inotify wd:/gm) ?? []).length;
3104
+ }
3105
+ catch {
3106
+ /* fd closed between readdir and read */
3107
+ }
3108
+ }
3109
+ return total;
3110
+ }
3111
+ async function waitFor(check, ms = 4000) {
3112
+ const deadline = Date.now() + ms;
3113
+ while (Date.now() < deadline) {
3114
+ if (check())
3115
+ return;
3116
+ await new Promise((r) => setTimeout(r, 25));
3117
+ }
3118
+ expect(check()).toBe(true);
3119
+ }
3120
+ function startFolderOnly(opts = {}) {
3121
+ const shouldEmit = createWatchPathFilter(root, false);
3122
+ return startScopedRecursiveWatch(root, shouldEmit, opts.onEvent ?? (() => { }), opts.onError ?? ((err) => { throw err; }), undefined, {
3123
+ recursiveWatches: false,
3124
+ maxWatchedPaths: opts.maxWatchedPaths,
3125
+ onWatchBudgetExceeded: opts.onWatchBudgetExceeded,
3126
+ listChildDirs: opts.listChildDirs,
3127
+ });
3128
+ }
3129
+ it("takes exactly one watch per in-scope directory and none per file", () => {
3130
+ const { inScopeDirs } = seedTree();
3131
+ const before = isLinux ? inotifyWatchDescriptors() : 0;
3132
+ const backend = startFolderOnly();
3133
+ try {
3134
+ expect(backend.mode).toBe("native");
3135
+ expect(backend.attachFailures).toBe(0);
3136
+ // 4 in-scope directories; 8 in-scope files and an excluded subtree add nothing.
3137
+ expect(backend.watchedPathCount()).toBe(inScopeDirs.length);
3138
+ if (isLinux) {
3139
+ // Measured immediately, before any drain window: a whole-tree
3140
+ // bootstrap watch would show up here as extra descriptors.
3141
+ expect(inotifyWatchDescriptors() - before).toBe(inScopeDirs.length);
3142
+ }
3143
+ }
3144
+ finally {
3145
+ backend.close();
3146
+ }
3147
+ if (isLinux)
3148
+ expect(inotifyWatchDescriptors()).toBe(before);
3149
+ });
3150
+ it("reports a content change to a file inside a watched folder without a file watch", async () => {
3151
+ const { files } = seedTree();
3152
+ const seen = [];
3153
+ const backend = startFolderOnly({ onEvent: (p) => seen.push(p) });
3154
+ try {
3155
+ await new Promise((r) => setTimeout(r, 150));
3156
+ const target = files.find((f) => f.includes(path.join("docs", "deep")));
3157
+ fs.appendFileSync(target, "more");
3158
+ await waitFor(() => seen.includes(target));
3159
+ }
3160
+ finally {
3161
+ backend.close();
3162
+ }
3163
+ });
3164
+ it("attaches a folder created after start, then reports files written inside it", async () => {
3165
+ seedTree();
3166
+ const seen = [];
3167
+ const backend = startFolderOnly({ onEvent: (p) => seen.push(p) });
3168
+ try {
3169
+ await new Promise((r) => setTimeout(r, 150));
3170
+ const before = backend.watchedPathCount();
3171
+ const created = path.join(root, "notes", "later");
3172
+ fs.mkdirSync(created);
3173
+ await waitFor(() => backend.watchedPathCount() === before + 1);
3174
+ const file = path.join(created, "fresh.md");
3175
+ fs.writeFileSync(file, "hello");
3176
+ await waitFor(() => seen.includes(file));
3177
+ }
3178
+ finally {
3179
+ backend.close();
3180
+ }
3181
+ });
3182
+ it("scans a folder that already holds files when it attaches, so nothing arrives silently", async () => {
3183
+ seedTree();
3184
+ const seen = [];
3185
+ const backend = startFolderOnly({ onEvent: (p) => seen.push(p) });
3186
+ try {
3187
+ await new Promise((r) => setTimeout(r, 150));
3188
+ // Build the subtree OUTSIDE the watched root, then move it in with one
3189
+ // rename: the folder watch only ever sees the directory appear, never
3190
+ // the files inside it. Chokidar walked such a directory on addDir;
3191
+ // the folder-only backend must do the same.
3192
+ const staging = fs.mkdtempSync(path.join(os.tmpdir(), "folder-watch-stage-"));
3193
+ fs.mkdirSync(path.join(staging, "inner"));
3194
+ fs.writeFileSync(path.join(staging, "a.md"), "a");
3195
+ fs.writeFileSync(path.join(staging, "inner", "b.md"), "b");
3196
+ const dest = path.join(root, "docs", "moved");
3197
+ fs.renameSync(staging, dest);
3198
+ await waitFor(() => seen.includes(path.join(dest, "a.md")) &&
3199
+ seen.includes(path.join(dest, "inner", "b.md")));
3200
+ expect(backend.watchedPathCount()).toBe(4 + 2);
3201
+ }
3202
+ finally {
3203
+ backend.close();
3204
+ }
3205
+ });
3206
+ it("re-attaches when a watched folder is swapped for a new one at the same path (Linux inode change)", async () => {
3207
+ seedTree();
3208
+ const seen = [];
3209
+ const backend = startFolderOnly({ onEvent: (p) => seen.push(p) });
3210
+ try {
3211
+ await new Promise((r) => setTimeout(r, 150));
3212
+ expect(backend.watchedPathCount()).toBe(4);
3213
+ // Atomic swap: the old `docs` moves out of the tree and a replacement
3214
+ // (built outside, so its creation is never observed) lands at the same
3215
+ // pathname — both renames complete before any watch callback runs. On
3216
+ // Linux the old handle follows the moved-away INODE, so without an
3217
+ // identity check the backend would keep it, consider `docs` covered,
3218
+ // and never hear another event from the replacement subtree.
3219
+ const staging = fs.mkdtempSync(path.join(os.tmpdir(), "folder-watch-swap-"));
3220
+ fs.mkdirSync(path.join(staging, "deep"));
3221
+ fs.writeFileSync(path.join(staging, "a.md"), "a");
3222
+ fs.writeFileSync(path.join(staging, "deep", "b.md"), "b");
3223
+ const parked = fs.mkdtempSync(path.join(os.tmpdir(), "folder-watch-parked-"));
3224
+ const docs = path.join(root, "docs");
3225
+ fs.renameSync(docs, path.join(parked, "docs"));
3226
+ fs.renameSync(staging, docs);
3227
+ // The replacement's existing files are announced, exactly like a
3228
+ // populated folder moved in from outside.
3229
+ await waitFor(() => seen.includes(path.join(docs, "a.md")) &&
3230
+ seen.includes(path.join(docs, "deep", "b.md")));
3231
+ // A later write inside the replacement is reported: the new inode is
3232
+ // watched, and the stale handles (docs + docs/deep) are gone — still
3233
+ // root, docs, docs/deep, notes.
3234
+ const after = path.join(docs, "deep", "after.md");
3235
+ fs.writeFileSync(after, "after");
3236
+ await waitFor(() => seen.includes(after));
3237
+ expect(backend.watchedPathCount()).toBe(4);
3238
+ // The moved-away tree must not keep reporting under its OLD path.
3239
+ seen.length = 0;
3240
+ fs.writeFileSync(path.join(parked, "docs", "stale.md"), "stale");
3241
+ await new Promise((r) => setTimeout(r, 300));
3242
+ expect(seen.filter((p) => p.startsWith(docs + path.sep))).toEqual([]);
3243
+ fs.rmSync(parked, { recursive: true, force: true });
3244
+ }
3245
+ finally {
3246
+ backend.close();
3247
+ }
3248
+ });
3249
+ it("covers a folder created after its parent was listed but before the plan attached (walk/attach race)", async () => {
3250
+ seedTree();
3251
+ const seen = [];
3252
+ const late = path.join(root, "notes", "late");
3253
+ // The plan walk lists `notes`, then — before any watch existed on the
3254
+ // old code path — `notes/late` appears. The snapshot plan never holds
3255
+ // it, and a shallow watch on `notes` attached only after the whole walk
3256
+ // would never have seen it appear. On a real HQ root the walk takes
3257
+ // seconds; agents create directories the whole time.
3258
+ const listChildDirs = (dir) => {
3259
+ const children = fs
3260
+ .readdirSync(dir, { withFileTypes: true })
3261
+ .filter((e) => e.isDirectory())
3262
+ .map((e) => path.join(dir, e.name));
3263
+ if (dir === path.join(root, "notes"))
3264
+ fs.mkdirSync(late);
3265
+ return children;
3266
+ };
3267
+ const backend = startFolderOnly({ onEvent: (p) => seen.push(p), listChildDirs });
3268
+ try {
3269
+ // root, docs, docs/deep, notes — plus `late`, picked up by the live
3270
+ // watch on `notes` rather than by the plan.
3271
+ await waitFor(() => backend.watchedPathCount() === 5);
3272
+ const file = path.join(late, "inside.md");
3273
+ fs.writeFileSync(file, "x");
3274
+ await waitFor(() => seen.includes(file));
3275
+ }
3276
+ finally {
3277
+ backend.close();
3278
+ }
3279
+ });
3280
+ it("releases every watch beneath a deleted folder", async () => {
3281
+ seedTree();
3282
+ const backend = startFolderOnly();
3283
+ try {
3284
+ await new Promise((r) => setTimeout(r, 150));
3285
+ expect(backend.watchedPathCount()).toBe(4);
3286
+ fs.rmSync(path.join(root, "docs"), { recursive: true, force: true });
3287
+ // docs and docs/deep both go; root and notes remain.
3288
+ await waitFor(() => backend.watchedPathCount() === 2);
3289
+ if (isLinux) {
3290
+ await waitFor(() => inotifyWatchDescriptors() <= 2);
3291
+ }
3292
+ }
3293
+ finally {
3294
+ backend.close();
3295
+ }
3296
+ });
3297
+ it("stops attaching at the budget and reports the exhaustion exactly once, counting real watches", () => {
3298
+ seedTree();
3299
+ const exceeded = vi.fn();
3300
+ const backend = startFolderOnly({
3301
+ maxWatchedPaths: 2,
3302
+ onWatchBudgetExceeded: exceeded,
3303
+ });
3304
+ try {
3305
+ expect(backend.watchedPathCount()).toBe(2);
3306
+ expect(exceeded).toHaveBeenCalledTimes(1);
3307
+ expect(exceeded.mock.calls[0][0]).toMatchObject({
3308
+ watchedPaths: 2,
3309
+ maxWatchedPaths: 2,
3310
+ });
3311
+ expect(typeof exceeded.mock.calls[0][0].offendingPath).toBe("string");
3312
+ }
3313
+ finally {
3314
+ backend.close();
3315
+ }
3316
+ });
3317
+ it("selects the folder-only native backend on linux and leaves darwin/win32 on recursive native", () => {
3318
+ expect(selectTreeWatchBackend("linux", false)).toBe("native-shallow");
3319
+ expect(selectTreeWatchBackend("darwin", false)).toBe("native-recursive");
3320
+ expect(selectTreeWatchBackend("win32", false)).toBe("native-recursive");
3321
+ expect(selectTreeWatchBackend("freebsd", false)).toBe("chokidar");
3322
+ expect(selectTreeWatchBackend("linux", true)).toBe("polling");
3323
+ expect(selectTreeWatchBackend("darwin", true)).toBe("polling");
3324
+ });
3325
+ it("TreeWatcher on linux starts natively with one watch per directory (no chokidar, no file watches)", async () => {
3326
+ if (!isLinux)
3327
+ return;
3328
+ const { inScopeDirs } = seedTree();
3329
+ const before = inotifyWatchDescriptors();
3330
+ const w = new TreeWatcher({ hqRoot: root, debounceMs: 50 });
3331
+ const changed = vi.fn();
3332
+ w.onChange(changed);
3333
+ w.start();
3334
+ try {
3335
+ expect(w.watchMode()).toBe("native");
3336
+ expect(inotifyWatchDescriptors() - before).toBe(inScopeDirs.length);
3337
+ fs.writeFileSync(path.join(root, "notes", "new.md"), "hi");
3338
+ await waitFor(() => changed.mock.calls.length > 0);
3339
+ }
3340
+ finally {
3341
+ w.dispose();
3342
+ }
3343
+ });
3344
+ });
3054
3345
  //# sourceMappingURL=watcher.test.js.map