import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { DatabaseSync } from 'node:sqlite'; import { canonical, databaseSnapshot, digest } from './desktop-f-state.mjs'; import { snapshotPersistentData } from '../src/core/desktop/install-manager.mjs'; const deployed = JSON.parse(fs.readFileSync(process.argv[2] ?? '.qa/desktop-host-f-formal-start.json')); assert.equal(deployed.success, true); const output = process.argv[3] ?? '.qa/desktop-host-f-formal-artifacts.json'; const before = JSON.parse(fs.readFileSync(JSON.parse(fs.readFileSync('.qa/task-control-j-formal-baseline-location.json')).before)); const database = new DatabaseSync(path.join(deployed.dataRoot, 'workbench.sqlite'), { readOnly: true }); let originalRecords; try { originalRecords = database.prepare("SELECT * FROM records WHERE kind='artifact' ORDER BY rowid").all(); } finally { database.close(); } const original = new Map(), producers = [], referenced = new Map(); for (const row of originalRecords) { const artifact = JSON.parse(row.payload); assert.equal(artifact.id, row.id); assert.equal(original.has(row.id), false); for (const [column, field] of [['session_id', 'sessionId'], ['project_id', 'projectId'], ['task_id', 'taskId'], ['run_id', 'runId']]) { assert.equal(row[column] ?? null, artifact[field] ?? null, artifact.id + ': ' + field); } assert.match(artifact.sha256, /^[a-f0-9]{64}$/); assert.equal(artifact.relative, artifact.sha256.slice(0, 2) + '/' + artifact.sha256 + '.blob'); const blob = { path: artifact.relative, bytes: artifact.bytes, sha256: artifact.sha256 }; if (referenced.has(blob.path)) assert.deepEqual(referenced.get(blob.path), blob); referenced.set(blob.path, blob); original.set(row.id, artifact); producers.push({ id: row.id, sessionId: row.session_id, projectId: row.project_id, taskId: row.task_id, runId: row.run_id, rowSha256: digest(row), metadataSha256: digest(artifact) }); } assert.deepEqual(canonical(databaseSnapshot(deployed.dataRoot).tables.records), canonical(before.database.tables.records)); const physical = snapshotPersistentData(deployed.dataRoot).artifacts; assert.deepEqual(physical, before.persistent.artifacts, 'Every original physical blob byte remains exact.'); const physicalByPath = new Map(physical.map(value => [value.path, value])); for (const blob of referenced.values()) assert.deepEqual(physicalByPath.get(blob.path), blob, 'Original referenced blob: ' + blob.path); assert.equal(physical.length, deployed.counts.artifacts); const base = 'http://127.0.0.1:4318'; const bootstrap = await (await fetch(base + '/api/bootstrap', { headers: { Connection: 'close' } })).json(); assert.equal(bootstrap.version, '1.20.1'); const health = await (await fetch(base + '/api/health', { headers: { Connection: 'close' } })).json(); assert.equal(health.pid, deployed.identity.pid); const pages = [], ids = new Set(); let offset = 0, total = null; while (total === null || offset < total) { const response = await fetch(base + '/api/operations?artifactOffset=' + offset + '&artifactLimit=100', { headers: { 'X-Workbench-Token': bootstrap.token, Connection: 'close' }, signal: AbortSignal.timeout(30000) }); assert.equal(response.status, 200); const report = await response.json(), page = report.artifacts; assert.equal(report.version, '1.20.1'); assert.equal(page.offset, offset); if (total === null) total = page.total; assert.equal(page.total, total); assert.equal(page.checked, page.results.length); for (const row of page.results) { assert.equal(row.passed, true, row.id + ': ' + row.error); assert.equal(ids.has(row.id), false); const artifact = original.get(row.id); assert.ok(artifact, 'Inspection returned an unknown original artifact ID.'); assert.equal(row.sha256, artifact.sha256); assert.equal(row.bytes, artifact.bytes); ids.add(row.id); } pages.push({ offset, checked: page.checked, total, results: page.results, checks: report.checks, checkedAt: report.checkedAt }); offset += page.checked; assert.ok(page.checked > 0 || total === 0); } assert.equal(ids.size, total); assert.equal(total, original.size); assert.deepEqual([...ids].sort(), [...original.keys()].sort()); assert.deepEqual(canonical(databaseSnapshot(deployed.dataRoot).tables.records), canonical(before.database.tables.records)); const result = { success: true, version: '1.20.1', pid: health.pid, checked: ids.size, total, pages, artifactRecords: original.size, blobFiles: physical.length, referencedBlobFiles: referenced.size, unreferencedBlobFiles: physical.filter(value => !referenced.has(value.path)), producers, physical, checkedAt: new Date().toISOString(), realModelRequests: 0, scope: 'Each API inspection ID/hash/byte length matched its original SQLite artifact metadata and recorded producer identity. All referenced deduplicated blobs matched physical byte hashes; every physical blob matched the user-started snapshot. Record and physical-file counts are separate. No source/tool/model/credential or persistent user-state mutation; unknown/source attention remains recorded in each original inspection.' }; const bytes = Buffer.from(JSON.stringify(result, null, 2)); fs.writeFileSync(output, bytes, { flag: 'wx' }); console.log(JSON.stringify({ success: true, total, checked: ids.size, pages: pages.length, reportSha256: createHash('sha256').update(bytes).digest('hex') }));