agentschat-mcp 0.36.24 → 0.36.26
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/CHANGELOG.md +12 -0
- package/README.md +8 -0
- package/codex/README.md +1 -1
- package/codex/app-server.ts +79 -22
- package/codex/bots-config.ts +31 -11
- package/codex/bridge.ts +123 -25
- package/codex/config.ts +128 -21
- package/codex/gui-channel.ts +66 -9
- package/codex/inbox-sync.ts +65 -12
- package/codex/loop-grants.ts +41 -11
- package/codex/manager.ts +101 -25
- package/codex/owner.ts +81 -9
- package/codex/processes.ts +51 -7
- package/codex/run.ts +70 -17
- package/codex/runtime-home.ts +41 -11
- package/codex/thread-history.ts +77 -30
- package/codex/transport.ts +122 -19
- package/connector/auth.ts +57 -5
- package/connector/backfill.ts +92 -10
- package/connector/descriptor.ts +27 -1
- package/connector/identities.ts +82 -14
- package/connector/ingest.ts +11 -2
- package/connector/normalize.ts +32 -12
- package/connector/run.ts +314 -67
- package/connector/server.ts +95 -26
- package/dist/codex-bots.js +410 -73
- package/dist/codex-bridge.js +1996 -576
- package/dist/connector.js +1286 -493
- package/dist/dsh.js +1776 -299
- package/dist/muse.js +967 -221
- package/dist/server.js +7645 -1172
- package/dsh/client.js +7 -5
- package/dsh/identity-ui/client.js +7 -5
- package/dsh/identity-ui/index.js +43 -8
- package/dsh/inbound-bridge/index.js +595 -146
- package/dsh/src/cli.ts +430 -91
- package/dsh/src/patch.ts +295 -15
- package/dsh/tests/dsh-hardening-127.test.ts +207 -0
- package/dsh/tests/dsh.test.ts +990 -3
- package/muse/scripts/ensure-muse-wakes.mjs +5 -3
- package/muse/scripts/muse-wake-receiver.mjs +4 -1
- package/muse/src/bridge.ts +54 -13
- package/muse/src/cli.ts +226 -41
- package/muse/src/client.ts +249 -34
- package/muse/src/config.ts +128 -12
- package/muse/src/policy.ts +28 -8
- package/muse/src/types.ts +1 -0
- package/muse/src/typing.ts +18 -1
- package/muse/src/wake.ts +237 -35
- package/muse/tests/bridge.test.mjs +131 -0
- package/muse/tests/client.test.mjs +327 -0
- package/muse/tests/config.test.mjs +78 -1
- package/muse/tests/hardening.test.mjs +199 -0
- package/muse/tests/hardening126.test.mjs +292 -0
- package/muse/tests/policy.test.mjs +83 -1
- package/muse/tests/wake.test.mjs +134 -6
- package/package.json +3 -3
- package/scripts/ensure-grok-wakes.mjs +93 -8
- package/scripts/example-url-wake-receiver.mjs +254 -61
- package/scripts/grok-bind-register.sh +38 -8
- package/skills/agentschat-team-lead/SKILL.md +1 -1
- package/skills/onboarding.md +14 -10
- package/src/argcheck.ts +1890 -1
- package/src/cli.mjs +98 -14
- package/src/dedup.ts +57 -8
- package/src/grok-bind.ts +160 -33
- package/src/heartbeat.ts +39 -11
- package/src/identity-guard.ts +93 -20
- package/src/identity.ts +67 -11
- package/src/mentions.ts +26 -5
- package/src/onboarding-status.ts +134 -11
- package/src/profile-selection.ts +269 -18
- package/src/profile-store.ts +222 -28
- package/src/read-cursor.ts +256 -9
- package/src/reconnect.ts +98 -2
- package/src/redact.ts +34 -15
- package/src/rest-cli.ts +713 -159
- package/src/server.ts +2487 -342
- package/src/session-bind.ts +731 -132
- package/src/session-route.ts +183 -12
- package/src/team-lead-skill.ts +3 -0
- package/src/timestamps.ts +56 -23
- package/src/tool-annotations.ts +22 -1
- package/src/wake.ts +524 -90
package/codex/thread-history.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { mkdirSync,
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { chmodSync, lstatSync, mkdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
+
import { assertDirectoryAncestors, atomicReplaceRegularFile } from "./config.ts";
|
|
4
5
|
|
|
5
6
|
export interface ThreadHistory { id: string; createdAt?: number; turns: any[] }
|
|
6
7
|
export function priorChannelThreads(threads: Record<string, string>, channel: string): string[] {
|
|
7
|
-
|
|
8
|
+
if (!threads || typeof threads !== "object" || typeof channel !== "string" || !channel) return [];
|
|
9
|
+
return [...new Set(Object.entries(threads).filter(([key, id]) => {
|
|
10
|
+
if (["__proto__", "constructor", "prototype"].includes(key.toLowerCase())) return false;
|
|
11
|
+
if (typeof id !== "string" || !id.trim()) return false;
|
|
8
12
|
if (key === channel) return true;
|
|
9
13
|
try { const parts = JSON.parse(key); return Array.isArray(parts) && parts[0] === channel; }
|
|
10
14
|
catch { return false; }
|
|
@@ -13,43 +17,86 @@ export function priorChannelThreads(threads: Record<string, string>, channel: st
|
|
|
13
17
|
|
|
14
18
|
/** Keep complete source turns privately; seed the new task with past chat, not old instructions. */
|
|
15
19
|
export function preserveChannelHistory(directory: string, channel: string, histories: ThreadHistory[], redact: (s: string) => string): string {
|
|
16
|
-
|
|
20
|
+
if (typeof channel !== "string" || !channel.trim() || channel.length > 256 || /[\x00-\x1f\x7f-\x9f\u2028\u2029]/.test(channel)) {
|
|
21
|
+
throw new Error("Invalid history channel");
|
|
22
|
+
}
|
|
23
|
+
const checkDirectory = (path: string) => {
|
|
24
|
+
try {
|
|
25
|
+
const stat = lstatSync(path);
|
|
26
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) throw new Error("History directory must be a non-symlink directory");
|
|
27
|
+
} catch (error: any) { if (error.code !== "ENOENT") throw error; }
|
|
28
|
+
};
|
|
29
|
+
assertDirectoryAncestors(directory);
|
|
30
|
+
const root = join(directory, "history");
|
|
31
|
+
checkDirectory(directory);
|
|
32
|
+
checkDirectory(root);
|
|
33
|
+
mkdirSync(root, {recursive:true, mode:0o700});
|
|
34
|
+
checkDirectory(root);
|
|
35
|
+
if (process.platform !== "win32") chmodSync(root, 0o700);
|
|
17
36
|
const file = join(root, createHash("sha256").update(channel).digest("hex").slice(0,24) + ".json");
|
|
18
|
-
|
|
19
|
-
|
|
37
|
+
try {
|
|
38
|
+
const stat = lstatSync(file);
|
|
39
|
+
if (stat.isSymbolicLink() || !stat.isFile()) throw new Error("History archive must be a regular file, not a symlink");
|
|
40
|
+
} catch (e: any) {
|
|
41
|
+
if (e.code !== "ENOENT") throw e;
|
|
42
|
+
}
|
|
43
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.${randomBytes(4).toString("hex")}.tmp`;
|
|
44
|
+
try {
|
|
45
|
+
writeFileSync(tmp, redact(JSON.stringify({channel_id:channel, threads:histories})), {flag:"wx", mode:0o600});
|
|
46
|
+
if (process.platform !== "win32") chmodSync(tmp, 0o600);
|
|
47
|
+
atomicReplaceRegularFile(tmp, file, () => {
|
|
48
|
+
try {
|
|
49
|
+
const stat = lstatSync(file);
|
|
50
|
+
if (stat.isSymbolicLink() || !stat.isFile()) throw new Error("History archive must be a regular file, not a symlink");
|
|
51
|
+
} catch (error: any) { if (error.code !== "ENOENT") throw error; }
|
|
52
|
+
});
|
|
53
|
+
} catch (err) {
|
|
54
|
+
try { unlinkSync(tmp); } catch { /* Best-effort temp cleanup. */ }
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
20
57
|
const records: { order: string; sequence: number; value: any }[] = [];
|
|
21
58
|
let sequence = 0;
|
|
22
|
-
for (const history of histories)
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
59
|
+
for (const history of Array.isArray(histories) ? histories : []) {
|
|
60
|
+
if (!history || typeof history !== "object") continue;
|
|
61
|
+
for (const turn of Array.isArray(history.turns) ? history.turns : []) {
|
|
62
|
+
if (!turn || typeof turn !== "object") continue;
|
|
63
|
+
// Codex turn IDs are UUIDv7 and sort by creation time; keep item order within a turn.
|
|
64
|
+
const order = String(turn.id ?? history.createdAt ?? history.id);
|
|
65
|
+
for (const item of Array.isArray(turn.items) ? turn.items : []) {
|
|
66
|
+
if (!item || typeof item !== "object") continue;
|
|
67
|
+
if (item.type === "userMessage") {
|
|
68
|
+
const contentParts = Array.isArray(item.content) ? item.content : [];
|
|
69
|
+
let text = contentParts.filter((part: any) => part?.type === "text" && typeof part.text === "string").map((part: any) => part.text).join("\n");
|
|
70
|
+
const marker = "AgentsChat message:\n";
|
|
71
|
+
if (text.includes(marker)) {
|
|
72
|
+
try {
|
|
73
|
+
let message = JSON.parse(text.slice(text.lastIndexOf(marker) + marker.length));
|
|
74
|
+
if (message && typeof message === "object" && !Array.isArray(message)) {
|
|
75
|
+
message = Object.fromEntries(Object.entries(message).filter(([key]) =>
|
|
76
|
+
!["__proto__", "constructor", "prototype"].includes(key.toLowerCase())));
|
|
77
|
+
if (message.channel_id === channel) records.push({order,sequence:sequence++,value:{...message,role:"user"}});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
} catch { /* Older bridges used plain-text envelopes; retain them below. */ }
|
|
81
|
+
}
|
|
82
|
+
// Do not recursively import a previous migration's large preview.
|
|
83
|
+
// The complete source remains in the private export; real messages above
|
|
84
|
+
// have already been extracted from their envelope.
|
|
85
|
+
if (text.startsWith("Previous conversations for this channel have been consolidated.")) {
|
|
86
|
+
const end = text.lastIndexOf("End of historical context.\n");
|
|
87
|
+
if (end >= 0) text = text.slice(end + "End of historical context.\n".length);
|
|
88
|
+
}
|
|
89
|
+
if (text.trim()) records.push({order,sequence:sequence++,value:{role:"user",content:text}});
|
|
90
|
+
} else if (item.type === "agentMessage" && (!item.phase || item.phase === "final_answer") && typeof item.text === "string") {
|
|
91
|
+
records.push({order,sequence:sequence++,value:{role:"assistant",content:item.text}});
|
|
42
92
|
}
|
|
43
|
-
if (text.trim()) records.push({order,sequence:sequence++,value:{role:"user",content:text}});
|
|
44
|
-
} else if (item.type === "agentMessage" && (!item.phase || item.phase === "final_answer")) {
|
|
45
|
-
records.push({order,sequence:sequence++,value:{role:"assistant",content:item.text}});
|
|
46
93
|
}
|
|
47
94
|
}
|
|
48
95
|
}
|
|
49
96
|
records.sort((a,b) => a.order.localeCompare(b.order) || a.sequence-b.sequence);
|
|
50
97
|
const seen = new Set<string>();
|
|
51
98
|
const lines = records.filter(({value}) => {
|
|
52
|
-
if (!value.id) return true;
|
|
99
|
+
if (typeof value.id !== "string" || !value.id) return true;
|
|
53
100
|
if (seen.has(value.id)) return false;
|
|
54
101
|
seen.add(value.id); return true;
|
|
55
102
|
}).map(({value}) => redact(JSON.stringify(value)));
|
package/codex/transport.ts
CHANGED
|
@@ -2,8 +2,41 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import WebSocket from "ws";
|
|
3
3
|
import type { BridgeConfig } from "./config.ts";
|
|
4
4
|
import { HeartbeatMonitor } from "../src/heartbeat.ts";
|
|
5
|
+
import { resolveDrainReconnectDelay } from "../src/reconnect.ts";
|
|
5
6
|
import { InboxSync } from "./inbox-sync.ts";
|
|
6
7
|
|
|
8
|
+
function safeChannel(channel: unknown): channel is string {
|
|
9
|
+
return typeof channel === "string" && channel.trim().length > 0 && channel === channel.trim() && channel.length <= 128 &&
|
|
10
|
+
!/[.\/\\\x00-\x1f\x7f-\x9f\u2028\u2029]/.test(channel) && !/%(?:2e%2e|2f|5c|00)/i.test(channel) &&
|
|
11
|
+
!/^(?:__proto__|constructor|prototype)$/i.test(channel);
|
|
12
|
+
}
|
|
13
|
+
function requireChannel(channel: unknown): asserts channel is string {
|
|
14
|
+
if (!safeChannel(channel)) throw new Error("Invalid AgentsChat channel");
|
|
15
|
+
}
|
|
16
|
+
function apiUrl(path: string, base: string): URL {
|
|
17
|
+
const invalid = () => new Error("Invalid AgentsChat API path");
|
|
18
|
+
if (typeof path !== "string" || !path.startsWith("/") || path.startsWith("//") || /[\x00-\x1f\x7f-\x9f\u2028\u2029\\]/.test(path)) throw invalid();
|
|
19
|
+
try {
|
|
20
|
+
const pathname = path.split(/[?#]/, 1)[0]!;
|
|
21
|
+
if (/%25(?:2e|2f|5c|00)/i.test(pathname)) throw invalid();
|
|
22
|
+
const decoded = decodeURIComponent(pathname);
|
|
23
|
+
if ([pathname, decoded].some(value => value.split("/").includes("..")) || /[\x00-\x1f\x7f-\x9f\u2028\u2029\\]/.test(decoded)) throw invalid();
|
|
24
|
+
const origin = new URL(base).origin, resolved = new URL(path, base);
|
|
25
|
+
if (resolved.origin !== origin) throw invalid();
|
|
26
|
+
return resolved;
|
|
27
|
+
} catch { throw invalid(); }
|
|
28
|
+
}
|
|
29
|
+
function rawWsFrameBytes(raw: WebSocket.RawData): number {
|
|
30
|
+
if (Buffer.isBuffer(raw)) return raw.byteLength;
|
|
31
|
+
if (raw instanceof ArrayBuffer) return raw.byteLength;
|
|
32
|
+
if (Array.isArray(raw)) {
|
|
33
|
+
let total = 0;
|
|
34
|
+
for (const chunk of raw) total += chunk.byteLength;
|
|
35
|
+
return total;
|
|
36
|
+
}
|
|
37
|
+
return Buffer.byteLength(String(raw), "utf8");
|
|
38
|
+
}
|
|
39
|
+
|
|
7
40
|
export class AgentsChatTransport {
|
|
8
41
|
private socket?: WebSocket;
|
|
9
42
|
private authenticated = false;
|
|
@@ -17,44 +50,84 @@ export class AgentsChatTransport {
|
|
|
17
50
|
private syncTimer?: ReturnType<typeof setInterval>;
|
|
18
51
|
constructor(private config: BridgeConfig, private receive: (m: unknown) => void, private log: (s: string) => void = console.error,
|
|
19
52
|
recover?: (m: unknown) => boolean, private syncIntervalMs = 60_000) {
|
|
53
|
+
this.syncIntervalMs = Number.isFinite(syncIntervalMs) && syncIntervalMs > 0
|
|
54
|
+
? Math.min(Math.floor(syncIntervalMs), 2_147_483_647) : 60_000;
|
|
20
55
|
if (recover) this.inboxSync = new InboxSync(config.stateDir, path => this.api(path), recover,
|
|
21
56
|
() => !this.closed && this.authenticated, log);
|
|
22
57
|
}
|
|
23
58
|
async api(path: string, body?: unknown) {
|
|
59
|
+
const resolved = apiUrl(path, this.config.apiUrl);
|
|
24
60
|
let response: Response;
|
|
25
61
|
try {
|
|
26
|
-
response = await fetch(
|
|
62
|
+
response = await fetch(resolved, { method: body ? "POST" : "GET", redirect: "error",
|
|
27
63
|
headers: { Authorization: `Bearer ${this.config.token}`, "Content-Type": "application/json" },
|
|
28
64
|
...(body ? { body: JSON.stringify(body) } : {}), signal: AbortSignal.timeout(15_000) });
|
|
29
65
|
} catch { throw new Error("AgentsChat request failed or timed out"); }
|
|
30
|
-
if (!response.ok)
|
|
31
|
-
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
try { await response.body?.cancel?.(); } catch { /* Preserve the HTTP error. */ }
|
|
68
|
+
throw new Error(`AgentsChat HTTP ${response.status}`);
|
|
69
|
+
}
|
|
70
|
+
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
|
71
|
+
try {
|
|
72
|
+
reader = response.body?.getReader();
|
|
73
|
+
if (!reader) throw new Error("Missing response body");
|
|
74
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
75
|
+
let bytes = 0, text = "";
|
|
76
|
+
while (true) {
|
|
77
|
+
const { done, value } = await reader.read();
|
|
78
|
+
if (done) break;
|
|
79
|
+
bytes += value.byteLength;
|
|
80
|
+
if (bytes > 4 * 1024 * 1024) {
|
|
81
|
+
try { await reader.cancel(); } catch { /* Preserve the invalid-response error. */ }
|
|
82
|
+
throw new Error("Response too large");
|
|
83
|
+
}
|
|
84
|
+
text += decoder.decode(value, { stream: true });
|
|
85
|
+
}
|
|
86
|
+
const value = JSON.parse(text + decoder.decode());
|
|
87
|
+
if (value && typeof value === "object" && Object.hasOwn(value, "__proto__")) throw new Error("Invalid JSON prototype key");
|
|
88
|
+
return value;
|
|
89
|
+
} catch { throw new Error("Invalid AgentsChat response"); }
|
|
90
|
+
finally { reader?.releaseLock(); }
|
|
32
91
|
}
|
|
33
92
|
setTyping(channel: string, active: boolean) {
|
|
93
|
+
requireChannel(channel);
|
|
34
94
|
clearInterval(this.typing.get(channel)); this.typing.delete(channel);
|
|
35
95
|
if (!active || this.closed) return;
|
|
96
|
+
if (this.typing.size >= 64) throw new Error("AgentsChat typing limit reached");
|
|
36
97
|
const pulse = () => {
|
|
37
|
-
if (this.authenticated && this.socket?.readyState === WebSocket.OPEN)
|
|
38
|
-
this.socket.send(JSON.stringify({ type: "typing", channel_id: channel, sender_id: this.config.agentId, cross_pod: true }));
|
|
98
|
+
if (this.authenticated && this.socket?.readyState === WebSocket.OPEN) {
|
|
99
|
+
try { this.socket.send(JSON.stringify({ type: "typing", channel_id: channel, sender_id: this.config.agentId, cross_pod: true })); }
|
|
100
|
+
catch { /* The socket may close between the state check and send. */ }
|
|
101
|
+
}
|
|
39
102
|
};
|
|
40
|
-
pulse();
|
|
103
|
+
pulse();
|
|
104
|
+
const typingTimer = setInterval(pulse, 2000);
|
|
105
|
+
typingTimer.unref?.();
|
|
106
|
+
this.typing.set(channel, typingTimer);
|
|
107
|
+
}
|
|
108
|
+
private clearTyping() {
|
|
109
|
+
for (const timer of this.typing.values()) clearInterval(timer);
|
|
110
|
+
this.typing.clear();
|
|
41
111
|
}
|
|
42
112
|
private rejectPending() {
|
|
43
113
|
for (const p of this.pending.values()) { clearTimeout(p.timer); p.reject(new Error("AgentsChat acknowledgement unavailable")); }
|
|
44
114
|
this.pending.clear();
|
|
45
115
|
}
|
|
46
116
|
async send(channel: string, text: string) {
|
|
117
|
+
requireChannel(channel);
|
|
47
118
|
// Use the authenticated connection and wait for the server's delivery ACK.
|
|
48
119
|
// Never fall back after an attempted send: an ACK may have been lost.
|
|
49
120
|
if (this.authenticated && this.socket?.readyState === WebSocket.OPEN) {
|
|
121
|
+
if (this.pending.size >= 256) throw new Error("AgentsChat pending message limit reached");
|
|
50
122
|
const id = randomUUID();
|
|
51
123
|
await new Promise<void>((resolve, reject) => {
|
|
52
124
|
const timer = setTimeout(() => { this.pending.delete(id); reject(new Error("AgentsChat acknowledgement timed out")); }, 15000);
|
|
125
|
+
timer.unref?.();
|
|
53
126
|
this.pending.set(id, { resolve, reject, timer });
|
|
54
|
-
this.socket!.send(JSON.stringify({ type: "message", id, channel_id: channel,
|
|
127
|
+
try { this.socket!.send(JSON.stringify({ type: "message", id, channel_id: channel,
|
|
55
128
|
sender_id: this.config.agentId, sender_type: "agent", content_type: "text", content: text }), error => {
|
|
56
129
|
if (error) { clearTimeout(timer); this.pending.delete(id); reject(new Error("AgentsChat socket send failed")); }
|
|
57
|
-
});
|
|
130
|
+
}); } catch { clearTimeout(timer); this.pending.delete(id); reject(new Error("AgentsChat socket send failed")); }
|
|
58
131
|
});
|
|
59
132
|
return;
|
|
60
133
|
}
|
|
@@ -64,12 +137,22 @@ export class AgentsChatTransport {
|
|
|
64
137
|
}
|
|
65
138
|
start() {
|
|
66
139
|
if (this.closed) return;
|
|
140
|
+
clearTimeout(this.retry); this.retry = undefined;
|
|
141
|
+
clearInterval(this.syncTimer); this.syncTimer = undefined;
|
|
142
|
+
this.heartbeat?.stop(); this.heartbeat = undefined;
|
|
143
|
+
this.authenticated = false; this.clearTyping(); this.rejectPending();
|
|
144
|
+
const previous = this.socket; this.socket = undefined;
|
|
145
|
+
previous?.terminate();
|
|
67
146
|
const socket = new WebSocket(this.config.wsUrl, { maxPayload: 1_048_576, handshakeTimeout: 15_000 });
|
|
68
147
|
this.socket = socket;
|
|
69
148
|
const current = () => !this.closed && this.socket === socket;
|
|
70
|
-
const send = (value: unknown) => {
|
|
149
|
+
const send = (value: unknown) => {
|
|
150
|
+
if (current() && socket.readyState === WebSocket.OPEN) {
|
|
151
|
+
try { socket.send(JSON.stringify(value)); } catch { /* A concurrent close is handled below. */ }
|
|
152
|
+
}
|
|
153
|
+
};
|
|
71
154
|
const join = (channel: string) => {
|
|
72
|
-
if (!this.config.channels.length || this.config.channels.includes(channel)) {
|
|
155
|
+
if (safeChannel(channel) && (!this.config.channels.length || this.config.channels.includes(channel))) {
|
|
73
156
|
this.inboxSync?.watch(channel);
|
|
74
157
|
send({ type: "join_channel", channel_id: channel, agent_id: this.config.agentId });
|
|
75
158
|
}
|
|
@@ -79,17 +162,21 @@ export class AgentsChatTransport {
|
|
|
79
162
|
this.heartbeat.start();
|
|
80
163
|
socket.on("open", () => send({ type: "auth", agent_id: this.config.agentId, token: this.config.token, capabilities: ["chat", "codex"] }));
|
|
81
164
|
socket.on("message", raw => {
|
|
82
|
-
if (!current()) return;
|
|
165
|
+
if (!current() || rawWsFrameBytes(raw) > 1_048_576) return;
|
|
83
166
|
let data: any; try { data = JSON.parse(String(raw)); } catch { return; }
|
|
84
|
-
|
|
167
|
+
if (!data || typeof data !== "object" || Array.isArray(data) || Object.hasOwn(data, "__proto__")) return;
|
|
168
|
+
if (data.type !== "auth_error") this.heartbeat?.receivedPong();
|
|
85
169
|
if (data.type === "auth_ok") {
|
|
86
170
|
this.authenticated = true; this.delay = 1000; this.log(`AgentsChat connected as ${this.config.agentId}`);
|
|
87
|
-
if (this.inboxSync && !this.syncTimer)
|
|
171
|
+
if (this.inboxSync && !this.syncTimer) {
|
|
172
|
+
this.syncTimer = setInterval(() => { void this.inboxSync!.sync(); }, this.syncIntervalMs);
|
|
173
|
+
this.syncTimer.unref?.();
|
|
174
|
+
}
|
|
88
175
|
void this.api("/api/channels/mine").then((body: any) => {
|
|
89
176
|
if (!current()) return;
|
|
90
177
|
const channels = Array.isArray(body) ? body : body.channels;
|
|
91
178
|
if (!Array.isArray(channels)) throw new Error("Invalid membership response");
|
|
92
|
-
const ids = channels.map((c: any) => c
|
|
179
|
+
const ids = channels.map((c: any) => c?.id ?? c?.channel_id).filter((id: any) => safeChannel(id) &&
|
|
93
180
|
(!this.config.channels.length || this.config.channels.includes(id)));
|
|
94
181
|
this.inboxSync?.memberships(ids);
|
|
95
182
|
for (const id of ids) join(id);
|
|
@@ -100,15 +187,31 @@ export class AgentsChatTransport {
|
|
|
100
187
|
if (pending) { clearTimeout(pending.timer); this.pending.delete(id); pending.resolve(); }
|
|
101
188
|
} else if (data.type === "channel_created" && typeof data.channel_id === "string") join(data.channel_id);
|
|
102
189
|
else if (["message", "thread_reply"].includes(data.type)) this.receive(data);
|
|
103
|
-
else if (data.type === "shard_moved" || data.type === "please_reconnect")
|
|
104
|
-
|
|
190
|
+
else if (data.type === "shard_moved" || data.type === "please_reconnect") {
|
|
191
|
+
this.delay = resolveDrainReconnectDelay(data);
|
|
192
|
+
socket.terminate();
|
|
193
|
+
} else if (data.type === "auth_error") {
|
|
194
|
+
this.log("AgentsChat authentication rejected; check profile token (backing off 30s)");
|
|
195
|
+
this.delay = 30_000;
|
|
196
|
+
socket.close();
|
|
197
|
+
} else if (data.type === "error") this.log("AgentsChat returned an error; check account and channel permissions");
|
|
105
198
|
});
|
|
106
199
|
socket.on("error", () => this.log("AgentsChat socket error"));
|
|
107
200
|
socket.on("close", () => {
|
|
108
201
|
if (!current()) return;
|
|
109
|
-
|
|
110
|
-
this.
|
|
202
|
+
clearInterval(this.syncTimer); this.syncTimer = undefined;
|
|
203
|
+
this.authenticated = false; this.clearTyping(); this.rejectPending(); this.heartbeat?.stop(); this.log("AgentsChat disconnected; reconnecting with history reconciliation");
|
|
204
|
+
this.retry = setTimeout(() => this.start(), this.delay);
|
|
205
|
+
this.retry.unref?.();
|
|
206
|
+
this.delay = Math.min(this.delay * 2, 30_000);
|
|
111
207
|
});
|
|
112
208
|
}
|
|
113
|
-
stop() {
|
|
209
|
+
stop() {
|
|
210
|
+
this.closed = true; this.authenticated = false; this.clearTyping(); this.rejectPending();
|
|
211
|
+
clearTimeout(this.retry); this.retry = undefined;
|
|
212
|
+
clearInterval(this.syncTimer); this.syncTimer = undefined;
|
|
213
|
+
this.heartbeat?.stop(); this.heartbeat = undefined;
|
|
214
|
+
const socket = this.socket; this.socket = undefined;
|
|
215
|
+
socket?.terminate();
|
|
216
|
+
}
|
|
114
217
|
}
|
package/connector/auth.ts
CHANGED
|
@@ -38,6 +38,8 @@ export function sign(payload: string, secret: string): string {
|
|
|
38
38
|
|
|
39
39
|
/** Constant-time check that `sigHex` is a valid HMAC of `payload` under ANY of `secrets`. */
|
|
40
40
|
export function verifySignature(payload: string, sigHex: string, secrets: readonly string[]): boolean {
|
|
41
|
+
if (typeof payload !== "string" || !Array.isArray(secrets)) return false;
|
|
42
|
+
if (typeof sigHex !== "string" || !/^[0-9a-f]{64}$/i.test(sigHex)) return false;
|
|
41
43
|
let sigBuf: Buffer;
|
|
42
44
|
try {
|
|
43
45
|
sigBuf = Buffer.from(sigHex, "hex");
|
|
@@ -46,7 +48,14 @@ export function verifySignature(payload: string, sigHex: string, secrets: readon
|
|
|
46
48
|
}
|
|
47
49
|
if (sigBuf.length === 0) return false;
|
|
48
50
|
for (const secret of secrets) {
|
|
49
|
-
if (
|
|
51
|
+
if (
|
|
52
|
+
typeof secret !== "string" ||
|
|
53
|
+
secret.length === 0 ||
|
|
54
|
+
secret.length > 8192 ||
|
|
55
|
+
DISALLOWED_TOKEN_PAYLOAD_CTRL_RE.test(secret)
|
|
56
|
+
) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
50
59
|
const expected = Buffer.from(hmacHex(payload, secret), "hex");
|
|
51
60
|
if (expected.length !== sigBuf.length) continue; // no timing leak on length
|
|
52
61
|
if (timingSafeEqual(sigBuf, expected)) return true;
|
|
@@ -58,8 +67,32 @@ export function verifySignature(payload: string, sigHex: string, secrets: readon
|
|
|
58
67
|
* Build a signed, optionally-expiring token (mirrors auth.py `make_token`).
|
|
59
68
|
* `base64url(f"{payload}:{exp}:{sig}")`, unpadded; `exp` is unix-seconds, 0 = never.
|
|
60
69
|
*/
|
|
70
|
+
const DISALLOWED_TOKEN_PAYLOAD_CTRL_RE = /[\x00-\x1f\x7f-\x9f\u2028\u2029]/;
|
|
71
|
+
const PROTOTYPE_PAYLOAD_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
72
|
+
const STRICT_BASE64URL_RE = /^[A-Za-z0-9_-]+$/;
|
|
73
|
+
|
|
61
74
|
export function makeToken(payload: string, secret: string, ttlSeconds = 0): string {
|
|
62
|
-
|
|
75
|
+
if (
|
|
76
|
+
typeof payload !== "string" ||
|
|
77
|
+
payload.length > 8192 ||
|
|
78
|
+
DISALLOWED_TOKEN_PAYLOAD_CTRL_RE.test(payload) ||
|
|
79
|
+
PROTOTYPE_PAYLOAD_KEYS.has(payload.toLowerCase())
|
|
80
|
+
) {
|
|
81
|
+
throw new Error("invalid token payload");
|
|
82
|
+
}
|
|
83
|
+
if (
|
|
84
|
+
typeof secret !== "string" ||
|
|
85
|
+
secret.length === 0 ||
|
|
86
|
+
secret.length > 8192 ||
|
|
87
|
+
DISALLOWED_TOKEN_PAYLOAD_CTRL_RE.test(secret)
|
|
88
|
+
) {
|
|
89
|
+
throw new Error("invalid token secret");
|
|
90
|
+
}
|
|
91
|
+
if (!Number.isFinite(ttlSeconds) || ttlSeconds < 0) {
|
|
92
|
+
throw new RangeError("ttlSeconds must be finite and non-negative");
|
|
93
|
+
}
|
|
94
|
+
const exp = ttlSeconds > 0 ? Math.ceil(Math.floor(Date.now() / 1000) + ttlSeconds) : 0;
|
|
95
|
+
if (!Number.isSafeInteger(exp)) throw new RangeError("expiration exceeds safe integer range");
|
|
63
96
|
const signed = `${payload}:${exp}`;
|
|
64
97
|
const sig = hmacHex(signed, secret);
|
|
65
98
|
return Buffer.from(`${signed}:${sig}`, "utf8").toString("base64url");
|
|
@@ -80,18 +113,37 @@ export function makeUpgradeToken(
|
|
|
80
113
|
* any signature not matching a secret in the verify list.
|
|
81
114
|
*/
|
|
82
115
|
export function verifyToken(token: string, secrets: readonly string[]): string | null {
|
|
116
|
+
if (
|
|
117
|
+
typeof token !== "string" ||
|
|
118
|
+
token.length === 0 ||
|
|
119
|
+
token.length > 4096 ||
|
|
120
|
+
!STRICT_BASE64URL_RE.test(token) ||
|
|
121
|
+
!Array.isArray(secrets)
|
|
122
|
+
) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
83
125
|
let decoded: string;
|
|
84
126
|
try {
|
|
85
|
-
|
|
127
|
+
const rawBuf = Buffer.from(token, "base64url");
|
|
128
|
+
if (rawBuf.toString("base64url") !== token) return null;
|
|
129
|
+
decoded = rawBuf.toString("utf8");
|
|
86
130
|
} catch {
|
|
87
131
|
return null;
|
|
88
132
|
}
|
|
89
133
|
const parts = decoded.split(":");
|
|
90
134
|
if (parts.length < 3) return null;
|
|
91
135
|
const sig = parts[parts.length - 1];
|
|
92
|
-
const
|
|
93
|
-
if (
|
|
136
|
+
const expText = parts[parts.length - 2];
|
|
137
|
+
if (!/^\d+$/.test(expText)) return null;
|
|
138
|
+
const exp = Number(expText);
|
|
139
|
+
if (!Number.isSafeInteger(exp) || String(exp) !== expText) return null;
|
|
94
140
|
const payload = parts.slice(0, -2).join(":");
|
|
141
|
+
if (
|
|
142
|
+
DISALLOWED_TOKEN_PAYLOAD_CTRL_RE.test(payload) ||
|
|
143
|
+
PROTOTYPE_PAYLOAD_KEYS.has(payload.toLowerCase())
|
|
144
|
+
) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
95
147
|
if (exp !== 0 && Math.floor(Date.now() / 1000) > exp) return null;
|
|
96
148
|
const signed = `${payload}:${exp}`;
|
|
97
149
|
return verifySignature(signed, sig, secrets) ? payload : null;
|
package/connector/backfill.ts
CHANGED
|
@@ -6,32 +6,114 @@
|
|
|
6
6
|
* Timestamp comparison uses the same padded-ISO cursor as the MCP path so
|
|
7
7
|
* lexical `>` matches chronology (whole-second vs fractional).
|
|
8
8
|
*/
|
|
9
|
+
import { join } from "node:path";
|
|
9
10
|
import { normalizeTimestampForCursor } from "../src/timestamps.ts";
|
|
10
11
|
import { serverLoopTick, type AgentsChatMessage } from "./normalize.ts";
|
|
11
12
|
|
|
12
13
|
export type BackfillMsg = AgentsChatMessage;
|
|
13
14
|
|
|
15
|
+
const CANONICAL_CURSOR_TS_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z$/;
|
|
16
|
+
const RESERVED_CURSOR_BOT_NAMES = /^(?:__proto__|constructor|prototype|con|prn|aux|nul|com[1-9]|lpt[1-9])$/i;
|
|
17
|
+
|
|
18
|
+
export function canonicalCursorTimestamp(raw: unknown): string | null {
|
|
19
|
+
if (typeof raw !== "string" || raw.length === 0 || raw.length > 128) return null;
|
|
20
|
+
const norm = normalizeTimestampForCursor(raw, "after");
|
|
21
|
+
return typeof norm === "string" && CANONICAL_CURSOR_TS_RE.test(norm) ? norm : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isPlainMessageRecord(m: unknown): m is BackfillMsg {
|
|
25
|
+
return Boolean(
|
|
26
|
+
m &&
|
|
27
|
+
typeof m === "object" &&
|
|
28
|
+
!Array.isArray(m) &&
|
|
29
|
+
!Object.prototype.hasOwnProperty.call(m, "__proto__"),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function compareNormalizedTimestamps(aRaw: unknown, bRaw: unknown): number {
|
|
34
|
+
const aNorm = canonicalCursorTimestamp(aRaw) ?? "";
|
|
35
|
+
const bNorm = canonicalCursorTimestamp(bRaw) ?? "";
|
|
36
|
+
return aNorm < bNorm ? -1 : aNorm > bNorm ? 1 : 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
14
39
|
export function planBackfill(
|
|
15
40
|
after: string | undefined,
|
|
16
41
|
msgs: BackfillMsg[],
|
|
17
42
|
agentId: string,
|
|
18
43
|
): { seed?: string; replay: BackfillMsg[] } {
|
|
19
44
|
const list = Array.isArray(msgs) ? msgs : [];
|
|
20
|
-
|
|
21
|
-
|
|
45
|
+
const afterTs = canonicalCursorTimestamp(after);
|
|
46
|
+
if (!afterTs) {
|
|
47
|
+
let newestRaw = "";
|
|
48
|
+
let newestNorm = "";
|
|
22
49
|
for (const m of list) {
|
|
23
|
-
|
|
24
|
-
|
|
50
|
+
if (!isPlainMessageRecord(m) || typeof m.content !== "string" || m.content === "__typing__") continue;
|
|
51
|
+
const raw = typeof m.timestamp === "string" ? m.timestamp : "";
|
|
52
|
+
const norm = canonicalCursorTimestamp(raw);
|
|
53
|
+
if (!norm) continue;
|
|
54
|
+
if (norm > newestNorm) {
|
|
55
|
+
newestNorm = norm;
|
|
56
|
+
newestRaw = raw;
|
|
57
|
+
}
|
|
25
58
|
}
|
|
26
|
-
return
|
|
59
|
+
return newestRaw ? { seed: newestRaw, replay: [] } : { replay: [] };
|
|
27
60
|
}
|
|
28
|
-
const afterTs = normalizeTimestampForCursor(after, "after") || after;
|
|
29
61
|
const replay = list.filter((m) => {
|
|
30
|
-
if (!m || m.content === "__typing__") return false;
|
|
62
|
+
if (!isPlainMessageRecord(m) || typeof m.content !== "string" || m.content === "__typing__") return false;
|
|
31
63
|
if (m.sender_id === agentId && !serverLoopTick(m)) return false;
|
|
32
|
-
const msgTs =
|
|
33
|
-
return
|
|
64
|
+
const msgTs = canonicalCursorTimestamp(m.timestamp);
|
|
65
|
+
return msgTs !== null && msgTs > afterTs;
|
|
34
66
|
});
|
|
35
|
-
replay.sort((a, b) =>
|
|
67
|
+
replay.sort((a, b) => compareNormalizedTimestamps(a.timestamp, b.timestamp));
|
|
36
68
|
return { replay };
|
|
37
69
|
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Select up to 10 chronological channel context messages strictly after `sinceTs`
|
|
73
|
+
* (excluding typing placeholders and `excludeId`), using padded ISO comparison so
|
|
74
|
+
* whole-second (`...Z`) and fractional (`....500Z`) timestamps never invert order.
|
|
75
|
+
*/
|
|
76
|
+
export function selectChannelContextMessages(
|
|
77
|
+
msgs: BackfillMsg[],
|
|
78
|
+
sinceTs?: string,
|
|
79
|
+
excludeId?: string,
|
|
80
|
+
): BackfillMsg[] {
|
|
81
|
+
const list = Array.isArray(msgs) ? msgs : [];
|
|
82
|
+
const filtered = list
|
|
83
|
+
.filter(
|
|
84
|
+
(m) =>
|
|
85
|
+
isPlainMessageRecord(m) &&
|
|
86
|
+
typeof m.content === "string" &&
|
|
87
|
+
m.content.length > 0 &&
|
|
88
|
+
m.content !== "__typing__" &&
|
|
89
|
+
(!excludeId || m.id !== excludeId),
|
|
90
|
+
)
|
|
91
|
+
.sort((a, b) => compareNormalizedTimestamps(a.timestamp, b.timestamp));
|
|
92
|
+
if (!sinceTs) return filtered.slice(-10);
|
|
93
|
+
const normSince = canonicalCursorTimestamp(sinceTs);
|
|
94
|
+
if (!normSince) return [];
|
|
95
|
+
return filtered
|
|
96
|
+
.filter((m) => {
|
|
97
|
+
const norm = canonicalCursorTimestamp(m.timestamp);
|
|
98
|
+
return norm !== null && norm > normSince;
|
|
99
|
+
})
|
|
100
|
+
.slice(-10);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Sanitize `botId` when building per-bot cursor filenames so path separators,
|
|
105
|
+
* `..` traversal segments, or Windows-invalid filename characters cannot escape
|
|
106
|
+
* `cursorDir` or fail persistence while keeping standard agent IDs unchanged.
|
|
107
|
+
*/
|
|
108
|
+
export function safeCursorBotFile(cursorDir: string, botId: string): string {
|
|
109
|
+
const raw = typeof botId === "string" && botId.trim() ? botId : "default";
|
|
110
|
+
const cleaned = raw
|
|
111
|
+
.replace(/[^A-Za-z0-9._-]/g, "_")
|
|
112
|
+
.replace(/\.+$/, "")
|
|
113
|
+
.replace(/\.{2,}/g, "_");
|
|
114
|
+
const safeId =
|
|
115
|
+
cleaned && !RESERVED_CURSOR_BOT_NAMES.test(cleaned)
|
|
116
|
+
? cleaned.slice(0, 128)
|
|
117
|
+
: "default";
|
|
118
|
+
return join(cursorDir, `last-seen-msg-ts-${safeId}.json`);
|
|
119
|
+
}
|
package/connector/descriptor.ts
CHANGED
|
@@ -36,7 +36,33 @@ export const SUPPORTED_OPS = ["send", "typing", "get_chat_info"] as const;
|
|
|
36
36
|
* Build the descriptor the connector hands the gateway at handshake.
|
|
37
37
|
* Single-tenant agentschat: narrow, honest capability set.
|
|
38
38
|
*/
|
|
39
|
+
const DESCRIPTOR_PROTO_KEYS = new Set([
|
|
40
|
+
"prototype",
|
|
41
|
+
...Object.getOwnPropertyNames(Object.prototype).map((k) => k.toLowerCase()),
|
|
42
|
+
]);
|
|
43
|
+
|
|
39
44
|
export function buildDescriptor(overrides: Partial<CapabilityDescriptor> = {}): CapabilityDescriptor {
|
|
45
|
+
if (
|
|
46
|
+
!overrides ||
|
|
47
|
+
typeof overrides !== "object" ||
|
|
48
|
+
Array.isArray(overrides) ||
|
|
49
|
+
Object.prototype.hasOwnProperty.call(overrides, "__proto__")
|
|
50
|
+
) {
|
|
51
|
+
throw new Error("invalid descriptor overrides");
|
|
52
|
+
}
|
|
53
|
+
const keys = new Set<string>();
|
|
54
|
+
for (const key of Object.keys(overrides)) {
|
|
55
|
+
const trimmed = key.trim();
|
|
56
|
+
if (!trimmed || DESCRIPTOR_PROTO_KEYS.has(trimmed.toLowerCase()) ||
|
|
57
|
+
keys.has(trimmed)) throw new Error("invalid or duplicate descriptor override key");
|
|
58
|
+
keys.add(trimmed);
|
|
59
|
+
}
|
|
60
|
+
if ([...keys].some(key => !Object.hasOwn(overrides, key))) {
|
|
61
|
+
throw new Error("descriptor override keys must not contain surrounding whitespace");
|
|
62
|
+
}
|
|
63
|
+
const resolvedOps = Array.isArray(overrides.supported_ops)
|
|
64
|
+
? [...overrides.supported_ops]
|
|
65
|
+
: [...SUPPORTED_OPS];
|
|
40
66
|
return {
|
|
41
67
|
contract_version: CONTRACT_VERSION,
|
|
42
68
|
platform: "agentschat",
|
|
@@ -50,7 +76,7 @@ export function buildDescriptor(overrides: Partial<CapabilityDescriptor> = {}):
|
|
|
50
76
|
emoji: "🤖",
|
|
51
77
|
pii_safe: false,
|
|
52
78
|
platform_hint: "AgentsChat named skills are reusable instructions. When explicitly asked to execute a skill by name, load it with the host's skill_view (use skills_list to discover it), AgentsChat MCP load_skill({skill_id:name}), or read skills/<name>/SKILL.md from the installed AgentsChat MCP package in this runtime. Report a missing loader or skill; do not pretend it ran. Keep skill text out of chat and keep replies in the originating chat.",
|
|
53
|
-
supported_ops: [...SUPPORTED_OPS],
|
|
54
79
|
...overrides,
|
|
80
|
+
supported_ops: resolvedOps,
|
|
55
81
|
};
|
|
56
82
|
}
|