feat(memory): release Mnemo project bridge and schema2 recovery
Official contracts and Windows source CI / windows-source (push) Has been cancelled

This commit is contained in:
Agent Workbench Dev
2026-11-11 03:03:43 +08:00
parent f7705d9634
commit e306f40cae
76 changed files with 10475 additions and 127 deletions
@@ -0,0 +1,37 @@
import { createHash } from 'node:crypto';
import { DatabaseSync } from 'node:sqlite';
export const REQUEST_LEDGER_SQL = `CREATE TABLE IF NOT EXISTS model_attempts (
seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL, connection_id TEXT, session_id TEXT, task_id TEXT, run_id TEXT,
status TEXT, model TEXT, protocol TEXT, legacy_position INTEGER UNIQUE,
payload TEXT NOT NULL CHECK(json_valid(payload)), payload_sha256 TEXT NOT NULL,
initial_payload TEXT NOT NULL CHECK(json_valid(initial_payload)), initial_sha256 TEXT NOT NULL);
CREATE INDEX IF NOT EXISTS model_attempts_id ON model_attempts(id);
CREATE INDEX IF NOT EXISTS model_attempts_connection ON model_attempts(connection_id,seq);
CREATE INDEX IF NOT EXISTS model_attempts_session ON model_attempts(session_id,task_id,run_id,seq);
CREATE INDEX IF NOT EXISTS model_attempts_status ON model_attempts(status,seq);`;
export const normalizedSchemaSql = sql => sql === null ? null : sql.replace(/\s+/g, ' ').trim();
export function databaseSchemaObjects(database) {
return database.prepare("SELECT type,name,tbl_name,sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' ORDER BY name").all()
.map(row => ({ type: row.type, name: row.name, table: row.tbl_name, sql: normalizedSchemaSql(row.sql) }));
}
const reference = new DatabaseSync(':memory:');
let objects;
try { reference.exec(REQUEST_LEDGER_SQL); objects = databaseSchemaObjects(reference); }
finally { reference.close(); }
export const REQUEST_LEDGER_OBJECTS = Object.freeze(objects.map(row => Object.freeze(row)));
export const REQUEST_LEDGER_MIGRATION = Object.freeze({
id: 'model-attempt-ledger-v1', from: 1, to: 2,
definitionSha256: createHash('sha256').update(JSON.stringify({ objects, marker: 'modelAttemptLedger',
legacyProjection: 'array-order/seq=index+1/legacy_position=index/current=initial=JSON.stringify(entry)/sha256-utf8/v1' })).digest('hex'),
});
// These are the exact columns written by the original migration, including
// search projections. Hashing only payloads would miss altered ownership.
export function legacyLedgerRow(entry, index) {
const payload = JSON.stringify(entry), sha = createHash('sha256').update(payload).digest('hex');
return { seq: index + 1, id: entry.id, connection_id: entry.connectionId ?? null, session_id: entry.sessionId ?? null,
task_id: entry.taskId ?? null, run_id: entry.runId ?? null, status: entry.status ?? null, model: entry.model ?? null,
protocol: entry.protocol ?? null, legacy_position: index, payload, payload_sha256: sha, initial_payload: payload, initial_sha256: sha };
}