@indigoai-us/hq-cloud 5.21.0 → 5.23.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/index.d.ts +10 -4
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +10 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/journal.d.ts +76 -1
  6. package/dist/journal.d.ts.map +1 -1
  7. package/dist/journal.js +148 -1
  8. package/dist/journal.js.map +1 -1
  9. package/dist/journal.test.js +251 -5
  10. package/dist/journal.test.js.map +1 -1
  11. package/dist/prefix-coalesce.d.ts +38 -0
  12. package/dist/prefix-coalesce.d.ts.map +1 -0
  13. package/dist/prefix-coalesce.js +69 -0
  14. package/dist/prefix-coalesce.js.map +1 -0
  15. package/dist/prefix-coalesce.test.d.ts +2 -0
  16. package/dist/prefix-coalesce.test.d.ts.map +1 -0
  17. package/dist/prefix-coalesce.test.js +77 -0
  18. package/dist/prefix-coalesce.test.js.map +1 -0
  19. package/dist/public-surface.test.d.ts +15 -0
  20. package/dist/public-surface.test.d.ts.map +1 -0
  21. package/dist/public-surface.test.js +105 -0
  22. package/dist/public-surface.test.js.map +1 -0
  23. package/dist/remote-pull.d.ts +145 -1
  24. package/dist/remote-pull.d.ts.map +1 -1
  25. package/dist/remote-pull.js +258 -1
  26. package/dist/remote-pull.js.map +1 -1
  27. package/dist/remote-pull.test.js +470 -2
  28. package/dist/remote-pull.test.js.map +1 -1
  29. package/dist/schemas/source-channels.d.ts +14 -0
  30. package/dist/schemas/source-channels.d.ts.map +1 -1
  31. package/dist/schemas/source-channels.js +16 -0
  32. package/dist/schemas/source-channels.js.map +1 -1
  33. package/dist/scope-shrink.d.ts +109 -0
  34. package/dist/scope-shrink.d.ts.map +1 -0
  35. package/dist/scope-shrink.js +196 -0
  36. package/dist/scope-shrink.js.map +1 -0
  37. package/dist/scope-shrink.test.d.ts +13 -0
  38. package/dist/scope-shrink.test.d.ts.map +1 -0
  39. package/dist/scope-shrink.test.js +342 -0
  40. package/dist/scope-shrink.test.js.map +1 -0
  41. package/dist/sources/get.d.ts.map +1 -1
  42. package/dist/sources/get.js +6 -3
  43. package/dist/sources/get.js.map +1 -1
  44. package/dist/sources/get.test.js +7 -7
  45. package/dist/sources/get.test.js.map +1 -1
  46. package/dist/sources/list.d.ts.map +1 -1
  47. package/dist/sources/list.js +4 -2
  48. package/dist/sources/list.js.map +1 -1
  49. package/dist/sources/list.test.js +6 -6
  50. package/dist/sources/list.test.js.map +1 -1
  51. package/dist/types.d.ts +48 -1
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/vault-client.d.ts +178 -0
  54. package/dist/vault-client.d.ts.map +1 -1
  55. package/dist/vault-client.js +73 -0
  56. package/dist/vault-client.js.map +1 -1
  57. package/dist/vault-client.test.js +226 -0
  58. package/dist/vault-client.test.js.map +1 -1
  59. package/package.json +1 -1
  60. package/src/index.ts +68 -0
  61. package/src/journal.test.ts +284 -5
  62. package/src/journal.ts +167 -2
  63. package/src/prefix-coalesce.test.ts +95 -0
  64. package/src/prefix-coalesce.ts +72 -0
  65. package/src/public-surface.test.ts +112 -0
  66. package/src/remote-pull.test.ts +540 -3
  67. package/src/remote-pull.ts +419 -2
  68. package/src/schemas/source-channels.ts +17 -0
  69. package/src/scope-shrink.test.ts +402 -0
  70. package/src/scope-shrink.ts +264 -0
  71. package/src/sources/get.test.ts +7 -7
  72. package/src/sources/get.ts +6 -3
  73. package/src/sources/list.test.ts +6 -6
  74. package/src/sources/list.ts +4 -2
  75. package/src/types.ts +49 -1
  76. package/src/vault-client.test.ts +335 -0
  77. package/src/vault-client.ts +223 -0
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Scope-shrink detection + classification + clean removal (US-005).
3
+ *
4
+ * Implements the "hybrid scope-change contract" decided in US-000 Task 3
5
+ * (see companies/indigo/projects/hq-sync-browse-vs-sync/references.md):
6
+ *
7
+ * - Compare the current pull's `prefixSet` against the last `PullRecord`'s
8
+ * `prefixSet` for the same company. Files in the journal covered by the
9
+ * previous scope but NOT covered by the new scope are **orphans**.
10
+ * - Classify each orphan **clean** (safe to silently delete) or **dirty**
11
+ * (locally modified — sacred, never silently delete).
12
+ * - The remote-pull caller drives:
13
+ * * default mode: abort the leg if any dirty orphan exists;
14
+ * * `--force-scope-shrink`: continue, leave dirty files on disk,
15
+ * tombstone their journal entries.
16
+ *
17
+ * The pure-detection layer here intentionally does NOT touch disk for the
18
+ * tombstone write — that lives in `journal.ts`. It DOES touch disk for the
19
+ * orphan classification (hash + stat) because cleanliness is a function of
20
+ * the file's current on-disk state vs the journal.
21
+ */
22
+ import type { JournalEntry, PullRecord, SyncJournal } from "./types.js";
23
+ export interface OrphanClassification {
24
+ /** Relative path (journal key). */
25
+ path: string;
26
+ /** Journal entry as of last sync. */
27
+ entry: JournalEntry;
28
+ /** True iff the local file is provably unchanged since last sync. */
29
+ clean: boolean;
30
+ /** Why we called it dirty — surfaced in the abort error for operators. */
31
+ dirtyReason?: "modified-after-sync" | "hash-mismatch" | "stat-error";
32
+ }
33
+ export interface ScopeShrinkPlan {
34
+ /** Set of files covered by `lastPrefixSet` but not by `currentPrefixSet`. */
35
+ orphans: OrphanClassification[];
36
+ /** Subset of `orphans` with `clean === true`. */
37
+ clean: OrphanClassification[];
38
+ /** Subset of `orphans` with `clean === false`. */
39
+ dirty: OrphanClassification[];
40
+ /** True iff at least one orphan was found. */
41
+ scopeChangeDetected: boolean;
42
+ }
43
+ export interface BuildScopeShrinkPlanInput {
44
+ journal: SyncJournal;
45
+ hqRoot: string;
46
+ /** Coalesced prefixes used by the LAST pull for this company. */
47
+ lastPrefixSet: string[];
48
+ /** Coalesced prefixes the CURRENT pull will use. */
49
+ currentPrefixSet: string[];
50
+ }
51
+ /**
52
+ * Build a scope-shrink plan: find orphans, classify each clean/dirty.
53
+ * Pure given the journal + filesystem state — no network, no journal
54
+ * mutation.
55
+ *
56
+ * **Tombstone-aware:** journal entries that already carry a `removedAt`
57
+ * marker are skipped — they represent a prior scope-shrink prune and must
58
+ * not be re-flagged as orphans on each subsequent pull (that's the whole
59
+ * point of the tombstone retention window).
60
+ *
61
+ * **Direction-aware:** only `direction: "down"` entries (and pre-ETag
62
+ * legacy entries without an explicit direction marker) participate in
63
+ * shrink detection. Push-only files (`direction: "up"`) represent local
64
+ * authorship — they aren't in scope-as-pulled, so a scope change doesn't
65
+ * orphan them.
66
+ */
67
+ export declare function buildScopeShrinkPlan(input: BuildScopeShrinkPlanInput): ScopeShrinkPlan;
68
+ /**
69
+ * Structured error thrown when the engine refuses to proceed because a scope
70
+ * shrink would orphan dirty files. The CLI catches this and renders the
71
+ * operator-facing message; the engine never prints directly.
72
+ */
73
+ export declare class ScopeShrinkBlockedError extends Error {
74
+ readonly companyUid: string;
75
+ readonly fromMode: PullRecord["syncMode"] | "unknown";
76
+ readonly toMode: PullRecord["syncMode"];
77
+ readonly dirty: OrphanClassification[];
78
+ readonly clean: OrphanClassification[];
79
+ readonly code = "SCOPE_SHRINK_BLOCKED";
80
+ constructor(companyUid: string, fromMode: PullRecord["syncMode"] | "unknown", toMode: PullRecord["syncMode"], dirty: OrphanClassification[], clean: OrphanClassification[]);
81
+ }
82
+ export interface ApplyScopeShrinkInput {
83
+ journal: SyncJournal;
84
+ plan: ScopeShrinkPlan;
85
+ hqRoot: string;
86
+ /**
87
+ * When `true`, dirty files are LEFT ON DISK and their journal entries are
88
+ * tombstoned anyway. When `false` (default), the caller should have
89
+ * already aborted on dirty orphans — this function still tombstones any
90
+ * dirty entries handed to it, on the assumption the caller knows what
91
+ * it's doing.
92
+ */
93
+ forceScopeShrink: boolean;
94
+ reason?: "scope_shrink" | "narrow_apply" | "manual";
95
+ }
96
+ export interface ApplyScopeShrinkResult {
97
+ cleanRemoved: number;
98
+ dirtyTombstoned: number;
99
+ }
100
+ /**
101
+ * Apply a scope-shrink plan: delete clean orphans on disk + tombstone their
102
+ * journal entries. With `forceScopeShrink: true`, dirty orphans are
103
+ * preserved on disk but their journal entries are also tombstoned.
104
+ *
105
+ * Returns counts for the audit log row (`scope_shrink_blocked` /
106
+ * `scope_shrink_forced`).
107
+ */
108
+ export declare function applyScopeShrink(input: ApplyScopeShrinkInput): ApplyScopeShrinkResult;
109
+ //# sourceMappingURL=scope-shrink.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope-shrink.d.ts","sourceRoot":"","sources":["../src/scope-shrink.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAIH,OAAO,KAAK,EACV,YAAY,EACZ,UAAU,EACV,WAAW,EACZ,MAAM,YAAY,CAAC;AAIpB,MAAM,WAAW,oBAAoB;IACnC,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,KAAK,EAAE,YAAY,CAAC;IACpB,qEAAqE;IACrE,KAAK,EAAE,OAAO,CAAC;IACf,0EAA0E;IAC1E,WAAW,CAAC,EACR,qBAAqB,GACrB,eAAe,GACf,YAAY,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,6EAA6E;IAC7E,OAAO,EAAE,oBAAoB,EAAE,CAAC;IAChC,iDAAiD;IACjD,KAAK,EAAE,oBAAoB,EAAE,CAAC;IAC9B,kDAAkD;IAClD,KAAK,EAAE,oBAAoB,EAAE,CAAC;IAC9B,8CAA8C;IAC9C,mBAAmB,EAAE,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,yBAAyB;IACxC,OAAO,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,iEAAiE;IACjE,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,oDAAoD;IACpD,gBAAgB,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,yBAAyB,GAC/B,eAAe,CAoBjB;AA8ED;;;;GAIG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;aAG9B,UAAU,EAAE,MAAM;aAClB,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,GAAG,SAAS;aAC5C,MAAM,EAAE,UAAU,CAAC,UAAU,CAAC;aAC9B,KAAK,EAAE,oBAAoB,EAAE;aAC7B,KAAK,EAAE,oBAAoB,EAAE;IAN/C,QAAQ,CAAC,IAAI,0BAA0B;gBAErB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,GAAG,SAAS,EAC5C,MAAM,EAAE,UAAU,CAAC,UAAU,CAAC,EAC9B,KAAK,EAAE,oBAAoB,EAAE,EAC7B,KAAK,EAAE,oBAAoB,EAAE;CAShD;AAED,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,WAAW,CAAC;IACrB,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf;;;;;;OAMG;IACH,gBAAgB,EAAE,OAAO,CAAC;IAC1B,MAAM,CAAC,EAAE,cAAc,GAAG,cAAc,GAAG,QAAQ,CAAC;CACrD;AAED,MAAM,WAAW,sBAAsB;IACrC,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,qBAAqB,GAC3B,sBAAsB,CA4BxB"}
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Scope-shrink detection + classification + clean removal (US-005).
3
+ *
4
+ * Implements the "hybrid scope-change contract" decided in US-000 Task 3
5
+ * (see companies/indigo/projects/hq-sync-browse-vs-sync/references.md):
6
+ *
7
+ * - Compare the current pull's `prefixSet` against the last `PullRecord`'s
8
+ * `prefixSet` for the same company. Files in the journal covered by the
9
+ * previous scope but NOT covered by the new scope are **orphans**.
10
+ * - Classify each orphan **clean** (safe to silently delete) or **dirty**
11
+ * (locally modified — sacred, never silently delete).
12
+ * - The remote-pull caller drives:
13
+ * * default mode: abort the leg if any dirty orphan exists;
14
+ * * `--force-scope-shrink`: continue, leave dirty files on disk,
15
+ * tombstone their journal entries.
16
+ *
17
+ * The pure-detection layer here intentionally does NOT touch disk for the
18
+ * tombstone write — that lives in `journal.ts`. It DOES touch disk for the
19
+ * orphan classification (hash + stat) because cleanliness is a function of
20
+ * the file's current on-disk state vs the journal.
21
+ */
22
+ import * as fs from "fs";
23
+ import * as path from "path";
24
+ import { hashFile, tombstoneEntry } from "./journal.js";
25
+ import { isCoveredByAny } from "./prefix-coalesce.js";
26
+ /**
27
+ * Build a scope-shrink plan: find orphans, classify each clean/dirty.
28
+ * Pure given the journal + filesystem state — no network, no journal
29
+ * mutation.
30
+ *
31
+ * **Tombstone-aware:** journal entries that already carry a `removedAt`
32
+ * marker are skipped — they represent a prior scope-shrink prune and must
33
+ * not be re-flagged as orphans on each subsequent pull (that's the whole
34
+ * point of the tombstone retention window).
35
+ *
36
+ * **Direction-aware:** only `direction: "down"` entries (and pre-ETag
37
+ * legacy entries without an explicit direction marker) participate in
38
+ * shrink detection. Push-only files (`direction: "up"`) represent local
39
+ * authorship — they aren't in scope-as-pulled, so a scope change doesn't
40
+ * orphan them.
41
+ */
42
+ export function buildScopeShrinkPlan(input) {
43
+ const { journal, hqRoot, lastPrefixSet, currentPrefixSet } = input;
44
+ const orphans = [];
45
+ for (const [relPath, entry] of Object.entries(journal.files)) {
46
+ if (entry.removedAt)
47
+ continue; // tombstone — already pruned
48
+ if (entry.direction !== "down")
49
+ continue;
50
+ if (!isCoveredByAny(relPath, lastPrefixSet))
51
+ continue;
52
+ if (isCoveredByAny(relPath, currentPrefixSet))
53
+ continue;
54
+ orphans.push(classifyOrphan(relPath, entry, hqRoot));
55
+ }
56
+ const clean = orphans.filter((o) => o.clean);
57
+ const dirty = orphans.filter((o) => !o.clean);
58
+ return {
59
+ orphans,
60
+ clean,
61
+ dirty,
62
+ scopeChangeDetected: orphans.length > 0,
63
+ };
64
+ }
65
+ /**
66
+ * Classify a single orphan:
67
+ *
68
+ * - **Clean** when:
69
+ * * the local file is missing (already removed by the user — harmless), OR
70
+ * * `sha256(localFile) === entry.hash` AND `stat.mtime ≤ entry.syncedAt`.
71
+ * - **Dirty** otherwise.
72
+ *
73
+ * Symlinks: we don't re-hash with the symlink-target convention here —
74
+ * the safer default is to treat any symlink whose lstat exists but whose
75
+ * target hash doesn't match `entry.hash` (via `hashFile` reading the
76
+ * target) as dirty. In practice symlinks materialize through the same
77
+ * code path as files; a stale-target symlink is correctly flagged dirty
78
+ * and the operator-facing message points at the path either way.
79
+ */
80
+ function classifyOrphan(relPath, entry, hqRoot) {
81
+ const absPath = path.join(hqRoot, relPath);
82
+ let stat;
83
+ try {
84
+ stat = fs.lstatSync(absPath);
85
+ }
86
+ catch (err) {
87
+ const code = err.code;
88
+ if (code === "ENOENT") {
89
+ return { path: relPath, entry, clean: true };
90
+ }
91
+ return {
92
+ path: relPath,
93
+ entry,
94
+ clean: false,
95
+ dirtyReason: "stat-error",
96
+ };
97
+ }
98
+ // mtime guard: a local edit moves mtime past syncedAt. We use ≤ because
99
+ // a download stamps syncedAt at the close of the write; mtime is set by
100
+ // the OS before that, so an unmodified pulled file has mtime ≤ syncedAt.
101
+ const mtimeMs = stat.mtimeMs;
102
+ const syncedAtMs = Date.parse(entry.syncedAt);
103
+ if (!Number.isNaN(syncedAtMs) && mtimeMs > syncedAtMs + 1000) {
104
+ // 1s grace for filesystem clock jitter.
105
+ return {
106
+ path: relPath,
107
+ entry,
108
+ clean: false,
109
+ dirtyReason: "modified-after-sync",
110
+ };
111
+ }
112
+ // Hash check — final word. If the content matches the journaled hash,
113
+ // the file is provably what the last pull left there.
114
+ let actualHash;
115
+ try {
116
+ actualHash = hashFile(absPath);
117
+ }
118
+ catch {
119
+ return {
120
+ path: relPath,
121
+ entry,
122
+ clean: false,
123
+ dirtyReason: "stat-error",
124
+ };
125
+ }
126
+ if (actualHash !== entry.hash) {
127
+ return {
128
+ path: relPath,
129
+ entry,
130
+ clean: false,
131
+ dirtyReason: "hash-mismatch",
132
+ };
133
+ }
134
+ return { path: relPath, entry, clean: true };
135
+ }
136
+ /**
137
+ * Structured error thrown when the engine refuses to proceed because a scope
138
+ * shrink would orphan dirty files. The CLI catches this and renders the
139
+ * operator-facing message; the engine never prints directly.
140
+ */
141
+ export class ScopeShrinkBlockedError extends Error {
142
+ companyUid;
143
+ fromMode;
144
+ toMode;
145
+ dirty;
146
+ clean;
147
+ code = "SCOPE_SHRINK_BLOCKED";
148
+ constructor(companyUid, fromMode, toMode, dirty, clean) {
149
+ super(`Sync scope shrank for ${companyUid} (${fromMode} → ${toMode}); ` +
150
+ `${dirty.length} dirty file(s) outside the new scope would be ` +
151
+ `pruned from the journal. Resolve or pass --force-scope-shrink.`);
152
+ this.companyUid = companyUid;
153
+ this.fromMode = fromMode;
154
+ this.toMode = toMode;
155
+ this.dirty = dirty;
156
+ this.clean = clean;
157
+ this.name = "ScopeShrinkBlockedError";
158
+ }
159
+ }
160
+ /**
161
+ * Apply a scope-shrink plan: delete clean orphans on disk + tombstone their
162
+ * journal entries. With `forceScopeShrink: true`, dirty orphans are
163
+ * preserved on disk but their journal entries are also tombstoned.
164
+ *
165
+ * Returns counts for the audit log row (`scope_shrink_blocked` /
166
+ * `scope_shrink_forced`).
167
+ */
168
+ export function applyScopeShrink(input) {
169
+ const { journal, plan, hqRoot, forceScopeShrink } = input;
170
+ const reason = input.reason ?? "scope_shrink";
171
+ let cleanRemoved = 0;
172
+ let dirtyTombstoned = 0;
173
+ for (const orphan of plan.clean) {
174
+ const absPath = path.join(hqRoot, orphan.path);
175
+ try {
176
+ fs.unlinkSync(absPath);
177
+ }
178
+ catch (err) {
179
+ const code = err.code;
180
+ if (code !== "ENOENT")
181
+ throw err; // missing-on-disk is fine; anything else escalates
182
+ }
183
+ tombstoneEntry(journal, orphan.path, reason);
184
+ cleanRemoved++;
185
+ }
186
+ if (forceScopeShrink) {
187
+ for (const orphan of plan.dirty) {
188
+ // Do NOT delete the file — that's the entire point of the `--force`
189
+ // contract: keep dirty content on disk, prune only the journal entry.
190
+ tombstoneEntry(journal, orphan.path, reason);
191
+ dirtyTombstoned++;
192
+ }
193
+ }
194
+ return { cleanRemoved, dirtyTombstoned };
195
+ }
196
+ //# sourceMappingURL=scope-shrink.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope-shrink.js","sourceRoot":"","sources":["../src/scope-shrink.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAM7B,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAoCtD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAgC;IAEhC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,gBAAgB,EAAE,GAAG,KAAK,CAAC;IACnE,MAAM,OAAO,GAA2B,EAAE,CAAC;IAE3C,KAAK,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7D,IAAI,KAAK,CAAC,SAAS;YAAE,SAAS,CAAC,6BAA6B;QAC5D,IAAI,KAAK,CAAC,SAAS,KAAK,MAAM;YAAE,SAAS;QACzC,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,aAAa,CAAC;YAAE,SAAS;QACtD,IAAI,cAAc,CAAC,OAAO,EAAE,gBAAgB,CAAC;YAAE,SAAS;QACxD,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAC7C,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CAAC;IAC1D,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,IAAI,cAAc,CAAC;IAC9C,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,IAAI,eAAe,GAAG,CAAC,CAAC;IAExB,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QAChC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;QAC/C,IAAI,CAAC;YACH,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,GAAI,GAA6B,CAAC,IAAI,CAAC;YACjD,IAAI,IAAI,KAAK,QAAQ;gBAAE,MAAM,GAAG,CAAC,CAAC,mDAAmD;QACvF,CAAC;QACD,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC7C,YAAY,EAAE,CAAC;IACjB,CAAC;IAED,IAAI,gBAAgB,EAAE,CAAC;QACrB,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAChC,oEAAoE;YACpE,sEAAsE;YACtE,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YAC7C,eAAe,EAAE,CAAC;QACpB,CAAC;IACH,CAAC;IAED,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Scope-shrink detection + classification + clean removal (US-005).
3
+ *
4
+ * Pins the hybrid-contract semantics decided in US-000 Task 3:
5
+ * - orphans = (covered by last scope) ∧ ¬(covered by current scope)
6
+ * - tombstones are NOT re-flagged on subsequent pulls
7
+ * - clean orphan: local missing OR hash matches + mtime ≤ syncedAt
8
+ * - dirty orphan: anything else
9
+ * - applyScopeShrink deletes clean orphans, leaves dirty files alone
10
+ * when `forceScopeShrink: true` but tombstones the journal entry
11
+ */
12
+ export {};
13
+ //# sourceMappingURL=scope-shrink.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope-shrink.test.d.ts","sourceRoot":"","sources":["../src/scope-shrink.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG"}
@@ -0,0 +1,342 @@
1
+ /**
2
+ * Scope-shrink detection + classification + clean removal (US-005).
3
+ *
4
+ * Pins the hybrid-contract semantics decided in US-000 Task 3:
5
+ * - orphans = (covered by last scope) ∧ ¬(covered by current scope)
6
+ * - tombstones are NOT re-flagged on subsequent pulls
7
+ * - clean orphan: local missing OR hash matches + mtime ≤ syncedAt
8
+ * - dirty orphan: anything else
9
+ * - applyScopeShrink deletes clean orphans, leaves dirty files alone
10
+ * when `forceScopeShrink: true` but tombstones the journal entry
11
+ */
12
+ import { describe, it, expect, beforeEach, afterEach } from "vitest";
13
+ import * as fs from "fs";
14
+ import * as os from "os";
15
+ import * as path from "path";
16
+ import * as crypto from "crypto";
17
+ import { buildScopeShrinkPlan, applyScopeShrink, ScopeShrinkBlockedError, } from "./scope-shrink.js";
18
+ function sha256(content) {
19
+ return crypto.createHash("sha256").update(content).digest("hex");
20
+ }
21
+ function emptyJournal() {
22
+ return { version: "2", lastSync: "", files: {}, pulls: [] };
23
+ }
24
+ describe("buildScopeShrinkPlan", () => {
25
+ let hqRoot;
26
+ beforeEach(() => {
27
+ hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-scope-shrink-"));
28
+ });
29
+ afterEach(() => {
30
+ fs.rmSync(hqRoot, { recursive: true, force: true });
31
+ });
32
+ it("returns empty plan when no orphans exist", () => {
33
+ const journal = {
34
+ ...emptyJournal(),
35
+ files: {
36
+ "companies/indigo/meetings/a.md": {
37
+ hash: "h",
38
+ size: 1,
39
+ syncedAt: "2026-05-01T00:00:00.000Z",
40
+ direction: "down",
41
+ },
42
+ },
43
+ };
44
+ const plan = buildScopeShrinkPlan({
45
+ journal,
46
+ hqRoot,
47
+ lastPrefixSet: ["companies/indigo/"],
48
+ currentPrefixSet: ["companies/indigo/"],
49
+ });
50
+ expect(plan.orphans).toEqual([]);
51
+ expect(plan.scopeChangeDetected).toBe(false);
52
+ });
53
+ it("flags files covered by lastPrefixSet but not currentPrefixSet as orphans", () => {
54
+ const meetingsAbs = path.join(hqRoot, "companies/indigo/meetings/a.md");
55
+ const scratchAbs = path.join(hqRoot, "companies/indigo/scratch/jacob/draft.md");
56
+ fs.mkdirSync(path.dirname(meetingsAbs), { recursive: true });
57
+ fs.mkdirSync(path.dirname(scratchAbs), { recursive: true });
58
+ fs.writeFileSync(meetingsAbs, "meetings");
59
+ fs.writeFileSync(scratchAbs, "draft");
60
+ const now = new Date().toISOString();
61
+ const journal = {
62
+ ...emptyJournal(),
63
+ files: {
64
+ "companies/indigo/meetings/a.md": {
65
+ hash: sha256("meetings"),
66
+ size: 8,
67
+ syncedAt: now,
68
+ direction: "down",
69
+ },
70
+ "companies/indigo/scratch/jacob/draft.md": {
71
+ hash: sha256("draft"),
72
+ size: 5,
73
+ syncedAt: now,
74
+ direction: "down",
75
+ },
76
+ },
77
+ };
78
+ // Backdate the scratch file so mtime ≤ syncedAt.
79
+ const past = Date.now() - 60_000;
80
+ fs.utimesSync(scratchAbs, past / 1000, past / 1000);
81
+ fs.utimesSync(meetingsAbs, past / 1000, past / 1000);
82
+ const plan = buildScopeShrinkPlan({
83
+ journal,
84
+ hqRoot,
85
+ lastPrefixSet: ["companies/indigo/"],
86
+ currentPrefixSet: ["companies/indigo/meetings/"],
87
+ });
88
+ expect(plan.orphans.map((o) => o.path)).toEqual([
89
+ "companies/indigo/scratch/jacob/draft.md",
90
+ ]);
91
+ expect(plan.clean).toHaveLength(1);
92
+ expect(plan.dirty).toHaveLength(0);
93
+ expect(plan.scopeChangeDetected).toBe(true);
94
+ });
95
+ it("classifies a locally modified orphan as dirty (hash mismatch)", () => {
96
+ const abs = path.join(hqRoot, "companies/indigo/scratch/notes.md");
97
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
98
+ fs.writeFileSync(abs, "ORIGINAL");
99
+ const journal = {
100
+ ...emptyJournal(),
101
+ files: {
102
+ "companies/indigo/scratch/notes.md": {
103
+ hash: sha256("DIFFERENT"), // doesn't match what's on disk
104
+ size: 8,
105
+ syncedAt: new Date().toISOString(),
106
+ direction: "down",
107
+ },
108
+ },
109
+ };
110
+ const plan = buildScopeShrinkPlan({
111
+ journal,
112
+ hqRoot,
113
+ lastPrefixSet: ["companies/indigo/"],
114
+ currentPrefixSet: ["companies/indigo/meetings/"],
115
+ });
116
+ expect(plan.dirty).toHaveLength(1);
117
+ expect(plan.dirty[0]?.dirtyReason).toBe("hash-mismatch");
118
+ });
119
+ it("classifies a recently mtime-touched file as dirty", () => {
120
+ const abs = path.join(hqRoot, "companies/indigo/scratch/notes.md");
121
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
122
+ fs.writeFileSync(abs, "ORIGINAL");
123
+ const syncedAt = new Date(Date.now() - 60_000).toISOString();
124
+ // Touch mtime forward, well past syncedAt + 1s grace.
125
+ const future = Date.now() + 10_000;
126
+ fs.utimesSync(abs, future / 1000, future / 1000);
127
+ const journal = {
128
+ ...emptyJournal(),
129
+ files: {
130
+ "companies/indigo/scratch/notes.md": {
131
+ hash: sha256("ORIGINAL"), // hash matches, but mtime is forward
132
+ size: 8,
133
+ syncedAt,
134
+ direction: "down",
135
+ },
136
+ },
137
+ };
138
+ const plan = buildScopeShrinkPlan({
139
+ journal,
140
+ hqRoot,
141
+ lastPrefixSet: ["companies/indigo/"],
142
+ currentPrefixSet: ["companies/indigo/meetings/"],
143
+ });
144
+ expect(plan.dirty).toHaveLength(1);
145
+ expect(plan.dirty[0]?.dirtyReason).toBe("modified-after-sync");
146
+ });
147
+ it("treats a locally-missing orphan as clean (already removed by user)", () => {
148
+ const journal = {
149
+ ...emptyJournal(),
150
+ files: {
151
+ "companies/indigo/scratch/gone.md": {
152
+ hash: sha256("gone"),
153
+ size: 4,
154
+ syncedAt: new Date().toISOString(),
155
+ direction: "down",
156
+ },
157
+ },
158
+ };
159
+ const plan = buildScopeShrinkPlan({
160
+ journal,
161
+ hqRoot,
162
+ lastPrefixSet: ["companies/indigo/"],
163
+ currentPrefixSet: ["companies/indigo/meetings/"],
164
+ });
165
+ expect(plan.clean).toHaveLength(1);
166
+ expect(plan.clean[0]?.path).toBe("companies/indigo/scratch/gone.md");
167
+ });
168
+ it("skips tombstoned entries (do not re-flag on subsequent pulls)", () => {
169
+ const journal = {
170
+ ...emptyJournal(),
171
+ files: {
172
+ "companies/indigo/scratch/already.md": {
173
+ hash: "h",
174
+ size: 1,
175
+ syncedAt: "2026-05-01T00:00:00.000Z",
176
+ direction: "down",
177
+ removedAt: "2026-05-02T00:00:00.000Z",
178
+ removedReason: "scope_shrink",
179
+ },
180
+ },
181
+ };
182
+ const plan = buildScopeShrinkPlan({
183
+ journal,
184
+ hqRoot,
185
+ lastPrefixSet: ["companies/indigo/"],
186
+ currentPrefixSet: ["companies/indigo/meetings/"],
187
+ });
188
+ expect(plan.orphans).toEqual([]);
189
+ });
190
+ it("skips push-only entries (direction: 'up')", () => {
191
+ const journal = {
192
+ ...emptyJournal(),
193
+ files: {
194
+ "companies/indigo/scratch/local-only.md": {
195
+ hash: "h",
196
+ size: 1,
197
+ syncedAt: "2026-05-01T00:00:00.000Z",
198
+ direction: "up",
199
+ },
200
+ },
201
+ };
202
+ const plan = buildScopeShrinkPlan({
203
+ journal,
204
+ hqRoot,
205
+ lastPrefixSet: ["companies/indigo/"],
206
+ currentPrefixSet: ["companies/indigo/meetings/"],
207
+ });
208
+ expect(plan.orphans).toEqual([]);
209
+ });
210
+ });
211
+ describe("applyScopeShrink", () => {
212
+ let hqRoot;
213
+ beforeEach(() => {
214
+ hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-scope-shrink-apply-"));
215
+ });
216
+ afterEach(() => {
217
+ fs.rmSync(hqRoot, { recursive: true, force: true });
218
+ });
219
+ it("deletes clean orphans on disk + tombstones their journal entries", () => {
220
+ const abs = path.join(hqRoot, "companies/indigo/scratch/clean.md");
221
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
222
+ fs.writeFileSync(abs, "clean");
223
+ const syncedAt = new Date().toISOString();
224
+ const past = Date.now() - 60_000;
225
+ fs.utimesSync(abs, past / 1000, past / 1000);
226
+ const journal = {
227
+ ...emptyJournal(),
228
+ files: {
229
+ "companies/indigo/scratch/clean.md": {
230
+ hash: sha256("clean"),
231
+ size: 5,
232
+ syncedAt,
233
+ direction: "down",
234
+ },
235
+ },
236
+ };
237
+ const plan = buildScopeShrinkPlan({
238
+ journal,
239
+ hqRoot,
240
+ lastPrefixSet: ["companies/indigo/"],
241
+ currentPrefixSet: ["companies/indigo/meetings/"],
242
+ });
243
+ const result = applyScopeShrink({
244
+ journal,
245
+ plan,
246
+ hqRoot,
247
+ forceScopeShrink: false,
248
+ });
249
+ expect(result.cleanRemoved).toBe(1);
250
+ expect(result.dirtyTombstoned).toBe(0);
251
+ expect(fs.existsSync(abs)).toBe(false);
252
+ expect(journal.files["companies/indigo/scratch/clean.md"]?.removedAt).toBeTruthy();
253
+ expect(journal.files["companies/indigo/scratch/clean.md"]?.removedReason).toBe("scope_shrink");
254
+ });
255
+ it("leaves dirty files on disk when forceScopeShrink: true, but tombstones the entry", () => {
256
+ const abs = path.join(hqRoot, "companies/indigo/scratch/dirty.md");
257
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
258
+ fs.writeFileSync(abs, "MODIFIED");
259
+ const journal = {
260
+ ...emptyJournal(),
261
+ files: {
262
+ "companies/indigo/scratch/dirty.md": {
263
+ hash: sha256("ORIGINAL"), // mismatch — dirty
264
+ size: 8,
265
+ syncedAt: new Date().toISOString(),
266
+ direction: "down",
267
+ },
268
+ },
269
+ };
270
+ const plan = buildScopeShrinkPlan({
271
+ journal,
272
+ hqRoot,
273
+ lastPrefixSet: ["companies/indigo/"],
274
+ currentPrefixSet: ["companies/indigo/meetings/"],
275
+ });
276
+ const result = applyScopeShrink({
277
+ journal,
278
+ plan,
279
+ hqRoot,
280
+ forceScopeShrink: true,
281
+ });
282
+ expect(result.dirtyTombstoned).toBe(1);
283
+ expect(fs.existsSync(abs)).toBe(true); // PRESERVED
284
+ expect(fs.readFileSync(abs, "utf-8")).toBe("MODIFIED");
285
+ expect(journal.files["companies/indigo/scratch/dirty.md"]?.removedAt).toBeTruthy();
286
+ });
287
+ it("accepts a custom reason for the tombstone (e.g. narrow_apply)", () => {
288
+ const abs = path.join(hqRoot, "companies/indigo/scratch/clean.md");
289
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
290
+ fs.writeFileSync(abs, "x");
291
+ const past = Date.now() - 60_000;
292
+ fs.utimesSync(abs, past / 1000, past / 1000);
293
+ const journal = {
294
+ ...emptyJournal(),
295
+ files: {
296
+ "companies/indigo/scratch/clean.md": {
297
+ hash: sha256("x"),
298
+ size: 1,
299
+ syncedAt: new Date().toISOString(),
300
+ direction: "down",
301
+ },
302
+ },
303
+ };
304
+ const plan = buildScopeShrinkPlan({
305
+ journal,
306
+ hqRoot,
307
+ lastPrefixSet: ["companies/indigo/"],
308
+ currentPrefixSet: ["companies/indigo/meetings/"],
309
+ });
310
+ applyScopeShrink({
311
+ journal,
312
+ plan,
313
+ hqRoot,
314
+ forceScopeShrink: false,
315
+ reason: "narrow_apply",
316
+ });
317
+ expect(journal.files["companies/indigo/scratch/clean.md"]?.removedReason).toBe("narrow_apply");
318
+ });
319
+ });
320
+ describe("ScopeShrinkBlockedError", () => {
321
+ it("carries from/to syncMode + dirty/clean orphan lists for the CLI", () => {
322
+ const err = new ScopeShrinkBlockedError("cmp_indigo", "all", "shared", [
323
+ {
324
+ path: "companies/indigo/scratch/notes.md",
325
+ entry: {
326
+ hash: "h",
327
+ size: 1,
328
+ syncedAt: "",
329
+ direction: "down",
330
+ },
331
+ clean: false,
332
+ dirtyReason: "hash-mismatch",
333
+ },
334
+ ], []);
335
+ expect(err.code).toBe("SCOPE_SHRINK_BLOCKED");
336
+ expect(err.fromMode).toBe("all");
337
+ expect(err.toMode).toBe("shared");
338
+ expect(err.dirty).toHaveLength(1);
339
+ expect(err.name).toBe("ScopeShrinkBlockedError");
340
+ });
341
+ });
342
+ //# sourceMappingURL=scope-shrink.test.js.map