@anyswarm/core 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (398) hide show
  1. package/README.md +11 -10
  2. package/deploy/worker/Dockerfile +12 -4
  3. package/dist/deploy/worker/Dockerfile +12 -4
  4. package/dist/examples/README.md +24 -12
  5. package/dist/examples/authoring/README.md +24 -0
  6. package/dist/examples/authoring/business.d.ts +2 -0
  7. package/dist/examples/authoring/business.js +4 -0
  8. package/dist/examples/authoring/config.d.ts +5 -0
  9. package/dist/examples/authoring/config.js +2 -0
  10. package/dist/examples/authoring/eject.json +5 -0
  11. package/dist/examples/authoring/harness.d.ts +4 -0
  12. package/dist/examples/authoring/harness.js +19 -0
  13. package/dist/examples/autoresearch/README.md +33 -0
  14. package/dist/examples/autoresearch/REQUIREMENTS.zh.md +18 -0
  15. package/dist/examples/autoresearch/config.d.ts +51 -0
  16. package/dist/examples/autoresearch/config.js +57 -0
  17. package/dist/examples/autoresearch/demo.d.ts +11 -0
  18. package/dist/examples/autoresearch/demo.js +52 -0
  19. package/dist/examples/autoresearch/eject.json +15 -0
  20. package/dist/examples/autoresearch/frontiercs2.d.ts +87 -0
  21. package/dist/examples/autoresearch/frontiercs2.js +211 -0
  22. package/dist/examples/autoresearch/prepare.d.ts +95 -0
  23. package/dist/examples/autoresearch/prepare.js +257 -0
  24. package/dist/examples/autoresearch/process.d.ts +13 -0
  25. package/dist/examples/autoresearch/process.js +48 -0
  26. package/dist/examples/autoresearch/project.d.ts +115 -0
  27. package/dist/examples/autoresearch/project.js +410 -0
  28. package/dist/examples/autoresearch/research-service.d.ts +106 -0
  29. package/dist/examples/autoresearch/research-service.js +255 -0
  30. package/dist/examples/code/README.md +25 -0
  31. package/dist/examples/code/REQUIREMENTS.zh.md +11 -0
  32. package/dist/examples/code/candidate.d.ts +19 -0
  33. package/dist/examples/code/candidate.js +80 -0
  34. package/dist/examples/code/config.d.ts +32 -0
  35. package/dist/examples/code/config.js +18 -0
  36. package/dist/examples/code/eject.json +5 -0
  37. package/dist/examples/code/project.d.ts +3 -0
  38. package/dist/examples/code/project.js +605 -0
  39. package/dist/examples/code/prompts.d.ts +5 -0
  40. package/dist/examples/code/prompts.js +12 -0
  41. package/dist/examples/code/verification.d.ts +19 -0
  42. package/dist/examples/code/verification.js +268 -0
  43. package/dist/examples/code/worktree.d.ts +17 -0
  44. package/dist/examples/code/worktree.js +233 -0
  45. package/dist/examples/harness-session/README.md +3 -6
  46. package/dist/examples/lead-worker/harness/dynamic-scheduler.d.ts +2 -1
  47. package/dist/examples/lead-worker/harness/dynamic-scheduler.js +5 -2
  48. package/dist/examples/lead-worker/harness/index.d.ts +60 -0
  49. package/dist/examples/lead-worker/harness/index.js +77 -6
  50. package/dist/examples/lead-worker/project.js +2 -2
  51. package/dist/examples/lead-worker/services/board.d.ts +5 -0
  52. package/dist/examples/lead-worker/services/board.js +19 -1
  53. package/dist/examples/messages/README.md +5 -5
  54. package/dist/examples/messages/handoff.d.ts +2 -0
  55. package/dist/examples/messages/handoff.js +5 -1
  56. package/dist/examples/messages/project.js +52 -2
  57. package/dist/examples/notes/README.md +13 -18
  58. package/dist/examples/notes/project.js +67 -6
  59. package/dist/examples/peermesh/harness/router.js +3 -1
  60. package/dist/examples/peermesh/harness/team.d.ts +8 -0
  61. package/dist/examples/peermesh/harness/team.js +13 -0
  62. package/dist/examples/peermesh/project.js +22 -4
  63. package/dist/examples/services/agent-control/README.md +3 -3
  64. package/dist/examples/services/mathlib-search/Dockerfile +4 -2
  65. package/dist/examples/services/mathlib-search/README.md +1 -1
  66. package/dist/examples/services/mcp-managed/README.md +2 -2
  67. package/dist/examples/services/persistent-board/README.md +13 -3
  68. package/dist/examples/services/persistent-board/business.d.ts +7 -0
  69. package/dist/examples/services/persistent-board/business.js +7 -0
  70. package/dist/examples/services/persistent-board/index.js +10 -0
  71. package/dist/examples/services/persistent-board/live.d.ts +3 -0
  72. package/dist/examples/services/persistent-board/live.js +58 -0
  73. package/dist/examples/subagents/README.md +5 -5
  74. package/dist/examples/subagents/business.js +3 -1
  75. package/dist/examples/subagents/harness/controller.d.ts +15 -0
  76. package/dist/examples/subagents/harness/controller.js +181 -32
  77. package/dist/examples/subagents/project.d.ts +2 -0
  78. package/dist/examples/subagents/project.js +43 -34
  79. package/dist/examples/subagents/prompts/prompts.d.ts +1 -0
  80. package/dist/examples/subagents/prompts/prompts.js +3 -0
  81. package/dist/examples/subagents/services/collaboration-service.d.ts +3 -2
  82. package/dist/examples/subagents/services/collaboration-service.js +14 -2
  83. package/dist/examples/substrate/README.md +2 -2
  84. package/dist/examples/substrate/harness/index.d.ts +1 -1
  85. package/dist/examples/substrate/harness/index.js +77 -12
  86. package/dist/examples/substrate/project.js +4 -1
  87. package/dist/examples/super/README.md +40 -0
  88. package/dist/examples/super/actions.d.ts +95 -0
  89. package/dist/examples/super/actions.js +230 -0
  90. package/dist/examples/super/artifact.d.ts +4 -0
  91. package/dist/examples/super/artifact.js +51 -0
  92. package/dist/examples/super/config.d.ts +80 -0
  93. package/dist/examples/super/config.js +83 -0
  94. package/dist/examples/super/coordinator.d.ts +33 -0
  95. package/dist/examples/super/coordinator.js +151 -0
  96. package/dist/examples/super/daemon.d.ts +11 -0
  97. package/dist/examples/super/daemon.js +109 -0
  98. package/dist/examples/super/decisions.d.ts +8 -0
  99. package/dist/examples/super/decisions.js +52 -0
  100. package/dist/examples/super/eject.json +5 -0
  101. package/dist/examples/super/execution.d.ts +11 -0
  102. package/dist/examples/super/execution.js +136 -0
  103. package/dist/examples/super/github.d.ts +62 -0
  104. package/dist/examples/super/github.js +269 -0
  105. package/dist/examples/super/inbox.d.ts +162 -0
  106. package/dist/examples/super/inbox.js +284 -0
  107. package/dist/examples/super/location.d.ts +5 -0
  108. package/dist/examples/super/location.js +64 -0
  109. package/dist/examples/super/outbox.d.ts +5 -0
  110. package/dist/examples/super/outbox.js +103 -0
  111. package/dist/examples/super/project.d.ts +111 -0
  112. package/dist/examples/super/project.js +253 -0
  113. package/dist/examples/super/prompts.d.ts +3 -0
  114. package/dist/examples/super/prompts.js +25 -0
  115. package/dist/examples/taskpool/deploy/service-hosts/Dockerfile +4 -2
  116. package/dist/examples/taskpool/harness/controller.d.ts +35 -6
  117. package/dist/examples/taskpool/harness/controller.js +73 -7
  118. package/dist/examples/taskpool/project.js +18 -2
  119. package/dist/scripts/anyswarm-applications.d.ts +19 -0
  120. package/dist/scripts/anyswarm-applications.js +19 -0
  121. package/dist/scripts/anyswarm-arguments.d.ts +35 -5
  122. package/dist/scripts/anyswarm-arguments.js +87 -23
  123. package/dist/scripts/anyswarm-author.d.ts +5 -0
  124. package/dist/scripts/anyswarm-author.js +24 -0
  125. package/dist/scripts/anyswarm-eject.d.ts +11 -0
  126. package/dist/scripts/anyswarm-eject.js +164 -0
  127. package/dist/scripts/anyswarm-harnesses.d.ts +2 -0
  128. package/dist/scripts/anyswarm-harnesses.js +31 -0
  129. package/dist/scripts/anyswarm-installation.js +19 -13
  130. package/dist/scripts/anyswarm-launch.d.ts +37 -0
  131. package/dist/scripts/anyswarm-launch.js +86 -0
  132. package/dist/scripts/anyswarm-project.d.ts +12 -2
  133. package/dist/scripts/anyswarm-project.js +121 -20
  134. package/dist/scripts/anyswarm-source-runner.d.ts +1 -0
  135. package/dist/scripts/anyswarm-source-runner.js +232 -0
  136. package/dist/scripts/anyswarm-tui-config.d.ts +36 -0
  137. package/dist/scripts/anyswarm-tui-config.js +189 -0
  138. package/dist/scripts/anyswarm-tui-session.d.ts +66 -0
  139. package/dist/scripts/anyswarm-tui-session.js +304 -0
  140. package/dist/scripts/anyswarm-tui-terminal.d.ts +41 -0
  141. package/dist/scripts/anyswarm-tui-terminal.js +154 -0
  142. package/dist/scripts/anyswarm-tui.d.ts +2 -0
  143. package/dist/scripts/anyswarm-tui.js +1012 -0
  144. package/dist/scripts/anyswarm.js +47 -2
  145. package/dist/scripts/build-provenance.json +1 -0
  146. package/dist/scripts/build.js +7 -1
  147. package/dist/scripts/generate-architecture-api.js +2 -0
  148. package/dist/scripts/generate-architecture-prompts.js +24 -2
  149. package/dist/scripts/local-environment.d.ts +6 -1
  150. package/dist/scripts/local-environment.js +52 -16
  151. package/dist/scripts/local-setup-smoke.js +3 -2
  152. package/dist/scripts/local-setup-worker.d.ts +1 -1
  153. package/dist/scripts/local-setup-worker.js +15 -6
  154. package/dist/scripts/local-setup.d.ts +2 -2
  155. package/dist/scripts/local-setup.js +72 -12
  156. package/dist/scripts/opensandbox-agent-worker.js +2 -2
  157. package/dist/scripts/pack-release.d.ts +1 -0
  158. package/dist/scripts/pack-release.js +63 -0
  159. package/dist/scripts/pi-session-bridge.js +11 -3
  160. package/dist/scripts/run-harness.js +3 -1
  161. package/dist/scripts/setup-bootstrap.js +43 -6
  162. package/dist/src/core/allocation-ledger.js +4 -3
  163. package/dist/src/core/host-identity.d.ts +2 -0
  164. package/dist/src/core/host-identity.js +11 -0
  165. package/dist/src/core/index.d.ts +4 -2
  166. package/dist/src/core/index.js +12 -4
  167. package/dist/src/core/mcp-service.js +2 -2
  168. package/dist/src/core/runtime-management.d.ts +6 -0
  169. package/dist/src/core/runtime-management.js +24 -0
  170. package/dist/src/core/sandbox/executor-driver.d.ts +4 -2
  171. package/dist/src/core/sandbox/executor-driver.js +12 -14
  172. package/dist/src/core/sandbox/local-executor.d.ts +13 -1
  173. package/dist/src/core/sandbox/local-executor.js +69 -19
  174. package/dist/src/core/sandbox/local-worker.js +32 -21
  175. package/dist/src/core/sandbox/opensandbox-backend.d.ts +2 -0
  176. package/dist/src/core/sandbox/opensandbox-backend.js +36 -11
  177. package/dist/src/core/sandbox/sandbox-connection.d.ts +2 -0
  178. package/dist/src/core/sandbox/sandbox-connection.js +14 -6
  179. package/dist/src/core/service-live-state.d.ts +14 -0
  180. package/dist/src/core/service-live-state.js +161 -0
  181. package/dist/src/core/service-observation-types.d.ts +21 -1
  182. package/dist/src/core/service-process.d.ts +1 -1
  183. package/dist/src/core/service-process.js +45 -4
  184. package/dist/src/core/service-types.d.ts +5 -1
  185. package/dist/src/core/services.d.ts +1 -1
  186. package/dist/src/core/services.js +8 -3
  187. package/dist/src/core/types.d.ts +3 -0
  188. package/dist/src/core/worker-image.js +3 -3
  189. package/dist/src/harness/harness-session-types.d.ts +10 -0
  190. package/dist/src/harness/harness-session.js +42 -5
  191. package/dist/src/harness/project-harness.d.ts +13 -1
  192. package/dist/src/harness/project-harness.js +36 -10
  193. package/dist/src/harness/project-input.js +81 -15
  194. package/dist/src/harness/project-rpc-client.d.ts +1 -0
  195. package/dist/src/harness/project-rpc-client.js +5 -0
  196. package/dist/src/harness/project-rpc.d.ts +8 -2
  197. package/dist/src/harness/project-rpc.js +31 -7
  198. package/dist/src/harness/project-runtime.js +3 -1
  199. package/dist/src/harness/typescript-loader.d.ts +2 -0
  200. package/dist/src/harness/typescript-loader.js +41 -0
  201. package/dist/src/trace-viewer/public/app.js +6 -5
  202. package/dist/src/trace-viewer/reader.mjs +42 -2
  203. package/docs/INDEX.md +3 -1
  204. package/docs/architecture/00-overview.md +1 -1
  205. package/docs/architecture/01-agent-core.md +1 -1
  206. package/docs/architecture/06-observability-experiments.md +1 -1
  207. package/docs/architecture/07-run-configuration.md +18 -12
  208. package/docs/architecture/08-prompts.md +22 -0
  209. package/docs/architecture/10-service-api-reference.md +53 -18
  210. package/docs/architecture/11-storage-reference.md +18 -1
  211. package/docs/architecture/12-code-coverage.md +148 -84
  212. package/docs/architecture/15-core-runtime.md +8 -0
  213. package/docs/architecture/16-pi-opensandbox-backend.md +5 -5
  214. package/docs/architecture/17-harness-reporting.md +1 -1
  215. package/docs/architecture/19-substrate-harness.md +28 -1
  216. package/docs/architecture/20-lead-worker-harness.md +34 -1
  217. package/docs/architecture/22-website.md +5 -3
  218. package/docs/architecture/23-project-harness.md +124 -5
  219. package/docs/architecture/24-runtime-recovery.md +10 -3
  220. package/docs/architecture/25-foreground-tui.md +51 -0
  221. package/docs/architecture/26-code.md +39 -0
  222. package/docs/architecture/27-personal.md +43 -0
  223. package/docs/architecture/28-autoresearch.md +31 -0
  224. package/docs/architecture/INDEX.md +4 -0
  225. package/docs/architecture/prompts/08-lead-worker-harness.md +4 -2
  226. package/docs/architecture/prompts/08-subagents-harness.md +21 -0
  227. package/docs/architecture/prompts/21-personal.md +44 -0
  228. package/docs/architecture/prompts/22-autoresearch.md +63 -0
  229. package/docs/architecture/prompts/26-code.md +55 -0
  230. package/docs/architecture/prompts/source-manifest.json +152 -33
  231. package/docs/architecture/source-map.json +489 -21
  232. package/docs/designs/core-harness/06-project-harness.md +13 -2
  233. package/docs/designs/core-harness/README.md +5 -0
  234. package/docs/en/agents.md +1 -1
  235. package/docs/en/build-your-harness.md +3 -5
  236. package/docs/en/changelog.md +13 -4
  237. package/docs/en/first-harness.md +8 -11
  238. package/docs/en/harnesses/lead-worker.md +1 -1
  239. package/docs/en/harnesses/peermesh.md +2 -2
  240. package/docs/en/harnesses/subagents.md +2 -2
  241. package/docs/en/harnesses/substrate.md +4 -4
  242. package/docs/en/harnesses/taskpool.md +2 -2
  243. package/docs/en/latest.md +12 -123
  244. package/docs/en/overview.md +1 -1
  245. package/docs/en/quickstart.md +40 -16
  246. package/docs/en/reference/api.md +14 -4
  247. package/docs/en/reference/cli.md +58 -14
  248. package/docs/en/runtime.md +2 -2
  249. package/docs/en/services.md +4 -0
  250. package/docs/third-party-notices.md +1 -0
  251. package/docs/workflows/development.md +17 -12
  252. package/docs/workflows/release-and-docs-versioning.md +76 -0
  253. package/docs/zh/agents.md +1 -1
  254. package/docs/zh/build-your-harness.md +3 -5
  255. package/docs/zh/changelog.md +13 -4
  256. package/docs/zh/first-harness.md +8 -11
  257. package/docs/zh/harnesses/lead-worker.md +1 -1
  258. package/docs/zh/harnesses/peermesh.md +2 -2
  259. package/docs/zh/harnesses/subagents.md +2 -2
  260. package/docs/zh/harnesses/substrate.md +4 -4
  261. package/docs/zh/harnesses/taskpool.md +2 -2
  262. package/docs/zh/latest.md +13 -124
  263. package/docs/zh/overview.md +1 -1
  264. package/docs/zh/quickstart.md +40 -16
  265. package/docs/zh/reference/api.md +14 -4
  266. package/docs/zh/reference/cli.md +58 -14
  267. package/docs/zh/runtime.md +2 -2
  268. package/docs/zh/services.md +4 -0
  269. package/examples/README.md +24 -12
  270. package/examples/authoring/README.md +24 -0
  271. package/examples/authoring/business.ts +4 -0
  272. package/examples/authoring/config.ts +2 -0
  273. package/examples/authoring/eject.json +5 -0
  274. package/examples/authoring/harness.ts +21 -0
  275. package/examples/autoresearch/README.md +33 -0
  276. package/examples/autoresearch/REQUIREMENTS.zh.md +18 -0
  277. package/examples/autoresearch/config.ts +90 -0
  278. package/examples/autoresearch/demo.ts +55 -0
  279. package/examples/autoresearch/eject.json +15 -0
  280. package/examples/autoresearch/frontiercs2.ts +237 -0
  281. package/examples/autoresearch/prepare.ts +267 -0
  282. package/examples/autoresearch/process.ts +50 -0
  283. package/examples/autoresearch/project.ts +397 -0
  284. package/examples/autoresearch/research-service.ts +302 -0
  285. package/examples/code/README.md +25 -0
  286. package/examples/code/REQUIREMENTS.zh.md +11 -0
  287. package/examples/code/candidate.ts +89 -0
  288. package/examples/code/config.ts +19 -0
  289. package/examples/code/eject.json +5 -0
  290. package/examples/code/project.ts +581 -0
  291. package/examples/code/prompts.ts +16 -0
  292. package/examples/code/verification.ts +239 -0
  293. package/examples/code/worktree.ts +188 -0
  294. package/examples/harness-session/README.md +3 -6
  295. package/examples/lead-worker/harness/dynamic-scheduler.ts +6 -3
  296. package/examples/lead-worker/harness/index.ts +61 -6
  297. package/examples/lead-worker/project.ts +2 -2
  298. package/examples/lead-worker/services/board.ts +14 -1
  299. package/examples/messages/README.md +5 -5
  300. package/examples/messages/handoff.ts +6 -2
  301. package/examples/messages/project.ts +47 -2
  302. package/examples/notes/README.md +13 -18
  303. package/examples/notes/project.ts +57 -9
  304. package/examples/peermesh/harness/router.ts +2 -2
  305. package/examples/peermesh/harness/team.ts +11 -0
  306. package/examples/peermesh/project.ts +19 -4
  307. package/examples/services/agent-control/README.md +3 -3
  308. package/examples/services/mathlib-search/Dockerfile +4 -2
  309. package/examples/services/mathlib-search/README.md +1 -1
  310. package/examples/services/mcp-managed/README.md +2 -2
  311. package/examples/services/persistent-board/README.md +13 -3
  312. package/examples/services/persistent-board/business.ts +10 -0
  313. package/examples/services/persistent-board/index.ts +10 -0
  314. package/examples/services/persistent-board/live.ts +54 -0
  315. package/examples/subagents/README.md +5 -5
  316. package/examples/subagents/business.ts +3 -1
  317. package/examples/subagents/harness/controller.ts +161 -29
  318. package/examples/subagents/project.ts +47 -33
  319. package/examples/subagents/prompts/prompts.ts +4 -0
  320. package/examples/subagents/services/collaboration-service.ts +14 -2
  321. package/examples/substrate/README.md +2 -2
  322. package/examples/substrate/harness/index.ts +67 -11
  323. package/examples/substrate/project.ts +5 -1
  324. package/examples/super/README.md +40 -0
  325. package/examples/super/actions.ts +196 -0
  326. package/examples/super/artifact.ts +56 -0
  327. package/examples/super/config.ts +95 -0
  328. package/examples/super/coordinator.ts +158 -0
  329. package/examples/super/daemon.ts +84 -0
  330. package/examples/super/decisions.ts +46 -0
  331. package/examples/super/eject.json +5 -0
  332. package/examples/super/execution.ts +131 -0
  333. package/examples/super/github.ts +242 -0
  334. package/examples/super/inbox.ts +306 -0
  335. package/examples/super/location.ts +54 -0
  336. package/examples/super/outbox.ts +97 -0
  337. package/examples/super/project.ts +227 -0
  338. package/examples/super/prompts.ts +27 -0
  339. package/examples/taskpool/deploy/service-hosts/Dockerfile +4 -2
  340. package/examples/taskpool/harness/controller.ts +59 -7
  341. package/examples/taskpool/project.ts +18 -2
  342. package/npm-shrinkwrap.development.json +2199 -0
  343. package/npm-shrinkwrap.json +86 -1920
  344. package/package.development.json +92 -0
  345. package/package.json +13 -7
  346. package/scripts/anyswarm-applications.ts +39 -0
  347. package/scripts/anyswarm-arguments.ts +94 -28
  348. package/scripts/anyswarm-author.ts +25 -0
  349. package/scripts/anyswarm-eject.ts +145 -0
  350. package/scripts/anyswarm-harnesses.ts +27 -0
  351. package/scripts/anyswarm-installation.ts +19 -13
  352. package/scripts/anyswarm-launch.ts +108 -0
  353. package/scripts/anyswarm-project.ts +114 -21
  354. package/scripts/anyswarm-source-runner.ts +203 -0
  355. package/scripts/anyswarm-tui-config.ts +174 -0
  356. package/scripts/anyswarm-tui-session.ts +295 -0
  357. package/scripts/anyswarm-tui-terminal.ts +147 -0
  358. package/scripts/anyswarm-tui.ts +912 -0
  359. package/scripts/anyswarm.ts +43 -2
  360. package/scripts/build.ts +8 -1
  361. package/scripts/generate-architecture-api.ts +2 -0
  362. package/scripts/generate-architecture-prompts.ts +35 -2
  363. package/scripts/local-environment.ts +55 -17
  364. package/scripts/local-setup-smoke.ts +3 -2
  365. package/scripts/local-setup-worker.ts +17 -7
  366. package/scripts/local-setup.ts +66 -13
  367. package/scripts/opensandbox-agent-worker.ts +2 -2
  368. package/scripts/pack-release.ts +67 -0
  369. package/scripts/pi-session-bridge.ts +10 -2
  370. package/scripts/run-harness.ts +3 -1
  371. package/scripts/setup-bootstrap.ts +41 -6
  372. package/src/core/allocation-ledger.ts +5 -3
  373. package/src/core/host-identity.ts +11 -0
  374. package/src/core/index.ts +12 -6
  375. package/src/core/mcp-service.ts +2 -2
  376. package/src/core/runtime-management.ts +22 -0
  377. package/src/core/sandbox/executor-driver.ts +15 -18
  378. package/src/core/sandbox/local-executor.ts +65 -19
  379. package/src/core/sandbox/local-worker.ts +29 -21
  380. package/src/core/sandbox/opensandbox-backend.ts +36 -11
  381. package/src/core/sandbox/sandbox-connection.ts +14 -5
  382. package/src/core/service-live-state.ts +156 -0
  383. package/src/core/service-observation-types.ts +23 -1
  384. package/src/core/service-process.ts +36 -3
  385. package/src/core/service-types.ts +3 -1
  386. package/src/core/services.ts +8 -3
  387. package/src/core/types.ts +3 -0
  388. package/src/core/worker-image.ts +3 -3
  389. package/src/harness/harness-session-types.ts +13 -1
  390. package/src/harness/harness-session.ts +40 -7
  391. package/src/harness/project-harness.ts +36 -10
  392. package/src/harness/project-input.ts +73 -15
  393. package/src/harness/project-rpc-client.ts +5 -0
  394. package/src/harness/project-rpc.ts +29 -8
  395. package/src/harness/project-runtime.ts +2 -1
  396. package/src/harness/typescript-loader.ts +37 -0
  397. package/src/trace-viewer/public/app.js +6 -5
  398. package/src/trace-viewer/reader.mjs +42 -2
