// Read-only reconciliation after the original driver stopped in its final // metadata assertion. Never resumes the task, applies files, or writes config. import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { DatabaseSync } from 'node:sqlite'; import { execFileSync } from 'node:child_process'; import { canonical, digest, publicSnapshot } from './state.mjs'; import { inspectorClosed } from './inspector.mjs'; import { root, origin, dataRoot, save, client, meta, sha, connectionId } from './common.mjs'; const read = name => JSON.parse(fs.readFileSync(path.join(root, name))); assert.equal(fs.existsSync(path.join(root, 'MANIFEST.json')), false); assert.equal(fs.existsSync(path.join(root, 'node/reconciliation.json')), false); const before = read('node/before.json'), original = read('node/original-public-metadata.json'); const outcome = read('node/model-outcome.json'), source = read('node/source-identity.json'); const session = read('node/session.json'); assert.equal(outcome.success, false); assert.equal(outcome.stopReason, 'step_limit'); assert.equal(outcome.usage.requests, 8); assert.equal(fs.existsSync(path.join(root, 'node/result.json')), false, 'Preserve the original incomplete finalization.'); const { api, boot, close } = await client(); try { const host = await api('/host/status'); assert.equal(host.identity.pid, 36552); assert.equal(host.activity.active, false); assert.equal(host.desktopWindows.count, 0); assert.equal(boot.version, '1.25.2'); await inspectorClosed(); const after = fs.existsSync(path.join(root, 'node/reconciled-after.json')) ? read('node/reconciled-after.json') : await publicSnapshot(origin, dataRoot()); if (!fs.existsSync(path.join(root, 'node/reconciled-after.json'))) save('node/reconciled-after.json', after); const currentSession = await api('/sessions/' + outcome.sessionId); assert.deepEqual(currentSession.execution.task.usage, outcome.usage); assert.equal(currentSession.status, 'paused'); assert.equal(currentSession.execution.deliveries.length, 0); for (const item of before.exports) assert.deepEqual(after.exports.find(row => row.id === item.id), item); for (const table of ['sessions', 'records', 'events', 'outbox']) { const rows = new Map(after.database.tables[table].rows.map(row => [row.id, row])); for (const row of before.database.tables[table].rows) assert.deepEqual(rows.get(row.id), row, table + '/' + row.id); } const artifacts = new Map(after.persistent.artifacts.map(row => [row.path, row])); for (const item of before.persistent.artifacts) assert.deepEqual(artifacts.get(item.path), item); // The sequential opening snapshot and the driver's subsequent config freeze // straddle an external pool edit. Check both identities explicitly. const current = read('node/final-public-metadata.json'); assert.deepEqual(canonical({ ...current.config, configRevision: original.config.configRevision }), canonical(original.config)); assert.deepEqual(after.modelCenter.selected, before.modelCenter.selected); assert.deepEqual(read('node/after.json').modelCenter.pools, original.registry.pools); assert.deepEqual(after.modelCenter.pools, original.registry.pools); const poolBefore = before.modelCenter.pools[0], poolAfter = after.modelCenter.pools[0]; assert.equal(after.modelCenter.pools.length, before.modelCenter.pools.length); assert.deepEqual(poolAfter, { ...poolBefore, revision: poolBefore.revision + 1, members: [...poolBefore.members, { connectionId: '1a7a55a9-29f2-4b9f-8ea2-5a55afd7eacd', weight: 1 }] }); const observedMetadata = { config: meta('config'), registry: meta('modelRegistry') }; assert.deepEqual(canonical(observedMetadata.config), canonical(current.config)); const laterRegistry = structuredClone(observedMetadata.registry); assert.equal(laterRegistry.revision, current.registry.revision); assert.deepEqual(canonical(laterRegistry), canonical(current.registry)); save('node/external-pool-edit-reconciliation.json', { observedMetadata, openingSnapshotPool: poolBefore, driverFrozenPool: original.registry.pools[0], observedPool: poolAfter, interpretation: 'DeepSeek was added between the sequential opening snapshot and the subsequent driver config freeze. The original driver preserved revision 7. The first reconciliation timing inference was incorrect; this comparison corrects it without overwriting that error or restoring away the edit.' }); const registry = structuredClone(current.registry); assert.ok(registry.revision > original.registry.revision); registry.revision = original.registry.revision; const initialConnection = original.registry.connections.find(row => row.id === connectionId); const finalConnection = registry.connections.find(row => row.id === connectionId); assert.equal(finalConnection.revision, initialConnection.revision + 2); finalConnection.revision = initialConnection.revision; finalConnection.updatedAt = initialConnection.updatedAt; for (const value of [registry, original.registry]) value.connections.sort((a, b) => a.id.localeCompare(b.id)); assert.deepEqual(canonical(registry), canonical(original.registry)); const previousAttempts = read('node/original-attempts.json'), attempts = meta('modelAttempts'); for (const item of previousAttempts) assert.deepEqual(attempts.find(row => row.id === item.id), item); const publicConnection = after.modelCenter.connections.find(row => row.id === connectionId); assert.equal(publicConnection.credentialRevision, 1); assert.equal(publicConnection.hasKey, true); assert.equal(publicConnection.modelIdentityPolicy, 'record'); assert.equal(publicConnection.modelIdentityAliasSource, 'https://api.apikey.fan'); assert.deepEqual(publicConnection.modelIdentityAliases, []); assert.equal(after.modelCenter.connections.find(row => row.name === 'jojocode').enabled, false); // Match the exact original metadata-row hash by retaining a raw array prefix. // This proves every old diagnostic field and its order survived. New external // diagnostic jobs are observations, never rolled back or attributed to Y. const db = new DatabaseSync(path.join(dataRoot(), 'workbench.sqlite'), { readOnly: true }); let externalDiagnostics; try { const row = db.prepare("SELECT * FROM metadata WHERE key='modelNetworkDiagnostics'").get(); const jobs = JSON.parse(row.value); const expected = before.database.tables.metadata.rows.find(value => value.id === row.key).sha256; const matches = []; for (let count = 0; count <= jobs.length; count++) { if (digest({ ...row, value: JSON.stringify(jobs.slice(0, count)) }) === expected) matches.push(count); } assert.equal(matches.length, 1, 'Every original diagnostic must match its original complete row hash.'); externalDiagnostics = jobs.slice(matches[0]); assert.equal(externalDiagnostics.length, 3); for (const job of externalDiagnostics) { assert.ok(job.finishedAt); assert.equal(job.httpRequests, 0); assert.equal(job.modelRequests, 0); assert.equal(job.credentialAccess, false); } save('node/diagnostic-reconciliation.json', { originalRowSha256: expected, originalJobs: matches[0], originalRowExactAfterRemovingOnlyAppendedJobs: true, externalJobs: externalDiagnostics, attribution: 'Not started by the Y driver; preserve separately. No inference about the operator.' }); const previousProjects = new Map(before.database.tables.projects.rows.map(value => [value.id, value])); const rows = db.prepare('SELECT * FROM projects ORDER BY rowid').all(); for (const row of rows.filter(value => value.id !== outcome.projectId)) { assert.equal(digest({ ...row, position: row.position - 1 }), previousProjects.get(row.id)?.sha256, 'Original project row, after reversing only the one owned prepend: ' + row.id); } assert.equal(rows.length, previousProjects.size + 1); } finally { db.close(); } const metadataRows = new Map(after.database.tables.metadata.rows.map(row => [row.id, row])); for (const row of before.database.tables.metadata.rows) { if (!['config', 'modelRegistry', 'modelAttempts', 'modelNetworkDiagnostics'].includes(row.id)) assert.deepEqual(metadataRows.get(row.id), row); } const project = outcome.sourceRoot; for (const [name, expected] of Object.entries(source.files).filter(([name]) => !name.startsWith('src/'))) { assert.equal(sha(fs.readFileSync(path.join(project, name))), expected, name); } const staged = execFileSync('git', ['-C', project, 'show', ':owner-staged.txt'], { encoding: 'utf8', windowsHide: true }); assert.equal(staged, source.stagedBefore); for (const change of session.changes) assert.equal(sha(fs.readFileSync(path.join(project, change.path))), change.sha256); const plans = session.execution.contextPlans.map(plan => ({ id: plan.id, runId: plan.runId, requestBytes: plan.requestBytes, estimatedInputTokens: plan.estimatedInputTokens, reservation: plan.tokenReservation.reservedTokens, method: plan.tokenReservation.method, excludedSources: plan.tokenReservation.routes.reduce((sum, route) => sum + route.excludedSources.length, 0), actualUsage: plan.actualUsage })); save('node/offline-analysis.json', { plans, reads: session.execution.task.readProgress, tools: session.events.filter(event => event.kind === 'tool').map(event => ({ tool: event.tool, phase: event.phase })), checks: session.execution.checks.map(job => ({ id: job.id, status: job.status, results: job.results.map(value => ({ name: value.name, status: value.status, tests: value.tests })) })), applied: session.execution.changeSets.map(value => ({ id: value.id, status: value.status, fingerprint: value.fingerprint })), postApplyChecksExecuted: false, gitDeliveryCompleted: false, supervisorRanPostApplyChecks: false, outlierExclusionTriggered: plans.some(plan => plan.excludedSources > 0), noExtraModelRequests: true, implementationSuppliedBySupervisor: false }); save('node/driver-original-log.txt', fs.readFileSync('.qa/reliability-1.25.2/live-y-node.log')); const reconciliation = { passed: true, engineeringSuccess: false, originalDriverCompleted: false, originalErrorPreserved: true, configRestorationHadAlreadyCompletedBeforeOriginalAssertion: true, originalExportsExact: before.exports.length, originalArtifactFilesExact: before.persistent.artifacts.length, originalProjectRowsExactAfterOneOwnedPrependReversal: true, originalRowsExact: ['sessions', 'records', 'events', 'outbox'], originalAttemptsExact: previousAttempts.length, rawConfigExceptRevisionExact: true, registryExceptMonotonicRevisionsTimestampOrderExact: true, otherMetadataRowsExact: true, appendedExternalDiagnostics: externalDiagnostics.map(value => value.id), noExternalChangesRolledBack: true, budgetsSelectionPoolsAndIdentityRestoredAtOriginalFinalization: true, externalPoolAdditionAcrossOpeningSnapshotAndConfigFreezePreserved: 'DeepSeek; pool revision 6 to 7', credentialRevision: 1, hasKey: true, ownerBytesAndStagingExact: true, pid: after.pid, version: after.version, inspectorClosed: true, observedAt: new Date().toISOString(), realModelRequests: 0, commandsExecutedInSample: 0 }; save('node/reconciliation.json', reconciliation); console.log(JSON.stringify(reconciliation)); } finally { await close(); }