Files
Agent-Workbench/tests/python-api.test.mjs

125 lines
11 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { fixture, launchWorkbench, until, compatibleFixture, call, completion, assertToolPairs } from './helpers.mjs';
import { deliveryGit } from './fixtures/delivery-project.mjs';
import { captureWorkspaceSource, assertWorkspaceSource } from './fixtures/workspace-project.mjs';
import { createPythonDeliveryProject, pythonFixes, assertPythonProtected } from './fixtures/python-project.mjs';
const pythonEnv = () => process.env.AGENT_WORKBENCH_TEST_PYTHON ? { AGENT_WORKBENCH_PYTHON_PATH: process.env.AGENT_WORKBENCH_TEST_PYTHON } : {};
test('Python HTTP plans keep npm defaults, require owned approval and preserve actual results across restart', { timeout: 60000 }, async t => {
const f = fixture(t), protectedFiles = createPythonDeliveryProject(f); f.store.save(f.session); f.store.close();
const host = await launchWorkbench(t, f.data, pythonEnv()), route = '/sessions/' + f.session.id;
assert.equal(host.bootstrap.capabilities.pythonChecks, 'bound-interpreter-and-structured-results');
let session = await host.api(route + '/check-plans', {});
assert.deepEqual(session.execution.plans.at(-1).checks.map(check => check.name), ['test', 'build']);
session = await host.api(route + '/check-plans', { names: ['python:test', 'python:compile'] });
const plan = session.execution.plans.at(-1), other = await host.api('/sessions', { projectId: f.project.id });
await assert.rejects(host.api('/sessions/' + other.id + '/check-jobs', { planId: plan.id }), /不属于/);
assert.equal(session.execution.checks.length, 0);
await host.api(route + '/check-jobs', { planId: plan.id });
await until(async () => { session = await host.api(route); return session.execution.checks.at(-1)?.finishedAt; }, 20000);
const failed = session.execution.checks.at(-1);
assert.equal(failed.status, 'failed'); assert.equal(failed.results[0].tests.failed, 5);
assert.equal(failed.results[0].tests.total, 6); assert.equal(failed.results[1].compilation.compiled, 6);
const artifactId = failed.results[0].metricsArtifactId;
const original = await host.api('/artifacts/' + artifactId + '?session=' + session.id);
assert.equal(JSON.parse(original.content).tests.failed, 5);
assertPythonProtected(assert, f.projectRoot, protectedFiles);
await host.close();
const restarted = await launchWorkbench(t, f.data, pythonEnv()), restored = await restarted.api(route);
assert.deepEqual(restored.execution.checks.at(-1), failed);
assert.equal((await restarted.api('/artifacts/' + artifactId + '?session=' + session.id)).content, original.content);
assert.equal(restored.approvals.length, session.approvals.length);
});
test('an isolated three-module Python Task repairs, checks and delivers a source-bound Git report without extra model calls', { timeout: 90000 }, async t => {
const f = fixture(t), protectedFiles = createPythonDeliveryProject(f), source = captureWorkspaceSource(f);
const sourceBytes = Object.fromEntries(Object.keys(pythonFixes).map(relative => [relative, fs.readFileSync(path.join(f.projectRoot, relative))]));
f.store.save(f.session); f.store.close(); let mode = 'repair';
const model = await compatibleFixture(t, (url, input) => {
if (!input?.messages) return { data: [{ id: 'python-http-fixture' }] };
const last = input.messages.findLastIndex(value => value.role === 'user');
const results = input.messages.slice(last + 1).filter(value => value.role === 'tool').map(value => JSON.parse(value.content));
const feedback = input.messages.filter(value => value.role === 'system').map(value => { try { return JSON.parse(value.content.split('\n').at(-1)); } catch { return {}; } }).find(value => value.latest_tools)?.data.entries.map(entry => entry.result);
const latestResults = feedback ?? results, latest = latestResults.at(-1);
// Follow actual returned states: archived older groups change the number of
// visible tool messages and must not make this fixture repeat a call ID.
// Review is now a system action, so the original user turn and completed
// tools remain visible. The applied batch starts a fresh check plan.
if (!latest || mode === 'delivery' && latest.change_set_id) return completion([call('python-plan-' + mode, 'plan_checks', { names: ['python:test', 'python:compile'] })]);
if (latest.id && Array.isArray(latest.checks)) return completion([call('python-check-' + mode, 'run_checks', { plan_id: latest.id })]);
if (latest.check_status === 'failed' && mode === 'repair') return completion(Object.entries(pythonFixes).map(([relative, content], index) => call('python-fix-' + index, 'propose_file', { path: relative, content, reason: 'Repair the actual sealed Python failure' })));
if (latest.change_id && mode === 'repair') return completion([call('python-batch', 'prepare_change_set', { change_ids: latestResults.filter(value => value.change_id).map(value => value.change_id) })]);
if (latest.check_status === 'passed' && mode === 'delivery') return completion([call('python-delivery', 'plan_delivery', { message: 'fix: repair Python workflow in its isolated branch' }),
call('python-after-review', 'run_command', { command: 'Set-Content -LiteralPath AFTER-REVIEW.txt -Value unexpected', reason: 'A later call must not execute after the review boundary' })]);
return completion([], 'Python改动已保存,等待审查。');
});
const host = await launchWorkbench(t, f.data, pythonEnv()), parent = '/sessions/' + f.session.id;
await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'python-http-fixture', apiKey: 'SYNTHETIC-PYTHON-QA-ONLY', maxSteps: 12 });
const workspace = await host.api(parent + '/workspaces', { label: 'Python工程实际交付' });
await host.api(parent + '/workspaces/' + workspace.id + '/create', { fingerprint: workspace.fingerprint });
await until(async () => (await host.api(parent + '/workspaces/' + workspace.id)).status === 'ready', 25000);
const opened = await host.api(parent + '/workspaces/' + workspace.id + '/open', {}), route = '/sessions/' + opened.session.id;
await host.api(route + '/messages', { content: 'Repair only the three Python workflow modules, preserve tests and declarations, execute Python tests and compilation and prepare a reviewed Git delivery.' });
let session;
const wait = async (predicate, { allowAppliedPause = false } = {}) => {
try {
await until(async () => {
session = await host.api(route);
const appliedPause = allowAppliedPause && session.status === 'paused'
&& session.execution.changeSets.at(-1)?.status === 'completed'
&& session.execution.task.delivery?.status === 'files_applied'
&& session.execution.task.verification?.status === 'failed';
if (session.status === 'failed' || session.status === 'paused' && !appliedPause) throw new Error('Unexpected Python Task state: ' + session.status + '/' + session.execution.run.stopReason);
return predicate(session);
}, 30000);
} catch (error) {
fs.writeFileSync(path.resolve('.qa', 'python-api-state-failure.json'), JSON.stringify({ error: error.message, session,
requests: model.requests.map(request => request.input) }, null, 2) + '\n');
throw error;
}
};
await wait(value => value.status === 'awaiting_approval');
assert.equal(session.execution.checks.length, 0); assert.match(session.approvals.at(-1).command, /unittest/);
await host.api(route + '/approvals/' + session.approvals.at(-1).id, { allow: true });
await wait(value => value.status === 'needs_review');
assert.equal(session.execution.checks[0].results[0].tests.failed, 5);
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 wait(value => value.execution.changeSets.at(-1).status === 'completed', { allowAppliedPause: true });
assert.equal(session.status, 'paused'); assert.equal(session.execution.task.verification.status, 'failed');
assert.equal(session.execution.task.delivery.status, 'files_applied');
mode = 'delivery'; await host.api(route + '/continue-reviewed', {});
await wait(value => value.status === 'awaiting_approval');
await host.api(route + '/approvals/' + session.approvals.at(-1).id, { allow: true });
await wait(value => value.execution.run.stopReason === 'git_review');
const passing = session.execution.checks.at(-1), requests = model.requests.length;
assert.equal(passing.status, 'passed'); assert.equal(passing.results[0].tests.passed, 6);
assert.equal(passing.results[1].compilation.compiled, 6); assert.ok(passing.results.every(result => result.evidenceValidated));
assert.equal(session.execution.task.id, taskId); assert.equal(fs.existsSync(path.join(workspace.root, 'AFTER-REVIEW.txt')), false);
await host.api(route + '/delivery-jobs', { planId: session.execution.deliveryPlans.at(-1).id });
await wait(value => value.execution.deliveries.at(-1).status === 'completed' && !!value.execution.deliveries.at(-1).reportArtifactId);
assert.equal(model.requests.length, requests); assert.equal(session.status, 'completed');
const job = session.execution.deliveries.at(-1), artifactRoute = '/artifacts/' + job.reportArtifactId + '?session=' + session.id;
const metadata = await host.api(artifactRoute + '&limit=1');
const download = await fetch(host.url + '/api' + artifactRoute + '&download=1', { headers: { 'X-Workbench-Token': host.bootstrap.token, Connection: 'close' } });
assert.equal(download.status, 200);
const bytes = Buffer.from(await download.arrayBuffer());
assert.equal(bytes.length, metadata.bytes);
assert.equal(createHash('sha256').update(bytes).digest('hex'), metadata.sha256);
const report = JSON.parse(bytes.toString('utf8'));
assert.equal(report.status, 'verified_committed'); assert.equal(report.workspace.workspaceId, workspace.id);
assert.equal(report.workspace.sourceRoot, f.projectRoot); assert.equal(report.task.id, taskId);
assert.deepEqual(job.paths.slice().sort(), Object.keys(pythonFixes).sort());
assert.equal(report.checks.at(-1).results[0].tests.passed, 6); assert.equal(report.checks.at(-1).results[0].pythonEvidence.verified, true);
assert.equal(deliveryGit({ projectRoot: workspace.root }, ['rev-parse', 'HEAD']).trim(), job.commit);
assertWorkspaceSource(assert, f, source, workspace.branch); assertPythonProtected(assert, f.projectRoot, protectedFiles); assertPythonProtected(assert, workspace.root, protectedFiles);
for (const [relative, bytes] of Object.entries(sourceBytes)) assert.ok(fs.readFileSync(path.join(f.projectRoot, relative)).equals(bytes), relative);
assert.equal(fs.existsSync(path.join(workspace.root, 'src/workflow/__pycache__')), false);
assertToolPairs((await host.api(route + '/export')).session.providerMessages);
});