@@ -0,0 +1,108 @@
1
+ import { loadProjectHarnessModule, type Harness, type HarnessLifetime } from '../src/harness/project-harness.js';
2
+ import { basename, isAbsolute, resolve } from 'node:path';
3
+ import type { HarnessCommand } from './anyswarm-arguments.js';
4
+ import { findApplication, type ApplicationObservation } from './anyswarm-applications.js';
5
+ import { builtins } from './anyswarm-harnesses.js';
6
+
7
+ export type LaunchContext = {
8
+ cwd: string;
9
+ configuration?: unknown;
10
+ signal: AbortSignal;
11
+ outputRoot?: string;
12
+ progress?: (message: string) => void;
13
+ };
14
+ export type ActionRun = {
15
+ kind: 'start-run';
16
+ cwd: string;
17
+ prompt: string;
18
+ configuration?: Record<string, unknown>;
19
+ };
20
+
21
+ export function actionRun(value: unknown): ActionRun | undefined {
22
+ if (!value || typeof value !== 'object' || !('kind' in value) || value.kind !== 'start-run') return;
23
+ const run = value as ActionRun;
24
+ if (typeof run.cwd !== 'string' || !run.cwd || typeof run.prompt !== 'string' || !run.prompt.trim()) {
25
+ throw new Error('A standalone action must supply a workspace and prompt to start a Run.');
26
+ }
27
+ return run;
28
+ }
29
+
30
+ export function actionReport(value: unknown): { exitCode: number; result: unknown } | undefined {
31
+ if (!value || typeof value !== 'object' || !('kind' in value) || value.kind !== 'action-report') return;
32
+ if (!('result' in value) || !('exitCode' in value) || typeof value.exitCode !== 'number' ||
33
+ !Number.isSafeInteger(value.exitCode) || value.exitCode < 0 || value.exitCode > 125) {
34
+ throw new Error('An action report must supply a result and an exit code from 0 to 125.');
35
+ }
36
+ return { exitCode: value.exitCode, result: value.result };
37
+ }
38
+
39
+ export function configuredStopTimeout(configuration: Record<string, unknown> | undefined) {
40
+ const ms = configuration?.closeTimeoutMs ?? configuration?.cleanupTimeoutMs;
41
+ return typeof ms === 'number' && Number.isSafeInteger(ms) && ms > 0 ? Math.min(2_147_483_647, ms + 5000) : undefined;
42
+ }
43
+
44
+ export function requestedTraceRoot(value: unknown, runId: string | null) {
45
+ if (!runId || !value || typeof value !== 'object' || !('kind' in value) || value.kind !== 'trace.request' ||
46
+ !('runDirectory' in value) || typeof value.runDirectory !== 'string' || !isAbsolute(value.runDirectory) ||
47
+ basename(value.runDirectory) !== runId) return;
48
+ return value.runDirectory;
49
+ }
50
+
51
+ type SlashAction = (options: LaunchContext & { args: string }) => unknown | Promise<unknown>;
52
+
53
+ export async function loadHarness(launchDirectory: string, specifier: string,
54
+ options?: Parameters<typeof loadProjectHarnessModule>[3]) {
55
+ const application = findApplication(specifier);
56
+ if (application && !application.available) throw new Error(application.title + ' is not included in this SDK build.');
57
+ const { module, definition } = await loadProjectHarnessModule(launchDirectory, specifier, builtins, options);
58
+ const observe = typeof module.observe === 'function'
59
+ ? module.observe as (options: LaunchContext) => Promise<ApplicationObservation | undefined>
60
+ : undefined;
61
+ const actions = new Map<string, SlashAction>();
62
+ if (module.slashActions && typeof module.slashActions === 'object') {
63
+ for (const [name, handler] of Object.entries(module.slashActions)) {
64
+ if (!/^[a-z][\w-]*$/.test(name) || typeof handler !== 'function') {
65
+ throw new Error('Harness slashActions must map command names to functions.');
66
+ }
67
+ actions.set(name, handler as SlashAction);
68
+ }
69
+ }
70
+ const workspace = typeof module.resolveWorkspace === 'function'
71
+ ? module.resolveWorkspace as (options: Pick<LaunchContext, 'cwd' | 'configuration'>) => string
72
+ : undefined;
73
+ return { definition, observe, actions, workspace };
74
+ }
75
+
76
+ export function harnessLifetime(harness: Harness, requested?: HarnessLifetime, preferred?: HarnessLifetime): HarnessLifetime {
77
+ const supported = harness.lifetimes ?? ['task'];
78
+ const lifetime = requested ?? (preferred && supported.includes(preferred) ? preferred : supported[0]);
79
+ if (!supported.includes(lifetime)) throw new Error('This Harness does not support the requested lifetime.');
80
+ return lifetime;
81
+ }
82
+
83
+ export function splitInput(text: string): [string, string] {
84
+ const end = text.search(/\s/);
85
+ return end < 0 ? [text, ''] : [text.slice(0, end), text.slice(end).trimStart()];
86
+ }
87
+
88
+ export function interactiveTerminal() {
89
+ return process.stdin.isTTY && process.stdout.isTTY && process.env.TERM !== 'dumb';
90
+ }
91
+
92
+ export async function runHarnessSelection(command: HarnessCommand, launchDirectory: string,
93
+ options?: Parameters<typeof loadProjectHarnessModule>[3]) {
94
+ if (command.mode === 'action') {
95
+ const { runHarnessAction } = await import('./anyswarm-project.js');
96
+ return runHarnessAction(command, launchDirectory, options);
97
+ }
98
+ const mode = command.mode === 'auto' ? (interactiveTerminal() ? 'tui' : 'headless') : command.mode;
99
+ if (mode === 'tui') {
100
+ const { runAnySwarmTui } = await import('./anyswarm-tui.js');
101
+ return runAnySwarmTui({ ...command, command: 'tui' }, launchDirectory);
102
+ }
103
+ const { runProjectCommand } = await import('./anyswarm-project.js');
104
+ const project = { ...command, command: 'harness' as const, defaults: true };
105
+ if (mode === 'rpc') return runProjectCommand({ ...project, mode }, launchDirectory, options);
106
+ return runProjectCommand({ ...project, mode, cwd: command.cwd && resolve(launchDirectory, command.cwd),
107
+ prompt: command.prompt ?? 'Run this Harness.' }, launchDirectory, options);
108
+ }
@@ -1,18 +1,14 @@
1
+ import { resolve } from 'node:path';
1
2
  import { frameworkErrorFields } from '../src/core/error-diagnostics.js';
