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 } from './helpers.mjs'; import { createEngineFixture } from '../scripts/engine-fixtures.mjs'; test('engine HTTP checks bind the detected installation and current task, page original bytes and preserve them across host restart', async t => { const f = fixture(t), root = createEngineFixture(path.join(f.root, 'sdk 源码 #')); f.store.close(); let host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: root }); const route = '/sessions/' + f.session.id, installation = await host.api('/engines/deepseek/availability'); assert.equal(installation.status, 'available'); assert.equal(installation.capabilities.projectExecution, false); await assert.rejects(host.api(route + '/engines/jobs', { root, identitySha256: '0'.repeat(64) }), /检测结果不一致/); assert.equal((await host.api(route)).execution.engineJobs.length, 0); const started = await host.api(route + '/engines/jobs', { root, identitySha256: installation.identitySha256 }); const jobRoute = route + '/engines/jobs/' + started.job.id; await until(async () => (await host.api(route)).execution.engineJobs.at(-1).reportArtifactId, 15000); const job = await host.api(jobRoute), session = await host.api(route); assert.equal(job.status, 'completed'); assert.equal(job.report, undefined); assert.equal(job.reportSummary.modelRequests, 0); assert.equal(job.taskId, session.execution.task.id); assert.equal(job.runId, session.execution.run.id); assert.equal((await host.api(route + '/engines')).jobs[0].id, job.id); const response = await fetch(host.url + '/api' + jobRoute + '/download', { headers: { 'X-Workbench-Token': host.bootstrap.token } }); assert.equal(response.status, 200); assert.match(response.headers.get('content-disposition'), /attachment/); const text = await response.text(), original = JSON.parse(text); assert.equal(createHash('sha256').update(text).digest('hex'), job.reportSha256); assert.equal(original.owner.sessionId, f.session.id); assert.equal(original.report.responses.length, 6); assert.equal(original.report.cleanup.rootExited, true); let content = '', offset = 0, page; do { page = await host.api(jobRoute + '/report?offset=' + offset + '&limit=1700'); assert.equal(page.sha256, job.reportSha256); content += page.content; offset += page.content.length; } while (page.truncated); assert.equal(content, text); await assert.rejects(host.api(jobRoute + '/report?limit=0'), /读取位置或页长/); const other = await host.api('/sessions', { projectId: f.project.id }); await assert.rejects(host.api('/sessions/' + other.id + '/engines/jobs/' + job.id + '/report'), /不属于/); const wire = path.join(root, 'wire.jsonl'), before = fs.readFileSync(wire); await host.close(); host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: root }); const reopened = await host.api(jobRoute); assert.equal(reopened.status, 'completed'); assert.equal(reopened.reportSha256, job.reportSha256); assert.equal((await host.api(jobRoute + '/report?limit=1700')).content, text.slice(0, 1700)); assert.deepEqual(fs.readFileSync(wire), before); assert.equal((await host.api(route)).execution.task.usage.requests, 0); }); test('HTTP stop seals a pending SDK call, rejects automatic replay and keeps failures attached to the original session', async t => { const f = fixture(t), root = createEngineFixture(path.join(f.root, 'sdk'), { mode: 'hang-init' }); f.store.close(); const host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: root }), route = '/sessions/' + f.session.id; const started = await host.api(route + '/engines/jobs', { root }); await until(async () => (await host.api(route)).execution.engineJobs.at(-1).status === 'initializing', 15000); const other = await host.api('/sessions', { projectId: f.project.id }); await assert.rejects(host.api('/sessions/' + other.id + '/messages', { content: 'owned request' }), /在执行/); await host.api(route + '/engines/stop', {}); const job = await host.api(route + '/engines/jobs/' + started.job.id); assert.equal(job.status, 'cancelled'); assert.equal(job.reportSummary.rootExited, true); assert.equal(job.taskId, started.job.taskId); await assert.rejects(host.api(route + '/engines/jobs', { root }), /重新验证/); const text = fs.readFileSync(path.join(root, 'wire.jsonl'), 'utf8'); assert.equal(text.includes('notifications/cancelled'), false); fs.writeFileSync(path.join(root, 'fixture-options.json'), '{}'); const retry = await host.api(route + '/engines/jobs', { root, retry: true }); await until(async () => (await host.api(route)).execution.engineJobs.at(-1).reportArtifactId, 15000); assert.equal((await host.api(route + '/engines/jobs/' + retry.job.id)).status, 'completed'); assert.notEqual(retry.job.id, job.id); assert.equal((await host.api(route)).execution.engineJobs.length, 2); });