@zhushanwen/subagent-core 0.2.0
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 +114 -0
- package/dist/chunk-3VOERJPJ.js +22 -0
- package/dist/chunk-A4IVZWVX.js +31 -0
- package/dist/execution/engine/engines/zcode/constants.cjs +54 -0
- package/dist/execution/engine/engines/zcode/constants.d.cts +31 -0
- package/dist/execution/engine/engines/zcode/constants.d.ts +31 -0
- package/dist/execution/engine/engines/zcode/constants.js +22 -0
- package/dist/execution/engine/engines/zcode/reader.cjs +276 -0
- package/dist/execution/engine/engines/zcode/reader.d.cts +20 -0
- package/dist/execution/engine/engines/zcode/reader.d.ts +20 -0
- package/dist/execution/engine/engines/zcode/reader.js +239 -0
- package/dist/execution/engine/paths.cjs +55 -0
- package/dist/execution/engine/paths.d.cts +8 -0
- package/dist/execution/engine/paths.d.ts +8 -0
- package/dist/execution/engine/paths.js +26 -0
- package/dist/execution/relay-env.cjs +62 -0
- package/dist/execution/relay-env.d.cts +33 -0
- package/dist/execution/relay-env.d.ts +33 -0
- package/dist/execution/relay-env.js +20 -0
- package/dist/index.cjs +1852 -0
- package/dist/index.d.cts +1184 -0
- package/dist/index.d.ts +1184 -0
- package/dist/index.js +1810 -0
- package/dist/types-BxyAidGf.d.cts +875 -0
- package/dist/types-BxyAidGf.d.ts +875 -0
- package/package.json +99 -0
- package/src/__tests__/agent-opts-resolver-schema-prompt.test.ts +142 -0
- package/src/__tests__/append-system-prompt-assembly.test.ts +242 -0
- package/src/__tests__/builtin-workflows-structure.test.ts +180 -0
- package/src/__tests__/derive-closed-display-parity.test.ts +93 -0
- package/src/__tests__/fr4-get-state-handshake.test.ts +125 -0
- package/src/__tests__/fr8-orphan-recovery.test.ts +259 -0
- package/src/__tests__/m2-append-content-probe.test.ts +73 -0
- package/src/__tests__/manifest-store.test.ts +368 -0
- package/src/__tests__/record-store-cache.test.ts +335 -0
- package/src/__tests__/record-store-index.test.ts +560 -0
- package/src/__tests__/record-store-last-line.test.ts +129 -0
- package/src/__tests__/review-fix-loop-script.test.ts +288 -0
- package/src/__tests__/review-fix-loop-utils.test.ts +2377 -0
- package/src/__tests__/robustness-low-batch1.test.ts +286 -0
- package/src/__tests__/robustness-medium-batch1.test.ts +69 -0
- package/src/__tests__/robustness-medium-batch2.test.ts +65 -0
- package/src/__tests__/robustness-medium-batch3.test.ts +30 -0
- package/src/__tests__/robustness-medium-batch4.test.ts +358 -0
- package/src/__tests__/session-runner.test.ts +69 -0
- package/src/__tests__/smoke.test.ts +9 -0
- package/src/__tests__/stream-sink.test.ts +203 -0
- package/src/core/__tests__/host-services.test.ts +144 -0
- package/src/core/__tests__/logger.test.ts +78 -0
- package/src/core/__tests__/notify-ports.test.ts +104 -0
- package/src/core/host-services.ts +97 -0
- package/src/core/logger.ts +47 -0
- package/src/core/notify-ports.ts +126 -0
- package/src/execution/__tests__/__fixtures__/notifier-golden-snapshots.json +53 -0
- package/src/execution/__tests__/__fixtures__/truncline.snapshot.json +1 -0
- package/src/execution/__tests__/agent-result-mapper.test.ts +150 -0
- package/src/execution/__tests__/alive-store.test.ts +147 -0
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +490 -0
- package/src/execution/__tests__/channel-registry-handshake.test.ts +242 -0
- package/src/execution/__tests__/chat-engine-routing.test.ts +610 -0
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +190 -0
- package/src/execution/__tests__/concurrency-pool.test.ts +250 -0
- package/src/execution/__tests__/config.test.ts +302 -0
- package/src/execution/__tests__/delivery-methods.test.ts +424 -0
- package/src/execution/__tests__/dialog-queue.test.ts +299 -0
- package/src/execution/__tests__/epipe-fallback.test.ts +241 -0
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +243 -0
- package/src/execution/__tests__/execute-nesting.test.ts +323 -0
- package/src/execution/__tests__/execute-options-mapper.test.ts +200 -0
- package/src/execution/__tests__/execution-record.test.ts +1394 -0
- package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +171 -0
- package/src/execution/__tests__/finalize-record.test.ts +367 -0
- package/src/execution/__tests__/finalized-marker.test.ts +104 -0
- package/src/execution/__tests__/format-schema-instruction.test.ts +169 -0
- package/src/execution/__tests__/gc-timer.test.ts +184 -0
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +254 -0
- package/src/execution/__tests__/helpers/mock-extension-api.ts +30 -0
- package/src/execution/__tests__/helpers/spawn-mock.ts +242 -0
- package/src/execution/__tests__/host-mode.test.ts +87 -0
- package/src/execution/__tests__/lifecycle-manager-lock.test.ts +211 -0
- package/src/execution/__tests__/lifecycle-manager.test.ts +383 -0
- package/src/execution/__tests__/lifecycle-predicates.test.ts +116 -0
- package/src/execution/__tests__/manifest-parentid.test.ts +117 -0
- package/src/execution/__tests__/model-resolver.test.ts +465 -0
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +287 -0
- package/src/execution/__tests__/nested-visibility.test.ts +325 -0
- package/src/execution/__tests__/output-collector.test.ts +358 -0
- package/src/execution/__tests__/path-encoding.test.ts +104 -0
- package/src/execution/__tests__/pi-invocation.test.ts +134 -0
- package/src/execution/__tests__/record-store.test.ts +1057 -0
- package/src/execution/__tests__/records-cwd-isolation.test.ts +91 -0
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +339 -0
- package/src/execution/__tests__/recursive-visibility-env.test.ts +273 -0
- package/src/execution/__tests__/relay-env.test.ts +42 -0
- package/src/execution/__tests__/resource-policy.test.ts +109 -0
- package/src/execution/__tests__/rpc-mode.test.ts +89 -0
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +267 -0
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +253 -0
- package/src/execution/__tests__/run-spawn-edges.test.ts +687 -0
- package/src/execution/__tests__/run-spawn-integration.test.ts +933 -0
- package/src/execution/__tests__/run-spawn-resume.test.ts +322 -0
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +196 -0
- package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +199 -0
- package/src/execution/__tests__/session-context-resolver.test.ts +167 -0
- package/src/execution/__tests__/session-file-gc.test.ts +293 -0
- package/src/execution/__tests__/session-reconstructor.test.ts +379 -0
- package/src/execution/__tests__/session-runner-epipe.test.ts +178 -0
- package/src/execution/__tests__/session-runner-schema-env.test.ts +350 -0
- package/src/execution/__tests__/spawn-args.test.ts +445 -0
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +189 -0
- package/src/execution/__tests__/spawn-event-adapter.test.ts +167 -0
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +207 -0
- package/src/execution/__tests__/spawned-children.test.ts +92 -0
- package/src/execution/__tests__/start-sync-model-guard.test.ts +150 -0
- package/src/execution/__tests__/stdin-writer.test.ts +462 -0
- package/src/execution/__tests__/stream-sink-retirement.test.ts +261 -0
- package/src/execution/__tests__/subagent-service-abort.test.ts +60 -0
- package/src/execution/__tests__/subagent-service-message-close.test.ts +629 -0
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +180 -0
- package/src/execution/__tests__/subagent-service.test.ts +788 -0
- package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +310 -0
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +612 -0
- package/src/execution/__tests__/temp-prompt.test.ts +53 -0
- package/src/execution/__tests__/timeout-integration.test.ts +615 -0
- package/src/execution/__tests__/tombstone-store.test.ts +73 -0
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +194 -0
- package/src/execution/__tests__/turn-limiter.test.ts +65 -0
- package/src/execution/__tests__/ui-channels.test.ts +187 -0
- package/src/execution/__tests__/ui-interaction-model.test.ts +67 -0
- package/src/execution/__tests__/ui-request-handler-factory.test.ts +369 -0
- package/src/execution/__tests__/ui-request-handler.test.ts +204 -0
- package/src/execution/__tests__/ui-request-observability.test.ts +113 -0
- package/src/execution/__tests__/ui-request-queue.test.ts +133 -0
- package/src/execution/__tests__/worktree-manager.test.ts +633 -0
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +229 -0
- package/src/execution/__tests__/worktree-reconcile.integration.test.ts +181 -0
- package/src/execution/__tests__/worktree-registry.test.ts +199 -0
- package/src/execution/agent-registry.ts +199 -0
- package/src/execution/agent-result-mapper.ts +90 -0
- package/src/execution/alive-store.ts +119 -0
- package/src/execution/argv-mirror.ts +113 -0
- package/src/execution/best-effort.ts +38 -0
- package/src/execution/channel-registry-access.ts +144 -0
- package/src/execution/concurrency-pool.ts +116 -0
- package/src/execution/config.ts +165 -0
- package/src/execution/dialog-queue.ts +330 -0
- package/src/execution/engine/__tests__/common/data-dir.test.ts +78 -0
- package/src/execution/engine/__tests__/common/errors.test.ts +132 -0
- package/src/execution/engine/__tests__/common/event-journal.test.ts +177 -0
- package/src/execution/engine/__tests__/common/kill-chain.test.ts +192 -0
- package/src/execution/engine/__tests__/common/nesting-guard.test.ts +81 -0
- package/src/execution/engine/__tests__/common/persona-router.test.ts +123 -0
- package/src/execution/engine/__tests__/common/pool-manager.test.ts +154 -0
- package/src/execution/engine/__tests__/common/schema-emulation.test.ts +128 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +28 -0
- package/src/execution/engine/__tests__/conformance/agent-event-invariants.ts +141 -0
- package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +109 -0
- package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +101 -0
- package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +77 -0
- package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +104 -0
- package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +201 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +76 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +79 -0
- package/src/execution/engine/__tests__/engine-discovery.test.ts +87 -0
- package/src/execution/engine/__tests__/model-prompt.test.ts +197 -0
- package/src/execution/engine/__tests__/paths.test.ts +39 -0
- package/src/execution/engine/__tests__/registry.test.ts +120 -0
- package/src/execution/engine/__tests__/routing.test.ts +231 -0
- package/src/execution/engine/common/data-dir.ts +66 -0
- package/src/execution/engine/common/errors.ts +183 -0
- package/src/execution/engine/common/event-journal.ts +254 -0
- package/src/execution/engine/common/journal-replay.ts +62 -0
- package/src/execution/engine/common/kill-chain.ts +221 -0
- package/src/execution/engine/common/nesting-guard.ts +50 -0
- package/src/execution/engine/common/persona-router.ts +108 -0
- package/src/execution/engine/common/pool-manager.ts +226 -0
- package/src/execution/engine/common/schema-emulation.ts +189 -0
- package/src/execution/engine/common/session-view-projection.ts +51 -0
- package/src/execution/engine/engine-discovery.ts +65 -0
- package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +469 -0
- package/src/execution/engine/engines/pi/__tests__/reader.test.ts +155 -0
- package/src/execution/engine/engines/pi/__tests__/task-spec-mapper.test.ts +164 -0
- package/src/execution/engine/engines/pi/pi-engine.ts +415 -0
- package/src/execution/engine/engines/pi/reader.ts +48 -0
- package/src/execution/engine/engines/pi/registration.ts +35 -0
- package/src/execution/engine/engines/pi/task-spec-mapper.ts +100 -0
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-spawn.json +39 -0
- package/src/execution/engine/engines/zcode/__tests__/launcher.test.ts +150 -0
- package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +246 -0
- package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +228 -0
- package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +210 -0
- package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +71 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +127 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +580 -0
- package/src/execution/engine/engines/zcode/constants.ts +43 -0
- package/src/execution/engine/engines/zcode/golden-sample.ts +39 -0
- package/src/execution/engine/engines/zcode/launcher.ts +161 -0
- package/src/execution/engine/engines/zcode/parser.ts +436 -0
- package/src/execution/engine/engines/zcode/preparer.ts +363 -0
- package/src/execution/engine/engines/zcode/reader.ts +381 -0
- package/src/execution/engine/engines/zcode/registration.ts +37 -0
- package/src/execution/engine/engines/zcode/zcode-engine.ts +658 -0
- package/src/execution/engine/host-task-spec.ts +47 -0
- package/src/execution/engine/model-prompt.ts +158 -0
- package/src/execution/engine/paths.ts +42 -0
- package/src/execution/engine/port.ts +153 -0
- package/src/execution/engine/registry.ts +133 -0
- package/src/execution/engine/routing.ts +218 -0
- package/src/execution/engine/types.ts +309 -0
- package/src/execution/execute-options-mapper.ts +115 -0
- package/src/execution/execution-record.ts +984 -0
- package/src/execution/finalize-record.ts +254 -0
- package/src/execution/finalized-marker.ts +70 -0
- package/src/execution/get-state-handshake.ts +104 -0
- package/src/execution/host-mode.ts +56 -0
- package/src/execution/idle-gc.ts +47 -0
- package/src/execution/lifecycle-manager.ts +513 -0
- package/src/execution/lifecycle-predicates.ts +65 -0
- package/src/execution/manifest-store.ts +256 -0
- package/src/execution/model-config-service.ts +250 -0
- package/src/execution/model-resolver.ts +248 -0
- package/src/execution/notifier.ts +372 -0
- package/src/execution/notify-ledger.ts +580 -0
- package/src/execution/output-collector.ts +228 -0
- package/src/execution/path-encoding.ts +60 -0
- package/src/execution/pi-invocation.ts +120 -0
- package/src/execution/record-entry.ts +132 -0
- package/src/execution/record-store.ts +1265 -0
- package/src/execution/relay-env.ts +37 -0
- package/src/execution/session-context-resolver.ts +64 -0
- package/src/execution/session-file-gc.ts +120 -0
- package/src/execution/session-pending.ts +170 -0
- package/src/execution/session-reconstructor.ts +678 -0
- package/src/execution/session-runner.ts +1789 -0
- package/src/execution/sessions-index.ts +304 -0
- package/src/execution/spawn-event-adapter.ts +363 -0
- package/src/execution/stdin-writer.ts +198 -0
- package/src/execution/stream-sink.ts +126 -0
- package/src/execution/subagent-service.ts +2268 -0
- package/src/execution/subprocess-agent-runner.ts +317 -0
- package/src/execution/temp-prompt.ts +62 -0
- package/src/execution/tombstone-store.ts +72 -0
- package/src/execution/turn-limiter.ts +102 -0
- package/src/execution/types.ts +1023 -0
- package/src/execution/ui-channels.ts +216 -0
- package/src/execution/ui-interaction-model.ts +48 -0
- package/src/execution/ui-request-handler-factory.ts +243 -0
- package/src/execution/ui-request-observability.ts +81 -0
- package/src/execution/ui-request-queue.ts +184 -0
- package/src/execution/worktree-manager.ts +688 -0
- package/src/execution/worktree-registry.ts +279 -0
- package/src/index.ts +87 -0
- package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +349 -0
- package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +202 -0
- package/src/orchestration/__tests__/agent-call-stream.test.ts +152 -0
- package/src/orchestration/__tests__/args-validator.test.ts +143 -0
- package/src/orchestration/__tests__/config-loader.test.ts +503 -0
- package/src/orchestration/__tests__/error-recovery-handlers.test.ts +809 -0
- package/src/orchestration/__tests__/error-recovery-postmessage-defense.test.ts +404 -0
- package/src/orchestration/__tests__/error-recovery-serialize-failed-result.test.ts +56 -0
- package/src/orchestration/__tests__/error-recovery-workflow-call.test.ts +166 -0
- package/src/orchestration/__tests__/execute-agent-call.test.ts +451 -0
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +600 -0
- package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +96 -0
- package/src/orchestration/__tests__/lifecycle.test.ts +659 -0
- package/src/orchestration/__tests__/non-cloneable-return-e2e.test.ts +95 -0
- package/src/orchestration/__tests__/review-fix-loop-scriptpath-failfast.test.ts +183 -0
- package/src/orchestration/__tests__/script-lint.test.ts +831 -0
- package/src/orchestration/__tests__/skill-discovery.test.ts +210 -0
- package/src/orchestration/__tests__/test-mocks.ts +197 -0
- package/src/orchestration/__tests__/worker-exit-without-result.test.ts +368 -0
- package/src/orchestration/__tests__/worker-host.test.ts +120 -0
- package/src/orchestration/__tests__/worker-returnmeta-passthrough.test.ts +164 -0
- package/src/orchestration/__tests__/worker-script-builder-runtime.test.ts +563 -0
- package/src/orchestration/__tests__/worker-script-builder.test.ts +309 -0
- package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +129 -0
- package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +317 -0
- package/src/orchestration/__tests__/workflow-script-lint-memo.test.ts +110 -0
- package/src/orchestration/agent-opts-resolver.ts +174 -0
- package/src/orchestration/args-validator.ts +127 -0
- package/src/orchestration/config-loader.ts +316 -0
- package/src/orchestration/error-recovery.ts +1018 -0
- package/src/orchestration/execute-agent-call.ts +256 -0
- package/src/orchestration/launcher.ts +455 -0
- package/src/orchestration/lifecycle.ts +399 -0
- package/src/orchestration/models/__tests__/budget.test.ts +307 -0
- package/src/orchestration/models/agent-call.ts +83 -0
- package/src/orchestration/models/budget.ts +118 -0
- package/src/orchestration/models/ports.ts +164 -0
- package/src/orchestration/models/run-runtime.ts +104 -0
- package/src/orchestration/models/run-spec.ts +83 -0
- package/src/orchestration/models/run-state.ts +44 -0
- package/src/orchestration/models/trace.ts +183 -0
- package/src/orchestration/models/types.ts +301 -0
- package/src/orchestration/models/workflow-run.ts +254 -0
- package/src/orchestration/models/workflow-script-registry.ts +35 -0
- package/src/orchestration/models/workflow-script.ts +117 -0
- package/src/orchestration/script-lint.ts +766 -0
- package/src/orchestration/skill-discovery.ts +135 -0
- package/src/orchestration/worker-handle.ts +115 -0
- package/src/orchestration/worker-host.ts +98 -0
- package/src/orchestration/worker-script-builder.ts +421 -0
- package/src/orchestration/workflow-files.ts +85 -0
- package/src/orchestration/workflow-script-registry-impl.ts +133 -0
- package/src/shared/__tests__/agent-ref.test.ts +34 -0
- package/src/shared/__tests__/meta-parser.test.ts +304 -0
- package/src/shared/__tests__/model-ref.test.ts +306 -0
- package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +288 -0
- package/src/shared/__tests__/resource-discovery.test.ts +749 -0
- package/src/shared/__tests__/resource-meta.test.ts +51 -0
- package/src/shared/__tests__/schema-jsonify.test.ts +81 -0
- package/src/shared/__tests__/timer-delay.test.ts +61 -0
- package/src/shared/agent-event.ts +13 -0
- package/src/shared/agent-ref.ts +57 -0
- package/src/shared/meta-parser.ts +261 -0
- package/src/shared/model-ref.ts +286 -0
- package/src/shared/resource-discovery.ts +835 -0
- package/src/shared/resource-meta.ts +65 -0
- package/src/shared/schema-env.ts +44 -0
- package/src/shared/schema-jsonify.ts +58 -0
- package/src/shared/timer-delay.ts +54 -0
- package/src/shared/xml-injection.ts +35 -0
- package/workflows/README.md +81 -0
- package/workflows/_shared/agent-refs.cjs +40 -0
- package/workflows/chain.js +137 -0
- package/workflows/map-reduce.js +180 -0
- package/workflows/parallel.js +154 -0
- package/workflows/review-fix-loop-utils.cjs +1542 -0
- package/workflows/review-fix-loop.js +1605 -0
- package/workflows/scatter-gather.js +184 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* buildWorkerScript — workflow() 全局函数注入测试。
|
|
3
|
+
*
|
|
4
|
+
* 验证生成的 worker 源码字符串包含 workflow 嵌套调用所需的全部契约:
|
|
5
|
+
* - workflow() 全局函数声明
|
|
6
|
+
* - workflow-call 消息(Worker → Main)
|
|
7
|
+
* - workflow-result 消息处理(Main → Worker)
|
|
8
|
+
* - execute() context 包含 workflow
|
|
9
|
+
* - name 参数校验
|
|
10
|
+
*/
|
|
11
|
+
import { describe, expect, it } from "vitest";
|
|
12
|
+
|
|
13
|
+
import { buildWorkerScript } from "../worker-script-builder.ts";
|
|
14
|
+
|
|
15
|
+
describe("buildWorkerScript — workflow() global injection", () => {
|
|
16
|
+
const script = buildWorkerScript("// noop user script");
|
|
17
|
+
|
|
18
|
+
it("injects workflow() global function", () => {
|
|
19
|
+
expect(script).toContain("async function workflow");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('workflow() sends workflow-call message', () => {
|
|
23
|
+
expect(script).toContain('type: "workflow-call"');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('handles workflow-result message', () => {
|
|
27
|
+
// worker 是 workflow-result 的接收方,用条件分支处理(非对象字面量)
|
|
28
|
+
expect(script).toContain('msg.type === "workflow-result"');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("execute() context includes workflow", () => {
|
|
32
|
+
expect(script).toContain(
|
|
33
|
+
"module.exports.execute({ agent, parallel, pipeline, phase, log, workflow, $ARGS, $WORKSPACE, $BUDGET })",
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("workflow() validates name argument", () => {
|
|
38
|
+
expect(script).toContain(
|
|
39
|
+
"workflow() requires a workflow name string as first argument",
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// ── H3: agent() task/agent 分支 skill 字段传递 ──
|
|
45
|
+
|
|
46
|
+
describe("buildWorkerScript — agent() skill field in task/agent branch", () => {
|
|
47
|
+
const script = buildWorkerScript("// noop user script");
|
|
48
|
+
|
|
49
|
+
it("task/agent branch includes skill in opts whitelist", () => {
|
|
50
|
+
// H3: agent({task, agent, skill}) 的 skill 在 task/agent 分支被丢弃。
|
|
51
|
+
// 验证生成的 worker 源码中,task/agent 分支的 opts 构造含 skill 字段。
|
|
52
|
+
// 找到 task/agent 分支的 opts 构造代码(含 firstArg.task || firstArg.agent)
|
|
53
|
+
const taskAgentBranch = script.match(/firstArg\.task \|\| firstArg\.agent[\s\S]*?\};/);
|
|
54
|
+
expect(taskAgentBranch).toBeTruthy();
|
|
55
|
+
expect(taskAgentBranch![0]).toContain("skill: firstArg.skill");
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// ── W1: postMessage 防御 + parallel() 降级类型安全 ──
|
|
60
|
+
|
|
61
|
+
describe("buildWorkerScript — W1 postMessage defense & parallel degrade", () => {
|
|
62
|
+
const script = buildWorkerScript("// noop user script");
|
|
63
|
+
|
|
64
|
+
describe("_safePost wrapper", () => {
|
|
65
|
+
it("injects _safePost function", () => {
|
|
66
|
+
expect(script).toContain("function _safePost(msg, context)");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("_safePost wraps postMessage in try/catch", () => {
|
|
70
|
+
// _safePost 在 module scope(parentPort 解析为 _parentPort),
|
|
71
|
+
// 用宽松正则匹配「try { <something>.postMessage(msg)」避免绑死变量名。
|
|
72
|
+
expect(script).toMatch(/_safePost[\s\S]*?try \{ _parentPort\.postMessage\(msg\)/);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("_safePost logs failure with context to workerLogs", () => {
|
|
76
|
+
expect(script).toContain('_pushWorkerLog("error"');
|
|
77
|
+
expect(script).toContain('"[postMessage failed:" + context + "]"');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("agent() uses _safePost", () => {
|
|
82
|
+
it("agent-call postMessage guarded by _safePost", () => {
|
|
83
|
+
expect(script).toContain("_safePost({ type: \"agent-call\"");
|
|
84
|
+
expect(script).toContain('"agent-call"');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("agent() throws on postMessage failure", () => {
|
|
88
|
+
expect(script).toContain("postMessage failed for agent-call");
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("workflow() uses _safePost", () => {
|
|
93
|
+
it("workflow-call postMessage guarded by _safePost", () => {
|
|
94
|
+
expect(script).toContain("_safePost({ type: \"workflow-call\"");
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("return/error use _safePost", () => {
|
|
99
|
+
it("return postMessage uses _safePost", () => {
|
|
100
|
+
expect(script).toContain('_safePost({ type: "return"');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("error postMessage uses _safePost", () => {
|
|
104
|
+
expect(script).toContain('_safePost({ type: "error"');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("parallel() degrade returns object", () => {
|
|
109
|
+
it("rejected results become {status:failed,error} objects", () => {
|
|
110
|
+
expect(script).toContain('status: "failed"');
|
|
111
|
+
expect(script).toMatch(/parallel[\s\S]*?status: "failed"/);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("non-object fulfilled values wrapped as failed", () => {
|
|
115
|
+
expect(script).toContain("agent returned non-object result");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("object fulfilled values pass through unchanged", () => {
|
|
119
|
+
expect(script).toContain("!Array.isArray(v)");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("pipeline() error observability", () => {
|
|
124
|
+
it("single-arg mode logs stage errors before re-throwing", () => {
|
|
125
|
+
expect(script).toContain("[pipeline stage ");
|
|
126
|
+
expect(script).toContain("_pushWorkerLog(\"error\"");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("cartesian mode logs stage errors instead of silent swallow", () => {
|
|
130
|
+
expect(script).toContain("[pipeline cartesian stage failed");
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// ── W2: agent() returnMeta 模式 ──
|
|
136
|
+
// 验证 returnMeta 标志:known fields 放行 + handler resolve 分支 + 缓存重放分支,
|
|
137
|
+
// 且两处分支结构对称(returnMeta===true → {value, sessionFile, worktreePath, error})。
|
|
138
|
+
|
|
139
|
+
describe("buildWorkerScript — W2 agent() returnMeta mode", () => {
|
|
140
|
+
const script = buildWorkerScript("// noop user script");
|
|
141
|
+
|
|
142
|
+
describe("returnMeta known field whitelist", () => {
|
|
143
|
+
it("includes returnMeta in _knownFields Set", () => {
|
|
144
|
+
expect(script).toContain('"returnMeta"');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("includes returnMeta in unknown-fields warning text", () => {
|
|
148
|
+
expect(script).toContain("cwd, fork, worktree, returnMeta");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("does NOT warn about returnMeta when only returnMeta is passed (parity with fork/worktree)", () => {
|
|
152
|
+
// fork/worktree 已是 known fields;returnMeta 加入后不应触发 warn 文案中的 returnMeta。
|
|
153
|
+
// 简化断言:warning 文案里 Known 列表含 returnMeta(即被识别)。
|
|
154
|
+
expect(script).toMatch(/Known fields:.*returnMeta/);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("_pendingCalls stores returnMeta flag", () => {
|
|
159
|
+
it("stores returnMeta: opts.returnMeta === true in _pendingCalls.set", () => {
|
|
160
|
+
expect(script).toContain(
|
|
161
|
+
"returnMeta: opts.returnMeta === true",
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe("agent-result handler resolve branch (改动 9b)", () => {
|
|
167
|
+
it("branches on pending.returnMeta", () => {
|
|
168
|
+
expect(script).toContain("if (pending.returnMeta)");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("resolve branch returns {value, sessionFile, worktreePath, error}", () => {
|
|
172
|
+
const handlerBlock = script.match(
|
|
173
|
+
/if \(pending\.returnMeta\) \{[\s\S]*?\} else \{[\s\S]*?pending\.resolve\(_value\);[\s\S]*?\}/,
|
|
174
|
+
);
|
|
175
|
+
expect(handlerBlock).toBeTruthy();
|
|
176
|
+
expect(handlerBlock![0]).toContain("value: _value");
|
|
177
|
+
expect(handlerBlock![0]).toContain("sessionFile: msg.result.sessionFile");
|
|
178
|
+
expect(handlerBlock![0]).toContain("worktreePath: msg.result.worktreePath");
|
|
179
|
+
expect(handlerBlock![0]).toContain("error: msg.result.error");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("fallback resolve single value uses msg.result.parsedOutput ?? msg.result.content", () => {
|
|
183
|
+
expect(script).toContain(
|
|
184
|
+
"const _value = msg.result.parsedOutput ?? msg.result.content;",
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe("cache replay returnMeta branch (改动 9c)", () => {
|
|
190
|
+
it("early-returns undefined when !cached", () => {
|
|
191
|
+
expect(script).toContain("if (!cached) return undefined;");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("branches on opts.returnMeta === true", () => {
|
|
195
|
+
expect(script).toContain("if (opts.returnMeta === true)");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("cache replay returns {value, sessionFile, worktreePath, error} from cached", () => {
|
|
199
|
+
const cacheBlock = script.match(
|
|
200
|
+
/if \(opts\.returnMeta === true\) \{[\s\S]*?return _cachedValue;/,
|
|
201
|
+
);
|
|
202
|
+
expect(cacheBlock).toBeTruthy();
|
|
203
|
+
expect(cacheBlock![0]).toContain("value: _cachedValue");
|
|
204
|
+
expect(cacheBlock![0]).toContain("sessionFile: cached.sessionFile");
|
|
205
|
+
expect(cacheBlock![0]).toContain("worktreePath: cached.worktreePath");
|
|
206
|
+
expect(cacheBlock![0]).toContain("error: cached.error");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("cache replay fallback uses cached.parsedOutput ?? cached.content", () => {
|
|
210
|
+
expect(script).toContain(
|
|
211
|
+
"const _cachedValue = cached.parsedOutput ?? cached.content;",
|
|
212
|
+
);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe("handler (9b) and cache replay (9c) branch symmetry", () => {
|
|
217
|
+
it("both expose worktreePath field (CL-1/DEC-2 core)", () => {
|
|
218
|
+
// handler 读 msg.result.worktreePath;cache replay 读 cached.worktreePath。
|
|
219
|
+
expect(script).toContain("msg.result.worktreePath");
|
|
220
|
+
expect(script).toContain("cached.worktreePath");
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// ── thinkingLevel 参数透传(agent() 三分支) ──
|
|
226
|
+
// 验证 thinkingLevel 在 string / task-agent / object.prompt 三个分支均被透传至
|
|
227
|
+
// agent-call 的 opts,且加入 _knownFields 白名单(object.prompt 分支整体透传,
|
|
228
|
+
// 不被 unknown-fields warn 误报)。底层 AgentCallOpts→mapToExecuteOptions→
|
|
229
|
+
// buildSpawnArgs 拼 pi CLI --model provider/modelId:thinkingLevel 已打通,
|
|
230
|
+
// 此处只验入口层 wiring。
|
|
231
|
+
|
|
232
|
+
describe("buildWorkerScript — agent() thinkingLevel passthrough (3 branches)", () => {
|
|
233
|
+
const script = buildWorkerScript("// noop user script");
|
|
234
|
+
|
|
235
|
+
it("string branch extracts thinkingLevel from secondArg (parity with model/scene/phase)", () => {
|
|
236
|
+
// agent("prompt", {thinkingLevel:"high"}) → string 分支 opts 提取 thinkingLevel
|
|
237
|
+
const stringBranch = script.match(/typeof firstArg === "string"[\s\S]*?\};/);
|
|
238
|
+
expect(stringBranch).toBeTruthy();
|
|
239
|
+
expect(stringBranch![0]).toContain(
|
|
240
|
+
"thinkingLevel: (secondArg && typeof secondArg === \"object\" && secondArg.thinkingLevel) || $THINKING_LEVEL",
|
|
241
|
+
);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("task/agent branch includes thinkingLevel in opts (parity with model/skill/timeoutMs)", () => {
|
|
245
|
+
// agent({task, agent, thinkingLevel}) → task/agent 快捷分支透传 thinkingLevel
|
|
246
|
+
const taskAgentBranch = script.match(/firstArg\.task \|\| firstArg\.agent[\s\S]*?\};/);
|
|
247
|
+
expect(taskAgentBranch).toBeTruthy();
|
|
248
|
+
expect(taskAgentBranch![0]).toContain("thinkingLevel: firstArg.thinkingLevel");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("thinkingLevel is a known field (object.prompt branch passes through without unknown-fields warn)", () => {
|
|
252
|
+
// object.prompt 分支 opts = firstArg 整体透传,thinkingLevel 自然带入;
|
|
253
|
+
// 但必须进 _knownFields 白名单,否则 Object.keys(opts) 含 thinkingLevel 会触发
|
|
254
|
+
// unknown-fields warn(workerLogs 污染)。验证 Set 与 warn 文案均识别 thinkingLevel。
|
|
255
|
+
expect(script).toContain('"thinkingLevel"');
|
|
256
|
+
expect(script).toMatch(/Known fields:.*thinkingLevel/);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// ── P3/P4: run-level model/thinkingLevel override global injection ──
|
|
261
|
+
// Option B 对称单路径:workflow 顶层 model/thinkingLevel 经 workerData →
|
|
262
|
+
// $MODEL/$THINKING_LEVEL worker global → agent() 三分支 fallback。
|
|
263
|
+
// 验证生成的 worker 源码字符串包含 global 注入与三分支 fallback 逻辑。
|
|
264
|
+
// 运行时真生效需 worker 集成 harness(设计文档 §6 降级为源码字符串断言)。
|
|
265
|
+
|
|
266
|
+
describe("P3/P4 run-level model/thinkingLevel global injection + agent() fallback", () => {
|
|
267
|
+
const script = buildWorkerScript("// noop user script");
|
|
268
|
+
|
|
269
|
+
it("injects $MODEL global from workerData.model", () => {
|
|
270
|
+
expect(script).toContain('const $MODEL = (workerData.model && typeof workerData.model === "string") ? workerData.model : undefined;');
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("injects $THINKING_LEVEL global from workerData.thinkingLevel", () => {
|
|
274
|
+
expect(script).toContain('const $THINKING_LEVEL = (workerData.thinkingLevel && typeof workerData.thinkingLevel === "string") ? workerData.thinkingLevel : undefined;');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("string branch falls back to $MODEL when secondArg.model omitted", () => {
|
|
278
|
+
const stringBranch = script.match(/typeof firstArg === "string"[\s\S]*?\};/);
|
|
279
|
+
expect(stringBranch).toBeTruthy();
|
|
280
|
+
expect(stringBranch![0]).toContain('secondArg.model) || $MODEL');
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("string branch falls back to $THINKING_LEVEL when secondArg.thinkingLevel omitted", () => {
|
|
284
|
+
const stringBranch = script.match(/typeof firstArg === "string"[\s\S]*?\};/);
|
|
285
|
+
expect(stringBranch).toBeTruthy();
|
|
286
|
+
expect(stringBranch![0]).toContain('secondArg.thinkingLevel) || $THINKING_LEVEL');
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("task/agent branch falls back to $MODEL when firstArg.model omitted", () => {
|
|
290
|
+
const taskAgentBranch = script.match(/firstArg\.task \|\| firstArg\.agent[\s\S]*?\};/);
|
|
291
|
+
expect(taskAgentBranch).toBeTruthy();
|
|
292
|
+
expect(taskAgentBranch![0]).toContain('model: firstArg.model || $MODEL');
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("task/agent branch falls back to $THINKING_LEVEL when firstArg.thinkingLevel omitted", () => {
|
|
296
|
+
const taskAgentBranch = script.match(/firstArg\.task \|\| firstArg\.agent[\s\S]*?\};/);
|
|
297
|
+
expect(taskAgentBranch).toBeTruthy();
|
|
298
|
+
expect(taskAgentBranch![0]).toContain('thinkingLevel: firstArg.thinkingLevel || $THINKING_LEVEL');
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("object branch injects $MODEL when opts.model omitted (guard fallback)", () => {
|
|
302
|
+
expect(script).toContain('if (!opts.model && $MODEL) opts.model = $MODEL;');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("object branch injects $THINKING_LEVEL when opts.thinkingLevel omitted (guard fallback)", () => {
|
|
306
|
+
expect(script).toContain('if (!opts.thinkingLevel && $THINKING_LEVEL) opts.thinkingLevel = $THINKING_LEVEL;');
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* buildWorkerScript 模板 hoisting(IF5/IF6)— byte-identical 快照锚定。
|
|
3
|
+
*
|
|
4
|
+
* ES8:快照先行——fixture 由「改造前实现」生成落盘(本文件同 commit),
|
|
5
|
+
* 断言重构后输出与 fixture 逐字节一致,锁定 PRE/POST 边界换行与 userScript
|
|
6
|
+
* 缩进语义(AC-4「逐字保留」不变式)。IF6(_KNOWN_FIELDS 提升至生成源
|
|
7
|
+
* module scope)落地时基线已按设计在同一 commit 内更新为最终形态。
|
|
8
|
+
*
|
|
9
|
+
* 样例脚本覆盖 $ARGS / schema / parallel / pipeline / workflow / phase / log 特性。
|
|
10
|
+
*/
|
|
11
|
+
import { readFileSync } from "node:fs";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { dirname, join } from "node:path";
|
|
14
|
+
|
|
15
|
+
import { describe, expect, it } from "vitest";
|
|
16
|
+
|
|
17
|
+
import { buildWorkerScript } from "../worker-script-builder.ts";
|
|
18
|
+
|
|
19
|
+
const sampleScript = [
|
|
20
|
+
"// sample exercising template features",
|
|
21
|
+
'const name = $ARGS.name ?? "default";',
|
|
22
|
+
'phase("build");',
|
|
23
|
+
'const spec = await agent("build it", { label: "builder", schema: { type: "object" } });',
|
|
24
|
+
"const results = await parallel([",
|
|
25
|
+
' agent({ task: "unit tests", agent: "./tester.md" }),',
|
|
26
|
+
' agent({ task: "lint", agent: "./linter.md", skill: "lint-skill" }),',
|
|
27
|
+
"]);",
|
|
28
|
+
"await pipeline([(x) => x, (x) => x]);",
|
|
29
|
+
'await workflow("deploy", { env: "prod" });',
|
|
30
|
+
'log("done " + name + " " + $WORKSPACE + " " + $BUDGET.remaining());',
|
|
31
|
+
"module.exports = { execute: async (ctx) => ctx.agent(\"finalize\") };",
|
|
32
|
+
].join("\n");
|
|
33
|
+
|
|
34
|
+
function loadFixture(): string {
|
|
35
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
36
|
+
return readFileSync(join(here, "__fixtures__", "worker-template.snapshot.txt"), "utf8");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("buildWorkerScript — 模板 hoisting byte-identical 快照(IF5)", () => {
|
|
40
|
+
it("特性全量样例的输出与 fixture 逐字节一致", () => {
|
|
41
|
+
const out = buildWorkerScript(sampleScript);
|
|
42
|
+
expect(out).toBe(loadFixture());
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("首尾边界锚定:use strict 开头 + error handler 结尾 + userScript 缩进注入", () => {
|
|
46
|
+
const out = buildWorkerScript(sampleScript);
|
|
47
|
+
// PRE 边界:第一行 use strict(无前导换行)
|
|
48
|
+
expect(out.startsWith('"use strict";\n')).toBe(true);
|
|
49
|
+
// userScript 段:两空格缩进注入(AC-4 拼接语义)
|
|
50
|
+
expect(out).toContain("\n // sample exercising template features\n");
|
|
51
|
+
// userScript 段结束后一个空行再接 auto-invoke 段(POST 首部换行语义)
|
|
52
|
+
expect(out).toContain("ctx.agent(\"finalize\") };\n\n // ── Auto-invoke execute() for module.exports pattern ──");
|
|
53
|
+
// POST 边界:以 error handler 的 "});" 结尾(无尾随换行)
|
|
54
|
+
expect(out.endsWith('_safePost({ type: "error", runId, error: err.message || String(err), workerLogs: _workerLogs }, "error");\n});')).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("空 userScript 与任意脚本的拼接语义稳定(PRE/POST 不含 userScript 状态)", () => {
|
|
58
|
+
const a = buildWorkerScript("x");
|
|
59
|
+
const b = buildWorkerScript("y");
|
|
60
|
+
// 除 userScript 段外完全一致:以 userScript 行为界切割比对
|
|
61
|
+
const [preA] = a.split("\n x\n");
|
|
62
|
+
const [preB] = b.split("\n y\n");
|
|
63
|
+
expect(preA).toBe(preB);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("A8 rfl 仪表透传字段字面量锚定(tier-1 §7.1):live resolve 与缓存重放两对称点均含三字段", () => {
|
|
67
|
+
const out = buildWorkerScript("// noop");
|
|
68
|
+
// live resolve 分支(agent-result 消息处理)
|
|
69
|
+
expect(out).toContain("usage: msg.result.usage,");
|
|
70
|
+
expect(out).toContain("durationMs: msg.result.durationMs,");
|
|
71
|
+
expect(out).toContain("sessionId: msg.result.sessionId,");
|
|
72
|
+
// 缓存重放分支(_callCache 命中重建,对称点)
|
|
73
|
+
expect(out).toContain("usage: cached.usage,");
|
|
74
|
+
expect(out).toContain("durationMs: cached.durationMs,");
|
|
75
|
+
expect(out).toContain("sessionId: cached.sessionId,");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("buildWorkerScript — _KNOWN_FIELDS module scope 提升(IF6)", () => {
|
|
80
|
+
const script = buildWorkerScript("// noop user script");
|
|
81
|
+
|
|
82
|
+
it("生成源含 module 级 _KNOWN_FIELDS 声明(与 _workerLogs 同级,IIFE 外)", () => {
|
|
83
|
+
// 声明在 IIFE 开始之前(module scope):const _KNOWN_FIELDS 必须出现在 "(async () => {" 前
|
|
84
|
+
const iifePos = script.indexOf("(async () => {");
|
|
85
|
+
const knownPos = script.indexOf("const _KNOWN_FIELDS = new Set([");
|
|
86
|
+
expect(knownPos).toBeGreaterThan(-1);
|
|
87
|
+
expect(knownPos).toBeLessThan(iifePos);
|
|
88
|
+
// 紧随 _workerLogs 声明之后(design IF6:与 _workerLogs/:62 同级)
|
|
89
|
+
expect(script.indexOf("const _workerLogs = [];")).toBeLessThan(knownPos);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("agent() 体内不再重建 Set(引用 _KNOWN_FIELDS,无 new Set)", () => {
|
|
93
|
+
// agent() 函数体(到 parallel 声明前)内不得有 new Set 构造
|
|
94
|
+
const agentPos = script.indexOf("async function agent(firstArg, secondArg)");
|
|
95
|
+
const parallelPos = script.indexOf("async function parallel(calls)");
|
|
96
|
+
expect(agentPos).toBeGreaterThan(-1);
|
|
97
|
+
expect(parallelPos).toBeGreaterThan(agentPos);
|
|
98
|
+
const agentBody = script.slice(agentPos, parallelPos);
|
|
99
|
+
expect(agentBody).not.toContain("new Set(");
|
|
100
|
+
expect(agentBody).toContain("_KNOWN_FIELDS.has(k)");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("字段集合内容逐字段一致(18 known fields 不丢失——P4 增 engine,预算语义对齐增 maxTurns)", () => {
|
|
104
|
+
expect(script).toContain(
|
|
105
|
+
'const _KNOWN_FIELDS = new Set(["prompt", "description", "schema", "model", "scene", "label", "task", "agent", "phase", "skill", "timeoutMs", "maxTurns", "cwd", "fork", "worktree", "returnMeta", "thinkingLevel", "engine"]);',
|
|
106
|
+
);
|
|
107
|
+
// unknown-fields 警告文案不变(known 列表仍全量)
|
|
108
|
+
expect(script).toMatch(/Known fields:.*returnMeta.*thinkingLevel.*engine/);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("agent() 各分支均透传 maxTurns(预算语义对齐:脚本作者可显式传 turn 上限)", () => {
|
|
112
|
+
// task/agent shortcut 分支:显式转发
|
|
113
|
+
expect(script).toContain("maxTurns: firstArg.maxTurns,");
|
|
114
|
+
// string+secondArg 分支:显式转发(?? 语义保真:显式 0 不被抹成 undefined,F-2/U5)
|
|
115
|
+
expect(script).toContain(
|
|
116
|
+
'maxTurns: (secondArg && typeof secondArg === "object" ? secondArg.maxTurns : undefined) ?? undefined,',
|
|
117
|
+
);
|
|
118
|
+
// opts = firstArg 直传分支无需处理(整对象透传)
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// [F-2 maxTurns:0 吞没修复] 旧字符串分支 `&& maxTurns) || undefined` 把显式 0
|
|
122
|
+
// 抹成 undefined → runSpawn 落 env 兑底(SPAWN_WATCHDOG env 设置时误挂 watchdog),
|
|
123
|
+
// 与对象分支保真语义分裂。锁定生成源为 ?? 形态(0/null/undefined 区分处理)。
|
|
124
|
+
it("string 分支 maxTurns 用 ?? 语义(显式 0 不抹成 undefined,F-2)", () => {
|
|
125
|
+
const out = buildWorkerScript("// noop");
|
|
126
|
+
expect(out).not.toContain('secondArg.maxTurns) || undefined');
|
|
127
|
+
expect(out).toContain("?? undefined");
|
|
128
|
+
});
|
|
129
|
+
});
|