typeclaw 0.46.0 → 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 (52) 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 +396 -136
  7. package/src/agent/tools/webfetch/fetch.ts +22 -2
  8. package/src/agent/tools-without-local-file-operands.ts +9 -6
  9. package/src/bundled-plugins/backup/README.md +23 -10
  10. package/src/bundled-plugins/backup/git-auth.ts +6 -4
  11. package/src/bundled-plugins/backup/index.ts +83 -43
  12. package/src/bundled-plugins/backup/runner.ts +83 -0
  13. package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
  14. package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
  15. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
  16. package/src/bundled-plugins/github-cli-auth/index.ts +110 -31
  17. package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
  18. package/src/bundled-plugins/memory/README.md +9 -0
  19. package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
  20. package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
  21. package/src/bundled-plugins/researcher/write-report.ts +3 -3
  22. package/src/bundled-plugins/security/policies/private-surface-read.ts +610 -50
  23. package/src/bundled-plugins/security/policies/ssrf.ts +119 -34
  24. package/src/channels/adapters/github/inbound.ts +11 -0
  25. package/src/channels/adapters/github/index.ts +48 -54
  26. package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
  27. package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
  28. package/src/channels/adapters/slack-bot-edit.ts +85 -20
  29. package/src/channels/adapters/slack-bot.ts +7 -2
  30. package/src/channels/adapters/slack-format.ts +245 -0
  31. package/src/channels/github-token-bridge.ts +2 -3
  32. package/src/container/docker-app.ts +42 -13
  33. package/src/container/start.ts +13 -0
  34. package/src/cron/bridge.ts +9 -2
  35. package/src/doctor/checks.ts +5 -2
  36. package/src/git/bound-gc-config.ts +82 -0
  37. package/src/hostd/spawn.ts +133 -47
  38. package/src/init/dockerfile.ts +13 -0
  39. package/src/inspect/replay.ts +21 -9
  40. package/src/inspect/session-list.ts +71 -33
  41. package/src/mcp/tools.ts +1 -0
  42. package/src/network/internal-destinations.ts +267 -0
  43. package/src/plugin/types.ts +3 -2
  44. package/src/sandbox/env-exposure.ts +18 -15
  45. package/src/sandbox/policy.ts +5 -3
  46. package/src/sandbox/privileged-runtime.ts +1 -0
  47. package/src/skills/typeclaw-config/SKILL.md +22 -6
  48. package/src/skills/typeclaw-permissions/SKILL.md +22 -9
  49. package/src/skills/typeclaw-plugins/SKILL.md +37 -0
  50. package/src/usage/scan.ts +11 -2
  51. package/typeclaw.schema.json +5 -0
  52. package/src/git/secret-history.ts +0 -371
