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
@@ -194,23 +194,32 @@ export function createWebexRenewalManager(opts: WebexRenewalManagerOptions = {})
194
194
  void scheduleTick(input.containerName)
195
195
  },
196
196
 
197
- async stop(containerName: string): Promise<void> {
197
+ // Must NOT await the in-flight tick: a successful tick runs onRenewalOk →
198
+ // hostdRestart → container stop() → deregister RPC → handleDeregister →
199
+ // webexRenewal.stop(), so awaiting inFlight here waits on the very tick that
200
+ // is parked waiting for this restart — a self-deadlock that wedges the
201
+ // daemon's per-container serial chain forever. Clearing latestInput (not the
202
+ // await) is what fails the tick's onRenewalOk guard; drain() still awaits.
203
+ stop(containerName: string): Promise<void> {
198
204
  const handle = timers.get(containerName)
199
205
  if (handle) {
200
206
  timers.delete(containerName)
201
207
  handle.stop()
202
208
  }
203
209
  latestInput.delete(containerName)
204
- const promise = inFlight.get(containerName)
205
- if (promise) await promise.catch(() => {})
210
+ return Promise.resolve()
206
211
  },
207
212
 
208
213
  async drain(): Promise<void> {
209
214
  for (const [, handle] of timers) handle.stop()
210
215
  timers.clear()
211
- latestInput.clear()
216
+ // Clear latestInput AFTER awaiting in-flight: the onRenewalOk guard reads
217
+ // latestInput, so clearing first would suppress the restart of a tick that
218
+ // is mid-login when drain() runs. drain() is shutdown — it waits for that
219
+ // work to finish rather than dropping it.
212
220
  const promises = Array.from(inFlight.values())
213
221
  await Promise.allSettled(promises)
222
+ latestInput.clear()
214
223
  },
215
224
  }
216
225
  }
@@ -40,10 +40,9 @@ export const END_HANDLER_TIMEOUT_MS = 60_000
40
40
  // (loadSelf, loadMemory shard reads, prefetchChannelContext) before
41
41
  // ensureLive fires.
42
42
  //
43
- // The bundled memory plugin's session.prompt fires memory-retrieval
44
- // detached, so it returns in <1ms in steady state; this ceiling guards
45
- // third-party hooks that legitimately need to do work inline, AND a
46
- // regression in the memory plugin that re-introduces an inline await.
43
+ // The bundled memory plugin does per-turn retrieval from session.turn.start;
44
+ // this ceiling guards third-party hooks that legitimately need to do work
45
+ // inline, AND regressions that re-introduce expensive inline prompt work.
47
46
  export const PROMPT_HANDLER_TIMEOUT_MS = 20_000
48
47
 
49
48
  export type RegisteredHook<K extends keyof Hooks> = {
@@ -89,6 +89,7 @@ export {
89
89
  type RegisteredCommand,
90
90
  type RegisteredCronJob,
91
91
  type RegisteredDoctorCheck,
92
+ type RegisteredPluginDisposer,
92
93
  type RegisteredSkillDir,
93
94
  type RegisteredSkillEntry,
94
95
  type RegisteredSubagent,
@@ -51,6 +51,7 @@ export type LoadPluginsResult = {
51
51
  failedPlugins: FailedPlugin[]
52
52
  markBooted: () => void
53
53
  setSpawnSubagent: (fn: SpawnSubagentFn) => void
54
+ disposePlugins: () => Promise<void>
54
55
  }
55
56
 
56
57
  export async function loadPlugins(opts: LoadPluginsOptions): Promise<LoadPluginsResult> {
@@ -181,9 +182,22 @@ export async function loadPlugins(opts: LoadPluginsOptions): Promise<LoadPlugins
181
182
  setSpawnSubagent: (fn) => {
182
183
  spawnSubagentImpl = fn
183
184
  },
185
+ disposePlugins: () => disposePlugins(registry),
184
186
  }
185
187
  }
186
188
 
189
+ async function disposePlugins(registry: PluginRegistry): Promise<void> {
190
+ await Promise.all(
191
+ registry.disposers.map(async (disposer) => {
192
+ try {
193
+ await disposer.dispose()
194
+ } catch (err) {
195
+ disposer.logger.warn(`onDispose failed: ${err instanceof Error ? err.message : String(err)}`)
196
+ }
197
+ }),
198
+ )
199
+ }
200
+
187
201
  // Tags WHICH sub-phase failed (for the failedPlugins report) while preserving
188
202
  // the ORIGINAL error so the caller can keep PluginSecurityError fatal even when
189
203
  // it surfaces deep inside registration.
@@ -34,6 +34,11 @@ export type RegisteredCommand = {
34
34
  command: PluginCommand
35
35
  logger: PluginLogger
36
36
  }
37
+ export type RegisteredPluginDisposer = {
38
+ pluginName: string
39
+ logger: PluginLogger
40
+ dispose: () => void | Promise<void>
41
+ }
37
42
 
38
43
  export type PluginRegistry = {
39
44
  tools: RegisteredTool[]
@@ -43,6 +48,7 @@ export type PluginRegistry = {
43
48
  skillsDirs: RegisteredSkillDir[]
44
49
  doctorChecks: RegisteredDoctorCheck[]
45
50
  commands: RegisteredCommand[]
51
+ disposers: RegisteredPluginDisposer[]
46
52
  }
47
53
 
48
54
  export type RegisterContributionsOptions = {
@@ -159,6 +165,10 @@ export function registerContributions(opts: RegisterContributionsOptions): void
159
165
  registry.commands.push({ pluginName, commandName, command, logger })
160
166
  }
161
167
  }
168
+
169
+ if (ex.onDispose) {
170
+ registry.disposers.push({ pluginName, logger, dispose: ex.onDispose })
171
+ }
162
172
  }
