Files
Agent-Workbench/scripts/live-ac/common.mjs
T

76 lines
5.0 KiB
JavaScript

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 { Agent, fetch } from 'undici';
// Recovery sample AC: re-run the sealed AB expense task on the formally upgraded
// 1.25.4 host to observe whether bounded output-truncation recovery lets the model
// split its proposals and progress, where AB (1.25.3) ended at the 2048 ceiling.
export const origin = 'http://127.0.0.1:4318';
export const batch = path.resolve('.qa/live-recovery-ac');
// Same DeepSeek connection and low-reasoning parameters as the frozen AB sample.
export const connectionId = '1a7a55a9-29f2-4b9f-8ea2-5a55afd7eacd';
export const experiment = { key: 'deepseek', id: connectionId, parameters: { thinking: { type: 'enabled' }, reasoning_effort: 'low' } };
export const root = batch;
// The AB sample froze the connection's model/protocol/credentialRevision; reuse it
// so AC targets exactly the same real route without re-reading any credential.
const abCurrent = path.resolve('.qa/live-delivery-ab/current.json');
if (fs.existsSync(abCurrent)) {
const frozen = JSON.parse(fs.readFileSync(abCurrent));
experiment.model = frozen.experiment.model; experiment.protocol = frozen.experiment.protocol;
const connection = frozen.center.connections.find(c => c.id === connectionId);
experiment.credentialRevision = connection?.credentialRevision ?? 1;
experiment.abConnectionRevision = frozen.connectionRevision;
}
export const sha = value => createHash('sha256').update(value).digest('hex');
export const saveAt = (directory, relative, value) => {
const file = path.join(directory, relative); fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value, null, 2) + '\n', { flag: 'wx' });
};
export const save = (relative, value) => saveAt(root, relative, value);
export const readRoot = () => JSON.parse(fs.readFileSync(path.join(root, 'current.json')));
export const dataRoot = () => JSON.parse(fs.readFileSync(path.join(batch, 'current.json'))).host.identity.dataRoot;
export function meta(name) {
assert.ok(['config', 'modelRegistry', 'modelAttempts', 'modelNetworkDiagnostics'].includes(name));
const db = new DatabaseSync(path.join(dataRoot(), 'workbench.sqlite'), { readOnly: true });
try { const row = db.prepare('SELECT value FROM metadata WHERE key=?').get(name); return row ? JSON.parse(row.value) : null; }
finally { db.close(); }
}
export async function client() {
const dispatcher = new Agent({ connect: { autoSelectFamily: true, timeout: 5000 } }); let token;
const api = async (route, input, timeout = 30000) => {
const response = await fetch(origin + '/api' + route, { dispatcher,
method: input === undefined ? 'GET' : 'POST', redirect: 'error', signal: AbortSignal.timeout(timeout),
headers: { ...(token ? { 'X-Workbench-Token': token } : {}), 'Content-Type': 'application/json', Connection: 'close' },
...(input === undefined ? {} : { body: JSON.stringify(input) }) });
const value = await response.json();
if (!response.ok) throw Object.assign(new Error(route + ': ' + (value.error ?? response.status)), { status: response.status, response: value });
return value;
};
const boot = await api('/bootstrap'); token = boot.token;
return { api, boot, close: () => dispatcher.close() };
}
export async function captureAttempts(api, attempts, relative) {
const captures = [];
for (const attempt of attempts) for (const kind of ['request', 'response']) {
const id = attempt[kind + 'ArtifactId']; if (!id) continue;
let content = '', offset = 0, page;
do { page = await api('/artifacts/' + id + '?session=' + attempt.sessionId + '&offset=' + offset + '&limit=16000');
assert.ok(page.content.length || !page.truncated); content += page.content; offset += page.content.length;
} while (page.truncated);
assert.equal(sha(content), page.sha256); assert.equal(page.sha256, attempt[kind + 'Sha256']);
const name = `${relative}/${attempt.id}-${kind}.${kind === 'request' ? 'json' : attempt.streaming ? 'sse' : 'txt'}`;
save(name, content); captures.push({ attemptId: attempt.id, kind, artifactId: id, path: name, sha256: sha(content), bytes: Buffer.byteLength(content) });
}
return captures;
}
// AC keeps its own independent request budget, separate from the sealed AA/AB batch.
export function acUsage() {
const before = new Set(JSON.parse(fs.readFileSync(path.join(root, 'original-attempts.json'))).map(row => row.id));
const attempts = (meta('modelAttempts') ?? []).filter(row => !before.has(row.id));
const tokens = attempts.reduce((sum, row) => sum + (row.usage?.total_tokens ?? 0), 0);
return { requests: attempts.length, tokens, unknown: attempts.filter(row => row.usageStatus !== 'reported').length };
}
export function assertAcBudget() { const usage = acUsage(); assert.ok(usage.requests < 40 && usage.tokens < 400000, 'AC sample budget exhausted'); return usage; }