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.
Files changed (163) hide show
  1. package/README.md +1 -1
  2. package/package.json +3 -2
  3. package/src/agent/git-nudge.ts +2 -1
  4. package/src/agent/index.ts +44 -30
  5. package/src/agent/live-subagents.ts +18 -0
  6. package/src/agent/multimodal/index.ts +1 -1
  7. package/src/agent/multimodal/look-at.ts +78 -44
  8. package/src/agent/multimodal/looker.ts +161 -39
  9. package/src/agent/network/response-body.ts +26 -0
  10. package/src/agent/network/safe-http.ts +207 -0
  11. package/src/agent/plugin-tools.ts +377 -157
  12. package/src/agent/provider-error.ts +52 -1
  13. package/src/agent/restart/index.ts +61 -40
  14. package/src/agent/restart-handoff/index.ts +73 -4
  15. package/src/agent/reviewer-bash-policy.ts +42 -1
  16. package/src/agent/session-origin.ts +2 -0
  17. package/src/agent/subagent-drain.ts +117 -7
  18. package/src/agent/subagents.ts +49 -16
  19. package/src/agent/throttle-circuit.ts +25 -5
  20. package/src/agent/tool-error-remediation.ts +72 -0
  21. package/src/agent/tool-file-safety.ts +1310 -0
  22. package/src/agent/tools/channel-fetch-attachment.ts +10 -3
  23. package/src/agent/tools/ddg.ts +16 -2
  24. package/src/agent/tools/post-github-review.ts +199 -0
  25. package/src/agent/tools/spawn-subagent.ts +21 -18
  26. package/src/agent/tools/webfetch/fetch.ts +163 -183
  27. package/src/agent/tools/webfetch/tool.ts +2 -3
  28. package/src/agent/tools/websearch.ts +23 -2
  29. package/src/agent/tools-without-local-file-operands.ts +30 -0
  30. package/src/agent/turn-runner.ts +86 -6
  31. package/src/bundled-plugins/backup/README.md +23 -10
  32. package/src/bundled-plugins/backup/git-auth.ts +6 -4
  33. package/src/bundled-plugins/backup/index.ts +85 -44
  34. package/src/bundled-plugins/backup/runner.ts +85 -1
  35. package/src/bundled-plugins/doc-render/render.ts +93 -12
  36. package/src/bundled-plugins/doc-render/skills/typeclaw-render-pdf/SKILL.md +13 -13
  37. package/src/bundled-plugins/github-cli-auth/effective-approval.ts +16 -6
  38. package/src/bundled-plugins/github-cli-auth/gh-command.ts +555 -15
  39. package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
  40. package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
  41. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
  42. package/src/bundled-plugins/github-cli-auth/index.ts +207 -132
  43. package/src/bundled-plugins/github-cli-auth/review-checkout.ts +138 -0
  44. package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
  45. package/src/bundled-plugins/guard/policies/uncommitted-changes.ts +2 -1
  46. package/src/bundled-plugins/memory/README.md +32 -11
  47. package/src/bundled-plugins/memory/append-tool.ts +15 -8
  48. package/src/bundled-plugins/memory/citations.ts +22 -9
  49. package/src/bundled-plugins/memory/dreaming.ts +13 -8
  50. package/src/bundled-plugins/memory/find-entry-tool.ts +4 -0
  51. package/src/bundled-plugins/memory/index.ts +0 -4
  52. package/src/bundled-plugins/memory/load-memory.ts +8 -3
  53. package/src/bundled-plugins/memory/provenance-index.ts +717 -0
  54. package/src/bundled-plugins/memory/provenance-sanitize.ts +40 -0
  55. package/src/bundled-plugins/memory/references/store-reference-tool.ts +4 -0
  56. package/src/bundled-plugins/memory/search-tool.ts +163 -60
  57. package/src/bundled-plugins/memory/stream-events.ts +2 -0
  58. package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
  59. package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
  60. package/src/bundled-plugins/memory/vector/hybrid.ts +126 -13
  61. package/src/bundled-plugins/researcher/write-report.ts +8 -5
  62. package/src/bundled-plugins/reviewer/index.ts +2 -2
  63. package/src/bundled-plugins/reviewer/reviewer.ts +10 -3
  64. package/src/bundled-plugins/reviewer/skills/code-review.ts +19 -9
  65. package/src/bundled-plugins/scout/scout.ts +8 -0
  66. package/src/bundled-plugins/security/index.ts +4 -4
  67. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +1 -0
  68. package/src/bundled-plugins/security/policies/private-surface-read.ts +787 -36
  69. package/src/bundled-plugins/security/policies/secret-exfil-read.ts +1 -0
  70. package/src/bundled-plugins/security/policies/ssrf.ts +147 -42
  71. package/src/channels/adapters/discord-bot-channel-resolver.ts +26 -14
  72. package/src/channels/adapters/discord-bot-thread-room.ts +44 -12
  73. package/src/channels/adapters/discord-bot.ts +66 -6
  74. package/src/channels/adapters/discord-channel-resolver.ts +91 -23
  75. package/src/channels/adapters/discord-id.ts +4 -0
  76. package/src/channels/adapters/discord-resolver-cache.ts +41 -0
  77. package/src/channels/adapters/discord.ts +57 -7
  78. package/src/channels/adapters/github/auth-app.ts +41 -12
  79. package/src/channels/adapters/github/inbound.ts +11 -0
  80. package/src/channels/adapters/github/index.ts +71 -65
  81. package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
  82. package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
  83. package/src/channels/adapters/github/review-submitter.ts +302 -0
  84. package/src/channels/adapters/kakaotalk-channel-resolver.ts +5 -3
  85. package/src/channels/adapters/kakaotalk-fetch-attachment.ts +8 -4
  86. package/src/channels/adapters/kakaotalk-typing.ts +17 -15
  87. package/src/channels/adapters/slack-attachment-download.ts +71 -0
  88. package/src/channels/adapters/slack-bot-edit.ts +85 -20
  89. package/src/channels/adapters/slack-bot.ts +26 -10
  90. package/src/channels/adapters/slack-classify.ts +21 -2
  91. package/src/channels/adapters/slack-format.ts +245 -0
  92. package/src/channels/adapters/slack.ts +59 -7
  93. package/src/channels/adapters/telegram-bot.ts +18 -5
  94. package/src/channels/adapters/webex-bot.ts +8 -3
  95. package/src/channels/adapters/webex.ts +8 -3
  96. package/src/channels/fetch-attachment.ts +43 -0
  97. package/src/{bundled-plugins/github-cli-auth/approve-idempotency.ts → channels/github-review-verdict-coordinator.ts} +25 -1
  98. package/src/channels/github-token-bridge.ts +12 -6
  99. package/src/channels/manager.ts +9 -0
  100. package/src/channels/router.ts +248 -6
  101. package/src/channels/types.ts +28 -1
  102. package/src/cli/channel.ts +1 -1
  103. package/src/cli/init.ts +1 -1
  104. package/src/cli/model.ts +57 -7
  105. package/src/compose/stop.ts +17 -5
  106. package/src/config/config.ts +6 -6
  107. package/src/config/models-mutation.ts +13 -1
  108. package/src/config/providers.ts +176 -0
  109. package/src/container/docker-app.ts +42 -13
  110. package/src/container/start.ts +62 -10
  111. package/src/cron/bridge.ts +9 -2
  112. package/src/doctor/checks.ts +8 -3
  113. package/src/doctor/commit.ts +2 -1
  114. package/src/doctor/file-ownership.ts +52 -0
  115. package/src/dreams/git.ts +2 -1
  116. package/src/git/bound-gc-config.ts +82 -0
  117. package/src/git/hookless.ts +5 -0
  118. package/src/git/reconcile-ignored.ts +18 -5
  119. package/src/git/system-commit.ts +12 -6
  120. package/src/hostd/spawn.ts +133 -47
  121. package/src/init/dockerfile.ts +99 -5
  122. package/src/init/index.ts +17 -5
  123. package/src/init/models-dev.ts +4 -0
  124. package/src/init/validate-api-key.ts +2 -0
  125. package/src/inspect/replay.ts +21 -9
  126. package/src/inspect/session-list.ts +71 -33
  127. package/src/mcp/tools.ts +17 -3
  128. package/src/network/internal-destinations.ts +267 -0
  129. package/src/permissions/builtins.ts +3 -1
  130. package/src/plugin/context.ts +2 -0
  131. package/src/plugin/index.ts +1 -0
  132. package/src/plugin/manager.ts +4 -0
  133. package/src/plugin/types.ts +22 -0
  134. package/src/run/index.ts +40 -0
  135. package/src/run/llm-fetch-observer.ts +41 -4
  136. package/src/sandbox/build.ts +37 -15
  137. package/src/sandbox/canonical-secrets.ts +18 -0
  138. package/src/sandbox/env-exposure.ts +83 -0
  139. package/src/sandbox/errors.ts +13 -6
  140. package/src/sandbox/hidden-paths.ts +102 -44
  141. package/src/sandbox/index.ts +22 -4
  142. package/src/sandbox/package-install.ts +3 -27
  143. package/src/sandbox/policy.ts +16 -28
  144. package/src/sandbox/privileged-runtime.ts +256 -0
  145. package/src/sandbox/session-tmp.ts +8 -2
  146. package/src/sandbox/writable-zones.ts +204 -140
  147. package/src/secrets/index.ts +8 -0
  148. package/src/secrets/refresh-provider-oauth.ts +128 -0
  149. package/src/server/command-runner.ts +48 -4
  150. package/src/server/index.ts +4 -0
  151. package/src/skills/typeclaw-channel-github/SKILL.md +49 -56
  152. package/src/skills/typeclaw-claude-code/SKILL.md +24 -60
  153. package/src/skills/typeclaw-claude-code/references/auth-flow.md +17 -158
  154. package/src/skills/typeclaw-codex-cli/SKILL.md +19 -49
  155. package/src/skills/typeclaw-codex-cli/references/auth-flow.md +17 -154
  156. package/src/skills/typeclaw-config/SKILL.md +27 -11
  157. package/src/skills/typeclaw-config/references/recommended-mounts.md +24 -24
  158. package/src/skills/typeclaw-github-contributing/SKILL.md +23 -26
  159. package/src/skills/typeclaw-memory/SKILL.md +9 -3
  160. package/src/skills/typeclaw-permissions/SKILL.md +36 -19
  161. package/src/skills/typeclaw-plugins/SKILL.md +37 -0
  162. package/src/usage/scan.ts +11 -2
  163. package/typeclaw.schema.json +25 -0
