typeclaw 0.39.2 → 0.39.3

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 (73) hide show
  1. package/auth.schema.json +110 -0
  2. package/package.json +2 -2
  3. package/secrets.schema.json +110 -0
  4. package/src/agent/index.ts +44 -19
  5. package/src/agent/plugin-tools.ts +12 -12
  6. package/src/agent/session-origin.ts +2 -0
  7. package/src/agent/todo/store.ts +21 -1
  8. package/src/agent/tools/channel-reply.ts +18 -5
  9. package/src/agent/tools/channel-send.ts +13 -2
  10. package/src/bundled-plugins/doc-render/render.ts +2 -2
  11. package/src/bundled-plugins/github-cli-auth/approve-idempotency.ts +40 -18
  12. package/src/channels/adapters/discord-author-resolver.ts +52 -0
  13. package/src/channels/adapters/discord-bot.ts +1 -1
  14. package/src/channels/adapters/discord-channel-resolver.ts +44 -0
  15. package/src/channels/adapters/discord-classify.ts +121 -0
  16. package/src/channels/adapters/discord-reactions.ts +138 -0
  17. package/src/channels/adapters/discord.ts +416 -0
  18. package/src/channels/adapters/github/outbound.ts +23 -4
  19. package/src/channels/adapters/kakaotalk.ts +8 -1
  20. package/src/channels/adapters/line.ts +1 -1
  21. package/src/channels/adapters/slack-author-resolver.ts +54 -0
  22. package/src/channels/adapters/slack-bot.ts +3 -1
  23. package/src/channels/adapters/slack-channel-resolver.ts +49 -0
  24. package/src/channels/adapters/slack-classify.ts +83 -0
  25. package/src/channels/adapters/slack-reactions.ts +126 -0
  26. package/src/channels/adapters/slack.ts +416 -0
  27. package/src/channels/adapters/telegram-bot.ts +2 -1
  28. package/src/channels/adapters/webex-bot.ts +5 -2
  29. package/src/channels/adapters/webex.ts +55 -2
  30. package/src/channels/github-review-turn-ledger.ts +32 -0
  31. package/src/channels/github-verdict-activity.ts +60 -0
  32. package/src/channels/index.ts +6 -0
  33. package/src/channels/manager.ts +130 -6
  34. package/src/channels/pr-verdict-activity-bridge.ts +43 -0
  35. package/src/channels/router.ts +161 -9
  36. package/src/channels/schema.ts +4 -0
  37. package/src/channels/types.ts +17 -1
  38. package/src/cli/builtins.ts +1 -0
  39. package/src/cli/channel.ts +170 -18
  40. package/src/cli/index.ts +11 -0
  41. package/src/cli/init.ts +2 -0
  42. package/src/cli/update-check.ts +14 -0
  43. package/src/cli/update-notify.ts +91 -0
  44. package/src/cli/update-suppression.ts +19 -0
  45. package/src/config/channels-mutation.ts +3 -1
  46. package/src/config/config.ts +51 -0
  47. package/src/config/index.ts +1 -0
  48. package/src/container/start.ts +30 -6
  49. package/src/doctor/channel-checks.ts +57 -3
  50. package/src/hostd/daemon.ts +22 -5
  51. package/src/hostd/index.ts +11 -1
  52. package/src/hostd/paths.ts +4 -0
  53. package/src/hostd/protocol.ts +15 -2
  54. package/src/init/cli-version.ts +1 -1
  55. package/src/init/discord-auth.ts +50 -0
  56. package/src/init/index.ts +94 -4
  57. package/src/init/reconcile-plugin-deps.ts +30 -5
  58. package/src/init/run-owner-claim.ts +2 -0
  59. package/src/init/slack-auth.ts +51 -0
  60. package/src/mcp/manager.ts +94 -7
  61. package/src/mcp/tools.ts +7 -4
  62. package/src/permissions/resolve.ts +2 -0
  63. package/src/role-claim/match-rule.ts +2 -0
  64. package/src/run/index.ts +74 -29
  65. package/src/secrets/discord-store.ts +96 -0
  66. package/src/secrets/schema.ts +34 -0
  67. package/src/secrets/slack-store.ts +96 -0
  68. package/src/server/command-runner.ts +21 -0
  69. package/src/skills/typeclaw-cron/SKILL.md +13 -2
  70. package/src/tui/format.ts +0 -19
  71. package/src/tui/index.ts +2 -8
  72. package/src/update/check.ts +192 -0
  73. package/typeclaw.schema.json +332 -0
