@zhushanwen/subagent-core 0.2.0 → 0.4.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.
Files changed (195) hide show
  1. package/README.md +45 -2
  2. package/agents/analyst.md +60 -0
  3. package/agents/coder.md +69 -0
  4. package/agents/debugger.md +66 -0
  5. package/agents/doc-reviewer.md +49 -0
  6. package/agents/explorer.md +63 -0
  7. package/agents/general-purpose.md +32 -0
  8. package/agents/orchestrator.md +61 -0
  9. package/agents/planner.md +53 -0
  10. package/agents/researcher.md +67 -0
  11. package/agents/reviewer.md +73 -0
  12. package/dist/chunk-4KN4TTG7.js +240 -0
  13. package/dist/chunk-APZY4IME.js +27 -0
  14. package/dist/chunk-X7SZ5HLQ.js +66 -0
  15. package/dist/execution/engine/engines/zcode/constants.cjs +65 -0
  16. package/dist/execution/engine/engines/zcode/constants.d.cts +93 -1
  17. package/dist/execution/engine/engines/zcode/constants.d.ts +93 -1
  18. package/dist/execution/engine/engines/zcode/constants.js +43 -1
  19. package/dist/execution/engine/engines/zcode/reader.d.cts +1 -1
  20. package/dist/execution/engine/engines/zcode/reader.d.ts +1 -1
  21. package/dist/execution/engine/engines/zcode/reader.js +4 -234
  22. package/dist/execution/engine/paths.js +7 -19
  23. package/dist/index.cjs +14709 -100
  24. package/dist/index.d.cts +4053 -200
  25. package/dist/index.d.ts +4053 -200
  26. package/dist/index.js +14358 -102
  27. package/dist/{types-BxyAidGf.d.cts → types-DpUO16pj.d.cts} +464 -1
  28. package/dist/{types-BxyAidGf.d.ts → types-DpUO16pj.d.ts} +464 -1
  29. package/package.json +6 -3
  30. package/src/__tests__/review-fix-loop-script.test.ts +90 -2
  31. package/src/__tests__/review-fix-loop-utils.test.ts +21 -0
  32. package/src/__tests__/smoke.test.ts +9 -2
  33. package/src/execution/__tests__/agent-profile.test.ts +232 -0
  34. package/src/execution/__tests__/agents-assembly.test.ts +219 -0
  35. package/src/execution/__tests__/chat-engine-routing.test.ts +71 -0
  36. package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +1 -0
  37. package/src/execution/__tests__/create-concurrency-pool.test.ts +194 -0
  38. package/src/execution/__tests__/delivery-methods.test.ts +2 -0
  39. package/src/execution/__tests__/descendant-sweep.test.ts +269 -0
  40. package/src/execution/__tests__/dialog-queue.test.ts +199 -1
  41. package/src/execution/__tests__/epipe-fallback.test.ts +2 -0
  42. package/src/execution/__tests__/execution-runtime-face.test.ts +272 -0
  43. package/src/execution/__tests__/finalize-record.test.ts +106 -0
  44. package/src/execution/__tests__/gc-timer.test.ts +2 -0
  45. package/src/execution/__tests__/get-record-for-action-restart.test.ts +2 -0
  46. package/src/execution/__tests__/get-state-handshake.test.ts +127 -0
  47. package/src/execution/__tests__/keep-alive-no-progress.test.ts +378 -0
  48. package/src/execution/__tests__/kill-all-escalation.test.ts +196 -0
  49. package/src/execution/__tests__/lifecycle-manager-idle-timer-identity.test.ts +117 -0
  50. package/src/execution/__tests__/lifecycle-manager.test.ts +39 -0
  51. package/src/execution/__tests__/manifest-store-tmp-recovery.test.ts +99 -0
  52. package/src/execution/__tests__/max-turns-to-watchdog-ms.test.ts +113 -0
  53. package/src/execution/__tests__/notify-ledger.test.ts +294 -0
  54. package/src/execution/__tests__/record-store-orphan-revive.test.ts +159 -0
  55. package/src/execution/__tests__/record-store.test.ts +83 -0
  56. package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +4 -0
  57. package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +1 -0
  58. package/src/execution/__tests__/run-spawn-edges.test.ts +407 -17
  59. package/src/execution/__tests__/run-spawn-integration.test.ts +1 -1
  60. package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +12 -4
  61. package/src/execution/__tests__/service-kill-escalation.test.ts +89 -0
  62. package/src/execution/__tests__/session-pending.test.ts +228 -0
  63. package/src/execution/__tests__/session-runner-branch-cache-lru.test.ts +110 -0
  64. package/src/execution/__tests__/session-runner-close-prune.test.ts +181 -0
  65. package/src/execution/__tests__/session-runner-epipe.test.ts +1 -0
  66. package/src/execution/__tests__/session-runner-heartbeat-idle-fallback.test.ts +226 -0
  67. package/src/execution/__tests__/settled-watchdog.test.ts +289 -0
  68. package/src/execution/__tests__/spawned-children.test.ts +153 -10
  69. package/src/execution/__tests__/subagent-actions-core.test.ts +997 -0
  70. package/src/execution/__tests__/subagent-service-message-close.test.ts +71 -10
  71. package/src/execution/__tests__/subagent-service-multiproc-guard.test.ts +174 -0
  72. package/src/execution/__tests__/subagent-service-notify-gate.test.ts +266 -0
  73. package/src/execution/__tests__/subagent-service-parent-guard.test.ts +3 -0
  74. package/src/execution/__tests__/subagent-service-recovery-bounds.test.ts +316 -0
  75. package/src/execution/__tests__/timeout-integration.test.ts +7 -7
  76. package/src/execution/__tests__/ui-request-handler-factory.test.ts +95 -1
  77. package/src/execution/__tests__/ui-request-queue.test.ts +25 -0
  78. package/src/execution/__tests__/worktree-git-ops.test.ts +571 -0
  79. package/src/execution/__tests__/worktree-reconcile-aging.test.ts +204 -0
  80. package/src/execution/agent-registry.ts +225 -1
  81. package/src/execution/agents-assembly.ts +88 -0
  82. package/src/execution/concurrency-pool.ts +71 -9
  83. package/src/execution/dialog-queue.ts +101 -3
  84. package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +49 -2
  85. package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +87 -1
  86. package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +26 -0
  87. package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +83 -1
  88. package/src/execution/engine/__tests__/conformance/zcode-appserver-harness.ts +131 -0
  89. package/src/execution/engine/__tests__/registry.test.ts +90 -1
  90. package/src/execution/engine/engine-discovery.ts +12 -16
  91. package/src/execution/engine/engines/pi/task-spec-mapper.ts +3 -3
  92. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/fake-appserver.mjs +274 -0
  93. package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-appserver.json +36 -0
  94. package/src/execution/engine/engines/zcode/__tests__/connection.test.ts +472 -0
  95. package/src/execution/engine/engines/zcode/__tests__/preparer-appserver.test.ts +387 -0
  96. package/src/execution/engine/engines/zcode/__tests__/session-channel.test.ts +780 -0
  97. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-appserver.test.ts +815 -0
  98. package/src/execution/engine/engines/zcode/__tests__/zcode-engine-degrade.test.ts +462 -0
  99. package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +119 -1
  100. package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +136 -6
  101. package/src/execution/engine/engines/zcode/appserver-home.ts +442 -0
  102. package/src/execution/engine/engines/zcode/appserver-probe.ts +141 -0
  103. package/src/execution/engine/engines/zcode/connection.ts +585 -0
  104. package/src/execution/engine/engines/zcode/constants.ts +129 -0
  105. package/src/execution/engine/engines/zcode/golden-sample.ts +54 -8
  106. package/src/execution/engine/engines/zcode/preparer.ts +25 -22
  107. package/src/execution/engine/engines/zcode/session-channel.ts +656 -0
  108. package/src/execution/engine/engines/zcode/zcode-engine.ts +909 -41
  109. package/src/execution/engine/host-task-spec.ts +3 -3
  110. package/src/execution/engine/port.ts +41 -1
  111. package/src/execution/engine/registry.ts +53 -0
  112. package/src/execution/finalize-record.ts +63 -5
  113. package/src/execution/get-state-handshake.ts +85 -10
  114. package/src/execution/lifecycle-manager.ts +27 -3
  115. package/src/execution/manifest-store.ts +53 -62
  116. package/src/execution/notifier.ts +10 -10
  117. package/src/execution/notify-ledger.ts +117 -16
  118. package/src/execution/record-entry.ts +8 -2
  119. package/src/execution/record-store.ts +29 -15
  120. package/src/execution/session-pending.ts +213 -62
  121. package/src/execution/session-runner.ts +974 -128
  122. package/src/execution/sessions-index.ts +10 -55
  123. package/src/execution/settled-watchdog.ts +99 -0
  124. package/src/execution/subagent-actions-core.ts +686 -0
  125. package/src/execution/subagent-service.ts +396 -49
  126. package/src/execution/ui-request-handler-factory.ts +27 -4
  127. package/src/execution/worktree-git-ops.ts +397 -0
  128. package/src/execution/worktree-manager.ts +72 -10
  129. package/src/execution/worktree-registry.ts +6 -12
  130. package/src/index.ts +413 -6
  131. package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +42 -3
  132. package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +2 -4
  133. package/src/orchestration/__tests__/args-meta.test.ts +358 -0
  134. package/src/orchestration/__tests__/error-recovery-handlers.test.ts +1 -8
  135. package/src/orchestration/__tests__/error-recovery-rebuild-failure.test.ts +312 -0
  136. package/src/orchestration/__tests__/error-recovery-terminal-hardening.test.ts +382 -0
  137. package/src/orchestration/__tests__/file-run-store-prune.test.ts +168 -0
  138. package/src/orchestration/__tests__/file-run-store-throttle.test.ts +168 -0
  139. package/src/orchestration/__tests__/file-run-store.test.ts +389 -0
  140. package/src/orchestration/__tests__/helpers/flush-microtasks.ts +13 -0
  141. package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +34 -0
  142. package/src/orchestration/__tests__/lifecycle-abort-broadcast-signal.test.ts +357 -0
  143. package/src/orchestration/__tests__/lifecycle-recover-crashed.test.ts +289 -0
  144. package/src/orchestration/__tests__/lifecycle.test.ts +77 -0
  145. package/src/orchestration/__tests__/run-snapshot.test.ts +353 -0
  146. package/src/orchestration/__tests__/script-generate.test.ts +314 -0
  147. package/src/orchestration/__tests__/worker-pending-timeout-abort.test.ts +275 -0
  148. package/src/orchestration/__tests__/worker-script-builder-runtime.test.ts +78 -1
  149. package/src/orchestration/__tests__/workflow-files.test.ts +186 -0
  150. package/src/orchestration/__tests__/workflow-run-summary.test.ts +119 -0
  151. package/src/orchestration/__tests__/workflow-script-registry-impl.test.ts +124 -0
  152. package/src/orchestration/agent-opts-resolver.ts +7 -7
  153. package/src/orchestration/args-meta.ts +198 -0
  154. package/src/orchestration/error-recovery.ts +416 -146
  155. package/src/orchestration/execute-agent-call.ts +9 -9
  156. package/src/orchestration/file-run-store.ts +327 -0
  157. package/src/orchestration/launcher.ts +35 -26
  158. package/src/orchestration/lifecycle.ts +355 -79
  159. package/src/orchestration/models/agent-call.ts +7 -7
  160. package/src/orchestration/models/budget.ts +5 -5
  161. package/src/orchestration/models/run-runtime.ts +13 -13
  162. package/src/orchestration/models/trace.ts +8 -8
  163. package/src/orchestration/models/workflow-run.ts +27 -27
  164. package/src/orchestration/models/workflow-script.ts +4 -4
  165. package/src/orchestration/run-snapshot.ts +266 -0
  166. package/src/orchestration/script-generate.ts +154 -0
  167. package/src/orchestration/script-lint.ts +33 -33
  168. package/src/orchestration/worker-handle.ts +10 -10
  169. package/src/orchestration/worker-host.ts +10 -10
  170. package/src/orchestration/worker-script-builder.ts +401 -346
  171. package/src/orchestration/workflow-files.ts +37 -11
  172. package/src/orchestration/workflow-run-summary.ts +69 -0
  173. package/src/orchestration/workflow-script-registry-impl.ts +31 -9
  174. package/src/shared/__tests__/agent-ref.test.ts +209 -2
  175. package/src/shared/__tests__/atomic-write.test.ts +267 -0
  176. package/src/shared/__tests__/bounded-serialize.test.ts +236 -0
  177. package/src/shared/__tests__/injection-render.test.ts +518 -0
  178. package/src/shared/__tests__/resource-discovery-host-roots.test.ts +474 -0
  179. package/src/shared/__tests__/resource-discovery.test.ts +3 -2
  180. package/src/shared/agent-ref.ts +143 -2
  181. package/src/shared/atomic-write.ts +320 -0
  182. package/src/shared/bounded-serialize.ts +154 -0
  183. package/src/shared/injection-render.ts +279 -0
  184. package/src/shared/meta-parser.ts +41 -1
  185. package/src/shared/resource-discovery.ts +104 -37
  186. package/src/shared/resource-meta.ts +14 -0
  187. package/src/shared/xml-injection.ts +9 -9
  188. package/workflows/README.md +9 -9
  189. package/workflows/chain.js +4 -2
  190. package/workflows/map-reduce.js +5 -3
  191. package/workflows/parallel.js +5 -3
  192. package/workflows/review-fix-loop-utils.cjs +14 -3
  193. package/workflows/review-fix-loop.js +18 -7
  194. package/workflows/scatter-gather.js +4 -2
  195. package/dist/chunk-3VOERJPJ.js +0 -22
