@aiwg/cli 2026.10.15 → 2026.10.18

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 (286) hide show
  1. package/agentic/code/providers/pi/aiwg-bridge.ts +45 -7
  2. package/dist/src/artifacts/citation-parser.js +7 -6
  3. package/dist/src/artifacts/corpus-tools/citation-densify.js +4 -2
  4. package/dist/src/artifacts/corpus-tools/cli.js +8 -3
  5. package/dist/src/artifacts/corpus-tools/discovery-log.js +26 -24
  6. package/dist/src/artifacts/corpus-tools/expand-cli.js +3 -2
  7. package/dist/src/artifacts/corpus-tools/expand-dates.js +107 -0
  8. package/dist/src/artifacts/corpus-tools/expand-queue.js +4 -1
  9. package/dist/src/artifacts/corpus-tools/expand-review.js +104 -0
  10. package/dist/src/artifacts/corpus-tools/expand-state.js +17 -1
  11. package/dist/src/artifacts/corpus-tools/expand.js +16 -17
  12. package/dist/src/artifacts/corpus-tools/induction-audit.js +7 -6
  13. package/dist/src/artifacts/corpus-tools/integrity-scan.js +3 -3
  14. package/dist/src/artifacts/corpus-tools/radar-shared.js +7 -9
  15. package/dist/src/artifacts/corpus-tools/ref-files.js +11 -0
  16. package/dist/src/artifacts/corpus-tools/resolve-cli.js +11 -3
  17. package/dist/src/artifacts/corpus-tools/resolve.js +42 -11
  18. package/dist/src/artifacts/corpus-tools/retrieval-lab.js +2 -1
  19. package/dist/src/artifacts/corpus-tools/sidecar-lint.js +4 -4
  20. package/dist/src/artifacts/corpus-tools/sidecar-repair.js +2 -1
  21. package/dist/src/artifacts/corpus-views/ref-parser.js +3 -17
  22. package/dist/src/artifacts/graph-query.js +5 -3
  23. package/dist/src/artifacts/hybrid-query.js +11 -2
  24. package/dist/src/artifacts/index-refresh.js +2 -0
  25. package/dist/src/artifacts/query-engine-corpus.js +217 -0
  26. package/dist/src/artifacts/query-engine-discover-output.js +229 -0
  27. package/dist/src/artifacts/query-engine-discover.js +285 -0
  28. package/dist/src/artifacts/query-engine-entries.js +188 -0
  29. package/dist/src/artifacts/query-engine-index.js +249 -0
  30. package/dist/src/artifacts/query-engine-matching.js +143 -0
  31. package/dist/src/artifacts/query-engine-options.js +2 -0
  32. package/dist/src/artifacts/query-engine-rules.js +202 -0
  33. package/dist/src/artifacts/query-engine-scoring.js +436 -0
  34. package/dist/src/artifacts/query-engine-show-card.js +194 -0
  35. package/dist/src/artifacts/query-engine-show-load.js +211 -0
  36. package/dist/src/artifacts/query-engine-show.js +300 -0
  37. package/dist/src/artifacts/query-engine.js +7 -2576
  38. package/dist/src/cli/handlers/daemon.js +17 -1
  39. package/dist/src/cli/handlers/doctor-args.js +10 -5
  40. package/dist/src/cli/handlers/doctor-help.js +4 -1
  41. package/dist/src/cli/handlers/index.js +2 -2
  42. package/dist/src/cli/handlers/mc-agents.js +111 -0
  43. package/dist/src/cli/handlers/mc-args.js +54 -0
  44. package/dist/src/cli/handlers/mc-control.js +227 -0
  45. package/dist/src/cli/handlers/mc-dispatch.js +203 -0
  46. package/dist/src/cli/handlers/mc-help.js +171 -0
  47. package/dist/src/cli/handlers/mc-run.js +343 -0
  48. package/dist/src/cli/handlers/mc-session.js +138 -0
  49. package/dist/src/cli/handlers/mc-status.js +258 -0
  50. package/dist/src/cli/handlers/mc-summary.js +43 -0
  51. package/dist/src/cli/handlers/mc-types.js +3 -0
  52. package/dist/src/cli/handlers/mc-worker.js +205 -0
  53. package/dist/src/cli/handlers/mc.js +12 -1665
  54. package/dist/src/cli/handlers/models-refusal.js +13 -0
  55. package/dist/src/cli/handlers/models-resource-sample.js +10 -0
  56. package/dist/src/cli/handlers/models.js +5 -3
  57. package/dist/src/cli/handlers/regenerate.js +8 -16
  58. package/dist/src/cli/handlers/remove-bundled.js +133 -0
  59. package/dist/src/cli/handlers/remove-options.js +31 -0
  60. package/dist/src/cli/handlers/remove-user.js +174 -0
  61. package/dist/src/cli/handlers/remove.js +201 -0
  62. package/dist/src/cli/handlers/sessions.js +7 -7
  63. package/dist/src/cli/handlers/subcommands-bundles.js +257 -0
  64. package/dist/src/cli/handlers/subcommands-discovery.js +165 -0
  65. package/dist/src/cli/handlers/subcommands-list-format.js +129 -0
  66. package/dist/src/cli/handlers/subcommands-list.js +250 -0
  67. package/dist/src/cli/handlers/subcommands-plugins.js +213 -0
  68. package/dist/src/cli/handlers/subcommands-quickref.js +58 -0
  69. package/dist/src/cli/handlers/subcommands-rlm.js +113 -0
  70. package/dist/src/cli/handlers/subcommands-services.js +66 -0
  71. package/dist/src/cli/handlers/subcommands-storage.js +259 -0
  72. package/dist/src/cli/handlers/subcommands.js +18 -1976
  73. package/dist/src/cli/handlers/update.js +157 -0
  74. package/dist/src/cli/handlers/use.js +14 -4
  75. package/dist/src/cli/handlers/utilities.js +3 -560
  76. package/dist/src/cli/handlers/utility-doctor.js +412 -0
  77. package/dist/src/cli/scope-resolver.js +1 -142
  78. package/dist/src/cli/services/deployment-verification.js +0 -1
  79. package/dist/src/cli/user-scope-mirror.js +173 -0
  80. package/dist/src/config/aiwg-config.js +2 -1
  81. package/dist/src/config/authorization-cli.js +7 -3
  82. package/dist/src/config/dispatch-host-thresholds.js +2 -0
  83. package/dist/src/config/dispatch-thresholds-runtime.d.mts +3 -2
  84. package/dist/src/config/dispatch-thresholds-runtime.mjs +21 -7
  85. package/dist/src/config/framework-aliases.js +12 -0
  86. package/dist/src/config/host-resource-report.d.mts +24 -0
  87. package/dist/src/config/host-resource-report.mjs +110 -0
  88. package/dist/src/config/host-resource-runtime.d.mts +37 -0
  89. package/dist/src/config/host-resource-runtime.mjs +296 -0
  90. package/dist/src/config/installed-selection.js +22 -0
  91. package/dist/src/config/worker-invocation-marker.d.mts +1 -0
  92. package/dist/src/config/worker-invocation-marker.mjs +2 -0
  93. package/dist/src/decision/ensemble-study/artifacts.js +7 -0
  94. package/dist/src/decision/ensemble-study/gate-policy.js +58 -0
  95. package/dist/src/decision/ensemble-study/gates.js +78 -0
  96. package/dist/src/decision/ensemble-study/protocol.js +18 -21
  97. package/dist/src/decision/ensemble-study/score.js +3 -2
  98. package/dist/src/decision/ensemble-study/statistics.js +7 -26
  99. package/dist/src/decision/heldout/access.js +74 -0
  100. package/dist/src/decision/heldout/calibration.js +1 -0
  101. package/dist/src/decision/heldout/collector.js +8 -1
  102. package/dist/src/decision/sensitivity/comparative-gates.js +135 -0
  103. package/dist/src/decision/sensitivity/comparative.js +18 -41
  104. package/dist/src/effects/verifiers/file.js +22 -5
  105. package/dist/src/extensions/claude-attribution.js +82 -0
  106. package/dist/src/extensions/claude-hooks-installer.js +133 -69
  107. package/dist/src/extensions/commands/daemon.js +109 -0
  108. package/dist/src/extensions/commands/definitions.js +5 -89
  109. package/dist/src/gates/evaluate.js +2 -358
  110. package/dist/src/gates/heldout.js +17 -7
  111. package/dist/src/gates/observations.js +363 -0
  112. package/dist/src/gates/pack-composition.js +270 -0
  113. package/dist/src/gates/pack-tightening.js +197 -0
  114. package/dist/src/gates/pack-validation.js +262 -0
  115. package/dist/src/gates/registry-error.js +5 -0
  116. package/dist/src/gates/registry.js +13 -685
  117. package/dist/src/lint/check-patterns.js +163 -0
  118. package/dist/src/lint/checks.js +268 -0
  119. package/dist/src/lint/reference-resolution.js +54 -0
  120. package/dist/src/lint/runner.js +3 -465
  121. package/dist/src/mcp/helpers.mjs +11 -1
  122. package/dist/src/mcp/json-document.mjs +29 -0
  123. package/dist/src/mcp/muse-native-config.mjs +1 -28
  124. package/dist/src/memory/project-registry.js +44 -4
  125. package/dist/src/mission-protocol/worker-review-gate.js +7 -0
  126. package/dist/src/models/worker-plan.js +36 -5
  127. package/dist/src/models/wrapper-deployment.js +25 -12
  128. package/dist/src/models/wrapper-enforcement.js +47 -7
  129. package/dist/src/network-analysis/research.js +2 -1
  130. package/dist/src/plugin/command-ownership.js +34 -0
  131. package/dist/src/plugin/skill-command-frontmatter.js +197 -0
  132. package/dist/src/plugin/skill-command-translator.js +6 -232
  133. package/dist/src/policy/hitl-check.js +14 -4
  134. package/dist/src/policy/hitl-consume.js +54 -0
  135. package/dist/src/policy/hitl-receipt.js +2 -0
  136. package/dist/src/policy/hitl.js +11 -3
  137. package/dist/src/providers/context-output-ownership.js +26 -0
  138. package/dist/src/providers/deployment-ownership.d.mts +19 -0
  139. package/dist/src/providers/deployment-ownership.mjs +226 -0
  140. package/dist/src/providers/deployment-removal.js +200 -0
  141. package/dist/src/providers/provider-context-contracts.js +162 -0
  142. package/dist/src/providers/provider-definition-schema.js +136 -0
  143. package/dist/src/providers/provider-definitions.js +8 -1298
  144. package/dist/src/providers/provider-seeds-1.js +360 -0
  145. package/dist/src/providers/provider-seeds-2.js +387 -0
  146. package/dist/src/providers/provider-seeds-3.js +262 -0
  147. package/dist/src/providers/removal-transaction.js +86 -0
  148. package/dist/src/providers/settings-document.d.mts +3 -0
  149. package/dist/src/providers/settings-document.mjs +47 -0
  150. package/dist/src/providers/settings-removal.js +190 -0
  151. package/dist/src/providers/skill-ownership.d.mts +19 -2
  152. package/dist/src/providers/skill-ownership.mjs +12 -4
  153. package/dist/src/providers/transformation-receipt-integration.js +15 -271
  154. package/dist/src/providers/transformation-receipt-outputs.js +99 -0
  155. package/dist/src/providers/transformation-receipt-source.js +194 -0
  156. package/dist/src/research/clients/arxiv.js +4 -30
  157. package/dist/src/research/clients/base.js +48 -22
  158. package/dist/src/research/identity.js +42 -4
  159. package/dist/src/research/query-cli.js +3 -87
  160. package/dist/src/research/query-options.js +87 -0
  161. package/dist/src/research/reference-match.js +105 -24
  162. package/dist/src/research/services/acquisition.js +9 -7
  163. package/dist/src/research/services/citation.js +5 -2
  164. package/dist/src/research/types.js +2 -0
  165. package/dist/src/rules/activation.mjs +15 -6
  166. package/dist/src/rules/core-startup.mjs +2 -2
  167. package/dist/src/serve/mission-conductor.js +52 -4
  168. package/dist/src/serve/mission-summary.js +56 -0
  169. package/dist/src/sessions/adapters/grok-build-native.js +23 -0
  170. package/dist/src/sessions/adapters/grok-build.js +14 -7
  171. package/dist/src/sessions/workspace-discovery.js +5 -5
  172. package/dist/src/smiths/context-pipeline/aiwg-md.js +39 -46
  173. package/dist/src/smiths/context-pipeline/claude-hook.js +1 -1
  174. package/dist/src/smiths/context-pipeline/code-regions.js +28 -0
  175. package/dist/src/smiths/context-pipeline/context-file-safety.js +3 -0
  176. package/dist/src/smiths/context-pipeline/finalization.js +81 -51
  177. package/dist/src/smiths/context-pipeline/generator.js +10 -11
  178. package/dist/src/smiths/context-pipeline/managed-hook.js +1 -1
  179. package/dist/src/smiths/context-pipeline/muse-bridge.js +2 -2
  180. package/dist/src/smiths/context-pipeline/provider-policy.js +6 -12
  181. package/dist/src/smiths/context-pipeline/workspace-audit.js +299 -0
  182. package/dist/src/smiths/context-pipeline/workspace-bootstrap.js +148 -0
  183. package/dist/src/smiths/context-pipeline/workspace-context-shared.js +111 -0
  184. package/dist/src/smiths/context-pipeline/workspace-context.js +8 -1373
  185. package/dist/src/smiths/context-pipeline/workspace-doctor.js +192 -0
  186. package/dist/src/smiths/context-pipeline/workspace-document.js +201 -0
  187. package/dist/src/smiths/context-pipeline/workspace-extraction.js +205 -0
  188. package/dist/src/smiths/context-pipeline/workspace-migration.js +250 -0
  189. package/dist/src/smiths/hook-bridge/index.js +9 -0
  190. package/package.json +13 -1
  191. package/schemas/decision/D17StudyProtocol.v1.schema.json +39 -67
  192. package/schemas/decision/D29Study.v10.schema.json +65 -0
  193. package/tools/agents/deploy-agents.mjs +24 -675
  194. package/tools/agents/deploy-command-mirrors.mjs +229 -0
  195. package/tools/agents/deploy-options.mjs +258 -0
  196. package/tools/agents/deploy-support.mjs +165 -0
  197. package/tools/agents/providers/base-addon-discovery.mjs +139 -0
  198. package/tools/agents/providers/base-behaviors.mjs +168 -0
  199. package/tools/agents/providers/base-context.mjs +205 -0
  200. package/tools/agents/providers/base-contract.mjs +116 -0
  201. package/tools/agents/providers/base-files.mjs +252 -0
  202. package/tools/agents/providers/base-flat-artifacts.mjs +233 -0
  203. package/tools/agents/providers/base-framework-discovery.mjs +309 -0
  204. package/tools/agents/providers/base-frontmatter.mjs +196 -0
  205. package/tools/agents/providers/base-inventory.mjs +269 -0
  206. package/tools/agents/providers/base-kernel-skills.mjs +354 -0
  207. package/tools/agents/providers/base-migrations.mjs +258 -0
  208. package/tools/agents/providers/base-models-platforms.mjs +273 -0
  209. package/tools/agents/providers/base-rule-index.mjs +450 -0
  210. package/tools/agents/providers/base-runtime.mjs +60 -0
  211. package/tools/agents/providers/base-skill-copy.mjs +273 -0
  212. package/tools/agents/providers/base.mjs +16 -3364
  213. package/tools/agents/providers/claude-post-deploy.mjs +20 -1
  214. package/tools/agents/providers/context-write.mjs +4 -0
  215. package/tools/agents/providers/hermes-startup.mjs +0 -5
  216. package/tools/agents/providers/muse.mjs +7 -2
  217. package/tools/agents/providers/pi.mjs +3 -1
  218. package/tools/agents/providers/rule-delivery-surface.mjs +359 -0
  219. package/tools/agents/providers/rule-delivery.mjs +20 -1144
  220. package/tools/agents/providers/rule-deployment-commit.mjs +148 -0
  221. package/tools/agents/providers/rule-deployment-prepare.mjs +461 -0
  222. package/tools/agents/providers/rule-deployment-state.mjs +300 -0
  223. package/tools/agents/providers/rule-projection.mjs +3 -0
  224. package/tools/daemon/README.md +511 -0
  225. package/tools/daemon/adapters/base-adapter.mjs +84 -0
  226. package/tools/daemon/adapters/webhook-adapter.mjs +107 -0
  227. package/tools/daemon/agent-supervisor.mjs +299 -0
  228. package/tools/daemon/automation-engine.mjs +310 -0
  229. package/tools/daemon/autonomous-engine.mjs +346 -0
  230. package/tools/daemon/behavior-loader.mjs +316 -0
  231. package/tools/daemon/behavior.mjs +174 -0
  232. package/tools/daemon/concierge/intent-router.mjs +401 -0
  233. package/tools/daemon/concierge/orchestrator.mjs +272 -0
  234. package/tools/daemon/concierge/response-translator.mjs +462 -0
  235. package/tools/daemon/config.mjs +377 -0
  236. package/tools/daemon/cron-scheduler.mjs +178 -0
  237. package/tools/daemon/daemon-main.mjs +1169 -0
  238. package/tools/daemon/event-router.mjs +84 -0
  239. package/tools/daemon/file-watcher.mjs +383 -0
  240. package/tools/daemon/index.mjs +1181 -0
  241. package/tools/daemon/init-profile.mjs +124 -0
  242. package/tools/daemon/ipc-client.mjs +197 -0
  243. package/tools/daemon/ipc-ops-handlers.mjs +236 -0
  244. package/tools/daemon/ipc-server.mjs +275 -0
  245. package/tools/daemon/maintenance-owner.mjs +79 -0
  246. package/tools/daemon/memory-manager.mjs +386 -0
  247. package/tools/daemon/message-router.mjs +194 -0
  248. package/tools/daemon/provider-watcher.mjs +585 -0
  249. package/tools/daemon/pty-adapter.mjs +420 -0
  250. package/tools/daemon/repl-chat.mjs +359 -0
  251. package/tools/daemon/resource-bounds.mjs +88 -0
  252. package/tools/daemon/sandbox-transport.mjs +295 -0
  253. package/tools/daemon/scheduled-task-runner.mjs +535 -0
  254. package/tools/daemon/task-store.mjs +450 -0
  255. package/tools/daemon/tmux-manager.mjs +324 -0
  256. package/tools/daemon/update-policy.mjs +517 -0
  257. package/tools/daemon/web-server.mjs +399 -0
  258. package/tools/daemon/web-ui/index.html +585 -0
  259. package/tools/daemon/web-ui/styles.css +156 -0
  260. package/tools/docker/Dockerfile.daemon +35 -0
  261. package/tools/docker/container-manager.mjs +291 -0
  262. package/tools/messaging/adapter-registry.mjs +230 -0
  263. package/tools/messaging/adapters/base.mjs +326 -0
  264. package/tools/messaging/adapters/discord.mjs +462 -0
  265. package/tools/messaging/adapters/slack.mjs +402 -0
  266. package/tools/messaging/adapters/telegram.mjs +552 -0
  267. package/tools/messaging/chat-handler.mjs +297 -0
  268. package/tools/messaging/command-router.mjs +269 -0
  269. package/tools/messaging/event-bus.mjs +199 -0
  270. package/tools/messaging/index.mjs +456 -0
  271. package/tools/messaging/message-formatter.mjs +320 -0
  272. package/tools/messaging/room-manager.mjs +277 -0
  273. package/tools/messaging/types.mjs +94 -0
  274. package/tools/providers/pi-bridge-policy.mjs +4 -0
  275. package/tools/ralph-external/daemon-supervisor.mjs +394 -0
  276. package/tools/ralph-external/lib/behavior-registry.mjs +270 -0
  277. package/tools/ralph-external/lib/worker-invocation-processes.mjs +7 -4
  278. package/tools/ralph-external/lib/worker-isolation-args.mjs +217 -0
  279. package/tools/ralph-external/lib/worker-isolation-status.mjs +19 -0
  280. package/tools/ralph-external/lib/worker-isolation.mjs +11 -237
  281. package/tools/ralph-external/lib/worker-overflow.mjs +10 -2
  282. package/tools/ralph-external/lib/worker-termination.mjs +15 -8
  283. package/tools/ralph-external/lib/worker-unit-cleanup.mjs +72 -0
  284. package/tools/ralph-external/session-launcher.mjs +7 -7
  285. package/tools/scripts/resource-preflight.mjs +3 -242
  286. package/vscode-extension/schemas/aiwg.config.v1.json +2 -4
