@indigoai-us/hq-cloud 5.24.0 → 5.26.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 (66) hide show
  1. package/dist/bin/sync-runner.d.ts +151 -17
  2. package/dist/bin/sync-runner.d.ts.map +1 -1
  3. package/dist/bin/sync-runner.js +280 -18
  4. package/dist/bin/sync-runner.js.map +1 -1
  5. package/dist/bin/sync-runner.test.js +429 -15
  6. package/dist/bin/sync-runner.test.js.map +1 -1
  7. package/dist/cli/share.d.ts +9 -0
  8. package/dist/cli/share.d.ts.map +1 -1
  9. package/dist/cli/share.js +54 -1
  10. package/dist/cli/share.js.map +1 -1
  11. package/dist/cli/share.test.js +6 -3
  12. package/dist/cli/share.test.js.map +1 -1
  13. package/dist/cli/sync.d.ts +21 -0
  14. package/dist/cli/sync.d.ts.map +1 -1
  15. package/dist/cli/sync.js.map +1 -1
  16. package/dist/index.d.ts +4 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +6 -0
  19. package/dist/index.js.map +1 -1
  20. package/dist/personal-vault-exclusions.d.ts +128 -0
  21. package/dist/personal-vault-exclusions.d.ts.map +1 -0
  22. package/dist/personal-vault-exclusions.js +231 -0
  23. package/dist/personal-vault-exclusions.js.map +1 -0
  24. package/dist/personal-vault-exclusions.test.d.ts +22 -0
  25. package/dist/personal-vault-exclusions.test.d.ts.map +1 -0
  26. package/dist/personal-vault-exclusions.test.js +198 -0
  27. package/dist/personal-vault-exclusions.test.js.map +1 -0
  28. package/dist/sync/index.d.ts +11 -0
  29. package/dist/sync/index.d.ts.map +1 -0
  30. package/dist/sync/index.js +9 -0
  31. package/dist/sync/index.js.map +1 -0
  32. package/dist/sync/push-event.d.ts +110 -0
  33. package/dist/sync/push-event.d.ts.map +1 -0
  34. package/dist/sync/push-event.js +153 -0
  35. package/dist/sync/push-event.js.map +1 -0
  36. package/dist/sync/push-event.test.d.ts +15 -0
  37. package/dist/sync/push-event.test.d.ts.map +1 -0
  38. package/dist/sync/push-event.test.js +188 -0
  39. package/dist/sync/push-event.test.js.map +1 -0
  40. package/dist/sync/push-transport.d.ts +67 -0
  41. package/dist/sync/push-transport.d.ts.map +1 -0
  42. package/dist/sync/push-transport.js +66 -0
  43. package/dist/sync/push-transport.js.map +1 -0
  44. package/dist/watcher.d.ts +160 -0
  45. package/dist/watcher.d.ts.map +1 -1
  46. package/dist/watcher.js +298 -0
  47. package/dist/watcher.js.map +1 -1
  48. package/dist/watcher.test.d.ts +2 -0
  49. package/dist/watcher.test.d.ts.map +1 -0
  50. package/dist/watcher.test.js +334 -0
  51. package/dist/watcher.test.js.map +1 -0
  52. package/package.json +3 -2
  53. package/src/bin/sync-runner.test.ts +557 -15
  54. package/src/bin/sync-runner.ts +404 -27
  55. package/src/cli/share.test.ts +8 -3
  56. package/src/cli/share.ts +66 -1
  57. package/src/cli/sync.ts +22 -0
  58. package/src/index.ts +27 -0
  59. package/src/personal-vault-exclusions.test.ts +256 -0
  60. package/src/personal-vault-exclusions.ts +277 -0
  61. package/src/sync/index.ts +19 -0
  62. package/src/sync/push-event.test.ts +224 -0
  63. package/src/sync/push-event.ts +208 -0
  64. package/src/sync/push-transport.ts +84 -0
  65. package/src/watcher.test.ts +388 -0
  66. package/src/watcher.ts +386 -0
package/src/watcher.ts CHANGED
@@ -16,9 +16,185 @@ import { createIgnoreFilter, isWithinSizeLimit } from "./ignore.js";
16
16
  import { readJournal, writeJournal, hashFile, updateEntry } from "./journal.js";
17
17
  import { uploadFile, deleteRemoteFile } from "./s3.js";
