Files
Agent-Workbench/docs/project-cache-f-release-evidence/0031-audit-cache-checkpoint.mjs
T
2026-11-01 09:31:00 +08:00

145 lines
7.4 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
const root = process.cwd();
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
const read = file => JSON.parse(fs.readFileSync(file, 'utf8'));
const git = args => execFileSync('git', args, { windowsHide: true, maxBuffer: 64 * 1024 * 1024 });
const mode = process.argv[2] ?? 'working';
assert.ok(['working', 'staged', 'committed'].includes(mode));
const archive = 'docs/project-cache-f-interim-evidence';
const manifest = read(archive + '/manifest.json');
const identity = read('docs/project-cache-pending-native-identity.json');
const baseline = read('docs/project-cache-release-code-baseline.json');
assert.equal(manifest.success, true);
assert.equal(manifest.checkpointOnly, true);
assert.equal(identity.success, false);
assert.equal(identity.status, 'pending_native_review');
assert.equal(identity.formal.native.userStoppedComputerUse, true);
assert.equal(identity.formal.native.success, false);
assert.equal(identity.codeCommit, baseline.codeCommit);
assert.equal(manifest.codeCommit, baseline.codeCommit);
assert.equal(manifest.files.length, 867);
assert.equal(identity.evidence.files, manifest.files.length);
assert.equal(identity.independentSource.tests.pass, 590);
assert.equal(identity.independentSource.tests.fail, 0);
assert.equal(identity.ui.classes, 27);
assert.equal(identity.ui.groups, 203);
assert.equal(identity.ui.formalPanels, 24);
assert.equal(identity.formal.artifactRecords, 1466);
assert.equal(identity.formal.blobFiles, 1376);
assert.equal(identity.realModelRequests, 0);
const seen = new Set(), secretFindings = [];
let total = 0;
for (const row of manifest.files) {
const target = row.archived.replaceAll('\\', '/');
assert.ok(target.startsWith(archive + '/'));
assert.equal(path.dirname(path.resolve(target)), path.resolve(archive));
assert.equal(seen.has(target), false, 'Duplicate archived path');
seen.add(target);
assert.doesNotMatch(row.original, /(?:\.sqlite(?:-(?:wal|shm))?|\.(?:db|key|pem|pfx|p12))$/i);
const stat = fs.lstatSync(target);
assert.equal(stat.isSymbolicLink(), false);
assert.equal(stat.isFile(), true);
const bytes = fs.readFileSync(target);
assert.equal(bytes.length, row.bytes, target);
assert.equal(sha(bytes), row.sha256, target);
total += bytes.length;
if (!/\.(?:png|jpg|jpeg)$/i.test(target) && !bytes.includes(0)) {
const text = bytes.toString('utf8');
for (const [kind, pattern] of [
['provider_key', /\bsk-(?:proj-|ant-|svcacct-)?[A-Za-z0-9_-]{25,}/g],
['private_key', /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/g],
['aws_key', /\bAKIA[A-Z0-9]{16}\b/g],
]) {
const matches = text.match(pattern);
if (matches) secretFindings.push({ file: target, kind, count: matches.length });
}
}
}
assert.equal(total, manifest.bytes);
assert.equal(total, identity.evidence.bytes);
const archiveFiles = fs.readdirSync(archive).sort();
assert.deepEqual(archiveFiles, [...seen].map(file => path.basename(file)).concat(['.gitattributes', 'manifest.json']).sort());
assert.equal(fs.readFileSync(archive + '/.gitattributes', 'utf8'), '* -text -whitespace\n');
assert.deepEqual(secretFindings, [], 'Review file/kind only; do not print credential matches');
for (const row of [...baseline.inputs, ...baseline.builds, ...baseline.benchmarks]) {
const bytes = fs.readFileSync(row.path);
assert.equal(bytes.length, row.bytes, row.path);
assert.equal(sha(bytes), row.sha256, row.path);
}
const fixed = [
'AGENTS.md', 'CHANGELOG.md', 'README.md', 'task_plan.md', 'findings.md', 'progress.md',
'docs/CAPABILITIES.md', 'docs/MILESTONE-AUDIT.md', 'docs/NEXT-STEPS.md',
'docs/PLAN-1.19.md', 'docs/PLAN-1.20.md', 'docs/PRODUCT-PLAN.md',
'docs/QA-1.19.md', 'docs/RETROSPECTIVE-1.19.md', 'docs/RESUME-1.19.md', 'docs/VALIDATION.md',
];
const baselineFiles = new Set(git(['ls-tree', '-r', '--name-only', '-z', baseline.codeCommit]).toString('utf8').split('\0').filter(Boolean));
const reports = fs.readdirSync('docs').filter(name => /^project-cache-.*\.json$/.test(name))
.map(name => 'docs/' + name)
.filter(file => !baselineFiles.has(file));
const allowed = [...new Set([...fixed, ...reports, ...seen, archive + '/.gitattributes', archive + '/manifest.json'])].sort();
const allowedSet = new Set(allowed);
let changed;
if (mode === 'committed') {
assert.equal(git(['status', '--porcelain']).toString('utf8').trim(), '');
changed = git(['diff-tree', '--no-commit-id', '--name-only', '-r', '-z', 'HEAD']).toString('utf8').split('\0').filter(Boolean);
} else {
changed = git(['status', '--porcelain=v1', '--untracked-files=all', '-z']).toString('utf8').split('\0').filter(Boolean)
.map(row => row.slice(3));
}
assert.ok(changed.length > 800);
for (const file of changed) assert.equal(allowedSet.has(file), true, 'Out-of-scope path: ' + file);
for (const file of [...seen, archive + '/.gitattributes', archive + '/manifest.json', ...reports]) {
assert.ok(changed.includes(file), 'Expected checkpoint file: ' + file);
}
const changedMarkdown = changed.filter(file => file.endsWith('.md'));
let links = 0;
for (const file of changedMarkdown) {
const text = fs.readFileSync(file, 'utf8');
for (const match of text.matchAll(/\[[^\]\r\n]*\]\(([^)\r\n]+)\)/g)) {
let target = match[1].replace(/^<|>$/g, '').split(/[#?]/)[0];
if (!target || /^[a-z][a-z0-9+.-]*:/i.test(target)) continue;
target = decodeURIComponent(target);
assert.equal(fs.existsSync(path.resolve(path.dirname(file), target)), true, file + ' local link: ' + target);
links++;
}
}
let exact = 0, normalized = 0;
if (mode !== 'working') {
const ref = mode === 'staged' ? '' : 'HEAD';
if (mode === 'staged') {
const staged = git(['diff', '--cached', '--name-only', '-z']).toString('utf8').split('\0').filter(Boolean).sort();
assert.deepEqual(staged, changed.slice().sort());
}
for (const file of changed) {
const original = fs.readFileSync(file);
const blob = git(['show', ref + ':' + file]);
if (blob.equals(original)) exact++;
else {
assert.equal(file.startsWith(archive + '/'), false, 'Archive requires original bytes');
assert.ok(blob.equals(Buffer.from(original.toString('utf8').replaceAll('\r\n', '\n'))), 'Unexpected Git transform: ' + file);
normalized++;
}
}
}
const pathspec = '.qa/cache-f-checkpoint-pathspec.nul';
if (mode === 'working') fs.writeFileSync(pathspec, Buffer.from(changed.join('\0') + '\0'));
const result = {
success: true, checkpointOnly: true, status: 'pending_native_review', at: new Date().toISOString(),
mode, branch: git(['branch', '--show-current']).toString('utf8').trim(),
head: git(['rev-parse', 'HEAD']).toString('utf8').trim(), codeCommit: baseline.codeCommit,
archivedFiles: seen.size, archivedBytes: total, changedFiles: changed.length,
frozenInputs: baseline.inputs.length, frozenBuilds: baseline.builds.length, frozenBenchmarks: baseline.benchmarks.length,
markdownFiles: changedMarkdown.length, localLinks: links, credentialPatternFindings: secretFindings.length,
exactGitFiles: exact, normalizedGitFiles: normalized, pathspec,
note: 'Checkpoint only; native review, complete F seal and final ACCEPTANCE remain pending. No installer, upgrade, activation, UI or real-model action was replayed.',
};
fs.writeFileSync('.qa/cache-f-checkpoint-' + mode + '-audit.json', JSON.stringify(result, null, 2) + '\n');
console.log(JSON.stringify(result));