@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
@@ -27,5 +27,97 @@ declare const ZCODE_FALLBACK_DEFAULT_MODEL = "builtin:bigmodel-coding-plan/GLM-5
27
27
  declare const ZCODE_KILL_GRACE_MS = 5000;
28
28
  /** 错误信息里保留的 stdout 尾部长度(engine_run_failed 规格:够诊断、不刷屏)。 */
29
29
  declare const ZCODE_ERROR_TAIL_CHARS = 2000;
30
+ /**
31
+ * [R2] app-server 控制面请求默认超时(ms)。依据:旧 zsw 实现 REQUEST_TIMEOUT_MS=15s
32
+ * (60+ fake-server 用例 + 真机 e2e 验证值);R5 probe 冒烟用独立更短预算,不走本值。
33
+ */
34
+ declare const ZCODE_APPSERVER_REQUEST_TIMEOUT_MS = 15000;
35
+ /**
36
+ * [R2] 连接崩溃错误信息附带的 stderr 尾部长度(字符)。设计 zcode-engine-appserver-
37
+ * resident.md §3.1 失败路径 2 / §3.3 错误规格表「进程意外退出」行:stderr 尾 400 字符。
38
+ */
39
+ declare const ZCODE_APPSERVER_STDERR_TAIL_CHARS = 400;
40
+ /**
41
+ * [R2] stderr 滚动缓冲上限(字符):只保最近内容供崩溃诊断。旧 zsw 实现同值 2048。
42
+ */
43
+ declare const ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS = 2048;
44
+ /**
45
+ * [R3] 终态判定后 session/read 兜底拉取的超时(ms)。read 是全文权威来源(不变量 1
46
+ * 的双来源之二),失败不抛——降级收尾帧/delta 聚合。旧 zsw 实现 READ_TIMEOUT_MS=5000。
47
+ */
48
+ declare const ZCODE_APPSERVER_TURN_READ_TIMEOUT_MS = 5000;
49
+ /**
50
+ * [R3] 任务收尾 session/close 的控制面超时(ms,best-effort——失败只 warn 不抛)。
51
+ * 旧 zsw 实现 RELEASE_CLOSE_TIMEOUT_MS=1500(wave2 D2,同值同语义)。
52
+ */
53
+ declare const ZCODE_APPSERVER_TURN_CLOSE_TIMEOUT_MS = 1500;
54
+ /**
55
+ * [R3] 一轮终态等待的缺省预算(ms)。终态双保险(turn.terminal 权威 + 收尾帧宽松
56
+ * 匹配防洪堤)之后的最后一道:全漂移/进程假死时不挂死任务。R4 接线后任务级
57
+ * timeout / abort 链可显式传入更贴任务的值。
58
+ */
59
+ declare const ZCODE_APPSERVER_TURN_DEFAULT_TIMEOUT_MS = 300000;
60
+ /**
61
+ * [R4 D7] 常驻 HOME 的池 key(固定名)。锚定不变量 poolDir == HOME == db 所在目录:
62
+ * HOME = resolvePoolDir(engineDataDir, 'zcode', 'home-appserver'),SQLite 落
63
+ * HOME/.zcode/cli/db/db.sqlite,journal 同落该池目录。锁被活宿主持有时派生
64
+ * `home-appserver-2`… 后缀目录,handle.poolKey 记实际目录名。
65
+ */
66
+ declare const ZCODE_APPSERVER_POOL_KEY = "home-appserver";
67
+ /** [R4 D7] 目录锁文件名(HOME 内;O_EXCL 创建,内容 {pid}=持锁宿主进程 pid)。 */
68
+ declare const ZCODE_APPSERVER_LOCKFILE_NAME = "lockfile";
69
+ /** [R4 D6③/D7] app-server 进程 pidfile 文件名(HOME 内;与 lockfile.pid 严格分离)。 */
70
+ declare const ZCODE_APPSERVER_PIDFILE_NAME = "appserver.pid";
71
+ /** [R4 D7] 目录锁心跳间隔(ms):仅刷新 lockfile mtime,不参与活持有否决。 */
72
+ declare const ZCODE_APPSERVER_LOCK_HEARTBEAT_MS = 30000;
73
+ /** [R4 D7] 派生目录后缀上限(home-appserver-2 … -N)——极端并发场景防无限循环。 */
74
+ declare const ZCODE_APPSERVER_MAX_DERIVED_HOMES = 8;
75
+ /**
76
+ * [R4 D3] abort 链第一级:session/stop 的控制面超时(ms)。stop 失败/超时即落
77
+ * killChain(协议此时已不可信)。
78
+ */
79
+ declare const ZCODE_APPSERVER_STOP_TIMEOUT_MS = 3000;
80
+ /**
81
+ * [R4 D3] abort 链第二级:stop 送达后的终态确认 grace 窗口(ms)。窗口内终态
82
+ * 到达 → 不杀共享进程;超时 → killChain 连坐。
83
+ */
84
+ declare const ZCODE_APPSERVER_ABORT_GRACE_MS = 3000;
85
+ /** [R4 D6③] 孤儿回收的 SIGTERM→SIGKILL grace 窗口(ms)。 */
86
+ declare const ZCODE_APPSERVER_PIDFILE_GRACE_MS = 2000;
87
+ /**
88
+ * [R4/R5] 执行模式定向 env(appserver | spawn)。定向时不探不降;缺省走 app-server
89
+ * (R5 降级链在缺省路径插 probe 门控)。spawn 降级路径(D2 兜底)继续用
90
+ * home-<provider>-<model> 池。
91
+ */
92
+ declare const ZCODE_MODE_ENV_VAR = "XYZ_ZCODE_MODE";
93
+ /**
94
+ * [R4] app-server 内部错误码中「模型配置缺失」的归类判据(A.3:-32603 内部错误族,
95
+ * 消息含 "Model config is missing"——错误规格表第 2 行:报 engine_credential_missing)。
96
+ */
97
+ declare const ZCODE_APPSERVER_ERR_MODEL_CONFIG_MISSING = -32603;
98
+ /**
99
+ * [R5 D8] 协议冒烟探针的总预算(ms):独立短命连接上 create 探针会话 → close →
100
+ * shutdown 的全部时间窗。不发模型请求(无 session/send)、不产生费用。超预算按
101
+ * 探针失败处理(错误规格表末行:结论 failed → 降级 spawn)。
102
+ */
103
+ declare const ZCODE_APPSERVER_PROBE_BUDGET_MS = 10000;
104
+ declare const ZCODE_APPSERVER_DRIFT_RPC_CODES: readonly [-32601, -32602];
105
+ /**
106
+ * [R5] app-server 内部错误码「会话忙」:send 时该会话已有轮在跑(busy 不排队不打断)。
107
+ * 单会话一任务是结构保证,运行中命中即 bug(错误文案引导上报 sessionId 与 state 流水)。
108
+ */
109
+ declare const ZCODE_APPSERVER_ERR_BUSY_SESSION = -32010;
110
+ /**
111
+ * [R5 D8] 探针连接的 env 标记:探针用独立短命连接(不污染主连接),但 env 与主连接
112
+ * 同源(同一常驻 HOME)。此标记供 fake/诊断侧区分「探针进程」与「常驻进程」(测试
113
+ * 断言探针帧序、故障注入只命中探针或只命中主连接的判据)。
114
+ */
115
+ declare const ZCODE_APPSERVER_PROBE_CONN_ENV = "ZCODE_APPSERVER_PROBE_CONN";
116
+ /**
117
+ * [R5] 崩溃收割确认的兜底窗口(ms):killChain 在 `exit` 事件 resolve,而连接
118
+ * finalize(onClose → 在途 turn 崩溃收割)挂 `close` 事件——channel 退订前等 onClose
119
+ * 触发,本窗口兜底防 `close` 永不到达时 dispose/teardown 挂死。
120
+ */
121
+ declare const ZCODE_APPSERVER_HARVEST_GRACE_MS = 1000;
30
122
 