2
3
  import { captureHarnessSnapshot, createHarnessSession, failureCode, getHarnessExitCode, loadProjectHarness, stopHarnessTransport } from '../src/harness/project-harness.js';
3
4
  import { resolveHarnessInput } from '../src/harness/project-input.js';
4
5
  import { runProjectRpc } from '../src/harness/project-rpc.js';
5
- import type { ProjectCommand } from './anyswarm-arguments.js';
6
+ import type { HarnessCommand, ProjectCommand } from './anyswarm-arguments.js';
6
7
 
7
- const builtins = {
8
- 'lead-worker': new URL('../examples/lead-worker/project.js', import.meta.url).href,
9
- peermesh: new URL('../examples/peermesh/project.js', import.meta.url).href,
10
- notes: new URL('../examples/notes/project.js', import.meta.url).href,
11
- messages: new URL('../examples/messages/project.js', import.meta.url).href,
12
- taskpool: new URL('../examples/taskpool/project.js', import.meta.url).href,
13
- substrate: new URL('../examples/substrate/project.js', import.meta.url).href,
14
- subagents: new URL('../examples/subagents/project.js', import.meta.url).href,
15
- };
8
+ import { findApplication } from './anyswarm-applications.js';
9
+ import { builtins } from './anyswarm-harnesses.js';
10
+ import { actionReport, actionRun, configuredStopTimeout, harnessLifetime, loadHarness, requestedTraceRoot, splitInput, type ActionRun } from './anyswarm-launch.js';
11
+ import { LauncherConfiguration } from './anyswarm-tui-config.js';
16
12
 
