@astrofoundry/astrogate 0.0.0-stage → 0.1.1
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 +2 -2
- package/agents/foreman.md +27 -0
- package/cli/astro-install.sh +242 -0
- package/cli/astro.sh +449 -0
- package/cli/astro.test.ts +65 -0
- package/dist/bridge.js +9950 -0
- package/dist/claude.js +7899 -0
- package/dist/extensions/bridge/index.js +9937 -0
- package/dist/extensions/gate/index.js +286 -0
- package/dist/extensions/guard/index.js +364 -0
- package/dist/extensions/subagents/index.js +8458 -0
- package/extensions/bridge/admin.ts +86 -0
- package/extensions/bridge/approvals.ts +147 -0
- package/extensions/bridge/attachments.ts +65 -0
- package/extensions/bridge/chunk.ts +50 -0
- package/extensions/bridge/claude-run.test.ts +62 -0
- package/extensions/bridge/claude-run.ts +301 -0
- package/extensions/bridge/commands.ts +89 -0
- package/extensions/bridge/config.ts +263 -0
- package/extensions/bridge/conversations.ts +144 -0
- package/extensions/bridge/daemon.ts +60 -0
- package/extensions/bridge/discord.test.ts +547 -0
- package/extensions/bridge/gateway.ts +222 -0
- package/extensions/bridge/index.ts +951 -0
- package/extensions/bridge/launcher.ts +36 -0
- package/extensions/bridge/lifecycle.test.ts +78 -0
- package/extensions/bridge/open-work.ts +74 -0
- package/extensions/bridge/plugin.ts +172 -0
- package/extensions/bridge/quote.ts +39 -0
- package/extensions/bridge/reports.ts +65 -0
- package/extensions/bridge/rest.ts +217 -0
- package/extensions/bridge/setup.ts +218 -0
- package/extensions/gate/approvals.test.ts +40 -0
- package/extensions/gate/config.ts +110 -0
- package/extensions/gate/core.ts +114 -0
- package/extensions/gate/index.test.ts +202 -0
- package/extensions/gate/index.ts +114 -0
- package/extensions/gate/risky.ts +14 -0
- package/extensions/gate/types.ts +19 -0
- package/extensions/guard/config.test.ts +107 -0
- package/extensions/guard/config.ts +120 -0
- package/extensions/guard/defaults.ts +40 -0
- package/extensions/guard/index.test.ts +313 -0
- package/extensions/guard/index.ts +183 -0
- package/extensions/guard/rules.test.ts +118 -0
- package/extensions/guard/rules.ts +101 -0
- package/extensions/guard/types.ts +12 -0
- package/extensions/subagents/agents.test.ts +137 -0
- package/extensions/subagents/agents.ts +207 -0
- package/extensions/subagents/bundled.test.ts +29 -0
- package/extensions/subagents/child.test.ts +202 -0
- package/extensions/subagents/child.ts +367 -0
- package/extensions/subagents/index.test.ts +123 -0
- package/extensions/subagents/index.ts +352 -0
- package/extensions/subagents/pi-paths.test.ts +39 -0
- package/extensions/subagents/pi-paths.ts +58 -0
- package/extensions/subagents/render.ts +221 -0
- package/extensions/subagents/sessions.test.ts +32 -0
- package/extensions/subagents/sessions.ts +34 -0
- package/extensions/subagents/ticker.test.ts +40 -0
- package/extensions/subagents/ticker.ts +36 -0
- package/harness/claude/agents.ts +93 -0
- package/harness/claude/claude.test.ts +171 -0
- package/harness/claude/hook.ts +77 -0
- package/harness/claude/main.ts +122 -0
- package/harness/claude/mcp.ts +174 -0
- package/harness/claude/settings.ts +63 -0
- package/package.json +71 -4
- package/release.sha256 +95 -0
- package/release.sha256.sig +6 -0
- package/specialists/AGENTS.md +67 -0
- package/specialists/README.md +81 -0
- package/specialists/entry/specialist-cli.sh +22 -0
- package/specialists/entry/specialist-install.sh +861 -0
- package/specialists/entry/specialist-install.test.ts +305 -0
- package/specialists/lib/argv.ts +19 -0
- package/specialists/lib/audit.ts +20 -0
- package/specialists/lib/config.ts +27 -0
- package/specialists/lib/errors.ts +8 -0
- package/specialists/lib/flush.test.ts +21 -0
- package/specialists/lib/lib.test.ts +87 -0
- package/specialists/lib/lock.test.ts +42 -0
- package/specialists/lib/lock.ts +67 -0
- package/specialists/lib/main.ts +40 -0
- package/specialists/lib/output.ts +11 -0
- package/specialists/lib/paths.ts +22 -0
- package/specialists/lib/pinned.test.ts +55 -0
- package/specialists/lib/pinned.ts +67 -0
- package/specialists/lib/proc.ts +49 -0
- package/specialists/lib/repo.ts +198 -0
- package/specialists/lib/runtime.test.ts +81 -0
- package/specialists/lib/sanitize.ts +43 -0
- package/specialists/lib/secrets.ts +28 -0
- package/specialists/lib/ssh.ts +38 -0
- package/specialists/lib/tunnel.test.ts +44 -0
- package/specialists/lib/tunnel.ts +76 -0
- package/specialists/provision/provision.sh +96 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { configDir } from "./config.ts";
|
|
5
|
+
|
|
6
|
+
/** A later task for the same specialist in the same channel continues the conversation within this window. */
|
|
7
|
+
export const CONVERSATION_IDLE_MS = 30 * 60 * 1000;
|
|
8
|
+
/** Conversations quiet for longer are forgotten; their session files are pruned on the same schedule. */
|
|
9
|
+
export const CONVERSATION_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
10
|
+
const REMEMBERED_ANSWERS = 50;
|
|
11
|
+
|
|
12
|
+
export interface Conversation {
|
|
13
|
+
/** Pi session id of the child that holds this conversation. */
|
|
14
|
+
id: string;
|
|
15
|
+
/** When the conversation started or the specialist last answered. */
|
|
16
|
+
lastAt: number;
|
|
17
|
+
/** The bot's answer messages; a reply to any of them continues here. */
|
|
18
|
+
messageIds: string[];
|
|
19
|
+
/** The user the last answer asked for something (a `Needs you:` line), and when. */
|
|
20
|
+
asked?: { userId: string; at: number };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ConversationState {
|
|
24
|
+
/** One live conversation per `<channelId>:<specialist>`. */
|
|
25
|
+
conversations: Record<string, Conversation>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function conversationKey(channelId: string, specialist: string): string {
|
|
29
|
+
return `${channelId}:${specialist}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function statePath(): string {
|
|
33
|
+
return join(configDir(), "conversations.json");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function sessionsRoot(): string {
|
|
37
|
+
return join(configDir(), "sessions");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isConversation(value: unknown): value is Conversation {
|
|
41
|
+
if (typeof value !== "object" || value === null) return false;
|
|
42
|
+
const c = value as Record<string, unknown>;
|
|
43
|
+
const asked = c.asked as Record<string, unknown> | undefined;
|
|
44
|
+
const askedOk = asked === undefined || (typeof asked === "object" && asked !== null && typeof asked.userId === "string" && typeof asked.at === "number");
|
|
45
|
+
return typeof c.id === "string" && typeof c.lastAt === "number" && Array.isArray(c.messageIds) && c.messageIds.every((m) => typeof m === "string") && askedOk;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Reads the saved state; an unreadable or malformed file starts empty. */
|
|
49
|
+
export function loadState(): ConversationState {
|
|
50
|
+
let raw: unknown;
|
|
51
|
+
try {
|
|
52
|
+
raw = JSON.parse(readFileSync(statePath(), "utf-8"));
|
|
53
|
+
} catch {
|
|
54
|
+
return { conversations: {} };
|
|
55
|
+
}
|
|
56
|
+
const conversations: Record<string, Conversation> = {};
|
|
57
|
+
const source = typeof raw === "object" && raw !== null ? (raw as Record<string, unknown>).conversations : undefined;
|
|
58
|
+
if (typeof source === "object" && source !== null) {
|
|
59
|
+
for (const [key, value] of Object.entries(source as Record<string, unknown>)) if (isConversation(value)) conversations[key] = value;
|
|
60
|
+
}
|
|
61
|
+
return { conversations };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function saveState(state: ConversationState): void {
|
|
65
|
+
const path = statePath();
|
|
66
|
+
const tmp = `${path}.tmp`;
|
|
67
|
+
writeFileSync(tmp, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
|
|
68
|
+
renameSync(tmp, path);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A session id Pi accepts: specialist, UTC timestamp, random suffix. */
|
|
72
|
+
export function newConversationId(specialist: string, now: number, random: () => string = () => randomBytes(3).toString("hex")): string {
|
|
73
|
+
const stamp = new Date(now).toISOString().replace(/\D/g, "").slice(0, 14);
|
|
74
|
+
return `${specialist}-${stamp}-${random()}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface ResolveInput {
|
|
78
|
+
channelId: string;
|
|
79
|
+
specialist: string;
|
|
80
|
+
/** Id of the message this one replies to, if any. */
|
|
81
|
+
repliedTo?: string;
|
|
82
|
+
/** The user asked for a fresh conversation. */
|
|
83
|
+
fresh: boolean;
|
|
84
|
+
now: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface ResolvedConversation {
|
|
88
|
+
key: string;
|
|
89
|
+
id: string;
|
|
90
|
+
resumed: boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The conversation a task belongs to: the one it replies to, else the recent one, else a new one. */
|
|
94
|
+
export function resolveConversation(state: ConversationState, input: ResolveInput, newId: () => string): ResolvedConversation {
|
|
95
|
+
const key = conversationKey(input.channelId, input.specialist);
|
|
96
|
+
const current = state.conversations[key];
|
|
97
|
+
if (!input.fresh && current) {
|
|
98
|
+
const repliesHere = input.repliedTo !== undefined && current.messageIds.includes(input.repliedTo);
|
|
99
|
+
if (repliesHere || input.now - current.lastAt < CONVERSATION_IDLE_MS) return { key, id: current.id, resumed: true };
|
|
100
|
+
}
|
|
101
|
+
return { key, id: newId(), resumed: false };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function startConversation(state: ConversationState, key: string, id: string, now: number): ConversationState {
|
|
105
|
+
return { conversations: { ...state.conversations, [key]: { id, lastAt: now, messageIds: [] } } };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Remembers the bot's answer messages and refreshes the idle window; `askedUser` is the user an answer starting with `Needs you:` asks. */
|
|
109
|
+
export function recordAnswer(state: ConversationState, key: string, messageIds: string[], now: number, askedUser?: string): ConversationState {
|
|
110
|
+
const current = state.conversations[key];
|
|
111
|
+
if (!current) return state;
|
|
112
|
+
const merged = [...current.messageIds, ...messageIds].slice(-REMEMBERED_ANSWERS);
|
|
113
|
+
const next: Conversation = { id: current.id, lastAt: now, messageIds: merged, ...(askedUser ? { asked: { userId: askedUser, at: now } } : {}) };
|
|
114
|
+
return { conversations: { ...state.conversations, [key]: next } };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function specialistOf(key: string, channelId: string): string | null {
|
|
118
|
+
return key.startsWith(`${channelId}:`) ? key.slice(channelId.length + 1) : null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The specialist whose answer in this channel is the replied-to message. */
|
|
122
|
+
export function replySpecialist(state: ConversationState, channelId: string, messageId: string): string | null {
|
|
123
|
+
for (const [key, c] of Object.entries(state.conversations)) {
|
|
124
|
+
const specialist = specialistOf(key, channelId);
|
|
125
|
+
if (specialist && c.messageIds.includes(messageId)) return specialist;
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The specialist whose newest answer in this channel asked this user for something within the idle window. */
|
|
131
|
+
export function askingSpecialist(state: ConversationState, channelId: string, userId: string, now: number): string | null {
|
|
132
|
+
let found: { specialist: string; at: number } | null = null;
|
|
133
|
+
for (const [key, c] of Object.entries(state.conversations)) {
|
|
134
|
+
const specialist = specialistOf(key, channelId);
|
|
135
|
+
if (!specialist || c.asked?.userId !== userId || now - c.asked.at >= CONVERSATION_IDLE_MS) continue;
|
|
136
|
+
if (!found || c.asked.at > found.at) found = { specialist, at: c.asked.at };
|
|
137
|
+
}
|
|
138
|
+
return found?.specialist ?? null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function forgetStale(state: ConversationState, now: number): ConversationState {
|
|
142
|
+
const conversations = Object.fromEntries(Object.entries(state.conversations).filter(([, c]) => now - c.lastAt < CONVERSATION_MAX_AGE_MS));
|
|
143
|
+
return { conversations };
|
|
144
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Discord bridge as its own Node process, hosted by no harness: the
|
|
3
|
+
* LaunchAgent's run.sh executes the bundle of this file (`dist/bridge.js`), and
|
|
4
|
+
* the bridge starts Pi or Claude Code for each task as `runner` says. launchd's
|
|
5
|
+
* stop signal lets it cancel open approvals before it exits; a failed start
|
|
6
|
+
* exits, so launchd's KeepAlive tries again.
|
|
7
|
+
*
|
|
8
|
+
* bridge.js runs the bridge
|
|
9
|
+
* bridge.js check prints what the bridge would start with, as JSON, and connects to nothing
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { defaultDirs, discoverAgents } from "../subagents/agents.ts";
|
|
14
|
+
import { packageRoot } from "../subagents/pi-paths.ts";
|
|
15
|
+
import { loadConfig, loadToken } from "./config.ts";
|
|
16
|
+
import { Bridge, pluginPath } from "./index.ts";
|
|
17
|
+
|
|
18
|
+
function log(line: string): void {
|
|
19
|
+
process.stderr.write(`[astro-discord ${new Date().toISOString()}] ${line}\n`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The facts a start depends on: the package this bundle belongs to, the agents it finds, and the configuration. */
|
|
23
|
+
function check(): void {
|
|
24
|
+
const loaded = loadConfig();
|
|
25
|
+
if (!loaded.config) {
|
|
26
|
+
for (const error of loaded.errors) log(`config error: ${error}`);
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
const version = (JSON.parse(readFileSync(join(packageRoot(), "package.json"), "utf-8")) as { version: string }).version;
|
|
30
|
+
const agents = discoverAgents(process.cwd(), "user", defaultDirs()).agents.map((a) => a.name);
|
|
31
|
+
process.stdout.write(`${JSON.stringify({ version, runner: loaded.config.runner, reports: loaded.config.reports.map((r) => r.report), agents, plugin: existsSync(pluginPath()) })}\n`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function main(): Promise<void> {
|
|
35
|
+
if (process.argv[2] === "check") {
|
|
36
|
+
check();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const loaded = loadConfig();
|
|
40
|
+
if (!loaded.config) {
|
|
41
|
+
for (const error of loaded.errors) log(`config error: ${error}`);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
// `pi` from PATH: this process's own script is the bridge, which the default would start again as if it were Pi.
|
|
45
|
+
const bridge = new Bridge({ defaults: {}, invocation: (args) => ({ command: "pi", args }) }, loaded.config, loadToken());
|
|
46
|
+
let stopping = false;
|
|
47
|
+
const stop = () => {
|
|
48
|
+
if (stopping) return;
|
|
49
|
+
stopping = true;
|
|
50
|
+
void bridge.shutdown().finally(() => process.exit(0));
|
|
51
|
+
};
|
|
52
|
+
process.on("SIGTERM", stop);
|
|
53
|
+
process.on("SIGINT", stop);
|
|
54
|
+
await bridge.start();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
main().catch((err: unknown) => {
|
|
58
|
+
log(`start failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
59
|
+
process.exit(1);
|
|
60
|
+
});
|