@@ -0,0 +1,82 @@
1
+ import { hooklessGitArgs } from './hookless'
2
+ import { resolveAgentGit } from './resolve-agent-git'
3
+
4
+ // Machine-local `git config` written into the agent repo on every `typeclaw
5
+ // start` to bound git's gc/repack/pack-objects memory. Agents force-commit
6
+ // sessions/ + memory/, so packs grow to GB scale; on such a repo git's default
7
+ // auto-gc was measured at ~1.8GB RSS and OOM-killed the container. These keys cap
8
+ // the peak (git-config(1): peak ≈ pack.threads × pack.windowMemory + overhead).
9
+ // Do NOT "modernize" any back to a git default without re-checking the OOM:
10
+ // gc.auto/autoPackLimit=0 no surprise full repack from the backup's `git
11
+ // commit`; reclamation moves to the idle backup path
12
+ // (see bound-git-maintenance / Fix 2).
13
+ // pack.threads=1 default auto-detects to CPU count; windowMemory is
14
+ // PER THREAD, so N cores = N× the spike.
15
+ // pack.windowMemory=64m default is UNLIMITED — the primary per-thread bound.
16
+ // pack.deltaCacheSize=1 virtually disables the 256MB write-phase delta cache.
17
+ // core.bigFileThreshold=10m load-bearing for append-only JSONL: bigger objects
18
+ // store plain-deflated with NO delta attempt, keeping
19
+ // transcripts out of the memory-heavy delta pipeline.
20
+ // core.multiPackIndex=true required for Fix 2's `maintenance incremental-repack`.
21
+ export const BOUND_GC_CONFIG: ReadonlyArray<readonly [key: string, value: string]> = [
22
+ ['gc.auto', '0'],
23
+ ['gc.autoPackLimit', '0'],
24
+ ['gc.bigPackThreshold', '100m'],
25
+ ['pack.threads', '1'],
26
+ ['pack.windowMemory', '64m'],
27
+ ['pack.deltaCacheSize', '1'],
28
+ ['pack.window', '5'],
29
+ ['pack.depth', '10'],
30
+ ['core.bigFileThreshold', '10m'],
31
+ ['core.deltaBaseCacheLimit', '16m'],
32
+ ['core.multiPackIndex', 'true'],
33
+ ] as const
34
+
35
+ // Writes BOUND_GC_CONFIG into the agent repo's machine-local git config.
36
+ //
37
+ // Best-effort, exactly like untrackTrulyIgnoredFiles / commitSystemFile: no-ops
38
+ // when the folder is not a git repo or Bun is missing, and NEVER throws — start()
39
+ // hygiene must not block boot. These are `git config` (local scope) writes: they
40
+ // live in .git/config, are not tracked, not committed, and are idempotent —
41
+ // re-running just re-sets the same values.
42
+ //
43
+ // Returns the number of keys successfully written (0 when skipped) so callers /
44
+ // tests can assert the write happened without coupling to git internals.
45
+ export async function applyBoundGcConfig(cwd: string): Promise<{ applied: number }> {
46
+ const bun = getBun()
47
+ if (!bun) return { applied: 0 }
48
+ const repo = resolveAgentGit(cwd)
49
+ if (!repo) return { applied: 0 }
50
+
51
+ let applied = 0
52
+ for (const [key, value] of BOUND_GC_CONFIG) {
53
+ if (await setLocalConfig(bun, cwd, repo.gitArgs, key, value)) applied += 1
54
+ }
55
+ return { applied }
56
+ }
57
+
58
+ async function setLocalConfig(
59
+ bun: BunLike,
60
+ cwd: string,
61
+ gitArgs: readonly string[],
62
+ key: string,
63
+ value: string,
64
+ ): Promise<boolean> {
65
+ try {
66
+ const proc = bun.spawn({
67
+ cmd: ['git', ...hooklessGitArgs([...gitArgs, 'config', '--local', key, value])],
68
+ cwd,
69
+ stdout: 'pipe',
70
+ stderr: 'pipe',
71
+ })
72
+ return (await proc.exited) === 0
73
+ } catch {
74
+ return false
75
+ }
76
+ }
77
+
78
+ type BunLike = { spawn: typeof Bun.spawn }
79
+
80
+ function getBun(): BunLike | undefined {
81
+ return (globalThis as { Bun?: BunLike }).Bun
82
+ }
@@ -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 {
@@ -673,6 +674,15 @@ RUN echo "${encoded}" | base64 -d > ${TYPECLAW_ENTRYPOINT_PATH} \\
673
674
  && chmod +x ${TYPECLAW_ENTRYPOINT_PATH}`
674
675
  }
675
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
+
676
686
  // Layer 7: install @huggingface/transformers with its linux-native binaries.
677
687
  //
678
688
  // The host node_modules is bind-mounted over /agent at runtime carrying
@@ -1418,6 +1428,7 @@ WORKDIR /agent
1418
1428
  ARG TARGETARCH
1419
1429
 
1420
1430
  ${ghKeyringLayer}${toggleAptLayer}${cloudflaredBlock}${claudeCodeBlock}${codexCliBlock}${renderEntrypointShimLayer()}
1431
+ ${renderGitAskPassLayer()}
1421
1432
 
1422
1433
  ${LAYER_TRANSFORMERS_INSTALL}
1423
1434
 
@@ -1481,6 +1492,7 @@ ${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
1481
1492
  ${renderChromeForTestingLayer(buildKit)}
1482
1493
 
1483
1494
  ${cloudflaredBlock}${claudeCodeBlock}${codexCliBlock}${renderEntrypointShimLayer()}
1495
+ ${renderGitAskPassLayer()}
1484
1496
 
1485
1497
  ${LAYER_TRANSFORMERS_INSTALL}
1486
1498
 
@@ -1558,6 +1570,7 @@ ${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
1558
1570
  ${renderChromeForTestingLayer(true)}
1559
1571
 
1560
1572
  ${renderEntrypointShimLayer()}
1573
+ ${renderGitAskPassLayer()}
1561
1574
 
1562
1575
  ${LAYER_TRANSFORMERS_INSTALL}
1563
1576
  `
@@ -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 {