@@ -12,6 +12,8 @@ export type McpRefreshResult = { ok: true; name: string; toolCount: number } | {
12
12
 
13
13
  export type McpManager = {
14
14
  connectAll(opts?: { signal?: AbortSignal }): Promise<McpConnectResult[]>
15
+ ensureConnected(name: string): Promise<McpConnection | undefined>
16
+ whenInitialConnectSettled(opts?: { timeoutMs?: number }): Promise<void>
15
17
  getConnection(name: string): McpConnection | undefined
16
18
  listServers(): { name: string; description?: string; connected: boolean; toolCount?: number }[]
17
19
  refresh(): Promise<McpRefreshResult[]>
@@ -31,11 +33,55 @@ export function createMcpManager(
31
33
  const connect = opts.connect ?? connectMcpServer
32
34
  const connections = new Map<string, McpConnection>()
33
35
  const toolCounts = new Map<string, number>()
36
+ // In-flight connect promises keyed by server name. Shared by connectAll (boot
37
+ // warm-up) and ensureConnected (lazy, on a tool call) so a tool call racing
38
+ // the warm-up — or two concurrent tool calls — coalesce onto one connect
39
+ // attempt instead of spawning the server twice. connectServer stores the
40
+ // connection on success and clears the entry once settled.
41
+ const inflight = new Map<string, Promise<McpConnectResult>>()
42
+ // The first connectAll() (the boot warm-up). whenInitialConnectSettled awaits
43
+ // it so the catalog render can wait out the warm-up window.
44
+ let initialConnect: Promise<McpConnectResult[]> | null = null
45
+ // Shutdown latch. Because the boot warm-up runs un-awaited, closeAll() can
46
+ // return while a connect is still in flight; without this, that late attempt
47
+ // would cache (and never close) a live connection on an already-closed
48
+ // manager. Once set, a resolving attempt closes its connection instead of
49
+ // caching it, and new ensureConnected() calls are refused.
50
+ let closed = false
51
+
52
+ function connectServer(server: McpServer, signal: AbortSignal | undefined): Promise<McpConnectResult> {
53
+ const established = connections.get(server.name)
54
+ if (established !== undefined) {
55
+ return Promise.resolve({
56
+ ok: true,
57
+ name: server.name,
58
+ connection: established,
59
+ toolCount: toolCounts.get(server.name) ?? 0,
60
+ })
61
+ }
62
+ const pending = inflight.get(server.name)
63
+ if (pending !== undefined) return pending
64
+
65
+ const attempt = connectOne(server, opts.env, connect, signal).then(async (result) => {
66
+ inflight.delete(server.name)
67
+ if (result.ok) {
68
+ if (closed) {
69
+ await result.connection.close().catch(() => {})
70
+ } else {
71
+ connections.set(result.name, result.connection)
72
+ toolCounts.set(result.name, result.toolCount)
73
+ }
74
+ }
75
+ return result
76
+ })
77
+ inflight.set(server.name, attempt)
78
+ return attempt
79
+ }
34
80
 
35
81
  return {
36
82
  async connectAll(connectOpts: { signal?: AbortSignal } = {}): Promise<McpConnectResult[]> {
37
83
  const firstIndexByName = new Map<string, number>()
38
- const results = await Promise.all(
84
+ const attempt = Promise.all(
39
85
  activeServers.map((server, index): Promise<McpConnectResult> => {
40
86
  // The name is the tool namespace and the connections key, so a second
41
87
  // server sharing a name would silently shadow the first. Fail the
@@ -51,15 +97,51 @@ export function createMcpManager(
51
97
  })
52
98
  }
53
99
  firstIndexByName.set(server.name, index)
54
- return connectOne(server, opts.env, connect, connectOpts.signal)
100
+ return connectServer(server, connectOpts.signal)
55
101
  }),
56
102
  )
57
- for (const result of results) {
58
- if (!result.ok) continue
59
- connections.set(result.name, result.connection)
60
- toolCounts.set(result.name, result.toolCount)
103
+ initialConnect ??= attempt
104
+ return attempt
105
+ },
106
+ async ensureConnected(name: string): Promise<McpConnection | undefined> {
107
+ if (closed) return undefined
108
+ const established = connections.get(name)
109
+ if (established !== undefined) return established
110
+ const server = activeServers.find((candidate) => candidate.name === name)
111
+ if (server === undefined) return undefined
112
+ const result = await connectServer(server, undefined)
113
+ return result.ok ? result.connection : undefined
114
+ },
115
+ async whenInitialConnectSettled(readinessOpts: { timeoutMs?: number } = {}): Promise<void> {
116
+ if (initialConnect === null) return
117
+ const settled = initialConnect.then(
118
+ () => {},
119
+ () => {},
120
+ )
121
+ // No timeout (the catalog-render default) means await the real connectAll
122
+ // settle. A fixed cap can't be correct here: a server is cached only after
123
+ // connect() THEN the FULL paginated listTools(), whose total time is bounded
124
+ // only by the server's own per-phase deadlines (connect timeout + a
125
+ // configurable, up-to-10min request timeout, times the number of cursor
126
+ // pages). Any fixed bound would silently drop a healthy slow/paginated
127
+ // server from the first catalog. connectAll settles on its own per those
128
+ // deadlines; infinite pagination is a pre-existing connectAll liveness risk,
129
+ // not introduced here. An explicit timeoutMs is honored for tests/callers.
130
+ const timeoutMs = readinessOpts.timeoutMs
131
+ if (timeoutMs === undefined || timeoutMs <= 0) {
132
+ await settled
133
+ return
134
+ }
135
+ let timer: ReturnType<typeof setTimeout> | undefined
136
+ const timeout = new Promise<void>((resolve) => {
137
+ timer = setTimeout(resolve, timeoutMs)
138
+ timer.unref?.()
139
+ })
140
+ try {
141
+ await Promise.race([settled, timeout])
142
+ } finally {
143
+ if (timer !== undefined) clearTimeout(timer)
61
144
  }
62
- return results
63
145
  },
64
146
  getConnection(name: string): McpConnection | undefined {
65
147
  return connections.get(name)
@@ -92,9 +174,14 @@ export function createMcpManager(
92
174
  )
93
175
  },
94
176
  async closeAll(): Promise<void> {
177
+ // Latch shutdown first so any connect still in flight (the un-awaited boot
178
+ // warm-up) closes its own connection on resolve instead of caching it.
179
+ closed = true
95
180
  await Promise.allSettled([...connections.values()].map((connection) => connection.close()))
96
181
  connections.clear()
97
182
  toolCounts.clear()
183
+ inflight.clear()
184
+ initialConnect = null
98
185
  },
99
186
  }
100
187
  }
package/src/mcp/tools.ts CHANGED
@@ -27,7 +27,7 @@ function createListToolsTool(manager: McpManager): Tool<McpListToolsArgs> {
27
27
  server: z.string().describe('The MCP server name from the system prompt catalog.'),
28
28
  }),
29
29
  async execute(args) {
30
- const connection = manager.getConnection(args.server)
30
+ const connection = await manager.ensureConnected(args.server)
31
31
  if (connection === undefined) return textResult(unknownServerMessage(manager, args.server))
32
32
 
33
33
  const tools = await safeListTools(connection)
@@ -51,7 +51,7 @@ function createDescribeTool(manager: McpManager): Tool<McpDescribeArgs> {
51
51
  }),
52
52
  async execute(args) {
53
53
  const resolved = resolveToolArgs(args.server, args.tool)
54
- const connection = manager.getConnection(resolved.server)
54
+ const connection = await manager.ensureConnected(resolved.server)
55
55
  if (connection === undefined) return textResult(unknownServerMessage(manager, resolved.server))
56
56
 
57
57
  const tools = await safeListTools(connection)
@@ -89,7 +89,7 @@ function createCallTool(manager: McpManager): Tool<McpCallArgs> {
89
89
  }),
90
90
  async execute(args) {
91
91
  const resolved = resolveToolArgs(args.server, args.tool)
92
- const connection = manager.getConnection(resolved.server)
92
+ const connection = await manager.ensureConnected(resolved.server)
93
93
  if (connection === undefined) return textResult(unknownServerMessage(manager, resolved.server))
94
94
 
95
95
  const result = await safeCallTool(connection, resolved.tool, args.args ?? {})
@@ -104,9 +104,12 @@ function resolveToolArgs(server: string, tool: string): { server: string; tool:
104
104
  }
105
105
 
106
106
  function unknownServerMessage(manager: McpManager, server: string): string {
107
+ // List every configured server, not just connected ones: under lazy connect a
108
+ // valid server is reached only when ensureConnected returns undefined for a
109
+ // name that isn't configured at all, so filtering by `connected` here would
110
+ // report "none" while servers exist.
107
111
  const available = manager
108
112
  .listServers()
109
- .filter((entry) => entry.connected)
110
113
  .map((entry) => entry.name)
111
114
  .join(', ')
112
115
  return `Unknown MCP server ${JSON.stringify(server)}. Available servers: ${available || 'none'}.`
@@ -4,7 +4,9 @@ import type { AdapterId } from '@/channels/schema'
4
4
  import type { MatchRule, Platform } from './match-rule'
5
5
 
6
6
  const ADAPTER_TO_PLATFORM: Record<AdapterId, Platform> = {
7
+ slack: 'slack',
7
8
  'slack-bot': 'slack',
9
+ discord: 'discord',
8
10
  'discord-bot': 'discord',
9
11
  github: 'github',
10
12
  'telegram-bot': 'telegram',
@@ -29,7 +29,9 @@ const ADAPTER_TO_PLATFORM: Record<
29
29
  ChannelKey['adapter'],
30
30
  'slack' | 'discord' | 'telegram' | 'webex' | 'kakao' | 'line' | 'github'
31
31
  > = {
32
+ slack: 'slack',
32
33
  'slack-bot': 'slack',
34
+ discord: 'discord',
33
35
  'discord-bot': 'discord',
34
36
  github: 'github',
35
37
  'telegram-bot': 'telegram',
package/src/run/index.ts CHANGED
@@ -28,19 +28,15 @@ import {
28
28
  createChannelManager,
29
29
  createChannelsReloadable,
30
30
  createGithubTokenBridge,
31
+ createPrVerdictActivityBridge,
31
32
  createSubagentCompletionBridge,
33
+ setReviewObserver,
32
34
  type ChannelManager,
35
+ type PrVerdictActivityBridge,
33
36
  type SubagentCompletionBridge,
34
37
  } from '@/channels'
35
38
  import { createTunnelBridge, type TunnelBridge } from '@/channels/tunnel-bridge'
36
- import {
37
- createConfigReloadable,
38
- getConfig,
39
- loadConfigSync,
40
- loadPluginConfigsSync,
41
- reloadConfig,
42
- withDefaultPlugins,
43
- } from '@/config'
39
+ import { createConfigReloadable, getConfig, loadConfigBundleSync, reloadConfig, withDefaultPlugins } from '@/config'
44
40
  import {
45
41
  type CountStore,
46
42
  type CronConsumer,
@@ -161,23 +157,36 @@ export async function startAgent({
161
157
  const tuiToken = process.env.TYPECLAW_TUI_TOKEN
162
158
  const tuiTokenOpt = tuiToken !== undefined && tuiToken !== '' ? { tuiToken } : {}
163
159
 
164
- const pluginConfigsByName = loadPluginConfigsSync(cwd)
160
+ const { config: cwdConfig, pluginConfigs: pluginConfigsByName } = loadConfigBundleSync(cwd)
165
161
  // Vector agents omit the system-prompt `# Memory` section and inject memory
166
162
  // per-turn instead. Derived once here: `memory.vector.enabled` is
167
163
  // restart-required, so a single boot read is coherent for the process.
168
164
  const suppressSystemMemory = vectorEnabledFromMemoryConfig(pluginConfigsByName.memory)
169
- const cwdConfig = loadConfigSync(cwd)
170
165
  const githubTokenBridge = createGithubTokenBridge()
171
166
  const mcpManager =
172
167
  cwdConfig.mcpServers.length > 0 ? createMcpManager(cwdConfig.mcpServers, { env: process.env }) : null
168
+ const mcpManagerOpt = mcpManager !== null ? { mcpManager } : {}
169
+
170
+ // Warm up MCP connections in the BACKGROUND so boot doesn't block on each
171
+ // server's subprocess spawn + listTools() (worst case the 15s connect
172
+ // timeout). Tool calls lazily ensureConnected() and the catalog render awaits
173
+ // whenInitialConnectSettled(), so correctness never depends on this finishing
174
+ // first. closeAll() below cleans these up if boot aborts.
173
175
  if (mcpManager !== null) {
174
- const results = await mcpManager.connectAll()
175
- for (const result of results) {
176
- if (!result.ok) console.warn(`[mcp] ${result.name} failed to connect: ${result.error.message}`)
177
- }
176
+ void mcpManager.connectAll().then((results) => {
177
+ for (const result of results) {
178
+ if (!result.ok) console.warn(`[mcp] ${result.name} failed to connect: ${result.error.message}`)
179
+ }
180
+ })
178
181
  }
179
- const mcpManagerOpt = mcpManager !== null ? { mcpManager } : {}
180
- const pluginsLoaded = await loadPlugins({
182
+
183
+ // loadPlugins and the vector startup unit have no mutual data dependency, so
184
+ // run them concurrently. loadPlugins stays UNCAUGHT so a bundled-plugin or
185
+ // plugin-security failure still aborts boot; when it does, close the background
186
+ // MCP warm-up so its servers can't leak — startAgent returns no stop() handler
187
+ // on this path, so this is the only cleanup chance. closeAll() also closes any
188
+ // connection the warm-up establishes after shutdown begins.
189
+ const pluginsLoadedPromise = loadPlugins({
181
190
  entries: withDefaultPlugins(cwdConfig.plugins),
182
191
  agentDir: cwd,
183
192
  configsByName: pluginConfigsByName,
@@ -186,6 +195,15 @@ export async function startAgent({
186
195
  hasGithubAppTokenResolver: githubTokenBridge.hasAppTokenResolver,
187
196
  ...(cwdConfig.roles !== undefined ? { roles: cwdConfig.roles } : {}),
188
197
  })
198
+ const vectorStartupPromise = suppressSystemMemory ? runVectorStartup(cwd) : Promise.resolve()
199
+ let pluginsLoaded: LoadPluginsResult
200
+ try {
201
+ const [loaded] = await Promise.all([pluginsLoadedPromise, vectorStartupPromise])
202
+ pluginsLoaded = loaded
203
+ } catch (err) {
204
+ if (mcpManager !== null) await mcpManager.closeAll()
205
+ throw err
206
+ }
189
207
 
190
208
  reloadRegistry.register(
191
209
  createConfigReloadable({
@@ -224,19 +242,6 @@ export async function startAgent({
224
242
  // v2-only parser rejects the file and hydrate below sees no channels. Runs
225
243
  // exactly once per folder; a folder already at v2 is a no-op.
226
244
  runStartupMigrations(cwd)
227
- if (suppressSystemMemory) {
228
- await buildStartupVectorIndex(cwd, embed).catch((err) => {
229
- console.warn(`[vector] startup index build failed: ${err instanceof Error ? err.message : String(err)}`)
230
- })
231
-
232
- // Warm the embedder now (even when the index needed no rebuild above, which
233
- // skips embed() entirely) so the first channel turn's query embed doesn't
234
- // pay the one-time ONNX init on its critical path. Non-fatal: a failure here
235
- // degrades to the per-turn lazy load, same as before this step existed.
236
- await warmEmbedder().catch((err) => {
237
- console.warn(`[vector] embedder warm-up failed: ${err instanceof Error ? err.message : String(err)}`)
238
- })
239
- }
240
245
 
241
246
  // Channel adapters read `process.env[TOKEN_ENV]` (see channels/manager.ts).
242
247
  // Hydrate fills any unset env var from secrets.json#channels via env-wins:
@@ -319,6 +324,14 @@ export async function startAgent({
319
324
  claimHandler: claimController.claimHandler,
320
325
  githubTokenBridge,
321
326
  stream,
327
+ newestRunningChildSubagentStartedAt: (sessionId) =>
328
+ liveSubagentRegistry
329
+ .list({ parentSessionId: sessionId })
330
+ .reduce<number | null>(
331
+ (newest, child) =>
332
+ child.status === 'running' && (newest === null || child.startedAt > newest) ? child.startedAt : newest,
333
+ null,
334
+ ),
322
335
  onReload: async () => {
323
336
  const { results } = await reloadRegistry.reloadAll()
324
337
  return formatChannelReloadSummary(results)
@@ -657,6 +670,22 @@ export async function startAgent({
657
670
  router: channelManager.router,
658
671
  })
659
672
 
673
+ // Fan a landed formal review verdict out to the sibling sessions reviewing the
674
+ // same PR so they stand down from a redundant verdict (the per-thread fan-out
675
+ // duplicate-review fix). The github-cli-auth plugin records the verdict in the
676
+ // turn-ledger but has no stream access, so the ledger's review observer is the
677
+ // seam: it publishes onto the broadcast bus here, and the bridge routes it.
678
+ const prVerdictActivityBridge: PrVerdictActivityBridge = createPrVerdictActivityBridge({
679
+ stream,
680
+ router: channelManager.router,
681
+ })
682
+ setReviewObserver((review) => {
683
+ stream.publish({
684
+ target: { kind: 'broadcast' },
685
+ payload: { kind: 'pr.verdict-activity', ...review },
686
+ })
687
+ })
688
+
660
689
  reloadRegistry.register(createChannelsReloadable({ manager: channelManager }))
661
690
 
662
691
  // Two-phase channel restart-resume around adapter startup, to close the race
@@ -853,6 +882,8 @@ export async function startAgent({
853
882
  void disposeMaterializedSkills(pluginRuntime)
854
883
  tunnelBridge.stop()
855
884
  subagentCompletionBridge.stop()
885
+ prVerdictActivityBridge.stop()
886
+ setReviewObserver(null)
856
887
  await tunnelManager.stop()
857
888
  await channelManager.stop()
858
889
  await mcpManager?.closeAll()
@@ -895,6 +926,20 @@ export async function startAgent({
895
926
  }
896
927
  }
897
928
 
929
+ async function runVectorStartup(cwd: string): Promise<void> {
930
+ await buildStartupVectorIndex(cwd, embed).catch((err) => {
931
+ console.warn(`[vector] startup index build failed: ${err instanceof Error ? err.message : String(err)}`)
932
+ })
933
+
934
+ // Warm the embedder after the index pass (even when the index needed no
935
+ // rebuild above, which skips embed() entirely) so the first channel turn's
936
+ // query embed doesn't pay the one-time ONNX init on its critical path.
937
+ // Non-fatal: a failure here degrades to the per-turn lazy load.
938
+ await warmEmbedder().catch((err) => {
939
+ console.warn(`[vector] embedder warm-up failed: ${err instanceof Error ? err.message : String(err)}`)
940
+ })
941
+ }
942
+
898
943
  function buildLocalTuiUrl(port: number, token: string | null): string {
899
944
  if (token === null) return `ws://localhost:${port}`
900
945
  const url = new URL(`ws://localhost:${port}`)
@@ -0,0 +1,96 @@
1
+ import { sendHttp } from '@/hostd/client'
2
+
3
+ import { type DiscordAccountRecord, type DiscordChannelBlock, discordChannelBlockSchema } from './schema'
4
+ import { SecretsBackend } from './storage'
5
+
6
+ export type SetDiscordAccountInput = DiscordAccountRecord
7
+
8
+ export type SecretsDiscordCredentialStoreOptions =
9
+ | { mode: 'host'; secretsPath: string }
10
+ | { mode: 'container'; secretsPath: string; hostdUrl: string; restartToken: string; containerName: string }
11
+
12
+ const EMPTY_BLOCK: DiscordChannelBlock = { currentAccount: null, accounts: {} }
13
+
14
+ export class SecretsDiscordCredentialStore {
15
+ private readonly backend: SecretsBackend
16
+ private writeChain: Promise<void> = Promise.resolve()
17
+
18
+ constructor(private readonly options: SecretsDiscordCredentialStoreOptions) {
19
+ this.backend = new SecretsBackend(options.secretsPath)
20
+ }
21
+
22
+ async getAccount(id?: string): Promise<DiscordAccountRecord | null> {
23
+ const block = this.readBlock()
24
+ const accountId = id ?? block.currentAccount
25
+ if (!accountId) return null
26
+ return block.accounts[accountId] ?? null
27
+ }
28
+
29
+ async setAccount(account: SetDiscordAccountInput): Promise<void> {
30
+ await this.writeBlock((block) => {
31
+ const accounts = { ...block.accounts, [account.account_id]: account }
32
+ return { ...block, currentAccount: block.currentAccount ?? account.account_id, accounts }
33
+ })
34
+ }
35
+
36
+ async removeAccount(id: string): Promise<void> {
37
+ await this.writeBlock((block) => {
38
+ const accounts = { ...block.accounts }
39
+ delete accounts[id]
40
+ const currentAccount = block.currentAccount === id ? (Object.keys(accounts)[0] ?? null) : block.currentAccount
41
+ return { ...block, currentAccount, accounts }
42
+ })
43
+ }
44
+
45
+ async listAccounts(): Promise<Array<DiscordAccountRecord & { is_current: boolean }>> {
46
+ const block = this.readBlock()
47
+ return Object.values(block.accounts).map((account) => ({
48
+ ...account,
49
+ is_current: account.account_id === block.currentAccount,
50
+ }))
51
+ }
52
+
53
+ async setCurrentAccount(id: string): Promise<void> {
54
+ await this.writeBlock((block) => ({ ...block, currentAccount: id }))
55
+ }
56
+
57
+ private readBlock(): DiscordChannelBlock {
58
+ const channels =
59
+ this.options.mode === 'container' ? this.backend.tryReadChannelsSync() : this.backend.readChannelsSync()
60
+ return parseBlock(channels?.discord)
61
+ }
62
+
63
+ private async writeBlock(update: (current: DiscordChannelBlock) => DiscordChannelBlock): Promise<void> {
64
+ return this.enqueueWrite(async () => {
65
+ if (this.options.mode === 'container') {
66
+ const next = update(this.readBlock())
67
+ const response = await sendHttp(
68
+ {
69
+ kind: 'secrets-patch',
70
+ containerName: this.options.containerName,
71
+ patch: { channels: { discord: next } },
72
+ },
73
+ { url: this.options.hostdUrl, token: this.options.restartToken },
74
+ )
75
+ if (!response.ok) throw new Error(`secrets-patch failed: ${response.reason}`)
76
+ return
77
+ }
78
+
79
+ await this.backend.updateChannelsAsync(async (channels) => {
80
+ const next = { ...channels, discord: update(parseBlock(channels.discord)) }
81
+ return { result: undefined, next }
82
+ })
83
+ })
84
+ }
85
+
86
+ private enqueueWrite(op: () => Promise<void>): Promise<void> {
87
+ const next = this.writeChain.then(op, op)
88
+ this.writeChain = next.catch(() => {})
89
+ return next
90
+ }
91
+ }
92
+
93
+ function parseBlock(value: unknown): DiscordChannelBlock {
94
+ if (value === undefined) return EMPTY_BLOCK
95
+ return discordChannelBlockSchema.parse(value)
96
+ }
@@ -35,6 +35,19 @@ const discordBotChannelSchema = z.object({
35
35
  token: secretFieldSchema.optional(),
36
36
  })
37
37
 
38
+ export const discordAccountRecordSchema = z.object({
39
+ account_id: z.string(),
40
+ token: z.string(),
41
+ username: z.string().optional(),
42
+ created_at: z.string(),
43
+ updated_at: z.string(),
44
+ })
45
+
46
+ export const discordChannelBlockSchema = z.object({
47
+ currentAccount: z.string().nullable(),
48
+ accounts: z.record(z.string(), discordAccountRecordSchema),
49
+ })
50
+
38
51
  const telegramBotChannelSchema = z.object({
39
52
  token: secretFieldSchema.optional(),
40
53
  })
@@ -144,6 +157,21 @@ export const webexChannelBlockSchema = z.object({
144
157
  accounts: z.record(z.string(), webexAccountRecordSchema),
145
158
  })
146
159
 
160
+ export const slackAccountRecordSchema = z.object({
161
+ account_id: z.string(),
162
+ token: z.string(),
163
+ cookie: z.string(),
164
+ workspace_id: z.string(),
165
+ workspace_name: z.string().optional(),
166
+ created_at: z.string(),
167
+ updated_at: z.string(),
168
+ })
169
+
170
+ export const slackChannelBlockSchema = z.object({
171
+ currentAccount: z.string().nullable(),
172
+ accounts: z.record(z.string(), slackAccountRecordSchema),
173
+ })
174
+
147
175
  export const kakaoPendingLoginRecordSchema = z.object({
148
176
  device_uuid: z.string(),
149
177
  device_type: z.union([z.literal('pc'), z.literal('tablet')]),
@@ -160,12 +188,14 @@ export const kakaoChannelBlockSchema = z.object({
160
188
  export const channelsSchema = z
161
189
  .object({
162
190
  'slack-bot': slackBotChannelSchema.optional(),
191
+ discord: discordChannelBlockSchema.optional(),
163
192
  'discord-bot': discordBotChannelSchema.optional(),
164
193
  github: githubChannelSchema.optional(),
165
194
  'telegram-bot': telegramBotChannelSchema.optional(),
166
195
  line: lineChannelBlockSchema.optional(),
167
196
  kakaotalk: kakaoChannelBlockSchema.optional(),
168
197
  webex: webexChannelBlockSchema.optional(),
198
+ slack: slackChannelBlockSchema.optional(),
169
199
  })
170
200
  .catchall(z.unknown())
171
201
 
@@ -186,6 +216,8 @@ export type Channels = z.infer<typeof channelsSchema>
186
216
  export type GithubPatAuthBlock = z.infer<typeof githubPatAuthSchema>
187
217
  export type GithubAppAuthBlock = z.infer<typeof githubAppAuthSchema>
188
218
  export type GithubSecretsBlock = z.infer<typeof githubChannelSchema>
219
+ export type DiscordAccountRecord = z.infer<typeof discordAccountRecordSchema>
220
+ export type DiscordChannelBlock = z.infer<typeof discordChannelBlockSchema>
189
221
  export type LineAccountRecord = z.infer<typeof lineAccountRecordSchema>
190
222
  export type LineChannelBlock = z.infer<typeof lineChannelBlockSchema>
191
223
  export type KakaoAccountRecord = z.infer<typeof kakaoAccountRecordSchema>
@@ -194,6 +226,8 @@ export type KakaoChannelBlock = z.infer<typeof kakaoChannelBlockSchema>
194
226
  export type WebexAccountRecord = z.infer<typeof webexAccountRecordSchema>
195
227
  export type WebexChannelBlock = z.infer<typeof webexChannelBlockSchema>
196
228
  export type WebexEncryptedPassword = z.infer<typeof webexEncryptedPasswordSchema>
229
+ export type SlackAccountRecord = z.infer<typeof slackAccountRecordSchema>
230
+ export type SlackChannelBlock = z.infer<typeof slackChannelBlockSchema>
197
231
  export type SecretsFile = z.infer<typeof secretsFileSchema>
198
232
 
199
233
  export type ParseSecretsResult = { ok: true; file: SecretsFile } | { ok: false; reason: string }
@@ -0,0 +1,96 @@
1
+ import { sendHttp } from '@/hostd/client'
2
+
3
+ import { type SlackAccountRecord, type SlackChannelBlock, slackChannelBlockSchema } from './schema'
4
+ import { SecretsBackend } from './storage'
5
+
6
+ export type SetSlackAccountInput = SlackAccountRecord
7
+
8
+ export type SecretsSlackCredentialStoreOptions =
9
+ | { mode: 'host'; secretsPath: string }
10
+ | { mode: 'container'; secretsPath: string; hostdUrl: string; restartToken: string; containerName: string }
11
+
12
+ const EMPTY_BLOCK: SlackChannelBlock = { currentAccount: null, accounts: {} }
13
+
14
+ export class SecretsSlackCredentialStore {
15
+ private readonly backend: SecretsBackend
16
+ private writeChain: Promise<void> = Promise.resolve()
17
+
18
+ constructor(private readonly options: SecretsSlackCredentialStoreOptions) {
19
+ this.backend = new SecretsBackend(options.secretsPath)
20
+ }
21
+
22
+ async getAccount(id?: string): Promise<SlackAccountRecord | null> {
23
+ const block = this.readBlock()
24
+ const accountId = id ?? block.currentAccount
25
+ if (!accountId) return null
26
+ return block.accounts[accountId] ?? null
27
+ }
28
+
29
+ async setAccount(account: SetSlackAccountInput): Promise<void> {
30
+ await this.writeBlock((block) => {
31
+ const accounts = { ...block.accounts, [account.account_id]: account }
32
+ return { ...block, currentAccount: block.currentAccount ?? account.account_id, accounts }
33
+ })
34
+ }
35
+
36
+ async removeAccount(id: string): Promise<void> {
37
+ await this.writeBlock((block) => {
38
+ const accounts = { ...block.accounts }
39
+ delete accounts[id]
40
+ const currentAccount = block.currentAccount === id ? (Object.keys(accounts)[0] ?? null) : block.currentAccount
41
+ return { ...block, currentAccount, accounts }
42
+ })
43
+ }
44
+
45
+ async listAccounts(): Promise<Array<SlackAccountRecord & { is_current: boolean }>> {
46
+ const block = this.readBlock()
47
+ return Object.values(block.accounts).map((account) => ({
48
+ ...account,
49
+ is_current: account.account_id === block.currentAccount,
50
+ }))
51
+ }
52
+
53
+ async setCurrentAccount(id: string): Promise<void> {
54
+ await this.writeBlock((block) => ({ ...block, currentAccount: id }))
55
+ }
56
+
57
+ private readBlock(): SlackChannelBlock {
58
+ const channels =
59
+ this.options.mode === 'container' ? this.backend.tryReadChannelsSync() : this.backend.readChannelsSync()
60
+ return parseBlock(channels?.slack)
61
+ }
62
+
63
+ private async writeBlock(update: (current: SlackChannelBlock) => SlackChannelBlock): Promise<void> {
64
+ return this.enqueueWrite(async () => {
65
+ if (this.options.mode === 'container') {
66
+ const next = update(this.readBlock())
67
+ const response = await sendHttp(
68
+ {
69
+ kind: 'secrets-patch',
70
+ containerName: this.options.containerName,
71
+ patch: { channels: { slack: next } },
72
+ },
73
+ { url: this.options.hostdUrl, token: this.options.restartToken },
74
+ )
75
+ if (!response.ok) throw new Error(`secrets-patch failed: ${response.reason}`)
76
+ return
77
+ }
78
+
79
+ await this.backend.updateChannelsAsync(async (channels) => {
80
+ const next = { ...channels, slack: update(parseBlock(channels.slack)) }
81
+ return { result: undefined, next }
82
+ })
83
+ })
84
+ }
85
+
86
+ private enqueueWrite(op: () => Promise<void>): Promise<void> {
87
+ const next = this.writeChain.then(op, op)
88
+ this.writeChain = next.catch(() => {})
89
+ return next
90
+ }
91
+ }
92
+
93
+ function parseBlock(value: unknown): SlackChannelBlock {
94
+ if (value === undefined) return EMPTY_BLOCK
95
+ return slackChannelBlockSchema.parse(value)
96
+ }
@@ -1,3 +1,5 @@
1
+ import { delimiter, join } from 'node:path'
2
+
1
3
  import {
2
4
  createSessionWithDispose,
3
5
  renderTurnTimeAnchor,
@@ -476,6 +478,24 @@ function resolveSessionIdForOrigin(origin: SessionOrigin): string {
476
478
  // hanging visibly past user expectations.
477
479
  const EXEC_ABORT_GRACE_MS = 5_000
478
480
 
481
+ // Prepend the agent folder's node_modules/.bin to PATH so a documented
482
+ // bare-command call like `ctx.exec`agent-webex auth status`` resolves.
483
+ // Without it the call exits 127: the container PATH excludes
484
+ // node_modules/.bin, so locally-installed package bins (the agent-* CLI
485
+ // surface) don't resolve while globals (git, bunx) do. Prepend matches
486
+ // npm/bun run-script semantics (local bins shadow globals); ctx.exec is a
487
+ // trusted plugin-author surface, so shadowing is acceptable. Per-spawn env
488
+ // (never mutate process.env); skip the trailing delimiter on empty PATH so
489
+ // no empty segment implies a current-directory lookup.
490
+ function buildExecEnv(cwd: string): Record<string, string | undefined> {
491
+ const localBin = join(cwd, 'node_modules', '.bin')
492
+ const currentPath = process.env.PATH
493
+ return {
494
+ ...process.env,
495
+ PATH: currentPath ? `${localBin}${delimiter}${currentPath}` : localBin,
496
+ }
497
+ }
498
+
479
499
  export async function runExecForCommand(
480
500
  strings: TemplateStringsArray,
481
501
  values: readonly unknown[],
@@ -501,6 +521,7 @@ export async function runExecForCommand(
501
521
  const proc = Bun.spawn({
502
522
  cmd: ['sh', '-c', cmd],
503
523
  cwd: opts.cwd,
524
+ env: buildExecEnv(opts.cwd),
504
525
  stdout: 'pipe',
505
526
  stderr: 'pipe',
506
527
  detached: true,