From da2e6313818a0d527583bb539eef7e34141bda8c Mon Sep 17 00:00:00 2001 From: Agent Workbench Dev Date: Sat, 7 Nov 2026 12:38:44 +0800 Subject: [PATCH] feat(models): preserve complete incremental request ledger and exports --- scripts/verify-request-ledger-ui.mjs | 176 ++++++++++++++++ src/client/ModelCenter.tsx | 13 +- src/client/ModelRequestLedger.css | 13 ++ src/client/ModelRequestLedger.tsx | 80 ++++++++ src/client/api.ts | 6 + src/core/models/model-hub.mjs | 48 +++-- src/core/models/request-ledger.mjs | 190 +++++++++++++++++ src/core/storage/state-database.mjs | 14 +- src/server/app.mjs | 9 + src/server/hot-upgrade.mjs | 10 + src/server/operations.mjs | 2 +- src/server/store.mjs | 2 +- .../legacy-request-ledger/SOURCE.json | 8 + .../legacy-request-ledger/state-database.mjs | 130 ++++++++++++ tests/fixtures/request-ledger-data.mjs | 23 +++ tests/request-ledger.test.mjs | 192 ++++++++++++++++++ 16 files changed, 888 insertions(+), 28 deletions(-) create mode 100644 scripts/verify-request-ledger-ui.mjs create mode 100644 src/client/ModelRequestLedger.css create mode 100644 src/client/ModelRequestLedger.tsx create mode 100644 src/core/models/request-ledger.mjs create mode 100644 tests/fixtures/legacy-request-ledger/SOURCE.json create mode 100644 tests/fixtures/legacy-request-ledger/state-database.mjs create mode 100644 tests/fixtures/request-ledger-data.mjs create mode 100644 tests/request-ledger.test.mjs diff --git a/scripts/verify-request-ledger-ui.mjs b/scripts/verify-request-ledger-ui.mjs new file mode 100644 index 00000000..b5c00af4 --- /dev/null +++ b/scripts/verify-request-ledger-ui.mjs @@ -0,0 +1,176 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import net from 'node:net'; +import assert from 'node:assert/strict'; +import { randomUUID } from 'node:crypto'; +import { DatabaseSync } from 'node:sqlite'; +import { chromium, _electron } from 'playwright'; +import { expect } from 'playwright/test'; +import { fixture, launchWorkbench, until } from '../tests/helpers.mjs'; +import { Provider } from '../src/server/provider.mjs'; +import { HostController } from '../src/core/desktop/host-controller.mjs'; +import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs'; +import { projectEnvironment } from '../src/core/process-environment.mjs'; +import { usageSamples, usageModelId, cacheUsageFixture } from '../tests/fixtures/cache-usage-model.mjs'; +import { historicalAttempts, inspectExport, hash } from '../tests/fixtures/request-ledger-data.mjs'; +import { openWorkbenchTool } from './ui-navigation.mjs'; + +assert.ok(process.argv.slice(2).every(value => value === '--desktop')); +assert.ok(process.env.AGENT_WORKBENCH_QA_APP_ROOT, 'Use a frozen independent candidate'); +const desktop = process.argv.includes('--desktop'), appRoot = fs.realpathSync.native(process.env.AGENT_WORKBENCH_QA_APP_ROOT); +const directory = path.resolve('.qa/m2-request-ledger-2026-11-07', (desktop ? 'electron-' : 'web-') + randomUUID()); +fs.mkdirSync(directory, { recursive: true }); console.log('EVIDENCE ' + directory); +const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true }); +const model = await cacheUsageFixture(t, { ...usageSamples.chat, streaming: true }); +const hub = Object.assign(Object.create(Provider.prototype), { apiKey: '' }).modelHub(f.store); +const connection = hub.upsert({ name: '自有账本验证 · Chat', model: usageModelId, protocol: 'chat-completions', streaming: true, + baseUrl: model.baseUrl, requiresKey: false, modelIdentityPolicy: 'require-match' }); +hub.select({ type: 'connection', id: connection.id }); +// Config setup uses source contracts; the candidate itself receives an old +// schema-1 database with the complete original array and performs migration. +assert.equal(hub.ledger.rollbackInitialization(), true); +const historical = historicalAttempts(); historical.at(-1).status = 'running'; delete historical.at(-1).finishedAt; +historical.at(-1).usageStatus = 'unknown'; historical.at(-1).usage = null; +f.store.storage.setMeta('modelAttempts', historical); +const originalMetadata = f.store.storage.database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get().value; +fs.writeFileSync(path.join(directory, 'legacy-metadata.json'), originalMetadata); f.store.close(); +const checks = [], screenshots = [], downloads = [], errors = [], externalBlocked = [], pages = []; +let host, controller, browser, app, page, window, port, result, desktopStdout = '', desktopStderr = '', beforeRestart, current; +const mark = value => { checks.push(value); console.log('PASS ' + value); }; +const center = () => page.getByRole('dialog', { name: '模型中心', exact: true }); +const ledger = () => center().getByRole('region', { name: '完整请求账本', exact: true }); +const entries = () => ledger().locator('.mc-attempts article'); +const pager = () => ledger().getByRole('navigation', { name: '请求账本分页 · 列表上方', exact: true }); +async function openLedger() { await openWorkbenchTool(page, '打开模型中心'); await center().getByRole('button', { name: '请求账本', exact: true }).click(); await expect(ledger()).toHaveAttribute('aria-busy', 'false'); } +async function resize(width, height = 960) { + if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height }); await until(() => page.evaluate(w => innerWidth === w, width)); } + else await page.setViewportSize({ width, height }); +} +async function shot(name, scope) { + await scope.scrollIntoViewIfNeeded(); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'No unintended page overflow'); + const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); + screenshots.push({ file: path.basename(file), sha256: hash(fs.readFileSync(file)) }); fs.writeFileSync(path.join(directory, name + '.txt'), await scope.innerText()); +} +async function download(name, action) { + const file = path.join(directory, name); let native = null; + if (app) { + const id = randomUUID(); + await app.evaluate(({ session }, { id, file }) => { + globalThis.workbenchQaDownloads ??= {}; globalThis.workbenchQaDownloads[id] = { state: 'waiting' }; + session.defaultSession.once('will-download', (_event, item) => { + const value = globalThis.workbenchQaDownloads[id]; value.suggestedFilename = item.getFilename(); value.state = 'started'; item.setSavePath(file); + item.once('done', (_event, state) => { value.state = state; value.bytes = item.getReceivedBytes(); value.totalBytes = item.getTotalBytes(); }); + }); + }, { id, file }); + await action(); await expect.poll(() => app.evaluate((_electron, id) => globalThis.workbenchQaDownloads[id].state, id), { timeout: 30000 }).toBe('completed'); + native = await app.evaluate((_electron, id) => globalThis.workbenchQaDownloads[id], id); + assert.equal(native.bytes, native.totalBytes); assert.equal(native.bytes, fs.statSync(file).size); + } else { const pending = page.waitForEvent('download'); await action(); await (await pending).saveAs(file); } + const bytes = fs.readFileSync(file); downloads.push({ file: name, bytes: bytes.length, sha256: hash(bytes), ...(native ? { native, + scope: 'Actual Electron DownloadItem; QA selects owned path, Windows save dialog not verified' } : { scope: 'Actual browser download' }) }); return bytes; +} +async function start() { + if (desktop) { + if (!port) { const socket = net.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); port = socket.address().port; await new Promise(resolve => socket.close(resolve)); } + assert.notEqual(port, 4318); controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start(); + host = { url: controller.url, api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) }; + app = await _electron.launch({ executablePath: desktopExecutableIdentity(appRoot).path, + args: [path.join(appRoot, 'dist-desktop/main.cjs'), '--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port), '--hidden-window'], + cwd: appRoot, env: projectEnvironment(), timeout: 60000 }); + app.process().stdout?.on('data', bytes => { desktopStdout += bytes.toString('utf8'); }); app.process().stderr?.on('data', bytes => { desktopStderr += bytes.toString('utf8'); }); + page = await app.firstWindow(); window = await app.browserWindow(page); await window.evaluate(win => win.showInactive()); + } else { host = await launchWorkbench(t, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ acceptDownloads: true }); } + page.on('pageerror', error => errors.push(error.message)); + await page.route('**/*', route => { const url = new URL(route.request().url()); if (url.origin === new URL(host.url).origin || ['data:', 'blob:'].includes(url.protocol)) return route.continue(); externalBlocked.push(url.origin); return route.abort(); }); + await page.addInitScript(id => localStorage.setItem('awb-project', id), f.project.id); + if (desktop) { + // main.mjs awaits its initial loadURL. Replacing that navigation early + // rejects startup and closes the actual Electron app. + await page.waitForURL(url => url.origin === new URL(host.url).origin && url.searchParams.get('desktop') === '1'); + await page.waitForLoadState('networkidle'); await page.reload(); + } else await page.goto(host.url); + await page.waitForLoadState('networkidle'); +} +async function stop() { await app?.close(); await browser?.close(); if (controller && await controller.health()) await controller.stop(); await host?.close?.(); app = null; browser = null; page = null; window = null; } +async function filter(query = '', status = '', connectionId = '') { + await ledger().getByLabel('搜索请求账本', { exact: true }).fill(query); await ledger().getByLabel('筛选账本连接', { exact: true }).selectOption(connectionId); + await ledger().getByLabel('筛选账本结果', { exact: true }).selectOption(status); await ledger().getByRole('button', { name: '筛选记录', exact: true }).click(); + await expect(ledger()).toHaveAttribute('aria-busy', 'false'); +} +function originalState() { + const database = new DatabaseSync(path.join(f.data, 'workbench.sqlite'), { readOnly: true }); + try { const original = database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get().value; assert.equal(original, originalMetadata); + return { originalMetadataSha256: hash(original), databaseVersion: database.prepare('PRAGMA user_version').get().user_version }; } + finally { database.close(); } +} +try { + await start(); const initial = await host.api('/model-center/attempts?limit=2000'); assert.equal(initial.total, 1107); assert.equal(initial.items.length, 1107); + const unknown = await host.api('/model-center/attempts/1107'); assert.equal(unknown.item.status, 'unknown'); assert.deepEqual(unknown.initial.item, historical.at(-1)); + assert.equal(originalState().databaseVersion, 2); assert.equal((await host.api('/bootstrap')).storage.schemaVersion, 2); assert.equal(model.requests.length, 0); fs.writeFileSync(path.join(directory, 'initial-page.json'), JSON.stringify(initial, null, 2)); + await openLedger(); await resize(1280); await expect(entries()).toHaveCount(50); await expect(ledger()).toContainText('1,107 条已保存'); await shot('ledger-controls-1280', ledger().locator('.mc-ledger-filter')); + const unknownRow = entries().first(); await unknownRow.locator('.mc-ledger-source > summary').focus(); await page.keyboard.press('Enter'); + const sourceBytes = await download('legacy-unknown-source.json', () => unknownRow.getByRole('button', { name: '下载本条与初始记录', exact: true }).click()); + assert.deepEqual(JSON.parse(sourceBytes), unknown); await resize(390, 844); await shot('unknown-initial-source-390', unknownRow.locator('.mc-ledger-source')); + mark('Candidate migrates all 1107 old receipts, keeps metadata bytes, renders recovered unknown and downloads its unchanged initial record with keyboard expansion'); + + await resize(1280); const sequences = []; + while (true) { + const values = await entries().evaluateAll(rows => rows.map(row => Number(row.dataset.ledgerSequence))); pages.push(values); sequences.push(...values); + if (await pager().getByRole('button', { name: '下一页', exact: true }).isDisabled()) break; + await pager().getByRole('button', { name: '下一页', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', String(values.at(-1) - 1)); + } + assert.deepEqual(sequences, Array.from({ length: 1107 }, (_, index) => 1107 - index)); assert.equal(pages.length, 23); + await pager().getByRole('button', { name: '上一页', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '57'); + await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '1107'); + fs.writeFileSync(path.join(directory, 'ui-page-sequences.json'), JSON.stringify(pages)); mark('All 23 UI pages cross old 100/1000 boundaries exactly once; previous page and refresh retain complete ordered history without model dispatch'); + + await filter('owned-history', 'failed', 'owned-beta'); const filtered = historical.filter(value => value.connectionId === 'owned-beta' && value.status === 'failed'); + await expect(pager()).toContainText('当前范围 ' + filtered.length + ' 条'); + const filteredBytes = await download('filtered-complete.ndjson', () => ledger().getByRole('button', { name: '导出当前范围全部记录', exact: true }).click()); + assert.deepEqual(inspectExport(filteredBytes).records.map(value => value.item), filtered); await shot('historical-filter-1280', ledger().locator('.mc-ledger-filter')); + await filter('owned-no-match', 'failed', 'owned-beta'); await expect(ledger()).toContainText('当前范围没有请求记录'); + const emptyBytes = await download('empty-complete.ndjson', () => ledger().getByRole('button', { name: '导出当前范围全部记录', exact: true }).click()); assert.equal(inspectExport(emptyBytes).records.length, 0); + let injected = 0; const pattern = '**/api/model-center/attempts?*'; await page.route(pattern, route => { injected++; return route.fulfill({ status: 400, contentType: 'application/json', body: JSON.stringify({ error: '自有账本读取失败,原记录保留,请刷新核对。' }) }); }); + await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(ledger().getByRole('alert')).toContainText('自有账本读取失败'); + await expect(ledger().getByLabel('筛选账本连接', { exact: true })).toHaveValue('owned-beta'); + await expect(ledger().getByLabel('搜索请求账本', { exact: true })).toHaveValue('owned-no-match'); + await expect(ledger().getByLabel('筛选账本结果', { exact: true })).toHaveValue('failed'); + await shot('owned-read-error-1280', ledger().getByRole('alert')); await page.unroute(pattern); assert.equal(injected, 1); + await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(ledger()).toContainText('当前范围没有请求记录'); + await filter(); await expect(entries()).toHaveCount(50); assert.equal(model.requests.length, 0); await resize(390, 844); await shot('ledger-controls-390', ledger().locator('.mc-ledger-filter')); + mark('Deleted historical connection/status/text filters and complete/empty exports work; a separately injected local read failure recovers without changing history or contacting a model'); + + current = await host.api('/sessions', { projectId: f.project.id }); await host.api('/sessions/' + current.id + '/messages', { content: '自有账本增量验证:只回复已收到,不修改文件或调用工具。' }); + await until(async () => (await host.api('/sessions/' + current.id)).status === 'completed', 30000); assert.equal(model.requests.length, 1); + for (const field of ['max_tokens', 'max_completion_tokens', 'max_output_tokens']) assert.equal(field in model.requests[0].input, false); + await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '1108'); + const row = entries().first(); await row.locator('.model-usage-details > summary').click(); await expect(row).toContainText('300 tokens'); + const responseBytes = await download('actual-local-response.sse', () => row.getByRole('button', { name: '下载已保存的响应', exact: true }).click()); assert.equal(hash(responseBytes), hash(model.responses[0])); + await resize(1280); await shot('new-incremental-receipt-1280', row.locator('.model-usage-details')); + const allBytes = await download('all-1108-complete.ndjson', () => ledger().getByRole('button', { name: '导出当前范围全部记录', exact: true }).click()); const full = inspectExport(allBytes); + assert.equal(full.records.length, 1108); assert.deepEqual(full.records.slice(0, 1107).map(value => value.initial.item), historical); + for (let index = 0; index < 1106; index++) assert.deepEqual(full.records[index].item, historical[index]); assert.equal(full.records[1106].item.status, 'unknown'); + assert.equal(full.records[1107].item.sessionId, current.id); assert.equal(full.records[1107].initial.item.status, 'running'); + beforeRestart = await host.api('/model-center/attempts?limit=2000'); fs.writeFileSync(path.join(directory, 'before-restart.json'), JSON.stringify(beforeRestart, null, 2)); + mark('One actual local SSE request writes the new source-bound receipt, preserves I10 usage, downloads exact response bytes and exports all 1108 current/initial records with count and line SHA'); + + await stop(); await start(); const restored = await host.api('/model-center/attempts?limit=2000'); assert.deepEqual(restored, beforeRestart); + await openLedger(); await resize(1280); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '1108'); await expect(ledger()).toContainText('1,108 条已保存'); + await shot('restart-preserved-1280', ledger().locator('.mc-ledger-filter')); assert.equal(model.requests.length, 1); assert.equal(originalState().databaseVersion, 2); + const config = (await host.api('/bootstrap')).config; assert.equal(config.executionMode, 'unrestricted'); assert.equal(config.capacityMode, 'passthrough'); assert.equal(config.historyPolicy, 'preserve'); + assert.equal((await host.api('/bootstrap')).storage.schemaVersion, 2); + const finalCenter = await host.api('/model-center'); assert.equal(finalCenter.attemptCount, 1108); assert.equal(finalCenter.attempts.length, 100); assert.equal(finalCenter.storage.attemptsRetained, null); + fs.writeFileSync(path.join(directory, 'final-model-center.json'), JSON.stringify(finalCenter, null, 2)); fs.writeFileSync(path.join(directory, 'after-restart.json'), JSON.stringify(restored, null, 2)); + assert.deepEqual(errors, []); assert.deepEqual(externalBlocked, []); mark('Owned host restart retains current/initial SHA, unknown/error history and default passthrough permissions exactly, with zero replay and a complete saved count'); + result = { success: true, appRoot, actualElectron: desktop, checks, screenshots, downloads, pages: pages.length, ...originalState(), errors, externalBlocked, + syntheticRequests: model.requests.length, realModelRequests: 0, realCredentialsRead: 0, productionWrites: 0, viewportWidths: [1280, 390] }; +} catch (cause) { + if (page) { await page.screenshot({ path: path.join(directory, 'failure.png') }).catch(() => {}); fs.writeFileSync(path.join(directory, 'failure.txt'), await page.locator('body').innerText().catch(() => '')); } + result = { success: false, appRoot, actualElectron: desktop, checks, screenshots, downloads, errors, externalBlocked, error: cause.stack, syntheticRequests: model.requests.length, realModelRequests: 0 }; +} finally { + fs.writeFileSync(path.join(directory, 'requests.json'), JSON.stringify(model.requests, null, 2)); await stop(); + fs.cpSync(f.data, path.join(directory, 'retained-owned-data'), { recursive: true, errorOnExist: true }); for (const dispose of disposers.reverse()) await dispose(); + if (desktop) { fs.writeFileSync(path.join(directory, 'desktop-stdout.txt'), desktopStdout); fs.writeFileSync(path.join(directory, 'desktop-stderr.txt'), desktopStderr); } + fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); +} +console.log(JSON.stringify({ directory, ...result })); if (!result.success) process.exitCode = 1; diff --git a/src/client/ModelCenter.tsx b/src/client/ModelCenter.tsx index 0145b2c3..22b5fee3 100644 --- a/src/client/ModelCenter.tsx +++ b/src/client/ModelCenter.tsx @@ -1,13 +1,9 @@ import { useEffect, useRef, useState } from 'react'; import { Activity, Check, CircleHelp, KeyRound, Layers3, Plus, Power, RefreshCw, Route, Server, Settings2 } from 'lucide-react'; -import { api, downloadArtifact } from './api'; +import { api } from './api'; import type { Config, ModelSelection, ModelSnapshot, RuntimeFailure } from './types'; import './ModelCenter.css'; -import { ModelUsageDetails } from './ModelUsageDetails'; -import type { TokenUsage } from '../shared/contracts/model-usage.mjs'; -import { ModelIdentityDetails } from './ModelIdentityDetails'; -import { ModelRoutingDetails, type RoutingDecision } from './ModelRoutingDetails'; -import type { ModelIdentity } from '../shared/contracts/model-identity.mjs'; +import { ModelRequestLedger, type ModelAttempt } from './ModelRequestLedger'; import { ModelWaitSettings, type WaitSettings } from './ModelWaitSettings'; import { NativeParameters } from './NativeParameters'; import { ModelErrorDetails } from './ModelErrorDetails'; @@ -27,9 +23,8 @@ const upstreamCapacity = (value: { model: string; upstreamModel?: CatalogModel } type Connection = { id: string; name: string; brand: string; protocol: string; parameterProfile?: string; messagesAuth?: string; baseUrl: string; model: string; enabled: boolean; revision: number; hasKey: boolean; credentialSource?: { connectionId: string }; upstreamModel?: CatalogModel; requiresKey: boolean; modelIdentityPolicy?: string; modelIdentityAliases?: string[]; modelIdentityAliasSource?: string; nativeParameters?: Record; outputTokenField?: string; streaming?: boolean; capabilityDeclarations?: Record; capabilities?: Record; capabilityEvidence?: { checkedAt: string }; contextLimit: number; maxOutputTokens: number; concurrency: number; status: string; activeRequests: number; savedCredential?: { status: string }; health: { state: string; reason?: string; cooldownUntil?: number } }; type Pool = { id?: string; name: string; members: { connectionId: string; weight: number }[]; strategy: string; sticky: boolean; maxAttempts: number; revision?: number; queueTimeoutMs: number }; -type Attempt = { routingDecision?: RoutingDecision; modelIdentity?: ModelIdentity; diagnostic?: RuntimeFailure; requestUrl?: string; upstreamResponse?: { requestId?: string | null; requestUrl?: string; responseUrl?: string | null; redirectMode?: string }; id: string; connectionId: string; poolId: string | null; model: string; protocol?: string; streaming?: boolean; rejections?: { connectionName: string; capability: string; reason: string }[]; status: string; startedAt: string; ordinal: number; usageStatus: string; reason?: string; taskId?: string; sessionId?: string; runId: string; requestArtifactId?: string; responseArtifactId?: string; responseSha256?: string; responseRedacted?: boolean; stream?: { bytes: number; capturedBytes?: number; frames: number; sha256: string; terminal: boolean }; usage?: TokenUsage | null }; type ImportRecord = { id: string; source: string; path: string; enabled: boolean; lastCheckedAt?: string; lastSyncedAt?: string; lastError?: string; lastResult?: { imported?: number; conflicts?: string[] } }; -type Center = { revision: number; selected: { type: string; id: string }; waitSettings: WaitSettings; connections: Connection[]; pools: Pool[]; imports: ImportRecord[]; attempts: Attempt[]; waiting: number }; +type Center = { revision: number; selected: { type: string; id: string }; waitSettings: WaitSettings; connections: Connection[]; pools: Pool[]; imports: ImportRecord[]; attempts: ModelAttempt[]; waiting: number }; type Operations = { checkedAt: string; version: string; scope: string; activeTasks: number; queue: number; checks: { id: string; title: string; status: string; detail: string }[]; artifacts?: { offset: number; checked: number; total: number; remaining: number; olderOffset: number | null; newerOffset: number | null; results: { id: string; passed: boolean; error?: string }[] } }; type SavedCredential = { id: string; name: string; revision: number; status: string; enabled?: boolean; savedAt?: string; message: string }; @@ -207,7 +202,7 @@ export function ModelCenter({ onConfig, busy, runModel }: { onConfig: (value: Co
模型池成员与权重{connections.map(connection => { const member = pool.members.find(value => value.connectionId === connection.id); return ; })}
} } - {tab === 'attempts' && <>

请求账本

最近100次物理请求;最多保存1000次。未知用量保持未知。

{view.attempts.length} 条记录
{!view.attempts.length &&

当前没有模型中心请求。选择连接或模型池执行任务后,实际尝试会保存在这里。

}
{view.attempts.map(value =>
{states[value.status]}{value.model}

{connections.find(connection => connection.id === value.connectionId)?.name || '未派发'} · {value.protocol ? protocolLabel(value.protocol) : '能力筛选'}{value.streaming ? ' · 流式' : ''} · {value.ordinal ? '尝试 ' + value.ordinal : '未发送'} · {value.usageStatus === 'reported' ? `${value.usage?.prompt_tokens ?? '?'} 输入 / ${value.usage?.completion_tokens ?? '?'} 输出 tokens` : value.usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知'}

Run {value.runId}{value.stream &&

{value.stream.terminal ? "响应完整终结" : "响应未完整,片段保留待核对"} · 已接收 {value.stream.bytes.toLocaleString()} bytes · {value.stream.frames} 个事件{value.stream.capturedBytes !== undefined && value.stream.capturedBytes < value.stream.bytes ? " · 仅保存上限内片段" : ""}

原流 SHA256 {value.stream.sha256}{value.responseSha256 && 工件 SHA256 {value.responseSha256}{value.responseRedacted ? "(已脱敏)" : ""}}{value.sessionId && value.responseArtifactId && }
}{value.requestUrl &&

请求地址:{value.requestUrl}{value.upstreamResponse?.responseUrl && <>
响应地址:{value.upstreamResponse.responseUrl}}

}{value.reason &&

{value.reason}

}{value.diagnostic && }{!value.diagnostic && value.upstreamResponse?.requestId &&

上游请求 ID:{value.upstreamResponse.requestId}

}{value.requestArtifactId && value.sessionId && }{!!value.rejections?.length &&

{value.rejections.map(item => item.connectionName + ':' + (capabilityLabels[item.capability] ?? item.capability) + ' · ' + item.reason).join(';')}

}
)}
} + {tab === 'attempts' && } {tab === 'imports' && <>

已有配置导入

先预览来源和模型,再导入。原客户端配置保持原样。

{view.imports.length > 0 && <>

已登记来源

{view.imports.map(value =>
{value.source}{value.path}{value.enabled ? '每分钟检查' : '自动同步已关闭'}{value.lastCheckedAt &&

最近检查 {new Date(value.lastCheckedAt).toLocaleString()}

}{value.lastError &&

{value.lastError}

}{!!value.lastResult?.conflicts?.length &&

有 {value.lastResult.conflicts.length} 项与手动修改冲突,已保留工作台中的修改。

}
)}} {discovery.map(value =>
{value.source}{value.path || '当前版本尚无可发现的配置路径'}{value.available ? value.supported ? '可预览' : '当前不支持' : '未发现'}{value.detail &&

{value.detail}

}
)} diff --git a/src/client/ModelRequestLedger.css b/src/client/ModelRequestLedger.css new file mode 100644 index 00000000..5d8822cd --- /dev/null +++ b/src/client/ModelRequestLedger.css @@ -0,0 +1,13 @@ +.mc-ledger { min-width: 0; } +.mc-ledger-filter { display: grid; grid-template-columns: minmax(160px, 2fr) minmax(130px, 1fr) minmax(110px, 1fr) auto; align-items: end; gap: 12px; margin: 12px 0; } +.mc-ledger-filter label { display: grid; gap: 6px; min-width: 0; font-size: 12px; color: var(--muted); } +.mc-ledger-filter input, .mc-ledger-filter select { width: 100%; min-width: 0; min-height: 40px; border: 1px solid var(--line, #c6d5d1); border-radius: 6px; background: var(--paper, white); color: var(--ink); padding: 8px; font: inherit; } +.mc-ledger-actions, .mc-ledger-pages { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 12px 0; } +.mc-ledger-actions > span, .mc-ledger-pages > span { color: var(--muted); font-size: 12px; line-height: 1.7; } +.mc-ledger-pages > span { flex: 1; } +.mc-ledger-source { margin: 8px 0; font-size: 12px; line-height: 1.6; overflow-wrap: anywhere; } +.mc-ledger-source > summary { cursor: pointer; } +.mc-ledger-source dl { display: grid; grid-template-columns: 70px minmax(0, 1fr); gap: 6px; } +.mc-ledger-source dd { margin: 0; } +.mc-ledger input:focus-visible, .mc-ledger select:focus-visible, .mc-ledger summary:focus-visible { outline: 2px solid var(--accent, #237a68); outline-offset: 2px; } +@media (max-width: 620px) { .mc-ledger-filter { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; } .mc-ledger-filter > label:first-child { grid-column: 1 / -1; } .mc-ledger-filter > button { grid-column: 1 / -1; } .mc-ledger-actions > span { flex-basis: 100%; } } diff --git a/src/client/ModelRequestLedger.tsx b/src/client/ModelRequestLedger.tsx new file mode 100644 index 00000000..fc24240d --- /dev/null +++ b/src/client/ModelRequestLedger.tsx @@ -0,0 +1,80 @@ +import { useEffect, useState } from 'react'; +import { api, downloadArtifact, downloadBlob, downloadModelLedger } from './api'; +import { ModelUsageDetails } from './ModelUsageDetails'; +import { ModelRoutingDetails, type RoutingDecision } from './ModelRoutingDetails'; +import { ModelIdentityDetails } from './ModelIdentityDetails'; +import { ModelErrorDetails } from './ModelErrorDetails'; +import { protocolLabel } from '../shared/contracts/model-endpoint.mjs'; +import type { ModelIdentity } from '../shared/contracts/model-identity.mjs'; +import type { TokenUsage } from '../shared/contracts/model-usage.mjs'; +import type { RuntimeFailure } from './types'; +import './ModelRequestLedger.css'; + +export type ModelAttempt = { routingDecision?: RoutingDecision; modelIdentity?: ModelIdentity; diagnostic?: RuntimeFailure; requestUrl?: string; + upstreamResponse?: { requestId?: string | null; responseUrl?: string | null }; id: string; connectionId?: string; model?: string; protocol?: string; streaming?: boolean; + rejections?: { connectionName: string; capability: string; reason: string }[]; status: string; startedAt: string; ordinal?: number; usageStatus: string; reason?: string; + taskId?: string; sessionId?: string; runId?: string; requestArtifactId?: string; responseArtifactId?: string; responseSha256?: string; responseRedacted?: boolean; + stream?: { bytes: number; capturedBytes?: number; frames: number; sha256: string; terminal: boolean }; usage?: TokenUsage | null }; +type ReceiptReference = { sequence: number; payloadSha256: string; initialSha256: string; origin: string; legacyIndex?: number }; +type LedgerPage = { items: ModelAttempt[]; references: ReceiptReference[]; total: number; retainedTotal: number; nextCursor: string | null; hasMore: boolean; snapshotSequence: number; connectionIds: string[] }; +const states: Record = { completed: '收到响应', failed: '请求失败', unknown: '结果未知', interrupted: '已中断', running: '请求中', rejected: '未派发' }; +const address = (filters: Record, cursor: string | null = null) => { + const params = new URLSearchParams({ ...filters, limit: '50', ...(cursor ? { cursor } : {}) }); return '/model-center/attempts?' + params.toString(); +}; + +export function ModelRequestLedger({ connections, refreshVersion }: { connections: { id: string; name: string }[]; refreshVersion: unknown }) { + const [page, setPage] = useState(null), [cursor, setCursor] = useState(null), [previous, setPrevious] = useState<(string | null)[]>([]); + const [filters, setFilters] = useState>({}), [query, setQuery] = useState(''), [status, setStatus] = useState(''), [connection, setConnection] = useState(''); + const [historyConnections, setHistoryConnections] = useState([]); + const [loading, setLoading] = useState(false), [working, setWorking] = useState(false), [error, setError] = useState(''), [revision, setRevision] = useState(0); + useEffect(() => { + let alive = true; setLoading(true); setError(''); setPage(null); + api(address(filters, cursor)).then(value => { if (alive) { setPage(value); setHistoryConnections(value.connectionIds); } }).catch(cause => { if (alive) setError(cause.message); }).finally(() => { if (alive) setLoading(false); }); + return () => { alive = false; }; + }, [filters, cursor, revision, refreshVersion]); + async function act(action: () => Promise) { setWorking(true); setError(''); try { await action(); } catch (cause) { setError(cause instanceof Error ? cause.message : '记录未能读取,请刷新核对。'); } finally { setWorking(false); } } + const disabled = working || loading; + function reset() { setCursor(null); setPrevious([]); setRevision(value => value + 1); } + const ledgerConnections = [...connections, ...historyConnections.filter(id => !connections.some(value => value.id === id)).map(id => ({ id, name: id + '(历史连接)' }))]; + function pager(location: string) { return page && ; } + return
+

请求账本

完整保存历史请求;每页展示 50 条,可筛选和导出全部记录。未知用量保持未知。

+ {page ? page.retainedTotal.toLocaleString('zh-CN') + ' 条已保存' : loading ? '正在读取' : '尚未取得记录'}
+
{ event.preventDefault(); setFilters({ ...(query ? { query } : {}), ...(status ? { status } : {}), ...(connection ? { connectionId: connection } : {}) }); reset(); }}> + + + + +
+
+ + {page && {Object.keys(filters).length ? '本次筛选 ' : '当前范围 '}{page.total.toLocaleString('zh-CN')} 条 · 记录按序号分页,进行中的字段仍会更新}
+ {error &&

{error}

} + {!loading && page?.items.length === 0 &&

当前范围没有请求记录。调整筛选不会发送模型请求。

} + {pager('列表上方')} +
{page?.items.map((value, index) => { + const source = page.references[index]; + return
+
{states[value.status] ?? value.status}{value.model ?? '未派发'}
+

{connections.find(connection => connection.id === value.connectionId)?.name || value.connectionId || '未派发'} · {value.protocol ? protocolLabel(value.protocol) : '原记录未保存协议'}{value.streaming ? ' · 流式' : ''} · {value.ordinal ? '尝试 ' + value.ordinal : '未发送'} · {value.usageStatus === 'reported' ? `${value.usage?.prompt_tokens ?? '?'} 输入 / ${value.usage?.completion_tokens ?? '?'} 输出 tokens` : value.usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知'}

+ Run {value.runId ?? '原记录未保存'} +
记录来源与 SHA256

序号 {source.sequence} · {source.origin === 'legacy-metadata' ? '原账本完整迁移,原 metadata 保留' : '本次尝试独立保存'}

+
本条记录
{source.payloadSha256}
初始记录
{source.initialSha256}
+ +
+ {value.stream &&

{value.stream.terminal ? '响应完整终结' : '响应未完整,片段保留待核对'} · 已接收 {value.stream.bytes.toLocaleString()} bytes · {value.stream.frames} 个事件{value.stream.capturedBytes !== undefined && value.stream.capturedBytes < value.stream.bytes ? ' · 仅保存上限内片段' : ''}

原流 SHA256 {value.stream.sha256}
} + {value.responseSha256 && 工件 SHA256 {value.responseSha256}{value.responseRedacted ? '(已脱敏)' : ''}} + {value.sessionId && value.responseArtifactId && } + {value.requestUrl &&

请求地址:{value.requestUrl}{value.upstreamResponse?.responseUrl && <>
响应地址:{value.upstreamResponse.responseUrl}}

} + {value.reason &&

{value.reason}

} + + {value.diagnostic && }{!value.diagnostic && value.upstreamResponse?.requestId &&

上游请求 ID:{value.upstreamResponse.requestId}

} + {value.requestArtifactId && value.sessionId && } + {!!value.rejections?.length &&

{value.rejections.map(item => item.connectionName + ':' + item.capability + ' · ' + item.reason).join(';')}

} +
; + })}
+ {pager('列表下方')} +
; +} diff --git a/src/client/api.ts b/src/client/api.ts index 6cbcb03b..c3c47c69 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -36,6 +36,12 @@ export async function downloadArtifact(sessionId: string, artifactId: string, na } downloadBlob(await response.blob(), name); } +export async function downloadModelLedger(filters: Record) { + const params = new URLSearchParams(filters); + const response = await fetch('/api/model-center/attempts/export' + (params.size ? '?' + params.toString() : ''), { headers: { 'X-Workbench-Token': accessToken } }); + if (!response.ok) { const value = await response.json().catch(() => ({})); throw new Error(value.error || '请求账本导出失败,请保留原记录并刷新核对。'); } + downloadBlob(await response.blob(), 'orven-model-requests.ndjson'); +} export async function downloadResearchDocument(sessionId: string, jobId: string, kind: 'bundle' | 'raw' = 'bundle') { const response = await fetch('/api/sessions/' + sessionId + '/research/jobs/' + jobId + '/download?kind=' + kind, { headers: { 'X-Workbench-Token': accessToken } }); if (!response.ok) { const value = await response.json().catch(() => ({})); throw new Error(value.error || '研究证据下载失败。'); } diff --git a/src/core/models/model-hub.mjs b/src/core/models/model-hub.mjs index 2e1eb28d..e68eb2dd 100644 --- a/src/core/models/model-hub.mjs +++ b/src/core/models/model-hub.mjs @@ -16,6 +16,7 @@ import { modelIdentitySettings } from '../../shared/contracts/model-identity.mjs import { NetworkDiagnostics } from './network-diagnostics.mjs'; import { ModelCatalog, credentialScope } from './model-catalog.mjs'; import { capacitySettings, routedCapacity } from '../../shared/contracts/model-capacity.mjs'; +import { ModelRequestLedger } from './request-ledger.mjs'; const legacyId = 'legacy-default'; const stamp = () => new Date().toISOString(); @@ -34,11 +35,9 @@ export class ModelHub { this.store = store; this.provider = provider; this.state = store.storage.meta('modelRegistry', { schema: 1, revision: 0, selected: { type: 'legacy', id: legacyId }, connections: [], pools: [], imports: {} }); this.health = new Map(); this.leases = new Map(); this.cursors = new Map(); this.sticky = new Map(); this.waiting = 0; - this.attempts = store.storage.meta('modelAttempts', []); + this.ledger = new ModelRequestLedger(store.storage); // A process exit leaves a request outcome unknown; never replay it here. - let recovered = false; - for (const attempt of this.attempts) if (attempt.status === 'running') { attempt.status = 'unknown'; attempt.finishedAt = stamp(); attempt.reason = '宿主重启,请求结果与用量待核对。'; recovered = true; } - if (recovered) this.saveAttempts(); + this.ledger.recoverRunning(); provider.credentials ??= new Map(); if (store.storage.meta('modelCredentials', null)) this.vault().restore(this); } @@ -86,7 +85,27 @@ export class ModelHub { throw cause; } finally { this.editingRegistry = false; } } - saveAttempts() { if (!unrestricted(this.store)) this.attempts = this.attempts.slice(-1000); this.store.storage.transaction(() => this.store.storage.setMeta('modelAttempts', this.attempts)); } + get attempts() { return this.compatAttempts ??= this.ledger.all(); } + set attempts(value) { this.compatAttempts = value; } + adoptRequestLedger() { + if (this.ledger) { Object.setPrototypeOf(this.ledger, ModelRequestLedger.prototype); return this.ledger; } + const retained = Object.getOwnPropertyDescriptor(this, 'attempts'); + if (retained && (!Array.isArray(retained.value) || JSON.stringify(retained.value) !== JSON.stringify(this.store.storage.meta('modelAttempts', [])))) throw error('原内存请求账本与保存来源不同,请先核对,未重放任何请求。', 409); + const ledger = new ModelRequestLedger(this.store.storage); + if (retained) { delete this.attempts; this.compatAttempts = retained.value; } + this.ledger = ledger; return ledger; + } + saveAttempts(attempt = null) { + if (!attempt) return this.ledger.saveAll(this.attempts); + const sequence = this.ledger.save(attempt); + if (this.compatAttempts) { + const positions = this.compatAttempts.map((value, index) => ({ value, index })).filter(({ value }) => value === attempt || this.ledger.positions.get(value) === sequence); + const sameId = positions.length ? positions : this.compatAttempts.map((value, index) => ({ value, index })).filter(({ value }) => value.id === attempt.id); + if (sameId.length === 1) this.compatAttempts[sameId[0].index] = attempt; + } + return sequence; + } + addAttempt(attempt) { this.ledger.save(attempt); this.compatAttempts?.push(attempt); } connections() { const saved = this.state.legacyConnection; return [{ ...saved, id: legacyId, name: saved?.name || this.store.config.model || '未配置模型', brand: saved?.brand || '自定义', protocol: saved?.protocol || 'chat-completions', baseUrl: this.store.config.baseUrl, @@ -144,9 +163,10 @@ export class ModelHub { ...(this.state.capabilityResults?.[connection.id] ? { capabilityEvidence: this.state.capabilityResults[connection.id] } : {}), capabilities: capabilityView({ ...connection, capabilityEvidence: this.state.capabilityResults?.[connection.id] }, credential?.revision ?? 0) }; } - view() { return { revision: this.state.revision, selected: this.state.selected, waitSettings: modelWaitSettings(this.store.config), connections: this.connections().map(value => this.status(value)), pools: this.state.pools, - imports: Object.entries(this.state.imports).map(([id, value]) => ({ id, ...value })), attempts: this.attempts.slice(-100).reverse(), waiting: this.waiting, - storage: { credentials: this.credentialVault ? 'memory-or-windows-user-dpapi' : 'memory', restart: '临时凭据在宿主退出后需要重新填写;已明确本机加密保存的连接按用户与原配置加载。', attemptsRetained: 1000 } }; } + view() { const page = this.ledger.page({ limit: 100 }); return { revision: this.state.revision, selected: this.state.selected, waitSettings: modelWaitSettings(this.store.config), connections: this.connections().map(value => this.status(value)), pools: this.state.pools, + imports: Object.entries(this.state.imports).map(([id, value]) => ({ id, ...value })), attempts: page.items, attemptCount: page.retainedTotal, attemptsHasMore: page.hasMore, waiting: this.waiting, + storage: { credentials: this.credentialVault ? 'memory-or-windows-user-dpapi' : 'memory', restart: '临时凭据在宿主退出后需要重新填写;已明确本机加密保存的连接按用户与原配置加载。', attemptsRetained: null, + attemptStorage: 'incremental-sqlite', attemptPreview: 100, originalMetadataPreserved: true } }; } selectionView() { const selected = this.state.selected; const name = selected.type === 'pool' ? this.state.pools.find(value => value.id === selected.id)?.name @@ -192,7 +212,7 @@ export class ModelHub { const previous = this.state.capabilityResults[id]; const results = previous?.connectionRevision === connection.revision && previous.credentialRevision === member.credentialRevision ? { ...previous.results } : {}; results[scope] = 'verified'; - const attempt = this.attempts.find(value => value.id === attemptId); + const attempt = this.ledger.find(attemptId); if (!attempt || attempt.connectionId !== id || attempt.status !== 'completed') throw error('检测原请求账本不完整,未更新能力结论。'); this.state.capabilityResults[id] = { schema: 1, connectionRevision: connection.revision, credentialRevision: member.credentialRevision, results, checkedAt: stamp(), protocol: modelProtocol(connection.protocol), attemptId: attempt?.id, source: 'explicit-model-probe', @@ -364,9 +384,9 @@ export class ModelHub { try { lease = await this.acquire(routing, runId, excluded, signal, deadline, requirements, { probe: config.capabilityProbe === true }); } catch (cause) { if (!lastError && cause.routingDecision) { - this.attempts.push({ id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null, + this.addAttempt({ id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null, poolId: routing.type === 'pool' ? routing.id : null, status: 'rejected', reason: cause.message, routingDecision: cause.routingDecision, - startedAt: stamp(), finishedAt: stamp(), usage: null, usageStatus: 'not_dispatched' }); this.saveAttempts(); + startedAt: stamp(), finishedAt: stamp(), usage: null, usageStatus: 'not_dispatched' }); } throw lastError ?? cause; } @@ -388,7 +408,7 @@ export class ModelHub { this.store.core.artifact.set(artifact.id, artifact); this.store.save(session); } - this.attempts.push(attempt); this.saveAttempts(); + this.addAttempt(attempt); const progress = event => { if (event.modelIdentity) attempt.modelIdentity = event.modelIdentity; if (event.wireAttemptId) attempt.wireAttemptId = event.wireAttemptId; @@ -398,7 +418,7 @@ export class ModelHub { attempt.usage = event.consumption.usage; attempt.usageStatus = event.consumption.usageStatus; onProgress?.({ consumption: { ...event.consumption, wireAttemptId: event.consumption.attemptId, attemptId: attempt.id, connectionId: member.id, connectionName: member.name }, connectionId: member.id, model: member.model, modelAttemptId: attempt.id }); - this.saveAttempts(); + this.saveAttempts(attempt); return; } if (event.streamCapture !== undefined || event.responseCapture !== undefined) { @@ -456,7 +476,7 @@ export class ModelHub { if (config.capacityMode === 'passthrough' || (!diagnostic?.retryable && !diagnostic?.failoverAllowed) || count === routing.maxAttempts) throw cause; onProgress?.({ phase: 'retrying', attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, modelAttemptId: attempt.id, diagnostic, waitMs: 0, switching: true }); - } finally { attempt.finishedAt = stamp(); lease.release(); this.saveAttempts(); } + } finally { attempt.finishedAt = stamp(); lease.release(); this.saveAttempts(attempt); } } throw lastError ?? new ModelRequestError({ message: '模型池请求未完成。', kind: 'connection' }); } diff --git a/src/core/models/request-ledger.mjs b/src/core/models/request-ledger.mjs new file mode 100644 index 00000000..9dda735b --- /dev/null +++ b/src/core/models/request-ledger.mjs @@ -0,0 +1,190 @@ +import { randomUUID, createHash } from 'node:crypto'; +import { DatabaseSync } from 'node:sqlite'; + +const metadataKey = 'modelAttemptLedger'; +const sha = value => createHash('sha256').update(value).digest('hex'); +const invalid = (message, status = 400) => Object.assign(new Error(message), { status }); +const fields = { connectionId: 'connection_id', sessionId: 'session_id', taskId: 'task_id', runId: 'run_id', status: 'status', model: 'model', protocol: 'protocol' }; +const ownership = value => JSON.stringify([value.id, value.connectionId ?? null, value.poolId ?? null, value.projectId ?? null, value.sessionId ?? null, value.taskId ?? null, value.runId ?? null, value.requestId ?? null, value.model ?? null, value.protocol ?? null]); +const integer = (value, minimum = 0) => Number.isSafeInteger(value) && value >= minimum; +const reference = row => ({ sequence: row.seq, payloadSha256: row.payload_sha256, initialSha256: row.initial_sha256, + origin: row.legacy_position === null ? 'incremental-ledger' : 'legacy-metadata', ...(row.legacy_position === null ? {} : { legacyIndex: row.legacy_position }) }); + +export function ledgerQuery(url, { exportOnly = false } = {}) { + const allowed = [...Object.keys(fields), 'query', ...(exportOnly ? [] : ['cursor', 'limit'])], params = url.searchParams; + for (const key of params.keys()) if (!allowed.includes(key) || params.getAll(key).length !== 1) throw invalid('请求账本查询参数无效。'); + return { ...(exportOnly ? {} : { cursor: params.get('cursor'), limit: params.get('limit') ?? 50 }), + filters: Object.fromEntries([...Object.keys(fields), 'query'].filter(key => params.has(key) && params.get(key) !== '').map(key => [key, params.get(key)])) }; +} + +function where(filters = {}) { + const terms = [], parameters = []; + for (const key of Object.keys(filters).sort()) { + const value = filters[key]; if (typeof value !== 'string' || !value || ![...Object.keys(fields), 'query'].includes(key)) throw invalid('请求账本筛选字段无效。'); + if (key === 'query') { terms.push("payload LIKE ? ESCAPE '\\'"); parameters.push('%' + value.replace(/[\\%_]/g, '\\$&') + '%'); } + else { terms.push(fields[key] + '=?'); parameters.push(value); } + } + return { sql: terms.length ? ' AND ' + terms.join(' AND ') : '', parameters }; +} +function decode(row) { + if (sha(row.payload) !== row.payload_sha256) throw invalid('本机请求账本原记录 SHA256 不匹配,请核对保存来源。', 409); + return JSON.parse(row.payload); +} + +// The legacy metadata stays byte-for-byte in its original row. A one-time +// transaction copies its entries into an indexed table. New requests update +// only their own row; the all-records accessor is compatibility/read-only use. +export class ModelRequestLedger { + constructor(storage) { + this.storage = storage; this.database = storage.database; this.positions = new WeakMap(); this.serialized = new WeakMap(); + const originalVersion = this.database.prepare('PRAGMA user_version').get().user_version; + const state = storage.meta(metadataKey); + if (state && state.schemaVersion !== 1) throw invalid('请求账本由未接入的新版本创建,请使用对应程序。', 409); + if (!state) { + const original = this.database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get()?.value; + const entries = original ? JSON.parse(original) : []; + if (!Array.isArray(entries) || entries.some(value => !value || typeof value !== 'object' || Array.isArray(value) || typeof value.id !== 'string' || !value.id)) throw invalid('原请求账本格式无法完整迁移,原 metadata 已保留。', 409); + this.state = { schemaVersion: 1, id: randomUUID(), legacyEntries: entries.length, legacyMetadataSha256: original ? sha(original) : null, + originalMetadataPreserved: true, migratedAt: new Date().toISOString() }; + storage.transaction(() => { + this.database.exec(`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);`); + for (const [index, entry] of entries.entries()) this.insert(entry, JSON.stringify(entry), index); + storage.setMeta(metadataKey, this.state); + this.database.exec('PRAGMA user_version=2'); + }); + this.initialization = { originalVersion }; + } else { + this.state = state; + if (originalVersion < 2) storage.transaction(() => this.database.exec('PRAGMA user_version=2')); + } + } + insert(value, serialized, legacyIndex = null) { + const result = this.database.prepare(`INSERT INTO model_attempts(id,connection_id,session_id,task_id,run_id,status,model,protocol,legacy_position,payload,payload_sha256,initial_payload,initial_sha256) + VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(value.id, value.connectionId ?? null, value.sessionId ?? null, value.taskId ?? null, value.runId ?? null, + value.status ?? null, value.model ?? null, value.protocol ?? null, legacyIndex, serialized, sha(serialized), serialized, sha(serialized)); + return Number(result.lastInsertRowid); + } + attach(row) { const value = decode(row); this.positions.set(value, row.seq); this.serialized.set(value, row.payload); return value; } + all() { return this.database.prepare('SELECT * FROM model_attempts ORDER BY seq').all().map(row => this.attach(row)); } + find(id) { const rows = this.database.prepare('SELECT * FROM model_attempts WHERE id=?').all(id); if (rows.length > 1) throw invalid('请求账本原身份不唯一,请按序号核对。', 409); return rows[0] ? decode(rows[0]) : undefined; } + rollbackInitialization() { + if (!this.initialization) return false; + // A failed synchronous handler adoption has not dispatched any request. + // Revert only this verified migration, never a ledger with new outcomes. + this.storage.transaction(() => { + const state = this.storage.meta(metadataKey), original = this.database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get()?.value; + const entries = original ? JSON.parse(original) : [], rows = this.database.prepare('SELECT * FROM model_attempts ORDER BY seq').all(); + if (state?.id !== this.state.id || (original ? sha(original) : null) !== this.state.legacyMetadataSha256 || rows.length !== entries.length + || rows.some((row, index) => row.legacy_position !== index || row.payload !== row.initial_payload || row.initial_payload !== JSON.stringify(entries[index]) + || sha(row.payload) !== row.payload_sha256 || sha(row.initial_payload) !== row.initial_sha256)) throw invalid('请求账本已有新记录或来源变化,不能回退原迁移,请保留现场核对。', 409); + this.database.exec('DROP TABLE model_attempts; PRAGMA user_version=' + this.initialization.originalVersion); + this.database.prepare('DELETE FROM metadata WHERE key=?').run(metadataKey); + }); + this.initialization = null; return true; + } + save(value) { return this.saveAll([value])[0]; } + saveAll(values) { + if (values.every(value => value && this.serialized.get(value) === JSON.stringify(value))) return values.map(value => this.positions.get(value)); + const updates = []; + this.storage.transaction(() => { + for (const value of values) { + if (!value || typeof value !== 'object' || typeof value.id !== 'string' || !value.id) throw invalid('请求账本记录需要原尝试身份。'); + const serialized = JSON.stringify(value); if (this.serialized.get(value) === serialized) continue; + const sequence = this.positions.get(value), matches = sequence ? [this.database.prepare('SELECT * FROM model_attempts WHERE seq=?').get(sequence)] + : this.database.prepare('SELECT * FROM model_attempts WHERE id=?').all(value.id); + if (matches.length > 1 || sequence && !matches[0]) throw invalid('请求账本记录身份不唯一或原行已变化,请核对来源。', 409); + let savedSequence; + if (matches[0]) { + const original = decode(matches[0]); + if (this.serialized.has(value) && this.serialized.get(value) !== matches[0].payload) throw invalid('请求账本原记录已更新,请核对保存来源,不能覆盖另一份进展。', 409); + if (ownership(original) !== ownership(value)) throw invalid('请求账本记录不能移动到其他连接、型号、协议、会话或任务。', 409); + this.database.prepare('UPDATE model_attempts SET status=?,payload=?,payload_sha256=? WHERE seq=?').run(value.status ?? null, serialized, sha(serialized), matches[0].seq); + savedSequence = matches[0].seq; + } else savedSequence = this.insert(value, serialized); + updates.push({ value, serialized, sequence: savedSequence }); + } + }); + for (const update of updates) { this.positions.set(update.value, update.sequence); this.serialized.set(update.value, update.serialized); } + return values.map(value => this.positions.get(value)); + } + recoverRunning() { + const running = this.database.prepare("SELECT * FROM model_attempts WHERE status='running' ORDER BY seq").all().map(row => this.attach(row)); + for (const value of running) { value.status = 'unknown'; value.finishedAt = new Date().toISOString(); value.reason = '宿主重启,请求结果与用量待核对。'; } + if (running.length) this.saveAll(running); return running.length; + } + page({ cursor = null, limit = 50, filters = {} } = {}) { + if (!/^\d+$/.test(String(limit)) || !integer(Number(limit), 1)) throw invalid('请求账本每页数量需要正整数。'); + const filtered = where(filters), scope = sha(JSON.stringify({ ledger: this.state.id, filters: Object.fromEntries(Object.entries(filters).sort()) })); + const current = this.database.prepare('SELECT COALESCE(MAX(seq),0) AS seq,COUNT(*) AS total FROM model_attempts').get(); + let snapshot = current.seq, before = null; + if (cursor !== null) { + let value; try { if (typeof cursor !== 'string' || !/^[A-Za-z0-9_-]+$/.test(cursor)) throw new Error(); value = JSON.parse(Buffer.from(cursor, 'base64url').toString()); } catch { throw invalid('请求账本游标无效。'); } + if (value?.schemaVersion !== 1 || value.scope !== scope || !integer(value.snapshot) || !integer(value.before, 1) || value.before > value.snapshot || value.snapshot > current.seq) throw invalid('请求账本游标不属于当前筛选或来源。'); + snapshot = value.snapshot; before = value.before; + if (!this.database.prepare('SELECT seq FROM model_attempts WHERE seq=? AND seq<=?' + filtered.sql).get(before, snapshot, ...filtered.parameters)) throw invalid('请求账本原游标已变化,请刷新记录。', 409); + } + const count = this.database.prepare('SELECT COUNT(*) AS total FROM model_attempts WHERE seq<=?' + filtered.sql).get(snapshot, ...filtered.parameters).total; + const rows = this.database.prepare('SELECT * FROM model_attempts WHERE seq<=?' + (before === null ? '' : ' AND seq Number(limit); + return { schemaVersion: 1, items: shown.map(row => decode(row)), references: shown.map(reference), total: count, retainedTotal: current.total, snapshotSequence: snapshot, + connectionIds: this.database.prepare('SELECT DISTINCT connection_id FROM model_attempts WHERE connection_id IS NOT NULL ORDER BY connection_id').all().map(row => row.connection_id), + hasMore, nextCursor: hasMore ? Buffer.from(JSON.stringify({ schemaVersion: 1, scope, snapshot, before: shown.at(-1).seq })).toString('base64url') : null, + filters, source: this.state, viewSnapshot: 'sequence-watermark; running record fields can still update' }; + } + record(sequence) { + if (!/^\d+$/.test(String(sequence)) || !integer(Number(sequence), 1)) throw invalid('请求账本序号无效。'); + const row = this.database.prepare('SELECT * FROM model_attempts WHERE seq=?').get(Number(sequence)); if (!row) throw invalid('请求账本原记录不存在。', 404); + if (sha(row.initial_payload) !== row.initial_sha256) throw invalid('请求账本初始来源 SHA256 不匹配。', 409); + return { item: decode(row), reference: reference(row), initial: { item: JSON.parse(row.initial_payload), sha256: row.initial_sha256 } }; + } + exportSnapshot(filters = {}) { + const filtered = where(filters), database = new DatabaseSync(this.storage.filename, { readOnly: true }); + try { + database.exec('PRAGMA query_only=ON; PRAGMA busy_timeout=5000; BEGIN;'); + const snapshot = database.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM model_attempts').get().seq; + const count = database.prepare('SELECT COUNT(*) AS total FROM model_attempts WHERE seq<=?' + filtered.sql).get(snapshot, ...filtered.parameters).total; + const rows = database.prepare('SELECT * FROM model_attempts WHERE seq<=?' + filtered.sql + ' ORDER BY seq').iterate(snapshot, ...filtered.parameters); + return { header: { schemaVersion: 1, type: 'header', format: 'orven-model-attempts-ndjson', snapshotSequence: snapshot, expectedRecords: count, + filters, source: this.state, recordedAt: new Date().toISOString(), snapshot: 'SQLite read transaction', includesInitialReceipts: true, artifactBodies: 'Original artifacts remain separately downloadable by their saved session/ID/SHA.' }, rows, + close() { try { rows.return?.(); database.exec('ROLLBACK'); } finally { database.close(); } } }; + } catch (cause) { database.close(); throw cause; } + } +} + +export async function streamLedgerExport(response, snapshot) { + const checksum = createHash('sha256'); let count = 0; + const write = async line => { + if (response.destroyed || response.writableEnded) throw invalid('请求账本导出连接已关闭。', 499); + // ServerResponse can report backpressure after its socket has already + // flushed, without emitting another drain. The chunk callback reports + // actual write completion and bounds queued data to this one record. + await new Promise((resolve, reject) => { + let settled = false; + const clear = () => { response.off('close', closed); response.off('error', failed); }; + const finish = error => { if (settled) return; settled = true; clear(); error ? reject(error) : resolve(); }; + const closed = () => finish(invalid('请求账本导出被中断,部分内容不能当作完整导出。', 499)), failed = error => finish(error); + response.once('close', closed); response.once('error', failed); + try { response.write(line, finish); } catch (cause) { finish(cause); } + }); + }; + try { + await write(JSON.stringify(snapshot.header) + '\n'); + for (const row of snapshot.rows) { + decode(row); if (sha(row.initial_payload) !== row.initial_sha256) throw invalid('请求账本初始来源 SHA256 不匹配。', 409); + const line = '{"type":"attempt","reference":' + JSON.stringify(reference(row)) + ',"item":' + row.payload + + ',"initial":{"sha256":' + JSON.stringify(row.initial_sha256) + ',"item":' + row.initial_payload + '}}\n'; + checksum.update(line); await write(line); count++; + } + if (count !== snapshot.header.expectedRecords) throw invalid('请求账本导出数量与快照不匹配。', 409); + await write(JSON.stringify({ type: 'footer', complete: true, records: count, recordLinesSha256: checksum.digest('hex') }) + '\n'); response.end(); + } finally { snapshot.close(); } +} diff --git a/src/core/storage/state-database.mjs b/src/core/storage/state-database.mjs index 75173d08..52dcc23e 100644 --- a/src/core/storage/state-database.mjs +++ b/src/core/storage/state-database.mjs @@ -1,9 +1,9 @@ import fs from 'node:fs'; import path from 'node:path'; import { DatabaseSync } from 'node:sqlite'; -import { randomUUID } from 'node:crypto'; +import { randomUUID, createHash } from 'node:crypto'; -const DATABASE_SCHEMA = 1; +const DATABASE_SCHEMA = 2; export class StateDatabase { constructor(directory, { faultInjector } = {}) { @@ -27,7 +27,7 @@ export class StateDatabase { task_id TEXT, run_id TEXT, type TEXT NOT NULL, revision INTEGER NOT NULL, created_at TEXT NOT NULL, payload TEXT NOT NULL CHECK(json_valid(payload))); CREATE INDEX IF NOT EXISTS events_session ON events(session_id,seq); CREATE TABLE IF NOT EXISTS outbox (seq INTEGER PRIMARY KEY REFERENCES events(seq), published_at TEXT); - PRAGMA user_version=${DATABASE_SCHEMA}; + PRAGMA user_version=${Math.max(version, 1)}; `); } catch (error) { this.database.close(); throw error; } } @@ -39,6 +39,14 @@ export class StateDatabase { } meta(key, fallback = null) { + // Legacy in-process readers receive the full ledger projection. The + // original metadata row remains unchanged for archived source verification. + if (key === 'modelAttempts' && this.database.prepare("SELECT value FROM metadata WHERE key='modelAttemptLedger'").get()) { + return this.database.prepare('SELECT payload,payload_sha256 FROM model_attempts ORDER BY seq').all().map(row => { + if (createHash('sha256').update(row.payload).digest('hex') !== row.payload_sha256) throw new Error('本机请求账本原记录 SHA256 不匹配,请核对保存来源。'); + return JSON.parse(row.payload); + }); + } const row = this.database.prepare('SELECT value FROM metadata WHERE key=?').get(key); return row ? JSON.parse(row.value) : structuredClone(fallback); } diff --git a/src/server/app.mjs b/src/server/app.mjs index fd256ae1..d407ebc6 100644 --- a/src/server/app.mjs +++ b/src/server/app.mjs @@ -26,6 +26,7 @@ import { nativeParameterSettings } from '../shared/contracts/native-parameters.m import { localDirectories } from './local-directories.mjs'; import { readInstallationProgress } from '../core/desktop/installation-progress.mjs'; import { recordPageQuery } from '../shared/contracts/record-page.mjs'; +import { ledgerQuery, streamLedgerExport } from '../core/models/request-ledger.mjs'; export function createSessionPublisher(clients) { return session => { @@ -204,6 +205,14 @@ return async (request, response) => { const hub = provider.modelHub(store); const importer = () => hub.importer ??= new ConfigImporter(hub); if (method === 'GET' && route === '/api/model-center') return json(response, hub.view()); + if (method === 'GET' && route === '/api/model-center/attempts') return json(response, hub.ledger.page(ledgerQuery(url))); + if (method === 'GET' && route === '/api/model-center/attempts/export') { + const snapshot = hub.ledger.exportSnapshot(ledgerQuery(url, { exportOnly: true }).filters); + response.writeHead(200, { 'Content-Type': 'application/x-ndjson; charset=utf-8', 'Content-Disposition': 'attachment; filename="orven-model-requests.ndjson"', 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' }); + await streamLedgerExport(response, snapshot); return; + } + const attemptRecord = route.match(/^\/api\/model-center\/attempts\/([0-9]+)$/); + if (method === 'GET' && attemptRecord) return json(response, hub.ledger.record(attemptRecord[1])); if (method === 'GET' && route === '/api/model-center/network-diagnostics') return json(response, hub.networkDiagnostics().view()); const networkStop = route.match(/^\/api\/model-center\/network-diagnostics\/([a-f0-9-]{36})\/stop$/i); if (method === 'POST' && networkStop) return json(response, await hub.networkDiagnostics().stop(networkStop[1], await body(request))); diff --git a/src/server/hot-upgrade.mjs b/src/server/hot-upgrade.mjs index a7872327..ad8f79ea 100644 --- a/src/server/hot-upgrade.mjs +++ b/src/server/hot-upgrade.mjs @@ -12,6 +12,7 @@ import { APP_VERSION } from './diagnostics.mjs'; import { hostAuthority } from '../core/desktop/host-authority.mjs'; import { CredentialVault } from '../core/models/credential-vault.mjs'; import { NetworkDiagnostics } from '../core/models/network-diagnostics.mjs'; +import { StateDatabase } from '../core/storage/state-database.mjs'; // Keep the existing objects and their in-memory connection. No credential is // extracted, transferred, returned, or persisted by this operation. @@ -34,11 +35,14 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true const oldAuthorityPrototype = oldAuthority ? Object.getPrototypeOf(oldAuthority) : null; const oldPrototypes = [Object.getPrototypeOf(store), Object.getPrototypeOf(provider), Object.getPrototypeOf(runtime)]; const oldModelPrototypes = { hub: provider.hub ? Object.getPrototypeOf(provider.hub) : null, + ledger: provider.hub?.ledger ? Object.getPrototypeOf(provider.hub.ledger) : null, importer: provider.hub?.importer ? Object.getPrototypeOf(provider.hub.importer) : null, vault: provider.hub?.credentialVault ? Object.getPrototypeOf(provider.hub.credentialVault) : null, network: provider.hub?.networkChecks ? Object.getPrototypeOf(provider.hub.networkChecks) : null, catalog: provider.hub?.catalog ? Object.getPrototypeOf(provider.hub.catalog) : null }; const oldStoreProperties = { ...store }; + const oldHubProperties = provider.hub ? { ...provider.hub } : null, oldStoragePrototype = store.storage ? Object.getPrototypeOf(store.storage) : null; + let initializedLedger = null; const oldRuntimeProperties = { filePreviews: runtime.filePreviews, browsing: runtime.browsing, previews: runtime.previews, evaluations:runtime.evaluations, environments: runtime.environments, appDirectory: runtime.appDirectory, workers: runtime.workers, controlledEngines: runtime.controlledEngines, checks: runtime.checks, deliveries: runtime.deliveries, codeIndex: runtime.codeIndex, changeSets: runtime.changeSets, contextPlans: runtime.contextPlans, workspaces: runtime.workspaces, research: runtime.research, engines: runtime.engines, browser: runtime.browser, taskPlans: runtime.taskPlans, projectMemories: runtime.projectMemories, taskCheckpoints: runtime.taskCheckpoints, skills: runtime.skills, mcp: runtime.mcp }; const oldUpdates = store.listeners('update'); const oldInputs = runtime.inputs; @@ -64,11 +68,13 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true runtime.results = null; runtime.resumes = null; store.ensureStorage(); + Object.setPrototypeOf(store.storage, StateDatabase.prototype); // A SQLite host can retain an artifact object from an earlier bundle. // Reopen the same blobs through the new API without mutating that object; // oldStoreProperties restores it if installing this handler fails. store.artifactStore = new ArtifactStore(store.directory); store.claimRuntime(); + if (provider.hub) { const retainedLedger = provider.hub.ledger; provider.hub.adoptRequestLedger(); if (!retainedLedger) initializedLedger = provider.hub.ledger; } store.removeAllListeners('update'); store.on('update', createSessionPublisher(clients)); server.removeAllListeners('request'); server.on('request', handler); server.prependOnceListener('close', closeStorage); @@ -79,6 +85,7 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true void oldRuntimeProperties.filePreviews?.stopAll(); return { version: APP_VERSION, pid: process.pid, hasKey: provider.hasKey(), projects: store.projects.length, sessions: store.sessions.size, appDirectory: root }; } catch (error) { + initializedLedger?.rollbackInitialization(); if (oldCompanion) server.workbenchCompanion = oldCompanion; else delete server.workbenchCompanion; if (oldAuthority) { for (const key of Object.keys(oldAuthority)) if (!Object.hasOwn(oldAuthorityProperties, key)) delete oldAuthority[key]; @@ -98,7 +105,10 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true if (oldInputs === undefined) delete runtime.inputs; else runtime.inputs = oldInputs; if (oldResumes === undefined) delete runtime.resumes; else runtime.resumes = oldResumes; Object.setPrototypeOf(store, oldPrototypes[0]); Object.setPrototypeOf(provider, oldPrototypes[1]); Object.setPrototypeOf(runtime, oldPrototypes[2]); + if (oldStoragePrototype && store.storage === oldStoreProperties.storage) Object.setPrototypeOf(store.storage, oldStoragePrototype); + if (oldHubProperties) { for (const key of Object.keys(provider.hub)) if (!Object.hasOwn(oldHubProperties, key)) delete provider.hub[key]; Object.assign(provider.hub, oldHubProperties); } if (oldModelPrototypes.hub) Object.setPrototypeOf(provider.hub, oldModelPrototypes.hub); + if (oldModelPrototypes.ledger) Object.setPrototypeOf(provider.hub.ledger, oldModelPrototypes.ledger); if (oldModelPrototypes.importer) Object.setPrototypeOf(provider.hub.importer, oldModelPrototypes.importer); if (oldModelPrototypes.vault) Object.setPrototypeOf(provider.hub.credentialVault, oldModelPrototypes.vault); if (oldModelPrototypes.network) Object.setPrototypeOf(provider.hub.networkChecks, oldModelPrototypes.network); diff --git a/src/server/operations.mjs b/src/server/operations.mjs index 822669bb..fa7d5c8c 100644 --- a/src/server/operations.mjs +++ b/src/server/operations.mjs @@ -51,7 +51,7 @@ export function inspectOperations({ store, provider, runtime, server, appDirecto for (const directory of [...new Set([path.resolve(appDirectory), path.resolve(store.directory)])]) { const value = fs.statfsSync(directory, { bigint: true }), bytes = value.bavail * value.bsize; disk.push({ directory, freeBytes: bytes.toString(), status: bytes < 64n * 1024n * 1024n ? 'attention' : 'passed' }); } return { status: disk.some(value => value.status === 'attention') ? 'attention' : 'passed', detail: disk.map(value => value.directory + ' · 剩余' + Number(BigInt(value.freeBytes) / (1024n * 1024n)).toLocaleString() + ' MB').join(';') }; }); - return { version, checkedAt: new Date().toISOString(), checks, activeTasks: runtime.active.size, queue: center.waiting, modelAttempts: center.attempts.length, modelRequests: 0, + return { version, checkedAt: new Date().toISOString(), checks, activeTasks: runtime.active.size, queue: center.waiting, modelAttempts: center.attemptCount, modelRequests: 0, artifacts, activity, unknown: { total: unknown.length, items: unknown.slice(0, 100), omitted: Math.max(0, unknown.length - 100) }, sources, disk, scope: '本地只读巡检:数据库quick_check、明确分页的原工件、现有管理器与待核对归属、当前程序/已登记来源及磁盘余量;不发送模型请求、不执行项目命令或自动修复。一次分页通过不代表全部历史工件已核对。' }; } diff --git a/src/server/store.mjs b/src/server/store.mjs index c995be78..389e38b1 100644 --- a/src/server/store.mjs +++ b/src/server/store.mjs @@ -747,7 +747,7 @@ export class Store extends EventEmitter { get eventCursor() { return this.storage.cursor; } eventsAfter(cursor, options) { return this.storage.eventsAfter(cursor, options); } storageInfo() { - return { engine: 'sqlite', schemaVersion: 1, eventCursor: this.eventCursor, pendingPublications: this.storage.pendingPublications(), + return { engine: 'sqlite', schemaVersion: this.storage.database.prepare('PRAGMA user_version').get().user_version, eventCursor: this.eventCursor, pendingPublications: this.storage.pendingPublications(), migratedFromJson: !!this.storage.meta('migration'), recovery: 'verify-unknown-results', compatibilityExport: 'explicit-json' }; } diff --git a/tests/fixtures/legacy-request-ledger/SOURCE.json b/tests/fixtures/legacy-request-ledger/SOURCE.json new file mode 100644 index 00000000..64f47753 --- /dev/null +++ b/tests/fixtures/legacy-request-ledger/SOURCE.json @@ -0,0 +1,8 @@ +{ + "base": "a93bfd6885decdbfe33c8f16c9b16771984599cb", + "sourcePath": "src/core/storage/state-database.mjs", + "bytes": 8565, + "sha256": "2a38f7564df44c4ee5183d850064be1d46fba7e072e2b87f73afb8e1a85a1517", + "gitBlob": "75173d0872981f5eebaeca371017e3f50fe135a5", + "scope": "Exact previous committed storage source, used only with owned test data to verify schema compatibility and failed handler migration rollback." +} diff --git a/tests/fixtures/legacy-request-ledger/state-database.mjs b/tests/fixtures/legacy-request-ledger/state-database.mjs new file mode 100644 index 00000000..75173d08 --- /dev/null +++ b/tests/fixtures/legacy-request-ledger/state-database.mjs @@ -0,0 +1,130 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { DatabaseSync } from 'node:sqlite'; +import { randomUUID } from 'node:crypto'; + +const DATABASE_SCHEMA = 1; + +export class StateDatabase { + constructor(directory, { faultInjector } = {}) { + this.filename = path.join(directory, 'workbench.sqlite'); + this.faultInjector = faultInjector; + this.database = new DatabaseSync(this.filename); + try { + const version = this.database.prepare('PRAGMA user_version').get().user_version; + if (version > DATABASE_SCHEMA) throw new Error('任务数据库由更新版本创建,请使用对应版本打开。'); + this.database.exec('PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000;'); + this.database.exec(` + CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT NOT NULL CHECK(json_valid(value))); + CREATE TABLE IF NOT EXISTS projects (id TEXT PRIMARY KEY, position INTEGER NOT NULL, payload TEXT NOT NULL CHECK(json_valid(payload))); + CREATE TABLE IF NOT EXISTS sessions (id TEXT PRIMARY KEY, project_id TEXT NOT NULL REFERENCES projects(id), revision INTEGER NOT NULL, payload TEXT NOT NULL CHECK(json_valid(payload))); + CREATE TABLE IF NOT EXISTS records ( + kind TEXT NOT NULL, id TEXT NOT NULL, session_id TEXT NOT NULL REFERENCES sessions(id), project_id TEXT NOT NULL REFERENCES projects(id), + task_id TEXT, run_id TEXT, status TEXT, payload TEXT NOT NULL CHECK(json_valid(payload)), PRIMARY KEY(kind,id)); + CREATE INDEX IF NOT EXISTS records_owner ON records(kind,session_id,task_id,run_id); + CREATE TABLE IF NOT EXISTS events ( + seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL UNIQUE, session_id TEXT NOT NULL REFERENCES sessions(id), project_id TEXT NOT NULL, + task_id TEXT, run_id TEXT, type TEXT NOT NULL, revision INTEGER NOT NULL, created_at TEXT NOT NULL, payload TEXT NOT NULL CHECK(json_valid(payload))); + CREATE INDEX IF NOT EXISTS events_session ON events(session_id,seq); + CREATE TABLE IF NOT EXISTS outbox (seq INTEGER PRIMARY KEY REFERENCES events(seq), published_at TEXT); + PRAGMA user_version=${DATABASE_SCHEMA}; + `); + } catch (error) { this.database.close(); throw error; } + } + + transaction(action) { + this.database.exec('BEGIN IMMEDIATE'); + try { const result = action(); this.faultInjector?.('before_commit'); this.database.exec('COMMIT'); return result; } + catch (error) { try { this.database.exec('ROLLBACK'); } catch {} throw error; } + } + + meta(key, fallback = null) { + const row = this.database.prepare('SELECT value FROM metadata WHERE key=?').get(key); + return row ? JSON.parse(row.value) : structuredClone(fallback); + } + setMeta(key, value) { + this.database.prepare('INSERT INTO metadata(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value').run(key, JSON.stringify(value)); + } + get initialized() { return this.meta('initialized', false); } + get cursor() { return this.database.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM events').get().seq; } + + writeProjects(projects) { + const statement = this.database.prepare('INSERT INTO projects(id,position,payload) VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET position=excluded.position,payload=excluded.payload'); + projects.forEach((project, index) => statement.run(project.id, index, JSON.stringify(project))); + } + writeSession(session) { + const existing = this.database.prepare('SELECT project_id FROM sessions WHERE id=?').get(session.id); + if (existing && existing.project_id !== session.projectId) throw new Error('会话不能移动到其他项目。'); + this.database.prepare('INSERT INTO sessions(id,project_id,revision,payload) VALUES(?,?,?,?) ON CONFLICT(id) DO UPDATE SET revision=excluded.revision,payload=excluded.payload').run( + session.id, session.projectId, session.revision ?? 0, JSON.stringify(session)); + } + writeRecords(records) { + const statement = this.database.prepare(`INSERT INTO records(kind,id,session_id,project_id,task_id,run_id,status,payload) VALUES(?,?,?,?,?,?,?,?) + ON CONFLICT(kind,id) DO UPDATE SET status=excluded.status,payload=excluded.payload`); + for (const record of records) { + const existing = this.database.prepare('SELECT session_id,project_id,task_id,run_id FROM records WHERE kind=? AND id=?').get(record.kind, record.id); + if (existing && (existing.session_id !== record.sessionId || existing.project_id !== record.projectId || existing.task_id !== record.taskId || existing.run_id !== record.runId)) { + throw new Error('持久执行记录不能移动到其他任务或项目。'); + } + statement.run(record.kind, record.id, record.sessionId, record.projectId, record.taskId, record.runId, record.status, JSON.stringify(record.value)); + } + } + appendEvent(session, type, payload) { + const entry = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, + taskId: session.taskId ?? null, runId: session.runId ?? null, revision: session.revision ?? 0, + type, createdAt: new Date().toISOString(), payload }; + const result = this.database.prepare('INSERT INTO events(id,session_id,project_id,task_id,run_id,type,revision,created_at,payload) VALUES(?,?,?,?,?,?,?,?,?)').run( + entry.id, entry.sessionId, entry.projectId, entry.taskId, entry.runId, type, entry.revision, entry.createdAt, JSON.stringify(payload)); + entry.seq = Number(result.lastInsertRowid); + this.database.prepare('INSERT INTO outbox(seq) VALUES(?)').run(entry.seq); + return entry; + } + + initialize({ projects, config, sessions, records, migration }) { + return this.transaction(() => { + this.writeProjects(projects); this.setMeta('config', config); + for (const session of sessions) { + this.writeSession(session); + session.eventSeq = this.appendEvent(session, 'session.imported', { status: session.status, source: migration ? 'json-v1' : 'new' }).seq; + this.writeSession(session); + } + this.writeRecords(records); this.faultInjector?.('migration_records'); + this.setMeta('migration', migration); this.setMeta('initialized', true); + }); + } + + save(session, records, type, payload) { + return this.transaction(() => { + this.writeSession(session); this.writeRecords(records); this.faultInjector?.('after_state'); + const event = this.appendEvent(session, type, payload); + session.eventSeq = event.seq; this.writeSession(session); this.faultInjector?.('after_event'); + return event; + }); + } + + load() { + return { + projects: this.database.prepare('SELECT payload FROM projects ORDER BY position').all().map(row => JSON.parse(row.payload)), + config: this.meta('config', {}), + sessions: this.database.prepare('SELECT payload FROM sessions').all().map(row => JSON.parse(row.payload)), + records: this.database.prepare('SELECT kind,payload FROM records').all().map(row => ({ kind: row.kind, value: JSON.parse(row.payload) })), + }; + } + eventsAfter(cursor = 0, { sessionId = null, limit = 100 } = {}) { + if (!Number.isSafeInteger(cursor) || cursor < 0 || !Number.isInteger(limit) || limit < 1 || limit > 500) throw new Error('事件游标或分页范围无效。'); + const rows = sessionId + ? this.database.prepare('SELECT * FROM events WHERE seq>? AND session_id=? ORDER BY seq LIMIT ?').all(cursor, sessionId, limit + 1) + : this.database.prepare('SELECT * FROM events WHERE seq>? ORDER BY seq LIMIT ?').all(cursor, limit + 1); + const events = rows.slice(0, limit).map(row => ({ seq: row.seq, id: row.id, sessionId: row.session_id, projectId: row.project_id, + taskId: row.task_id, runId: row.run_id, revision: row.revision, type: row.type, createdAt: row.created_at, payload: JSON.parse(row.payload) })); + return { events, nextCursor: events.at(-1)?.seq ?? cursor, hasMore: rows.length > limit, currentCursor: this.cursor }; + } + published(seq) { this.database.prepare('UPDATE outbox SET published_at=? WHERE seq=?').run(new Date().toISOString(), seq); } + pendingPublications() { return this.database.prepare('SELECT COUNT(*) AS count FROM outbox WHERE published_at IS NULL').get().count; } + close() { if (this.database) { this.database.close(); this.database = null; } } +} + +export function runtimeOwnerAlive(owner) { + if (!owner?.pid || owner.pid === process.pid) return false; + try { process.kill(owner.pid, 0); return true; } catch (error) { return error.code === 'EPERM'; } +} diff --git a/tests/fixtures/request-ledger-data.mjs b/tests/fixtures/request-ledger-data.mjs new file mode 100644 index 00000000..948ce7b8 --- /dev/null +++ b/tests/fixtures/request-ledger-data.mjs @@ -0,0 +1,23 @@ +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +export const hash = bytes => createHash('sha256').update(bytes).digest('hex'); +export function historicalAttempts(count = 1107) { + return Array.from({ length: count }, (_, index) => ({ id: 'owned-history-' + index, connectionId: index % 2 ? 'owned-beta' : 'owned-alpha', + model: 'owned-history-model-' + (index % 3), protocol: 'chat-completions', runId: 'owned-run-' + index, taskId: 'owned-task-' + Math.floor(index / 3), + sessionId: 'owned-session-' + Math.floor(index / 10), projectId: 'owned-project', ordinal: 1, status: index % 7 ? 'completed' : 'failed', + startedAt: '2026-11-01T00:00:00Z', finishedAt: '2026-11-01T00:00:01Z', ...(index % 7 ? {} : { reason: '历史上游原错误 HTTP 400 · 未自动重发' }), + usageStatus: index % 5 ? 'reported' : 'unknown', usage: index % 5 ? { prompt_tokens: index, completion_tokens: 10, total_tokens: index + 10, + protocol: 'chat-completions', raw: { prompt_tokens: index, completion_tokens: 10, total_tokens: index + 10, prompt_cache_hit_tokens: 0, custom_old: ['原字段', index] } } : null })); +} +export function inspectExport(bytes) { + const lines = Buffer.isBuffer(bytes) ? bytes.toString('utf8').split('\n') : bytes.split('\n'); assert.equal(lines.pop(), ''); + const values = lines.map(line => JSON.parse(line)), header = values.shift(), footer = values.pop(); + assert.equal(header.format, 'orven-model-attempts-ndjson'); assert.equal(header.type, 'header'); assert.equal(footer.type, 'footer'); assert.equal(footer.complete, true); + assert.equal(header.expectedRecords, values.length); assert.equal(footer.records, values.length); + assert.equal(footer.recordLinesSha256, hash(lines.slice(1, -1).map(line => line + '\n').join(''))); + for (const value of values) { + assert.equal(value.type, 'attempt'); assert.equal(hash(JSON.stringify(value.item)), value.reference.payloadSha256); + assert.equal(hash(JSON.stringify(value.initial.item)), value.reference.initialSha256); assert.equal(value.initial.sha256, value.reference.initialSha256); + } + return { header, footer, records: values }; +} diff --git a/tests/request-ledger.test.mjs b/tests/request-ledger.test.mjs new file mode 100644 index 00000000..019b67f5 --- /dev/null +++ b/tests/request-ledger.test.mjs @@ -0,0 +1,192 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import http from 'node:http'; +import { EventEmitter } from 'node:events'; +import { fixture, until, workspace } from './helpers.mjs'; +import { Provider } from '../src/server/provider.mjs'; +import { Runtime } from '../src/server/runtime.mjs'; +import { Store } from '../src/server/store.mjs'; +import { createRequestHandler } from '../src/server/app.mjs'; +import { ModelRequestLedger, ledgerQuery, streamLedgerExport } from '../src/core/models/request-ledger.mjs'; +import { usageSamples, usageModelId, cacheUsageFixture } from './fixtures/cache-usage-model.mjs'; +import { historicalAttempts, inspectExport, hash } from './fixtures/request-ledger-data.mjs'; +import { StateDatabase as LegacyStateDatabase } from './fixtures/legacy-request-ledger/state-database.mjs'; +import { ModelHub } from '../src/core/models/model-hub.mjs'; +import { installUpgrade } from '../src/server/hot-upgrade.mjs'; +const originalMeta = store => store.storage.database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get()?.value; +const provider = () => Object.assign(Object.create(Provider.prototype), { apiKey: '' }); +const owned = (id, status = 'completed') => ({ id, connectionId: 'owned-alpha', model: 'owned-new', runId: id, status, usageStatus: 'unknown', usage: null, startedAt: '2026-11-07T00:00:00Z' }); +class Response extends EventEmitter { + constructor(onWrite = () => {}) { super(); this.chunks = []; this.onWrite = onWrite; } + write(value, callback) { this.chunks.push(value); const writable = this.onWrite(value) !== false; if (writable) queueMicrotask(() => callback?.()); return writable; } + end() { this.writableEnded = true; } +} + +test('legacy >1000 entries migrate exactly in both permission modes; metadata is unchanged and lazy normal reads do not load all receipts', t => { + for (const modernDefaults of [true, false]) { + const f = fixture(t, { modernDefaults }), rows = historicalAttempts(); f.store.storage.setMeta('modelAttempts', rows); const original = originalMeta(f.store); + const hub = provider().modelHub(f.store); assert.equal(hub.compatAttempts, undefined); + assert.equal(hub.view().attemptCount, rows.length); assert.equal(hub.view().attempts.length, 100); assert.equal(hub.view().storage.attemptsRetained, null); + assert.equal(hub.compatAttempts, undefined); assert.equal(originalMeta(f.store), original); assert.deepEqual(f.store.storage.meta('modelAttempts'), rows); + assert.equal(hub.ledger.state.legacyMetadataSha256, hash(original)); + const result = hub.ledger.page({ limit: 2000 }); assert.deepEqual(result.items, [...rows].reverse()); assert.equal(result.hasMore, false); + assert.equal(result.references.at(-1).legacyIndex, 0); assert.equal(hub.ledger.record(1).initial.sha256, hash(JSON.stringify(rows[0]))); + } +}); + +test('ordinary progress writes only one current receipt, keeps older payload SHA unchanged and rolls back injected commit failures', t => { + const f = fixture(t, { modernDefaults: true }), rows = historicalAttempts(); f.store.storage.setMeta('modelAttempts', rows); + const hub = provider().modelHub(f.store), original = originalMeta(f.store), first = hub.ledger.record(1); + f.store.storage.database.exec('CREATE TEMP TABLE owned_ledger_writes(seq INTEGER,bytes INTEGER); CREATE TEMP TRIGGER owned_ledger_update AFTER UPDATE ON model_attempts BEGIN INSERT INTO owned_ledger_writes VALUES(new.seq,length(new.payload)); END;'); + const current = owned('owned-current', 'running'); hub.addAttempt(current); current.status = 'completed'; current.reason = '只改当前记录'; hub.saveAttempts(current); + assert.equal(hub.compatAttempts, undefined); assert.equal(originalMeta(f.store), original); assert.deepEqual(hub.ledger.record(1), first); + const writes = f.store.storage.database.prepare('SELECT * FROM owned_ledger_writes').all(); assert.equal(writes.length, 1); assert.equal(writes[0].seq, 1108); + const final = hub.ledger.record(1108); current.reason = '未提交修改'; f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('owned ledger commit failure'); }; + assert.throws(() => hub.saveAttempts(current), /owned ledger commit failure/); assert.deepEqual(hub.ledger.record(1108), final); + f.store.storage.faultInjector = null; hub.saveAttempts(current); assert.equal(hub.ledger.record(1108).item.reason, '未提交修改'); assert.equal(hub.ledger.record(1108).initial.item.status, 'running'); + assert.equal(f.store.storage.database.prepare('SELECT COUNT(*) AS n FROM owned_ledger_writes').get().n, 2); +}); + +test('migration failure is atomic, malformed old metadata stays original, duplicate legacy IDs keep separate origins and recovery never replays', t => { + const f = fixture(t, { modernDefaults: true }), rows = [owned('owned-duplicate'), owned('owned-duplicate', 'running')]; f.store.storage.setMeta('modelAttempts', rows); const original = originalMeta(f.store); + f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('owned migration failure'); }; + assert.throws(() => new ModelRequestLedger(f.store.storage), /owned migration failure/); assert.equal(originalMeta(f.store), original); assert.equal(f.store.storage.meta('modelAttemptLedger'), null); + f.store.storage.faultInjector = null; const hub = provider().modelHub(f.store); assert.equal(hub.view().attemptCount, 2); + assert.equal(hub.ledger.record(2).item.status, 'unknown'); assert.deepEqual(hub.ledger.record(2).initial.item, rows[1]); assert.equal(originalMeta(f.store), original); + assert.throws(() => hub.ledger.find('owned-duplicate'), /不唯一/); assert.equal(new ModelRequestLedger(f.store.storage).page().total, 2); + const broken = fixture(t, { modernDefaults: true }); broken.store.storage.setMeta('modelAttempts', [null]); const raw = originalMeta(broken.store); + assert.throws(() => provider().modelHub(broken.store), /完整迁移/); assert.equal(originalMeta(broken.store), raw); assert.equal(broken.store.storage.meta('modelAttemptLedger'), null); +}); + +test('keyset pages include every old entry once despite new arrivals and cursor scope, numeric boundaries and literal searches are validated', t => { + const f = fixture(t, { modernDefaults: true }), rows = historicalAttempts(); f.store.storage.setMeta('modelAttempts', rows); const ledger = new ModelRequestLedger(f.store.storage); + let page = ledger.page({ limit: 65 }), collected = [...page.items]; ledger.save(owned('owned-late-arrival')); + while (page.hasMore) { page = ledger.page({ cursor: page.nextCursor, limit: 65 }); collected.push(...page.items); } + assert.deepEqual(collected, [...rows].reverse()); assert.equal(page.total, rows.length); assert.equal(page.retainedTotal, rows.length + 1); assert.equal(ledger.page().items[0].id, 'owned-late-arrival'); + const filtered = ledger.page({ filters: { connectionId: 'owned-beta', status: 'failed' }, limit: 1 }); + assert.throws(() => ledger.page({ cursor: filtered.nextCursor, filters: { connectionId: 'owned-alpha', status: 'failed' } }), /当前筛选/); + const other = new ModelRequestLedger(fixture(t, { modernDefaults: true }).store.storage); assert.throws(() => other.page({ cursor: filtered.nextCursor, filters: { connectionId: 'owned-beta', status: 'failed' } }), /当前筛选/); + for (const limit of [0, -1, '1.5', 'Infinity', Number.MAX_SAFE_INTEGER + 1]) assert.throws(() => ledger.page({ limit }), /正整数/); + assert.equal(ledger.page({ limit: Number.MAX_SAFE_INTEGER }).items.length, 1108); + ledger.save({ ...owned('owned-literal'), reason: '仅这条记录有 100%_literal' }); assert.equal(ledger.page({ filters: { query: '100%_literal' } }).total, 1); + assert.equal(ledger.page({ filters: { query: "' OR 1=1 --" } }).total, 0); + assert.throws(() => ledgerQuery(new URL('http://localhost/?limit=1&limit=2')), /参数/); assert.throws(() => ledgerQuery(new URL('http://localhost/?cursor=bad'), { exportOnly: true }), /参数/); +}); + +test('record source checks reject reassignment and corrupted payload while initial and historical receipts remain intact', t => { + const f = fixture(t, { modernDefaults: true }), ledger = new ModelRequestLedger(f.store.storage), value = owned('owned-provenance'); ledger.save(value); + for (const key of ['id', 'connectionId', 'poolId', 'sessionId', 'taskId', 'runId', 'projectId', 'requestId', 'model', 'protocol']) { + const changed = { ...value, [key]: 'owned-wrong' }; + if (key === 'id') continue; assert.throws(() => ledger.save(changed), /不能移动/); + } + const before = ledger.record(1); f.store.storage.database.prepare('UPDATE model_attempts SET payload=? WHERE seq=1').run(JSON.stringify({ ...value, reason: 'external corrupt payload' })); + assert.throws(() => ledger.record(1), /SHA256/); assert.throws(() => ledger.page(), /SHA256/); + assert.throws(() => f.store.storage.meta('modelAttempts'), /SHA256/); + f.store.storage.database.prepare('UPDATE model_attempts SET payload=? WHERE seq=1').run(JSON.stringify(value)); assert.deepEqual(ledger.record(1), before); +}); + +test('versioned migration prevents a previous storage program from silently opening a partial ledger; only untouched initialization can roll back', t => { + const f = fixture(t, { modernDefaults: true }), rows = historicalAttempts(7); f.store.storage.setMeta('modelAttempts', rows); const original = originalMeta(f.store); + assert.equal(f.store.storage.database.prepare('PRAGMA user_version').get().user_version, 1); + assert.equal(f.store.storageInfo().schemaVersion, 1); + const ledger = new ModelRequestLedger(f.store.storage); assert.equal(f.store.storage.database.prepare('PRAGMA user_version').get().user_version, 2); + assert.equal(f.store.storageInfo().schemaVersion, 2); + assert.throws(() => new LegacyStateDatabase(f.data), /更新版本/); assert.equal(ledger.page().total, rows.length); + assert.equal(ledger.rollbackInitialization(), true); assert.equal(originalMeta(f.store), original); + assert.equal(f.store.storageInfo().schemaVersion, 1); + const prior = new LegacyStateDatabase(f.data); f.onCleanup(() => prior.close()); assert.deepEqual(prior.meta('modelAttempts'), rows); + const active = new ModelRequestLedger(f.store.storage); active.save(owned('owned-after-migration')); + assert.throws(() => active.rollbackInitialization(), /已有新记录/); assert.equal(active.page().total, 8); assert.equal(originalMeta(f.store), original); +}); + +test('a stale cached receipt cannot overwrite another writer; unchanged progress is a read-only no-op and the full compatibility view stays current', t => { + const f = fixture(t, { modernDefaults: true }), hub = provider().modelHub(f.store), current = owned('owned-cache-current', 'running'); hub.addAttempt(current); + assert.equal(hub.attempts[0].status, 'running'); current.status = 'completed'; hub.saveAttempts(current); assert.equal(hub.attempts[0].status, 'completed'); + f.store.storage.faultInjector = phase => { if (phase === 'before_commit') throw new Error('unchanged receipts must not commit'); }; + assert.doesNotThrow(() => hub.saveAttempts(current)); f.store.storage.faultInjector = null; + hub.ledger.save({ ...current, reason: 'owned concurrent progress' }); current.reason = 'stale overwritten progress'; + assert.throws(() => hub.saveAttempts(current), /已更新/); assert.equal(hub.ledger.record(1).item.reason, 'owned concurrent progress'); +}); + +async function retainedLegacy(t) { + const f = fixture(t, { modernDefaults: true }); f.store.storage.close(); f.store.storage = new LegacyStateDatabase(f.data); + const rows = historicalAttempts(107); f.store.storage.setMeta('modelAttempts', rows); const actual = new Provider(), runtime = new Runtime(f.store, actual); + actual.credentials = new Map(); + const hub = Object.assign(Object.create(ModelHub.prototype), { store: f.store, provider: actual, + state: { schema: 1, revision: 0, selected: { type: 'legacy', id: 'legacy-default' }, connections: [], pools: [], imports: {} }, + health: new Map(), leases: new Map(), cursors: new Map(), sticky: new Map(), waiting: 0 }); + Object.defineProperty(hub, 'attempts', { value: structuredClone(rows), writable: true, configurable: true, enumerable: true }); actual.hub = hub; + const originalHandler = (_request, response) => response.end('owned previous handler'), server = http.createServer(originalHandler); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); f.onCleanup(() => runtime.stopAll()); + f.onCleanup(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); })); + return { ...f, hub, rows, server, actual, originalHandler, context: { store: f.store, server, provider: actual, runtime, token: 'owned-legacy-ledger-token', clients: new Set(), started: Date.now() } }; +} + +test('retained legacy hub shape adopts the new ledger on the same previous source storage connection, keeping saved bytes and credential objects', async t => { + const f = await retainedLegacy(t), storage = f.store.storage, database = storage.database, original = originalMeta(f.store), credentials = f.actual.credentials; + installUpgrade(f.context, { appDirectory: workspace, updateServiceInfo: false }); + assert.equal(f.store.storage, storage); assert.equal(storage.database, database); assert.equal(f.actual.hub, f.hub); assert.equal(f.actual.credentials, credentials); + assert.equal(Object.hasOwn(f.hub, 'attempts'), false); assert.equal(f.hub.view().attemptCount, 107); assert.equal(originalMeta(f.store), original); + assert.equal(database.prepare('PRAGMA user_version').get().user_version, 2); assert.deepEqual(storage.meta('modelAttempts'), f.rows); +}); + +test('failed retained handler adoption restores the original storage prototype, ledger shape, handler, schema and exact original metadata', async t => { + const f = await retainedLegacy(t), storage = f.store.storage, prototype = Object.getPrototypeOf(storage), attempts = f.hub.attempts, original = originalMeta(f.store); + const on = f.server.on; let failed = false; t.mock.method(f.server, 'on', function(event, handler) { + if (event === 'request' && !failed) { failed = true; throw new Error('owned failure after ledger adoption'); } return on.call(this, event, handler); + }); + assert.throws(() => installUpgrade(f.context, { appDirectory: workspace, updateServiceInfo: false }), /owned failure after ledger adoption/); + assert.equal(Object.getPrototypeOf(storage), prototype); assert.equal(f.store.storage, storage); assert.equal(f.hub.attempts, attempts); assert.equal(f.hub.ledger, undefined); + assert.equal(f.server.listeners('request')[0], f.originalHandler); assert.equal(storage.database.prepare('PRAGMA user_version').get().user_version, 1); + assert.equal(originalMeta(f.store), original); assert.equal(storage.meta('modelAttemptLedger'), null); + assert.equal(storage.database.prepare("SELECT COUNT(*) AS n FROM sqlite_schema WHERE name='model_attempts'").get().n, 0); +}); + +test('full export holds a read snapshot while live rows change, keeps all raw fields and ends with complete count and byte checksum', async t => { + const f = fixture(t, { modernDefaults: true }), rows = historicalAttempts(); f.store.storage.setMeta('modelAttempts', rows); const ledger = new ModelRequestLedger(f.store.storage); + const snapshot = ledger.exportSnapshot(), response = new Response(line => { if (JSON.parse(line).type === 'header') { ledger.save({ ...rows[500], reason: 'export-time change' }); ledger.save(owned('owned-during-export')); } }); + await streamLedgerExport(response, snapshot); const result = inspectExport(response.chunks.join('')); + assert.equal(result.records.length, 1107); assert.deepEqual(result.records.map(value => value.item), rows); assert.equal(ledger.page().retainedTotal, 1108); + const filtered = new Response(); await streamLedgerExport(filtered, ledger.exportSnapshot({ connectionId: 'owned-beta', status: 'failed' })); + assert.deepEqual(inspectExport(filtered.chunks.join('')).records.map(value => value.item), rows.filter(value => value.connectionId === 'owned-beta' && value.status === 'failed')); +}); + +test('cancelled export releases its snapshot and partial bytes never acquire a complete footer or cause requests', async t => { + const f = fixture(t, { modernDefaults: true }), ledger = new ModelRequestLedger(f.store.storage); ledger.save(owned('owned-partial-export')); + const response = new Response(() => false), snapshot = ledger.exportSnapshot(); let closed = false; const close = snapshot.close; + snapshot.close = () => { closed = true; close(); }; const exporting = streamLedgerExport(response, snapshot); + response.destroyed = true; response.emit('close'); await assert.rejects(exporting, /被中断/); assert.equal(closed, true); assert.doesNotMatch(response.chunks.join(''), /"complete":true/); + const complete = new Response(); await streamLedgerExport(complete, ledger.exportSnapshot()); assert.equal(inspectExport(complete.chunks.join('')).records.length, 1); +}); + +for (const [name, sample] of Object.entries(usageSamples)) test(`actual ${name} request uses incremental ledger, retains source-bound usage and original artifact, and restart never dispatches again`, async t => { + const f = fixture(t, { modernDefaults: true }), model = await cacheUsageFixture(t, { ...sample, streaming: true }), actual = new Provider(), hub = actual.modelHub(f.store); + const connection = hub.upsert({ name: 'Owned incremental ' + name, model: usageModelId, protocol: sample.protocol, baseUrl: model.baseUrl, streaming: true, requiresKey: false }); hub.select({ type: 'connection', id: connection.id }); + const runtime = new Runtime(f.store, actual); f.onCleanup(() => runtime.stopAll()); + runtime.start(f.session.id, '只说明已收到完整回复,不运行工具。'); await until(() => !runtime.isActive(f.session.id), 15000); + assert.equal(model.requests.length, 1); assert.equal(f.store.core.tool.size, 0); assert.equal(hub.compatAttempts, undefined); + const receipt = hub.ledger.record(1); assert.equal(receipt.item.status, 'completed'); assert.deepEqual(receipt.item.usage.raw, sample.raw); + assert.equal(receipt.item.usage.tokenAccounting.metrics.cacheWrite.tokens, name === 'chat' ? 300 : name === 'responses' ? 7000 : 248); + const artifact = f.store.owned('artifact', receipt.item.responseArtifactId, f.session.id); assert.equal(artifact.sha256, hash(model.responses[0])); + assert.equal(f.store.storage.meta('modelAttempts')[0].id, receipt.item.id); await runtime.stopAll(); f.store.close(); + const restored = new Store(f.data); f.onCleanup(() => restored.close()); const fresh = provider().modelHub(restored); assert.deepEqual(fresh.ledger.record(1), receipt); assert.equal(model.requests.length, 1); +}); + +test('actual authenticated local HTTP pages and exports the entire ledger with source details, invalid query/origin and no model execution', async t => { + const f = fixture(t, { modernDefaults: true }), rows = historicalAttempts(), actual = new Provider(); f.store.storage.setMeta('modelAttempts', rows); + const runtime = new Runtime(f.store, actual), token = 'owned-ledger-token', server = http.createServer(); f.onCleanup(() => runtime.stopAll()); + server.on('request', createRequestHandler({ appDirectory: workspace, server, store: f.store, provider: actual, runtime, token, clients: new Set(), started: Date.now(), getPort: () => server.address().port })); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); f.onCleanup(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); })); + const base = 'http://127.0.0.1:' + server.address().port, fetchOwned = route => fetch(base + '/api/model-center' + route, { signal: AbortSignal.timeout(8000), headers: { 'X-Workbench-Token': token, Connection: 'close' } }); + console.log('[request-ledger-http] reading first page'); + const page = await (await fetchOwned('/attempts?limit=65')).json(); assert.equal(page.items.length, 65); assert.equal(page.total, 1107); + console.log('[request-ledger-http] reading first source record'); + const record = await (await fetchOwned('/attempts/1')).json(); assert.deepEqual(record.item, rows[0]); assert.equal(record.reference.payloadSha256, hash(JSON.stringify(rows[0]))); + console.log('[request-ledger-http] starting complete export'); + const response = await fetchOwned('/attempts/export'); assert.equal(response.status, 200); assert.match(response.headers.get('content-type'), /ndjson/); + assert.deepEqual(inspectExport(Buffer.from(await response.arrayBuffer())).records.map(value => value.item), rows); + console.log('[request-ledger-http] complete export received, checking invalid requests'); + assert.equal((await fetchOwned('/attempts?limit=0')).status, 400); assert.equal((await fetchOwned('/attempts/999999')).status, 404); + const blocked = await fetch(base + '/api/model-center/attempts', { signal: AbortSignal.timeout(8000), headers: { Origin: 'https://owned-untrusted.example', 'X-Workbench-Token': token, Connection: 'close' } }); assert.equal(blocked.status, 403); + assert.equal(actual.activeRequests ?? 0, 0); assert.equal(runtime.active.size, 0); assert.equal(originalMeta(f.store), JSON.stringify(rows)); +});