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

34 lines
3.0 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
const read = file => JSON.parse(fs.readFileSync(file, 'utf8'));
const install = read(read('.qa/cache-f-install-location.json').result);
assert.equal(install.success, true);
const references = install.cases.flatMap(item => ['processLog', 'setupLog', 'uninstallLog'].filter(key => item[key]).map(key => ({ key, path: item[key], sha256: item[key + 'Sha256'] })));
const formal = read(read('.qa/cache-f-formal-baseline-location.json').staged); assert.equal(formal.success, true);
references.push({ key: 'setupLog', path: formal.setupLog, sha256: formal.setupLogSha256 });
const directory = '.qa/cache-f-log-completions'; assert.equal(fs.existsSync(directory), false);
const checked = [], completions = [];
for (const item of references) {
const bytes = fs.readFileSync(item.path), current = hash(bytes);
if (current === item.sha256) { checked.push({ ...item, bytes: bytes.length, exact: true }); continue; }
assert.equal(item.key, 'uninstallLog', 'Only the declared uninstall completion trailer may append.');
const prefix = bytes.subarray(0, bytes.length - 199), tail = bytes.subarray(bytes.length - 199);
assert.equal(hash(prefix), item.sha256, 'The original receipt must match the exact prefix.');
const lines = tail.toString('utf8').split('\r\n'); assert.equal(lines.pop(), '');
assert.deepEqual(lines.map(line => { assert.match(line, /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} /); return line.slice(26); }),
['Uninstallation process succeeded.', 'Removed all? Yes', 'Need to restart Windows? No', 'Log closed.']);
const archived = path.join(directory, String(completions.length + 1) + '-original-receipt-prefix-log.txt');
completions.push({ source: path.resolve(item.path), original: { path: archived, bytes: prefix.length, sha256: item.sha256 },
completed: { path: path.resolve(item.path), bytes: bytes.length, sha256: current }, appendedBytes: 199, appendedText: tail.toString('utf8'), prefix });
checked.push({ ...item, bytes: bytes.length, exact: false, exactOriginalPrefix: true });
}
fs.mkdirSync(directory);
for (const item of completions) { fs.writeFileSync(item.original.path, item.prefix, { flag: 'wx' }); delete item.prefix; }
const result = { success: true, at: new Date().toISOString(), checked, completions,
scope: 'All declared installer/process logs match their original SHA, or the uninstall receipt matches the exact original prefix followed solely by the four fixed 199-byte success/removed-all/no-restart/closed lines. Preserve both original prefix and final log; never rewrite the original receipt.' };
fs.writeFileSync(path.join(directory, 'index.json'), JSON.stringify(result, null, 2) + '\n', { flag: 'wx' });
console.log(JSON.stringify({ success: true, references: checked.length, exact: checked.filter(item => item.exact).length, completions: completions.length }));