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
package/connector/run.ts CHANGED
@@ -32,19 +32,29 @@
32
32
  */
33
33
 
34
34
  import WS from "ws";
35
- import { readFileSync } from "node:fs";
35
+ import { lstatSync, readFileSync, realpathSync } from "node:fs";
36
+ import { dirname, isAbsolute, resolve } from "node:path";
37
+ import { fileURLToPath } from "node:url";
36
38
  import { startConnector, PlatformHttpError, PlatformNetworkError } from "./server.ts";
37
- import { requireIdentity, type Identity } from "./identities.ts";
38
- import { join } from "node:path";
39
+ import { requireIdentity, validIdentityId, type Identity } from "./identities.ts";
39
40
  import { redactSecrets } from "../src/redact.ts";
40
41
  import { flushCursor, loadCursor, persistCursor } from "../src/read-cursor.ts";
41
42
  import { normalizeTimestampForCursor } from "../src/timestamps.ts";
42
43
  import { MessageDedup } from "../src/dedup.ts";
43
- import { planBackfill } from "./backfill.ts";
44
+ import { canonicalCursorTimestamp, planBackfill, safeCursorBotFile, selectChannelContextMessages } from "./backfill.ts";
44
45
  import { ingestAgentsChatFrame } from "./ingest.ts";
45
46
  import { HeartbeatMonitor, WS_CLOSED } from "../src/heartbeat.ts";
46
-
47
- const log = (m: string) => process.stderr.write(`[agentschat-connector] ${m}\n`);
47
+ import { resolveDrainReconnectDelay, withTimeoutController } from "../src/reconnect.ts";
48
+
49
+ const CONNECTOR_CTRL_RE = /[\x00-\x1f\x7f-\x9f\u2028\u2029]/;
50
+ const CONNECTOR_LOG_STRIP_RE = /[\x00-\x1f\x7f-\x9f\u2028\u2029]+/g;
51
+ const GATEWAY_PROTO_KEYS = new Set(["__proto__", "constructor", "prototype"]);
52
+ const CONNECTOR_PROTO_KEYS = new Set([
53
+ "prototype",
54
+ ...Object.getOwnPropertyNames(Object.prototype).map((k) => k.toLowerCase()),
55
+ ]);
56
+ const log = (m: string) =>
57
+ process.stderr.write(`[agentschat-connector] ${redactSecrets(m).replace(CONNECTOR_LOG_STRIP_RE, " ")}\n`);
48
58
 
