typeclaw 0.46.0 → 0.46.2

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 (75) hide show
  1. package/README.md +1 -1
  2. package/package.json +2 -2
  3. package/src/agent/multimodal/looker.ts +11 -1
  4. package/src/agent/network/safe-http.ts +17 -8
  5. package/src/agent/plugin-tools.ts +1 -3
  6. package/src/agent/tool-file-safety.ts +414 -145
  7. package/src/agent/tools/webfetch/fetch.ts +22 -2
  8. package/src/agent/tools/webfetch/strategies/jq.ts +1 -2
  9. package/src/agent/tools/webfetch/strategies/readability.ts +39 -16
  10. package/src/agent/tools/webfetch/strategies/selector.ts +4 -2
  11. package/src/agent/tools/webfetch/strategies/snapshot.ts +3 -2
  12. package/src/agent/tools/webfetch/tool.ts +3 -3
  13. package/src/agent/tools-without-local-file-operands.ts +9 -6
  14. package/src/bundled-plugins/backup/README.md +23 -10
  15. package/src/bundled-plugins/backup/git-auth.ts +6 -4
  16. package/src/bundled-plugins/backup/index.ts +104 -47
  17. package/src/bundled-plugins/backup/runner.ts +102 -5
  18. package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
  19. package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
  20. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
  21. package/src/bundled-plugins/github-cli-auth/index.ts +110 -31
  22. package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
  23. package/src/bundled-plugins/memory/README.md +9 -0
  24. package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
  25. package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
  26. package/src/bundled-plugins/memory/vector/hybrid.ts +10 -2
  27. package/src/bundled-plugins/memory/vector/store.ts +21 -9
  28. package/src/bundled-plugins/researcher/write-report.ts +3 -3
  29. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +8 -3
  30. package/src/bundled-plugins/security/policies/private-surface-read.ts +616 -50
  31. package/src/bundled-plugins/security/policies/ssrf.ts +119 -34
  32. package/src/channels/adapters/github/inbound.ts +11 -0
  33. package/src/channels/adapters/github/index.ts +48 -54
  34. package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
  35. package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
  36. package/src/channels/adapters/slack-bot-edit.ts +85 -20
  37. package/src/channels/adapters/slack-bot.ts +7 -2
  38. package/src/channels/adapters/slack-format.ts +245 -0
  39. package/src/channels/github-token-bridge.ts +2 -3
  40. package/src/cli/inspect.ts +16 -0
  41. package/src/container/docker-app.ts +42 -13
  42. package/src/container/shared.ts +19 -6
  43. package/src/container/start.ts +13 -0
  44. package/src/cron/bridge.ts +9 -2
  45. package/src/doctor/checks.ts +5 -2
  46. package/src/git/bound-gc-config.ts +82 -0
  47. package/src/hostd/spawn.ts +133 -47
  48. package/src/init/checkpoint.ts +4 -4
  49. package/src/init/dockerfile.ts +100 -88
  50. package/src/init/gitignore.ts +5 -6
  51. package/src/init/run-bun-install.ts +16 -2
  52. package/src/init/validate-api-key.ts +1 -0
  53. package/src/inspect/item-list.ts +12 -6
  54. package/src/inspect/replay.ts +21 -9
  55. package/src/inspect/session-list.ts +71 -33
  56. package/src/mcp/tools.ts +1 -0
  57. package/src/network/internal-destinations.ts +267 -0
  58. package/src/permissions/permissions.ts +93 -2
  59. package/src/plugin/types.ts +3 -2
  60. package/src/role-claim/code.ts +9 -5
  61. package/src/run/index.ts +45 -36
  62. package/src/sandbox/canonical-secrets.ts +6 -0
  63. package/src/sandbox/env-exposure.ts +18 -15
  64. package/src/sandbox/index.ts +1 -0
  65. package/src/sandbox/policy.ts +5 -3
  66. package/src/sandbox/privileged-runtime.ts +1 -0
  67. package/src/sandbox/symlinks.ts +1 -1
  68. package/src/secrets/export-claude-credentials-file.ts +13 -22
  69. package/src/secrets/export-codex-auth-file.ts +12 -28
  70. package/src/skills/typeclaw-config/SKILL.md +22 -6
  71. package/src/skills/typeclaw-permissions/SKILL.md +22 -9
  72. package/src/skills/typeclaw-plugins/SKILL.md +37 -0
  73. package/src/usage/scan.ts +11 -2
  74. package/typeclaw.schema.json +5 -0
  75. package/src/git/secret-history.ts +0 -371
