typeclaw 0.41.0 → 0.42.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.
Files changed (91) hide show
  1. package/README.md +3 -2
  2. package/auth.schema.json +57 -0
  3. package/package.json +4 -4
  4. package/secrets.schema.json +57 -0
  5. package/src/agent/adaptive-thinking-compat.ts +79 -0
  6. package/src/agent/index.ts +89 -107
  7. package/src/agent/plugin-tools.ts +77 -130
  8. package/src/agent/provider-error.ts +37 -0
  9. package/src/agent/reload-tool.ts +1 -1
  10. package/src/agent/session-origin.ts +1 -0
  11. package/src/agent/subagents.ts +2 -2
  12. package/src/agent/system-prompt.ts +19 -5
  13. package/src/agent/tool-result-budget.ts +1 -1
  14. package/src/agent/tools/channel-history-render.ts +21 -0
  15. package/src/agent/tools/channel-history.ts +2 -17
  16. package/src/agent/tools/channel-read.ts +206 -0
  17. package/src/agent/tools/stream-snapshot.ts +8 -3
  18. package/src/bundled-plugins/agent-browser/index.ts +18 -8
  19. package/src/bundled-plugins/agent-browser/skills/agent-browser/SKILL.md +26 -2
  20. package/src/bundled-plugins/github-cli-auth/index.ts +25 -6
  21. package/src/bundled-plugins/memory/README.md +4 -1
  22. package/src/bundled-plugins/memory/belief-sentence.ts +50 -0
  23. package/src/bundled-plugins/memory/dreaming.ts +13 -6
  24. package/src/bundled-plugins/memory/load-memory.ts +22 -2
  25. package/src/bundled-plugins/memory/memory-logger.ts +1 -1
  26. package/src/bundled-plugins/memory/vector/hybrid.ts +73 -6
  27. package/src/bundled-plugins/memory/vector/passages.ts +2 -24
  28. package/src/bundled-plugins/memory/vector/truncation.ts +28 -0
  29. package/src/bundled-plugins/tool-result-cap/README.md +10 -1
  30. package/src/bundled-plugins/tool-result-cap/cap-result.ts +8 -0
  31. package/src/capabilities/control-plane.ts +24 -0
  32. package/src/capabilities/index.ts +2 -0
  33. package/src/capabilities/runtime.ts +30 -0
  34. package/src/channels/adapters/discord-bot.ts +112 -0
  35. package/src/channels/adapters/instagram-channel-resolver.ts +113 -0
  36. package/src/channels/adapters/instagram-classify.ts +69 -0
  37. package/src/channels/adapters/instagram-format.ts +8 -0
  38. package/src/channels/adapters/instagram.ts +346 -0
  39. package/src/channels/adapters/slack-bot.ts +147 -0
  40. package/src/channels/manager.ts +96 -44
  41. package/src/channels/router.ts +75 -2
  42. package/src/channels/schema.ts +5 -0
  43. package/src/channels/types.ts +46 -0
  44. package/src/cli/channel.ts +121 -9
  45. package/src/cli/hostd.ts +4 -3
  46. package/src/cli/init.ts +1 -0
  47. package/src/cli/logs.ts +2 -2
  48. package/src/cli/restart.ts +5 -3
  49. package/src/cli/shell.ts +2 -2
  50. package/src/cli/start.ts +2 -2
  51. package/src/cli/status.ts +37 -14
  52. package/src/cli/stop.ts +2 -2
  53. package/src/compose/restart.ts +4 -3
  54. package/src/compose/start.ts +2 -2
  55. package/src/compose/stop.ts +2 -2
  56. package/src/config/channels-mutation.ts +2 -1
  57. package/src/config/config.ts +10 -1
  58. package/src/config/providers.ts +61 -9
  59. package/src/container/controller.ts +104 -0
  60. package/src/container/index.ts +9 -0
  61. package/src/doctor/channel-checks.ts +28 -0
  62. package/src/hostd/daemon.ts +11 -7
  63. package/src/hostd/protocol.ts +2 -0
  64. package/src/init/index.ts +51 -2
  65. package/src/init/instagram-auth.ts +289 -0
  66. package/src/init/run-owner-claim.ts +1 -0
  67. package/src/inspect/logs-item.ts +2 -2
  68. package/src/mcp/client.ts +1 -1
  69. package/src/permissions/match-rule.ts +1 -1
  70. package/src/permissions/resolve.ts +1 -0
  71. package/src/role-claim/match-rule.ts +2 -1
  72. package/src/run/index.ts +21 -9
  73. package/src/run/{codex-fetch-observer.ts → llm-fetch-observer.ts} +182 -82
  74. package/src/sandbox/availability.ts +2 -2
  75. package/src/sandbox/build.ts +1 -1
  76. package/src/sandbox/policy.ts +2 -2
  77. package/src/secrets/discord-store.ts +4 -13
  78. package/src/secrets/instagram-store.ts +103 -0
  79. package/src/secrets/kakao-store.ts +4 -13
  80. package/src/secrets/line-store.ts +4 -13
  81. package/src/secrets/schema.ts +18 -0
  82. package/src/secrets/secrets-provider.ts +86 -0
  83. package/src/secrets/slack-store.ts +4 -13
  84. package/src/secrets/webex-store.ts +4 -13
  85. package/src/server/index.ts +2 -2
  86. package/src/skills/typeclaw-channel-instagram/SKILL.md +30 -0
  87. package/src/skills/typeclaw-config/SKILL.md +5 -3
  88. package/src/skills/typeclaw-plugins/SKILL.md +1 -1
  89. package/src/stream/broker.ts +4 -3
  90. package/src/stream/types.ts +1 -0
  91. package/typeclaw.schema.json +175 -1