18
18
  import type { UploadAuthor } from "./s3.js";
19
+ import { isPersonalVaultExcluded } from "./personal-vault-exclusions.js";
20
+ import { PERSONAL_VAULT_EXCLUDED_TOP_LEVEL } from "./personal-vault.js";
19
21
 
20
22
  const DEBOUNCE_MS = 2000;
21
23
 
24
+ /**
25
+ * Injectable clock seam (US-001).
26
+ *
27
+ * Production code uses the host timers; tests inject a {@link FakeClock} so the
28
+ * debounce window can be advanced deterministically without real wall-clock
29
+ * sleeps. US-002 (real chokidar watcher) and US-003 (runner wiring) build on
30
+ * this same seam — keep the surface minimal and stable.
31
+ */
32
+ export interface Clock {
33
+ /** Schedule `fn` to run after `ms`. Returns an opaque handle. */
34
+ setTimeout(fn: () => void, ms: number): unknown;
35
+ /** Cancel a previously scheduled timeout. Safe to call with a stale handle. */
36
+ clearTimeout(handle: unknown): void;
37
+ /** Current epoch milliseconds. */
38
+ now(): number;
39
+ }
40
+
41
+ /** Real clock backed by host timers + Date.now. The production default. */
42
+ export const systemClock: Clock = {
43
+ setTimeout: (fn, ms) => setTimeout(fn, ms),
44
+ clearTimeout: (handle) => clearTimeout(handle as ReturnType<typeof setTimeout>),
45
+ now: () => Date.now(),
46
+ };
47
+
48
+ interface FakeTimer {
49
+ id: number;
50
+ fireAt: number;
51
+ fn: () => void;
52
+ }
53
+
54
+ /**
55
+ * Deterministic clock for tests. Advance virtual time with {@link advance};
56
+ * any timers whose deadline has passed fire in scheduled order. No real timers
57
+ * are ever created, so a test using only this clock leaks nothing.
58
+ */
59
+ export class FakeClock implements Clock {
60
+ private current = 0;
61
+ private nextId = 1;
62
+ private timers = new Map<number, FakeTimer>();
63
+
64
+ now(): number {
65
+ return this.current;
66
+ }
67
+
68
+ setTimeout(fn: () => void, ms: number): unknown {
69
+ const id = this.nextId++;
70
+ this.timers.set(id, { id, fireAt: this.current + Math.max(0, ms), fn });
71
+ return id;
72
+ }
73
+
74
+ clearTimeout(handle: unknown): void {
75
+ if (typeof handle === "number") this.timers.delete(handle);
76
+ }
77
+
78
+ /**
79
+ * Advance virtual time by `ms`, firing every timer whose deadline falls in
80
+ * the interval (in deadline order). Timers scheduled by a firing callback are
81
+ * honored within the same advance if their new deadline is still within the
82
+ * advanced window.
83
+ */
84
+ advance(ms: number): void {
85
+ const target = this.current + ms;
86
+ while (true) {
87
+ const due = [...this.timers.values()]
88
+ .filter((t) => t.fireAt <= target)
89
+ .sort((a, b) => a.fireAt - b.fireAt || a.id - b.id);
90
+ if (due.length === 0) break;
91
+ const next = due[0];
92
+ this.timers.delete(next.id);
93
+ this.current = Math.max(this.current, next.fireAt);
94
+ next.fn();
95
+ }
96
+ this.current = target;
97
+ }
98
+
99
+ /** Number of timers still pending — a leak check for tests. */
100
+ pendingTimerCount(): number {
101
+ return this.timers.size;
102
+ }
103
+ }
104
+
105
+ /** A push pass to run when a debounced change settles. May be async. */
106
+ export type PushFn = () => void | Promise<void>;
107
+
108
+ export interface WatchPushDriverOptions {
109
+ /** Quiet window (ms) before a settled change triggers a push. */
110
+ debounceMs?: number;
111
+ /** Clock seam — defaults to {@link systemClock}; tests inject {@link FakeClock}. */
112
+ clock?: Clock;
113
+ /** Push pass to invoke when the window settles. */
114
+ push: PushFn;
115
+ }
116
+
117
+ /**
118
+ * The reusable debounce + coalesce + concurrency-guard core of event-driven
119
+ * push (US-001 seam).
120
+ *
121
+ * It is intentionally decoupled from chokidar and from S3: callers feed it
122
+ * synthetic or real change notifications via {@link notifyChange}, and it
123
+ * invokes the injected `push` fn at most once per quiet window. A push that is
124
+ * still in flight is never overlapped — a change arriving mid-push is collapsed
125
+ * and re-triggers a single follow-up pass after the next quiet window.
126
+ *
127
+ * US-002 wires a real chokidar watcher's events into {@link notifyChange};
128
+ * US-003 supplies the targeted-push `push` fn. Tests drive it directly with a
129
+ * {@link FakeClock} and a spy `push` fn (no real S3, no 10-minute sleep).
130
+ */
131
+ export class WatchPushDriver {
132
+ private readonly debounceMs: number;
133
+ private readonly clock: Clock;
134
+ private readonly push: PushFn;
135
+ private timer: unknown = null;
136
+ private pushing = false;
137
+ private pendingWhilePushing = false;
138
+ private disposed = false;
139
+
140
+ constructor(opts: WatchPushDriverOptions) {
141
+ this.debounceMs = opts.debounceMs ?? DEBOUNCE_MS;
142
+ this.clock = opts.clock ?? systemClock;
143
+ this.push = opts.push;
144
+ }
145
+
146
+ /**
147
+ * Register a change. Resets the quiet window; the push fires `debounceMs`
148
+ * after the LAST change in a burst, coalescing the burst to one push.
149
+ */
150
+ notifyChange(): void {
151
+ if (this.disposed) return;
152
+ if (this.timer !== null) {
153
+ this.clock.clearTimeout(this.timer);
154
+ this.timer = null;
155
+ }
156
+ this.timer = this.clock.setTimeout(() => {
157
+ this.timer = null;
158
+ void this.fire();
159
+ }, this.debounceMs);
160
+ }
161
+
162
+ private async fire(): Promise<void> {
163
+ if (this.disposed) return;
164
+ if (this.pushing) {
165
+ // A pass is in flight — collapse this trigger; re-arm after it settles.
166
+ this.pendingWhilePushing = true;
167
+ return;
168
+ }
169
+ this.pushing = true;
170
+ try {
171
+ await this.push();
172
+ } finally {
173
+ this.pushing = false;
174
+ if (this.pendingWhilePushing && !this.disposed) {
175
+ this.pendingWhilePushing = false;
176
+ // Re-arm a fresh quiet window for the change(s) seen mid-push.
177
+ this.notifyChange();
178
+ }
179
+ }
180
+ }
181
+
182
+ /** True while a push pass is executing. */
183
+ isPushing(): boolean {
184
+ return this.pushing;
185
+ }
186
+
187
+ /** Cancel any pending debounce timer; idempotent. Leaves no timers behind. */
188
+ dispose(): void {
189
+ this.disposed = true;
190
+ if (this.timer !== null) {
191
+ this.clock.clearTimeout(this.timer);
192
+ this.timer = null;
193
+ }
194
+ this.pendingWhilePushing = false;
195
+ }
196
+ }
197
+
22
198
  interface PendingChange {
23
199
  type: "add" | "change" | "unlink";
24
200
  absolutePath: string;
@@ -146,3 +322,213 @@ export class SyncWatcher {
146
322
  }
147
323
  }
