shadok-ai 0.19.6 → 0.19.8
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/README.md +19 -0
- package/context/report-skill/SKILL.md +48 -0
- package/context/report-skill/scripts/report.mjs +60 -0
- package/dist/agent-reports.d.ts +102 -0
- package/dist/agent-reports.js +161 -0
- package/dist/agent-reports.js.map +1 -0
- package/dist/rooms.d.ts +135 -0
- package/dist/rooms.js +237 -0
- package/dist/rooms.js.map +1 -0
- package/dist/server.js +386 -6
- package/dist/server.js.map +1 -1
- package/dist/telegram.d.ts +27 -0
- package/dist/telegram.js +163 -3
- package/dist/telegram.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +499 -3
package/README.md
CHANGED
|
@@ -76,6 +76,15 @@ Enter to skip; you can add it later from the web UI).
|
|
|
76
76
|
- **Channels** — one `claude` process each, running in parallel. Renaming,
|
|
77
77
|
grouping, and closing all sync live between the browser and Telegram: it's
|
|
78
78
|
one list, server-owned, not two copies.
|
|
79
|
+
- **Rooms** — a channel with **no agent**, where people talk: same column,
|
|
80
|
+
same groups, same chat, nothing running behind it and no quota spent. **+ new
|
|
81
|
+
room** under the agents; an admin can mirror one into a Telegram topic, and a
|
|
82
|
+
message there lands in the room (commands answer that there is no agent
|
|
83
|
+
here). Closing a room archives it: it leaves the list for everyone and its
|
|
84
|
+
messages stay on disk. The column tells the three kinds apart at a glance —
|
|
85
|
+
an **agent** is a round avatar with initials, a **room** a square slate tile
|
|
86
|
+
with a speech bubble (and no presence dot), and an agent with **scheduled
|
|
87
|
+
prompts** wears a dashed ring and a clock.
|
|
79
88
|
- **Groups** — on a desktop the groups live in their own **left column**: pick
|
|
80
89
|
one to narrow the agents pane to it, and the global tools (Profiles, Ledger,
|
|
81
90
|
Secrets, Users, Security, Telegram, Sound) stay in the header at the top. Each
|
|
@@ -326,6 +335,15 @@ Enter to skip; you can add it later from the web UI).
|
|
|
326
335
|
otherwise just print a local path you cannot open. **Artifacts are denied**
|
|
327
336
|
cockpit-wide for the same reason — a page on claude.ai is not the file you
|
|
328
337
|
asked for (`SHADOK_ALLOW_ARTIFACTS=1` lifts it).
|
|
338
|
+
- **Agents can ask an admin for help** — an agent stuck on something only a
|
|
339
|
+
human can fix (a Linear token that expired, no access to the Slack token it
|
|
340
|
+
needs, a Claude API key answering 401, a bug) files a report through the
|
|
341
|
+
`shadok-report` skill instead of failing silently. Every admin gets a badge
|
|
342
|
+
and a sound on the **Reports** button (🚩 in the header), and the Telegram
|
|
343
|
+
owner gets a DM. The panel shows each report with what the agent was doing,
|
|
344
|
+
merges a repeated failure into one card (`×3`), opens the vault on the
|
|
345
|
+
credential named (`--secret LINEAR_API_KEY`) and lets you mark it resolved.
|
|
346
|
+
Agents never put a secret's value in a report, only its name.
|
|
329
347
|
- **Photos and videos, viewable** — an image or a **video** (mp4/webm/mov…) in
|
|
330
348
|
the chat renders inline (a video gets a real player), and clicking a photo
|
|
331
349
|
opens it full-size in a lightbox. The 🖼 button in an agent's bar opens a
|
|
@@ -1306,6 +1324,7 @@ client, or immediately on an explicit `stop` (which ends it for everyone).
|
|
|
1306
1324
|
| `{type:"term-attach"}` · `{type:"term-detach"}` | **experimental, tmux only** — open/close the pane's raw byte pipe |
|
|
1307
1325
|
| `{type:"term-input", data}` · `{type:"term-resize", cols, rows}` | raw input (base64) / match the pane to the viewport |
|
|
1308
1326
|
| `{type:"stop", sessionId?}` | ends the session for all clients; `sessionId` targets another channel (zombie cleanup) |
|
|
1327
|
+
| `{type:"room-join", roomId}` · `{type:"room-post", roomId, text, clientId?}` | a ROOM link (people only, same-origin browser): join a room (answered with `room-history`), then post to it (fanned out as `room-message`, refusals as `room-error`). A connection is an agent link or a room link, never both — `start` is refused after `room-join` |
|
|
1309
1328
|
|
|
1310
1329
|
**server → client**
|
|
1311
1330
|
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: shadok-report
|
|
3
|
+
description: Ask an admin for help when you are STUCK on something only a human can fix — a token that expired or answers 401/403 (Linear, Slack, GitHub, the Claude API…), a credential or access you were never given, a broken tool or a shadok bug. The admins get a notification. Use it instead of failing silently or retrying for ever.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# shadok-report
|
|
7
|
+
|
|
8
|
+
Nobody may be watching your chat — you may be running on a schedule, or the
|
|
9
|
+
person who started you may have left. When you hit a wall only a human can
|
|
10
|
+
remove, **report it**: every admin of this cockpit gets a badge and a sound, and
|
|
11
|
+
the Telegram owner gets a DM.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
node ~/.claude/skills/shadok-report/scripts/report.mjs --kind access --secret LINEAR_API_KEY \
|
|
15
|
+
"Linear token expired" <<'BODY'
|
|
16
|
+
GET https://api.linear.app/graphql → 401 "Authentication required".
|
|
17
|
+
I was triaging the sprint board; nothing was written.
|
|
18
|
+
BODY
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- **title** (the argument): one line, what is wrong. Same title again = the
|
|
22
|
+
same report (the admins see `×2`, not a second card), so keep it stable while
|
|
23
|
+
the problem is the same.
|
|
24
|
+
- **`--kind`**: `access` (a credential missing, expired or refused), `bug`
|
|
25
|
+
(something is broken — the default), `blocked` (anything else stopping you).
|
|
26
|
+
- **`--secret NAME`**: with `access`, the environment variable concerned
|
|
27
|
+
(`LINEAR_API_KEY`, `SLACK_BOT_TOKEN`, `ANTHROPIC_API_KEY`…). The admin gets a
|
|
28
|
+
button that opens the vault on that name. The **name only — never the value.**
|
|
29
|
+
- **body** (stdin as above, or `--body "…"`, optional): the error you got, what you were doing, what
|
|
30
|
+
you already tried. Short and factual.
|
|
31
|
+
|
|
32
|
+
## Rules
|
|
33
|
+
|
|
34
|
+
- **Never put a secret value in a report** — no token, no key, no password,
|
|
35
|
+
not even partially. Name the variable; that is enough.
|
|
36
|
+
- **Report once, then move on.** Don't loop on the same failure: report, tell
|
|
37
|
+
the person in your chat (if any) that you asked an admin, and do whatever
|
|
38
|
+
else you can without that access. A reload may be needed once the admin
|
|
39
|
+
fixes the secret — they will tell you, or reload you.
|
|
40
|
+
- **Not for questions the person in front of you can answer.** If someone is
|
|
41
|
+
talking to you, ask them first. This is for what nobody in the conversation
|
|
42
|
+
can fix.
|
|
43
|
+
|
|
44
|
+
## If it fails
|
|
45
|
+
|
|
46
|
+
`403` means your session key is missing or you predate this feature: say so in
|
|
47
|
+
your chat and ask for *Reload agent*. `429` means you already filed many reports
|
|
48
|
+
this hour: stop filing, wait.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Ask an admin for help: POST /reports with this agent's session key.
|
|
3
|
+
// A script rather than a raw curl so the body can come from stdin without any
|
|
4
|
+
// quoting trap, and so a refusal is said plainly.
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
const die = (m) => { console.error(m); process.exit(1); };
|
|
7
|
+
const USAGE = 'usage: report.mjs [--kind access|bug|blocked] [--secret ENV_NAME] [--body "…"] "title" [<<< body]';
|
|
8
|
+
|
|
9
|
+
let kind, secret, bodyArg;
|
|
10
|
+
const rest = [];
|
|
11
|
+
for (let i = 0; i < args.length; i++) {
|
|
12
|
+
if (args[i] === "--kind" || args[i] === "-k") kind = args[++i];
|
|
13
|
+
else if (args[i] === "--secret" || args[i] === "-s") secret = args[++i];
|
|
14
|
+
else if (args[i] === "--body" || args[i] === "-b") bodyArg = args[++i];
|
|
15
|
+
else if (args[i] === "--help" || args[i] === "-h") die(USAGE);
|
|
16
|
+
else rest.push(args[i]);
|
|
17
|
+
}
|
|
18
|
+
const title = rest.join(" ").trim();
|
|
19
|
+
if (!title) die(USAGE);
|
|
20
|
+
|
|
21
|
+
// The body comes from `--body` or from stdin. stdin is read only when it is not
|
|
22
|
+
// a TTY AND something actually arrives quickly: an agent's Bash tool hands the
|
|
23
|
+
// script a pipe that is never written nor closed, and a plain `for await` over
|
|
24
|
+
// it hung for ever (measured). Once data starts, it is read to the end.
|
|
25
|
+
async function readStdin() {
|
|
26
|
+
if (process.stdin.isTTY) return "";
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
let buf = "";
|
|
29
|
+
let started = false;
|
|
30
|
+
const done = () => { clearTimeout(idle); process.stdin.pause(); process.stdin.removeAllListeners(); resolve(buf); };
|
|
31
|
+
const idle = setTimeout(() => { if (!started) done(); }, 400);
|
|
32
|
+
process.stdin.setEncoding("utf8");
|
|
33
|
+
process.stdin.on("data", (c) => { started = true; buf += c; });
|
|
34
|
+
process.stdin.on("end", done);
|
|
35
|
+
process.stdin.on("error", done);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const body = bodyArg ?? (await readStdin());
|
|
39
|
+
|
|
40
|
+
const port = process.env.SHADOK_PORT;
|
|
41
|
+
const key = process.env.SHADOK_SESSION_KEY;
|
|
42
|
+
if (!port) die("Not inside a shadok-ai agent (SHADOK_PORT unset).");
|
|
43
|
+
if (!key) die("SHADOK_SESSION_KEY is unset — say so in your chat and ask the human to reload this agent.");
|
|
44
|
+
|
|
45
|
+
let res;
|
|
46
|
+
try {
|
|
47
|
+
res = await fetch(`http://127.0.0.1:${port}/reports`, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: { "content-type": "application/json", "x-shadok-session-key": key },
|
|
50
|
+
body: JSON.stringify({ title, ...(kind ? { kind } : {}), ...(secret ? { secret } : {}), ...(body.trim() ? { body } : {}) }),
|
|
51
|
+
signal: AbortSignal.timeout(30_000),
|
|
52
|
+
});
|
|
53
|
+
} catch (e) {
|
|
54
|
+
die(`The cockpit did not answer (${e?.message ?? e}). Say so in your chat instead.`);
|
|
55
|
+
}
|
|
56
|
+
const out = await res.json().catch(() => ({}));
|
|
57
|
+
if (!res.ok) die(`Refused (${res.status}): ${out.error ?? "no reason given"}`);
|
|
58
|
+
const how = ["the cockpit's Reports panel", out.notified?.telegram ? "a Telegram DM to the owner" : null].filter(Boolean).join(" and ");
|
|
59
|
+
process.stdin.destroy();
|
|
60
|
+
console.log(`reported${out.merged ? ` again (×${out.count})` : ""} — the admins were told through ${how}.`);
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/** What kind of help is asked. `access` is the common one: a credential that is
|
|
2
|
+
* missing, expired or refused — which an admin fixes in the vault. */
|
|
3
|
+
export declare const REPORT_KINDS: readonly ["access", "bug", "blocked"];
|
|
4
|
+
export type ReportKind = (typeof REPORT_KINDS)[number];
|
|
5
|
+
export interface AgentReport {
|
|
6
|
+
id: string;
|
|
7
|
+
/** First filed. */
|
|
8
|
+
at: number;
|
|
9
|
+
/** Last filed again (a duplicate bumps it and `count`). */
|
|
10
|
+
lastAt: number;
|
|
11
|
+
sessionId: string;
|
|
12
|
+
/** The agent's display name and group at filing time — the agent may be gone
|
|
13
|
+
* by the time someone reads it. */
|
|
14
|
+
agent: string;
|
|
15
|
+
group?: string | null;
|
|
16
|
+
profile?: string | null;
|
|
17
|
+
kind: ReportKind;
|
|
18
|
+
title: string;
|
|
19
|
+
body?: string;
|
|
20
|
+
/** For `access`: the environment variable / vault name concerned, so the panel
|
|
21
|
+
* can open the vault on it. */
|
|
22
|
+
secret?: string;
|
|
23
|
+
count: number;
|
|
24
|
+
status: "open" | "resolved";
|
|
25
|
+
resolvedBy?: string;
|
|
26
|
+
resolvedAt?: number;
|
|
27
|
+
/** When the owner was last DMed about it (a duplicate re-notifies at most hourly). */
|
|
28
|
+
notifiedAt?: number;
|
|
29
|
+
/** Admins who have opened the panel since it was (re)filed. */
|
|
30
|
+
seenBy?: string[];
|
|
31
|
+
}
|
|
32
|
+
export declare const TITLE_MAX = 200;
|
|
33
|
+
export declare const BODY_MAX = 8000;
|
|
34
|
+
/** A runaway agent must not flood the admins: new reports per agent per hour. */
|
|
35
|
+
export declare const MAX_NEW_PER_HOUR = 10;
|
|
36
|
+
/** A duplicate re-notifies at most this often. */
|
|
37
|
+
export declare const RENOTIFY_MS = 3600000;
|
|
38
|
+
export declare const MAX_REPORTS = 500;
|
|
39
|
+
/** Resolved reports are kept this long, open ones until resolved. */
|
|
40
|
+
export declare const RESOLVED_TTL_MS: number;
|
|
41
|
+
export type ReportInput = {
|
|
42
|
+
kind: ReportKind;
|
|
43
|
+
title: string;
|
|
44
|
+
body?: string;
|
|
45
|
+
secret?: string;
|
|
46
|
+
};
|
|
47
|
+
/** Validate what an agent posted. Never echoes the body back. */
|
|
48
|
+
export declare function reportInput(raw: unknown): {
|
|
49
|
+
ok: true;
|
|
50
|
+
input: ReportInput;
|
|
51
|
+
} | {
|
|
52
|
+
ok: false;
|
|
53
|
+
error: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Refuse a NEW report from an agent that already filed `MAX_NEW_PER_HOUR` in the
|
|
57
|
+
* last hour. Duplicates are not counted (they merge), so an agent retrying the
|
|
58
|
+
* same failure in a loop is never refused — it just bumps the count.
|
|
59
|
+
*/
|
|
60
|
+
export declare function rateRefusal(list: AgentReport[], sessionId: string, now: number): string | null;
|
|
61
|
+
/**
|
|
62
|
+
* File a report. An OPEN report from the same agent with the same title is the
|
|
63
|
+
* same problem seen again: it is bumped (count, lastAt, latest body, unread
|
|
64
|
+
* again), never duplicated.
|
|
65
|
+
*/
|
|
66
|
+
export declare function fileReport(list: AgentReport[], input: ReportInput, who: {
|
|
67
|
+
sessionId: string;
|
|
68
|
+
agent: string;
|
|
69
|
+
group?: string | null;
|
|
70
|
+
profile?: string | null;
|
|
71
|
+
}, now: number, id?: string): {
|
|
72
|
+
list: AgentReport[];
|
|
73
|
+
report: AgentReport;
|
|
74
|
+
merged: boolean;
|
|
75
|
+
};
|
|
76
|
+
/** DM the owner for a new report; for a duplicate, only if the last DM is an hour old. */
|
|
77
|
+
export declare function shouldNotify(r: AgentReport, merged: boolean, now: number): boolean;
|
|
78
|
+
export interface ReportsInbox {
|
|
79
|
+
/** Open first (newest activity first), then resolved. */
|
|
80
|
+
reports: (AgentReport & {
|
|
81
|
+
unread: boolean;
|
|
82
|
+
})[];
|
|
83
|
+
/** Open reports this admin has not seen since they were (re)filed. */
|
|
84
|
+
badge: number;
|
|
85
|
+
}
|
|
86
|
+
export declare function reportsInbox(list: AgentReport[], viewer: string): ReportsInbox;
|
|
87
|
+
export declare function markReportsSeen(list: AgentReport[], viewer: string): AgentReport[];
|
|
88
|
+
/** Resolve (or reopen) one report. */
|
|
89
|
+
export declare function resolveReport(list: AgentReport[], id: string, by: string, now: number, reopen?: boolean): {
|
|
90
|
+
ok: true;
|
|
91
|
+
list: AgentReport[];
|
|
92
|
+
report: AgentReport;
|
|
93
|
+
} | {
|
|
94
|
+
ok: false;
|
|
95
|
+
error: string;
|
|
96
|
+
};
|
|
97
|
+
export declare const kindLabel: (k: ReportKind) => string;
|
|
98
|
+
/** The Telegram DM. Short: the panel holds the detail. */
|
|
99
|
+
export declare function notificationText(r: AgentReport, cockpitTitle?: string | null): string;
|
|
100
|
+
export declare function reportsFile(cwd?: string): string;
|
|
101
|
+
export declare function loadReports(file?: string): AgentReport[];
|
|
102
|
+
export declare function saveReports(list: AgentReport[], file?: string): void;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent reports — the place an agent that is STUCK goes to ask an admin for
|
|
3
|
+
* help: an expired Linear token, no access to the Slack token it was told to
|
|
4
|
+
* use, a Claude API key answering 401, a bug in shadok itself.
|
|
5
|
+
*
|
|
6
|
+
* Before this, the only way out was to say it in its own chat, which nobody
|
|
7
|
+
* reads if nobody is watching that agent — so a cron agent blocked on a dead
|
|
8
|
+
* token failed every hour, silently, for days. A report lands in ONE place every
|
|
9
|
+
* admin sees (the Reports panel, with a badge), and the Telegram owner gets a DM.
|
|
10
|
+
*
|
|
11
|
+
* Spec: docs/superpowers/specs/2026-11-10-agent-reports.md. Pure cores
|
|
12
|
+
* (`reportInput`, `fileReport`, `rateRefusal`, `shouldNotify`, `reportsInbox`,
|
|
13
|
+
* `markReportsSeen`, `resolveReport`, `notificationText`) are tested; the file
|
|
14
|
+
* I/O is the thin part.
|
|
15
|
+
*/
|
|
16
|
+
import { randomBytes } from "node:crypto";
|
|
17
|
+
import fs from "node:fs";
|
|
18
|
+
import os from "node:os";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import { instanceKey } from "./paths.js";
|
|
21
|
+
/** What kind of help is asked. `access` is the common one: a credential that is
|
|
22
|
+
* missing, expired or refused — which an admin fixes in the vault. */
|
|
23
|
+
export const REPORT_KINDS = ["access", "bug", "blocked"];
|
|
24
|
+
export const TITLE_MAX = 200;
|
|
25
|
+
export const BODY_MAX = 8000;
|
|
26
|
+
/** A runaway agent must not flood the admins: new reports per agent per hour. */
|
|
27
|
+
export const MAX_NEW_PER_HOUR = 10;
|
|
28
|
+
/** A duplicate re-notifies at most this often. */
|
|
29
|
+
export const RENOTIFY_MS = 3600_000;
|
|
30
|
+
export const MAX_REPORTS = 500;
|
|
31
|
+
/** Resolved reports are kept this long, open ones until resolved. */
|
|
32
|
+
export const RESOLVED_TTL_MS = 30 * 24 * 3600_000;
|
|
33
|
+
/** Same shape as the vault's names (src/secrets.ts): what an env var can be. */
|
|
34
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/;
|
|
35
|
+
/** Validate what an agent posted. Never echoes the body back. */
|
|
36
|
+
export function reportInput(raw) {
|
|
37
|
+
const b = (raw ?? {});
|
|
38
|
+
const title = typeof b.title === "string" ? b.title.trim().replace(/\s+/g, " ") : "";
|
|
39
|
+
if (!title)
|
|
40
|
+
return { ok: false, error: "a report needs a title: one line saying what is wrong" };
|
|
41
|
+
if (title.length > TITLE_MAX)
|
|
42
|
+
return { ok: false, error: `title too long (max ${TITLE_MAX} characters) — put the detail in the body` };
|
|
43
|
+
const kind = b.kind === undefined || b.kind === "" ? "bug" : b.kind;
|
|
44
|
+
if (!REPORT_KINDS.includes(kind))
|
|
45
|
+
return { ok: false, error: `kind must be one of ${REPORT_KINDS.join(", ")}` };
|
|
46
|
+
const body = typeof b.body === "string" ? b.body.trim() : "";
|
|
47
|
+
if (body.length > BODY_MAX)
|
|
48
|
+
return { ok: false, error: `body too long (max ${BODY_MAX} characters)` };
|
|
49
|
+
const secret = typeof b.secret === "string" ? b.secret.trim() : "";
|
|
50
|
+
if (secret && !ENV_NAME.test(secret))
|
|
51
|
+
return { ok: false, error: "secret must be an environment variable NAME (e.g. LINEAR_API_KEY), never its value" };
|
|
52
|
+
return { ok: true, input: { kind: kind, title, ...(body ? { body } : {}), ...(secret ? { secret } : {}) } };
|
|
53
|
+
}
|
|
54
|
+
const norm = (s) => s.toLowerCase().replace(/\s+/g, " ").trim();
|
|
55
|
+
/** Drop what has aged out: resolved reports past their TTL, then the oldest beyond the cap. */
|
|
56
|
+
function prune(list, now) {
|
|
57
|
+
const kept = list.filter((r) => r.status === "open" || now - (r.resolvedAt ?? r.lastAt) <= RESOLVED_TTL_MS);
|
|
58
|
+
return kept.length > MAX_REPORTS ? [...kept].sort((a, b) => a.lastAt - b.lastAt).slice(-MAX_REPORTS) : kept;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Refuse a NEW report from an agent that already filed `MAX_NEW_PER_HOUR` in the
|
|
62
|
+
* last hour. Duplicates are not counted (they merge), so an agent retrying the
|
|
63
|
+
* same failure in a loop is never refused — it just bumps the count.
|
|
64
|
+
*/
|
|
65
|
+
export function rateRefusal(list, sessionId, now) {
|
|
66
|
+
const recent = list.filter((r) => r.sessionId === sessionId && now - r.at < 3600_000).length;
|
|
67
|
+
return recent >= MAX_NEW_PER_HOUR ? `too many reports from this agent in the last hour (max ${MAX_NEW_PER_HOUR}) — wait for an admin` : null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* File a report. An OPEN report from the same agent with the same title is the
|
|
71
|
+
* same problem seen again: it is bumped (count, lastAt, latest body, unread
|
|
72
|
+
* again), never duplicated.
|
|
73
|
+
*/
|
|
74
|
+
export function fileReport(list, input, who, now, id) {
|
|
75
|
+
const same = list.find((r) => r.status === "open" && r.sessionId === who.sessionId && norm(r.title) === norm(input.title));
|
|
76
|
+
if (same) {
|
|
77
|
+
const report = {
|
|
78
|
+
...same,
|
|
79
|
+
lastAt: now,
|
|
80
|
+
count: same.count + 1,
|
|
81
|
+
kind: input.kind,
|
|
82
|
+
...(input.body ? { body: input.body } : {}),
|
|
83
|
+
...(input.secret ? { secret: input.secret } : {}),
|
|
84
|
+
seenBy: [],
|
|
85
|
+
};
|
|
86
|
+
return { list: prune(list.map((r) => (r.id === same.id ? report : r)), now), report, merged: true };
|
|
87
|
+
}
|
|
88
|
+
const report = {
|
|
89
|
+
id: id ?? randomBytes(9).toString("base64url"),
|
|
90
|
+
at: now,
|
|
91
|
+
lastAt: now,
|
|
92
|
+
sessionId: who.sessionId,
|
|
93
|
+
agent: who.agent,
|
|
94
|
+
...(who.group !== undefined ? { group: who.group } : {}),
|
|
95
|
+
...(who.profile ? { profile: who.profile } : {}),
|
|
96
|
+
...input,
|
|
97
|
+
count: 1,
|
|
98
|
+
status: "open",
|
|
99
|
+
seenBy: [],
|
|
100
|
+
};
|
|
101
|
+
return { list: prune([...list, report], now), report, merged: false };
|
|
102
|
+
}
|
|
103
|
+
/** DM the owner for a new report; for a duplicate, only if the last DM is an hour old. */
|
|
104
|
+
export function shouldNotify(r, merged, now) {
|
|
105
|
+
if (!merged)
|
|
106
|
+
return true;
|
|
107
|
+
return now - (r.notifiedAt ?? 0) >= RENOTIFY_MS;
|
|
108
|
+
}
|
|
109
|
+
export function reportsInbox(list, viewer) {
|
|
110
|
+
const rows = [...list]
|
|
111
|
+
.sort((a, b) => (a.status === b.status ? b.lastAt - a.lastAt : a.status === "open" ? -1 : 1))
|
|
112
|
+
.map((r) => ({ ...r, unread: r.status === "open" && !(r.seenBy ?? []).includes(viewer) }));
|
|
113
|
+
return { reports: rows, badge: rows.filter((r) => r.unread).length };
|
|
114
|
+
}
|
|
115
|
+
export function markReportsSeen(list, viewer) {
|
|
116
|
+
return list.map((r) => ((r.seenBy ?? []).includes(viewer) ? r : { ...r, seenBy: [...(r.seenBy ?? []), viewer] }));
|
|
117
|
+
}
|
|
118
|
+
/** Resolve (or reopen) one report. */
|
|
119
|
+
export function resolveReport(list, id, by, now, reopen = false) {
|
|
120
|
+
const r = list.find((x) => x.id === id);
|
|
121
|
+
if (!r)
|
|
122
|
+
return { ok: false, error: "no such report" };
|
|
123
|
+
const next = reopen
|
|
124
|
+
? (({ resolvedBy: _b, resolvedAt: _a, ...rest }) => ({ ...rest, status: "open" }))(r)
|
|
125
|
+
: { ...r, status: "resolved", resolvedBy: by, resolvedAt: now };
|
|
126
|
+
return { ok: true, list: list.map((x) => (x.id === id ? next : x)), report: next };
|
|
127
|
+
}
|
|
128
|
+
const KIND_LABEL = { access: "🔑 access", bug: "🐞 bug", blocked: "⛔ blocked" };
|
|
129
|
+
export const kindLabel = (k) => KIND_LABEL[k];
|
|
130
|
+
/** The Telegram DM. Short: the panel holds the detail. */
|
|
131
|
+
export function notificationText(r, cockpitTitle) {
|
|
132
|
+
const where = cockpitTitle ? ` · ${cockpitTitle}` : "";
|
|
133
|
+
const again = r.count > 1 ? ` (×${r.count})` : "";
|
|
134
|
+
const lines = [`🆘 ${r.agent} needs an admin${where}`, `${KIND_LABEL[r.kind]}: ${r.title}${again}`];
|
|
135
|
+
if (r.secret)
|
|
136
|
+
lines.push(`secret: ${r.secret}`);
|
|
137
|
+
if (r.body)
|
|
138
|
+
lines.push("", r.body.length > 600 ? r.body.slice(0, 600) + "…" : r.body);
|
|
139
|
+
lines.push("", "Open the cockpit → Reports to resolve it.");
|
|
140
|
+
return lines.join("\n");
|
|
141
|
+
}
|
|
142
|
+
// ── Storage ──────────────────────────────────────────────────────────────────
|
|
143
|
+
export function reportsFile(cwd = process.cwd()) {
|
|
144
|
+
return path.join(os.homedir(), ".shadok-ai", "reports", instanceKey(cwd) + ".json");
|
|
145
|
+
}
|
|
146
|
+
export function loadReports(file = reportsFile()) {
|
|
147
|
+
try {
|
|
148
|
+
const v = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
149
|
+
return Array.isArray(v) ? v.filter((r) => r && typeof r.id === "string" && typeof r.title === "string") : [];
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export function saveReports(list, file = reportsFile()) {
|
|
156
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
157
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
158
|
+
fs.writeFileSync(tmp, JSON.stringify(list, null, 2), { mode: 0o600 });
|
|
159
|
+
fs.renameSync(tmp, file);
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=agent-reports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-reports.js","sourceRoot":"","sources":["../src/agent-reports.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;uEACuE;AACvE,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,KAAK,EAAE,SAAS,CAAU,CAAC;AA+BlE,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,CAAC;AAC7B,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,CAAC;AAC7B,iFAAiF;AACjF,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AACnC,kDAAkD;AAClD,MAAM,CAAC,MAAM,WAAW,GAAG,QAAQ,CAAC;AACpC,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC;AAC/B,qEAAqE;AACrE,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,GAAG,EAAE,GAAG,QAAQ,CAAC;AAElD,gFAAgF;AAChF,MAAM,QAAQ,GAAG,gCAAgC,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AM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,EAAE,KAAK,QAAQ,IAAI,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC/G,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAmB,EAAE,IAAI,GAAW,WAAW,EAAE;IAC3E,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IACnE,MAAM,GAAG,GAAG,GAAG,IAAI,IAAI,OAAO,CAAC,GAAG,MAAM,CAAC;IACzC,EAAE,CAAC,aAAa,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IACtE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;AAC3B,CAAC"}
|
package/dist/rooms.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { TgBinding } from "./channels.js";
|
|
2
|
+
import { type Group, type Viewer } from "./groups.js";
|
|
3
|
+
/**
|
|
4
|
+
* Rooms: channels with no agent, where PEOPLE talk.
|
|
5
|
+
*
|
|
6
|
+
* A room is deliberately NOT a row of the channel registry (`channels.ts`).
|
|
7
|
+
* That registry answers "which agents exist", and every consumer of it reads it
|
|
8
|
+
* that way: the client resumes each row, the Telegram reconciler opens a bridge
|
|
9
|
+
* (= spawns an agent) behind each mirrored row, `firstAgentPlan` counts rows,
|
|
10
|
+
* crons, kinship and the ledger list them. A row with no process behind it
|
|
11
|
+
* would make each of those either spawn a `claude` or misbehave, and the only
|
|
12
|
+
* thing a room must never do is run one. A separate registry makes that
|
|
13
|
+
* mistake impossible rather than merely checked. Design:
|
|
14
|
+
* docs/superpowers/specs/2026-11-02-human-rooms-design.md.
|
|
15
|
+
*
|
|
16
|
+
* Storage, per launch dir (keyed like channels, crons and the ledger):
|
|
17
|
+
* ~/.shadok-ai/rooms/<key>.json the room list (rewritten atomically)
|
|
18
|
+
* ~/.shadok-ai/rooms/<key>.read.json per account, the last message seen
|
|
19
|
+
* ~/.shadok-ai/rooms/<key>/<roomId>.jsonl the messages, APPEND-ONLY
|
|
20
|
+
* Messages are appended and never rewritten, the reason the secret audit log is
|
|
21
|
+
* JSONL too: two processes never rewrite each other's lines, and a crash
|
|
22
|
+
* mid-write costs one truncated last line, which `parseMessages` skips.
|
|
23
|
+
*/
|
|
24
|
+
export interface Room {
|
|
25
|
+
/** `r_` + 16 hex chars: never UUID-shaped, so it can never pass for a session id. */
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
/** Same group ids as channels: one column, one set of groups. */
|
|
29
|
+
group?: string | null;
|
|
30
|
+
createdBy: string;
|
|
31
|
+
createdAt: number;
|
|
32
|
+
/** Present iff the room is bound to a Telegram topic — what IS. Server-owned. */
|
|
33
|
+
telegram?: TgBinding | null;
|
|
34
|
+
/** What the user WANTS (same meaning as `Channel.mirror`). */
|
|
35
|
+
mirror?: boolean;
|
|
36
|
+
/** Closed rooms are archived, never deleted: a conversation between people is
|
|
37
|
+
* a record someone wants back the week after. */
|
|
38
|
+
archivedAt?: number;
|
|
39
|
+
}
|
|
40
|
+
export interface RoomMessage {
|
|
41
|
+
id: string;
|
|
42
|
+
ts: number;
|
|
43
|
+
/** Display name: the account on the web, the sender on Telegram. */
|
|
44
|
+
author: string;
|
|
45
|
+
/** Set only when the server KNOWS the account (a web session). */
|
|
46
|
+
account?: string;
|
|
47
|
+
origin: "web" | "telegram";
|
|
48
|
+
text: string;
|
|
49
|
+
/** The Telegram message id, for the mirror (dedup). */
|
|
50
|
+
tgMessageId?: number;
|
|
51
|
+
}
|
|
52
|
+
export declare const ROOM_ID_RE: RegExp;
|
|
53
|
+
export declare const ROOM_NAME_MAX = 80;
|
|
54
|
+
/** A room message is chat, not a document: past this it is refused, out loud. */
|
|
55
|
+
export declare const ROOM_TEXT_MAX = 16000;
|
|
56
|
+
export declare const ROOM_PAGE = 200;
|
|
57
|
+
export declare function newRoomId(): string;
|
|
58
|
+
export declare function isRoomId(s: unknown): s is string;
|
|
59
|
+
/** The trimmed name, or an explicit refusal. */
|
|
60
|
+
export declare function roomName(raw: unknown): {
|
|
61
|
+
ok: true;
|
|
62
|
+
name: string;
|
|
63
|
+
} | {
|
|
64
|
+
ok: false;
|
|
65
|
+
error: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* The group a NEW room is filed into: the one the caller asked for, but only if
|
|
69
|
+
* it exists and the caller may see it; otherwise the commons (null). A caller
|
|
70
|
+
* can place a room in a group they belong to, never plant one in a group they
|
|
71
|
+
* do not — `spawnGroupForRequest`'s rule, minus the parent clause (a room has
|
|
72
|
+
* no launcher).
|
|
73
|
+
*/
|
|
74
|
+
export declare function roomGroupFor(requested: unknown, groups: readonly Group[], viewer: Viewer | null): string | null;
|
|
75
|
+
/**
|
|
76
|
+
* The rooms a viewer may see: not archived, and the group rule of
|
|
77
|
+
* `visibleChannels` — no group → the commons, visible to all; a dangling group
|
|
78
|
+
* id → HIDDEN (fail closed, a lost group row must never reveal its rooms).
|
|
79
|
+
*/
|
|
80
|
+
export declare function visibleRooms(rooms: readonly Room[], groups: readonly Group[], viewer: Viewer | null): Room[];
|
|
81
|
+
export declare function canSeeRoom(room: Room | undefined, groups: readonly Group[], viewer: Viewer | null): boolean;
|
|
82
|
+
/** Why a post is refused, or null. A `code` a client can switch on, and words. */
|
|
83
|
+
export declare function postRefusal(room: Room | undefined, text: unknown): {
|
|
84
|
+
code: string;
|
|
85
|
+
error: string;
|
|
86
|
+
} | null;
|
|
87
|
+
/** Pure: the list with one room's mutable fields changed. Server-owned fields
|
|
88
|
+
* (`id`, `createdBy`, `createdAt`, `telegram`, `archivedAt`) never move here. */
|
|
89
|
+
export declare function patchRoomIn(list: readonly Room[], id: string, patch: {
|
|
90
|
+
name?: string;
|
|
91
|
+
group?: string | null;
|
|
92
|
+
mirror?: boolean;
|
|
93
|
+
}): Room[];
|
|
94
|
+
/** Pure: parse a JSONL message file, skipping any line that is not a message
|
|
95
|
+
* (a truncated last line after a crash, a stray edit). */
|
|
96
|
+
export declare function parseMessages(raw: string): RoomMessage[];
|
|
97
|
+
/** Pure: the page of messages strictly before `before` (all when absent), the
|
|
98
|
+
* newest `limit` of them, oldest first. */
|
|
99
|
+
export declare function pageMessages(msgs: readonly RoomMessage[], before: number | undefined, limit?: number): RoomMessage[];
|
|
100
|
+
/** Pure: how many messages after `lastRead` were written by someone else. */
|
|
101
|
+
export declare function unreadCount(msgs: readonly RoomMessage[], lastRead: number, account: string | null): number;
|
|
102
|
+
/** Pure: the (live) room mirrored into this Telegram topic. */
|
|
103
|
+
export declare function roomForTopic(rooms: readonly Room[], chatId: number, threadId: number | undefined): Room | undefined;
|
|
104
|
+
/** The text a room message becomes in a Telegram topic. Pure. */
|
|
105
|
+
export declare function telegramLine(m: Pick<RoomMessage, "author" | "text">): string;
|
|
106
|
+
export declare function roomsRoot(home?: string): string;
|
|
107
|
+
/**
|
|
108
|
+
* The on-disk store. A class only so tests can point it at a scratch dir; the
|
|
109
|
+
* server keeps one. The message cache is keyed on the file's size: an append
|
|
110
|
+
* always grows it, so a stale entry is impossible without a rewrite, and the
|
|
111
|
+
* 4 s list poll does not re-read every room each time.
|
|
112
|
+
*/
|
|
113
|
+
export declare class RoomStore {
|
|
114
|
+
private readonly root;
|
|
115
|
+
private readonly key;
|
|
116
|
+
private cache;
|
|
117
|
+
constructor(root?: string, key?: string);
|
|
118
|
+
private listFile;
|
|
119
|
+
private readFile;
|
|
120
|
+
private msgFile;
|
|
121
|
+
private writeAtomic;
|
|
122
|
+
list(): Room[];
|
|
123
|
+
save(list: Room[]): void;
|
|
124
|
+
get(id: string): Room | undefined;
|
|
125
|
+
/** Read-modify-write of ONE room; returns it, or undefined when unknown. */
|
|
126
|
+
update(id: string, fn: (r: Room) => Room): Room | undefined;
|
|
127
|
+
create(room: Room): void;
|
|
128
|
+
messages(id: string): RoomMessage[];
|
|
129
|
+
append(id: string, m: RoomMessage): void;
|
|
130
|
+
/** `{ [roomId]: { [account]: ts } }` */
|
|
131
|
+
private marks;
|
|
132
|
+
lastRead(id: string, account: string): number;
|
|
133
|
+
/** Only ever moves FORWARD: an old tab catching up must not resurrect unread. */
|
|
134
|
+
markRead(id: string, account: string, ts: number): void;
|
|
135
|
+
}
|