@astrofoundry/pi-astro 0.40.50 β 0.40.51
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/extensions/astro-discord/arcane-watch-monitor.ts +3 -1
- package/extensions/astro-discord/arcane-watch.test.ts +6 -1
- package/extensions/astro-discord/discord.test.ts +4 -0
- package/extensions/astro-discord/index.ts +7 -1
- package/extensions/astro-discord/rest.ts +5 -0
- package/extensions/astro-discord/security-monitor.test.ts +5 -0
- package/extensions/astro-discord/security-monitor.ts +2 -0
- package/package.json +1 -1
|
@@ -18,6 +18,7 @@ import type { InvestigationRequest } from "./security-monitor.ts";
|
|
|
18
18
|
export interface WatchRest {
|
|
19
19
|
createMessage(channelId: string, message: CreateMessage): Promise<DiscordMessage>;
|
|
20
20
|
startThread(channelId: string, messageId: string, name: string): Promise<{ id: string }>;
|
|
21
|
+
archiveThread(threadId: string): Promise<void>;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export interface WatchDeps {
|
|
@@ -129,7 +130,8 @@ export class ArcaneWatch {
|
|
|
129
130
|
const starter = await this.deps.rest.createMessage(thread.id, { content: "π The arcane specialist reads only; a risky read still asks an owner here. Write in this thread to continue." });
|
|
130
131
|
const owner = this.deps.config().owners[0];
|
|
131
132
|
const ok = await this.deps.investigate({ threadId: thread.id, starterId: starter.id, userId: owner, task });
|
|
132
|
-
if (
|
|
133
|
+
if (ok) await this.deps.rest.archiveThread(thread.id).catch((err) => this.deps.log(`arcane watch: archiving the thread of ${name} failed: ${reason(err)}`));
|
|
134
|
+
else this.deps.log(`arcane watch: the investigation of ${name} did not answer`);
|
|
133
135
|
} catch (err) {
|
|
134
136
|
this.deps.log(`arcane watch: investigation of ${name} failed: ${reason(err)}`);
|
|
135
137
|
await this.post(channelId, { content: `β Could not start the investigation of ${name} (${reason(err)}); ask the arcane specialist in the channel.`, replyTo: messageId });
|
|
@@ -199,6 +199,7 @@ describe("container watch monitor", () => {
|
|
|
199
199
|
function harness() {
|
|
200
200
|
const messages: { channelId: string; message: CreateMessage }[] = [];
|
|
201
201
|
const threads: string[] = [];
|
|
202
|
+
const archived: string[] = [];
|
|
202
203
|
const investigations: InvestigationRequest[] = [];
|
|
203
204
|
const logs: string[] = [];
|
|
204
205
|
const clock = { now: T0 };
|
|
@@ -214,6 +215,9 @@ describe("container watch monitor", () => {
|
|
|
214
215
|
threads.push(messageId);
|
|
215
216
|
return { id: `t${messageId}` };
|
|
216
217
|
},
|
|
218
|
+
archiveThread: async (threadId) => {
|
|
219
|
+
archived.push(threadId);
|
|
220
|
+
},
|
|
217
221
|
},
|
|
218
222
|
runEntry: async (service, caller, args) => {
|
|
219
223
|
expect([service, caller, ...args]).toEqual(["arcane", "arcane-watch", "container-watch"]);
|
|
@@ -229,7 +233,7 @@ describe("container watch monitor", () => {
|
|
|
229
233
|
stopping: () => false,
|
|
230
234
|
now: () => clock.now,
|
|
231
235
|
});
|
|
232
|
-
return { watch, messages, threads, investigations, logs, clock, answer };
|
|
236
|
+
return { watch, messages, threads, archived, investigations, logs, clock, answer };
|
|
233
237
|
}
|
|
234
238
|
|
|
235
239
|
const SCHEDULE = config().reports[0];
|
|
@@ -250,6 +254,7 @@ describe("container watch monitor", () => {
|
|
|
250
254
|
expect(h.investigations).toHaveLength(1);
|
|
251
255
|
expect(h.investigations[0]).toMatchObject({ threadId: "t500", starterId: "501", userId: OWNER });
|
|
252
256
|
expect(h.investigations[0].task).toContain("restarted 3 times");
|
|
257
|
+
expect(h.archived).toEqual(["t500"]);
|
|
253
258
|
});
|
|
254
259
|
|
|
255
260
|
it("posts a host-wide restart once, untagged, and investigates nothing", async () => {
|
|
@@ -302,6 +302,10 @@ describe("rest", () => {
|
|
|
302
302
|
await rest.startThread("c1", "m9", "Rule 100021 on arcane");
|
|
303
303
|
expect(calls[3].url).toBe("https://discord.com/api/v10/channels/c1/messages/m9/threads");
|
|
304
304
|
expect(JSON.parse(calls[3].init.body as string)).toEqual({ name: "Rule 100021 on arcane", auto_archive_duration: 10080 });
|
|
305
|
+
await rest.archiveThread("m9");
|
|
306
|
+
expect(calls[4].url).toBe("https://discord.com/api/v10/channels/m9");
|
|
307
|
+
expect(calls[4].init.method).toBe("PATCH");
|
|
308
|
+
expect(JSON.parse(calls[4].init.body as string)).toEqual({ archived: true });
|
|
305
309
|
});
|
|
306
310
|
});
|
|
307
311
|
|
|
@@ -843,7 +843,7 @@ class Bridge {
|
|
|
843
843
|
const posted = await this.rest.createMessage(report.channelId, { content: autoFailureText(a, problems, rollback, owners), mentionUsers: owners, ...(page ? { replyTo: page.id } : {}) });
|
|
844
844
|
const thread = await this.rest.startThread(report.channelId, posted.id, `Update of ${a.stack}`.slice(0, 100));
|
|
845
845
|
const starter = await this.rest.createMessage(thread.id, { content: "π The foreman reads only and asks the specialists for evidence. Write here to continue with the arcane specialist." });
|
|
846
|
-
void this.investigateIn("foreman", { threadId: thread.id, starterId: starter.id, userId: owners[0], task: foremanTask(a, problems, rollback, Date.now()) });
|
|
846
|
+
void this.investigateIn("foreman", { threadId: thread.id, starterId: starter.id, userId: owners[0], task: foremanTask(a, problems, rollback, Date.now()) }).then((ok) => (ok ? this.archiveThread(thread.id) : undefined));
|
|
847
847
|
} catch (err) {
|
|
848
848
|
log(`arcane auto: reporting ${a.stack} failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
849
849
|
}
|
|
@@ -1192,6 +1192,11 @@ class Bridge {
|
|
|
1192
1192
|
}
|
|
1193
1193
|
}
|
|
1194
1194
|
|
|
1195
|
+
/** A thread whose work is done leaves the channel list; a failure only costs the 7-day automatic archive. */
|
|
1196
|
+
private async archiveThread(threadId: string): Promise<void> {
|
|
1197
|
+
await this.rest.archiveThread(threadId).catch((err) => log(`discord: archiving thread ${threadId} failed: ${err instanceof Error ? err.message : String(err)}`));
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1195
1200
|
private rememberCarefulThread(report: ArcaneReport, stack: string, threadId: string): void {
|
|
1196
1201
|
const current = readPostedArcane();
|
|
1197
1202
|
const target = current && current.date === report.date ? current : report;
|
|
@@ -1214,6 +1219,7 @@ class Bridge {
|
|
|
1214
1219
|
const outcome = stackOutcome(ran.answer);
|
|
1215
1220
|
await this.markArcane(report, entry.stack, outcome.state, outcome.detail);
|
|
1216
1221
|
log(`arcane: careful ${entry.stack} ${outcome.detail}`);
|
|
1222
|
+
await this.archiveThread(threadId);
|
|
1217
1223
|
if (outcome.state !== "updated") return;
|
|
1218
1224
|
const applied: Applied = { stack: entry.stack, finishedAt: ran.at, detail: outcome.detail, ...(outcome.commit ? { commit: outcome.commit } : {}) };
|
|
1219
1225
|
void this.superviseArcane(report, [applied]).catch((err) => log(`arcane: supervising ${entry.stack} failed: ${err instanceof Error ? err.message : String(err)}`));
|
|
@@ -113,6 +113,11 @@ export class DiscordRest {
|
|
|
113
113
|
return (await this.call("POST", `/channels/${channelId}/messages/${messageId}/threads`, JSON.stringify({ name: name.slice(0, 100), auto_archive_duration: 10080 }), "application/json")).json() as Promise<{ id: string }>;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
/** Takes a thread the bot started out of the channel list once its work is done; it stays readable, and any new message reopens it. */
|
|
117
|
+
async archiveThread(threadId: string): Promise<void> {
|
|
118
|
+
await this.call("PATCH", `/channels/${threadId}`, JSON.stringify({ archived: true }), "application/json");
|
|
119
|
+
}
|
|
120
|
+
|
|
116
121
|
/** Shows the "Cortex is typingβ¦" indicator for about ten seconds. */
|
|
117
122
|
async triggerTyping(channelId: string): Promise<void> {
|
|
118
123
|
await this.call("POST", `/channels/${channelId}/typing`);
|
|
@@ -32,6 +32,7 @@ class FakeRest implements SecurityRest {
|
|
|
32
32
|
replies: string[] = [];
|
|
33
33
|
deferred = 0;
|
|
34
34
|
threads: string[] = [];
|
|
35
|
+
archived: string[] = [];
|
|
35
36
|
failComponents = false;
|
|
36
37
|
private next = 100;
|
|
37
38
|
|
|
@@ -57,6 +58,9 @@ class FakeRest implements SecurityRest {
|
|
|
57
58
|
this.threads.push(messageId);
|
|
58
59
|
return { id: messageId };
|
|
59
60
|
}
|
|
61
|
+
async archiveThread(threadId: string) {
|
|
62
|
+
this.archived.push(threadId);
|
|
63
|
+
}
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
function wazuh(over: Partial<WazuhEvent>): WazuhEvent {
|
|
@@ -257,6 +261,7 @@ describe("security monitor", () => {
|
|
|
257
261
|
expect(h.investigations[0].task).toContain("Read only: change nothing.");
|
|
258
262
|
expect(h.rest.messages[0].channelId).toBe(messageId);
|
|
259
263
|
expect(readSecurityState().alerts[0]).toMatchObject({ investigation: "done", threadId: messageId });
|
|
264
|
+
expect(h.rest.archived).toEqual([messageId]);
|
|
260
265
|
expect(h.rest.edits.at(-1)?.components[1].components?.[0].label).toBe("Investigate again");
|
|
261
266
|
await h.monitor.onButton({ interactionId: "i", token: "t", userId: OWNER, messageId, customId: "security:investigate" });
|
|
262
267
|
expect(h.rest.threads).toHaveLength(1);
|
|
@@ -61,6 +61,7 @@ export interface SecurityRest {
|
|
|
61
61
|
ephemeralReply(interactionId: string, interactionToken: string, content: string): Promise<void>;
|
|
62
62
|
deferUpdate(interactionId: string, interactionToken: string): Promise<void>;
|
|
63
63
|
startThread(channelId: string, messageId: string, name: string): Promise<{ id: string }>;
|
|
64
|
+
archiveThread(threadId: string): Promise<void>;
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
export interface InvestigationRequest {
|
|
@@ -616,6 +617,7 @@ export class SecurityMonitor {
|
|
|
616
617
|
this.deps.log(`security: investigation of ${alert.key} failed: ${reason(err)}`);
|
|
617
618
|
}
|
|
618
619
|
await this.setInvestigation(alert.messageId, ok ? "done" : "failed", threadId);
|
|
620
|
+
if (ok) await this.deps.rest.archiveThread(threadId).catch((err) => this.deps.log(`security: archiving the thread of ${alert.key} failed: ${reason(err)}`));
|
|
619
621
|
}
|
|
620
622
|
|
|
621
623
|
/** An investigation a restart cut off can be started again. */
|