@@ -0,0 +1,217 @@
1
+ // Build and probe the systemd worker launch contract without starting workers.
2
+ import { spawnSync } from 'node:child_process';
3
+ import { buildSystemdClientEnv, resolveSystemdClient, resolveSystemdRun, CONNECTION_NAMES, unitEnvironmentNames } from './systemd-client.mjs';
4
+
5
+ const UNIT_SAFE = /[^A-Za-z0-9:_.@-]/g;
6
+ const transportBootstrapVariables = ['NODE_OPTIONS', 'NODE_PATH', 'LD_PRELOAD', 'LD_LIBRARY_PATH', 'LD_AUDIT', 'LD_DEBUG', 'LD_DEBUG_OUTPUT'];
7
+
8
+ /** Derive a unit-safe `aiwg-worker-<name>-<epoch>` unit name. */
9
+ export function proposeWorkerUnitName(name, nowSeconds = Math.floor(Date.now() / 1000)) {
10
+ const stem = String(name ?? 'worker').replace(UNIT_SAFE, '-').replace(/^-+|-+$/g, '').slice(0, 64) || 'worker';
11
+ return `aiwg-worker-${stem}-${nowSeconds}`;
12
+ }
13
+
14
+ export function assertAbsolute(label, value) {
15
+ if (typeof value !== 'string' || !value.startsWith('/') || value.includes('\0')) {
16
+ throw new Error(`Worker isolation requires an absolute NUL-free ${label}`);
17
+ }
18
+ }
19
+
20
+ /**
21
+ * Detect the worker isolation mode without launching anything.
22
+ * `{ spawn }` is injectable so tests never touch the real user manager.
23
+ * Returns `{ mode: 'systemd-unit' | 'degraded', reason, detail? }`.
24
+ */
25
+ export function detectWorkerIsolation({ spawn = spawnSync } = {}) {
26
+ if (process.platform !== 'linux') {
27
+ return { mode: 'degraded', reason: `systemd user isolation requires Linux (this host: ${process.platform})` };
28
+ }
29
+ let runner;
30
+ try {
31
+ runner = spawn(resolveSystemdRun(), ['--version'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
32
+ } catch (error) {
33
+ return { mode: 'degraded', reason: `systemd-run is unavailable: ${error.message}` };
34
+ }
35
+ if (runner.error || runner.status !== 0) {
36
+ return { mode: 'degraded', reason: 'systemd-run is unavailable (no --version response)' };
37
+ }
38
+ let manager;
39
+ try {
40
+ manager = spawn(resolveSystemdClient('systemctl'), ['--user', 'show', '-p', 'Version', '--value'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
41
+ } catch (error) {
42
+ return { mode: 'degraded', reason: `systemd user manager is unreachable: ${error.message}` };
43
+ }
44
+ if (manager.error || manager.status !== 0) {
45
+ return { mode: 'degraded', reason: 'systemd user manager is unreachable (systemctl --user failed)' };
46
+ }
47
+ return { mode: 'systemd-unit', reason: 'systemd user manager is reachable', detail: String(manager.stdout ?? '').trim() };
48
+ }
49
+
50
+ /**
51
+ * Build the `systemd-run --user` argv for one isolated worker. Pure and
52
+ * fully unit-tested. `kind` is `'unit'` (default, matches the field
53
+ * prototype: own unit retained for post-exit status inspection) or `'scope'`.
54
+ * Failed units require explicit `systemctl --user reset-failed <unit>` cleanup
55
+ * after Result/ExecMainStatus have been recorded; --collect would erase them.
56
+ * `env` supplies passthrough values (defaults to the conductor's current
57
+ * `PATH`/`HOME`); only the allowlisted `envNames` are forwarded, and their
58
+ * values are never written into argv. Each is requested with bare
59
+ * `--setenv=NAME` (no `=value`), which (systemd >= 230; confirmed on this
60
+ * host's systemd 259) imports the value from systemd-run's *own* invoking
61
+ * process environment rather than rendering it into the unit's argv — so a
62
+ * secret value never appears in `/proc/<systemd-run-pid>/cmdline`. That
63
+ * import is additive to whatever the user manager already injects into every
64
+ * unit (`systemctl --user show-environment`: LANG, XDG_*, SSH_AUTH_SOCK,
65
+ * etc. on an interactive manager), so `managerEnvNames` — the caller's own
66
+ * live snapshot of that list — drives a single `-p UnsetEnvironment=...`
67
+ * removing every manager-injected name that is not on the allowlist, plus
68
+ * all client-only connection names. Bootstrap controls are refused.
69
+ * Scopes ignore UnsetEnvironment, so launchIsolatedWorker strips connection
70
+ * names with the private wrapper used for attached units.
71
+ *
72
+ * `attached: true` (#2892 B2 / #2895) is `kind: 'unit'` plus `--wait --pipe
73
+ * --quiet`: systemd-run itself stays attached for the lifetime of the unit,
74
+ * connecting the unit's stdin/stdout/stderr directly to systemd-run's own
75
+ * (i.e. to whatever stdio the *caller* gave the systemd-run child process),
76
+ * and systemd-run's own exit mirrors the unit's exit status. This is how an
77
+ * MCP stdio worker can be launched resource-confined while still giving the
78
+ * conductor a live, bidirectional stdio stream to speak JSON-RPC over — the
79
+ * same resource ceiling (`MemoryMax`/`MemorySwapMax`) and environment-
80
+ * allowlist contract as the fire-and-forget `'unit'` path, just attached
81
+ * instead of detached, plus the transport-bootstrap hardening
82
+ * (`KillMode=control-group`/`ExitType=cgroup`/`UMask=0077`, refusal of
83
+ * `NODE_OPTIONS`/`LD_PRELOAD`/etc. in the allowlist) `launchIsolatedWorker`
84
+ * layers on top. Attached is therefore incompatible with `stdinPath`/
85
+ * `stdoutPath`/`stderrPath`/`logFile` (those redirect to files; `--pipe`
86
+ * redirects to the systemd-run client's own stdio) — passing any of them
87
+ * with `attached: true` is refused rather than silently ignored.
88
+ *
89
+ * `pipe`/`wait` are separate boolean flags (independent of `attached`) for a
90
+ * synchronous verification caller that wants `--pipe`/`--wait` (and, when
91
+ * both are set, the mandatory `--quiet` below) without the attached/async
92
+ * child contract — see `verification-isolation.mjs`.
93
+ *
94
+ * `stdinPath`, when given, feeds the worker's stdin from that file
95
+ * (`StandardInputFile=`) instead of the default `StandardInput=null` — this
96
+ * is how a resolved #2887 invocation's brief reaches the worker. `stdoutPath`
97
+ * / `stderrPath`, when given, route stdout/stderr to distinct files; either
98
+ * falls back to the combined `logFile` when omitted, so existing combined-log
99
+ * callers are unaffected. Both use systemd's `truncate:` output mode
100
+ * (systemd >= 248) rather than `file:` (which appends with O_APPEND), so a
101
+ * pre-existing file at that path is replaced, not appended to.
102
+ */
103
+ export function buildWorkerIsolationArgs({
104
+ unit,
105
+ memoryMaxGb,
106
+ command,
107
+ args = [],
108
+ cwd,
109
+ logFile,
110
+ stdinPath,
111
+ stdoutPath,
112
+ stderrPath,
113
+ env = process.env,
114
+ envNames = ['PATH', 'HOME'],
115
+ managerEnvNames = [],
116
+ kind = 'unit',
117
+ extraProperties = [],
118
+ pipe = false,
119
+ wait = false,
120
+ memoryMax,
121
+ attached = false,
122
+ } = {}) {
123
+ if (typeof unit !== 'string' || !/^[A-Za-z0-9:_.@-]+$/.test(unit)) {
124
+ throw new Error('Worker isolation requires a unit-safe unit name');
125
+ }
126
+ const hasMemoryMaxGb = memoryMaxGb !== undefined;
127
+ if (hasMemoryMaxGb && (!Number.isFinite(memoryMaxGb) || memoryMaxGb <= 0)) {
128
+ throw new Error('Worker isolation requires a positive memoryMaxGb ceiling');
129
+ }
130
+ if (!hasMemoryMaxGb && memoryMax === undefined) {
131
+ throw new Error('Worker isolation requires a positive memoryMaxGb ceiling');
132
+ }
133
+ assertAbsolute('worker binary', command);
134
+ for (const token of args) {
135
+ if (typeof token !== 'string' || token.includes('\0')) throw new Error('Worker isolation argv must be NUL-free strings');
136
+ }
137
+ if (kind !== 'unit' && kind !== 'scope') throw new Error("Worker isolation kind must be 'unit' or 'scope'");
138
+ if (attached && (stdinPath !== undefined || stdoutPath !== undefined || stderrPath !== undefined || logFile !== undefined)) {
139
+ throw new Error('Worker isolation attached mode connects stdio directly through systemd-run and cannot also redirect stdinPath/stdoutPath/stderrPath/logFile');
140
+ }
141
+ if (cwd !== undefined) assertAbsolute('working directory', cwd);
142
+ if (logFile !== undefined) assertAbsolute('log file', logFile);
143
+ if (stdinPath !== undefined) assertAbsolute('stdin brief', stdinPath);
144
+ if (stdoutPath !== undefined) assertAbsolute('stdout path', stdoutPath);
145
+ if (stderrPath !== undefined) assertAbsolute('stderr path', stderrPath);
146
+ if (!Array.isArray(extraProperties) || extraProperties.some(entry => typeof entry !== 'string' || entry.includes('\0'))) {
147
+ throw new Error('Worker isolation extraProperties must be an array of NUL-free "Name=value" strings');
148
+ }
149
+
150
+ const argv = ['systemd-run', '--user', '--expand-environment=no'];
151
+ if (attached) {
152
+ if (kind !== 'unit' || logFile !== undefined) throw new Error('Attached worker requires a unit with separate pipe captures');
153
+ argv.push('--wait', '--pipe', '--quiet');
154
+ argv.push('-p', 'KillMode=control-group', '-p', 'ExitType=cgroup', '-p', 'UMask=0077',
155
+ '-p', `UnsetEnvironment=${transportBootstrapVariables.join(' ')}`);
156
+ }
157
+ if (kind === 'unit') argv.push(`--unit=${unit}`);
158
+ else argv.push('--scope');
159
+ // Attached launches already pass --wait --pipe --quiet above.
160
+ if (pipe && !attached) argv.push('--pipe');
161
+ if (wait && !attached) argv.push('--wait');
162
+ // --pipe connects the unit's stdio directly to systemd-run's own, but
163
+ // systemd-run still writes its *own* "Running as unit:.../Finished with
164
+ // result:..." status lines to that same stderr unless --quiet is given —
165
+ // verified empirically on this host. A synchronous verification caller
166
+ // needs the check's actual stderr, not systemd-run's narration mixed into
167
+ // it, so --quiet is mandatory whenever both --pipe and --wait are used.
168
+ if (pipe && wait && !attached) argv.push('--quiet');
169
+ const requestedNames = [];
170
+ for (const name of unitEnvironmentNames(envNames)) {
171
+ const value = env?.[name];
172
+ if (value === undefined) continue;
173
+ if (typeof value !== 'string' || value.includes('\0')) throw new Error(`Worker isolation environment value for ${name} is not a NUL-free string`);
174
+ requestedNames.push(name);
175
+ argv.push(`--setenv=${name}`);
176
+ }
177
+ if (kind === 'unit') {
178
+ const toUnset = [...new Set([...managerEnvNames, ...CONNECTION_NAMES])].filter(name => !requestedNames.includes(name));
179
+ if (toUnset.length > 0) argv.push('-p', `UnsetEnvironment=${toUnset.join(' ')}`);
180
+ }
181
+ // stdin is /dev/null unless a brief file is given; attached (supervised)
182
+ // launches rely on systemd-run --pipe to carry stdin, so leave it unset.
183
+ if (stdinPath !== undefined) argv.push('-p', `StandardInputFile=${stdinPath}`);
184
+ else if (!attached) argv.push('-p', 'StandardInput=null');
185
+ if (hasMemoryMaxGb) argv.push('-p', `MemoryMax=${memoryMaxGb}G`);
186
+ else if (memoryMax !== undefined) argv.push('-p', `MemoryMax=${memoryMax}`);
187
+ argv.push('-p', 'MemorySwapMax=0');
188
+ const outPath = stdoutPath ?? logFile;
189
+ const errPath = stderrPath ?? logFile;
190
+ if (!pipe && outPath !== undefined) argv.push('-p', `StandardOutput=truncate:${outPath}`);
191
+ if (!pipe && errPath !== undefined) argv.push('-p', `StandardError=truncate:${errPath}`);
192
+ // Additional conductor-supplied confinement properties (ReadOnlyPaths,
193
+ // ReadWritePaths, NoNewPrivileges, PrivateUsers, PrivateNetwork, ...),
194
+ // appended verbatim as repeated `-p Name=value` pairs. Callers are
195
+ // responsible for probing which of these this host's systemd/AppArmor
196
+ // policy actually enforces (see verification-isolation.mjs) — some
197
+ // properties silently no-op or fall back without a non-zero exit on
198
+ // kernels/policies that deny the namespace privilege they need.
199
+ for (const property of extraProperties) argv.push('-p', property);
200
+ if (cwd !== undefined) argv.push(`--working-directory=${cwd}`);
201
+ argv.push('--', command, ...args);
202
+ return argv;
203
+ }
204
+
205
+ /**
206
+ * Query the user manager's own inherited-environment variable names
207
+ * (`systemctl --user show-environment`), required so a `kind: 'unit'`
208
+ * launch can `UnsetEnvironment=` every manager-injected name that is not on
209
+ * the caller's allowlist (see `buildWorkerIsolationArgs`' doc comment).
210
+ * Extracted from `launchIsolatedWorker` so verification callers can reuse
211
+ * the exact same security-relevant query instead of re-deriving it.
212
+ */
213
+ export function resolveManagerEnvNames({ spawn = spawnSync } = {}) {
214
+ const shown = spawn(resolveSystemdClient('systemctl'), ['--user', 'show-environment'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
215
+ if (shown.error || shown.status !== 0) throw new Error(shown.error?.message ?? (String(shown.stderr ?? '').trim() || `exit ${shown.status}`));
216
+ return String(shown.stdout ?? '').split('\n').map(line => line.slice(0, line.indexOf('='))).filter(Boolean);
217
+ }
@@ -46,3 +46,22 @@ export function getWorkerIsolationStatus(unit, { spawn = spawnSync, timeoutMs =
46
46
  ...(fields.ExecMainCode ? { execMainCode: Number(fields.ExecMainCode) } : {}),
47
47
  };
48
48
  }
49
+
50
+ /**
51
+ * Read the kernel-reported `ControlGroup` systemd placed this unit's
52
+ * processes under. This is the containment authority for `systemd-unit`
53
+ * mode worker identity (#2895 round 2, MAJOR a): a candidate pid is only
54
+ * trustworthy if its own `/proc/<pid>/cgroup` matches this value, never
55
+ * because a worker-writable record merely names a pid that happens to
56
+ * resample successfully. Returns null (never a forged match) when the
57
+ * property is unavailable.
58
+ */
59
+ export function readUnitControlGroup(unit, { spawn = spawnSync } = {}) {
60
+ if (typeof unit !== 'string' || !/^[A-Za-z0-9:_.@-]+$/.test(unit)) return null;
61
+ try {
62
+ const shown = spawn(resolveSystemdClient('systemctl'), ['--user', 'show', unit, '-p', 'ControlGroup', '--value'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
63
+ if (shown.error || shown.status !== 0) return null;
64
+ const value = String(shown.stdout ?? '').trim();
65
+ return value || null;
66
+ } catch { return null; }
67
+ }
@@ -38,224 +38,14 @@ import { constants, openSync, closeSync, writeFileSync, fsyncSync, renameSync, l
38
38
  import path from 'node:path';
39
39
  import { sampleWorkerIdentity, createWorkerIdentityCapture } from '../process-monitor.mjs';
40
40
  import { checkWorkerDispatch } from '../../scripts/resource-preflight.mjs';
41
- import { buildSystemdClientEnv, resolveSystemdClient, resolveSystemdRun, CONNECTION_NAMES, unitEnvironmentNames, isClientControlVariable } from './systemd-client.mjs';
41
+ import { buildSystemdClientEnv, resolveSystemdClient, resolveSystemdRun, unitEnvironmentNames, isClientControlVariable } from './systemd-client.mjs';
42
42
 
43
- const UNIT_SAFE = /[^A-Za-z0-9:_.@-]/g;
44
- const transportBootstrapVariables = ['NODE_OPTIONS', 'NODE_PATH', 'LD_PRELOAD', 'LD_LIBRARY_PATH', 'LD_AUDIT', 'LD_DEBUG', 'LD_DEBUG_OUTPUT'];
43
+ import { assertAbsolute, proposeWorkerUnitName, detectWorkerIsolation, buildWorkerIsolationArgs, resolveManagerEnvNames } from './worker-isolation-args.mjs';
44
+ import { createWorkerUnitOwner } from './worker-unit-cleanup.mjs';
45
+ export { proposeWorkerUnitName, detectWorkerIsolation, buildWorkerIsolationArgs, resolveManagerEnvNames };
45
46
  export { NATIVE_SANDBOX_UNAVAILABLE, workerNeedsNativeNamespaces, nativeSandboxFailure,
46
47
  nativeSandboxProbeMessage, probeWorkerNativeSandbox } from './worker-native-sandbox.mjs';
47
48
 
48
- /** Derive a unit-safe `aiwg-worker-<name>-<epoch>` unit name. */
49
- export function proposeWorkerUnitName(name, nowSeconds = Math.floor(Date.now() / 1000)) {
50
- const stem = String(name ?? 'worker').replace(UNIT_SAFE, '-').replace(/^-+|-+$/g, '').slice(0, 64) || 'worker';
51
- return `aiwg-worker-${stem}-${nowSeconds}`;
52
- }
53
-
54
- function assertAbsolute(label, value) {
55
- if (typeof value !== 'string' || !value.startsWith('/') || value.includes('\0')) {
56
- throw new Error(`Worker isolation requires an absolute NUL-free ${label}`);
57
- }
58
- }
59
-
60
- /**
61
- * Detect the worker isolation mode without launching anything.
62
- * `{ spawn }` is injectable so tests never touch the real user manager.
63
- * Returns `{ mode: 'systemd-unit' | 'degraded', reason, detail? }`.
64
- */
65
- export function detectWorkerIsolation({ spawn = spawnSync } = {}) {
66
- if (process.platform !== 'linux') {
67
- return { mode: 'degraded', reason: `systemd user isolation requires Linux (this host: ${process.platform})` };
68
- }
69
- let runner;
70
- try {
71
- runner = spawn(resolveSystemdRun(), ['--version'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
72
- } catch (error) {
73
- return { mode: 'degraded', reason: `systemd-run is unavailable: ${error.message}` };
74
- }
75
- if (runner.error || runner.status !== 0) {
76
- return { mode: 'degraded', reason: 'systemd-run is unavailable (no --version response)' };
77
- }
78
- let manager;
79
- try {
80
- manager = spawn(resolveSystemdClient('systemctl'), ['--user', 'show', '-p', 'Version', '--value'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
81
- } catch (error) {
82
- return { mode: 'degraded', reason: `systemd user manager is unreachable: ${error.message}` };
83
- }
84
- if (manager.error || manager.status !== 0) {
85
- return { mode: 'degraded', reason: 'systemd user manager is unreachable (systemctl --user failed)' };
86
- }
87
- return { mode: 'systemd-unit', reason: 'systemd user manager is reachable', detail: String(manager.stdout ?? '').trim() };
88
- }
89
-
90
- /**
91
- * Build the `systemd-run --user` argv for one isolated worker. Pure and
92
- * fully unit-tested. `kind` is `'unit'` (default, matches the field
93
- * prototype: own unit retained for post-exit status inspection) or `'scope'`.
94
- * Failed units require explicit `systemctl --user reset-failed <unit>` cleanup
95
- * after Result/ExecMainStatus have been recorded; --collect would erase them.
96
- * `env` supplies passthrough values (defaults to the conductor's current
97
- * `PATH`/`HOME`); only the allowlisted `envNames` are forwarded, and their
98
- * values are never written into argv. Each is requested with bare
99
- * `--setenv=NAME` (no `=value`), which (systemd >= 230; confirmed on this
100
- * host's systemd 259) imports the value from systemd-run's *own* invoking
101
- * process environment rather than rendering it into the unit's argv — so a
102
- * secret value never appears in `/proc/<systemd-run-pid>/cmdline`. That
103
- * import is additive to whatever the user manager already injects into every
104
- * unit (`systemctl --user show-environment`: LANG, XDG_*, SSH_AUTH_SOCK,
105
- * etc. on an interactive manager), so `managerEnvNames` — the caller's own
106
- * live snapshot of that list — drives a single `-p UnsetEnvironment=...`
107
- * removing every manager-injected name that is not on the allowlist, plus
108
- * all client-only connection names. Bootstrap controls are refused.
109
- * Scopes ignore UnsetEnvironment, so launchIsolatedWorker strips connection
110
- * names with the private wrapper used for attached units.
111
- *
112
- * `attached: true` (#2892 B2 / #2895) is `kind: 'unit'` plus `--wait --pipe
113
- * --quiet`: systemd-run itself stays attached for the lifetime of the unit,
114
- * connecting the unit's stdin/stdout/stderr directly to systemd-run's own
115
- * (i.e. to whatever stdio the *caller* gave the systemd-run child process),
116
- * and systemd-run's own exit mirrors the unit's exit status. This is how an
117
- * MCP stdio worker can be launched resource-confined while still giving the
118
- * conductor a live, bidirectional stdio stream to speak JSON-RPC over — the
119
- * same resource ceiling (`MemoryMax`/`MemorySwapMax`) and environment-
120
- * allowlist contract as the fire-and-forget `'unit'` path, just attached
121
- * instead of detached, plus the transport-bootstrap hardening
122
- * (`KillMode=control-group`/`ExitType=cgroup`/`UMask=0077`, refusal of
123
- * `NODE_OPTIONS`/`LD_PRELOAD`/etc. in the allowlist) `launchIsolatedWorker`
124
- * layers on top. Attached is therefore incompatible with `stdinPath`/
125
- * `stdoutPath`/`stderrPath`/`logFile` (those redirect to files; `--pipe`
126
- * redirects to the systemd-run client's own stdio) — passing any of them
127
- * with `attached: true` is refused rather than silently ignored.
128
- *
129
- * `pipe`/`wait` are separate boolean flags (independent of `attached`) for a
130
- * synchronous verification caller that wants `--pipe`/`--wait` (and, when
131
- * both are set, the mandatory `--quiet` below) without the attached/async
132
- * child contract — see `verification-isolation.mjs`.
133
- *
134
- * `stdinPath`, when given, feeds the worker's stdin from that file
135
- * (`StandardInputFile=`) instead of the default `StandardInput=null` — this
136
- * is how a resolved #2887 invocation's brief reaches the worker. `stdoutPath`
137
- * / `stderrPath`, when given, route stdout/stderr to distinct files; either
138
- * falls back to the combined `logFile` when omitted, so existing combined-log
139
- * callers are unaffected. Both use systemd's `truncate:` output mode
140
- * (systemd >= 248) rather than `file:` (which appends with O_APPEND), so a
141
- * pre-existing file at that path is replaced, not appended to.
142
- */
143
- export function buildWorkerIsolationArgs({
144
- unit,
145
- memoryMaxGb,
146
- command,
147
- args = [],
148
- cwd,
149
- logFile,
150
- stdinPath,
151
- stdoutPath,
152
- stderrPath,
153
- env = process.env,
154
- envNames = ['PATH', 'HOME'],
155
- managerEnvNames = [],
156
- kind = 'unit',
157
- extraProperties = [],
158
- pipe = false,
159
- wait = false,
160
- memoryMax,
161
- attached = false,
162
- } = {}) {
163
- if (typeof unit !== 'string' || !/^[A-Za-z0-9:_.@-]+$/.test(unit)) {
164
- throw new Error('Worker isolation requires a unit-safe unit name');
165
- }
166
- const hasMemoryMaxGb = memoryMaxGb !== undefined;
167
- if (hasMemoryMaxGb && (!Number.isFinite(memoryMaxGb) || memoryMaxGb <= 0)) {
168
- throw new Error('Worker isolation requires a positive memoryMaxGb ceiling');
169
- }
170
- if (!hasMemoryMaxGb && memoryMax === undefined) {
171
- throw new Error('Worker isolation requires a positive memoryMaxGb ceiling');
172
- }
173
- assertAbsolute('worker binary', command);
174
- for (const token of args) {
175
- if (typeof token !== 'string' || token.includes('\0')) throw new Error('Worker isolation argv must be NUL-free strings');
176
- }
177
- if (kind !== 'unit' && kind !== 'scope') throw new Error("Worker isolation kind must be 'unit' or 'scope'");
178
- if (attached && (stdinPath !== undefined || stdoutPath !== undefined || stderrPath !== undefined || logFile !== undefined)) {
179
- throw new Error('Worker isolation attached mode connects stdio directly through systemd-run and cannot also redirect stdinPath/stdoutPath/stderrPath/logFile');
180
- }
181
- if (cwd !== undefined) assertAbsolute('working directory', cwd);
182
- if (logFile !== undefined) assertAbsolute('log file', logFile);
183
- if (stdinPath !== undefined) assertAbsolute('stdin brief', stdinPath);
184
- if (stdoutPath !== undefined) assertAbsolute('stdout path', stdoutPath);
185
- if (stderrPath !== undefined) assertAbsolute('stderr path', stderrPath);
186
- if (!Array.isArray(extraProperties) || extraProperties.some(entry => typeof entry !== 'string' || entry.includes('\0'))) {
187
- throw new Error('Worker isolation extraProperties must be an array of NUL-free "Name=value" strings');
188
- }
189
-
190
- const argv = ['systemd-run', '--user', '--expand-environment=no'];
191
- if (attached) {
192
- if (kind !== 'unit' || logFile !== undefined) throw new Error('Attached worker requires a unit with separate pipe captures');
193
- argv.push('--wait', '--pipe', '--quiet');
194
- argv.push('-p', 'KillMode=control-group', '-p', 'ExitType=cgroup', '-p', 'UMask=0077',
195
- '-p', `UnsetEnvironment=${transportBootstrapVariables.join(' ')}`);
196
- }
197
- if (kind === 'unit') argv.push(`--unit=${unit}`);
198
- else argv.push('--scope');
199
- // Attached launches already pass --wait --pipe --quiet above.
200
- if (pipe && !attached) argv.push('--pipe');
201
- if (wait && !attached) argv.push('--wait');
202
- // --pipe connects the unit's stdio directly to systemd-run's own, but
203
- // systemd-run still writes its *own* "Running as unit:.../Finished with
204
- // result:..." status lines to that same stderr unless --quiet is given —
205
- // verified empirically on this host. A synchronous verification caller
206
- // needs the check's actual stderr, not systemd-run's narration mixed into
207
- // it, so --quiet is mandatory whenever both --pipe and --wait are used.
208
- if (pipe && wait && !attached) argv.push('--quiet');
209
- const requestedNames = [];
210
- for (const name of unitEnvironmentNames(envNames)) {
211
- const value = env?.[name];
212
- if (value === undefined) continue;
213
- if (typeof value !== 'string' || value.includes('\0')) throw new Error(`Worker isolation environment value for ${name} is not a NUL-free string`);
214
- requestedNames.push(name);
215
- argv.push(`--setenv=${name}`);
216
- }
217
- if (kind === 'unit') {
218
- const toUnset = [...new Set([...managerEnvNames, ...CONNECTION_NAMES])].filter(name => !requestedNames.includes(name));
219
- if (toUnset.length > 0) argv.push('-p', `UnsetEnvironment=${toUnset.join(' ')}`);
220
- }
221
- // stdin is /dev/null unless a brief file is given; attached (supervised)
222
- // launches rely on systemd-run --pipe to carry stdin, so leave it unset.
223
- if (stdinPath !== undefined) argv.push('-p', `StandardInputFile=${stdinPath}`);
224
- else if (!attached) argv.push('-p', 'StandardInput=null');
225
- if (hasMemoryMaxGb) argv.push('-p', `MemoryMax=${memoryMaxGb}G`);
226
- else if (memoryMax !== undefined) argv.push('-p', `MemoryMax=${memoryMax}`);
227
- argv.push('-p', 'MemorySwapMax=0');
228
- const outPath = stdoutPath ?? logFile;
229
- const errPath = stderrPath ?? logFile;
230
- if (!pipe && outPath !== undefined) argv.push('-p', `StandardOutput=truncate:${outPath}`);
231
- if (!pipe && errPath !== undefined) argv.push('-p', `StandardError=truncate:${errPath}`);
232
- // Additional conductor-supplied confinement properties (ReadOnlyPaths,
233
- // ReadWritePaths, NoNewPrivileges, PrivateUsers, PrivateNetwork, ...),
234
- // appended verbatim as repeated `-p Name=value` pairs. Callers are
235
- // responsible for probing which of these this host's systemd/AppArmor
236
- // policy actually enforces (see verification-isolation.mjs) — some
237
- // properties silently no-op or fall back without a non-zero exit on
238
- // kernels/policies that deny the namespace privilege they need.
239
- for (const property of extraProperties) argv.push('-p', property);
240
- if (cwd !== undefined) argv.push(`--working-directory=${cwd}`);
241
- argv.push('--', command, ...args);
242
- return argv;
243
- }
244
-
245
- /**
246
- * Query the user manager's own inherited-environment variable names
247
- * (`systemctl --user show-environment`), required so a `kind: 'unit'`
248
- * launch can `UnsetEnvironment=` every manager-injected name that is not on
249
- * the caller's allowlist (see `buildWorkerIsolationArgs`' doc comment).
250
- * Extracted from `launchIsolatedWorker` so verification callers can reuse
251
- * the exact same security-relevant query instead of re-deriving it.
252
- */
253
- export function resolveManagerEnvNames({ spawn = spawnSync } = {}) {
254
- const shown = spawn(resolveSystemdClient('systemctl'), ['--user', 'show-environment'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
255
- if (shown.error || shown.status !== 0) throw new Error(shown.error?.message ?? (String(shown.stderr ?? '').trim() || `exit ${shown.status}`));
256
- return String(shown.stdout ?? '').split('\n').map(line => line.slice(0, line.indexOf('='))).filter(Boolean);
257
- }
258
-
259
49
  /**
260
50
  * Gate, then launch one worker in its own resource scope (FR-OG-107/108).
261
51
  *
@@ -499,6 +289,7 @@ export function launchIsolatedWorker({
499
289
  };
500
290
  }
501
291
  const unit = proposeUnit(name);
292
+ const owner = createWorkerUnitOwner(unit, kind);
502
293
  // A private host-owned wrapper strips manager/client connection variables
503
294
  // before spawning the real worker. Environment values never enter argv.
504
295
  const wrapped = attached || kind === 'scope';
@@ -506,7 +297,7 @@ export function launchIsolatedWorker({
506
297
  const isolatedArgs = wrapped ? [fileURLToPath(import.meta.url), '--worker-environment', JSON.stringify(unitEnvironmentNames(envNames)),
507
298
  ...(runDir ? ['--run-dir', runDir, '--invocation-id', name] : []), '--', command, ...args] : args;
508
299
  const argv = buildWorkerIsolationArgs({ unit, memoryMaxGb, command: isolatedCommand, args: isolatedArgs,
509
- cwd, logFile, stdinPath, stdoutPath, stderrPath, env, envNames, managerEnvNames, kind, attached });
300
+ cwd, logFile, stdinPath, stdoutPath, stderrPath, env, envNames, managerEnvNames, kind, attached, extraProperties: owner.properties });
510
301
  // #2915 round 8 (D2): same dispatch-commit boundary as the degraded path
511
302
  // above — every systemd-unit preparation check (gate, manager-environment
512
303
  // query, argv construction) has already run by this point.
@@ -519,7 +310,7 @@ export function launchIsolatedWorker({
519
310
  const child = spawnAsync(systemdRun, launchArgs, launchOptions);
520
311
  const outcome = { launched: Number.isInteger(child.pid) && child.pid > 0, mode: 'systemd-unit', unit, child, gate: resolvedGate };
521
312
  child.once('error', error => { outcome.launched = false; outcome.error = error.message; });
522
- return outcome;
313
+ return owner.track(outcome);
523
314
  });
524
315
  }
525
316
  const launchArgs = argv.slice(1);
@@ -529,7 +320,7 @@ export function launchIsolatedWorker({
529
320
  const child = spawn(systemdRun, launchArgs, launchOptions);
530
321
  if (child.error || child.status !== 0) {
531
322
  const stderr = String(child.stderr ?? '').trim();
532
- return {
323
+ return owner.track({
533
324
  launched: false,
534
325
  mode: 'systemd-unit',
535
326
  unit,
@@ -542,7 +333,7 @@ export function launchIsolatedWorker({
542
333
  ...(onDispatchCommit ? { dispatchState: 'submission-uncertain' } : {}),
543
334
  diagnostic: `systemd-run launch failed: ${child.error?.message ?? stderr ?? `exit ${child.status}`}`,
544
335
  gate: resolvedGate,
545
- };
336
+ });
546
337
  }
547
338
  let pid;
548
339
  try {
@@ -552,7 +343,7 @@ export function launchIsolatedWorker({
552
343
  } catch {
553
344
  pid = undefined;
554
345
  }
555
- return { launched: true, mode: 'systemd-unit', unit, pid, gate: resolvedGate };
346
+ return owner.track({ launched: true, mode: 'systemd-unit', unit, pid, gate: resolvedGate });
556
347
  });
557
348
  }
558
349
 
@@ -596,24 +387,7 @@ export function killWorkerUnit(unit, { spawn = spawnSync, timeoutMs = 10000, sig
596
387
  diagnostic: result.error ? result.error.message : String(result.stderr ?? '').trim() || null };
597
388
  }
598
389
 
599
- /**
600
- * Read the kernel-reported `ControlGroup` systemd placed this unit's
601
- * processes under. This is the containment authority for `systemd-unit`
602
- * mode worker identity (#2895 round 2, MAJOR a): a candidate pid is only
603
- * trustworthy if its own `/proc/<pid>/cgroup` matches this value, never
604
- * because a worker-writable record merely names a pid that happens to
605
- * resample successfully. Returns null (never a forged match) when the
606
- * property is unavailable.
607
- */
608
- export function readUnitControlGroup(unit, { spawn = spawnSync } = {}) {
609
- if (typeof unit !== 'string' || !/^[A-Za-z0-9:_.@-]+$/.test(unit)) return null;
610
- try {
611
- const shown = spawn(resolveSystemdClient('systemctl'), ['--user', 'show', unit, '-p', 'ControlGroup', '--value'], { encoding: 'utf8', timeout: 10000, env: buildSystemdClientEnv({ envNames: [] }) });
612
- if (shown.error || shown.status !== 0) return null;
613
- const value = String(shown.stdout ?? '').trim();
614
- return value || null;
615
- } catch { return null; }
616
- }
390
+ export { readUnitControlGroup } from './worker-isolation-status.mjs';
617
391
 
618
392
  export { checkWorkerDispatch };
619
393
 
@@ -1,4 +1,5 @@
1
1
  /** Stop authority and bounded termination proof for MCP stdout overflow. */
2
+ import { recordWorkerUnitStop, readRecordedWorkerUnitStop } from './worker-unit-cleanup.mjs';
2
3
  import { importImpl } from '../../_resolve-impl.mjs';
3
4
  import { getWorkerIsolationStatus, killWorkerUnit, stopWorkerUnit } from './worker-isolation.mjs';
4
5
  import { signalOwnedWorker } from '../session-launcher.mjs';
@@ -21,11 +22,13 @@ export async function stopOverflowWorker(launch, {
21
22
  // Kill and stop are one unit-stop authority: a caller that substitutes the stop
22
23
  // also controls the kill, so the real systemctl never bypasses it.
23
24
  stopUnit = stopWorkerUnit, killUnit = stopUnit === stopWorkerUnit ? killWorkerUnit : () => ({ ok: false }),
24
- unitStatus = getWorkerIsolationStatus,
25
+ unitStatus = getWorkerIsolationStatus, recordUnitResult = recordWorkerUnitStop,
25
26
  sample = sampleWorkerIdentity, children = sampleWorkerChildren, platform = process.platform,
26
27
  signal = signalOwnedWorker, killPid = (pid, sig) => process.kill(pid, sig),
27
28
  clock = Date.now, sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),
28
29
  } = {}) {
30
+ const recorded = readRecordedWorkerUnitStop(launch);
31
+ if (recorded) return recorded;
29
32
  let delivered = false;
30
33
  const uncertain = diagnostic => ({ termination: 'uncertain', diagnostic, delivered });
31
34
  const deadline = clock() + deadlineMs;
@@ -76,7 +79,12 @@ export async function stopOverflowWorker(launch, {
76
79
  return uncertain('OG_WORKER_MCP_STOP_UNCONFIRMED');
77
80
  }
78
81
  for (;;) {
79
- try { if (!unitStatus(launch.unit, { timeoutMs: remaining() }).active) return { termination: 'confirmed', delivered, ...(diagnostic ? { diagnostic } : {}) }; }
82
+ const recorded = readRecordedWorkerUnitStop(launch);
83
+ if (recorded) return recorded;
84
+ try {
85
+ const native = unitStatus(launch.unit, { timeoutMs: remaining() });
86
+ if (!native.active) return recordUnitResult(launch, native, { termination: 'confirmed', delivered, ...(diagnostic ? { diagnostic } : {}) });
87
+ }
80
88
  catch { /* Retry unavailable/transient unit state until the deadline. */ }
81
89
  if (clock() >= deadline) break;
82
90
  await sleep(Math.min(pollIntervalMs, deadline - clock()));
@@ -3,9 +3,13 @@ import { sampleWorkerIdentity, isSafeWorkerGroupTarget } from '../process-monito
3
3
  import { getWorkerIsolationStatus } from './worker-isolation-status.mjs';
4
4
  import { scanWorkerInvocationProcesses } from './worker-invocation-processes.mjs';
5
5
 
6
+ const TERMINATION_BUDGET_MS = 2000;
7
+ const TERMINATION_ATTEMPT_MS = 100;
8
+ const TERMINATION_MAX_ATTEMPTS = 21;
9
+
6
10
  export function observeWorkerTermination({ mode, native, workerExited, identity,
7
11
  supervisor = sampleWorkerIdentity(process.pid), kill = process.kill, invocationId,
8
- scan = scanWorkerInvocationProcesses, deadline } = {}) {
12
+ scan = scanWorkerInvocationProcesses, deadline, now = () => performance.now() } = {}) {
9
13
  if (mode === 'systemd-unit') {
10
14
  const exited = workerExited === true || [1, 2, 3].includes(native?.execMainCode);
11
15
  return { confirmed: exited && native?.active === false && native?.cgroupEmpty === true,
@@ -25,7 +29,10 @@ export function observeWorkerTermination({ mode, native, workerExited, identity,
25
29
  // readable stat/cgroup links instead of failing closed on every
26
30
  // unreadable post-start process.
27
31
  if (error.code === 'ESRCH') {
28
- return scan({ invocationId, startTicks: identity.startTicks, uid: identity.uid, deadline,
32
+ // Exit-time callers get one scan, at most 2 s. An explicit deadline
33
+ // leaves the scan budget and retries to the recheck caller.
34
+ return scan({ invocationId, startTicks: identity.startTicks, uid: identity.uid, now,
35
+ deadline: deadline ?? now() + TERMINATION_BUDGET_MS,
29
36
  workerPid: identity.pid, workerPgid: identity.pgid, workerSession: identity.sessionId });
30
37
  }
31
38
  return { confirmed: false, missing: `empty owned process group (signal-0 returned ${error.code ?? 'unknown'})` };
@@ -57,10 +64,10 @@ export async function recheckWorkerTermination({ processRecord, exitRecord,
57
64
  if (processRecord.isolation !== 'systemd-unit' && processRecord.invocationMarker !== processRecord.invocationId) {
58
65
  return { confirmed: false, missing: 'escaped-descendant evidence: launcher-bound invocation marker in process.json' };
59
66
  }
60
- const deadline = now() + 2000;
67
+ const deadline = now() + TERMINATION_BUDGET_MS;
61
68
  // Both elapsed time and sample count bound the check, including injected clocks.
62
69
  let evidence;
63
- for (let attempt = 0; attempt < 21; attempt++) {
70
+ for (let attempt = 0; attempt < TERMINATION_MAX_ATTEMPTS; attempt++) {
64
71
  if (evidence && now() >= deadline) return evidence;
65
72
  let native = null;
66
73
  // Use the unit's retained exit state whenever a unit name is recorded,
@@ -68,15 +75,15 @@ export async function recheckWorkerTermination({ processRecord, exitRecord,
68
75
  // selection is unchanged, but unit (cgroup) evidence is preferred
69
76
  // wherever it is available. Non-unit modes still ignore it below.
70
77
  if (typeof processRecord.unit === 'string' && processRecord.unit !== '') {
71
- try { native = status(processRecord.unit, { timeoutMs: Math.max(1, Math.min(100, deadline - now())) }); }
78
+ try { native = status(processRecord.unit, { timeoutMs: Math.max(1, Math.min(TERMINATION_ATTEMPT_MS, deadline - now())) }); }
72
79
  catch { /* Unavailable is never empty. */ }
73
80
  }
74
81
  evidence = observe({ mode: processRecord.isolation, native, identity, invocationId: processRecord.invocationId,
75
- deadline: Math.min(deadline, now() + 100),
82
+ deadline: Math.min(deadline, now() + TERMINATION_ATTEMPT_MS), now,
76
83
  workerExited: Number.isInteger(exitRecord.exitCode) && exitRecord.exitCode >= 0 && exitRecord.exitCode <= 255
77
84
  || exitRecord.captureCoverage === 'complete' && exitRecord.workerTermination === 'confirmed' && /^SIG[A-Z0-9]+$/.test(exitRecord.signal ?? '') });
78
- if (evidence.confirmed || evidence.markerPresent || now() >= deadline || attempt === 20) return evidence;
79
- await wait(Math.min(100, Math.max(0, deadline - now())));
85
+ if (evidence.confirmed || evidence.markerPresent || now() >= deadline || attempt === TERMINATION_MAX_ATTEMPTS - 1) return evidence;
86
+ await wait(Math.min(TERMINATION_ATTEMPT_MS, Math.max(0, deadline - now())));
80
87
  }
81
88
  return evidence;
82
89
  }