@indigoai-us/hq-cloud 5.22.0 → 5.24.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.
- package/dist/bin/sync-runner.d.ts +20 -0
- package/dist/bin/sync-runner.d.ts.map +1 -1
- package/dist/bin/sync-runner.js +18 -0
- package/dist/bin/sync-runner.js.map +1 -1
- package/dist/bin/sync-runner.test.js +46 -2
- package/dist/bin/sync-runner.test.js.map +1 -1
- package/dist/cli/share.d.ts +77 -20
- package/dist/cli/share.d.ts.map +1 -1
- package/dist/cli/share.js +278 -61
- package/dist/cli/share.js.map +1 -1
- package/dist/cli/share.test.js +484 -3
- package/dist/cli/share.test.js.map +1 -1
- package/dist/cli/sync.d.ts +27 -0
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/journal.d.ts +76 -1
- package/dist/journal.d.ts.map +1 -1
- package/dist/journal.js +148 -1
- package/dist/journal.js.map +1 -1
- package/dist/journal.test.js +251 -5
- package/dist/journal.test.js.map +1 -1
- package/dist/prefix-coalesce.d.ts +38 -0
- package/dist/prefix-coalesce.d.ts.map +1 -0
- package/dist/prefix-coalesce.js +69 -0
- package/dist/prefix-coalesce.js.map +1 -0
- package/dist/prefix-coalesce.test.d.ts +2 -0
- package/dist/prefix-coalesce.test.d.ts.map +1 -0
- package/dist/prefix-coalesce.test.js +77 -0
- package/dist/prefix-coalesce.test.js.map +1 -0
- package/dist/public-surface.test.d.ts +15 -0
- package/dist/public-surface.test.d.ts.map +1 -0
- package/dist/public-surface.test.js +105 -0
- package/dist/public-surface.test.js.map +1 -0
- package/dist/remote-pull.d.ts +145 -1
- package/dist/remote-pull.d.ts.map +1 -1
- package/dist/remote-pull.js +258 -1
- package/dist/remote-pull.js.map +1 -1
- package/dist/remote-pull.test.js +470 -2
- package/dist/remote-pull.test.js.map +1 -1
- package/dist/scope-shrink.d.ts +109 -0
- package/dist/scope-shrink.d.ts.map +1 -0
- package/dist/scope-shrink.js +196 -0
- package/dist/scope-shrink.js.map +1 -0
- package/dist/scope-shrink.test.d.ts +13 -0
- package/dist/scope-shrink.test.d.ts.map +1 -0
- package/dist/scope-shrink.test.js +342 -0
- package/dist/scope-shrink.test.js.map +1 -0
- package/dist/types.d.ts +48 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/vault-client.d.ts +178 -0
- package/dist/vault-client.d.ts.map +1 -1
- package/dist/vault-client.js +73 -0
- package/dist/vault-client.js.map +1 -1
- package/dist/vault-client.test.js +226 -0
- package/dist/vault-client.test.js.map +1 -1
- package/package.json +1 -1
- package/src/bin/sync-runner.test.ts +56 -2
- package/src/bin/sync-runner.ts +39 -0
- package/src/cli/share.test.ts +577 -3
- package/src/cli/share.ts +395 -85
- package/src/cli/sync.ts +28 -0
- package/src/index.ts +67 -0
- package/src/journal.test.ts +284 -5
- package/src/journal.ts +167 -2
- package/src/prefix-coalesce.test.ts +95 -0
- package/src/prefix-coalesce.ts +72 -0
- package/src/public-surface.test.ts +112 -0
- package/src/remote-pull.test.ts +540 -3
- package/src/remote-pull.ts +419 -2
- package/src/scope-shrink.test.ts +402 -0
- package/src/scope-shrink.ts +264 -0
- package/src/types.ts +49 -1
- package/src/vault-client.test.ts +335 -0
- package/src/vault-client.ts +223 -0
|
@@ -0,0 +1,402 @@
|
|
|
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
|
+
|
|
13
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
14
|
+
import * as fs from "fs";
|
|
15
|
+
import * as os from "os";
|
|
16
|
+
import * as path from "path";
|
|
17
|
+
import * as crypto from "crypto";
|
|
18
|
+
import {
|
|
19
|
+
buildScopeShrinkPlan,
|
|
20
|
+
applyScopeShrink,
|
|
21
|
+
ScopeShrinkBlockedError,
|
|
22
|
+
} from "./scope-shrink.js";
|
|
23
|
+
import type { SyncJournal } from "./types.js";
|
|
24
|
+
|
|
25
|
+
function sha256(content: string): string {
|
|
26
|
+
return crypto.createHash("sha256").update(content).digest("hex");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function emptyJournal(): SyncJournal {
|
|
30
|
+
return { version: "2", lastSync: "", files: {}, pulls: [] };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe("buildScopeShrinkPlan", () => {
|
|
34
|
+
let hqRoot: string;
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-scope-shrink-"));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
fs.rmSync(hqRoot, { recursive: true, force: true });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("returns empty plan when no orphans exist", () => {
|
|
45
|
+
const journal: SyncJournal = {
|
|
46
|
+
...emptyJournal(),
|
|
47
|
+
files: {
|
|
48
|
+
"companies/indigo/meetings/a.md": {
|
|
49
|
+
hash: "h",
|
|
50
|
+
size: 1,
|
|
51
|
+
syncedAt: "2026-05-01T00:00:00.000Z",
|
|
52
|
+
direction: "down",
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const plan = buildScopeShrinkPlan({
|
|
57
|
+
journal,
|
|
58
|
+
hqRoot,
|
|
59
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
60
|
+
currentPrefixSet: ["companies/indigo/"],
|
|
61
|
+
});
|
|
62
|
+
expect(plan.orphans).toEqual([]);
|
|
63
|
+
expect(plan.scopeChangeDetected).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("flags files covered by lastPrefixSet but not currentPrefixSet as orphans", () => {
|
|
67
|
+
const meetingsAbs = path.join(
|
|
68
|
+
hqRoot,
|
|
69
|
+
"companies/indigo/meetings/a.md",
|
|
70
|
+
);
|
|
71
|
+
const scratchAbs = path.join(
|
|
72
|
+
hqRoot,
|
|
73
|
+
"companies/indigo/scratch/jacob/draft.md",
|
|
74
|
+
);
|
|
75
|
+
fs.mkdirSync(path.dirname(meetingsAbs), { recursive: true });
|
|
76
|
+
fs.mkdirSync(path.dirname(scratchAbs), { recursive: true });
|
|
77
|
+
fs.writeFileSync(meetingsAbs, "meetings");
|
|
78
|
+
fs.writeFileSync(scratchAbs, "draft");
|
|
79
|
+
|
|
80
|
+
const now = new Date().toISOString();
|
|
81
|
+
const journal: SyncJournal = {
|
|
82
|
+
...emptyJournal(),
|
|
83
|
+
files: {
|
|
84
|
+
"companies/indigo/meetings/a.md": {
|
|
85
|
+
hash: sha256("meetings"),
|
|
86
|
+
size: 8,
|
|
87
|
+
syncedAt: now,
|
|
88
|
+
direction: "down",
|
|
89
|
+
},
|
|
90
|
+
"companies/indigo/scratch/jacob/draft.md": {
|
|
91
|
+
hash: sha256("draft"),
|
|
92
|
+
size: 5,
|
|
93
|
+
syncedAt: now,
|
|
94
|
+
direction: "down",
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
// Backdate the scratch file so mtime ≤ syncedAt.
|
|
99
|
+
const past = Date.now() - 60_000;
|
|
100
|
+
fs.utimesSync(scratchAbs, past / 1000, past / 1000);
|
|
101
|
+
fs.utimesSync(meetingsAbs, past / 1000, past / 1000);
|
|
102
|
+
|
|
103
|
+
const plan = buildScopeShrinkPlan({
|
|
104
|
+
journal,
|
|
105
|
+
hqRoot,
|
|
106
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
107
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
108
|
+
});
|
|
109
|
+
expect(plan.orphans.map((o) => o.path)).toEqual([
|
|
110
|
+
"companies/indigo/scratch/jacob/draft.md",
|
|
111
|
+
]);
|
|
112
|
+
expect(plan.clean).toHaveLength(1);
|
|
113
|
+
expect(plan.dirty).toHaveLength(0);
|
|
114
|
+
expect(plan.scopeChangeDetected).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("classifies a locally modified orphan as dirty (hash mismatch)", () => {
|
|
118
|
+
const abs = path.join(hqRoot, "companies/indigo/scratch/notes.md");
|
|
119
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
120
|
+
fs.writeFileSync(abs, "ORIGINAL");
|
|
121
|
+
|
|
122
|
+
const journal: SyncJournal = {
|
|
123
|
+
...emptyJournal(),
|
|
124
|
+
files: {
|
|
125
|
+
"companies/indigo/scratch/notes.md": {
|
|
126
|
+
hash: sha256("DIFFERENT"), // doesn't match what's on disk
|
|
127
|
+
size: 8,
|
|
128
|
+
syncedAt: new Date().toISOString(),
|
|
129
|
+
direction: "down",
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const plan = buildScopeShrinkPlan({
|
|
135
|
+
journal,
|
|
136
|
+
hqRoot,
|
|
137
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
138
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
139
|
+
});
|
|
140
|
+
expect(plan.dirty).toHaveLength(1);
|
|
141
|
+
expect(plan.dirty[0]?.dirtyReason).toBe("hash-mismatch");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("classifies a recently mtime-touched file as dirty", () => {
|
|
145
|
+
const abs = path.join(hqRoot, "companies/indigo/scratch/notes.md");
|
|
146
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
147
|
+
fs.writeFileSync(abs, "ORIGINAL");
|
|
148
|
+
|
|
149
|
+
const syncedAt = new Date(Date.now() - 60_000).toISOString();
|
|
150
|
+
// Touch mtime forward, well past syncedAt + 1s grace.
|
|
151
|
+
const future = Date.now() + 10_000;
|
|
152
|
+
fs.utimesSync(abs, future / 1000, future / 1000);
|
|
153
|
+
|
|
154
|
+
const journal: SyncJournal = {
|
|
155
|
+
...emptyJournal(),
|
|
156
|
+
files: {
|
|
157
|
+
"companies/indigo/scratch/notes.md": {
|
|
158
|
+
hash: sha256("ORIGINAL"), // hash matches, but mtime is forward
|
|
159
|
+
size: 8,
|
|
160
|
+
syncedAt,
|
|
161
|
+
direction: "down",
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const plan = buildScopeShrinkPlan({
|
|
167
|
+
journal,
|
|
168
|
+
hqRoot,
|
|
169
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
170
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
171
|
+
});
|
|
172
|
+
expect(plan.dirty).toHaveLength(1);
|
|
173
|
+
expect(plan.dirty[0]?.dirtyReason).toBe("modified-after-sync");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("treats a locally-missing orphan as clean (already removed by user)", () => {
|
|
177
|
+
const journal: SyncJournal = {
|
|
178
|
+
...emptyJournal(),
|
|
179
|
+
files: {
|
|
180
|
+
"companies/indigo/scratch/gone.md": {
|
|
181
|
+
hash: sha256("gone"),
|
|
182
|
+
size: 4,
|
|
183
|
+
syncedAt: new Date().toISOString(),
|
|
184
|
+
direction: "down",
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const plan = buildScopeShrinkPlan({
|
|
190
|
+
journal,
|
|
191
|
+
hqRoot,
|
|
192
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
193
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
194
|
+
});
|
|
195
|
+
expect(plan.clean).toHaveLength(1);
|
|
196
|
+
expect(plan.clean[0]?.path).toBe("companies/indigo/scratch/gone.md");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("skips tombstoned entries (do not re-flag on subsequent pulls)", () => {
|
|
200
|
+
const journal: SyncJournal = {
|
|
201
|
+
...emptyJournal(),
|
|
202
|
+
files: {
|
|
203
|
+
"companies/indigo/scratch/already.md": {
|
|
204
|
+
hash: "h",
|
|
205
|
+
size: 1,
|
|
206
|
+
syncedAt: "2026-05-01T00:00:00.000Z",
|
|
207
|
+
direction: "down",
|
|
208
|
+
removedAt: "2026-05-02T00:00:00.000Z",
|
|
209
|
+
removedReason: "scope_shrink",
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
const plan = buildScopeShrinkPlan({
|
|
214
|
+
journal,
|
|
215
|
+
hqRoot,
|
|
216
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
217
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
218
|
+
});
|
|
219
|
+
expect(plan.orphans).toEqual([]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("skips push-only entries (direction: 'up')", () => {
|
|
223
|
+
const journal: SyncJournal = {
|
|
224
|
+
...emptyJournal(),
|
|
225
|
+
files: {
|
|
226
|
+
"companies/indigo/scratch/local-only.md": {
|
|
227
|
+
hash: "h",
|
|
228
|
+
size: 1,
|
|
229
|
+
syncedAt: "2026-05-01T00:00:00.000Z",
|
|
230
|
+
direction: "up",
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
const plan = buildScopeShrinkPlan({
|
|
235
|
+
journal,
|
|
236
|
+
hqRoot,
|
|
237
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
238
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
239
|
+
});
|
|
240
|
+
expect(plan.orphans).toEqual([]);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("applyScopeShrink", () => {
|
|
245
|
+
let hqRoot: string;
|
|
246
|
+
|
|
247
|
+
beforeEach(() => {
|
|
248
|
+
hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-scope-shrink-apply-"));
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
afterEach(() => {
|
|
252
|
+
fs.rmSync(hqRoot, { recursive: true, force: true });
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("deletes clean orphans on disk + tombstones their journal entries", () => {
|
|
256
|
+
const abs = path.join(hqRoot, "companies/indigo/scratch/clean.md");
|
|
257
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
258
|
+
fs.writeFileSync(abs, "clean");
|
|
259
|
+
|
|
260
|
+
const syncedAt = new Date().toISOString();
|
|
261
|
+
const past = Date.now() - 60_000;
|
|
262
|
+
fs.utimesSync(abs, past / 1000, past / 1000);
|
|
263
|
+
|
|
264
|
+
const journal: SyncJournal = {
|
|
265
|
+
...emptyJournal(),
|
|
266
|
+
files: {
|
|
267
|
+
"companies/indigo/scratch/clean.md": {
|
|
268
|
+
hash: sha256("clean"),
|
|
269
|
+
size: 5,
|
|
270
|
+
syncedAt,
|
|
271
|
+
direction: "down",
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const plan = buildScopeShrinkPlan({
|
|
277
|
+
journal,
|
|
278
|
+
hqRoot,
|
|
279
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
280
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
281
|
+
});
|
|
282
|
+
const result = applyScopeShrink({
|
|
283
|
+
journal,
|
|
284
|
+
plan,
|
|
285
|
+
hqRoot,
|
|
286
|
+
forceScopeShrink: false,
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
expect(result.cleanRemoved).toBe(1);
|
|
290
|
+
expect(result.dirtyTombstoned).toBe(0);
|
|
291
|
+
expect(fs.existsSync(abs)).toBe(false);
|
|
292
|
+
expect(
|
|
293
|
+
journal.files["companies/indigo/scratch/clean.md"]?.removedAt,
|
|
294
|
+
).toBeTruthy();
|
|
295
|
+
expect(
|
|
296
|
+
journal.files["companies/indigo/scratch/clean.md"]?.removedReason,
|
|
297
|
+
).toBe("scope_shrink");
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("leaves dirty files on disk when forceScopeShrink: true, but tombstones the entry", () => {
|
|
301
|
+
const abs = path.join(hqRoot, "companies/indigo/scratch/dirty.md");
|
|
302
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
303
|
+
fs.writeFileSync(abs, "MODIFIED");
|
|
304
|
+
|
|
305
|
+
const journal: SyncJournal = {
|
|
306
|
+
...emptyJournal(),
|
|
307
|
+
files: {
|
|
308
|
+
"companies/indigo/scratch/dirty.md": {
|
|
309
|
+
hash: sha256("ORIGINAL"), // mismatch — dirty
|
|
310
|
+
size: 8,
|
|
311
|
+
syncedAt: new Date().toISOString(),
|
|
312
|
+
direction: "down",
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const plan = buildScopeShrinkPlan({
|
|
318
|
+
journal,
|
|
319
|
+
hqRoot,
|
|
320
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
321
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
322
|
+
});
|
|
323
|
+
const result = applyScopeShrink({
|
|
324
|
+
journal,
|
|
325
|
+
plan,
|
|
326
|
+
hqRoot,
|
|
327
|
+
forceScopeShrink: true,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
expect(result.dirtyTombstoned).toBe(1);
|
|
331
|
+
expect(fs.existsSync(abs)).toBe(true); // PRESERVED
|
|
332
|
+
expect(fs.readFileSync(abs, "utf-8")).toBe("MODIFIED");
|
|
333
|
+
expect(
|
|
334
|
+
journal.files["companies/indigo/scratch/dirty.md"]?.removedAt,
|
|
335
|
+
).toBeTruthy();
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it("accepts a custom reason for the tombstone (e.g. narrow_apply)", () => {
|
|
339
|
+
const abs = path.join(hqRoot, "companies/indigo/scratch/clean.md");
|
|
340
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
341
|
+
fs.writeFileSync(abs, "x");
|
|
342
|
+
const past = Date.now() - 60_000;
|
|
343
|
+
fs.utimesSync(abs, past / 1000, past / 1000);
|
|
344
|
+
|
|
345
|
+
const journal: SyncJournal = {
|
|
346
|
+
...emptyJournal(),
|
|
347
|
+
files: {
|
|
348
|
+
"companies/indigo/scratch/clean.md": {
|
|
349
|
+
hash: sha256("x"),
|
|
350
|
+
size: 1,
|
|
351
|
+
syncedAt: new Date().toISOString(),
|
|
352
|
+
direction: "down",
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
const plan = buildScopeShrinkPlan({
|
|
357
|
+
journal,
|
|
358
|
+
hqRoot,
|
|
359
|
+
lastPrefixSet: ["companies/indigo/"],
|
|
360
|
+
currentPrefixSet: ["companies/indigo/meetings/"],
|
|
361
|
+
});
|
|
362
|
+
applyScopeShrink({
|
|
363
|
+
journal,
|
|
364
|
+
plan,
|
|
365
|
+
hqRoot,
|
|
366
|
+
forceScopeShrink: false,
|
|
367
|
+
reason: "narrow_apply",
|
|
368
|
+
});
|
|
369
|
+
expect(
|
|
370
|
+
journal.files["companies/indigo/scratch/clean.md"]?.removedReason,
|
|
371
|
+
).toBe("narrow_apply");
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
describe("ScopeShrinkBlockedError", () => {
|
|
376
|
+
it("carries from/to syncMode + dirty/clean orphan lists for the CLI", () => {
|
|
377
|
+
const err = new ScopeShrinkBlockedError(
|
|
378
|
+
"cmp_indigo",
|
|
379
|
+
"all",
|
|
380
|
+
"shared",
|
|
381
|
+
[
|
|
382
|
+
{
|
|
383
|
+
path: "companies/indigo/scratch/notes.md",
|
|
384
|
+
entry: {
|
|
385
|
+
hash: "h",
|
|
386
|
+
size: 1,
|
|
387
|
+
syncedAt: "",
|
|
388
|
+
direction: "down",
|
|
389
|
+
},
|
|
390
|
+
clean: false,
|
|
391
|
+
dirtyReason: "hash-mismatch",
|
|
392
|
+
},
|
|
393
|
+
],
|
|
394
|
+
[],
|
|
395
|
+
);
|
|
396
|
+
expect(err.code).toBe("SCOPE_SHRINK_BLOCKED");
|
|
397
|
+
expect(err.fromMode).toBe("all");
|
|
398
|
+
expect(err.toMode).toBe("shared");
|
|
399
|
+
expect(err.dirty).toHaveLength(1);
|
|
400
|
+
expect(err.name).toBe("ScopeShrinkBlockedError");
|
|
401
|
+
});
|
|
402
|
+
});
|
|
@@ -0,0 +1,264 @@
|
|
|
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
|
+
|
|
23
|
+
import * as fs from "fs";
|
|
24
|
+
import * as path from "path";
|
|
25
|
+
import type {
|
|
26
|
+
JournalEntry,
|
|
27
|
+
PullRecord,
|
|
28
|
+
SyncJournal,
|
|
29
|
+
} from "./types.js";
|
|
30
|
+
import { hashFile, tombstoneEntry } from "./journal.js";
|
|
31
|
+
import { isCoveredByAny } from "./prefix-coalesce.js";
|
|
32
|
+
|
|
33
|
+
export interface OrphanClassification {
|
|
34
|
+
/** Relative path (journal key). */
|
|
35
|
+
path: string;
|
|
36
|
+
/** Journal entry as of last sync. */
|
|
37
|
+
entry: JournalEntry;
|
|
38
|
+
/** True iff the local file is provably unchanged since last sync. */
|
|
39
|
+
clean: boolean;
|
|
40
|
+
/** Why we called it dirty — surfaced in the abort error for operators. */
|
|
41
|
+
dirtyReason?:
|
|
42
|
+
| "modified-after-sync"
|
|
43
|
+
| "hash-mismatch"
|
|
44
|
+
| "stat-error";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ScopeShrinkPlan {
|
|
48
|
+
/** Set of files covered by `lastPrefixSet` but not by `currentPrefixSet`. */
|
|
49
|
+
orphans: OrphanClassification[];
|
|
50
|
+
/** Subset of `orphans` with `clean === true`. */
|
|
51
|
+
clean: OrphanClassification[];
|
|
52
|
+
/** Subset of `orphans` with `clean === false`. */
|
|
53
|
+
dirty: OrphanClassification[];
|
|
54
|
+
/** True iff at least one orphan was found. */
|
|
55
|
+
scopeChangeDetected: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface BuildScopeShrinkPlanInput {
|
|
59
|
+
journal: SyncJournal;
|
|
60
|
+
hqRoot: string;
|
|
61
|
+
/** Coalesced prefixes used by the LAST pull for this company. */
|
|
62
|
+
lastPrefixSet: string[];
|
|
63
|
+
/** Coalesced prefixes the CURRENT pull will use. */
|
|
64
|
+
currentPrefixSet: string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Build a scope-shrink plan: find orphans, classify each clean/dirty.
|
|
69
|
+
* Pure given the journal + filesystem state — no network, no journal
|
|
70
|
+
* mutation.
|
|
71
|
+
*
|
|
72
|
+
* **Tombstone-aware:** journal entries that already carry a `removedAt`
|
|
73
|
+
* marker are skipped — they represent a prior scope-shrink prune and must
|
|
74
|
+
* not be re-flagged as orphans on each subsequent pull (that's the whole
|
|
75
|
+
* point of the tombstone retention window).
|
|
76
|
+
*
|
|
77
|
+
* **Direction-aware:** only `direction: "down"` entries (and pre-ETag
|
|
78
|
+
* legacy entries without an explicit direction marker) participate in
|
|
79
|
+
* shrink detection. Push-only files (`direction: "up"`) represent local
|
|
80
|
+
* authorship — they aren't in scope-as-pulled, so a scope change doesn't
|
|
81
|
+
* orphan them.
|
|
82
|
+
*/
|
|
83
|
+
export function buildScopeShrinkPlan(
|
|
84
|
+
input: BuildScopeShrinkPlanInput,
|
|
85
|
+
): ScopeShrinkPlan {
|
|
86
|
+
const { journal, hqRoot, lastPrefixSet, currentPrefixSet } = input;
|
|
87
|
+
const orphans: OrphanClassification[] = [];
|
|
88
|
+
|
|
89
|
+
for (const [relPath, entry] of Object.entries(journal.files)) {
|
|
90
|
+
if (entry.removedAt) continue; // tombstone — already pruned
|
|
91
|
+
if (entry.direction !== "down") continue;
|
|
92
|
+
if (!isCoveredByAny(relPath, lastPrefixSet)) continue;
|
|
93
|
+
if (isCoveredByAny(relPath, currentPrefixSet)) continue;
|
|
94
|
+
orphans.push(classifyOrphan(relPath, entry, hqRoot));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const clean = orphans.filter((o) => o.clean);
|
|
98
|
+
const dirty = orphans.filter((o) => !o.clean);
|
|
99
|
+
return {
|
|
100
|
+
orphans,
|
|
101
|
+
clean,
|
|
102
|
+
dirty,
|
|
103
|
+
scopeChangeDetected: orphans.length > 0,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Classify a single orphan:
|
|
109
|
+
*
|
|
110
|
+
* - **Clean** when:
|
|
111
|
+
* * the local file is missing (already removed by the user — harmless), OR
|
|
112
|
+
* * `sha256(localFile) === entry.hash` AND `stat.mtime ≤ entry.syncedAt`.
|
|
113
|
+
* - **Dirty** otherwise.
|
|
114
|
+
*
|
|
115
|
+
* Symlinks: we don't re-hash with the symlink-target convention here —
|
|
116
|
+
* the safer default is to treat any symlink whose lstat exists but whose
|
|
117
|
+
* target hash doesn't match `entry.hash` (via `hashFile` reading the
|
|
118
|
+
* target) as dirty. In practice symlinks materialize through the same
|
|
119
|
+
* code path as files; a stale-target symlink is correctly flagged dirty
|
|
120
|
+
* and the operator-facing message points at the path either way.
|
|
121
|
+
*/
|
|
122
|
+
function classifyOrphan(
|
|
123
|
+
relPath: string,
|
|
124
|
+
entry: JournalEntry,
|
|
125
|
+
hqRoot: string,
|
|
126
|
+
): OrphanClassification {
|
|
127
|
+
const absPath = path.join(hqRoot, relPath);
|
|
128
|
+
let stat: fs.Stats;
|
|
129
|
+
try {
|
|
130
|
+
stat = fs.lstatSync(absPath);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
133
|
+
if (code === "ENOENT") {
|
|
134
|
+
return { path: relPath, entry, clean: true };
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
path: relPath,
|
|
138
|
+
entry,
|
|
139
|
+
clean: false,
|
|
140
|
+
dirtyReason: "stat-error",
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// mtime guard: a local edit moves mtime past syncedAt. We use ≤ because
|
|
145
|
+
// a download stamps syncedAt at the close of the write; mtime is set by
|
|
146
|
+
// the OS before that, so an unmodified pulled file has mtime ≤ syncedAt.
|
|
147
|
+
const mtimeMs = stat.mtimeMs;
|
|
148
|
+
const syncedAtMs = Date.parse(entry.syncedAt);
|
|
149
|
+
if (!Number.isNaN(syncedAtMs) && mtimeMs > syncedAtMs + 1000) {
|
|
150
|
+
// 1s grace for filesystem clock jitter.
|
|
151
|
+
return {
|
|
152
|
+
path: relPath,
|
|
153
|
+
entry,
|
|
154
|
+
clean: false,
|
|
155
|
+
dirtyReason: "modified-after-sync",
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Hash check — final word. If the content matches the journaled hash,
|
|
160
|
+
// the file is provably what the last pull left there.
|
|
161
|
+
let actualHash: string;
|
|
162
|
+
try {
|
|
163
|
+
actualHash = hashFile(absPath);
|
|
164
|
+
} catch {
|
|
165
|
+
return {
|
|
166
|
+
path: relPath,
|
|
167
|
+
entry,
|
|
168
|
+
clean: false,
|
|
169
|
+
dirtyReason: "stat-error",
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (actualHash !== entry.hash) {
|
|
173
|
+
return {
|
|
174
|
+
path: relPath,
|
|
175
|
+
entry,
|
|
176
|
+
clean: false,
|
|
177
|
+
dirtyReason: "hash-mismatch",
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
return { path: relPath, entry, clean: true };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Structured error thrown when the engine refuses to proceed because a scope
|
|
185
|
+
* shrink would orphan dirty files. The CLI catches this and renders the
|
|
186
|
+
* operator-facing message; the engine never prints directly.
|
|
187
|
+
*/
|
|
188
|
+
export class ScopeShrinkBlockedError extends Error {
|
|
189
|
+
readonly code = "SCOPE_SHRINK_BLOCKED";
|
|
190
|
+
constructor(
|
|
191
|
+
public readonly companyUid: string,
|
|
192
|
+
public readonly fromMode: PullRecord["syncMode"] | "unknown",
|
|
193
|
+
public readonly toMode: PullRecord["syncMode"],
|
|
194
|
+
public readonly dirty: OrphanClassification[],
|
|
195
|
+
public readonly clean: OrphanClassification[],
|
|
196
|
+
) {
|
|
197
|
+
super(
|
|
198
|
+
`Sync scope shrank for ${companyUid} (${fromMode} → ${toMode}); ` +
|
|
199
|
+
`${dirty.length} dirty file(s) outside the new scope would be ` +
|
|
200
|
+
`pruned from the journal. Resolve or pass --force-scope-shrink.`,
|
|
201
|
+
);
|
|
202
|
+
this.name = "ScopeShrinkBlockedError";
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface ApplyScopeShrinkInput {
|
|
207
|
+
journal: SyncJournal;
|
|
208
|
+
plan: ScopeShrinkPlan;
|
|
209
|
+
hqRoot: string;
|
|
210
|
+
/**
|
|
211
|
+
* When `true`, dirty files are LEFT ON DISK and their journal entries are
|
|
212
|
+
* tombstoned anyway. When `false` (default), the caller should have
|
|
213
|
+
* already aborted on dirty orphans — this function still tombstones any
|
|
214
|
+
* dirty entries handed to it, on the assumption the caller knows what
|
|
215
|
+
* it's doing.
|
|
216
|
+
*/
|
|
217
|
+
forceScopeShrink: boolean;
|
|
218
|
+
reason?: "scope_shrink" | "narrow_apply" | "manual";
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface ApplyScopeShrinkResult {
|
|
222
|
+
cleanRemoved: number;
|
|
223
|
+
dirtyTombstoned: number;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Apply a scope-shrink plan: delete clean orphans on disk + tombstone their
|
|
228
|
+
* journal entries. With `forceScopeShrink: true`, dirty orphans are
|
|
229
|
+
* preserved on disk but their journal entries are also tombstoned.
|
|
230
|
+
*
|
|
231
|
+
* Returns counts for the audit log row (`scope_shrink_blocked` /
|
|
232
|
+
* `scope_shrink_forced`).
|
|
233
|
+
*/
|
|
234
|
+
export function applyScopeShrink(
|
|
235
|
+
input: ApplyScopeShrinkInput,
|
|
236
|
+
): ApplyScopeShrinkResult {
|
|
237
|
+
const { journal, plan, hqRoot, forceScopeShrink } = input;
|
|
238
|
+
const reason = input.reason ?? "scope_shrink";
|
|
239
|
+
let cleanRemoved = 0;
|
|
240
|
+
let dirtyTombstoned = 0;
|
|
241
|
+
|
|
242
|
+
for (const orphan of plan.clean) {
|
|
243
|
+
const absPath = path.join(hqRoot, orphan.path);
|
|
244
|
+
try {
|
|
245
|
+
fs.unlinkSync(absPath);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
248
|
+
if (code !== "ENOENT") throw err; // missing-on-disk is fine; anything else escalates
|
|
249
|
+
}
|
|
250
|
+
tombstoneEntry(journal, orphan.path, reason);
|
|
251
|
+
cleanRemoved++;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (forceScopeShrink) {
|
|
255
|
+
for (const orphan of plan.dirty) {
|
|
256
|
+
// Do NOT delete the file — that's the entire point of the `--force`
|
|
257
|
+
// contract: keep dirty content on disk, prune only the journal entry.
|
|
258
|
+
tombstoneEntry(journal, orphan.path, reason);
|
|
259
|
+
dirtyTombstoned++;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return { cleanRemoved, dirtyTombstoned };
|
|
264
|
+
}
|