agentschat-mcp 0.36.25 → 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 +7 -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 +4277 -929
- 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 +1 -1
- 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/onboarding.md +9 -9
- package/src/argcheck.ts +113 -33
- 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 +285 -93
- 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 +10 -1
- package/src/wake.ts +510 -91
package/connector/server.ts
CHANGED
|
@@ -11,14 +11,33 @@
|
|
|
11
11
|
* All connector frames are newline-terminated (relay wire contract v1).
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
14
15
|
import { createServer, type Server as HttpServer, type IncomingMessage } from "node:http";
|
|
15
16
|
import { WebSocketServer, WebSocket } from "ws";
|
|
16
17
|
import { verifyUpgradeToken, CLOSE_UNAUTHORIZED } from "./auth.ts";
|
|
17
18
|
import { buildDescriptor, type CapabilityDescriptor } from "./descriptor.ts";
|
|
18
19
|
import { toWireEvent, serverLoopTick, matchesLiveLoop, type AgentsChatMessage } from "./normalize.ts";
|
|
20
|
+
import { redactSecrets } from "../src/redact.ts";
|
|
19
21
|
import { MessageDedup } from "../src/dedup.ts";
|
|
20
22
|
import { IdentityTable, routeInboundTargets, hermesSourceProfile, type Identity } from "./identities.ts";
|
|
21
23
|
|
|
24
|
+
const CONNECTOR_LOG_CTRL_RE = /[\x00-\x1f\x7f-\x9f\u2028\u2029]+/g;
|
|
25
|
+
|
|
26
|
+
function safeSecretEqual(a: unknown, b: unknown): boolean {
|
|
27
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
28
|
+
const da = createHash("sha256").update(a, "utf8").digest();
|
|
29
|
+
const db = createHash("sha256").update(b, "utf8").digest();
|
|
30
|
+
return timingSafeEqual(da, db) && a.length === b.length;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function sanitizeConnectorLogField(value: unknown, maxLen = 128): string {
|
|
34
|
+
const cleaned = redactSecrets(String(value ?? ""))
|
|
35
|
+
.replace(CONNECTOR_LOG_CTRL_RE, " ")
|
|
36
|
+
.trim()
|
|
37
|
+
.slice(0, maxLen);
|
|
38
|
+
return cleaned || "?";
|
|
39
|
+
}
|
|
40
|
+
|
|
22
41
|
/** What the connector needs from agentschat to fulfil outbound ops, per identity. */
|
|
23
42
|
export interface AgentsChatHooks {
|
|
24
43
|
sendMessage(botId: string, chatId: string, content: string, replyTo?: string): Promise<{ id?: string }>;
|
|
@@ -103,12 +122,14 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
103
122
|
const legacy = !config.identities || config.identities.length === 0;
|
|
104
123
|
const table = new IdentityTable(
|
|
105
124
|
legacy ? [{ botId: "default", agentId: "default", token: "", gatewayId: "", secret: "" }] : config.identities!,
|
|
125
|
+
legacy ? { allowEmptyCredentials: true } : undefined,
|
|
106
126
|
);
|
|
107
127
|
function safeLabel(value: string) {
|
|
128
|
+
let scrubbed = redactSecrets(String(value ?? "")).replace(CONNECTOR_LOG_CTRL_RE, " ");
|
|
108
129
|
for (const id of table.all()) for (const secret of [id.token, id.secret]) {
|
|
109
|
-
if (secret)
|
|
130
|
+
if (secret) scrubbed = scrubbed.split(secret).join("[redacted]");
|
|
110
131
|
}
|
|
111
|
-
return JSON.stringify(
|
|
132
|
+
return JSON.stringify(scrubbed.slice(0, 128));
|
|
112
133
|
}
|
|
113
134
|
// Normalize hooks to the per-identity shape. Legacy single-tenant hooks take
|
|
114
135
|
// (chatId, ...); wrap them to ignore the botId. Per-identity hooks take botId first.
|
|
@@ -132,18 +153,25 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
132
153
|
res.end(JSON.stringify({ ok: true, service: "agentschat-connector", contract_version: 1, identities: table.size }));
|
|
133
154
|
});
|
|
134
155
|
|
|
135
|
-
const wss = new WebSocketServer({ noServer: true });
|
|
156
|
+
const wss = new WebSocketServer({ noServer: true, maxPayload: 2 * 1024 * 1024 });
|
|
136
157
|
|
|
137
158
|
http.on("upgrade", (req: IncomingMessage, socket, head) => {
|
|
138
|
-
|
|
159
|
+
let pathname: string;
|
|
160
|
+
try {
|
|
161
|
+
pathname = new URL(req.url ?? "/", "http://localhost").pathname;
|
|
162
|
+
} catch {
|
|
163
|
+
socket.destroy();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
139
166
|
if (pathname !== "/relay") {
|
|
140
167
|
socket.destroy();
|
|
141
168
|
return;
|
|
142
169
|
}
|
|
143
170
|
const auth = req.headers.authorization ?? "";
|
|
144
|
-
const token = auth.startsWith("Bearer ") ? auth.slice(7).trim() : "";
|
|
171
|
+
const token = auth.length <= 8192 && auth.startsWith("Bearer ") ? auth.slice(7).trim() : "";
|
|
145
172
|
const payload = peekPayload(token);
|
|
146
|
-
const secrets = payload
|
|
173
|
+
const secrets = payload && Object.prototype.hasOwnProperty.call(config.secrets, payload) &&
|
|
174
|
+
Array.isArray(config.secrets[payload]) ? config.secrets[payload] : undefined;
|
|
147
175
|
const secret = secrets?.find((s) => verifyUpgradeToken(token, [s]) === payload);
|
|
148
176
|
const gatewayId = secret ? payload : null;
|
|
149
177
|
if (!gatewayId) {
|
|
@@ -165,16 +193,20 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
165
193
|
|
|
166
194
|
ws.on("message", async (data) => {
|
|
167
195
|
// Newline-delimited: the gateway may batch frames; split and handle each.
|
|
196
|
+
const bytes = Array.isArray(data) ? data.reduce((n, chunk) => n + chunk.byteLength, 0) : data.byteLength;
|
|
197
|
+
if (bytes > 2 * 1024 * 1024) { ws.close(1009, "message too large"); return; }
|
|
168
198
|
const text = data.toString();
|
|
169
|
-
|
|
170
|
-
|
|
199
|
+
const lines = text.split("\n").filter(line => line.trim());
|
|
200
|
+
if (lines.length > 128) { ws.close(1009, "batch too large"); return; }
|
|
201
|
+
for (const line of lines) {
|
|
171
202
|
let frame: any;
|
|
172
203
|
try {
|
|
173
204
|
frame = JSON.parse(line);
|
|
205
|
+
if (frame && typeof frame === "object" && Object.prototype.hasOwnProperty.call(frame, "__proto__")) continue;
|
|
174
206
|
} catch {
|
|
175
207
|
continue;
|
|
176
208
|
}
|
|
177
|
-
await handleFrame(conn, frame).catch((e) => log(`[connector] frame error: ${e}`));
|
|
209
|
+
await handleFrame(conn, frame).catch((e) => log(`[connector] frame error: ${redactSecrets(String(e))}`));
|
|
178
210
|
}
|
|
179
211
|
});
|
|
180
212
|
ws.on("close", () => {
|
|
@@ -193,7 +225,7 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
193
225
|
// registered identity (fail closed — never front an identity we can't send as).
|
|
194
226
|
const identity = legacy ? table.all()[0] : table.forBot(botId);
|
|
195
227
|
const reason = frame.platform !== "agentschat" ? "unsupported_platform" : !identity ? "unknown_identity"
|
|
196
|
-
: !legacy && (identity.gatewayId !== conn.gatewayId || identity.secret
|
|
228
|
+
: !legacy && (identity.gatewayId !== conn.gatewayId || !safeSecretEqual(identity.secret, conn.secret)) ? "credential_mismatch" : null;
|
|
197
229
|
const labels = `gatewayId=${safeLabel(conn.gatewayId)} platform=${safeLabel(frame.platform === "agentschat" ? "agentschat" : "(unsupported)")} botId=${safeLabel(identity ? botId : "(unknown)")}`;
|
|
198
230
|
if (reason) {
|
|
199
231
|
log(`[connector] hello rejected ${labels} reason=${reason}`);
|
|
@@ -239,15 +271,28 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
239
271
|
: typeof frame.botId === "string" && frame.botId ? frame.botId : null;
|
|
240
272
|
if (legacy && requested && requested === conn.legacyAlias) requested = "default";
|
|
241
273
|
let identity = requested && conn.fronted.has(requested) ? table.forBot(requested) : null;
|
|
242
|
-
if (identity && ((!legacy && (identity.gatewayId !== conn.gatewayId || identity.secret
|
|
274
|
+
if (identity && ((!legacy && (identity.gatewayId !== conn.gatewayId || !safeSecretEqual(identity.secret, conn.secret))) ||
|
|
243
275
|
(frame.platform !== undefined && frame.platform !== "agentschat"))) identity = null;
|
|
244
276
|
if (!identity) {
|
|
245
|
-
|
|
246
|
-
|
|
277
|
+
const safeReq = sanitizeConnectorLogField(requested ?? "?", 128);
|
|
278
|
+
log(`[connector] outbound failed: no usable identity for botId=${safeReq}`);
|
|
279
|
+
return { success: false, error: `no usable identity for outbound (botId=${safeReq})` };
|
|
280
|
+
}
|
|
281
|
+
if (typeof chatId !== "string" || chatId !== chatId.trim() || chatId.length < 1 || chatId.length > 128 ||
|
|
282
|
+
/[\s\x00-\x1f\x7f-\x9f\u2028\u2029/\\]/.test(chatId) || /(?:%2f|%5c|%00|%2e%2e)/i.test(chatId) ||
|
|
283
|
+
chatId === "." || chatId.includes("..") ||
|
|
284
|
+
["__proto__", "constructor", "prototype"].includes(chatId.toLowerCase())) {
|
|
285
|
+
return { success: false, error: "invalid chat_id" };
|
|
247
286
|
}
|
|
248
287
|
switch (op) {
|
|
249
288
|
case "send": {
|
|
250
|
-
const
|
|
289
|
+
const rawReplyTo = action.reply_to;
|
|
290
|
+
const safeReplyTo = typeof rawReplyTo === "string" && rawReplyTo.length > 0 && rawReplyTo.length <= 256 &&
|
|
291
|
+
!/[\s\x00-\x1f\x7f-\x9f\u2028\u2029]/.test(rawReplyTo) &&
|
|
292
|
+
!["__proto__", "constructor", "prototype"].includes(rawReplyTo.toLowerCase())
|
|
293
|
+
? rawReplyTo
|
|
294
|
+
: undefined;
|
|
295
|
+
const r = await hooks.sendMessage(identity.botId, chatId, action.content ?? "", safeReplyTo);
|
|
251
296
|
return { success: true, message_id: r?.id };
|
|
252
297
|
}
|
|
253
298
|
case "typing": {
|
|
@@ -258,16 +303,20 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
258
303
|
const info = await hooks.getChatInfo(identity.botId, chatId);
|
|
259
304
|
return info ?? {};
|
|
260
305
|
}
|
|
261
|
-
default:
|
|
262
|
-
|
|
263
|
-
|
|
306
|
+
default: {
|
|
307
|
+
const safeOp = sanitizeConnectorLogField(op, 64);
|
|
308
|
+
log(`[connector] outbound failed: unsupported op ${safeOp}`);
|
|
309
|
+
return { success: false, error: `unsupported op: ${safeOp}` };
|
|
310
|
+
}
|
|
264
311
|
}
|
|
265
312
|
}
|
|
266
313
|
|
|
267
314
|
function send(ws: WebSocket, obj: any) {
|
|
268
315
|
// Newline-delimited (gateway ws_transport.py splits on "\n"): a frame without the
|
|
269
316
|
// terminator never reaches the gateway's frame handler.
|
|
270
|
-
if (ws.readyState === WebSocket.OPEN)
|
|
317
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
318
|
+
try { ws.send(JSON.stringify(obj) + "\n"); } catch {}
|
|
319
|
+
}
|
|
271
320
|
}
|
|
272
321
|
|
|
273
322
|
const ready = new Promise<void>((resolve, reject) => {
|
|
@@ -290,6 +339,7 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
290
339
|
http.close();
|
|
291
340
|
},
|
|
292
341
|
async injectAgentsChatMessage(msg: AgentsChatMessage) {
|
|
342
|
+
if (!msg || typeof msg !== "object") return;
|
|
293
343
|
// Route to the identity this message is ADDRESSED to, then only to gateway
|
|
294
344
|
// socket(s) fronting THAT identity — never broadcast across identities.
|
|
295
345
|
//
|
|
@@ -302,7 +352,7 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
302
352
|
// unaddressed is dropped — injecting joined-channel chatter into the
|
|
303
353
|
// agent's session would burn its tokens on messages not meant for it.
|
|
304
354
|
// Own server loop ticks bypass those gates only after live-record verification.
|
|
305
|
-
const isDm = typeof msg.channel_id === "string" && msg.channel_id
|
|
355
|
+
const isDm = typeof msg.channel_id === "string" && /^dm[-:]/i.test(msg.channel_id);
|
|
306
356
|
const owner = (msg as any).__botId ?? (msg as any).dm_owner;
|
|
307
357
|
const isTick = (msg.meta as any)?.kind === "loop_tick";
|
|
308
358
|
const tick = serverLoopTick(msg);
|
|
@@ -318,7 +368,11 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
318
368
|
content: msg.content,
|
|
319
369
|
});
|
|
320
370
|
if (!targets.length) {
|
|
321
|
-
|
|
371
|
+
const safeChan = sanitizeConnectorLogField((msg as any).channel_id ?? "?", 128);
|
|
372
|
+
const rawMentions = Array.isArray((msg as any).mentioned_ids)
|
|
373
|
+
? (msg as any).mentioned_ids.slice(0, 16).map((m: unknown) => sanitizeConnectorLogField(m, 64))
|
|
374
|
+
: [];
|
|
375
|
+
log(`[connector] inbound unaddressed (channel=${safeChan} mentions=${JSON.stringify(rawMentions)}) — dropped`);
|
|
322
376
|
return;
|
|
323
377
|
}
|
|
324
378
|
await Promise.all(targets.map(target => {
|
|
@@ -355,11 +409,14 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
355
409
|
try {
|
|
356
410
|
const ctx = await Promise.race([
|
|
357
411
|
hooks.getChannelContext(target.botId, msg.channel_id!, since, msg.id, controller.signal),
|
|
358
|
-
new Promise<null>(resolve => {
|
|
412
|
+
new Promise<null>(resolve => {
|
|
413
|
+
timer = setTimeout(() => { controller.abort(); resolve(null); }, 1000);
|
|
414
|
+
timer.unref?.();
|
|
415
|
+
}),
|
|
359
416
|
]);
|
|
360
417
|
if (ctx && ctx.length) {
|
|
361
418
|
baseEvent.context = ctx.slice(-10).map((c) => ({
|
|
362
|
-
text: String(c?.text ?? "").slice(0, 500),
|
|
419
|
+
text: redactSecrets(String(c?.text ?? "").slice(0, 4096)).slice(0, 500),
|
|
363
420
|
source: { user_name: c?.user_name, user_id: c?.user_id },
|
|
364
421
|
}));
|
|
365
422
|
}
|
|
@@ -370,7 +427,14 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
370
427
|
}
|
|
371
428
|
}
|
|
372
429
|
if (msg.timestamp && Number.isFinite(Date.parse(msg.timestamp)) &&
|
|
373
|
-
(!since || Date.parse(msg.timestamp) > Date.parse(since)))
|
|
430
|
+
(!since || Date.parse(msg.timestamp) > Date.parse(since))) {
|
|
431
|
+
lastAddressed.delete(key);
|
|
432
|
+
lastAddressed.set(key, msg.timestamp);
|
|
433
|
+
if (lastAddressed.size > 2000) {
|
|
434
|
+
const oldestKey = lastAddressed.keys().next().value;
|
|
435
|
+
if (oldestKey !== undefined) lastAddressed.delete(oldestKey);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
374
438
|
}
|
|
375
439
|
if (tick) {
|
|
376
440
|
baseEvent.context = [...(baseEvent.context ?? []), {
|
|
@@ -409,13 +473,16 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
409
473
|
for (const conn of sockets) {
|
|
410
474
|
for (const botId of conn.fronted) {
|
|
411
475
|
const id = table.forBot(botId);
|
|
412
|
-
if (!id || id.gatewayId !== conn.gatewayId || id.secret
|
|
476
|
+
if (!id || id.gatewayId !== conn.gatewayId || !safeSecretEqual(id.secret, conn.secret)) conn.fronted.delete(botId);
|
|
413
477
|
}
|
|
414
478
|
}
|
|
415
479
|
// Rebuild upgrade-auth secrets in place so the upgrade handler sees them.
|
|
416
480
|
for (const k of Object.keys(config.secrets)) delete config.secrets[k];
|
|
417
481
|
for (const id of next) {
|
|
418
|
-
(config.secrets
|
|
482
|
+
if (!Object.prototype.hasOwnProperty.call(config.secrets, id.gatewayId)) {
|
|
483
|
+
config.secrets[id.gatewayId] = [];
|
|
484
|
+
}
|
|
485
|
+
config.secrets[id.gatewayId].push(id.secret);
|
|
419
486
|
}
|
|
420
487
|
log(`[connector] identities reloaded: ${table.size}`);
|
|
421
488
|
},
|
|
@@ -424,11 +491,13 @@ export function startConnector(config: ConnectorConfig): ConnectorHandle {
|
|
|
424
491
|
|
|
425
492
|
/** Peek the payload head of an upgrade token without verifying (to index secrets). */
|
|
426
493
|
function peekPayload(token: string): string | null {
|
|
494
|
+
if (typeof token !== "string" || token.length === 0 || token.length > 4096) return null;
|
|
427
495
|
try {
|
|
428
496
|
const decoded = Buffer.from(token, "base64url").toString("utf8");
|
|
429
497
|
const parts = decoded.split(":");
|
|
430
498
|
if (parts.length < 3) return null;
|
|
431
|
-
|
|
499
|
+
const payload = parts.slice(0, -2).join(":");
|
|
500
|
+
return ["__proto__", "constructor", "prototype"].includes(payload.toLowerCase()) ? null : payload;
|
|
432
501
|
} catch {
|
|
433
502
|
return null;
|
|
434
503
|
}
|