From f0eecbda472c3f4b57af5a5cbef3e26a5e86b662 Mon Sep 17 00:00:00 2001 From: Agent Workbench Dev Date: Sat, 31 Oct 2026 05:04:55 +0800 Subject: [PATCH] feat: connect controlled engines to owned Tasks, approvals and budget ledger --- docs/QA-1.14.md | 14 +- docs/RETROSPECTIVE-1.14.md | 2 +- docs/controlled-engine-kernel-evidence.json | 110 +++++ docs/controlled-engine-live-bridge.tap | 52 +++ docs/controlled-engine-worker-compatible.tap | 412 ++++++++++++++++++ ...ontrolled-engine-worker-first-spec-log.txt | 59 +++ docs/controlled-engine-worker-kernel.tap | 160 +++++++ findings.md | 3 + progress.md | 3 + src/core/engines/controlled-installation.mjs | 6 +- src/core/engines/controlled-plugin.mjs | 1 + src/core/engines/controlled-session.mjs | 39 +- src/core/engines/engine-runner.mjs | 268 ++++++++++++ src/core/tasks/worker-manager.mjs | 44 +- src/server/app.mjs | 17 + src/server/provider.mjs | 4 +- src/server/runtime.mjs | 28 +- src/server/store.mjs | 28 +- src/shared/contracts/engine-execution.mjs | 113 +++++ src/shared/contracts/engines.mjs | 3 + src/shared/contracts/execution.mjs | 2 +- src/shared/contracts/workers.mjs | 16 +- task_plan.md | 6 +- tests/controlled-engine.test.mjs | 2 + tests/engine-worker-http.test.mjs | 44 ++ tests/engine-worker.test.mjs | 195 +++++++++ tests/fixtures/controlled-worker.mjs | 55 +++ 27 files changed, 1653 insertions(+), 33 deletions(-) create mode 100644 docs/controlled-engine-kernel-evidence.json create mode 100644 docs/controlled-engine-live-bridge.tap create mode 100644 docs/controlled-engine-worker-compatible.tap create mode 100644 docs/controlled-engine-worker-first-spec-log.txt create mode 100644 docs/controlled-engine-worker-kernel.tap create mode 100644 src/core/engines/engine-runner.mjs create mode 100644 src/shared/contracts/engine-execution.mjs create mode 100644 tests/engine-worker-http.test.mjs create mode 100644 tests/engine-worker.test.mjs create mode 100644 tests/fixtures/controlled-worker.mjs diff --git a/docs/QA-1.14.md b/docs/QA-1.14.md index 67dc8500..8159cba8 100644 --- a/docs/QA-1.14.md +++ b/docs/QA-1.14.md @@ -12,6 +12,18 @@ 实际Harness加本地固定模型夹具通过:完整启动目录核对、两次代理模型请求、一次原工具调用、prompt入队收据、所属原活动输出与正常退出;0真实模型请求、0stderr。原输入/协议/通知/桥数据与退出保存在controlled-engine-native-first.json及对应-log.txt。本轮只读取独立夹具README,没有开放用户工程或现有真实连接。 +## B:执行账本与权限接入 + +原引擎工具进入所属Runtime的校验/审批/原结果账本,调用ID以引擎Run命名空间保存原sourceCallId。下一轮直接核对savedToolResult并封存真正反馈的工件;读取/提案/检查与工作台既有范围一致。引擎来源、所属叠加输入在接收工具、开始工具和批准后执行前复核,等待审批期间的变化不能使用旧许可。模型Key留在工作台Provider,原Native输入与实际兼容Chat请求分别保存。 + +首轮17项15通过/2断言失败,原spec输出保持;按既有check_status/message契约修正。第二轮相关兼容67/67通过;随后最新内核25/25通过,包括等待审批来源变化、损坏报告拒绝交付、原模型预算恢复单次记账、未知核对和完整HTTP原记录分页/下载/归属。各轮分别保存,不拼接统计。界面代码和原生三工程尚待独立验收。 + +关闭原SDK进程后的桥请求也拒绝进入模型/工具回调,追加7/7桥专项通过。原记录分别为controlled-engine-worker-first-spec-log.txt、controlled-engine-worker-compatible.tap、controlled-engine-worker-kernel.tap和controlled-engine-live-bridge.tap;来源字节和当前内核输入索引为controlled-engine-kernel-evidence.json。 + +本轮模型实际尝试由引擎桥记账,外层合成工具步骤不再重复记账。重启对原reservedPerAttempt/maxAttempts工件复核后保守记录未封存尝试,第二次恢复不重复计账;损坏预算不能减少累计用量。正常输出前关闭并封存引擎,worker成果仍需原报告、实际提案/检查和父审查。 + +已加入启动/运行中原创建时间和后代身份观察,关闭后复核完整所属树;活后代或观察失败保留unknown。原SDK会话不自动resume,人工核对独立保存review,原输出不改写为成功。下一步实际Harness+HTTP固定模型三工程及桌面/390px交付验证。 + ## 尚待验收 -B:受限桥与自有Task/Run审批/账本接入;C:独立工程、累计预算、进程树和父审查;D:恢复/桌面/390px;E:Purge来源/兼容/同题对照;F:完整回归、独立发行及保留状态升级。正式4318继续使用不可变1.13.0。 +C:实际Harness独立工程/父审查;D:恢复/桌面/390px;E:Purge来源/兼容/同题对照;F:完整回归、独立发行及保留状态升级。正式4318继续使用不可变1.13.0。 diff --git a/docs/RETROSPECTIVE-1.14.md b/docs/RETROSPECTIVE-1.14.md index 61dc1a98..8f140193 100644 --- a/docs/RETROSPECTIVE-1.14.md +++ b/docs/RETROSPECTIVE-1.14.md @@ -5,7 +5,7 @@ | 切片 | 当前实现/验证 | 遗留与下一步 | | --- | --- | --- | | A:版本/协议 | 新受控安装身份、七份关键运行器、独立profile叠加/桥;7/7边界专项和实际Harness固定模型2请求/1工具/收据/wait/正常退出通过 | 原SDK没有单会话取消或反向审批;桥不能单凭启动成功宣称工程交付 | -| B:工具桥 | 原生项目/终端/模型/重试插件禁用,appReady核对完整工具schema;原进程/Run/SDK/来源、输入上限和重复ID守卫 | 接入自有执行账本、文件审查及固定检查,保持原工具身份和未知状态 | +| B:工具桥 | 原目录/身份桥接自有Runtime/账本,命名空间保留sourceCallId,反馈核对真实结果;等待审批的来源复核、单次尝试计账、报告损坏/预算恢复/HTTP分页已通过最新25项;原67项兼容保持 | 实际Harness三工程及最终冻结发行单独验收;已关闭原进程的桥请求追加拒绝 | | C–F | 尚未完成 | 独立工程/停止恢复/父审查、真实界面、Purge同题对照、完整发行与状态保持升级 | 前置研究确认sdk-minimal仍含danger-full-access策略和本机工具,不能把profile名字当权限边界;采用宿主受限桥和实际目录检查。Harness现有DeepSeek provider使用Messages API,本工作台现有连接是兼容Chat层;自有代理provider保持两层分离,使引擎不取得模型Key,也不把一种协议冒充另一种协议。 diff --git a/docs/controlled-engine-kernel-evidence.json b/docs/controlled-engine-kernel-evidence.json new file mode 100644 index 00000000..46ff32a3 --- /dev/null +++ b/docs/controlled-engine-kernel-evidence.json @@ -0,0 +1,110 @@ +{ + "OriginalLogs": [ + { + "Passed": 15, + "Tests": 17, + "Sha256": "30a580a96ccf133f1dc7eb854555f3083bfab1fcb18ba0dbd19699d6f90a3871", + "Target": "docs\\controlled-engine-worker-first-spec-log.txt", + "Source": ".qa\\controlled-worker-first.tap", + "Bytes": 3808 + }, + { + "Passed": 67, + "Tests": 67, + "Sha256": "aab191b90b1afeba5a04e6d1b791cb6ba88e7d9fbe27846b5197329abcd2fa72", + "Target": "docs\\controlled-engine-worker-compatible.tap", + "Source": ".qa\\controlled-worker-second.tap", + "Bytes": 18885 + }, + { + "Passed": 25, + "Tests": 25, + "Sha256": "79ddc25b37d7fbdf644e5610b7dc265a742d98443ce9bb51ddc2462a704f80c3", + "Target": "docs\\controlled-engine-worker-kernel.tap", + "Source": ".qa\\controlled-worker-final-kernel.tap", + "Bytes": 7231 + }, + { + "Passed": 7, + "Tests": 7, + "Sha256": "66f50b9d67268a39b31ca71049682443715385aedc698fc0349450610d9c2a7f", + "Target": "docs\\controlled-engine-live-bridge.tap", + "Source": ".qa\\controlled-bridge-live-guards.tap", + "Bytes": 2169 + } + ], + "FullReleaseAccepted": false, + "Stage": "1.14 B kernel", + "RealModelRequests": 0, + "CurrentKernelInputs": [ + { + "Sha256": "ced3adcea9badc6542e7cd123dc8ea1034f6010b594f058bed5a5e110dfa8c0b", + "Path": "src/core/engines/engine-runner.mjs" + }, + { + "Sha256": "5fae9b63d4ddea31b523fb529d8954766123785aeffe31e067b16325f200f9e9", + "Path": "src/core/engines/controlled-session.mjs" + }, + { + "Sha256": "ffe6bae64285f2e47128d5227f091f2346d923ff9a170b39728f64350b1a47e3", + "Path": "src/core/engines/controlled-plugin.mjs" + }, + { + "Sha256": "6a65e9c34889e093fe64772199582b115ba7f6b1c0965a5c3507f1098c9019be", + "Path": "src/core/engines/controlled-installation.mjs" + }, + { + "Sha256": "3d4e35659422d63805b90502e80a95cd4a2367c22e8758251479b5a8895db605", + "Path": "src/shared/contracts/engine-execution.mjs" + }, + { + "Sha256": "cd0a6378682182b8c29884e725a284a7b4f32c1355067883321de071ff113bfb", + "Path": "src/shared/contracts/engines.mjs" + }, + { + "Sha256": "7f5f50db8996d0449601dc6f219e4b241895745ce52acaa835636d5435ace0f7", + "Path": "src/shared/contracts/workers.mjs" + }, + { + "Sha256": "6d25f6048a5fc42358a067d64cd4715e35c19ef99b28c85d739b50e73bdbb544", + "Path": "src/shared/contracts/execution.mjs" + }, + { + "Sha256": "3a1284e55b6fa8f3cf9a2c42ab448673c8aff874577fab209db7629473614461", + "Path": "src/core/tasks/worker-manager.mjs" + }, + { + "Sha256": "855da237e17877212ca909967cd79d818ed38c7a00251f6733e4c727ed72d505", + "Path": "src/server/app.mjs" + }, + { + "Sha256": "4697e0bd0266d305e202c796cf3436a97701fae1bba2434d3663c9b89e29be1b", + "Path": "src/server/store.mjs" + }, + { + "Sha256": "9b362c96e5c78d77afd43ac79b28c1edaae5b4f753f3cf079fef6605914507d9", + "Path": "src/server/runtime.mjs" + }, + { + "Sha256": "2d22386c679b666de63efcc43d8a5703b166358667d48b17cc544c0cfddc6393", + "Path": "src/server/provider.mjs" + }, + { + "Sha256": "c2d344fe8b795789073a16848c910a96fdd95103caf1a6092fcd7a3f0e5f5fbb", + "Path": "tests/controlled-engine.test.mjs" + }, + { + "Sha256": "2af6091fb1de61c93d79a305a7c6fdb6ae7d5c09d9e677319cab8e5fe9337ba7", + "Path": "tests/engine-worker.test.mjs" + }, + { + "Sha256": "ed3880a1becaa99d836f2c85a76967a6d443b7ec2c71beec8e26360c1040266b", + "Path": "tests/engine-worker-http.test.mjs" + }, + { + "Sha256": "a9454456d6747bdff786089d2b7992b873a35dd2bdb54735f1dced4f43cc610e", + "Path": "tests/fixtures/controlled-worker.mjs" + } + ], + "CreatedAt": "2026-10-30T21:04:53.2660093Z" +} \ No newline at end of file diff --git a/docs/controlled-engine-live-bridge.tap b/docs/controlled-engine-live-bridge.tap new file mode 100644 index 00000000..ada23d22 --- /dev/null +++ b/docs/controlled-engine-live-bridge.tap @@ -0,0 +1,52 @@ +TAP version 13 +# Subtest: controlled installation pins the native kernel and bridge independently of the existing connection-probe identity +ok 1 - controlled installation pins the native kernel and bridge independently of the existing connection-probe identity + --- + duration_ms: 924.3909 + type: 'test' + ... +# Subtest: controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract +ok 2 - controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract + --- + duration_ms: 0.5062 + type: 'test' + ... +# Subtest: owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations +ok 3 - owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations + --- + duration_ms: 5.4906 + type: 'test' + ... +# Subtest: bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch +ok 4 - bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch + --- + duration_ms: 773.2805 + type: 'test' + ... +# Subtest: bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect +ok 5 - bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect + --- + duration_ms: 694.599 + type: 'test' + ... +# Subtest: bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity +ok 6 - bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity + --- + duration_ms: 652.8104 + type: 'test' + ... +# Subtest: stopped and oversized bridge requests never reach a local model or tool callback +ok 7 - stopped and oversized bridge requests never reach a local model or tool callback + --- + duration_ms: 656.527 + type: 'test' + ... +1..7 +# tests 7 +# suites 0 +# pass 7 +# fail 0 +# cancelled 0 +# skipped 0 +# todo 0 +# duration_ms 3799.7591 diff --git a/docs/controlled-engine-worker-compatible.tap b/docs/controlled-engine-worker-compatible.tap new file mode 100644 index 00000000..849b30bc --- /dev/null +++ b/docs/controlled-engine-worker-compatible.tap @@ -0,0 +1,412 @@ +TAP version 13 +# Subtest: controlled installation pins the native kernel and bridge independently of the existing connection-probe identity +ok 1 - controlled installation pins the native kernel and bridge independently of the existing connection-probe identity + --- + duration_ms: 816.2291 + type: 'test' + ... +# Subtest: controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract +ok 2 - controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract + --- + duration_ms: 0.5492 + type: 'test' + ... +# Subtest: owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations +ok 3 - owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations + --- + duration_ms: 5.4234 + type: 'test' + ... +# Subtest: bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch +ok 4 - bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch + --- + duration_ms: 950.2421 + type: 'test' + ... +# Subtest: bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect +ok 5 - bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect + --- + duration_ms: 679.3481 + type: 'test' + ... +# Subtest: bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity +ok 6 - bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity + --- + duration_ms: 697.0703 + type: 'test' + ... +# Subtest: stopped and oversized bridge requests never reach a local model or tool callback +ok 7 - stopped and oversized bridge requests never reach a local model or tool callback + --- + duration_ms: 590.4988 + type: 'test' + ... +# Subtest: engine HTTP checks bind the detected installation and current task, page original bytes and preserve them across host restart +ok 8 - engine HTTP checks bind the detected installation and current task, page original bytes and preserve them across host restart + --- + duration_ms: 2573.4285 + type: 'test' + ... +# Subtest: HTTP stop seals a pending SDK call, rejects automatic replay and keeps failures attached to the original session +ok 9 - HTTP stop seals a pending SDK call, rejects automatic replay and keeps failures attached to the original session + --- + duration_ms: 2958.6899 + type: 'test' + ... +# Subtest: native text and call IDs preserve original result pairing while unsupported content fails closed +ok 10 - native text and call IDs preserve original result pairing while unsupported content fails closed + --- + duration_ms: 0.7762 + type: 'test' + ... +# Subtest: one engine child freezes real installation identity before creating tasks and rejects a changed source +ok 11 - one engine child freezes real installation identity before creating tasks and rejects a changed source + --- + duration_ms: 5899.1837 + type: 'test' + ... +# Subtest: owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs +ok 12 - owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs + --- + duration_ms: 17601.082 + type: 'test' + ... +# Subtest: forged native scope cannot change a parent file and a final completion claim cannot replace worker proof +ok 13 - forged native scope cannot change a parent file and a final completion claim cannot replace worker proof + --- + duration_ms: 9063.3297 + type: 'test' + ... +# Subtest: duplicate native IDs are rejected without replaying a saved tool +ok 14 - duplicate native IDs are rejected without replaying a saved tool + --- + duration_ms: 8481.8966 + type: 'test' + ... +# Subtest: native input exceeding the context budget is retained and sends no model request +ok 15 - native input exceeding the context budget is retained and sends no model request + --- + duration_ms: 7140.3817 + type: 'test' + ... +# Subtest: stop retains a late reported model usage once and executes no tool from that output +ok 16 - stop retains a late reported model usage once and executes no tool from that output + --- + duration_ms: 7565.0223 + type: 'test' + ... +# Subtest: a source change after the original model intent stops before native tools execute +ok 17 - a source change after the original model intent stops before native tools execute + --- + duration_ms: 7435.7032 + type: 'test' + ... +# Subtest: missing or noncompleted turn end cannot be adopted even with an owned receipt and assistant text +ok 18 - missing or noncompleted turn end cannot be adopted even with an owned receipt and assistant text + --- + duration_ms: 0.5515 + type: 'test' + ... +# Subtest: closed root with an original live descendant remains unknown and retains its process tree +ok 19 - closed root with an original live descendant remains unknown and retains its process tree + --- + duration_ms: 0.2928 + type: 'test' + ... +# Subtest: host recovery reserves original pending attempts exactly once and explicit review preserves unknown outcomes +ok 20 - host recovery reserves original pending attempts exactly once and explicit review preserves unknown outcomes + --- + duration_ms: 10865.7804 + type: 'test' + ... +# Subtest: corrupted model reservation cannot reduce recovered budget or permit a new engine request +ok 21 - corrupted model reservation cannot reduce recovered budget or permit a new engine request + --- + duration_ms: 8811.645 + type: 'test' + ... +# Subtest: engine detection seals the actual CLI, SDK, profile and Node and rejects mixed versions without launch +ok 22 - engine detection seals the actual CLI, SDK, profile and Node and rejects mixed versions without launch + --- + duration_ms: 731.2372 + type: 'test' + ... +# Subtest: owned engine probe waits for real ready identity, verifies six native responses and saves immutable task evidence +ok 23 - owned engine probe waits for real ready identity, verifies six native responses and saves immutable task evidence + --- + duration_ms: 1384.3111 + type: 'test' + ... +# Subtest: an invalid startup nonce is rejected before initialize and preserves the process close and partial report +ok 24 - an invalid startup nonce is rejected before initialize and preserves the process close and partial report + --- + duration_ms: 1236.3923 + type: 'test' + ... +# Subtest: SDK identity and session directory mismatches retain raw responses and never become verified +ok 25 - SDK identity and session directory mismatches retain raw responses and never become verified + --- + duration_ms: 3841.0501 + type: 'test' + ... +# Subtest: cancel during a pending initialize closes only the owned process without sending an unsupported MCP cancellation +ok 26 - cancel during a pending initialize closes only the owned process without sending an unsupported MCP cancellation + --- + duration_ms: 1337.7515 + type: 'test' + ... +# Subtest: cancel before readiness sends no protocol request and a user retry creates a different process and record +ok 27 - cancel before readiness sends no protocol request and a user retry creates a different process and record + --- + duration_ms: 1766.3237 + type: 'test' + ... +# Subtest: source or scratch changes and unexpected model traffic fail after preserving the actual observed result +ok 28 - source or scratch changes and unexpected model traffic fail after preserving the actual observed result + --- + duration_ms: 3876.5429 + type: 'test' + ... +# Subtest: unowned idle notifications stay quarantined and server requests receive a rejection without granting operations +ok 29 - unowned idle notifications stay quarantined and server requests receive a rejection without granting operations + --- + duration_ms: 2487.8291 + type: 'test' + ... +# Subtest: engine probe environment excludes parent secrets and NODE_OPTIONS while preserving operating system paths +ok 30 - engine probe environment excludes parent secrets and NODE_OPTIONS while preserving operating system paths + --- + duration_ms: 1244.7198 + type: 'test' + ... +# Subtest: foreign sessions and altered reports or task bindings cannot reuse verified engine evidence +ok 31 - foreign sessions and altered reports or task bindings cannot reuse verified engine evidence + --- + duration_ms: 1117.1807 + type: 'test' + ... +# Subtest: restart never replays an active engine check or stops a historical PID, and keeps prior task state +ok 32 - restart never replays an active engine check or stops a historical PID, and keeps prior task state + --- + duration_ms: 1359.5664 + type: 'test' + ... +# Subtest: model argument validation and runtime activity do not grant external engine prompt or project execution +ok 33 - model argument validation and runtime activity do not grant external engine prompt or project execution + --- + duration_ms: 661.7343 + type: 'test' + ... +# Subtest: a follow-up question receives the actual failed engine check from its original Task and keeps its report unchanged +ok 34 - a follow-up question receives the actual failed engine check from its original Task and keeps its report unchanged + --- + duration_ms: 1925.1735 + type: 'test' + ... +# Subtest: HTTP worker identities require a current sealed plan, reject overrides and close unused plans without touching source +ok 35 - HTTP worker identities require a current sealed plan, reject overrides and close unused plans without touching source + --- + duration_ms: 10680.8387 + type: 'test' + ... +# Subtest: HTTP unknown recovery only accepts the original evidence SHA and preserves changed source for explicit retention +ok 36 - HTTP unknown recovery only accepts the original evidence SHA and preserves changed source for explicit retention + --- + duration_ms: 13802.8412 + type: 'test' + ... +# Subtest: verified isolated outputs form a new parent batch, rerun real checks and commit only reviewed source +ok 37 - verified isolated outputs form a new parent batch, rerun real checks and commit only reviewed source + --- + duration_ms: 34905.4242 + type: 'test' + ... +# Subtest: overlapping worker paths are explicit conflicts even if each independent task checks successfully +ok 38 - overlapping worker paths are explicit conflicts even if each independent task checks successfully + --- + duration_ms: 23076.2678 + type: 'test' + ... +# Subtest: source edits after isolated work preserve the owner file and invalidate collection without proposing a merge +ok 39 - source edits after isolated work preserve the owner file and invalidate collection without proposing a merge + --- + duration_ms: 21509.2095 + type: 'test' + ... +# Subtest: changed child bytes after parent preview block the entire batch before any parent write +ok 40 - changed child bytes after parent preview block the entire batch before any parent write + --- + duration_ms: 28009.8619 + type: 'test' + ... +# Subtest: corrupt original check logs cannot become collected outputs or a parent review +ok 41 - corrupt original check logs cannot become collected outputs or a parent review + --- + duration_ms: 21518.3373 + type: 'test' + ... +# Subtest: stopping a parent accounts late responses and executes none of their proposed tools +ok 42 - stopping a parent accounts late responses and executes none of their proposed tools + --- + duration_ms: 10380.3694 + type: 'test' + ... +# Subtest: unreported interrupted requests reserve estimated usage and the total deadline cannot reset on resume +ok 43 - unreported interrupted requests reserve estimated usage and the total deadline cannot reset on resume + --- + duration_ms: 10919.4728 + type: 'test' + ... +# Subtest: a response exceeding the cumulative token allocation stops before registering or executing its tools +ok 44 - a response exceeding the cumulative token allocation stops before registering or executing its tools + --- + duration_ms: 10330.442 + type: 'test' + ... +# Subtest: cumulative tool allocation rejects the entire returned batch before any tool side effect +ok 45 - cumulative tool allocation rejects the entire returned batch before any tool side effect + --- + duration_ms: 10120.0901 + type: 'test' + ... +# Subtest: single response 16 tool ceiling rejects the entire returned batch before any tool side effect +ok 46 - single response 16 tool ceiling rejects the entire returned batch before any tool side effect + --- + duration_ms: 10288.9937 + type: 'test' + ... +# Subtest: consumed request steps remain consumed across explicit worker resumes +ok 47 - consumed request steps remain consumed across explicit worker resumes + --- + duration_ms: 11051.708 + type: 'test' + ... +# Subtest: a saved completed response with missing usage is conservatively accounted exactly once during recovery +ok 48 - a saved completed response with missing usage is conservatively accounted exactly once during recovery + --- + duration_ms: 8958.148 + type: 'test' + ... +# Subtest: an unexecuted second workspace plan resumes its original identity without recreating the first checkout +ok 49 - an unexecuted second workspace plan resumes its original identity without recreating the first checkout + --- + duration_ms: 9815.1841 + type: 'test' + ... +# Subtest: a verified interrupted parent proposal resumes sealing without duplicating or replacing its original change +ok 50 - a verified interrupted parent proposal resumes sealing without duplicating or replacing its original change + --- + duration_ms: 32012.3475 + type: 'test' + ... +# Subtest: recovery attaches a sealed parent batch saved before its group reference without creating another batch +ok 51 - recovery attaches a sealed parent batch saved before its group reference without creating another batch + --- + duration_ms: 28124.7263 + type: 'test' + ... +# Subtest: explicitly retaining an unexecuted parent merge cancels its batch and frees the next parent goal +ok 52 - explicitly retaining an unexecuted parent merge cancels its batch and frees the next parent goal + --- + duration_ms: 28818.7589 + type: 'test' + ... +# Subtest: unknown collaboration blocks ordinary proposals but permits only original batch recovery and rollback +ok 53 - unknown collaboration blocks ordinary proposals but permits only original batch recovery and rollback + --- + duration_ms: 31832.0692 + type: 'test' + ... +# Subtest: after a restart the batch application lazily reinstates the worker result guard before any parent write +ok 54 - after a restart the batch application lazily reinstates the worker result guard before any parent write + --- + duration_ms: 27876.2984 + type: 'test' + ... +# Subtest: unknown original tool inputs must match before acknowledgement, and reviewed checkpoint IDs preserve the unknown fact +ok 55 - unknown original tool inputs must match before acknowledgement, and reviewed checkpoint IDs preserve the unknown fact + --- + duration_ms: 11211.8706 + type: 'test' + ... +# Subtest: a parent stop observes and waits for a manually approved child check and its real descendant process +ok 56 - a parent stop observes and waits for a manually approved child check and its real descendant process + --- + duration_ms: 10807.5113 + type: 'test' + ... +# Subtest: a real host crash preserves unknown checks, refuses a still living owned PID and never replays a model or tool +ok 57 - a real host crash preserves unknown checks, refuses a still living owned PID and never replays a model or tool + --- + duration_ms: 19019.2047 + type: 'test' + ... +# Subtest: a persisted child initialization resumes the original control run after a failure before its worker record +ok 58 - a persisted child initialization resumes the original control run after a failure before its worker record + --- + duration_ms: 9889.5335 + type: 'test' + ... +# Subtest: failed request intent persistence never dispatches a worker request or changes source +ok 59 - failed request intent persistence never dispatches a worker request or changes source + --- + duration_ms: 10482.6794 + type: 'test' + ... +# Subtest: failed usage persistence rolls back the uncommitted usage and conservatively reserves one unknown request without tools +ok 60 - failed usage persistence rolls back the uncommitted usage and conservatively reserves one unknown request without tools + --- + duration_ms: 10040.0269 + type: 'test' + ... +# Subtest: a child file change after observation prevents acknowledgement even when the tool inputs did not change +ok 61 - a child file change after observation prevents acknowledgement even when the tool inputs did not change + --- + duration_ms: 10021.0669 + type: 'test' + ... +# Subtest: worker scopes and allocated budgets reject aliases, extra permissions and aggregate overflow +ok 62 - worker scopes and allocated budgets reject aliases, extra permissions and aggregate overflow + --- + duration_ms: 653.4276 + type: 'test' + ... +# Subtest: two sequential Git workspaces freeze a new parent goal, separate owned tasks and preserve all source bytes +ok 63 - two sequential Git workspaces freeze a new parent goal, separate owned tasks and preserve all source bytes + --- + duration_ms: 8956.1164 + type: 'test' + ... +# Subtest: real isolated changes, approvals and npm checks produce original owned outputs, not model completion claims +ok 64 - real isolated changes, approvals and npm checks produce original owned outputs, not model completion claims + --- + duration_ms: 22042.2963 + type: 'test' + ... +# Subtest: forged worker tools cannot execute terminal or Git even when absent from the advertised tool list +ok 65 - forged worker tools cannot execute terminal or Git even when absent from the advertised tool list + --- + duration_ms: 9099.6576 + type: 'test' + ... +# Subtest: a completion-only model pauses each worker without fabricated changes or checks +ok 66 - a completion-only model pauses each worker without fabricated changes or checks + --- + duration_ms: 10901.1151 + type: 'test' + ... +# Subtest: restart retains frozen scope and marks interrupted parent and workers unknown without sending requests +ok 67 - restart retains frozen scope and marks interrupted parent and workers unknown without sending requests + --- + duration_ms: 8780.2684 + type: 'test' + ... +1..67 +# tests 67 +# suites 0 +# pass 67 +# fail 0 +# cancelled 0 +# skipped 0 +# todo 0 +# duration_ms 643018.4941 diff --git a/docs/controlled-engine-worker-first-spec-log.txt b/docs/controlled-engine-worker-first-spec-log.txt new file mode 100644 index 00000000..460ee61e --- /dev/null +++ b/docs/controlled-engine-worker-first-spec-log.txt @@ -0,0 +1,59 @@ +✔ controlled installation pins the native kernel and bridge independently of the existing connection-probe identity (870.6639ms) +✔ controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract (0.5375ms) +✔ owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations (5.2037ms) +✔ bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch (639.515ms) +✔ bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect (675.3767ms) +✔ bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity (629.76ms) +✔ stopped and oversized bridge requests never reach a local model or tool callback (713.8051ms) +✔ native text and call IDs preserve original result pairing while unsupported content fails closed (0.8087ms) +✔ one engine child freezes real installation identity before creating tasks and rejects a changed source (6165.6107ms) +✖ owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs (17171.0179ms) +✖ forged native scope cannot change a parent file and a final completion claim cannot replace worker proof (8831.888ms) +✔ duplicate native IDs are rejected without replaying a saved tool (8616.6411ms) +✔ native input exceeding the context budget is retained and sends no model request (7015.7346ms) +✔ stop retains a late reported model usage once and executes no tool from that output (7397.2983ms) +✔ a source change after the original model intent stops before native tools execute (7301.4165ms) +✔ missing or noncompleted turn end cannot be adopted even with an owned receipt and assistant text (0.6823ms) +✔ closed root with an original live descendant remains unknown and retains its process tree (0.3306ms) +ℹ tests 17 +ℹ suites 0 +ℹ pass 15 +ℹ fail 2 +ℹ cancelled 0 +ℹ skipped 0 +ℹ todo 0 +ℹ duration_ms 66242.3459 + +✖ failing tests: + +test at tests\engine-worker.test.mjs:41:1 +✖ owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs (17171.0179ms) + AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: + + assert.ok(context.requests.some(request => request.messages.some(message => message.role === 'tool' && JSON.parse(message.content).status === 'passed'))) + + at TestContext. (file:///D:/Ai-project/REA%E9%80%86%E5%90%91/Agent-Workbench/tests/engine-worker.test.mjs:63:10) + at async Test.run (node:internal/test_runner/test:1409:7) + at async Test.processPendingSubtests (node:internal/test_runner/test:974:7) { + generatedMessage: true, + code: 'ERR_ASSERTION', + actual: false, + expected: true, + operator: '==', + diff: 'simple' + } + +test at tests\engine-worker.test.mjs:68:1 +✖ forged native scope cannot change a parent file and a final completion claim cannot replace worker proof (8831.888ms) + AssertionError [ERR_ASSERTION]: The "string" argument must be of type string. Received type undefined (undefined) + at TestContext. (file:///D:/Ai-project/REA%E9%80%86%E5%90%91/Agent-Workbench/tests/engine-worker.test.mjs:76:78) + at process.processTicksAndRejections (node:internal/process/task_queues:104:5) + at async Test.run (node:internal/test_runner/test:1409:7) + at async Test.processPendingSubtests (node:internal/test_runner/test:974:7) { + generatedMessage: true, + code: 'ERR_ASSERTION', + actual: undefined, + expected: /范围/, + operator: 'match', + diff: 'simple' + } diff --git a/docs/controlled-engine-worker-kernel.tap b/docs/controlled-engine-worker-kernel.tap new file mode 100644 index 00000000..9778c07a --- /dev/null +++ b/docs/controlled-engine-worker-kernel.tap @@ -0,0 +1,160 @@ +TAP version 13 +# Subtest: controlled installation pins the native kernel and bridge independently of the existing connection-probe identity +ok 1 - controlled installation pins the native kernel and bridge independently of the existing connection-probe identity + --- + duration_ms: 823.5923 + type: 'test' + ... +# Subtest: controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract +ok 2 - controlled profiles disable every native project/terminal provider and native model/retry route using the actual include patch contract + --- + duration_ms: 0.5387 + type: 'test' + ... +# Subtest: owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations +ok 3 - owned tool catalogues seal full schemas canonically and reject duplicate or malformed declarations + --- + duration_ms: 5.7335 + type: 'test' + ... +# Subtest: bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch +ok 4 - bridge authentication, current Run, process, SDK session and browser-origin checks reject before dispatch + --- + duration_ms: 769.9066 + type: 'test' + ... +# Subtest: bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect +ok 5 - bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect + --- + duration_ms: 654.6547 + type: 'test' + ... +# Subtest: bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity +ok 6 - bridge model requests bind the exact catalogue, route and output bound and preserve raw request/result identity + --- + duration_ms: 699.4788 + type: 'test' + ... +# Subtest: stopped and oversized bridge requests never reach a local model or tool callback +ok 7 - stopped and oversized bridge requests never reach a local model or tool callback + --- + duration_ms: 665.6117 + type: 'test' + ... +# Subtest: engine HTTP checks bind the detected installation and current task, page original bytes and preserve them across host restart +ok 8 - engine HTTP checks bind the detected installation and current task, page original bytes and preserve them across host restart + --- + duration_ms: 2732.0424 + type: 'test' + ... +# Subtest: HTTP stop seals a pending SDK call, rejects automatic replay and keeps failures attached to the original session +ok 9 - HTTP stop seals a pending SDK call, rejects automatic replay and keeps failures attached to the original session + --- + duration_ms: 3160.8457 + type: 'test' + ... +# Subtest: controlled engine HTTP freezes one child, pages original owned input/report, rejects foreign reads and prevents changed-source reuse +ok 10 - controlled engine HTTP freezes one child, pages original owned input/report, rejects foreign reads and prevents changed-source reuse + --- + duration_ms: 11219.5604 + type: 'test' + ... +# Subtest: native text and call IDs preserve original result pairing while unsupported content fails closed +ok 11 - native text and call IDs preserve original result pairing while unsupported content fails closed + --- + duration_ms: 0.8848 + type: 'test' + ... +# Subtest: one engine child freezes real installation identity before creating tasks and rejects a changed source +ok 12 - one engine child freezes real installation identity before creating tasks and rejects a changed source + --- + duration_ms: 5971.1886 + type: 'test' + ... +# Subtest: owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs +ok 13 - owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs + --- + duration_ms: 18042.8416 + type: 'test' + ... +# Subtest: forged native scope cannot change a parent file and a final completion claim cannot replace worker proof +ok 14 - forged native scope cannot change a parent file and a final completion claim cannot replace worker proof + --- + duration_ms: 9566.7856 + type: 'test' + ... +# Subtest: duplicate native IDs are rejected without replaying a saved tool +ok 15 - duplicate native IDs are rejected without replaying a saved tool + --- + duration_ms: 8715.6494 + type: 'test' + ... +# Subtest: native input exceeding the context budget is retained and sends no model request +ok 16 - native input exceeding the context budget is retained and sends no model request + --- + duration_ms: 7280.4503 + type: 'test' + ... +# Subtest: stop retains a late reported model usage once and executes no tool from that output +ok 17 - stop retains a late reported model usage once and executes no tool from that output + --- + duration_ms: 7614.7364 + type: 'test' + ... +# Subtest: a source change after the original model intent stops before native tools execute +ok 18 - a source change after the original model intent stops before native tools execute + --- + duration_ms: 7676.3322 + type: 'test' + ... +# Subtest: an engine source change while approval is pending invalidates the old permission before any check starts +ok 19 - an engine source change while approval is pending invalidates the old permission before any check starts + --- + duration_ms: 9807.1356 + type: 'test' + ... +# Subtest: missing or noncompleted turn end cannot be adopted even with an owned receipt and assistant text +ok 20 - missing or noncompleted turn end cannot be adopted even with an owned receipt and assistant text + --- + duration_ms: 0.6841 + type: 'test' + ... +# Subtest: closed root with an original live descendant remains unknown and retains its process tree +ok 21 - closed root with an original live descendant remains unknown and retains its process tree + --- + duration_ms: 0.2756 + type: 'test' + ... +# Subtest: host recovery reserves original pending attempts exactly once and explicit review preserves unknown outcomes +ok 22 - host recovery reserves original pending attempts exactly once and explicit review preserves unknown outcomes + --- + duration_ms: 11081.577 + type: 'test' + ... +# Subtest: corrupted model reservation cannot reduce recovered budget or permit a new engine request +ok 23 - corrupted model reservation cannot reduce recovered budget or permit a new engine request + --- + duration_ms: 9010.1322 + type: 'test' + ... +# Subtest: HTTP worker identities require a current sealed plan, reject overrides and close unused plans without touching source +ok 24 - HTTP worker identities require a current sealed plan, reject overrides and close unused plans without touching source + --- + duration_ms: 10348.3848 + type: 'test' + ... +# Subtest: HTTP unknown recovery only accepts the original evidence SHA and preserves changed source for explicit retention +ok 25 - HTTP unknown recovery only accepts the original evidence SHA and preserves changed source for explicit retention + --- + duration_ms: 14049.5917 + type: 'test' + ... +1..25 +# tests 25 +# suites 0 +# pass 25 +# fail 0 +# cancelled 0 +# skipped 0 +# todo 0 +# duration_ms 140419.1483 diff --git a/findings.md b/findings.md index 4b555b41..cbc4e5dd 100644 --- a/findings.md +++ b/findings.md @@ -2,6 +2,9 @@ ## 2026-10-31 1.14 受控引擎前置核对 +- B进一步审查发现仅在桥收到工具时核对来源,无法覆盖用户等待检查审批期间的变化。现增加Runtime开始工具和批准后执行之前的所属引擎/叠加输入复核;变化旧许可不能启动检查。实际模型事件与引擎轮次分开,原模型请求/预算工件参与恢复计账与成果核对。 +- B草稿审查确认外层Runtime与引擎model回调会双重记账,worker.output又早于引擎关闭封存;需按实际请求单次计账并先finish再成果校验。现有Store.recordResponse只改变内存,必须与每次attempt标记同次save保持恢复一致。Owned process tree复用src/core/check-runner.mjs,不能只检查根进程已退出。 +- 恢复查询的一次不存在checks/check-runner路径和一次无效findings标题补丁均未写入产品;已按实际import/标题修正。公开源码分析沿用普通文件工具,不重新跑A原验证。 - 原程序验证确认自有Cordis bridge与LLM adapter可在原sdk-minimal通过实际完整目录/唯一provider核对,2次本地模型请求和1次read_file后观测原收据、assistant/message和turn/end(completed),正常退出/空stderr。7/7边界专项通过。原DeepSeek provider使用Messages API,与现有兼容Chat层不同,采用自有代理provider桥保持协议和凭据分层。 - Native RequestMessage实测为system/user/assistant/tool的内容块,原工具结果带toolCallId;后续桥须按原字段转换并用真实调用账本反馈,不能从裁剪后的发送历史拼造原结果。Include patch实测契约为{id,disabled:true},不会使用推测的select/update字段。 - 新查询错误包括几处不存在的lib/types/patch路径与PowerShell foreach后直接管道的解析失败;已按实际目录和先收集变量再管道修正,未启动模型或改变用户资源。 diff --git a/progress.md b/progress.md index c5978433..fafdb5a5 100644 --- a/progress.md +++ b/progress.md @@ -2,6 +2,9 @@ ## 2026-10-31 1.14 受控外部引擎阶段 +- 新相关整轮67/67通过、0失败/取消/跳过,原TAP controlled-worker-second.tap保存;含原引擎探针和worker/停止/恢复/合并/HTTP兼容。该轮期间开发了界面及追加「等待审批期间来源变化」补强,最新补强会单独验收,F按最终冻结输入另验。实际Harness三工程HTTP夹具验收脚本已准备,尚未运行;源文件/TS静态检查通过,正式1.13保持。 +- B/C首轮17项15通过/2失败,原输出controlled-worker-first.tap保留(Node默认spec格式)。两个失败来自专项断言错误:run_checks实际返回completed与check_status:passed,错误文本字段是message;已读实际源码修正,没有改低产品完成门槛。引擎计划、真实工具反馈/单次记账、停止迟到输出、来源变化及所属树专项已实现;后续用显式TAP新轮验证。池既有上限8次,原模型输入验证同步尊重该封存上限。 +- 接续a01c418:A原证据沿用;B草稿尚未测试且未接Runtime/Store/WorkerManager。先完成实际模型请求单次记账、引擎封存与worker成果的顺序、所属进程树和恢复未知状态,再做独立工程/UI/Purge/发行。规划文件保持当前状态,正式4318不重启。恢复时一次将.qa纳入文件枚举产生过宽输出,改为限定src/tests/scripts及具体证据文件。 - 切片A:受控安装身份/profile/原SDK/桥已实现,7/7边界专项及实际Harness本地固定模型2请求/1次工具/收据/原活动/wait/正常退出通过;0真实模型请求,原模型输入/工具/通知/退出完整保存controlled-native-01e1d8d0及docs副本。正式1.13/连接保持;继续B自有账本、C独立工程和停止恢复。 - 用户授权继续全量开发并及时回顾。接续60b63bc的干净工作区,保留正式1.13/PID23172和当前模型连接,开始PLAN-1.14六切片;本地源码/夹具与独立工程先行,真实模型任务继续暂缓。 - 源码核对确认sdk-minimal是完整Cordis树,仍含本机文件/终端工具、danger-full-access;原SDK只有收据/事件/wait/resume/进程关闭,不能假定原生审批或单会话取消。下一步固定实际profile/工具注册调用链,构建自有受限叠加插件,禁用原生项目写入与终端。 diff --git a/src/core/engines/controlled-installation.mjs b/src/core/engines/controlled-installation.mjs index a8909c2d..115b9c6f 100644 --- a/src/core/engines/controlled-installation.mjs +++ b/src/core/engines/controlled-installation.mjs @@ -1,14 +1,14 @@ import path from 'node:path'; import { pathToFileURL } from 'node:url'; -import { engineDigest, engineError } from '../../shared/contracts/engines.mjs'; +import { engineDigest, engineError, engineCanonicalDigest } from '../../shared/contracts/engines.mjs'; import { resolveDeepSeekInstallation, assertDeepSeekInstallation, engineFileIdentity } from './deepseek-installation.mjs'; -import { CONTROLLED_BRIDGE_PLUGIN, CONTROLLED_DISABLED_PLUGINS, canonicalEngineValue } from './controlled-plugin.mjs'; +import { CONTROLLED_BRIDGE_PLUGIN, CONTROLLED_DISABLED_PLUGINS } from './controlled-plugin.mjs'; export const CONTROLLED_ENGINE_SCOPE = 'deepseek-workbench-engine-v1'; export const CONTROLLED_CRITICAL_FILES = Object.freeze(['packages/core/tools/lib/index.js', 'packages/core/agent/lib/index.js', 'packages/core/agent-loop/lib/index.js', 'packages/core/system-prompt/lib/index.js', 'packages/llm/llm/lib/index.js', 'packages/llm/llm-retry/lib/index.js', 'vendor/include/lib/index.js']); -export const controlledDigest = value => engineDigest(canonicalEngineValue(value)); +export const controlledDigest = engineCanonicalDigest; export function resolveControlledInstallation(appDirectory = process.cwd(), root) { const base = resolveDeepSeekInstallation(appDirectory, root); diff --git a/src/core/engines/controlled-plugin.mjs b/src/core/engines/controlled-plugin.mjs index a6fb4476..6a361995 100644 --- a/src/core/engines/controlled-plugin.mjs +++ b/src/core/engines/controlled-plugin.mjs @@ -59,6 +59,7 @@ export async function apply(ctx, config) { tools: options.tools ?? [], toolHistory: options.toolHistory ?? null, model: options.model, maxTokens: options.maxTokens ?? config.maxOutputTokens }, options.signal); const message = result.message; + if (result.finishReason === 'length') throw new Error('Owned model output reached its bound; incomplete tools are not executed.'); if (!message || message.role !== 'assistant' || typeof message.content !== 'string' || message.tool_calls !== undefined && !Array.isArray(message.tool_calls)) throw new Error('Invalid owned model result.'); let index = 0; diff --git a/src/core/engines/controlled-session.mjs b/src/core/engines/controlled-session.mjs index 2019314c..47891e24 100644 --- a/src/core/engines/controlled-session.mjs +++ b/src/core/engines/controlled-session.mjs @@ -6,6 +6,7 @@ import { DeepSeekAdapter } from './deepseek-adapter.mjs'; import { engineFileIdentity } from './deepseek-installation.mjs'; import { engineError } from '../../shared/contracts/engines.mjs'; import { isId } from '../../shared/contracts/execution.mjs'; +import { readOwnedProcessTree } from '../owned-process-tree.mjs'; import { CONTROLLED_BRIDGE_PLUGIN } from './controlled-plugin.mjs'; import { assertControlledInstallation, controlledDigest, controlledPatch, controlledToolCatalogue, CONTROLLED_ENGINE_SCOPE } from './controlled-installation.mjs'; @@ -18,6 +19,7 @@ export class ControlledDeepSeekSession extends DeepSeekAdapter { constructor(installation, options = {}) { super(installation, options); this.maximumFrameBytes = options.maximumFrameBytes ?? 4 * 1024 * 1024; this.pending = new Set(); this.closed = false; + this.processTreeReader = options.processTreeReader ?? readOwnedProcessTree; } async open({ directory, cwd, owner, sdkSessionId, tools, config, signal, onModel, onTool, onState = () => {}, onNotification = () => {} }) { for (const field of ['sessionId', 'projectId', 'taskId', 'runId', 'jobId']) if (!isId(owner?.[field])) throw engineError('受控引擎所属身份无效。', 400); @@ -59,6 +61,15 @@ export class ControlledDeepSeekSession extends DeepSeekAdapter { { timeoutMs: this.timeoutMs, maximumFrameBytes: this.maximumFrameBytes, cancellationMethod: null }); this.report.process = { pid: this.client.pid, connectionId: this.client.id, startedAt: this.client.startedAt, ownership: 'one-process-per-engine-run' }; onState('starting', this.report.process); + this.publishOwnership = () => onState('ownership', this.report.process); + try { await this.captureTree(); } catch (error) { this.report.process.treeObservationError = error.message; throw error; } + this.treeTimer = setInterval(() => { + if (this.closed || this.treePending) return; + this.treePending = this.captureTree().catch(error => { + this.report.process.treeObservationError = error.message; this.client.fail(error); + this.publishOwnership(); + }).finally(() => { this.treePending = null; }); + }, 1200); this.treeTimer.unref(); this.client.on('server-request', request => { this.report.rejectedServerRequests.push({ id: request.id, method: request.method, parametersSha256: controlledDigest(request.params ?? {}) }); this.client.fail(engineError('原SDK反向请求未在受控契约中开放。')); @@ -94,8 +105,19 @@ export class ControlledDeepSeekSession extends DeepSeekAdapter { if (engineFileIdentity(input.path).sha256 !== input.sha256) throw engineError('所属叠加插件或启动输入发生变化。'); } } + async captureTree(publish = true) { + const info = this.report.process, tree = await this.processTreeReader(info); + if (tree.supported) { + if (tree.root) info.rootCreatedAt ??= tree.root.createdAt; + const known = new Map((info.descendants ?? []).map(value => [value.pid + ':' + value.createdAt, value])); + for (const value of tree.processes) if (value.pid !== info.pid) known.set(value.pid + ':' + value.createdAt, value); + info.descendants = [...known.values()].sort((left, right) => left.pid - right.pid); + info.treeRemainingPids = tree.processes.map(value => value.pid); info.treeObservedAt = at(); + } + if (publish) this.publishOwnership?.(); return tree; + } async handleBridge(request, response, { authentication, onModel, onTool, signal }) { - if (this.closed || signal?.aborted || request.method !== 'POST' || !['/model', '/tool'].includes(request.url) + if (this.closed || this.client?.exited === true || signal?.aborted || request.method !== 'POST' || !['/model', '/tool'].includes(request.url) || request.headers.origin !== undefined || request.headers['content-type'] !== 'application/json' || !equalSecret(request.headers.authorization, authentication)) throw engineError('桥请求不属于当前受控Run。', 403); const chunks = []; let size = 0; @@ -153,6 +175,7 @@ export class ControlledDeepSeekSession extends DeepSeekAdapter { if (!assistant || !Array.isArray(assistant.data?.message?.content)) throw engineError('所属活动没有完整assistant原消息。'); const ends = activity.filter(event => event?.type === 'turn/end'); this.report.activity = { receiptIndex: receipt, endEvents: ends }; + if (ends.length !== 1 || ends[0].data?.reason?.kind !== 'completed') throw engineError('所属活动没有唯一的正常结束事件,原输出不能视为完成。'); this.report.finalText = assistant.data.message.content.filter(block => block.type === 'text').map(block => block.text).join(''); return this.report.finalText; } @@ -160,21 +183,33 @@ export class ControlledDeepSeekSession extends DeepSeekAdapter { if (this.closePromise) return this.closePromise; const interrupted = force || this.parentSignal?.aborted === true; this.closed = true; + clearInterval(this.treeTimer); this.bridgeController?.abort(new Error('所属引擎正在关闭。')); return this.closePromise = (async () => { let failure = null; if (this.client) { + await this.treePending; if (!interrupted && !this.client.exited) { try { await this.client.request('shutdown', {}, { timeoutMs: 1000 }); } catch (error) { failure = error; } } try { this.report.cleanup = await this.client.close({ force: interrupted || !!failure }); } catch (error) { failure ??= error; } + try { + const tree = await this.captureTree(false); + this.report.cleanup ??= { rootExited: false }; + this.report.cleanup.treeConfirmed = tree.supported ? tree.processes.length === 0 && !!this.report.process.rootCreatedAt + && !this.report.process.treeObservationError : this.report.cleanup.rootExited; + this.report.cleanup.remainingProcesses = tree.processes; + } catch (error) { + failure ??= error; this.report.cleanup ??= { rootExited: false }; this.report.cleanup.treeConfirmed = false; + } this.report.trace = this.client.trace; this.report.stderr = this.client.stderr; } if (this.guard) { this.guard.closeAllConnections(); await new Promise(resolve => this.guard.close(resolve)); } await Promise.allSettled([...this.pending]); if (!interrupted && this.report.launchInputs) { try { this.verifyLaunch(); } catch (error) { failure ??= error; } } if (!interrupted && this.report.cleanup?.exit?.code !== 0) failure ??= engineError('所属引擎未正常退出。'); - this.report.finishedAt = at(); this.report.status = failure || !this.report.cleanup?.rootExited ? 'unknown' : interrupted ? 'cancelled' : 'closed'; + this.report.finishedAt = at(); this.report.status = this.client && (!this.report.cleanup?.rootExited || !this.report.cleanup?.treeConfirmed) + ? 'unknown' : failure ? 'failed' : interrupted ? 'cancelled' : 'closed'; if (failure) this.report.error = failure.message; return this.report; })(); diff --git a/src/core/engines/engine-runner.mjs b/src/core/engines/engine-runner.mjs new file mode 100644 index 00000000..8ef5acf4 --- /dev/null +++ b/src/core/engines/engine-runner.mjs @@ -0,0 +1,268 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import os from 'node:os'; +import { randomUUID } from 'node:crypto'; +import { ControlledDeepSeekSession } from './controlled-session.mjs'; +import { resolveControlledInstallation, assertControlledInstallation, controlledDigest } from './controlled-installation.mjs'; +import { engineRunInput, engineRunReport, verifyEngineInput, nativeMessagesToChat } from '../../shared/contracts/engine-execution.mjs'; +import { engineError } from '../../shared/contracts/engines.mjs'; +import { modelSnapshot, accountedTokens, checkBudget, BudgetExceeded } from '../../shared/contracts/execution.mjs'; +import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs'; +import { workerForSession } from '../../shared/contracts/workers.mjs'; + +const at = () => new Date().toISOString(); +const deferred = () => { + let resolve, reject; const promise = new Promise((yes, no) => { resolve = yes; reject = no; }); + promise.catch(() => {}); return { promise, resolve, reject }; +}; + +export class EngineRunner { + constructor(store, provider, { appDirectory = process.cwd(), sessionFactory = installation => new ControlledDeepSeekSession(installation), directoryRoot = null } = {}) { + this.store = store; this.provider = provider; this.appDirectory = appDirectory; this.sessionFactory = sessionFactory; + this.directoryRoot = directoryRoot; this.drivers = new Map(); + } + availability(root) { return resolveControlledInstallation(this.appDirectory, root); } + selected(input) { + const installation = this.availability(input.root); + if (installation.controlledStatus !== 'available' || installation.controlledSha256 !== input.controlledSha256) { + throw engineError('受控引擎与刚才检测的程序/插件身份不一致,请重新检测后制定新计划。'); + } + return installation; + } + assertCurrent(session, run) { + const driver = this.drivers.get(run.id); + if (!driver || driver.session !== session || driver.run !== run) throw engineError('原引擎执行不属于当前Run。'); + driver.assertInput(); driver.native?.verifyLaunch?.(); + } + complete(session, run, messages, progress, planned, tools) { + let driver = this.drivers.get(run.id); + if (!driver) { + if (workerForSession(this.store, session) !== run.worker || !run.worker?.engine) throw engineError('外部引擎只能在所属受限子工程中执行。'); + assertControlledInstallation(run.worker.engine); + driver = new EngineRunDriver(this, session, run, planned, tools); this.drivers.set(run.id, driver); + } + return driver.complete(messages, progress).catch(error => { driver.fail(error); throw error; }); + } + async finish(session, run) { + const driver = this.drivers.get(run.id); if (!driver) return; + try { await driver.close(); } finally { this.drivers.delete(run.id); } + if (driver.job.requiresVerification) { + session.status = 'paused'; run.stopReason = 'engine_verification'; + this.store.message(session, 'system', '所属外部引擎的原进程或结果待核对。请在父工程核对原输入与现场后继续。'); + } else if (driver.job.status !== 'completed' && session.status === 'completed') { + session.status = 'paused'; run.stopReason = 'engine_incomplete'; + this.store.message(session, 'system', '原引擎没有完成全部协议和结果核验,不能交付。'); + } + return driver.job; + } +} + +class EngineRunDriver { + constructor(manager, session, run, seedPlan, tools) { + this.manager = manager; this.store = manager.store; this.provider = manager.provider; this.session = session; this.run = run; + this.queue = []; this.waiter = null; this.pendingCalls = new Map(); this.finished = false; + const base = path.resolve(manager.directoryRoot ?? path.join(os.tmpdir(), 'AgentWorkbench-Engine-Runs')); + fs.mkdirSync(base, { recursive: true }); if (fs.lstatSync(base).isSymbolicLink()) throw engineError('引擎运行目录不能是链接。'); + const id = randomUUID(); + this.job = { schemaVersion: 1, id, sessionId: session.id, projectId: session.projectId, taskId: run.taskId, runId: run.id, + workerId: run.worker.id, collaborationId: run.worker.collaborationId, installation: structuredClone(run.worker.engine), + sdkSessionId: randomUUID(), directory: path.join(base, id), model: modelSnapshot(run.config), budget: { ...run.record.budget, maxToolCalls: run.worker.budget.maxToolCalls }, + tools: structuredClone(tools), sourceContextPlanId: seedPlan.id, sourceRequestSha256: seedPlan.requestSha256, + status: 'queued', startedAt: at(), finishedAt: null, requiresVerification: false, verified: false, native: null, requests: [], calls: [] }; + const artifact = this.store.artifactStore.write(session, run.record, engineRunInput(this.job), { kind: 'engine-run-input' }); + this.store.core.artifact.set(artifact.id, artifact); this.job.inputArtifactId = artifact.id; this.job.inputSha256 = artifact.sha256; + run.record.engine = 'deepseek-workbench'; run.record.engineRunId = id; + this.save('queued'); + } + save(phase) { this.store.putRecord('engine_run', this.session, this.job, 'engine_run.' + phase); } + assertInput() { + verifyEngineInput(this.store, this.session, this.job); assertControlledInstallation(this.job.installation); + if (this.session.runId !== this.run.id || workerForSession(this.store, this.session) !== this.run.worker) throw engineError('引擎原Run已变化。'); + } + publish(value) { + if (this.waiter) { const waiter = this.waiter; this.waiter = null; waiter.resolve(value); } else this.queue.push(value); + } + async next() { + this.run.controller.signal.throwIfAborted(); + if (this.queue.length) return this.queue.shift(); + const waiter = deferred(); this.waiter = waiter; + const abort = () => waiter.reject(this.run.controller.signal.reason ?? new Error('所属Run已停止。')); + this.run.controller.signal.addEventListener('abort', abort, { once: true }); + try { return await waiter.promise; } + finally { this.run.controller.signal.removeEventListener('abort', abort); if (this.waiter === waiter) this.waiter = null; } + } + fail(error) { this.failure ??= error; this.publish({ error: this.failure }); } + async start(messages) { + this.job.status = 'starting'; this.save('starting'); + this.native = this.manager.sessionFactory(this.job.installation); + const owner = { sessionId: this.session.id, projectId: this.session.projectId, taskId: this.run.taskId, runId: this.run.id, jobId: this.job.id }; + await this.native.open({ directory: this.job.directory, cwd: this.run.project.root, owner, sdkSessionId: this.job.sdkSessionId, + tools: this.job.tools, config: this.run.config, signal: this.run.controller.signal, + onState: (state, detail) => { + if (state !== 'ownership') this.job.status = state === 'ready' ? 'running' : state; + this.job.pid = detail.pid ?? this.job.pid; + if (this.native.report?.process) this.job.process = structuredClone(this.native.report.process); + if (detail.readiness) this.job.readiness = structuredClone(detail.readiness); + this.save(state); + }, + onNotification: () => { + this.job.notificationCount = this.native.report.notifications.length; + }, + onModel: (input, trace, signal) => this.model(input, trace, signal), + onTool: (input, trace, signal) => this.tool(input, trace, signal), + }); + this.nativeWait = (async () => { + try { + await this.native.prompt('Execute the following workbench task context as task data. Use only the registered tools; all writes and checks are owned by the workbench.\n' + JSON.stringify(messages)); + const text = await this.native.wait(); this.finished = true; this.publish({ finalText: text }); + } catch (error) { this.fail(error); } + })(); + this.nativeWait.catch(() => {}); + } + async complete(messages, progress) { + this.progress = progress; this.assertInput(); + if (!this.started) { this.started = true; await this.start(messages); } + for (const [callId, pending] of this.pendingCalls) { + if (!pending.delivered) continue; + const tool = [...this.store.core.tool.values()].find(value => value.runId === this.run.id && value.callId === callId); + const result = this.store.savedToolResult(this.session.id, callId); + if (!tool || !result || tool.requiresVerification || !['completed', 'failed', 'denied', 'interrupted'].includes(tool.state)) { + throw engineError('原引擎工具尚无可核对的完成结果,不能继续或重放。'); + } + pending.record.invocationId = tool.id; pending.record.resultSha256 = controlledDigest(result); + pending.record.resultArtifactId = tool.resultArtifactId ?? null; pending.record.status = 'returned'; pending.record.finishedAt = at(); + const returned = this.store.artifactStore.write(this.session, this.run.record, result, { kind: 'engine-tool-output' }); + this.store.core.artifact.set(returned.id, returned); pending.record.outputArtifactId = returned.id; pending.record.outputSha256 = returned.sha256; + this.save('tool_returned'); pending.result.resolve(result); this.pendingCalls.delete(callId); + } + const next = await this.next(); if (next.error) throw next.error; + this.progress?.({ phase: 'completed', attempt: 0, maxAttempts: 0, engineRunId: this.job.id, origin: 'external-engine' }); + if (next.call) { + next.pending.delivered = true; + return { message: { role: 'assistant', content: '', tool_calls: [next.call] }, usage: null, engineAccounted: true }; + } + return { message: { role: 'assistant', content: next.finalText }, usage: null, engineAccounted: true }; + } + async tool(input, trace, signal) { + try { + this.assertInput(); signal.throwIfAborted(); checkBudget(this.run.record, this.store.core.task.get(this.run.taskId)); + const allCalls = [...this.store.core.engine_run.values()].filter(job => job.workerId === this.run.worker.id).reduce((total, job) => total + job.calls.length, 0); + if (allCalls >= this.run.worker.budget.maxToolCalls) throw new BudgetExceeded('worker_tool_limit', '原引擎工具次数已到所属子任务累计上限。'); + const callId = this.job.id + ':' + input.callId; + if (this.pendingCalls.has(callId) || this.job.calls.some(call => call.callId === callId)) throw engineError('原引擎调用已经登记,不会再次执行。'); + const record = { sourceCallId: input.callId, callId, name: input.name, argumentsSha256: controlledDigest(input.arguments), + bridgeTraceId: trace.id, status: 'received', receivedAt: at() }; + const artifact = this.store.artifactStore.write(this.session, this.run.record, { owner: trace.id, ...input }, { kind: 'engine-tool-input' }); + this.store.core.artifact.set(artifact.id, artifact); record.inputArtifactId = artifact.id; record.inputSha256 = artifact.sha256; + const pending = { record, result: deferred(), delivered: false }; this.job.calls.push(record); this.pendingCalls.set(callId, pending); + this.job.status = 'waiting_tool'; this.save('tool_received'); + const abort = () => pending.result.reject(signal.reason ?? new Error('所属引擎工具已停止。')); + signal.addEventListener('abort', abort, { once: true }); + this.publish({ call: { id: callId, type: 'function', function: { name: input.name, arguments: JSON.stringify(input.arguments) } }, pending }); + try { return await pending.result.promise; } finally { signal.removeEventListener('abort', abort); } + } catch (error) { this.fail(error); throw error; } + } + async model(input, trace, signal) { + let request, dispatched = false; + if (this.modelActive) { const error = engineError('所属引擎已有模型请求,不能并发扩展封存预算。'); this.fail(error); throw error; } + this.modelActive = true; + try { + this.assertInput(); signal.throwIfAborted(); + const task = this.store.core.task.get(this.run.taskId); checkBudget(this.run.record, task); + const messages = nativeMessagesToChat(input), tools = input.tools.map(tool => ({ type: 'function', function: tool })); + const config = { ...this.run.config, maxOutputTokens: input.maxTokens }, body = completionBody(config, messages, tools), metrics = requestMetrics(body); + const maximumAttempts = config.mode === 'demo' ? 1 : config.modelRouting?.maxAttempts ?? 1; + const credit = Math.min(this.run.record.budget.maxTokens - accountedTokens(this.run.record.usage), task.budget.maxTokens - accountedTokens(task.usage)); + const estimate = metrics.estimatedInputTokens + 128 + config.maxOutputTokens; + const attempts = Math.min(maximumAttempts, Math.floor(credit / estimate), this.run.worker.budget.maxSteps - task.usage.requests); + if (metrics.estimatedInputTokens + 128 + config.maxOutputTokens > config.contextLimit || attempts < 1) { + throw new BudgetExceeded('engine_input_budget', '原引擎完整模型输入或累计尝试超出封存预算,未发送模型请求。'); + } + const selected = config.modelRouting ? { ...config, modelRouting: { ...config.modelRouting, maxAttempts: attempts } } : config; + request = { id: randomUUID(), bridgeTraceId: trace.id, sourceSha256: controlledDigest(input), requestSha256: metrics.sha256, + requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens, reservedPerAttempt: estimate, + maxAttempts: attempts, accountedAttempts: 0, observedAttempts: 0, status: 'prepared', startedAt: at() }; + const owner = { sessionId: this.session.id, projectId: this.session.projectId, taskId: this.run.taskId, runId: this.run.id }; + const plan = { ...request, ...owner }; + const artifact = this.store.artifactStore.write(this.session, this.run.record, + JSON.parse(this.provider.redact(JSON.stringify({ schemaVersion: 1, owner, native: input, body, plan }))), { kind: 'engine-model-input' }); + this.store.core.artifact.set(artifact.id, artifact); request.inputArtifactId = artifact.id; request.inputSha256 = artifact.sha256; + this.job.requests.push(request); + const modelEvent = this.store.event(this.session, { kind: 'model', source: 'engine_model_bridge', engineRunId: this.job.id, engineModelRequestId: request.id, + taskId: this.job.taskId, runId: this.job.runId, label: '引擎请求模型服务', phase: 'running', startedAt: at(), + output: '原引擎完整输入已封存,模型服务由工作台代理。' }); + request.eventId = modelEvent.id; request.status = 'dispatched'; request.dispatchedAt = at(); this.job.status = 'running'; this.save('model_intent'); + dispatched = true; + const response = await this.provider.complete(selected, messages, this.run.project, signal, progress => { + if (progress.phase === 'running') { + const attempt = progress.attempt ?? 1; + if (!Number.isSafeInteger(attempt) || attempt < 1 || attempt > request.maxAttempts) throw engineError('模型尝试超出原引擎封存预算。'); + request.observedAttempts = Math.max(request.observedAttempts, attempt); + } + if (progress.connectionId) { request.connectionId = progress.connectionId; request.actualModel = progress.model; } + modelEvent.phase = progress.phase; modelEvent.attempts = progress.attempt; modelEvent.connectionId = progress.connectionId ?? null; + modelEvent.actualModel = progress.model ?? config.model; + modelEvent.label = progress.switching ? '引擎切换模型池连接' : progress.phase === 'failed' ? '引擎模型请求失败' : progress.phase === 'completed' ? '引擎模型响应已收到' : '引擎请求模型服务'; + if (['completed', 'failed'].includes(progress.phase)) modelEvent.finishedAt = at(); + this.save('model_progress'); + }, plan, tools, { attempts: 1 }); + const count = Math.max(1, request.observedAttempts); + request.actualAttemptsFinal = count; request.responseReceivedAt = at(); request.status = 'responded'; + while (request.accountedAttempts < count - 1) { + request.accountedAttempts++; this.store.recordResponse(this.session, this.run.record, null, estimate); + } + request.accountedAttempts++; request.usage = response.usage ?? null; + this.store.recordResponse(this.session, this.run.record, response.usage, estimate); + this.save('model_usage'); + const output = JSON.parse(this.provider.redact(JSON.stringify(response))), saved = this.store.artifactStore.write(this.session, this.run.record, output, { kind: 'engine-model-output' }); + this.store.core.artifact.set(saved.id, saved); request.responseArtifactId = saved.id; request.responseSha256 = saved.sha256; + request.status = 'completed'; request.finishedAt = at(); this.save('model_completed'); + modelEvent.phase = 'completed'; modelEvent.finishedAt = at(); modelEvent.output = '原模型响应与尝试用量已保存。'; this.save('model_event_completed'); + checkBudget(this.run.record, task); signal.throwIfAborted(); return output; + } catch (error) { + this.fail(error); + if (request) { + request.actualAttemptsFinal ??= request.observedAttempts || request.maxAttempts; + if (dispatched) while (request.accountedAttempts < request.actualAttemptsFinal) { + request.accountedAttempts++; this.store.recordResponse(this.session, this.run.record, null, request.reservedPerAttempt); + } + request.status = signal.aborted ? 'interrupted' : 'failed'; request.error = this.provider.redact(error.message); request.finishedAt = at(); this.save('model_failed'); + const event = this.session.events.find(value => value.id === request.eventId); + if (event) { event.phase = request.status; event.finishedAt = at(); event.output = request.error; this.save('model_event_failed'); } + } + throw error; + } finally { + this.modelActive = false; + } + } + async close() { + if (this.closePromise) return this.closePromise; + return this.closePromise = this.finishClose(); + } + async finishClose() { + this.job.status = 'closing'; this.save('closing'); + for (const pending of this.pendingCalls.values()) pending.result.reject(new Error('所属引擎已结束,未返回结果保留原状态。')); + let native; + try { native = this.native ? await this.native.close({ force: !this.finished || !!this.failure || this.run.controller.signal.aborted }) : null; } + catch (error) { this.failure ??= error; } + if (this.nativeWait) await this.nativeWait; + this.job.native = native ? JSON.parse(this.provider.redact(JSON.stringify(native))) : null; + const launched = !!this.native?.client || !!native?.process?.pid || !!this.job.pid; + const unknown = launched && (!native?.cleanup?.rootExited || native?.cleanup?.treeConfirmed === false || native?.status === 'unknown'); + this.job.requiresVerification = unknown; + this.job.status = unknown ? 'unknown' : this.finished && !this.failure && native?.status === 'closed' ? 'completed' + : this.run.controller.signal.aborted ? 'cancelled' : 'failed'; + this.job.verified = this.job.status === 'completed'; this.job.finishedAt = at(); + this.job.error = this.failure ? this.provider.redact(this.failure.message) : native?.error ?? null; + this.job.originalOutcome = { status: this.job.status, requiresVerification: this.job.requiresVerification, verified: this.job.verified, error: this.job.error }; + try { + verifyEngineInput(this.store, this.session, this.job); + const artifact = this.store.artifactStore.write(this.session, this.run.record, engineRunReport(this.job), { kind: 'engine-run-report' }); + this.store.core.artifact.set(artifact.id, artifact); this.job.reportArtifactId = artifact.id; this.job.reportSha256 = artifact.sha256; + this.save('finished'); + } catch (error) { + this.job.status = 'unknown'; this.job.requiresVerification = true; this.job.verified = false; + this.job.error = '原引擎结果尚未完整封存:' + this.provider.redact(error.message); this.save('seal_failed'); + } + } +} diff --git a/src/core/tasks/worker-manager.mjs b/src/core/tasks/worker-manager.mjs index 0c37bacb..67b4418c 100644 --- a/src/core/tasks/worker-manager.mjs +++ b/src/core/tasks/worker-manager.mjs @@ -15,6 +15,8 @@ import { parseTestEvidence } from '../check-runner.mjs'; import { verifyPlanEvidence } from '../context/request-plan.mjs'; import { repairToolPairs } from '../../server/store.mjs'; import { readOwnedProcessTree } from '../owned-process-tree.mjs'; +import { assertControlledInstallation } from '../engines/controlled-installation.mjs'; +import { verifyEngineInput, originalEngineRun, assertEngineWorkerResult } from '../../shared/contracts/engine-execution.mjs'; const at = () => new Date().toISOString(); const sourceIdentity = source => Object.fromEntries(['root', 'head', 'reference', 'branch', 'gitDir', 'commonDir', 'indexSha256', @@ -81,6 +83,7 @@ export class WorkerManager { || [...this.store.core.collaboration.values()].some(group => group.projectId === session.projectId && !COLLABORATION_CLOSED.has(group.status))) throw workerConflict('项目已有未收尾的父协作,请先完成或明确保留原成果。'); const initial = structuredClone(this.store.config), config = this.runtime.provider.prepareConfig ? this.runtime.provider.prepareConfig(initial, this.store) : initial; const definition = collaborationInput(input, config), project = this.store.project(session.projectId); + const engine = definition.engine ? this.runtime.engineRunner().selected(definition.engine) : null; const temporaryId = randomUUID(), controller = new AbortController(), active = { sessionId: session.id, projectId: project.id, group: { children: [], status: 'planning' }, controller }; active.promise = new Promise(resolve => { active.settle = resolve; }); this.active.set(temporaryId, active); @@ -95,7 +98,7 @@ export class WorkerManager { const group = { schemaVersion: 1, id: randomUUID(), projectId: project.id, sessionId: session.id, taskId: session.taskId, runId: run.id, goal: definition.goal, budget: definition.budget, model: modelSnapshot(config), config: configView(config), source: { identity: sourceIdentity(source), changedPaths: source.changedPaths, dirty: source.dirty, scope: source.scope }, - children: definition.children.map(child => ({ ...child, id: randomUUID(), workspaceId: randomUUID(), projectId: randomUUID(), sessionId: randomUUID(), taskId: randomUUID() })), + children: definition.children.map(child => ({ ...child, ...(engine ? { engine: structuredClone(engine) } : {}), id: randomUUID(), workspaceId: randomUUID(), projectId: randomUUID(), sessionId: randomUUID(), taskId: randomUUID() })), status: 'planned', requiresVerification: false, createdAt: at(), updatedAt: at(), startedAt: null, deadline: null, finishedAt: null, summary: '父子目标、范围、模型和预算已封存,尚未创建工作区或请求模型。', collection: null, merge: null }; group.fingerprint = workerHash(collaborationSeal(group)); @@ -141,7 +144,7 @@ export class WorkerManager { await this.initializeChild(group, child); this.save(session, group, 'workspace_registered'); await this.faultInjector?.('workspace_registered', group, child); signal.throwIfAborted(); } - await this.sourceFor(group); group.status = 'ready'; group.summary = '两个独立工作区已核对,来源HEAD、索引和已有修改保持。可以开始受限子任务。'; + await this.sourceFor(group); group.status = 'ready'; group.summary = group.children.length + ' 个独立工作区已核对,来源HEAD、索引和已有修改保持。可以开始受限子任务。'; } catch (error) { const uncertain = group.children.some(child => { const workspace = this.store.core.workspace.get(child.workspaceId); return workspace && (workspace.status !== 'ready' || !this.store.core.worker.has(child.id) && this.store.sessions.get(child.sessionId)?.taskId); }); @@ -195,11 +198,16 @@ export class WorkerManager { this.store.putRecord('worker', session, worker, 'worker.ready'); return worker; } configFor(group, worker) { + if (worker.engine) { + assertControlledInstallation(worker.engine); + for (const job of this.store.core.engine_run.values()) if (job.workerId === worker.id) verifyEngineInput(this.store, this.store.session(worker.sessionId), job); + } const initial = structuredClone(this.store.config), current = this.runtime.provider.prepareConfig ? this.runtime.provider.prepareConfig(initial, this.store) : initial; if (JSON.stringify(modelSnapshot(current)) !== JSON.stringify(group.model)) throw workerConflict('模型连接或池快照已变化;不能在原协作中悄悄替换模型。'); const usage = workerUsage(this.store, worker), remaining = group.deadline - Date.now(); if (remaining <= 0) throw new BudgetExceeded('time_limit', '父协作总时限已用完,原任务与成果保留。'); - if (usage.accountedTokens >= worker.budget.maxTokens || usage.steps >= worker.budget.maxSteps || usage.toolCalls >= worker.budget.maxToolCalls) throw new BudgetExceeded('worker_budget', '子任务累计预算已用完,续接不能重置预算。'); + if (usage.accountedTokens >= worker.budget.maxTokens || usage.steps >= worker.budget.maxSteps || usage.toolCalls >= worker.budget.maxToolCalls + || worker.engine && usage.requests >= worker.budget.maxSteps) throw new BudgetExceeded('worker_budget', '子任务累计预算已用完,续接不能重置预算。'); return { ...group.config, maxSteps: worker.budget.maxSteps - usage.steps, maxTaskTokens: worker.budget.maxTokens, maxRunTokens: worker.budget.maxTokens - usage.accountedTokens, maxRunDurationMs: remaining }; } @@ -227,7 +235,7 @@ export class WorkerManager { group.requiresVerification = outcomes.some(outcome => outcome.status === 'rejected') || workers.some(worker => worker.requiresVerification || ['running', 'stopping'].includes(worker.status)); group.status = group.requiresVerification ? 'unknown' : workers.every(worker => worker.status === 'completed') ? 'ready_to_collect' : workers.some(worker => this.store.session(worker.sessionId).status === 'needs_review') ? 'awaiting_review' : 'paused'; - group.summary = group.status === 'ready_to_collect' ? '两个子任务均有实际改动和当前通过的检查,等待父任务核对原成果。' + group.summary = group.status === 'ready_to_collect' ? group.children.length + ' 个子任务均有实际改动和当前通过的检查,等待父任务核对原成果。' : group.status === 'awaiting_review' ? '子任务提案已保存,请分别审查应用后继续;完成状态以实际检查为准。' : '子任务已停止或需要处理,已完成结果保留。'; } catch (error) { group.status = 'paused'; group.summary = this.runtime.provider.redact(error.message ?? String(error)); } session.status = group.status === 'awaiting_review' ? 'needs_review' : 'paused'; this.save(session, group, group.status); return group; @@ -263,7 +271,7 @@ export class WorkerManager { return worker; } uncertain(worker) { - return ['tool', 'check_job', 'change_set', 'delivery_job'].flatMap(kind => [...this.store.core[kind].values()] + return ['tool', 'check_job', 'change_set', 'delivery_job', 'engine_run'].flatMap(kind => [...this.store.core[kind].values()] .filter(value => value.taskId === worker.taskId && (value.requiresVerification || value.status === 'unknown' || value.state === 'unknown')) .map(value => ({ kind, id: value.id }))); } @@ -283,6 +291,7 @@ export class WorkerManager { const baseline = this.baseline(session, worker), project = this.store.project(worker.projectId); await verifyWorkspaceProject(this.store, project); if (this.uncertain(worker).length) throw workerConflict('子任务仍有结果未知的工具、检查或文件批次'); + assertEngineWorkerResult(this.store, session, worker); if (session.changes.some(change => ['pending', 'conflict'].includes(change.status))) throw workerConflict('子任务仍有未审查改动'); const before = new Map(baseline.files.map(file => [workerPathKey(file.path), file])); const { inspection, files: current } = await this.inventory(project), after = new Map(current.map(file => [workerPathKey(file.path), file])); @@ -546,6 +555,24 @@ export class WorkerManager { logSha256: this.store.core.artifact.get(result.logArtifactId)?.sha256 ?? null })) }); } const session = this.store.sessions.get(child.sessionId); + const engines = []; + for (const job of this.store.core.engine_run.values()) if (job.taskId === child.taskId) { + verifyEngineInput(this.store, session, job); + if (job.reportArtifactId) originalEngineRun(this.store, session, job); + const processInfo = job.process ?? job.native?.process; + let tree = null; + if (job.requiresVerification || job.status === 'unknown') { + if (processInfo) { + tree = await readOwnedProcessTree(processInfo); + if (!tree.supported || tree.processes.length) throw workerConflict('原引擎进程树仍存活或无法核对,请先人工核对原创建身份;不会按历史PID停止。'); + } + } + engines.push({ id: job.id, status: job.status, requiresVerification: !!job.requiresVerification, + inputArtifactId: job.inputArtifactId, inputSha256: job.inputSha256, reportArtifactId: job.reportArtifactId ?? null, + reportSha256: job.reportSha256 ?? null, process: processInfo ?? null, remainingProcesses: tree?.processes ?? null, + missingProcessIdentity: !processInfo && job.status === 'unknown', requests: job.requests, calls: job.calls, + originalOutcome: job.originalOutcome ?? null, error: job.error ?? null }); + } const tools = [...this.store.core.tool.values()].filter(value => value.taskId === child.taskId && value.requiresVerification).map(tool => { const call = session?.providerMessages.flatMap(message => message.tool_calls ?? []).find(call => call.id === tool.callId); const input = call?.function.arguments; @@ -562,7 +589,7 @@ export class WorkerManager { children.push({ id: child.id, workspaceId: child.workspaceId, workspaceStatus: workspace?.status ?? 'absent', workerStatus: worker?.status ?? 'absent', taskId: child.taskId, sessionId: child.sessionId, projectId: child.projectId, inventoryFingerprint: inventory ? workerHash(inventory.files) : null, sourceFingerprint: inventory?.inspection.sourceFingerprint ?? null, inventoryError, - tools, checks, changes, changeSets: [...this.store.core.change_set.values()].filter(set => set.taskId === child.taskId).map(set => ({ id: set.id, status: set.status, requiresVerification: !!set.requiresVerification, observationFingerprint: set.observationFingerprint ?? null })) }); + tools, checks, changes, ...(engines.length ? { engines } : {}), changeSets: [...this.store.core.change_set.values()].filter(set => set.taskId === child.taskId).map(set => ({ id: set.id, status: set.status, requiresVerification: !!set.requiresVerification, observationFingerprint: set.observationFingerprint ?? null })) }); } const parent = this.store.session(group.sessionId); return { schemaVersion: 1, kind: 'worker-recovery-observation', collaborationId: group.id, groupFingerprint: group.fingerprint, @@ -612,10 +639,15 @@ export class WorkerManager { if (!retain && !merged && JSON.stringify(current.source) !== JSON.stringify(group.source.identity)) throw workerConflict('父来源已经变化,只能明确保留现场后重新规划。'); if (current.children.some(child => !['ready', 'absent', 'prepared', 'discarded', 'resolved_external'].includes(child.workspaceStatus))) throw workerConflict('原工作区仍需专门核对,不能开始子执行或结束恢复。'); if (!retain && current.children.some(child => ['discarded', 'resolved_external'].includes(child.workspaceStatus))) throw workerConflict('原工作区没有核对为可执行,只能明确保留现场结束。'); + if (!retain && current.children.some(child => child.engines?.some(job => job.missingProcessIdentity))) throw workerConflict('引擎原进程创建身份没有封存,只能人工处理后明确保留现场结束。'); if (!retain) await this.recoverMerge(session, group); for (const child of group.children) { const worker = this.store.core.worker.get(child.id); if (!worker) continue; const childSession = this.store.session(child.sessionId); + for (const job of this.store.core.engine_run.values()) if (job.taskId === child.taskId && job.requiresVerification) { + job.review = { source: 'user', evidenceSha256, reviewedAt: at(), result: 'unknown-preserved' }; + job.status = 'resolved_external'; job.requiresVerification = false; + } for (const tool of this.store.core.tool.values()) if (tool.taskId === child.taskId && tool.requiresVerification) { tool.originalState = tool.state; tool.state = 'resolved_external'; tool.requiresVerification = false; tool.review = { source: 'user', evidenceSha256, reviewedAt: at(), result: 'unknown-preserved' }; diff --git a/src/server/app.mjs b/src/server/app.mjs index 1e89ae68..f4fca2f2 100644 --- a/src/server/app.mjs +++ b/src/server/app.mjs @@ -7,6 +7,7 @@ import { assertProposalUnsealed } from '../shared/contracts/change-sets.mjs'; import { assertWorkspacesResolved } from '../shared/contracts/workspaces.mjs'; import { verifyWorkspaceProject } from '../core/workspaces/git-workspaces.mjs'; import { publicEngineJob } from '../shared/contracts/engines.mjs'; +import { publicEngineRun, verifyEngineInput, originalEngineRun } from '../shared/contracts/engine-execution.mjs'; import { verifyBrowserJob } from '../core/browser/browser-manager.mjs'; import { ConfigImporter } from '../core/models/config-import.mjs'; import { ProjectMemoryManager } from '../core/memory/project-memory.mjs'; @@ -185,6 +186,7 @@ return async (request, response) => { } if (method === 'GET' && route === '/api/research/availability') return json(response, runtime.researchManager().availability()); if (method === 'GET' && route === '/api/engines/deepseek/availability') return json(response, runtime.engineManager().availability(url.searchParams.has('root') ? url.searchParams.get('root') : undefined)); + if (method === 'GET' && route === '/api/engines/deepseek/controlled-availability') return json(response, runtime.engineRunner().availability(url.searchParams.has('root') ? url.searchParams.get('root') : undefined)); if (method === 'GET' && route === '/api/events') { const cursor = Math.max(cursorNumber(url.searchParams.get('after')), cursorNumber(request.headers['last-event-id'])); if (cursor > store.eventCursor) throw Object.assign(new Error('事件游标超出当前历史,请重新加载工作台。'), { status: 409 }); @@ -277,6 +279,21 @@ return async (request, response) => { } if (method === 'GET' && !action) return json(response, store.publicSession(session)); if (method === 'GET' && action === 'execution') return json(response, store.executionSnapshot(session)); + if (method === 'GET' && action === 'engine-runs') return json(response, [...store.core.engine_run.values()].filter(job => job.sessionId === id).slice(-30).map(publicEngineRun)); + const engineRun = action.match(/^engine-runs\/([a-f0-9-]+)\/(input|report|download)$/); + if (method === 'GET' && engineRun) { + const job = store.owned('engine_run', engineRun[1], id); + const text = engineRun[2] === 'input' ? store.artifactStore.read(verifyEngineInput(store, session, job)) : originalEngineRun(store, session, job); + if (engineRun[2] !== 'download') { + const artifact = store.owned('artifact', engineRun[2] === 'input' ? job.inputArtifactId : job.reportArtifactId, id); + const offset = cursorNumber(url.searchParams.get('offset')), limit = Math.min(16000, url.searchParams.has('limit') ? cursorNumber(url.searchParams.get('limit')) : 4000); + if (!limit || offset > text.length) throw Object.assign(new Error('原引擎记录的读取位置或页长超出范围。'), { status: 400 }); + return json(response, { ...artifact, content: text.slice(offset, offset + limit), offset, totalCharacters: text.length, truncated: offset + limit < text.length }); + } + response.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store', + 'X-Content-Type-Options': 'nosniff', ...(engineRun[2] === 'download' ? { 'Content-Disposition': 'attachment; filename="agent-workbench-engine-run-' + job.id + '.json"' } : {}) }); + response.end(text); return; + } if (method === 'GET' && action === 'mcp/jobs') return json(response, runtime.mcpRegistry().jobs(session)); if (method === 'POST' && action === 'mcp/probe') { const started = runtime.mcpRegistry().start(session, await body(request)); diff --git a/src/server/provider.mjs b/src/server/provider.mjs index e887c01c..176c0ec3 100644 --- a/src/server/provider.mjs +++ b/src/server/provider.mjs @@ -177,13 +177,13 @@ export class Provider { const response = await this.request(config, '/models', undefined, undefined, key); return (response.data ?? response.models ?? []).map(item => String(item.id ?? item.name)).filter(Boolean).slice(0, 100); } - async complete(config, messages, project, signal, onProgress, requestPlan = null, tools = toolDefinitions) { + async complete(config, messages, project, signal, onProgress, requestPlan = null, tools = toolDefinitions, requestPolicy = {}) { const body = completionBody(config, messages, tools); assertPlannedRequest(body, requestPlan); if (config.mode === 'demo') return this.demo(messages, project); this.validate(config); const execute = async (selected, key, input, progress) => { - const value = await this.request(selected, '/chat/completions', input, signal, key, progress, config.modelRouting ? { attempts: 1 } : {}); + const value = await this.request(selected, '/chat/completions', input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy }); if (!value.choices?.[0]?.message || value.choices[0].finish_reason === 'length') throw new ModelRequestError({ kind: 'protocol', source: 'model_connection', stage: 'model_request', title: '模型响应不完整', message: '模型响应缺少完整message或达到输出上限,未执行工具。', retryable: false, occurredAt: new Date().toISOString() }); return value; diff --git a/src/server/runtime.mjs b/src/server/runtime.mjs index 855714ba..d1be74f1 100644 --- a/src/server/runtime.mjs +++ b/src/server/runtime.mjs @@ -22,6 +22,7 @@ import { WorkspaceManager, verifyWorkspaceProject } from '../core/workspaces/git import { assertWorkspacesResolved, projectWorkspaceIdentity } from '../shared/contracts/workspaces.mjs'; import { ResearchManager } from '../core/research-manager.mjs'; import { EngineManager } from '../core/engine-manager.mjs'; +import { EngineRunner } from '../core/engines/engine-runner.mjs'; import { BrowserManager, publicBrowserPlan } from '../core/browser/browser-manager.mjs'; import { browserPlanInput } from '../shared/contracts/browser.mjs'; import { TaskPlanManager } from '../core/tasks/task-plan-manager.mjs'; @@ -118,6 +119,7 @@ export class Runtime { redact: value => this.provider.redact(value), projectBusy: projectId => this.projectHasActivity(projectId) }); } requestPlanner() { return this.contextPlans ??= new RequestPlanner(this.store); } + engineRunner() { return this.controlledEngines ??= new EngineRunner(this.store, this.provider, { appDirectory: this.appDirectory }); } memoryManager() { return this.projectMemories ??= new ProjectMemoryManager(this.store); } checkpointManager() { return this.taskCheckpoints ??= new TaskCheckpointManager(this.store, { taskPlans: this.taskPlanner(), redact: value => this.provider.redact(value) }); } @@ -267,29 +269,38 @@ export class Runtime { ' tokens;省略 ' + planned.record.archivedGroups + ' 个完整消息组,原历史和参数已保存为SHA256工件。估算与服务实际用量分别记录。' }); } let requestEvent; - let response, requestStarted = false, responseAccounted = false; + const externalEngine = !!run.worker?.engine; + if (externalEngine) { planned.record.engineStepOnly = true; planned.record.engine = 'deepseek-workbench'; } + let response, requestStarted = false, responseAccounted = externalEngine; try { signal.throwIfAborted(); checkBudget(run.record, task); - if (run.worker) { planned.record.workerDispatchedAt = now(); this.store.save(session, { type: 'worker.request_intent' }); } + if (run.worker && !externalEngine) { planned.record.workerDispatchedAt = now(); this.store.save(session, { type: 'worker.request_intent' }); } requestStarted = true; - response = await this.provider.complete(config, planned.messages, run.project, signal, progress => { - if (!requestEvent) requestEvent = this.store.event(session, { kind: 'model', runId: run.id, source: 'model_connection', label: '请求模型服务', phase: 'running', startedAt: now(), contextPlanId: planned.record.id }); + const progressHandler = progress => { + if (!requestEvent) requestEvent = this.store.event(session, { kind: externalEngine ? 'engine' : 'model', runId: run.id, + source: externalEngine ? 'controlled_engine' : 'model_connection', label: externalEngine ? '受控引擎执行' : '请求模型服务', phase: 'running', startedAt: now(), contextPlanId: planned.record.id }); + if (progress.engineRunId) requestEvent.engineRunId = progress.engineRunId; planned.record.status = progress.phase === 'running' || progress.phase === 'retrying' ? 'requesting' : planned.record.status; planned.record.requestAttempts = Math.max(planned.record.requestAttempts, progress.attempt ?? 0); stepRecord.modelAttempts = Math.max(stepRecord.modelAttempts, progress.attempt ?? 0); requestEvent.phase = progress.phase; requestEvent.attempts = progress.attempt; if (progress.connectionId) { requestEvent.connectionId = progress.connectionId; requestEvent.modelAttemptId = progress.modelAttemptId; requestEvent.actualModel = progress.model; } requestEvent.label = progress.switching ? '正在切换模型池连接' : progress.phase === 'retrying' ? '正在重试模型连接' : progress.phase === 'completed' ? '模型响应已收到' : progress.phase === 'failed' ? '模型连接失败' : '请求模型服务'; + if (externalEngine) requestEvent.label = progress.phase === 'completed' ? '引擎原结果已收到' : '受控引擎执行'; requestEvent.output = progress.phase === 'retrying' ? '第 ' + progress.attempt + ' 次请求失败,将自动重试(最多 ' + progress.maxAttempts + ' 次)。' : progress.phase === 'completed' ? '模型响应已收到;工具请求将在校验后执行。' : progress.diagnostic?.message ?? '正在等待模型响应。'; + if (externalEngine) requestEvent.output = progress.phase === 'completed' ? '原引擎工具请求或最终输出已返回,后续按所属任务核验。' : '等待所属引擎;实际模型尝试另有记录。'; if (progress.diagnostic) requestEvent.diagnostic = progress.diagnostic; if (progress.durationMs !== undefined) requestEvent.durationMs = progress.durationMs; if (['completed', 'failed'].includes(progress.phase)) requestEvent.finishedAt = now(); this.store.save(session); - }, planned.record, tools); + }; + response = externalEngine + ? await this.engineRunner().complete(session, run, planned.messages, progressHandler, planned.record, tools) + : await this.provider.complete(config, planned.messages, run.project, signal, progressHandler, planned.record, tools); this.requestPlanner().finish(session, planned.record, { response, attempts: planned.record.requestAttempts }); - if (run.worker) { + if (run.worker && !externalEngine) { this.store.recordResponse(session, run.record, response.usage, planned.record.estimatedInputTokens + Math.ceil(Buffer.byteLength(JSON.stringify(response.message), 'utf8') / 3) + 8); planned.record.workerUsageAccounted = true; this.store.save(session); responseAccounted = true; } @@ -323,6 +334,7 @@ export class Runtime { if (!calls.length) { this.store.finishStep(session, stepRecord); session.status = session.changes.some(change => ['pending', 'conflict'].includes(change.status)) ? 'needs_review' : 'completed'; + if (externalEngine) await this.engineRunner().finish(session, run); if (session.status === 'completed' && run.worker) { try { await this.workerManager().output(session, run.worker); signal.throwIfAborted(); } catch (error) { @@ -407,6 +419,8 @@ export class Runtime { output: diagnostic.message, diagnostic: session.lastFailure }); } this.store.save(session); + } finally { + if (run.worker?.engine) await this.engineRunner().finish(session, run); } } async executeTool(session, call, run) { @@ -419,6 +433,7 @@ export class Runtime { const args = JSON.parse(call.function.arguments); validateToolArgs(tool, args); assertWorkerTool(this.store, session, tool, args); + if (run.worker?.engine) this.engineRunner().assertCurrent(session, run); event.input = JSON.parse(this.provider.redact(JSON.stringify(args))); const proofHint = !!this.store.core.task.get(run.taskId)?.currentTaskPlanId && PLAN_READ_TOOLS.includes(tool); const readMetadata = { ...(proofHint ? { tool_invocation_id: event.invocationId } : {}), @@ -580,6 +595,7 @@ export class Runtime { const cwd = safePath(run.project); event.phase = 'running'; event.executedAt = now(); session.status = 'running'; this.store.invocationState(session, event, 'running'); + if (run.worker?.engine) this.engineRunner().assertCurrent(session, run); if (execute) return execute(); return this.executeCommand(command, cwd, run.controller.signal, output => { event.output = output; this.store.save(session); diff --git a/src/server/store.mjs b/src/server/store.mjs index 82caaa21..b5329fe8 100644 --- a/src/server/store.mjs +++ b/src/server/store.mjs @@ -10,6 +10,7 @@ import { ACTIVE_STATES, CORE_KINDS, createTask, createRun, ownedRecord, emptyUsa import { WORKSPACE_ACTIVE, projectWorkspaceIdentity, assertWorkspacePath } from '../shared/contracts/workspaces.mjs'; import { RESEARCH_ACTIVE } from '../shared/contracts/research.mjs'; import { ENGINE_ACTIVE, publicEngineJob } from '../shared/contracts/engines.mjs'; +import { ENGINE_RUN_ACTIVE, publicEngineRun, verifyEngineModelInput } from '../shared/contracts/engine-execution.mjs'; import { BROWSER_ACTIVE } from '../shared/contracts/browser.mjs'; import { publicBrowserPlan } from '../core/browser/browser-manager.mjs'; import { MCP_ACTIVE } from '../core/tools/mcp-contract.mjs'; @@ -192,7 +193,7 @@ export class Store extends EventEmitter { step.status = 'interrupted'; step.finishedAt = now(); changed = true; } for (const plan of this.core.context_plan.values()) if (plan.sessionId === session.id) { - if (plan.workerDispatchedAt && !plan.workerUsageAccounted) { + if (plan.workerDispatchedAt && !plan.workerUsageAccounted && !plan.engineStepOnly) { const run = this.core.run.get(plan.runId); if (run && this.core.task.get(run.taskId)?.workerId) { this.recordResponse(session, run, null, plan.estimatedInputTokens + (run.model.maxOutputTokens ?? 2048)); @@ -252,6 +253,28 @@ export class Store extends EventEmitter { worker.summary = '子执行在宿主中断时没有完整收尾;原进程结束及实际修改待核对,不按历史PID停止进程或重放工具。'; session.status = 'paused'; session.recoveryNotice = worker.summary; changed = true; } + for (const job of this.core.engine_run.values()) if (job.sessionId === session.id && ENGINE_RUN_ACTIVE.has(job.status)) { + for (const request of job.requests) if (request.dispatchedAt && !request.finishedAt) { + const run = this.core.run.get(job.runId); + try { + verifyEngineModelInput(this, session, job, request); + const count = request.actualAttemptsFinal ?? request.maxAttempts; + if (run) while (request.accountedAttempts < count) { + request.accountedAttempts++; this.recordResponse(session, run, null, request.reservedPerAttempt); + request.recoveredUnknownAttempts = (request.recoveredUnknownAttempts ?? 0) + 1; + } + } catch (error) { + request.verificationError = error.message; + if (run) this.recordResponse(session, run, null, Math.max(0, this.core.task.get(job.taskId).budget.maxTokens - accountedTokens(this.core.task.get(job.taskId).usage))); + request.recoveryConservativeCharge = true; + } + request.status = 'interrupted'; request.finishedAt = now(); + request.error = '宿主中断,原模型结果未完整封存;按原尝试预算核对,不自动请求。'; + } + job.status = 'unknown'; job.requiresVerification = true; job.verified = false; job.finishedAt = now(); + job.error = '受控引擎宿主中断,原工具/模型结果及进程树待核对。原输入保留,不按历史PID终止或重放。'; + session.status = 'paused'; session.recoveryNotice = job.error; changed = true; + } for (const job of this.core.engine_job.values()) if (job.sessionId === session.id && ENGINE_ACTIVE.has(job.status)) { job.status = 'unknown'; job.requiresVerification = true; job.verification = null; job.finishedAt = now(); job.summary = '外部引擎连接检查中断,原进程关闭状态未知。不会根据历史PID停止进程或重放请求,请查看所属原记录。'; @@ -307,7 +330,7 @@ export class Store extends EventEmitter { for (const group of this.core.collaboration.values()) { const unresolved = group.children.some(child => this.core.worker.get(child.id)?.requiresVerification || this.core.workspace.get(child.workspaceId)?.requiresVerification - || ['tool', 'check_job', 'change_set'].some(kind => [...this.core[kind].values()].some(record => record.taskId === child.taskId && record.requiresVerification))) + || ['tool', 'check_job', 'change_set', 'engine_run'].some(kind => [...this.core[kind].values()].some(record => record.taskId === child.taskId && record.requiresVerification))) || [...this.core.change_set.values()].some(set => set.taskId === group.taskId && set.requiresVerification); if (!COLLABORATION_CLOSED.has(group.status) && unresolved && (!group.requiresVerification || group.status !== 'unknown')) { group.status = 'unknown'; group.requiresVerification = true; group.finishedAt = now(); @@ -584,6 +607,7 @@ export class Store extends EventEmitter { researchTargets: [...this.core.research_target.values()].filter(value => value.sessionId === session.id && value.taskId === task?.id).slice(-32), researchJobs: [...this.core.research_job.values()].filter(value => value.sessionId === session.id && (value.taskId === task?.id || value.requiresVerification)).slice(-20), engineJobs: [...this.core.engine_job.values()].filter(value => value.sessionId === session.id && (value.taskId === task?.id || value.requiresVerification)).slice(-20).map(publicEngineJob), + engineRuns: [...this.core.engine_run.values()].filter(value => value.sessionId === session.id && (value.taskId === task?.id || value.requiresVerification)).slice(-20).map(publicEngineRun), browserPlans: [...this.core.browser_plan.values()].filter(value => value.sessionId === session.id && value.taskId === task?.id).slice(-5).map(publicBrowserPlan), browserJobs: [...this.core.browser_job.values()].filter(value => value.sessionId === session.id && (value.taskId === task?.id || value.requiresVerification)).slice(-12), contextPlans: [...this.core.context_plan.values()].filter(value => value.sessionId === session.id && value.taskId === task?.id).slice(-30), diff --git a/src/shared/contracts/engine-execution.mjs b/src/shared/contracts/engine-execution.mjs new file mode 100644 index 00000000..5ded3f0c --- /dev/null +++ b/src/shared/contracts/engine-execution.mjs @@ -0,0 +1,113 @@ +import { engineError, engineCanonicalDigest as controlledDigest } from './engines.mjs'; +import { requestMetrics } from './model-request.mjs'; + +export const ENGINE_RUN_ACTIVE = new Set(['queued', 'starting', 'initializing', 'running', 'waiting_tool', 'closing', 'stopping']); +export function selectedEngineInput(input) { + if (!input || typeof input !== 'object' || Array.isArray(input) + || Object.keys(input).some(key => !['id', 'root', 'controlledSha256'].includes(key)) + || input.id !== 'deepseek-harness' || typeof input.root !== 'string' + || !/^[a-f0-9]{64}$/.test(input.controlledSha256 ?? '')) throw engineError('请选择已经检测并封存身份的受控DeepSeek引擎。', 400); + return structuredClone(input); +} + +export function engineRunInput(job) { + return { schemaVersion: 1, id: job.id, sessionId: job.sessionId, projectId: job.projectId, taskId: job.taskId, runId: job.runId, + workerId: job.workerId, collaborationId: job.collaborationId, installation: job.installation, sdkSessionId: job.sdkSessionId, + directory: job.directory, model: job.model, budget: job.budget, tools: job.tools, + sourceContextPlanId: job.sourceContextPlanId, sourceRequestSha256: job.sourceRequestSha256 }; +} +export function engineRunReport(job) { + return { schemaVersion: 1, owner: { sessionId: job.sessionId, projectId: job.projectId, taskId: job.taskId, runId: job.runId, jobId: job.id }, + inputSha256: job.inputSha256, installationSha256: job.installation.controlledSha256, sdkSessionId: job.sdkSessionId, + outcome: job.originalOutcome, native: job.native, requests: job.requests, calls: job.calls }; +} +export function publicEngineRun(job) { + const { native, requests, calls, ...view } = job; + return { ...view, nativeSummary: { status: native?.status ?? null, pid: native?.process?.pid ?? job.pid ?? null, + rootExited: native?.cleanup?.rootExited ?? null, treeConfirmed: native?.cleanup?.treeConfirmed ?? null, + receipt: native?.receipt ?? null, notifications: native?.notifications?.length ?? job.notificationCount ?? 0 }, + modelRequests: requests?.length ?? 0, toolRequests: calls?.length ?? 0 }; +} +export function verifyEngineInput(store, session, job) { + store.owned('engine_run', job.id, session.id); + const input = store.owned('artifact', job.inputArtifactId, session.id); + if (input.kind !== 'engine-run-input' || input.taskId !== job.taskId || input.runId !== job.runId || input.sha256 !== job.inputSha256 + || store.artifactStore.read(input) !== JSON.stringify(engineRunInput(job))) throw engineError('受控引擎与原任务/范围/程序输入不一致。'); + return input; +} +export function originalEngineRun(store, session, job) { + verifyEngineInput(store, session, job); + const artifact = store.owned('artifact', job.reportArtifactId, session.id); + if (artifact.kind !== 'engine-run-report' || artifact.taskId !== job.taskId || artifact.runId !== job.runId || artifact.sha256 !== job.reportSha256) { + throw engineError('受控引擎原报告身份无效。'); + } + const bytes = store.artifactStore.read(artifact); + if (bytes !== JSON.stringify(engineRunReport(job))) throw engineError('受控引擎原报告与当前账本不一致。'); + return bytes; +} +export function verifyEngineModelInput(store, session, job, request) { + const artifact = store.owned('artifact', request.inputArtifactId, session.id); + if (artifact.kind !== 'engine-model-input' || artifact.taskId !== job.taskId || artifact.runId !== job.runId || artifact.sha256 !== request.inputSha256) throw engineError('原引擎模型输入工件归属无效。'); + const input = JSON.parse(store.artifactStore.read(artifact)), metrics = requestMetrics(input.body); + for (const key of ['id', 'bridgeTraceId', 'sourceSha256', 'requestSha256', 'requestBytes', 'estimatedInputTokens', 'reservedPerAttempt', 'maxAttempts']) { + if (input.plan[key] !== request[key]) throw engineError('原引擎模型请求或尝试预算与封存输入不一致。'); + } + if (input.owner.sessionId !== job.sessionId || input.owner.taskId !== job.taskId || input.owner.runId !== job.runId || input.owner.projectId !== job.projectId + || controlledDigest(input.native) !== request.sourceSha256 || metrics.sha256 !== request.requestSha256 || metrics.bytes !== request.requestBytes + || !Number.isSafeInteger(request.maxAttempts) || request.maxAttempts < 1 || request.maxAttempts > 8 + || request.reservedPerAttempt !== metrics.estimatedInputTokens + 128 + input.body.max_tokens) throw engineError('原引擎模型输入、归属或预算核对失败。'); + return input; +} +export function assertEngineWorkerResult(store, session, worker) { + if (!worker.engine) return; + const job = [...store.core.engine_run.values()].findLast(value => value.sessionId === session.id && value.runId === worker.currentRunId); + if (!job || job.status !== 'completed' || job.requiresVerification || !job.verified + || job.installation.controlledSha256 !== worker.engine.controlledSha256) throw engineError('原引擎运行缺少完整协议、停止和账本依据。'); + originalEngineRun(store, session, job); + if (job.calls.some(call => call.status !== 'returned' || !call.invocationId) + || job.requests.some(request => request.status !== 'completed')) throw engineError('引擎原调用或模型请求尚未完整核对。'); + for (const call of job.calls) { + const tool = store.owned('tool', call.invocationId, session.id), result = store.savedToolResult(session.id, call.callId); + if (tool.runId !== job.runId || tool.callId !== call.callId || tool.requiresVerification || controlledDigest(result) !== call.resultSha256) { + throw engineError('引擎原工具结果与自有执行账本不一致。'); + } + const artifact = store.owned('artifact', call.outputArtifactId, session.id); + if (artifact.kind !== 'engine-tool-output' || artifact.taskId !== job.taskId || artifact.runId !== job.runId || artifact.sha256 !== call.outputSha256 + || store.artifactStore.read(artifact) !== JSON.stringify(result)) throw engineError('反馈给原引擎的工具结果工件不一致。'); + } + for (const request of job.requests) { + verifyEngineModelInput(store, session, job, request); + const artifact = store.owned('artifact', request.responseArtifactId, session.id); + if (artifact.kind !== 'engine-model-output' || artifact.taskId !== job.taskId || artifact.runId !== job.runId || artifact.sha256 !== request.responseSha256 + || !JSON.parse(store.artifactStore.read(artifact)).message || request.accountedAttempts !== request.actualAttemptsFinal) throw engineError('原引擎模型输出或尝试账本未完整核对。'); + } +} + +export function nativeMessagesToChat(input) { + if (!input || !Array.isArray(input.messages)) throw engineError('原引擎模型输入没有消息数组。'); + const messages = input.messages.map(message => { + if (!message || !['system', 'developer', 'user', 'assistant', 'tool'].includes(message.role) || !Array.isArray(message.content)) { + throw engineError('原引擎消息不符合已验证的文本协议。'); + } + const text = [], calls = []; + for (const block of message.content) { + if (block?.type === 'text' && typeof block.text === 'string') text.push(block.text); + else if (block?.type === 'reasoning' && typeof block.text === 'string') { + // Reasoning is not promoted into user-visible text or a tool request. + } else if (block?.type === 'tool-call' && message.role === 'assistant' && typeof block.id === 'string' + && typeof block.name === 'string' && typeof block.arguments === 'string') { + JSON.parse(block.arguments); calls.push({ id: block.id, type: 'function', function: { name: block.name, arguments: block.arguments } }); + } else throw engineError('当前受控桥只接入文本和完整工具调用,原内容类型尚未验证。'); + } + if (message.role === 'tool') { + if (typeof message.toolCallId !== 'string' || !message.toolCallId) throw engineError('原工具消息缺少toolCallId。'); + return { role: 'tool', tool_call_id: message.toolCallId, content: text.join('') }; + } + return { role: message.role, content: text.join(''), ...(calls.length ? { tool_calls: calls } : {}) }; + }); + if (input.system !== null && input.system !== undefined) { + if (typeof input.system !== 'string') throw engineError('原单次系统提示格式无效。'); + messages.unshift({ role: 'system', content: input.system }); + } + return messages; +} diff --git a/src/shared/contracts/engines.mjs b/src/shared/contracts/engines.mjs index a7850961..dc66b795 100644 --- a/src/shared/contracts/engines.mjs +++ b/src/shared/contracts/engines.mjs @@ -10,6 +10,9 @@ export const ENGINE_LIMITS = Object.freeze([ ]); export const engineError = (message, status = 409, code = 'ENGINE_CONTRACT') => Object.assign(new Error(message), { status, code }); export const engineDigest = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value), 'utf8').digest('hex'); +const canonical = value => Array.isArray(value) ? value.map(canonical) : value && typeof value === 'object' + ? Object.fromEntries(Object.keys(value).sort().map(key => [key, canonical(value[key])])) : value; +export const engineCanonicalDigest = value => engineDigest(canonical(value)); export function engineInput(job) { return { schemaVersion: 1, jobId: job.id, sessionId: job.sessionId, projectId: job.projectId, diff --git a/src/shared/contracts/execution.mjs b/src/shared/contracts/execution.mjs index ec873391..5abdab18 100644 --- a/src/shared/contracts/execution.mjs +++ b/src/shared/contracts/execution.mjs @@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto'; export const EXECUTION_SCHEMA = 1; export const ACTIVE_STATES = new Set(['running', 'awaiting_approval', 'stopping']); -export const CORE_KINDS = ['task', 'run', 'step', 'tool', 'artifact', 'check_plan', 'check_job', 'delivery_plan', 'delivery_job', 'change_set', 'context_plan', 'workspace', 'research_target', 'research_job', 'engine_job', 'browser_plan', 'browser_job', 'task_plan', 'task_proof', 'memory', 'task_checkpoint', 'mcp_job', 'collaboration', 'worker']; +export const CORE_KINDS = ['task', 'run', 'step', 'tool', 'artifact', 'check_plan', 'check_job', 'delivery_plan', 'delivery_job', 'change_set', 'context_plan', 'workspace', 'research_target', 'research_job', 'engine_job', 'engine_run', 'browser_plan', 'browser_job', 'task_plan', 'task_proof', 'memory', 'task_checkpoint', 'mcp_job', 'collaboration', 'worker']; export const timestamp = () => new Date().toISOString(); export const isId = value => typeof value === 'string' && /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(value); diff --git a/src/shared/contracts/workers.mjs b/src/shared/contracts/workers.mjs index 46b61a6d..34fdc607 100644 --- a/src/shared/contracts/workers.mjs +++ b/src/shared/contracts/workers.mjs @@ -1,5 +1,6 @@ import { createHash } from 'node:crypto'; import { isId, accountedTokens, emptyUsage } from './execution.mjs'; +import { selectedEngineInput } from './engine-execution.mjs'; export const WORKER_TOOLS = new Set(['inspect_session', 'inspect_project', 'index_project', 'query_code', 'read_artifact', 'list_files', 'read_file', 'search_files', 'propose_file', 'propose_patch', 'prepare_change_set', 'plan_checks', 'run_checks']); @@ -29,8 +30,9 @@ const text = (value, maximum, label) => { }; export function collaborationInput(input, config) { - if (!input || Array.isArray(input) || Object.keys(input).some(key => !['goal', 'budget', 'children'].includes(key))) throw workerConflict('协作只接受父目标、预算和两个子任务。'); - if (!Array.isArray(input.children) || input.children.length !== 2) throw workerConflict('本阶段需要两个受限子任务。'); + if (!input || Array.isArray(input) || Object.keys(input).some(key => !['goal', 'budget', 'children', 'engine'].includes(key))) throw workerConflict('协作只接受父目标、预算、子任务和已检测的引擎。'); + const engine = input.engine === undefined ? null : selectedEngineInput(input.engine); + if (!Array.isArray(input.children) || input.children.length !== (engine ? 1 : 2)) throw workerConflict(engine ? '受控引擎计划需要一个所属子工程。' : '本阶段需要两个受限子任务。'); const raw = input.budget ?? {}; if (!raw || typeof raw !== 'object' || Array.isArray(raw) || Object.keys(raw).some(key => !['maxTokens', 'maxSteps', 'maxToolCalls', 'maxDurationMs'].includes(key))) throw workerConflict('协作预算字段无效。'); const tokenMaximum = Math.min(config.maxTaskTokens ?? 1000000, config.maxRunTokens ?? 200000); @@ -47,12 +49,12 @@ export function collaborationInput(input, config) { if (new Set(paths.map(workerPathKey)).size !== paths.length) throw workerConflict('子任务范围不能重复。'); const part = item.budget ?? {}; if (!part || Array.isArray(part) || Object.keys(part).some(key => !['maxTokens', 'maxSteps', 'maxToolCalls'].includes(key))) throw workerConflict('子预算字段无效。'); - const allocate = key => index === 0 ? Math.floor(budget[key] / 2) : budget[key] - Math.floor(budget[key] / 2); + const allocate = key => engine ? budget[key] : index === 0 ? Math.floor(budget[key] / 2) : budget[key] - Math.floor(budget[key] / 2); return { label: text(item.label, 64, '子任务名称'), goal: text(item.goal, 8000, '子目标'), paths, budget: Object.fromEntries(['maxTokens', 'maxSteps', 'maxToolCalls'].map(key => [key, bounded(part[key], allocate(key), key === 'maxTokens' ? 500 : 1, budget[key], key)])) }; }); for (const key of ['maxTokens', 'maxSteps', 'maxToolCalls']) if (children.reduce((sum, child) => sum + child.budget[key], 0) > budget[key]) throw workerConflict('子任务预算之和不能超过父预算:' + key); - return { goal: text(input.goal, 20000, '父目标'), budget, children }; + return { goal: text(input.goal, 20000, '父目标'), budget, children, ...(engine ? { engine } : {}) }; } export function collaborationSeal(group) { @@ -64,7 +66,8 @@ export function workerSeal(worker) { parentSessionId: worker.parentSessionId, parentTaskId: worker.parentTaskId, parentRunId: worker.parentRunId, projectId: worker.projectId, sessionId: worker.sessionId, taskId: worker.taskId, workspaceId: worker.workspaceId, label: worker.label, goal: worker.goal, paths: worker.paths, budget: worker.budget, model: worker.model, baseCommit: worker.baseCommit, - baselineArtifactId: worker.baselineArtifactId, baselineSha256: worker.baselineSha256, baselineRunId: worker.baselineRunId }; + baselineArtifactId: worker.baselineArtifactId, baselineSha256: worker.baselineSha256, baselineRunId: worker.baselineRunId, + ...(worker.engine ? { engine: worker.engine } : {}) }; } export function workerForProject(store, projectId) { return [...(store.core.worker?.values() ?? [])].find(value => value.projectId === projectId) ?? null; @@ -79,7 +82,8 @@ export function workerForSession(store, session) { || definition.taskId !== worker.taskId || definition.projectId !== worker.projectId || definition.sessionId !== worker.sessionId || definition.workspaceId !== worker.workspaceId || JSON.stringify(definition.paths) !== JSON.stringify(worker.paths) || JSON.stringify(definition.budget) !== JSON.stringify(worker.budget) || definition.goal !== worker.goal - || JSON.stringify(group.model) !== JSON.stringify(worker.model)) throw workerConflict('子任务与父任务的封存分工不一致。'); + || JSON.stringify(group.model) !== JSON.stringify(worker.model) + || JSON.stringify(definition.engine) !== JSON.stringify(worker.engine)) throw workerConflict('子任务与父任务的封存分工不一致。'); const task = store.core.task.get(worker.taskId); if (!task || task.goal !== worker.goal || task.workerId !== worker.id || task.budget.maxTokens !== worker.budget.maxTokens) throw workerConflict('子任务目标、身份或累计预算已变化。'); const original = store.owned('artifact', worker.contractArtifactId, session.id); diff --git a/task_plan.md b/task_plan.md index e64f17a7..d275f3dd 100644 --- a/task_plan.md +++ b/task_plan.md @@ -5,9 +5,9 @@ 用户授权进入下一阶段全量开发并及时回顾。接续60b63bc/正式1.13.0,开发使用新分支feat/controlled-engines-1.14;当前正式PID23172、Provider、连接与全部历史保持。沿用PLAN-1.14的六切片和本地Git,不读取真实Key、不发送真实/付费模型请求、不主动委派助手子代理。 1. [complete] A:新受控安装身份固定原SDK/CLI/Node及七份关键运行器,独立profile叠加禁用原生项目/终端/模型/重试插件。7/7边界专项和实际Harness+本地固定模型2请求/1工具、prompt收据/原活动/wait/正常退出通过;完整原输入和日志封存。原SDK缺失单会话取消/反向审批/安全恢复如实保留,尚不计工程交付。 -2. [in_progress] B:受限桥已核对完整schema、原进程/Run/SDK/来源、输入上限和重复ID;继续将项目读取、提案与检查交回自有Task/Run审批和执行账本。来源或目录变化使旧权限失效。 -3. [pending] C:独立工程执行、累计预算、原输出/账本、所属进程停止、未知结果核对与父成果审查;自有Provider夹具和真实Node/React/Python工具链分别验收。 -4. [pending] D:恢复与桌面/390px界面,展示原安装身份、范围、事件、审批、原结果/差异/报告;完善1.13窄屏标题栏,每个状态按所属Run核对。 +2. [complete] B:原工具/模型桥接自有Runtime/Store/WorkerManager;原调用命名空间与savedToolResult工件反馈,校验/等待审批后来源核对、单次实际尝试记账、原输入/报告/HTTP归属。67项相关兼容及最新25项内核通过;原首轮15/17断言失败保留,关闭原进程的桥请求追加复核。 +3. [in_progress] C:已接独立worker工程、累计预算、原输出封存、原进程树与未知恢复;继续实际Harness+本地HTTP模型夹具和真实Node/React/Python功能工程/父审查/Git验收。 +4. [in_progress] D:执行方式/实际安装检测、单引擎工程和分页原运行面板已实现,TS/构建通过;继续真实桌面/390px、停止/核对与窄屏标题栏验收。 5. [pending] E:分别核对Purge 1.1.78/1.1.81来源、兼容性、启停和生效点,建立同题/同预算对照与指标;本地夹具与真实模型收益分开记录,未经重复验证不默认启用。 6. [pending] F:按最终固定代码/基准完成相关与完整回归、真实工程/UI、独立源码/便携、发行与保留状态升级;各切片保存原失败、回顾和本地commit。 diff --git a/tests/controlled-engine.test.mjs b/tests/controlled-engine.test.mjs index e9dfacaa..3c434ea3 100644 --- a/tests/controlled-engine.test.mjs +++ b/tests/controlled-engine.test.mjs @@ -74,6 +74,8 @@ test('bridge authentication, current Run, process, SDK session and browser-origi assert.equal((await b.post('/tool', data, overrides)).status, 403); } assert.equal(calls, 0); assert.equal(b.driver.report.toolRequests, 0); + b.driver.client.exited = true; + assert.equal((await b.post('/tool', data)).status, 403); assert.equal(calls, 0); }); test('bridge duplicates, unknown tools, malformed arguments and source changes are refused before a second side effect', async t => { diff --git a/tests/engine-worker-http.test.mjs b/tests/engine-worker-http.test.mjs new file mode 100644 index 00000000..89ccfe27 --- /dev/null +++ b/tests/engine-worker-http.test.mjs @@ -0,0 +1,44 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; +import { fixture, until } from './helpers.mjs'; +import { workerHttp } from './fixtures/workers-http.mjs'; +import { ControlledWorkerFixture } from './fixtures/controlled-worker.mjs'; +import { createEngineFixture } from '../scripts/engine-fixtures.mjs'; +import { EngineRunner } from '../src/core/engines/engine-runner.mjs'; +import { resolveControlledInstallation, CONTROLLED_CRITICAL_FILES } from '../src/core/engines/controlled-installation.mjs'; + +test('controlled engine HTTP freezes one child, pages original owned input/report, rejects foreign reads and prevents changed-source reuse', async t => { + const f = fixture(t), host = await workerHttp(f), root = createEngineFixture(path.join(f.root, 'harness')); + for (const file of CONTROLLED_CRITICAL_FILES) { fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), 'export const fixture = true;\n'); } + host.runtime.controlledEngines = new EngineRunner(f.store, host.provider, { directoryRoot: path.join(f.root, 'engines'), sessionFactory: () => new ControlledWorkerFixture() }); + const installation = await host.api('/engines/deepseek/controlled-availability?root=' + encodeURIComponent(root)); + assert.equal(installation.controlledStatus, 'available'); + const input = { goal: 'HTTP controlled engine', engine: { id: 'deepseek-harness', root, controlledSha256: installation.controlledSha256 }, + children: [{ label: 'engine-1', goal: 'Repair and check one child', paths: ['src/a.mjs'] }] }; + const route = '/sessions/' + f.session.id, group = (await host.api(route + '/collaborations', input)).group; + assert.equal(group.children.length, 1); const action = operation => host.api(route + '/collaborations/' + group.id + '/' + operation, { fingerprint: group.fingerprint }); + await action('prepare'); await until(() => f.store.core.collaboration.get(group.id).status === 'ready', 20000); + await action('start'); await until(() => f.store.core.collaboration.get(group.id).status === 'awaiting_review', 20000); + const childRoute = '/sessions/' + group.children[0].sessionId, view = await host.api(childRoute + '/engine-runs'), job = view[0]; + assert.equal(job.status, 'completed'); assert.equal(job.nativeSummary.treeConfirmed, true); assert.equal(job.modelRequests, 2); assert.equal(job.toolRequests, 1); + const original = f.store.artifactStore.read(f.store.core.artifact.get(job.reportArtifactId)); + const first = await host.api(childRoute + '/engine-runs/' + job.id + '/report?offset=0&limit=200'); + assert.equal(first.content, original.slice(0, 200)); assert.equal(first.sha256, job.reportSha256); assert.equal(first.truncated, true); + const second = await host.api(childRoute + '/engine-runs/' + job.id + '/report?offset=200&limit=200'); assert.equal(second.content, original.slice(200, 400)); + const raw = await fetch(host.url + '/api' + childRoute + '/engine-runs/' + job.id + '/download', { headers: { 'X-Workbench-Token': host.token } }); + assert.equal(await raw.text(), original); assert.match(raw.headers.get('content-disposition'), /attachment/); + assert.equal((await host.api(childRoute + '/engine-runs/' + job.id + '/input?limit=200')).sha256, job.inputSha256); + assert.equal((await host.raw(childRoute + '/engine-runs/' + job.id + '/report?offset=999999')).status, 400); + assert.equal((await host.raw(childRoute + '/engine-runs', undefined, 'foreign')).status, 403); + assert.ok((await host.raw(route + '/engine-runs/' + job.id + '/report')).status >= 400); + assert.equal((await host.raw(childRoute + '/engines/jobs', { root })).status, 409); + assert.equal((await host.api(childRoute + '/execution')).engineRuns[0].id, job.id); + const artifact = f.store.core.artifact.get(job.reportArtifactId); fs.appendFileSync(path.join(f.store.artifactStore.root, artifact.relative), 'damage'); + assert.ok((await host.raw(childRoute + '/engine-runs/' + job.id + '/report')).status >= 400); + const before = f.store.core.task.size; fs.appendFileSync(resolveControlledInstallation(f.root, root).controlledInputs[0].path, '// changed\n'); + const another = f.store.createSession(f.project.id); + assert.equal((await host.raw('/sessions/' + another.id + '/collaborations', input)).status, 409); + assert.equal(f.store.core.task.size, before); assert.equal(host.requests.length, 2); +}); diff --git a/tests/engine-worker.test.mjs b/tests/engine-worker.test.mjs new file mode 100644 index 00000000..c0545314 --- /dev/null +++ b/tests/engine-worker.test.mjs @@ -0,0 +1,195 @@ +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 { controlledWorker, ControlledWorkerFixture } from './fixtures/controlled-worker.mjs'; +import { applyWorkerProposals, approveWorkerChecks, ownerBytes } from './fixtures/workers-project.mjs'; +import { ControlledDeepSeekSession } from '../src/core/engines/controlled-session.mjs'; +import { Store } from '../src/server/store.mjs'; +import { nativeMessagesToChat, originalEngineRun, verifyEngineModelInput } from '../src/shared/contracts/engine-execution.mjs'; + +const usage = { total_tokens: 168, prompt_tokens: 123, completion_tokens: 45 }; +async function prepare(f, context) { + const group = await context.manager.plan(f.session, context.definition); + await (await context.manager.prepare(f.session, group.id, group.fingerprint)).promise; + assert.equal(group.status, 'ready', group.summary); return group; +} +const jobs = (f, group) => [...f.store.core.engine_run.values()].filter(job => job.collaborationId === group.id); + +test('native text and call IDs preserve original result pairing while unsupported content fails closed', () => { + const input = { system: 'fixed', messages: [{ role: 'assistant', content: [{ type: 'reasoning', text: 'private' }, { type: 'tool-call', id: 'native', name: 'read_file', arguments: '{"path":"README.md"}' }] }, + { role: 'tool', toolCallId: 'native', content: [{ type: 'text', text: 'result' }] }] }; + const mapped = nativeMessagesToChat(input); assert.equal(mapped[1].content, ''); assert.equal(mapped[1].tool_calls[0].id, 'native'); assert.equal(mapped[2].tool_call_id, 'native'); + assert.throws(() => nativeMessagesToChat({ messages: [{ role: 'user', content: [{ type: 'image', url: 'invalid' }] }] }), /尚未验证/); + assert.throws(() => nativeMessagesToChat({ messages: [{ role: 'tool', content: [] }] }), /toolCallId/); +}); + +test('one engine child freezes real installation identity before creating tasks and rejects a changed source', async t => { + const f = fixture(t), context = controlledWorker(f); + const group = await context.manager.plan(f.session, context.definition); assert.equal(group.children.length, 1); + assert.equal(group.children[0].engine.controlledSha256, context.installation.controlledSha256); + fs.appendFileSync(context.installation.controlledInputs[0].path, '// changed\n'); + assert.throws(() => context.runtime.engineRunner().selected(context.definition.engine), /不一致/); + await (await context.manager.prepare(f.session, group.id, group.fingerprint)).promise; + const worker = f.store.core.worker.get(group.children[0].id); group.deadline = Date.now() + 60000; + assert.throws(() => context.manager.configFor(group, worker), /已变化/); + assert.equal(context.requests.length, 0); +}); + +test('owned native tools return saved results, use real approvals/checks and account each actual request once across new engine Runs', async t => { + const f = fixture(t), context = controlledWorker(f), original = ownerBytes(f), group = await prepare(f, context); + await (await context.manager.start(f.session, group.id, group.fingerprint)).promise; + assert.equal(group.status, 'awaiting_review', group.summary); assert.equal(jobs(f, group)[0].status, 'completed'); + await applyWorkerProposals(f, context.runtime, group); + await approveWorkerChecks(f, context.runtime, group, context.manager.start(f.session, group.id, group.fingerprint)); + assert.equal(group.status, 'ready_to_collect', group.summary); assert.deepEqual(ownerBytes(f), original); + const child = group.children[0], session = f.store.session(child.sessionId), worker = f.store.core.worker.get(child.id); + assert.equal(worker.status, 'completed', worker.summary); assert.equal(session.approvals.filter(value => value.status === 'approved').length, 1); + const all = jobs(f, group), requestCount = all.reduce((sum, job) => sum + job.requests.length, 0); + assert.equal(all.length, 2); assert.equal(f.store.core.task.get(worker.taskId).usage.requests, requestCount); + assert.equal(f.store.core.task.get(worker.taskId).usage.totalTokens, requestCount * 168); + assert.ok(all.every(job => job.status === 'completed' && job.requests.every(request => request.accountedAttempts === 1))); + for (const job of all) { + originalEngineRun(f.store, session, job); + for (const request of job.requests) verifyEngineModelInput(f.store, session, job, request); + for (const entry of job.calls) { + assert.equal(entry.callId, job.id + ':' + entry.sourceCallId); + assert.equal(f.store.core.tool.get(entry.invocationId).callId, entry.callId); assert.ok(entry.outputArtifactId); + } + } + assertToolPairs(session.providerMessages); + assert.ok(context.requests.some(request => request.messages.some(message => message.role === 'tool' + && JSON.parse(message.content).status === 'completed' && JSON.parse(message.content).check_status === 'passed'))); + assert.equal([...f.store.core.context_plan.values()].filter(plan => plan.taskId === worker.taskId).some(plan => plan.workerDispatchedAt), false); + assert.equal(context.runtime.controlledEngines.drivers.size, 0); + const finalReport = f.store.core.artifact.get(all.at(-1).reportArtifactId); + fs.appendFileSync(path.join(f.store.artifactStore.root, finalReport.relative), 'damaged'); + await assert.rejects(context.manager.output(session, worker), /校验失败/); +}); + +test('forged native scope cannot change a parent file and a final completion claim cannot replace worker proof', async t => { + const f = fixture(t), context = controlledWorker(f, { respond: ({ messages }) => messages.some(message => message.role === 'tool') + ? { message: { role: 'assistant', content: 'claimed complete' }, usage } + : { message: { role: 'assistant', content: '', tool_calls: [call('outside', 'propose_file', { path: 'README.md', content: 'forged', reason: 'forged' })] }, usage } }); + const original = ownerBytes(f), group = await prepare(f, context); + await (await context.manager.start(f.session, group.id, group.fingerprint)).promise; + const session = f.store.session(group.children[0].sessionId); assert.deepEqual(ownerBytes(f), original); + assert.equal(session.changes.length, 0); assert.equal(group.status, 'paused'); assert.equal(f.store.core.run.get(session.runId).stopReason, 'worker_incomplete'); + assert.equal(context.native[0].received[0].result.status, 'error'); assert.match(context.native[0].received[0].result.message, /范围/); +}); + +test('duplicate native IDs are rejected without replaying a saved tool', async t => { + const f = fixture(t), context = controlledWorker(f, { respond: () => ({ message: { role: 'assistant', content: '', tool_calls: [call('same', 'read_file', { path: 'README.md' })] }, usage }) }); + const group = await prepare(f, context); await (await context.manager.start(f.session, group.id, group.fingerprint)).promise; + const job = jobs(f, group)[0]; assert.equal(job.calls.length, 1); assert.equal(f.store.core.tool.size, 1); assert.equal(job.status, 'failed'); + assert.equal(context.requests.length, 2); assert.match(job.error, /已经登记/); +}); + +test('native input exceeding the context budget is retained and sends no model request', async t => { + class Oversized extends ControlledWorkerFixture { + async wait() { const { onModel, tools, config, signal } = this.options; return onModel({ model: config.model, maxTokens: config.maxOutputTokens, + tools: tools.map(value => value.function), messages: [{ role: 'user', content: [{ type: 'text', text: 'x'.repeat(2000000) }] }] }, { id: randomUUID() }, signal); } + } + const f = fixture(t), context = controlledWorker(f, { sessionFactory: () => new Oversized() }), group = await prepare(f, context); + await (await context.manager.start(f.session, group.id, group.fingerprint)).promise; + assert.equal(context.requests.length, 0); assert.equal(jobs(f, group)[0].requests.length, 0); + assert.equal(f.store.core.task.get(group.children[0].taskId).usage.requests, 0); assert.match(jobs(f, group)[0].error, /预算/); +}); + +test('stop retains a late reported model usage once and executes no tool from that output', async t => { + let release; const waiting = new Promise(resolve => { release = resolve; }); + const f = fixture(t), context = controlledWorker(f, { respond: async () => { await waiting; return { message: { role: 'assistant', content: '', tool_calls: [call('late', 'propose_file', { path: 'src/a.mjs', content: 'late', reason: 'late' })] }, usage }; } }); + const group = await prepare(f, context), started = context.manager.start(f.session, group.id, group.fingerprint); + await until(() => context.requests.length === 1); const stopping = context.runtime.stop(f.session.id); release(); + await stopping; await started.promise; + const task = f.store.core.task.get(group.children[0].taskId), session = f.store.session(group.children[0].sessionId); + assert.equal(task.usage.requests, 1); assert.equal(task.usage.totalTokens, 168); assert.equal(session.changes.length, 0); + assert.equal(jobs(f, group)[0].calls.length, 0); assert.equal(jobs(f, group)[0].status, 'cancelled'); + assert.ok(jobs(f, group)[0].requests[0].responseArtifactId); assert.equal(context.runtime.controlledEngines.drivers.size, 0); +}); + +test('a source change after the original model intent stops before native tools execute', async t => { + const f = fixture(t); let context; + context = controlledWorker(f, { respond: () => { fs.appendFileSync(context.installation.controlledInputs[0].path, '// runtime changed\n'); + return { message: { role: 'assistant', content: '', tool_calls: [call('changed', 'propose_file', { path: 'src/a.mjs', content: 'bad', reason: 'bad' })] }, usage }; } }); + const group = await prepare(f, context); await (await context.manager.start(f.session, group.id, group.fingerprint)).promise; + assert.equal(f.store.session(group.children[0].sessionId).changes.length, 0); assert.equal(jobs(f, group)[0].calls.length, 0); + assert.equal(jobs(f, group)[0].requiresVerification, false); assert.match(jobs(f, group)[0].error, /已变化/); +}); +test('an engine source change while approval is pending invalidates the old permission before any check starts', async t => { + const f = fixture(t), context = controlledWorker(f, { respond: ({ messages }) => { + const result = messages.filter(message => message.role === 'tool').at(-1); + return { message: { role: 'assistant', content: '', tool_calls: [result ? call('check', 'run_checks', { plan_id: JSON.parse(result.content).id }) : call('plan', 'plan_checks', { names: ['test', 'build'] })] }, usage }; + } }), group = await prepare(f, context), started = context.manager.start(f.session, group.id, group.fingerprint); + const session = f.store.session(group.children[0].sessionId); + await until(() => session.approvals.some(approval => approval.status === 'pending'), 15000); + fs.appendFileSync(context.installation.controlledInputs[0].path, '// changed while awaiting approval\n'); + context.runtime.decide(session.id, session.approvals.find(approval => approval.status === 'pending').id, true); + await started.promise; assert.equal(f.store.core.check_job.size, 0); assert.equal(jobs(f, group)[0].status, 'failed'); + assert.equal(context.requests.length, 2); assert.match(jobs(f, group)[0].error, /已变化/); +}); + +test('missing or noncompleted turn end cannot be adopted even with an owned receipt and assistant text', async () => { + for (const kind of [null, 'failed', 'cancelled']) { + const native = new ControlledDeepSeekSession({}); native.request = async () => ({}); native.report = { receipt: { messageId: 'owned' }, notifications: [ + { method: 'session.event', params: { event: { type: 'agent/inbox/spliced', data: { inserted: [{ id: 'owned' }] } } } }, + { method: 'session.event', params: { event: { type: 'assistant/message', data: { message: { content: [{ type: 'text', text: 'claim' }] } } } } }, + ...(kind ? [{ method: 'session.event', params: { event: { type: 'turn/end', data: { reason: { kind } } } } }] : [])] }; + await assert.rejects(native.wait(), /正常结束/); + } +}); + +test('closed root with an original live descendant remains unknown and retains its process tree', async () => { + const native = new ControlledDeepSeekSession({}, { processTreeReader: async () => ({ supported: true, processes: [{ pid: 91, createdAt: '2026-10-31T00:00:00Z' }] }) }); + native.report = { process: { pid: 90, startedAt: new Date().toISOString() }, notifications: [] }; + native.client = { exited: true, close: async () => ({ rootExited: true, exit: { code: 0 } }), trace: [], stderr: '' }; + const result = await native.close({ force: true }); assert.equal(result.status, 'unknown'); assert.equal(result.cleanup.treeConfirmed, false); + assert.equal(result.process.descendants[0].pid, 91); +}); + +async function pendingRecovery(f, context) { + const group = await prepare(f, context); await (await context.manager.start(f.session, group.id, group.fingerprint)).promise; + const job = jobs(f, group)[0], session = f.store.session(job.sessionId), old = job.requests[0]; + const input = verifyEngineModelInput(f.store, session, job, old); + const request = { id: randomUUID(), bridgeTraceId: randomUUID(), sourceSha256: old.sourceSha256, requestSha256: old.requestSha256, + requestBytes: old.requestBytes, estimatedInputTokens: old.estimatedInputTokens, reservedPerAttempt: old.reservedPerAttempt, + maxAttempts: 2, accountedAttempts: 0, observedAttempts: 1, status: 'dispatched', startedAt: new Date().toISOString(), dispatchedAt: new Date().toISOString() }; + const artifact = f.store.artifactStore.write(session, f.store.core.run.get(job.runId), { schemaVersion: 1, owner: input.owner, + native: input.native, body: input.body, plan: { ...request, ...input.owner } }, { kind: 'engine-model-input' }); + f.store.core.artifact.set(artifact.id, artifact); request.inputArtifactId = artifact.id; request.inputSha256 = artifact.sha256; + job.requests.push(request); job.status = 'running'; job.finishedAt = null; job.native = null; job.reportArtifactId = null; job.reportSha256 = null; + job.originalOutcome = null; job.process = { pid: 2147483000, startedAt: new Date().toISOString(), rootCreatedAt: '2026-10-31T00:00:00.000Z', descendants: [] }; + group.status = 'running'; f.store.save(f.session); + const worker = f.store.core.worker.get(group.children[0].id); worker.status = 'running'; worker.ownerPid = null; + session.status = 'running'; const run = f.store.core.run.get(job.runId); run.status = 'running'; run.finishedAt = null; + f.store.save(session); return { job, request, worker, group, before: structuredClone(f.store.core.task.get(worker.taskId).usage) }; +} + +test('host recovery reserves original pending attempts exactly once and explicit review preserves unknown outcomes', async t => { + const f = fixture(t), context = controlledWorker(f), pending = await pendingRecovery(f, context); f.store.close(); + const reopened = new Store(f.data), task = reopened.core.task.get(pending.worker.taskId); + assert.equal(task.usage.requests, pending.before.requests + 2); assert.equal(task.usage.unknownRequests, pending.before.unknownRequests + 2); + assert.equal(task.usage.estimatedUnknownTokens, pending.before.estimatedUnknownTokens + 2 * pending.request.reservedPerAttempt); + const firstUsage = structuredClone(task.usage); reopened.close(); + const second = new Store(f.data); f.onCleanup(() => second.close()); assert.deepEqual(second.core.task.get(task.id).usage, firstUsage); + const { Runtime } = await import('../src/server/runtime.mjs'); const runtime = new Runtime(second, context.provider); f.onCleanup(() => runtime.stopAll()); + const manager = runtime.workerManager(), parent = second.session(f.session.id), group = second.core.collaboration.get(pending.group.id); + assert.equal(group.status, 'unknown'); assert.throws(() => manager.start(parent, group.id, group.fingerprint), /核对/); + await (await manager.reconcile(parent, group.id, group.fingerprint)).promise; + assert.ok(group.evidence, group.summary); const original = structuredClone(second.core.engine_run.get(pending.job.id)); + await manager.acknowledge(parent, group.id, group.fingerprint, group.evidence.sha256); + const reviewed = second.core.engine_run.get(pending.job.id); assert.equal(reviewed.status, 'resolved_external'); assert.equal(reviewed.verified, false); + assert.deepEqual(reviewed.originalOutcome, original.originalOutcome); assert.deepEqual(reviewed.requests, original.requests); + assert.equal(context.requests.length, 2); assert.equal(reviewed.review.result, 'unknown-preserved'); +}); + +test('corrupted model reservation cannot reduce recovered budget or permit a new engine request', async t => { + const f = fixture(t), context = controlledWorker(f), pending = await pendingRecovery(f, context); + pending.request.reservedPerAttempt = 1; f.store.save(f.store.session(pending.worker.sessionId)); f.store.close(); + const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); const job = reopened.core.engine_run.get(pending.job.id); + assert.equal(job.requiresVerification, true); assert.match(job.requests.at(-1).verificationError, /预算|不一致/); + assert.equal(reopened.executionSnapshot(reopened.session(pending.worker.sessionId)).accountedTaskTokens, pending.worker.budget.maxTokens); + assert.equal(job.requests.at(-1).recoveryConservativeCharge, true); assert.equal(context.requests.length, 2); +}); diff --git a/tests/fixtures/controlled-worker.mjs b/tests/fixtures/controlled-worker.mjs new file mode 100644 index 00000000..a0b0062c --- /dev/null +++ b/tests/fixtures/controlled-worker.mjs @@ -0,0 +1,55 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { randomUUID } from 'node:crypto'; +import { createEngineFixture } from '../../scripts/engine-fixtures.mjs'; +import { CONTROLLED_CRITICAL_FILES, resolveControlledInstallation, controlledDigest } from '../../src/core/engines/controlled-installation.mjs'; +import { EngineRunner } from '../../src/core/engines/engine-runner.mjs'; +import { prepareWorkerFixture } from './workers-project.mjs'; + +// A protocol fixture, not the native Harness. Native acceptance is independent. +export class ControlledWorkerFixture { + constructor() { this.report = { notifications: [], bridgeRequests: [] }; this.received = []; } + async open(options) { + this.options = options; this.report.owner = options.owner; this.report.sdkSessionId = options.sdkSessionId; + options.onState('ready', {}); return this.report; + } + async prompt(content) { this.content = content; this.report.receipt = { messageId: randomUUID() }; return this.report.receipt; } + async wait() { + const { onModel, onTool, tools, config, signal } = this.options; + const messages = [{ role: 'user', content: [{ type: 'text', text: this.content }] }]; + for (let step = 0; step < 100; step++) { + signal.throwIfAborted(); + const input = { messages: structuredClone(messages), system: 'Protocol fixture for the owned task.', + tools: tools.map(value => value.function ?? value), toolHistory: null, model: config.model, maxTokens: config.maxOutputTokens }; + const result = await onModel(input, { id: randomUUID() }, signal); + signal.throwIfAborted(); + const message = result.message, content = [{ type: 'text', text: message.content }]; + for (const call of message.tool_calls ?? []) content.push({ type: 'tool-call', id: call.id, name: call.function.name, arguments: call.function.arguments }); + messages.push({ role: 'assistant', content }); + if (!message.tool_calls?.length) return message.content; + for (const call of message.tool_calls) { + const input = { callId: call.id, name: call.function.name, arguments: JSON.parse(call.function.arguments) }; + const result = await onTool(input, { id: randomUUID() }, signal); + this.received.push({ input, result, resultSha256: controlledDigest(result) }); + messages.push({ role: 'tool', toolCallId: call.id, content: [{ type: 'text', text: JSON.stringify(result) }] }); + } + } + throw new Error('Fixture exceeded its step bound.'); + } + async close({ force } = {}) { + return Object.assign(this.report, { status: force ? 'cancelled' : 'closed', cleanup: { rootExited: true, treeConfirmed: true, remainingProcesses: [] } }); + } +} +export function controlledWorker(f, { respond = null, sessionFactory = () => new ControlledWorkerFixture() } = {}) { + const context = prepareWorkerFixture(f, { respond }); + const root = createEngineFixture(path.join(f.root, 'controlled-harness')); + for (const file of CONTROLLED_CRITICAL_FILES) { + fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), 'export const fixture = true;\n'); + } + const installation = resolveControlledInstallation(f.root, root), native = []; + context.runtime.controlledEngines = new EngineRunner(f.store, context.provider, { directoryRoot: path.join(f.root, 'owned-engines'), + sessionFactory: () => { const value = sessionFactory(); native.push(value); return value; } }); + const definition = { goal: '交付受控引擎的独立工程成果', engine: { id: 'deepseek-harness', root, controlledSha256: installation.controlledSha256 }, + budget: { maxSteps: 30 }, children: [{ label: 'engine-1', goal: '实现独立文件并通过当前检查', paths: ['src/a.mjs'] }] }; + return { ...context, installation, root, native, definition }; +}