@@ -0,0 +1,289 @@
1
+ import { readFile, rm, writeFile } from 'node:fs/promises'
2
+ import { join } from 'node:path'
3
+
4
+ import {
5
+ createAccountId,
6
+ InstagramClient as RealInstagramClient,
7
+ InstagramCredentialManager,
8
+ type InstagramAccount,
9
+ } from 'agent-messenger/instagram'
10
+
11
+ import { SecretsInstagramCredentialStore } from '@/secrets/instagram-store'
12
+
13
+ export type InstagramBootstrapStatus = { ok: true } | { ok: false; reason: string }
14
+
15
+ // Instagram gates a fresh login behind one of two interactive second factors:
16
+ // a 2FA code (authenticator app / SMS the account already has enabled) or a
17
+ // checkpoint challenge (a suspicious-login verification code sent to email or
18
+ // SMS). Both require a human to read a code off a device, so they only work
19
+ // with a TTY. The callbacks return the entered code, or `null` when the user
20
+ // aborts / no interactive prompt is available (cron/container re-auth) — in
21
+ // which case the bootstrap fails loudly instead of hanging.
22
+ export type InstagramLoginCallbacks = {
23
+ onTwoFactorCode?: (info: InstagramTwoFactorPrompt) => Promise<string | null>
24
+ onChallengeCode?: (info: InstagramChallengePrompt) => Promise<InstagramChallengeResponse | null>
25
+ }
26
+
27
+ export type InstagramTwoFactorPrompt = { username: string }
28
+
29
+ export type InstagramChallengePrompt = { contactPoint: string; method: InstagramChallengeMethod }
30
+
31
+ export type InstagramChallengeMethod = 'email' | 'sms'
32
+
33
+ export type InstagramChallengeResponse = { code: string }
34
+
35
+ export type InstagramLoginInput = {
36
+ username: string
37
+ password: string
38
+ agentDir: string
39
+ callbacks?: InstagramLoginCallbacks
40
+ client?: InstagramLoginClient
41
+ credentialManager?: InstagramLoginCredentialManager
42
+ }
43
+
44
+ export type InstagramAuthenticateResult = {
45
+ userId: string
46
+ requiresTwoFactor?: boolean
47
+ twoFactorInfo?: Record<string, unknown>
48
+ challengeRequired?: boolean
49
+ challengePath?: string
50
+ }
51
+
52
+ export type InstagramTwoFactorResult = { userId: string }
53
+
54
+ export type InstagramChallengeSendResult = { contactPoint: string; stepName: string }
55
+
56
+ export type InstagramChallengeSubmitResult = { userId: string }
57
+
58
+ // Structural subset of the upstream InstagramClient the bootstrap drives.
59
+ // Declared here (rather than relying on the SDK class) so tests can inject a
60
+ // fake without standing up the real HTTP client, mirroring the LINE adapter.
61
+ export type InstagramLoginClient = {
62
+ authenticate(username: string, password: string): Promise<InstagramAuthenticateResult>
63
+ twoFactorLogin(username: string, code: string, twoFactorIdentifier: string): Promise<InstagramTwoFactorResult>
64
+ challengeSendCode(apiPath: string, method?: InstagramChallengeMethod): Promise<InstagramChallengeSendResult>
65
+ challengeSubmitCode(apiPath: string, code: string): Promise<InstagramChallengeSubmitResult>
66
+ setSessionPath(path: string): void
67
+ getUserId(): string | null
68
+ }
69
+
70
+ export type InstagramLoginCredentialManager = {
71
+ ensureAccountPaths(accountId: string): Promise<{ session_path: string }>
72
+ getAccount(accountId?: string): Promise<InstagramAccount | null>
73
+ setAccount(account: InstagramAccount): Promise<void>
74
+ setCurrent(accountId: string): Promise<boolean>
75
+ }
76
+
77
+ export function instagramSecretsPath(agentDir: string): string {
78
+ return join(agentDir, 'secrets.json')
79
+ }
80
+
81
+ export function instagramConfigDir(agentDir: string): string {
82
+ return join(agentDir, 'workspace', '.agent-messenger')
83
+ }
84
+
85
+ export async function runInstagramBootstrap(input: InstagramLoginInput): Promise<InstagramBootstrapStatus> {
86
+ try {
87
+ const store = new SecretsInstagramCredentialStore({
88
+ mode: 'host',
89
+ secretsPath: instagramSecretsPath(input.agentDir),
90
+ })
91
+ const result = await withInstagramConfigDir(
92
+ instagramConfigDir(input.agentDir),
93
+ async (): Promise<InstagramBootstrapStatus> => {
94
+ const sdkManager = new InstagramCredentialManager()
95
+ const manager = input.credentialManager ?? sdkManager
96
+ const accountId = createAccountId(input.username)
97
+ const paths = await manager.ensureAccountPaths(accountId)
98
+ const client = input.client ?? buildInstagramClient(sdkManager)
99
+ client.setSessionPath(paths.session_path)
100
+
101
+ // A checkpoint login persists a partial SDK session (challenge_path +
102
+ // cookies) inside authenticate(), before we know whether the operator
103
+ // can finish it. Snapshot the file up front and restore it on any
104
+ // failure — whether completeLogin returns a non-ok status (fail-closed
105
+ // / cancelled) or throws (e.g. challengeSubmitCode rejecting a wrong
106
+ // code) — so a half-written session is never left behind. Rethrow so
107
+ // the outer catch reports the error.
108
+ const sessionSnapshot = await snapshotSessionFile(paths.session_path)
109
+ try {
110
+ const result = await completeLogin({ store, manager, client, accountId, input })
111
+ if (!result.ok) await restoreSessionFile(paths.session_path, sessionSnapshot)
112
+ return result
113
+ } catch (err) {
114
+ await restoreSessionFile(paths.session_path, sessionSnapshot)
115
+ throw err
116
+ }
117
+ },
118
+ )
119
+ return result
120
+ } catch (err) {
121
+ return { ok: false, reason: err instanceof Error ? err.message : String(err) }
122
+ }
123
+ }
124
+
125
+ type ResolvedUserId = { ok: true; userId: string } | { ok: false; reason: string }
126
+
127
+ async function completeLogin(args: {
128
+ store: SecretsInstagramCredentialStore
129
+ manager: InstagramLoginCredentialManager
130
+ client: InstagramLoginClient
131
+ accountId: string
132
+ input: InstagramLoginInput
133
+ }): Promise<InstagramBootstrapStatus> {
134
+ const { store, manager, client, accountId, input } = args
135
+ const resolved = await resolveUserId(client, input)
136
+ if (!resolved.ok) return resolved
137
+ const userId = resolved.userId || client.getUserId()
138
+ if (userId === null || userId === '') return { ok: false, reason: 'Instagram login did not authenticate' }
139
+ return persistAccount({ store, manager, accountId, username: input.username, userId })
140
+ }
141
+
142
+ // Drives the authenticate → (optional 2FA / challenge) → userId path. Each
143
+ // second factor is completed in place so a successful code entry continues to
144
+ // the same persist step a clean login would, and an aborted / unsupported
145
+ // prompt fails loudly rather than leaving a half-written session.
146
+ async function resolveUserId(client: InstagramLoginClient, input: InstagramLoginInput): Promise<ResolvedUserId> {
147
+ const auth = await client.authenticate(input.username, input.password)
148
+ if (auth.requiresTwoFactor) return completeTwoFactor(client, input, auth)
149
+ if (auth.challengeRequired) return completeChallenge(client, input, auth)
150
+ return { ok: true, userId: auth.userId }
151
+ }
152
+
153
+ async function completeTwoFactor(
154
+ client: InstagramLoginClient,
155
+ input: InstagramLoginInput,
156
+ auth: InstagramAuthenticateResult,
157
+ ): Promise<ResolvedUserId> {
158
+ const identifier = readTwoFactorIdentifier(auth.twoFactorInfo)
159
+ if (identifier === null) {
160
+ return { ok: false, reason: 'Instagram requested 2FA but did not return a two-factor identifier.' }
161
+ }
162
+ const prompt = input.callbacks?.onTwoFactorCode
163
+ if (prompt === undefined) return twoFactorUnavailable()
164
+ const code = await prompt({ username: input.username })
165
+ if (code === null || code.trim() === '') {
166
+ return { ok: false, reason: 'Instagram 2FA was cancelled — no verification code was entered.' }
167
+ }
168
+ const result = await client.twoFactorLogin(input.username, code.trim(), identifier)
169
+ return { ok: true, userId: result.userId }
170
+ }
171
+
172
+ async function completeChallenge(
173
+ client: InstagramLoginClient,
174
+ input: InstagramLoginInput,
175
+ auth: InstagramAuthenticateResult,
176
+ ): Promise<ResolvedUserId> {
177
+ const path = auth.challengePath
178
+ if (path === undefined || path === '') {
179
+ return { ok: false, reason: 'Instagram requested a checkpoint but did not return a challenge path.' }
180
+ }
181
+ const prompt = input.callbacks?.onChallengeCode
182
+ if (prompt === undefined) return challengeUnavailable()
183
+
184
+ // Instagram sends the verification code to email by default; SMS is only an
185
+ // option when the account has a verified phone. Send-then-prompt keeps the
186
+ // code delivery inside the interactive window so a stale contact point can't
187
+ // leave the user waiting on a code that was never dispatched.
188
+ const method: InstagramChallengeMethod = 'email'
189
+ const sent = await client.challengeSendCode(path, method)
190
+ const response = await prompt({ contactPoint: sent.contactPoint, method })
191
+ if (response === null || response.code.trim() === '') {
192
+ return { ok: false, reason: 'Instagram checkpoint was cancelled — no verification code was entered.' }
193
+ }
194
+ const result = await client.challengeSubmitCode(path, response.code.trim())
195
+ return { ok: true, userId: result.userId }
196
+ }
197
+
198
+ async function persistAccount(args: {
199
+ store: SecretsInstagramCredentialStore
200
+ manager: InstagramLoginCredentialManager
201
+ accountId: string
202
+ username: string
203
+ userId: string
204
+ }): Promise<InstagramBootstrapStatus> {
205
+ const { store, manager, accountId, username, userId } = args
206
+ const now = new Date().toISOString()
207
+ await manager.setAccount({
208
+ account_id: accountId,
209
+ username,
210
+ pk: userId,
211
+ created_at: now,
212
+ updated_at: now,
213
+ })
214
+ await manager.setCurrent(accountId)
215
+
216
+ const sdkAccount = await manager.getAccount(accountId)
217
+ if (sdkAccount === null) {
218
+ return { ok: false, reason: 'Instagram login authenticated but did not persist SDK credentials' }
219
+ }
220
+ await store.setAccount({
221
+ account_id: sdkAccount.account_id,
222
+ username: sdkAccount.username,
223
+ ...(sdkAccount.full_name !== undefined ? { full_name: sdkAccount.full_name } : {}),
224
+ ...(sdkAccount.profile_pic_url !== undefined ? { profile_pic_url: sdkAccount.profile_pic_url } : {}),
225
+ ...(sdkAccount.pk !== undefined ? { pk: sdkAccount.pk } : {}),
226
+ created_at: sdkAccount.created_at,
227
+ updated_at: sdkAccount.updated_at,
228
+ })
229
+ await store.setCurrentAccount(sdkAccount.account_id)
230
+ return { ok: true }
231
+ }
232
+
233
+ // The SDK returns `two_factor_info.two_factor_identifier`; it's the opaque
234
+ // token `twoFactorLogin` must echo back. Read defensively — a shape drift
235
+ // surfaces as a clear error instead of an SDK-internal crash.
236
+ function readTwoFactorIdentifier(info: Record<string, unknown> | undefined): string | null {
237
+ const identifier = info?.['two_factor_identifier']
238
+ return typeof identifier === 'string' && identifier !== '' ? identifier : null
239
+ }
240
+
241
+ async function snapshotSessionFile(path: string): Promise<string | null> {
242
+ try {
243
+ return await readFile(path, 'utf8')
244
+ } catch {
245
+ return null
246
+ }
247
+ }
248
+
249
+ // Restore the pre-login state: rewrite the prior contents, or delete the file
250
+ // when no session existed before, so a failed second factor leaves the session
251
+ // path exactly as it was found.
252
+ async function restoreSessionFile(path: string, snapshot: string | null): Promise<void> {
253
+ if (snapshot === null) {
254
+ await rm(path, { force: true })
255
+ return
256
+ }
257
+ await writeFile(path, snapshot, { mode: 0o600 })
258
+ }
259
+
260
+ function buildInstagramClient(manager: InstagramCredentialManager): InstagramLoginClient {
261
+ return new RealInstagramClient(manager) as unknown as InstagramLoginClient
262
+ }
263
+
264
+ function twoFactorUnavailable(): { ok: false; reason: string } {
265
+ return {
266
+ ok: false,
267
+ reason:
268
+ 'Instagram account requires a 2FA code, but no interactive prompt is available. Run `typeclaw channel add instagram` (or `typeclaw channel reauth instagram`) from a terminal to enter the code.',
269
+ }
270
+ }
271
+
272
+ function challengeUnavailable(): { ok: false; reason: string } {
273
+ return {
274
+ ok: false,
275
+ reason:
276
+ 'Instagram account requires a checkpoint verification code, but no interactive prompt is available. Run `typeclaw channel add instagram` (or `typeclaw channel reauth instagram`) from a terminal to enter the code.',
277
+ }
278
+ }
279
+
280
+ async function withInstagramConfigDir<T>(dir: string, fn: () => Promise<T>): Promise<T> {
281
+ const previous = process.env.AGENT_MESSENGER_CONFIG_DIR
282
+ process.env.AGENT_MESSENGER_CONFIG_DIR = dir
283
+ try {
284
+ return await fn()
285
+ } finally {
286
+ if (previous === undefined) delete process.env.AGENT_MESSENGER_CONFIG_DIR
287
+ else process.env.AGENT_MESSENGER_CONFIG_DIR = previous
288
+ }
289
+ }
@@ -14,6 +14,7 @@ const CHANNEL_LABELS: Record<ChannelKind, string> = {
14
14
  'telegram-bot': 'Telegram',
15
15
  webex: 'Webex (User)',
16
16
  'webex-bot': 'Webex',
17
+ instagram: 'Instagram',
17
18
  line: 'LINE',
18
19
  kakaotalk: 'KakaoTalk',
19
20
  }
