@aiwg/cli 2026.10.15 → 2026.10.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agentic/code/providers/pi/aiwg-bridge.ts +45 -7
- package/dist/src/artifacts/citation-parser.js +7 -6
- package/dist/src/artifacts/corpus-tools/citation-densify.js +4 -2
- package/dist/src/artifacts/corpus-tools/cli.js +8 -3
- package/dist/src/artifacts/corpus-tools/discovery-log.js +26 -24
- package/dist/src/artifacts/corpus-tools/expand-cli.js +3 -2
- package/dist/src/artifacts/corpus-tools/expand-dates.js +107 -0
- package/dist/src/artifacts/corpus-tools/expand-queue.js +4 -1
- package/dist/src/artifacts/corpus-tools/expand-review.js +104 -0
- package/dist/src/artifacts/corpus-tools/expand-state.js +17 -1
- package/dist/src/artifacts/corpus-tools/expand.js +16 -17
- package/dist/src/artifacts/corpus-tools/induction-audit.js +7 -6
- package/dist/src/artifacts/corpus-tools/integrity-scan.js +3 -3
- package/dist/src/artifacts/corpus-tools/radar-shared.js +7 -9
- package/dist/src/artifacts/corpus-tools/ref-files.js +11 -0
- package/dist/src/artifacts/corpus-tools/resolve-cli.js +11 -3
- package/dist/src/artifacts/corpus-tools/resolve.js +23 -9
- package/dist/src/artifacts/corpus-tools/retrieval-lab.js +2 -1
- package/dist/src/artifacts/corpus-tools/sidecar-lint.js +4 -4
- package/dist/src/artifacts/corpus-tools/sidecar-repair.js +2 -1
- package/dist/src/artifacts/corpus-views/ref-parser.js +3 -17
- package/dist/src/artifacts/graph-query.js +5 -3
- package/dist/src/artifacts/hybrid-query.js +11 -2
- package/dist/src/artifacts/index-refresh.js +2 -0
- package/dist/src/cli/handlers/daemon.js +17 -1
- package/dist/src/cli/handlers/doctor-args.js +10 -5
- package/dist/src/cli/handlers/doctor-help.js +4 -1
- package/dist/src/cli/handlers/index.js +2 -2
- package/dist/src/cli/handlers/models-resource-sample.js +10 -0
- package/dist/src/cli/handlers/models.js +3 -2
- package/dist/src/cli/handlers/regenerate.js +8 -16
- package/dist/src/cli/handlers/sessions.js +7 -7
- package/dist/src/cli/handlers/subcommands.js +8 -0
- package/dist/src/cli/handlers/update.js +157 -0
- package/dist/src/cli/handlers/use.js +14 -4
- package/dist/src/cli/handlers/utilities.js +3 -560
- package/dist/src/cli/handlers/utility-doctor.js +412 -0
- package/dist/src/cli/services/deployment-verification.js +0 -1
- package/dist/src/config/aiwg-config.js +2 -1
- package/dist/src/config/authorization-cli.js +7 -3
- package/dist/src/config/dispatch-thresholds-runtime.d.mts +3 -2
- package/dist/src/config/dispatch-thresholds-runtime.mjs +17 -7
- package/dist/src/config/framework-aliases.js +12 -0
- package/dist/src/config/host-resource-report.d.mts +19 -0
- package/dist/src/config/host-resource-report.mjs +80 -0
- package/dist/src/config/host-resource-runtime.d.mts +34 -0
- package/dist/src/config/host-resource-runtime.mjs +293 -0
- package/dist/src/config/installed-selection.js +22 -0
- package/dist/src/config/worker-invocation-marker.d.mts +1 -0
- package/dist/src/config/worker-invocation-marker.mjs +2 -0
- package/dist/src/decision/heldout/access.js +74 -0
- package/dist/src/decision/heldout/calibration.js +1 -0
- package/dist/src/decision/heldout/collector.js +8 -1
- package/dist/src/effects/verifiers/file.js +22 -5
- package/dist/src/extensions/claude-attribution.js +82 -0
- package/dist/src/extensions/claude-hooks-installer.js +115 -69
- package/dist/src/extensions/commands/daemon.js +109 -0
- package/dist/src/extensions/commands/definitions.js +5 -89
- package/dist/src/gates/heldout.js +17 -7
- package/dist/src/lint/check-patterns.js +163 -0
- package/dist/src/lint/checks.js +268 -0
- package/dist/src/lint/reference-resolution.js +54 -0
- package/dist/src/lint/runner.js +3 -465
- package/dist/src/mcp/helpers.mjs +11 -1
- package/dist/src/memory/project-registry.js +44 -4
- package/dist/src/models/worker-plan.js +36 -5
- package/dist/src/models/wrapper-deployment.js +25 -12
- package/dist/src/models/wrapper-enforcement.js +47 -7
- package/dist/src/network-analysis/research.js +2 -1
- package/dist/src/policy/hitl-check.js +14 -4
- package/dist/src/policy/hitl-consume.js +54 -0
- package/dist/src/policy/hitl-receipt.js +2 -0
- package/dist/src/policy/hitl.js +11 -3
- package/dist/src/providers/provider-context-contracts.js +162 -0
- package/dist/src/providers/provider-definition-schema.js +136 -0
- package/dist/src/providers/provider-definitions.js +8 -1298
- package/dist/src/providers/provider-seeds-1.js +360 -0
- package/dist/src/providers/provider-seeds-2.js +387 -0
- package/dist/src/providers/provider-seeds-3.js +262 -0
- package/dist/src/research/clients/arxiv.js +4 -30
- package/dist/src/research/clients/base.js +48 -22
- package/dist/src/research/identity.js +34 -2
- package/dist/src/research/query-cli.js +3 -87
- package/dist/src/research/query-options.js +87 -0
- package/dist/src/research/reference-match.js +70 -24
- package/dist/src/research/services/acquisition.js +9 -7
- package/dist/src/research/services/citation.js +5 -2
- package/dist/src/research/types.js +2 -0
- package/dist/src/serve/mission-conductor.js +48 -4
- package/dist/src/sessions/adapters/grok-build-native.js +23 -0
- package/dist/src/sessions/adapters/grok-build.js +14 -7
- package/dist/src/sessions/workspace-discovery.js +5 -5
- package/dist/src/smiths/context-pipeline/aiwg-md.js +16 -46
- package/dist/src/smiths/context-pipeline/claude-hook.js +1 -1
- package/dist/src/smiths/context-pipeline/code-regions.js +28 -0
- package/dist/src/smiths/context-pipeline/finalization.js +81 -51
- package/dist/src/smiths/context-pipeline/generator.js +9 -10
- package/dist/src/smiths/context-pipeline/managed-hook.js +1 -1
- package/dist/src/smiths/context-pipeline/muse-bridge.js +2 -2
- package/dist/src/smiths/context-pipeline/provider-policy.js +6 -12
- package/dist/src/smiths/context-pipeline/workspace-audit.js +299 -0
- package/dist/src/smiths/context-pipeline/workspace-bootstrap.js +148 -0
- package/dist/src/smiths/context-pipeline/workspace-context-shared.js +111 -0
- package/dist/src/smiths/context-pipeline/workspace-context.js +8 -1373
- package/dist/src/smiths/context-pipeline/workspace-doctor.js +192 -0
- package/dist/src/smiths/context-pipeline/workspace-document.js +201 -0
- package/dist/src/smiths/context-pipeline/workspace-extraction.js +205 -0
- package/dist/src/smiths/context-pipeline/workspace-migration.js +250 -0
- package/package.json +10 -1
- package/schemas/decision/D29Study.v10.schema.json +65 -0
- package/tools/agents/deploy-agents.mjs +10 -0
- package/tools/agents/providers/claude-post-deploy.mjs +12 -1
- package/tools/agents/providers/context-write.mjs +4 -0
- package/tools/agents/providers/pi.mjs +3 -1
- package/tools/agents/providers/rule-delivery-surface.mjs +332 -0
- package/tools/agents/providers/rule-delivery.mjs +20 -1144
- package/tools/agents/providers/rule-deployment-commit.mjs +144 -0
- package/tools/agents/providers/rule-deployment-prepare.mjs +452 -0
- package/tools/agents/providers/rule-deployment-state.mjs +300 -0
- package/tools/daemon/README.md +511 -0
- package/tools/daemon/adapters/base-adapter.mjs +84 -0
- package/tools/daemon/adapters/webhook-adapter.mjs +107 -0
- package/tools/daemon/agent-supervisor.mjs +299 -0
- package/tools/daemon/automation-engine.mjs +310 -0
- package/tools/daemon/autonomous-engine.mjs +346 -0
- package/tools/daemon/behavior-loader.mjs +316 -0
- package/tools/daemon/behavior.mjs +174 -0
- package/tools/daemon/concierge/intent-router.mjs +401 -0
- package/tools/daemon/concierge/orchestrator.mjs +272 -0
- package/tools/daemon/concierge/response-translator.mjs +462 -0
- package/tools/daemon/config.mjs +377 -0
- package/tools/daemon/cron-scheduler.mjs +178 -0
- package/tools/daemon/daemon-main.mjs +1169 -0
- package/tools/daemon/event-router.mjs +84 -0
- package/tools/daemon/file-watcher.mjs +383 -0
- package/tools/daemon/index.mjs +1181 -0
- package/tools/daemon/init-profile.mjs +124 -0
- package/tools/daemon/ipc-client.mjs +197 -0
- package/tools/daemon/ipc-ops-handlers.mjs +236 -0
- package/tools/daemon/ipc-server.mjs +275 -0
- package/tools/daemon/maintenance-owner.mjs +79 -0
- package/tools/daemon/memory-manager.mjs +386 -0
- package/tools/daemon/message-router.mjs +194 -0
- package/tools/daemon/provider-watcher.mjs +585 -0
- package/tools/daemon/pty-adapter.mjs +420 -0
- package/tools/daemon/repl-chat.mjs +359 -0
- package/tools/daemon/resource-bounds.mjs +88 -0
- package/tools/daemon/sandbox-transport.mjs +295 -0
- package/tools/daemon/scheduled-task-runner.mjs +535 -0
- package/tools/daemon/task-store.mjs +450 -0
- package/tools/daemon/tmux-manager.mjs +324 -0
- package/tools/daemon/update-policy.mjs +517 -0
- package/tools/daemon/web-server.mjs +399 -0
- package/tools/daemon/web-ui/index.html +585 -0
- package/tools/daemon/web-ui/styles.css +156 -0
- package/tools/docker/Dockerfile.daemon +35 -0
- package/tools/docker/container-manager.mjs +291 -0
- package/tools/messaging/adapter-registry.mjs +230 -0
- package/tools/messaging/adapters/base.mjs +326 -0
- package/tools/messaging/adapters/discord.mjs +462 -0
- package/tools/messaging/adapters/slack.mjs +402 -0
- package/tools/messaging/adapters/telegram.mjs +552 -0
- package/tools/messaging/chat-handler.mjs +297 -0
- package/tools/messaging/command-router.mjs +269 -0
- package/tools/messaging/event-bus.mjs +199 -0
- package/tools/messaging/index.mjs +456 -0
- package/tools/messaging/message-formatter.mjs +320 -0
- package/tools/messaging/room-manager.mjs +277 -0
- package/tools/messaging/types.mjs +94 -0
- package/tools/providers/pi-bridge-policy.mjs +4 -0
- package/tools/ralph-external/daemon-supervisor.mjs +394 -0
- package/tools/ralph-external/lib/behavior-registry.mjs +270 -0
- package/tools/ralph-external/lib/worker-invocation-processes.mjs +3 -2
- package/tools/ralph-external/lib/worker-isolation-args.mjs +217 -0
- package/tools/ralph-external/lib/worker-isolation-status.mjs +19 -0
- package/tools/ralph-external/lib/worker-isolation.mjs +11 -237
- package/tools/ralph-external/lib/worker-overflow.mjs +10 -2
- package/tools/ralph-external/lib/worker-unit-cleanup.mjs +72 -0
- package/tools/ralph-external/session-launcher.mjs +7 -7
- package/tools/scripts/resource-preflight.mjs +3 -242
|
@@ -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,
|
|
41
|
+
import { buildSystemdClientEnv, resolveSystemdClient, resolveSystemdRun, unitEnvironmentNames, isClientControlVariable } from './systemd-client.mjs';
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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()));
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Reap only this launch's retained unit after its raw exit state is recorded.
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { buildSystemdClientEnv, resolveSystemdClient } from './systemd-client.mjs';
|
|
5
|
+
|
|
6
|
+
const owners = new WeakMap();
|
|
7
|
+
const recordedStops = new WeakMap();
|
|
8
|
+
|
|
9
|
+
export function createWorkerUnitOwner(unit, kind) {
|
|
10
|
+
const description = `AIWG worker ${randomUUID()}`;
|
|
11
|
+
return {
|
|
12
|
+
properties: kind === 'unit' ? [`Description=${description}`] : [],
|
|
13
|
+
track(outcome) {
|
|
14
|
+
if (kind === 'unit') owners.set(outcome.child ?? outcome, { unit, description });
|
|
15
|
+
return outcome;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resetRecordedWorkerUnit(outcome, native, { spawn = spawnSync, log = console.warn } = {}) {
|
|
21
|
+
const owner = owners.get(outcome?.child ?? outcome);
|
|
22
|
+
if (!owner || native?.active !== false || typeof native.result !== 'string'
|
|
23
|
+
|| !Number.isInteger(native.execMainStatus)) return;
|
|
24
|
+
// Retain retry authority on transient failures; caller-supplied names never authorize reset.
|
|
25
|
+
try {
|
|
26
|
+
const client = resolveSystemdClient('systemctl');
|
|
27
|
+
const options = { encoding: 'utf8', timeout: 500, killSignal: 'SIGKILL', env: buildSystemdClientEnv({ envNames: [] }) };
|
|
28
|
+
const shown = spawn(client, ['--user', 'show', '-p', 'Description', '--value', '--', owner.unit], options);
|
|
29
|
+
if (shown.error || shown.status !== 0) throw new Error(shown.error?.message ?? (String(shown.stderr ?? '').trim() || 'ownership query failed'));
|
|
30
|
+
// A failed submission may have collided with somebody else's existing unit.
|
|
31
|
+
if (String(shown.stdout ?? '').trim() !== owner.description) {
|
|
32
|
+
owners.delete(outcome?.child ?? outcome);
|
|
33
|
+
throw new Error('unit ownership does not match this launch');
|
|
34
|
+
}
|
|
35
|
+
const reset = spawn(client, ['--user', 'reset-failed', '--', owner.unit], options);
|
|
36
|
+
if (reset.error || reset.status !== 0) throw new Error(reset.error?.message ?? (String(reset.stderr ?? '').trim() || 'reset-failed failed'));
|
|
37
|
+
owners.delete(outcome?.child ?? outcome);
|
|
38
|
+
return true;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
try { log(`Worker unit cleanup failed for ${owner.unit}: ${error.message}`); } catch { /* diagnostics cannot replace the result */ }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function recordRawStatus(outcome, native, result) {
|
|
45
|
+
if (native && (owners.has(outcome?.child ?? outcome) || readRecordedWorkerUnitStop(outcome))) {
|
|
46
|
+
result.nativeResult = native.result;
|
|
47
|
+
result.nativeExecMainStatus = native.execMainStatus ?? null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function recordWorkerUnitStop(outcome, native, result, options) {
|
|
52
|
+
recordRawStatus(outcome, native, result);
|
|
53
|
+
if (resetRecordedWorkerUnit(outcome, native, options) === true) {
|
|
54
|
+
recordedStops.set(outcome?.child ?? outcome, {
|
|
55
|
+
result: Object.freeze({ ...result }), native: Object.freeze({ ...native }),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A repeated teardown must not lose positive stop evidence when reset unloads the unit.
|
|
62
|
+
export function readRecordedWorkerUnitStop(outcome, { native = false } = {}) {
|
|
63
|
+
const recorded = recordedStops.get(outcome?.child ?? outcome);
|
|
64
|
+
const result = native ? recorded?.native : recorded?.result;
|
|
65
|
+
return result ? { ...result } : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function publishWorkerUnitExit(publish, outcome, native, result, options) {
|
|
69
|
+
recordRawStatus(outcome, native, result);
|
|
70
|
+
await publish('exit.json', result);
|
|
71
|
+
resetRecordedWorkerUnit(outcome, native, options);
|
|
72
|
+
}
|