typeclaw 0.45.0 → 0.46.0

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 (147) hide show
  1. package/README.md +1 -1
  2. package/package.json +3 -2
  3. package/src/agent/git-nudge.ts +2 -1
  4. package/src/agent/index.ts +67 -25
  5. package/src/agent/live-subagents.ts +18 -0
  6. package/src/agent/loop-guard.ts +208 -10
  7. package/src/agent/multimodal/index.ts +1 -1
  8. package/src/agent/multimodal/look-at.ts +78 -44
  9. package/src/agent/multimodal/looker.ts +151 -39
  10. package/src/agent/network/response-body.ts +26 -0
  11. package/src/agent/network/safe-http.ts +198 -0
  12. package/src/agent/plugin-tools.ts +438 -162
  13. package/src/agent/provider-error.ts +62 -4
  14. package/src/agent/restart/index.ts +61 -40
  15. package/src/agent/restart-handoff/index.ts +73 -4
  16. package/src/agent/retry-same-ref.ts +66 -0
  17. package/src/agent/reviewer-bash-policy.ts +42 -1
  18. package/src/agent/session-origin.ts +2 -0
  19. package/src/agent/subagent-drain.ts +117 -7
  20. package/src/agent/subagents.ts +77 -18
  21. package/src/agent/throttle-circuit.ts +25 -5
  22. package/src/agent/tool-error-remediation.ts +72 -0
  23. package/src/agent/tool-file-safety.ts +1050 -0
  24. package/src/agent/tools/channel-fetch-attachment.ts +10 -3
  25. package/src/agent/tools/channel-reply.ts +15 -1
  26. package/src/agent/tools/channel-send.ts +17 -1
  27. package/src/agent/tools/ddg.ts +16 -2
  28. package/src/agent/tools/post-github-review.ts +199 -0
  29. package/src/agent/tools/spawn-subagent.ts +21 -18
  30. package/src/agent/tools/webfetch/fetch.ts +143 -183
  31. package/src/agent/tools/webfetch/tool.ts +2 -3
  32. package/src/agent/tools/websearch.ts +23 -2
  33. package/src/agent/tools-without-local-file-operands.ts +27 -0
  34. package/src/agent/turn-runner.ts +109 -15
  35. package/src/bundled-plugins/backup/index.ts +2 -1
  36. package/src/bundled-plugins/backup/runner.ts +2 -1
  37. package/src/bundled-plugins/doc-render/render.ts +93 -12
  38. package/src/bundled-plugins/doc-render/skills/typeclaw-render-pdf/SKILL.md +13 -13
  39. package/src/bundled-plugins/github-cli-auth/effective-approval.ts +16 -6
  40. package/src/bundled-plugins/github-cli-auth/gh-command.ts +555 -15
  41. package/src/bundled-plugins/github-cli-auth/index.ts +131 -135
  42. package/src/bundled-plugins/github-cli-auth/review-checkout.ts +138 -0
  43. package/src/bundled-plugins/guard/policies/uncommitted-changes.ts +2 -1
  44. package/src/bundled-plugins/memory/README.md +23 -11
  45. package/src/bundled-plugins/memory/append-tool.ts +15 -8
  46. package/src/bundled-plugins/memory/citations.ts +22 -9
  47. package/src/bundled-plugins/memory/dreaming.ts +13 -8
  48. package/src/bundled-plugins/memory/find-entry-tool.ts +4 -0
  49. package/src/bundled-plugins/memory/index.ts +0 -4
  50. package/src/bundled-plugins/memory/load-memory.ts +8 -3
  51. package/src/bundled-plugins/memory/provenance-index.ts +717 -0
  52. package/src/bundled-plugins/memory/provenance-sanitize.ts +40 -0
  53. package/src/bundled-plugins/memory/references/store-reference-tool.ts +4 -0
  54. package/src/bundled-plugins/memory/search-tool.ts +163 -60
  55. package/src/bundled-plugins/memory/stream-events.ts +2 -0
  56. package/src/bundled-plugins/memory/vector/hybrid.ts +126 -13
  57. package/src/bundled-plugins/researcher/write-report.ts +6 -3
  58. package/src/bundled-plugins/reviewer/index.ts +2 -2
  59. package/src/bundled-plugins/reviewer/reviewer.ts +10 -3
  60. package/src/bundled-plugins/reviewer/skills/code-review.ts +24 -9
  61. package/src/bundled-plugins/scout/scout.ts +8 -0
  62. package/src/bundled-plugins/security/index.ts +4 -4
  63. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +1 -0
  64. package/src/bundled-plugins/security/policies/private-surface-read.ts +220 -29
  65. package/src/bundled-plugins/security/policies/secret-exfil-read.ts +1 -0
  66. package/src/bundled-plugins/security/policies/ssrf.ts +29 -9
  67. package/src/channels/adapters/discord-bot-channel-resolver.ts +26 -14
  68. package/src/channels/adapters/discord-bot-thread-room.ts +44 -12
  69. package/src/channels/adapters/discord-bot.ts +91 -10
  70. package/src/channels/adapters/discord-channel-resolver.ts +91 -23
  71. package/src/channels/adapters/discord-id.ts +4 -0
  72. package/src/channels/adapters/discord-resolver-cache.ts +41 -0
  73. package/src/channels/adapters/discord.ts +57 -7
  74. package/src/channels/adapters/github/auth-app.ts +41 -12
  75. package/src/channels/adapters/github/index.ts +23 -11
  76. package/src/channels/adapters/github/review-submitter.ts +302 -0
  77. package/src/channels/adapters/kakaotalk-channel-resolver.ts +12 -1
  78. package/src/channels/adapters/kakaotalk-fetch-attachment.ts +8 -4
  79. package/src/channels/adapters/kakaotalk-typing.ts +127 -0
  80. package/src/channels/adapters/kakaotalk.ts +19 -0
  81. package/src/channels/adapters/slack-attachment-download.ts +71 -0
  82. package/src/channels/adapters/slack-bot.ts +19 -8
  83. package/src/channels/adapters/slack-classify.ts +21 -2
  84. package/src/channels/adapters/slack.ts +59 -7
  85. package/src/channels/adapters/telegram-bot.ts +18 -5
  86. package/src/channels/adapters/webex-bot.ts +8 -3
  87. package/src/channels/adapters/webex.ts +8 -3
  88. package/src/channels/fetch-attachment.ts +43 -0
  89. package/src/{bundled-plugins/github-cli-auth/approve-idempotency.ts → channels/github-review-verdict-coordinator.ts} +25 -1
  90. package/src/channels/github-token-bridge.ts +10 -3
  91. package/src/channels/manager.ts +9 -0
  92. package/src/channels/router.ts +568 -12
  93. package/src/channels/types.ts +28 -1
  94. package/src/cli/channel.ts +1 -1
  95. package/src/cli/init.ts +1 -1
  96. package/src/cli/model.ts +57 -7
  97. package/src/compose/stop.ts +17 -5
  98. package/src/config/config.ts +6 -6
  99. package/src/config/models-mutation.ts +13 -1
  100. package/src/config/providers.ts +176 -0
  101. package/src/container/start.ts +49 -10
  102. package/src/doctor/checks.ts +3 -1
  103. package/src/doctor/commit.ts +2 -1
  104. package/src/doctor/file-ownership.ts +52 -0
  105. package/src/dreams/git.ts +2 -1
  106. package/src/git/hookless.ts +5 -0
  107. package/src/git/reconcile-ignored.ts +18 -5
  108. package/src/git/secret-history.ts +371 -0
  109. package/src/git/system-commit.ts +12 -6
  110. package/src/init/dockerfile.ts +86 -5
  111. package/src/init/index.ts +17 -5
  112. package/src/init/models-dev.ts +4 -0
  113. package/src/init/validate-api-key.ts +2 -0
  114. package/src/mcp/tools.ts +16 -3
  115. package/src/permissions/builtins.ts +3 -1
  116. package/src/plugin/context.ts +2 -0
  117. package/src/plugin/index.ts +1 -0
  118. package/src/plugin/manager.ts +4 -0
  119. package/src/plugin/types.ts +21 -0
  120. package/src/run/index.ts +40 -0
  121. package/src/run/llm-fetch-observer.ts +188 -13
  122. package/src/sandbox/build.ts +37 -15
  123. package/src/sandbox/canonical-secrets.ts +18 -0
  124. package/src/sandbox/env-exposure.ts +80 -0
  125. package/src/sandbox/errors.ts +13 -6
  126. package/src/sandbox/hidden-paths.ts +102 -44
  127. package/src/sandbox/index.ts +22 -4
  128. package/src/sandbox/package-install.ts +3 -27
  129. package/src/sandbox/policy.ts +11 -25
  130. package/src/sandbox/privileged-runtime.ts +255 -0
  131. package/src/sandbox/session-tmp.ts +8 -2
  132. package/src/sandbox/writable-zones.ts +204 -140
  133. package/src/secrets/index.ts +8 -0
  134. package/src/secrets/refresh-provider-oauth.ts +128 -0
  135. package/src/server/command-runner.ts +48 -4
  136. package/src/server/index.ts +4 -0
  137. package/src/skills/typeclaw-channel-github/SKILL.md +49 -56
  138. package/src/skills/typeclaw-claude-code/SKILL.md +24 -60
  139. package/src/skills/typeclaw-claude-code/references/auth-flow.md +17 -158
  140. package/src/skills/typeclaw-codex-cli/SKILL.md +19 -49
  141. package/src/skills/typeclaw-codex-cli/references/auth-flow.md +17 -154
  142. package/src/skills/typeclaw-config/SKILL.md +5 -5
  143. package/src/skills/typeclaw-config/references/recommended-mounts.md +24 -24
  144. package/src/skills/typeclaw-github-contributing/SKILL.md +23 -26
  145. package/src/skills/typeclaw-memory/SKILL.md +9 -3
  146. package/src/skills/typeclaw-permissions/SKILL.md +19 -15
  147. package/typeclaw.schema.json +20 -0