@@ -1,9 +1,9 @@
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 {
4
5
  CLAUDE_CREDENTIALS_FILE_NAME,
5
6
  CLAUDE_CREDENTIALS_RELATIVE_PATH,
6
- CLAUDE_DEFAULT_CONFIG_DIR_NAME,
7
7
  } from '@/secrets/export-claude-credentials-file'
8
8
 
9
9
  import { GHCR_BASE_IMAGE_REPO } from './cli-version'
@@ -137,6 +137,13 @@ export const TYPECLAW_ENTRYPOINT_PATH = '/usr/local/bin/typeclaw-entrypoint'
137
137
  // a path, and resolves `/agent/typeclaw.json` (`Cannot run json files`).
138
138
  export const TYPECLAW_CLI_ENTRY = '/agent/node_modules/typeclaw/src/cli/index.ts'
139
139
 
140
+ // Perf: `--smol` selects JSC's small-heap profile (grows slower, GCs more often),
141
+ // trading a little CPU for lower resident memory. The agent runtime is a
142
+ // burst-then-idle workload, so temporary allocations get reclaimed instead of
143
+ // settling into a high-water RSS. Flag goes before `run`. Used at every exec site
144
+ // below so the runtime phase inherits it on every network-policy path.
145
+ const RUNTIME_BUN = 'bun --smol run'
146
+
140
147
  // IPv4 networks the container is forbidden to egress to when
141
148
  // `network.blockInternal` is true. Loopback (127/8) is NOT here — loopback
142
149
  // traffic uses the `lo` interface, which the shim's first ACCEPT rule
@@ -353,68 +360,6 @@ set -eu
353
360
  # -nolisten tcp refuse TCP connections (Unix socket only).
354
361
  # Defense-in-depth — we are in a netns with
355
362
  # no inbound exposure anyway.
356
- # link_persistent_home_files symlinks credential files that tools write
357
- # to $HOME into a bind-mounted location so they survive container
358
- # restarts. The container's $HOME (/root by default) lives on Docker's
359
- # writable overlay and is wiped on every \`stop\`+\`start\` cycle, so
360
- # without this symlink the operator would have to re-paste credentials
361
- # after every restart.
362
- #
363
- # Two files are linked today, both following the same contract:
364
- # - ~/.codex/auth.json — Codex CLI rotates OAuth tokens in place by
365
- # rewriting auth.json with refreshed credentials.
366
- # - $CLAUDE_CONFIG_DIR/.credentials.json, or ~/.claude/.credentials.json
367
- # by default — Claude Code rotates OAuth tokens in place by rewriting
368
- # .credentials.json on every successful refresh (anthropics/claude-code
369
- # #53063). Linux/Windows path; macOS uses the Keychain entry "Claude
370
- # Code-credentials" with the same JSON shape.
371
- #
372
- # The persist root lives under /agent/.typeclaw/home/ (bind-mounted
373
- # from the agent folder via the -v <cwd>:/agent flag in start.ts).
374
- # Namespacing under .typeclaw/ keeps the agent's top-level layout clean and reserves
375
- # a system-owned subtree we can extend later (e.g. ~/.gemini/) without
376
- # colliding with user files. The directory is gitignored by buildGitignore()
377
- # so credentials never enter history.
378
- #
379
- # Three invariants this function enforces:
380
- #
381
- # 1. Symlink is unconditional and idempotent. We never check whether
382
- # auth.json exists before linking — \`ln -sfn\` creates a dangling
383
- # symlink on first boot, and the first \`codex login\` write goes
384
- # through it to land at the persistent location. -f replaces an
385
- # existing symlink; -n stops ln from dereferencing into a directory
386
- # if a previous container life happened to write a real ~/.codex/
387
- # dir before this code shipped.
388
- #
389
- # 2. We symlink credential FILES for tools whose config dirs are mostly
390
- # scratch/history (Codex, Claude). We do not redirect global config
391
- # locations such as XDG_CONFIG_HOME or ~/.config here because tools like
392
- # git also read config from those paths; first-party bundles that need
393
- # persistence should set their own app-specific env vars instead.
394
- #
395
- # 3. We mkdir -p the target's parent on every boot. /agent is bind-
396
- # mounted, so the host-side path may exist or not depending on
397
- # whether the operator ever started the container before this code
398
- # shipped. mkdir -p is idempotent and cheap.
399
- #
400
- # 4. The root is overridable via TYPECLAW_PERSIST_HOME_ROOT, which only
401
- # the shim's executable tests set. Production never sets it, so the
402
- # in-container path is always /agent/.typeclaw/home/. The override
403
- # lets the shim's behavioral tests verify symlink semantics against
404
- # a real tmpdir on the host without touching /agent (which doesn't
405
- # exist on developer machines and CI runners).
406
- link_persistent_home_files() {
407
- persist_root="\${TYPECLAW_PERSIST_HOME_ROOT:-/agent/.typeclaw/home}"
408
- mkdir -p "$persist_root/.codex" "$HOME/.codex"
409
- ln -sfn "$persist_root/.codex/auth.json" "$HOME/.codex/auth.json"
410
- claude_config_dir="\${CLAUDE_CONFIG_DIR:-}"
411
- if [ -z "$claude_config_dir" ]; then
412
- claude_config_dir="$HOME/${CLAUDE_DEFAULT_CONFIG_DIR_NAME}"
413
- fi
414
- mkdir -p "$persist_root/${CLAUDE_DEFAULT_CONFIG_DIR_NAME}" "$claude_config_dir"
415
- ln -sfn "$persist_root/${CLAUDE_CREDENTIALS_RELATIVE_PATH}" "$claude_config_dir/${CLAUDE_CREDENTIALS_FILE_NAME}"
416
- }
417
-
418
363
  # link_configured_symlinks creates the operator's \`sandbox.symlinks\` at the real
