typeclaw 0.48.9 → 0.48.10

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.
@@ -119,19 +119,24 @@ export type HostDaemonRegisterPayload = {
119
119
  }
120
120
 
121
121
  // Injected only on the daemon-owned restart path (reuseCurrentHostDaemon).
122
- // The daemon registers the container in-process and reports its own HTTP port,
123
- // so the restart skips the `http-info`/`register` self-RPCs — those round-trips
124
- // can time out under IPC congestion and silently drop the TYPECLAW_HOSTD_* env
125
- // triple, booting a container whose hostd-bridged adapters can't construct.
122
+ // The daemon updates the container registration in-process and reports its own
123
+ // HTTP port, so restart lifecycle changes skip socket self-RPCs — those
124
+ // round-trips can time out under IPC congestion and silently drop the
125
+ // TYPECLAW_HOSTD_* env triple or leave stale broker state behind.
126
126
  export type CurrentHostDaemon = {
127
127
  httpPort: number
128
128
  register: (payload: HostDaemonRegisterPayload) => Promise<{ ok: true } | { ok: false; reason: string }>
129
+ deregister: (containerName: string) => Promise<void>
129
130
  }
130
131
 
131
132
  export type StartOptions = {
132
133
  cwd: string
133
134
  preferredHostPort: number
134
135
  forceBuild?: boolean
136
+ // A parent live renderer must be the terminal's only cursor owner. Compose
137
+ // disables process output so child progress and warnings cannot corrupt it.
138
+ streamOutput?: boolean
139
+ onWarning?: (warning: string) => void
135
140
  exec?: DockerExec
136
141
  // Test seam: allows tests to inject a deterministic port allocator. In
137
142
  // production we go through the real kernel via `findFreePort`.
@@ -140,9 +145,10 @@ export type StartOptions = {
140
145
  // Hostd's supervisor restart callback already runs inside the daemon process.
141
146
  // Reusing that daemon avoids a self-shutdown when disk source has drifted.
142
147
  reuseCurrentHostDaemon?: boolean
143
- // Set by hostd's supervisor restart wrapper. When present, registration goes
144
- // through the daemon in-process (no socket round-trips), so the env triple is
145
- // injected from known-good values even under IPC congestion. See type docs.
148
+ // Set by hostd's supervisor restart wrapper. When present, registration
149
+ // lifecycle changes go through the daemon in-process (no socket round-trips),
150
+ // so known-good control values and rollback survive IPC congestion.
151
+ // See type docs.
146
152
  currentHostDaemon?: CurrentHostDaemon
147
153
  ensureDeps?: (cwd: string, opts?: { force?: boolean }) => Promise<EnsureDepsResult>
148
154
  // Test seam for host embedding model provisioning. Production callers use
@@ -242,6 +248,8 @@ async function runStart({
242
248
  cwd,
243
249
  preferredHostPort,
244
250
  forceBuild = false,
251
+ streamOutput = true,
252
+ onWarning,
245
253
  exec = defaultDockerExec,
246
254
  allocatePort = findFreePort,
247
255
  cliEntry,
@@ -327,7 +335,11 @@ async function runStart({
327
335
  }
328
336
 
329
337
  const githubCliDeniedRoots = resolveGithubCliDeniedRoots(cwd, await loadTypeclawConfig(cwd))
330
- await refreshGithubCliStore(cwd, githubCliDeniedRoots, provisionGithubCliStoreForAgent)
338
+ const githubCliWarning = await refreshGithubCliStore(cwd, githubCliDeniedRoots, provisionGithubCliStoreForAgent)
339
+ if (githubCliWarning !== null) {
340
+ if (streamOutput) process.stderr.write(githubCliWarning)
341
+ else onWarning?.(githubCliWarning.trim())
342
+ }
331
343
 
332
344
  if (agentMessengerPolicy.migrate) {
333
345
  const agentMessengerMigration = await migrateAgentMessengerConfigDir(cwd)
@@ -551,16 +563,23 @@ async function runStart({
551
563
 
552
564
  let built = false
553
565
  if (plan.needsBuild) {
554
- const buildOk = await runImageBuild({
566
+ const buildResult = await runImageBuild({
555
567
  exec,
556
568
  cwd,
557
569
  imageTag: plan.imageTag,
558
570
  buildContext: plan.buildContext,
559
571
  hasBuildx,
572
+ streamOutput,
560
573
  })
561
- if (!buildOk) {
574
+ if (!buildResult.ok) {
562
575
  await cleanupHostDaemonRegistration(containerName, hostd)
563
- return { ok: false, reason: 'docker build failed' }
576
+ const retryDetail = buildResult.credentialHelperRetried
577
+ ? ' The build was also retried without the configured Docker credential helper.'
578
+ : ''
579
+ const detail = streamOutput
580
+ ? ''
581
+ : ` (exit code ${buildResult.exitCode}). Run \`typeclaw start --build\` from this agent directory to view the full build output.${retryDetail}`
582
+ return { ok: false, reason: `docker build failed${detail}` }
564
583
  }
565
584
  built = true
566
585
  }
@@ -575,6 +594,44 @@ async function runStart({
575
594
  }
576
595
  }
577
596
 
597
+ // The build and model provisioning above can outlive a hostd registration.
598
+ // Refresh both the registry entry and the launch plan at the run boundary:
599
+ // a broker restart may have invalidated the initial control tokens while
600
+ // those pre-run steps were in flight. Keep that early registration because
601
+ // it remains the cleanup target for build/model failures.
602
+ const initialHostd = hostd
603
+ try {
604
+ if (cliEntry) {
605
+ const refreshedHostd = await registerWithDaemon({
606
+ cwd,
607
+ containerName,
608
+ cliEntry,
609
+ hostPort,
610
+ reuseCurrentHostDaemon,
611
+ currentHostDaemon,
612
+ })
613
+ if (refreshedHostd.state !== 'registered') {
614
+ await cleanupHostDaemonRegistration(containerName, initialHostd)
615
+ }
616
+ hostd = refreshedHostd
617
+ hostdControl = refreshedHostd.state === 'registered' ? refreshedHostd.control : undefined
618
+ plan = await planStart({
619
+ cwd,
620
+ hostPort,
621
+ imageExists: true,
622
+ forceBuild: false,
623
+ hostdControl,
624
+ publishHost,
625
+ tuiToken,
626
+ platform,
627
+ hostIdentity,
628
+ })
629
+ }
630
+ } catch (error) {
631
+ await cleanupHostDaemonRegistration(containerName, hostd.state === 'registered' ? hostd : initialHostd)
632
+ return { ok: false, reason: error instanceof Error ? error.message : String(error) }
633
+ }
634
+
578
635
  let run = await execRunWithConflictRetry(exec, plan.runArgs, cwd, containerName, archiveBeforeRemove)
579
636
 
580
637
  // TOCTOU: another process may have grabbed the port between our probe and
@@ -616,7 +673,8 @@ async function runStart({
616
673
  }
617
674
  hostPort = await allocatePort(0)
618
675
  if (cliEntry) {
619
- hostd = await registerWithDaemon({
676
+ const previousHostd = hostd
677
+ const retriedHostd = await registerWithDaemon({
620
678
  cwd,
621
679
  containerName,
622
680
  cliEntry,
@@ -624,7 +682,11 @@ async function runStart({
624
682
  reuseCurrentHostDaemon,
625
683
  currentHostDaemon,
626
684
  })
627
- hostdControl = hostd.state === 'registered' ? hostd.control : undefined
685
+ if (retriedHostd.state !== 'registered') {
686
+ await cleanupHostDaemonRegistration(containerName, previousHostd)
687
+ }
688
+ hostd = retriedHostd
689
+ hostdControl = retriedHostd.state === 'registered' ? retriedHostd.control : undefined
628
690
  }
629
691
  plan = await planStart({
630
692
  cwd,
@@ -674,19 +736,19 @@ async function refreshGithubCliStore(
674
736
  agentDir: string,
675
737
  deniedRoots: readonly string[],
676
738
  provision: (options: ProvisionGithubCliStoreOptions) => GithubCliProvisionResult | Promise<GithubCliProvisionResult>,
677
- ): Promise<void> {
739
+ ): Promise<string | null> {
678
740
  let refreshed = false
679
741
  try {
680
742
  refreshed = (await provision({ agentDir, deniedRoots })).ok
681
743
  } catch {
682
744
  refreshed = false
683
745
  }
684
- if (refreshed) return
746
+ if (refreshed) return null
685
747
 
686
- process.stderr.write(
748
+ return (
687
749
  'typeclaw: warning: Could not refresh GitHub CLI credentials from the host. ' +
688
- 'Keeping the previously persisted credential store. Run `gh auth login --hostname github.com` on the host, ' +
689
- 'then restart TypeClaw.\n',
750
+ 'Keeping the previously persisted credential store. Run `gh auth login --hostname github.com` on the host, ' +
751
+ 'then restart TypeClaw.\n'
690
752
  )
691
753
  }
692
754
 
@@ -1042,8 +1104,9 @@ async function runImageBuild(args: {
1042
1104
  imageTag: string
1043
1105
  buildContext: string
1044
1106
  hasBuildx: boolean
1045
- }): Promise<boolean> {
1046
- const { exec, cwd, imageTag, buildContext, hasBuildx } = args
1107
+ streamOutput: boolean
1108
+ }): Promise<{ ok: true } | { ok: false; exitCode: number; credentialHelperRetried: boolean }> {
1109
+ const { exec, cwd, imageTag, buildContext, hasBuildx, streamOutput } = args
1047
1110
  const buildArgv = (frontend: 'buildx' | 'legacy'): string[] =>
1048
1111
  frontend === 'buildx'
1049
1112
  ? ['buildx', 'build', '--load', '-t', imageTag, buildContext]
@@ -1051,12 +1114,17 @@ async function runImageBuild(args: {
1051
1114
 
1052
1115
  let sanitizedConfig: SanitizedDockerConfig | null = null
1053
1116
  const attempt = async (frontend: 'buildx' | 'legacy'): Promise<DockerExecResult> =>
1054
- exec(buildArgv(frontend), { cwd, inheritStdio: true, captureStderr: true, env: sanitizedConfig?.env })
1117
+ exec(buildArgv(frontend), {
1118
+ cwd,
1119
+ ...(streamOutput ? { inheritStdio: true, captureStderr: true } : {}),
1120
+ ...(streamOutput ? {} : { captureStdout: false, maxCapturedStderrBytes: 32 * 1024 }),
1121
+ env: sanitizedConfig?.env,
1122
+ })
1055
1123
 
1056
1124
  try {
1057
1125
  let frontend: 'buildx' | 'legacy' = hasBuildx ? 'buildx' : 'legacy'
1058
1126
  let result = await attempt(frontend)
1059
- if (result.exitCode === 0) return true
1127
+ if (result.exitCode === 0) return { ok: true }
1060
1128
 
1061
1129
  // Same-frontend retry: a broken credential helper aborts the pull before
1062
1130
  // the builder ever matters, so strip it and retry the identical build.
@@ -1064,7 +1132,7 @@ async function runImageBuild(args: {
1064
1132
  sanitizedConfig = await createSanitizedDockerConfig()
1065
1133
  if (sanitizedConfig) {
1066
1134
  result = await attempt(frontend)
1067
- if (result.exitCode === 0) return true
1135
+ if (result.exitCode === 0) return { ok: true }
1068
1136
  }
1069
1137
  }
1070
1138
 
@@ -1075,23 +1143,23 @@ async function runImageBuild(args: {
1075
1143
  await refreshDockerfile(cwd, { buildKit: false })
1076
1144
  frontend = 'legacy'
1077
1145
  result = await attempt(frontend)
1078
- if (result.exitCode === 0) return true
1146
+ if (result.exitCode === 0) return { ok: true }
1079
1147
  if (sanitizedConfig === null && isMissingDockerCredentialHelper(result.stderr)) {
1080
1148
  sanitizedConfig = await createSanitizedDockerConfig()
1081
1149
  if (sanitizedConfig) {
1082
1150
  result = await attempt(frontend)
1083
- if (result.exitCode === 0) return true
1151
+ if (result.exitCode === 0) return { ok: true }
1084
1152
  }
1085
1153
  }
1086
1154
  }
1087
1155
 
1088
- if (sanitizedConfig !== null) {
1089
- process.stderr.write(
1156
+ if (sanitizedConfig !== null && streamOutput) {
1157
+ const guidance =
1090
1158
  'typeclaw: docker build still failed after retrying public image pulls without the configured ' +
1091
- 'credential helper. Your ~/.docker/config.json credsStore/credHelpers may be broken.\n',
1092
- )
1159
+ 'credential helper. Your ~/.docker/config.json credsStore/credHelpers may be broken.'
1160
+ process.stderr.write(`${guidance}\n`)
1093
1161
  }
1094
- return false
1162
+ return { ok: false, exitCode: result.exitCode, credentialHelperRetried: sanitizedConfig !== null }
1095
1163
  } finally {
1096
1164
  await sanitizedConfig?.cleanup()
1097
1165
  }
@@ -1448,6 +1516,7 @@ async function registerWithDaemon({
1448
1516
  return {
1449
1517
  state: 'registered',
1450
1518
  control: { url: `http://${CONTAINER_HOSTD_HOST}:${currentHostDaemon.httpPort}`, token, brokerToken },
1519
+ deregister: (registeredContainerName) => currentHostDaemon.deregister(registeredContainerName),
1451
1520
  }
1452
1521
  }
1453
1522
 
@@ -1458,6 +1527,9 @@ async function registerWithDaemon({
1458
1527
  return {
1459
1528
  state: 'registered',
1460
1529
  control: { url: `http://${CONTAINER_HOSTD_HOST}:${prepared.httpPort}`, token, brokerToken },
1530
+ deregister: async (registeredContainerName) => {
1531
+ await sendToDaemon({ kind: 'deregister', containerName: registeredContainerName })
1532
+ },
1461
1533
  }
1462
1534
  }
1463
1535
 
@@ -1492,7 +1564,7 @@ async function useCurrentHostDaemon(): Promise<{ ok: true; httpPort: number } |
1492
1564
  }
1493
1565
 
1494
1566
  type PreparedHostDaemonStatus =
1495
- | { state: 'registered'; control: HostDaemonControl }
1567
+ | { state: 'registered'; control: HostDaemonControl; deregister: (containerName: string) => Promise<void> }
1496
1568
  | { state: 'unavailable'; reason: string }
1497
1569
  | { state: 'disabled' }
1498
1570
 
@@ -1503,7 +1575,7 @@ function stripHostDaemonControl(status: PreparedHostDaemonStatus): HostDaemonSta
1503
1575
 
1504
1576
  async function cleanupHostDaemonRegistration(containerName: string, status: PreparedHostDaemonStatus): Promise<void> {
1505
1577
  if (status.state !== 'registered') return
1506
- await sendToDaemon({ kind: 'deregister', containerName }).catch(() => {})
1578
+ await status.deregister(containerName).catch(() => {})
1507
1579
  }
1508
1580
 
1509
1581
  // process.env.TZ is honored first because users who explicitly set it (e.g.
@@ -323,6 +323,8 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
323
323
  const cwds = new Map<string, string>()
324
324
  const restartTokens = new Map<string, string>()
325
325
  const brokerTokens = new Map<string, string>()
326
+ const registrationGenerations = new Map<string, number>()
327
+ let nextRegistrationGeneration = 0
326
328
  const perContainerSerial = new Map<string, Promise<unknown>>()
327
329
  const gcMisses = new Map<string, number>()
328
330
  let stopped = false
@@ -398,6 +400,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
398
400
  brokerTokens.delete(containerName)
399
401
  cwds.delete(containerName)
400
402
  restartTokens.delete(containerName)
403
+ registrationGenerations.delete(containerName)
401
404
  gcMisses.delete(containerName)
402
405
  if (opts.portbroker)
403
406
  await withCleanupTimeout(opts.portbroker.stop(containerName, 'fatal-auth'), cleanupStepTimeoutMs)
@@ -410,6 +413,8 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
410
413
 
411
414
  const applyRegistration = async (payload: RegisterPayload): Promise<void> => {
412
415
  const alreadyRegistered = cwds.has(payload.containerName)
416
+ registrationGenerations.set(payload.containerName, ++nextRegistrationGeneration)
417
+ gcMisses.delete(payload.containerName)
413
418
  cwds.set(payload.containerName, payload.cwd)
414
419
  if (payload.restartToken) restartTokens.set(payload.containerName, payload.restartToken)
415
420
  else restartTokens.delete(payload.containerName)
@@ -471,6 +476,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
471
476
  const hadCwd = cwds.delete(req.containerName)
472
477
  restartTokens.delete(req.containerName)
473
478
  brokerTokens.delete(req.containerName)
479
+ registrationGenerations.delete(req.containerName)
474
480
  gcMisses.delete(req.containerName)
475
481
  if (opts.portbroker)
476
482
  await withCleanupTimeout(opts.portbroker.stop(req.containerName, 'deregistered'), cleanupStepTimeoutMs)
@@ -500,9 +506,9 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
500
506
  return { ok: true, result }
501
507
  }
502
508
 
503
- // Lets the supervisor's restart re-register the child in-process instead of
504
- // over the socket. Awaits restore for the same no-mutation-before-restore
505
- // invariant the socket dispatcher enforces before handleRegister.
509
+ // Lets the supervisor's restart update child registration lifecycle
510
+ // in-process instead of over the socket. Both callbacks await restore for
511
+ // the same no-mutation-before-restore invariant as the socket dispatcher.
506
512
  const registerCurrentChild = async (
507
513
  payload: HostDaemonRegisterPayload,
508
514
  ): Promise<{ ok: true } | { ok: false; reason: string }> => {
@@ -511,6 +517,11 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
511
517
  return reply.ok ? { ok: true } : { ok: false, reason: reply.reason }
512
518
  }
513
519
 
520
+ const deregisterCurrentChild = async (containerName: string): Promise<void> => {
521
+ await awaitRestored()
522
+ await handleDeregister({ containerName })
523
+ }
524
+
514
525
  // Auth: only restart containers that registered with this daemon. The
515
526
  // socket is 0o600 + UID-bound, but inside a container any process that
516
527
  // reaches the mounted socket could otherwise restart any peer container on
@@ -531,7 +542,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
531
542
  containerName: req.containerName,
532
543
  cwd,
533
544
  build: req.build,
534
- currentHostDaemon: { httpPort, register: registerCurrentChild },
545
+ currentHostDaemon: { httpPort, register: registerCurrentChild, deregister: deregisterCurrentChild },
535
546
  })
536
547
  if (!ack.ok) return ack
537
548
  const result: RestartResult = { containerName: req.containerName, scheduled: true }
@@ -790,7 +801,11 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
790
801
  }
791
802
  httpPort = httpServer.port ?? 0
792
803
  log({ kind: 'daemon-http-listening', host: httpHostname, port: httpPort })
793
- opts.currentHostDaemonHolder?.set({ httpPort, register: registerCurrentChild })
804
+ opts.currentHostDaemonHolder?.set({
805
+ httpPort,
806
+ register: registerCurrentChild,
807
+ deregister: deregisterCurrentChild,
808
+ })
794
809
 
795
810
  const sockets = new Set<NetSocket>()
796
811
  const listener = createServer((socket) => {
@@ -812,28 +827,36 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
812
827
 
813
828
  const runGc = async (): Promise<void> => {
814
829
  for (const name of Array.from(cwds.keys())) {
830
+ const generation = registrationGenerations.get(name)
831
+ if (generation === undefined) continue
815
832
  const status = await probeContainerAlive(name)
816
- if (status === 'alive') {
817
- gcMisses.delete(name)
818
- continue
819
- }
820
- if (status === 'unknown') continue
821
- const misses = (gcMisses.get(name) ?? 0) + 1
822
- if (misses < gcMissesToDeregister) {
823
- gcMisses.set(name, misses)
824
- continue
825
- }
826
- gcMisses.delete(name)
827
833
  void runSerially(name, async () => {
834
+ // A probe is only authoritative for the registration it observed before
835
+ // awaiting Docker. Re-registration installs a new generation before any
836
+ // later async registration work, so stale probes cannot spend its miss
837
+ // budget or remove it.
838
+ if (registrationGenerations.get(name) !== generation) return
839
+ if (status === 'alive') {
840
+ gcMisses.delete(name)
841
+ return
842
+ }
843
+ if (status === 'unknown') return
844
+ const misses = (gcMisses.get(name) ?? 0) + 1
845
+ if (misses < gcMissesToDeregister) {
846
+ gcMisses.set(name, misses)
847
+ return
848
+ }
849
+ gcMisses.delete(name)
828
850
  const hadCwd = cwds.delete(name)
829
851
  restartTokens.delete(name)
852
+ brokerTokens.delete(name)
853
+ registrationGenerations.delete(name)
830
854
  if (opts.portbroker) await withCleanupTimeout(opts.portbroker.stop(name, 'deregistered'), cleanupStepTimeoutMs)
831
855
  if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(name), cleanupStepTimeoutMs)
832
856
  if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(name), cleanupStepTimeoutMs)
833
857
  if (opts.teamsRenewal) await withCleanupTimeout(opts.teamsRenewal.stop(name), cleanupStepTimeoutMs)
834
858
  await removeRegistrationFile(name)
835
859
  if (hadCwd) log({ kind: 'deregister', containerName: name, reason: 'gone' })
836
- return { ok: true }
837
860
  })
838
861
  }
839
862
  }
@@ -89,6 +89,21 @@ function isValidSessionFileBasename(name: string): boolean {
89
89
  return name.endsWith('.jsonl')
90
90
  }
91
91
 
92
+ function errorChainHasCode(error: unknown, expectedCode: string): boolean {
93
+ const seen = new Set<object>()
94
+ let current = error
95
+ while (typeof current === 'object' && current !== null && !seen.has(current)) {
96
+ seen.add(current)
97
+ try {
98
+ if ('code' in current && current.code === expectedCode) return true
99
+ current = 'cause' in current ? current.cause : undefined
100
+ } catch {
101
+ return false
102
+ }
103
+ }
104
+ return false
105
+ }
106
+
92
107
  // The production wiring for channel-routed sessions. Channel inbounds arrive
93
108
  // at the router, the router calls this factory to get an AgentSession, and
94
109
  // the agent uses `channel_send` to reply. If `channelRouter` is missing here
@@ -181,41 +196,46 @@ function tryReopenOrCreate(
181
196
  logger: FactoryLogger,
182
197
  ): SessionManager {
183
198
  if (existingSessionFile === undefined) {
184
- logger.warn(
185
- `[channels] session ${existingSessionId} has no sessionFile (v2 mapping not yet migrated); creating new`,
186
- )
199
+ logger.warn('[channels] persisted session has no session file; creating new')
187
200
  return SessionManager.create(cwd, sessionDir)
188
201
  }
189
202
  if (!isValidSessionFileBasename(existingSessionFile)) {
190
- logger.warn(
191
- `[channels] session ${existingSessionId} has invalid sessionFile (${JSON.stringify(existingSessionFile)}); creating new`,
192
- )
203
+ logger.warn('[channels] persisted session file is invalid; creating new')
193
204
  return SessionManager.create(cwd, sessionDir)
194
205
  }
195
206
  const path = join(sessionDir, existingSessionFile)
207
+ let missingDuringCap = false
196
208
  if (capOptions !== null) {
197
209
  try {
198
210
  const stats = capJsonlFileInPlace(path, capOptions)
199
211
  if (stats.entriesMutated > 0) {
200
212
  logger.info(
201
- `[channels] rehydrate-cap ${existingSessionFile}: entriesMutated=${stats.entriesMutated} imagesReplaced=${stats.imagesReplaced} textsTruncated=${stats.textsTruncated} bytesElided=${stats.bytesElided}`,
213
+ `[channels] rehydrate-cap complete: entriesMutated=${stats.entriesMutated} imagesReplaced=${stats.imagesReplaced} textsTruncated=${stats.textsTruncated} bytesElided=${stats.bytesElided}`,
202
214
  )
203
215
  }
204
216
  } catch (err) {
205
217
  // Capping is best-effort: if the rewrite fails, fall through to the
206
218
  // regular open path so the session still rehydrates uncapped rather
207
- // than being killed by a transient FS error.
208
- const reason = err instanceof Error ? err.message : String(err)
209
- logger.warn(`[channels] rehydrate-cap failed for ${existingSessionFile}: ${reason}; continuing with open`)
219
+ // than being killed by a transient FS error. Missing files are expected
220
+ // during concurrent cleanup; open either creates a fresh transcript or
221
+ // reaches the fallback below.
222
+ if (errorChainHasCode(err, 'ENOENT')) {
223
+ missingDuringCap = true
224
+ } else {
225
+ logger.warn('[channels] rehydrate-cap unavailable; continuing with open')
226
+ }
210
227
  }
211
228
  }
212
229
  try {
213
- return SessionManager.open(path)
230
+ const sessionManager = SessionManager.open(path)
231
+ if (sessionManager.getSessionId() !== existingSessionId && !missingDuringCap) {
232
+ logger.warn('[channels] persisted session was replaced during rehydrate')
233
+ }
234
+ return sessionManager
214
235
  } catch (err) {
215
- const reason = err instanceof Error ? err.message : String(err)
216
- logger.warn(
217
- `[channels] could not rehydrate session ${existingSessionId} from ${existingSessionFile}: ${reason}; creating new`,
218
- )
236
+ if (!errorChainHasCode(err, 'ENOENT')) {
237
+ logger.warn('[channels] persisted session rehydrate failed; creating new')
238
+ }
219
239
  return SessionManager.create(cwd, sessionDir)
220
240
  }
221
241
  }