@aiwg/cli 2026.10.14 → 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/addons/aiwg-utils/rules/anti-laziness.core.md +13 -0
- package/agentic/code/addons/aiwg-utils/rules/anti-laziness.md +118 -0
- 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 +37 -0
- package/dist/src/artifacts/corpus-tools/discovery-log.js +27 -25
- package/dist/src/artifacts/corpus-tools/expand-cli.js +90 -0
- package/dist/src/artifacts/corpus-tools/expand-dates.js +107 -0
- package/dist/src/artifacts/corpus-tools/expand-queue.js +41 -0
- package/dist/src/artifacts/corpus-tools/expand-review.js +104 -0
- package/dist/src/artifacts/corpus-tools/expand-state.js +56 -0
- package/dist/src/artifacts/corpus-tools/expand.js +124 -0
- 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 +99 -0
- package/dist/src/artifacts/corpus-tools/resolve-corpus.js +41 -0
- package/dist/src/artifacts/corpus-tools/resolve.js +119 -0
- 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 +45 -0
- package/dist/src/cli/handlers/doctor-help.js +8 -3
- 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 -586
- 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-context.js +33 -4
- 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 +9 -2
- 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/features/catalog.js +1 -1
- package/dist/src/gates/evaluate.js +2 -2
- package/dist/src/gates/heldout.js +52 -0
- package/dist/src/gates/index.js +2 -1
- 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-installation-store.mjs +228 -0
- package/dist/src/models/worker-installation.mjs +7 -162
- 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 +14 -34
- package/dist/src/research/clients/base.js +53 -26
- package/dist/src/research/clients/crossref.js +9 -1
- package/dist/src/research/clients/semantic-scholar.js +73 -44
- package/dist/src/research/identity.js +117 -0
- package/dist/src/research/index.js +1 -0
- 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 +135 -0
- 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/rules/core-startup.d.mts +2 -0
- package/dist/src/rules/core-startup.mjs +31 -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/agents-md.js +34 -0
- package/dist/src/smiths/context-pipeline/aiwg-md.js +19 -46
- package/dist/src/smiths/context-pipeline/bridge-ownership.js +50 -0
- 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 +49 -114
- package/dist/src/smiths/context-pipeline/managed-hook.js +11 -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 +13 -2
- package/schemas/decision/D29Study.v10.schema.json +65 -0
- package/tools/agents/deploy-agents.mjs +12 -123
- package/tools/agents/providers/antigravity.mjs +1 -1
- package/tools/agents/providers/claude-post-deploy.mjs +12 -1
- package/tools/agents/providers/context-write.mjs +16 -0
- package/tools/agents/providers/grok-build.mjs +8 -5
- package/tools/agents/providers/hermes-skill-lifecycle.mjs +144 -0
- package/tools/agents/providers/hermes-startup.mjs +209 -0
- package/tools/agents/providers/hermes.mjs +9 -359
- package/tools/agents/providers/model-wrapper.mjs +5 -1
- package/tools/agents/providers/muse.mjs +3 -3
- package/tools/agents/providers/pi.mjs +4 -2
- package/tools/agents/providers/rule-delivery-surface.mjs +332 -0
- package/tools/agents/providers/rule-delivery.mjs +20 -1346
- 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/agents/providers/rule-presentation.mjs +13 -4
- package/tools/agents/providers/rule-projection.mjs +119 -0
- package/tools/agents/providers/rule-startup.mjs +70 -0
- package/tools/agents/providers/stale-artifact-prune.mjs +128 -0
- package/tools/agents/providers/warp.mjs +2 -2
- package/tools/agents/providers/windsurf.mjs +11 -57
- 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/plugin/plugin-links.mjs +3 -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,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automation Engine - Trigger-action rules for the daemon
|
|
3
|
+
*
|
|
4
|
+
* Processes configurable rules that map daemon events to agent actions.
|
|
5
|
+
* Supports approval gates, cooldowns, and condition evaluation.
|
|
6
|
+
*
|
|
7
|
+
* @implements @.aiwg/requirements/use-cases/UC-AUTO-001.md
|
|
8
|
+
* @tests @test/unit/daemon/automation-engine.test.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { EventEmitter } from 'node:events';
|
|
12
|
+
|
|
13
|
+
export class AutomationEngine extends EventEmitter {
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
super();
|
|
16
|
+
this.rules = [];
|
|
17
|
+
this.supervisor = options.supervisor || null;
|
|
18
|
+
this.enabled = options.enabled !== false;
|
|
19
|
+
this.cooldowns = new Map(); // ruleId -> lastFiredAt
|
|
20
|
+
this.executionLog = [];
|
|
21
|
+
this.maxLogEntries = options.maxLogEntries || 500;
|
|
22
|
+
this.approvalHandler = options.approvalHandler || null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Load rules from configuration
|
|
27
|
+
* @param {Array} rules - Array of rule definitions
|
|
28
|
+
*/
|
|
29
|
+
loadRules(rules) {
|
|
30
|
+
this.rules = [];
|
|
31
|
+
|
|
32
|
+
for (const rule of rules) {
|
|
33
|
+
const validated = this._validateRule(rule);
|
|
34
|
+
if (validated) {
|
|
35
|
+
this.rules.push(validated);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
this.emit('rules:loaded', { count: this.rules.length });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Process an incoming event against all rules
|
|
44
|
+
* @param {object} event - Event to process
|
|
45
|
+
*/
|
|
46
|
+
async processEvent(event) {
|
|
47
|
+
if (!this.enabled) return;
|
|
48
|
+
|
|
49
|
+
const matchingRules = this.rules.filter((rule) =>
|
|
50
|
+
this._matchesRule(rule, event)
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
for (const rule of matchingRules) {
|
|
54
|
+
await this._executeRule(rule, event);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Enable/disable the engine
|
|
60
|
+
*/
|
|
61
|
+
setEnabled(enabled) {
|
|
62
|
+
this.enabled = enabled;
|
|
63
|
+
this.emit('engine:toggled', { enabled });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get engine status
|
|
68
|
+
*/
|
|
69
|
+
getStatus() {
|
|
70
|
+
return {
|
|
71
|
+
enabled: this.enabled,
|
|
72
|
+
ruleCount: this.rules.length,
|
|
73
|
+
rules: this.rules.map((r) => ({
|
|
74
|
+
id: r.id,
|
|
75
|
+
trigger: r.trigger,
|
|
76
|
+
action: r.action,
|
|
77
|
+
enabled: r.enabled !== false,
|
|
78
|
+
cooldownMs: r.cooldownMs || 0,
|
|
79
|
+
requiresApproval: r.requiresApproval || false,
|
|
80
|
+
})),
|
|
81
|
+
recentExecutions: this.executionLog.slice(-10),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get a specific rule by id
|
|
87
|
+
*/
|
|
88
|
+
getRule(id) {
|
|
89
|
+
return this.rules.find((r) => r.id === id) || null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Enable or disable a specific rule
|
|
94
|
+
*/
|
|
95
|
+
setRuleEnabled(id, enabled) {
|
|
96
|
+
const rule = this.rules.find((r) => r.id === id);
|
|
97
|
+
if (!rule) return false;
|
|
98
|
+
rule.enabled = enabled;
|
|
99
|
+
this.emit('rule:toggled', { id, enabled });
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Get execution log
|
|
105
|
+
*/
|
|
106
|
+
getExecutionLog(limit = 50) {
|
|
107
|
+
return this.executionLog.slice(-limit);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Get rule count
|
|
112
|
+
*/
|
|
113
|
+
get ruleCount() {
|
|
114
|
+
return this.rules.length;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// --- Private methods ---
|
|
118
|
+
|
|
119
|
+
_validateRule(rule) {
|
|
120
|
+
if (!rule.id || typeof rule.id !== 'string') {
|
|
121
|
+
this.emit('rule:invalid', { rule, error: 'Missing or invalid rule id' });
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!rule.trigger) {
|
|
126
|
+
this.emit('rule:invalid', { rule, error: 'Missing trigger' });
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!rule.action) {
|
|
131
|
+
this.emit('rule:invalid', { rule, error: 'Missing action' });
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
id: rule.id,
|
|
137
|
+
trigger: rule.trigger,
|
|
138
|
+
action: rule.action,
|
|
139
|
+
enabled: rule.enabled !== false,
|
|
140
|
+
cooldownMs: rule.cooldownMs || 0,
|
|
141
|
+
requiresApproval: rule.requiresApproval || false,
|
|
142
|
+
conditions: rule.conditions || [],
|
|
143
|
+
description: rule.description || '',
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_matchesRule(rule, event) {
|
|
148
|
+
if (rule.enabled === false) return false;
|
|
149
|
+
|
|
150
|
+
// Check trigger type match
|
|
151
|
+
if (rule.trigger.type && rule.trigger.type !== event.type) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Check trigger source match
|
|
156
|
+
if (rule.trigger.source && rule.trigger.source !== event.source) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Check trigger pattern (regex on event type or source)
|
|
161
|
+
if (rule.trigger.pattern) {
|
|
162
|
+
const regex = new RegExp(rule.trigger.pattern);
|
|
163
|
+
const matchTarget = `${event.type || ''}:${event.source || ''}`;
|
|
164
|
+
if (!regex.test(matchTarget)) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Check conditions
|
|
170
|
+
for (const condition of rule.conditions || []) {
|
|
171
|
+
if (!this._evaluateCondition(condition, event)) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Check cooldown
|
|
177
|
+
if (rule.cooldownMs > 0) {
|
|
178
|
+
const lastFired = this.cooldowns.get(rule.id);
|
|
179
|
+
if (lastFired && Date.now() - lastFired < rule.cooldownMs) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
_evaluateCondition(condition, event) {
|
|
188
|
+
const { field, operator, value } = condition;
|
|
189
|
+
|
|
190
|
+
// Navigate to the field value in the event
|
|
191
|
+
const fieldValue = this._getNestedField(event, field);
|
|
192
|
+
|
|
193
|
+
switch (operator) {
|
|
194
|
+
case 'eq':
|
|
195
|
+
case 'equals':
|
|
196
|
+
return fieldValue === value;
|
|
197
|
+
case 'neq':
|
|
198
|
+
case 'not_equals':
|
|
199
|
+
return fieldValue !== value;
|
|
200
|
+
case 'contains':
|
|
201
|
+
return typeof fieldValue === 'string' && fieldValue.includes(value);
|
|
202
|
+
case 'matches':
|
|
203
|
+
return typeof fieldValue === 'string' && new RegExp(value).test(fieldValue);
|
|
204
|
+
case 'gt':
|
|
205
|
+
return typeof fieldValue === 'number' && fieldValue > value;
|
|
206
|
+
case 'lt':
|
|
207
|
+
return typeof fieldValue === 'number' && fieldValue < value;
|
|
208
|
+
case 'gte':
|
|
209
|
+
return typeof fieldValue === 'number' && fieldValue >= value;
|
|
210
|
+
case 'lte':
|
|
211
|
+
return typeof fieldValue === 'number' && fieldValue <= value;
|
|
212
|
+
case 'exists':
|
|
213
|
+
return fieldValue !== undefined && fieldValue !== null;
|
|
214
|
+
case 'not_exists':
|
|
215
|
+
return fieldValue === undefined || fieldValue === null;
|
|
216
|
+
default:
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
_getNestedField(obj, path) {
|
|
222
|
+
const parts = path.split('.');
|
|
223
|
+
let current = obj;
|
|
224
|
+
for (const part of parts) {
|
|
225
|
+
if (current === undefined || current === null) return undefined;
|
|
226
|
+
current = current[part];
|
|
227
|
+
}
|
|
228
|
+
return current;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async _executeRule(rule, event) {
|
|
232
|
+
// Check approval
|
|
233
|
+
if (rule.requiresApproval) {
|
|
234
|
+
if (this.approvalHandler) {
|
|
235
|
+
const approved = await this.approvalHandler(rule, event);
|
|
236
|
+
if (!approved) {
|
|
237
|
+
this._logExecution(rule, event, 'denied');
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
// No approval handler, skip rule
|
|
242
|
+
this._logExecution(rule, event, 'skipped_no_approver');
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Update cooldown
|
|
248
|
+
this.cooldowns.set(rule.id, Date.now());
|
|
249
|
+
|
|
250
|
+
try {
|
|
251
|
+
if (rule.action.type === 'agent' && this.supervisor) {
|
|
252
|
+
// Submit to agent supervisor
|
|
253
|
+
const prompt = this._interpolateTemplate(rule.action.prompt, event);
|
|
254
|
+
const task = this.supervisor.submit(prompt, {
|
|
255
|
+
agent: rule.action.agent,
|
|
256
|
+
priority: rule.action.priority || 0,
|
|
257
|
+
metadata: { ruleId: rule.id, eventType: event.type },
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
this._logExecution(rule, event, 'submitted', { taskId: task.id });
|
|
261
|
+
this.emit('rule:executed', { ruleId: rule.id, taskId: task.id, event });
|
|
262
|
+
} else if (rule.action.type === 'notify') {
|
|
263
|
+
this.emit('rule:notify', {
|
|
264
|
+
ruleId: rule.id,
|
|
265
|
+
message: this._interpolateTemplate(rule.action.message, event),
|
|
266
|
+
channel: rule.action.channel,
|
|
267
|
+
event,
|
|
268
|
+
});
|
|
269
|
+
this._logExecution(rule, event, 'notified');
|
|
270
|
+
} else if (rule.action.type === 'webhook') {
|
|
271
|
+
this.emit('rule:webhook', {
|
|
272
|
+
ruleId: rule.id,
|
|
273
|
+
url: rule.action.url,
|
|
274
|
+
payload: event,
|
|
275
|
+
});
|
|
276
|
+
this._logExecution(rule, event, 'webhook_sent');
|
|
277
|
+
} else {
|
|
278
|
+
this._logExecution(rule, event, 'unknown_action_type');
|
|
279
|
+
}
|
|
280
|
+
} catch (err) {
|
|
281
|
+
this._logExecution(rule, event, 'error', { error: err.message });
|
|
282
|
+
this.emit('rule:error', { ruleId: rule.id, error: err.message });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
_interpolateTemplate(template, event) {
|
|
287
|
+
if (!template) return '';
|
|
288
|
+
|
|
289
|
+
return template.replace(/\{\{(\w+(?:\.\w+)*)\}\}/g, (match, path) => {
|
|
290
|
+
const value = this._getNestedField(event, path);
|
|
291
|
+
return value !== undefined ? String(value) : match;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
_logExecution(rule, event, status, extra = {}) {
|
|
296
|
+
const entry = {
|
|
297
|
+
ruleId: rule.id,
|
|
298
|
+
eventType: event.type,
|
|
299
|
+
status,
|
|
300
|
+
timestamp: new Date().toISOString(),
|
|
301
|
+
...extra,
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
this.executionLog.push(entry);
|
|
305
|
+
|
|
306
|
+
if (this.executionLog.length > this.maxLogEntries) {
|
|
307
|
+
this.executionLog.shift();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AutonomousThinkingEngine — Self-directed task generation with safety constraints.
|
|
3
|
+
*
|
|
4
|
+
* Periodically reviews project state and proposes bounded improvement tasks.
|
|
5
|
+
* Off by default. When enabled, constrained by allowlist, budget cap,
|
|
6
|
+
* daily task limit, and optional human approval gate.
|
|
7
|
+
*
|
|
8
|
+
* @implements Plan: Daemon Starter — Autonomous Mode
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { EventEmitter } from 'events';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {Object} AutonomousConfig
|
|
15
|
+
* @property {boolean} enabled
|
|
16
|
+
* @property {number} thinking_interval_minutes
|
|
17
|
+
* @property {number} max_daily_tasks
|
|
18
|
+
* @property {number} budget_cap_usd
|
|
19
|
+
* @property {boolean} require_approval
|
|
20
|
+
* @property {string[]} allowed_actions
|
|
21
|
+
* @property {string[]} blocked_actions
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {Object} AutonomousEngineOptions
|
|
26
|
+
* @property {import('../ralph-external/daemon-supervisor.mjs').DaemonSupervisor} supervisor
|
|
27
|
+
* @property {AutonomousConfig} config
|
|
28
|
+
* @property {import('../messaging/room-manager.mjs').RoomManager} [roomManager]
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {Object} Proposal
|
|
33
|
+
* @property {string} id
|
|
34
|
+
* @property {string} action
|
|
35
|
+
* @property {string} description
|
|
36
|
+
* @property {string} prompt
|
|
37
|
+
* @property {number} estimatedMinutes
|
|
38
|
+
* @property {number} estimatedCostUsd
|
|
39
|
+
* @property {'pending'|'approved'|'rejected'|'submitted'} status
|
|
40
|
+
* @property {string} createdAt
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
export class AutonomousEngine extends EventEmitter {
|
|
44
|
+
/** @type {import('../ralph-external/daemon-supervisor.mjs').DaemonSupervisor} */
|
|
45
|
+
#supervisor;
|
|
46
|
+
|
|
47
|
+
/** @type {AutonomousConfig} */
|
|
48
|
+
#config;
|
|
49
|
+
|
|
50
|
+
/** @type {import('../messaging/room-manager.mjs').RoomManager|null} */
|
|
51
|
+
#roomManager;
|
|
52
|
+
|
|
53
|
+
/** @type {NodeJS.Timeout|null} */
|
|
54
|
+
#timer = null;
|
|
55
|
+
|
|
56
|
+
/** @type {number} */
|
|
57
|
+
#dailyTaskCount = 0;
|
|
58
|
+
|
|
59
|
+
/** @type {number} */
|
|
60
|
+
#dailySpendUsd = 0;
|
|
61
|
+
|
|
62
|
+
/** @type {string|null} */
|
|
63
|
+
#lastResetDate = null;
|
|
64
|
+
|
|
65
|
+
/** @type {Map<string, Proposal>} */
|
|
66
|
+
#proposals = new Map();
|
|
67
|
+
|
|
68
|
+
/** @type {number} */
|
|
69
|
+
#proposalCounter = 0;
|
|
70
|
+
|
|
71
|
+
/** @type {boolean} */
|
|
72
|
+
#running = false;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {AutonomousEngineOptions} options
|
|
76
|
+
*/
|
|
77
|
+
constructor(options) {
|
|
78
|
+
super();
|
|
79
|
+
this.#supervisor = options.supervisor;
|
|
80
|
+
this.#config = options.config;
|
|
81
|
+
this.#roomManager = options.roomManager || null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Start the autonomous thinking loop.
|
|
86
|
+
*/
|
|
87
|
+
start() {
|
|
88
|
+
if (this.#running) return;
|
|
89
|
+
if (!this.#config.enabled) return;
|
|
90
|
+
|
|
91
|
+
this.#running = true;
|
|
92
|
+
const intervalMs = (this.#config.thinking_interval_minutes || 30) * 60 * 1000;
|
|
93
|
+
|
|
94
|
+
this.#timer = setInterval(() => {
|
|
95
|
+
this.#think().catch(err => {
|
|
96
|
+
console.error(`[autonomous] Thinking cycle error: ${err.message}`);
|
|
97
|
+
});
|
|
98
|
+
}, intervalMs);
|
|
99
|
+
|
|
100
|
+
console.log(`[autonomous] Started (interval: ${this.#config.thinking_interval_minutes}min, ` +
|
|
101
|
+
`max daily: ${this.#config.max_daily_tasks}, budget: $${this.#config.budget_cap_usd})`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Stop the autonomous thinking loop.
|
|
106
|
+
*/
|
|
107
|
+
stop() {
|
|
108
|
+
if (this.#timer) {
|
|
109
|
+
clearInterval(this.#timer);
|
|
110
|
+
this.#timer = null;
|
|
111
|
+
}
|
|
112
|
+
this.#running = false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Enable autonomous mode at runtime.
|
|
117
|
+
*/
|
|
118
|
+
enable() {
|
|
119
|
+
this.#config.enabled = true;
|
|
120
|
+
if (!this.#running) this.start();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Disable autonomous mode at runtime.
|
|
125
|
+
*/
|
|
126
|
+
disable() {
|
|
127
|
+
this.#config.enabled = false;
|
|
128
|
+
this.stop();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Approve a pending proposal.
|
|
133
|
+
*
|
|
134
|
+
* @param {string} proposalId
|
|
135
|
+
* @returns {boolean}
|
|
136
|
+
*/
|
|
137
|
+
async approveProposal(proposalId) {
|
|
138
|
+
const proposal = this.#proposals.get(proposalId);
|
|
139
|
+
if (!proposal || proposal.status !== 'pending') return false;
|
|
140
|
+
|
|
141
|
+
proposal.status = 'approved';
|
|
142
|
+
await this.#submitProposal(proposal);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Reject a pending proposal.
|
|
148
|
+
*
|
|
149
|
+
* @param {string} proposalId
|
|
150
|
+
* @param {string} [reason]
|
|
151
|
+
* @returns {boolean}
|
|
152
|
+
*/
|
|
153
|
+
rejectProposal(proposalId, reason) {
|
|
154
|
+
const proposal = this.#proposals.get(proposalId);
|
|
155
|
+
if (!proposal || proposal.status !== 'pending') return false;
|
|
156
|
+
|
|
157
|
+
proposal.status = 'rejected';
|
|
158
|
+
this.emit('proposal:rejected', { proposal, reason });
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Run one thinking cycle.
|
|
164
|
+
*/
|
|
165
|
+
async #think() {
|
|
166
|
+
this.#resetDailyCountersIfNeeded();
|
|
167
|
+
|
|
168
|
+
// Check daily limits
|
|
169
|
+
if (this.#dailyTaskCount >= (this.#config.max_daily_tasks || 10)) {
|
|
170
|
+
console.log('[autonomous] Daily task limit reached, skipping');
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (this.#dailySpendUsd >= (this.#config.budget_cap_usd || 5)) {
|
|
175
|
+
console.log('[autonomous] Daily budget cap reached, skipping');
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Generate a thinking prompt
|
|
180
|
+
const prompt = this.#buildThinkingPrompt();
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
// Use the supervisor to run a lightweight analysis task
|
|
184
|
+
const result = await this.#supervisor.submit({
|
|
185
|
+
prompt,
|
|
186
|
+
priority: 1, // Lowest priority — interactive and scheduled tasks first
|
|
187
|
+
metadata: {
|
|
188
|
+
source: 'autonomous',
|
|
189
|
+
type: 'thinking',
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// The task output should contain a JSON proposal
|
|
194
|
+
// For now, track that we submitted a thinking task
|
|
195
|
+
this.#dailyTaskCount++;
|
|
196
|
+
this.emit('think:completed', { taskId: result?.loopId || result?.taskId });
|
|
197
|
+
} catch (error) {
|
|
198
|
+
console.error(`[autonomous] Failed to submit thinking task: ${error.message}`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Build the prompt for a thinking cycle.
|
|
204
|
+
*
|
|
205
|
+
* @returns {string}
|
|
206
|
+
*/
|
|
207
|
+
#buildThinkingPrompt() {
|
|
208
|
+
const allowed = (this.#config.allowed_actions || []).join(', ');
|
|
209
|
+
const blocked = (this.#config.blocked_actions || []).join(', ');
|
|
210
|
+
|
|
211
|
+
return `You are the autonomous maintenance daemon for this project.
|
|
212
|
+
|
|
213
|
+
Review the following and propose ONE specific, bounded improvement:
|
|
214
|
+
- Check .aiwg/ artifact health (missing fields, stale dates)
|
|
215
|
+
- Check recent git log (last 24h) for incomplete work
|
|
216
|
+
- Check test coverage gaps
|
|
217
|
+
- Check documentation drift
|
|
218
|
+
|
|
219
|
+
Constraints:
|
|
220
|
+
- You may ONLY perform actions in this allowlist: [${allowed}]
|
|
221
|
+
- You may NOT perform: [${blocked}]
|
|
222
|
+
- Keep the task under 10 minutes estimated execution time
|
|
223
|
+
- Keep estimated cost under $${this.#config.budget_cap_usd || 5}
|
|
224
|
+
|
|
225
|
+
Respond with ONLY a JSON object (no markdown fences):
|
|
226
|
+
{
|
|
227
|
+
"action": "one of the allowed actions",
|
|
228
|
+
"description": "what you want to do and why",
|
|
229
|
+
"prompt": "the exact prompt to execute the action",
|
|
230
|
+
"estimatedMinutes": 5,
|
|
231
|
+
"estimatedCostUsd": 0.50,
|
|
232
|
+
"skip": false,
|
|
233
|
+
"skipReason": "only if skip is true — why nothing needs doing"
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
If the project is in good shape and nothing needs doing, set skip: true.`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Submit an approved proposal as a task.
|
|
241
|
+
*
|
|
242
|
+
* @param {Proposal} proposal
|
|
243
|
+
*/
|
|
244
|
+
async #submitProposal(proposal) {
|
|
245
|
+
// Validate against constraints
|
|
246
|
+
if (!this.#validateProposal(proposal)) {
|
|
247
|
+
proposal.status = 'rejected';
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
const result = await this.#supervisor.submit({
|
|
253
|
+
prompt: proposal.prompt,
|
|
254
|
+
priority: 1,
|
|
255
|
+
metadata: {
|
|
256
|
+
source: 'autonomous',
|
|
257
|
+
type: 'proposal',
|
|
258
|
+
proposalId: proposal.id,
|
|
259
|
+
action: proposal.action,
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
proposal.status = 'submitted';
|
|
264
|
+
this.#dailyTaskCount++;
|
|
265
|
+
this.#dailySpendUsd += proposal.estimatedCostUsd || 0;
|
|
266
|
+
|
|
267
|
+
this.emit('proposal:submitted', {
|
|
268
|
+
proposal,
|
|
269
|
+
taskId: result?.loopId || result?.taskId,
|
|
270
|
+
});
|
|
271
|
+
} catch (error) {
|
|
272
|
+
console.error(`[autonomous] Failed to submit proposal ${proposal.id}: ${error.message}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Validate a proposal against safety constraints.
|
|
278
|
+
*
|
|
279
|
+
* @param {Proposal} proposal
|
|
280
|
+
* @returns {boolean}
|
|
281
|
+
*/
|
|
282
|
+
#validateProposal(proposal) {
|
|
283
|
+
const allowed = this.#config.allowed_actions || [];
|
|
284
|
+
const blocked = this.#config.blocked_actions || [];
|
|
285
|
+
|
|
286
|
+
if (blocked.includes(proposal.action)) {
|
|
287
|
+
console.warn(`[autonomous] Blocked action: ${proposal.action}`);
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (allowed.length > 0 && !allowed.includes(proposal.action)) {
|
|
292
|
+
console.warn(`[autonomous] Action not in allowlist: ${proposal.action}`);
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if ((this.#dailySpendUsd + (proposal.estimatedCostUsd || 0)) > (this.#config.budget_cap_usd || 5)) {
|
|
297
|
+
console.warn('[autonomous] Proposal would exceed daily budget');
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Reset daily counters at midnight.
|
|
306
|
+
*/
|
|
307
|
+
#resetDailyCountersIfNeeded() {
|
|
308
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
309
|
+
if (this.#lastResetDate !== today) {
|
|
310
|
+
this.#dailyTaskCount = 0;
|
|
311
|
+
this.#dailySpendUsd = 0;
|
|
312
|
+
this.#lastResetDate = today;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Get pending proposals.
|
|
318
|
+
*
|
|
319
|
+
* @returns {Proposal[]}
|
|
320
|
+
*/
|
|
321
|
+
getPendingProposals() {
|
|
322
|
+
return [...this.#proposals.values()].filter(p => p.status === 'pending');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Get status summary.
|
|
327
|
+
*
|
|
328
|
+
* @returns {Object}
|
|
329
|
+
*/
|
|
330
|
+
getStatus() {
|
|
331
|
+
return {
|
|
332
|
+
enabled: this.#config.enabled,
|
|
333
|
+
running: this.#running,
|
|
334
|
+
dailyTaskCount: this.#dailyTaskCount,
|
|
335
|
+
dailyTaskLimit: this.#config.max_daily_tasks || 10,
|
|
336
|
+
dailySpendUsd: this.#dailySpendUsd,
|
|
337
|
+
budgetCapUsd: this.#config.budget_cap_usd || 5,
|
|
338
|
+
pendingProposals: this.getPendingProposals().length,
|
|
339
|
+
allowedActions: this.#config.allowed_actions || [],
|
|
340
|
+
blockedActions: this.#config.blocked_actions || [],
|
|
341
|
+
requireApproval: this.#config.require_approval || false,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export default AutonomousEngine;
|