@@ -1,5 +1,7 @@
1
1
  import { existsSync } from 'node:fs'
2
- import { open, readFile, unlink, writeFile } from 'node:fs/promises'
2
+ import { lstat, open, readFile, unlink, writeFile } from 'node:fs/promises'
3
+
4
+ import lockfile from 'proper-lockfile'
3
5
 
4
6
  import { isWindows } from '@/shared'
5
7
 
@@ -14,6 +16,9 @@ export type EnsureDaemonOptions = {
14
16
  // Test seam: tests inject a deterministic version probe + respawn so the
15
17
  // unit test can exercise the drift path without spawning a real daemon.
16
18
  expectedVersion?: string
19
+ // Test seam: parks execution inside the spawn lock, just before spawning, so a
20
+ // test can prove a concurrent caller cannot enter the critical section.
21
+ onSpawnEnter?: () => Promise<void>
17
22
  }
18
23
 
19
24
  export type EnsureDaemonResult =
@@ -23,6 +28,18 @@ export type EnsureDaemonResult =
23
28
  const DEFAULT_SPAWN_TIMEOUT_MS = 5_000
24
29
  const SHUTDOWN_TIMEOUT_MS = 5_000
25
30
  const POLL_INTERVAL_MS = 50
31
+ const EXIT_SETTLE_MS = 500
32
+ // proper-lockfile refreshes the held lock's mtime every `stale/2` ms, so a lock
33
+ // held across the full spawn (readiness poll + EXIT_SETTLE_MS grace) is never
34
+ // seen as stale by a contender — only a crashed holder, whose refresh timer
35
+ // died with it, is reclaimed. Mirrors the models/secrets locks' 30s ceiling.
36
+ const LOCK_STALE_MS = 30_000
37
+ const LOCK_RETRY_BACKOFF = {
38
+ factor: 1,
39
+ minTimeout: POLL_INTERVAL_MS,
40
+ maxTimeout: POLL_INTERVAL_MS,
41
+ randomize: false,
42
+ } as const
26
43
 
