@zhushanwen/subagent-core 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/chunk-KXVBIVOU.js +16 -0
- package/dist/chunk-T2SYBW3I.js +24 -0
- package/dist/chunk-VURUAGMM.js +2305 -0
- package/dist/{types-C3fE3R9x.d.cts → engine-discovery-scan-ocpM8FmI.d.cts} +770 -668
- package/dist/{types-C3fE3R9x.d.ts → engine-discovery-scan-ocpM8FmI.d.ts} +770 -668
- package/dist/execution/engine/engine-discovery-scan.cjs +2210 -0
- package/dist/execution/engine/engine-discovery-scan.d.cts +4 -0
- package/dist/execution/engine/engine-discovery-scan.d.ts +4 -0
- package/dist/execution/engine/engine-discovery-scan.js +20 -0
- package/dist/execution/engine/paths.cjs +6 -23
- package/dist/execution/engine/paths.d.cts +1 -8
- package/dist/execution/engine/paths.d.ts +1 -8
- package/dist/execution/engine/paths.js +1 -1
- package/dist/execution/relay-env.cjs +12 -28
- package/dist/execution/relay-env.d.cts +1 -33
- package/dist/execution/relay-env.d.ts +1 -33
- package/dist/execution/relay-env.js +5 -3
- package/dist/index.cjs +14045 -16059
- package/dist/index.d.cts +1035 -1579
- package/dist/index.d.ts +1035 -1579
- package/dist/index.js +9838 -13800
- package/dist.bundle/index.cjs +28875 -30323
- package/package.json +20 -26
- package/src/__tests__/record-store-last-line.test.ts +16 -7
- package/src/core/error-message.ts +7 -6
- package/src/core/host-services.ts +8 -2
- package/src/core/notify-ports.ts +17 -2
- package/src/execution/__tests__/chat-engine-routing.test.ts +62 -28
- package/src/execution/__tests__/chat-round-first-round-watchdog.test.ts +255 -0
- package/src/execution/__tests__/collect-coordinator-service.test.ts +36 -47
- package/src/execution/__tests__/collect-mixed-dispatch.test.ts +20 -53
- package/src/execution/__tests__/delivery-methods.test.ts +179 -237
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +0 -2
- package/src/execution/__tests__/execute-nesting.test.ts +20 -13
- package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +20 -33
- package/src/execution/__tests__/gc-timer.test.ts +7 -22
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +20 -10
- package/src/execution/__tests__/helpers/fake-engine-port.ts +187 -0
- package/src/execution/__tests__/helpers/spawn-mock.ts +36 -9
- package/src/execution/__tests__/helpers/subagent-service-mocks.ts +1 -1
- package/src/execution/__tests__/idle-gc.test.ts +200 -0
- package/src/execution/__tests__/lifecycle-predicates.test.ts +26 -21
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +5 -2
- package/src/execution/__tests__/reconcile-sweep.test.ts +200 -0
- package/src/execution/__tests__/record-store.test.ts +60 -6
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +20 -13
- package/src/execution/__tests__/round-supervisor.test.ts +293 -0
- package/src/execution/__tests__/session-pending.test.ts +119 -17
- package/src/execution/__tests__/stream-sink-retirement.test.ts +10 -10
- package/src/execution/__tests__/subagent-service-multiproc-guard.test.ts +1 -11
- package/src/execution/__tests__/subagent-service-notify-gate.test.ts +33 -30
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +2 -9
- package/src/execution/__tests__/subagent-service-recovery-bounds.test.ts +59 -129
- package/src/execution/__tests__/subagent-service.test.ts +2 -2
- package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +109 -7
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +108 -36
- package/src/execution/__tests__/sync-collect-recovery.test.ts +21 -36
- package/src/execution/__tests__/workflow-state-root.test.ts +232 -0
- package/src/execution/agent-registry.ts +6 -2
- package/src/execution/dialog-queue.ts +6 -73
- package/src/execution/engine/__tests__/common/capability-gate.test.ts +115 -15
- package/src/execution/engine/__tests__/common/errors.test.ts +11 -18
- package/src/execution/engine/__tests__/common/kill-chain.test.ts +7 -0
- package/src/execution/engine/__tests__/common/nesting-guard.test.ts +9 -3
- package/src/execution/engine/__tests__/common/session-view-service.test.ts +90 -3
- package/src/execution/engine/__tests__/conformance/H9-test-disposition.md +116 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/fake-engine-protocol.mjs +377 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/recorded.fixture.json +331 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/engine-protocol/smoke-run.fixture.json +213 -0
- package/src/execution/engine/__tests__/conformance/chat-round-protocol.test.ts +230 -0
- package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +69 -42
- package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +4 -81
- package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +67 -44
- package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +81 -54
- package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +133 -70
- package/src/execution/engine/__tests__/conformance/engine-crash.test.ts +143 -0
- package/src/execution/engine/__tests__/conformance/fake-engine-capabilities.ts +19 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +7 -2
- package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +6 -5
- package/src/execution/engine/__tests__/conformance/protocol-blackbox.test.ts +398 -0
- package/src/execution/engine/__tests__/conformance/registry-fork-filter.test.ts +221 -0
- package/src/execution/engine/__tests__/conformance/round-liveness-supervisor.test.ts +302 -0
- package/src/execution/engine/__tests__/conformance/static-spawn-guard.test.ts +58 -0
- package/src/execution/engine/__tests__/engine-discovery-scan.test.ts +558 -0
- package/src/execution/engine/__tests__/host-task-spec.test.ts +45 -0
- package/src/execution/engine/__tests__/model-ref-split.test.ts +74 -0
- package/src/execution/engine/__tests__/registry.test.ts +206 -1
- package/src/execution/engine/__tests__/routing.test.ts +97 -0
- package/src/execution/engine/__tests__/run-failure-worktree-cleanup.test.ts +97 -0
- package/src/execution/engine/client/__tests__/__fixtures__/fake-engine.mjs +294 -0
- package/src/execution/engine/client/__tests__/engine-client.test.ts +516 -0
- package/src/execution/engine/client/__tests__/mirror.test.ts +67 -0
- package/src/execution/engine/client/__tests__/pid-file.test.ts +257 -0
- package/src/execution/engine/client/__tests__/protocol-closure.test.ts +95 -0
- package/src/execution/engine/client/__tests__/remote-engine.test.ts +457 -0
- package/src/execution/engine/client/client-options.ts +103 -0
- package/src/execution/engine/client/engine-client.ts +687 -0
- package/src/execution/engine/client/mirror.ts +167 -0
- package/src/execution/engine/client/pid-file.ts +392 -0
- package/src/execution/engine/client/reaper.ts +95 -0
- package/src/execution/engine/client/remote-engine.ts +441 -0
- package/src/execution/engine/client/reverse-router.ts +255 -0
- package/src/execution/engine/common/capability-gate.ts +109 -22
- package/src/execution/engine/common/errors.ts +9 -25
- package/src/execution/engine/common/event-journal.ts +4 -50
- package/src/execution/engine/common/journal-replay.ts +3 -1
- package/src/execution/engine/common/kill-chain.ts +18 -251
- package/src/execution/engine/common/nesting-guard.ts +18 -114
- package/src/execution/engine/common/pool-manager.ts +4 -0
- package/src/execution/engine/common/session-view-service.ts +56 -84
- package/src/execution/engine/config.ts +151 -0
- package/src/execution/engine/d8-compat.ts +346 -0
- package/src/execution/engine/engine-discovery-roots.ts +107 -0
- package/src/execution/engine/engine-discovery-scan.ts +269 -0
- package/src/execution/engine/engine-discovery.ts +30 -7
- package/src/execution/engine/engine-inspect-package.ts +282 -0
- package/src/execution/engine/engine-manifest.ts +244 -0
- package/src/execution/engine/host/__tests__/host-askuser-endpoint.test.ts +123 -0
- package/src/execution/engine/host/__tests__/host-bridge.test.ts +135 -0
- package/src/execution/engine/host/__tests__/spawned-children.test.ts +73 -0
- package/src/execution/engine/host/host-bridge.ts +150 -0
- package/src/execution/engine/host/host-ui-endpoint.ts +47 -0
- package/src/execution/engine/host/pi-host-binding.ts +91 -0
- package/src/execution/engine/host/spawned-children.ts +139 -0
- package/src/execution/engine/host-task-spec.ts +29 -2
- package/src/execution/engine/model-validation.ts +55 -2
- package/src/execution/engine/paths.ts +16 -41
- package/src/execution/engine/port.ts +48 -2
- package/src/execution/engine/registry.ts +153 -34
- package/src/execution/engine/routing.ts +225 -52
- package/src/execution/engine/types.ts +25 -200
- package/src/execution/finalize-record.ts +25 -0
- package/src/execution/idle-gc.ts +89 -14
- package/src/execution/lifecycle-manager.ts +10 -7
- package/src/execution/lifecycle-predicates.ts +10 -8
- package/src/execution/record-store.ts +29 -2
- package/src/execution/relay-env.ts +19 -36
- package/src/execution/round-supervisor/domain.ts +89 -0
- package/src/execution/round-supervisor/index.ts +33 -0
- package/src/execution/round-supervisor/notify-accounting.ts +87 -0
- package/src/execution/round-supervisor/reconcile-sweep.ts +235 -0
- package/src/execution/round-supervisor/service-binding.test.ts +661 -0
- package/src/execution/round-supervisor/service-binding.ts +273 -0
- package/src/execution/round-supervisor/supervisor.ts +426 -0
- package/src/execution/session-pending.ts +68 -74
- package/src/execution/settled-watchdog.ts +39 -0
- package/src/execution/subagent-service.ts +746 -400
- package/src/execution/subprocess-agent-runner.ts +16 -8
- package/src/execution/types.ts +41 -117
- package/src/execution/ui-channels.ts +13 -214
- package/src/execution/ui-request-handler-factory.ts +7 -4
- package/src/execution/ui-request-observability.ts +2 -2
- package/src/execution/workflow-state-root.ts +67 -0
- package/src/execution/worktree-git-ops.ts +5 -0
- package/src/execution/worktree-manager.ts +11 -1
- package/src/index.ts +55 -26
- package/src/orchestration/__tests__/execute-agent-call.test.ts +1 -1
- package/src/orchestration/execute-agent-call.ts +1 -1
- package/src/orchestration/file-run-store.ts +144 -59
- package/src/orchestration/models/types.ts +43 -42
- package/src/shared/zcode-model-ref.ts +31 -0
- package/dist/chunk-43ONBFZX.js +0 -240
- package/dist/chunk-A4IVZWVX.js +0 -31
- package/dist/chunk-APZY4IME.js +0 -27
- package/dist/chunk-V3VHZ2VX.js +0 -59
- package/dist/execution/engine/engines/zcode/constants.cjs +0 -107
- package/dist/execution/engine/engines/zcode/constants.d.cts +0 -135
- package/dist/execution/engine/engines/zcode/constants.d.ts +0 -135
- package/dist/execution/engine/engines/zcode/constants.js +0 -54
- package/dist/execution/engine/engines/zcode/reader.cjs +0 -276
- package/dist/execution/engine/engines/zcode/reader.d.cts +0 -21
- package/dist/execution/engine/engines/zcode/reader.d.ts +0 -21
- package/dist/execution/engine/engines/zcode/reader.js +0 -9
- package/src/__tests__/append-system-prompt-assembly.test.ts +0 -243
- package/src/__tests__/fr4-get-state-handshake.test.ts +0 -125
- package/src/__tests__/session-runner.test.ts +0 -69
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +0 -490
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +0 -191
- package/src/execution/__tests__/descendant-sweep-guards.test.ts +0 -220
- package/src/execution/__tests__/descendant-sweep.test.ts +0 -269
- package/src/execution/__tests__/engine-model-validation.test.ts +0 -741
- package/src/execution/__tests__/epipe-fallback.test.ts +0 -248
- package/src/execution/__tests__/execution-runtime-face.test.ts +0 -272
- package/src/execution/__tests__/get-state-handshake.test.ts +0 -127
- package/src/execution/__tests__/helpers/session-runner-mocks.ts +0 -113
- package/src/execution/__tests__/keep-alive-no-progress.test.ts +0 -280
- package/src/execution/__tests__/kill-all-escalation.test.ts +0 -196
- package/src/execution/__tests__/max-turns-to-watchdog-ms.test.ts +0 -113
- package/src/execution/__tests__/output-collector.test.ts +0 -469
- package/src/execution/__tests__/pi-invocation.test.ts +0 -166
- package/src/execution/__tests__/recursive-visibility-env.test.ts +0 -149
- package/src/execution/__tests__/rpc-mode.test.ts +0 -89
- package/src/execution/__tests__/run-and-finalize-anchoring.test.ts +0 -387
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +0 -277
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +0 -254
- package/src/execution/__tests__/run-spawn-edges.test.ts +0 -1079
- package/src/execution/__tests__/run-spawn-integration.test.ts +0 -933
- package/src/execution/__tests__/run-spawn-resume.test.ts +0 -322
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +0 -196
- package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +0 -207
- package/src/execution/__tests__/service-kill-escalation.test.ts +0 -91
- package/src/execution/__tests__/session-runner-branch-cache-lru.test.ts +0 -110
- package/src/execution/__tests__/session-runner-close-prune.test.ts +0 -181
- package/src/execution/__tests__/session-runner-dispatch.test.ts +0 -489
- package/src/execution/__tests__/session-runner-epipe.test.ts +0 -179
- package/src/execution/__tests__/session-runner-heartbeat-idle-fallback.test.ts +0 -227
- package/src/execution/__tests__/session-runner-lifecycle-helpers.test.ts +0 -290
- package/src/execution/__tests__/session-runner-schema-env.test.ts +0 -352
- package/src/execution/__tests__/settled-watchdog.test.ts +0 -524
- package/src/execution/__tests__/spawn-args.test.ts +0 -446
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +0 -192
- package/src/execution/__tests__/spawn-event-adapter.test.ts +0 -167
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +0 -208
- package/src/execution/__tests__/spawned-children.test.ts +0 -236
- package/src/execution/__tests__/start-sync-model-guard.test.ts +0 -150
- package/src/execution/__tests__/stdin-writer.test.ts +0 -464
- package/src/execution/__tests__/subagent-service-message-close.test.ts +0 -693
- package/src/execution/__tests__/temp-prompt.test.ts +0 -53
- package/src/execution/__tests__/timeout-integration.test.ts +0 -616
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +0 -194
- package/src/execution/__tests__/turn-limiter.test.ts +0 -65
- package/src/execution/__tests__/ui-request-handler.test.ts +0 -205
- package/src/execution/__tests__/ui-request-queue.test.ts +0 -298
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +0 -230
- package/src/execution/engine/__tests__/common/schema-emulation.test.ts +0 -128
- package/src/execution/engine/__tests__/common/session-view-service-zcode-dbpath.test.ts +0 -149
- package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +0 -28
- package/src/execution/engine/__tests__/conformance/zcode-appserver-harness.ts +0 -128
- package/src/execution/engine/__tests__/paths.test.ts +0 -39
- package/src/execution/engine/common/schema-emulation.ts +0 -189
- package/src/execution/engine/common/session-view-projection.ts +0 -51
- package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +0 -615
- package/src/execution/engine/engines/pi/__tests__/reader.test.ts +0 -155
- package/src/execution/engine/engines/pi/__tests__/spawn-opts-direct.test.ts +0 -257
- package/src/execution/engine/engines/pi/argv-mirror.ts +0 -135
- package/src/execution/engine/engines/pi/get-state-handshake.ts +0 -179
- package/src/execution/engine/engines/pi/output-collector.ts +0 -317
- package/src/execution/engine/engines/pi/pi-engine.ts +0 -811
- package/src/execution/engine/engines/pi/pi-invocation.ts +0 -120
- package/src/execution/engine/engines/pi/reader.ts +0 -59
- package/src/execution/engine/engines/pi/registration.ts +0 -37
- package/src/execution/engine/engines/pi/session-runner.ts +0 -2840
- package/src/execution/engine/engines/pi/spawn-event-adapter.ts +0 -404
- package/src/execution/engine/engines/pi/stdin-writer.ts +0 -199
- package/src/execution/engine/engines/pi/temp-prompt.ts +0 -62
- package/src/execution/engine/engines/pi/turn-limiter.ts +0 -102
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/fake-appserver.mjs +0 -301
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-appserver.json +0 -36
- package/src/execution/engine/engines/zcode/__tests__/appserver-launcher.test.ts +0 -321
- package/src/execution/engine/engines/zcode/__tests__/connection.test.ts +0 -473
- package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +0 -43
- package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +0 -124
- package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +0 -210
- package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +0 -71
- package/src/execution/engine/engines/zcode/__tests__/session-channel-dispose-harvest.test.ts +0 -247
- package/src/execution/engine/engines/zcode/__tests__/session-channel-turn-timers.test.ts +0 -451
- package/src/execution/engine/engines/zcode/__tests__/session-channel.test.ts +0 -792
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-appserver.test.ts +0 -782
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-degrade.test.ts +0 -168
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-dispose.test.ts +0 -210
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-retry.test.ts +0 -353
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-status.test.ts +0 -282
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine-timeout.test.ts +0 -372
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +0 -145
- package/src/execution/engine/engines/zcode/appserver-launcher.ts +0 -167
- package/src/execution/engine/engines/zcode/connection.ts +0 -598
- package/src/execution/engine/engines/zcode/constants.ts +0 -176
- package/src/execution/engine/engines/zcode/golden-sample.ts +0 -47
- package/src/execution/engine/engines/zcode/parser.ts +0 -102
- package/src/execution/engine/engines/zcode/preparer.ts +0 -235
- package/src/execution/engine/engines/zcode/reader.ts +0 -381
- package/src/execution/engine/engines/zcode/registration.ts +0 -37
- package/src/execution/engine/engines/zcode/session-channel.ts +0 -968
- package/src/execution/engine/engines/zcode/zcode-engine.ts +0 -1431
- package/src/execution/round-settlement.ts +0 -93
- package/src/execution/ui-request-queue.ts +0 -215
- package/src/shared/schema-env.ts +0 -44
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// fake 引擎 CLI(W2 EngineClient / RemoteEngine 测试 fixture)。
|
|
2
|
+
//
|
|
3
|
+
// 形态对齐协议 v1(stdout 独占 NDJSON 帧;stderr 样本;stdin 帧驱动):
|
|
4
|
+
// - 模式(--mode / FAKE_ENGINE_MODE):normal(缺省)/ crash(启动即崩,stderr 留样本)/
|
|
5
|
+
// hang(不回 initialize 应答,供握手超时);
|
|
6
|
+
// - 协议版本(FAKE_PROTOCOL_VERSION,缺省 1):版本协商越界测试用;
|
|
7
|
+
// - run 动作脚本(FAKE_RUN_ACTIONS,JSON 数组):emit / streamDelta / poolResolved /
|
|
8
|
+
// handleReady / childSpawned / childStateChanged / askUser / log / delay / spawnGrandchild /
|
|
9
|
+
// exit —— 逐动作播放,播完回 run 终态应答;
|
|
10
|
+
// - cancel:受理应答;FAKE_CANCEL_SETTLE=1 时同步收敛 run 终态(测收敛路径),
|
|
11
|
+
// 缺省继续挂(测 3s 收敛窗口超时杀链);
|
|
12
|
+
// - spawnGrandchild:spawn 同组长眠子进程(不 detached → 与引擎同组,供组杀断言),
|
|
13
|
+
// pid 经 childSpawned 上报。
|
|
14
|
+
//
|
|
15
|
+
// 反向请求应答(core → 引擎的帧②,id = 反向请求 id)由 stdin 循环统一分发:
|
|
16
|
+
// askUser 的最终结果到达后写入 ASKUSER_RESULTS(动作层取出继续播放)。
|
|
17
|
+
|
|
18
|
+
import { spawn } from "node:child_process";
|
|
19
|
+
import { createInterface } from "node:readline";
|
|
20
|
+
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
function argOf(name, fallback) {
|
|
23
|
+
const i = args.indexOf(name);
|
|
24
|
+
return i >= 0 && args[i + 1] !== undefined ? args[i + 1] : (process.env[name] ?? fallback);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const MODE = argOf("--mode", "normal");
|
|
28
|
+
const ENGINE_ID = argOf("--engine-id", "fake");
|
|
29
|
+
const PROTOCOL_VERSION = Number(argOf("--protocol-version", process.env.FAKE_PROTOCOL_VERSION ?? "1"));
|
|
30
|
+
const CANCEL_SETTLES = (argOf("--cancel-settle", process.env.FAKE_CANCEL_SETTLE ?? "0") === "1");
|
|
31
|
+
const RUN_HANGS = (argOf("--run-hang", process.env.FAKE_RUN_HANG ?? "0") === "1");
|
|
32
|
+
const RUN_ACTIONS = JSON.parse(argOf("--run-actions", process.env.FAKE_RUN_ACTIONS ?? "[]"));
|
|
33
|
+
|
|
34
|
+
function send(frame) {
|
|
35
|
+
process.stdout.write(`${JSON.stringify(frame)}\n`);
|
|
36
|
+
}
|
|
37
|
+
function sendError(id, code, message) {
|
|
38
|
+
send({ id, error: { code, message, recovery: "inspect the fake engine scenario" } });
|
|
39
|
+
}
|
|
40
|
+
function reverseRequest(id, method, params) {
|
|
41
|
+
send({ id, method, params });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ── run 状态(cancel 收敛路径用)──
|
|
45
|
+
let activeRun = null; // { id, resolve }
|
|
46
|
+
const askUserResults = new Map(); // reverse id → result(帧②到达)
|
|
47
|
+
|
|
48
|
+
function runOutcomeAborted() {
|
|
49
|
+
return {
|
|
50
|
+
handle: {
|
|
51
|
+
v: 1,
|
|
52
|
+
engineId: ENGINE_ID,
|
|
53
|
+
sessionRef: { sessionId: "fake-aborted" },
|
|
54
|
+
poolKey: "shared",
|
|
55
|
+
adapterVersion: "fake-adapter",
|
|
56
|
+
},
|
|
57
|
+
outcome: { content: "", error: "engine_run_failed: aborted (fake)", exitCode: null, engineId: ENGINE_ID },
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function playRunActions(requestId) {
|
|
62
|
+
for (const action of RUN_ACTIONS) {
|
|
63
|
+
if (activeRun === null) return; // run 已被 cancel 收敛
|
|
64
|
+
switch (action.op) {
|
|
65
|
+
case "emit":
|
|
66
|
+
send({ method: "event", params: { runId: action.runId ?? "run-1", seq: action.seq ?? 1, event: action.event } });
|
|
67
|
+
break;
|
|
68
|
+
case "streamDelta":
|
|
69
|
+
reverseRequest(`rev-stream-${requestId}`, "host/streamDelta", { runId: action.runId ?? "run-1", delta: action.delta ?? "" });
|
|
70
|
+
break;
|
|
71
|
+
case "poolResolved":
|
|
72
|
+
reverseRequest(`rev-pool-${requestId}`, "host/poolResolved", { runId: action.runId ?? "run-1", poolKey: action.poolKey ?? "shared" });
|
|
73
|
+
break;
|
|
74
|
+
case "handleReady":
|
|
75
|
+
reverseRequest(`rev-ready-${requestId}`, "host/handleReady", {
|
|
76
|
+
runId: action.runId ?? "run-1",
|
|
77
|
+
sessionRef: action.sessionRef ?? { sessionId: "fake-session" },
|
|
78
|
+
poolKey: action.poolKey ?? "shared",
|
|
79
|
+
});
|
|
80
|
+
break;
|
|
81
|
+
case "childSpawned":
|
|
82
|
+
reverseRequest(`rev-child-${requestId}`, "host/childSpawned", { pid: action.pid, recordId: action.recordId ?? "rec-1" });
|
|
83
|
+
break;
|
|
84
|
+
case "childStateChanged":
|
|
85
|
+
reverseRequest(`rev-childst-${requestId}`, "host/childStateChanged", {
|
|
86
|
+
pid: action.pid,
|
|
87
|
+
recordId: action.recordId ?? "rec-1",
|
|
88
|
+
state: action.state ?? "exited",
|
|
89
|
+
killed: action.killed ?? false,
|
|
90
|
+
...(action.exitCode !== undefined ? { exitCode: action.exitCode } : {}),
|
|
91
|
+
...(action.signal !== undefined ? { signal: action.signal } : {}),
|
|
92
|
+
});
|
|
93
|
+
break;
|
|
94
|
+
case "log":
|
|
95
|
+
reverseRequest(`rev-log-${requestId}`, "host/log", { level: action.level ?? "debug", component: action.component ?? "fake", message: action.message ?? "" });
|
|
96
|
+
break;
|
|
97
|
+
case "askUser": {
|
|
98
|
+
const revId = `rev-ask-${requestId}-${Math.random().toString(36).slice(2, 7)}`;
|
|
99
|
+
reverseRequest(revId, "host/askUser", { runId: action.runId ?? "run-1", request: action.request ?? { method: "select", id: "q1" } });
|
|
100
|
+
// 等两阶段最终结果(ack 之后 core 异步补帧②)——挂起至结果到达。
|
|
101
|
+
await new Promise((resolve) => {
|
|
102
|
+
const poll = setInterval(() => {
|
|
103
|
+
if (askUserResults.has(revId)) {
|
|
104
|
+
clearInterval(poll);
|
|
105
|
+
if (action.echoEvent !== false) {
|
|
106
|
+
send({
|
|
107
|
+
method: "event",
|
|
108
|
+
params: { runId: action.runId ?? "run-1", seq: action.seq ?? 99, event: { type: "error", message: `askUser-result:${JSON.stringify(askUserResults.get(revId))}` } },
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
resolve();
|
|
112
|
+
}
|
|
113
|
+
}, 20);
|
|
114
|
+
});
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "spawnGrandchild": {
|
|
118
|
+
// 同组后代(不 detached):进程组收割断言对象。node 长眠进程。
|
|
119
|
+
const grandchild = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], {
|
|
120
|
+
detached: false,
|
|
121
|
+
stdio: "ignore",
|
|
122
|
+
});
|
|
123
|
+
grandchild.unref();
|
|
124
|
+
reverseRequest(`rev-grand-${requestId}`, "host/childSpawned", { pid: grandchild.pid, recordId: action.recordId ?? "rec-grand" });
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case "delay":
|
|
128
|
+
await new Promise((resolve) => setTimeout(resolve, action.ms ?? 10));
|
|
129
|
+
break;
|
|
130
|
+
case "exit":
|
|
131
|
+
process.stderr.write(action.stderr ?? "fake engine mid-run crash\n");
|
|
132
|
+
process.exit(action.code ?? 137);
|
|
133
|
+
break; // unreachable
|
|
134
|
+
default:
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function fakeRunResult(requestId) {
|
|
141
|
+
return {
|
|
142
|
+
handle: {
|
|
143
|
+
v: 1,
|
|
144
|
+
engineId: ENGINE_ID,
|
|
145
|
+
sessionRef: { sessionId: `fake-session-${requestId}` },
|
|
146
|
+
poolKey: "shared",
|
|
147
|
+
engineVersion: "fake-1.0.0",
|
|
148
|
+
adapterVersion: "fake-adapter",
|
|
149
|
+
},
|
|
150
|
+
outcome: {
|
|
151
|
+
content: `fake-content-${requestId}`,
|
|
152
|
+
engineId: ENGINE_ID,
|
|
153
|
+
exitCode: 0,
|
|
154
|
+
durationMs: 1,
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const CAPABILITIES = {
|
|
160
|
+
schemaEnforcement: "emulated",
|
|
161
|
+
steer: "unsupported",
|
|
162
|
+
conversation: "unsupported",
|
|
163
|
+
personaInjection: "prompt",
|
|
164
|
+
eventGranularity: "stream",
|
|
165
|
+
sandbox: "emulated",
|
|
166
|
+
sessionRead: "full",
|
|
167
|
+
resume: "cold",
|
|
168
|
+
interrupt: "kill-only",
|
|
169
|
+
permissionMode: "fixed",
|
|
170
|
+
maxTurns: false,
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// initialize 应答 capabilities 局部覆盖(gate 位多声明测试:模拟「引擎实态与
|
|
174
|
+
// manifest 不符」——如实态 sandbox 降为 none)。仅影响应答,不改变 run 行为。
|
|
175
|
+
const CAPS_OVERRIDE = argOf("--caps-override", process.env.FAKE_CAPS_OVERRIDE ?? "");
|
|
176
|
+
const ANSWERED_CAPABILITIES = CAPS_OVERRIDE
|
|
177
|
+
? { ...CAPABILITIES, ...JSON.parse(CAPS_OVERRIDE) }
|
|
178
|
+
: CAPABILITIES;
|
|
179
|
+
|
|
180
|
+
const MODELS = [
|
|
181
|
+
{ id: "glm-4.6", aliases: ["glm"], canonicalRef: "zai/glm-4.6" },
|
|
182
|
+
{ id: "mimo-v2.5-pro", canonicalRef: "xiaomi-token-plan-cn/mimo-v2.5-pro" },
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
if (MODE === "crash") {
|
|
186
|
+
process.stderr.write(`fatal: fake engine boot failure ${"x".repeat(600)}\n`);
|
|
187
|
+
process.exit(1);
|
|
188
|
+
}
|
|
189
|
+
if (MODE === "hang") {
|
|
190
|
+
// 不回任何帧——握手超时 / 假死场景。保持进程活着。
|
|
191
|
+
setInterval(() => {}, 60_000);
|
|
192
|
+
} else {
|
|
193
|
+
// normal:stdin NDJSON 循环
|
|
194
|
+
const rl = createInterface({ input: process.stdin });
|
|
195
|
+
rl.on("line", (line) => {
|
|
196
|
+
let frame;
|
|
197
|
+
try {
|
|
198
|
+
frame = JSON.parse(line);
|
|
199
|
+
} catch {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (frame.method === undefined) {
|
|
203
|
+
// 帧②:反向请求的应答(id 为字符串 = 反向请求 id)
|
|
204
|
+
if (typeof frame.id === "string") askUserResults.set(frame.id, frame.result);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const { id, method, params } = frame;
|
|
208
|
+
switch (method) {
|
|
209
|
+
case "initialize":
|
|
210
|
+
send({
|
|
211
|
+
id,
|
|
212
|
+
result: {
|
|
213
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
214
|
+
engineId: ENGINE_ID,
|
|
215
|
+
engineVersion: "fake-1.0.0",
|
|
216
|
+
adapterVersion: "fake-adapter",
|
|
217
|
+
capabilities: ANSWERED_CAPABILITIES,
|
|
218
|
+
models: MODELS,
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
break;
|
|
222
|
+
case "probe":
|
|
223
|
+
send({ id, result: { ok: true, engineVersion: "fake-1.0.0", checks: [{ name: "boot", ok: true }] } });
|
|
224
|
+
break;
|
|
225
|
+
case "ping":
|
|
226
|
+
send({ id, result: { pong: true } });
|
|
227
|
+
break;
|
|
228
|
+
case "run": {
|
|
229
|
+
if (RUN_HANGS) return; // run 挂起不回终态(cancel 收敛窗口测试)
|
|
230
|
+
const requestId = params?.runId ?? "run-1";
|
|
231
|
+
// run-params 回显:RemoteEngine 帧映射断言的数据源(seq 0)。
|
|
232
|
+
send({
|
|
233
|
+
method: "event",
|
|
234
|
+
params: {
|
|
235
|
+
runId: requestId,
|
|
236
|
+
seq: 0,
|
|
237
|
+
event: { type: "error", message: `run-params:${JSON.stringify({ task: params?.task, ctx: params?.ctx, runId: params?.runId })}` },
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
activeRun = { id, resolve: null };
|
|
241
|
+
void (async () => {
|
|
242
|
+
await playRunActions(requestId);
|
|
243
|
+
if (activeRun !== null && activeRun.id === id) {
|
|
244
|
+
activeRun = null;
|
|
245
|
+
send({ id, result: fakeRunResult(requestId) });
|
|
246
|
+
}
|
|
247
|
+
})();
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
case "cancel":
|
|
251
|
+
send({ id, result: { ok: true } });
|
|
252
|
+
if (CANCEL_SETTLES && activeRun !== null) {
|
|
253
|
+
const runFrameId = activeRun.id;
|
|
254
|
+
activeRun = null;
|
|
255
|
+
send({ id: runFrameId, result: runOutcomeAborted() });
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
case "interact":
|
|
259
|
+
send({ id, result: { ok: true, delivered: true } });
|
|
260
|
+
break;
|
|
261
|
+
case "read":
|
|
262
|
+
send({
|
|
263
|
+
id,
|
|
264
|
+
result: {
|
|
265
|
+
engineId: ENGINE_ID,
|
|
266
|
+
sessionId: params?.handle?.sessionRef?.sessionId ?? "fake-session",
|
|
267
|
+
turns: [{ text: "fake turn", thinking: "", toolCalls: [], closed: true }],
|
|
268
|
+
source: "native",
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
break;
|
|
272
|
+
case "listModels":
|
|
273
|
+
send({ id, result: { models: MODELS } });
|
|
274
|
+
break;
|
|
275
|
+
case "validateModel":
|
|
276
|
+
send({ id, result: { canonicalRef: params?.modelRef ?? "" } });
|
|
277
|
+
break;
|
|
278
|
+
case "dispose":
|
|
279
|
+
send({ id, result: { ok: true } });
|
|
280
|
+
setTimeout(() => process.exit(0), 20);
|
|
281
|
+
break;
|
|
282
|
+
default:
|
|
283
|
+
sendError(id, "engine_unknown_method", `unknown method ${method}`);
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
// stdin EOF:宿主死亡信号。fake 引擎不实现自灭(自灭守卫归引擎包,SDK 层已测),
|
|
288
|
+
// 但正常退出路径(dispose / 宿主杀组)会终止本进程——EOF 后保活由测试管理。
|
|
289
|
+
rl.on("close", () => {
|
|
290
|
+
// 宿主 stdin 关闭:挂住直到被杀(孤儿场景由 pidfile 清扫/组杀处理)。
|
|
291
|
+
setInterval(() => {}, 60_000);
|
|
292
|
+
});
|
|
293
|
+
process.stderr.write("fake engine ready\n");
|
|
294
|
+
}
|