49
59
  function need(name: string): string {
50
60
  const v = process.env[name];
@@ -55,29 +65,165 @@ function need(name: string): string {
55
65
  return v;
56
66
  }
57
67
 
58
- const API = (process.env.AGENTCHAT_API_URL || "https://agents-chat.com").replace(/\/$/, "");
59
- const WS_URL = process.env.AGENTCHAT_WS_URL || API.replace(/^http/, "ws") + "/ws";
60
- const PORT = Number(process.env.RELAY_PORT || 8765);
61
- const HOST = process.env.RELAY_HOST || "127.0.0.1";
68
+ /** URL has already canonicalized IPv4 shorthand and IPv6 spellings. */
69
+ function isUnsafeConnectorHost(hostname: string): boolean {
70
+ const host = hostname.toLowerCase().replace(/\.$/, "").replace(/^\[|\]$/g, "");
71
+ if (
72
+ ["metadata", "instance-data", "metadata.google.internal", "metadata.goog"].includes(host) ||
73
+ host.endsWith(".metadata.google.internal") ||
74
+ host.endsWith(".metadata.goog")
75
+ ) {
76
+ return true;
77
+ }
78
+ const unsafeV4 = (ip: string) =>
79
+ ip === "0.0.0.0" || ip.startsWith("0.") || ip.startsWith("169.254.") || ip === "100.100.100.200";
80
+ if (!host.includes(":")) return unsafeV4(host);
81
+ const halves = host.split("::");
82
+ const left = halves[0] ? halves[0].split(":") : [];
83
+ const right = halves[1] ? halves[1].split(":") : [];
84
+ const words = [...left, ...Array(Math.max(0, 8 - left.length - right.length)).fill("0"), ...right].map(v => parseInt(v, 16));
85
+ if (words.every(v => v === 0) || (words[0] & 0xffc0) === 0xfe80 ||
86
+ host === "fd00:ec2::254") return true;
87
+ if (
88
+ (words.slice(0, 5).every(v => v === 0) && words[5] === 0xffff) ||
89
+ (words.slice(0, 4).every(v => v === 0) && words[4] === 0xffff && words[5] === 0)
90
+ ) {
91
+ return unsafeV4(`${words[6] >>> 8}.${words[6] & 255}.${words[7] >>> 8}.${words[7] & 255}`);
92
+ }
93
+ return false;
94
+ }
95
+
96
+ /** Reject symlink traversal, except verified Darwin system aliases in ancestors. */
97
+ export function assertSafeConnectorAncestors(target: string, directoryLeaf = false): void {
98
+ // Preserve dot segments until every traversed component has been checked.
99
+ const absolute = (isAbsolute(target) ? target : `${process.cwd()}/${target}`).replace(/\/+$/, "") || "/";
100
+ let dir = directoryLeaf ? absolute : dirname(absolute);
101
+ let leaf = directoryLeaf;
102
+ for (;;) {
103
+ const stat = lstatSync(dir);
104
+ let systemAlias = false;
105
+ if (!leaf && process.platform === "darwin" && ["/tmp", "/var", "/etc"].includes(dir) && stat.isSymbolicLink()) {
106
+ const privateRoot = lstatSync("/private");
107
+ const privateDir = lstatSync(`/private${dir}`);
108
+ systemAlias = realpathSync(dir) === `/private${dir}` &&
109
+ !privateRoot.isSymbolicLink() && privateRoot.isDirectory() &&
110
+ !privateDir.isSymbolicLink() && privateDir.isDirectory();
111
+ }
112
+ if (!systemAlias && (stat.isSymbolicLink() || !stat.isDirectory())) throw new Error("unsafe connector directory");
113
+ const parent = dirname(dir);
114
+ if (parent === dir) return;
115
+ dir = parent;
116
+ leaf = false;
117
+ }
118
+ }
119
+
120
+ /** Recheck the cursor directory on every read/write; failures preserve dirty state. */
121
+ export function loadConnectorCursor(file: string, warn: (message: string) => void): Map<string, string> {
122
+ try { assertSafeConnectorAncestors(dirname(file), true); }
123
+ catch { warn("[connector] unsafe cursor directory; cursor not loaded"); return new Map(); }
124
+ return loadCursor(file, warn);
125
+ }
126
+ export function persistConnectorCursor(file: string, map: Map<string, string>, warn: (message: string) => void): boolean {
127
+ try { assertSafeConnectorAncestors(dirname(file), true); }
128
+ catch { warn("[connector] unsafe cursor directory; cursor not persisted"); return false; }
129
+ return persistCursor(file, map, warn);
130
+ }
131
+
132
+ /** Validate startup addresses before opening listeners or sending credentials. */
133
+ export function readConnectorConfig(env: NodeJS.ProcessEnv = process.env) {
134
+ const API = (env.AGENTCHAT_API_URL || "https://agents-chat.com").replace(/\/$/, "");
135
+ const WS_URL = env.AGENTCHAT_WS_URL || API.replace(/^http/, "ws") + "/ws";
136
+ const PORT = Number(env.RELAY_PORT || 8765);
137
+ if (!Number.isInteger(PORT) || PORT < 0 || PORT > 65535) throw new Error("invalid RELAY_PORT");
138
+ for (const [name, raw, protocols] of [
139
+ ["AGENTCHAT_API_URL", API, ["http:", "https:"]],
140
+ ["AGENTCHAT_WS_URL", WS_URL, ["ws:", "wss:"]],
141
+ ] as const) {
142
+ if (CONNECTOR_CTRL_RE.test(raw)) throw new Error(`invalid ${name}`);
143
+ let url: URL;
144
+ try { url = new URL(raw); } catch { throw new Error(`invalid ${name}`); }
145
+ const loopback = ["127.0.0.1", "localhost", "[::1]"].includes(url.hostname);
146
+ if (!(protocols as readonly string[]).includes(url.protocol) || url.username || url.password || url.port === "0" || isUnsafeConnectorHost(url.hostname) ||
147
+ (name === "AGENTCHAT_API_URL" && (url.search || url.hash || raw.includes("?") || raw.includes("#"))) ||
148
+ ((url.protocol === "http:" || url.protocol === "ws:") && !loopback && env.AGENTSCHAT_ALLOW_INSECURE_HTTP !== "1")) {
149
+ throw new Error(`invalid ${name}`);
150
+ }
151
+ }
152
+ return { API, WS_URL, PORT, HOST: env.RELAY_HOST || "127.0.0.1" };
153
+ }
154
+
155
+ /** Count actual response bytes, including chunked bodies and dishonest headers. */
156
+ export async function readBoundedJson(response: Response): Promise<unknown> {
157
+ const limit = 4 * 1024 * 1024;
158
+ const reader = response.body?.getReader();
159
+ if (!reader) throw new Error("invalid JSON response");
160
+ const decoder = new TextDecoder("utf-8", { fatal: true });
161
+ let bytes = 0;
162
+ let text = "";
163
+ try {
164
+ for (;;) {
165
+ const { done, value } = await reader.read();
166
+ if (done) break;
167
+ bytes += value.byteLength;
168
+ if (bytes > limit) throw new Error("JSON response too large");
169
+ text += decoder.decode(value, { stream: true });
170
+ }
171
+ text += decoder.decode();
172
+ const parsed = JSON.parse(text);
173
+ if (parsed && typeof parsed === "object" && Object.prototype.hasOwnProperty.call(parsed, "__proto__")) {
174
+ throw new Error("invalid JSON response");
175
+ }
176
+ return parsed;
177
+ } catch (error) {
178
+ void reader.cancel().catch(() => {});
179
+ throw error;
180
+ } finally {
181
+ reader.releaseLock();
182
+ }
183
+ }
62
184
 
63
185
  // ── Resolve identities: multiplex (RELAY_IDENTITIES[_FILE]) or single-tenant (legacy env) ──
64
186
 
65
- function parseIdentitiesJson(raw: string, source: string): Identity[] {
187
+ export function parseIdentitiesJson(raw: string, source: string): Identity[] {
66
188
  let parsed: any;
67
189
  try {
68
190
  parsed = JSON.parse(raw);
69
191
  } catch {
70
192
  throw new Error(`${source} is not valid JSON`);
71
193
  }
72
- if (!Array.isArray(parsed) || parsed.length === 0) {
73
- throw new Error(`${source} must be a non-empty JSON array`);
194
+ if (!Array.isArray(parsed) || parsed.length === 0 || parsed.length > 256) {
195
+ throw new Error(`${source} must be a JSON array of 1..256 identities`);
74
196
  }
75
197
  const seen = new Set<string>();
76
198
  for (const [index, it] of parsed.entries()) {
199
+ if (!it || typeof it !== "object" || Object.getPrototypeOf(it) !== Object.prototype ||
200
+ Object.prototype.hasOwnProperty.call(it, "__proto__")) {
201
+ throw new Error(`${source} entry ${index}: invalid identity object`);
202
+ }
77
203
  const invalid = ["botId", "token", "gatewayId", "secret"].filter(k => typeof it?.[k] !== "string" || !it[k].trim());
78
204
  for (const k of ["agentId", "profile"]) {
79
205
  if (it?.[k] !== undefined && (typeof it[k] !== "string" || !it[k].trim())) invalid.push(k);
80
206
  }
207
+ for (const k of ["botId", "agentId", "token", "gatewayId", "secret", "profile"]) {
208
+ if (typeof it[k] === "string" && CONNECTOR_CTRL_RE.test(it[k])) invalid.push(k);
209
+ }
210
+ if (typeof it.botId === "string" && !validIdentityId(it.botId)) invalid.push("botId");
211
+ if (it.agentId !== undefined && (typeof it.agentId !== "string" || !validIdentityId(it.agentId))) invalid.push("agentId");
212
+ if (typeof it.gatewayId === "string" && (it.gatewayId.length > 256 || GATEWAY_PROTO_KEYS.has(it.gatewayId.trim().toLowerCase()))) {
213
+ invalid.push("gatewayId");
214
+ }
215
+ if (
216
+ it.profile !== undefined &&
217
+ (typeof it.profile !== "string" ||
218
+ it.profile.trim().length > 128 ||
219
+ it.profile.trim() === "." ||
220
+ it.profile.includes("..") ||
221
+ /[/\\]/.test(it.profile.trim()) ||
222
+ /(?:%2f|%5c|%00|%2e%2e)/i.test(it.profile.trim()) ||
223
+ CONNECTOR_PROTO_KEYS.has(it.profile.trim().toLowerCase()))
224
+ ) {
225
+ invalid.push("profile");
226
+ }
81
227
  if (invalid.length) throw new Error(`${source} entry ${index}: missing or invalid string fields: ${invalid.join(", ")}`);
82
228
  if (seen.has(it.botId)) throw new Error(`${source} entry ${index}: duplicate botId`);
83
229
  seen.add(it.botId);
@@ -93,11 +239,18 @@ function parseIdentitiesJson(raw: string, source: string): Identity[] {
93
239
  }
94
240
 
95
241
  /** Load identities; throws on bad JSON/shape (SIGHUP keeps prior set). */
96
- function loadIdentities(): Identity[] {
242
+ export function loadIdentities(): Identity[] {
97
243
  const file = (process.env.RELAY_IDENTITIES_FILE || "").trim();
98
244
  if (file) {
99
245
  let raw: string;
100
- try { raw = readFileSync(file, "utf8"); }
246
+ try {
247
+ assertSafeConnectorAncestors(file);
248
+ const stat = lstatSync(file);
249
+ if (stat.isSymbolicLink() || !stat.isFile() || stat.size > 1_048_576) {
250
+ throw new Error("Unsafe identities file");
251
+ }
252
+ raw = readFileSync(file, "utf8");
253
+ }
101
254
  catch { throw new Error("RELAY_IDENTITIES_FILE could not be read"); }
102
255
  return parseIdentitiesJson(raw, "RELAY_IDENTITIES_FILE");
103
256
  }
@@ -122,6 +275,19 @@ function resolveIdentitiesAtStartup(): Identity[] {
122
275
  }
123
276
  }
124
277
 
278
+ /** Bound timers to Node's signed 32-bit range after applying the caller's minimum. */
279
+ export function clampTimerMs(raw: unknown, fallback: number, minMs: number): number {
280
+ const value = Number(raw);
281
+ return Math.min(2_147_483_647, Math.max(minMs, Math.floor(Number.isFinite(value) && value > 0 ? value : fallback)));
282
+ }
283
+
284
+ /** Both the source CLI and the bundled dist/connector.js run directly. */
285
+ export function isConnectorEntrypoint(entry: string | undefined, moduleUrl: string, isMain = false): boolean {
286
+ return isMain || Boolean(entry && resolve(entry) === fileURLToPath(moduleUrl));
287
+ }
288
+
289
+ async function main() {
290
+ const { API, WS_URL, PORT, HOST } = readConnectorConfig();
125
291
  /** Mutable identity list — hooks and reload close over this binding. */
126
292
  let identities = resolveIdentitiesAtStartup();
127
293
 
@@ -152,6 +318,7 @@ function scheduleReconnect(id: Identity, delay: number) {
152
318
  reconnectTimerByBot.delete(id.botId);
153
319
  if (currentIdentity(id)) connectIdentity(id);
154
320
  }, delay);
321
+ (timer as any).unref?.();
155
322
  reconnectTimerByBot.set(id.botId, timer);
156
323
  }
157
324
  const backoffByBot = new Map<string, number>();
@@ -160,9 +327,10 @@ const heartbeatsByBot = new Map<string, HeartbeatMonitor>();
160
327
  const dedup = new MessageDedup();
161
328
 
162
329
  // Same defaults as stdio MCP (src/server.ts): 15s ping / 45s pong / 30s connect.
163
- const HB_PING_MS = Math.max(5_000, Number(process.env.AGENTSCHAT_CONNECTOR_PING_MS || 15_000));
164
- const HB_PONG_MS = Math.max(HB_PING_MS + 5_000, Number(process.env.AGENTSCHAT_CONNECTOR_PONG_TIMEOUT_MS || 45_000));
165
- const HB_CONNECT_MS = Math.max(5_000, Number(process.env.AGENTSCHAT_CONNECTOR_CONNECT_TIMEOUT_MS || 30_000));
330
+ const HB_PING_MS = clampTimerMs(process.env.AGENTSCHAT_CONNECTOR_PING_MS, 15_000, 5_000);
331
+ const HB_PONG_MS = clampTimerMs(process.env.AGENTSCHAT_CONNECTOR_PONG_TIMEOUT_MS, 45_000, Math.min(2_147_483_647, HB_PING_MS + 5_000));
332
+ const HB_CONNECT_MS = clampTimerMs(process.env.AGENTSCHAT_CONNECTOR_CONNECT_TIMEOUT_MS, 30_000, 5_000);
333
+ const HTTP_TIMEOUT_MS = clampTimerMs(process.env.AGENTSCHAT_CONNECTOR_HTTP_TIMEOUT_MS, 10_000, 2_000);
166
334
 
167
335
  function stopHeartbeat(botId: string) {
168
336
  const hb = heartbeatsByBot.get(botId);
@@ -175,7 +343,9 @@ function stopHeartbeat(botId: string) {
175
343
  // stdio MCP (`last-seen-msg-ts-<agent>.json`): reconnect REST-replays the
176
344
  // gap after this watermark; empty cursor seeds from newest and does not replay.
177
345
  const CURSOR_DIR = process.env.AGENTCHAT_CURSOR_DIR || process.cwd();
178
- const cursorFlushMs = Math.max(500, Number(process.env.AGENTSCHAT_MCP_CURSOR_FLUSH_MS || 5000));
346
+ try { assertSafeConnectorAncestors(CURSOR_DIR, true); }
347
+ catch { throw new Error("unsafe connector cursor directory"); }
348
+ const cursorFlushMs = clampTimerMs(process.env.AGENTSCHAT_MCP_CURSOR_FLUSH_MS, 5_000, 500);
179
349
 
180
350
  type CursorStore = {
181
351
  file: string;
@@ -185,18 +355,18 @@ type CursorStore = {
185
355
  };
186
356
  const cursors = new Map<string, CursorStore>();
187
357
 
188
- function cursorFor(id: Identity): CursorStore {
358
+ function cursorFor(id: Pick<Identity, "botId">): CursorStore {
189
359
  let s = cursors.get(id.botId);
190
360
  if (s) return s;
191
- const file = join(CURSOR_DIR, `last-seen-msg-ts-${id.botId}.json`);
192
- s = { file, map: loadCursor(file, (m) => log(m.trimEnd())), state: { dirty: false }, timer: null };
361
+ const file = safeCursorBotFile(CURSOR_DIR, id.botId);
362
+ s = { file, map: loadConnectorCursor(file, (m) => log(m.trimEnd())), state: { dirty: false }, timer: null };
193
363
  cursors.set(id.botId, s);
194
364
  return s;
195
365
  }
196
366
 
197
367
  function flushOne(s: CursorStore) {
198
368
  if (s.timer) { clearTimeout(s.timer); s.timer = null; }
199
- flushCursor(s.state, () => persistCursor(s.file, s.map, (m) => log(m.trimEnd())));
369
+ flushCursor(s.state, () => persistConnectorCursor(s.file, s.map, (m) => log(m.trimEnd())));
200
370
  }
201
371
 
202
372
  function scheduleSave(s: CursorStore) {
@@ -206,12 +376,13 @@ function scheduleSave(s: CursorStore) {
206
376
  (s.timer as any).unref?.();
207
377
  }
208
378
 
209
- function advanceCursor(id: Identity, channelId: string | undefined, timestamp: string | undefined) {
210
- if (typeof channelId !== "string" || typeof timestamp !== "string" || !timestamp) return;
379
+ function advanceCursor(id: Pick<Identity, "botId">, channelId: string | undefined, timestamp: string | undefined) {
380
+ if (!validIdentityId(channelId) || typeof timestamp !== "string" || !timestamp) return;
381
+ const currentTs = canonicalCursorTimestamp(timestamp);
382
+ if (!currentTs) return;
211
383
  const s = cursorFor(id);
212
384
  const prev = s.map.get(channelId) || "";
213
- const currentTs = normalizeTimestampForCursor(timestamp, "after") || timestamp;
214
- const prevTs = normalizeTimestampForCursor(prev, "after") || prev;
385
+ const prevTs = canonicalCursorTimestamp(prev) ?? "";
215
386
  if (currentTs > prevTs) {
216
387
  s.map.set(channelId, timestamp);
217
388
  scheduleSave(s);
@@ -219,9 +390,13 @@ function advanceCursor(id: Identity, channelId: string | undefined, timestamp: s
219
390
  }
220
391
 
221
392
  function joinChannel(ws: WS, id: Identity, channelId: string, name?: string) {
393
+ if (!validIdentityId(channelId)) return;
222
394
  try {
223
395
  ws.send(JSON.stringify({ type: "join_channel", channel_id: channelId, agent_id: id.agentId }));
224
- log(`joined channel ${channelId}${name ? ` (${name})` : ""}`);
396
+ const safeName = typeof name === "string"
397
+ ? redactSecrets(name).replace(CONNECTOR_LOG_STRIP_RE, " ").trim().slice(0, 128)
398
+ : "";
399
+ log(`joined channel ${channelId}${safeName ? ` (${safeName})` : ""}`);
225
400
  } catch (e: any) {
226
401
  log(`join ${channelId} failed: ${e?.message ?? e}`);
227
402
  }
@@ -232,18 +407,23 @@ function joinChannel(ws: WS, id: Identity, channelId: string, name?: string) {
232
407
  // Hermes never sees inbound messages despite REST membership.
233
408
  async function joinMemberships(ws: WS, id: Identity) {
234
409
  try {
235
- const r = await fetch(`${API}/api/channels/mine`, { headers: { Authorization: `Bearer ${id.token}` } });
410
+ const r = await fetch(`${API}/api/channels/mine`, {
411
+ redirect: "error",
412
+ headers: { Authorization: `Bearer ${id.token}` },
413
+ signal: AbortSignal.timeout(HTTP_TIMEOUT_MS),
414
+ });
236
415
  if (!r.ok) {
416
+ await r.body?.cancel().catch(() => {});
237
417
  log(`list mine failed: ${r.status}`);
238
418
  return;
239
419
  }
240
- const body = await r.json() as any;
420
+ const body = await readBoundedJson(r) as any;
241
421
  if (!currentIdentity(id) || socketsByBot.get(id.botId) !== ws) return;
242
422
  const channels = Array.isArray(body) ? body : (body.channels || []);
243
423
  const joined: string[] = [];
244
424
  for (const ch of channels) {
245
425
  const channelId = ch?.id || ch?.channel_id;
246
- if (!channelId) continue;
426
+ if (!validIdentityId(channelId)) continue;
247
427
  joinChannel(ws, id, channelId, ch?.name);
248
428
  joined.push(channelId);
249
429
  }
@@ -266,15 +446,21 @@ async function joinMemberships(ws: WS, id: Identity) {
266
446
  async function backfillIdentity(id: Identity, channelIds: string[]) {
267
447
  const s = cursorFor(id);
268
448
  for (const channelId of channelIds) {
449
+ if (!validIdentityId(channelId)) continue;
269
450
  if (!currentIdentity(id)) return;
270
451
  try {
271
452
  const after = s.map.get(channelId);
272
453
  const params = after ? `?after=${encodeURIComponent(after)}&limit=50` : `?limit=1`;
273
454
  const r = await fetch(`${API}/api/channels/${encodeURIComponent(channelId)}/messages${params}`, {
455
+ redirect: "error",
274
456
  headers: { Authorization: `Bearer ${id.token}` },
457
+ signal: AbortSignal.timeout(HTTP_TIMEOUT_MS),
275
458
  });
276
- if (!r.ok) continue;
277
- const msgs = (((await r.json()) as any)?.messages ?? []) as any[];
459
+ if (!r.ok) {
460
+ await r.body?.cancel().catch(() => {});
461
+ continue;
462
+ }
463
+ const msgs = (((await readBoundedJson(r)) as any)?.messages ?? []) as any[];
278
464
  if (!currentIdentity(id)) return;
279
465
  const plan = planBackfill(after, msgs, id.agentId);
280
466
  if (plan.seed) {
@@ -312,7 +498,8 @@ function connectIdentity(id: Identity) {
312
498
  }
313
499
 
314
500
  let heartbeatForced = false;
315
- const ws = new WS(WS_URL);
501
+ const MAX_PLATFORM_WS_BYTES = 4 * 1024 * 1024;
502
+ const ws = new WS(WS_URL, { maxPayload: MAX_PLATFORM_WS_BYTES });
316
503
  socketsByBot.set(id.botId, ws);
317
504
 
318
505
  const hb = new HeartbeatMonitor(
@@ -348,31 +535,60 @@ function connectIdentity(id: Identity) {
348
535
 
349
536
  ws.on("open", () => {
350
537
  if (!currentIdentity(id) || socketsByBot.get(id.botId) !== ws) { ws.close(); return; }
351
- backoffByBot.set(id.botId, 1000);
352
538
  try {
353
539
  ws.send(JSON.stringify({ type: "auth", agent_id: id.agentId, token: id.token, capabilities: ["chat"] }));
354
540
  } catch {}
355
541
  });
356
542
  ws.on("message", (raw: any) => {
357
543
  if (!currentIdentity(id) || socketsByBot.get(id.botId) !== ws) return;
544
+ const rawBytes = Array.isArray(raw)
545
+ ? raw.reduce((n: number, chunk: any) => n + (chunk?.byteLength ?? Buffer.byteLength(String(chunk), "utf8")), 0)
546
+ : typeof raw === "string"
547
+ ? Buffer.byteLength(raw, "utf8")
548
+ : (raw?.byteLength ?? 0);
549
+ if (rawBytes > MAX_PLATFORM_WS_BYTES) return;
358
550
  let data: any;
359
551
  try { data = JSON.parse(String(raw)); } catch { return; }
552
+ if (!data || typeof data !== "object" || Array.isArray(data) || Object.prototype.hasOwnProperty.call(data, "__proto__")) return;
360
553
  if (data.type === "pong") {
361
554
  hb.receivedPong();
362
555
  return;
363
556
  }
364
557
  if (data.type === "auth_ok") {
365
558
  hb.receivedPong(); // alive signal, same as stdio MCP
559
+ backoffByBot.set(id.botId, 1000);
366
560
  log(`connected to agentschat as ${id.agentId}`);
367
561
  void joinMemberships(ws, id);
368
562
  return;
369
563
  }
370
- if (data.type === "channel_created" && data.channel_id) {
564
+ if (data.type === "auth_error") {
565
+ const reason = redactSecrets(String(data.error ?? data.reason ?? "rejected")).slice(0, 200);
566
+ log(`agentschat WS auth_error for ${id.botId}: ${reason}; backing off 30000ms`);
567
+ backoffByBot.set(id.botId, 30_000);
568
+ try { ws.close(); } catch {}
569
+ return;
570
+ }
571
+ if (data.type === "channel_created" && validIdentityId(data.channel_id)) {
371
572
  hb.receivedPong();
372
573
  joinChannel(ws, id, data.channel_id, data.name);
373
574
  return;
374
575
  }
375
- if (data.type === "message") {
576
+ if (data.type === "please_reconnect" || data.type === "shard_moved") {
577
+ if (heartbeatForced) return;
578
+ heartbeatForced = true;
579
+ const delay = resolveDrainReconnectDelay(data);
580
+ log(`agentschat WS ${data.type} for ${id.botId}; reconnecting after jitter ${delay}ms`);
581
+ backoffByBot.set(id.botId, 1000);
582
+ try {
583
+ ws.removeAllListeners("close");
584
+ ws.close();
585
+ } catch {}
586
+ socketsByBot.delete(id.botId);
587
+ stopHeartbeat(id.botId);
588
+ scheduleReconnect(id, delay);
589
+ return;
590
+ }
591
+ if (data.type === "message" || data.type === "thread_reply") {
376
592
  hb.receivedPong(); // any live traffic counts as liveness
377
593
  if (!data.__source) data.__source = "live";
378
594
  // Cursor advances even when shared multiplex dedup skips broadcast.
@@ -405,30 +621,44 @@ const connector = startConnector({
405
621
  async sendMessage(botId, chatId, content, replyTo) {
406
622
  const id = requireIdentity(identities, botId);
407
623
  const res = await fetch(`${API}/api/channels/${encodeURIComponent(chatId)}/messages`, {
624
+ redirect: "error",
408
625
  method: "POST",
409
626
  headers: { "Content-Type": "application/json", Authorization: `Bearer ${id.token}` },
410
627
  body: JSON.stringify({ sender_id: id.agentId, content_type: "text", content, ...(replyTo ? { parent_id: replyTo } : {}) }),
628
+ signal: AbortSignal.timeout(HTTP_TIMEOUT_MS),
411
629
  }).catch(() => { throw new PlatformNetworkError(); });
412
- if (!res.ok) throw new PlatformHttpError(res.status);
413
- const data = (await res.json()) as any;
630
+ if (!res.ok) {
631
+ await res.body?.cancel().catch(() => {});
632
+ throw new PlatformHttpError(res.status);
633
+ }
634
+ const data = (await readBoundedJson(res)) as any;
414
635
  return { id: data?.id };
415
636
  },
416
637
  async getLoops(botId) {
417
638
  const id = requireIdentity(identities, botId);
418
639
  const res = await fetch(`${API}/api/loops/mine`, {
640
+ redirect: "error",
419
641
  headers: { Authorization: `Bearer ${id.token}` }, signal: AbortSignal.timeout(5000),
420
642
  });
421
- if (!res.ok) throw new PlatformHttpError(res.status);
422
- return await res.json();
643
+ if (!res.ok) {
644
+ await res.body?.cancel().catch(() => {});
645
+ throw new PlatformHttpError(res.status);
646
+ }
647
+ return await readBoundedJson(res);
423
648
  },
424
649
  async getChatInfo(botId, chatId) {
425
650
  const id = requireIdentity(identities, botId);
426
651
  const res = await fetch(`${API}/api/channels/${encodeURIComponent(chatId)}`, {
652
+ redirect: "error",
427
653
  headers: { Authorization: `Bearer ${id.token}` },
654
+ signal: AbortSignal.timeout(HTTP_TIMEOUT_MS),
428
655
  });
429
- if (!res.ok) return { name: chatId, type: chatId.startsWith("dm-") ? "dm" : "group" };
430
- const data = (await res.json()) as any;
431
- return { name: data?.name ?? chatId, type: chatId.startsWith("dm-") ? "dm" : "group" };
656
+ if (!res.ok) {
657
+ await res.body?.cancel().catch(() => {});
658
+ return { name: chatId, type: /^dm[-:]/i.test(chatId) ? "dm" : "group" };
659
+ }
660
+ const data = (await readBoundedJson(res)) as any;
661
+ return { name: data?.name ?? chatId, type: /^dm[-:]/i.test(chatId) ? "dm" : "group" };
432
662
  },
433
663
  async sendTyping(botId, chatId) {
434
664
  const id = requireIdentity(identities, botId);
@@ -443,22 +673,29 @@ const connector = startConnector({
443
673
  // failure returns null and the addressed message still delivers without it.
444
674
  async getChannelContext(botId, chatId, sinceTs, excludeId, signal) {
445
675
  const id = requireIdentity(identities, botId);
446
- const res = await fetch(`${API}/api/channels/${encodeURIComponent(chatId)}/messages?limit=50`, {
447
- signal,
448
- headers: { Authorization: `Bearer ${id.token}` },
449
- });
450
- if (!res.ok) return null;
451
- const msgs = (((await res.json()) as any)?.messages ?? [])
452
- .filter((m: any) => m?.content && m.content !== "__typing__" && m?.id !== excludeId)
453
- .sort((a: any, b: any) => String(a.timestamp ?? "").localeCompare(String(b.timestamp ?? "")));
454
- const windowed = sinceTs ? msgs.filter((m: any) => String(m.timestamp ?? "") > sinceTs) : msgs;
455
- const tail = windowed.slice(-10);
456
- if (!tail.length) return null;
457
- return tail.map((m: any) => ({
458
- text: redactSecrets(String(m.content)).slice(0, 500),
459
- user_name: m.sender_name ?? m.sender_id,
460
- user_id: m.sender_id,
461
- }));
676
+ const { signal: boundedSignal, cleanup } = withTimeoutController(signal, HTTP_TIMEOUT_MS);
677
+ let res: Response;
678
+ try {
679
+ res = await fetch(`${API}/api/channels/${encodeURIComponent(chatId)}/messages?limit=50`, {
680
+ redirect: "error",
681
+ signal: boundedSignal,
682
+ headers: { Authorization: `Bearer ${id.token}` },
683
+ });
684
+ if (!res.ok) {
685
+ await res.body?.cancel().catch(() => {});
686
+ return null;
687
+ }
688
+ const rawMsgs = (((await readBoundedJson(res)) as any)?.messages ?? []) as any[];
689
+ const tail = selectChannelContextMessages(rawMsgs, sinceTs, excludeId);
690
+ if (!tail.length) return null;
691
+ return tail.map((m: any) => ({
692
+ text: redactSecrets(String(m.content ?? "").slice(0, 4096)).slice(0, 500),
693
+ user_name: m.sender_name ?? m.sender_id,
694
+ user_id: m.sender_id,
695
+ }));
696
+ } finally {
697
+ cleanup();
698
+ }
462
699
  },
463
700
  },
464
701
  logger: log,
@@ -528,18 +765,22 @@ function reloadIdentitiesFromConfig() {
528
765
  log(`hot-reload complete: ${identities.length} identit${identities.length === 1 ? "y" : "ies"} (${added.length} added, ${removed.length} removed)`);
529
766
  }
530
767
 
531
- process.on("SIGHUP", () => {
532
- try {
533
- reloadIdentitiesFromConfig();
534
- } catch (e: any) {
535
- log(`hot-reload FAILED (keeping previous identities): ${e?.message ?? e}`);
536
- }
537
- });
768
+ if (process.platform !== "win32") {
769
+ process.on("SIGHUP", () => {
770
+ try {
771
+ reloadIdentitiesFromConfig();
772
+ } catch (e: any) {
773
+ log(`hot-reload FAILED (keeping previous identities): ${e?.message ?? e}`);
774
+ }
775
+ });
776
+ }
538
777
 
539
778
  for (const sig of ["SIGINT", "SIGTERM"] as const) {
540
779
  process.on(sig, () => {
541
780
  (process as any).__shutdown = true;
542
781
  for (const botId of reconnectTimerByBot.keys()) cancelReconnect(botId);
782
+ for (const [, t] of backfillTimerByBot) clearTimeout(t);
783
+ backfillTimerByBot.clear();
543
784
  for (const botId of [...heartbeatsByBot.keys()]) stopHeartbeat(botId);
544
785
  for (const ws of socketsByBot.values()) {
545
786
  try { ws.close(); } catch {}
@@ -549,3 +790,9 @@ for (const sig of ["SIGINT", "SIGTERM"] as const) {
549
790
  process.exit(0);
550
791
  });
551
792
  }
793
+
794
+ }
795
+
796
+ if (isConnectorEntrypoint(process.argv[1], import.meta.url, import.meta.main) || process.argv.includes("--connector")) {
797
+ await main();
798
+ }