typeclaw 0.42.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 (59) hide show
  1. package/package.json +3 -3
  2. package/src/agent/adaptive-thinking-compat.ts +79 -0
  3. package/src/agent/index.ts +79 -101
  4. package/src/agent/plugin-tools.ts +77 -130
  5. package/src/agent/provider-error.ts +37 -0
  6. package/src/agent/reload-tool.ts +1 -1
  7. package/src/agent/subagents.ts +2 -2
  8. package/src/agent/system-prompt.ts +19 -5
  9. package/src/agent/tool-result-budget.ts +1 -1
  10. package/src/agent/tools/stream-snapshot.ts +8 -3
  11. package/src/bundled-plugins/agent-browser/index.ts +18 -8
  12. package/src/bundled-plugins/memory/README.md +4 -1
  13. package/src/bundled-plugins/memory/belief-sentence.ts +50 -0
  14. package/src/bundled-plugins/memory/dreaming.ts +13 -6
  15. package/src/bundled-plugins/memory/load-memory.ts +22 -2
  16. package/src/bundled-plugins/memory/memory-logger.ts +1 -1
  17. package/src/capabilities/control-plane.ts +24 -0
  18. package/src/capabilities/index.ts +2 -0
  19. package/src/capabilities/runtime.ts +30 -0
  20. package/src/channels/manager.ts +41 -47
  21. package/src/channels/router.ts +11 -2
  22. package/src/cli/channel.ts +49 -12
  23. package/src/cli/hostd.ts +4 -3
  24. package/src/cli/logs.ts +2 -2
  25. package/src/cli/restart.ts +5 -3
  26. package/src/cli/shell.ts +2 -2
  27. package/src/cli/start.ts +2 -2
  28. package/src/cli/status.ts +2 -2
  29. package/src/cli/stop.ts +2 -2
  30. package/src/compose/restart.ts +4 -3
  31. package/src/compose/start.ts +2 -2
  32. package/src/compose/stop.ts +2 -2
  33. package/src/config/config.ts +10 -1
  34. package/src/config/providers.ts +61 -9
  35. package/src/container/controller.ts +104 -0
  36. package/src/container/index.ts +9 -0
  37. package/src/init/index.ts +1 -1
  38. package/src/init/instagram-auth.ts +189 -34
  39. package/src/inspect/logs-item.ts +2 -2
  40. package/src/mcp/client.ts +1 -1
  41. package/src/run/index.ts +21 -9
  42. package/src/run/{codex-fetch-observer.ts → llm-fetch-observer.ts} +182 -82
  43. package/src/sandbox/availability.ts +2 -2
  44. package/src/sandbox/build.ts +1 -1
  45. package/src/sandbox/policy.ts +2 -2
  46. package/src/secrets/discord-store.ts +4 -13
  47. package/src/secrets/instagram-store.ts +4 -13
  48. package/src/secrets/kakao-store.ts +4 -13
  49. package/src/secrets/line-store.ts +4 -13
  50. package/src/secrets/secrets-provider.ts +86 -0
  51. package/src/secrets/slack-store.ts +4 -13
  52. package/src/secrets/webex-store.ts +4 -13
  53. package/src/server/index.ts +2 -2
  54. package/src/skills/typeclaw-channel-instagram/SKILL.md +1 -1
  55. package/src/skills/typeclaw-config/SKILL.md +5 -3
  56. package/src/skills/typeclaw-plugins/SKILL.md +1 -1
  57. package/src/stream/broker.ts +4 -3
  58. package/src/stream/types.ts +1 -0
  59. package/typeclaw.schema.json +14 -0
@@ -1,3 +1,4 @@
1
+ import { readFile, rm, writeFile } from 'node:fs/promises'
1
2
  import { join } from 'node:path'
2
3
 
