@adaptic/maestro 1.10.0 → 1.10.2
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/.env.example +9 -0
- package/bin/maestro.mjs +92 -0
- package/framework-features.json +10 -0
- package/package.json +1 -1
- package/scripts/cadence/launchd-socket-mode-wrapper.sh +95 -0
- package/scripts/healthcheck.sh +15 -9
- package/scripts/local-triggers/generate-plists.sh +15 -0
- package/scripts/local-triggers/generate-plists.test.mjs +19 -7
- package/scripts/poller/slack-socket-mode.mjs +739 -0
- package/scripts/poller/slack-socket-mode.test.mjs +688 -0
- package/scripts/setup/init-slack-socket-mode.mjs +260 -0
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* slack-socket-mode.test.mjs — node:test coverage for slack-socket-mode.mjs
|
|
3
|
+
*
|
|
4
|
+
* Strategy: dependency-inject WebSocket, fetch, and writeInbox so every
|
|
5
|
+
* test runs end-to-end without touching the network or persisting to disk.
|
|
6
|
+
* The mock WebSocket exposes hooks for "Slack sent this envelope" and
|
|
7
|
+
* captures every message the client sends back (for ack assertions).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { test } from "node:test";
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import { promises as fsp } from "node:fs";
|
|
13
|
+
import { writeFileSync, mkdirSync } from "node:fs";
|
|
14
|
+
import { tmpdir } from "node:os";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
shouldKeepEvent,
|
|
19
|
+
eventToInboxItem,
|
|
20
|
+
openSocketConnection,
|
|
21
|
+
nextBackoffMs,
|
|
22
|
+
createSocketModeClient,
|
|
23
|
+
loadAgentIdentity,
|
|
24
|
+
loadPeerSlackIds,
|
|
25
|
+
RECONNECT_INITIAL_MS,
|
|
26
|
+
RECONNECT_MAX_MS,
|
|
27
|
+
} from "./slack-socket-mode.mjs";
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Fixtures
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
const AGENT_SLACK_ID = "UAGENT0001";
|
|
34
|
+
const PEER_SOPHIE = "USOPHIE001";
|
|
35
|
+
const PEER_LUCAS = "ULUCAS0001";
|
|
36
|
+
const PRINCIPAL_SLACK_ID = "UCEO000001";
|
|
37
|
+
const HUMAN_SLACK_ID = "UHUMAN0001";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Build a Socket Mode envelope mimicking what Slack actually sends.
|
|
41
|
+
* Defaults are sensible enough that each test just overrides the fields
|
|
42
|
+
* it cares about.
|
|
43
|
+
*/
|
|
44
|
+
function makeEnvelope(overrides = {}) {
|
|
45
|
+
const event = {
|
|
46
|
+
type: "message",
|
|
47
|
+
user: HUMAN_SLACK_ID,
|
|
48
|
+
text: "hello",
|
|
49
|
+
ts: "1735689600.001000",
|
|
50
|
+
channel: "DCHANNEL01", // DM by default — prefix "D"
|
|
51
|
+
...overrides.event,
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
envelope_id: overrides.envelope_id || "envelope-abc-123",
|
|
55
|
+
type: overrides.type || "events_api",
|
|
56
|
+
accepts_response_payload: false,
|
|
57
|
+
payload: {
|
|
58
|
+
type: "event_callback",
|
|
59
|
+
event,
|
|
60
|
+
event_id: overrides.event_id || "Ev0001",
|
|
61
|
+
event_time: 1735689600,
|
|
62
|
+
},
|
|
63
|
+
...(overrides.extra || {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Create a fresh tmpdir for each test that needs one. Caller is
|
|
69
|
+
* responsible for cleaning it up.
|
|
70
|
+
*/
|
|
71
|
+
async function makeTmpRoot() {
|
|
72
|
+
const path = join(
|
|
73
|
+
tmpdir(),
|
|
74
|
+
`slack-socket-test-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
|
75
|
+
);
|
|
76
|
+
await fsp.mkdir(path, { recursive: true });
|
|
77
|
+
return path;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function rmRoot(path) {
|
|
81
|
+
try { await fsp.rm(path, { recursive: true, force: true }); } catch { /* */ }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
// shouldKeepEvent — filter tests
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
test("shouldKeepEvent keeps DM messages from humans", () => {
|
|
89
|
+
const envelope = makeEnvelope({
|
|
90
|
+
event: { user: HUMAN_SLACK_ID, channel: "DABCDEF01", text: "hey, can you help?" },
|
|
91
|
+
});
|
|
92
|
+
const res = shouldKeepEvent(envelope, {
|
|
93
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
94
|
+
peerSlackIds: new Set([PEER_SOPHIE]),
|
|
95
|
+
});
|
|
96
|
+
assert.equal(res.keep, true, res.reason);
|
|
97
|
+
assert.equal(res.reason, "dm");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("shouldKeepEvent keeps channel @mentions of the agent", () => {
|
|
101
|
+
const envelope = makeEnvelope({
|
|
102
|
+
event: {
|
|
103
|
+
user: HUMAN_SLACK_ID,
|
|
104
|
+
channel: "C0CHANNEL1",
|
|
105
|
+
text: `hey <@${AGENT_SLACK_ID}> can you look at this`,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
const res = shouldKeepEvent(envelope, {
|
|
109
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
110
|
+
peerSlackIds: new Set(),
|
|
111
|
+
});
|
|
112
|
+
assert.equal(res.keep, true);
|
|
113
|
+
assert.equal(res.reason, "mention");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("shouldKeepEvent drops channel messages without @mention", () => {
|
|
117
|
+
const envelope = makeEnvelope({
|
|
118
|
+
event: { user: HUMAN_SLACK_ID, channel: "C0CHANNEL1", text: "general chitchat" },
|
|
119
|
+
});
|
|
120
|
+
const res = shouldKeepEvent(envelope, {
|
|
121
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
122
|
+
peerSlackIds: new Set(),
|
|
123
|
+
});
|
|
124
|
+
assert.equal(res.keep, false);
|
|
125
|
+
assert.equal(res.reason, "no-mention-no-dm");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("shouldKeepEvent drops the agent's own messages even in DMs", () => {
|
|
129
|
+
const envelope = makeEnvelope({
|
|
130
|
+
event: { user: AGENT_SLACK_ID, channel: "DABCDEF01", text: "I sent this myself" },
|
|
131
|
+
});
|
|
132
|
+
const res = shouldKeepEvent(envelope, {
|
|
133
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
134
|
+
peerSlackIds: new Set(),
|
|
135
|
+
});
|
|
136
|
+
assert.equal(res.keep, false);
|
|
137
|
+
assert.equal(res.reason, "self-message");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("shouldKeepEvent drops peer-agent messages even in DMs", () => {
|
|
141
|
+
// Sophie DMing Ravi about something — Ravi should not auto-respond.
|
|
142
|
+
const envelope = makeEnvelope({
|
|
143
|
+
event: { user: PEER_SOPHIE, channel: "DPEER00001", text: "hey ravi, status?" },
|
|
144
|
+
});
|
|
145
|
+
const res = shouldKeepEvent(envelope, {
|
|
146
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
147
|
+
peerSlackIds: new Set([PEER_SOPHIE, PEER_LUCAS]),
|
|
148
|
+
});
|
|
149
|
+
assert.equal(res.keep, false);
|
|
150
|
+
assert.equal(res.reason, "peer-agent");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("shouldKeepEvent drops bot messages", () => {
|
|
154
|
+
const envelope = makeEnvelope({
|
|
155
|
+
event: { user: "", bot_id: "B12345", channel: "C0CHANNEL1", text: "deploy completed" },
|
|
156
|
+
});
|
|
157
|
+
const res = shouldKeepEvent(envelope, {
|
|
158
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
159
|
+
peerSlackIds: new Set(),
|
|
160
|
+
});
|
|
161
|
+
assert.equal(res.keep, false);
|
|
162
|
+
assert.equal(res.reason, "bot-message");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("shouldKeepEvent allows thread_broadcast and file_share subtypes", () => {
|
|
166
|
+
for (const subtype of ["thread_broadcast", "file_share"]) {
|
|
167
|
+
const envelope = makeEnvelope({
|
|
168
|
+
event: {
|
|
169
|
+
user: HUMAN_SLACK_ID,
|
|
170
|
+
channel: "DABCDEF01",
|
|
171
|
+
text: "thread reply also broadcast",
|
|
172
|
+
subtype,
|
|
173
|
+
thread_ts: "1735689500.000100",
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
const res = shouldKeepEvent(envelope, {
|
|
177
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
178
|
+
peerSlackIds: new Set(),
|
|
179
|
+
});
|
|
180
|
+
assert.equal(res.keep, true, `subtype ${subtype} should be kept: ${res.reason}`);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("shouldKeepEvent drops channel_join and other system subtypes", () => {
|
|
185
|
+
const envelope = makeEnvelope({
|
|
186
|
+
event: { user: HUMAN_SLACK_ID, channel: "DABCDEF01", text: "joined", subtype: "channel_join" },
|
|
187
|
+
});
|
|
188
|
+
const res = shouldKeepEvent(envelope, {
|
|
189
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
190
|
+
peerSlackIds: new Set(),
|
|
191
|
+
});
|
|
192
|
+
assert.equal(res.keep, false);
|
|
193
|
+
assert.match(res.reason, /event-subtype-channel_join/);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("shouldKeepEvent rejects non-events_api envelopes", () => {
|
|
197
|
+
const envelope = { envelope_id: "x", type: "slash_commands", payload: {} };
|
|
198
|
+
const res = shouldKeepEvent(envelope, { ownSlackId: AGENT_SLACK_ID, peerSlackIds: new Set() });
|
|
199
|
+
assert.equal(res.keep, false);
|
|
200
|
+
assert.match(res.reason, /envelope-type-slash_commands/);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("shouldKeepEvent handles malformed envelopes safely", () => {
|
|
204
|
+
assert.equal(shouldKeepEvent(null, { ownSlackId: AGENT_SLACK_ID, peerSlackIds: new Set() }).keep, false);
|
|
205
|
+
assert.equal(shouldKeepEvent({}, { ownSlackId: AGENT_SLACK_ID, peerSlackIds: new Set() }).keep, false);
|
|
206
|
+
const noEvent = { type: "events_api", payload: {} };
|
|
207
|
+
assert.equal(shouldKeepEvent(noEvent, { ownSlackId: AGENT_SLACK_ID, peerSlackIds: new Set() }).keep, false);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// ---------------------------------------------------------------------------
|
|
211
|
+
// eventToInboxItem — translation tests
|
|
212
|
+
// ---------------------------------------------------------------------------
|
|
213
|
+
|
|
214
|
+
test("eventToInboxItem: DM event → poller-compatible inbox item", () => {
|
|
215
|
+
const envelope = makeEnvelope({
|
|
216
|
+
event: {
|
|
217
|
+
user: HUMAN_SLACK_ID,
|
|
218
|
+
channel: "DABCDEF01",
|
|
219
|
+
text: "hey, can you draft the board memo?",
|
|
220
|
+
ts: "1735689600.001000",
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
const item = eventToInboxItem(envelope, {
|
|
224
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
225
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
226
|
+
agentFirstName: "Ravi",
|
|
227
|
+
});
|
|
228
|
+
assert.equal(item.id, "1735689600-001000");
|
|
229
|
+
assert.equal(item.service, "slack");
|
|
230
|
+
assert.equal(item.channel_id, "DABCDEF01");
|
|
231
|
+
assert.match(item.channel, /^dm\//);
|
|
232
|
+
assert.match(item.subject, /^DM from /);
|
|
233
|
+
assert.equal(item.content, "hey, can you draft the board memo?");
|
|
234
|
+
assert.equal(item.priority_signals.mentions_agent, true, "DM always mentions agent");
|
|
235
|
+
assert.equal(item.priority_signals.from_ceo, false);
|
|
236
|
+
assert.equal(item.is_reply, false);
|
|
237
|
+
assert.equal(item.raw_ref, "slack:DABCDEF01:1735689600.001000");
|
|
238
|
+
assert.equal(item.source, "socket-mode");
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("eventToInboxItem: channel @mention sets the right priority signals", () => {
|
|
242
|
+
const envelope = makeEnvelope({
|
|
243
|
+
event: {
|
|
244
|
+
user: PRINCIPAL_SLACK_ID,
|
|
245
|
+
channel: "C0CHANNEL1",
|
|
246
|
+
text: `<@${AGENT_SLACK_ID}> urgent — please review the deck`,
|
|
247
|
+
ts: "1735689700.002000",
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
const item = eventToInboxItem(envelope, {
|
|
251
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
252
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
253
|
+
agentFirstName: "Ravi",
|
|
254
|
+
});
|
|
255
|
+
assert.equal(item.priority_signals.from_ceo, true);
|
|
256
|
+
assert.equal(item.priority_signals.tagged_urgent, true);
|
|
257
|
+
assert.equal(item.priority_signals.mentions_agent, true);
|
|
258
|
+
assert.equal(item.channel, "C0CHANNEL1");
|
|
259
|
+
assert.match(item.subject, /^#C0CHANNEL1/);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test("eventToInboxItem: thread reply preserves thread_id + is_reply", () => {
|
|
263
|
+
const envelope = makeEnvelope({
|
|
264
|
+
event: {
|
|
265
|
+
user: HUMAN_SLACK_ID,
|
|
266
|
+
channel: "C0CHANNEL1",
|
|
267
|
+
text: `<@${AGENT_SLACK_ID}> following up on this`,
|
|
268
|
+
ts: "1735689800.003000",
|
|
269
|
+
thread_ts: "1735689500.000100",
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
const item = eventToInboxItem(envelope, {
|
|
273
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
274
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
275
|
+
agentFirstName: "Ravi",
|
|
276
|
+
});
|
|
277
|
+
assert.equal(item.is_reply, true);
|
|
278
|
+
assert.equal(item.thread_id, "1735689500.000100");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test("eventToInboxItem: first-name mention (no @) still flips mentions_agent", () => {
|
|
282
|
+
// "Hey Ravi, can you …" in a channel — no explicit @-mention but the
|
|
283
|
+
// agent's first name appears. The poller uses this same heuristic.
|
|
284
|
+
const envelope = makeEnvelope({
|
|
285
|
+
event: {
|
|
286
|
+
user: HUMAN_SLACK_ID,
|
|
287
|
+
channel: "C0CHANNEL1",
|
|
288
|
+
text: "hey Ravi, what's the latest on the model registry?",
|
|
289
|
+
ts: "1735689900.004000",
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
const item = eventToInboxItem(envelope, {
|
|
293
|
+
ownSlackId: AGENT_SLACK_ID,
|
|
294
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
295
|
+
agentFirstName: "Ravi",
|
|
296
|
+
});
|
|
297
|
+
assert.equal(item.priority_signals.mentions_agent, true);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// ---------------------------------------------------------------------------
|
|
301
|
+
// openSocketConnection — fetch wrapper tests
|
|
302
|
+
// ---------------------------------------------------------------------------
|
|
303
|
+
|
|
304
|
+
test("openSocketConnection returns the URL on success", async () => {
|
|
305
|
+
const fakeFetch = async () => ({
|
|
306
|
+
json: async () => ({ ok: true, url: "wss://wss-primary.slack.com/link/?ticket=abc" }),
|
|
307
|
+
});
|
|
308
|
+
const res = await openSocketConnection({ appToken: "xapp-1-abc", fetchFn: fakeFetch });
|
|
309
|
+
assert.equal(res.url, "wss://wss-primary.slack.com/link/?ticket=abc");
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test("openSocketConnection surfaces Slack errors verbatim", async () => {
|
|
313
|
+
const fakeFetch = async () => ({
|
|
314
|
+
json: async () => ({ ok: false, error: "invalid_auth" }),
|
|
315
|
+
});
|
|
316
|
+
const res = await openSocketConnection({ appToken: "xapp-1-abc", fetchFn: fakeFetch });
|
|
317
|
+
assert.equal(res.error, "invalid_auth");
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("openSocketConnection rejects when token missing", async () => {
|
|
321
|
+
const res = await openSocketConnection({ appToken: "", fetchFn: async () => ({}) });
|
|
322
|
+
assert.equal(res.error, "missing-app-token");
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// ---------------------------------------------------------------------------
|
|
326
|
+
// nextBackoffMs — exponential back-off with jitter
|
|
327
|
+
// ---------------------------------------------------------------------------
|
|
328
|
+
|
|
329
|
+
test("nextBackoffMs doubles up to RECONNECT_MAX_MS", () => {
|
|
330
|
+
let cur = RECONNECT_INITIAL_MS;
|
|
331
|
+
let prev = cur;
|
|
332
|
+
for (let i = 0; i < 8; i++) {
|
|
333
|
+
cur = nextBackoffMs(cur);
|
|
334
|
+
assert.ok(cur >= prev, `backoff should not decrease: ${prev} → ${cur}`);
|
|
335
|
+
assert.ok(cur <= RECONNECT_MAX_MS * 1.5, `backoff capped near max: ${cur}`);
|
|
336
|
+
prev = cur;
|
|
337
|
+
}
|
|
338
|
+
// After many doublings, we're at or near the cap.
|
|
339
|
+
assert.ok(cur >= RECONNECT_MAX_MS * 0.9, `eventually approaches max: ${cur}`);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
// ---------------------------------------------------------------------------
|
|
343
|
+
// MockWebSocket — used by client integration tests
|
|
344
|
+
// ---------------------------------------------------------------------------
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Minimal WebSocket polyfill that captures every send and lets the test
|
|
348
|
+
* synthesise inbound frames. Mirrors the global WebSocket API surface
|
|
349
|
+
* the client uses: readyState (constant 1 = OPEN), addEventListener,
|
|
350
|
+
* send, close.
|
|
351
|
+
*/
|
|
352
|
+
class MockWebSocket {
|
|
353
|
+
static instances = [];
|
|
354
|
+
constructor(url) {
|
|
355
|
+
this.url = url;
|
|
356
|
+
this.readyState = 1; // OPEN
|
|
357
|
+
this.sent = [];
|
|
358
|
+
this.listeners = { open: [], message: [], error: [], close: [] };
|
|
359
|
+
MockWebSocket.instances.push(this);
|
|
360
|
+
// Fire `open` on the next tick so the client gets a chance to attach
|
|
361
|
+
// its listeners.
|
|
362
|
+
queueMicrotask(() => this.dispatch("open", {}));
|
|
363
|
+
}
|
|
364
|
+
addEventListener(type, fn) {
|
|
365
|
+
if (!this.listeners[type]) this.listeners[type] = [];
|
|
366
|
+
this.listeners[type].push(fn);
|
|
367
|
+
}
|
|
368
|
+
dispatch(type, event) {
|
|
369
|
+
for (const fn of this.listeners[type] || []) {
|
|
370
|
+
try { fn(event); } catch (err) { /* swallow */ }
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
send(data) {
|
|
374
|
+
this.sent.push(data);
|
|
375
|
+
}
|
|
376
|
+
close(code = 1000, reason = "") {
|
|
377
|
+
this.readyState = 3; // CLOSED
|
|
378
|
+
this.dispatch("close", { code, reason });
|
|
379
|
+
}
|
|
380
|
+
/** test helper: simulate Slack pushing an envelope. */
|
|
381
|
+
push(envelope) {
|
|
382
|
+
const raw = typeof envelope === "string" ? envelope : JSON.stringify(envelope);
|
|
383
|
+
this.dispatch("message", { data: raw });
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Wait for a predicate to become truthy, polling every 5ms.
|
|
389
|
+
*/
|
|
390
|
+
async function waitFor(predicate, { timeoutMs = 2000 } = {}) {
|
|
391
|
+
const deadline = Date.now() + timeoutMs;
|
|
392
|
+
while (Date.now() < deadline) {
|
|
393
|
+
if (await predicate()) return true;
|
|
394
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// ---------------------------------------------------------------------------
|
|
400
|
+
// Client integration tests
|
|
401
|
+
// ---------------------------------------------------------------------------
|
|
402
|
+
|
|
403
|
+
test("client acks every envelope and writes inbox for DM events", async () => {
|
|
404
|
+
MockWebSocket.instances = [];
|
|
405
|
+
const root = await makeTmpRoot();
|
|
406
|
+
let client;
|
|
407
|
+
try {
|
|
408
|
+
const writes = [];
|
|
409
|
+
client = createSocketModeClient({
|
|
410
|
+
agentRoot: root,
|
|
411
|
+
appToken: "xapp-test-token",
|
|
412
|
+
identity: {
|
|
413
|
+
firstName: "Ravi",
|
|
414
|
+
fullName: "Ravi Patel",
|
|
415
|
+
slackMemberId: AGENT_SLACK_ID,
|
|
416
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
417
|
+
},
|
|
418
|
+
peerSlackIds: new Set([PEER_SOPHIE]),
|
|
419
|
+
fetchFn: async () => ({ json: async () => ({ ok: true, url: "wss://example/ws" }) }),
|
|
420
|
+
WebSocketCtor: MockWebSocket,
|
|
421
|
+
writeInbox: (service, item) => writes.push({ service, item }),
|
|
422
|
+
});
|
|
423
|
+
await client.start();
|
|
424
|
+
await waitFor(() => MockWebSocket.instances.length > 0);
|
|
425
|
+
const ws = MockWebSocket.instances[0];
|
|
426
|
+
|
|
427
|
+
ws.push({ type: "hello", num_connections: 1, debug_info: {} });
|
|
428
|
+
ws.push(makeEnvelope({
|
|
429
|
+
envelope_id: "env-1",
|
|
430
|
+
event: { user: HUMAN_SLACK_ID, channel: "DABC", text: "hey, draft the memo" },
|
|
431
|
+
}));
|
|
432
|
+
|
|
433
|
+
await waitFor(() => writes.length === 1);
|
|
434
|
+
assert.equal(writes.length, 1);
|
|
435
|
+
assert.equal(writes[0].service, "slack");
|
|
436
|
+
assert.equal(writes[0].item.channel_id, "DABC");
|
|
437
|
+
assert.equal(writes[0].item.priority_signals.mentions_agent, true);
|
|
438
|
+
|
|
439
|
+
// One ack for the events_api envelope. The `hello` envelope has no
|
|
440
|
+
// envelope_id so it isn't acked.
|
|
441
|
+
assert.equal(ws.sent.length, 1);
|
|
442
|
+
const ackPayload = JSON.parse(ws.sent[0]);
|
|
443
|
+
assert.equal(ackPayload.envelope_id, "env-1");
|
|
444
|
+
} finally {
|
|
445
|
+
client?.stop();
|
|
446
|
+
await rmRoot(root);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test("client skips bot messages and the agent's own messages without writing inbox", async () => {
|
|
451
|
+
MockWebSocket.instances = [];
|
|
452
|
+
const root = await makeTmpRoot();
|
|
453
|
+
let client;
|
|
454
|
+
try {
|
|
455
|
+
const writes = [];
|
|
456
|
+
client = createSocketModeClient({
|
|
457
|
+
agentRoot: root,
|
|
458
|
+
appToken: "xapp-test-token",
|
|
459
|
+
identity: {
|
|
460
|
+
firstName: "Ravi",
|
|
461
|
+
fullName: "Ravi Patel",
|
|
462
|
+
slackMemberId: AGENT_SLACK_ID,
|
|
463
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
464
|
+
},
|
|
465
|
+
peerSlackIds: new Set(),
|
|
466
|
+
fetchFn: async () => ({ json: async () => ({ ok: true, url: "wss://example/ws" }) }),
|
|
467
|
+
WebSocketCtor: MockWebSocket,
|
|
468
|
+
writeInbox: (service, item) => writes.push({ service, item }),
|
|
469
|
+
});
|
|
470
|
+
await client.start();
|
|
471
|
+
await waitFor(() => MockWebSocket.instances.length > 0);
|
|
472
|
+
const ws = MockWebSocket.instances[0];
|
|
473
|
+
|
|
474
|
+
ws.push(makeEnvelope({
|
|
475
|
+
envelope_id: "env-bot",
|
|
476
|
+
event: { user: "", bot_id: "B1", channel: "C1", text: "auto deploy" },
|
|
477
|
+
}));
|
|
478
|
+
ws.push(makeEnvelope({
|
|
479
|
+
envelope_id: "env-self",
|
|
480
|
+
event: { user: AGENT_SLACK_ID, channel: "DSELF", text: "I sent this" },
|
|
481
|
+
}));
|
|
482
|
+
|
|
483
|
+
// Both get acked but neither hits writeInbox.
|
|
484
|
+
await waitFor(() => ws.sent.length === 2);
|
|
485
|
+
assert.equal(writes.length, 0, "no inbox writes for bot/self messages");
|
|
486
|
+
const ackIds = ws.sent.map((m) => JSON.parse(m).envelope_id).sort();
|
|
487
|
+
assert.deepEqual(ackIds, ["env-bot", "env-self"]);
|
|
488
|
+
} finally {
|
|
489
|
+
client?.stop();
|
|
490
|
+
await rmRoot(root);
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
test("client skips messages from peer agents", async () => {
|
|
495
|
+
MockWebSocket.instances = [];
|
|
496
|
+
const root = await makeTmpRoot();
|
|
497
|
+
let client;
|
|
498
|
+
try {
|
|
499
|
+
const writes = [];
|
|
500
|
+
client = createSocketModeClient({
|
|
501
|
+
agentRoot: root,
|
|
502
|
+
appToken: "xapp-test-token",
|
|
503
|
+
identity: {
|
|
504
|
+
firstName: "Ravi",
|
|
505
|
+
fullName: "Ravi Patel",
|
|
506
|
+
slackMemberId: AGENT_SLACK_ID,
|
|
507
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
508
|
+
},
|
|
509
|
+
peerSlackIds: new Set([PEER_SOPHIE, PEER_LUCAS]),
|
|
510
|
+
fetchFn: async () => ({ json: async () => ({ ok: true, url: "wss://example/ws" }) }),
|
|
511
|
+
WebSocketCtor: MockWebSocket,
|
|
512
|
+
writeInbox: (service, item) => writes.push({ service, item }),
|
|
513
|
+
});
|
|
514
|
+
await client.start();
|
|
515
|
+
await waitFor(() => MockWebSocket.instances.length > 0);
|
|
516
|
+
const ws = MockWebSocket.instances[0];
|
|
517
|
+
|
|
518
|
+
// Peer agent DMs Ravi — drop, but ack so Slack doesn't redeliver.
|
|
519
|
+
ws.push(makeEnvelope({
|
|
520
|
+
envelope_id: "env-peer",
|
|
521
|
+
event: { user: PEER_SOPHIE, channel: "DPEER", text: "fyi from sophie" },
|
|
522
|
+
}));
|
|
523
|
+
|
|
524
|
+
await waitFor(() => ws.sent.length === 1);
|
|
525
|
+
assert.equal(writes.length, 0);
|
|
526
|
+
assert.equal(JSON.parse(ws.sent[0]).envelope_id, "env-peer");
|
|
527
|
+
} finally {
|
|
528
|
+
client?.stop();
|
|
529
|
+
await rmRoot(root);
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
test("client schedules a reconnect on socket close", async () => {
|
|
534
|
+
MockWebSocket.instances = [];
|
|
535
|
+
const root = await makeTmpRoot();
|
|
536
|
+
|
|
537
|
+
// Capture setTimeout calls so we can assert the reconnect was scheduled.
|
|
538
|
+
const scheduled = [];
|
|
539
|
+
const fakeSetTimeout = (fn, ms) => {
|
|
540
|
+
scheduled.push({ fn, ms });
|
|
541
|
+
return { _id: scheduled.length - 1 };
|
|
542
|
+
};
|
|
543
|
+
const fakeClearTimeout = () => { /* noop */ };
|
|
544
|
+
|
|
545
|
+
try {
|
|
546
|
+
const client = createSocketModeClient({
|
|
547
|
+
agentRoot: root,
|
|
548
|
+
appToken: "xapp-test-token",
|
|
549
|
+
identity: {
|
|
550
|
+
firstName: "Ravi",
|
|
551
|
+
fullName: "Ravi Patel",
|
|
552
|
+
slackMemberId: AGENT_SLACK_ID,
|
|
553
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
554
|
+
},
|
|
555
|
+
peerSlackIds: new Set(),
|
|
556
|
+
fetchFn: async () => ({ json: async () => ({ ok: true, url: "wss://example/ws" }) }),
|
|
557
|
+
WebSocketCtor: MockWebSocket,
|
|
558
|
+
writeInbox: () => {},
|
|
559
|
+
setTimeout: fakeSetTimeout,
|
|
560
|
+
clearTimeout: fakeClearTimeout,
|
|
561
|
+
});
|
|
562
|
+
await client.start();
|
|
563
|
+
await waitFor(() => MockWebSocket.instances.length > 0);
|
|
564
|
+
const ws = MockWebSocket.instances[0];
|
|
565
|
+
|
|
566
|
+
// Simulate Slack closing the socket.
|
|
567
|
+
ws.dispatch("close", { code: 1006, reason: "server-restart" });
|
|
568
|
+
|
|
569
|
+
// A reconnect must have been scheduled with the initial backoff.
|
|
570
|
+
const reconnect = scheduled.find((s) => s.ms >= RECONNECT_INITIAL_MS && s.ms <= RECONNECT_MAX_MS * 2);
|
|
571
|
+
assert.ok(reconnect, `expected reconnect scheduled, got: ${JSON.stringify(scheduled.map((s) => s.ms))}`);
|
|
572
|
+
} finally {
|
|
573
|
+
await rmRoot(root);
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test("client handles 'disconnect' envelope by closing + scheduling reconnect", async () => {
|
|
578
|
+
MockWebSocket.instances = [];
|
|
579
|
+
const root = await makeTmpRoot();
|
|
580
|
+
let client;
|
|
581
|
+
try {
|
|
582
|
+
client = createSocketModeClient({
|
|
583
|
+
agentRoot: root,
|
|
584
|
+
appToken: "xapp-test-token",
|
|
585
|
+
identity: {
|
|
586
|
+
firstName: "Ravi",
|
|
587
|
+
fullName: "Ravi Patel",
|
|
588
|
+
slackMemberId: AGENT_SLACK_ID,
|
|
589
|
+
principalSlackId: PRINCIPAL_SLACK_ID,
|
|
590
|
+
},
|
|
591
|
+
peerSlackIds: new Set(),
|
|
592
|
+
fetchFn: async () => ({ json: async () => ({ ok: true, url: "wss://example/ws" }) }),
|
|
593
|
+
WebSocketCtor: MockWebSocket,
|
|
594
|
+
writeInbox: () => {},
|
|
595
|
+
});
|
|
596
|
+
await client.start();
|
|
597
|
+
await waitFor(() => MockWebSocket.instances.length > 0);
|
|
598
|
+
const ws = MockWebSocket.instances[0];
|
|
599
|
+
|
|
600
|
+
// Slack's polite "please reconnect" message.
|
|
601
|
+
ws.push({ type: "disconnect", reason: "refresh_requested" });
|
|
602
|
+
|
|
603
|
+
// Socket should now be closed.
|
|
604
|
+
await waitFor(() => ws.readyState === 3, { timeoutMs: 1000 });
|
|
605
|
+
assert.equal(ws.readyState, 3);
|
|
606
|
+
} finally {
|
|
607
|
+
client?.stop();
|
|
608
|
+
await rmRoot(root);
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
test("client refuses to connect when .emergency-stop present", async () => {
|
|
613
|
+
MockWebSocket.instances = [];
|
|
614
|
+
const root = await makeTmpRoot();
|
|
615
|
+
writeFileSync(join(root, ".emergency-stop"), "");
|
|
616
|
+
try {
|
|
617
|
+
const client = createSocketModeClient({
|
|
618
|
+
agentRoot: root,
|
|
619
|
+
appToken: "xapp-test-token",
|
|
620
|
+
identity: {
|
|
621
|
+
firstName: "Ravi", fullName: "Ravi Patel",
|
|
622
|
+
slackMemberId: AGENT_SLACK_ID, principalSlackId: PRINCIPAL_SLACK_ID,
|
|
623
|
+
},
|
|
624
|
+
peerSlackIds: new Set(),
|
|
625
|
+
fetchFn: async () => ({ json: async () => ({ ok: true, url: "wss://example/ws" }) }),
|
|
626
|
+
WebSocketCtor: MockWebSocket,
|
|
627
|
+
writeInbox: () => {},
|
|
628
|
+
});
|
|
629
|
+
await client.start();
|
|
630
|
+
// Give it a couple of ticks; no socket should be constructed.
|
|
631
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
632
|
+
assert.equal(MockWebSocket.instances.length, 0, "no socket should open under emergency stop");
|
|
633
|
+
client.stop();
|
|
634
|
+
} finally {
|
|
635
|
+
await rmRoot(root);
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
// ---------------------------------------------------------------------------
|
|
640
|
+
// loadAgentIdentity / loadPeerSlackIds — config readers
|
|
641
|
+
// ---------------------------------------------------------------------------
|
|
642
|
+
|
|
643
|
+
test("loadAgentIdentity returns safe defaults for missing config", () => {
|
|
644
|
+
const id = loadAgentIdentity("/no/such/path");
|
|
645
|
+
assert.equal(id.firstName, "Agent");
|
|
646
|
+
assert.equal(id.slackMemberId, "");
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
test("loadAgentIdentity reads firstName + slackMemberId from agent.json", async () => {
|
|
650
|
+
const root = await makeTmpRoot();
|
|
651
|
+
try {
|
|
652
|
+
mkdirSync(join(root, "config"), { recursive: true });
|
|
653
|
+
writeFileSync(
|
|
654
|
+
join(root, "config/agent.json"),
|
|
655
|
+
JSON.stringify({
|
|
656
|
+
firstName: "Ravi", fullName: "Ravi Patel",
|
|
657
|
+
slackMemberId: AGENT_SLACK_ID,
|
|
658
|
+
principal: { slackMemberId: PRINCIPAL_SLACK_ID },
|
|
659
|
+
}),
|
|
660
|
+
);
|
|
661
|
+
const id = loadAgentIdentity(root);
|
|
662
|
+
assert.equal(id.firstName, "Ravi");
|
|
663
|
+
assert.equal(id.slackMemberId, AGENT_SLACK_ID);
|
|
664
|
+
assert.equal(id.principalSlackId, PRINCIPAL_SLACK_ID);
|
|
665
|
+
} finally { await rmRoot(root); }
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
test("loadPeerSlackIds excludes the agent's own slack id", async () => {
|
|
669
|
+
const root = await makeTmpRoot();
|
|
670
|
+
try {
|
|
671
|
+
mkdirSync(join(root, "config"), { recursive: true });
|
|
672
|
+
writeFileSync(
|
|
673
|
+
join(root, "config/known-agents.json"),
|
|
674
|
+
JSON.stringify({
|
|
675
|
+
agents: [
|
|
676
|
+
{ name: "Ravi", slackId: AGENT_SLACK_ID, repo: "ravi-ai" },
|
|
677
|
+
{ name: "Sophie", slackId: PEER_SOPHIE, repo: "sophie-ai" },
|
|
678
|
+
{ name: "Lucas", slackId: PEER_LUCAS, repo: "lucas-ai" },
|
|
679
|
+
],
|
|
680
|
+
}),
|
|
681
|
+
);
|
|
682
|
+
const ids = loadPeerSlackIds(root, AGENT_SLACK_ID);
|
|
683
|
+
assert.equal(ids.size, 2);
|
|
684
|
+
assert.ok(ids.has(PEER_SOPHIE));
|
|
685
|
+
assert.ok(ids.has(PEER_LUCAS));
|
|
686
|
+
assert.ok(!ids.has(AGENT_SLACK_ID));
|
|
687
|
+
} finally { await rmRoot(root); }
|
|
688
|
+
});
|