import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { fixture, launchWorkbench, until, compatibleFixture, call, completion, assertToolPairs } from './helpers.mjs'; import { createDeliveryProject, deliveryGit } from './fixtures/delivery-project.mjs'; import { captureWorkspaceSource, assertWorkspaceSource } from './fixtures/workspace-project.mjs'; function replaceControlFile(file, contents) { // Git marks .git as hidden on Windows. Open the existing ordinary file // without O_CREAT; writeFileSync(path) would fail for that hidden file. const descriptor = fs.openSync(file, 'r+'), bytes = Buffer.isBuffer(contents) ? contents : Buffer.from(contents); try { fs.writeSync(descriptor, bytes, 0, bytes.length, 0); fs.ftruncateSync(descriptor, bytes.length); } finally { fs.closeSync(descriptor); } } test('workspace HTTP ownership, reviewed creation and reopen survive restart without changing source files', { timeout: 60000 }, async t => { const f = fixture(t); createDeliveryProject(f); const before = captureWorkspaceSource(f); f.store.close(); const host = await launchWorkbench(t, f.data), parent = '/sessions/' + f.session.id; assert.equal(host.bootstrap.capabilities.workspaces, 'owned-git-worktree-and-explicit-recovery'); const plan = await host.api(parent + '/workspaces', { label: 'API隔离工作区' }); assert.equal(plan.source.dirty, true); assert.equal(fs.existsSync(plan.root), false); await assert.rejects(host.api(parent + '/workspaces/' + plan.id + '/create', {}), /指纹/); await assert.rejects(host.api(parent + '/messages', { content: 'edit source during review' }), /工作区/); const other = await host.api('/sessions', { projectId: f.project.id }); await assert.rejects(host.api('/sessions/' + other.id + '/workspaces/' + plan.id), /不属于/); await assert.rejects(host.api('/sessions/' + other.id + '/workspaces/' + plan.id + '/create', { fingerprint: plan.fingerprint }), /不属于/); await Promise.all([host.api(parent + '/workspaces/' + plan.id + '/create', { fingerprint: plan.fingerprint }), host.api(parent + '/workspaces/' + plan.id + '/create', { fingerprint: plan.fingerprint })]); let record; await until(async () => { record = await host.api(parent + '/workspaces/' + plan.id); return record.status === 'ready'; }, 25000); assertWorkspaceSource(assert, f, before, record.branch); const opened = await host.api(parent + '/workspaces/' + plan.id + '/open', {}); assert.equal(opened.project.id, record.childProjectId); assert.equal(opened.session.id, record.childSessionId); assert.equal(opened.session.execution.workspaceIdentity.sourceProjectId, f.project.id); await host.close(); const restarted = await launchWorkbench(t, f.data); const reopened = await restarted.api(parent + '/workspaces/' + plan.id + '/open', {}); assert.deepEqual(reopened.project, opened.project); assert.equal(reopened.session.id, opened.session.id); assert.equal((await restarted.api('/bootstrap')).projects.filter(value => value.workspace?.id === plan.id).length, 1); const gitFile = path.join(record.root, '.git'), actualPointer = fs.readFileSync(gitFile); replaceControlFile(gitFile, 'gitdir: ' + path.join(f.projectRoot, '.git') + '\n'); await assert.rejects(restarted.api(parent + '/workspaces/' + plan.id + '/open', {}), /登记|外部修改/); await assert.rejects(restarted.api('/projects/' + record.childProjectId + '/file?path=src/add.mjs'), /登记|外部修改/); await assert.rejects(restarted.api('/sessions/' + record.childSessionId + '/proposals', { path: 'src/add.mjs', content: 'changed', reason: 'invalid identity' }), /登记|外部修改/); replaceControlFile(gitFile, actualPointer); assertWorkspaceSource(assert, f, before, record.branch); }); test('isolated model Task applies a batch, executes real checks and commits at the Git review boundary with a source-bound report', { timeout: 90000 }, async t => { const f = fixture(t); createDeliveryProject(f); const before = captureWorkspaceSource(f); f.store.close(); let mode = 'repair'; const model = await compatibleFixture(t, (url, input) => { if (!input?.messages) return { data: [{ id: 'workspace-http-fixture' }] }; const lastUser = input.messages.findLastIndex(value => value.role === 'user'), results = input.messages.slice(lastUser + 1) .filter(value => value.role === 'tool').map(value => JSON.parse(value.content)); if (!results.length) return completion([call('plan-' + mode, 'plan_checks', {})]); if (results.length === 1) return completion([call('check-' + mode, 'run_checks', { plan_id: results[0].id })]); if (results.length === 2 && mode === 'repair') return completion([ call('isolate-add', 'propose_file', { path: 'src/add.mjs', content: "import { sum } from './math.mjs';\nexport const add = sum;\n", reason: 'Repair only the isolated module' }), call('isolate-math', 'propose_file', { path: 'src/math.mjs', content: 'export const sum = (a,b) => a+b;\n', reason: 'Add isolated arithmetic helper' }), ]); if (results.length === 4 && mode === 'repair') return completion([call('isolate-batch', 'prepare_change_set', { change_ids: results.filter(value => value.change_id).map(value => value.change_id) })]); if (results.length === 2 && mode === 'delivery') return completion([ call('isolated-delivery-plan', 'plan_delivery', { message: 'feat: repair isolated addition' }), call('after-review-must-not-run', 'run_command', { command: "Set-Content -LiteralPath 'AFTER-REVIEW.txt' -Value 'unexpected'", reason: 'This deferred call must be interrupted at review' }), ]); return completion([], '已保存独立工作区的提案,等待审查。'); }); const host = await launchWorkbench(t, f.data), parent = '/sessions/' + f.session.id; await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'workspace-http-fixture', apiKey: 'SYNTHETIC-WORKSPACE-QA-ONLY', maxSteps: 12 }); const plan = await host.api(parent + '/workspaces', { label: '独立工程交付' }); await host.api(parent + '/workspaces/' + plan.id + '/create', { fingerprint: plan.fingerprint }); await until(async () => (await host.api(parent + '/workspaces/' + plan.id)).status === 'ready', 25000); const opened = await host.api(parent + '/workspaces/' + plan.id + '/open', {}), route = '/sessions/' + opened.session.id; await host.api(route + '/messages', { content: 'Repair addition in this isolated project, run the real tests and build, deliver a reviewed local Git commit.' }); let session; await until(async () => { session = await host.api(route); return session.status === 'awaiting_approval'; }, 25000); assert.equal(session.approvals.at(-1).cwd, plan.root); await host.api(route + '/approvals/' + session.approvals.at(-1).id, { allow: true }); await until(async () => { session = await host.api(route); return session.status === 'needs_review'; }, 30000); assert.equal(session.execution.checks[0].results[0].tests.failed, 3); const taskId = session.execution.task.id, batch = session.execution.changeSets.at(-1); await host.api(route + '/change-sets/' + batch.id + '/apply', { fingerprint: batch.fingerprint }); await until(async () => { session = await host.api(route); return session.execution.changeSets.at(-1).status === 'completed'; }); mode = 'delivery'; await host.api(route + '/continue-reviewed', {}); await until(async () => { session = await host.api(route); return session.status === 'awaiting_approval'; }); await host.api(route + '/approvals/' + session.approvals.at(-1).id, { allow: true }); await until(async () => { session = await host.api(route); return session.execution.run.stopReason === 'git_review'; }, 30000); assert.equal(session.execution.task.id, taskId); assert.equal(session.execution.checks.at(-1).status, 'passed'); assert.equal(session.execution.checks.at(-1).results[0].tests.passed, 3); assert.equal(session.approvals.length, 2); assert.equal(fs.existsSync(path.join(plan.root, 'AFTER-REVIEW.txt')), false); const requests = model.requests.length, deliveryPlan = session.execution.deliveryPlans.at(-1); await host.api(route + '/delivery-jobs', { planId: deliveryPlan.id }); await until(async () => { session = await host.api(route); return session.execution.deliveries.at(-1).status === 'completed' && !!session.execution.deliveries.at(-1).reportArtifactId; }, 30000); assert.equal(model.requests.length, requests); assert.equal(session.status, 'completed'); assert.equal(session.execution.run.status, 'completed'); const job = session.execution.deliveries.at(-1), report = JSON.parse((await host.api('/artifacts/' + job.reportArtifactId + '?session=' + session.id + '&limit=64000')).content); assert.equal(report.status, 'verified_committed'); assert.equal(report.workspace.workspaceId, plan.id); assert.equal(report.workspace.root, plan.root); assert.equal(report.workspace.sourceRoot, f.projectRoot); assert.equal(report.task.workspaceIdentity.workspaceId, plan.id); assert.ok(report.runs.every(value => value.workspaceIdentity.workspaceId === plan.id)); assert.equal(deliveryGit({ projectRoot: plan.root }, ['rev-parse', 'HEAD']).trim(), job.commit); assertWorkspaceSource(assert, f, before, plan.branch); const exported = await host.api(route + '/export'); assertToolPairs(exported.session.providerMessages); });