31
- export { ZCODE_ADAPTER_VERSION, ZCODE_CLI_DEFAULT_PATH, ZCODE_ENGINE_ID, ZCODE_ERROR_TAIL_CHARS, ZCODE_FALLBACK_DEFAULT_MODEL, ZCODE_KILL_GRACE_MS, ZCODE_POOL_CONFIG_SUFFIX, ZCODE_POOL_DB_RELATIVE_PATH, ZCODE_V2_CONFIG_PATH_SUFFIX };
123
+ export { ZCODE_ADAPTER_VERSION, ZCODE_APPSERVER_ABORT_GRACE_MS, ZCODE_APPSERVER_DRIFT_RPC_CODES, ZCODE_APPSERVER_ERR_BUSY_SESSION, ZCODE_APPSERVER_ERR_MODEL_CONFIG_MISSING, ZCODE_APPSERVER_HARVEST_GRACE_MS, ZCODE_APPSERVER_LOCKFILE_NAME, ZCODE_APPSERVER_LOCK_HEARTBEAT_MS, ZCODE_APPSERVER_MAX_DERIVED_HOMES, ZCODE_APPSERVER_PIDFILE_GRACE_MS, ZCODE_APPSERVER_PIDFILE_NAME, ZCODE_APPSERVER_POOL_KEY, ZCODE_APPSERVER_PROBE_BUDGET_MS, ZCODE_APPSERVER_PROBE_CONN_ENV, ZCODE_APPSERVER_REQUEST_TIMEOUT_MS, ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS, ZCODE_APPSERVER_STDERR_TAIL_CHARS, ZCODE_APPSERVER_STOP_TIMEOUT_MS, ZCODE_APPSERVER_TURN_CLOSE_TIMEOUT_MS, ZCODE_APPSERVER_TURN_DEFAULT_TIMEOUT_MS, ZCODE_APPSERVER_TURN_READ_TIMEOUT_MS, ZCODE_CLI_DEFAULT_PATH, ZCODE_ENGINE_ID, ZCODE_ERROR_TAIL_CHARS, ZCODE_FALLBACK_DEFAULT_MODEL, ZCODE_KILL_GRACE_MS, ZCODE_MODE_ENV_VAR, ZCODE_POOL_CONFIG_SUFFIX, ZCODE_POOL_DB_RELATIVE_PATH, ZCODE_V2_CONFIG_PATH_SUFFIX };
@@ -1,21 +1,63 @@
1
1
  import {
2
2
  ZCODE_ADAPTER_VERSION,
3
+ ZCODE_APPSERVER_ABORT_GRACE_MS,
4
+ ZCODE_APPSERVER_DRIFT_RPC_CODES,
5
+ ZCODE_APPSERVER_ERR_BUSY_SESSION,
6
+ ZCODE_APPSERVER_ERR_MODEL_CONFIG_MISSING,
7
+ ZCODE_APPSERVER_HARVEST_GRACE_MS,
8
+ ZCODE_APPSERVER_LOCKFILE_NAME,
9
+ ZCODE_APPSERVER_LOCK_HEARTBEAT_MS,
10
+ ZCODE_APPSERVER_MAX_DERIVED_HOMES,
11
+ ZCODE_APPSERVER_PIDFILE_GRACE_MS,
12
+ ZCODE_APPSERVER_PIDFILE_NAME,
13
+ ZCODE_APPSERVER_POOL_KEY,
14
+ ZCODE_APPSERVER_PROBE_BUDGET_MS,
15
+ ZCODE_APPSERVER_PROBE_CONN_ENV,
16
+ ZCODE_APPSERVER_REQUEST_TIMEOUT_MS,
17
+ ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS,
18
+ ZCODE_APPSERVER_STDERR_TAIL_CHARS,
19
+ ZCODE_APPSERVER_STOP_TIMEOUT_MS,
20
+ ZCODE_APPSERVER_TURN_CLOSE_TIMEOUT_MS,
21
+ ZCODE_APPSERVER_TURN_DEFAULT_TIMEOUT_MS,
22
+ ZCODE_APPSERVER_TURN_READ_TIMEOUT_MS,
3
23
  ZCODE_CLI_DEFAULT_PATH,
4
24
  ZCODE_ENGINE_ID,
5
25
  ZCODE_ERROR_TAIL_CHARS,
6
26
  ZCODE_FALLBACK_DEFAULT_MODEL,
7
27
  ZCODE_KILL_GRACE_MS,
28
+ ZCODE_MODE_ENV_VAR,
8
29
  ZCODE_POOL_CONFIG_SUFFIX,
9
30
  ZCODE_POOL_DB_RELATIVE_PATH,
10
31
  ZCODE_V2_CONFIG_PATH_SUFFIX
11
- } from "../../../../chunk-3VOERJPJ.js";
32
+ } from "../../../../chunk-X7SZ5HLQ.js";
12
33
  export {
13
34
  ZCODE_ADAPTER_VERSION,
35
+ ZCODE_APPSERVER_ABORT_GRACE_MS,
36
+ ZCODE_APPSERVER_DRIFT_RPC_CODES,
37
+ ZCODE_APPSERVER_ERR_BUSY_SESSION,
38
+ ZCODE_APPSERVER_ERR_MODEL_CONFIG_MISSING,
39
+ ZCODE_APPSERVER_HARVEST_GRACE_MS,
40
+ ZCODE_APPSERVER_LOCKFILE_NAME,
41
+ ZCODE_APPSERVER_LOCK_HEARTBEAT_MS,
42
+ ZCODE_APPSERVER_MAX_DERIVED_HOMES,
43
+ ZCODE_APPSERVER_PIDFILE_GRACE_MS,
44
+ ZCODE_APPSERVER_PIDFILE_NAME,
45
+ ZCODE_APPSERVER_POOL_KEY,
46
+ ZCODE_APPSERVER_PROBE_BUDGET_MS,
47
+ ZCODE_APPSERVER_PROBE_CONN_ENV,
48
+ ZCODE_APPSERVER_REQUEST_TIMEOUT_MS,
49
+ ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS,
50
+ ZCODE_APPSERVER_STDERR_TAIL_CHARS,
51
+ ZCODE_APPSERVER_STOP_TIMEOUT_MS,
52
+ ZCODE_APPSERVER_TURN_CLOSE_TIMEOUT_MS,
53
+ ZCODE_APPSERVER_TURN_DEFAULT_TIMEOUT_MS,
54
+ ZCODE_APPSERVER_TURN_READ_TIMEOUT_MS,
14
55
  ZCODE_CLI_DEFAULT_PATH,
15
56
  ZCODE_ENGINE_ID,
16
57
  ZCODE_ERROR_TAIL_CHARS,
17
58
  ZCODE_FALLBACK_DEFAULT_MODEL,
18
59
  ZCODE_KILL_GRACE_MS,
60
+ ZCODE_MODE_ENV_VAR,
19
61
  ZCODE_POOL_CONFIG_SUFFIX,
20
62
  ZCODE_POOL_DB_RELATIVE_PATH,
21
63
  ZCODE_V2_CONFIG_PATH_SUFFIX
@@ -1,4 +1,4 @@
1
- import { S as SessionView } from '../../../../types-BxyAidGf.cjs';
1
+ import { S as SessionView } from '../../../../types-DpUO16pj.cjs';
2
2
 
3
3
  type ZcodeReaderErrorCode = "engine_session_read_failed";
4
4
  declare class ZcodeReaderError extends Error {
@@ -1,4 +1,4 @@
1
- import { S as SessionView } from '../../../../types-BxyAidGf.js';
1
+ import { S as SessionView } from '../../../../types-DpUO16pj.js';
2
2
 
3
3
  type ZcodeReaderErrorCode = "engine_session_read_failed";
4
4
  declare class ZcodeReaderError extends Error {
@@ -1,238 +1,8 @@
1
1
  import {
2
- ZCODE_ENGINE_ID
3
- } from "../../../../chunk-3VOERJPJ.js";
4
-
5
- // src/execution/engine/engines/zcode/reader.ts
6
- import * as fs from "fs";
7
- var ZcodeReaderError = class extends Error {
8
- code;
9
- /** 原始失败细节(缺文件/表漂移/运行时不支持)。 */
10
- detail;
11
- constructor(detail, hint) {
12
- super(
13
- `[engine_session_read_failed] zcode \u539F\u751F session \u8BFB\u53D6\u5931\u8D25\uFF1A${detail}\u3002` + (hint ?? "\u8C03\u7528\u65B9\u5E94\u964D\u7EA7\u5230\u7B2C\u2461\u7EA7\uFF08\u5BBF\u4E3B event journal\uFF09\u6216\u7B2C\u2462\u7EA7\uFF08outcome-only\uFF09\u3002")
14
- );
15
- this.name = "ZcodeReaderError";
16
- this.code = "engine_session_read_failed";
17
- this.detail = detail;
18
- }
19
- };
20
- function parseJsonField(raw, ctx) {
21
- try {
22
- const v = JSON.parse(raw);
23
- return typeof v === "object" && v !== null && !Array.isArray(v) ? v : void 0;
24
- } catch (err) {
25
- throw new ZcodeReaderError(
26
- `${ctx} \u884C data \u4E0D\u662F\u5408\u6CD5 JSON\uFF08${err instanceof Error ? err.message : String(err)}\uFF09`,
27
- "db \u5185\u5BB9\u635F\u574F\u6216\u7248\u672C\u4E0D\u517C\u5BB9\u2014\u2014\u8C03\u7528\u65B9\u5E94\u964D\u7EA7\u5230\u7B2C\u2461\u7EA7\uFF08\u5BBF\u4E3B event journal\uFF09\u3002"
28
- );
29
- }
30
- }
31
- function finiteOr(v, fallback) {
32
- const n = typeof v === "number" ? v : Number(v);
33
- return Number.isFinite(n) ? n : fallback;
34
- }
35
- function usageFromStepFinish(part) {
36
- const tokens = part.tokens;
37
- if (typeof tokens !== "object" || tokens === null) return void 0;
38
- const t = tokens;
39
- const cache = typeof t.cache === "object" && t.cache !== null ? t.cache : {};
40
- if (t.input === void 0 && t.output === void 0) return void 0;
41
- return {
42
- input: finiteOr(t.input, 0),
43
- output: finiteOr(t.output, 0),
44
- cacheRead: finiteOr(cache.read, 0),
45
- cacheWrite: finiteOr(cache.write, 0)
46
- };
47
- }
48
- function toolFromPart(part) {
49
- const toolName = typeof part.tool === "string" ? part.tool : "unknown";
50
- let state;
51
- if (typeof part.state === "string") {
52
- try {
53
- const v = JSON.parse(part.state);
54
- state = typeof v === "object" && v !== null ? v : void 0;
55
- } catch {
56
- state = void 0;
57
- }
58
- }
59
- const status = typeof state?.status === "string" ? state.status : "unknown";
60
- const output = state?.output;
61
- return {
62
- toolName,
63
- ...state?.input !== void 0 ? { args: state.input } : {},
64
- // 输出形态(实测):string(内容)或 object(结构化)——分别映射到 pi ToolCallResult
65
- // 的 content[] / details,不发明第二形状
66
- ...typeof output === "string" ? { result: { content: [output] } } : {},
67
- ...typeof output === "object" && output !== null ? { result: { details: output } } : {},
68
- ...status !== "completed" ? { isError: true } : {}
69
- };
70
- }
71
- var TotalsAcc = class {
72
- input = 0;
73
- output = 0;
74
- cacheRead = 0;
75
- cacheWrite = 0;
76
- cost = 0;
77
- has = false;
78
- add(u) {
79
- this.has = true;
80
- this.input += u.input;
81
- this.output += u.output;
82
- this.cacheRead += u.cacheRead;
83
- this.cacheWrite += u.cacheWrite;
84
- this.cost += u.cost ?? 0;
85
- }
86
- toTotal() {
87
- if (!this.has) return void 0;
88
- return {
89
- input: this.input,
90
- output: this.output,
91
- cacheRead: this.cacheRead,
92
- cacheWrite: this.cacheWrite,
93
- cost: this.cost,
94
- total: this.input + this.output + this.cacheRead + this.cacheWrite
95
- };
96
- }
97
- };
98
- function closeTurn(acc, turns, totals) {
99
- if (acc === null) return;
100
- turns.push({ text: acc.text, thinking: acc.thinking, toolCalls: acc.toolCalls, closed: true });
101
- if (acc.usageDelta !== void 0) totals.add(acc.usageDelta);
102
- }
103
- function rowToMessageRow(v) {
104
- if (typeof v === "object" && v !== null && "id" in v && "data" in v) {
105
- const r = v;
106
- if (typeof r.id === "string" && typeof r.data === "string") return { id: r.id, data: r.data };
107
- }
108
- throw new ZcodeReaderError("message \u884C\u5F62\u72B6\u5F02\u5E38\uFF08id/data \u5217\u7F3A\u5931\u6216\u7C7B\u578B\u6F02\u79FB\uFF09");
109
- }
110
- function rowToPartRow(v) {
111
- if (typeof v === "object" && v !== null && "data" in v) {
112
- const r = v;
113
- if (typeof r.data === "string") return { data: r.data };
114
- }
115
- throw new ZcodeReaderError("part \u884C\u5F62\u72B6\u5F02\u5E38\uFF08data \u5217\u7F3A\u5931\u6216\u7C7B\u578B\u6F02\u79FB\uFF09");
116
- }
117
- function resolveSessionId(db, sessionId) {
118
- if (sessionId !== void 0) {
119
- const hit = db.prepare("SELECT id FROM session WHERE id = ?").get(sessionId);
120
- if (hit === void 0) {
121
- throw new ZcodeReaderError(
122
- `session \u4E0D\u5B58\u5728\uFF1A${sessionId}`,
123
- "sessionId \u53EF\u80FD\u6765\u81EA\u5DF2\u6E05\u7406\u7684\u6C60\u2014\u2014\u964D\u7EA7\u5230 outcome-only\u3002"
124
- );
125
- }
126
- return sessionId;
127
- }
128
- const latest = db.prepare("SELECT id FROM session ORDER BY time_created DESC LIMIT 1").get();
129
- if (latest === void 0 || typeof latest.id !== "string") {
130
- throw new ZcodeReaderError("db \u5185\u65E0\u4EFB\u4F55 session \u884C");
131
- }
132
- return latest.id;
133
- }
134
- function loadGroupedParts(db, sessionId) {
135
- const grouped = /* @__PURE__ */ new Map();
136
- for (const raw of db.prepare(
137
- "SELECT part.message_id AS mid, part.data AS data FROM part JOIN message ON part.message_id = message.id WHERE part.session_id = ? ORDER BY message.sequence, part.sequence"
138
- ).all(sessionId)) {
139
- if (typeof raw !== "object" || raw === null || !("mid" in raw)) continue;
140
- const mid = String(raw.mid);
141
- const arr = grouped.get(mid) ?? [];
142
- arr.push(rowToPartRow(raw));
143
- grouped.set(mid, arr);
144
- }
145
- return grouped;
146
- }
147
- function appendPartText(current, part) {
148
- return current === "" ? String(part.text ?? "") : current + "\n" + String(part.text ?? "");
149
- }
150
- function applyPartToTurn(part, acc, turns, totals) {
151
- switch (part.type) {
152
- case "text":
153
- acc = acc ?? { text: "", thinking: "", toolCalls: [] };
154
- acc.text = appendPartText(acc.text, part);
155
- return acc;
156
- case "reasoning":
157
- acc = acc ?? { text: "", thinking: "", toolCalls: [] };
158
- acc.thinking = appendPartText(acc.thinking, part);
159
- return acc;
160
- case "tool":
161
- acc = acc ?? { text: "", thinking: "", toolCalls: [] };
162
- acc.toolCalls.push(toolFromPart(part));
163
- return acc;
164
- case "step-finish": {
165
- const u = usageFromStepFinish(part);
166
- const cost = finiteOr(part.cost, 0);
167
- if (acc === null) acc = { text: "", thinking: "", toolCalls: [] };
168
- if (u !== void 0) acc.usageDelta = cost > 0 ? { ...u, cost } : u;
169
- closeTurn(acc, turns, totals);
170
- return null;
171
- }
172
- default:
173
- return acc;
174
- }
175
- }
176
- function collectTurns(messages, grouped, totals) {
177
- const turns = [];
178
- for (const msg of messages) {
179
- const parsedMsg = parseJsonField(msg.data, "message");
180
- if (parsedMsg?.role !== "assistant") continue;
181
- let acc = null;
182
- for (const partRow of grouped.get(msg.id) ?? []) {
183
- const part = parseJsonField(partRow.data, "part");
184
- if (part === void 0) continue;
185
- acc = applyPartToTurn(part, acc, turns, totals);
186
- }
187
- closeTurn(acc, turns, totals);
188
- }
189
- return turns;
190
- }
191
- function buildView(db, sessionId) {
192
- const messages = db.prepare("SELECT id, data FROM message WHERE session_id = ? ORDER BY sequence").all(sessionId).map(rowToMessageRow);
193
- const totals = new TotalsAcc();
194
- const turns = collectTurns(messages, loadGroupedParts(db, sessionId), totals);
195
- const usageTotal = totals.toTotal();
196
- return {
197
- engineId: ZCODE_ENGINE_ID,
198
- sessionId,
199
- ...usageTotal !== void 0 ? { usage: usageTotal } : {},
200
- turns,
201
- source: "native"
202
- };
203
- }
204
- async function readZcodeSessionView(dbPath, sessionId) {
205
- if (!fs.existsSync(dbPath)) {
206
- throw new ZcodeReaderError(`db \u6587\u4EF6\u4E0D\u5B58\u5728\uFF1A${dbPath}`);
207
- }
208
- const sqliteModuleId = "node:sqlite";
209
- const sqliteMod = await import(sqliteModuleId).catch(() => void 0);
210
- const DatabaseSyncCtor = sqliteMod?.DatabaseSync;
211
- if (typeof DatabaseSyncCtor !== "function") {
212
- throw new ZcodeReaderError(
213
- "\u5F53\u524D node \u8FD0\u884C\u65F6\u4E0D\u652F\u6301 node:sqlite\uFF08\u9700 >=22.13\uFF09",
214
- "\u5347\u7EA7 node \u6216\u6539\u7528\u7B2C\u2461\u7EA7\uFF08\u5BBF\u4E3B event journal\uFF09\u8BFB\u53D6\u3002"
215
- );
216
- }
217
- const open = DatabaseSyncCtor;
218
- let db;
219
- try {
220
- db = new open(dbPath, { readOnly: true });
221
- return buildView(db, resolveSessionId(db, sessionId));
222
- } catch (err) {
223
- if (err instanceof ZcodeReaderError) throw err;
224
- throw new ZcodeReaderError(
225
- `sqlite \u67E5\u8BE2\u5931\u8D25\uFF08${err instanceof Error ? err.message : String(err)}\uFF09`,
226
- "\u7591\u4F3C\u8868\u7ED3\u6784\u6F02\u79FB\uFF08zcode schema_migration \u5347\u7EA7\uFF09\u2014\u2014\u8C03\u7528\u65B9\u5E94\u964D\u7EA7\u5230\u7B2C\u2461\u7EA7\uFF1B\u5E76\u628A\u65B0 schema \u6837\u672C\u8865\u5F55\u8FDB golden \u5E93\u540E\u66F4\u65B0 reader\u3002"
227
- );
228
- } finally {
229
- try {
230
- db?.close();
231
- } catch (err) {
232
- void err;
233
- }
234
- }
235
- }
2
+ ZcodeReaderError,
3
+ readZcodeSessionView
4
+ } from "../../../../chunk-4KN4TTG7.js";
5
+ import "../../../../chunk-X7SZ5HLQ.js";
236
6
  export {
237
7
  ZcodeReaderError,
238
8
  readZcodeSessionView
@@ -1,22 +1,10 @@
1
- // src/execution/engine/paths.ts
2
- import { join } from "path";
3
- var MAX_SEG_CHARS = 80;
4
- function sanitizeSeg(input) {
5
- const s = input.replace(/[^A-Za-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
6
- return s.length > 0 ? s.slice(0, MAX_SEG_CHARS) : "default";
7
- }
8
- function resolveEnginesRoot(dataDir) {
9
- return join(dataDir, "engines");
10
- }
11
- function resolveEngineDir(dataDir, engineId) {
12
- return join(resolveEnginesRoot(dataDir), sanitizeSeg(engineId));
13
- }
14
- function resolvePoolDir(dataDir, engineId, poolKey) {
15
- return join(resolveEngineDir(dataDir, sanitizeSeg(engineId)), sanitizeSeg(poolKey));
16
- }
17
- function resolveJournalPath(dataDir, engineId, poolKey, taskId) {
18
- return join(resolvePoolDir(dataDir, engineId, poolKey), `journal-${sanitizeSeg(taskId)}.jsonl`);
19
- }
1
+ import {
2
+ resolveEngineDir,
3
+ resolveEnginesRoot,
4
+ resolveJournalPath,
5
+ resolvePoolDir,
6
+ sanitizeSeg
7
+ } from "../../chunk-APZY4IME.js";
20
8
  export {
21
9
  resolveEngineDir,
22
10
  resolveEnginesRoot,