@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.
Files changed (180) hide show
  1. package/agentic/code/providers/pi/aiwg-bridge.ts +45 -7
  2. package/dist/src/artifacts/citation-parser.js +7 -6
  3. package/dist/src/artifacts/corpus-tools/citation-densify.js +4 -2
  4. package/dist/src/artifacts/corpus-tools/cli.js +8 -3
  5. package/dist/src/artifacts/corpus-tools/discovery-log.js +26 -24
  6. package/dist/src/artifacts/corpus-tools/expand-cli.js +3 -2
  7. package/dist/src/artifacts/corpus-tools/expand-dates.js +107 -0
  8. package/dist/src/artifacts/corpus-tools/expand-queue.js +4 -1
  9. package/dist/src/artifacts/corpus-tools/expand-review.js +104 -0
  10. package/dist/src/artifacts/corpus-tools/expand-state.js +17 -1
  11. package/dist/src/artifacts/corpus-tools/expand.js +16 -17
  12. package/dist/src/artifacts/corpus-tools/induction-audit.js +7 -6
  13. package/dist/src/artifacts/corpus-tools/integrity-scan.js +3 -3
  14. package/dist/src/artifacts/corpus-tools/radar-shared.js +7 -9
  15. package/dist/src/artifacts/corpus-tools/ref-files.js +11 -0
  16. package/dist/src/artifacts/corpus-tools/resolve-cli.js +11 -3
  17. package/dist/src/artifacts/corpus-tools/resolve.js +23 -9
  18. package/dist/src/artifacts/corpus-tools/retrieval-lab.js +2 -1
  19. package/dist/src/artifacts/corpus-tools/sidecar-lint.js +4 -4
  20. package/dist/src/artifacts/corpus-tools/sidecar-repair.js +2 -1
  21. package/dist/src/artifacts/corpus-views/ref-parser.js +3 -17
  22. package/dist/src/artifacts/graph-query.js +5 -3
  23. package/dist/src/artifacts/hybrid-query.js +11 -2
  24. package/dist/src/artifacts/index-refresh.js +2 -0
  25. package/dist/src/cli/handlers/daemon.js +17 -1
  26. package/dist/src/cli/handlers/doctor-args.js +10 -5
  27. package/dist/src/cli/handlers/doctor-help.js +4 -1
  28. package/dist/src/cli/handlers/index.js +2 -2
  29. package/dist/src/cli/handlers/models-resource-sample.js +10 -0
  30. package/dist/src/cli/handlers/models.js +3 -2
  31. package/dist/src/cli/handlers/regenerate.js +8 -16
  32. package/dist/src/cli/handlers/sessions.js +7 -7
  33. package/dist/src/cli/handlers/subcommands.js +8 -0
  34. package/dist/src/cli/handlers/update.js +157 -0
  35. package/dist/src/cli/handlers/use.js +14 -4
  36. package/dist/src/cli/handlers/utilities.js +3 -560
  37. package/dist/src/cli/handlers/utility-doctor.js +412 -0
  38. package/dist/src/cli/services/deployment-verification.js +0 -1
  39. package/dist/src/config/aiwg-config.js +2 -1
  40. package/dist/src/config/authorization-cli.js +7 -3
  41. package/dist/src/config/dispatch-thresholds-runtime.d.mts +3 -2
  42. package/dist/src/config/dispatch-thresholds-runtime.mjs +17 -7
  43. package/dist/src/config/framework-aliases.js +12 -0
  44. package/dist/src/config/host-resource-report.d.mts +19 -0
  45. package/dist/src/config/host-resource-report.mjs +80 -0
  46. package/dist/src/config/host-resource-runtime.d.mts +34 -0
  47. package/dist/src/config/host-resource-runtime.mjs +293 -0
  48. package/dist/src/config/installed-selection.js +22 -0
  49. package/dist/src/config/worker-invocation-marker.d.mts +1 -0
  50. package/dist/src/config/worker-invocation-marker.mjs +2 -0
  51. package/dist/src/decision/heldout/access.js +74 -0
  52. package/dist/src/decision/heldout/calibration.js +1 -0
  53. package/dist/src/decision/heldout/collector.js +8 -1
  54. package/dist/src/effects/verifiers/file.js +22 -5
  55. package/dist/src/extensions/claude-attribution.js +82 -0
  56. package/dist/src/extensions/claude-hooks-installer.js +115 -69
  57. package/dist/src/extensions/commands/daemon.js +109 -0
  58. package/dist/src/extensions/commands/definitions.js +5 -89
  59. package/dist/src/gates/heldout.js +17 -7
  60. package/dist/src/lint/check-patterns.js +163 -0
  61. package/dist/src/lint/checks.js +268 -0
  62. package/dist/src/lint/reference-resolution.js +54 -0
  63. package/dist/src/lint/runner.js +3 -465
  64. package/dist/src/mcp/helpers.mjs +11 -1
  65. package/dist/src/memory/project-registry.js +44 -4
  66. package/dist/src/models/worker-plan.js +36 -5
  67. package/dist/src/models/wrapper-deployment.js +25 -12
  68. package/dist/src/models/wrapper-enforcement.js +47 -7
  69. package/dist/src/network-analysis/research.js +2 -1
  70. package/dist/src/policy/hitl-check.js +14 -4
  71. package/dist/src/policy/hitl-consume.js +54 -0
  72. package/dist/src/policy/hitl-receipt.js +2 -0
  73. package/dist/src/policy/hitl.js +11 -3
  74. package/dist/src/providers/provider-context-contracts.js +162 -0
  75. package/dist/src/providers/provider-definition-schema.js +136 -0
  76. package/dist/src/providers/provider-definitions.js +8 -1298
  77. package/dist/src/providers/provider-seeds-1.js +360 -0
  78. package/dist/src/providers/provider-seeds-2.js +387 -0
  79. package/dist/src/providers/provider-seeds-3.js +262 -0
  80. package/dist/src/research/clients/arxiv.js +4 -30
  81. package/dist/src/research/clients/base.js +48 -22
  82. package/dist/src/research/identity.js +34 -2
  83. package/dist/src/research/query-cli.js +3 -87
  84. package/dist/src/research/query-options.js +87 -0
  85. package/dist/src/research/reference-match.js +70 -24
  86. package/dist/src/research/services/acquisition.js +9 -7
  87. package/dist/src/research/services/citation.js +5 -2
  88. package/dist/src/research/types.js +2 -0
  89. package/dist/src/serve/mission-conductor.js +48 -4
  90. package/dist/src/sessions/adapters/grok-build-native.js +23 -0
  91. package/dist/src/sessions/adapters/grok-build.js +14 -7
  92. package/dist/src/sessions/workspace-discovery.js +5 -5
  93. package/dist/src/smiths/context-pipeline/aiwg-md.js +16 -46
  94. package/dist/src/smiths/context-pipeline/claude-hook.js +1 -1
  95. package/dist/src/smiths/context-pipeline/code-regions.js +28 -0
  96. package/dist/src/smiths/context-pipeline/finalization.js +81 -51
  97. package/dist/src/smiths/context-pipeline/generator.js +9 -10
  98. package/dist/src/smiths/context-pipeline/managed-hook.js +1 -1
  99. package/dist/src/smiths/context-pipeline/muse-bridge.js +2 -2
  100. package/dist/src/smiths/context-pipeline/provider-policy.js +6 -12
  101. package/dist/src/smiths/context-pipeline/workspace-audit.js +299 -0
  102. package/dist/src/smiths/context-pipeline/workspace-bootstrap.js +148 -0
  103. package/dist/src/smiths/context-pipeline/workspace-context-shared.js +111 -0
  104. package/dist/src/smiths/context-pipeline/workspace-context.js +8 -1373
  105. package/dist/src/smiths/context-pipeline/workspace-doctor.js +192 -0
  106. package/dist/src/smiths/context-pipeline/workspace-document.js +201 -0
  107. package/dist/src/smiths/context-pipeline/workspace-extraction.js +205 -0
  108. package/dist/src/smiths/context-pipeline/workspace-migration.js +250 -0
  109. package/package.json +10 -1
  110. package/schemas/decision/D29Study.v10.schema.json +65 -0
  111. package/tools/agents/deploy-agents.mjs +10 -0
  112. package/tools/agents/providers/claude-post-deploy.mjs +12 -1
  113. package/tools/agents/providers/context-write.mjs +4 -0
  114. package/tools/agents/providers/pi.mjs +3 -1
  115. package/tools/agents/providers/rule-delivery-surface.mjs +332 -0
  116. package/tools/agents/providers/rule-delivery.mjs +20 -1144
  117. package/tools/agents/providers/rule-deployment-commit.mjs +144 -0
  118. package/tools/agents/providers/rule-deployment-prepare.mjs +452 -0
  119. package/tools/agents/providers/rule-deployment-state.mjs +300 -0
  120. package/tools/daemon/README.md +511 -0
  121. package/tools/daemon/adapters/base-adapter.mjs +84 -0
  122. package/tools/daemon/adapters/webhook-adapter.mjs +107 -0
  123. package/tools/daemon/agent-supervisor.mjs +299 -0
  124. package/tools/daemon/automation-engine.mjs +310 -0
  125. package/tools/daemon/autonomous-engine.mjs +346 -0
  126. package/tools/daemon/behavior-loader.mjs +316 -0
  127. package/tools/daemon/behavior.mjs +174 -0
  128. package/tools/daemon/concierge/intent-router.mjs +401 -0
  129. package/tools/daemon/concierge/orchestrator.mjs +272 -0
  130. package/tools/daemon/concierge/response-translator.mjs +462 -0
  131. package/tools/daemon/config.mjs +377 -0
  132. package/tools/daemon/cron-scheduler.mjs +178 -0
  133. package/tools/daemon/daemon-main.mjs +1169 -0
  134. package/tools/daemon/event-router.mjs +84 -0
  135. package/tools/daemon/file-watcher.mjs +383 -0
  136. package/tools/daemon/index.mjs +1181 -0
  137. package/tools/daemon/init-profile.mjs +124 -0
  138. package/tools/daemon/ipc-client.mjs +197 -0
  139. package/tools/daemon/ipc-ops-handlers.mjs +236 -0
  140. package/tools/daemon/ipc-server.mjs +275 -0
  141. package/tools/daemon/maintenance-owner.mjs +79 -0
  142. package/tools/daemon/memory-manager.mjs +386 -0
  143. package/tools/daemon/message-router.mjs +194 -0
  144. package/tools/daemon/provider-watcher.mjs +585 -0
  145. package/tools/daemon/pty-adapter.mjs +420 -0
  146. package/tools/daemon/repl-chat.mjs +359 -0
  147. package/tools/daemon/resource-bounds.mjs +88 -0
  148. package/tools/daemon/sandbox-transport.mjs +295 -0
  149. package/tools/daemon/scheduled-task-runner.mjs +535 -0
  150. package/tools/daemon/task-store.mjs +450 -0
  151. package/tools/daemon/tmux-manager.mjs +324 -0
  152. package/tools/daemon/update-policy.mjs +517 -0
  153. package/tools/daemon/web-server.mjs +399 -0
  154. package/tools/daemon/web-ui/index.html +585 -0
  155. package/tools/daemon/web-ui/styles.css +156 -0
  156. package/tools/docker/Dockerfile.daemon +35 -0
  157. package/tools/docker/container-manager.mjs +291 -0
  158. package/tools/messaging/adapter-registry.mjs +230 -0
  159. package/tools/messaging/adapters/base.mjs +326 -0
  160. package/tools/messaging/adapters/discord.mjs +462 -0
  161. package/tools/messaging/adapters/slack.mjs +402 -0
  162. package/tools/messaging/adapters/telegram.mjs +552 -0
  163. package/tools/messaging/chat-handler.mjs +297 -0
  164. package/tools/messaging/command-router.mjs +269 -0
  165. package/tools/messaging/event-bus.mjs +199 -0
  166. package/tools/messaging/index.mjs +456 -0
  167. package/tools/messaging/message-formatter.mjs +320 -0
  168. package/tools/messaging/room-manager.mjs +277 -0
  169. package/tools/messaging/types.mjs +94 -0
  170. package/tools/providers/pi-bridge-policy.mjs +4 -0
  171. package/tools/ralph-external/daemon-supervisor.mjs +394 -0
  172. package/tools/ralph-external/lib/behavior-registry.mjs +270 -0
  173. package/tools/ralph-external/lib/worker-invocation-processes.mjs +3 -2
  174. package/tools/ralph-external/lib/worker-isolation-args.mjs +217 -0
  175. package/tools/ralph-external/lib/worker-isolation-status.mjs +19 -0
  176. package/tools/ralph-external/lib/worker-isolation.mjs +11 -237
  177. package/tools/ralph-external/lib/worker-overflow.mjs +10 -2
  178. package/tools/ralph-external/lib/worker-unit-cleanup.mjs +72 -0
  179. package/tools/ralph-external/session-launcher.mjs +7 -7
  180. package/tools/scripts/resource-preflight.mjs +3 -242
