import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { randomUUID } from 'node:crypto'; import { fixture, call, until, assertToolPairs } from './helpers.mjs'; import { workerDefinition, prepareWorkerFixture, completedWorkers, ownerBytes } from './fixtures/workers-project.mjs'; import { collaborationInput, workerPath, workerForSession } from '../src/shared/contracts/workers.mjs'; import { proposeFile } from '../src/server/files.mjs'; import { Store } from '../src/server/store.mjs'; test('worker scopes and allocated budgets reject aliases, extra permissions and aggregate overflow', t => { const f = fixture(t); for (const value of ['../outside', 'C:/elsewhere', '/root', 'src\\file', 'src/../x', 'src/a.', 'src/a ', 'CON.mjs', '.git/config', '.env', 'src/*']) assert.throws(() => workerPath(value)); const valid = collaborationInput(workerDefinition(), f.store.config); assert.equal(valid.children.length, 2); assert.equal(valid.children.reduce((sum, child) => sum + child.budget.maxTokens, 0), valid.budget.maxTokens); assert.throws(() => collaborationInput({ ...workerDefinition(), children: [workerDefinition().children[0]] }, f.store.config), /两个/); assert.throws(() => collaborationInput({ ...workerDefinition(), children: workerDefinition().children.map(child => ({ ...child, budget: { maxTokens: valid.budget.maxTokens } })) }, f.store.config), /之和/); assert.throws(() => collaborationInput({ ...workerDefinition(), command: 'powershell' }, f.store.config), /只接受/); }); test('two sequential Git workspaces freeze a new parent goal, separate owned tasks and preserve all source bytes', async t => { const f = fixture(t), { manager, runtime } = prepareWorkerFixture(f); const oldTaskId = f.session.taskId, original = ownerBytes(f); f.session.status = 'failed'; f.store.save(f.session); const group = await manager.plan(f.session, workerDefinition()); assert.notEqual(group.taskId, oldTaskId); assert.equal(f.store.core.task.get(oldTaskId).goal, 'Repair addition, verify the project and deliver only reviewed files'); const preparation = manager.prepare(f.session, group.id, group.fingerprint); assert.equal(runtime.hasActivity(), true); assert.throws(() => manager.prepare(f.session, group.id, group.fingerprint), /创建|执行|重放/); await preparation.promise; assert.equal(group.status, 'ready', group.summary); assert.deepEqual(ownerBytes(f), original); assert.equal(new Set(group.children.map(child => child.projectId)).size, 2); for (const child of group.children) { const worker = f.store.core.worker.get(child.id), session = f.store.session(child.sessionId); assert.equal(workerForSession(f.store, session).id, child.id); assert.equal(f.store.core.task.get(child.taskId).goal, child.goal); assert.notEqual(f.store.project(child.projectId).root, f.projectRoot); assert.throws(() => runtime.start(session.id, '替换目标'), /父协作|受限/); assert.throws(() => proposeFile(f.store, session, { path: 'README.md', content: 'outside', reason: 'outside' }), /范围/); const other = f.store.createSession(child.projectId); assert.throws(() => proposeFile(f.store, other, { path: child.paths[0], content: 'other', reason: 'other' }), /原受限/); } }); test('real isolated changes, approvals and npm checks produce original owned outputs, not model completion claims', async t => { const f = fixture(t), { group, manager, runtime, requests, original } = await completedWorkers(f); assert.equal(group.status, 'ready_to_collect', JSON.stringify(manager.view(f.session).groups[0].workers.map(worker => { const session = f.store.session(worker.sessionId); return { status: worker.status, summary: worker.summary, failure: session.failure, reason: f.store.core.run.get(session.runId).stopReason, messages: session.messages.slice(-3).map(message => message.content), checks: session.execution?.checks }; }))); assert.deepEqual(ownerBytes(f), original); assert.equal(requests.some(request => request.tools.includes('run_command')), false); assert.equal(requests.some(request => request.tools.includes('plan_delivery')), false); assert.equal(requests.some(request => request.tools.includes('call_registered_mcp')), false); for (const child of group.children) { const worker = f.store.core.worker.get(child.id), session = f.store.session(child.sessionId); assert.equal(worker.status, 'completed', worker.summary); assert.ok(worker.result); assert.equal((await manager.output(session, worker)).check.results[0].tests.passed, 3); assert.equal(session.approvals.filter(approval => approval.status === 'approved').length, 1); assertToolPairs(session.providerMessages); assert.throws(() => proposeFile(f.store, session, { path: child.paths[0], content: 'changed', reason: 'changed' }), /封存/); } assert.equal(runtime.hasActivity(), false); }); test('forged worker tools cannot execute terminal or Git even when absent from the advertised tool list', async t => { const f = fixture(t), { manager, runtime } = prepareWorkerFixture(f); const group = await manager.plan(f.session, workerDefinition()); await (await manager.prepare(f.session, group.id, group.fingerprint)).promise; const child = group.children[0], session = f.store.session(child.sessionId), record = f.store.core.run.get(session.runId); for (const tool of ['run_command', 'plan_delivery', 'plan_task']) { const invocation = call(randomUUID(), tool, tool === 'run_command' ? { command: 'write file', reason: 'forged' } : tool === 'plan_task' ? { expected_revision: 0, steps: [] } : {}); const result = await runtime.executeTool(session, invocation, { id: record.id, taskId: child.taskId, record, project: f.store.project(child.projectId), controller: new AbortController(), config: f.store.config }); assert.equal(result.status, 'error'); } assert.equal(session.approvals.length, 0); }); test('a completion-only model pauses each worker without fabricated changes or checks', async t => { const f = fixture(t), { manager } = prepareWorkerFixture(f, { respond: async () => ({ message: { role: 'assistant', content: '全部完成' }, usage: { total_tokens: 10 } }) }); const group = await manager.plan(f.session, workerDefinition()); await (await manager.prepare(f.session, group.id, group.fingerprint)).promise; await (await manager.start(f.session, group.id, group.fingerprint)).promise; assert.equal(group.status, 'paused'); for (const child of group.children) { assert.equal(f.store.core.worker.get(child.id).result, null); assert.equal(f.store.core.run.get(f.store.session(child.sessionId).runId).stopReason, 'worker_incomplete', JSON.stringify(f.store.session(child.sessionId).failure ?? f.store.core.worker.get(child.id).summary)); } }); test('restart retains frozen scope and marks interrupted parent and workers unknown without sending requests', async t => { const f = fixture(t), { manager } = prepareWorkerFixture(f); const group = await manager.plan(f.session, workerDefinition()); await (await manager.prepare(f.session, group.id, group.fingerprint)).promise; group.status = 'running'; f.store.save(f.session); const worker = f.store.core.worker.get(group.children[0].id), session = f.store.session(worker.sessionId); worker.status = 'running'; f.store.save(session); f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); assert.equal(reopened.core.collaboration.get(group.id).status, 'unknown'); assert.equal(reopened.core.worker.get(worker.id).status, 'unknown'); assert.equal(reopened.core.worker.get(worker.id).requiresVerification, true); assert.deepEqual(reopened.core.worker.get(worker.id).paths, worker.paths); });