@ngockhoale/ukit 3.5.11 → 3.5.14
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 +62 -0
- package/manifests/documentation.yaml +11 -0
- package/manifests/platform.full.yaml +39 -2
- package/package.json +1 -1
- package/scripts/audit/field-replay.mjs +194 -0
- package/scripts/audit/field-telemetry.mjs +341 -0
- package/scripts/release/candidate-state.mjs +4 -1
- package/template_project/.claude/agents/code-reviewer.md +1 -0
- package/template_project/.claude/agents/feature-implementer.md +1 -0
- package/template_project/.claude/hooks/completion-gate.sh +3 -3
- package/template_project/.claude/hooks/context-hardcap-gate.sh +40 -230
- package/template_project/.claude/hooks/context-window-guard.sh +1 -1
- package/template_project/.claude/hooks/sensitive-data-guard.mjs +297 -13
- package/template_project/.claude/settings.json +2 -2
- package/template_project/.claude/skills/code-review/SKILL.md +14 -0
- package/template_project/.claude/skills/testing-quality/SKILL.md +31 -1
- package/template_project/.claude/skills/testing-quality/memory-safe-tests.md +133 -0
- package/template_project/.claude/ukit/index/post-edit-verify.mjs +3 -7
- package/template_project/.claude/ukit/index/pre-edit-backup.mjs +1 -0
- package/template_project/.claude/ukit/index/test-batches-adapters.mjs +261 -0
- package/template_project/.claude/ukit/index/test-batches.mjs +829 -0
- package/template_project/.claude/ukit/runtime/compact-threshold.mjs +3 -3
- package/template_project/.claude/ukit/runtime/execution-ledger.mjs +92 -3
- package/template_project/.claude/ukit/runtime/output-compression.mjs +28 -1
- package/template_project/.claude/ukit/runtime/safe-patch-core.mjs +22 -0
- package/template_project/.claude/ukit/runtime/stop-coordinator.mjs +40 -4
- package/template_project/.omp/agents/code-reviewer.md +1 -0
- package/template_project/.omp/agents/feature-implementer.md +1 -0
- package/template_project/.omp/config.yml +1 -1
- package/template_project/AGENTS.md +1 -1
- package/template_project/CLAUDE.md +1 -1
- package/template_project/docs/AI_HANDOFF/RULES.md +1 -1
- package/template_project/docs/memory/testing.md +10 -0
- package/template_project/instructions/core.md +1 -1
- package/template_project/ukit/storage/config.json +3 -3
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// Framework adapters for test-batches.mjs. node: builtins only.
|
|
2
|
+
//
|
|
3
|
+
// Adapter contract (implement this for any other framework — see
|
|
4
|
+
// .claude/skills/testing-quality/memory-safe-tests.md):
|
|
5
|
+
// discover(ctx) → { ok, version, files: [relPosix], tests: [{ id, file }] | null, error }
|
|
6
|
+
// tests === null means the framework cannot list case ids (file-level batching).
|
|
7
|
+
// batchCommand(ctx, files, report) → { cmd, args, env } one fresh process running exactly `files`
|
|
8
|
+
// parseReport(ctx, files, report) → { ok, results: [{ id, file, outcome }], fileErrors: [{ file, message }], error }
|
|
9
|
+
// outcome ∈ passed | failed | error | skipped | xfailed | xpassed
|
|
10
|
+
// leakCommand(ctx, file, samples) → { cmd, args, env } optional: in-process sampling for detect-leaks
|
|
11
|
+
// Ids returned by discover and parseReport for the same test MUST be equal strings.
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { spawnSync } from 'node:child_process';
|
|
15
|
+
|
|
16
|
+
const toPosix = (p) => String(p).split(path.sep).join('/').replace(/\\/g, '/');
|
|
17
|
+
const rel = (root, p) => toPosix(path.isAbsolute(p) ? path.relative(root, p) : p);
|
|
18
|
+
const readText = (f) => { try { return fs.readFileSync(f, 'utf8'); } catch { return null; } };
|
|
19
|
+
const tail = (s, n = 1500) => String(s ?? '').slice(-n);
|
|
20
|
+
|
|
21
|
+
function run(cmd, args, { cwd, env, timeoutMs = 10 * 60 * 1000 } = {}) {
|
|
22
|
+
const res = spawnSync(cmd, args, { cwd, env: { ...process.env, ...(env ?? {}) }, encoding: 'utf8', timeout: timeoutMs, maxBuffer: 256 * 1024 * 1024, windowsHide: true });
|
|
23
|
+
return { code: res.status, signal: res.signal, stdout: res.stdout ?? '', stderr: res.stderr ?? '', error: res.error };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// ------------------------------------------------------------------ pytest
|
|
27
|
+
|
|
28
|
+
// Written into the run directory and loaded with `-p ukit_batch_report`. Records one JSON line per test
|
|
29
|
+
// (final outcome after teardown) plus an RSS sample, and a session-end line listing live threads/children.
|
|
30
|
+
// ru_maxrss units: bytes on macOS, KiB on Linux; the resource module does not exist on Windows (→ null).
|
|
31
|
+
export const PYTEST_PLUGIN = String.raw`import json, os, sys, threading
|
|
32
|
+
try:
|
|
33
|
+
import resource
|
|
34
|
+
except ImportError:
|
|
35
|
+
resource = None
|
|
36
|
+
|
|
37
|
+
_OUT = os.environ.get("UKIT_BATCH_REPORT")
|
|
38
|
+
_state = {}
|
|
39
|
+
_RANK = {"passed": 0, "skipped": 1, "xfailed": 1, "xpassed": 2, "failed": 3, "error": 4}
|
|
40
|
+
|
|
41
|
+
def _rss():
|
|
42
|
+
peak = cur = None
|
|
43
|
+
if resource is not None:
|
|
44
|
+
r = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
|
|
45
|
+
peak = r if sys.platform == "darwin" else r * 1024
|
|
46
|
+
try:
|
|
47
|
+
with open("/proc/self/statm") as f:
|
|
48
|
+
cur = int(f.read().split()[1]) * os.sysconf("SC_PAGE_SIZE")
|
|
49
|
+
except Exception:
|
|
50
|
+
pass
|
|
51
|
+
return peak, cur
|
|
52
|
+
|
|
53
|
+
def _write(obj):
|
|
54
|
+
if not _OUT:
|
|
55
|
+
return
|
|
56
|
+
with open(_OUT, "a", encoding="utf-8") as f:
|
|
57
|
+
f.write(json.dumps(obj) + "\n")
|
|
58
|
+
|
|
59
|
+
def _outcome(rep):
|
|
60
|
+
xf = hasattr(rep, "wasxfail")
|
|
61
|
+
if rep.when == "call":
|
|
62
|
+
if rep.passed:
|
|
63
|
+
return "xpassed" if xf else "passed"
|
|
64
|
+
if rep.skipped:
|
|
65
|
+
return "xfailed" if xf else "skipped"
|
|
66
|
+
return "failed"
|
|
67
|
+
if rep.failed:
|
|
68
|
+
return "error"
|
|
69
|
+
if rep.skipped:
|
|
70
|
+
return "xfailed" if xf else "skipped"
|
|
71
|
+
return None
|
|
72
|
+
|
|
73
|
+
def pytest_runtest_logreport(report):
|
|
74
|
+
o = _outcome(report)
|
|
75
|
+
if o is not None:
|
|
76
|
+
prev = _state.get(report.nodeid)
|
|
77
|
+
if prev is None or _RANK[o] > _RANK[prev]:
|
|
78
|
+
_state[report.nodeid] = o
|
|
79
|
+
if report.when == "teardown":
|
|
80
|
+
peak, cur = _rss()
|
|
81
|
+
_write({"id": report.nodeid, "outcome": _state.pop(report.nodeid, "passed"), "peakRss": peak, "rss": cur})
|
|
82
|
+
|
|
83
|
+
def pytest_sessionfinish(session, exitstatus):
|
|
84
|
+
names = [t.name for t in threading.enumerate() if t is not threading.main_thread() and t.is_alive()]
|
|
85
|
+
kids = []
|
|
86
|
+
try:
|
|
87
|
+
import multiprocessing
|
|
88
|
+
kids = [p.pid for p in multiprocessing.active_children()]
|
|
89
|
+
except Exception:
|
|
90
|
+
pass
|
|
91
|
+
peak, cur = _rss()
|
|
92
|
+
_write({"session": True, "exitstatus": int(exitstatus), "liveThreads": names, "liveChildren": kids, "peakRss": peak, "rss": cur})
|
|
93
|
+
`;
|
|
94
|
+
|
|
95
|
+
export function pythonCmd(ctx) {
|
|
96
|
+
if (ctx.python) return ctx.python;
|
|
97
|
+
for (const c of [path.join(ctx.root, '.venv', 'bin', 'python'), path.join(ctx.root, 'venv', 'bin', 'python'), path.join(ctx.root, '.venv', 'Scripts', 'python.exe')]) {
|
|
98
|
+
if (fs.existsSync(c)) return c;
|
|
99
|
+
}
|
|
100
|
+
return process.platform === 'win32' ? 'python' : 'python3';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// `--collect-only -q` prints one node id per line, then a blank line and a summary.
|
|
104
|
+
export function parsePytestCollect(stdout) {
|
|
105
|
+
const ids = [];
|
|
106
|
+
for (const raw of String(stdout).split(/\r?\n/)) {
|
|
107
|
+
const line = raw.trim();
|
|
108
|
+
if (!line) break;
|
|
109
|
+
if (line.includes('::')) ids.push(line);
|
|
110
|
+
}
|
|
111
|
+
return ids;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const pytest = {
|
|
115
|
+
name: 'pytest',
|
|
116
|
+
discover(ctx) {
|
|
117
|
+
const py = pythonCmd(ctx);
|
|
118
|
+
const ver = run(py, ['-m', 'pytest', '--version'], { cwd: ctx.root });
|
|
119
|
+
const version = (`${ver.stdout}${ver.stderr}`.match(/pytest\s+([\d.]+)/) ?? [])[1] ?? null;
|
|
120
|
+
const res = run(py, ['-m', 'pytest', '--collect-only', '-q', '-p', 'no:cacheprovider', ...ctx.extra], { cwd: ctx.root });
|
|
121
|
+
if (res.error) return { ok: false, error: `pytest discovery could not start: ${res.error.message}` };
|
|
122
|
+
if (res.code === 5) return { ok: true, version, files: [], tests: [] };
|
|
123
|
+
if (res.code !== 0) return { ok: false, version, error: `pytest collection failed (exit ${res.code}) — collection errors must be fixed before a full run:\n${tail(res.stdout + res.stderr)}` };
|
|
124
|
+
const tests = parsePytestCollect(res.stdout).map((id) => ({ id, file: id.split('::')[0] }));
|
|
125
|
+
return { ok: true, version, files: [...new Set(tests.map((t) => t.file))], tests };
|
|
126
|
+
},
|
|
127
|
+
batchCommand(ctx, files, report) {
|
|
128
|
+
const args = ['-m', 'pytest', '-p', 'no:cacheprovider', '-p', 'ukit_batch_report', '-q', '-rN'];
|
|
129
|
+
if (ctx.resourceWarnings) args.push('-W', 'error::ResourceWarning');
|
|
130
|
+
args.push(...ctx.extra, ...files);
|
|
131
|
+
const pp = [ctx.pluginDir, process.env.PYTHONPATH].filter(Boolean).join(path.delimiter);
|
|
132
|
+
return { cmd: pythonCmd(ctx), args, env: { UKIT_BATCH_REPORT: report, PYTHONPATH: pp, PYTHONDONTWRITEBYTECODE: '1' } };
|
|
133
|
+
},
|
|
134
|
+
parseReport(ctx, files, report) {
|
|
135
|
+
const text = readText(report);
|
|
136
|
+
if (text == null) return { ok: false, results: [], fileErrors: [], error: 'no result file written (process died before the first test finished?)' };
|
|
137
|
+
const results = [];
|
|
138
|
+
let session = null;
|
|
139
|
+
for (const line of text.split('\n')) {
|
|
140
|
+
if (!line.trim()) continue;
|
|
141
|
+
let row; try { row = JSON.parse(line); } catch { continue; }
|
|
142
|
+
if (row.session) { session = row; continue; }
|
|
143
|
+
results.push({ id: row.id, file: row.id.split('::')[0], outcome: row.outcome, rss: row.rss, peakRss: row.peakRss });
|
|
144
|
+
}
|
|
145
|
+
return { ok: true, results, fileErrors: [], session };
|
|
146
|
+
},
|
|
147
|
+
leakCommand(ctx, file, samples) {
|
|
148
|
+
// Same file twice in ONE process: pass 2 growth after pass-1 warm-up is the in-process retention signal.
|
|
149
|
+
const cmd = this.batchCommand(ctx, [file, file], samples);
|
|
150
|
+
cmd.args.splice(cmd.args.indexOf('-q'), 0, '--keep-duplicates');
|
|
151
|
+
return cmd;
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// ------------------------------------------------------------------ vitest / jest (shared JSON report shape)
|
|
156
|
+
|
|
157
|
+
// Explains the common "framework not installed locally" failure instead of a bare npx error.
|
|
158
|
+
function missingHint(ctx, name) {
|
|
159
|
+
return fs.existsSync(path.join(ctx.root, 'node_modules', name)) ? '' : ` — ${name} is not installed locally (no node_modules/${name}); install project dependencies first`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function nodeBin(ctx, name, entry) {
|
|
163
|
+
const local = path.join(ctx.root, 'node_modules', name, entry);
|
|
164
|
+
return fs.existsSync(local) ? { cmd: process.execPath, pre: [local] } : { cmd: process.platform === 'win32' ? 'npx.cmd' : 'npx', pre: ['--no-install', name] };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Jest-compatible JSON (vitest --reporter=json mirrors it): testResults[].name = file, assertionResults[].
|
|
168
|
+
export function parseJestLikeReport(root, files, json) {
|
|
169
|
+
const results = [];
|
|
170
|
+
const fileErrors = [];
|
|
171
|
+
const map = { passed: 'passed', failed: 'failed', pending: 'skipped', skipped: 'skipped', todo: 'skipped', disabled: 'skipped' };
|
|
172
|
+
const seenFiles = new Set();
|
|
173
|
+
for (const suite of json?.testResults ?? []) {
|
|
174
|
+
const file = rel(root, suite.name);
|
|
175
|
+
seenFiles.add(file);
|
|
176
|
+
const asserts = suite.assertionResults ?? [];
|
|
177
|
+
for (const a of asserts) {
|
|
178
|
+
const id = `${file} > ${[...(a.ancestorTitles ?? []), a.title].join(' > ')}`;
|
|
179
|
+
results.push({ id, file, outcome: map[a.status] ?? 'error' });
|
|
180
|
+
}
|
|
181
|
+
if (suite.status === 'failed' && suite.message && !asserts.some((a) => a.status === 'failed')) {
|
|
182
|
+
fileErrors.push({ file, message: tail(suite.message, 600) });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
for (const f of files) if (!seenFiles.has(f)) fileErrors.push({ file: f, message: 'file missing from the framework report' });
|
|
186
|
+
return { results, fileErrors };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const vitest = {
|
|
190
|
+
name: 'vitest',
|
|
191
|
+
discover(ctx) {
|
|
192
|
+
const bin = nodeBin(ctx, 'vitest', 'vitest.mjs');
|
|
193
|
+
const ver = run(bin.cmd, [...bin.pre, '--version'], { cwd: ctx.root });
|
|
194
|
+
const version = (`${ver.stdout}`.match(/(\d+\.\d+\.\d+)/) ?? [])[1] ?? null;
|
|
195
|
+
const filesOut = path.join(ctx.tmpDir, 'vitest-files.json');
|
|
196
|
+
const fr = run(bin.cmd, [...bin.pre, 'list', '--filesOnly', `--json=${filesOut}`, ...ctx.extra], { cwd: ctx.root });
|
|
197
|
+
let fileList = null;
|
|
198
|
+
try { fileList = JSON.parse(fs.readFileSync(filesOut, 'utf8')); } catch { /* handled below */ }
|
|
199
|
+
if (!Array.isArray(fileList)) return { ok: false, version, error: `vitest list --filesOnly failed (exit ${fr.code})${missingHint(ctx, 'vitest')}:\n${tail(fr.stdout + fr.stderr)}` };
|
|
200
|
+
const files = [...new Set(fileList.map((r) => rel(ctx.root, r.file)))].sort();
|
|
201
|
+
// Case ids: `vitest list` collects (imports) files; skipped/todo cases are not listed — they are reported, not lost.
|
|
202
|
+
const listOut = path.join(ctx.tmpDir, 'vitest-list.json');
|
|
203
|
+
run(bin.cmd, [...bin.pre, 'list', `--json=${listOut}`, ...ctx.extra], { cwd: ctx.root });
|
|
204
|
+
let list = null;
|
|
205
|
+
try { list = JSON.parse(fs.readFileSync(listOut, 'utf8')); } catch { list = null; }
|
|
206
|
+
const tests = Array.isArray(list) ? list.map((r) => { const file = rel(ctx.root, r.file); return { id: `${file} > ${r.name}`, file }; }) : null;
|
|
207
|
+
return { ok: true, version, files, tests };
|
|
208
|
+
},
|
|
209
|
+
batchCommand(ctx, files, report) {
|
|
210
|
+
const bin = nodeBin(ctx, 'vitest', 'vitest.mjs');
|
|
211
|
+
return { cmd: bin.cmd, args: [...bin.pre, 'run', ...files, '--reporter=json', `--outputFile=${report}`, '--no-file-parallelism', '--passWithNoTests', ...ctx.extra], env: {} };
|
|
212
|
+
},
|
|
213
|
+
parseReport(ctx, files, report) {
|
|
214
|
+
const text = readText(report);
|
|
215
|
+
if (text == null) return { ok: false, results: [], fileErrors: [], error: 'no JSON report written' };
|
|
216
|
+
let json; try { json = JSON.parse(text); } catch (e) { return { ok: false, results: [], fileErrors: [], error: `unreadable JSON report: ${e.message}` }; }
|
|
217
|
+
return { ok: true, ...parseJestLikeReport(ctx.root, files, json) };
|
|
218
|
+
},
|
|
219
|
+
leakCommand(ctx, file, samples) {
|
|
220
|
+
const cmd = this.batchCommand(ctx, [file], path.join(ctx.tmpDir, 'leak-report.json'));
|
|
221
|
+
return { ...cmd, env: { NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ''} --require ${JSON.stringify(ctx.samplerFile)}`.trim(), UKIT_LEAK_SAMPLES: samples } };
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const jest = {
|
|
226
|
+
name: 'jest',
|
|
227
|
+
discover(ctx) {
|
|
228
|
+
const bin = nodeBin(ctx, 'jest', path.join('bin', 'jest.js'));
|
|
229
|
+
const ver = run(bin.cmd, [...bin.pre, '--version'], { cwd: ctx.root });
|
|
230
|
+
const version = (`${ver.stdout}`.match(/(\d+\.\d+\.\d+)/) ?? [])[1] ?? null;
|
|
231
|
+
const res = run(bin.cmd, [...bin.pre, '--listTests', '--json', ...ctx.extra], { cwd: ctx.root });
|
|
232
|
+
let list = null;
|
|
233
|
+
try { list = JSON.parse(res.stdout.trim().split('\n').filter((l) => l.startsWith('[')).pop() ?? ''); } catch { list = null; }
|
|
234
|
+
if (!Array.isArray(list)) return { ok: false, version, error: `jest --listTests failed (exit ${res.code})${missingHint(ctx, 'jest')}:\n${tail(res.stdout + res.stderr)}` };
|
|
235
|
+
// Jest cannot list case ids without running files → file-level inventory (tests: null).
|
|
236
|
+
return { ok: true, version, files: [...new Set(list.map((f) => rel(ctx.root, f)))].sort(), tests: null };
|
|
237
|
+
},
|
|
238
|
+
batchCommand(ctx, files, report) {
|
|
239
|
+
const bin = nodeBin(ctx, 'jest', path.join('bin', 'jest.js'));
|
|
240
|
+
return { cmd: bin.cmd, args: [...bin.pre, '--runInBand', '--json', `--outputFile=${report}`, '--passWithNoTests', ...ctx.extra, '--runTestsByPath', ...files], env: {} };
|
|
241
|
+
},
|
|
242
|
+
parseReport: vitest.parseReport,
|
|
243
|
+
leakCommand(ctx, file, samples) {
|
|
244
|
+
const cmd = this.batchCommand(ctx, [file], path.join(ctx.tmpDir, 'leak-report.json'));
|
|
245
|
+
return { ...cmd, env: { NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ''} --require ${JSON.stringify(ctx.samplerFile)}`.trim(), UKIT_LEAK_SAMPLES: samples } };
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
// In-process sampler for JS detect-leaks: every node process in the tree appends {pid,t,rss,heap} every 250 ms.
|
|
250
|
+
export const NODE_SAMPLER = String.raw`const fs = require('fs');
|
|
251
|
+
const out = process.env.UKIT_LEAK_SAMPLES;
|
|
252
|
+
if (out) {
|
|
253
|
+
const t0 = Date.now();
|
|
254
|
+
const tick = () => { try { const m = process.memoryUsage(); fs.appendFileSync(out, JSON.stringify({ pid: process.pid, t: Date.now() - t0, rss: m.rss, heap: m.heapUsed }) + '\n'); } catch {} };
|
|
255
|
+
tick();
|
|
256
|
+
setInterval(tick, 250).unref();
|
|
257
|
+
process.on('exit', tick);
|
|
258
|
+
}
|
|
259
|
+
`;
|
|
260
|
+
|
|
261
|
+
export const ADAPTERS = Object.freeze({ pytest, vitest, jest });
|