typeclaw 0.45.1 → 0.46.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +3 -2
- package/src/agent/git-nudge.ts +2 -1
- package/src/agent/index.ts +44 -30
- package/src/agent/live-subagents.ts +18 -0
- package/src/agent/multimodal/index.ts +1 -1
- package/src/agent/multimodal/look-at.ts +78 -44
- package/src/agent/multimodal/looker.ts +161 -39
- package/src/agent/network/response-body.ts +26 -0
- package/src/agent/network/safe-http.ts +207 -0
- package/src/agent/plugin-tools.ts +377 -157
- package/src/agent/provider-error.ts +52 -1
- package/src/agent/restart/index.ts +61 -40
- package/src/agent/restart-handoff/index.ts +73 -4
- package/src/agent/reviewer-bash-policy.ts +42 -1
- package/src/agent/session-origin.ts +2 -0
- package/src/agent/subagent-drain.ts +117 -7
- package/src/agent/subagents.ts +49 -16
- package/src/agent/throttle-circuit.ts +25 -5
- package/src/agent/tool-error-remediation.ts +72 -0
- package/src/agent/tool-file-safety.ts +1310 -0
- package/src/agent/tools/channel-fetch-attachment.ts +10 -3
- package/src/agent/tools/ddg.ts +16 -2
- package/src/agent/tools/post-github-review.ts +199 -0
- package/src/agent/tools/spawn-subagent.ts +21 -18
- package/src/agent/tools/webfetch/fetch.ts +163 -183
- package/src/agent/tools/webfetch/tool.ts +2 -3
- package/src/agent/tools/websearch.ts +23 -2
- package/src/agent/tools-without-local-file-operands.ts +30 -0
- package/src/agent/turn-runner.ts +86 -6
- package/src/bundled-plugins/backup/README.md +23 -10
- package/src/bundled-plugins/backup/git-auth.ts +6 -4
- package/src/bundled-plugins/backup/index.ts +85 -44
- package/src/bundled-plugins/backup/runner.ts +85 -1
- package/src/bundled-plugins/doc-render/render.ts +93 -12
- package/src/bundled-plugins/doc-render/skills/typeclaw-render-pdf/SKILL.md +13 -13
- package/src/bundled-plugins/github-cli-auth/effective-approval.ts +16 -6
- package/src/bundled-plugins/github-cli-auth/gh-command.ts +555 -15
- package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
- package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
- package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
- package/src/bundled-plugins/github-cli-auth/index.ts +207 -132
- package/src/bundled-plugins/github-cli-auth/review-checkout.ts +138 -0
- package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
- package/src/bundled-plugins/guard/policies/uncommitted-changes.ts +2 -1
- package/src/bundled-plugins/memory/README.md +32 -11
- package/src/bundled-plugins/memory/append-tool.ts +15 -8
- package/src/bundled-plugins/memory/citations.ts +22 -9
- package/src/bundled-plugins/memory/dreaming.ts +13 -8
- package/src/bundled-plugins/memory/find-entry-tool.ts +4 -0
- package/src/bundled-plugins/memory/index.ts +0 -4
- package/src/bundled-plugins/memory/load-memory.ts +8 -3
- package/src/bundled-plugins/memory/provenance-index.ts +717 -0
- package/src/bundled-plugins/memory/provenance-sanitize.ts +40 -0
- package/src/bundled-plugins/memory/references/store-reference-tool.ts +4 -0
- package/src/bundled-plugins/memory/search-tool.ts +163 -60
- package/src/bundled-plugins/memory/stream-events.ts +2 -0
- package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
- package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
- package/src/bundled-plugins/memory/vector/hybrid.ts +126 -13
- package/src/bundled-plugins/researcher/write-report.ts +8 -5
- package/src/bundled-plugins/reviewer/index.ts +2 -2
- package/src/bundled-plugins/reviewer/reviewer.ts +10 -3
- package/src/bundled-plugins/reviewer/skills/code-review.ts +19 -9
- package/src/bundled-plugins/scout/scout.ts +8 -0
- package/src/bundled-plugins/security/index.ts +4 -4
- package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +1 -0
- package/src/bundled-plugins/security/policies/private-surface-read.ts +787 -36
- package/src/bundled-plugins/security/policies/secret-exfil-read.ts +1 -0
- package/src/bundled-plugins/security/policies/ssrf.ts +147 -42
- package/src/channels/adapters/discord-bot-channel-resolver.ts +26 -14
- package/src/channels/adapters/discord-bot-thread-room.ts +44 -12
- package/src/channels/adapters/discord-bot.ts +66 -6
- package/src/channels/adapters/discord-channel-resolver.ts +91 -23
- package/src/channels/adapters/discord-id.ts +4 -0
- package/src/channels/adapters/discord-resolver-cache.ts +41 -0
- package/src/channels/adapters/discord.ts +57 -7
- package/src/channels/adapters/github/auth-app.ts +41 -12
- package/src/channels/adapters/github/inbound.ts +11 -0
- package/src/channels/adapters/github/index.ts +71 -65
- package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
- package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
- package/src/channels/adapters/github/review-submitter.ts +302 -0
- package/src/channels/adapters/kakaotalk-channel-resolver.ts +5 -3
- package/src/channels/adapters/kakaotalk-fetch-attachment.ts +8 -4
- package/src/channels/adapters/kakaotalk-typing.ts +17 -15
- package/src/channels/adapters/slack-attachment-download.ts +71 -0
- package/src/channels/adapters/slack-bot-edit.ts +85 -20
- package/src/channels/adapters/slack-bot.ts +26 -10
- package/src/channels/adapters/slack-classify.ts +21 -2
- package/src/channels/adapters/slack-format.ts +245 -0
- package/src/channels/adapters/slack.ts +59 -7
- package/src/channels/adapters/telegram-bot.ts +18 -5
- package/src/channels/adapters/webex-bot.ts +8 -3
- package/src/channels/adapters/webex.ts +8 -3
- package/src/channels/fetch-attachment.ts +43 -0
- package/src/{bundled-plugins/github-cli-auth/approve-idempotency.ts → channels/github-review-verdict-coordinator.ts} +25 -1
- package/src/channels/github-token-bridge.ts +12 -6
- package/src/channels/manager.ts +9 -0
- package/src/channels/router.ts +248 -6
- package/src/channels/types.ts +28 -1
- package/src/cli/channel.ts +1 -1
- package/src/cli/init.ts +1 -1
- package/src/cli/model.ts +57 -7
- package/src/compose/stop.ts +17 -5
- package/src/config/config.ts +6 -6
- package/src/config/models-mutation.ts +13 -1
- package/src/config/providers.ts +176 -0
- package/src/container/docker-app.ts +42 -13
- package/src/container/start.ts +62 -10
- package/src/cron/bridge.ts +9 -2
- package/src/doctor/checks.ts +8 -3
- package/src/doctor/commit.ts +2 -1
- package/src/doctor/file-ownership.ts +52 -0
- package/src/dreams/git.ts +2 -1
- package/src/git/bound-gc-config.ts +82 -0
- package/src/git/hookless.ts +5 -0
- package/src/git/reconcile-ignored.ts +18 -5
- package/src/git/system-commit.ts +12 -6
- package/src/hostd/spawn.ts +133 -47
- package/src/init/dockerfile.ts +99 -5
- package/src/init/index.ts +17 -5
- package/src/init/models-dev.ts +4 -0
- package/src/init/validate-api-key.ts +2 -0
- package/src/inspect/replay.ts +21 -9
- package/src/inspect/session-list.ts +71 -33
- package/src/mcp/tools.ts +17 -3
- package/src/network/internal-destinations.ts +267 -0
- package/src/permissions/builtins.ts +3 -1
- package/src/plugin/context.ts +2 -0
- package/src/plugin/index.ts +1 -0
- package/src/plugin/manager.ts +4 -0
- package/src/plugin/types.ts +22 -0
- package/src/run/index.ts +40 -0
- package/src/run/llm-fetch-observer.ts +41 -4
- package/src/sandbox/build.ts +37 -15
- package/src/sandbox/canonical-secrets.ts +18 -0
- package/src/sandbox/env-exposure.ts +83 -0
- package/src/sandbox/errors.ts +13 -6
- package/src/sandbox/hidden-paths.ts +102 -44
- package/src/sandbox/index.ts +22 -4
- package/src/sandbox/package-install.ts +3 -27
- package/src/sandbox/policy.ts +16 -28
- package/src/sandbox/privileged-runtime.ts +256 -0
- package/src/sandbox/session-tmp.ts +8 -2
- package/src/sandbox/writable-zones.ts +204 -140
- package/src/secrets/index.ts +8 -0
- package/src/secrets/refresh-provider-oauth.ts +128 -0
- package/src/server/command-runner.ts +48 -4
- package/src/server/index.ts +4 -0
- package/src/skills/typeclaw-channel-github/SKILL.md +49 -56
- package/src/skills/typeclaw-claude-code/SKILL.md +24 -60
- package/src/skills/typeclaw-claude-code/references/auth-flow.md +17 -158
- package/src/skills/typeclaw-codex-cli/SKILL.md +19 -49
- package/src/skills/typeclaw-codex-cli/references/auth-flow.md +17 -154
- package/src/skills/typeclaw-config/SKILL.md +27 -11
- package/src/skills/typeclaw-config/references/recommended-mounts.md +24 -24
- package/src/skills/typeclaw-github-contributing/SKILL.md +23 -26
- package/src/skills/typeclaw-memory/SKILL.md +9 -3
- package/src/skills/typeclaw-permissions/SKILL.md +36 -19
- package/src/skills/typeclaw-plugins/SKILL.md +37 -0
- package/src/usage/scan.ts +11 -2
- package/typeclaw.schema.json +25 -0
|
@@ -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
|
|
23
|
+
type DiscordChannelInfo = Awaited<ReturnType<DiscordClient['getChannel']>>
|
|
14
24
|
|
|
15
|
-
export function createDiscordChannelResolver(options: DiscordChannelResolverOptions):
|
|
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
|
|
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
|
-
|
|
21
|
-
|
|
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
|
|
26
|
-
|
|
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)
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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,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
|
|
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 =
|
|
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
|
-
//
|
|
15
|
-
//
|
|
16
|
-
private readonly tokenCache = new Map<
|
|
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
|
|
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(
|
|
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
|
-
|
|
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(
|
|
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(
|
|
140
|
-
|
|
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(
|
|
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, '')
|
|
@@ -566,6 +566,13 @@ export function classifyGithubInbound(
|
|
|
566
566
|
const reviewer = readUser(review.user)
|
|
567
567
|
const body = readString(review, 'body')
|
|
568
568
|
const hasBody = body !== null && body.trim() !== ''
|
|
569
|
+
// A body-less COMMENTED review is the container GitHub submits when a user
|
|
570
|
+
// replies inline to a review thread; those replies arrive separately as
|
|
571
|
+
// pull_request_review_comment events. Synthesizing "@x submitted a review"
|
|
572
|
+
// here duplicates that traffic with a contentless line that trips the
|
|
573
|
+
// solo-human engagement fallback into a reflexive "Thanks for the review!".
|
|
574
|
+
// APPROVED / CHANGES_REQUESTED are real verdicts, so their synthesis stays.
|
|
575
|
+
if (!hasBody && isCommentedReviewState(readString(review, 'state'))) return null
|
|
569
576
|
const text = hasBody
|
|
570
577
|
? body
|
|
571
578
|
: reviewer !== null
|
|
@@ -912,6 +919,10 @@ function isActionableReviewState(state: string | null): boolean {
|
|
|
912
919
|
return state?.toLowerCase() !== 'approved'
|
|
913
920
|
}
|
|
914
921
|
|
|
922
|
+
function isCommentedReviewState(state: string | null): boolean {
|
|
923
|
+
return state?.toLowerCase() === 'commented'
|
|
924
|
+
}
|
|
925
|
+
|
|
915
926
|
async function resolveTeamMembership(
|
|
916
927
|
event: string,
|
|
917
928
|
payload: Record<string, unknown>,
|