@@ -0,0 +1,320 @@
1
+ /**
2
+ * Converts AiwgEvent objects into platform-agnostic AiwgMessage structures
3
+ * that platform adapters translate to native rendering.
4
+ *
5
+ * @implements @.aiwg/architecture/adrs/ADR-messaging-bot-mode.md
6
+ */
7
+
8
+ import { Severity, ButtonStyle } from './types.mjs';
9
+
10
+ /**
11
+ * @typedef {Object} AiwgMessage
12
+ * @property {string} title
13
+ * @property {string} body
14
+ * @property {'info'|'warning'|'critical'} severity
15
+ * @property {Array<{label: string, value: string, inline: boolean}>} fields
16
+ * @property {Array<{id: string, label: string, style: string, command: string}>} [actions]
17
+ * @property {string} [threadId]
18
+ * @property {string} project
19
+ * @property {string} timestamp
20
+ * @property {string} [codeBlock]
21
+ * @property {string} [linkUrl]
22
+ * @property {string} [linkText]
23
+ */
24
+
25
+ /**
26
+ * Severity colors for Slack/Discord
27
+ */
28
+ const SEVERITY_COLORS = {
29
+ [Severity.INFO]: '#36a64f', // Green
30
+ [Severity.WARNING]: '#daa520', // Goldenrod
31
+ [Severity.CRITICAL]: '#dc3545', // Red
32
+ };
33
+
34
+ /**
35
+ * Severity emoji for Telegram
36
+ */
37
+ const SEVERITY_EMOJI = {
38
+ [Severity.INFO]: '\u2705', // Green check
39
+ [Severity.WARNING]: '\u26a0\ufe0f', // Warning
40
+ [Severity.CRITICAL]: '\ud83d\udea8', // Siren
41
+ };
42
+
43
+ /**
44
+ * Format an AiwgEvent into an AiwgMessage.
45
+ *
46
+ * @param {import('./event-bus.mjs').AiwgEvent} event
47
+ * @returns {AiwgMessage}
48
+ */
49
+ export function formatEvent(event) {
50
+ const formatter = EVENT_FORMATTERS[event.topic] || formatGenericEvent;
51
+ return formatter(event);
52
+ }
53
+
54
+ /**
55
+ * Get the color for a severity level.
56
+ *
57
+ * @param {'info'|'warning'|'critical'} severity
58
+ * @returns {string}
59
+ */
60
+ export function getSeverityColor(severity) {
61
+ return SEVERITY_COLORS[severity] || SEVERITY_COLORS[Severity.INFO];
62
+ }
63
+
64
+ /**
65
+ * Get the emoji for a severity level.
66
+ *
67
+ * @param {'info'|'warning'|'critical'} severity
68
+ * @returns {string}
69
+ */
70
+ export function getSeverityEmoji(severity) {
71
+ return SEVERITY_EMOJI[severity] || SEVERITY_EMOJI[Severity.INFO];
72
+ }
73
+
74
+ // ============================================================================
75
+ // Event-specific formatters
76
+ // ============================================================================
77
+
78
+ const EVENT_FORMATTERS = {
79
+ 'ralph.started': formatRalphStarted,
80
+ 'ralph.iteration': formatRalphIteration,
81
+ 'ralph.completed': formatRalphCompleted,
82
+ 'ralph.failed': formatRalphFailed,
83
+ 'ralph.aborted': formatRalphAborted,
84
+ 'gate.pending': formatGatePending,
85
+ 'gate.approved': formatGateApproved,
86
+ 'gate.rejected': formatGateRejected,
87
+ 'gate.timeout': formatGateTimeout,
88
+ 'security.critical': formatSecurityCritical,
89
+ 'health.degraded': formatHealthDegraded,
90
+ 'health.recovered': formatHealthRecovered,
91
+ 'daemon.started': formatDaemonStarted,
92
+ };
93
+
94
+ function formatRalphStarted(event) {
95
+ const d = event.details;
96
+ return {
97
+ title: `Ralph Loop Started`,
98
+ body: event.summary,
99
+ severity: Severity.INFO,
100
+ fields: [
101
+ { label: 'Objective', value: d.objective || 'N/A', inline: false },
102
+ { label: 'Max Iterations', value: String(d.maxIterations || 'N/A'), inline: true },
103
+ { label: 'Loop ID', value: event.loopId || 'N/A', inline: true },
104
+ ],
105
+ project: event.project,
106
+ timestamp: event.timestamp,
107
+ };
108
+ }
109
+
110
+ function formatRalphIteration(event) {
111
+ const d = event.details;
112
+ const progress = d.progress != null ? `${d.progress}%` : 'N/A';
113
+ return {
114
+ title: `Iteration ${d.iteration}/${d.maxIterations}`,
115
+ body: event.summary,
116
+ severity: d.progress >= 80 ? Severity.INFO : Severity.WARNING,
117
+ fields: [
118
+ { label: 'Progress', value: progress, inline: true },
119
+ { label: 'Status', value: d.status || 'in_progress', inline: true },
120
+ ],
121
+ threadId: event.loopId,
122
+ project: event.project,
123
+ timestamp: event.timestamp,
124
+ };
125
+ }
126
+
127
+ function formatRalphCompleted(event) {
128
+ const d = event.details;
129
+ return {
130
+ title: `Ralph Loop Completed`,
131
+ body: event.summary,
132
+ severity: Severity.INFO,
133
+ fields: [
134
+ { label: 'Iterations', value: String(d.iterations || 'N/A'), inline: true },
135
+ { label: 'Result', value: d.success ? 'Success' : 'Partial', inline: true },
136
+ { label: 'Loop ID', value: event.loopId || 'N/A', inline: true },
137
+ ],
138
+ threadId: event.loopId,
139
+ project: event.project,
140
+ timestamp: event.timestamp,
141
+ };
142
+ }
143
+
144
+ function formatRalphFailed(event) {
145
+ const d = event.details;
146
+ return {
147
+ title: `Ralph Loop Failed`,
148
+ body: event.summary,
149
+ severity: Severity.CRITICAL,
150
+ fields: [
151
+ { label: 'Reason', value: d.reason || 'Unknown', inline: false },
152
+ { label: 'Iterations', value: String(d.iterations || 'N/A'), inline: true },
153
+ { label: 'Loop ID', value: event.loopId || 'N/A', inline: true },
154
+ ],
155
+ codeBlock: d.lastError || undefined,
156
+ threadId: event.loopId,
157
+ project: event.project,
158
+ timestamp: event.timestamp,
159
+ };
160
+ }
161
+
162
+ function formatRalphAborted(event) {
163
+ return {
164
+ title: `Ralph Loop Aborted`,
165
+ body: event.summary,
166
+ severity: Severity.WARNING,
167
+ fields: [
168
+ { label: 'Reason', value: event.details.reason || 'User request', inline: false },
169
+ { label: 'Loop ID', value: event.loopId || 'N/A', inline: true },
170
+ ],
171
+ threadId: event.loopId,
172
+ project: event.project,
173
+ timestamp: event.timestamp,
174
+ };
175
+ }
176
+
177
+ function formatGatePending(event) {
178
+ const d = event.details;
179
+ return {
180
+ title: `HITL Gate: Approval Required`,
181
+ body: event.summary,
182
+ severity: Severity.WARNING,
183
+ fields: [
184
+ { label: 'Gate', value: d.gateName || event.gateId || 'N/A', inline: true },
185
+ { label: 'Quality Score', value: d.qualityScore != null ? `${d.qualityScore}%` : 'N/A', inline: true },
186
+ { label: 'Artifacts', value: String(d.artifactCount || 0), inline: true },
187
+ ],
188
+ actions: [
189
+ {
190
+ id: `approve_${event.gateId}`,
191
+ label: 'Approve',
192
+ style: ButtonStyle.PRIMARY,
193
+ command: `approve ${event.gateId}`,
194
+ },
195
+ {
196
+ id: `reject_${event.gateId}`,
197
+ label: 'Reject',
198
+ style: ButtonStyle.DANGER,
199
+ command: `reject ${event.gateId}`,
200
+ },
201
+ ],
202
+ project: event.project,
203
+ timestamp: event.timestamp,
204
+ };
205
+ }
206
+
207
+ function formatGateApproved(event) {
208
+ return {
209
+ title: `Gate Approved`,
210
+ body: event.summary,
211
+ severity: Severity.INFO,
212
+ fields: [
213
+ { label: 'Gate', value: event.gateId || 'N/A', inline: true },
214
+ { label: 'Approved By', value: event.details.approvedBy || 'N/A', inline: true },
215
+ ],
216
+ project: event.project,
217
+ timestamp: event.timestamp,
218
+ };
219
+ }
220
+
221
+ function formatGateRejected(event) {
222
+ return {
223
+ title: `Gate Rejected`,
224
+ body: event.summary,
225
+ severity: Severity.WARNING,
226
+ fields: [
227
+ { label: 'Gate', value: event.gateId || 'N/A', inline: true },
228
+ { label: 'Rejected By', value: event.details.rejectedBy || 'N/A', inline: true },
229
+ { label: 'Reason', value: event.details.reason || 'N/A', inline: false },
230
+ ],
231
+ project: event.project,
232
+ timestamp: event.timestamp,
233
+ };
234
+ }
235
+
236
+ function formatGateTimeout(event) {
237
+ return {
238
+ title: `Gate Timed Out`,
239
+ body: event.summary,
240
+ severity: Severity.CRITICAL,
241
+ fields: [
242
+ { label: 'Gate', value: event.gateId || 'N/A', inline: true },
243
+ { label: 'Action', value: event.details.timeoutAction || 'block', inline: true },
244
+ ],
245
+ project: event.project,
246
+ timestamp: event.timestamp,
247
+ };
248
+ }
249
+
250
+ function formatSecurityCritical(event) {
251
+ return {
252
+ title: `Security Alert: Critical Finding`,
253
+ body: event.summary,
254
+ severity: Severity.CRITICAL,
255
+ fields: [
256
+ { label: 'Finding', value: event.details.finding || 'N/A', inline: false },
257
+ { label: 'File', value: event.details.file || 'N/A', inline: true },
258
+ { label: 'Severity', value: 'CRITICAL', inline: true },
259
+ ],
260
+ project: event.project,
261
+ timestamp: event.timestamp,
262
+ };
263
+ }
264
+
265
+ function formatHealthDegraded(event) {
266
+ return {
267
+ title: `System Health Degraded`,
268
+ body: event.summary,
269
+ severity: Severity.WARNING,
270
+ fields: [
271
+ { label: 'Component', value: event.details.component || 'N/A', inline: true },
272
+ { label: 'Status', value: event.details.status || 'degraded', inline: true },
273
+ ],
274
+ project: event.project,
275
+ timestamp: event.timestamp,
276
+ };
277
+ }
278
+
279
+ function formatHealthRecovered(event) {
280
+ return {
281
+ title: `System Health Recovered`,
282
+ body: event.summary,
283
+ severity: Severity.INFO,
284
+ fields: [
285
+ { label: 'Component', value: event.details.component || 'N/A', inline: true },
286
+ ],
287
+ project: event.project,
288
+ timestamp: event.timestamp,
289
+ };
290
+ }
291
+
292
+ function formatDaemonStarted(event) {
293
+ const d = event.details;
294
+ return {
295
+ title: `AIWG Daemon Started`,
296
+ body: event.summary,
297
+ severity: Severity.INFO,
298
+ fields: [
299
+ { label: 'PID', value: String(d.pid || process.pid), inline: true },
300
+ { label: 'Adapters', value: (d.adapters || []).join(', ') || 'none', inline: true },
301
+ ],
302
+ project: event.project,
303
+ timestamp: event.timestamp,
304
+ };
305
+ }
306
+
307
+ function formatGenericEvent(event) {
308
+ return {
309
+ title: event.topic,
310
+ body: event.summary,
311
+ severity: event.severity || Severity.INFO,
312
+ fields: Object.entries(event.details || {}).map(([key, value]) => ({
313
+ label: key,
314
+ value: String(value),
315
+ inline: true,
316
+ })),
317
+ project: event.project,
318
+ timestamp: event.timestamp,
319
+ };
320
+ }
@@ -0,0 +1,277 @@
1
+ /**
2
+ * RoomManager — Multi-room coordination for messaging adapters.
3
+ *
4
+ * Manages a registry of rooms across all platforms, room-to-task bindings,
5
+ * and targeted message delivery. Each room has a purpose classification
6
+ * (interactive, notifications, logs) and visibility settings.
7
+ *
8
+ * @implements Plan: Daemon Starter — Multi-Room Messaging
9
+ */
10
+
11
+ import { EventEmitter } from 'events';
12
+
13
+ /**
14
+ * @typedef {Object} RoomConfig
15
+ * @property {string} id - Composite key "platform:platformRoomId"
16
+ * @property {string} platform - "telegram" | "discord" | "slack"
17
+ * @property {string} platformRoomId - Native channel/chat ID
18
+ * @property {string} label - Human-readable name
19
+ * @property {'interactive'|'notifications'|'logs'} purpose - Room purpose
20
+ * @property {boolean} isDefault - Receives broadcast messages
21
+ * @property {'own'|'all'|string[]} visibility - Task visibility scope
22
+ * @property {'static'|'dynamic'} source - How the room was added
23
+ * @property {string} joinedAt - ISO-8601 timestamp
24
+ */
25
+
26
+ /**
27
+ * @typedef {Object} RoomManagerOptions
28
+ * @property {RoomConfig[]} [rooms] - Initial rooms from config
29
+ */
30
+
31
+ export class RoomManager extends EventEmitter {
32
+ /** @type {Map<string, RoomConfig>} */
33
+ #rooms = new Map();
34
+
35
+ /** @type {Map<string, string>} taskId -> room composite key */
36
+ #taskBindings = new Map();
37
+
38
+ /**
39
+ * @param {RoomManagerOptions} [options]
40
+ */
41
+ constructor(options = {}) {
42
+ super();
43
+ if (options.rooms) {
44
+ for (const room of options.rooms) {
45
+ this.addRoom(room.platform, room.platformRoomId, room);
46
+ }
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Register a room.
52
+ *
53
+ * @param {string} platform
54
+ * @param {string} platformRoomId
55
+ * @param {Partial<RoomConfig>} [options]
56
+ * @returns {RoomConfig}
57
+ */
58
+ addRoom(platform, platformRoomId, options = {}) {
59
+ const id = `${platform}:${platformRoomId}`;
60
+
61
+ if (this.#rooms.has(id)) {
62
+ return this.#rooms.get(id);
63
+ }
64
+
65
+ /** @type {RoomConfig} */
66
+ const room = {
67
+ id,
68
+ platform,
69
+ platformRoomId,
70
+ label: options.label || platformRoomId,
71
+ purpose: options.purpose || 'interactive',
72
+ isDefault: options.isDefault ?? options.is_default ?? false,
73
+ visibility: options.visibility || 'own',
74
+ source: options.source || 'static',
75
+ joinedAt: new Date().toISOString(),
76
+ };
77
+
78
+ this.#rooms.set(id, room);
79
+ this.emit('room:added', room);
80
+ return room;
81
+ }
82
+
83
+ /**
84
+ * Remove a room.
85
+ *
86
+ * @param {string} platform
87
+ * @param {string} platformRoomId
88
+ * @returns {boolean}
89
+ */
90
+ removeRoom(platform, platformRoomId) {
91
+ const id = `${platform}:${platformRoomId}`;
92
+ const room = this.#rooms.get(id);
93
+ if (!room) return false;
94
+
95
+ // Unbind any tasks associated with this room
96
+ for (const [taskId, roomId] of this.#taskBindings) {
97
+ if (roomId === id) {
98
+ this.#taskBindings.delete(taskId);
99
+ }
100
+ }
101
+
102
+ this.#rooms.delete(id);
103
+ this.emit('room:removed', room);
104
+ return true;
105
+ }
106
+
107
+ /**
108
+ * Bind a task to a room so its output is routed there.
109
+ *
110
+ * @param {string} taskId
111
+ * @param {string} platform
112
+ * @param {string} platformRoomId
113
+ */
114
+ bindTask(taskId, platform, platformRoomId) {
115
+ const roomId = `${platform}:${platformRoomId}`;
116
+ this.#taskBindings.set(taskId, roomId);
117
+ this.emit('task:bound', { taskId, roomId });
118
+ }
119
+
120
+ /**
121
+ * Remove a task-room binding.
122
+ *
123
+ * @param {string} taskId
124
+ */
125
+ unbindTask(taskId) {
126
+ this.#taskBindings.delete(taskId);
127
+ }
128
+
129
+ /**
130
+ * Look up which room a task is bound to.
131
+ *
132
+ * @param {string} taskId
133
+ * @returns {RoomConfig|null}
134
+ */
135
+ getRoomForTask(taskId) {
136
+ const roomId = this.#taskBindings.get(taskId);
137
+ if (!roomId) return null;
138
+ return this.#rooms.get(roomId) || null;
139
+ }
140
+
141
+ /**
142
+ * Get all rooms marked as default (receive broadcast messages).
143
+ *
144
+ * @param {string} [platform] - Filter by platform
145
+ * @returns {RoomConfig[]}
146
+ */
147
+ getBroadcastRooms(platform) {
148
+ const rooms = [];
149
+ for (const room of this.#rooms.values()) {
150
+ if (!room.isDefault) continue;
151
+ if (platform && room.platform !== platform) continue;
152
+ rooms.push(room);
153
+ }
154
+ return rooms;
155
+ }
156
+
157
+ /**
158
+ * Get all rooms, optionally filtered by platform.
159
+ *
160
+ * @param {string} [platform]
161
+ * @returns {RoomConfig[]}
162
+ */
163
+ getRooms(platform) {
164
+ if (!platform) return [...this.#rooms.values()];
165
+ return [...this.#rooms.values()].filter(r => r.platform === platform);
166
+ }
167
+
168
+ /**
169
+ * Get a specific room.
170
+ *
171
+ * @param {string} platform
172
+ * @param {string} platformRoomId
173
+ * @returns {RoomConfig|null}
174
+ */
175
+ getRoom(platform, platformRoomId) {
176
+ return this.#rooms.get(`${platform}:${platformRoomId}`) || null;
177
+ }
178
+
179
+ /**
180
+ * Check if a room should see a task's events based on visibility settings.
181
+ *
182
+ * @param {RoomConfig} room
183
+ * @param {string} taskId
184
+ * @returns {boolean}
185
+ */
186
+ shouldSeeTask(room, taskId) {
187
+ if (room.visibility === 'all') return true;
188
+ if (room.visibility === 'own') {
189
+ const boundRoom = this.#taskBindings.get(taskId);
190
+ return boundRoom === room.id;
191
+ }
192
+ if (Array.isArray(room.visibility)) {
193
+ return room.visibility.includes(taskId);
194
+ }
195
+ return false;
196
+ }
197
+
198
+ /**
199
+ * Get rooms that should receive events for a specific task.
200
+ *
201
+ * @param {string} taskId
202
+ * @returns {RoomConfig[]}
203
+ */
204
+ getRoomsForTaskEvent(taskId) {
205
+ const rooms = [];
206
+ for (const room of this.#rooms.values()) {
207
+ if (this.shouldSeeTask(room, taskId)) {
208
+ rooms.push(room);
209
+ }
210
+ }
211
+ return rooms;
212
+ }
213
+
214
+ /** @returns {number} */
215
+ get size() {
216
+ return this.#rooms.size;
217
+ }
218
+
219
+ /** @returns {number} */
220
+ get bindingCount() {
221
+ return this.#taskBindings.size;
222
+ }
223
+
224
+ /**
225
+ * Load rooms from a messaging config section.
226
+ *
227
+ * @param {Object} messagingConfig - The messaging section of daemon config
228
+ */
229
+ loadFromConfig(messagingConfig) {
230
+ if (!messagingConfig) return;
231
+
232
+ for (const [platform, platformConfig] of Object.entries(messagingConfig)) {
233
+ if (!platformConfig?.rooms || !Array.isArray(platformConfig.rooms)) continue;
234
+
235
+ for (const roomDef of platformConfig.rooms) {
236
+ const platformRoomId = roomDef.chat_id || roomDef.channel_id || roomDef.roomId;
237
+ if (!platformRoomId) continue;
238
+
239
+ this.addRoom(platform, platformRoomId, {
240
+ label: roomDef.label || roomDef.name || platformRoomId,
241
+ purpose: roomDef.purpose || 'interactive',
242
+ isDefault: roomDef.is_default ?? roomDef.isDefault ?? false,
243
+ visibility: roomDef.visibility || 'own',
244
+ source: 'static',
245
+ });
246
+ }
247
+ }
248
+ }
249
+
250
+ /**
251
+ * Serialize room state for status reporting.
252
+ *
253
+ * @returns {Object}
254
+ */
255
+ getStatus() {
256
+ const platforms = {};
257
+ for (const room of this.#rooms.values()) {
258
+ if (!platforms[room.platform]) platforms[room.platform] = [];
259
+ platforms[room.platform].push({
260
+ id: room.platformRoomId,
261
+ label: room.label,
262
+ purpose: room.purpose,
263
+ isDefault: room.isDefault,
264
+ boundTasks: [...this.#taskBindings.entries()]
265
+ .filter(([, rid]) => rid === room.id)
266
+ .map(([tid]) => tid),
267
+ });
268
+ }
269
+ return {
270
+ totalRooms: this.#rooms.size,
271
+ totalBindings: this.#taskBindings.size,
272
+ platforms,
273
+ };
274
+ }
275
+ }
276
+
277
+ export default RoomManager;
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Messaging subsystem type definitions and constants.
3
+ *
4
+ * @implements @.aiwg/architecture/adrs/ADR-messaging-bot-mode.md
5
+ */
6
+
7
+ /**
8
+ * Severity levels for events and messages
9
+ * @enum {string}
10
+ */
11
+ export const Severity = Object.freeze({
12
+ INFO: 'info',
13
+ WARNING: 'warning',
14
+ CRITICAL: 'critical',
15
+ });
16
+
17
+ /**
18
+ * Event topics emitted by AIWG subsystems
19
+ * @enum {string}
20
+ */
21
+ export const EventTopic = Object.freeze({
22
+ // Ralph lifecycle
23
+ RALPH_STARTED: 'ralph.started',
24
+ RALPH_ITERATION: 'ralph.iteration',
25
+ RALPH_COMPLETED: 'ralph.completed',
26
+ RALPH_FAILED: 'ralph.failed',
27
+ RALPH_ABORTED: 'ralph.aborted',
28
+
29
+ // HITL gates
30
+ GATE_PENDING: 'gate.pending',
31
+ GATE_APPROVED: 'gate.approved',
32
+ GATE_REJECTED: 'gate.rejected',
33
+ GATE_TIMEOUT: 'gate.timeout',
34
+
35
+ // Security
36
+ SECURITY_CRITICAL: 'security.critical',
37
+ SECURITY_WARNING: 'security.warning',
38
+ SECURITY_SCAN_DONE: 'security.scan_done',
39
+
40
+ // Health
41
+ HEALTH_CHECK: 'health.check',
42
+ HEALTH_DEGRADED: 'health.degraded',
43
+ HEALTH_RECOVERED: 'health.recovered',
44
+
45
+ // Build
46
+ BUILD_FAILED: 'build.failed',
47
+ BUILD_PASSED: 'build.passed',
48
+
49
+ // Daemon
50
+ DAEMON_STARTED: 'daemon.started',
51
+ DAEMON_STOPPING: 'daemon.stopping',
52
+
53
+ // Chat
54
+ CHAT_MESSAGE: 'chat.message',
55
+ CHAT_RESPONSE: 'chat.response',
56
+ CHAT_ERROR: 'chat.error',
57
+ });
58
+
59
+ /**
60
+ * Command permission levels
61
+ * @enum {string}
62
+ */
63
+ export const CommandPermission = Object.freeze({
64
+ READ: 'read',
65
+ WRITE: 'write',
66
+ });
67
+
68
+ /**
69
+ * Default command definitions with permission levels
70
+ */
71
+ export const COMMANDS = Object.freeze({
72
+ status: { permission: CommandPermission.READ, description: 'Show project status' },
73
+ 'ralph-status': { permission: CommandPermission.READ, description: 'Show Ralph loop status' },
74
+ approve: { permission: CommandPermission.WRITE, description: 'Approve a pending HITL gate' },
75
+ reject: { permission: CommandPermission.WRITE, description: 'Reject a pending HITL gate' },
76
+ health: { permission: CommandPermission.READ, description: 'Run health check' },
77
+ help: { permission: CommandPermission.READ, description: 'List available commands' },
78
+ ask: { permission: CommandPermission.READ, description: 'Ask AI a question (e.g., /ask what is our test coverage?)' },
79
+ join: { permission: CommandPermission.WRITE, description: 'Add this chat as a room (dynamic join)' },
80
+ leave: { permission: CommandPermission.WRITE, description: 'Remove this chat from rooms' },
81
+ rooms: { permission: CommandPermission.READ, description: 'List all active rooms' },
82
+ subscribe: { permission: CommandPermission.WRITE, description: 'Subscribe this room to a task (e.g., /subscribe task-0001)' },
83
+ unsubscribe: { permission: CommandPermission.WRITE, description: 'Unsubscribe from a task' },
84
+ });
85
+
86
+ /**
87
+ * Button styles for interactive messages
88
+ * @enum {string}
89
+ */
90
+ export const ButtonStyle = Object.freeze({
91
+ PRIMARY: 'primary',
92
+ DANGER: 'danger',
93
+ DEFAULT: 'default',
94
+ });
@@ -0,0 +1,4 @@
1
+ // Anchor the Pi bridge's policy loader to the installed AIWG package.
2
+ import { importImpl } from '../_resolve-impl.mjs';
3
+
4
+ export const { loadHitlLayers } = await importImpl(import.meta.url, 'policy/hitl-config.js');