@@ -1,6 +1,6 @@
1
1
  import { readFile } from 'node:fs/promises'
2
2
 
3
- import { DiscordBotClient, DiscordBotListener } from 'agent-messenger/discordbot'
3
+ import { DiscordBotClient, DiscordBotListener, type DiscordBotListenerOptions } from 'agent-messenger/discordbot'
4
4
  import {
5
5
  DiscordIntent,
6
6
  type DiscordFile,
@@ -10,6 +10,15 @@ import {
10
10
  type DiscordGatewayStickerItem,
11
11
  } from 'agent-messenger/discordbot'
12
12
 
13
+ import {
14
+ enrichHistoricalProvenance,
15
+ type HistoricalProvenanceResolver,
16
+ } from '@/bundled-plugins/memory/provenance-index'
17
+ import {
18
+ DEFAULT_ATTACHMENT_MAX_BYTES,
19
+ readAttachmentErrorSnippet,
20
+ readAttachmentResponse,
21
+ } from '@/channels/fetch-attachment'
13
22
  import {
14
23
  MEMBERSHIP_ENUMERATION_CAP,
15
24
  type MembershipResolver,
@@ -113,6 +122,7 @@ const consoleLogger: DiscordBotAdapterLogger = {
113
122
  }
114
123
 
115
124
  export type DiscordBotAdapterOptions = {
125
+ agentDir?: string
116
126
  router: ChannelRouter
117
127
  configRef: () => ChannelAdapterConfig
118
128
  token: string
@@ -121,6 +131,9 @@ export type DiscordBotAdapterOptions = {
121
131
  // exact REST calls without monkey-patching globalThis.fetch. Production
122
132
  // callers leave it undefined to use the global fetch.
123
133
  fetchImpl?: typeof fetch
134
+ createClient?: () => DiscordBotClient
135
+ createListener?: (client: DiscordBotClient, options: DiscordBotListenerOptions) => DiscordBotListener
136
+ enrichHistoricalProvenance?: typeof enrichHistoricalProvenance
124
137
  }
125
138
 
126
139
  export type DiscordBotAdapter = {
@@ -893,7 +906,7 @@ export function createFetchAttachmentCallback(deps: {
893
906
  }): FetchAttachmentCallback {
894
907
  const { token, logger } = deps
895
908
  const fetchImpl = deps.fetchImpl ?? fetch
896
- return async ({ ref, filename }) => {
909
+ return async ({ ref, filename, maxBytes = DEFAULT_ATTACHMENT_MAX_BYTES }) => {
897
910
  let url: URL
898
911
  try {
899
912
  url = new URL(ref)
@@ -906,13 +919,12 @@ export function createFetchAttachmentCallback(deps: {
906
919
  try {
907
920
  const res = await fetchImpl(url.toString(), { headers: { Authorization: `Bot ${token}` } })
908
921
  if (!res.ok) {
909
- const body = await res.text().catch(() => '')
922
+ const body = await readAttachmentErrorSnippet(res)
910
923
  const message = `discord cdn fetch ${res.status} ${res.statusText}${body ? `: ${body.slice(0, 200)}` : ''}`
911
924
  logger.error(`[discord-bot] fetchAttachment failed for ${url.toString()}: ${message}`)
912
925
  return { ok: false, error: message }
913
926
  }
914
- const arrayBuffer = await res.arrayBuffer()
915
- const buffer = Buffer.from(arrayBuffer)
927
+ const buffer = await readAttachmentResponse(res, maxBytes)
916
928
  const inferredFilename = filename ?? url.pathname.split('/').pop() ?? 'attachment'
917
929
  const contentType = res.headers.get('content-type') ?? undefined
918
930
  logger.info(
@@ -1018,15 +1030,19 @@ export const DISCORD_SLASH_COMMAND_NAMES = SLASH_COMMAND_NAMES
1018
1030
 
1019
1031
  export function createDiscordBotAdapter(options: DiscordBotAdapterOptions): DiscordBotAdapter {
1020
1032
  const logger = options.logger ?? consoleLogger
1021
- const client = new DiscordBotClient()
1033
+ const createClient = options.createClient ?? (() => new DiscordBotClient())
1034
+ const createListener =
1035
+ options.createListener ?? ((client, listenerOptions) => new DiscordBotListener(client, listenerOptions))
1036
+ const client = createClient()
1022
1037
  const fetchImpl = options.fetchImpl ?? fetch
1023
1038
  let listener: DiscordBotListener | null = null
1024
1039
  let botUserId: string | null = null
1040
+ let connected = false
1025
1041
  let started = false
1026
1042
  let inflightInbounds = 0
1027
1043
  let stopWaiters: Array<() => void> = []
1028
1044
 
1029
- const channelResolver = createDiscordChannelResolver({ token: options.token })
1045
+ const channelResolver = createDiscordChannelResolver({ token: options.token, fetchImpl })
1030
1046
  const threadRoomResolver = createDiscordThreadRoomResolver({ token: options.token, fetchImpl })
1031
1047
 
1032
1048
  // Discord mentions by snowflake id (`<@id>`/`<@!id>`), so no username form.
@@ -1142,7 +1158,16 @@ export function createDiscordBotAdapter(options: DiscordBotAdapterOptions): Disc
1142
1158
  // stays null), so the engagement gate cannot see "this is a thread" from
1143
1159
  // the payload alone. Resolve the channel's type/parent and stamp the
1144
1160
  // structural `room` signal for guild inbounds; DMs are never thread rooms.
1145
- const room = event.guild_id !== undefined ? await threadRoomResolver(event.channel_id) : undefined
1161
+ let room = event.guild_id !== undefined ? await threadRoomResolver(event.channel_id) : undefined
1162
+ if (room?.parentChat !== undefined && event.guild_id !== undefined) {
1163
+ const parentNames = await channelResolver({
1164
+ adapter: 'discord-bot',
1165
+ workspace: event.guild_id,
1166
+ chat: room.parentChat,
1167
+ thread: null,
1168
+ })
1169
+ if (parentNames.chatName !== undefined) room = { ...room, parentChatName: parentNames.chatName }
1170
+ }
1146
1171
  const basePayload =
1147
1172
  referenceResult.referenceContext === undefined
1148
1173
  ? { ...verdict.payload, text: hintedText }
@@ -1178,8 +1203,9 @@ export function createDiscordBotAdapter(options: DiscordBotAdapterOptions): Disc
1178
1203
  throw err
1179
1204
  }
1180
1205
 
1181
- listener = new DiscordBotListener(client, { intents: DISCORD_BOT_INTENTS })
1206
+ listener = createListener(client, { intents: DISCORD_BOT_INTENTS })
1182
1207
  listener.on('connected', (info) => {
1208
+ connected = true
1183
1209
  botUserId = info.user.id
1184
1210
  logger.info(`[discord-bot] connected as ${info.user.username} (${info.user.id})`)
1185
1211
  // For bots, the gateway's user.id IS the application id — the same
@@ -1211,9 +1237,15 @@ export function createDiscordBotAdapter(options: DiscordBotAdapterOptions): Disc
1211
1237
  })
1212
1238
  })
1213
1239
  listener.on('disconnected', () => {
1240
+ connected = false
1214
1241
  logger.warn('[discord-bot] disconnected; SDK will reconnect with backoff')
1215
1242
  })
1216
1243
  listener.on('error', (err) => {
1244
+ // agent-messenger 2.30.0 emits only `error` (not `disconnected`)
1245
+ // when a terminal Gateway close stops the listener. Match that
1246
+ // distinct SDK error contract without treating transient transport
1247
+ // errors as disconnects.
1248
+ if (isTerminalGatewayClose(err)) connected = false
1217
1249
  logger.error(`[discord-bot] gateway error: ${describe(err)}`)
1218
1250
  })
1219
1251
  listener.on('message_create', (event) => {
@@ -1259,10 +1291,52 @@ export function createDiscordBotAdapter(options: DiscordBotAdapterOptions): Disc
1259
1291
  options.router.unregisterMembership('discord-bot', membershipResolver)
1260
1292
  listener = null
1261
1293
  botUserId = null
1294
+ connected = false
1262
1295
  started = false
1263
1296
  logger.error(`[discord-bot] listener start failed: ${describe(err)}`)
1264
1297
  throw err
1265
1298
  }
1299
+
1300
+ if (options.agentDir !== undefined) {
1301
+ const runEnrichment = options.enrichHistoricalProvenance ?? enrichHistoricalProvenance
1302
+ const resolveHistorical: HistoricalProvenanceResolver = async (where) => {
1303
+ const key = {
1304
+ adapter: 'discord-bot' as const,
1305
+ workspace: where.workspace,
1306
+ chat: where.chat,
1307
+ thread: where.thread ?? null,
1308
+ }
1309
+ const [names, roomStatus] = await Promise.all([
1310
+ channelResolver(key),
1311
+ threadRoomResolver.resolveStatus(where.chat),
1312
+ ])
1313
+ const room = roomStatus.room
1314
+ let parentChatName = room?.parentChatName
1315
+ if (parentChatName === undefined && room?.parentChat !== undefined) {
1316
+ const parentNames = await channelResolver({ ...key, chat: room.parentChat })
1317
+ parentChatName = parentNames.chatName
1318
+ }
1319
+ return {
1320
+ where: {
1321
+ ...where,
1322
+ ...names,
1323
+ ...(room?.parentChat !== undefined ? { parentChat: room.parentChat } : {}),
1324
+ ...(parentChatName !== undefined ? { parentChatName } : {}),
1325
+ },
1326
+ parentChecked: roomStatus.parentChecked,
1327
+ }
1328
+ }
1329
+ void runEnrichment(options.agentDir, resolveHistorical, { adapter: 'discord-bot' }).then(
1330
+ (result) => {
1331
+ logger.info(
1332
+ `[discord-bot] historical provenance enrichment scanned=${result.scanned} attempted=${result.attempted} resolved=${result.resolved} failed=${result.failed} timed_out=${result.timedOut} changed=${String(result.changed)}`,
1333
+ )
1334
+ },
1335
+ (error: unknown) => {
1336
+ logger.warn(`[discord-bot] historical provenance enrichment failed: ${describe(error)}`)
1337
+ },
1338
+ )
1339
+ }
1266
1340
  },
1267
1341
 
1268
1342
  async stop(): Promise<void> {
@@ -1288,14 +1362,21 @@ export function createDiscordBotAdapter(options: DiscordBotAdapterOptions): Disc
1288
1362
  }
1289
1363
  listener?.stop()
1290
1364
  listener = null
1365
+ connected = false
1291
1366
  },
1292
1367
 
1293
1368
  isConnected(): boolean {
1294
- return botUserId !== null
1369
+ return started && connected
1295
1370
  },
1296
1371
  }
1297
1372
  }
1298
1373
 
1374
+ const TERMINAL_GATEWAY_CLOSE_PATTERN = /^Discord gateway closed with non-recoverable code \d+$/
1375
+
1376
+ function isTerminalGatewayClose(err: unknown): boolean {
1377
+ return err instanceof Error && TERMINAL_GATEWAY_CLOSE_PATTERN.test(err.message)
1378
+ }
1379
+
1299
1380
  function describe(err: unknown): string {
1300
1381
  return err instanceof Error ? err.message : String(err)
1301
1382
  }
@@ -1,44 +1,112 @@
1
1
  import type { DiscordClient } from 'agent-messenger/discord'
2
2
 
3
- import type { ChannelKey, ChannelNameResolver, ResolvedChannelNames } from '@/channels/types'
3
+ import type { ChannelKey, ChannelNameResolver, InboundMessage, ResolvedChannelNames } from '@/channels/types'
4
+
5
+ import { isDiscordSnowflake } from './discord-id'
6
+ import { DiscordResolverCache } from './discord-resolver-cache'
4
7
 
5
8
  const DEFAULT_TTL_MS = 24 * 60 * 60 * 1000
9
+ const DISCORD_THREAD_TYPES = new Set([10, 11, 12])
6
10
 
7
11
  export type DiscordChannelResolverOptions = {
8
- client: Pick<DiscordClient, 'getChannel'>
12
+ client: Pick<DiscordClient, 'getChannel' | 'getServer'>
9
13
  now?: () => number
10
14
  ttlMs?: number
15
+ maxCacheEntries?: number
16
+ }
17
+
18
+ export type DiscordChannelResolver = ChannelNameResolver & {
19
+ resolveRoom(chatId: string): Promise<InboundMessage['room']>
20
+ resolveRoomStatus(chatId: string): Promise<{ room: InboundMessage['room']; parentChecked: boolean }>
11
21
  }
12
22
 
13
- type CacheEntry<T> = { value: T; expiresAt: number }
23
+ type DiscordChannelInfo = Awaited<ReturnType<DiscordClient['getChannel']>>
14
24
 
15
- export function createDiscordChannelResolver(options: DiscordChannelResolverOptions): ChannelNameResolver {
25
+ export function createDiscordChannelResolver(options: DiscordChannelResolverOptions): DiscordChannelResolver {
16
26
  const now = options.now ?? Date.now
17
27
  const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS
18
- const chatCache = new Map<string, CacheEntry<string>>()
28
+ const channelCache = new DiscordResolverCache<DiscordChannelInfo>(options.maxCacheEntries)
29
+ const guildCache = new DiscordResolverCache<string>(options.maxCacheEntries)
30
+
31
+ const fetchChannel = async (channelId: string): Promise<DiscordChannelInfo | null> =>
32
+ await fetchCached(channelCache, channelId, async () => {
33
+ if (!isDiscordSnowflake(channelId)) return null
34
+ try {
35
+ const channel = await options.client.getChannel(channelId)
36
+ return validChannelMetadata(channel) ? channel : null
37
+ } catch (error) {
38
+ void error
39
+ return null
40
+ }
41
+ })
42
+
43
+ const resolve = (async (key: ChannelKey): Promise<ResolvedChannelNames> => {
44
+ if (key.workspace === '@dm') return {}
45
+ const [channel, workspaceName] = await Promise.all([
46
+ fetchChannel(key.chat),
47
+ key.workspace === '' || !isDiscordSnowflake(key.workspace)
48
+ ? Promise.resolve(null)
49
+ : fetchCached(guildCache, key.workspace, async () => {
50
+ try {
51
+ const guild = await options.client.getServer(key.workspace)
52
+ return guild.name || null
53
+ } catch (error) {
54
+ void error
55
+ return null
56
+ }
57
+ }),
58
+ ])
59
+ return {
60
+ ...(channel?.name ? { chatName: channel.name } : {}),
61
+ ...(workspaceName !== null ? { workspaceName } : {}),
62
+ }
63
+ }) as DiscordChannelResolver
19
64
 
20
- const fetchCached = async <T>(
21
- cache: Map<string, CacheEntry<T>>,
65
+ resolve.resolveRoomStatus = async (
66
+ chatId: string,
67
+ ): Promise<{ room: InboundMessage['room']; parentChecked: boolean }> => {
68
+ if (!isDiscordSnowflake(chatId)) return { room: { kind: 'thread' }, parentChecked: false }
69
+ const channel = await fetchChannel(chatId)
70
+ if (channel === null) return { room: { kind: 'thread' }, parentChecked: false }
71
+ if (!DISCORD_THREAD_TYPES.has(channel.type)) return { room: undefined, parentChecked: true }
72
+ if (channel.parent_id == null || !isDiscordSnowflake(channel.parent_id)) {
73
+ channelCache.delete(chatId)
74
+ return { room: { kind: 'thread' }, parentChecked: false }
75
+ }
76
+ const parent = await fetchChannel(channel.parent_id)
77
+ return {
78
+ room: {
79
+ kind: 'thread',
80
+ parentChat: channel.parent_id,
81
+ ...(parent?.name ? { parentChatName: parent.name } : {}),
82
+ },
83
+ parentChecked: true,
84
+ }
85
+ }
86
+ resolve.resolveRoom = async (chatId: string) => (await resolve.resolveRoomStatus(chatId)).room
87
+
88
+ return resolve
89
+
90
+ async function fetchCached<T>(
91
+ cache: DiscordResolverCache<T>,
22
92
  key: string,
23
93
  fetcher: () => Promise<T | null>,
24
- ): Promise<T | null> => {
25
- const cached = cache.get(key)
26
- if (cached && cached.expiresAt > now()) return cached.value
94
+ ): Promise<T | null> {
95
+ const currentTime = now()
96
+ const cached = cache.get(key, currentTime)
97
+ if (cached !== undefined) return cached
27
98
  const value = await fetcher()
28
- if (value !== null) cache.set(key, { value, expiresAt: now() + ttlMs })
99
+ if (value !== null) {
100
+ const fetchedAt = now()
101
+ cache.set(key, value, fetchedAt + ttlMs, fetchedAt)
102
+ }
29
103
  return value
30
104
  }
105
+ }
31
106
 
32
- return async (key: ChannelKey): Promise<ResolvedChannelNames> => {
33
- if (key.workspace === '@dm') return {}
34
- const chatName = await fetchCached(chatCache, key.chat, async () => {
35
- try {
36
- const channel = await options.client.getChannel(key.chat)
37
- return channel.name || null
38
- } catch {
39
- return null
40
- }
41
- })
42
- return chatName !== null ? { chatName } : {}
43
- }
107
+ function validChannelMetadata(channel: DiscordChannelInfo): boolean {
108
+ if (!Number.isInteger(channel.type) || channel.type < 0) return false
109
+ if (channel.parent_id != null && !isDiscordSnowflake(channel.parent_id)) return false
110
+ if (channel.guild_id !== undefined && !isDiscordSnowflake(channel.guild_id)) return false
111
+ return true
44
112
  }
@@ -0,0 +1,4 @@
1
+ export function isDiscordSnowflake(value: string): boolean {
2
+ if (!/^[1-9]\d{0,19}$/.test(value)) return false
3
+ return BigInt(value) <= 18_446_744_073_709_551_615n
4
+ }
@@ -0,0 +1,41 @@
1
+ export const DEFAULT_DISCORD_RESOLVER_CACHE_ENTRIES = 1_000
2
+
3
+ type CacheEntry<T> = { value: T; expiresAt: number }
4
+
5
+ export class DiscordResolverCache<T> {
6
+ readonly #entries = new Map<string, CacheEntry<T>>()
7
+ readonly #maxEntries: number
8
+
9
+ constructor(maxEntries = DEFAULT_DISCORD_RESOLVER_CACHE_ENTRIES) {
10
+ this.#maxEntries = Math.max(1, maxEntries)
11
+ }
12
+
13
+ get(key: string, now: number): T | undefined {
14
+ const entry = this.#entries.get(key)
15
+ if (entry === undefined) return undefined
16
+ if (entry.expiresAt <= now) {
17
+ this.#entries.delete(key)
18
+ return undefined
19
+ }
20
+ this.#entries.delete(key)
21
+ this.#entries.set(key, entry)
22
+ return entry.value
23
+ }
24
+
25
+ set(key: string, value: T, expiresAt: number, now: number): void {
26
+ for (const [candidate, entry] of this.#entries) {
27
+ if (entry.expiresAt <= now) this.#entries.delete(candidate)
28
+ }
29
+ this.#entries.delete(key)
30
+ while (this.#entries.size >= this.#maxEntries) {
31
+ const oldest = this.#entries.keys().next().value
32
+ if (oldest === undefined) break
33
+ this.#entries.delete(oldest)
34
+ }
35
+ this.#entries.set(key, { value, expiresAt })
36
+ }
37
+
38
+ delete(key: string): void {
39
+ this.#entries.delete(key)
40
+ }
41
+ }
@@ -5,6 +5,15 @@ import {
5
5
  type DiscordMessage,
6
6
  } from 'agent-messenger/discord'
7
7
 
8
+ import {
9
+ enrichHistoricalProvenance,
10
+ type HistoricalProvenanceResolver,
11
+ } from '@/bundled-plugins/memory/provenance-index'
12
+ import {
13
+ DEFAULT_ATTACHMENT_MAX_BYTES,
14
+ readAttachmentErrorSnippet,
15
+ readAttachmentResponse,
16
+ } from '@/channels/fetch-attachment'
8
17
  import type { MembershipResolver, MembershipResolverResult } from '@/channels/membership'
9
18
  import { deriveMembershipFromHistory } from '@/channels/membership-from-history'
10
19
  import type { ChannelRouter } from '@/channels/router'
@@ -48,6 +57,7 @@ export type DiscordCredentialStore = {
48
57
  }
49
58
 
50
59
  export type DiscordAdapterOptions = {
60
+ agentDir?: string
51
61
  router: ChannelRouter
52
62
  configRef: () => ChannelAdapterConfig
53
63
  logger?: DiscordAdapterLogger
@@ -56,6 +66,7 @@ export type DiscordAdapterOptions = {
56
66
  createClient?: () => DiscordClient
57
67
  createListener?: (client: DiscordClient) => DiscordListener
58
68
  fetchImpl?: typeof fetch
69
+ enrichHistoricalProvenance?: typeof enrichHistoricalProvenance
59
70
  }
60
71
 
61
72
  export type DiscordAdapter = {
@@ -143,7 +154,7 @@ export function createDiscordFetchAttachmentCallback(deps: {
143
154
  logger: DiscordAdapterLogger
144
155
  }): FetchAttachmentCallback {
145
156
  const fetchFn = deps.fetchImpl ?? fetch
146
- return async ({ ref, filename }) => {
157
+ return async ({ ref, filename, maxBytes = DEFAULT_ATTACHMENT_MAX_BYTES }) => {
147
158
  let url: URL
148
159
  try {
149
160
  url = new URL(ref)
@@ -158,12 +169,12 @@ export function createDiscordFetchAttachmentCallback(deps: {
158
169
  const headers = token !== null ? { Authorization: token } : undefined
159
170
  const res = await fetchFn(url.toString(), headers !== undefined ? { headers } : undefined)
160
171
  if (!res.ok) {
161
- const body = await res.text().catch(() => '')
172
+ const body = await readAttachmentErrorSnippet(res)
162
173
  const message = `discord cdn fetch ${res.status} ${res.statusText}${body ? `: ${body.slice(0, 200)}` : ''}`
163
174
  deps.logger.error(`[discord] fetchAttachment failed for ${url.toString()}: ${message}`)
164
175
  return { ok: false, error: message }
165
176
  }
166
- const buffer = Buffer.from(await res.arrayBuffer())
177
+ const buffer = await readAttachmentResponse(res, maxBytes)
167
178
  const inferredFilename = filename ?? url.pathname.split('/').pop() ?? 'attachment'
168
179
  const contentType = res.headers.get('content-type') ?? undefined
169
180
  return {
@@ -221,14 +232,15 @@ export function createDiscordAdapter(options: DiscordAdapterOptions): DiscordAda
221
232
  const handleMessage = async (event: DiscordGatewayMessageCreateEvent): Promise<void> => {
222
233
  inflightInbounds++
223
234
  try {
224
- const tag = await formatChannelTag(event.channel_id)
225
- logger.info(
226
- `[discord] inbound id=${event.id} author=${event.author.id || '(none)'} ${tag} text_len=${event.content.length}`,
227
- )
228
235
  const verdict = classifyInbound(event, options.configRef(), {
229
236
  selfUserId,
230
237
  selfAliases: options.selfAliasesRef?.() ?? [],
231
238
  })
239
+ const tag =
240
+ event.guild_id === undefined ? `channel=${event.channel_id}` : await formatChannelTag(event.channel_id)
241
+ logger.info(
242
+ `[discord] inbound id=${event.id} author=${event.author.id || '(none)'} ${tag} text_len=${event.content.length}`,
243
+ )
232
244
  if (verdict.kind === 'drop') {
233
245
  logger.info(`[discord] dropped id=${event.id} reason=${verdict.reason}${dropHint(verdict.reason)}`)
234
246
  return
@@ -240,9 +252,11 @@ export function createDiscordAdapter(options: DiscordAdapterOptions): DiscordAda
240
252
  filename: file.filename,
241
253
  mimetype: file.content_type,
242
254
  }))
255
+ const room = verdict.payload.isDm ? undefined : await channelResolver.resolveRoom(verdict.payload.chat)
243
256
  const payload = {
244
257
  ...verdict.payload,
245
258
  authorName: await authorResolver.resolve(verdict.payload.authorId),
259
+ ...(room !== undefined ? { room } : {}),
246
260
  ...(attachments.length > 0 ? { attachments } : {}),
247
261
  }
248
262
  logger.info(`[discord] routed id=${event.id} ${tag} mention=${payload.isBotMention}`)
@@ -336,6 +350,42 @@ export function createDiscordAdapter(options: DiscordAdapterOptions): DiscordAda
336
350
  listenerStartupError ?? new Error('listener.start() returned without emitting connected'),
337
351
  )
338
352
  }
353
+
354
+ if (options.agentDir !== undefined) {
355
+ const runEnrichment = options.enrichHistoricalProvenance ?? enrichHistoricalProvenance
356
+ const resolveHistorical: HistoricalProvenanceResolver = async (where) => {
357
+ const key = {
358
+ adapter: 'discord' as const,
359
+ workspace: where.workspace,
360
+ chat: where.chat,
361
+ thread: where.thread ?? null,
362
+ }
363
+ const [names, roomStatus] = await Promise.all([
364
+ channelResolver(key),
365
+ channelResolver.resolveRoomStatus(where.chat),
366
+ ])
367
+ const room = roomStatus.room
368
+ return {
369
+ where: {
370
+ ...where,
371
+ ...names,
372
+ ...(room?.parentChat !== undefined ? { parentChat: room.parentChat } : {}),
373
+ ...(room?.parentChatName !== undefined ? { parentChatName: room.parentChatName } : {}),
374
+ },
375
+ parentChecked: roomStatus.parentChecked,
376
+ }
377
+ }
378
+ void runEnrichment(options.agentDir, resolveHistorical, { adapter: 'discord' }).then(
379
+ (result) => {
380
+ logger.info(
381
+ `[discord] historical provenance enrichment scanned=${result.scanned} attempted=${result.attempted} resolved=${result.resolved} failed=${result.failed} timed_out=${result.timedOut} changed=${String(result.changed)}`,
382
+ )
383
+ },
384
+ (error: unknown) => {
385
+ logger.warn(`[discord] historical provenance enrichment failed: ${describeError(error)}`)
386
+ },
387
+ )
388
+ }
339
389
  },
340
390
 
341
391
  async stop(): Promise<void> {
@@ -11,9 +11,9 @@ export class AppAuthStrategy implements GithubAuthStrategy {
11
11
  private readonly appId: number
12
12
  private readonly privateKeyPem: string
13
13
  private readonly fetchImpl: typeof fetch
14
- // Keyed by installation id: a single App may span multiple owners, each a
15
- // separate installation with its own short-lived token.
16
- private readonly tokenCache = new Map<number, TokenCacheEntry>()
14
+ // Installation-level and repository-restricted tokens are distinct
15
+ // authorities even when GitHub serves them from the same installation.
16
+ private readonly tokenCache = new Map<string, TokenCacheEntry>()
17
17
  private readonly repoInstallationCache = new Map<string, number>()
18
18
  private soleInstallationId: number | null = null
19
19
  private _selfUser: GithubSelfUser | null = null
@@ -29,25 +29,29 @@ export class AppAuthStrategy implements GithubAuthStrategy {
29
29
  async token(context?: GithubAuthContext): Promise<string> {
30
30
  const jwt = await this.mintJwt()
31
31
  const installId = await this.resolveInstallationId(jwt, context)
32
- return this.installationToken(jwt, installId)
32
+ return this.installationToken(jwt, installId, context?.repoSlug)
33
33
  }
34
34
 
35
35
  async authHeaders(context?: GithubAuthContext): Promise<HeadersInit> {
36
36
  return githubJsonHeaders(await this.token(context))
37
37
  }
38
38
 
39
- private async installationToken(jwt: string, installId: number): Promise<string> {
40
- const cached = this.tokenCache.get(installId)
39
+ private async installationToken(jwt: string, installId: number, repoSlug?: string): Promise<string> {
40
+ const repository = repoSlug === undefined || repoSlug === '' ? undefined : canonicalRepoSlug(repoSlug)
41
+ const cacheKey =
42
+ repository === undefined ? `${installId}:installation` : `${installId}:repository:${repository.slug}`
43
+ const cached = this.tokenCache.get(cacheKey)
41
44
  if (cached && Date.now() < cached.expiresAt - 5 * 60 * 1000) return cached.value
42
45
  const response = await this.fetchImpl(`${GITHUB_API_BASE}/app/installations/${installId}/access_tokens`, {
43
46
  method: 'POST',
44
47
  headers: githubJsonHeaders(jwt),
48
+ body: JSON.stringify(repository === undefined ? {} : { repositories: [repository.name] }),
45
49
  })
46
50
  if (!response.ok) throw new Error(`GitHub App token mint failed: ${response.status}`)
47
51
  const raw = (await response.json()) as { token?: unknown; expires_at?: unknown }
48
52
  if (typeof raw.token !== 'string') throw new Error('GitHub App token response missing token')
49
53
  const expiresAt = typeof raw.expires_at === 'string' ? Date.parse(raw.expires_at) : Date.now() + 60 * 60 * 1000
50
- this.tokenCache.set(installId, { value: raw.token, expiresAt })
54
+ this.tokenCache.set(cacheKey, { value: raw.token, expiresAt })
51
55
  return raw.token
52
56
  }
53
57
 
@@ -116,10 +120,21 @@ export class AppAuthStrategy implements GithubAuthStrategy {
116
120
 
117
121
  private async resolveInstallationId(jwt: string, context?: GithubAuthContext): Promise<number> {
118
122
  if (context?.repoSlug !== undefined && context.repoSlug !== '') {
119
- return this.resolveInstallationByEndpoint(jwt, `repos/${context.repoSlug}/installation`, context.repoSlug)
123
+ const repository = canonicalRepoSlug(context.repoSlug)
124
+ return this.resolveInstallationByEndpoint(
125
+ jwt,
126
+ `repos/${context.repoSlug}/installation`,
127
+ `repository:${repository.slug}`,
128
+ context.repoSlug,
129
+ )
120
130
  }
121
131
  if (context?.owner !== undefined && context.owner !== '') {
122
- return this.resolveInstallationByEndpoint(jwt, `orgs/${context.owner}/installation`, context.owner)
132
+ return this.resolveInstallationByEndpoint(
133
+ jwt,
134
+ `orgs/${context.owner}/installation`,
135
+ `owner:${context.owner.toLocaleLowerCase()}`,
136
+ context.owner,
137
+ )
123
138
  }
124
139
  if (this.soleInstallationId !== null) return this.soleInstallationId
125
140
  const response = await this.fetchImpl(`${GITHUB_API_BASE}/app/installations`, { headers: githubJsonHeaders(jwt) })
@@ -136,8 +151,13 @@ export class AppAuthStrategy implements GithubAuthStrategy {
136
151
  return id
137
152
  }
138
153
 
139
- private async resolveInstallationByEndpoint(jwt: string, path: string, target: string): Promise<number> {
140
- const cached = this.repoInstallationCache.get(target)
154
+ private async resolveInstallationByEndpoint(
155
+ jwt: string,
156
+ path: string,
157
+ cacheKey: string,
158
+ target: string,
159
+ ): Promise<number> {
160
+ const cached = this.repoInstallationCache.get(cacheKey)
141
161
  if (cached !== undefined) return cached
142
162
  const response = await this.fetchImpl(`${GITHUB_API_BASE}/${path}`, { headers: githubJsonHeaders(jwt) })
143
163
  if (response.status === 404) {
@@ -146,11 +166,20 @@ export class AppAuthStrategy implements GithubAuthStrategy {
146
166
  if (!response.ok) throw new Error(`GitHub App installation lookup for ${target} failed: ${response.status}`)
147
167
  const raw = (await response.json()) as { id?: unknown }
148
168
  if (typeof raw.id !== 'number') throw new Error(`GitHub App installation for ${target} missing id`)
149
- this.repoInstallationCache.set(target, raw.id)
169
+ this.repoInstallationCache.set(cacheKey, raw.id)
150
170
  return raw.id
151
171
  }
152
172
  }
153
173
 
174
+ function canonicalRepoSlug(repoSlug: string): { slug: string; name: string } {
175
+ const slug = repoSlug.toLocaleLowerCase()
176
+ const [owner, name, ...rest] = slug.split('/')
177
+ if (owner === undefined || owner === '' || name === undefined || name === '' || rest.length > 0) {
178
+ throw new Error(`Invalid GitHub repository slug: ${repoSlug}`)
179
+ }
180
+ return { slug, name }
181
+ }
182
+
154
183
  function base64url(input: string | Buffer): string {
155
184
  const buf = typeof input === 'string' ? Buffer.from(input) : input
156
185
  return buf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')