163
173
 
164
174
  export function discardRegistrationsBy(pluginName: string, registry: PluginRegistry, hooks: HookBus): void {
@@ -169,6 +179,7 @@ export function discardRegistrationsBy(pluginName: string, registry: PluginRegis
169
179
  registry.skillsDirs = registry.skillsDirs.filter((d) => d.pluginName !== pluginName)
170
180
  registry.doctorChecks = registry.doctorChecks.filter((d) => d.pluginName !== pluginName)
171
181
  registry.commands = registry.commands.filter((c) => c.pluginName !== pluginName)
182
+ registry.disposers = registry.disposers.filter((d) => d.pluginName !== pluginName)
172
183
  hooks.unregisterAll(pluginName)
173
184
  }
174
185
 
@@ -181,6 +192,7 @@ export function emptyRegistry(): PluginRegistry {
181
192
  skillsDirs: [],
182
193
  doctorChecks: [],
183
194
  commands: [],
195
+ disposers: [],
184
196
  }
185
197
  }
186
198
 
@@ -183,7 +183,7 @@ export type SessionTurnStartEvent = {
183
183
  userPrompt: string
184
184
  origin?: SessionOrigin
185
185
  // Mutable ref: plugin writes retrieval results here; server/router reads after hook returns.
186
- // Only populated when vector.enabled and injection plan is index mode.
186
+ // Populated when a turn driver supplies a retrieval context bag.
187
187
  retrievalContext?: { results: string }
188
188
  }
189
189
 
@@ -294,6 +294,10 @@ export type PluginExports = {
294
294
  skillsDirs?: string[]
295
295
  hooks?: Hooks
296
296
  doctorChecks?: Record<string, PluginDoctorCheck>
297
+ // Released once when the agent stops, for plugin-lifetime resources a GC can't
298
+ // reclaim deterministically (open DB handles, timers). Disposers are isolated:
299
+ // one throwing never blocks the others.
300
+ onDispose?: () => void | Promise<void>
297
301
  }
298
302
 
299
303
  // `typeclaw doctor` plugin extension surface. Each check is read-only by
@@ -1,3 +1,5 @@
1
+ import { join } from 'node:path'
2
+
1
3
  import { SessionManager } from '@mariozechner/pi-coding-agent'
2
4
 
3
5
  import { createSession as defaultCreateSession } from '@/agent'
@@ -71,9 +73,6 @@ export type BuildChannelSessionFactoryDeps = {
71
73
  subagentRegistry?: SubagentRegistry
72
74
  getCreateSessionForSubagent?: () => CreateSessionForSubagent
73
75
  liveSessionRegistry?: LiveSessionRegistry
74
- // Forwarded to createSession: when true the `# Memory` section is omitted
75
- // from the system prompt (vector agents inject memory per-turn instead).
76
- suppressSystemMemory?: boolean
77
76
  }
78
77
 
