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'; // New bounded real sample Y on formal 1.25.2. Same fixed expense-report task, // same apikey GPT Chat streaming connection, same 16K/2048/8-request/90000-token caps. // Never reads, copies, decrypts or re-stores the real key; uses the formal // Provider's in-memory connection only. Never replays a sealed sample. export const root = path.resolve('.qa/live-delivery-y'); export const origin = 'http://127.0.0.1:4318'; export const connectionId = '93d0cf6d-4f32-4918-9da8-6f125aa8a6e5'; export const sha = value => createHash('sha256').update(value).digest('hex'); export const readRoot = () => JSON.parse(fs.readFileSync(path.join(root, 'current.json'))); export const dataRoot = () => readRoot().host.identity.dataRoot; export const save = (relative, value) => { const file = path.join(root, 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 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 }); 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; }