27
44
  export async function ensureDaemon(opts: EnsureDaemonOptions): Promise<EnsureDaemonResult> {
28
45
  if (await isDaemonReachable()) {
@@ -36,13 +53,13 @@ export async function ensureDaemon(opts: EnsureDaemonOptions): Promise<EnsureDae
36
53
  return { ok: false, reason: 'daemon version drifted but shutdown request did not complete' }
37
54
  }
38
55
  await ensureDirs()
39
- const respawn = await ensureDaemonWithRetry(opts, 1)
56
+ const respawn = await spawnUnderLock(opts)
40
57
  if (!respawn.ok) return respawn
41
58
  return { ...respawn, respawned: true }
42
59
  }
43
60
 
44
61
  await ensureDirs()
45
- const result = await ensureDaemonWithRetry(opts, 1)
62
+ const result = await spawnUnderLock(opts)
46
63
  if (!result.ok) return result
47
64
  return { ...result, respawned: false }
48
65
  }
@@ -90,19 +107,20 @@ async function requestShutdownAndWait(): Promise<boolean> {
90
107
 
91
108
  type SpawnAttemptResult = { ok: true; pid: number; spawned: boolean; httpPort: number } | { ok: false; reason: string }
92
109
 
93
- async function ensureDaemonWithRetry(opts: EnsureDaemonOptions, retriesLeft: number): Promise<SpawnAttemptResult> {
94
- const lock = await acquireLockOrWait(opts.spawnTimeoutMs ?? DEFAULT_SPAWN_TIMEOUT_MS)
95
- if (lock.kind === 'daemon-reachable') {
96
- const httpPort = await readHttpPort()
97
- if (httpPort === null) return { ok: false, reason: 'daemon did not report an HTTP control port' }
98
- return { ok: true, pid: await readPidQuiet(), spawned: false, httpPort }
99
- }
100
- if (lock.kind === 'stale-lock-cleared') {
101
- if (retriesLeft > 0) return ensureDaemonWithRetry(opts, retriesLeft - 1)
102
- return { ok: false, reason: 'stale lockfile cleared but retry budget exhausted' }
103
- }
104
- if (lock.kind === 'timeout') {
105
- return { ok: false, reason: lock.reason }
110
+ async function spawnUnderLock(opts: EnsureDaemonOptions): Promise<SpawnAttemptResult> {
111
+ const lock = await acquireSpawnLock(opts.spawnTimeoutMs ?? DEFAULT_SPAWN_TIMEOUT_MS)
112
+ if (lock.kind === 'contended') {
113
+ // Another caller holds the spawn lock. If a daemon came up meanwhile, reuse
114
+ // it; otherwise that caller is still mid-spawn, so back off without racing a
115
+ // second spawn. proper-lockfile reclaims a crashed holder's lock on its own
116
+ // (its mtime refresh timer dies with the process), so there's no lock to
117
+ // clear here — the ownership-unsafe manual clear is gone.
118
+ if (await isDaemonReachable()) {
119
+ const httpPort = await readHttpPort()
120
+ if (httpPort === null) return { ok: false, reason: 'daemon did not report an HTTP control port' }
121
+ return { ok: true, pid: await readPidQuiet(), spawned: false, httpPort }
122
+ }
123
+ return { ok: false, reason: 'another daemon spawn is in progress' }
106
124
  }
107
125
 
108
126
  try {
@@ -121,9 +139,10 @@ async function ensureDaemonWithRetry(opts: EnsureDaemonOptions, retriesLeft: num
121
139
  if (ready === null) return { ok: false, reason: 'daemon spawned but did not become reachable yet' }
122
140
  return { ok: true, pid: existingPid, spawned: false, httpPort: ready }
123
141
  }
142
+ await opts.onSpawnEnter?.()
124
143
  return await spawnDaemonDetached(opts)
125
144
  } finally {
126
- await releaseLock(lock.token)
145
+ await lock.release()
127
146
  }
128
147
  }
129
148
 
@@ -196,10 +215,18 @@ async function spawnDaemonDetached(opts: EnsureDaemonOptions): Promise<SpawnAtte
196
215
  // Timed out waiting for readiness. A still-running child is "slow-booting",
197
216
  // not "wedged" — killing it would throw away a daemon that's about to come up
198
217
  // and force every caller into a respawn loop. Only reap a child that already
199
- // EXITED (proc.exitCode !== null): that's a genuine failure with a dangling
200
- // pidfile to clean. A live-but-not-ready child is left running so the caller
201
- // can re-probe it (see registerWithDaemon's retry) — the next ensureDaemon()
202
- // fast-paths through isDaemonReachable() once its socket binds.
218
+ // EXITED: that's a genuine failure with a dangling pidfile to clean. A
219
+ // live-but-not-ready child is left running so the caller can re-probe it (see
220
+ // registerWithDaemon's retry) — the next ensureDaemon() fast-paths through
221
+ // isDaemonReachable() once its socket binds.
222
+ //
223
+ // `proc.exitCode` is a non-blocking snapshot, so a child that fails fast (e.g.
224
+ // a bad CLI entry) can still read as `null` here if the readiness deadline
225
+ // lands in the narrow window between the process exiting and Bun reaping it.
226
+ // Give it a bounded grace to settle by racing `proc.exited`; this makes the
227
+ // exited-vs-slow-booting classification deterministic instead of dependent on
228
+ // scheduler timing, without ever killing a still-live child.
229
+ if (proc.exitCode === null) await settleExit(proc, EXIT_SETTLE_MS)
203
230
  if (proc.exitCode !== null) {
204
231
  try {
205
232
  const raw = await readFile(pidfilePath(), 'utf8').catch(() => '')
@@ -210,40 +237,99 @@ async function spawnDaemonDetached(opts: EnsureDaemonOptions): Promise<SpawnAtte
210
237
  return { ok: false, reason: 'daemon spawned but did not become reachable yet' }
211
238
  }
212
239
 
213
- type LockToken = { path: string }
214
- type LockResult =
215
- | { kind: 'acquired'; token: LockToken }
216
- | { kind: 'daemon-reachable' }
217
- | { kind: 'stale-lock-cleared' }
218
- | { kind: 'timeout'; reason: string }
240
+ async function settleExit(proc: ReturnType<typeof Bun.spawn>, graceMs: number): Promise<void> {
241
+ let timer: ReturnType<typeof setTimeout> | undefined
242
+ const grace = new Promise<void>((resolve) => {
243
+ timer = setTimeout(resolve, graceMs)
244
+ })
245
+ try {
246
+ await Promise.race([proc.exited.then(() => undefined), grace])
247
+ } finally {
248
+ if (timer) clearTimeout(timer)
249
+ }
250
+ }
251
+
252
+ type LockResult = { kind: 'acquired'; release: () => Promise<void> } | { kind: 'contended' }
219
253
 
220
- async function acquireLockOrWait(timeoutMs: number): Promise<LockResult> {
254
+ async function acquireSpawnLock(timeoutMs: number): Promise<LockResult> {
221
255
  const path = lockfilePath()
222
- const deadline = Date.now() + timeoutMs
223
- while (Date.now() < deadline) {
224
- try {
225
- const handle = await open(path, 'wx')
226
- await handle.write(`${process.pid}\n`)
227
- await handle.close()
228
- return { kind: 'acquired', token: { path } }
229
- } catch {
230
- if (await isDaemonReachable()) return { kind: 'daemon-reachable' }
231
- await sleep(POLL_INTERVAL_MS)
232
- }
256
+ await clearLegacyFileLock(path)
257
+ const retries = Math.max(1, Math.ceil(timeoutMs / POLL_INTERVAL_MS))
258
+ try {
259
+ const release = await lockfile.lock(path, {
260
+ lockfilePath: path,
261
+ realpath: false,
262
+ stale: LOCK_STALE_MS,
263
+ retries: { ...LOCK_RETRY_BACKOFF, retries },
264
+ })
265
+ return { kind: 'acquired', release: () => release().catch(() => {}) }
266
+ } catch (error) {
267
+ if (errorCode(error) === 'ELOCKED') return { kind: 'contended' }
268
+ throw error
269
+ }
270
+ }
271
+
272
+ // The pre-proper-lockfile daemon left the lock as a regular FILE at this path.
273
+ // proper-lockfile locks by creating a DIRECTORY there, so a stale legacy file
274
+ // makes mkdir fail EEXIST (reported as ELOCKED) while its own reclaim rmdir
275
+ // fails ENOTDIR — wedging startup forever after an upgrade. Clear an abandoned
276
+ // legacy file, but never a live one: a co-existing old-binary caller mid-spawn
277
+ // may hold it before its daemon is reachable. The recorded pid is only a hint
278
+ // (pids get recycled), so it's trusted only while the file is fresh; an aged
279
+ // file is reclaimed regardless. Directories (a live proper-lockfile lock) and
280
+ // symlinks are left untouched.
281
+ async function clearLegacyFileLock(path: string): Promise<void> {
282
+ let observed: Awaited<ReturnType<typeof lstat>>
283
+ try {
284
+ observed = await lstat(path)
285
+ } catch (error) {
286
+ if (errorCode(error) === 'ENOENT') return
287
+ throw error
288
+ }
289
+ if (!observed.isFile()) return
290
+
291
+ let raw: string
292
+ try {
293
+ raw = await readFile(path, 'utf8')
294
+ } catch (error) {
295
+ const code = errorCode(error)
296
+ if (code === 'ENOENT' || code === 'EISDIR') return
297
+ throw error
233
298
  }
234
- // Lock held by something that never finished. Clear it so the caller can
235
- // retry once. Rare in practice (only happens if a previous ensureDaemon
236
- // process was killed mid-spawn).
299
+
300
+ const pid = Number.parseInt(raw.trim(), 10)
301
+ const hasValidPid = Number.isSafeInteger(pid) && pid > 0
302
+ const isFresh = Date.now() - observed.mtimeMs < LOCK_STALE_MS
303
+ // A bare pid is only trustworthy briefly: pid reuse would otherwise let an
304
+ // abandoned legacy file (whose recorded pid the OS reassigned to an unrelated
305
+ // process) look "held" forever. So preserve only a FRESH file that is either
306
+ // held by a live pid or still within the grace for a not-yet-written pid; once
307
+ // the file ages past LOCK_STALE_MS, reclaim it regardless of pid liveness.
308
+ if (isFresh && (!hasValidPid || processExists(pid))) return
309
+
237
310
  try {
311
+ const current = await lstat(path)
312
+ if (!current.isFile() || current.dev !== observed.dev || current.ino !== observed.ino) return
238
313
  await unlink(path)
239
- } catch {}
240
- return { kind: 'stale-lock-cleared' }
314
+ } catch (error) {
315
+ const code = errorCode(error)
316
+ if (code === 'ENOENT' || code === 'EISDIR' || code === 'EPERM') return
317
+ throw error
318
+ }
241
319
  }
242
320
 
243
- async function releaseLock(token: LockToken): Promise<void> {
321
+ function processExists(pid: number): boolean {
244
322
  try {
245
- await unlink(token.path)
246
- } catch {}
323
+ process.kill(pid, 0)
324
+ return true
325
+ } catch (error) {
326
+ // EPERM means the process exists but we may not signal it — still alive.
327
+ return errorCode(error) !== 'ESRCH'
328
+ }
329
+ }
330
+
331
+ function errorCode(error: unknown): string | undefined {
332
+ return typeof error === 'object' && error !== null ? (error as { code?: string }).code : undefined
247
333
  }
248
334
 
249
335
  async function readPidQuiet(): Promise<number> {
@@ -1,3 +1,4 @@
1
+ import { ASKPASS_SCRIPT, TYPECLAW_GIT_ASKPASS_PATH } from '@/bundled-plugins/github-cli-auth/git-askpass'
1
2
  import { validateDockerfileAppendLine } from '@/config/config'
2
3
  import type { DockerfileConfig, DockerfileFeatureToggle } from '@/config/config'
3
4
  import {
@@ -416,8 +417,8 @@ link_persistent_home_files() {
416
417
  }
417
418
 
418
419
  # link_configured_symlinks creates the operator's \`sandbox.symlinks\` at the real
419
- # container $HOME for the UNSANDBOXED (trusted/owner) bash path; low-trust bash
420
- # gets an equivalent in-jail symlink from the per-tool bwrap builder instead
420
+ # container $HOME for runtime-owned processes. Model-driven bash runs in bwrap
421
+ # for every role and gets an equivalent in-jail symlink from the per-tool builder
421
422
  # (src/sandbox/build.ts). TYPECLAW_SANDBOX_SYMLINKS is base64-encoded JSON of
422
423
  # [{from,to}] (set by start.ts only when non-empty). The whole job is done in
423
424
  # \`bun -e\` rather than POSIX shell because \`from\`/\`to\` are arbitrary operator
@@ -477,6 +478,37 @@ link_configured_symlinks() {
477
478
  ' || true
478
479
  }
479
480
 
481
+ # seed_runtime_home copies the image-baked Claude Code / Codex CLI settings from
482
+ # the build-time HOME (/root) into the managed runtime HOME the non-root re-exec
483
+ # switches to. The Dockerfile writes ~/.claude.json, ~/.claude/settings.json,
484
+ # and ~/.codex/hooks.json against /root during \`docker build\`; once the runtime
485
+ # runs under HOME=$runtime_home those files would be invisible, silently
486
+ # disabling Claude onboarding state and both CLIs' completion hooks for
487
+ # claudeCode/codexCli users. link_persistent_home_files only links CREDENTIALS,
488
+ # not this config, so we seed it here.
489
+ #
490
+ # Runs in the root setup phase (HOME is still /root), BEFORE the chown so the
491
+ # copies inherit the host ownership. \`cp -n\` never clobbers a file the operator
492
+ # already persisted into the runtime home (the bind-mounted persist root survives
493
+ # restarts), so seeding is idempotent and one-way: image defaults fill only what
494
+ # is missing.
495
+ seed_runtime_home() {
496
+ _src="\${1:-/root}"
497
+ _dst="$2"
498
+ [ "$_src" = "$_dst" ] && return 0
499
+ mkdir -p "$_dst"
500
+ [ -f "$_src/.claude.json" ] && cp -n "$_src/.claude.json" "$_dst/.claude.json" 2>/dev/null || true
501
+ if [ -f "$_src/.claude/settings.json" ]; then
502
+ mkdir -p "$_dst/.claude"
503
+ cp -n "$_src/.claude/settings.json" "$_dst/.claude/settings.json" 2>/dev/null || true
504
+ fi
505
+ if [ -f "$_src/.codex/hooks.json" ]; then
506
+ mkdir -p "$_dst/.codex"
507
+ cp -n "$_src/.codex/hooks.json" "$_dst/.codex/hooks.json" 2>/dev/null || true
508
+ fi
509
+ unset _src _dst
510
+ }
511
+
480
512
  start_xvfb() {
481
513
  if ! command -v Xvfb >/dev/null 2>&1; then
482
514
  return 0
@@ -491,9 +523,23 @@ start_xvfb() {
491
523
  rm -f "$xvfb_status"
492
524
  (
493
525
  set +e
494
- setpriv --bounding-set -net_admin --inh-caps -net_admin --ambient-caps -net_admin \\
495
- -- Xvfb :99 -screen 0 1920x1080x24 -ac +extension RANDR -nolisten tcp \\
496
- >/dev/null 2>&1
526
+ # The nested setpriv strips NET_ADMIN so Xvfb never inherits it (invariant
527
+ # 1). But when we've ALREADY re-exec'd into the non-root runtime phase
528
+ # (TYPECLAW_ENTRYPOINT_RUNTIME=1), the outer handoff emptied the bounding
529
+ # set and dropped CAP_SETPCAP — so a second \`setpriv --bounding-set\` here
530
+ # can no longer modify the bounding set and exits non-zero (127), which the
531
+ # liveness probe would misreport as "Xvfb exited immediately". There is also
532
+ # nothing left to strip: NET_ADMIN is already gone. So in the runtime phase
533
+ # launch Xvfb DIRECTLY; keep the nested drop only for the root fallback
534
+ # paths (where this shim is still PID 1 with the full capability set).
535
+ if [ "\${TYPECLAW_ENTRYPOINT_RUNTIME:-0}" = "1" ]; then
536
+ Xvfb :99 -screen 0 1920x1080x24 -ac +extension RANDR -nolisten tcp \\
537
+ >/dev/null 2>&1
538
+ else
539
+ setpriv --bounding-set -net_admin --inh-caps -net_admin --ambient-caps -net_admin \\
540
+ -- Xvfb :99 -screen 0 1920x1080x24 -ac +extension RANDR -nolisten tcp \\
541
+ >/dev/null 2>&1
542
+ fi
497
543
  printf '%s\\n' "$?" > "$xvfb_status"
498
544
  ) &
499
545
  xvfb_pid=$!
@@ -524,7 +570,35 @@ start_xvfb() {
524
570
  exit 1
525
571
  }
526
572
 
573
+ if [ "\${TYPECLAW_ENTRYPOINT_RUNTIME:-0}" = "1" ]; then
574
+ link_persistent_home_files
575
+ link_configured_symlinks
576
+ start_xvfb
577
+ exec bun run ${TYPECLAW_CLI_ENTRY} "$@"
578
+ fi
579
+
580
+ persist_root="\${TYPECLAW_PERSIST_HOME_ROOT:-/agent/.typeclaw/home}"
581
+ runtime_home="$persist_root/runtime"
582
+ if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
583
+ mkdir -p "$runtime_home"
584
+ seed_runtime_home "$HOME" "$runtime_home"
585
+ chown -R "$TYPECLAW_HOST_UID:$TYPECLAW_HOST_GID" "$persist_root"
586
+ fi
587
+
527
588
  if [ "\${TYPECLAW_NETWORK_BLOCK_INTERNAL:-0}" != "1" ]; then
589
+ if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
590
+ # NOTE: no \`--reset-env\`. setpriv --reset-env clears every non-terminal
591
+ # variable, which would wipe the runtime's own configuration: --env-file
592
+ # values (provider credentials, etc.) plus TYPECLAW_SANDBOX_SYMLINKS,
593
+ # TYPECLAW_TUI_TOKEN, TYPECLAW_CONTAINER_NAME, TYPECLAW_HOSTD_URL,
594
+ # TYPECLAW_MODEL_CACHE, and TZ — all injected by start.ts and read after the
595
+ # re-exec. We only need to OVERRIDE two variables for the runtime phase, and
596
+ # the trailing \`env HOME=… TYPECLAW_ENTRYPOINT_RUNTIME=1\` does exactly that
597
+ # while forwarding everything else unchanged.
598
+ exec setpriv --reuid="$TYPECLAW_HOST_UID" --regid="$TYPECLAW_HOST_GID" --clear-groups \\
599
+ --bounding-set=-all --inh-caps=-all --ambient-caps=-all \\
600
+ -- env HOME="$runtime_home" TYPECLAW_ENTRYPOINT_RUNTIME=1 "$0" "$@"
601
+ fi
528
602
  link_persistent_home_files
529
603
  link_configured_symlinks
530
604
  start_xvfb
@@ -572,6 +646,14 @@ ip6tables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
572
646
  ip6tables -A OUTPUT -o lo -j ACCEPT
573
647
  ${ipv6Rules.join('\n')}
574
648
 
649
+ if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
650
+ # No \`--reset-env\` — see the network-off handoff above: it would strip the
651
+ # --env-file values and the TypeClaw-injected runtime variables. The trailing
652
+ # \`env\` overrides only HOME + TYPECLAW_ENTRYPOINT_RUNTIME and forwards the rest.
653
+ exec setpriv --reuid="$TYPECLAW_HOST_UID" --regid="$TYPECLAW_HOST_GID" --clear-groups \\
654
+ --bounding-set=-all --inh-caps=-all --ambient-caps=-all \\
655
+ -- env HOME="$runtime_home" TYPECLAW_ENTRYPOINT_RUNTIME=1 "$0" "$@"
656
+ fi
575
657
  link_persistent_home_files
576
658
  link_configured_symlinks
577
659
  start_xvfb
@@ -592,6 +674,15 @@ RUN echo "${encoded}" | base64 -d > ${TYPECLAW_ENTRYPOINT_PATH} \\
592
674
  && chmod +x ${TYPECLAW_ENTRYPOINT_PATH}`
593
675
  }
594
676
 
677
+ // Bake the secret-free git-askpass helper (github-cli-auth broker delivers the
678
+ // token to it via env). Root-owned at build time so the non-root runtime, which
679
+ // cannot write the read-only /usr, still finds it.
680
+ function renderGitAskPassLayer(): string {
681
+ const encoded = Buffer.from(ASKPASS_SCRIPT, 'utf8').toString('base64')
682
+ return `RUN echo "${encoded}" | base64 -d > ${TYPECLAW_GIT_ASKPASS_PATH} \\
683
+ && chmod 755 ${TYPECLAW_GIT_ASKPASS_PATH}`
684
+ }
685
+
595
686
  // Layer 7: install @huggingface/transformers with its linux-native binaries.
596
687
  //
597
688
  // The host node_modules is bind-mounted over /agent at runtime carrying
@@ -1337,6 +1428,7 @@ WORKDIR /agent
1337
1428
  ARG TARGETARCH
1338
1429
 
1339
1430
  ${ghKeyringLayer}${toggleAptLayer}${cloudflaredBlock}${claudeCodeBlock}${codexCliBlock}${renderEntrypointShimLayer()}
1431
+ ${renderGitAskPassLayer()}
1340
1432
 
1341
1433
  ${LAYER_TRANSFORMERS_INSTALL}
1342
1434
 
@@ -1400,6 +1492,7 @@ ${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
1400
1492
  ${renderChromeForTestingLayer(buildKit)}
1401
1493
 
1402
1494
  ${cloudflaredBlock}${claudeCodeBlock}${codexCliBlock}${renderEntrypointShimLayer()}
1495
+ ${renderGitAskPassLayer()}
1403
1496
 
1404
1497
  ${LAYER_TRANSFORMERS_INSTALL}
1405
1498
 
@@ -1477,6 +1570,7 @@ ${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
1477
1570
  ${renderChromeForTestingLayer(true)}
1478
1571
 
1479
1572
  ${renderEntrypointShimLayer()}
1573
+ ${renderGitAskPassLayer()}
1480
1574
 
1481
1575
  ${LAYER_TRANSFORMERS_INSTALL}
1482
1576
  `
package/src/init/index.ts CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  type StartResult,
28
28
  stop,
29
29
  } from '@/container'
30
+ import { hooklessGitArgs } from '@/git/hookless'
30
31
  import { commitSystemFile } from '@/git/system-commit'
31
32
  import { createSecretsStoreForAgent, type Channels, type Secret, SecretsBackend } from '@/secrets'
32
33
  import { hostLocaleIsCjk } from '@/shared/host-locale'
@@ -653,7 +654,12 @@ export async function isHatched(dir: string): Promise<boolean> {
653
654
  const bun = (globalThis as { Bun?: { spawn: typeof Bun.spawn } }).Bun
654
655
  if (!bun) return false
655
656
  try {
656
- const proc = bun.spawn({ cmd: ['git', 'log', '--format=%s'], cwd: dir, stdout: 'pipe', stderr: 'pipe' })
657
+ const proc = bun.spawn({
658
+ cmd: ['git', ...hooklessGitArgs(['log', '--format=%s'])],
659
+ cwd: dir,
660
+ stdout: 'pipe',
661
+ stderr: 'pipe',
662
+ })
657
663
  if ((await proc.exited) !== 0) return false
658
664
  const subjects = (await new Response(proc.stdout).text()).split('\n')
659
665
  return subjects.includes(HATCHED_COMMIT_SUBJECT)
@@ -858,7 +864,7 @@ export async function initGitRepo(cwd: string): Promise<GitInitResult> {
858
864
  try {
859
865
  if (hasGit) {
860
866
  const head = bun.spawn({
861
- cmd: ['git', 'rev-parse', '--verify', 'HEAD'],
867
+ cmd: ['git', ...hooklessGitArgs(['rev-parse', '--verify', 'HEAD'])],
862
868
  cwd,
863
869
  env,
864
870
  stdout: 'pipe',
@@ -866,21 +872,27 @@ export async function initGitRepo(cwd: string): Promise<GitInitResult> {
866
872
  })
867
873
  if ((await head.exited) === 0) return { ok: true, skipped: true }
868
874
  } else {
869
- const init = bun.spawn({ cmd: ['git', 'init', '-b', 'main'], cwd, env, stdout: 'pipe', stderr: 'pipe' })
875
+ const init = bun.spawn({
876
+ cmd: ['git', ...hooklessGitArgs(['init', '-b', 'main'])],
877
+ cwd,
878
+ env,
879
+ stdout: 'pipe',
880
+ stderr: 'pipe',
881
+ })
870
882
  if ((await init.exited) !== 0) {
871
883
  const stderr = await new Response(init.stderr).text()
872
884
  return { ok: false, reason: `git init failed: ${stderr.trim() || 'no stderr'}` }
873
885
  }
874
886
  }
875
887
 
876
- const add = bun.spawn({ cmd: ['git', 'add', '.'], cwd, env, stdout: 'pipe', stderr: 'pipe' })
888
+ const add = bun.spawn({ cmd: ['git', ...hooklessGitArgs(['add', '.'])], cwd, env, stdout: 'pipe', stderr: 'pipe' })
877
889
  if ((await add.exited) !== 0) {
878
890
  const stderr = await new Response(add.stderr).text()
879
891
  return { ok: false, reason: `git add failed: ${stderr.trim() || 'no stderr'}` }
880
892
  }
881
893
 
882
894
  const commit = bun.spawn({
883
- cmd: ['git', 'commit', '-m', 'Initial commit 🥚'],
895
+ cmd: ['git', ...hooklessGitArgs(['commit', '-m', 'Initial commit 🥚'])],
884
896
  cwd,
885
897
  env,
886
898
  stdout: 'pipe',
@@ -32,6 +32,10 @@ const PROVIDER_TO_MODELS_DEV: Record<KnownProviderId, string> = {
32
32
  xai: 'xai',
33
33
  minimax: 'minimax',
34
34
  deepseek: 'deepseek',
35
+ // Upstage is not listed on models.dev (checked api.json — no `upstage` lab
36
+ // entry, only HuggingFace open-model ids). Mapping to `upstage` is harmless:
37
+ // the upstream lookup misses and the curated Solar entries surface anyway.
38
+ upstage: 'upstage',
35
39
  moonshot: 'moonshot',
36
40
  // moonshot-coding (Kimi Code subscription) is a billing surface, not a
37
41
  // separate model catalog. models.dev tracks the underlying Kimi model
@@ -10,6 +10,7 @@ const PROVIDER_PROBE: Partial<Record<KnownProviderId, { url: string; authHeader:
10
10
  xai: { url: 'https://api.x.ai/v1/models', authHeader: 'bearer' },
11
11
  minimax: { url: 'https://api.minimax.io/v1/models', authHeader: 'bearer' },
12
12
  deepseek: { url: 'https://api.deepseek.com/models', authHeader: 'bearer' },
13
+ upstage: { url: 'https://api.upstage.ai/v1/models', authHeader: 'bearer' },
13
14
  moonshot: { url: 'https://api.moonshot.ai/v1/models', authHeader: 'bearer' },
14
15
  'moonshot-coding': { url: 'https://api.kimi.com/coding/v1/models', authHeader: 'bearer' },
15
16
  }
@@ -167,6 +168,7 @@ export const API_KEY_DASHBOARD_URL: Partial<Record<KnownProviderId, string>> = {
167
168
  xai: 'https://console.x.ai',
168
169
  minimax: 'https://platform.minimax.io/user-center/basic-information/interface-key',
169
170
  deepseek: 'https://platform.deepseek.com/api_keys',
171
+ upstage: 'https://console.upstage.ai/api-keys',
170
172
  moonshot: 'https://platform.moonshot.ai/console/api-keys',
171
173
  'moonshot-coding': 'https://www.kimi.com/code/console',
172
174
  }
@@ -311,18 +311,30 @@ function describeErr(err: unknown): string {
311
311
  return String(err)
312
312
  }
313
313
 
314
+ // Explicit reader + `finally` cancel so a consumer that breaks early (peekSession
315
+ // stops after the first meta + a few user turns) releases the file descriptor
316
+ // deterministically, not relying on async-iterator finalization. Load-bearing when
317
+ // tens of thousands of session files are peeked under a low `ulimit -n`.
314
318
  async function* streamLines(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
319
+ const reader = stream.getReader()
315
320
  const decoder = new TextDecoder()
316
321
  let buf = ''
317
- for await (const chunk of stream) {
318
- buf += decoder.decode(chunk, { stream: true })
319
- let nl = buf.indexOf('\n')
320
- while (nl !== -1) {
321
- const line = buf.slice(0, nl)
322
- buf = buf.slice(nl + 1)
323
- yield line
324
- nl = buf.indexOf('\n')
322
+ try {
323
+ while (true) {
324
+ const { done, value } = await reader.read()
325
+ if (done) break
326
+ buf += decoder.decode(value, { stream: true })
327
+ let nl = buf.indexOf('\n')
328
+ while (nl !== -1) {
329
+ const line = buf.slice(0, nl)
330
+ buf = buf.slice(nl + 1)
331
+ yield line
332
+ nl = buf.indexOf('\n')
333
+ }
325
334
  }
335
+ if (buf.length > 0) yield buf
336
+ } finally {
337
+ await reader.cancel().catch(() => {})
338
+ reader.releaseLock()
326
339
  }
327
- if (buf.length > 0) yield buf
328
340
  }