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.
Files changed (84) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +8 -0
  3. package/codex/README.md +1 -1
  4. package/codex/app-server.ts +79 -22
  5. package/codex/bots-config.ts +31 -11
  6. package/codex/bridge.ts +123 -25
  7. package/codex/config.ts +128 -21
  8. package/codex/gui-channel.ts +66 -9
  9. package/codex/inbox-sync.ts +65 -12
  10. package/codex/loop-grants.ts +41 -11
  11. package/codex/manager.ts +101 -25
  12. package/codex/owner.ts +81 -9
  13. package/codex/processes.ts +51 -7
  14. package/codex/run.ts +70 -17
  15. package/codex/runtime-home.ts +41 -11
  16. package/codex/thread-history.ts +77 -30
  17. package/codex/transport.ts +122 -19
  18. package/connector/auth.ts +57 -5
  19. package/connector/backfill.ts +92 -10
  20. package/connector/descriptor.ts +27 -1
  21. package/connector/identities.ts +82 -14
  22. package/connector/ingest.ts +11 -2
  23. package/connector/normalize.ts +32 -12
  24. package/connector/run.ts +314 -67
  25. package/connector/server.ts +95 -26
  26. package/dist/codex-bots.js +410 -73
  27. package/dist/codex-bridge.js +1996 -576
  28. package/dist/connector.js +1286 -493
  29. package/dist/dsh.js +1776 -299
  30. package/dist/muse.js +967 -221
  31. package/dist/server.js +7645 -1172
  32. package/dsh/client.js +7 -5
  33. package/dsh/identity-ui/client.js +7 -5
  34. package/dsh/identity-ui/index.js +43 -8
  35. package/dsh/inbound-bridge/index.js +595 -146
  36. package/dsh/src/cli.ts +430 -91
  37. package/dsh/src/patch.ts +295 -15
  38. package/dsh/tests/dsh-hardening-127.test.ts +207 -0
  39. package/dsh/tests/dsh.test.ts +990 -3
  40. package/muse/scripts/ensure-muse-wakes.mjs +5 -3
  41. package/muse/scripts/muse-wake-receiver.mjs +4 -1
  42. package/muse/src/bridge.ts +54 -13
  43. package/muse/src/cli.ts +226 -41
  44. package/muse/src/client.ts +249 -34
  45. package/muse/src/config.ts +128 -12
  46. package/muse/src/policy.ts +28 -8
  47. package/muse/src/types.ts +1 -0
  48. package/muse/src/typing.ts +18 -1
  49. package/muse/src/wake.ts +237 -35
  50. package/muse/tests/bridge.test.mjs +131 -0
  51. package/muse/tests/client.test.mjs +327 -0
  52. package/muse/tests/config.test.mjs +78 -1
  53. package/muse/tests/hardening.test.mjs +199 -0
  54. package/muse/tests/hardening126.test.mjs +292 -0
  55. package/muse/tests/policy.test.mjs +83 -1
  56. package/muse/tests/wake.test.mjs +134 -6
  57. package/package.json +3 -3
  58. package/scripts/ensure-grok-wakes.mjs +93 -8
  59. package/scripts/example-url-wake-receiver.mjs +254 -61
  60. package/scripts/grok-bind-register.sh +38 -8
  61. package/skills/agentschat-team-lead/SKILL.md +1 -1
  62. package/skills/onboarding.md +14 -10
  63. package/src/argcheck.ts +1890 -1
  64. package/src/cli.mjs +98 -14
  65. package/src/dedup.ts +57 -8
  66. package/src/grok-bind.ts +160 -33
  67. package/src/heartbeat.ts +39 -11
  68. package/src/identity-guard.ts +93 -20
  69. package/src/identity.ts +67 -11
  70. package/src/mentions.ts +26 -5
  71. package/src/onboarding-status.ts +134 -11
  72. package/src/profile-selection.ts +269 -18
  73. package/src/profile-store.ts +222 -28
  74. package/src/read-cursor.ts +256 -9
  75. package/src/reconnect.ts +98 -2
  76. package/src/redact.ts +34 -15
  77. package/src/rest-cli.ts +713 -159
  78. package/src/server.ts +2487 -342
  79. package/src/session-bind.ts +731 -132
  80. package/src/session-route.ts +183 -12
  81. package/src/team-lead-skill.ts +3 -0
  82. package/src/timestamps.ts +56 -23
  83. package/src/tool-annotations.ts +22 -1
  84. package/src/wake.ts +524 -90
@@ -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) value = value.split(secret).join("[redacted]");
130
+ if (secret) scrubbed = scrubbed.split(secret).join("[redacted]");
110
131
  }
111
- return JSON.stringify(value.slice(0, 128));
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
- const { pathname } = new URL(req.url ?? "/", "http://localhost");
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 ? config.secrets[payload] : undefined;
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
- for (const line of text.split("\n")) {
170
- if (!line.trim()) continue;
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 !== conn.secret) ? "credential_mismatch" : null;
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 !== conn.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
- log(`[connector] outbound failed: no usable identity for botId=${requested ?? "?"}`);
246
- return { success: false, error: `no usable identity for outbound (botId=${requested ?? "?"})` };
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 r = await hooks.sendMessage(identity.botId, chatId, action.content ?? "", action.reply_to);
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
- log(`[connector] outbound failed: unsupported op ${String(op)}`);
263
- return { success: false, error: `unsupported op: ${String(op)}` };
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) ws.send(JSON.stringify(obj) + "\n");
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.startsWith("dm-");
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
- log(`[connector] inbound unaddressed (channel=${(msg as any).channel_id ?? "?"} mentions=${JSON.stringify((msg as any).mentioned_ids ?? [])}) — dropped`);
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 => { timer = setTimeout(() => { controller.abort(); resolve(null); }, 1000); }),
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))) lastAddressed.set(key, msg.timestamp);
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 !== conn.secret) conn.fronted.delete(botId);
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[id.gatewayId] ??= []).push(id.secret);
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
- return parts.slice(0, -2).join(":");
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
  }