omp-superagent 0.0.0-stage → 0.1.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/DESIGN.md +1502 -0
- package/LICENSE +21 -0
- package/README.ko.md +198 -0
- package/README.md +224 -2
- package/THIRD_PARTY_NOTICES.md +388 -0
- package/assets/architecture.mmd +24 -0
- package/assets/architecture.svg +126 -0
- package/assets/superagent-avatar.jpg +0 -0
- package/assets/superagent-mascot.webp +0 -0
- package/config.example.json +26 -0
- package/docs/OPERATIONS.md +1178 -0
- package/docs/discord-voice.md +96 -0
- package/docs/releases/0.1.0.md +222 -0
- package/docs/releases/0.1.1.md +60 -0
- package/docs/releases/0.1.2.md +53 -0
- package/package.json +65 -4
- package/scripts/ci.ts +33 -0
- package/scripts/prepare-ulw.ts +18 -0
- package/scripts/release.ts +58 -0
- package/scripts/setup.sh +25 -0
- package/scripts/test-omp.sh +36 -0
- package/skills/head/SKILL.md +100 -0
- package/skills/head/references/model-selection.md +83 -0
- package/skills/head/references/routing.md +181 -0
- package/skills/head/references/supervision.md +122 -0
- package/skills/lane/SKILL.md +66 -0
- package/skills/lane/references/workflow.md +95 -0
- package/src/autostart.ts +295 -0
- package/src/board-extension.ts +217 -0
- package/src/board.ts +396 -0
- package/src/cli.ts +253 -0
- package/src/codex-live-bridge.py +73 -0
- package/src/codex-live.ts +244 -0
- package/src/codex-voice-requirements.txt +5 -0
- package/src/codex_live_auth.py +192 -0
- package/src/codex_live_media.py +219 -0
- package/src/codex_live_provider.py +192 -0
- package/src/config.ts +266 -0
- package/src/contracts.ts +381 -0
- package/src/credentials.ts +172 -0
- package/src/decision-tool.ts +59 -0
- package/src/decisions.ts +143 -0
- package/src/delivery.ts +17 -0
- package/src/dialog.ts +73 -0
- package/src/discord-command-registration.ts +201 -0
- package/src/discord-commands.ts +29 -0
- package/src/discord-context.ts +118 -0
- package/src/discord-markdown.ts +172 -0
- package/src/discord-voice-draft.ts +124 -0
- package/src/discord-voice-link.ts +270 -0
- package/src/discord-voice.ts +150 -0
- package/src/discord.ts +1012 -0
- package/src/extension.ts +446 -0
- package/src/gateway-client.ts +229 -0
- package/src/gateway-protocol.ts +85 -0
- package/src/gateway-restart.ts +151 -0
- package/src/gateway-server.ts +440 -0
- package/src/gateway-store.ts +251 -0
- package/src/gateway.ts +654 -0
- package/src/head-recovery.ts +61 -0
- package/src/host-broker-services.ts +108 -0
- package/src/host-extension.ts +561 -0
- package/src/host-owner.ts +366 -0
- package/src/host-protocol.ts +144 -0
- package/src/host-services.ts +54 -0
- package/src/host.ts +415 -0
- package/src/installed-omp.ts +71 -0
- package/src/lane-extension.ts +184 -0
- package/src/lane-models.ts +75 -0
- package/src/lane-report.ts +70 -0
- package/src/lane-rows.ts +12 -0
- package/src/managed-ulw-probe.ts +172 -0
- package/src/managed-ulw.ts +471 -0
- package/src/message-order.ts +21 -0
- package/src/messenger-commands.ts +16 -0
- package/src/messengers.ts +143 -0
- package/src/native-presets.ts +370 -0
- package/src/native-sessions.ts +32 -0
- package/src/native-settings.ts +131 -0
- package/src/openai-live.ts +377 -0
- package/src/pr-github-schema.ts +110 -0
- package/src/pr-github.ts +170 -0
- package/src/pr-watch.ts +200 -0
- package/src/repository.ts +12 -0
- package/src/runtime.ts +4229 -0
- package/src/setup-judge.ts +34 -0
- package/src/setup-memory.ts +39 -0
- package/src/setup-profiles.ts +84 -0
- package/src/setup.ts +300 -0
- package/src/slack-command-registration.ts +217 -0
- package/src/slack-commands.ts +18 -0
- package/src/slack.ts +779 -0
- package/src/store.ts +862 -0
- package/src/telegram-command-registration.ts +155 -0
- package/src/telegram.ts +677 -0
- package/src/workflow.ts +94 -0
- package/upstream/ulw.json +9 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { chmodSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { isDeepStrictEqual } from "node:util";
|
|
6
|
+
import type { DialogResponse, DiscordPurposeRequest, GatewayRestartNotice, GatewayRestartRecord, GatewayRestartRequest, Inbound, Route } from "./contracts";
|
|
7
|
+
import { routeKey } from "./contracts";
|
|
8
|
+
import { authorizeDiscordPurpose } from "./discord-context";
|
|
9
|
+
import { DeliveryError } from "./delivery";
|
|
10
|
+
|
|
11
|
+
export type GatewayEvent =
|
|
12
|
+
| { kind: "message"; input: Inbound }
|
|
13
|
+
| { kind: "answer"; approvalId: string; response: DialogResponse; route: Route }
|
|
14
|
+
| ({ kind: "restart" } & GatewayRestartNotice)
|
|
15
|
+
| { kind: "reconnect"; barrier?: string };
|
|
16
|
+
export type GatewayReceipt = "accepted" | "rejected" | "unresolved";
|
|
17
|
+
export interface QueuedEvent { sequence: number; event: GatewayEvent; reconciliation?: true }
|
|
18
|
+
|
|
19
|
+
function identity(event: GatewayEvent): unknown {
|
|
20
|
+
if (event.kind !== "message") return event;
|
|
21
|
+
const { receivedAt: _, reply: _reply, discordContext: _context, ...input } = event.input;
|
|
22
|
+
return { ...event, input: { ...input, attachments: input.attachments.map(file => {
|
|
23
|
+
let location = file.url;
|
|
24
|
+
try {
|
|
25
|
+
const url = new URL(location);
|
|
26
|
+
if (input.route.platform === "discord" && url.protocol === "https:" &&
|
|
27
|
+
["cdn.discordapp.com", "media.discordapp.net"].includes(url.hostname)) {
|
|
28
|
+
for (const key of ["ex", "is", "hm"]) url.searchParams.delete(key);
|
|
29
|
+
url.searchParams.sort();
|
|
30
|
+
location = url.href;
|
|
31
|
+
}
|
|
32
|
+
} catch { /* Adapter-owned non-URL references remain exact. */ }
|
|
33
|
+
const { voice: _voice, durationSeconds: _duration, ...download } = file;
|
|
34
|
+
return { ...download, url: location };
|
|
35
|
+
}).sort((a, b) => a.id.localeCompare(b.id)) } };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A separate lifetime lock; never impersonates the head Store writer. */
|
|
39
|
+
export class GatewayStore {
|
|
40
|
+
private readonly lock: Database;
|
|
41
|
+
private readonly db: Database;
|
|
42
|
+
private closed = false;
|
|
43
|
+
|
|
44
|
+
constructor(stateDir: string) {
|
|
45
|
+
const path = join(stateDir, "coordination.sqlite");
|
|
46
|
+
this.lock = new Database(`${path}.gateway-lock`, { create: true, strict: true });
|
|
47
|
+
let database: Database | undefined;
|
|
48
|
+
try {
|
|
49
|
+
this.lock.run("PRAGMA busy_timeout = 100");
|
|
50
|
+
this.lock.run("BEGIN EXCLUSIVE");
|
|
51
|
+
this.db = database = new Database(path, { create: true, strict: true });
|
|
52
|
+
this.db.run("PRAGMA busy_timeout = 5000");
|
|
53
|
+
this.db.run("PRAGMA journal_mode = WAL");
|
|
54
|
+
this.db.run("PRAGMA synchronous = FULL");
|
|
55
|
+
this.db.run(`
|
|
56
|
+
CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
57
|
+
CREATE TABLE IF NOT EXISTS gateway_events (
|
|
58
|
+
sequence INTEGER PRIMARY KEY AUTOINCREMENT, identity TEXT NOT NULL UNIQUE,
|
|
59
|
+
data TEXT NOT NULL, received_at INTEGER NOT NULL,
|
|
60
|
+
delivered_at INTEGER, delivery_error TEXT, attempted_at INTEGER
|
|
61
|
+
);
|
|
62
|
+
CREATE INDEX IF NOT EXISTS gateway_events_pending ON gateway_events(delivered_at, sequence);
|
|
63
|
+
CREATE TABLE IF NOT EXISTS gateway_effects (
|
|
64
|
+
id TEXT PRIMARY KEY, operation TEXT NOT NULL, arguments TEXT NOT NULL,
|
|
65
|
+
state TEXT NOT NULL CHECK(state IN ('attempted','complete')),
|
|
66
|
+
result TEXT
|
|
67
|
+
);
|
|
68
|
+
`);
|
|
69
|
+
const columns = this.db.query("PRAGMA table_info(gateway_events)").all() as Array<{ name: string }>;
|
|
70
|
+
this.db.transaction(() => {
|
|
71
|
+
if (!columns.some(column => column.name === "attempted_at")) {
|
|
72
|
+
this.db.run("ALTER TABLE gateway_events ADD COLUMN attempted_at INTEGER");
|
|
73
|
+
// A pre-migration callback may already have reached native code.
|
|
74
|
+
this.db.run("UPDATE gateway_events SET attempted_at=received_at WHERE delivered_at IS NULL AND identity LIKE 'answer:%'");
|
|
75
|
+
}
|
|
76
|
+
// Old generic handler errors were incorrectly treated as completed.
|
|
77
|
+
// Inbox dedupe is safe; old callbacks must reconcile, never blind replay.
|
|
78
|
+
this.db.run("UPDATE gateway_events SET attempted_at=COALESCE(attempted_at,delivered_at,received_at),delivered_at=NULL,delivery_error='unresolved' WHERE delivery_error='Head rejected admitted event'");
|
|
79
|
+
}).immediate();
|
|
80
|
+
chmodSync(path, 0o600);
|
|
81
|
+
chmodSync(`${path}.gateway-lock`, 0o600);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
database?.close(true);
|
|
84
|
+
this.lock.close(true);
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getMeta(key: string): string | null {
|
|
90
|
+
return (this.db.query("SELECT value FROM metadata WHERE key = ?").get(key) as { value: string } | null)?.value ?? null;
|
|
91
|
+
}
|
|
92
|
+
setMeta(key: string, value: string): void {
|
|
93
|
+
this.db.query("INSERT INTO metadata VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value=excluded.value").run(key, value);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
validateDiscordPurpose(request: DiscordPurposeRequest): void {
|
|
97
|
+
if (this.getMeta(`user-source:${request.sourceId}`) !== "true")
|
|
98
|
+
throw new DeliveryError("Discord purpose requires an original received owner source", "rejected");
|
|
99
|
+
const row = this.db.query("SELECT data,state FROM inputs WHERE id=?").get(request.sourceId) as {
|
|
100
|
+
data: string; state: string;
|
|
101
|
+
} | null;
|
|
102
|
+
if (!row || row.state === "cancelled")
|
|
103
|
+
throw new DeliveryError("Discord purpose requires an original received owner source", "rejected");
|
|
104
|
+
const source: Inbound = JSON.parse(row.data);
|
|
105
|
+
if (source.id !== request.sourceId) throw new DeliveryError("Discord purpose source identity mismatch", "rejected");
|
|
106
|
+
authorizeDiscordPurpose(source.route, request.route);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getRestart(requestId?: string): GatewayRestartRecord | null {
|
|
110
|
+
const value = this.getMeta(requestId === undefined ? "gateway:restart" : `gateway:restart:${requestId}`);
|
|
111
|
+
return value === null ? null : JSON.parse(value) as GatewayRestartRecord;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Read only the head's durable authorized source and its delivered ACK. */
|
|
115
|
+
validateRestart(request: GatewayRestartRequest): Inbound {
|
|
116
|
+
const reject = () => new DeliveryError("Gateway restart requires its original completed owner command and sent acknowledgement", "rejected");
|
|
117
|
+
if (this.getMeta(`user-source:${request.inputId}`) !== "true") throw reject();
|
|
118
|
+
const row = this.db.query("SELECT data,target,state FROM inputs WHERE id=?").get(request.inputId) as {
|
|
119
|
+
data: string; target: string | null; state: string;
|
|
120
|
+
} | null;
|
|
121
|
+
if (!row || row.target !== "main" || row.state !== "done") throw reject();
|
|
122
|
+
const input = JSON.parse(row.data) as Inbound;
|
|
123
|
+
if (input.id !== request.inputId || input.text.trim() !== "/restart") throw reject();
|
|
124
|
+
const ack = this.db.query("SELECT source_id,route,state,remote_id FROM outputs WHERE id=?").get(request.ackOutputId) as {
|
|
125
|
+
source_id: string; route: string; state: string; remote_id: string | null;
|
|
126
|
+
} | null;
|
|
127
|
+
if (!ack || ack.source_id !== input.id || ack.state !== "sent" || !ack.remote_id?.trim()
|
|
128
|
+
|| !isDeepStrictEqual(JSON.parse(ack.route), input.route)) throw reject();
|
|
129
|
+
return input;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
setRestart(record: GatewayRestartRecord): void {
|
|
133
|
+
this.db.transaction(() => {
|
|
134
|
+
const previous = this.getRestart(record.requestId);
|
|
135
|
+
const latest = this.getRestart();
|
|
136
|
+
const source = this.getMeta(`gateway:restart-source:${record.inputId}`);
|
|
137
|
+
if (source !== null && source !== record.requestId || previous && (
|
|
138
|
+
previous.inputId !== record.inputId || previous.ackOutputId !== record.ackOutputId
|
|
139
|
+
|| previous.gatewayPid !== record.gatewayPid || previous.headPid !== record.headPid
|
|
140
|
+
|| previous.sessionId !== record.sessionId || previous.sessionFile !== record.sessionFile)) {
|
|
141
|
+
throw new DeliveryError("Gateway restart identity conflict", "rejected");
|
|
142
|
+
}
|
|
143
|
+
if (!previous) this.validateRestart(record);
|
|
144
|
+
const serialized = JSON.stringify(record);
|
|
145
|
+
this.setMeta(`gateway:restart:${record.requestId}`, serialized);
|
|
146
|
+
// New receipts advance latest; late updates belong only to their own request.
|
|
147
|
+
if (!previous || !latest || latest.requestId === record.requestId) this.setMeta("gateway:restart", serialized);
|
|
148
|
+
this.setMeta(`gateway:restart-source:${record.inputId}`, record.requestId);
|
|
149
|
+
}).immediate();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
admit(event: GatewayEvent): boolean {
|
|
153
|
+
const key = event.kind === "message" ? `message:${event.input.id}`
|
|
154
|
+
: event.kind === "answer" ? `answer:${event.approvalId}`
|
|
155
|
+
: event.kind === "restart" ? `restart:${event.requestId}:${event.outcome}`
|
|
156
|
+
: `reconnect:${crypto.randomUUID()}`;
|
|
157
|
+
return this.db.transaction(() => {
|
|
158
|
+
// Repeated unresolved admissions must not grow a reconnect poison queue.
|
|
159
|
+
if (event.kind === "reconnect" && this.db.query("SELECT 1 FROM gateway_events WHERE delivered_at IS NULL AND data = ?")
|
|
160
|
+
.get(JSON.stringify(event))) return false;
|
|
161
|
+
const existing = this.db.query("SELECT data FROM gateway_events WHERE identity = ?").get(key) as { data: string } | null;
|
|
162
|
+
if (existing) {
|
|
163
|
+
if (!isDeepStrictEqual(identity(JSON.parse(existing.data)), identity(event)))
|
|
164
|
+
throw new Error("Gateway ingress identity conflict");
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
this.db.query("INSERT INTO gateway_events(identity,data,received_at) VALUES (?,?,?)")
|
|
168
|
+
.run(key, JSON.stringify(event), Date.now());
|
|
169
|
+
return true;
|
|
170
|
+
}).immediate();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
next(sequence?: number): QueuedEvent | null {
|
|
174
|
+
let row = this.db.query<{ sequence: number; data: string; attempted_at: number | null; delivery_error: string | null }, [number | null]>(
|
|
175
|
+
`SELECT sequence,data,attempted_at,delivery_error FROM gateway_events
|
|
176
|
+
WHERE delivered_at IS NULL AND (?1 IS NULL OR sequence=?1) ORDER BY sequence LIMIT 1`)
|
|
177
|
+
.get(sequence ?? null);
|
|
178
|
+
if (sequence === undefined && row?.delivery_error === "unresolved") {
|
|
179
|
+
// Quarantine never grants replay authority, but must not trap the owner
|
|
180
|
+
// outside recovery controls or the native approval-cache startup barrier.
|
|
181
|
+
// Ordinary intake keeps its original sequence behind the unresolved event.
|
|
182
|
+
const control = this.db.query<NonNullable<typeof row>, []>(`SELECT sequence,data,attempted_at,delivery_error FROM gateway_events
|
|
183
|
+
WHERE delivered_at IS NULL AND (
|
|
184
|
+
(json_extract(data,'$.kind')='reconnect' AND json_extract(data,'$.barrier') IS NOT NULL)
|
|
185
|
+
OR (json_extract(data,'$.kind')='message' AND trim(json_extract(data,'$.input.text')) IN ('/restart','/stop','/stop head')))
|
|
186
|
+
ORDER BY CASE WHEN json_extract(data,'$.kind')='reconnect' THEN 0 ELSE 1 END,sequence LIMIT 1`)
|
|
187
|
+
.get();
|
|
188
|
+
if (control) row = control;
|
|
189
|
+
}
|
|
190
|
+
if (!row) return null;
|
|
191
|
+
const event: GatewayEvent = JSON.parse(row.data);
|
|
192
|
+
return { sequence: row.sequence, event, ...(event.kind === "answer" && row.attempted_at !== null ? { reconciliation: true as const } : {}) };
|
|
193
|
+
}
|
|
194
|
+
attempt(sequence: number): void {
|
|
195
|
+
this.db.query("UPDATE gateway_events SET attempted_at=COALESCE(attempted_at,?) WHERE sequence=? AND delivered_at IS NULL")
|
|
196
|
+
.run(Date.now(), sequence);
|
|
197
|
+
}
|
|
198
|
+
receipt(sequence: number, outcome: GatewayReceipt): void {
|
|
199
|
+
this.db.query("UPDATE gateway_events SET delivered_at=?,delivery_error=? WHERE sequence=? AND delivered_at IS NULL")
|
|
200
|
+
.run(outcome === "unresolved" ? null : Date.now(), outcome === "accepted" ? null : outcome, sequence);
|
|
201
|
+
}
|
|
202
|
+
hasPendingAnswer(approvalId: string, route: Route): boolean {
|
|
203
|
+
const row = this.db.query("SELECT data FROM gateway_events WHERE identity=? AND delivered_at IS NULL")
|
|
204
|
+
.get(`answer:${approvalId}`) as { data: string } | null;
|
|
205
|
+
if (!row) return false;
|
|
206
|
+
const event: GatewayEvent = JSON.parse(row.data);
|
|
207
|
+
return event.kind === "answer" && routeKey(event.route) === routeKey(route);
|
|
208
|
+
}
|
|
209
|
+
pending(): number {
|
|
210
|
+
return (this.db.query("SELECT COUNT(*) AS count FROM gateway_events WHERE delivered_at IS NULL").get() as { count: number }).count;
|
|
211
|
+
}
|
|
212
|
+
unresolved(): number {
|
|
213
|
+
// This local COUNT query always produces one numeric aggregate row.
|
|
214
|
+
const row = this.db.query("SELECT COUNT(*) AS count FROM gateway_events WHERE delivered_at IS NULL AND delivery_error='unresolved'")
|
|
215
|
+
.get() as { count: number };
|
|
216
|
+
return row.count;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
beginEffect(id: string, operation: string, args: unknown): { fresh: boolean; result: unknown } {
|
|
220
|
+
const serialized = JSON.stringify(args);
|
|
221
|
+
return this.db.transaction(() => {
|
|
222
|
+
const row = this.db.query("SELECT operation,arguments,state,result FROM gateway_effects WHERE id=?").get(id) as {
|
|
223
|
+
operation: string; arguments: string; state: string; result: string | null;
|
|
224
|
+
} | null;
|
|
225
|
+
if (row) {
|
|
226
|
+
if (row.operation !== operation || row.arguments !== serialized) throw new Error("Gateway effect identity conflict");
|
|
227
|
+
return { fresh: false, result: row.result === null
|
|
228
|
+
? { ok: false, error: "Gateway effect outcome uncertain", certainty: "uncertain" }
|
|
229
|
+
: JSON.parse(row.result) };
|
|
230
|
+
}
|
|
231
|
+
this.db.query("INSERT INTO gateway_effects(id,operation,arguments,state) VALUES (?,?,?,'attempted')").run(id, operation, serialized);
|
|
232
|
+
return { fresh: true, result: null };
|
|
233
|
+
}).immediate();
|
|
234
|
+
}
|
|
235
|
+
finishEffect(id: string, result: unknown): void {
|
|
236
|
+
this.db.query("UPDATE gateway_effects SET state='complete',result=? WHERE id=?").run(JSON.stringify(result), id);
|
|
237
|
+
}
|
|
238
|
+
token(): string {
|
|
239
|
+
const existing = this.getMeta("gateway:token");
|
|
240
|
+
if (existing) return existing;
|
|
241
|
+
const token = createHash("sha256").update(crypto.randomUUID()).update(crypto.randomUUID()).digest("hex");
|
|
242
|
+
this.setMeta("gateway:token", token);
|
|
243
|
+
return token;
|
|
244
|
+
}
|
|
245
|
+
close(): void {
|
|
246
|
+
if (this.closed) return;
|
|
247
|
+
this.closed = true;
|
|
248
|
+
this.db.close(true);
|
|
249
|
+
this.lock.close(true);
|
|
250
|
+
}
|
|
251
|
+
}
|