import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { DatabaseSync } from 'node:sqlite'; import { digest } from '../retained-host-state.mjs'; import { root, read, save, client, sha, captureSession } from './common.mjs'; // Explicitly record the independently discovered greeting; do not automatically // accept every new session, repeat its request, or add it to engineering results. const sessionId = '8f896f90-50f2-4d07-8da0-3b98832c6df5'; const attemptId = '821c6dc9-7e52-4e41-a1d9-36b4de3ecbdb'; const prefix = 'external-observations/' + sessionId, frozen = read('frozen.json'); assert.equal(fs.existsSync(path.join(root, prefix)), false, 'Do not overwrite an external observation.'); assert.equal(frozen.baseline.database.sessionIds.includes(sessionId), false); const { api, close } = await client(); try { const host = await api('/host/status'); assert.equal(host.identity.pid, frozen.host.identity.pid); assert.equal(host.activity.active, false); const captured = await captureSession(api, sessionId, prefix), state = captured.state; assert.ok(frozen.originalProjects.some(value => value.id === state.projectId)); assert.equal(state.status, 'completed'); assert.equal(state.execution.run.resumeOf, null); assert.deepEqual(state.messages.filter(value => value.role === 'user').map(value => value.content), ['你好']); assert.equal(state.changes.length, 0); assert.equal(state.execution.deliveries.length, 0); assert.equal(state.events.filter(value => value.kind === 'tool').length, 0); assert.deepEqual(captured.rows.map(value => value.id), [attemptId]); const db = new DatabaseSync(path.join(host.identity.dataRoot, 'workbench.sqlite'), { readOnly: true }), tables = {}; db.exec('BEGIN'); try { for (const table of ['sessions', 'events', 'records', 'model_attempts', 'outbox']) { const rows = table === 'outbox' ? db.prepare('SELECT outbox.* FROM outbox JOIN events USING(seq) WHERE events.session_id=? ORDER BY outbox.seq').all(sessionId) : db.prepare('SELECT * FROM ' + table + ' WHERE ' + (table === 'sessions' ? 'id' : 'session_id') + '=? ORDER BY rowid').all(sessionId); const sql = db.prepare("SELECT sql FROM sqlite_schema WHERE type='table' AND name=?").get(table).sql; assert.equal(sha(sql), frozen.baseline.database.tables[table].schemaSha256); tables[table] = { count: rows.length, schemaSha256: sha(sql), rows: rows.map(row => ({ id: row.id ?? row.seq, ...(row.kind ? { kind: row.kind } : {}), sha256: digest(row) })) }; } } finally { db.exec('ROLLBACK'); db.close(); } const index = { schemaVersion: 1, observedAt: new Date().toISOString(), sessionId, projectId: state.projectId, taskId: state.execution.task.id, runId: state.execution.run.id, title: state.title, scope: 'Independent new greeting in an existing project; attribution to a person is not asserted.', m4Engineering: false, requests: captured.total, usage: state.execution.task.usage, attempts: captured.rows.map(value => ({ id: value.id, status: value.status, usageStatus: value.usageStatus, modelIdentity: value.modelIdentity, requestSha256: value.requestSha256, responseSha256: value.responseSha256 })), exports: { id: sessionId, project: digest(captured.exported.project), session: digest(captured.exported.session), execution: digest(captured.exported.execution) }, artifacts: state.execution.artifacts.map(value => ({ id: value.id, path: value.relative, bytes: value.bytes, sha256: value.sha256 })), tables, captures: captured.captures, resumedHistoricalTask: false, toolCalls: 0, changes: 0, deliveries: 0, keysReadOrResaved: false, requestsReplayed: false, productOrProjectMutation: false }; const relative = prefix + '/index.json'; save(relative, index); save('external-observations/declared.json', { schemaVersion: 1, sources: [{ path: relative, sha256: sha(fs.readFileSync(path.join(root, relative))) }] }); console.log(JSON.stringify({ sessionId, projectId: state.projectId, requests: index.requests, totalTokens: index.usage.totalTokens, tableCounts: Object.fromEntries(Object.entries(tables).map(([key, value]) => [key, value.count])), artifacts: index.artifacts.length, modelIdentity: index.attempts[0].modelIdentity.status, scope: index.scope })); } finally { await close(); }