419
364
  # container $HOME for runtime-owned processes. Model-driven bash runs in bwrap
420
365
  # for every role and gets an equivalent in-jail symlink from the per-tool builder
@@ -431,8 +376,7 @@ link_persistent_home_files() {
431
376
  # per-entry so one bad symlink never blocks container boot.
432
377
  #
433
378
  # TYPECLAW_AGENT_DIR defaults to /agent (the bind-mount path) and is overridable
434
- # only by the shim's behavioral tests, which point it at a tmpdir — same escape
435
- # hatch as TYPECLAW_PERSIST_HOME_ROOT in link_persistent_home_files. Production
379
+ # only by the shim's behavioral tests, which point it at a tmpdir. Production
436
380
  # never sets it.
437
381
  link_configured_symlinks() {
438
382
  [ -n "\${TYPECLAW_SANDBOX_SYMLINKS:-}" ] || return 0
@@ -477,20 +421,72 @@ link_configured_symlinks() {
477
421
  ' || true
478
422
  }
479
423
 
424
+ # link_custom_claude_credentials keeps a custom CLAUDE_CONFIG_DIR credential file
425
+ # out of model-visible reach. The Claude Code exporter writes the OAuth refresh
426
+ # token to \$CLAUDE_CONFIG_DIR/.credentials.json when that env var is set. Under
427
+ # the default (unset) it lands in the runtime HOME's .claude, which bwrap never
428
+ # binds — invisible to sandboxed bash. But a custom dir under /agent (or any
429
+ # jail-visible root) would put the real token where model bash can read it.
430
+ #
431
+ # Instead of masking every possible custom location, plant a symlink from the
432
+ # custom path INTO the runtime HOME's .claude and let the exporter's
433
+ # symlink-aware writeAtomic() follow it — so the real bytes always live in the
434
+ # unbound, already-denied runtime HOME regardless of where CLAUDE_CONFIG_DIR
435
+ # points. This restores the invariant the old persistence helper used to provide
436
+ # (it linked into the masked persist root); the target moved to the ephemeral
437
+ # HOME, but the "credential is only an alias in the custom dir" property is
438
+ # preserved. Runs in the root phase before privilege drop so custom dirs under
439
+ # root-owned roots (e.g. /etc) can still be created. No-op when the value is
440
+ # unset/blank or already resolves to the runtime HOME's .claude (avoids a
441
+ # self-referential link). Re-linking is idempotent (an existing symlink is
442
+ # replaced); failures are non-fatal so a bad custom path never blocks boot.
443
+ #
444
+ # The from-path MUST match where the exporter actually writes. The exporter
445
+ # (src/secrets/export-claude-credentials-file.ts resolveClaudeConfigDir) applies
446
+ # JS String.trim() and treats an all-whitespace value as unset. Replicating that
447
+ # in POSIX sed would diverge on Unicode whitespace (\u00A0, \u2003, …) that
448
+ # .trim() strips but a byte-oriented sed class does not — a divergence is a
449
+ # credential-isolation bypass (exporter writes the trimmed path, alias sits at
450
+ # the padded path, token stays model-readable). So do the normalization in the
451
+ # same \`bun -e\` runtime the exporter uses, guaranteeing byte-for-byte identical
452
+ # trimming, and create the symlink there too.
453
+ link_custom_claude_credentials() {
454
+ CLAUDE_CONFIG_DIR="\${CLAUDE_CONFIG_DIR:-}" _CLAUDE_RUNTIME_HOME="$runtime_home" \\
455
+ _CLAUDE_CRED_FILE="${CLAUDE_CREDENTIALS_FILE_NAME}" _CLAUDE_CRED_REL="${CLAUDE_CREDENTIALS_RELATIVE_PATH}" bun -e '
456
+ import { mkdirSync, rmSync, symlinkSync } from "node:fs";
457
+ import { dirname, join } from "node:path";
458
+ const raw = process.env.CLAUDE_CONFIG_DIR;
459
+ const trimmed = raw?.trim();
460
+ if (trimmed === undefined || trimmed.length === 0) process.exit(0);
461
+ const runtimeHome = process.env._CLAUDE_RUNTIME_HOME || "/home/agent";
462
+ const from = join(trimmed, process.env._CLAUDE_CRED_FILE);
463
+ const to = join(runtimeHome, process.env._CLAUDE_CRED_REL);
464
+ if (from === to) process.exit(0);
465
+ try {
466
+ mkdirSync(trimmed, { recursive: true });
467
+ mkdirSync(dirname(to), { recursive: true });
468
+ // Unconditionally (re)point the alias, matching the old ln -sfn: a
469
+ // pre-existing real file at the custom path would otherwise keep the token
470
+ // model-readable, which is exactly the exposure this aliasing closes.
471
+ try { rmSync(from); } catch { /* nothing to remove */ }
472
+ symlinkSync(to, from);
473
+ } catch (e) {
474
+ console.error("typeclaw-entrypoint: failed to alias custom Claude credentials:", String(e));
475
+ }
476
+ ' || true
477
+ }
478
+
480
479
  # seed_runtime_home copies the image-baked Claude Code / Codex CLI settings from
481
- # the build-time HOME (/root) into the managed runtime HOME the non-root re-exec
482
- # switches to. The Dockerfile writes ~/.claude.json, ~/.claude/settings.json,
483
- # and ~/.codex/hooks.json against /root during \`docker build\`; once the runtime
484
- # runs under HOME=$runtime_home those files would be invisible, silently
485
- # disabling Claude onboarding state and both CLIs' completion hooks for
486
- # claudeCode/codexCli users. link_persistent_home_files only links CREDENTIALS,
487
- # not this config, so we seed it here.
480
+ # the build-time HOME (/root) into the ephemeral runtime HOME. The Dockerfile
481
+ # writes ~/.claude.json, ~/.claude/settings.json, and ~/.codex/hooks.json against
482
+ # /root during \`docker build\`; once the runtime runs under HOME=$runtime_home
483
+ # those files would otherwise be invisible, silently disabling Claude onboarding
484
+ # state and both CLIs' completion hooks for claudeCode/codexCli users.
488
485
  #
489
- # Runs in the root setup phase (HOME is still /root), BEFORE the chown so the
490
- # copies inherit the host ownership. \`cp -n\` never clobbers a file the operator
491
- # already persisted into the runtime home (the bind-mounted persist root survives
492
- # restarts), so seeding is idempotent and one-way: image defaults fill only what
493
- # is missing.
486
+ # Runs in the root setup phase (HOME is still /root), BEFORE the optional chown
487
+ # so copied files have the dynamic host ownership when the runtime drops
488
+ # privileges. \`cp -n\` keeps repeated shim execution idempotent within one
489
+ # container life; Docker removes this HOME with the container.
494
490
  seed_runtime_home() {
495
491
  _src="\${1:-/root}"
496
492
  _dst="$2"
@@ -570,19 +566,25 @@ start_xvfb() {
570
566
  }
571
567
 
572
568
  if [ "\${TYPECLAW_ENTRYPOINT_RUNTIME:-0}" = "1" ]; then
573
- link_persistent_home_files
574
569
  link_configured_symlinks
575
570
  start_xvfb
576
- exec bun run ${TYPECLAW_CLI_ENTRY} "$@"
571
+ exec ${RUNTIME_BUN} ${TYPECLAW_CLI_ENTRY} "$@"
577
572
  fi
578
573
 
579
- persist_root="\${TYPECLAW_PERSIST_HOME_ROOT:-/agent/.typeclaw/home}"
580
- runtime_home="$persist_root/runtime"
574
+ runtime_home="\${TYPECLAW_RUNTIME_HOME:-/home/agent}"
575
+ mkdir -p "$runtime_home"
576
+ seed_runtime_home "$HOME" "$runtime_home"
577
+ link_custom_claude_credentials
581
578
  if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
582
- mkdir -p "$runtime_home"
583
- seed_runtime_home "$HOME" "$runtime_home"
584
- chown -R "$TYPECLAW_HOST_UID:$TYPECLAW_HOST_GID" "$persist_root"
579
+ chown -R "$TYPECLAW_HOST_UID:$TYPECLAW_HOST_GID" "$runtime_home"
585
580
  fi
581
+ # Adopt the runtime HOME for the rest of this root phase, BEFORE any
582
+ # link_configured_symlinks call. The re-exec paths below override HOME again for
583
+ # the fresh runtime process, but the no-host-identity/root fallbacks run
584
+ # link_configured_symlinks in THIS process — without this they would create the
585
+ # operator's \`~/\` symlinks under the inherited build HOME (/root) instead of
586
+ # \$runtime_home.
587
+ export HOME="$runtime_home"
586
588
 
587
589
  if [ "\${TYPECLAW_NETWORK_BLOCK_INTERNAL:-0}" != "1" ]; then
588
590
  if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
@@ -598,10 +600,9 @@ if [ "\${TYPECLAW_NETWORK_BLOCK_INTERNAL:-0}" != "1" ]; then
598
600
  --bounding-set=-all --inh-caps=-all --ambient-caps=-all \\
599
601
  -- env HOME="$runtime_home" TYPECLAW_ENTRYPOINT_RUNTIME=1 "$0" "$@"
600
602
  fi
601
- link_persistent_home_files
602
603
  link_configured_symlinks
603
604
  start_xvfb
604
- exec bun run ${TYPECLAW_CLI_ENTRY} "$@"
605
+ exec env HOME="$runtime_home" ${RUNTIME_BUN} ${TYPECLAW_CLI_ENTRY} "$@"
605
606
  fi
606
607
 
607
608
  iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
@@ -653,10 +654,9 @@ if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
653
654
  --bounding-set=-all --inh-caps=-all --ambient-caps=-all \\
654
655
  -- env HOME="$runtime_home" TYPECLAW_ENTRYPOINT_RUNTIME=1 "$0" "$@"
655
656
  fi
656
- link_persistent_home_files
657
657
  link_configured_symlinks
658
658
  start_xvfb
659
- exec setpriv --bounding-set -net_admin --inh-caps -net_admin --ambient-caps -net_admin -- bun run ${TYPECLAW_CLI_ENTRY} "$@"
659
+ exec env HOME="$runtime_home" setpriv --bounding-set -net_admin --inh-caps -net_admin --ambient-caps -net_admin -- ${RUNTIME_BUN} ${TYPECLAW_CLI_ENTRY} "$@"
660
660
  `
661
661
  }
662
662
 
@@ -673,6 +673,15 @@ RUN echo "${encoded}" | base64 -d > ${TYPECLAW_ENTRYPOINT_PATH} \\
673
673
  && chmod +x ${TYPECLAW_ENTRYPOINT_PATH}`
674
674
  }
675
675
 
676
+ // Bake the secret-free git-askpass helper (github-cli-auth broker delivers the
677
+ // token to it via env). Root-owned at build time so the non-root runtime, which
678
+ // cannot write the read-only /usr, still finds it.
679
+ function renderGitAskPassLayer(): string {
680
+ const encoded = Buffer.from(ASKPASS_SCRIPT, 'utf8').toString('base64')
681
+ return `RUN echo "${encoded}" | base64 -d > ${TYPECLAW_GIT_ASKPASS_PATH} \\
682
+ && chmod 755 ${TYPECLAW_GIT_ASKPASS_PATH}`
683
+ }
684
+
676
685
  // Layer 7: install @huggingface/transformers with its linux-native binaries.
677
686
  //
678
687
  // The host node_modules is bind-mounted over /agent at runtime carrying
@@ -1418,6 +1427,7 @@ WORKDIR /agent
1418
1427
  ARG TARGETARCH
1419
1428
 
1420
1429
  ${ghKeyringLayer}${toggleAptLayer}${cloudflaredBlock}${claudeCodeBlock}${codexCliBlock}${renderEntrypointShimLayer()}
1430
+ ${renderGitAskPassLayer()}
1421
1431
 
1422
1432
  ${LAYER_TRANSFORMERS_INSTALL}
1423
1433
 
@@ -1481,6 +1491,7 @@ ${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
1481
1491
  ${renderChromeForTestingLayer(buildKit)}
1482
1492
 
1483
1493
  ${cloudflaredBlock}${claudeCodeBlock}${codexCliBlock}${renderEntrypointShimLayer()}
1494
+ ${renderGitAskPassLayer()}
1484
1495
 
1485
1496
  ${LAYER_TRANSFORMERS_INSTALL}
1486
1497
 
@@ -1558,6 +1569,7 @@ ${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
1558
1569
  ${renderChromeForTestingLayer(true)}
1559
1570
 
1560
1571
  ${renderEntrypointShimLayer()}
1572
+ ${renderGitAskPassLayer()}
1561
1573
 
1562
1574
  ${LAYER_TRANSFORMERS_INSTALL}
1563
1575
  `
@@ -42,12 +42,11 @@ export function buildGitignore(config: GitignoreConfig = { append: [] }): string
42
42
  # as a safety net so an agent folder cloned from a pre-rename machine never
43
43
  # stages credentials by accident.
44
44
  #
45
- # .typeclaw/ is the agent's local-scratch dir. It holds the persistent-$HOME
46
- # overlay (.typeclaw/home/, populated by the entrypoint shim's
47
- # \`link_persistent_home_files\` — see src/init/dockerfile.ts, mirrors container
48
- # $HOME files like ~/.codex/auth.json so tool credentials survive restarts) and
49
- # the in-progress init checkpoint (.typeclaw/init-progress.json, non-secret
50
- # wizard selections for resume). Local-only; never commit.
45
+ # .typeclaw/ is the agent's local-scratch dir. It holds the in-progress init
46
+ # checkpoint (.typeclaw/init-progress.json, non-secret wizard selections for
47
+ # resume). Upgraded agents may also retain legacy .typeclaw/home/ credentials;
48
+ # the runtime no longer uses or migrates them, but they must remain ignored.
49
+ # Local-only; never commit.
51
50
  ${TRULY_IGNORED_PATTERNS.join('\n')}
52
51
 
53
52
  # System-managed: gitignored by default so the agent never stages them by hand,
@@ -107,10 +107,24 @@ async function runTimedBunProcess({
107
107
  proc.kill('SIGKILL')
108
108
  }, timeoutMs)
109
109
  try {
110
- const code = await proc.exited
110
+ // Drain both pipes alongside exit: an undrained 'pipe' stdout/stderr holds
111
+ // its fd (and can deadlock a child that fills the buffer) until GC. A drain
112
+ // that rejects must not escape before the child is reaped — otherwise the
113
+ // `finally` clears the timeout while the process is still alive. Kill and
114
+ // await exit on any drain failure so the timer never disarms a live child.
115
+ const stdoutDrain = new Response(proc.stdout).text()
116
+ const stderrDrain = new Response(proc.stderr).text()
117
+ let code: number
118
+ let stderr: string
119
+ try {
120
+ ;[code, , stderr] = await Promise.all([proc.exited, stdoutDrain, stderrDrain])
121
+ } catch (err) {
122
+ proc.kill('SIGKILL')
123
+ await Promise.allSettled([proc.exited, stdoutDrain, stderrDrain])
124
+ throw err
125
+ }
111
126
  if (timedOut) return { ok: false, reason: timeoutReason(timeoutMs / 1000) }
112
127
  if (code === 0) return { ok: true }
113
- const stderr = await new Response(proc.stderr).text()
114
128
  return { ok: false, reason: failureReason(code, stderr) }
115
129
  } finally {
116
130
  clearTimeout(timeout)
@@ -156,6 +156,7 @@ async function readCapped(res: Response, maxBytes: number): Promise<string | nul
156
156
  return null
157
157
  } finally {
158
158
  await reader.cancel().catch(() => undefined)
159
+ reader.releaseLock()
159
160
  }
160
161
  }
161
162
 
@@ -5,6 +5,11 @@ import { listSessions, type ListSessionsOptions, mergeLiveSessions, type Session
5
5
 
6
6
  export type ListViewerItemsOptions = ListSessionsOptions & {
7
7
  containerRunning: boolean
8
+ // A writable row dispatches to runTuiViewer, which drives a raw-mode pi-tui
9
+ // terminal — unusable without a TTY. Required (not defaulted) so a non-TTY
10
+ // caller can't silently promote a writable row it then can't drive, which
11
+ // would auto-open a TUI on an explicit id past the picker guard.
12
+ interactive: boolean
8
13
  includeLogs?: boolean
9
14
  // Defaults to true. The detach-to-list path (after `typeclaw tui` esc) sets
10
15
  // this false: detaching ENDS the server-side session, so the just-killed
@@ -22,18 +27,19 @@ export type ViewerList = {
22
27
  }
23
28
 
24
29
  // Builds the session-viewer list. The writable TUI row is a heuristic, not an
25
- // authoritative query: when the container is up, the single most-recent
26
- // tui-origin session becomes the read+write `tui` item; every other session is
27
- // read-only. With the container down there is no live session to drive, so all
28
- // sessions are read-only. The `logs` row is appended last (container stdout,
29
- // available offline) so it sits below the divider in the picker.
30
+ // authoritative query: when the container is up AND stdin is a TTY, the single
31
+ // most-recent tui-origin session becomes the read+write `tui` item; every other
32
+ // session is read-only. With the container down (no live session to drive) or a
33
+ // non-TTY stdin (no terminal to drive it), all sessions are read-only. The
34
+ // `logs` row is appended last (container stdout, available offline) so it sits
35
+ // below the divider in the picker.
30
36
  export async function listViewerItems(opts: ListViewerItemsOptions): Promise<ViewerList> {
31
37
  const diskSessions = await listSessions(opts)
32
38
  const sessions =
33
39
  opts.liveSessions !== undefined && opts.liveSessions.length > 0
34
40
  ? mergeLiveSessions(diskSessions, opts.liveSessions)
35
41
  : diskSessions
36
- const allowWritable = opts.allowWritable !== false
42
+ const allowWritable = opts.interactive && opts.allowWritable !== false
37
43
  const writableSessionId = opts.containerRunning && allowWritable ? pickWritableSession(sessions) : null
38
44
 
39
45
  const items: ViewerItem[] = sessions.map((summary) =>
@@ -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
  }
@@ -39,36 +39,68 @@ export type ListSessionsOptions = {
39
39
  // character so empty-id filenames like `_.jsonl` don't slip through.
40
40
  const FILENAME_PATTERN = /^(?:.*_)?([^_/\\\s][^/\\\s]*)\.jsonl$/
41
41
 
42
- export async function listSessions(opts: ListSessionsOptions): Promise<SessionSummary[]> {
42
+ // Bounds concurrent per-file I/O. An agent folder can hold tens of thousands of
43
+ // session files, and an unbounded `Promise.all` over them opens one file
44
+ // descriptor per concurrent stat/stream — enough to blow past a 256 `ulimit -n`
45
+ // and crash the process with EMFILE. 32 keeps throughput high while staying well
46
+ // under any sane FD ceiling.
47
+ const SESSION_IO_CONCURRENCY = 32
48
+
49
+ // A dependency-free bounded worker pool: `concurrency` workers pull from a shared
50
+ // cursor until the list is drained, preserving input order in the result.
51
+ async function mapConcurrent<T, R>(
52
+ items: readonly T[],
53
+ concurrency: number,
54
+ fn: (item: T, index: number) => Promise<R>,
55
+ ): Promise<R[]> {
56
+ const results = new Array<R>(items.length)
57
+ let next = 0
58
+ const worker = async (): Promise<void> => {
59
+ while (true) {
60
+ const index = next++
61
+ if (index >= items.length) return
62
+ results[index] = await fn(items[index]!, index)
63
+ }
64
+ }
65
+ const workerCount = Math.min(concurrency, items.length)
66
+ await Promise.all(Array.from({ length: workerCount }, () => worker()))
67
+ return results
68
+ }
69
+
70
+ type StatEntry = { path: string; basename: string; sessionId: string; mtimeMs: number }
71
+
72
+ // Split from listSessions so resolveSession can match ids first and peek only the
73
+ // candidates, instead of reading every file body on disk. Reads no file content.
74
+ async function statSessionFiles(opts: ListSessionsOptions): Promise<StatEntry[]> {
43
75
  const entries = await readSessionFiles(opts.sessionsDir, opts.onWarn)
44
- const withStats = await Promise.all(
45
- entries.map(async (entry) => {
46
- const s = await safeStat(entry.path)
47
- if (s === null) return null
48
- const mtimeMs = s.mtimeMs
49
- if (opts.sinceMs !== undefined && mtimeMs < opts.sinceMs) return null
50
- return { ...entry, mtimeMs }
51
- }),
52
- )
53
- const valid = withStats.filter(
54
- (v): v is { path: string; basename: string; sessionId: string; mtimeMs: number } => v !== null,
55
- )
76
+ const withStats = await mapConcurrent(entries, SESSION_IO_CONCURRENCY, async (entry) => {
77
+ const s = await safeStat(entry.path)
78
+ if (s === null) return null
79
+ const mtimeMs = s.mtimeMs
80
+ if (opts.sinceMs !== undefined && mtimeMs < opts.sinceMs) return null
81
+ return { ...entry, mtimeMs }
82
+ })
83
+ const valid = withStats.filter((v): v is StatEntry => v !== null)
56
84
  valid.sort((a, b) => b.mtimeMs - a.mtimeMs)
57
- const limited = opts.limit !== undefined ? valid.slice(0, opts.limit) : valid
85
+ return valid
86
+ }
58
87
 
59
- return Promise.all(
60
- limited.map(async ({ path, basename, sessionId, mtimeMs }) => {
61
- const peek = await peekSession(path, opts.onWarn)
62
- return {
63
- sessionId,
64
- sessionFile: path,
65
- basename,
66
- mtimeMs,
67
- origin: peek.origin,
68
- firstPrompt: peek.firstPrompt,
69
- }
70
- }),
71
- )
88
+ async function summarizeEntry(entry: StatEntry, onWarn?: (msg: string) => void): Promise<SessionSummary> {
89
+ const peek = await peekSession(entry.path, onWarn)
90
+ return {
91
+ sessionId: entry.sessionId,
92
+ sessionFile: entry.path,
93
+ basename: entry.basename,
94
+ mtimeMs: entry.mtimeMs,
95
+ origin: peek.origin,
96
+ firstPrompt: peek.firstPrompt,
97
+ }
98
+ }
99
+
100
+ export async function listSessions(opts: ListSessionsOptions): Promise<SessionSummary[]> {
101
+ const valid = await statSessionFiles(opts)
102
+ const limited = opts.limit !== undefined ? valid.slice(0, opts.limit) : valid
103
+ return mapConcurrent(limited, SESSION_IO_CONCURRENCY, (entry) => summarizeEntry(entry, opts.onWarn))
72
104
  }
73
105
 
74
106
  // Overlay container-registry sessions onto the disk listing. A live session
@@ -105,17 +137,23 @@ export async function resolveSession(
105
137
  sessionIdOrPrefix: string,
106
138
  onWarn?: (msg: string) => void,
107
139
  ): Promise<ResolveResult> {
108
- const all = await listSessions({ sessionsDir, ...(onWarn !== undefined ? { onWarn } : {}) })
109
- const exact = all.find((s) => s.sessionId === sessionIdOrPrefix)
110
- if (exact !== undefined) return { ok: true, summary: exact }
140
+ // Match on stat-only metadata (filename-derived id + mtime) first, then peek
141
+ // ONLY the matched candidates. Peeking every session to find one by id/prefix
142
+ // opened a file descriptor per file — tens of thousands at once on a busy agent,
143
+ // which crashes with EMFILE. Id resolution never needs a file's body.
144
+ const entries = await statSessionFiles({ sessionsDir, ...(onWarn !== undefined ? { onWarn } : {}) })
145
+
146
+ const exact = entries.find((e) => e.sessionId === sessionIdOrPrefix)
147
+ if (exact !== undefined) return { ok: true, summary: await summarizeEntry(exact, onWarn) }
111
148
 
112
149
  if (sessionIdOrPrefix.length < MIN_PREFIX_LENGTH || !isSessionIdShape(sessionIdOrPrefix)) {
113
150
  return { ok: false, reason: 'not-found', matches: [] }
114
151
  }
115
- const prefixMatches = all.filter((s) => s.sessionId.startsWith(sessionIdOrPrefix))
152
+ const prefixMatches = entries.filter((e) => e.sessionId.startsWith(sessionIdOrPrefix))
116
153
  if (prefixMatches.length === 0) return { ok: false, reason: 'not-found', matches: [] }
117
- if (prefixMatches.length === 1) return { ok: true, summary: prefixMatches[0]! }
118
- return { ok: false, reason: 'ambiguous', matches: prefixMatches }
154
+ if (prefixMatches.length === 1) return { ok: true, summary: await summarizeEntry(prefixMatches[0]!, onWarn) }
155
+ const matches = await mapConcurrent(prefixMatches, SESSION_IO_CONCURRENCY, (e) => summarizeEntry(e, onWarn))
156
+ return { ok: false, reason: 'ambiguous', matches }
119
157
  }
120
158
 
121
159
  const SESSION_ID_SHAPE = /^[^_/\\\s][^/\\\s]*$/
package/src/mcp/tools.ts CHANGED
@@ -99,6 +99,7 @@ function createCallTool(manager: McpManager): Tool<McpCallArgs> {
99
99
  args: toolArgs,
100
100
  agentDir: ctx.agentDir,
101
101
  genericInputs: true,
102
+ logger: ctx.logger,
102
103
  signal: ctx.signal,
103
104
  })
104
105
  try {