79
78
  // Tight basename validation so a tampered or corrupt channels/sessions.json
@@ -141,7 +140,6 @@ export function buildChannelSessionFactory(deps: BuildChannelSessionFactoryDeps)
141
140
  ...(deps.getCreateSessionForSubagent !== undefined
142
141
  ? { createSessionForSubagent: deps.getCreateSessionForSubagent() }
143
142
  : {}),
144
- ...(deps.suppressSystemMemory !== undefined ? { suppressSystemMemory: deps.suppressSystemMemory } : {}),
145
143
  })
146
144
 
147
145
  const sessionId = sessionManager.getSessionId()
@@ -192,7 +190,7 @@ function tryReopenOrCreate(
192
190
  )
193
191
  return SessionManager.create(cwd, sessionDir)
194
192
  }
195
- const path = `${sessionDir}/${existingSessionFile}`
193
+ const path = join(sessionDir, existingSessionFile)
196
194
  if (capOptions !== null) {
197
195
  try {
198
196
  const stats = capJsonlFileInPlace(path, capOptions)
package/src/run/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { SessionManager } from '@mariozechner/pi-coding-agent'
2
2
 
3
3
  import { createSession, createSessionWithDispose } from '@/agent'
4
+ import { createProviderAuthReloadable } from '@/agent/auth-reloadable'
4
5
  import { LiveSessionRegistry } from '@/agent/live-sessions'
5
6
  import { LiveSubagentRegistry } from '@/agent/live-subagents'
6
7
  import { requestContainerRestart } from '@/agent/restart'
@@ -20,7 +21,6 @@ import {
20
21
  type SubagentShared,
21
22
  } from '@/agent/subagents'
22
23
  import { clearTodosForOrigin, markRestartAbortPendingForOrigin } from '@/agent/todo/continuation-wiring'
23
- import { vectorEnabledFromMemoryConfig } from '@/bundled-plugins/memory/vector/config'
24
24
  import { embed, warmEmbedder } from '@/bundled-plugins/memory/vector/embedder'
25
25
  import { buildStartupVectorIndex } from '@/bundled-plugins/memory/vector/startup'
26
26
  import { resolveCapOptionsFromConfig } from '@/bundled-plugins/tool-result-cap'
@@ -125,24 +125,32 @@ export type StartAgentResult = {
125
125
  stop: () => void | Promise<void>
126
126
  }
127
127
 
128
- // Thin wrapper that owns ONLY the boot-failure cleanup of process-global
129
- // installs. `installCodexFetchObserver` and `installFatalGuard` attach
130
- // process-wide listeners before the fallible boot work; if any boot step throws
131
- // before `startAgentRuntime` returns, the caller never receives a
132
- // `StartAgentResult` and can never call `stop()`, so those listeners would
133
- // strand and double-fire into the next `startAgent`. The runtime hands its
134
- // disposer back via `onProcessGlobalsInstalled`; on a boot throw we dispose and
128
+ // Owns boot-failure cleanup for everything installed before `startAgentRuntime`
129
+ // returns a `StartAgentResult`. `installCodexFetchObserver`/`installFatalGuard`
130
+ // attach process-wide listeners, and `loadPlugins` opens plugin-lifetime
131
+ // resources (the memory plugin's sqlite handle); if any boot step throws before
132
+ // the result exists, the caller never receives `stop()`, so each of those would
133
+ // strand. The runtime registers each cleanup via `registerBootCleanup` as boot
134
+ // progresses; on a throw we run them all (newest first, best-effort) and
135
135
  // rethrow, on success ownership transfers to the returned `stop()`. `return
136
136
  // await` is load-bearing — without it an async boot rejection would escape this
137
- // try. Dispose is idempotent, so the success path's `stop()` never double-fires.
137
+ // try. Cleanups are idempotent, so the success path's `stop()` never double-fires.
138
138
  export async function startAgent(options: StartAgentOptions): Promise<StartAgentResult> {
139
- const captured: { dispose: (() => void) | null } = { dispose: null }
139
+ const bootCleanups: Array<() => void | Promise<void>> = []
140
140
  try {
141
- return await startAgentRuntime(options, (dispose) => {
142
- captured.dispose = dispose
141
+ return await startAgentRuntime(options, (cleanup) => {
142
+ bootCleanups.push(cleanup)
143
143
  })
144
144
  } catch (err) {
145
- captured.dispose?.()
145
+ for (const cleanup of bootCleanups.reverse()) {
146
+ try {
147
+ await cleanup()
148
+ } catch (cleanupErr) {
149
+ console.warn(
150
+ `[run] boot-failure cleanup error: ${cleanupErr instanceof Error ? cleanupErr.message : cleanupErr}`,
151
+ )
152
+ }
153
+ }
146
154
  throw err
147
155
  }
148
156
  }
@@ -161,7 +169,7 @@ async function startAgentRuntime(
161
169
  createChannelManager: createChannelManagerFor = createChannelManager,
162
170
  createTunnelManager: createTunnelManagerFor = createTunnelManager,
163
171
  }: StartAgentOptions,
164
- onProcessGlobalsInstalled: (dispose: () => void) => void,
172
+ registerBootCleanup: (cleanup: () => void | Promise<void>) => void,
165
173
  ): Promise<StartAgentResult> {
166
174
  const reloadRegistry = new ReloadRegistry()
167
175
 
@@ -216,13 +224,9 @@ async function startAgentRuntime(
216
224
  if (sigtermHandler !== null) process.removeListener('SIGTERM', sigtermHandler)
217
225
  }
218
226
  let sigtermHandler: (() => void) | null = null
219
- onProcessGlobalsInstalled(disposeProcessGlobals)
227
+ registerBootCleanup(disposeProcessGlobals)
220
228
 
221
229
  const { config: cwdConfig, pluginConfigs: pluginConfigsByName } = loadConfigBundleSync(cwd)
222
- // Vector agents omit the system-prompt `# Memory` section and inject memory
223
- // per-turn instead. Derived once here: `memory.vector.enabled` is
224
- // restart-required, so a single boot read is coherent for the process.
225
- const suppressSystemMemory = vectorEnabledFromMemoryConfig(pluginConfigsByName.memory)
226
230
  const githubTokenBridge = createGithubTokenBridge()
227
231
  const mcpManager =
228
232
  cwdConfig.mcpServers.length > 0 ? createMcpManager(cwdConfig.mcpServers, { env: process.env }) : null
@@ -256,7 +260,7 @@ async function startAgentRuntime(
256
260
  hasGithubAppTokenResolver: githubTokenBridge.hasAppTokenResolver,
257
261
  ...(cwdConfig.roles !== undefined ? { roles: cwdConfig.roles } : {}),
258
262
  })
259
- const vectorStartupPromise = suppressSystemMemory ? runVectorStartup(cwd) : Promise.resolve()
263
+ const vectorStartupPromise = runVectorStartup(cwd)
260
264
  let pluginsLoaded: LoadPluginsResult
261
265
  try {
262
266
  const [loaded] = await Promise.all([pluginsLoadedPromise, vectorStartupPromise])
@@ -265,6 +269,9 @@ async function startAgentRuntime(
265
269
  if (mcpManager !== null) await mcpManager.closeAll()
266
270
  throw err
267
271
  }
272
+ // Plugins are loaded (sqlite handles open); from here any boot failure must
273
+ // release them — the caller gets no stop() on the failure path.
274
+ registerBootCleanup(() => pluginsLoaded.disposePlugins())
268
275
 
269
276
  reloadRegistry.register(
270
277
  createConfigReloadable({
@@ -368,7 +375,6 @@ async function startAgentRuntime(
368
375
  stream,
369
376
  reloadRegistry,
370
377
  pluginRuntime,
371
- suppressSystemMemory,
372
378
  getChannelRouter: () => channelManager.router,
373
379
  rehydrateCapOptions: resolveCapOptionsFromConfig(pluginConfigsByName['tool-result-cap']),
374
380
  permissions: pluginsLoaded.permissions,
@@ -560,6 +566,7 @@ async function startAgentRuntime(
560
566
  return name
561
567
  },
562
568
  })
569
+ registerBootCleanup(() => subagentConsumer.stop())
563
570
  subagentConsumer.start()
564
571
 
565
572
  // Populated by startScheduler's factory (onCountStore). The consumer
@@ -612,7 +619,6 @@ async function startAgentRuntime(
612
619
  containerName: containerNameOpt.containerName,
613
620
  sessionFactory,
614
621
  channelRouter: channelManager.router,
615
- suppressSystemMemory,
616
622
  ...mcpManagerOpt,
617
623
  }),
618
624
  subagent: (subName: string, payload?: unknown) =>
@@ -653,7 +659,6 @@ async function startAgentRuntime(
653
659
  channelRouter: channelManager.router,
654
660
  origin: cronOrigin,
655
661
  permissions: pluginsLoaded.permissions,
656
- suppressSystemMemory,
657
662
  ...(refOverride !== undefined ? { refOverride } : {}),
658
663
  ...(snap.hasAnyPluginContent
659
664
  ? {
@@ -700,11 +705,13 @@ async function startAgentRuntime(
700
705
  // delivers only to live subscribers (no replay), so a fire published before
701
706
  // the subscription exists would be lost. Subscribing to an empty stream is
702
707
  // harmless when there are no jobs.
708
+ registerBootCleanup(() => cronConsumer.stop())
703
709
  cronConsumer.start()
704
710
  const scheduler = await startScheduler({
705
711
  cwd,
706
712
  loadCron,
707
713
  createSchedulerFor: factory,
714
+ registerBootCleanup,
708
715
  stream,
709
716
  hasInternalJobs: internalJobs().length > 0,
710
717
  getSubagents: () => pluginRuntime.get().subagents,
@@ -720,6 +727,7 @@ async function startAgentRuntime(
720
727
  }
721
728
 
722
729
  const tunnelBridge: TunnelBridge = createTunnelBridge({ stream, channelManager })
730
+ registerBootCleanup(() => tunnelBridge.stop())
723
731
 
724
732
  // Bridge `subagent.completed` broadcasts into the channel router so a
725
733
  // backgrounded subagent finishing wakes up its parent channel session
@@ -730,6 +738,7 @@ async function startAgentRuntime(
730
738
  stream,
731
739
  router: channelManager.router,
732
740
  })
741
+ registerBootCleanup(() => subagentCompletionBridge.stop())
733
742
 
734
743
  // Fan a landed formal review verdict out to the sibling sessions reviewing the
735
744
  // same PR so they stand down from a redundant verdict (the per-thread fan-out
@@ -740,12 +749,25 @@ async function startAgentRuntime(
740
749
  stream,
741
750
  router: channelManager.router,
742
751
  })
752
+ registerBootCleanup(() => prVerdictActivityBridge.stop())
743
753
  setReviewObserver((review) => {
744
754
  stream.publish({
745
755
  target: { kind: 'broadcast' },
746
756
  payload: { kind: 'pr.verdict-activity', ...review },
747
757
  })
748
758
  })
759
+ registerBootCleanup(() => setReviewObserver(null))
760
+
761
+ // Registered before channels so its cache clear lands before any channel
762
+ // session teardown observes it. secrets.json provider credentials are not
763
+ // part of the typeclaw.json config diff, so a rotated key takes effect on
764
+ // `typeclaw reload` only via this dedicated scope. Live sessions captured
765
+ // their AuthStorage at creation, so teardown recreates them with fresh auth.
766
+ reloadRegistry.register(
767
+ createProviderAuthReloadable({
768
+ onProviderAuthChanged: () => channelManager.router.tearDownAllLive(),
769
+ }),
770
+ )
749
771
 
750
772
  reloadRegistry.register(createChannelsReloadable({ manager: channelManager }))
751
773
 
@@ -769,6 +791,7 @@ async function startAgentRuntime(
769
791
  console.warn(`[run] channel restart-resume reserve failed: ${err instanceof Error ? err.message : err}`)
770
792
  }
771
793
 
794
+ registerBootCleanup(() => channelManager.stop())
772
795
  await channelManager.start()
773
796
 
774
797
  if (restartReservation !== null) {
@@ -786,12 +809,10 @@ async function startAgentRuntime(
786
809
  // In-flight coalescing for direct ctx.spawnSubagent calls mirrors the
787
810
  // SubagentConsumer's stream-path gate (subagents.ts:441). Two queued
788
811
  // `new-session` messages for the same (name, inFlightKey) drop the second
789
- // on the consumer side; without the same gate here, two consecutive
790
- // session.prompt fires (cold-start prompt N immediately followed by prompt
791
- // N+1 on the same channel session) could both fire memory-retrieval spawns
792
- // racing to write `memory/.retrieval-cache/<sessionId>.md`. Awaiting the
793
- // spawn in the hook used to mask this; now that the hook is fire-and-forget,
794
- // the race is exposed and the gate is mandatory.
812
+ // on the consumer side; without the same gate here, two consecutive direct
813
+ // plugin spawns with the same in-flight key could race. Awaiting the spawn in
814
+ // the hook used to mask this; detached hook paths expose the race and make the
815
+ // gate mandatory.
795
816
  //
796
817
  // Same key shape as the consumer: `${name}:${inFlightKey(payload)}` when the
797
818
  // subagent declares one, else just `${name}`. Collisions resolve cleanly
@@ -898,11 +919,10 @@ async function startAgentRuntime(
898
919
  outbound,
899
920
  sessionFactory,
900
921
  channelRouter: channelManager.router,
901
- suppressSystemMemory,
902
922
  ...mcpManagerOpt,
903
923
  })
904
924
 
905
- const server = createServer({
925
+ const serverFactory = createServer({
906
926
  port,
907
927
  reloadAll: () => reloadRegistry.reloadAll(),
908
928
  reloadRegistry,
@@ -923,23 +943,26 @@ async function startAgentRuntime(
923
943
  ...runtimeVersionOpt,
924
944
  ...tuiTokenOpt,
925
945
  ...containerBrokerOpt,
926
- }).start()
946
+ })
947
+ let server: BunServer | null = null
948
+ registerBootCleanup(() => server?.stop(true))
949
+ server = serverFactory.start()
927
950
 
928
951
  // Tunnel manager starts AFTER the WS server is up so a slow/hanging
929
952
  // provider (PR 2's cloudflared first-URL wait) cannot block TUI, reload,
930
953
  // or channel adapter availability. External providers resolve URLs
931
954
  // synchronously; future managed providers will resolve asynchronously
932
955
  // and broadcast URL events when ready.
956
+ registerBootCleanup(() => tunnelManager.stop())
933
957
  await tunnelManager.start()
934
958
 
935
959
  let stopped = false
936
960
  const stop = async () => {
937
961
  if (stopped) return
938
962
  stopped = true
939
- // The crash-guard and codex-observer disposers run in `finally` so an
940
- // earlier async teardown rejection (tunnel/channel/mcp stop) cannot strand
941
- // the process-global listeners they removed — a stranded fatal-guard
942
- // listener would survive into the next `startAgent` and double-fire.
963
+ // The final disposers run in `finally` so an earlier async teardown
964
+ // rejection cannot strand process-global listeners or plugin-owned handles
965
+ // into the next `startAgent`.
943
966
  try {
944
967
  scheduler?.stop()
945
968
  cronConsumer.stop()
@@ -954,6 +977,7 @@ async function startAgentRuntime(
954
977
  await channelManager.stop()
955
978
  await mcpManager?.closeAll()
956
979
  } finally {
980
+ await pluginsLoaded.disposePlugins()
957
981
  disposeProcessGlobals()
958
982
  }
959
983
  }
@@ -1078,6 +1102,7 @@ async function startScheduler({
1078
1102
  cwd,
1079
1103
  loadCron,
1080
1104
  createSchedulerFor,
1105
+ registerBootCleanup,
1081
1106
  stream,
1082
1107
  hasInternalJobs,
1083
1108
  getSubagents,
@@ -1086,6 +1111,7 @@ async function startScheduler({
1086
1111
  cwd: string
1087
1112
  loadCron: LoadCronFn
1088
1113
  createSchedulerFor: SchedulerFactory
1114
+ registerBootCleanup: (cleanup: () => void | Promise<void>) => void
1089
1115
  stream: Stream
1090
1116
  hasInternalJobs: boolean
1091
1117
  getSubagents?: () => SubagentRegistry
@@ -1110,6 +1136,7 @@ async function startScheduler({
1110
1136
  stream.publish({ target: { kind: 'cron', jobId: job.id }, payload: job })
1111
1137
  }
1112
1138
  const scheduler = await createSchedulerFor({ cwd, file, onFire, onCountStore })
1139
+ registerBootCleanup(() => scheduler.stop())
1113
1140
  scheduler.start()
1114
1141
  return scheduler
1115
1142
  }
@@ -58,10 +58,6 @@ export type CommandRunnerOptions = {
58
58
  // wire for the handler/command path.
59
59
  channelRouter: ChannelRouter | undefined
60
60
  mcpManager?: McpManager
61
- // When true, prompt sessions spawned here omit the system-prompt `# Memory`
62
- // section (vector agents inject memory per-turn). Forwarded to createSession
63
- // so command/handler sessions stay coherent with the rest of the runtime.
64
- suppressSystemMemory?: boolean
65
61
  }
66
62
 
67
63
  type CommandHandle = {
@@ -201,7 +197,6 @@ export function createCommandRunner(opts: CommandRunnerOptions): CommandRunner {
201
197
  signal: abortController.signal,
202
198
  sessionFactory: opts.sessionFactory,
203
199
  channelRouter: opts.channelRouter,
204
- ...(opts.suppressSystemMemory !== undefined ? { suppressSystemMemory: opts.suppressSystemMemory } : {}),
205
200
  ...(opts.mcpManager !== undefined ? { mcpManager: opts.mcpManager } : {}),
206
201
  }),
207
202
  subagent: (subName, payload) =>
@@ -388,7 +383,6 @@ export async function runPromptForCommand(args: {
388
383
  // so the spawned session exposes `channel_send`.
389
384
  channelRouter?: ChannelRouter
390
385
  mcpManager?: McpManager
391
- suppressSystemMemory?: boolean
392
386
  // Test seam for the agent-session boundary. Production passes the real
393
387
  // `createSessionWithDispose`; tests inject a fake to verify wiring
394
388
  // (specifically: the sessionManager handed off must be persisted, not
@@ -418,14 +412,11 @@ export async function runPromptForCommand(args: {
418
412
  ...(args.mcpManager !== undefined ? { mcpManager: args.mcpManager } : {}),
419
413
  ...(args.runtimeVersion !== undefined ? { runtimeVersion: args.runtimeVersion } : {}),
420
414
  ...(args.containerName !== undefined ? { containerName: args.containerName } : {}),
421
- ...(args.suppressSystemMemory !== undefined ? { suppressSystemMemory: args.suppressSystemMemory } : {}),
422
415
  })
423
416
  const detachAbort = bindSignalToSession(args.signal, session)
424
417
  // Mirror the other turn drivers (TUI/channel/cron/subagent): fire
425
- // session.turn.start with a retrievalContext so a vector agent — whose
426
- // system-prompt `# Memory` section is suppressed — gets its long-term memory
427
- // injected per-turn into the user prompt here too. Without this, command and
428
- // handler prompt sessions would have no memory at all under vector mode.
418
+ // session.turn.start with a retrievalContext so vector memory is injected
419
+ // per-turn into command and handler prompts too.
429
420
  const turnEvent = { sessionId, agentDir: args.agentDir, origin: args.origin }
430
421
  const retrievalContext = { results: '' }
431
422
  try {
@@ -9,9 +9,9 @@ The agent's long-term memory is sharded across files in `memory/topics/<slug>.md
9
9
 
10
10
  ## Reading
11
11
 
12
- The `# Memory` section comes from topic shards only. Undreamed daily-stream events are **not** injected — call `memory_search` when you need them. When total shard bytes are above the 16 KB injection budget (or when speaking in a channel), shard bodies are dropped — only the heading + `cites=N, days=N, lastReinforced=YYYY-MM-DD` shows; call `memory_search` to fetch the bodies you need. The same `memory_search` covers both surfaces (topic shards and undreamed stream events), so one tool call reaches everything.
12
+ The `# Memory` block is injected per turn into the user prompt; the system prompt never contains long-term memory; vector memory is always on. Undreamed daily-stream events are **not** dumped wholesale into prompts — use `memory_search` when you need them. When speaking in a channel, shard bodies are dropped and only headings/slugs show; call `memory_search` to fetch the bodies you need. The same `memory_search` covers both surfaces (topic shards and undreamed stream events), so one tool call reaches everything.
13
13
 
14
- **Where the `# Memory` section lives depends on `memory.vector.enabled`.** With vector **off** (default), it's part of the system prompt, snapshotted once at session creation. With vector **on**, it is removed from the system prompt and injected fresh into each **user turn** instead: under budget you get all shard bodies, over budget you get the top-K shards/fragments most relevant to the current message (hybrid vector + keyword search). This keeps the system-prompt cache prefix stable across a session and lets retrieval track the current topic instead of a stale session-start snapshot. Either way `memory_search` remains available on demand.
14
+ Under budget you get shard bodies de-duplicated across turns; over budget you get the top-K shards/fragments/references most relevant to the current message (hybrid vector + keyword search). This keeps the system-prompt cache prefix stable across a session and lets retrieval track the current topic instead of a stale session-start snapshot. `memory_search` remains available on demand.
15
15
 
16
16
  ## Writing
17
17
 
@@ -380,7 +380,7 @@ hooks: {
380
380
 
381
381
  The `prompt` field on `SessionPromptEvent` is the system prompt as it's being composed by `createResourceLoader` (`basePrompt + IDENTITY.md + SOUL.md`), NOT the user's most recent message. Reading it as if it were the user's prompt — and feeding it to a retrieval system, classifier, or LLM — will keyword-mine TypeClaw's framing prose (`TypeClaw`, `subagent`, `AGENTS.md`) on every session.
382
382
 
383
- If you want the **user's actual prompt** (their message text), subscribe to `session.turn.start` and read `event.userPrompt`. The bundled memory plugin's `memory-retrieval` subagent learned this the hard way; see `src/bundled-plugins/memory/index.ts`'s `session.turn.start` handler.
383
+ If you want the **user's actual prompt** (their message text), subscribe to `session.turn.start` and read `event.userPrompt`. The bundled memory plugin's per-turn retrieval uses this hook so it embeds the user's text instead of the assembling system prompt; see `src/bundled-plugins/memory/index.ts`'s `session.turn.start` handler.
384
384
 
385
385
  #### CRITICAL: `session.prompt` and provider prompt caching
386
386
 
@@ -0,0 +1,24 @@
1
+ import { rm } from 'node:fs/promises'
2
+
3
+ // `Bun.gc(true)` is load-bearing, not suppression: bun:sqlite holds the DB file
4
+ // handle open past `db.close()` until the Statement wrappers are finalized (Bun
5
+ // #25964), and on Windows that open handle pins the dir so `rm` throws EBUSY.
6
+ // The forced GC runs those finalizers. Bun ignores `fs.rm`'s maxRetries, so the
7
+ // loop covers the brief window a sibling worker / just-exited subprocess needs.
8
+ const RETRIES = 10
9
+ const RETRY_DELAY_MS = 50
10
+ const RETRYABLE = new Set(['EBUSY', 'EPERM', 'ENOTEMPTY'])
11
+
12
+ export async function rmTempDir(dir: string): Promise<void> {
13
+ for (let attempt = 0; attempt <= RETRIES; attempt++) {
14
+ Bun.gc(true)
15
+ try {
16
+ await rm(dir, { recursive: true, force: true })
17
+ return
18
+ } catch (err) {
19
+ const code = (err as NodeJS.ErrnoException).code
20
+ if (attempt === RETRIES || code === undefined || !RETRYABLE.has(code)) throw err
21
+ await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS))
22
+ }
23
+ }
24
+ }
@@ -2528,11 +2528,7 @@
2528
2528
  "bufferBytes": 500000,
2529
2529
  "injectionBudgetBytes": 16384,
2530
2530
  "minIdleDeltaLines": 3,
2531
- "spawnTimeoutMs": 50000,
2532
- "retrievalSpawnTimeoutMs": 30000,
2533
- "vector": {
2534
- "enabled": false
2535
- }
2531
+ "spawnTimeoutMs": 50000
2536
2532
  },
2537
2533
  "type": "object",
2538
2534
  "properties": {
@@ -2566,12 +2562,6 @@
2566
2562
  "minimum": 1,
2567
2563
  "maximum": 9007199254740991
2568
2564
  },
2569
- "retrievalSpawnTimeoutMs": {
2570
- "default": 30000,
2571
- "type": "integer",
2572
- "minimum": 1,
2573
- "maximum": 9007199254740991
2574
- },
2575
2565
  "dreaming": {
2576
2566
  "type": "object",
2577
2567
  "properties": {
@@ -2580,18 +2570,6 @@
2580
2570
  "minLength": 1
2581
2571
  }
2582
2572
  }
2583
- },
2584
- "vector": {
2585
- "default": {
2586
- "enabled": false
2587
- },
2588
- "type": "object",
2589
- "properties": {
2590
- "enabled": {
2591
- "default": false,
2592
- "type": "boolean"
2593
- }
2594
- }
2595
2573
  }
2596
2574
  }
2597
2575
  },