typeclaw 0.39.7 → 0.41.0

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 (72) hide show
  1. package/package.json +1 -1
  2. package/scripts/dump-system-prompt.ts +4 -50
  3. package/src/agent/auth-reloadable.ts +24 -0
  4. package/src/agent/auth.ts +12 -0
  5. package/src/agent/index.ts +18 -58
  6. package/src/agent/multimodal/look-at.ts +87 -0
  7. package/src/bundled-plugins/guard/policies/non-workspace-write.ts +0 -2
  8. package/src/bundled-plugins/guard/policy.ts +0 -5
  9. package/src/bundled-plugins/memory/README.md +26 -37
  10. package/src/bundled-plugins/memory/index.ts +32 -147
  11. package/src/bundled-plugins/memory/load-memory.ts +7 -121
  12. package/src/bundled-plugins/memory/memory-logger.ts +8 -2
  13. package/src/bundled-plugins/memory/search-command.ts +2 -11
  14. package/src/bundled-plugins/memory/vector/doctor.ts +1 -1
  15. package/src/bundled-plugins/memory/vector/embedder.ts +4 -5
  16. package/src/bundled-plugins/memory/vector/hybrid.ts +47 -5
  17. package/src/bundled-plugins/memory/vector/index-on-write.ts +25 -20
  18. package/src/bundled-plugins/memory/vector/reference-index-on-write.ts +28 -23
  19. package/src/bundled-plugins/memory/vector/relevance-gate.ts +11 -7
  20. package/src/bundled-plugins/memory/vector/script.ts +87 -0
  21. package/src/bundled-plugins/memory/vector/store.ts +2 -0
  22. package/src/bundled-plugins/planner/planner.ts +4 -2
  23. package/src/bundled-plugins/reviewer/reviewer.ts +9 -1
  24. package/src/channels/adapters/describe-error.ts +40 -0
  25. package/src/channels/adapters/discord.ts +9 -12
  26. package/src/channels/adapters/kakaotalk-classify.ts +31 -6
  27. package/src/channels/adapters/kakaotalk.ts +15 -18
  28. package/src/channels/adapters/line.ts +11 -14
  29. package/src/channels/adapters/slack-bot.ts +15 -18
  30. package/src/channels/adapters/slack.ts +45 -21
  31. package/src/channels/adapters/telegram-bot.ts +15 -18
  32. package/src/channels/adapters/webex-bot.ts +13 -14
  33. package/src/channels/adapters/webex.ts +14 -15
  34. package/src/cli/compose.ts +20 -0
  35. package/src/cli/docker-preflight.ts +42 -0
  36. package/src/cli/hostd.ts +12 -3
  37. package/src/cli/init.ts +24 -22
  38. package/src/cli/logs.ts +7 -0
  39. package/src/cli/provider.ts +8 -1
  40. package/src/cli/reload.ts +6 -0
  41. package/src/cli/restart.ts +7 -0
  42. package/src/cli/shell.ts +7 -0
  43. package/src/cli/start.ts +7 -0
  44. package/src/cli/status.ts +9 -0
  45. package/src/cli/stop.ts +7 -0
  46. package/src/cli/tui.ts +6 -0
  47. package/src/config/config.ts +31 -8
  48. package/src/container/docker-app.ts +253 -0
  49. package/src/container/index.ts +11 -0
  50. package/src/container/start.ts +85 -41
  51. package/src/hostd/current-host-daemon.ts +21 -0
  52. package/src/hostd/daemon.ts +75 -15
  53. package/src/hostd/kakao-renewal-manager.ts +35 -10
  54. package/src/hostd/supervisor.ts +18 -3
  55. package/src/hostd/tailscale.ts +41 -5
  56. package/src/hostd/webex-renewal-manager.ts +13 -4
  57. package/src/plugin/hooks.ts +3 -4
  58. package/src/plugin/index.ts +1 -0
  59. package/src/plugin/manager.ts +14 -0
  60. package/src/plugin/registry.ts +12 -0
  61. package/src/plugin/types.ts +5 -1
  62. package/src/run/channel-session-factory.ts +3 -5
  63. package/src/run/index.ts +63 -36
  64. package/src/server/command-runner.ts +2 -11
  65. package/src/skills/typeclaw-memory/SKILL.md +2 -2
  66. package/src/skills/typeclaw-plugins/SKILL.md +1 -1
  67. package/src/test-helpers/rm-temp-dir.ts +24 -0
  68. package/typeclaw.schema.json +1 -23
  69. package/src/bundled-plugins/guard/policies/memory-retrieval-cache-write.ts +0 -43
  70. package/src/bundled-plugins/memory/memory-retrieval.ts +0 -111
  71. package/src/bundled-plugins/memory/vector/cache-write.ts +0 -19
  72. package/src/bundled-plugins/memory/vector/config.ts +0 -28
@@ -4,12 +4,11 @@ import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
4
4
  import { tmpdir } from 'node:os'
5
5
  import { isAbsolute, join, relative, resolve } from 'node:path'
6
6
 
7
- import { agentUsesVector } from '@/bundled-plugins/memory/vector/config'
8
- import { expandMountPath, loadConfigSync, withDefaultPlugins, type Config } from '@/config'
7
+ import { expandMountPath, loadConfigSync, withDefaultPlugins, type Config, type PortForward } from '@/config'
9
8
  import { commitGitignoreWithUntracks, untrackTrulyIgnoredFiles } from '@/git/reconcile-ignored'