148
324
  }
325
+
326
+ // ---------------------------------------------------------------------------
327
+ // US-002 — debounced, ignore-aware, exclusion-aware tree watcher.
328
+ //
329
+ // Unlike SyncWatcher (which does S3 work inline), TreeWatcher is a pure change
330
+ // detector: it emits a single debounced `changed` callback after a quiet window
331
+ // and never touches S3 itself. US-003 wires that callback to a targeted push.
332
+ //
333
+ // The emit decision composes the SAME filter stack the push walk uses, so a
334
+ // path that the push would skip never wakes the watcher:
335
+ // 1. createIgnoreFilter — .hqignore/.gitignore/DEFAULT_IGNORES
336
+ // 2. PERSONAL_VAULT_DEFAULT_EXCLUSIONS (personalMode) — .env/output/.beads/…
337
+ // 3. PERSONAL_VAULT_EXCLUDED_TOP_LEVEL (personalMode) — .git/companies/repos/workspace
338
+ // ---------------------------------------------------------------------------
339
+
340
+ /** Decision for a single path: emit a change for it, or ignore it. */
341
+ export type WatchPathFilter = (absolutePath: string, isDir?: boolean) => boolean;
342
+
343
+ /**
344
+ * Build the composite emit-decision predicate. Returns true when a change to
345
+ * `absolutePath` SHOULD wake the watcher (i.e. it survives every exclusion
346
+ * layer). Pure and chokidar-free so the matching logic is unit-testable
347
+ * directly.
348
+ *
349
+ * @param hqRoot sync root (== personal-vault root in personalMode).
350
+ * @param personalMode when true, also applies the personal-vault default
351
+ * exclusions and the excluded-top-level buckets.
352
+ */
353
+ export function createWatchPathFilter(
354
+ hqRoot: string,
355
+ personalMode = false,
356
+ ): WatchPathFilter {
357
+ const ignoreFilter = createIgnoreFilter(hqRoot);
358
+ const excludedTopLevel = new Set(PERSONAL_VAULT_EXCLUDED_TOP_LEVEL);
359
+
360
+ return (absolutePath: string, isDir = false): boolean => {
361
+ const rel = path.relative(hqRoot, absolutePath).split(path.sep).join("/");
362
+ // The root itself, or anything outside it, is never an emit target.
363
+ if (rel === "" || rel.startsWith("..")) return false;
364
+
365
+ // Layer 1: shared ignore stack (.hqignore/.gitignore/DEFAULT_IGNORES).
366
+ // createIgnoreFilter returns true = "sync this"; false = "ignored".
367
+ if (!ignoreFilter(absolutePath, isDir)) return false;
368
+
369
+ if (personalMode) {
370
+ // Layer 3: excluded top-level buckets (.git/companies/repos/workspace).
371
+ const topLevel = rel.split("/")[0];
372
+ if (excludedTopLevel.has(topLevel)) return false;
373
+
374
+ // Layer 2: personal-vault default exclusions (.env/output/.beads/…).
375
+ if (isPersonalVaultExcluded(rel, isDir)) return false;
376
+ }
377
+
378
+ return true;
379
+ };
380
+ }
381
+
382
+ export interface TreeWatcherOptions {
383
+ /** Sync root to watch (== personal-vault root in personalMode). */
384
+ hqRoot: string;
385
+ /** Quiet window (ms) before a settled burst emits one `changed` call. */
386
+ debounceMs?: number;
387
+ /** Apply personal-vault default + top-level exclusions. */
388
+ personalMode?: boolean;
389
+ /** Clock seam — defaults to {@link systemClock}; tests inject {@link FakeClock}. */
390
+ clock?: Clock;
391
+ /**
392
+ * Pre-built path filter override (test seam). When omitted, one is built
393
+ * from {@link createWatchPathFilter}.
394
+ */
395
+ pathFilter?: WatchPathFilter;
396
+ }
397
+
398
+ /**
399
+ * Chokidar-backed file watcher that emits a single debounced `changed` signal
400
+ * after a {@link debounceMs} quiet window, coalescing bursts. It honors the
401
+ * full exclusion stack via {@link createWatchPathFilter}, so excluded paths
402
+ * (`.env`, `output/`, `.git/`, `companies/` in personalMode, …) never emit.
403
+ *
404
+ * Lifecycle: {@link start} (idempotent), {@link stop}, {@link dispose}. Stop
405
+ * closes the chokidar watcher (releasing fds) and cancels any pending debounce
406
+ * timer. dispose() is stop() + permanent shutdown.
407
+ */
408
+ export class TreeWatcher {
409
+ private readonly hqRoot: string;
410
+ private readonly debounceMs: number;
411
+ private readonly clock: Clock;
412
+ private readonly shouldEmit: WatchPathFilter;
413
+ private watcher: FSWatcher | null = null;
414
+ private timer: unknown = null;
415
+ private listeners = new Set<() => void>();
416
+ private disposed = false;
417
+
418
+ constructor(opts: TreeWatcherOptions) {
419
+ this.hqRoot = opts.hqRoot;
420
+ this.debounceMs = opts.debounceMs ?? DEBOUNCE_MS;
421
+ this.clock = opts.clock ?? systemClock;
422
+ this.shouldEmit =
423
+ opts.pathFilter ?? createWatchPathFilter(opts.hqRoot, opts.personalMode ?? false);
424
+ }
425
+
426
+ /** Register a debounced-`changed` listener. Returns an unsubscribe fn. */
427
+ onChange(listener: () => void): () => void {
428
+ this.listeners.add(listener);
429
+ return () => this.listeners.delete(listener);
430
+ }
431
+
432
+ /**
433
+ * Begin watching. Idempotent — a second call while already running is a
434
+ * no-op (no second chokidar instance, no leaked fds).
435
+ */
436
+ start(): void {
437
+ if (this.disposed || this.watcher) return;
438
+
439
+ this.watcher = watch(this.hqRoot, {
440
+ // chokidar passes a stats hint so dir-only gitignore patterns (`foo/`)
441
+ // match directory entries during the descent decision. `ignored`
442
+ // returns true to SKIP, so invert the emit predicate. The watched root
443
+ // itself must NEVER be ignored (shouldEmit returns false for it because
444
+ // it is not a valid emit target), or chokidar tears down the whole watch.
445
+ ignored: (filePath: string, stats?: fs.Stats) => {
446
+ if (path.resolve(filePath) === path.resolve(this.hqRoot)) return false;
447
+ return !this.shouldEmit(filePath, stats?.isDirectory());
448
+ },
449
+ persistent: true,
450
+ ignoreInitial: true,
451
+ awaitWriteFinish: {
452
+ stabilityThreshold: 500,
453
+ pollInterval: 100,
454
+ },
455
+ });
456
+
457
+ const onEvent = (absolutePath: string) => this.handleEvent(absolutePath);
458
+ this.watcher
459
+ .on("add", onEvent)
460
+ .on("change", onEvent)
461
+ .on("unlink", onEvent)
462
+ .on("addDir", onEvent)
463
+ .on("unlinkDir", onEvent)
464
+ .on("error", (err) => console.error("TreeWatcher error:", err));
465
+ }
466
+
467
+ /**
468
+ * Test/seam entry point: feed a raw filesystem path as if chokidar reported
469
+ * it. Applies the emit filter then arms the debounce. Real chokidar events
470
+ * route through here too.
471
+ */
472
+ handleEvent(absolutePath: string): void {
473
+ if (this.disposed) return;
474
+ // Defensive: chokidar's `ignored` already filtered, but a path that slips
475
+ // through (or a synthetic test event) is re-checked here.
476
+ if (!this.shouldEmit(absolutePath, false)) return;
477
+ this.arm();
478
+ }
479
+
480
+ private arm(): void {
481
+ if (this.timer !== null) {
482
+ this.clock.clearTimeout(this.timer);
483
+ this.timer = null;
484
+ }
485
+ this.timer = this.clock.setTimeout(() => {
486
+ this.timer = null;
487
+ this.emit();
488
+ }, this.debounceMs);
489
+ }
490
+
491
+ private emit(): void {
492
+ if (this.disposed) return;
493
+ for (const l of this.listeners) {
494
+ try {
495
+ l();
496
+ } catch (err) {
497
+ console.error("TreeWatcher listener error:", err);
498
+ }
499
+ }
500
+ }
501
+
502
+ /** True while the chokidar watcher is active. */
503
+ isWatching(): boolean {
504
+ return this.watcher !== null;
505
+ }
506
+
507
+ /** Number of pending debounce timers — a leak check for tests. */
508
+ pendingTimerCount(): number {
509
+ return this.timer === null ? 0 : 1;
510
+ }
511
+
512
+ /**
513
+ * Stop watching: close the chokidar watcher (releasing fds) and cancel any
514
+ * pending debounce timer. Idempotent. The instance can be restarted with
515
+ * {@link start} unless {@link dispose} was called.
516
+ */
517
+ stop(): void {
518
+ if (this.timer !== null) {
519
+ this.clock.clearTimeout(this.timer);
520
+ this.timer = null;
521
+ }
522
+ if (this.watcher) {
523
+ void this.watcher.close();
524
+ this.watcher = null;
525
+ }
526
+ }
527
+
528
+ /** Permanent shutdown: stop() + drop listeners; further events are no-ops. */
529
+ dispose(): void {
530
+ this.stop();
531
+ this.disposed = true;
532
+ this.listeners.clear();
533
+ }
534
+ }