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
@@ -35,21 +35,83 @@ export interface Identity {
35
35
  profile?: string;
36
36
  }
37
37
 
38
- export class IdentityTable {
39
- private readonly byBot = new Map<string, Identity>();
38
+ const GATEWAY_PROTO_KEYS = new Set(["__proto__", "constructor", "prototype"]);
39
+ const IDENTITY_PROTO_KEYS = new Set([
40
+ "prototype",
41
+ ...Object.getOwnPropertyNames(Object.prototype).map((k) => k.toLowerCase()),
42
+ ]);
43
+ const IDENTITY_CTRL_RE = /[\x00-\x1f\x7f-\x9f\u2028\u2029]/;
44
+
45
+ export function validIdentityId(value: unknown): value is string {
46
+ return typeof value === "string" && value.length > 0 && value.length <= 128 && value !== "." && value !== ".." &&
47
+ !value.includes("..") && !/(?:%2f|%5c|%00|%2e%2e)/i.test(value) &&
48
+ !/[/\\\s\x00-\x1f\x7f-\x9f\u2028\u2029]/.test(value) &&
49
+ !IDENTITY_PROTO_KEYS.has(value.toLowerCase());
50
+ }
40
51
 
41
- constructor(identities: Identity[]) {
42
- for (const id of identities) {
43
- if (this.byBot.has(id.botId)) {
44
- throw new Error(`duplicate identity botId "${id.botId}" — ambiguous routing`);
52
+ function validOptionalProfile(profile: unknown): boolean {
53
+ if (profile === undefined) return true;
54
+ if (typeof profile !== "string" || IDENTITY_CTRL_RE.test(profile)) return false;
55
+ const trimmed = profile.trim();
56
+ return (
57
+ trimmed.length > 0 &&
58
+ trimmed.length <= 128 &&
59
+ trimmed !== "." &&
60
+ !trimmed.includes("..") &&
61
+ !/[/\\]/.test(trimmed) &&
62
+ !/(?:%2f|%5c|%00|%2e%2e)/i.test(trimmed) &&
63
+ !IDENTITY_PROTO_KEYS.has(trimmed.toLowerCase())
64
+ );
65
+ }
66
+
67
+ /** Validate the whole candidate set before exposing any credentials or replacing state. */
68
+ function validateIdentities(identities: readonly Identity[], allowEmptyCredentials = false): void {
69
+ const bots = new Set<string>();
70
+ const agents = new Set<string>();
71
+ for (const id of identities) {
72
+ if (!id || !validIdentityId(id.botId) || !validIdentityId(id.agentId)) {
73
+ throw new Error("invalid identity botId or agentId");
74
+ }
75
+ if ([id.token, id.gatewayId, id.secret].some(value =>
76
+ typeof value !== "string" || (!value.trim() && !(allowEmptyCredentials && value === "")))) {
77
+ throw new Error("missing identity credentials");
78
+ }
79
+ if (!allowEmptyCredentials) {
80
+ if (
81
+ IDENTITY_CTRL_RE.test(id.gatewayId) ||
82
+ id.gatewayId.length > 256 ||
83
+ GATEWAY_PROTO_KEYS.has(id.gatewayId.trim().toLowerCase())
84
+ ) {
85
+ throw new Error("invalid identity gatewayId");
86
+ }
87
+ if (IDENTITY_CTRL_RE.test(id.token) || IDENTITY_CTRL_RE.test(id.secret)) {
88
+ throw new Error("invalid identity credentials");
45
89
  }
46
- this.byBot.set(id.botId, id);
47
90
  }
91
+ if (!validOptionalProfile(id.profile)) {
92
+ throw new Error("invalid identity profile");
93
+ }
94
+ if (bots.has(id.botId.trim()) || agents.has(id.agentId.trim())) {
95
+ throw new Error("duplicate identity botId or agentId — ambiguous routing");
96
+ }
97
+ bots.add(id.botId.trim());
98
+ agents.add(id.agentId.trim());
99
+ }
100
+ }
101
+
102
+ export class IdentityTable {
103
+ private readonly byBot = new Map<string, Identity>();
104
+
105
+ constructor(identities: Identity[], opts?: { allowEmptyCredentials?: boolean }) {
106
+ // Legacy embedding uses an empty-credential placeholder only at construction.
107
+ // Replacements and platform credential lookups always retain strict validation.
108
+ validateIdentities(identities, opts?.allowEmptyCredentials === true);
109
+ this.install(identities);
48
110
  }
49
111
 
50
112
  /** The identity fronting `botId`, or null when unregistered (fail closed). */
51
113
  forBot(botId: string): Identity | null {
52
- return this.byBot.get(botId) ?? null;
114
+ return validIdentityId(botId) ? this.byBot.get(botId) ?? null : null;
53
115
  }
54
116
 
55
117
  isSingle(): boolean {
@@ -69,12 +131,17 @@ export class IdentityTable {
69
131
  * duplicate botIds the same way the constructor does.
70
132
  */
71
133
  replace(identities: Identity[]): void {
134
+ validateIdentities(identities);
135
+ this.install(identities);
136
+ }
137
+
138
+ private install(identities: Identity[]): void {
72
139
  const next = new Map<string, Identity>();
73
140
  for (const id of identities) {
74
141
  if (next.has(id.botId)) {
75
142
  throw new Error(`duplicate identity botId "${id.botId}" — ambiguous routing`);
76
143
  }
77
- next.set(id.botId, id);
144
+ next.set(id.botId, Object.freeze({ ...id }));
78
145
  }
79
146
  this.byBot.clear();
80
147
  for (const [k, v] of next) this.byBot.set(k, v);
@@ -83,7 +150,8 @@ export class IdentityTable {
83
150
 
84
151
  /** Platform hooks must never substitute another identity's credentials. */
85
152
  export function requireIdentity(identities: readonly Identity[], botId: string): Identity {
86
- const id = identities.find(id => id.botId === botId);
153
+ validateIdentities(identities);
154
+ const id = validIdentityId(botId) ? identities.find(id => id.botId === botId) : undefined;
87
155
  if (!id) throw new Error(`unknown identity: ${botId}`);
88
156
  return id;
89
157
  }
@@ -121,7 +189,7 @@ function matchesExactMention(content: string, id: string): boolean {
121
189
 
122
190
  /** All exact addressed targets, once each, in identity-table order. */
123
191
  export function routeInboundTargets(table: IdentityTable, ctx: InboundContext): Identity[] {
124
- if (ctx.channel_id?.startsWith("dm-")) {
192
+ if (/^dm[-:]/i.test(ctx.channel_id ?? "")) {
125
193
  const owner = ctx.dmOwnerBotId ? table.forBot(ctx.dmOwnerBotId) : null;
126
194
  return owner ? [owner] : [];
127
195
  }
@@ -132,7 +200,7 @@ export function routeInboundTargets(table: IdentityTable, ctx: InboundContext):
132
200
 
133
201
  export function routeInbound(table: IdentityTable, ctx: InboundContext): Identity | null {
134
202
  // DM: route to the identity that owns the DM channel.
135
- if (ctx.channel_id?.startsWith("dm-")) {
203
+ if (/^dm[-:]/i.test(ctx.channel_id ?? "")) {
136
204
  return ctx.dmOwnerBotId ? table.forBot(ctx.dmOwnerBotId) : null;
137
205
  }
138
206
  // Group/channel: an explicit mentioned_ids annotation wins when present.
@@ -179,7 +247,7 @@ export function resolveOutbound(table: IdentityTable, botId: string): Identity |
179
247
  */
180
248
  export function hermesSourceProfile(id: Identity, frontedCount: number): string | undefined {
181
249
  const named = typeof id.profile === "string" ? id.profile.trim() : "";
182
- if (named) return named;
183
- if (frontedCount > 1) return id.botId;
250
+ if (named && validOptionalProfile(id.profile)) return named;
251
+ if (frontedCount > 1 && validIdentityId(id.botId)) return id.botId;
184
252
  return undefined;
185
253
  }
@@ -1,4 +1,5 @@
1
1
  import { MessageDedup, messageDedupKey } from "../src/dedup.ts";
2
+ import { canonicalCursorTimestamp } from "./backfill.ts";
2
3
  import { serverLoopTick, type AgentsChatMessage } from "./normalize.ts";
3
4
 
4
5
  export type IngestIdentity = { botId: string; agentId: string };
@@ -24,11 +25,19 @@ export function ingestAgentsChatFrame(
24
25
  dedup: MessageDedup;
25
26
  },
26
27
  ): boolean {
27
- if (frame.content !== "__typing__") {
28
+ if (
29
+ !frame ||
30
+ typeof frame !== "object" ||
31
+ Array.isArray(frame) ||
32
+ Object.prototype.hasOwnProperty.call(frame, "__proto__")
33
+ ) {
34
+ return false;
35
+ }
36
+ if (frame.content !== "__typing__" && canonicalCursorTimestamp(frame.timestamp) !== null) {
28
37
  deps.advanceCursor(id, frame.channel_id, frame.timestamp);
29
38
  }
30
39
  const key = messageDedupKey(frame as { id?: string; channel_id?: string });
31
- const isDm = frame.channel_id?.startsWith("dm-");
40
+ const isDm = typeof frame.channel_id === "string" && /^dm[-:]/i.test(frame.channel_id);
32
41
  const scopedKey = key && (isDm ? JSON.stringify([id.botId, frame.channel_id, frame.id]) : key);
33
42
  if (scopedKey && deps.dedup.recordOrSkip(scopedKey)) return false;
34
43
  // Group fanout must filter self per TARGET, not per arrival socket: A can
@@ -24,6 +24,7 @@ export interface AgentsChatMessage {
24
24
  content?: string;
25
25
  timestamp?: string;
26
26
  reply_to?: string;
27
+ parent_id?: string;
27
28
  mentions?: string[];
28
29
  sender_type?: string;
29
30
  content_type?: string;
@@ -60,26 +61,29 @@ export interface WireEvent {
60
61
  * not become an agent turn (typing placeholder, or no channel to key a session on).
61
62
  */
62
63
  export function toWireEvent(msg: AgentsChatMessage, platform = "agentschat"): WireEvent | null {
63
- const content = msg.content ?? "";
64
+ if (!msg || typeof msg !== "object") return null;
65
+ const content = typeof msg.content === "string" ? msg.content : "";
64
66
  if (content === "__typing__") return null;
65
67
 
66
- const chatId = msg.channel_id ?? "";
68
+ const metadata = (value: string | undefined) =>
69
+ typeof value === "string" ? value.replace(/[\x00-\x1f\x7f-\x9f\u2028\u2029]/g, "") : undefined;
70
+ const chatId = metadata(msg.channel_id) ?? "";
67
71
  if (!chatId) return null;
68
72
 
69
- const isDm = chatId.startsWith("dm-");
73
+ const isDm = /^dm[-:]/i.test(chatId);
70
74
 
71
75
  return {
72
76
  text: content,
73
77
  message_type: "text",
74
- message_id: msg.id,
75
- reply_to_message_id: msg.reply_to,
78
+ message_id: metadata(msg.id),
79
+ reply_to_message_id: metadata(msg.reply_to ?? msg.parent_id),
76
80
  source: {
77
81
  platform,
78
82
  chat_id: chatId,
79
83
  chat_type: isDm ? "dm" : "group",
80
- chat_name: msg.channel_id ?? null,
81
- user_id: msg.sender_id,
82
- user_name: msg.sender_name ?? msg.sender_id,
84
+ chat_name: chatId,
85
+ user_id: metadata(msg.sender_id),
86
+ user_name: metadata(msg.sender_name ?? msg.sender_id),
83
87
  thread_id: null,
84
88
  // scope_id intentionally omitted: agentschat has no guild/scope concept.
85
89
  },
@@ -96,12 +100,25 @@ export interface ServerLoopTick {
96
100
  expires_at: null;
97
101
  }
98
102
 
103
+ const LOOP_TICK_CTRL_RE = /[\x00-\x1f\x7f-\x9f\u2028\u2029]/;
104
+
105
+ function hasOwnProto(value: unknown): boolean {
106
+ return Boolean(
107
+ value &&
108
+ typeof value === "object" &&
109
+ Object.prototype.hasOwnProperty.call(value, "__proto__"),
110
+ );
111
+ }
112
+
99
113
  /** Shape check only. Delivery must additionally verify the bot's live server record. */
100
114
  export function serverLoopTick(msg: AgentsChatMessage): ServerLoopTick | null {
115
+ if (!msg || typeof msg !== "object" || hasOwnProto(msg)) return null;
101
116
  const meta = msg.meta as Partial<ServerLoopTick> | undefined;
102
- if (!meta || meta.kind !== "loop_tick" ||
103
- typeof msg.id !== "string" || !msg.id || typeof msg.channel_id !== "string" || !msg.channel_id ||
104
- typeof msg.sender_id !== "string" || !msg.sender_id || msg.sender_type !== "agent" || msg.content_type !== "text" ||
117
+ if (!meta || typeof meta !== "object" || Array.isArray(meta) || hasOwnProto(meta) || meta.kind !== "loop_tick" ||
118
+ typeof msg.id !== "string" || !msg.id || LOOP_TICK_CTRL_RE.test(msg.id) ||
119
+ typeof msg.channel_id !== "string" || !msg.channel_id || LOOP_TICK_CTRL_RE.test(msg.channel_id) ||
120
+ typeof msg.sender_id !== "string" || !msg.sender_id || LOOP_TICK_CTRL_RE.test(msg.sender_id) ||
121
+ msg.sender_type !== "agent" || msg.content_type !== "text" ||
105
122
  typeof msg.timestamp !== "string" || !Number.isFinite(Date.parse(msg.timestamp)) ||
106
123
  typeof meta.loop_id !== "string" || !/^loop_[a-zA-Z0-9_-]+$/.test(meta.loop_id) ||
107
124
  typeof meta.prompt !== "string" || !meta.prompt.trim() || meta.prompt.length > 4000 ||
@@ -115,9 +132,12 @@ export function serverLoopTick(msg: AgentsChatMessage): ServerLoopTick | null {
115
132
  export function matchesLiveLoop(msg: AgentsChatMessage, agentId: string, response: unknown): boolean {
116
133
  const tick = serverLoopTick(msg);
117
134
  if (!tick || msg.sender_id !== agentId) return false;
135
+ if (!response || typeof response !== "object" || Array.isArray(response) || hasOwnProto(response)) return false;
118
136
  const rows = (response as {loops?: any[]})?.loops;
119
137
  if (!Array.isArray(rows)) return false;
120
- const matches = rows.filter(row => row?.loop_id === tick.loop_id);
138
+ const matches = rows.filter(
139
+ row => row && typeof row === "object" && !Array.isArray(row) && !hasOwnProto(row) && row.loop_id === tick.loop_id,
140
+ );
121
141
  if (matches.length !== 1) return false;
122
142
  const row = matches[0];
123
143
  return row.status === "active" && row.channel_id === msg.channel_id && row.prompt === tick.prompt &&