17
13
  const MAX_QUEUED_OUTPUT_BYTES = 1_048_576;
18
14
  const MAX_OUTPUT_RECORD_BYTES = 8 * 1024 * 1024;
@@ -25,13 +21,19 @@ interface OutputFrame {
25
21
  flushed: PromiseWithResolvers<void>;
26
22
  }
27
23
 
28
- export async function runProjectCommand(command: ProjectCommand, launchDirectory: string) {
24
+ export async function runProjectCommand(command: ProjectCommand, launchDirectory: string,
25
+ options: { onSourceError?: (error: unknown) => void; action?: {
26
+ run: ActionRun; loaded: Awaited<ReturnType<typeof loadHarness>>;
27
+ } } = {}) {
29
28
  const stdout = process.stdout.write;
30
29
  const transport = stdout.bind(process.stdout);
31
30
  const startup = new AbortController();
32
31
  let session: ReturnType<typeof createHarnessSession> | undefined;
33
32
  let signalCode: number | undefined;
34
33
  let outputFailed = false;
34
+ let workspace = 'cwd' in command ? command.cwd : undefined;
35
+ let trace: Promise<{ root: string; close(): Promise<void> } | undefined> | undefined;
36
+ let unsubscribeTrace: (() => void) | undefined;
35
37
  let lastWrite: Promise<void> | undefined;
36
38
  const outputQueue: OutputFrame[] = [];
37
39
  let queuedBytes = 0;
@@ -113,29 +115,67 @@ export async function runProjectCommand(command: ProjectCommand, launchDirectory
113
115
  // Only the host's captured transport can write protocol output.
114
116
  process.stdout.write = process.stderr.write.bind(process.stderr);
115
117
  try {
116
- const config = { launchDirectory, configPath: command.configPath, outputRoot: command.outputRoot,
117
- lifetime: command.lifetime, clientCapabilities: { questions: command.interaction === 'questions' } };
118
118
  const stopped = Promise.withResolvers<never>();
119
119
  const abortStartup = () => stopped.reject(startup.signal.reason);
120
120
  startup.signal.addEventListener('abort', abortStartup, { once: true });
121
- let definition;
121
+ const prepare = async () => {
122
+ const loaded = options.action?.loaded ?? (command.defaults
123
+ ? await loadHarness(launchDirectory, command.harness, options) : undefined);
124
+ const definition = loaded?.definition ?? await loadProjectHarness(launchDirectory, command.harness, builtins, options);
125
+ const config = { launchDirectory, configPath: command.configPath, outputRoot: command.outputRoot,
126
+ lifetime: command.lifetime, clientCapabilities: { questions: command.mode !== 'headless' && command.interaction === 'questions' } };
127
+ if (!command.defaults) return createHarnessSession(definition, config);
128
+ const launcher = new LauncherConfiguration(command.harness, command.harness, launchDirectory,
129
+ 'cwd' in command ? command.cwd : undefined, command.configPath);
130
+ let configuration = options.action?.run.configuration;
131
+ if (!options.action) {
132
+ await launcher.refresh();
133
+ if (launcher.error) throw new Error(launcher.error);
134
+ configuration = launcher.configuration;
135
+ if (!workspace && loaded?.workspace && command.mode !== 'rpc') {
136
+ workspace = loaded.workspace({ cwd: launcher.workspace, configuration });
137
+ }
138
+ }
139
+ const lifetime = harnessLifetime(definition, command.lifetime, findApplication(command.harness)?.lifetime);
140
+ return createHarnessSession(definition, { ...config, configPath: undefined, configuration, lifetime,
141
+ stopTimeoutMs: configuredStopTimeout(configuration) });
142
+ };
122
143
  try {
123
- definition = await Promise.race([loadProjectHarness(launchDirectory, command.harness, builtins), stopped.promise]);
144
+ session = await Promise.race([prepare(), stopped.promise]);
124
145
  startup.signal.throwIfAborted();
125
146
  } finally {
126
147
  startup.signal.removeEventListener('abort', abortStartup);
127
148
  }
128
- session = createHarnessSession(definition, config);
129
- const input = command.mode === 'foreground'
130
- ? resolveHarnessInput({ cwd: command.cwd, prompt: command.prompt }, launchDirectory)
149
+ unsubscribeTrace = session.onEvent(event => {
150
+ if (trace || event.type !== 'event') return;
151
+ const root = requestedTraceRoot(event.event, event.runId);
152
+ if (!root) return;
153
+ const modulePath = '../src/trace-viewer/server.mjs';
154
+ trace = import(modulePath).then(async ({ startTraceServer }) => {
155
+ const viewer = await startTraceServer({ root, port: 0 });
156
+ console.error('Live Trace URL ' + viewer.url);
157
+ return { root, close: () => viewer.close() };
158
+ }).catch(() => {
159
+ console.error('Could not open the live Trace Viewer. Replay this Run from its records.');
160
+ return undefined;
161
+ });
162
+ }, { initialSnapshot: false });
163
+ const input = command.mode !== 'rpc'
164
+ ? resolveHarnessInput({ cwd: workspace, prompt: command.prompt }, launchDirectory)
131
165
  : undefined;
132
- if (command.mode === 'rpc' || command.lifetime === 'interactive' || config.clientCapabilities.questions) {
166
+ if (command.mode === 'rpc' || (command.mode === 'foreground' && (command.lifetime === 'interactive' || command.interaction === 'questions'))) {
133
167
  const code = await runProjectRpc(session, process.stdin, write, launchDirectory, input);
134
168
  await lastWrite;
135
169
  return signalCode ?? (outputFailed ? 1 : code);
136
170
  }
137
- await session.start(input!, { requestId: 'foreground-start' });
138
- const result = await session.completion!;
171
+ const unsubscribe = command.mode === 'headless' ? session.onEvent(event => {
172
+ if (event.type !== 'result') void write(event, event.type === 'state');
173
+ }, { onError: pipeFailed }) : undefined;
174
+ let result;
175
+ try {
176
+ await session.start(input!, { requestId: 'foreground-start' });
177
+ result = await session.completion!;
178
+ } finally { unsubscribe?.(); }
139
179
  await write({ type: 'result', runId: result.runId, result });
140
180
  return signalCode ?? (outputFailed ? 1 : getHarnessExitCode(session));
141
181
  } catch (error) {
@@ -145,6 +185,12 @@ export async function runProjectCommand(command: ProjectCommand, launchDirectory
145
185
  }
146
186
  return signalCode ?? (outputFailed ? 1 : 2);
147
187
  } finally {
188
+ unsubscribeTrace?.();
189
+ const viewer = await trace;
190
+ if (viewer) {
191
+ await viewer.close().catch(() => {});
192
+ console.error('Replay with anys trace --root ' + JSON.stringify(viewer.root));
193
+ }
148
194
  process.stdout.write = stdout;
149
195
  process.off('SIGINT', signal);
150
196
  process.off('SIGTERM', signal);
@@ -152,3 +198,50 @@ export async function runProjectCommand(command: ProjectCommand, launchDirectory
152
198
  process.stderr.off('error', ignoreDiagnosticError);
153
199
  }
154
200
  }
201
+
202
+
203
+ export async function runHarnessAction(command: Extract<HarnessCommand, { mode: 'action' }>, launchDirectory: string,
204
+ options?: Parameters<typeof loadHarness>[2]) {
205
+ const controller = new AbortController();
206
+ let exitCode = 0;
207
+ let start: { run: ActionRun; loaded: Awaited<ReturnType<typeof loadHarness>> } | undefined;
208
+ const cancel = (signal: 'SIGINT' | 'SIGTERM') => {
209
+ exitCode ||= signal === 'SIGINT' ? 130 : 143;
210
+ controller.abort();
211
+ };
212
+ const stdout = process.stdout.write;
213
+ process.stdout.write = process.stderr.write.bind(process.stderr);
214
+ process.on('SIGINT', cancel).on('SIGTERM', cancel);
215
+ try {
216
+ const loaded = await loadHarness(launchDirectory, command.harness, options);
217
+ const [name, args] = splitInput(command.slash.slice(1));
218
+ const handler = loaded.actions.get(name);
219
+ if (!handler) throw new Error('Harness has no standalone /' + name + ' action.');
220
+ const launcher = new LauncherConfiguration(command.harness, command.harness, launchDirectory, command.cwd, command.configPath);
221
+ await launcher.refresh();
222
+ if (launcher.error && launcher.path) throw new Error(launcher.error);
223
+ controller.signal.throwIfAborted();
224
+ const result = await handler({ cwd: launcher.workspace, configuration: launcher.configuration,
225
+ signal: controller.signal, outputRoot: command.outputRoot && resolve(launchDirectory, command.outputRoot), args });
226
+ controller.signal.throwIfAborted();
227
+ const run = actionRun(result);
228
+ if (run) start = { run: { ...run, configuration: run.configuration ?? launcher.configuration }, loaded };
229
+ else if (result !== undefined) {
230
+ const report = actionReport(result);
231
+ const output = report ? report.result : result;
232
+ await new Promise<void>((done, reject) => {
233
+ stdout.call(process.stdout, JSON.stringify(output) + '\n', undefined, error => error ? reject(error) : done());
234
+ });
235
+ exitCode ||= report?.exitCode ?? 0;
236
+ }
237
+ if (!start) return exitCode;
238
+ } catch (error) {
239
+ if (!exitCode) console.error(error instanceof Error ? error.message : 'Harness action failed.');
240
+ return exitCode || 1;
241
+ } finally {
242
+ process.stdout.write = stdout;
243
+ process.off('SIGINT', cancel).off('SIGTERM', cancel);
244
+ }
245
+ return runProjectCommand({ ...command, defaults: true, command: 'harness', mode: 'headless',
246
+ cwd: start.run.cwd, prompt: start.run.prompt }, launchDirectory, { ...options, action: start });
247
+ }
@@ -0,0 +1,203 @@
1
+ import { join, resolve } from 'node:path';
2
+ import { pathToFileURL } from 'node:url';
3
+ import { registerHooks } from 'node:module';
4
+ import { closeSync, mkdtempSync, openSync, readSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
5
+ import { tmpdir } from 'node:os';
6
+ import { verifyExportedSdk } from './anyswarm-eject.js';
7
+ import type { Harness } from '../src/harness/project-harness.js';
8
+ import { installSourceLoader } from '../src/harness/typescript-loader.js';
9
+ import { parseAnySwarmCommand, parseHarnessSelection } from './anyswarm-arguments.js';
10
+
11
+ const [, , entry, ...args] = process.argv;
12
+ if (!entry) throw new TypeError('A TypeScript or JavaScript entry file is required');
13
+ const filename = resolve(entry);
14
+ const entryUrl = pathToFileURL(filename).href;
15
+ process.argv = [process.execPath, filename, ...args];
16
+ installSourceLoader();
17
+
18
+ const hasOption = (name: string, value: string) => args.includes(`${name}=${value}`) ||
19
+ args.some((arg, index) => arg === name && args[index + 1] === value);
20
+ const modernFlags = args.some(arg => /^--(?:headless|rpc|cmd)(?:=|$)/.test(arg));
21
+ const hasPrompt = args.some(arg => arg === '--prompt' || arg.startsWith('--prompt='));
22
+ const outputsRecords = !hasOption('--mode', 'tui') &&
23
+ (hasPrompt || hasOption('--mode', 'foreground') || hasOption('--mode', 'rpc'));
24
+
25
+ type SourceModule = { default?: Harness };
26
+ const isHarness = (source: SourceModule): source is { default: Harness } =>
27
+ source.default?.apiVersion === 1 && typeof source.default.run === 'function';
28
+ const isInteractiveOnly = (harness: Harness) => harness.lifetimes?.includes('interactive') && !harness.lifetimes.includes('task');
29
+
30
+ async function importWithDiagnostics<T>(load: () => Promise<T>, useStderr = true): Promise<T> {
31
+ if (!useStderr) return load();
32
+ const stdout = process.stdout.write;
33
+ process.stdout.write = process.stderr.write.bind(process.stderr);
34
+ try { return await load(); } finally { process.stdout.write = stdout; }
35
+ }
36
+
37
+ async function importImplicitSource(load: () => Promise<SourceModule>) {
38
+ const { init, parse } = await import('es-module-lexer');
39
+ await init;
40
+ const stdout = process.stdout.write;
41
+ let pending: number | undefined;
42
+ let position = 0;
43
+ let streaming = false;
44
+ let interactiveOutput = false;
45
+ let inputReplay: Promise<void> | undefined;
46
+ const hook = registerHooks({ load(url, context, nextLoad) {
47
+ const loaded = nextLoad(url, context);
48
+ if (url === entryUrl && loaded.format === 'module' && loaded.source !== undefined) {
49
+ try {
50
+ const code = typeof loaded.source === 'string' ? loaded.source : new TextDecoder().decode(loaded.source);
51
+ const hasDefault = parse(code)[1].some(entry => 'name' in entry && entry.name === 'default' && !entry.typeOnly);
52
+ if (!hasDefault) {
53
+ streaming = true;
54
+ process.stdout.write = stdout;
55
+ }
56
+ } catch { /* Let Node diagnose the source if the lexer cannot classify it. */ }
57
+ }
58
+ return loaded;
59
+ } });
60
+ type WriteCallback = (error?: Error | null) => void;
61
+ process.stdout.write = (chunk: string | Uint8Array, encoding?: BufferEncoding | WriteCallback, callback?: WriteCallback) => {
62
+ const charset = typeof encoding === 'string' ? encoding : undefined;
63
+ const done = typeof encoding === 'function' ? encoding : callback;
64
+ if (streaming && pending === undefined) {
65
+ interactiveOutput = true;
66
+ return stdout.call(process.stdout, chunk, charset, done);
67
+ }
68
+ if (pending === undefined) {
69
+ const directory = mkdtempSync(join(tmpdir(), 'anys-source-output-'));
70
+ const path = join(directory, 'stdout');
71
+ try {
72
+ pending = openSync(path, 'wx+', 0o600);
73
+ unlinkSync(path);
74
+ } finally { rmdirSync(directory); }
75
+ }
76
+ writeFileSync(pending, chunk, charset);
77
+ if (done) process.nextTick(done);
78
+ return true;
79
+ };
80
+ async function replay(destination: NodeJS.WriteStream) {
81
+ const fd = pending;
82
+ const write = destination === process.stdout ? stdout : destination.write;
83
+ try {
84
+ if (fd === undefined) return;
85
+ const chunk = Buffer.alloc(64 * 1024);
86
+ for (;;) {
87
+ const size = readSync(fd, chunk, 0, chunk.length, position);
88
+ if (!size) break;
89
+ position += size;
90
+ await new Promise<void>((done, reject) =>
91
+ write.call(destination, chunk.subarray(0, size), undefined, error => error ? reject(error) : done()));
92
+ }
93
+ } finally {
94
+ pending = undefined;
95
+ if (fd !== undefined) closeSync(fd);
96
+ }
97
+ }
98
+ const streamForInput = () => {
99
+ if (streaming) return;
100
+ streaming = true;
101
+ interactiveOutput = pending !== undefined;
102
+ inputReplay = replay(process.stdout);
103
+ void inputReplay.catch(() => {});
104
+ };
105
+ const stdin = process.stdin;
106
+ const read = stdin.read;
107
+ const ownRead = Object.getOwnPropertyDescriptor(stdin, 'read');
108
+ function readForInput(this: typeof stdin, size?: number) {
109
+ // Readable also calls read(0) internally to prepare its buffer.
110
+ if (size !== 0) streamForInput();
111
+ return read.call(this, size);
112
+ }
113
+ const watchInput = (event: string | symbol) => {
114
+ if (event === 'data' || event === 'readable' || event === 'keypress') streamForInput();
115
+ };
116
+ stdin.read = readForInput;
117
+ stdin.on('newListener', watchInput).on('resume', streamForInput);
118
+ // Explicit exit cannot wait on queued pipe writes. Keep stdout's normal
119
+ // backpressure and leave any queued replay writes in their original order.
120
+ const flushOnExit = () => {
121
+ if (pending === undefined || position > 0) return;
122
+ try {
123
+ for (;;) {
124
+ const chunk = Buffer.alloc(64 * 1024);
125
+ const size = readSync(pending, chunk, 0, chunk.length, position);
126
+ if (!size || !stdout.call(process.stdout, chunk.subarray(0, size))) return;
127
+ position += size;
128
+ }
129
+ } catch { /* Preserve the explicit exit code if output can no longer be written. */ }
130
+ };
131
+ process.once('exit', flushOnExit);
132
+ let destination: NodeJS.WriteStream = process.stderr;
133
+ try {
134
+ const source = await load();
135
+ if (!isHarness(source) || (args.length === 0 && isInteractiveOnly(source.default))) destination = process.stdout;
136
+ else if (interactiveOutput) {
137
+ throw new Error('Use an explicit --prompt for a task Harness that writes to stdout while reading stdin during import.');
138
+ }
139
+ return source;
140
+ } finally {
141
+ hook.deregister();
142
+ stdin.off('newListener', watchInput).off('resume', streamForInput);
143
+ if (stdin.read === readForInput) {
144
+ if (ownRead) Object.defineProperty(stdin, 'read', ownRead);
145
+ else Reflect.deleteProperty(stdin, 'read');
146
+ }
147
+ try { await (inputReplay ?? replay(destination)); }
148
+ finally {
149
+ process.stdout.write = stdout;
150
+ process.off('exit', flushOnExit);
151
+ }
152
+ }
153
+ }
154
+
155
+ try {
156
+ const verifySdk = await verifyExportedSdk(filename);
157
+ if (verifySdk) {
158
+ const sdk = '@anyswarm/core';
159
+ let checkingEntry = true;
160
+ registerHooks({ resolve(specifier, context, nextResolve) {
161
+ if (specifier !== sdk) return nextResolve(specifier, context);
162
+ const resolved = nextResolve(specifier, checkingEntry ? { ...context, parentURL: entryUrl } : context);
163
+ verifySdk(resolved.url);
164
+ return resolved;
165
+ } });
166
+ // Use Node's ESM conditions with the same parent and fallback as the entry.
167
+ try { import.meta.resolve(sdk); } finally { checkingEntry = false; }
168
+ }
169
+ const load = (): Promise<SourceModule> => import(entryUrl);
170
+ const interactive = process.stdin.isTTY && process.stdout.isTTY && process.env.TERM !== 'dumb';
171
+ const probeSourceOutput = modernFlags || (args.length === 0 && /\.(?:ts|mts)$/.test(filename)) ||
172
+ (!interactive && !outputsRecords && args.length > 0);
173
+ const source = await (probeSourceOutput ? importImplicitSource(load) : importWithDiagnostics(load, outputsRecords));
174
+ if (isHarness(source)) {
175
+ const diagnostics = { onSourceError(error: unknown) { console.error(error instanceof Error ? error.stack : String(error)); } };
176
+ if (modernFlags || (!hasPrompt && !args.includes('--help') &&
177
+ !args.some(arg => /^--mode(?:=|$)/.test(arg)) && (args.length > 0 || interactive))) {
178
+ const { runHarnessSelection } = await import('./anyswarm-launch.js');
179
+ const code = await runHarnessSelection(parseHarnessSelection(args, filename), process.cwd(), diagnostics);
180
+ process.stdout.write('', () => process.exit(code));
181
+ } else {
182
+ const command = parseAnySwarmCommand(['--harness', filename,
183
+ ...(args.length || isInteractiveOnly(source.default) ? args : ['--prompt', 'Run this Harness.'])]);
184
+ if ('help' in command) {
185
+ console.log(command.help);
186
+ } else if (command.command === 'tui') {
187
+ const { runAnySwarmTui } = await import('./anyswarm-tui.js');
188
+ process.exit(await runAnySwarmTui(command, process.cwd()));
189
+ } else if (command.command === 'harness') {
190
+ if (command.configPath) {
191
+ const configPath = resolve(command.configPath);
192
+ const { loadAnySwarmConfig } = await import('../src/core/sandbox/sandbox-connection.js');
193
+ await importWithDiagnostics(() => loadAnySwarmConfig(configPath));
194
+ }
195
+ const { runProjectCommand } = await import('./anyswarm-project.js');
196
+ process.exitCode = await runProjectCommand(command, process.cwd(), diagnostics);
197
+ } else throw new TypeError('Harness arguments must use TUI, foreground or RPC options');
198
+ }
199
+ }
200
+ } catch (error) {
201
+ console.error(error instanceof Error ? error.stack : String(error));
202
+ process.exitCode = 1;
203
+ }