@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,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DaemonSupervisor - Production-grade process governance wrapping AgentSupervisor
|
|
3
|
+
*
|
|
4
|
+
* Adds to AgentSupervisor:
|
|
5
|
+
* 1. Bounded priority queue (configurable maxQueueDepth)
|
|
6
|
+
* 2. Configurable concurrency cap (maxConcurrent)
|
|
7
|
+
* 3. Process group kill (-pid instead of pid)
|
|
8
|
+
* 4. Restart intensity tracking (Erlang/OTP max_restarts pattern)
|
|
9
|
+
* 5. SIGCHLD handling for zombie reaping
|
|
10
|
+
* 6. Budget aggregation vs daily limit
|
|
11
|
+
*
|
|
12
|
+
* @implements #513
|
|
13
|
+
* @tests @test/unit/daemon-supervisor.test.mjs
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { EventEmitter } from 'node:events';
|
|
17
|
+
import { isSafeWorkerGroupTarget } from './process-monitor.mjs';
|
|
18
|
+
import { ConsecutiveFailureCircuitBreaker } from './lib/consecutive-failure-circuit-breaker.mjs';
|
|
19
|
+
|
|
20
|
+
export class DaemonSupervisor extends EventEmitter {
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} options
|
|
23
|
+
* @param {object} options.agentSupervisor - AgentSupervisor instance to wrap
|
|
24
|
+
* @param {number} [options.maxConcurrent=4] - Maximum concurrent loops
|
|
25
|
+
* @param {number} [options.maxQueueDepth=20] - Maximum queue size before rejection
|
|
26
|
+
* @param {object} [options.restartIntensity] - Erlang/OTP restart intensity
|
|
27
|
+
* @param {number} [options.restartIntensity.maxRestarts=3] - Max restarts allowed
|
|
28
|
+
* @param {number} [options.restartIntensity.windowMs=300000] - Time window (5 min)
|
|
29
|
+
* @param {number} [options.dailyBudgetUsd=0] - Daily spend limit (0 = unlimited)
|
|
30
|
+
* @param {object} [options.circuitBreaker] - Circuit breaker config overrides
|
|
31
|
+
*/
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
super();
|
|
34
|
+
|
|
35
|
+
if (!options.agentSupervisor) {
|
|
36
|
+
throw new Error('DaemonSupervisor requires an agentSupervisor instance');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
this.agentSupervisor = options.agentSupervisor;
|
|
40
|
+
this.maxConcurrent = options.maxConcurrent ?? 4;
|
|
41
|
+
this.maxQueueDepth = options.maxQueueDepth ?? 20;
|
|
42
|
+
this.dailyBudgetUsd = options.dailyBudgetUsd ?? 0;
|
|
43
|
+
|
|
44
|
+
this.restartIntensity = {
|
|
45
|
+
maxRestarts: options.restartIntensity?.maxRestarts ?? 3,
|
|
46
|
+
windowMs: options.restartIntensity?.windowMs ?? 300_000,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Internal state
|
|
50
|
+
this._queue = []; // bounded priority queue
|
|
51
|
+
this._running = new Map(); // loopId -> { config, pid, startedAt }
|
|
52
|
+
this._restartHistory = new Map(); // loopId -> [timestamp, ...]
|
|
53
|
+
this._permanentlyFailed = new Set(); // loopIds that exceeded restart intensity
|
|
54
|
+
this._budgetUsed = 0; // aggregate USD spend today
|
|
55
|
+
this._budgetResetAt = this._nextMidnight();
|
|
56
|
+
this._shutdownInProgress = false;
|
|
57
|
+
|
|
58
|
+
// Circuit breaker for system-wide health
|
|
59
|
+
this.circuitBreaker = new ConsecutiveFailureCircuitBreaker({
|
|
60
|
+
failureThreshold: options.circuitBreaker?.failureThreshold ?? 5,
|
|
61
|
+
cooldownMs: options.circuitBreaker?.cooldownMs ?? 120_000,
|
|
62
|
+
...options.circuitBreaker,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Forward circuit breaker events
|
|
66
|
+
this.circuitBreaker.on('state:change', (evt) => this.emit('circuit:change', evt));
|
|
67
|
+
this.circuitBreaker.on('trip', (evt) => this.emit('circuit:trip', evt));
|
|
68
|
+
this.circuitBreaker.on('recover', () => this.emit('circuit:recover'));
|
|
69
|
+
|
|
70
|
+
// Wire up AgentSupervisor events
|
|
71
|
+
this._wireAgentSupervisorEvents();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Submit a loop configuration for execution.
|
|
76
|
+
* @param {object} loopConfig - Loop configuration
|
|
77
|
+
* @param {string} loopConfig.loopId - Unique loop identifier
|
|
78
|
+
* @param {string} loopConfig.prompt - Task prompt
|
|
79
|
+
* @param {number} [loopConfig.priority=0] - Queue priority (higher = first)
|
|
80
|
+
* @param {number} [loopConfig.estimatedCostUsd=0] - Estimated cost for budget check
|
|
81
|
+
* @returns {{ loopId: string, queued: boolean, position: number }}
|
|
82
|
+
*/
|
|
83
|
+
submit(loopConfig) {
|
|
84
|
+
if (this._shutdownInProgress) {
|
|
85
|
+
throw new Error('DaemonSupervisor is shutting down');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const loopId = loopConfig.loopId || `loop-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
89
|
+
|
|
90
|
+
// Check permanently failed
|
|
91
|
+
if (this._permanentlyFailed.has(loopId)) {
|
|
92
|
+
throw new Error(`Loop ${loopId} is permanently failed (restart intensity exceeded)`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Check circuit breaker
|
|
96
|
+
if (!this.circuitBreaker.canExecute()) {
|
|
97
|
+
const state = this.circuitBreaker.getState();
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Circuit breaker is ${state.state}: ${state.consecutiveFailures} consecutive failures. ` +
|
|
100
|
+
`Cooldown remaining: ${Math.ceil(state.cooldownRemainingMs / 1000)}s`
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Check budget gate
|
|
105
|
+
this._resetBudgetIfNewDay();
|
|
106
|
+
if (this.dailyBudgetUsd > 0) {
|
|
107
|
+
const estimatedCost = loopConfig.estimatedCostUsd || 0;
|
|
108
|
+
if (this._budgetUsed + estimatedCost >= this.dailyBudgetUsd) {
|
|
109
|
+
this.emit('budget:exceeded', {
|
|
110
|
+
budgetUsed: this._budgetUsed,
|
|
111
|
+
dailyLimit: this.dailyBudgetUsd,
|
|
112
|
+
requested: estimatedCost,
|
|
113
|
+
});
|
|
114
|
+
throw new Error(
|
|
115
|
+
`Daily budget exceeded: $${this._budgetUsed.toFixed(2)} used of $${this.dailyBudgetUsd.toFixed(2)} limit`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Warn at 90%
|
|
120
|
+
if (this._budgetUsed >= this.dailyBudgetUsd * 0.9) {
|
|
121
|
+
this.emit('budget:warning', {
|
|
122
|
+
budgetUsed: this._budgetUsed,
|
|
123
|
+
dailyLimit: this.dailyBudgetUsd,
|
|
124
|
+
percentUsed: ((this._budgetUsed / this.dailyBudgetUsd) * 100).toFixed(1),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Check queue capacity
|
|
130
|
+
if (this._queue.length >= this.maxQueueDepth) {
|
|
131
|
+
this.emit('loop:rejected', {
|
|
132
|
+
loopId,
|
|
133
|
+
reason: 'queue full',
|
|
134
|
+
queueDepth: this._queue.length,
|
|
135
|
+
maxQueueDepth: this.maxQueueDepth,
|
|
136
|
+
});
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Queue full: ${this._queue.length}/${this.maxQueueDepth}. Cannot enqueue loop ${loopId}`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Can we run immediately?
|
|
143
|
+
if (this._running.size < this.maxConcurrent) {
|
|
144
|
+
this._startLoop(loopId, loopConfig);
|
|
145
|
+
return { loopId, queued: false, position: 0 };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Enqueue
|
|
149
|
+
const entry = { loopId, config: loopConfig, enqueuedAt: Date.now() };
|
|
150
|
+
this._queue.push(entry);
|
|
151
|
+
this._queue.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0));
|
|
152
|
+
const position = this._queue.findIndex(e => e.loopId === loopId) + 1;
|
|
153
|
+
|
|
154
|
+
this.emit('loop:queued', { loopId, position, queueDepth: this._queue.length });
|
|
155
|
+
return { loopId, queued: true, position };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Cancel a loop (running or queued).
|
|
160
|
+
*/
|
|
161
|
+
cancel(loopId) {
|
|
162
|
+
// Check queue
|
|
163
|
+
const queueIdx = this._queue.findIndex(e => e.loopId === loopId);
|
|
164
|
+
if (queueIdx !== -1) {
|
|
165
|
+
this._queue.splice(queueIdx, 1);
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Check running — delegate to AgentSupervisor
|
|
170
|
+
const entry = this._running.get(loopId);
|
|
171
|
+
if (entry && entry.taskId) {
|
|
172
|
+
return this.agentSupervisor.cancel(entry.taskId);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Get comprehensive status snapshot.
|
|
180
|
+
*/
|
|
181
|
+
status() {
|
|
182
|
+
this._resetBudgetIfNewDay();
|
|
183
|
+
return {
|
|
184
|
+
running: Array.from(this._running.entries()).map(([id, entry]) => ({
|
|
185
|
+
loopId: id,
|
|
186
|
+
pid: entry.pid,
|
|
187
|
+
startedAt: entry.startedAt,
|
|
188
|
+
taskId: entry.taskId,
|
|
189
|
+
})),
|
|
190
|
+
queued: this._queue.map((e, i) => ({
|
|
191
|
+
loopId: e.loopId,
|
|
192
|
+
position: i + 1,
|
|
193
|
+
priority: e.config.priority || 0,
|
|
194
|
+
enqueuedAt: new Date(e.enqueuedAt).toISOString(),
|
|
195
|
+
})),
|
|
196
|
+
circuitState: this.circuitBreaker.getState(),
|
|
197
|
+
concurrencyUsed: this._running.size,
|
|
198
|
+
concurrencyMax: this.maxConcurrent,
|
|
199
|
+
queueDepth: this._queue.length,
|
|
200
|
+
queueMax: this.maxQueueDepth,
|
|
201
|
+
budgetUsed: this._budgetUsed,
|
|
202
|
+
budgetLimit: this.dailyBudgetUsd,
|
|
203
|
+
permanentlyFailed: Array.from(this._permanentlyFailed),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Record cost for budget tracking.
|
|
209
|
+
*/
|
|
210
|
+
recordCost(loopId, costUsd) {
|
|
211
|
+
this._budgetUsed += costUsd;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Graceful shutdown — drain queue, wait for running loops.
|
|
216
|
+
*/
|
|
217
|
+
async shutdown(timeoutMs = 30_000) {
|
|
218
|
+
this._shutdownInProgress = true;
|
|
219
|
+
|
|
220
|
+
// Reject all queued
|
|
221
|
+
while (this._queue.length > 0) {
|
|
222
|
+
const entry = this._queue.shift();
|
|
223
|
+
this.emit('loop:rejected', { loopId: entry.loopId, reason: 'shutdown' });
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Delegate shutdown to underlying supervisor
|
|
227
|
+
await this.agentSupervisor.shutdown(timeoutMs);
|
|
228
|
+
|
|
229
|
+
this._running.clear();
|
|
230
|
+
this.circuitBreaker.destroy();
|
|
231
|
+
this._shutdownInProgress = false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// --- Private ---
|
|
235
|
+
|
|
236
|
+
_startLoop(loopId, config) {
|
|
237
|
+
const task = this.agentSupervisor.submit(config.prompt, {
|
|
238
|
+
priority: config.priority || 0,
|
|
239
|
+
metadata: { loopId, ...config.metadata },
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
this._running.set(loopId, {
|
|
243
|
+
config,
|
|
244
|
+
taskId: task.id,
|
|
245
|
+
pid: null, // set when task:started fires
|
|
246
|
+
startedAt: new Date().toISOString(),
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
this.emit('loop:started', { loopId, taskId: task.id });
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
_wireAgentSupervisorEvents() {
|
|
253
|
+
this.agentSupervisor.on('task:started', ({ taskId, pid }) => {
|
|
254
|
+
// Find the loop entry with this taskId and record PID
|
|
255
|
+
for (const [loopId, entry] of this._running) {
|
|
256
|
+
if (entry.taskId === taskId) {
|
|
257
|
+
entry.pid = pid;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
this.agentSupervisor.on('task:completed', ({ taskId }) => {
|
|
264
|
+
const loopId = this._findLoopByTaskId(taskId);
|
|
265
|
+
if (loopId) {
|
|
266
|
+
this._running.delete(loopId);
|
|
267
|
+
this.circuitBreaker.recordSuccess();
|
|
268
|
+
this.emit('loop:completed', { loopId, taskId });
|
|
269
|
+
this._processQueue();
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
this.agentSupervisor.on('task:failed', ({ taskId, error }) => {
|
|
274
|
+
const loopId = this._findLoopByTaskId(taskId);
|
|
275
|
+
if (loopId) {
|
|
276
|
+
const entry = this._running.get(loopId);
|
|
277
|
+
this._running.delete(loopId);
|
|
278
|
+
this.circuitBreaker.recordFailure();
|
|
279
|
+
|
|
280
|
+
// Check restart intensity
|
|
281
|
+
if (this._shouldRestart(loopId)) {
|
|
282
|
+
this.emit('loop:recovered', { loopId, error, restarting: true });
|
|
283
|
+
this._startLoop(loopId, entry.config);
|
|
284
|
+
} else if (this._permanentlyFailed.has(loopId)) {
|
|
285
|
+
this.emit('loop:failed', { loopId, error, permanent: true, reason: 'restart intensity exceeded' });
|
|
286
|
+
} else {
|
|
287
|
+
this.emit('loop:failed', { loopId, error, permanent: false });
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
this._processQueue();
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
this.agentSupervisor.on('task:cancelled', ({ taskId }) => {
|
|
295
|
+
const loopId = this._findLoopByTaskId(taskId);
|
|
296
|
+
if (loopId) {
|
|
297
|
+
this._running.delete(loopId);
|
|
298
|
+
this._processQueue();
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
_findLoopByTaskId(taskId) {
|
|
304
|
+
for (const [loopId, entry] of this._running) {
|
|
305
|
+
if (entry.taskId === taskId) {
|
|
306
|
+
return loopId;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
_shouldRestart(loopId) {
|
|
313
|
+
if (!this._restartHistory.has(loopId)) {
|
|
314
|
+
this._restartHistory.set(loopId, []);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const history = this._restartHistory.get(loopId);
|
|
318
|
+
const now = Date.now();
|
|
319
|
+
|
|
320
|
+
// Add current failure
|
|
321
|
+
history.push(now);
|
|
322
|
+
|
|
323
|
+
// Prune entries outside the window
|
|
324
|
+
const windowStart = now - this.restartIntensity.windowMs;
|
|
325
|
+
while (history.length > 0 && history[0] < windowStart) {
|
|
326
|
+
history.shift();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Check if exceeded
|
|
330
|
+
if (history.length >= this.restartIntensity.maxRestarts) {
|
|
331
|
+
this._permanentlyFailed.add(loopId);
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
_processQueue() {
|
|
339
|
+
while (
|
|
340
|
+
this._running.size < this.maxConcurrent &&
|
|
341
|
+
this._queue.length > 0 &&
|
|
342
|
+
!this._shutdownInProgress &&
|
|
343
|
+
this.circuitBreaker.canExecute()
|
|
344
|
+
) {
|
|
345
|
+
const entry = this._queue.shift();
|
|
346
|
+
|
|
347
|
+
// Skip permanently failed loops
|
|
348
|
+
if (this._permanentlyFailed.has(entry.loopId)) {
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
this._startLoop(entry.loopId, entry.config);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Kill a running loop's entire process group (negative PID).
|
|
358
|
+
* This kills the process and all its children (shells, subprocesses).
|
|
359
|
+
*/
|
|
360
|
+
killProcessGroup(loopId, signal = 'SIGTERM') {
|
|
361
|
+
const entry = this._running.get(loopId);
|
|
362
|
+
if (!entry || !isSafeWorkerGroupTarget(entry.pid)) {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
try {
|
|
367
|
+
// Negative PID sends signal to entire process group
|
|
368
|
+
process.kill(-entry.pid, signal);
|
|
369
|
+
return true;
|
|
370
|
+
} catch (err) {
|
|
371
|
+
// ESRCH = process not found (already dead)
|
|
372
|
+
if (err.code === 'ESRCH') {
|
|
373
|
+
this._running.delete(loopId);
|
|
374
|
+
this._processQueue();
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
throw err;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
_resetBudgetIfNewDay() {
|
|
382
|
+
const now = Date.now();
|
|
383
|
+
if (now >= this._budgetResetAt) {
|
|
384
|
+
this._budgetUsed = 0;
|
|
385
|
+
this._budgetResetAt = this._nextMidnight();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
_nextMidnight() {
|
|
390
|
+
const d = new Date();
|
|
391
|
+
d.setHours(24, 0, 0, 0);
|
|
392
|
+
return d.getTime();
|
|
393
|
+
}
|
|
394
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BehaviorRegistry - Discovers, loads, validates, and merges behavior YAML files
|
|
3
|
+
*
|
|
4
|
+
* Three-tier discovery (later tiers override earlier):
|
|
5
|
+
* 1. agentic/code/behaviors/ — framework-shipped (read-only)
|
|
6
|
+
* 2. .aiwg/behaviors/ — project-local overrides
|
|
7
|
+
* 3. ~/.config/aiwg/behaviors/ — user-level overrides
|
|
8
|
+
*
|
|
9
|
+
* @implements #515
|
|
10
|
+
* @tests @test/unit/ralph-external/behavior-registry.test.mjs
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { homedir } from 'node:os';
|
|
16
|
+
import YAML from 'yaml';
|
|
17
|
+
|
|
18
|
+
const REQUIRED_FIELDS = ['name', 'version', 'agentTypes', 'directives', 'tools'];
|
|
19
|
+
|
|
20
|
+
export class BehaviorRegistry {
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} options
|
|
23
|
+
* @param {string} options.projectRoot - Project root directory
|
|
24
|
+
* @param {string} [options.frameworkRoot] - AIWG framework root (defaults to projectRoot)
|
|
25
|
+
*/
|
|
26
|
+
constructor(options = {}) {
|
|
27
|
+
this.projectRoot = options.projectRoot || process.cwd();
|
|
28
|
+
this.frameworkRoot = options.frameworkRoot || this.projectRoot;
|
|
29
|
+
|
|
30
|
+
this._behaviors = new Map(); // name -> Behavior
|
|
31
|
+
this._loaded = false;
|
|
32
|
+
|
|
33
|
+
// Discovery tier paths (priority order, later wins)
|
|
34
|
+
this._tiers = [
|
|
35
|
+
{ name: 'framework', path: join(this.frameworkRoot, 'agentic', 'code', 'behaviors') },
|
|
36
|
+
{ name: 'project', path: join(this.projectRoot, '.aiwg', 'behaviors') },
|
|
37
|
+
{ name: 'user', path: join(homedir(), '.config', 'aiwg', 'behaviors') },
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Discover all behavior YAML files across all tiers.
|
|
43
|
+
* Returns array of file paths found.
|
|
44
|
+
*/
|
|
45
|
+
async discover() {
|
|
46
|
+
const discovered = [];
|
|
47
|
+
|
|
48
|
+
for (const tier of this._tiers) {
|
|
49
|
+
try {
|
|
50
|
+
const entries = await readdir(tier.path);
|
|
51
|
+
const yamlFiles = entries.filter(f => f.endsWith('.yaml') || f.endsWith('.yml'));
|
|
52
|
+
for (const file of yamlFiles) {
|
|
53
|
+
discovered.push({ tier: tier.name, path: join(tier.path, file), file });
|
|
54
|
+
}
|
|
55
|
+
} catch (err) {
|
|
56
|
+
// Directory doesn't exist — skip silently
|
|
57
|
+
if (err.code !== 'ENOENT') throw err;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return discovered;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Load a single behavior by name.
|
|
66
|
+
* Searches all tiers; last-found (highest tier) wins.
|
|
67
|
+
*/
|
|
68
|
+
async load(name) {
|
|
69
|
+
const discovered = await this.discover();
|
|
70
|
+
const matching = discovered.filter(d => {
|
|
71
|
+
const baseName = d.file.replace(/\.(yaml|yml)$/, '');
|
|
72
|
+
return baseName === name;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (matching.length === 0) {
|
|
76
|
+
throw new Error(`Behavior '${name}' not found in any discovery tier`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Last match wins (highest tier)
|
|
80
|
+
const entry = matching[matching.length - 1];
|
|
81
|
+
const raw = await readFile(entry.path, 'utf-8');
|
|
82
|
+
const parsed = YAML.parse(raw);
|
|
83
|
+
|
|
84
|
+
const validation = this.validate(parsed);
|
|
85
|
+
if (!validation.valid) {
|
|
86
|
+
throw new Error(`Invalid behavior '${name}': ${validation.errors.join(', ')}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
parsed._tier = entry.tier;
|
|
90
|
+
parsed._path = entry.path;
|
|
91
|
+
return parsed;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Load all discovered behaviors. Later tiers override earlier ones.
|
|
96
|
+
*/
|
|
97
|
+
async loadAll() {
|
|
98
|
+
const discovered = await this.discover();
|
|
99
|
+
const byName = new Map();
|
|
100
|
+
|
|
101
|
+
for (const entry of discovered) {
|
|
102
|
+
try {
|
|
103
|
+
const raw = await readFile(entry.path, 'utf-8');
|
|
104
|
+
const parsed = YAML.parse(raw);
|
|
105
|
+
|
|
106
|
+
const validation = this.validate(parsed);
|
|
107
|
+
if (!validation.valid) {
|
|
108
|
+
console.warn(`Skipping invalid behavior at ${entry.path}: ${validation.errors.join(', ')}`);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
parsed._tier = entry.tier;
|
|
113
|
+
parsed._path = entry.path;
|
|
114
|
+
|
|
115
|
+
if (byName.has(parsed.name)) {
|
|
116
|
+
console.warn(
|
|
117
|
+
`Behavior '${parsed.name}' from ${entry.tier} tier overrides ${byName.get(parsed.name)._tier} tier`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
byName.set(parsed.name, parsed);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
console.warn(`Failed to load behavior at ${entry.path}: ${err.message}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this._behaviors = byName;
|
|
127
|
+
this._loaded = true;
|
|
128
|
+
return byName;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Get directives applicable to a given agent type.
|
|
133
|
+
*/
|
|
134
|
+
getDirectives(agentType) {
|
|
135
|
+
const directives = [];
|
|
136
|
+
|
|
137
|
+
for (const behavior of this._behaviors.values()) {
|
|
138
|
+
if (!this._matchesAgentType(behavior, agentType)) continue;
|
|
139
|
+
|
|
140
|
+
for (const directive of behavior.directives || []) {
|
|
141
|
+
directives.push({ ...directive, _behaviorName: behavior.name });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return directives;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Get toolset applicable to a given agent type.
|
|
150
|
+
*/
|
|
151
|
+
getToolset(agentType) {
|
|
152
|
+
const tools = [];
|
|
153
|
+
|
|
154
|
+
for (const behavior of this._behaviors.values()) {
|
|
155
|
+
if (!this._matchesAgentType(behavior, agentType)) continue;
|
|
156
|
+
|
|
157
|
+
for (const tool of behavior.tools || []) {
|
|
158
|
+
tools.push({ ...tool, _behaviorName: behavior.name });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return tools;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Validate a parsed behavior YAML against the required schema.
|
|
167
|
+
*/
|
|
168
|
+
validate(behaviorYaml) {
|
|
169
|
+
const errors = [];
|
|
170
|
+
|
|
171
|
+
if (!behaviorYaml || typeof behaviorYaml !== 'object') {
|
|
172
|
+
return { valid: false, errors: ['Behavior must be a non-null object'] };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
for (const field of REQUIRED_FIELDS) {
|
|
176
|
+
if (behaviorYaml[field] === undefined || behaviorYaml[field] === null) {
|
|
177
|
+
errors.push(`Missing required field: '${field}'`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (behaviorYaml.agentTypes && !Array.isArray(behaviorYaml.agentTypes)) {
|
|
182
|
+
errors.push("'agentTypes' must be an array");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (behaviorYaml.directives && !Array.isArray(behaviorYaml.directives)) {
|
|
186
|
+
errors.push("'directives' must be an array");
|
|
187
|
+
} else if (Array.isArray(behaviorYaml.directives)) {
|
|
188
|
+
for (const [i, d] of behaviorYaml.directives.entries()) {
|
|
189
|
+
if (!d.id) errors.push(`Directive at index ${i} missing 'id'`);
|
|
190
|
+
if (!d.rule) errors.push(`Directive at index ${i} missing 'rule'`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (behaviorYaml.tools && !Array.isArray(behaviorYaml.tools)) {
|
|
195
|
+
errors.push("'tools' must be an array");
|
|
196
|
+
} else if (Array.isArray(behaviorYaml.tools)) {
|
|
197
|
+
for (const [i, t] of behaviorYaml.tools.entries()) {
|
|
198
|
+
if (!t.tool) errors.push(`Tool at index ${i} missing 'tool'`);
|
|
199
|
+
if (!t.description) errors.push(`Tool at index ${i} missing 'description'`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return { valid: errors.length === 0, errors };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Merge multiple behaviors. Returns combined directives and toolset.
|
|
208
|
+
* Directive collisions: highest priority wins (default 0), then later attachment.
|
|
209
|
+
* Every collision warns with the winning and losing behaviors.
|
|
210
|
+
* Toolset collisions: error.
|
|
211
|
+
*/
|
|
212
|
+
merge(behaviors) {
|
|
213
|
+
const directiveMap = new Map(); // id -> directive
|
|
214
|
+
const toolMap = new Map(); // tool name -> tool
|
|
215
|
+
const warnings = [];
|
|
216
|
+
|
|
217
|
+
for (const behavior of behaviors) {
|
|
218
|
+
for (const directive of behavior.directives || []) {
|
|
219
|
+
const incoming = { ...directive, _behaviorName: behavior.name };
|
|
220
|
+
const previous = directiveMap.get(directive.id);
|
|
221
|
+
if (previous) {
|
|
222
|
+
const incomingPriority = directive.priority ?? 0;
|
|
223
|
+
const previousPriority = previous.priority ?? 0;
|
|
224
|
+
const incomingWins = incomingPriority >= previousPriority;
|
|
225
|
+
const winner = incomingWins ? incoming : previous;
|
|
226
|
+
const loser = incomingWins ? previous : incoming;
|
|
227
|
+
const reason = incomingPriority === previousPriority
|
|
228
|
+
? 'equal priority; later in attachment order'
|
|
229
|
+
: 'higher priority';
|
|
230
|
+
warnings.push(
|
|
231
|
+
`Directive '${directive.id}': '${winner._behaviorName}' (priority ${winner.priority ?? 0}) ` +
|
|
232
|
+
`wins over '${loser._behaviorName}' (priority ${loser.priority ?? 0}); ${reason}`
|
|
233
|
+
);
|
|
234
|
+
if (!incomingWins) continue;
|
|
235
|
+
}
|
|
236
|
+
directiveMap.set(directive.id, incoming);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
for (const tool of behavior.tools || []) {
|
|
240
|
+
if (toolMap.has(tool.tool)) {
|
|
241
|
+
throw new Error(
|
|
242
|
+
`Tool name collision: '${tool.tool}' defined in both ` +
|
|
243
|
+
`'${toolMap.get(tool.tool)._behaviorName}' and '${behavior.name}'`
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
toolMap.set(tool.tool, { ...tool, _behaviorName: behavior.name });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
directives: Array.from(directiveMap.values()),
|
|
252
|
+
toolset: Array.from(toolMap.values()),
|
|
253
|
+
warnings,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Get discovery tier info (for CLI display).
|
|
259
|
+
*/
|
|
260
|
+
getTierInfo() {
|
|
261
|
+
return this._tiers.map(t => ({ name: t.name, path: t.path }));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// --- Private ---
|
|
265
|
+
|
|
266
|
+
_matchesAgentType(behavior, agentType) {
|
|
267
|
+
if (!behavior.agentTypes || !Array.isArray(behavior.agentTypes)) return false;
|
|
268
|
+
return behavior.agentTypes.includes(agentType) || behavior.agentTypes.includes('*');
|
|
269
|
+
}
|
|
270
|
+
}
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
import { opendirSync, statSync, openSync, readSync, closeSync, constants } from 'node:fs';
|
|
3
3
|
import { StringDecoder } from 'node:string_decoder';
|
|
4
4
|
import { classifyUnreadableWorkerProcess, readProcCgroup } from './worker-invocation-ancestry.mjs';
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
import { importImpl } from '../../_resolve-impl.mjs';
|
|
6
|
+
const { WORKER_INVOCATION_MARKER } = await importImpl(import.meta.url, 'config/worker-invocation-marker.mjs');
|
|
7
|
+
export { WORKER_INVOCATION_MARKER };
|
|
7
8
|
|
|
8
9
|
function startedBeforeWorker(fs, path, startTicks) {
|
|
9
10
|
if (typeof startTicks !== 'string' || !/^\d+$/.test(startTicks)) return false;
|