Files
Agent-Workbench/scripts/live-ad/finish.mjs
T

109 lines
11 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { DatabaseSync } from 'node:sqlite';
import { publicSnapshot, digest, canonical } from '../retained-host-state.mjs';
import { root, origin, read, save, client, publicMetadata, compareConfig, sha } from './common.mjs';
import { inspectorClosed } from './inspector.mjs';
// Each read-only audit has a new identity; a failed audit is never overwritten.
const stage = process.argv.includes('--compatibility') ? 'compatibility' : 'node';
const prefix = 'audits/' + stage + '-' + randomUUID(), frozen = read('current.json'), compatibility = read('compatibility/result.json'), node = stage === 'compatibility' ? { executed:false, success:false, notYetRun:true } : read('node/result.json');
const { api, boot, close } = await client(); let result;
try {
await inspectorClosed(); const host = await api('/host/status'); assert.equal(host.activity.active, false); assert.equal(host.desktopWindows.count, 0);
assert.equal(host.identity.pid, frozen.host.identity.pid); assert.equal(host.identity.instanceId, frozen.host.identity.instanceId); assert.equal(boot.version, '1.25.8');
const after = await publicSnapshot(origin, host.identity.dataRoot); save(prefix + '/after.json', after);
const before = frozen.baseline, metaBefore = frozen.originalPublicMetadata, metaAfter = publicMetadata(host.identity.dataRoot);
const ownedSessionIds = new Set(compatibility.results.flatMap(row => row.preflights.map(value => value.sessionId)).concat(node.sessionId ? [node.sessionId] : []));
const ownedProjectIds = new Set(compatibility.results.map(row => row.projectId));
if (node.executed) ownedProjectIds.add(read('node/owned.json').project.id);
assert.deepEqual(new Set(boot.projects.filter(row => !frozen.originalProjects.some(value => value.id === row.id)).map(row => row.id)), ownedProjectIds);
assert.deepEqual(boot.projects.filter(row => !ownedProjectIds.has(row.id)), frozen.originalProjects);
assert.deepEqual(after.database.sessionIds.filter(id => !before.database.sessionIds.includes(id)).sort(), [...ownedSessionIds].sort());
const configComparison = compareConfig(metaBefore.config, metaAfter.config);
assert.deepEqual(metaAfter.modelAttempts.slice(0, metaBefore.modelAttempts.length), metaBefore.modelAttempts, 'Every original model attempt stays exact.');
const newAttempts = metaAfter.modelAttempts.slice(metaBefore.modelAttempts.length); assert.ok(newAttempts.every(row => ownedSessionIds.has(row.sessionId)));
assert.deepEqual(metaAfter.modelNetworkDiagnostics.slice(0, metaBefore.modelNetworkDiagnostics.length), metaBefore.modelNetworkDiagnostics);
const newDiagnostics = metaAfter.modelNetworkDiagnostics.slice(metaBefore.modelNetworkDiagnostics.length);
const knownDiagnosticIds = compatibility.results.map(row => read('compatibility/' + row.connectionId + '/network-start.json').job.id);
assert.deepEqual(new Set(newDiagnostics.map(row => row.id)), new Set(knownDiagnosticIds));
const oldRegistry = structuredClone(metaBefore.modelRegistry), currentRegistry = structuredClone(metaAfter.modelRegistry), registryChanges = {};
if (node.executed) {
const original = oldRegistry.connections.find(row => row.id === node.connectionId), actual = currentRegistry.connections.find(row => row.id === node.connectionId);
assert.ok(original && actual); assert.equal(actual.revision, original.revision + 2); assert.ok(actual.updatedAt >= original.updatedAt);
assert.equal(actual.modelIdentityPolicy, original.modelIdentityPolicy ?? 'record'); assert.deepEqual(actual.modelIdentityAliases, original.modelIdentityAliases ?? []); assert.equal(actual.modelIdentityAliasSource, original.modelIdentityAliasSource ?? '');
const normalized = { ...actual, revision: original.revision, updatedAt: original.updatedAt };
for (const key of ['modelIdentityPolicy','modelIdentityAliases','modelIdentityAliasSource']) if (!Object.hasOwn(original,key)) delete normalized[key];
assert.deepEqual(normalized, original);
assert.equal(currentRegistry.revision, oldRegistry.revision + 4);
registryChanges.connection = { id: node.connectionId, originalRevision: original.revision, currentRevision: actual.revision, originalUpdatedAt: original.updatedAt, currentUpdatedAt: actual.updatedAt,
explicitIdentityDefaults: ['modelIdentityPolicy','modelIdentityAliases','modelIdentityAliasSource'].filter(key=>!Object.hasOwn(original,key)), originalOrder: oldRegistry.connections.map(row=>row.id), currentOrder: currentRegistry.connections.map(row=>row.id) };
currentRegistry.revision = oldRegistry.revision;
currentRegistry.connections = oldRegistry.connections.map(value => value.id === original.id ? normalized : currentRegistry.connections.find(row=>row.id===value.id));
}
assert.deepEqual(currentRegistry, oldRegistry, 'Registry differs only by declared temporary policy/selection revisions, time and order.');
assert.deepEqual(after.modelCenter.selected, frozen.center.selected); assert.deepEqual(after.modelCenter.pools, frozen.center.pools);
for (const old of frozen.center.connections) {
const current = after.modelCenter.connections.find(row => row.id === old.id); assert.ok(current);
assert.equal(current.hasKey, old.hasKey); assert.equal(current.credentialRevision, old.credentialRevision);
}
const exportMap = new Map(after.exports.map(row=>[row.id,row])), artifactMap = new Map(after.persistent.artifacts.map(row=>[row.path,row]));
for (const original of before.exports) assert.deepEqual(exportMap.get(original.id), original, 'Original session export ' + original.id);
for (const original of before.persistent.artifacts) assert.deepEqual(artifactMap.get(original.path), original, 'Original artifact ' + original.path);
const db = new DatabaseSync(path.join(host.identity.dataRoot,'workbench.sqlite'), { readOnly: true });
const addedRows = {}; let checkedOriginalRows = 0;
try {
assert.deepEqual(Object.keys(after.database.tables),Object.keys(before.database.tables));
for (const [table, oldTable] of Object.entries(before.database.tables)) {
const nextTable = after.database.tables[table]; assert.equal(nextTable.schemaSha256,oldTable.schemaSha256); addedRows[table] = nextTable.count-oldTable.count;
const originals = new Set(oldTable.rows.map(row => (row.kind ?? '') + ':' + row.id)), nextRows = new Map(nextTable.rows.map(row => [(row.kind ?? '')+':'+row.id,row]));
for (const row of oldTable.rows) {
const current = nextRows.get((row.kind ?? '')+':'+row.id);
if (table === 'sqlite_sequence') {
const current = db.prepare('SELECT * FROM sqlite_sequence WHERE name=?').get('events'); assert.equal(current.seq,after.eventCursor);
assert.equal(digest({ ...current, seq:before.eventCursor }),row.sha256);
} else if (table === 'projects') {
const current = db.prepare('SELECT * FROM projects WHERE id=?').get(row.id); assert.equal(digest({...current,position:current.position-ownedProjectIds.size}),row.sha256);
} else if (table === 'metadata' && Object.hasOwn(metaBefore,row.id)) {
const original = db.prepare('SELECT * FROM metadata WHERE key=?').get(row.id);
assert.equal(digest({...original,value:JSON.stringify(metaBefore[row.id])}),row.sha256);
} else { assert.ok(current, table+': missing '+row.id); assert.equal(current.sha256,row.sha256,table+': changed '+row.id); }
checkedOriginalRows++;
}
if (['events','records','sessions'].includes(table)) {
const ownerColumns = table === 'records' ? 'id,kind,session_id,project_id' : table === 'events' ? 'id,session_id,project_id' : 'id,project_id';
for (const row of db.prepare('SELECT '+ownerColumns+' FROM '+table).all()) if (!originals.has((row.kind ?? '')+':'+row.id)) {
if (table === 'sessions') assert.ok(ownedSessionIds.has(row.id)); else assert.ok(ownedSessionIds.has(row.session_id)); assert.ok(ownedProjectIds.has(row.project_id));
}
} else if (table === 'outbox') {
for (const row of db.prepare('SELECT outbox.seq,events.session_id FROM outbox JOIN events USING(seq) WHERE outbox.seq>?').all(before.eventCursor)) assert.ok(ownedSessionIds.has(row.session_id));
} else if (table === 'metadata') assert.equal(addedRows[table],0);
else if (table === 'sqlite_sequence') assert.equal(nextTable.count,1);
}
const referencedArtifacts = new Map(db.prepare("SELECT payload FROM records WHERE kind='artifact'").all().map(row=>{const value=JSON.parse(row.payload); return [value.relative,value];}));
const oldArtifactPaths = new Set(before.persistent.artifacts.map(row=>row.path));
for (const artifact of after.persistent.artifacts) if (!oldArtifactPaths.has(artifact.path)) {
const reference = referencedArtifacts.get(artifact.path); assert.ok(reference && ownedSessionIds.has(reference.sessionId), 'New artifact must belong to this batch.');
assert.equal(reference.bytes,artifact.bytes); assert.equal(reference.sha256,artifact.sha256);
}
} finally { db.close(); }
for (const original of before.persistent.files) {
const actual = after.persistent.files.find(row=>row.path===original.path); assert.ok(actual);
if (original.path === 'config.json' && actual.sha256 !== original.sha256) {
const actualConfig = JSON.parse(fs.readFileSync(path.join(host.identity.dataRoot,original.path),'utf8'));
assert.deepEqual(actualConfig,metaAfter.config);
assert.equal(sha(JSON.stringify({...actualConfig,configRevision:metaBefore.config.configRevision},null,2)+'\n'),original.sha256,'Original config bytes reconstructed exactly after the declared revision increment.');
} else assert.equal(actual.sha256,original.sha256);
}
const newArtifactCount = after.persistent.artifacts.length-before.persistent.artifacts.length;
result = { success:true, stage, version:boot.version,pid:host.identity.pid,originalProjects:frozen.originalProjects.length,originalSessions:before.database.sessionIds.length,
addedProjects:ownedProjectIds.size,addedSessions:ownedSessionIds.size,addedRows,newArtifactCount,checkedOriginalRows, originalSessionExports:'exact',originalArtifactBytes:'exact',
originalRows:'exact except explicitly reconciled project positions, event sequence and four public metadata rows',configComparison,registryChanges,
oldCredentialRow:'exact hash, never decrypted or copied',oldModelAttempts:'every original entry exact',newModelAttempts:newAttempts.length,inspectorClosed:true,keysReadOrResaved:false,
reactPythonExecuted:false,purgeExecuted:false,nodePassed:node.success };
} catch (error) { result = {success:false,error:error.stack,keysReadOrResaved:false};process.exitCode=1; }
finally { save(prefix+'/result.json',result);await close(); }
console.log(JSON.stringify({prefix,...result}));