@@ -0,0 +1,141 @@
1
+ // src/execution/engine/engines/zcode/appserver-probe.ts
2
+ //
3
+ // [R5 D8] app-server 协议冒烟探针。设计权威源:docs/design/zcode-engine-appserver-
4
+ // resident.md §3.3 D8①(独立连接 create 探针会话 → close → shutdown、校验应答形状
5
+ // 与 sessionId 提取、预算 10s、不触发模型请求不产生费用)+ D2 降级链第①步(探针
6
+ // 失败 → 本任务起直接走 spawn)。
7
+ //
8
+ // 关键约束:
9
+ // - 独立短命连接(AppServerConnection 全新实例,不与常驻主连接共享进程/状态);
10
+ // 探针结束 shutdown 杀链收割,绝不留孤儿进程;
11
+ // - 必须用**已引导的常驻 HOME**(D7 教训:先 bootstrap 再 probe,否则 app-server
12
+ // 启动即因 config 缺失报 -32603,探针永远误降级 spawn)——homeDir 由调用方
13
+ // (zcode-engine 的 probe 门控)经 ensureAppServerHome 保证;
14
+ // - create 帧键集与 SessionChannel.createSession 逐字同源(A.2 ① strict——对面
15
+ // zod 拒未知键),但不带 model、绝不发 session/send(无模型请求、无费用);
16
+ // - 预算整体 race 兜底:请求级超时之外,进程假死/spawn 挂起由外层 deadline 收割。
17
+ //
18
+ // 结论消费方:zcode-engine 的 probe 门控(结论与 CLI mtime 绑定缓存——zcode 升级
19
+ // mtime 变化后首个任务前重探,D2③)。
20
+
21
+ import { getLogger } from "../../../../core/logger.ts";
22
+
23
+ import { AppServerConnection, buildAppServerEnv } from "./connection.ts";
24
+ import { extractCreatedSessionId, stableWorkspaceKey } from "./session-channel.ts";
25
+ import {
26
+ ZCODE_APPSERVER_PROBE_BUDGET_MS,
27
+ ZCODE_APPSERVER_PROBE_CONN_ENV,
28
+ ZCODE_KILL_GRACE_MS,
29
+ } from "./constants.ts";
30
+
31
+ const logger = getLogger("subagents");
32
+
33
+ /** 应答形状异常时的摘要截断长度(够定位形态、不刷屏)。 */
34
+ const CREATE_REPLY_LOG_CHARS = 300;
35
+
36
+ /** 冒烟探针参数(依赖全量注入:cliPath/homeDir/env 来自引擎,测试可换 tmp 路径)。 */
37
+ export interface AppServerSmokeProbeOptions {
38
+ /** zcode CLI 路径(node 脚本)。 */
39
+ cliPath: string;
40
+ /** 已引导的常驻 HOME(D7:探针连接的 --cwd 与 $HOME)。 */
41
+ homeDir: string;
42
+ /** env 基底(引擎 deps.processEnv;探针 env 在其上叠 HOME/嵌套标记/探针连接标记)。 */
43
+ baseEnv: NodeJS.ProcessEnv;
44
+ /** stderr tee 落盘路径(append;与常驻连接分文件——探针故障独立取证)。 */
45
+ stderrLogPath: string;
46
+ /** 总预算(缺省 ZCODE_APPSERVER_PROBE_BUDGET_MS;测试注入短预算验证超时路径)。 */
47
+ budgetMs?: number;
48
+ }
49
+
50
+ /** 冒烟探针结论(ok=false 时 detail 携带失败素材——降级 reason 的数据源)。 */
51
+ export interface AppServerSmokeProbeResult {
52
+ ok: boolean;
53
+ detail: string;
54
+ }
55
+
56
+ /** 错误出声用 message 提取(非 Error 值不抛二次异常)。 */
57
+ function errMessage(err: unknown): string {
58
+ return err instanceof Error ? err.message : String(err);
59
+ }
60
+
61
+ /**
62
+ * 跑一次协议冒烟:独立连接上 create 探针会话(校验 sessionId 可提取)→ close →
63
+ * shutdown。任何一步失败/超预算 → ok=false(调用方降级 spawn,不抛——探针失败是
64
+ * 结论不是异常)。
65
+ */
66
+ export async function runAppServerSmokeProbe(opts: AppServerSmokeProbeOptions): Promise<AppServerSmokeProbeResult> {
67
+ const budgetMs = opts.budgetMs ?? ZCODE_APPSERVER_PROBE_BUDGET_MS;
68
+ const conn = new AppServerConnection({
69
+ cliPath: opts.cliPath,
70
+ cwd: opts.homeDir,
71
+ env: {
72
+ ...buildAppServerEnv(opts.homeDir, opts.baseEnv),
73
+ // 探针连接标记(fake/诊断侧区分探针进程与常驻进程的判据;对真 CLI 透明)
74
+ [ZCODE_APPSERVER_PROBE_CONN_ENV]: "1",
75
+ },
76
+ stderrLogPath: opts.stderrLogPath,
77
+ requestTimeoutMs: budgetMs,
78
+ });
79
+
80
+ // 探针工作面:create(形状校验)→ close。绝不 send(无模型请求)。
81
+ const work = (async (): Promise<string> => {
82
+ const created = await conn.request("session/create", {
83
+ workspace: {
84
+ workspacePath: opts.homeDir,
85
+ workspaceKey: stableWorkspaceKey(opts.homeDir),
86
+ },
87
+ mode: "yolo",
88
+ persistence: "immediate",
89
+ });
90
+ const sessionId = extractCreatedSessionId(created);
91
+ if (sessionId === undefined) {
92
+ throw new Error(
93
+ `session/create 应答无可提取 sessionId: ${JSON.stringify(created).slice(0, CREATE_REPLY_LOG_CHARS)}`,
94
+ );
95
+ }
96
+ await conn.request("session/close", { sessionId });
97
+ return sessionId;
98
+ })();
99
+
100
+ // 预算 race:进程假死/spawn 挂起时请求级超时不够(帧可能永不到达)——外层
101
+ // deadline 兜底,超时即判探针失败(错误规格表末行:probe 预算耗尽 → 降级 spawn)
102
+ let timedOut = false;
103
+ const deadline = new Promise<void>((resolve) => {
104
+ const t = setTimeout(() => {
105
+ timedOut = true;
106
+ resolve();
107
+ }, budgetMs);
108
+ if (typeof t.unref === "function") t.unref();
109
+ });
110
+
111
+ let failure: unknown;
112
+ const sessionId = await Promise.race([
113
+ work.catch((err: unknown) => {
114
+ failure = err;
115
+ return undefined;
116
+ }),
117
+ deadline.then(() => undefined),
118
+ ]);
119
+
120
+ // 无论成败都收割探针进程(短命语义:shutdown 是杀链全序,幂等;失败只 debug——
121
+ // 探针结论已定,收割失败留给宿主 dispose 兜底)
122
+ await conn.shutdown({ graceMs: ZCODE_KILL_GRACE_MS }).catch((err: unknown) => {
123
+ logger.debug(`[zcode-probe] 探针连接 shutdown 失败(best-effort): ${errMessage(err)}`);
124
+ });
125
+
126
+ if (failure !== undefined) {
127
+ return { ok: false, detail: `探针会话失败: ${errMessage(failure)}` };
128
+ }
129
+ if (sessionId === undefined) {
130
+ return {
131
+ ok: false,
132
+ detail: timedOut
133
+ ? `探针 ${budgetMs}ms 预算耗尽(create/close 未完成——协议无响应或进程假死)`
134
+ : "探针未产出会话 id(未知形态)",
135
+ };
136
+ }
137
+ return {
138
+ ok: true,
139
+ detail: `协议冒烟通过(create→close→shutdown,探针会话 ${sessionId},未发模型请求)`,
140
+ };
141
+ }