@@ -1,4 +1,4 @@
1
- import { logs } from '@/container'
1
+ import { resolveController } from '@/container'
2
2
 
3
3
  export type StreamLogsOptions = {
4
4
  cwd: string
@@ -28,7 +28,7 @@ export async function streamLogs(opts: StreamLogsOptions): Promise<StreamLogsRes
28
28
  const out = lineSink(opts.stdout)
29
29
  const err = lineSink(opts.stderr)
30
30
 
31
- const result = await logs({
31
+ const result = await resolveController().logs({
32
32
  cwd: opts.cwd,
33
33
  follow: true,
34
34
  out,
package/src/mcp/client.ts CHANGED
@@ -206,7 +206,7 @@ function attachCloseCause(cause: unknown, closeCause: unknown): void {
206
206
 
207
207
  // A stdio MCP server is a child process the agent spawns, so it must NOT
208
208
  // inherit the full parent environment: that env holds unrelated credentials
209
- // (FIREWORKS_API_KEY, GH_TOKEN, channel tokens) and inheriting them leaks every
209
+ // (OPENAI_API_KEY, GH_TOKEN, channel tokens) and inheriting them leaks every
210
210
  // secret to every server. We start from a minimal allowlist needed to spawn and
211
211
  // run a process (PATH/HOME to launch npx/bunx, locale + temp for correctness),
212
212
  // then overlay only the secrets the server explicitly declares. This mirrors
@@ -16,7 +16,7 @@
16
16
  // error messages with typo suggestions; a single big regex would only ever
17
17
  // say "didn't match".
18
18
 
19
- export const PLATFORMS = ['slack', 'discord', 'telegram', 'webex', 'line', 'kakao', 'github'] as const
19
+ export const PLATFORMS = ['slack', 'discord', 'telegram', 'webex', 'instagram', 'line', 'kakao', 'github'] as const
20
20
  export type Platform = (typeof PLATFORMS)[number]
21
21
 
22
22
  const SUBAGENT_NAME = /^[a-z][a-z0-9-]*$/
@@ -9,6 +9,7 @@ const ADAPTER_TO_PLATFORM: Record<AdapterId, Platform> = {
9
9
  discord: 'discord',
10
10
  'discord-bot': 'discord',
11
11
  github: 'github',
12
+ instagram: 'instagram',
12
13
  'telegram-bot': 'telegram',
13
14
  webex: 'webex',
14
15
  'webex-bot': 'webex',
@@ -27,13 +27,14 @@ export type PartialChannelOrigin = {
27
27
 
28
28
  const ADAPTER_TO_PLATFORM: Record<
29
29
  ChannelKey['adapter'],
30
- 'slack' | 'discord' | 'telegram' | 'webex' | 'kakao' | 'line' | 'github'
30
+ 'slack' | 'discord' | 'telegram' | 'webex' | 'kakao' | 'line' | 'github' | 'instagram'
31
31
  > = {
32
32
  slack: 'slack',
33
33
  'slack-bot': 'slack',
34
34
  discord: 'discord',
35
35
  'discord-bot': 'discord',
36
36
  github: 'github',
37
+ instagram: 'instagram',
37
38
  'telegram-bot': 'telegram',
38
39
  webex: 'webex',
39
40
  'webex-bot': 'webex',
package/src/run/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { join } from 'node:path'
2
+
1
3
  import { SessionManager } from '@mariozechner/pi-coding-agent'
2
4
 
3
5
  import { createSession, createSessionWithDispose } from '@/agent'
@@ -24,6 +26,7 @@ import { clearTodosForOrigin, markRestartAbortPendingForOrigin } from '@/agent/t
24
26
  import { embed, warmEmbedder } from '@/bundled-plugins/memory/vector/embedder'
25
27
  import { buildStartupVectorIndex } from '@/bundled-plugins/memory/vector/startup'
26
28
  import { resolveCapOptionsFromConfig } from '@/bundled-plugins/tool-result-cap'
29
+ import { createRuntimeCapabilities, type RuntimeCapabilities } from '@/capabilities'
27
30
  import {
28
31
  createChannelManager,
29
32
  createChannelsReloadable,
@@ -79,8 +82,8 @@ import { createTunnelManager, type TunnelManager, type TunnelManagerOptions } fr
79
82
 
80
83
  import { BUNDLED_PLUGINS } from './bundled-plugins'
81
84
  import { buildChannelSessionFactory } from './channel-session-factory'
82
- import { installCodexFetchObserver } from './codex-fetch-observer'
83
85
  import { installFatalGuard } from './fatal-guard'
86
+ import { installLlmFetchObserver } from './llm-fetch-observer'
84
87
  import { createPluginRuntime, type PluginRuntime, type PluginSubagentEntry } from './plugin-runtime'
85
88
 
86
89
  type BunServer = ReturnType<Server['start']>
@@ -109,6 +112,10 @@ export type StartAgentOptions = {
109
112
  stream?: Stream
110
113
  createChannelManager?: ChannelManagerFactory
111
114
  createTunnelManager?: TunnelManagerFactory
115
+ // The container-stage capability bag. Defaults to createRuntimeCapabilities()
116
+ // over the real env + <cwd>/secrets.json. Injectable so tests can supply a
117
+ // fake secrets provider without touching process.env.
118
+ caps?: RuntimeCapabilities
112
119
  }
113
120
 
114
121
  export type StartAgentResult = {
@@ -126,7 +133,7 @@ export type StartAgentResult = {
126
133
  }
127
134
 
128
135
  // Owns boot-failure cleanup for everything installed before `startAgentRuntime`
129
- // returns a `StartAgentResult`. `installCodexFetchObserver`/`installFatalGuard`
136
+ // returns a `StartAgentResult`. `installLlmFetchObserver`/`installFatalGuard`
130
137
  // attach process-wide listeners, and `loadPlugins` opens plugin-lifetime
131
138
  // resources (the memory plugin's sqlite handle); if any boot step throws before
132
139
  // the result exists, the caller never receives `stop()`, so each of those would
@@ -168,18 +175,22 @@ async function startAgentRuntime(
168
175
  stream = createStream(),
169
176
  createChannelManager: createChannelManagerFor = createChannelManager,
170
177
  createTunnelManager: createTunnelManagerFor = createTunnelManager,
178
+ caps = createRuntimeCapabilities(process.env, join(cwd, 'secrets.json')),
171
179
  }: StartAgentOptions,
172
180
  registerBootCleanup: (cleanup: () => void | Promise<void>) => void,
173
181
  ): Promise<StartAgentResult> {
174
182
  const reloadRegistry = new ReloadRegistry()
175
183
 
176
184
  // Wrap globalThis.fetch BEFORE any plugin/session/manager construction so
177
- // every Codex Responses call from anywhere in the container is observed.
178
- // Logs one `[codex-fetch]` line per matched request with phase timings;
179
- // never aborts, never retries — purely passive instrumentation while we
180
- // investigate the recurring multi-minute Codex stalls (see issue #394).
181
- // Opt out with TYPECLAW_CODEX_FETCH_OBSERVER=off.
182
- const uninstallCodexFetchObserver = installCodexFetchObserver()
185
+ // every LLM provider stream from anywhere in the container is guarded. Logs
186
+ // one `[llm-fetch]` line per matched request with phase timings and applies
187
+ // TTFB/idle/overall deadlines that abort a stalled stream — turning a silent
188
+ // infinite hang (e.g. a rate-limited proxy holding the SSE connection open
189
+ // with zero bytes) into a retryable error the fallback path can surface.
190
+ // Covers Codex, Anthropic (`/v1/messages`), and OpenAI-compatible endpoints
191
+ // regardless of host, since base URLs are user-configured. Opt out with
192
+ // TYPECLAW_LLM_FETCH_OBSERVER=off.
193
+ const uninstallLlmFetchObserver = installLlmFetchObserver()
183
194
 
184
195
  // The host CLI sets TYPECLAW_CONTAINER_NAME when it `docker run`s us. When
185
196
  // running outside a typeclaw container (tests, ad-hoc `bun run typeclaw run`
@@ -217,7 +228,7 @@ async function startAgentRuntime(
217
228
  const disposeProcessGlobals = (): void => {
218
229
  if (processGlobalsDisposed) return
219
230
  processGlobalsDisposed = true
220
- uninstallCodexFetchObserver()
231
+ uninstallLlmFetchObserver()
221
232
  fatalGuard.dispose()
222
233
  // onSigterm is defined later in this scope but only ever invoked after init;
223
234
  // removing it here keeps a restarted startAgent() from stacking listeners.
@@ -365,6 +376,7 @@ async function startAgentRuntime(
365
376
 
366
377
  const channelManager = createChannelManagerFor({
367
378
  agentDir: cwd,
379
+ secretsProvider: caps.secrets,
368
380
  channelsConfigRef: () => getConfig().channels,
369
381
  aliasesRef: () => getConfig().alias,
370
382
  tunnelUrlForChannel: (name) => resolveTunnelUrlForChannel(name, tunnelManager),