@ngockhoale/ukit 3.4.75 → 3.4.77
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/CHANGELOG.md +58 -677
- package/package.json +11 -4
- package/src/cli/commands/markerStore.js +15 -0
- package/src/cli/index.js +7 -0
- package/src/core/markerStoreMigration.js +122 -0
- package/template_project/.claude/hooks/skill-router.sh +252 -33
- package/template_project/.claude/ukit/index/publication-intent.mjs +154 -26
- package/template_project/.claude/ukit/index/route-task.mjs +44 -1
- package/template_project/.claude/ukit/runtime/cron-operation-adapter.mjs +13 -1
- package/template_project/.claude/ukit/runtime/execution-ledger.mjs +410 -62
- package/template_project/.claude/ukit/runtime/operation-evidence.mjs +32 -4
- package/template_project/.claude/ukit/runtime/stop-coordinator.mjs +6 -1
- package/template_project/.omp/hooks/pre/ukit-bridge.js +441 -20
- package/scripts/bug/triage.mjs +0 -76
- package/scripts/docs/render-instructions.mjs +0 -66
- package/scripts/index/build-index.mjs +0 -102
- package/scripts/index/query-index.mjs +0 -128
- package/scripts/index/refresh-index.mjs +0 -195
- package/scripts/install/sync-installed-mirror.mjs +0 -263
- package/scripts/skill/audit-skill.mjs +0 -68
- package/scripts/test/run-handoff-isolated.mjs +0 -178
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// C5 (docs/AI_REPORT/archive/2026-09-20-CORRECTNESS.md §C5) — scoped mirror sync/check.
|
|
3
|
-
//
|
|
4
|
-
// The installed `.claude/` + `.omp/` mirror is a *rendered* copy of `template_project/.claude`
|
|
5
|
-
// + `template_project/.omp`, and hand-copying it drifted three separate ways this cycle:
|
|
6
|
-
// a template change never re-copied, a raw copy that kept its literal `{{token}}`
|
|
7
|
-
// because nobody rendered it, and `settings.json`'s two gateway-injected env keys,
|
|
8
|
-
// which make whole-file byte identity impossible by construction. Parallel lanes
|
|
9
|
-
// overwrote each other because there was no tool — only whoever remembered.
|
|
10
|
-
//
|
|
11
|
-
// node scripts/install/sync-installed-mirror.mjs --check report drift, exit non-zero
|
|
12
|
-
// node scripts/install/sync-installed-mirror.mjs rewrite drifted targets
|
|
13
|
-
//
|
|
14
|
-
// This reuses the real install pipeline (`buildInstallPlan` → `diffInstallPlan` →
|
|
15
|
-
// `applyDiffResults` → `applyGatewayResilienceEnv`) rather than reimplementing it, so
|
|
16
|
-
// "drift" is decided by exactly the code `ukit install` uses — including the
|
|
17
|
-
// gateway-env carve-out in `src/core/diffPlan.js` and the render step in
|
|
18
|
-
// `src/render/buildTemplateVariables.js`. Render-tokenized templates are compared in
|
|
19
|
-
// rendered form; `settings.json` is compared section-wise, never byte-wise.
|
|
20
|
-
//
|
|
21
|
-
// Scope is deliberately narrow — the tool only ever considers plan targets that live
|
|
22
|
-
// under `.claude/**` or `.omp/**`, so it can never touch `src/`, `docs/`, or any
|
|
23
|
-
// user-authored file:
|
|
24
|
-
//
|
|
25
|
-
// * `mergeStrategy: 'skip'` entries (e.g. `.claude/settings.local.json`) are
|
|
26
|
-
// user-owned, not template-derived. They are excluded from drift AND from apply —
|
|
27
|
-
// a hands-off file must never read as drift, and this tool is not an installer.
|
|
28
|
-
// * Mirror-only detection is limited to the target directories of *selected,
|
|
29
|
-
// non-link* manifest items — directories UKit owns wholesale from `template_project/`.
|
|
30
|
-
// A blanket walk of `.claude/` reports ~226 false positives, because pack-gated
|
|
31
|
-
// skill libraries (e.g. `.claude/skills/frontend-vue/**` on a `core`-only project)
|
|
32
|
-
// are present in the mirror but intentionally outside the current plan. Those
|
|
33
|
-
// directories are covered by `tests/consistency/templateParity.test.js` instead.
|
|
34
|
-
import fs from 'node:fs/promises';
|
|
35
|
-
import path from 'node:path';
|
|
36
|
-
import { fileURLToPath } from 'node:url';
|
|
37
|
-
|
|
38
|
-
import { buildPathConfig } from '../../src/core/paths.js';
|
|
39
|
-
import { loadManifest } from '../../src/manifest/loadManifest.js';
|
|
40
|
-
import { detectStack } from '../../src/stack/detectStack.js';
|
|
41
|
-
import { detectProjectContext } from '../../src/context/detectProjectContext.js';
|
|
42
|
-
import { detectProviders } from '../../src/context/detectProviders.js';
|
|
43
|
-
import { loadRuntimeConfig } from '../../src/core/runtimeConfig.js';
|
|
44
|
-
import { buildTemplateVariables } from '../../src/render/buildVariables.js';
|
|
45
|
-
import { buildInstallPlan } from '../../src/core/buildPlan.js';
|
|
46
|
-
import { diffInstallPlan } from '../../src/core/diffPlan.js';
|
|
47
|
-
import { applyDiffResults } from '../../src/core/applyPlan.js';
|
|
48
|
-
import { applyGatewayResilienceEnv } from '../../src/core/gatewayResilienceEnv.js';
|
|
49
|
-
|
|
50
|
-
const MANAGED_PREFIXES = ['.claude/', '.omp/'];
|
|
51
|
-
const USAGE = 'usage: sync-installed-mirror.mjs [--check]';
|
|
52
|
-
|
|
53
|
-
function isManaged(relativePath) {
|
|
54
|
-
return MANAGED_PREFIXES.some((prefix) => relativePath.startsWith(prefix));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function toRelative(projectRoot, targetPath) {
|
|
58
|
-
return path.relative(projectRoot, targetPath).split(path.sep).join('/');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async function readPackageVersion(packageRoot) {
|
|
62
|
-
try {
|
|
63
|
-
const pkg = JSON.parse(await fs.readFile(path.join(packageRoot, 'package.json'), 'utf8'));
|
|
64
|
-
return typeof pkg.version === 'string' ? pkg.version : null;
|
|
65
|
-
} catch {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// The full set of paths the plan manages, used to decide whether a file found in the
|
|
71
|
-
// mirror is unmanaged. Built from every entry (not just the managed ones) so a path
|
|
72
|
-
// that is managed by an out-of-scope entry is never reported as mirror-only.
|
|
73
|
-
function buildPlanTargetSet(plan, projectRoot) {
|
|
74
|
-
return new Set(
|
|
75
|
-
plan.entries
|
|
76
|
-
.map((entry) => toRelative(projectRoot, entry.targetPath))
|
|
77
|
-
.filter(Boolean),
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async function collectFilesRecursively(dir, out) {
|
|
82
|
-
let entries;
|
|
83
|
-
try {
|
|
84
|
-
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
85
|
-
} catch {
|
|
86
|
-
return out; // missing or unreadable directory — the create-drift lane covers it
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
for (const entry of entries) {
|
|
90
|
-
const fullPath = path.join(dir, entry.name);
|
|
91
|
-
// Symlinks are the adapter-link lane's business (`type: 'link'` items are excluded
|
|
92
|
-
// from mirror-only detection), and following one would walk another tool's tree.
|
|
93
|
-
if (entry.isSymbolicLink()) continue;
|
|
94
|
-
if (entry.isDirectory()) {
|
|
95
|
-
await collectFilesRecursively(fullPath, out);
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (entry.isFile()) out.push(fullPath);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return out;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Directories UKit installs wholesale from a template directory. Any file inside one
|
|
105
|
-
// that the plan does not ship is mirror-only drift rather than user content.
|
|
106
|
-
async function collectManagedDirectoryRoots({ plan, templatesRoot }) {
|
|
107
|
-
const roots = [];
|
|
108
|
-
|
|
109
|
-
for (const item of plan.selectedItems) {
|
|
110
|
-
if (item.type === 'link') continue;
|
|
111
|
-
const target = String(item.targetPath ?? '');
|
|
112
|
-
if (!isManaged(target)) continue;
|
|
113
|
-
|
|
114
|
-
const source = path.join(templatesRoot, String(item.sourceTemplate ?? ''));
|
|
115
|
-
let stat;
|
|
116
|
-
try {
|
|
117
|
-
stat = await fs.stat(source);
|
|
118
|
-
} catch {
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (stat.isDirectory()) roots.push(target.replace(/\/+$/, ''));
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return roots;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async function collectMirrorOnlyPaths({ plan, projectRoot, templatesRoot }) {
|
|
128
|
-
const planTargets = buildPlanTargetSet(plan, projectRoot);
|
|
129
|
-
const roots = await collectManagedDirectoryRoots({ plan, templatesRoot });
|
|
130
|
-
const mirrorOnly = [];
|
|
131
|
-
|
|
132
|
-
for (const root of roots) {
|
|
133
|
-
const files = await collectFilesRecursively(path.join(projectRoot, root), []);
|
|
134
|
-
for (const file of files) {
|
|
135
|
-
const relativePath = toRelative(projectRoot, file);
|
|
136
|
-
if (!planTargets.has(relativePath)) mirrorOnly.push(relativePath);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return [...new Set(mirrorOnly)].sort();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Hooks write their own state under `<managed dir>/.ukit/storage/` (stop-coordinator, task-watchdog,
|
|
144
|
-
// observability). That is protected runtime state, not template drift: it is reported for transparency but never
|
|
145
|
-
// fails the check, is never rewritten, and is never offered for deletion. Any other unshipped file stays mirror-only.
|
|
146
|
-
const RUNTIME_STATE_SEGMENT = '/.ukit/storage/';
|
|
147
|
-
function splitRuntimeState(paths) {
|
|
148
|
-
const runtimeState = paths.filter((p) => `/${p}`.includes(RUNTIME_STATE_SEGMENT));
|
|
149
|
-
return { mirrorOnly: paths.filter((p) => !runtimeState.includes(p)), runtimeState };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
async function main() {
|
|
153
|
-
const args = process.argv.slice(2);
|
|
154
|
-
const unknown = args.filter((arg) => arg !== '--check');
|
|
155
|
-
if (unknown.length > 0) {
|
|
156
|
-
console.error(`${USAGE}\nunknown flag: ${unknown[0]}`);
|
|
157
|
-
process.exit(2);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const checkOnly = args.includes('--check');
|
|
161
|
-
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
162
|
-
const projectRoot = process.cwd();
|
|
163
|
-
const pathConfig = buildPathConfig({ packageRoot, projectRoot });
|
|
164
|
-
|
|
165
|
-
const manifest = await loadManifest(pathConfig.manifestPath);
|
|
166
|
-
const stackContext = await detectStack(projectRoot);
|
|
167
|
-
const projectContext = await detectProjectContext(projectRoot);
|
|
168
|
-
const providerContext = await detectProviders(projectRoot);
|
|
169
|
-
const runtimeConfig = await loadRuntimeConfig(projectRoot);
|
|
170
|
-
|
|
171
|
-
// Same variable set as `runInstallPipeline`, so render-tokenized templates compare
|
|
172
|
-
// in the exact form `ukit install` would have written them.
|
|
173
|
-
const variables = buildTemplateVariables({
|
|
174
|
-
projectContext,
|
|
175
|
-
stackContext,
|
|
176
|
-
packageVersion: await readPackageVersion(packageRoot),
|
|
177
|
-
providerContext,
|
|
178
|
-
runtimeConfig,
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
const plan = await buildInstallPlan({
|
|
182
|
-
manifest,
|
|
183
|
-
stackContext,
|
|
184
|
-
templatesRoot: pathConfig.templatesRoot,
|
|
185
|
-
variables,
|
|
186
|
-
projectRoot,
|
|
187
|
-
// Omitted: `ukit install`'s own default adapter set (claude + codex + omp), so the
|
|
188
|
-
// `.omp/**` half of the mirror is in scope.
|
|
189
|
-
selectedAdapterItemIds: undefined,
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
const managedEntries = plan.entries.filter((entry) => {
|
|
193
|
-
if (entry.mergeStrategy === 'skip') return false;
|
|
194
|
-
return isManaged(toRelative(projectRoot, entry.targetPath));
|
|
195
|
-
});
|
|
196
|
-
const managedRelativePaths = new Set(managedEntries.map((entry) => toRelative(projectRoot, entry.targetPath)));
|
|
197
|
-
|
|
198
|
-
const diffResults = await diffInstallPlan({ entries: managedEntries });
|
|
199
|
-
const drift = diffResults
|
|
200
|
-
.filter((entry) => entry.action === 'create' || entry.action === 'update')
|
|
201
|
-
.map((entry) => ({ entry, relativePath: toRelative(projectRoot, entry.targetPath) }))
|
|
202
|
-
.sort((a, b) => a.relativePath.localeCompare(b.relativePath));
|
|
203
|
-
|
|
204
|
-
const { mirrorOnly, runtimeState } = splitRuntimeState(await collectMirrorOnlyPaths({
|
|
205
|
-
plan,
|
|
206
|
-
projectRoot,
|
|
207
|
-
templatesRoot: pathConfig.templatesRoot,
|
|
208
|
-
}));
|
|
209
|
-
|
|
210
|
-
if (checkOnly) {
|
|
211
|
-
for (const { entry, relativePath } of drift) {
|
|
212
|
-
console.log(`DRIFT ${relativePath} (${entry.action})`);
|
|
213
|
-
}
|
|
214
|
-
for (const relativePath of mirrorOnly) {
|
|
215
|
-
console.log(`MIRROR-ONLY ${relativePath}`);
|
|
216
|
-
}
|
|
217
|
-
for (const relativePath of runtimeState) {
|
|
218
|
-
console.log(`RUNTIME-STATE ${relativePath} (protected; not drift)`);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const stateNote = runtimeState.length ? `, ${runtimeState.length} protected runtime-state file(s) ignored` : '';
|
|
222
|
-
if (drift.length === 0 && mirrorOnly.length === 0) {
|
|
223
|
-
console.log(`ukit mirror check: clean (${managedRelativePaths.size} scoped targets${stateNote})`);
|
|
224
|
-
return 0;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
console.log(
|
|
228
|
-
`ukit mirror check: ${drift.length} drifted, ${mirrorOnly.length} mirror-only `
|
|
229
|
-
+ `(${managedRelativePaths.size} scoped targets${stateNote})`,
|
|
230
|
-
);
|
|
231
|
-
return 1;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const { writes } = await applyDiffResults(
|
|
235
|
-
drift.map(({ entry }) => entry),
|
|
236
|
-
{ backupRoot: pathConfig.backupRoot, projectRoot },
|
|
237
|
-
);
|
|
238
|
-
for (const write of writes) {
|
|
239
|
-
console.log(`${write.action === 'create' ? 'CREATED' : 'UPDATED'} ${toRelative(projectRoot, write.targetPath)}`);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Re-running the managed-gateway step is what makes a rewritten `settings.json`
|
|
243
|
-
// whole again: the rendered template does not carry the two injected env keys, so
|
|
244
|
-
// serializing the template alone would drop them.
|
|
245
|
-
const gateway = await applyGatewayResilienceEnv({ projectRoot });
|
|
246
|
-
if (gateway.changed) {
|
|
247
|
-
console.log(`ukit mirror sync: re-applied managed gateway env keys (${[...gateway.applied, ...gateway.migrated].join(', ')})`);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
for (const relativePath of mirrorOnly) {
|
|
251
|
-
console.log(`MIRROR-ONLY ${relativePath} — not shipped by any template; left in place, remove it by hand if stale`);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
console.log(`ukit mirror sync: ${writes.length} written, ${mirrorOnly.length} mirror-only left in place`);
|
|
255
|
-
return 0;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
try {
|
|
259
|
-
process.exit(await main());
|
|
260
|
-
} catch (error) {
|
|
261
|
-
console.error(error && error.message ? error.message : error);
|
|
262
|
-
process.exit(1);
|
|
263
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { initSkillAudit, recordAuditEntry, statusSkillAudit } from '../../src/skill/auditSkill.js';
|
|
3
|
-
|
|
4
|
-
const USAGE = 'Usage: yarn skill:audit <init|status|record> <skill-name>';
|
|
5
|
-
|
|
6
|
-
async function main() {
|
|
7
|
-
const [command, skillName, ...extra] = process.argv.slice(2);
|
|
8
|
-
const rootDir = process.cwd();
|
|
9
|
-
|
|
10
|
-
if (!command || !skillName) {
|
|
11
|
-
console.error(USAGE);
|
|
12
|
-
process.exitCode = 1;
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
if (extra.length > 0) {
|
|
16
|
-
throw new Error(`unexpected argument: ${extra[0]}. ${USAGE}`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (command === 'init') {
|
|
20
|
-
const result = await initSkillAudit({ rootDir, skillName });
|
|
21
|
-
console.log(`[skill:audit init] ${skillName}`);
|
|
22
|
-
console.log(`dir: ${result.auditDir}`);
|
|
23
|
-
console.log(`copied: ${result.copied.join(', ') || 'none'}`);
|
|
24
|
-
console.log(`skipped (already present): ${result.skipped.join(', ') || 'none'}`);
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (command === 'status') {
|
|
29
|
-
const result = await statusSkillAudit({ rootDir, skillName });
|
|
30
|
-
console.log(`[skill:audit status] ${skillName}`);
|
|
31
|
-
console.log(`entries: ${result.entryCount}`);
|
|
32
|
-
console.log(`latest: ${result.latest ? JSON.stringify(result.latest) : 'none'}`);
|
|
33
|
-
console.log(`open rationalizations: ${result.openRationalizations.length}`);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (command === 'record') {
|
|
38
|
-
const stdinRaw = await readStdin();
|
|
39
|
-
let entry;
|
|
40
|
-
try {
|
|
41
|
-
entry = JSON.parse(stdinRaw);
|
|
42
|
-
} catch (error) {
|
|
43
|
-
throw new Error(`skill:audit record expects a JSON entry on stdin: ${error.message}`);
|
|
44
|
-
}
|
|
45
|
-
const record = await recordAuditEntry({ rootDir, skillName, entry });
|
|
46
|
-
console.log(`[skill:audit record] ${skillName}`);
|
|
47
|
-
console.log(JSON.stringify(record, null, 2));
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
console.error(`Unknown command: ${command}. ${USAGE}`);
|
|
52
|
-
process.exitCode = 1;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async function readStdin() {
|
|
56
|
-
const chunks = [];
|
|
57
|
-
for await (const chunk of process.stdin) {
|
|
58
|
-
chunks.push(chunk);
|
|
59
|
-
}
|
|
60
|
-
return Buffer.concat(chunks).toString('utf8');
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
await main();
|
|
65
|
-
} catch (error) {
|
|
66
|
-
console.error(error && error.message ? error.message : error);
|
|
67
|
-
process.exit(1);
|
|
68
|
-
}
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Runs the handoff test lane in a disposable copy of the project so `ukit install`
|
|
3
|
-
// (which writes .claude/, .ukit/, .cache/) can never overwrite the source checkout.
|
|
4
|
-
// The child's native exit code is propagated; the copy is removed in every case.
|
|
5
|
-
import { spawnSync } from 'node:child_process';
|
|
6
|
-
import { constants, cpSync, existsSync, lstatSync, mkdtempSync, readdirSync, readlinkSync, realpathSync, rmSync } from 'node:fs';
|
|
7
|
-
import { buildIsolatedEnv } from '../../tests/helpers/testHomeIsolation.mjs';
|
|
8
|
-
import os from 'node:os';
|
|
9
|
-
import path from 'node:path';
|
|
10
|
-
|
|
11
|
-
// Project root is the directory the runner was invoked from (package.json scripts run at the root).
|
|
12
|
-
const sourceRoot = path.resolve(process.cwd());
|
|
13
|
-
const SKIP_TOP_LEVEL = new Set(['.git', 'node_modules']);
|
|
14
|
-
// Live handoff run/backlog state and runtime caches must never leak into the fixture: a copied
|
|
15
|
-
// RUN.md or queue makes the handoff gates act on the real project's pending work.
|
|
16
|
-
const LIVE_STATE_PREFIXES = [
|
|
17
|
-
'docs/AI_HANDOFF/RUN.md',
|
|
18
|
-
'docs/AI_HANDOFF/INDEX.md',
|
|
19
|
-
'docs/AI_HANDOFF/ACTIVE.md',
|
|
20
|
-
'docs/AI_HANDOFF/HISTORY.md',
|
|
21
|
-
'docs/AI_HANDOFF/tasks',
|
|
22
|
-
'docs/AI_HANDOFF/queued',
|
|
23
|
-
'docs/AI_HANDOFF/archive',
|
|
24
|
-
'docs/AI_HANDOFF/ARCHIVE',
|
|
25
|
-
'docs/AI_HANDOFF/blocked-tests',
|
|
26
|
-
'docs/AI_HANDOFF/benchmark',
|
|
27
|
-
'.ukit/storage/cache',
|
|
28
|
-
'.ukit/storage/security',
|
|
29
|
-
'.ukit/storage/runs',
|
|
30
|
-
];
|
|
31
|
-
const unsafeSymlinks = [];
|
|
32
|
-
|
|
33
|
-
function shouldCopy(src) {
|
|
34
|
-
const rel = path.relative(sourceRoot, src);
|
|
35
|
-
if (rel === '') return true;
|
|
36
|
-
const top = rel.split(path.sep)[0];
|
|
37
|
-
if (SKIP_TOP_LEVEL.has(top)) return false;
|
|
38
|
-
// Nested worktrees would recursively copy the whole repo again.
|
|
39
|
-
if (rel.split(path.sep).slice(0, 2).join('/') === '.claude/worktrees') return false;
|
|
40
|
-
const posixRel = rel.split(path.sep).join('/');
|
|
41
|
-
// Case-insensitive: ARCHIVE/archive are the same directory on macOS/Windows.
|
|
42
|
-
const lower = posixRel.toLowerCase();
|
|
43
|
-
if (LIVE_STATE_PREFIXES.some((p) => lower === p.toLowerCase() || lower.startsWith(`${p.toLowerCase()}/`))) return false;
|
|
44
|
-
// A symlink must stay inside the tree; otherwise the copy would write through to the owner.
|
|
45
|
-
if (lstatSync(src).isSymbolicLink()) {
|
|
46
|
-
const target = readlinkSync(src);
|
|
47
|
-
const resolved = path.resolve(path.dirname(src), target);
|
|
48
|
-
const inside = path.relative(sourceRoot, resolved);
|
|
49
|
-
if (path.isAbsolute(target) || inside.startsWith('..') || path.isAbsolute(inside)) {
|
|
50
|
-
unsafeSymlinks.push(posixRel);
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (LIVE_STATE_PREFIXES.some((p) => posixRel === p || posixRel.startsWith(`${p}/`))) return false;
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let childEnv = process.env;
|
|
59
|
-
// Hard bound for every child (git, install, the node:test run) so a hung test cannot stall a release.
|
|
60
|
-
const CHILD_TIMEOUT_MS = 20 * 60 * 1000;
|
|
61
|
-
|
|
62
|
-
function run(cwd, command, args) {
|
|
63
|
-
const result = spawnSync(command, args, { cwd, stdio: 'inherit', env: childEnv, timeout: CHILD_TIMEOUT_MS });
|
|
64
|
-
if (result.error) {
|
|
65
|
-
console.error(`[handoff-isolated] failed to start ${command}: ${result.error.message}`);
|
|
66
|
-
return 1;
|
|
67
|
-
}
|
|
68
|
-
if (result.signal) {
|
|
69
|
-
console.error(`[handoff-isolated] ${command} killed by signal ${result.signal}`);
|
|
70
|
-
return 1;
|
|
71
|
-
}
|
|
72
|
-
return result.status ?? 1;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function main() {
|
|
76
|
-
const copyRoot = mkdtempSync(path.join(os.tmpdir(), 'ukit-handoff-isolated-'));
|
|
77
|
-
console.log(`[handoff-isolated] copy=${copyRoot}`);
|
|
78
|
-
// Throwaway HOME so install/tests never touch the caller's real user layer.
|
|
79
|
-
const homeRoot = mkdtempSync(path.join(os.tmpdir(), 'ukit-handoff-home-'));
|
|
80
|
-
childEnv = { ...process.env, ...buildIsolatedEnv(homeRoot) };
|
|
81
|
-
let exitCode = 1;
|
|
82
|
-
try {
|
|
83
|
-
cpSync(sourceRoot, copyRoot, { recursive: true, filter: shouldCopy, verbatimSymlinks: true });
|
|
84
|
-
if (unsafeSymlinks.length > 0) {
|
|
85
|
-
console.error(`[handoff-isolated] unsafe source symlink (resolves outside the project): ${unsafeSymlinks.join(', ')}`);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Several handoff tests call git (worktree add, ls-files, check-ignore). The copy has no
|
|
90
|
-
// .git, so give it an isolated throwaway repo with the copied tree committed.
|
|
91
|
-
for (const gitArgs of [
|
|
92
|
-
['init', '-q'],
|
|
93
|
-
['add', '-A'],
|
|
94
|
-
['-c', 'user.email=isolated@ukit.test', '-c', 'user.name=isolated', 'commit', '-q', '-m', 'isolated snapshot'],
|
|
95
|
-
]) {
|
|
96
|
-
const code = run(copyRoot, 'git', gitArgs);
|
|
97
|
-
if (code !== 0) {
|
|
98
|
-
console.error(`[handoff-isolated] git snapshot setup failed (git ${gitArgs.find((a) => !a.startsWith('-') && !a.includes('=')) ?? gitArgs[0]} exit ${code})`);
|
|
99
|
-
exitCode = code;
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Dependencies are cloned (copy-on-write where supported) AFTER the snapshot commit so tool
|
|
105
|
-
// caches written under node_modules can never write through to the source checkout.
|
|
106
|
-
// UKIT_HANDOFF_DEPS_ROOT lets a worktree without node_modules borrow an installed checkout's.
|
|
107
|
-
const depsRoot = path.resolve(process.env.UKIT_HANDOFF_DEPS_ROOT || sourceRoot);
|
|
108
|
-
const sourceModules = path.join(depsRoot, 'node_modules');
|
|
109
|
-
const copyModules = path.join(copyRoot, 'node_modules');
|
|
110
|
-
if (existsSync(sourceModules) && !existsSync(copyModules)) {
|
|
111
|
-
// Vet dependency symlinks too: one that resolves outside node_modules (for example a link
|
|
112
|
-
// back to the source checkout) would let the copy write through to the owner's tree.
|
|
113
|
-
const realModules = realpathSync(sourceModules);
|
|
114
|
-
cpSync(sourceModules, copyModules, {
|
|
115
|
-
recursive: true,
|
|
116
|
-
verbatimSymlinks: true,
|
|
117
|
-
mode: constants.COPYFILE_FICLONE,
|
|
118
|
-
filter: (src) => {
|
|
119
|
-
if (!lstatSync(src).isSymbolicLink()) return true;
|
|
120
|
-
const resolved = path.resolve(path.dirname(src), readlinkSync(src));
|
|
121
|
-
const rel = path.relative(realModules, resolved);
|
|
122
|
-
return !path.isAbsolute(readlinkSync(src)) && !rel.startsWith('..') && !path.isAbsolute(rel);
|
|
123
|
-
},
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const installCode = run(copyRoot, process.execPath, [path.join(copyRoot, 'bin', 'ukit'), 'install']);
|
|
128
|
-
if (installCode !== 0) {
|
|
129
|
-
console.error(`[handoff-isolated] install failed with exit ${installCode}`);
|
|
130
|
-
exitCode = installCode;
|
|
131
|
-
} else {
|
|
132
|
-
// Optional targeted mode: positional args are handoff test files relative to the project root.
|
|
133
|
-
const handoffRoot = path.join(copyRoot, 'tests', 'handoff');
|
|
134
|
-
const targeted = [];
|
|
135
|
-
for (const rel of process.argv.slice(2)) {
|
|
136
|
-
const full = path.resolve(copyRoot, rel);
|
|
137
|
-
const within = path.relative(handoffRoot, full);
|
|
138
|
-
if (within === '' || within.startsWith('..') || path.isAbsolute(within) || path.isAbsolute(rel)) {
|
|
139
|
-
console.error(`[handoff-isolated] invalid handoff test path: ${rel}`);
|
|
140
|
-
exitCode = 2;
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
targeted.push(full);
|
|
144
|
-
}
|
|
145
|
-
const testFiles = [];
|
|
146
|
-
const testDir = path.join(copyRoot, 'tests', 'handoff');
|
|
147
|
-
if (targeted.length > 0) testFiles.push(...targeted);
|
|
148
|
-
else if (existsSync(testDir)) collectTests(testDir, testFiles);
|
|
149
|
-
if (testFiles.length === 0) {
|
|
150
|
-
console.error('[handoff-isolated] no handoff test files found');
|
|
151
|
-
exitCode = 1;
|
|
152
|
-
} else {
|
|
153
|
-
exitCode = run(copyRoot, process.execPath, ['--test', '--test-concurrency=1', ...testFiles]);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
} finally {
|
|
157
|
-
// Diagnostic escape hatch: keep the copy for inspection when explicitly requested.
|
|
158
|
-
if (process.env.UKIT_HANDOFF_KEEP_COPY === '1') {
|
|
159
|
-
console.log(`[handoff-isolated] keeping copy for inspection: ${copyRoot}`);
|
|
160
|
-
} else try {
|
|
161
|
-
rmSync(copyRoot, { recursive: true, force: true });
|
|
162
|
-
} catch (error) {
|
|
163
|
-
console.error(`[handoff-isolated] cleanup failed for ${copyRoot}: ${error.message}`);
|
|
164
|
-
}
|
|
165
|
-
try { rmSync(homeRoot, { recursive: true, force: true }); } catch {}
|
|
166
|
-
process.exitCode = exitCode;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function collectTests(dir, out) {
|
|
171
|
-
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
172
|
-
const full = path.join(dir, entry.name);
|
|
173
|
-
if (entry.isDirectory()) collectTests(full, out);
|
|
174
|
-
else if (/\.test\.(mjs|js)$/.test(entry.name)) out.push(full);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
main();
|