sdtk-spec-kit 0.3.16 → 0.3.18
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/README.md +131 -2
- package/assets/atlas/build_atlas.py +865 -0
- package/assets/atlas/doc_atlas_viewer_template.html +3706 -0
- package/assets/atlas/vendor/mermaid.min.js +2029 -0
- package/assets/manifest/toolkit-bundle.manifest.json +6 -6
- package/assets/manifest/toolkit-bundle.sha256.txt +2 -2
- package/assets/toolkit/toolkit/SDTK_TOOLKIT.md +14 -0
- package/assets/toolkit/toolkit/skills/sdtk-orchestrator/SKILL.md +1 -1
- package/package.json +1 -1
- package/src/commands/atlas.js +379 -0
- package/src/commands/entitlement.js +163 -0
- package/src/commands/help.js +140 -6
- package/src/commands/project.js +51 -0
- package/src/index.js +10 -1
- package/src/lib/atlas-ask.js +170 -0
- package/src/lib/atlas-config.js +200 -0
- package/src/lib/atlas-runner.js +355 -0
- package/src/lib/browser-open.js +39 -0
- package/src/lib/capabilities.js +98 -0
- package/src/lib/entitlements.js +203 -0
- package/src/lib/github-access.js +75 -0
- package/src/lib/legacy-auth-migration.js +56 -0
- package/src/lib/premium-loader.js +126 -0
- package/src/lib/premium-packs.js +333 -0
- package/src/lib/premium-sync.js +235 -0
- package/src/lib/project-audit.js +332 -0
- package/src/lib/project-config.js +108 -0
- package/src/lib/project-ingest.js +450 -0
- package/src/lib/project-refresh.js +666 -0
- package/src/lib/project-target-guard.js +28 -2
- package/src/lib/state.js +37 -46
- package/src/lib/suite-state.js +65 -0
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { parseFlags } = require("./args");
|
|
6
|
+
const { resolveProjectConfig } = require("./project-config");
|
|
7
|
+
const { assertProjectRefreshTargetAllowed } = require("./project-target-guard");
|
|
8
|
+
const { loadPremiumHandler } = require("./premium-loader");
|
|
9
|
+
|
|
10
|
+
const CAPABILITY = "spec.project.refresh";
|
|
11
|
+
const SCHEMA_VERSION = "bk085-r1";
|
|
12
|
+
const ISSUE_ID = "BK-085";
|
|
13
|
+
|
|
14
|
+
const DOC_SYNTHESIS_SCHEMA_VERSION = 1;
|
|
15
|
+
|
|
16
|
+
const VALID_CONFIDENCE_VALUES = new Set(["low", "medium", "high"]);
|
|
17
|
+
|
|
18
|
+
// Required baseline files/dirs that must exist before refresh can run.
|
|
19
|
+
const REQUIRED_BASELINE_FILES = [
|
|
20
|
+
"configPath",
|
|
21
|
+
"statePath",
|
|
22
|
+
"censusPath",
|
|
23
|
+
"docSynthesisStatePath",
|
|
24
|
+
];
|
|
25
|
+
const REQUIRED_BASELINE_DIRS = [
|
|
26
|
+
"docBaselineDir",
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
// Required staged docs that must still be present after refresh.
|
|
30
|
+
const REQUIRED_STAGED_DOCS = [
|
|
31
|
+
"docs/product/PROJECT_BASELINE_SUMMARY.md",
|
|
32
|
+
"docs/architecture/ARCH_DESIGN_PROJECT_BASELINE.md",
|
|
33
|
+
"docs/database/DATABASE_SPEC_PROJECT_BASELINE.md",
|
|
34
|
+
"docs/api/PROJECT_API_SURFACE_BASELINE.md",
|
|
35
|
+
"docs/dev/PROJECT_RUNTIME_BASELINE.md",
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
// BK-088: Required system-level documentation pack.
|
|
39
|
+
// These four docs must remain coherent after every successful refresh.
|
|
40
|
+
const REQUIRED_SYSTEM_DOCS = [
|
|
41
|
+
"docs/system/SYSTEM_CONTEXT.md",
|
|
42
|
+
"docs/system/MODULE_MAP.md",
|
|
43
|
+
"docs/system/INTEGRATION_CATALOG.md",
|
|
44
|
+
"docs/system/DEPLOYMENT_TOPOLOGY.md",
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const REFRESH_FLAG_DEFS = {
|
|
48
|
+
"project-path": { type: "string" },
|
|
49
|
+
"output-dir": { type: "string" },
|
|
50
|
+
json: { type: "boolean" },
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Return true when relPath is a safe relative path that stays inside baseDir.
|
|
55
|
+
*
|
|
56
|
+
* Rejects absolute paths, paths containing ".." segments, and paths that
|
|
57
|
+
* resolve outside baseDir after normalization.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} relPath
|
|
60
|
+
* @param {string} baseDir - absolute path used as the containment root
|
|
61
|
+
* @returns {boolean}
|
|
62
|
+
*/
|
|
63
|
+
function isSafeRelativeStagedPath(relPath, baseDir) {
|
|
64
|
+
if (!relPath || typeof relPath !== "string") return false;
|
|
65
|
+
if (path.isAbsolute(relPath)) return false;
|
|
66
|
+
const parts = relPath.replace(/\\/g, "/").split("/");
|
|
67
|
+
for (const part of parts) {
|
|
68
|
+
if (part === "..") return false;
|
|
69
|
+
}
|
|
70
|
+
const normalizedBase = path.resolve(baseDir);
|
|
71
|
+
const resolved = path.resolve(baseDir, relPath);
|
|
72
|
+
return (
|
|
73
|
+
resolved === normalizedBase ||
|
|
74
|
+
resolved.startsWith(normalizedBase + path.sep)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validate a single staged doc entry returned by the refresh handler.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} relPath
|
|
82
|
+
* @param {*} entry
|
|
83
|
+
* @param {string} docBaselineDir - absolute path used for path-safety check
|
|
84
|
+
* @returns {{ valid: true } | { valid: false, reason: string }}
|
|
85
|
+
*/
|
|
86
|
+
function validateRefreshEntry(relPath, entry, docBaselineDir) {
|
|
87
|
+
if (!isSafeRelativeStagedPath(relPath, docBaselineDir)) {
|
|
88
|
+
return {
|
|
89
|
+
valid: false,
|
|
90
|
+
reason: `staged doc path is invalid or unsafe: "${relPath}"`,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
|
|
94
|
+
return {
|
|
95
|
+
valid: false,
|
|
96
|
+
reason: `docBaseline.files["${relPath}"] must be a plain object`,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (typeof entry.content !== "string" || entry.content.trim() === "") {
|
|
100
|
+
return {
|
|
101
|
+
valid: false,
|
|
102
|
+
reason: `docBaseline.files["${relPath}"].content must be a non-empty string`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (!Array.isArray(entry.sourcePacks) || entry.sourcePacks.length === 0) {
|
|
106
|
+
return {
|
|
107
|
+
valid: false,
|
|
108
|
+
reason: `docBaseline.files["${relPath}"].sourcePacks must be a non-empty array`,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (!VALID_CONFIDENCE_VALUES.has(entry.confidence)) {
|
|
112
|
+
return {
|
|
113
|
+
valid: false,
|
|
114
|
+
reason: `docBaseline.files["${relPath}"].confidence must be low, medium, or high`,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (entry.reviewRequired !== true) {
|
|
118
|
+
return {
|
|
119
|
+
valid: false,
|
|
120
|
+
reason: `docBaseline.files["${relPath}"].reviewRequired must be true`,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (!Array.isArray(entry.nonClaims)) {
|
|
124
|
+
return {
|
|
125
|
+
valid: false,
|
|
126
|
+
reason: `docBaseline.files["${relPath}"].nonClaims must be an array`,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return { valid: true };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Load existing DOC_SYNTHESIS_STATE.json to get the list of previously managed doc paths.
|
|
134
|
+
*
|
|
135
|
+
* Returns a Set of relative doc paths that were previously managed (relative to docBaselineDir).
|
|
136
|
+
*
|
|
137
|
+
* @param {{ docSynthesisStatePath: string }} config
|
|
138
|
+
* @returns {Set<string>}
|
|
139
|
+
*/
|
|
140
|
+
function loadExistingManagedDocState(config) {
|
|
141
|
+
try {
|
|
142
|
+
const raw = fs.readFileSync(config.docSynthesisStatePath, "utf8");
|
|
143
|
+
const state = JSON.parse(raw);
|
|
144
|
+
const managed = new Set();
|
|
145
|
+
if (state && typeof state === "object" && state.docSources) {
|
|
146
|
+
for (const relPath of Object.keys(state.docSources)) {
|
|
147
|
+
managed.add(relPath);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return managed;
|
|
151
|
+
} catch (_) {
|
|
152
|
+
return new Set();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Validate the full refresh result contract returned by the premium handler.
|
|
158
|
+
*
|
|
159
|
+
* @param {*} result
|
|
160
|
+
* @param {{ docBaselineDir: string }} config
|
|
161
|
+
* @param {Set<string>} previousDocState - previously managed doc paths
|
|
162
|
+
* @returns {{ valid: true } | { valid: false, reason: string }}
|
|
163
|
+
*/
|
|
164
|
+
function validateRefreshResult(result, config, previousDocState) {
|
|
165
|
+
if (!result || typeof result !== "object" || Array.isArray(result)) {
|
|
166
|
+
return { valid: false, reason: "result is not an object" };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// All BK-084 top-level objects remain required.
|
|
170
|
+
const requiredObjects = [
|
|
171
|
+
"census",
|
|
172
|
+
"inventory",
|
|
173
|
+
"profile",
|
|
174
|
+
"moduleGraph",
|
|
175
|
+
"runtimeMarkers",
|
|
176
|
+
"evidencePacks",
|
|
177
|
+
"docBaseline",
|
|
178
|
+
"summary",
|
|
179
|
+
];
|
|
180
|
+
for (const key of requiredObjects) {
|
|
181
|
+
if (!(key in result)) {
|
|
182
|
+
return { valid: false, reason: `missing required key: ${key}` };
|
|
183
|
+
}
|
|
184
|
+
const val = result[key];
|
|
185
|
+
if (!val || typeof val !== "object" || Array.isArray(val)) {
|
|
186
|
+
return { valid: false, reason: `${key} must be a plain object` };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// New top-level refresh object is required.
|
|
191
|
+
if (!("refresh" in result)) {
|
|
192
|
+
return { valid: false, reason: "missing required key: refresh" };
|
|
193
|
+
}
|
|
194
|
+
const refresh = result.refresh;
|
|
195
|
+
if (!refresh || typeof refresh !== "object" || Array.isArray(refresh)) {
|
|
196
|
+
return { valid: false, reason: "refresh must be a plain object" };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// refresh.sourceMode must equal "incremental-managed-refresh".
|
|
200
|
+
if (refresh.sourceMode !== "incremental-managed-refresh") {
|
|
201
|
+
return {
|
|
202
|
+
valid: false,
|
|
203
|
+
reason: `refresh.sourceMode must be "incremental-managed-refresh", got "${refresh.sourceMode}"`,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// refresh.changedFiles and refresh.removedSourceFiles must be integers >= 0.
|
|
208
|
+
if (
|
|
209
|
+
typeof refresh.changedFiles !== "number" ||
|
|
210
|
+
!Number.isInteger(refresh.changedFiles) ||
|
|
211
|
+
refresh.changedFiles < 0
|
|
212
|
+
) {
|
|
213
|
+
return {
|
|
214
|
+
valid: false,
|
|
215
|
+
reason: "refresh.changedFiles must be an integer >= 0",
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
if (
|
|
219
|
+
typeof refresh.removedSourceFiles !== "number" ||
|
|
220
|
+
!Number.isInteger(refresh.removedSourceFiles) ||
|
|
221
|
+
refresh.removedSourceFiles < 0
|
|
222
|
+
) {
|
|
223
|
+
return {
|
|
224
|
+
valid: false,
|
|
225
|
+
reason: "refresh.removedSourceFiles must be an integer >= 0",
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// refresh.affectedEvidencePacks, refresh.regeneratedDocs, refresh.removedDocs must be arrays.
|
|
230
|
+
if (!Array.isArray(refresh.affectedEvidencePacks)) {
|
|
231
|
+
return { valid: false, reason: "refresh.affectedEvidencePacks must be an array" };
|
|
232
|
+
}
|
|
233
|
+
if (!Array.isArray(refresh.regeneratedDocs)) {
|
|
234
|
+
return { valid: false, reason: "refresh.regeneratedDocs must be an array" };
|
|
235
|
+
}
|
|
236
|
+
if (!Array.isArray(refresh.removedDocs)) {
|
|
237
|
+
return { valid: false, reason: "refresh.removedDocs must be an array" };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Validate docBaseline block.
|
|
241
|
+
const docBaseline = result.docBaseline;
|
|
242
|
+
if (
|
|
243
|
+
!docBaseline.files ||
|
|
244
|
+
typeof docBaseline.files !== "object" ||
|
|
245
|
+
Array.isArray(docBaseline.files)
|
|
246
|
+
) {
|
|
247
|
+
return { valid: false, reason: "docBaseline.files must be a plain object" };
|
|
248
|
+
}
|
|
249
|
+
if (
|
|
250
|
+
!docBaseline.summary ||
|
|
251
|
+
typeof docBaseline.summary !== "object" ||
|
|
252
|
+
Array.isArray(docBaseline.summary)
|
|
253
|
+
) {
|
|
254
|
+
return { valid: false, reason: "docBaseline.summary must be a plain object" };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Validate each file entry in docBaseline.files.
|
|
258
|
+
for (const [relPath, entry] of Object.entries(docBaseline.files)) {
|
|
259
|
+
const entryResult = validateRefreshEntry(relPath, entry, config.docBaselineDir);
|
|
260
|
+
if (!entryResult.valid) return entryResult;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Validate every path in refresh.removedDocs is safe under DOC_SYNTHESIS_BASELINE/docs/.
|
|
264
|
+
for (const removedPath of refresh.removedDocs) {
|
|
265
|
+
if (!isSafeRelativeStagedPath(removedPath, config.docBaselineDir)) {
|
|
266
|
+
return {
|
|
267
|
+
valid: false,
|
|
268
|
+
reason: `managed staged-doc path is invalid or unsafe in removedDocs: "${removedPath}"`,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// docBaseline.summary.removedFiles, when present, must equal refresh.removedDocs.
|
|
274
|
+
if (Array.isArray(docBaseline.summary.removedFiles)) {
|
|
275
|
+
const summaryRemoved = docBaseline.summary.removedFiles;
|
|
276
|
+
const contractRemoved = refresh.removedDocs;
|
|
277
|
+
const summarySet = new Set(summaryRemoved);
|
|
278
|
+
const contractSet = new Set(contractRemoved);
|
|
279
|
+
for (const p of contractSet) {
|
|
280
|
+
if (!summarySet.has(p)) {
|
|
281
|
+
return {
|
|
282
|
+
valid: false,
|
|
283
|
+
reason: `docBaseline.summary.removedFiles does not match refresh.removedDocs: "${p}" missing from summary`,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
for (const p of summarySet) {
|
|
288
|
+
if (!contractSet.has(p)) {
|
|
289
|
+
return {
|
|
290
|
+
valid: false,
|
|
291
|
+
reason: `docBaseline.summary.removedFiles does not match refresh.removedDocs: "${p}" missing from contract`,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Required staged docs must remain present after refresh (unless handler explicitly listed them in removedDocs).
|
|
298
|
+
const removedSet = new Set(refresh.removedDocs);
|
|
299
|
+
for (const requiredDoc of REQUIRED_STAGED_DOCS) {
|
|
300
|
+
if (removedSet.has(requiredDoc)) {
|
|
301
|
+
return {
|
|
302
|
+
valid: false,
|
|
303
|
+
reason: `required staged doc must not be removed: ${requiredDoc}`,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
if (!(requiredDoc in docBaseline.files)) {
|
|
307
|
+
return {
|
|
308
|
+
valid: false,
|
|
309
|
+
reason: `missing required staged doc in refresh result: ${requiredDoc}`,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Required system-level docs (BK-088) must remain complete after refresh.
|
|
315
|
+
for (const requiredDoc of REQUIRED_SYSTEM_DOCS) {
|
|
316
|
+
if (removedSet.has(requiredDoc)) {
|
|
317
|
+
return {
|
|
318
|
+
valid: false,
|
|
319
|
+
reason: `required staged system doc must not be removed: ${requiredDoc}`,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
if (!(requiredDoc in docBaseline.files)) {
|
|
323
|
+
return {
|
|
324
|
+
valid: false,
|
|
325
|
+
reason: `missing required staged system doc in refresh result: ${requiredDoc}`,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return { valid: true };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Safely delete stale managed staged-doc files.
|
|
335
|
+
*
|
|
336
|
+
* A file is deleted only when:
|
|
337
|
+
* 1. Its path is safe under the managed staged root (docBaselineDir).
|
|
338
|
+
* 2. It was previously recorded as managed in DOC_SYNTHESIS_STATE.json.
|
|
339
|
+
* 3. It is listed in the validated refresh.removedDocs set.
|
|
340
|
+
*
|
|
341
|
+
* @param {{ docBaselineDir: string }} config
|
|
342
|
+
* @param {Set<string>} previousDocState
|
|
343
|
+
* @param {string[]} removedDocs
|
|
344
|
+
* @returns {{ deleted: string[], skipped: string[], error: string|null }}
|
|
345
|
+
*/
|
|
346
|
+
function deleteRemovedManagedDocs(config, previousDocState, removedDocs) {
|
|
347
|
+
const deleted = [];
|
|
348
|
+
const skipped = [];
|
|
349
|
+
|
|
350
|
+
for (const relPath of removedDocs) {
|
|
351
|
+
// Safety: must be a safe relative path inside the managed staged root.
|
|
352
|
+
if (!isSafeRelativeStagedPath(relPath, config.docBaselineDir)) {
|
|
353
|
+
return { deleted, skipped, error: `unsafe staged-doc path: "${relPath}"` };
|
|
354
|
+
}
|
|
355
|
+
// Must have been previously managed.
|
|
356
|
+
if (!previousDocState.has(relPath)) {
|
|
357
|
+
skipped.push(relPath);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
const fullPath = path.join(config.docBaselineDir, relPath);
|
|
361
|
+
if (!fs.existsSync(fullPath)) {
|
|
362
|
+
skipped.push(relPath);
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
fs.unlinkSync(fullPath);
|
|
367
|
+
deleted.push(relPath);
|
|
368
|
+
} catch (err) {
|
|
369
|
+
return { deleted, skipped, error: `failed to delete "${relPath}": ${err.message}` };
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return { deleted, skipped, error: null };
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Print deterministic human-readable refresh summary.
|
|
378
|
+
*
|
|
379
|
+
* @param {*} result
|
|
380
|
+
* @param {{ outputDir: string, docBaselineDir: string }} config
|
|
381
|
+
* @param {string[]} deletedDocs
|
|
382
|
+
*/
|
|
383
|
+
function renderHumanRefresh(result, config, deletedDocs) {
|
|
384
|
+
const refresh = result.refresh;
|
|
385
|
+
const s = result.summary;
|
|
386
|
+
console.log("[project refresh] Incremental managed refresh complete.");
|
|
387
|
+
console.log(` Source mode: ${refresh.sourceMode}`);
|
|
388
|
+
console.log(` Changed files: ${refresh.changedFiles}`);
|
|
389
|
+
console.log(` Removed sources: ${refresh.removedSourceFiles}`);
|
|
390
|
+
if (refresh.affectedEvidencePacks.length > 0) {
|
|
391
|
+
console.log(` Affected packs: ${refresh.affectedEvidencePacks.join(", ")}`);
|
|
392
|
+
} else {
|
|
393
|
+
console.log(` Affected packs: 0`);
|
|
394
|
+
}
|
|
395
|
+
if (typeof s.regeneratedDocs === "number") {
|
|
396
|
+
console.log(` Regenerated docs: ${s.regeneratedDocs}`);
|
|
397
|
+
} else if (refresh.regeneratedDocs.length > 0) {
|
|
398
|
+
console.log(` Regenerated docs: ${refresh.regeneratedDocs.length}`);
|
|
399
|
+
}
|
|
400
|
+
if (deletedDocs.length > 0) {
|
|
401
|
+
console.log(` Removed docs: ${deletedDocs.length} (${deletedDocs.join(", ")})`);
|
|
402
|
+
}
|
|
403
|
+
console.log(` Output dir: ${config.outputDir}`);
|
|
404
|
+
console.log(` Staged baseline: ${config.docBaselineDir}`);
|
|
405
|
+
console.log(` Note: live /docs/ was not modified.`);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Execute sdtk-spec project refresh premium command.
|
|
410
|
+
*
|
|
411
|
+
* Flow:
|
|
412
|
+
* 1. Parse flags and resolve project config / output paths.
|
|
413
|
+
* 2. Reject the SDTK maintainer monorepo root via the target guard.
|
|
414
|
+
* 3. Verify required managed ingest baseline exists.
|
|
415
|
+
* 4. Load and verify the premium handler via loadPremiumHandler.
|
|
416
|
+
* 5. Build bounded refresh context object (mode: incremental-refresh).
|
|
417
|
+
* 6. Execute the premium handler.
|
|
418
|
+
* 7. Validate the returned refresh-result contract.
|
|
419
|
+
* 8. Load existing managed doc state (for stale deletion).
|
|
420
|
+
* 9. Rewrite managed foundation JSON outputs.
|
|
421
|
+
* 10. Rewrite evidence packs.
|
|
422
|
+
* 11. Rewrite staged docs under DOC_SYNTHESIS_BASELINE/docs/...
|
|
423
|
+
* 12. Remove stale managed staged docs.
|
|
424
|
+
* 13. Rewrite INGEST_STATE.json, DOC_SYNTHESIS_STATE.json, REFRESH_STATE.json.
|
|
425
|
+
* 14. Print human or JSON summary.
|
|
426
|
+
*
|
|
427
|
+
* @param {string[]} args - Remaining argv after "project refresh"
|
|
428
|
+
* @returns {Promise<number>} CLI exit code
|
|
429
|
+
*/
|
|
430
|
+
async function runProjectRefresh(args) {
|
|
431
|
+
const { flags } = parseFlags(args, REFRESH_FLAG_DEFS);
|
|
432
|
+
|
|
433
|
+
// 1. Resolve project config and canonical output paths.
|
|
434
|
+
let config;
|
|
435
|
+
try {
|
|
436
|
+
config = resolveProjectConfig(flags);
|
|
437
|
+
} catch (err) {
|
|
438
|
+
console.error(`[project refresh] ${err.message}`);
|
|
439
|
+
return 1;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// 2. Reject maintainer repo root as refresh target.
|
|
443
|
+
try {
|
|
444
|
+
assertProjectRefreshTargetAllowed("sdtk-spec", "refresh", config.projectPath);
|
|
445
|
+
} catch (err) {
|
|
446
|
+
console.error(`[project refresh] ${err.message}`);
|
|
447
|
+
return 1;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// 3. Verify required managed ingest baseline exists.
|
|
451
|
+
for (const key of REQUIRED_BASELINE_FILES) {
|
|
452
|
+
if (!fs.existsSync(config[key])) {
|
|
453
|
+
console.error(
|
|
454
|
+
`[project refresh] project refresh requires a prior "sdtk-spec project ingest" run. ` +
|
|
455
|
+
`Missing managed workspace file: ${config[key]}. ` +
|
|
456
|
+
`Run "sdtk-spec project ingest --project-path ${config.projectPath}" first.`
|
|
457
|
+
);
|
|
458
|
+
return 1;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
for (const key of REQUIRED_BASELINE_DIRS) {
|
|
462
|
+
if (!fs.existsSync(config[key])) {
|
|
463
|
+
console.error(
|
|
464
|
+
`[project refresh] project refresh requires a prior "sdtk-spec project ingest" run. ` +
|
|
465
|
+
`Missing managed workspace directory: ${config[key]}. ` +
|
|
466
|
+
`Run "sdtk-spec project ingest --project-path ${config.projectPath}" first.`
|
|
467
|
+
);
|
|
468
|
+
return 1;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// 4. Load and verify premium handler (entitlement + pack verification).
|
|
473
|
+
const loaderResult = await loadPremiumHandler(CAPABILITY, "projectRefresh");
|
|
474
|
+
if (!loaderResult.ok) {
|
|
475
|
+
console.error(`[project refresh] ${loaderResult.message}`);
|
|
476
|
+
return loaderResult.exitCode;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// 5. Build bounded refresh context object.
|
|
480
|
+
const context = {
|
|
481
|
+
capability: CAPABILITY,
|
|
482
|
+
mode: "incremental-refresh",
|
|
483
|
+
projectPath: config.projectPath,
|
|
484
|
+
outputDir: config.outputDir,
|
|
485
|
+
configPath: config.configPath,
|
|
486
|
+
statePath: config.statePath,
|
|
487
|
+
refreshStatePath: config.refreshStatePath,
|
|
488
|
+
censusPath: config.censusPath,
|
|
489
|
+
inventoryPath: config.inventoryPath,
|
|
490
|
+
profilePath: config.profilePath,
|
|
491
|
+
moduleGraphPath: config.moduleGraphPath,
|
|
492
|
+
runtimeMarkersPath: config.runtimeMarkersPath,
|
|
493
|
+
evidencePacksDir: config.evidencePacksDir,
|
|
494
|
+
docBaselineDir: config.docBaselineDir,
|
|
495
|
+
docBaselineDocsRoot: config.docBaselineDocsRoot,
|
|
496
|
+
docSynthesisStatePath: config.docSynthesisStatePath,
|
|
497
|
+
json: !!flags.json,
|
|
498
|
+
argv: args,
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// 6. Execute premium handler.
|
|
502
|
+
let result;
|
|
503
|
+
try {
|
|
504
|
+
result = await loaderResult.handler(context);
|
|
505
|
+
} catch (err) {
|
|
506
|
+
console.error(`[project refresh] Premium pack handler error: ${err.message}`);
|
|
507
|
+
return 4;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// 7. Validate refresh-result contract.
|
|
511
|
+
// Load previous doc state before validation so we can use it in stale-deletion.
|
|
512
|
+
const previousDocState = loadExistingManagedDocState(config);
|
|
513
|
+
const validation = validateRefreshResult(result, config, previousDocState);
|
|
514
|
+
if (!validation.valid) {
|
|
515
|
+
console.error(
|
|
516
|
+
`[project refresh] Premium pack output contract is invalid: ${validation.reason}`
|
|
517
|
+
);
|
|
518
|
+
return 3;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// 8. Rewrite managed foundation JSON outputs.
|
|
522
|
+
try {
|
|
523
|
+
fs.writeFileSync(config.censusPath, JSON.stringify(result.census, null, 2), "utf8");
|
|
524
|
+
fs.writeFileSync(config.inventoryPath, JSON.stringify(result.inventory, null, 2), "utf8");
|
|
525
|
+
fs.writeFileSync(config.profilePath, JSON.stringify(result.profile, null, 2), "utf8");
|
|
526
|
+
fs.writeFileSync(config.moduleGraphPath, JSON.stringify(result.moduleGraph, null, 2), "utf8");
|
|
527
|
+
fs.writeFileSync(config.runtimeMarkersPath, JSON.stringify(result.runtimeMarkers, null, 2), "utf8");
|
|
528
|
+
} catch (writeErr) {
|
|
529
|
+
console.error(`[project refresh] Foundation write error: ${writeErr.message}`);
|
|
530
|
+
return 4;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// 9. Rewrite evidence packs.
|
|
534
|
+
try {
|
|
535
|
+
if (!fs.existsSync(config.evidencePacksDir)) {
|
|
536
|
+
fs.mkdirSync(config.evidencePacksDir, { recursive: true });
|
|
537
|
+
}
|
|
538
|
+
for (const [packKey, packData] of Object.entries(result.evidencePacks)) {
|
|
539
|
+
const packFileName = `${packKey}.pack.json`;
|
|
540
|
+
fs.writeFileSync(
|
|
541
|
+
path.join(config.evidencePacksDir, packFileName),
|
|
542
|
+
JSON.stringify(packData, null, 2),
|
|
543
|
+
"utf8"
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
} catch (writeErr) {
|
|
547
|
+
console.error(`[project refresh] Evidence pack write error: ${writeErr.message}`);
|
|
548
|
+
return 4;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// 10. Rewrite staged docs under DOC_SYNTHESIS_BASELINE/docs/...
|
|
552
|
+
for (const [relPath, entry] of Object.entries(result.docBaseline.files)) {
|
|
553
|
+
const fullPath = path.join(config.docBaselineDir, relPath);
|
|
554
|
+
const dirPath = path.dirname(fullPath);
|
|
555
|
+
if (!fs.existsSync(dirPath)) {
|
|
556
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
557
|
+
}
|
|
558
|
+
try {
|
|
559
|
+
fs.writeFileSync(fullPath, entry.content, "utf8");
|
|
560
|
+
} catch (writeErr) {
|
|
561
|
+
console.error(
|
|
562
|
+
`[project refresh] Doc materialization error for "${relPath}": ${writeErr.message}`
|
|
563
|
+
);
|
|
564
|
+
return 4;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// 11. Remove stale managed staged docs that are listed in refresh.removedDocs
|
|
569
|
+
// only when they were previously managed.
|
|
570
|
+
const deleteResult = deleteRemovedManagedDocs(
|
|
571
|
+
config,
|
|
572
|
+
previousDocState,
|
|
573
|
+
result.refresh.removedDocs
|
|
574
|
+
);
|
|
575
|
+
if (deleteResult.error) {
|
|
576
|
+
console.error(`[project refresh] Stale doc removal error: ${deleteResult.error}`);
|
|
577
|
+
return 4;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// 12. Rewrite INGEST_STATE.json.
|
|
581
|
+
const ingestState = {
|
|
582
|
+
schemaVersion: SCHEMA_VERSION,
|
|
583
|
+
issueId: ISSUE_ID,
|
|
584
|
+
projectPath: config.projectPath,
|
|
585
|
+
outputDir: config.outputDir,
|
|
586
|
+
refreshedAt: new Date().toISOString(),
|
|
587
|
+
summary: result.summary,
|
|
588
|
+
};
|
|
589
|
+
try {
|
|
590
|
+
fs.writeFileSync(config.statePath, JSON.stringify(ingestState, null, 2), "utf8");
|
|
591
|
+
} catch (writeErr) {
|
|
592
|
+
console.error(`[project refresh] INGEST_STATE.json write error: ${writeErr.message}`);
|
|
593
|
+
return 4;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// 13. Rewrite DOC_SYNTHESIS_STATE.json.
|
|
597
|
+
const docSources = {};
|
|
598
|
+
for (const [relPath, entry] of Object.entries(result.docBaseline.files)) {
|
|
599
|
+
docSources[relPath] = entry.sourcePacks;
|
|
600
|
+
}
|
|
601
|
+
const docSynthesisState = {
|
|
602
|
+
schemaVersion: DOC_SYNTHESIS_SCHEMA_VERSION,
|
|
603
|
+
issueId: ISSUE_ID,
|
|
604
|
+
projectPath: config.projectPath,
|
|
605
|
+
outputDir: config.outputDir,
|
|
606
|
+
docBaselineDir: config.docBaselineDir,
|
|
607
|
+
refreshedAt: new Date().toISOString(),
|
|
608
|
+
docSources,
|
|
609
|
+
removedDocs: result.refresh.removedDocs,
|
|
610
|
+
summary: result.docBaseline.summary,
|
|
611
|
+
};
|
|
612
|
+
try {
|
|
613
|
+
fs.writeFileSync(
|
|
614
|
+
config.docSynthesisStatePath,
|
|
615
|
+
JSON.stringify(docSynthesisState, null, 2),
|
|
616
|
+
"utf8"
|
|
617
|
+
);
|
|
618
|
+
} catch (writeErr) {
|
|
619
|
+
console.error(`[project refresh] DOC_SYNTHESIS_STATE.json write error: ${writeErr.message}`);
|
|
620
|
+
return 4;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// 14. Write REFRESH_STATE.json.
|
|
624
|
+
const refreshState = {
|
|
625
|
+
schemaVersion: SCHEMA_VERSION,
|
|
626
|
+
issueId: ISSUE_ID,
|
|
627
|
+
projectPath: config.projectPath,
|
|
628
|
+
outputDir: config.outputDir,
|
|
629
|
+
refreshedAt: new Date().toISOString(),
|
|
630
|
+
sourceMode: result.refresh.sourceMode,
|
|
631
|
+
changedFiles: result.refresh.changedFiles,
|
|
632
|
+
removedSourceFiles: result.refresh.removedSourceFiles,
|
|
633
|
+
affectedEvidencePacks: result.refresh.affectedEvidencePacks,
|
|
634
|
+
regeneratedDocs: result.refresh.regeneratedDocs,
|
|
635
|
+
removedDocs: result.refresh.removedDocs,
|
|
636
|
+
summary: result.summary,
|
|
637
|
+
};
|
|
638
|
+
try {
|
|
639
|
+
fs.writeFileSync(
|
|
640
|
+
config.refreshStatePath,
|
|
641
|
+
JSON.stringify(refreshState, null, 2),
|
|
642
|
+
"utf8"
|
|
643
|
+
);
|
|
644
|
+
} catch (writeErr) {
|
|
645
|
+
console.error(`[project refresh] REFRESH_STATE.json write error: ${writeErr.message}`);
|
|
646
|
+
return 4;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// 15. Print summary.
|
|
650
|
+
if (flags.json) {
|
|
651
|
+
const jsonOut = {
|
|
652
|
+
refresh: result.refresh,
|
|
653
|
+
summary: result.summary,
|
|
654
|
+
outputDir: config.outputDir,
|
|
655
|
+
docBaselineDir: config.docBaselineDir,
|
|
656
|
+
refreshStatePath: config.refreshStatePath,
|
|
657
|
+
};
|
|
658
|
+
console.log(JSON.stringify(jsonOut));
|
|
659
|
+
} else {
|
|
660
|
+
renderHumanRefresh(result, config, deleteResult.deleted);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
return 0;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
module.exports = { runProjectRefresh };
|
|
@@ -22,14 +22,40 @@ function isMaintainerRoot(projectPath) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
function buildMaintainerRootMessage(cliName, action, projectPath) {
|
|
25
|
+
const actionLine =
|
|
26
|
+
action === "update"
|
|
27
|
+
? `Refusing to refresh managed project files into the SDTK maintainer repo root: ${projectPath}`
|
|
28
|
+
: action === "ingest"
|
|
29
|
+
? `Refusing to use the SDTK maintainer repo root as a project ingest target: ${projectPath}`
|
|
30
|
+
: action === "audit"
|
|
31
|
+
? `Refusing to use the SDTK maintainer repo root as a project audit target: ${projectPath}`
|
|
32
|
+
: action === "refresh"
|
|
33
|
+
? `Refusing to use the SDTK maintainer repo root as a project refresh target: ${projectPath}`
|
|
34
|
+
: `Refusing to refresh managed project files into the SDTK maintainer repo root: ${projectPath}`;
|
|
35
|
+
|
|
36
|
+
const overwriteLine =
|
|
37
|
+
action === "ingest"
|
|
38
|
+
? `The ${action} flow must target a consumer project repo and must not derive premium ingest artifacts from repo-owned maintainer files such as ${PROTECTED_FILES.join(", ")}.`
|
|
39
|
+
: action === "audit"
|
|
40
|
+
? `The ${action} flow must target a consumer project repo and must not run a premium audit against repo-owned maintainer files such as ${PROTECTED_FILES.join(", ")}.`
|
|
41
|
+
: action === "refresh"
|
|
42
|
+
? `The ${action} flow must target a consumer project repo and must not run a premium refresh against repo-owned maintainer files such as ${PROTECTED_FILES.join(", ")}.`
|
|
43
|
+
: `The ${action} flow would overwrite repo-owned files such as ${PROTECTED_FILES.join(", ")}.`;
|
|
44
|
+
|
|
25
45
|
const lines = [
|
|
26
|
-
|
|
27
|
-
|
|
46
|
+
actionLine,
|
|
47
|
+
overwriteLine,
|
|
28
48
|
"Target a consumer project path instead of the SDTK maintainer monorepo root.",
|
|
29
49
|
];
|
|
30
50
|
|
|
31
51
|
if (action === "update") {
|
|
32
52
|
lines.push(`If you only need package/runtime refresh, rerun ${cliName} update with --skip-project-files.`);
|
|
53
|
+
} else if (action === "ingest") {
|
|
54
|
+
lines.push(`If you only need premium project analysis, rerun ${cliName} project ingest against a consumer project checkout instead of the SDTK maintainer monorepo root.`);
|
|
55
|
+
} else if (action === "audit") {
|
|
56
|
+
lines.push(`If you only need a read-only project audit, rerun ${cliName} project audit against a consumer project checkout instead of the SDTK maintainer monorepo root.`);
|
|
57
|
+
} else if (action === "refresh") {
|
|
58
|
+
lines.push(`If you only need an incremental refresh, rerun ${cliName} project refresh against a consumer project checkout instead of the SDTK maintainer monorepo root.`);
|
|
33
59
|
} else {
|
|
34
60
|
lines.push(`If you only need runtime assets, use ${cliName} runtime install against the desired consumer project path instead of ${cliName} init.`);
|
|
35
61
|
}
|