10
9
  import { commitSystemFile as commitSystemFileShared } from '@/git/system-commit'
11
10
  import { send as sendToDaemon } from '@/hostd/client'
12
- import { ensureModels } from '@/hostd/models'
11
+ import { ensureModels as ensureHostModels } from '@/hostd/models'
13
12
  import { homeRoot } from '@/hostd/paths'
14
13
  import type { HttpInfoResult } from '@/hostd/protocol'
15
14
  import { ensureDaemon, type EnsureDaemonResult } from '@/hostd/spawn'
@@ -94,6 +93,27 @@ export type HostDaemonControl = {
94
93
  brokerToken: string
95
94
  }
96
95
 
96
+ // Mirrors the `register` RPC's fields minus `kind`; shared by socket and
97
+ // in-process registration so both paths record the same registry entry.
98
+ export type HostDaemonRegisterPayload = {
99
+ containerName: string
100
+ cwd: string
101
+ restartToken: string
102
+ wsHostPort: number
103
+ portForward: PortForward
104
+ brokerToken: string
105
+ }
106
+
107
+ // Injected only on the daemon-owned restart path (reuseCurrentHostDaemon).
108
+ // The daemon registers the container in-process and reports its own HTTP port,
109
+ // so the restart skips the `http-info`/`register` self-RPCs — those round-trips
110
+ // can time out under IPC congestion and silently drop the TYPECLAW_HOSTD_* env
111
+ // triple, booting a container whose hostd-bridged adapters can't construct.
112
+ export type CurrentHostDaemon = {
113
+ httpPort: number
114
+ register: (payload: HostDaemonRegisterPayload) => Promise<{ ok: true } | { ok: false; reason: string }>
115
+ }
116
+
97
117
  export type StartOptions = {
98
118
  cwd: string
99
119
  preferredHostPort: number
@@ -106,7 +126,14 @@ export type StartOptions = {
106
126
  // Hostd's supervisor restart callback already runs inside the daemon process.
107
127
  // Reusing that daemon avoids a self-shutdown when disk source has drifted.
108
128
  reuseCurrentHostDaemon?: boolean
129
+ // Set by hostd's supervisor restart wrapper. When present, registration goes
130
+ // through the daemon in-process (no socket round-trips), so the env triple is
131
+ // injected from known-good values even under IPC congestion. See type docs.
132
+ currentHostDaemon?: CurrentHostDaemon
109
133
  ensureDeps?: (cwd: string, opts?: { force?: boolean }) => Promise<EnsureDepsResult>
134
+ // Test seam for host embedding model provisioning. Production callers use
135
+ // the file-locked host cache downloader.
136
+ ensureModels?: () => Promise<void>
110
137
  // Test seam for the typeclaw-version auto-upgrade. Production callers omit
111
138
  // this and get the real autoUpgradeTypeclawDep (which reads the CLI's own
112
139
  // package.json). Tests inject a stub to simulate `bun -g update typeclaw`
@@ -183,7 +210,9 @@ export async function start({
183
210
  allocatePort = findFreePort,
184
211
  cliEntry,
185
212
  reuseCurrentHostDaemon = false,
213
+ currentHostDaemon,
186
214
  ensureDeps = (dir, opts) => ensureDepsInstalled({ cwd: dir, ...opts }),
215
+ ensureModels = ensureHostModels,
187
216
  autoUpgrade = (dir) => autoUpgradeTypeclawDep({ cwd: dir, localSpec: resolveTypeclawSpec(dir) }),
188
217
  forceBunUpdate = runBunUpdate,
189
218
  readInstalledVersion = readInstalledTypeclawVersionFromAgent,
@@ -352,19 +381,14 @@ export async function start({
352
381
  // container will actually load.
353
382
  const dockerfileRefresh = await refreshDockerfile(cwd, { buildKit: hasBuildx })
354
383
 
355
- // Provision the embedding model only when THIS agent opts into vector. The
356
- // container embedder runs with local_files_only, so the model must already
357
- // be on the host's ~/.typeclaw/models cache before the container boots —
358
- // otherwise the startup vector index build fails. Kick the download off here
359
- // (idempotent + file-locked) so it overlaps the docker build, then await it
360
- // just before `docker run`. A vector-opted-out agent never reaches this, so
361
- // a host whose containers are all opted out never downloads the ~280 MB
362
- // model — including every agent under `typeclaw compose`, since each agent's
363
- // start() makes this decision independently. The `.catch` swallow only keeps
364
- // an early return between here and the await from logging an unhandled
365
- // rejection; the real error is surfaced when we await at the run site below.
366
- const modelsReady = agentUsesVector(cwd) ? ensureModels() : null
367
- modelsReady?.catch(() => {})
384
+ // The container embedder runs with local_files_only, so the model must
385
+ // already be on the host's ~/.typeclaw/models cache before the container
386
+ // boots. Kick the download off here (idempotent + file-locked) so it
387
+ // overlaps the docker build, then await it just before `docker run`. The
388
+ // `.catch` swallow only keeps an early return between here and the await
389
+ // from logging an unhandled rejection; the real error is surfaced below.
390
+ const modelsReady = ensureModels()
391
+ modelsReady.catch(() => {})
368
392
 
369
393
  if (state.exists) {
370
394
  // Container holds the name but is not running. Without `--rm`, this is
@@ -424,7 +448,7 @@ export async function start({
424
448
  // Register AFTER port allocation so the daemon's portbroker has the right
425
449
  // wsHostPort. Re-register on TOCTOU retry below if the port changes.
426
450
  let hostd: PreparedHostDaemonStatus = cliEntry
427
- ? await registerWithDaemon({ cwd, containerName, cliEntry, hostPort, reuseCurrentHostDaemon })
451
+ ? await registerWithDaemon({ cwd, containerName, cliEntry, hostPort, reuseCurrentHostDaemon, currentHostDaemon })
428
452
  : { state: 'disabled' as const }
429
453
  let hostdControl = hostd.state === 'registered' ? hostd.control : undefined
430
454
 
@@ -457,15 +481,13 @@ export async function start({
457
481
  built = true
458
482
  }
459
483
 
460
- if (modelsReady) {
461
- try {
462
- await modelsReady
463
- } catch (error) {
464
- await cleanupHostDaemonRegistration(containerName, hostd)
465
- return {
466
- ok: false,
467
- reason: `embedding model provisioning failed (memory.vector.enabled): ${error instanceof Error ? error.message : String(error)}`,
468
- }
484
+ try {
485
+ await modelsReady
486
+ } catch (error) {
487
+ await cleanupHostDaemonRegistration(containerName, hostd)
488
+ return {
489
+ ok: false,
490
+ reason: `embedding model unavailable; ensure network access on first start or a populated model cache: ${error instanceof Error ? error.message : String(error)}`,
469
491
  }
470
492
  }
471
493
 
@@ -503,7 +525,14 @@ export async function start({
503
525
  }
504
526
  hostPort = await allocatePort(0)
505
527
  if (cliEntry) {
506
- hostd = await registerWithDaemon({ cwd, containerName, cliEntry, hostPort, reuseCurrentHostDaemon })
528
+ hostd = await registerWithDaemon({
529
+ cwd,
530
+ containerName,
531
+ cliEntry,
532
+ hostPort,
533
+ reuseCurrentHostDaemon,
534
+ currentHostDaemon,
535
+ })
507
536
  hostdControl = hostd.state === 'registered' ? hostd.control : undefined
508
537
  }
509
538
  plan = await planStart({
@@ -726,15 +755,10 @@ export async function planStart({
726
755
  runArgs.push(...dockerBindMount({ src: hostPath, dst: target, readonly: mount.readOnly }))
727
756
  }
728
757
 
729
- // Shared model cache mount for embeddings. Gated on vector opt-in: a
730
- // vector-opted-out container has no embedder to feed, and the host never
731
- // populates ~/.typeclaw/models for it (see ensureModels gating in start()),
732
- // so mounting an empty cache would only invite a confusing local_files_only
733
- // miss if something inside the container reached for the model anyway.
734
- if (agentUsesVector(cwd)) {
735
- runArgs.push(...dockerBindMount({ src: join(homeRoot(), 'models'), dst: '/opt/models', readonly: true }))
736
- runArgs.push('-e', 'TYPECLAW_MODEL_CACHE=/opt/models')
737
- }
758
+ // Shared model cache mount for embeddings. Vector memory is always on, and
759
+ // the embedder runs with local_files_only inside the container.
760
+ runArgs.push(...dockerBindMount({ src: join(homeRoot(), 'models'), dst: '/opt/models', readonly: true }))
761
+ runArgs.push('-e', 'TYPECLAW_MODEL_CACHE=/opt/models')
738
762
 
739
763
  runArgs.push(imageTag)
740
764
 
@@ -1158,27 +1182,47 @@ async function registerWithDaemon({
1158
1182
  cliEntry,
1159
1183
  hostPort,
1160
1184
  reuseCurrentHostDaemon,
1185
+ currentHostDaemon,
1161
1186
  }: {
1162
1187
  cwd: string
1163
1188
  containerName: string
1164
1189
  cliEntry: string
1165
1190
  hostPort: number
1166
1191
  reuseCurrentHostDaemon: boolean
1192
+ currentHostDaemon: CurrentHostDaemon | undefined
1167
1193
  }): Promise<PreparedHostDaemonStatus> {
1168
- const prepared = reuseCurrentHostDaemon ? await useCurrentHostDaemon() : await ensureDaemonWithBridgeRetry(cliEntry)
1169
- if (!prepared.ok) return { state: 'unavailable', reason: prepared.reason }
1170
1194
  const token = randomBytes(32).toString('base64url')
1171
1195
  const brokerToken = randomBytes(32).toString('base64url')
1172
1196
  const cfg = await loadTypeclawConfig(cwd)
1173
- const reply = await sendToDaemon({
1174
- kind: 'register',
1197
+ const payload: HostDaemonRegisterPayload = {
1175
1198
  containerName,
1176
1199
  cwd,
1177
1200
  restartToken: token,
1178
1201
  wsHostPort: hostPort,
1179
1202
  portForward: cfg.portForward,
1180
1203
  brokerToken,
1181
- })
1204
+ }
1205
+
1206
+ if (currentHostDaemon) {
1207
+ // A thrown/rejected registrar must degrade to a still-booting container,
1208
+ // not abort start() — the daemon-owned restart already stopped the old
1209
+ // container, so ok:false here would leave the agent dead.
1210
+ let reply: { ok: true } | { ok: false; reason: string }
1211
+ try {
1212
+ reply = await currentHostDaemon.register(payload)
1213
+ } catch (error) {
1214
+ return { state: 'unavailable', reason: error instanceof Error ? error.message : String(error) }
1215
+ }
1216
+ if (!reply.ok) return { state: 'unavailable', reason: reply.reason }
1217
+ return {
1218
+ state: 'registered',
1219
+ control: { url: `http://${CONTAINER_HOSTD_HOST}:${currentHostDaemon.httpPort}`, token, brokerToken },
1220
+ }
1221
+ }
1222
+
1223
+ const prepared = reuseCurrentHostDaemon ? await useCurrentHostDaemon() : await ensureDaemonWithBridgeRetry(cliEntry)
1224
+ if (!prepared.ok) return { state: 'unavailable', reason: prepared.reason }
1225
+ const reply = await sendToDaemon({ kind: 'register', ...payload })
1182
1226
  if (!reply.ok) return { state: 'unavailable', reason: reply.reason }
1183
1227
  return {
1184
1228
  state: 'registered',
@@ -0,0 +1,21 @@
1
+ import type { CurrentHostDaemon } from '@/container'
2
+
3
+ // Shared between the daemon (which alone knows its HTTP port + in-process
4
+ // registrar, both available only after boot) and the renewal callbacks in
5
+ // cli/hostd.ts (constructed before the daemon). `ready()` lets a caller that
6
+ // fires before boot await population instead of racing to a null read.
7
+ export type CurrentHostDaemonHolder = {
8
+ set: (value: CurrentHostDaemon) => void
9
+ ready: () => Promise<CurrentHostDaemon>
10
+ }
11
+
12
+ export function createCurrentHostDaemonHolder(): CurrentHostDaemonHolder {
13
+ let resolveReady: (value: CurrentHostDaemon) => void
14
+ const readyPromise = new Promise<CurrentHostDaemon>((resolve) => {
15
+ resolveReady = resolve
16
+ })
17
+ return {
18
+ set: (value) => resolveReady(value),
19
+ ready: () => readyPromise,
20
+ }
21
+ }
@@ -4,7 +4,7 @@ import { createServer, type Server, type Socket as NetSocket } from 'node:net'
4
4
  import { join } from 'node:path'
5
5
 
6
6
  import type { PortForward } from '@/config'
7
- import { defaultDockerExec, type DockerExec } from '@/container'
7
+ import { defaultDockerExec, type DockerExec, type HostDaemonRegisterPayload } from '@/container'
8
8
  import type { PortForwardEvent } from '@/portbroker'
9
9
  import {
10
10
  discordChannelBlockSchema,
@@ -17,6 +17,7 @@ import { SecretsBackend } from '@/secrets/storage'
17
17
  import { isWindows } from '@/shared'
18
18
 
19
19
  import { isDaemonReachable } from './client'
20
+ import type { CurrentHostDaemonHolder } from './current-host-daemon'
20
21
  import type { KakaoRenewalCallbacks, KakaoRenewalLogEvent } from './kakao-renewal-manager'
21
22
  import { ensureDirs, registrationFilePath, registrationsDir, socketPath } from './paths'
22
23
  import type {
@@ -40,6 +41,10 @@ export type DaemonOptions = {
40
41
  onLog?: (event: DaemonLogEvent | SupervisorLogEvent) => void
41
42
  gcIntervalMs?: number
42
43
  gcMissesToDeregister?: number
44
+ // Per-step bound on teardown calls (portbroker/renewal stop) run inside the
45
+ // per-container serial chain. Defaults to CLEANUP_STEP_TIMEOUT_MS; tests
46
+ // override it to a few ms to assert the chain never wedges on a hung step.
47
+ cleanupStepTimeoutMs?: number
43
48
  socket?: string
44
49
  // When provided, the daemon honors `restart` RPCs by invoking this with the
45
50
  // (containerName, cwd) it captured at register time. Omit to disable the
@@ -71,6 +76,10 @@ export type DaemonOptions = {
71
76
  // ticks hourly because Webex password tokens live ~27h (see
72
77
  // webex-renewal-manager.ts). Omit when the agent has no webex channel.
73
78
  webexRenewal?: WebexRenewalCallbacks
79
+ // Populated once the daemon is booted so direct callers of the restart
80
+ // (the renewal callbacks in cli/hostd.ts) get the in-process registration
81
+ // path instead of the socket self-RPC. Omit in tests that don't exercise it.
82
+ currentHostDaemonHolder?: CurrentHostDaemonHolder
74
83
  }
75
84
 
76
85
  export type RestartPreflight = (input: {
@@ -125,6 +134,14 @@ const DEFAULT_GC_MISSES_TO_DEREGISTER = 3
125
134
  const MAX_REQUEST_BUFFER_BYTES = 64 * 1024
126
135
  const MAX_HTTP_REQUEST_BYTES = 64 * 1024
127
136
 
137
+ // Upper bound on any single teardown step (portbroker/renewal stop) run inside
138
+ // the per-container runSerially chain. A teardown dependency must never own a
139
+ // container's serial queue forever — that wedges every later register/deregister
140
+ // for that container (the renewal-restart wedge). Larger than tailscale's 30s
141
+ // per-call budget so a slow-but-progressing serve-off isn't cut off, yet finite
142
+ // so a genuine hang releases the queue instead of poisoning it.
143
+ const CLEANUP_STEP_TIMEOUT_MS = 60_000
144
+
128
145
  // Preferred port for the HTTP control surface. Adjacent to CONTAINER_PORT
129
146
  // (8973) for mnemonics. Stability matters: containers cache the URL in
130
147
  // TYPECLAW_HOSTD_URL at `docker run` time, so a respawn that picks a fresh
@@ -218,6 +235,21 @@ function stringifyError(error: unknown): string {
218
235
  return error instanceof Error ? error.message : String(error)
219
236
  }
220
237
 
238
+ // Races a teardown step against a wall clock so a hung dependency can't hold the
239
+ // per-container serial chain open forever. Resolves (never rejects) on timeout
240
+ // so the surrounding runSerially op always settles and releases the queue.
241
+ async function withCleanupTimeout(work: Promise<unknown>, timeoutMs: number): Promise<void> {
242
+ let timer: ReturnType<typeof setTimeout> | undefined
243
+ const timeout = new Promise<void>((resolve) => {
244
+ timer = setTimeout(resolve, timeoutMs)
245
+ })
246
+ try {
247
+ await Promise.race([work.catch(() => {}), timeout])
248
+ } finally {
249
+ if (timer) clearTimeout(timer)
250
+ }
251
+ }
252
+
221
253
  function errorCode(error: Error): unknown {
222
254
  const direct = error as Error & { code?: unknown; cause?: unknown }
223
255
  if (direct.code !== undefined) return direct.code
@@ -277,6 +309,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
277
309
  const exec = opts.exec ?? defaultDockerExec
278
310
  const gcIntervalMs = opts.gcIntervalMs ?? DEFAULT_GC_INTERVAL_MS
279
311
  const gcMissesToDeregister = opts.gcMissesToDeregister ?? DEFAULT_GC_MISSES_TO_DEREGISTER
312
+ const cleanupStepTimeoutMs = opts.cleanupStepTimeoutMs ?? CLEANUP_STEP_TIMEOUT_MS
280
313
  const version = opts.version ?? UNVERSIONED_SENTINEL
281
314
  const cwds = new Map<string, string>()
282
315
  const restartTokens = new Map<string, string>()
@@ -357,9 +390,10 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
357
390
  cwds.delete(containerName)
358
391
  restartTokens.delete(containerName)
359
392
  gcMisses.delete(containerName)
360
- if (opts.portbroker) await opts.portbroker.stop(containerName, 'fatal-auth').catch(() => {})
361
- if (opts.kakaoRenewal) await opts.kakaoRenewal.stop(containerName).catch(() => {})
362
- if (opts.webexRenewal) await opts.webexRenewal.stop(containerName).catch(() => {})
393
+ if (opts.portbroker)
394
+ await withCleanupTimeout(opts.portbroker.stop(containerName, 'fatal-auth'), cleanupStepTimeoutMs)
395
+ if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(containerName), cleanupStepTimeoutMs)
396
+ if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(containerName), cleanupStepTimeoutMs)
363
397
  await removeRegistrationFile(containerName)
364
398
  log({ kind: 'registration-skipped', containerName, reason: `fatal broker auth: ${reason}` })
365
399
  })
@@ -400,7 +434,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
400
434
  }
401
435
  }
402
436
 
403
- const handleRegister = async (req: RegisterPayload): Promise<RpcResponse> => {
437
+ const registerContainer = async (req: RegisterPayload): Promise<RpcResponse> => {
404
438
  if (stopped) return { ok: false, reason: 'daemon stopping' }
405
439
  return runSerially(req.containerName, async () => {
406
440
  if (stopped) return { ok: false, reason: 'daemon stopping' }
@@ -417,15 +451,18 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
417
451
  })
418
452
  }
419
453
 
454
+ const handleRegister = async (req: RegisterPayload): Promise<RpcResponse> => registerContainer(req)
455
+
420
456
  const handleDeregister = async (req: { containerName: string }): Promise<RpcResponse> =>
421
457
  runSerially(req.containerName, async () => {
422
458
  const hadCwd = cwds.delete(req.containerName)
423
459
  restartTokens.delete(req.containerName)
424
460
  brokerTokens.delete(req.containerName)
425
461
  gcMisses.delete(req.containerName)
426
- if (opts.portbroker) await opts.portbroker.stop(req.containerName, 'deregistered').catch(() => {})
427
- if (opts.kakaoRenewal) await opts.kakaoRenewal.stop(req.containerName).catch(() => {})
428
- if (opts.webexRenewal) await opts.webexRenewal.stop(req.containerName).catch(() => {})
462
+ if (opts.portbroker)
463
+ await withCleanupTimeout(opts.portbroker.stop(req.containerName, 'deregistered'), cleanupStepTimeoutMs)
464
+ if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(req.containerName), cleanupStepTimeoutMs)
465
+ if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(req.containerName), cleanupStepTimeoutMs)
429
466
  await removeRegistrationFile(req.containerName)
430
467
  if (hadCwd) log({ kind: 'deregister', containerName: req.containerName, reason: 'requested' })
431
468
  return { ok: true }
@@ -449,6 +486,17 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
449
486
  return { ok: true, result }
450
487
  }
451
488
 
489
+ // Lets the supervisor's restart re-register the child in-process instead of
490
+ // over the socket. Awaits restore for the same no-mutation-before-restore
491
+ // invariant the socket dispatcher enforces before handleRegister.
492
+ const registerCurrentChild = async (
493
+ payload: HostDaemonRegisterPayload,
494
+ ): Promise<{ ok: true } | { ok: false; reason: string }> => {
495
+ await awaitRestored()
496
+ const reply = await registerContainer(payload)
497
+ return reply.ok ? { ok: true } : { ok: false, reason: reply.reason }
498
+ }
499
+
452
500
  // Auth: only restart containers that registered with this daemon. The
453
501
  // socket is 0o600 + UID-bound, but inside a container any process that
454
502
  // reaches the mounted socket could otherwise restart any peer container on
@@ -465,7 +513,12 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
465
513
  ? await opts.restartPreflight({ containerName: req.containerName, cwd, build: req.build })
466
514
  : null
467
515
  if (preflight) return preflight
468
- const ack = supervisor.scheduleRestart({ containerName: req.containerName, cwd, build: req.build })
516
+ const ack = supervisor.scheduleRestart({
517
+ containerName: req.containerName,
518
+ cwd,
519
+ build: req.build,
520
+ currentHostDaemon: { httpPort, register: registerCurrentChild },
521
+ })
469
522
  if (!ack.ok) return ack
470
523
  const result: RestartResult = { containerName: req.containerName, scheduled: true }
471
524
  return { ok: true, result }
@@ -699,6 +752,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
699
752
  }
700
753
  httpPort = httpServer.port ?? 0
701
754
  log({ kind: 'daemon-http-listening', host: httpHostname, port: httpPort })
755
+ opts.currentHostDaemonHolder?.set({ httpPort, register: registerCurrentChild })
702
756
 
703
757
  const sockets = new Set<NetSocket>()
704
758
  const listener = createServer((socket) => {
@@ -735,9 +789,9 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
735
789
  void runSerially(name, async () => {
736
790
  const hadCwd = cwds.delete(name)
737
791
  restartTokens.delete(name)
738
- if (opts.portbroker) await opts.portbroker.stop(name, 'deregistered').catch(() => {})
739
- if (opts.kakaoRenewal) await opts.kakaoRenewal.stop(name).catch(() => {})
740
- if (opts.webexRenewal) await opts.webexRenewal.stop(name).catch(() => {})
792
+ if (opts.portbroker) await withCleanupTimeout(opts.portbroker.stop(name, 'deregistered'), cleanupStepTimeoutMs)
793
+ if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(name), cleanupStepTimeoutMs)
794
+ if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(name), cleanupStepTimeoutMs)
741
795
  await removeRegistrationFile(name)
742
796
  if (hadCwd) log({ kind: 'deregister', containerName: name, reason: 'gone' })
743
797
  return { ok: true }
@@ -763,17 +817,23 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
763
817
  // after the teardown loop below already ran, leaking it. Let restore
764
818
  // settle (it never rejects past the detached catch) before tearing down.
765
819
  await (restoreComplete ?? Promise.resolve()).catch(() => {})
820
+ // Bound shutdown teardown too: a wedged broker/renewal stop must not
821
+ // block daemon.stop() forever, which would also stall the version-drift
822
+ // respawn flow that waits for this to return before unlinking the socket.
766
823
  if (opts.portbroker) {
767
824
  const names = Array.from(cwds.keys())
768
- await Promise.allSettled(names.map((n) => opts.portbroker!.stop(n, 'broker-stopped')))
825
+ await withCleanupTimeout(
826
+ Promise.allSettled(names.map((n) => opts.portbroker!.stop(n, 'broker-stopped'))),
827
+ cleanupStepTimeoutMs,
828
+ )
769
829
  }
770
830
  if (opts.kakaoRenewal) {
771
831
  const names = Array.from(cwds.keys())
772
- await Promise.allSettled(names.map((n) => opts.kakaoRenewal!.stop(n)))
832
+ await withCleanupTimeout(Promise.allSettled(names.map((n) => opts.kakaoRenewal!.stop(n))), cleanupStepTimeoutMs)
773
833
  }
774
834
  if (opts.webexRenewal) {
775
835
  const names = Array.from(cwds.keys())
776
- await Promise.allSettled(names.map((n) => opts.webexRenewal!.stop(n)))
836
+ await withCleanupTimeout(Promise.allSettled(names.map((n) => opts.webexRenewal!.stop(n))), cleanupStepTimeoutMs)
777
837
  }
778
838
  cwds.clear()
779
839
  restartTokens.clear()
@@ -104,7 +104,14 @@ export function createKakaoRenewalManager(opts: KakaoRenewalManagerOptions = {})
104
104
  accountId: result.account_id,
105
105
  previousUpdatedAt: result.previousUpdatedAt,
106
106
  })
107
- if (opts.onRenewalOk) {
107
+ // A tick that started before stop()/drain() (deregister, shutdown) or
108
+ // before a re-register with a different cwd can finish the slow login
109
+ // here. Restarting a container that is no longer the current
110
+ // registration would resurrect a just-deregistered agent or fight a
111
+ // shutdown, so skip onRenewalOk unless this container+cwd is still the
112
+ // live registration. Matches webex-renewal-manager's guard.
113
+ const current = latestInput.get(input.containerName)
114
+ if (opts.onRenewalOk && current?.cwd === input.cwd) {
108
115
  log({
109
116
  kind: 'kakao-renewal-restart-scheduled',
110
117
  containerName: input.containerName,
@@ -187,9 +194,21 @@ export function createKakaoRenewalManager(opts: KakaoRenewalManagerOptions = {})
187
194
 
188
195
  return {
189
196
  start(input: KakaoRenewalStartInput): void {
190
- if (opts.shouldRenew && !opts.shouldRenew(input)) return
197
+ // Tear down any prior registration for this container BEFORE the
198
+ // shouldRenew gate. The daemon calls start() on every registration, so a
199
+ // container that had Kakao can re-register to a non-Kakao cwd; if we
200
+ // returned early without clearing, the old timer would keep ticking and a
201
+ // post-login restart guard would still match the stale cwd. Clearing
202
+ // latestInput also makes an in-flight tick's onRenewalOk re-check fail.
191
203
  const existing = timers.get(input.containerName)
192
- if (existing) existing.stop()
204
+ if (existing) {
205
+ existing.stop()
206
+ timers.delete(input.containerName)
207
+ }
208
+ latestInput.delete(input.containerName)
209
+
210
+ if (opts.shouldRenew && !opts.shouldRenew(input)) return
211
+
193
212
  latestInput.set(input.containerName, input)
194
213
  const handle = schedule(() => {
195
214
  void scheduleTick(input.containerName)
@@ -198,26 +217,32 @@ export function createKakaoRenewalManager(opts: KakaoRenewalManagerOptions = {})
198
217
  void scheduleTick(input.containerName)
199
218
  },
200
219
 
201
- async stop(containerName: string): Promise<void> {
220
+ // Must NOT await the in-flight tick: a successful tick runs onRenewalOk →
221
+ // hostdRestart → container stop() → deregister RPC → handleDeregister →
222
+ // kakaoRenewal.stop(), so awaiting inFlight here waits on the very tick that
223
+ // is parked waiting for this restart — a self-deadlock that wedges the
224
+ // daemon's per-container serial chain forever. Clearing latestInput (not the
225
+ // await) is what fails the tick's onRenewalOk guard; drain() still awaits.
226
+ stop(containerName: string): Promise<void> {
202
227
  const handle = timers.get(containerName)
203
228
  if (handle) {
204
229
  timers.delete(containerName)
205
230
  handle.stop()
206
231
  }
207
232
  latestInput.delete(containerName)
208
- // Await any in-flight tick before resolving so the caller can rely on
209
- // "stop returned → no work outstanding". Daemon shutdown depends on
210
- // this; without it, a mid-tick `attemptLogin` HTTPS call is abandoned.
211
- const promise = inFlight.get(containerName)
212
- if (promise) await promise.catch(() => {})
233
+ return Promise.resolve()
213
234
  },
214
235
 
215
236
  async drain(): Promise<void> {
216
237
  for (const [, handle] of timers) handle.stop()
217
238
  timers.clear()
218
- latestInput.clear()
239
+ // Clear latestInput AFTER awaiting in-flight: the onRenewalOk guard reads
240
+ // latestInput, so clearing first would suppress the restart of a tick that
241
+ // is mid-login when drain() runs. drain() is shutdown — it waits for that
242
+ // work to finish rather than dropping it.
219
243
  const promises = Array.from(inFlight.values())
220
244
  await Promise.allSettled(promises)
245
+ latestInput.clear()
221
246
  },
222
247
  }
223
248
  }
@@ -1,3 +1,5 @@
1
+ import type { CurrentHostDaemon } from '@/container'
2
+
1
3
  import type { Response } from './protocol'
2
4
 
3
5
  export type SupervisorRestart = (input: {
@@ -8,6 +10,9 @@ export type SupervisorRestart = (input: {
8
10
  // image even if it already exists. Default false matches the host-side
9
11
  // `typeclaw restart` (no `--build` flag) behavior.
10
12
  build?: boolean
13
+ // Injected by the daemon so the restart registers the container in-process
14
+ // instead of over the socket — see CurrentHostDaemon docs in src/container.
15
+ currentHostDaemon?: CurrentHostDaemon
11
16
  }) => Promise<{ ok: true } | { ok: false; reason: string }>
12
17
 
13
18
  export type SupervisorOptions = {
@@ -20,7 +25,12 @@ export type SupervisorLogEvent =
20
25
  | { kind: 'restart-failed'; containerName: string; reason: string }
21
26
 
22
27
  export type Supervisor = {
23
- scheduleRestart: (input: { containerName: string; cwd: string; build?: boolean }) => Response
28
+ scheduleRestart: (input: {
29
+ containerName: string
30
+ cwd: string
31
+ build?: boolean
32
+ currentHostDaemon?: CurrentHostDaemon
33
+ }) => Response
24
34
  }
25
35
 
26
36
  export type SupervisorBuildOptions = {
@@ -36,7 +46,7 @@ export type SupervisorBuildOptions = {
36
46
  // surfaced via the log channel; there is no connected client to receive them.
37
47
  export function buildSupervisor({ restart, onLog, isStopped }: SupervisorBuildOptions): Supervisor {
38
48
  return {
39
- scheduleRestart: ({ containerName, cwd, build = false }): Response => {
49
+ scheduleRestart: ({ containerName, cwd, build = false, currentHostDaemon }): Response => {
40
50
  if (isStopped()) return { ok: false, reason: 'daemon stopping' }
41
51
  onLog({ kind: 'restart-scheduled', containerName, build })
42
52
  void runRestart()
@@ -44,7 +54,12 @@ export function buildSupervisor({ restart, onLog, isStopped }: SupervisorBuildOp
44
54
 
45
55
  async function runRestart(): Promise<void> {
46
56
  try {
47
- const result = await restart({ containerName, cwd, build })
57
+ const result = await restart({
58
+ containerName,
59
+ cwd,
60
+ build,
61
+ ...(currentHostDaemon ? { currentHostDaemon } : {}),
62
+ })
48
63
  if (result.ok) onLog({ kind: 'restart-completed', containerName })
49
64
  else onLog({ kind: 'restart-failed', containerName, reason: result.reason })
50
65
  } catch (error) {
@@ -36,6 +36,17 @@ type TailscaleStatus = {
36
36
  const MACOS_APP_CLI = '/Applications/Tailscale.app/Contents/MacOS/Tailscale'
37
37
  const WINDOWS_CLI = 'C:\\Program Files\\Tailscale\\tailscale.exe'
38
38
 
39
+ // Per-CLI-call wall clock. The tailscaled "serve config" is a global resource
40
+ // guarded by an etag; concurrent agent restarts racing `serve`/`serve off`
41
+ // surface "Another client is changing the serve config" and the losing CLI can
42
+ // block on the lock indefinitely. An unbounded `await proc.exited` then poisons
43
+ // stopAll() → portbroker.stop() → the daemon's per-container serial chain (the
44
+ // renewal-restart wedge). 30s is far longer than a healthy serve call yet bounds
45
+ // the hang. Kept below the daemon teardown budget so a timed-out call surfaces
46
+ // as a normal tailscale-serve-failed before the teardown wrapper trips.
47
+ const TAILSCALE_CLI_TIMEOUT_MS = 30_000
48
+ const TAILSCALE_TIMEOUT_EXIT_CODE = 124
49
+
39
50
  export function createTailscaleServeManager(opts: TailscaleServeManagerOptions): TailscaleServeManager {
40
51
  const exec = opts.exec ?? defaultTailscaleExec
41
52
  const log = opts.onLog ?? (() => {})
@@ -153,7 +164,11 @@ export const defaultTailscaleExec: TailscaleExec = async (args) => {
153
164
  return { exitCode: 127, stdout: '', stderr: lastError }
154
165
  }
155
166
 
156
- async function runTailscale(bin: string, args: string[]): Promise<TailscaleExecResult> {
167
+ export async function runTailscale(
168
+ bin: string,
169
+ args: string[],
170
+ timeoutMs: number = TAILSCALE_CLI_TIMEOUT_MS,
171
+ ): Promise<TailscaleExecResult> {
157
172
  const bun = getBun()
158
173
  if (!bun) return { exitCode: 127, stdout: '', stderr: 'bun runtime not available' }
159
174
  try {
@@ -163,10 +178,31 @@ async function runTailscale(bin: string, args: string[]): Promise<TailscaleExecR
163
178
  stderr: 'pipe',
164
179
  env: bin === MACOS_APP_CLI ? { ...process.env, TAILSCALE_BE_CLI: '1' } : process.env,
165
180
  })
166
- const exitCode = await proc.exited
167
- const stdout = await new Response(proc.stdout).text()
168
- const stderr = await new Response(proc.stderr).text()
169
- return { exitCode, stdout, stderr }
181
+ // Drain both pipes concurrently with the exit wait. Reading them only after
182
+ // `proc.exited` can deadlock: a child that fills its stdout/stderr pipe
183
+ // buffer blocks on write while we block on exit, so neither side advances.
184
+ const stdoutPromise = new Response(proc.stdout).text().catch(() => '')
185
+ const stderrPromise = new Response(proc.stderr).text().catch(() => '')
186
+
187
+ let timer: ReturnType<typeof setTimeout> | undefined
188
+ const timedOut = new Promise<'timeout'>((resolve) => {
189
+ timer = setTimeout(() => resolve('timeout'), timeoutMs)
190
+ })
191
+ const outcome = await Promise.race([proc.exited.then(() => 'exited' as const), timedOut])
192
+ if (timer) clearTimeout(timer)
193
+
194
+ if (outcome === 'timeout') {
195
+ try {
196
+ proc.kill()
197
+ } catch {}
198
+ return {
199
+ exitCode: TAILSCALE_TIMEOUT_EXIT_CODE,
200
+ stdout: '',
201
+ stderr: `tailscale ${args.join(' ')} timed out after ${timeoutMs}ms`,
202
+ }
203
+ }
204
+
205
+ return { exitCode: await proc.exited, stdout: await stdoutPromise, stderr: await stderrPromise }
170
206
  } catch (error) {
171
207
  const code = typeof error === 'object' && error !== null && 'code' in error ? String(error.code) : ''
172
208
  const exitCode = code === 'ENOENT' ? 127 : 1