import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { randomUUID, randomBytes, createCipheriv, createDecipheriv } from 'node:crypto'; import { fixture, compatibleFixture, completion, call } from './helpers.mjs'; import { Runtime } from '../src/server/runtime.mjs'; import { Store } from '../src/server/store.mjs'; import { Provider } from '../src/server/provider.mjs'; import { DeviceRegistry } from '../src/core/companion/device-registry.mjs'; import { CredentialVault } from '../src/core/models/credential-vault.mjs'; import { planHash } from '../src/shared/contracts/task-plans.mjs'; import { recordPage, recordPageQuery } from '../src/shared/contracts/record-page.mjs'; function paused(t) { const f = fixture(t, { modernDefaults: true }), runtime = new Runtime(f.store, new Provider()); const run = f.store.beginRun(f.session, 'Owned complete-record recovery fixture', false, f.store.config); f.session.providerMessages.push({ role: 'user', content: 'Owned complete-record recovery fixture' }); f.session.status = 'paused'; f.store.finishRun(f.session, run, 'owned_pause'); f.onCleanup(() => runtime.stopAll()); return { ...f, run, runtime, manager: runtime.checkpointManager() }; } function references(f, count, large = false) { const tools = [], calls = []; const small = f.store.artifactStore.write(f.session, f.run, { status: 'completed', message: 'owned-small-result' }); f.store.core.artifact.set(small.id, small); const big = large ? f.store.artifactStore.write(f.session, f.run, Buffer.alloc(64 * 1024 * 1024 + 1, 97)) : small; f.store.core.artifact.set(big.id, big); for (let index = 0; index < count; index++) { const input = call('owned-call-' + index, 'read_file', { path: 'README.md', index }); calls.push(input); const value = { id: randomUUID(), sessionId: f.session.id, projectId: f.project.id, taskId: f.session.taskId, runId: f.run.id, callId: input.id, tool: input.function.name, argsSha256: planHash(input.function.arguments), state: 'completed', requiresVerification: false, resultArtifactId: index === count - 1 ? big.id : small.id }; f.store.core.tool.set(value.id, value); tools.push(value); } f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: calls }, ...calls.map(value => ({ role: 'tool', tool_call_id: value.id, content: 'Owned stored result reference' }))); f.store.save(f.session); return { tools, small, big }; } function sealedTestCredentials() { const key = randomBytes(32); return { protect(bytes, entropy) { const iv = randomBytes(12), cipher = createCipheriv('aes-256-gcm', key, iv); cipher.setAAD(entropy); const encrypted = Buffer.concat([cipher.update(bytes), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), encrypted]); }, unprotect(bytes, entropy) { const cipher = createDecipheriv('aes-256-gcm', key, bytes.subarray(0, 12)); cipher.setAAD(entropy); cipher.setAuthTag(bytes.subarray(12, 28)); return Buffer.concat([cipher.update(bytes.subarray(28)), cipher.final()]); } }; } test('record cursors retain every row during new inserts and reject foreign, missing and malformed anchors', () => { const rows = Array.from({ length: 102 }, (_, index) => ({ id: 'row-' + index })).reverse(); const first = recordPage(rows, { limit: 50 }, 'scope'), next = recordPage([{ id: 'new' }, ...rows], { cursor: first.nextCursor, limit: 50 }, 'scope'); const last = recordPage(rows, { cursor: next.nextCursor, limit: 50 }, 'scope'); assert.equal(first.total, 102); assert.equal(new Set([...first.items, ...next.items, ...last.items].map(value => value.id)).size, 102); assert.equal(last.hasMore, false); assert.equal(last.nextCursor, null); assert.throws(() => recordPage(rows, { cursor: first.nextCursor }, 'foreign'), /范围/); assert.throws(() => recordPage([], { cursor: first.nextCursor }, 'scope'), /已变化/); for (const cursor of ['', '%', 'null', Buffer.from('null').toString('base64url')]) assert.throws(() => recordPage(rows, { cursor }, 'scope'), /游标/); for (const query of ['limit=0', 'limit=1&limit=2', 'cursor=x&cursor=y', 'unknown=x']) assert.throws(() => recordPage(rows, recordPageQuery(new URL('http://localhost/?' + query)), 'scope')); }); test('unrestricted MCP executes 31 real stdio calls in one task and preserves every report across pages and restart', async t => { const f = fixture(t, { modernDefaults: true }), provider = new Provider(); provider.setKey('owned-local-fixture-only'); f.store.saveConfig({ approvalMode: 'full' }); const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll()); const registry = runtime.mcpRegistry(), entry = path.join(f.root, 'owned-mcp.mjs'); fs.copyFileSync('tests/fixtures/registered-mcp-server.mjs', entry); let registration = registry.register(f.project.id, { name: 'Owned boundary MCP', executable: process.execPath, entry, timeoutMs: 2000 }); assert.equal((await registry.start(f.session, { id: registration.id, configurationRevision: 1, confirm: true }).promise).status, 'completed'); registration = registry.find(f.project.id, registration.id); registration = registry.configure(f.project.id, { id: registration.id, configurationRevision: registration.configurationRevision, enabled: true, allowedTools: ['echo'] }); const input = { registration_id: registration.id, configuration_revision: registration.configurationRevision, contract_sha256: registration.contract.sha256, tool: 'echo', arguments: { text: 'owned-boundary' } }; const server = await compatibleFixture(t, (_url, _body, requests) => requests.length <= 31 ? completion([call('owned-mcp-' + requests.length, 'call_registered_mcp', input)]) : completion([], 'Owned boundary done')); f.store.saveConfig({ mode: 'compatible', baseUrl: server.baseUrl, model: 'owned-fixture-model' }); runtime.start(f.session.id, 'Run the owned boundary sequence'); await runtime.active.get(f.session.id).promise; assert.equal(f.session.status, 'completed', JSON.stringify(f.session.error)); assert.equal(server.requests.length, 32); const jobs = registry.jobs(f.session); assert.equal(jobs.filter(value => value.taskId === f.session.taskId && value.operation === 'call').length, 31); const snapshot = f.store.executionSnapshot(f.session); assert.equal(snapshot.mcpJobCount, jobs.length); assert.equal(snapshot.mcpJobsTruncated, true); for (const job of jobs) { assert.equal(job.status, 'completed'); assert.equal(job.cleanup.rootExited, true); assert.ok(registry.report(f.session, job.id)); } const first = registry.jobs(f.session, { limit: 11 }), second = registry.jobs(f.session, { cursor: first.nextCursor, limit: 50 }); assert.equal(new Set([...first.items, ...second.items].map(value => value.id)).size, jobs.length); const hashes = jobs.map(value => value.reportSha256); await runtime.stopAll(); f.store.close(); const store = new Store(f.data); f.onCleanup(() => store.close()); const next = new Runtime(store, new Provider()); f.onCleanup(() => next.stopAll()); assert.deepEqual(next.mcpRegistry().jobs(store.session(f.session.id)).map(value => value.reportSha256), hashes); }); test('31 checkpoints remain readable and resumable with legacy v1 original SHA intact after restart', async t => { const f = paused(t); references(f, 1); const records = []; for (let index = 0; index < 31; index++) records.push(await f.manager.create(f.session)); assert.equal(f.manager.list(f.session).length, 31); const first = f.manager.list(f.session, { limit: 17 }), second = f.manager.list(f.session, { cursor: first.nextCursor, limit: 17 }); assert.equal(new Set([...first.items, ...second.items].map(value => value.id)).size, 31); const { bundle } = f.manager.original(f.session, records[0].id); bundle.schemaVersion = 1; const artifact = f.store.artifactStore.write(f.session, f.run, bundle, { kind: 'task-recovery-checkpoint' }); f.store.core.artifact.set(artifact.id, artifact); const legacy = { ...records[0], schemaVersion: 1, id: randomUUID(), artifactId: artifact.id, sha256: artifact.sha256 }; f.store.putRecord('task_checkpoint', f.session, legacy, 'owned.legacy_checkpoint'); const before = f.store.artifactStore.readBytes(artifact); f.store.close(); const store = new Store(f.data); f.onCleanup(() => store.close()); const runtime = new Runtime(store, new Provider()); f.onCleanup(() => runtime.stopAll()); const session = store.session(f.session.id), manager = runtime.checkpointManager(); const restarted = await manager.inspect(session, records.at(-1).id); assert.equal(restarted.resumable, true, JSON.stringify({ reasons: restarted.reasons, status: session.status })); assert.equal((await manager.inspect(session, legacy.id)).resumable, true); assert.deepEqual(store.artifactStore.readBytes(artifact), before); let resumes = 0; await manager.resume(session, records.at(-1).id, records.at(-1).sha256, { resume(id) { assert.equal(id, session.id); resumes++; } }); assert.equal(resumes, 1); assert.equal(store.core.tool.size, 1); }); test('2001 tool references and over 64MiB results use segmented indexes and streaming SHA verification without losing corruption detection', async t => { const f = paused(t), { big } = references(f, 2001, true); const readBytes = f.store.artifactStore.readBytes.bind(f.store.artifactStore); f.store.artifactStore.readBytes = artifact => { assert.notEqual(artifact.id, big.id, 'large source must be verified without whole-file buffering'); return readBytes(artifact); }; const checkpoint = await f.manager.create(f.session), manifest = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(checkpoint.artifactId))); assert.equal(manifest.schemaVersion, 2); assert.equal(manifest.state.toolIndex.count, 2001); assert.equal(manifest.state.toolIndex.segments.length, 8); assert.equal(f.manager.original(f.session, checkpoint.id).bundle.state.tools.length, 2001); assert.equal((await f.manager.inspect(f.session, checkpoint.id)).resumable, true); const filename = path.join(f.store.artifactStore.root, big.relative), handle = fs.openSync(filename, 'r+'); fs.writeSync(handle, Buffer.from('b'), 0, 1, big.bytes - 1); fs.closeSync(handle); await assert.rejects(f.manager.inspect(f.session, checkpoint.id), /工件校验失败/); assert.equal(fs.statSync(filename).size, big.bytes); }); test('checkpoint segment corruption is rejected and explicit restricted mode still controls creation without hiding history', async t => { const f = paused(t); references(f, 1); const checkpoint = await f.manager.create(f.session); const manifest = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(checkpoint.artifactId))), segment = f.store.core.artifact.get(manifest.state.toolIndex.segments[0].artifact.id); fs.writeFileSync(path.join(f.store.artifactStore.root, segment.relative), '{}'); await assert.rejects(f.manager.inspect(f.session, checkpoint.id), /工件校验失败/); f.store.saveConfig({ executionMode: 'restricted' }); for (let index = 0; index < 30; index++) f.store.core.task_checkpoint.set('owned-' + index, { ...checkpoint, id: 'owned-' + index }); assert.equal(f.manager.list(f.session).length, 31); await assert.rejects(f.manager.create(f.session), /30个/); }); test('102 independently sealed credentials restore by entry; corrupt, disabled and foreign entries preserve raw metadata and valid neighbors', t => { const f = fixture(t, { modernDefaults: true }), encryption = sealedTestCredentials(), provider = new Provider(); provider.credentials = new Map(); const vault = new CredentialVault(f.store, provider, encryption); const connections = Array.from({ length: 102 }, (_, index) => ({ id: randomUUID(), revision: 1, protocol: 'openai-chat', model: 'owned-' + index, baseUrl: 'http://127.0.0.1:1/v1', requiresKey: true })); f.store.storage.transaction(() => { for (const connection of connections) vault.stage(connection.id, vault.prepare(connection, 'owned-credential-' + connection.model, 1)); }); const metadata = f.store.storage.meta('modelCredentials'); assert.equal(Object.keys(metadata.entries).length, 102); f.store.close(); const store = new Store(f.data); f.onCleanup(() => store.close()); const nextProvider = new Provider(); nextProvider.credentials = new Map(); const next = new CredentialVault(store, nextProvider, encryption); const hub = { connections: () => connections }; next.restore(hub); assert.equal(nextProvider.credentials.size, 102); for (const connection of connections) assert.equal(nextProvider.credentials.get(connection.id).key, 'owned-credential-' + connection.model); const state = store.storage.meta('modelCredentials'); state.entries[connections[0].id] = null; state.entries[connections[1].id] = { unknown: 'owned-original-broken-entry' }; state.entries[connections[2].id].enabled = false; state.entries[connections[3].id].userSid = 'owned-other-user'; store.storage.transaction(() => store.storage.setMeta('modelCredentials', state)); const cleanProvider = new Provider(); cleanProvider.credentials = new Map(); const isolated = new CredentialVault(store, cleanProvider, encryption); isolated.restore(hub); assert.equal(cleanProvider.credentials.size, 98); assert.equal(isolated.status(connections[0]).status, 'invalid_record'); assert.equal(isolated.status(connections[1]).status, 'invalid_record'); assert.equal(isolated.status(connections[2]).status, 'disabled'); assert.equal(isolated.status(connections[3]).status, 'user_mismatch'); assert.deepEqual(store.storage.meta('modelCredentials'), state); assert.equal(JSON.stringify(isolated.view(hub)).includes('ciphertext'), false); state.schema = 99; store.storage.transaction(() => store.storage.setMeta('modelCredentials', state)); const future = new CredentialVault(store, new Provider(), encryption); assert.equal(future.blocked, true); assert.deepEqual(store.storage.meta('modelCredentials'), state); }); test('2001 companion operations and over 500 audit entries survive restart, preserve idempotency and expose older unknown without replay', t => { const f = paused(t), host = { instanceId: randomUUID(), ownerId: randomUUID(), userSid: 'owned-user', pid: process.pid, createdAt: new Date().toISOString() }; const registry = new DeviceRegistry(f.store, () => host); registry.initialize(); const grant = { projectIds: [f.project.id], permissions: { observe: true, approve: false, stop: true }, expiresInHours: 24 }; const paired = registry.consume(registry.pair(grant, 'g').token, 'Owned phone', 'g'), device = registry.authenticate(paired.token), requests = []; // Exercise each real product transaction, including its durable receipt. for (let index = 0; index < 2001; index++) { const input = { requestId: randomUUID(), taskId: f.session.taskId, runId: f.session.runId, revision: f.session.revision }; requests.push(input); registry.reserve(device, 'stop', f.session, input); if (index !== 0) registry.finish(device, input, 'completed'); } const before = registry.read(); assert.equal(Object.keys(before.operations).length, 2001); assert.equal(before.audit.length, 4002); assert.equal(registry.reserve(device, 'stop', f.session, requests[0]).duplicate, true); assert.throws(() => registry.reserve(device, 'stop', f.session, { ...requests[0], revision: -1 }), /请求ID/); f.store.close(); const store = new Store(f.data); f.onCleanup(() => store.close()); const nextHost = { ...host, instanceId: randomUUID() }, next = new DeviceRegistry(store, () => nextHost), current = next.authenticate(paired.token); const ids = [], audits = []; let page = {}; do { const result = next.operationsPage(current, page); ids.push(...result.items); page = { cursor: result.nextCursor }; if (!result.hasMore) break; } while (true); page = {}; do { const result = next.auditPage(page); audits.push(...result.items); page = { cursor: result.nextCursor }; if (!result.hasMore) break; } while (true); assert.equal(new Set(ids.map(value => value.id)).size, 2001); assert.equal(ids.at(-1).status, 'unknown'); assert.equal(audits.length, 4002); assert.equal(next.reserve(current, 'stop', store.session(f.session.id), requests[0]).status, 'pending'); assert.deepEqual(next.read().operations, before.operations); assert.equal(JSON.stringify(next.view()).includes('credentialHash'), false); });