From 29c0e755b40dcdc3c98e46e83c1bbe2b36f4d5f0 Mon Sep 17 00:00:00 2001 From: Agent Workbench Dev Date: Sun, 1 Nov 2026 14:35:41 +0800 Subject: [PATCH] fix: distinguish read-only evidence from file delivery requirements --- CHANGELOG.md | 1 + src/shared/contracts/task-control.mjs | 13 ++++++++++++- tests/python-api.test.mjs | 20 ++++++++++++++++---- tests/request-plan.test.mjs | 8 +++++++- tests/task-control.test.mjs | 15 ++++++++++++++- 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8ec0d4b..5e9741da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 1.20.1 · 真实案例任务控制修复 +- 文件交付只识别明确的正向要求,读取已保存工件和禁止写入不会误触发交付未完成暂停;合理的实际分页继续保留,重复读取保护不放宽。 - Windows停止核验处理进程在快照和用户查询之间退出的竞态,仅经新查询确认进程不存在才判定退出;用户权限失败、查询失败或PID复用继续拒绝。 - 所属宿主启动期间的健康探测超时可在原期限内继续只读等待,每次核对原进程创建/用户/程序身份,最终仍须完整连接证明;超时不重启第二个宿主。 - 只读诊断保留查看原引擎核验、登记工件和REA Evidence的工具,解释故障时仍可取回所属原报告;不重新运行引擎或REA分析,也不把未交付任务标成完成。 diff --git a/src/shared/contracts/task-control.mjs b/src/shared/contracts/task-control.mjs index e74870ae..da466404 100644 --- a/src/shared/contracts/task-control.mjs +++ b/src/shared/contracts/task-control.mjs @@ -20,11 +20,22 @@ export function taskContract(task, lookup = () => null) { const goal = original.contract?.goal ?? original.goal; const skip = /无需(?:任何)?测试|不(?:需要|用|要)(?:进行|做|运行)?测试|\b(?:no tests?(?: needed|required)?|without (?:running )?tests?)\b/i.test(goal); const required = !skip && /(?:运行|执行|通过|修复|补充|添加).{0,12}(?:测试|工程检查)|\b(?:run|pass|fix|add)\b.{0,24}\b(?:tests?|checks?)\b/i.test(goal); - const file = /(?:创建|新建|保存|写入).{0,60}(?:文件|HTML|[\w-]+\.(?:html?|txt|svg|md)\b)|\bcreate\b.{0,60}\b(?:file|html|svg)\b/i.test(goal); + const file = hasFileDeliveryRequest(goal); return { schemaVersion: 1, goal, kind: file ? 'file' : 'general', checks: skip ? 'skip' : required ? 'required' : 'unspecified', ...(original.id !== task.id ? { inheritedFromTaskId: original.id } : {}) }; } +function hasFileDeliveryRequest(goal) { + // Do not connect a past-tense modifier ("保存的原工件") to a later + // prohibition ("不修改文件") and turn a read into a delivery requirement. + const clauses = String(goal).split(/[,,;;。\n]|而是|但是|\bbut\b/i); + const action = /(?:创建|新建|保存|写入)(?!的|过的|好的|后的|下来的).{0,60}?(?:文件|HTML|[\w-]+\.(?:html?|txt|svg|md)\b)|\bcreate\b.{0,60}?\b(?:file|html|svg)\b/ig; + return clauses.some(clause => [...clause.matchAll(action)].some(match => { + const prefix = clause.slice(0, match.index); + return !/(?:不要|无需|不需要|不用|禁止|不得|不必|勿|不允许|不能|不再|避免|不)(?:\s|再|去|进行|执行|直接|自动|擅自|你|任何)*(?:.{0,24}(?:或|和|以及)\s*)?$|\b(?:do not|don't|without|never|no need to|must not|cannot|can't)\b.{0,40}$/i.test(prefix); + })); +} + export function updateTaskContract(task, instruction) { const current = taskContract(task); // Only an explicit new user instruction changes the check policy. diff --git a/tests/python-api.test.mjs b/tests/python-api.test.mjs index 989b1691..fa747afd 100644 --- a/tests/python-api.test.mjs +++ b/tests/python-api.test.mjs @@ -47,7 +47,9 @@ test('an isolated three-module Python Task repairs, checks and delivers a source 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. - if (!latest) return completion([call('python-plan-' + mode, 'plan_checks', { names: ['python:test', 'python:compile'] })]); + // 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) })]); @@ -63,9 +65,17 @@ test('an isolated three-module Python Task repairs, checks and delivers a source 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 => { + const wait = async (predicate, { allowAppliedPause = false } = {}) => { try { - await until(async () => { session = await host.api(route); if (['failed', 'paused'].includes(session.status)) throw new Error('Unexpected Python Task state: ' + session.status + '/' + session.execution.run.stopReason); return predicate(session); }, 30000); + 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'); @@ -79,7 +89,9 @@ test('an isolated three-module Python Task repairs, checks and delivers a source 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'); + 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 }); diff --git a/tests/request-plan.test.mjs b/tests/request-plan.test.mjs index feaf8326..a8935855 100644 --- a/tests/request-plan.test.mjs +++ b/tests/request-plan.test.mjs @@ -186,7 +186,10 @@ test('HTTP failure and explicit same-Task resume reuse exact completed results a const record = [...f.store.core.context_plan.values()].at(-1); assert.equal(record.requestSha256, requestMetrics(input).sha256); assert.ok(['prepared', 'requesting'].includes(record.status)); assertToolPairs(input.messages); if (requests.length === 1) return completion([call('only-effect', 'run_command', { command: 'node -e "const fs=require(\'node:fs\'); const file=\'request-effects.txt\'; fs.writeFileSync(file,String(Number(fs.existsSync(file)?fs.readFileSync(file,\'utf8\'):0)+1));"', reason: 'Record one controlled fixture effect.' })]); - if (requests.length < 6) return completion([call('read-round-' + requests.length, 'read_file', { path: 'README.md' })]); + if (requests.length < 6) { + const start = (requests.length - 2) * 100 + 1; + return completion([call('read-round-' + requests.length, 'read_file', { path: 'README.md', start_line: start, end_line: start + 99 })]); + } if (!failed) { failed = true; return { status: 503, body: { error: { message: 'controlled request failure' } } }; } assert.ok(input.messages[0].content.includes('Do not replay the completed command.')); return completion([], 'Original task completed from saved evidence.'); @@ -196,6 +199,9 @@ test('HTTP failure and explicit same-Task resume reuse exact completed results a runtime.start(f.session.id, 'Read evidence. Do not replay the completed command.'); await until(() => f.session.approvals.some(value => value.status === 'pending')); runtime.decide(f.session.id, f.session.approvals.at(-1).id, true); await runtime.active.get(f.session.id).promise; assert.equal(f.session.status, 'failed'); + assert.equal(model.requests.length, 6); assert.equal(failed, true); + const pages = f.session.events.filter(event => event.tool === 'read_file'); + assert.deepEqual(pages.map(event => [event.input.start_line, event.input.end_line]), [[1, 100], [101, 200], [201, 300], [301, 400]]); const original = structuredClone(f.session.providerMessages), taskId = f.session.taskId, firstRun = f.session.runId; assert.ok([...f.store.core.context_plan.values()].some(plan => plan.archivedGroups > 0)); assert.equal([...f.store.core.context_plan.values()].at(-1).status, 'failed'); diff --git a/tests/task-control.test.mjs b/tests/task-control.test.mjs index fa64e893..5cbda207 100644 --- a/tests/task-control.test.mjs +++ b/tests/task-control.test.mjs @@ -5,7 +5,7 @@ import path from 'node:path'; import { Runtime } from '../src/server/runtime.mjs'; import { Provider } from '../src/server/provider.mjs'; import { applyChange } from '../src/server/files.mjs'; -import { isDiagnosticQuestion } from '../src/shared/contracts/task-control.mjs'; +import { isDiagnosticQuestion, taskContract } from '../src/shared/contracts/task-control.mjs'; import { fixture, call, assertToolPairs } from './helpers.mjs'; const answer = content => ({ message: { role: 'assistant', content }, usage: { prompt_tokens: 11, completion_tokens: 3 } }); @@ -15,6 +15,19 @@ test('operational questions are diagnostic while explicit repair requests remain for (const text of ['先修复系统', '为什么失败,请帮我修复', '这个错误怎么回事,然后修一下', 'fix this error', '继续完成']) assert.equal(isDiagnosticQuestion(text), false, text); }); +test('file delivery requires a positive instruction, not saved evidence or a prohibited write', () => { + for (const goal of ['读取保存的原工件,分段取得真实依据,不执行命令或修改文件。', + '读取保存过的 HTML 文件并解释内容', '不要创建文件,只分析结果', '读取工件,不执行命令或创建文件', + 'Do not create a file; explain the evidence.', 'Read the saved file without creating HTML.']) { + assert.equal(taskContract({ goal }).kind, 'general', goal); + } + for (const goal of ['创建一个 HTML 文件,无需测试', '新建 result.txt', '把结果保存为 report.md 文件', + '请写入 SVG 文件', 'Create an HTML file without running tests.', '不要执行命令,创建一个 HTML 文件', + '文件不存在时创建 report.md 文件', '不要修改原有文件而是新建 HTML 文件']) { + assert.equal(taskContract({ goal }).kind, 'file', goal); + } +}); + test('an unsubstantiated text claim cannot complete a requested file delivery', async t => { const f = harness(t, () => answer('HTML 已创建完成。')); f.runtime.start(f.session.id, '创建一个 HTML 文件,无需测试'); await f.wait();