3
4
  import {
@@ -11,10 +12,31 @@ import { SecretsInstagramCredentialStore } from '@/secrets/instagram-store'
11
12
 
12
13
  export type InstagramBootstrapStatus = { ok: true } | { ok: false; reason: string }
13
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
+
14
35
  export type InstagramLoginInput = {
15
36
  username: string
16
37
  password: string
17
38
  agentDir: string
39
+ callbacks?: InstagramLoginCallbacks
18
40
  client?: InstagramLoginClient
19
41
  credentialManager?: InstagramLoginCredentialManager
20
42
  }
@@ -27,8 +49,20 @@ export type InstagramAuthenticateResult = {
27
49
  challengePath?: string
28
50
  }
29
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.
30
61
  export type InstagramLoginClient = {
31
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>
32
66
  setSessionPath(path: string): void
33
67
  getUserId(): string | null
34
68
  }
@@ -63,37 +97,23 @@ export async function runInstagramBootstrap(input: InstagramLoginInput): Promise
63
97
  const paths = await manager.ensureAccountPaths(accountId)
64
98
  const client = input.client ?? buildInstagramClient(sdkManager)
65
99
  client.setSessionPath(paths.session_path)
66
- const auth = await client.authenticate(input.username, input.password)
67
- if (auth.requiresTwoFactor) return unsupportedTwoFactor()
68
- if (auth.challengeRequired) return unsupportedChallenge()
69
- const userId = auth.userId || client.getUserId()
70
- if (userId === null || userId === '') return { ok: false, reason: 'Instagram login did not authenticate' }
71
-
72
- const now = new Date().toISOString()
73
- await manager.setAccount({
74
- account_id: accountId,
75
- username: input.username,
76
- pk: userId,
77
- created_at: now,
78
- updated_at: now,
79
- })
80
- await manager.setCurrent(accountId)
81
-
82
- const sdkAccount = await manager.getAccount(accountId)
83
- if (sdkAccount === null) {
84
- return { ok: false, reason: 'Instagram login authenticated but did not persist SDK credentials' }
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
85
116
  }
86
- await store.setAccount({
87
- account_id: sdkAccount.account_id,
88
- username: sdkAccount.username,
89
- ...(sdkAccount.full_name !== undefined ? { full_name: sdkAccount.full_name } : {}),
90
- ...(sdkAccount.profile_pic_url !== undefined ? { profile_pic_url: sdkAccount.profile_pic_url } : {}),
91
- ...(sdkAccount.pk !== undefined ? { pk: sdkAccount.pk } : {}),
92
- created_at: sdkAccount.created_at,
93
- updated_at: sdkAccount.updated_at,
94
- })
95
- await store.setCurrentAccount(sdkAccount.account_id)
96
- return { ok: true }
97
117
  },
98
118
  )
99
119
  return result
@@ -102,23 +122,158 @@ export async function runInstagramBootstrap(input: InstagramLoginInput): Promise
102
122
  }
103
123
  }
104
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
+
105
260
  function buildInstagramClient(manager: InstagramCredentialManager): InstagramLoginClient {
106
261
  return new RealInstagramClient(manager) as unknown as InstagramLoginClient
107
262
  }
108
263
 
109
- function unsupportedTwoFactor(): InstagramBootstrapStatus {
264
+ function twoFactorUnavailable(): { ok: false; reason: string } {
110
265
  return {
111
266
  ok: false,
112
267
  reason:
113
- 'Instagram account requires 2FA/checkpoint, which is not yet supported by the typeclaw Instagram channel. Disable 2FA on a dedicated agent account, or complete the checkpoint in a browser first.',
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.',
114
269
  }
115
270
  }
116
271
 
117
- function unsupportedChallenge(): InstagramBootstrapStatus {
272
+ function challengeUnavailable(): { ok: false; reason: string } {
118
273
  return {
119
274
  ok: false,
120
275
  reason:
121
- 'Instagram account requires 2FA/checkpoint, which is not yet supported by the typeclaw Instagram channel. Disable 2FA on a dedicated agent account, or complete the checkpoint in a browser first.',
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.',
122
277
  }
123
278
  }
124
279
 
@@ -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
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),