typeclaw 0.48.0 → 0.48.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/package.json +1 -1
  2. package/src/agent/plugin-tools.ts +68 -9
  3. package/src/agent/system-prompt.ts +8 -0
  4. package/src/agent-messenger/config-dir.ts +131 -0
  5. package/src/agent-messenger/host-config-dir.ts +121 -0
  6. package/src/bundled-plugins/guard/policies/managed-config.ts +7 -20
  7. package/src/bundled-plugins/guard/policies/non-workspace-write.ts +7 -20
  8. package/src/bundled-plugins/guard/policies/skill-authoring.ts +8 -20
  9. package/src/bundled-plugins/memory/README.md +2 -0
  10. package/src/bundled-plugins/memory/dreaming.ts +2 -0
  11. package/src/bundled-plugins/security/policies/private-surface-read.ts +10 -23
  12. package/src/cli/channel.ts +15 -16
  13. package/src/cli/hostd.ts +8 -13
  14. package/src/cli/init.ts +21 -7
  15. package/src/cli/model.ts +6 -3
  16. package/src/cli/restart.ts +13 -15
  17. package/src/compose/restart.ts +14 -6
  18. package/src/config/providers.ts +106 -143
  19. package/src/container/agent-operation-lock.ts +85 -0
  20. package/src/container/controller.ts +6 -0
  21. package/src/container/index.ts +8 -0
  22. package/src/container/restart.ts +55 -0
  23. package/src/container/shared.ts +5 -0
  24. package/src/container/start.ts +112 -4
  25. package/src/container/stop.ts +17 -1
  26. package/src/hostd/daemon.ts +1 -1
  27. package/src/hostd/paths.ts +17 -0
  28. package/src/hostd/supervisor.ts +27 -10
  29. package/src/init/dockerfile.ts +6 -5
  30. package/src/init/env-file.ts +42 -18
  31. package/src/init/instagram-auth.ts +55 -31
  32. package/src/init/kakaotalk-auth.ts +1 -5
  33. package/src/init/line-auth.ts +45 -29
  34. package/src/init/models-dev.ts +114 -7
  35. package/src/init/opengateway-catalog.ts +120 -0
  36. package/src/path-safety/real-intended-path.ts +111 -0
  37. package/src/sandbox/canonical-secrets.ts +10 -1
  38. package/src/sandbox/errors.ts +0 -32
  39. package/src/sandbox/privileged-runtime.ts +8 -94
  40. package/src/test-helpers/fake-agent-operation-lock.ts +66 -0
  41. package/typeclaw.schema.json +10 -30
@@ -0,0 +1,85 @@
1
+ import { resolve } from 'node:path'
2
+
3
+ import lockfile from 'proper-lockfile'
4
+
5
+ import { agentOperationLockPath, ensureAgentOperationLocksDir } from '@/hostd/paths'
6
+
7
+ import { containerNameFromCwd } from './shared'
8
+
9
+ export type AgentOperation = 'start' | 'stop' | 'restart' | 'line-auth' | 'instagram-auth'
10
+ export type AgentOperationLease = { readonly containerName: string; readonly agentDir: string }
11
+ export type AgentOperationReservation =
12
+ | { ok: true; lease: AgentOperationLease; release: () => Promise<void> }
13
+ | { ok: false; reason: string }
14
+ export type ReserveAgentOperationLock = (input: {
15
+ agentDir: string
16
+ operation: AgentOperation
17
+ }) => Promise<AgentOperationReservation>
18
+ export type AgentOperationLockResult<T> = { ok: true; value: T } | { ok: false; reason: string }
19
+ export type WithAgentOperationLock = <T>(
20
+ input: { agentDir: string; operation: AgentOperation; lease?: AgentOperationLease },
21
+ run: (lease: AgentOperationLease) => Promise<T>,
22
+ ) => Promise<AgentOperationLockResult<T>>
23
+
24
+ export const withAgentOperationLock: WithAgentOperationLock = async (input, run) => {
25
+ const agentDir = resolve(input.agentDir)
26
+ const containerName = containerNameFromCwd(agentDir)
27
+
28
+ if (input.lease !== undefined) {
29
+ if (input.lease.containerName !== containerName || resolve(input.lease.agentDir) !== agentDir) {
30
+ return {
31
+ ok: false,
32
+ reason: `Agent operation lease does not match agent ${containerName} at ${agentDir}.`,
33
+ }
34
+ }
35
+ return { ok: true, value: await run(input.lease) }
36
+ }
37
+
38
+ // Acquisition is deliberately outside the try that runs the operation: an
39
+ // ELOCKED thrown from inside `run` (a nested lifecycle call) must not be
40
+ // reported as contention for THIS lock.
41
+ const reservation = await reserveAgentOperationLock(input)
42
+ if (!reservation.ok) return reservation
43
+
44
+ try {
45
+ return { ok: true, value: await run(reservation.lease) }
46
+ } finally {
47
+ await reservation.release()
48
+ }
49
+ }
50
+
51
+ export const reserveAgentOperationLock: ReserveAgentOperationLock = async (input) => {
52
+ const agentDir = resolve(input.agentDir)
53
+ const containerName = containerNameFromCwd(agentDir)
54
+ const lease = { containerName, agentDir }
55
+ await ensureAgentOperationLocksDir()
56
+ const path = agentOperationLockPath(containerName)
57
+
58
+ let releaseLock: () => Promise<void>
59
+ try {
60
+ // Interactive QR and checkpoint prompts can hold this lease for minutes, so
61
+ // contenders fail after a bounded wait instead of queueing behind a human.
62
+ releaseLock = await lockfile.lock(path, {
63
+ lockfilePath: path,
64
+ realpath: false,
65
+ stale: 30_000,
66
+ retries: { retries: 20, factor: 1, minTimeout: 100, maxTimeout: 100, randomize: false },
67
+ })
68
+ } catch (error) {
69
+ if (errorCode(error) === 'ELOCKED') {
70
+ return {
71
+ ok: false,
72
+ reason: `Another TypeClaw lifecycle or channel-auth operation is already in progress for agent \`${containerName}\`. Wait for it to finish, then retry. If the previous process was killed, the lock is reclaimed within 30 seconds.`,
73
+ }
74
+ }
75
+ throw error
76
+ }
77
+
78
+ return { ok: true, lease, release: async () => await releaseLock().catch(() => {}) }
79
+ }
80
+
81
+ function errorCode(error: unknown): string | undefined {
82
+ return typeof error === 'object' && error !== null && 'code' in error
83
+ ? String((error as { code?: unknown }).code)
84
+ : undefined
85
+ }
@@ -1,4 +1,5 @@
1
1
  import { logs, type LogsOptions, type LogsResult } from './logs'
2
+ import { restart, type RestartOptions, type RestartResult } from './restart'
2
3
  import { containerNameFromCwd, imageTagFromCwd } from './shared'
3
4
  import { shell, type ShellResult } from './shell'
4
5
  import { start, type StartOptions, type StartResult } from './start'
@@ -17,6 +18,7 @@ import { stop, type StopOptions, type StopResult } from './stop'
17
18
  export interface Controller {
18
19
  start(options: StartOptions): Promise<StartResult>
19
20
  stop(options: StopOptions): Promise<StopResult>
21
+ restart(options: RestartOptions): Promise<RestartResult>
20
22
  status(options: StatusOptions): Promise<ContainerStatus>
21
23
  stats(options: StatsOptions): Promise<ContainerStats>
22
24
  logs(options: LogsOptions): Promise<LogsResult>
@@ -31,6 +33,7 @@ export function createLocalDockerController(): Controller {
31
33
  return {
32
34
  start: (options) => start(options),
33
35
  stop: (options) => stop(options),
36
+ restart: (options) => restart(options),
34
37
  status: (options) => status(options),
35
38
  stats: (options) => stats(options),
36
39
  logs: (options) => logs(options),
@@ -53,6 +56,9 @@ export function createNoopController(): Controller {
53
56
  async stop({ cwd }) {
54
57
  return { ok: false, reason: unsupported('stop', cwd) }
55
58
  },
59
+ async restart({ cwd }) {
60
+ return { ok: false, reason: unsupported('restart', cwd) }
61
+ },
56
62
  async status({ cwd }) {
57
63
  return { kind: 'missing', containerName: containerNameFromCwd(cwd), imageTag: imageTagFromCwd(cwd) }
58
64
  },
@@ -54,3 +54,11 @@ export {
54
54
  type StartResult,
55
55
  } from './start'
56
56
  export { planStop, stop, type StopOptions, type StopPlan, type StopResult } from './stop'
57
+ export { restart, type RestartOptions, type RestartResult } from './restart'
58
+ export {
59
+ type AgentOperation,
60
+ type AgentOperationLease,
61
+ type AgentOperationLockResult,
62
+ type WithAgentOperationLock,
63
+ withAgentOperationLock,
64
+ } from './agent-operation-lock'
@@ -0,0 +1,55 @@
1
+ import { type AgentOperationLease, type WithAgentOperationLock, withAgentOperationLock } from './agent-operation-lock'
2
+ import { type CurrentHostDaemon, start, type StartResult } from './start'
3
+ import { stop, type StopResult } from './stop'
4
+
5
+ export type RestartOptions = {
6
+ cwd: string
7
+ preferredHostPort: number
8
+ forceBuild?: boolean
9
+ cliEntry?: string
10
+ reuseCurrentHostDaemon?: boolean
11
+ currentHostDaemon?: CurrentHostDaemon
12
+ onStopped?: (result: Extract<StopResult, { ok: true }>) => void
13
+ operationLock?: WithAgentOperationLock
14
+ operationLease?: AgentOperationLease
15
+ }
16
+
17
+ export type RestartResult =
18
+ | { ok: true; stop: Extract<StopResult, { ok: true }>; start: Extract<StartResult, { ok: true }> }
19
+ | { ok: false; reason: string }
20
+
21
+ export async function restart(options: RestartOptions): Promise<RestartResult> {
22
+ const operationLock = options.operationLock ?? withAgentOperationLock
23
+ try {
24
+ const locked = await operationLock(
25
+ { agentDir: options.cwd, operation: 'restart', lease: options.operationLease },
26
+ async (lease) => await restartWithLease(options, lease, operationLock),
27
+ )
28
+ return locked.ok ? locked.value : { ok: false, reason: locked.reason }
29
+ } catch (error) {
30
+ return { ok: false, reason: error instanceof Error ? error.message : String(error) }
31
+ }
32
+ }
33
+
34
+ async function restartWithLease(
35
+ options: RestartOptions,
36
+ lease: AgentOperationLease,
37
+ operationLock: WithAgentOperationLock,
38
+ ): Promise<RestartResult> {
39
+ const stopped = await stop({ cwd: options.cwd, operationLock, operationLease: lease })
40
+ if (!stopped.ok) return { ok: false, reason: `stop failed: ${stopped.reason}` }
41
+ options.onStopped?.(stopped)
42
+
43
+ const started = await start({
44
+ cwd: options.cwd,
45
+ preferredHostPort: options.preferredHostPort,
46
+ forceBuild: options.forceBuild,
47
+ cliEntry: options.cliEntry,
48
+ reuseCurrentHostDaemon: options.reuseCurrentHostDaemon,
49
+ currentHostDaemon: options.currentHostDaemon,
50
+ operationLock,
51
+ operationLease: lease,
52
+ })
53
+ if (!started.ok) return { ok: false, reason: `start failed: ${started.reason}` }
54
+ return { ok: true, stop: stopped, start: started }
55
+ }
@@ -379,6 +379,11 @@ export function classifyRmStderr(stderr: string): BenignRmKind {
379
379
  return null
380
380
  }
381
381
 
382
+ export function isGenuineMissingContainer(stderr: string): boolean {
383
+ const normalized = stderr.toLowerCase()
384
+ return normalized.includes('no such container') || normalized.includes('no such object')
385
+ }
386
+
382
387
  // Detects Docker's name-conflict response from `docker run --name <X>`:
383
388
  // docker: Error response from daemon: Conflict. The container name
384
389
  // "/<X>" is already in use by container "<id>". You have to remove
@@ -4,6 +4,7 @@ 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 { migrateAgentMessengerConfigDir, resolveAgentMessengerConfigPolicy } from '@/agent-messenger/config-dir'
7
8
  import { expandMountPath, loadConfigSync, withDefaultPlugins, type Config, type PortForward } from '@/config'
8
9
  import { applyBoundGcConfig } from '@/git/bound-gc-config'
9
10
  import { commitGitignoreWithUntracks, untrackTrulyIgnoredFiles } from '@/git/reconcile-ignored'
@@ -32,6 +33,7 @@ import { linkWindowsDevTypeclaw, resolveBunLinkedPackage, type RunBunLink } from
32
33
  import { isWindows } from '@/shared'
33
34
  import { hostLocaleIsCjk } from '@/shared/host-locale'
34
35
 
36
+ import { type AgentOperationLease, type WithAgentOperationLock, withAgentOperationLock } from './agent-operation-lock'
35
37
  import { archiveContainerLogs, type DockerLogArchiver } from './log-archive'
36
38
  import { CONTAINER_PORT, TUI_TOKEN_LABEL, findFreePort, isPortAllocatedError, resolveTuiToken } from './port'
37
39
  import {
@@ -45,6 +47,7 @@ import {
45
47
  dockerConfigDir,
46
48
  imageTagFromCwd,
47
49
  isContainerNameConflict,
50
+ isGenuineMissingContainer,
48
51
  isMissingDockerCredentialHelper,
49
52
  parseContainerInspectOutput,
50
53
  sanitizeDockerConfigJson,
@@ -176,6 +179,8 @@ export type StartOptions = {
176
179
  // Test seam for the host-stage writable-config preflight.
177
180
  assertConfigWritable?: (cwd: string) => void
178
181
  archiveLogs?: DockerLogArchiver
182
+ operationLock?: WithAgentOperationLock
183
+ operationLease?: AgentOperationLease
179
184
  }
180
185
 
181
186
  export type HostDaemonStatus =
@@ -210,7 +215,20 @@ export type StartResult =
210
215
  }
211
216
  | { ok: false; reason: string }
212
217
 
213
- export async function start({
218
+ export async function start(options: StartOptions): Promise<StartResult> {
219
+ const operationLock = options.operationLock ?? withAgentOperationLock
220
+ try {
221
+ const locked = await operationLock(
222
+ { agentDir: options.cwd, operation: 'start', lease: options.operationLease },
223
+ async () => await runStart(options),
224
+ )
225
+ return locked.ok ? locked.value : { ok: false, reason: locked.reason }
226
+ } catch (error) {
227
+ return { ok: false, reason: error instanceof Error ? error.message : String(error) }
228
+ }
229
+ }
230
+
231
+ async function runStart({
214
232
  cwd,
215
233
  preferredHostPort,
216
234
  forceBuild = false,
@@ -246,7 +264,7 @@ export async function start({
246
264
  // side effects (template writes, .gitignore commits, package.json migration)
247
265
  // that would surprise a user invoking `compose start` against a partially-up
248
266
  // tree.
249
- const state = await inspectContainer(exec, containerName)
267
+ let state = await inspectContainer(exec, containerName)
250
268
 
251
269
  // Bound gc/repack memory even when start() is an already-running no-op below:
252
270
  // the running agent's backup keeps committing, so it must not keep using the
@@ -263,12 +281,44 @@ export async function start({
263
281
  }
264
282
  }
265
283
 
284
+ if (state.kind === 'indeterminate') {
285
+ return {
286
+ ok: false,
287
+ reason: `Docker state could not be determined for container ${containerName}; TypeClaw is refusing to migrate or launch while the container may be live. Resolve Docker access and retry. ${state.reason}`,
288
+ }
289
+ }
290
+
266
291
  if (state.kind === 'present' && state.running) {
267
292
  return await reportAlreadyRunning(exec, cwd, containerName)
268
293
  }
269
294
 
270
295
  assertConfigWritable(cwd)
271
296
 
297
+ const agentMessengerPolicy = resolveAgentMessengerConfigPolicy(cwd)
298
+ state = await inspectContainer(exec, containerName)
299
+ if (state.kind === 'malformed') {
300
+ return {
301
+ ok: false,
302
+ reason: `docker inspect returned malformed container identity/state (${state.reason}); preserving container ${containerName} and its logs.`,
303
+ }
304
+ }
305
+ if (state.kind === 'indeterminate') {
306
+ return {
307
+ ok: false,
308
+ reason: `Docker state could not be determined for container ${containerName}; TypeClaw is refusing to migrate or launch while the container may be live. Resolve Docker access and retry. ${state.reason}`,
309
+ }
310
+ }
311
+ if (state.kind === 'present' && state.running) {
312
+ return {
313
+ ok: false,
314
+ reason: `Container ${containerName} became running during start preflight; refusing to migrate agent-messenger credentials or launch a replacement.`,
315
+ }
316
+ }
317
+ if (agentMessengerPolicy.migrate) {
318
+ const agentMessengerMigration = await migrateAgentMessengerConfigDir(cwd)
319
+ if (!agentMessengerMigration.ok) return { ok: false, reason: agentMessengerMigration.reason }
320
+ }
321
+
272
322
  // TypeClaw owns Dockerfile, .gitignore, and the bun-workspaces shape of
273
323
  // package.json. Refresh them from the current CLI templates on every fresh
274
324
  // start (not just --build) so version drift between the agent folder and
@@ -674,6 +724,47 @@ export async function planStart({
674
724
  // misattribute to bot detection. 2g matches the Playwright/Puppeteer
675
725
  // canonical recommendation and is a memory cap, not an allocation (only
676
726
  // used pages count against the host).
727
+ // `--init` puts Docker's bundled `docker-init` (tini) at PID 1 and demotes
728
+ // the entrypoint shim to PID 2. It is load-bearing, not hygiene: without it
729
+ // the container leaks a kernel user namespace on every aborted or timed-out
730
+ // model bash call, until every `bwrap` in the container — and in every other
731
+ // typeclaw container on the same host — dies with
732
+ // `unshare user ns: No space left on device`.
733
+ //
734
+ // The chain: each model bash call runs the sandbox under `bash -c`, and the
735
+ // sandbox mints a fresh user namespace per call on BOTH strategies — under
736
+ // the default `proc-bind`/`tmpfs` it is `bwrap --unshare-all`, while
737
+ // `sandbox.realProc` splits the pid namespace out into `unshare --pid --fork
738
+ // --mount --mount-proc` and has bwrap take the user namespace via an
739
+ // explicit `--unshare-user` (see buildArgv in src/sandbox/build.ts). On
740
+ // timeout or abort the upstream bash tool SIGKILLs the process GROUP, then
741
+ // reaps its own direct `bash` child by waiting on the handle it holds. The
742
+ // sandbox descendant it holds no handle for is re-parented to PID 1 instead.
743
+ // Since the shim `exec`s into bun, PID 1 was the Bun runtime — and a JS
744
+ // runtime never `wait()`s on orphans it did not spawn, so that descendant
745
+ // stays a zombie for the life of the container. A zombie drops its
746
+ // `nsproxy` at `do_exit()` (so
747
+ // mount/net namespaces are freed early) but keeps its `struct cred` until
748
+ // it is reaped, and `cred->user_ns` pins the user namespace; its surviving
749
+ // `struct pid` pins the pid namespace, which pins that same user namespace
750
+ // again. The `UCOUNT_USER_NAMESPACES` charge is only returned by
751
+ // `free_user_ns()`, so it is never returned at all.
752
+ //
753
+ // That charge is accounted against the ANCESTOR (host) user namespace, so
754
+ // the exhausted resource is the host kernel's `user.max_user_namespaces`,
755
+ // shared across every container on the box. Raising the host sysctl only
756
+ // buys time; reaping is the fix. tini reaps re-parented orphans, which
757
+ // releases the creds and returns the ucount.
758
+ //
759
+ // Chosen over baking `tini` into the image because the per-agent Dockerfile
760
+ // pins a PUBLISHED `typeclaw-base:X.Y.Z` (see refreshDockerfile): a new
761
+ // baseline apt package would not exist in already-released base tags. This
762
+ // flag needs no image change and takes effect on the next container create.
763
+ // `--init` is create-time state, so a container launched before this flag
764
+ // existed keeps leaking: `start()` deliberately no-ops on a live container.
765
+ // Operators must `typeclaw restart` (removes and recreates), not
766
+ // `docker restart` (reuses the old HostConfig).
767
+ //
677
768
  // `seccomp=unconfined` lets `bwrap(1)` (installed in baseline; see
678
769
  // BASELINE_APT_PACKAGES in src/init/dockerfile.ts) create user/pid/mount
679
770
  // namespaces from inside the container. Docker's default seccomp profile
@@ -692,6 +783,7 @@ export async function planStart({
692
783
  const runArgs = [
693
784
  'run',
694
785
  '-d',
786
+ '--init',
695
787
  '--name',
696
788
  containerName,
697
789
  '--shm-size=2g',
@@ -764,6 +856,10 @@ export async function planStart({
764
856
  if (existsSync(join(cwd, ENV_FILE))) {
765
857
  runArgs.push('--env-file', join(cwd, ENV_FILE))
766
858
  }
859
+ const agentMessengerPolicy = resolveAgentMessengerConfigPolicy(cwd)
860
+ if (agentMessengerPolicy.override === null) {
861
+ runArgs.push('-e', `AGENT_MESSENGER_CONFIG_DIR=${agentMessengerPolicy.containerDir}`)
862
+ }
767
863
 
768
864
  // Propagate the host timezone so cron schedules in typeclaw.json (and
769
865
  // cron.json jobs without an explicit `timezone`) fire at wall-clock times
@@ -1003,10 +1099,22 @@ type InspectedState =
1003
1099
  | { kind: 'missing' }
1004
1100
  | { kind: 'present'; running: boolean; containerId: string }
1005
1101
  | { kind: 'malformed'; reason: string }
1102
+ | { kind: 'indeterminate'; reason: string }
1006
1103
 
1007
1104
  async function inspectContainer(exec: DockerExec, name: string): Promise<InspectedState> {
1008
- const result = await exec(['inspect', '--format', '{{.Id}}|{{.State.Running}}', name])
1009
- if (result.exitCode !== 0) return { kind: 'missing' }
1105
+ let result: DockerExecResult
1106
+ try {
1107
+ result = await exec(['inspect', '--format', '{{.Id}}|{{.State.Running}}', name])
1108
+ } catch (error) {
1109
+ return { kind: 'indeterminate', reason: error instanceof Error ? error.message : String(error) }
1110
+ }
1111
+ if (result.exitCode !== 0) {
1112
+ if (isGenuineMissingContainer(result.stderr)) return { kind: 'missing' }
1113
+ return {
1114
+ kind: 'indeterminate',
1115
+ reason: result.stderr.trim() || `docker inspect exited with code ${result.exitCode}`,
1116
+ }
1117
+ }
1010
1118
  const parsed = parseContainerInspectOutput(result.stdout)
1011
1119
  if (!parsed.ok) return { kind: 'malformed', reason: parsed.reason }
1012
1120
  return { kind: 'present', running: parsed.running, containerId: parsed.containerId }
@@ -1,6 +1,7 @@
1
1
  import { DEFAULT_LOG_RETENTION_DAYS, loadConfigSync } from '@/config'
2
2
  import { isDaemonReachable, send as sendToDaemon } from '@/hostd/client'
3
3
 
4
+ import { type AgentOperationLease, type WithAgentOperationLock, withAgentOperationLock } from './agent-operation-lock'
4
5
  import { archiveContainerLogs, type DockerLogArchiver } from './log-archive'
5
6
  import {
6
7
  classifyRmStderr,
@@ -22,9 +23,24 @@ export type StopOptions = {
22
23
  cwd: string
23
24
  exec?: DockerExec
24
25
  archiveLogs?: DockerLogArchiver
26
+ operationLock?: WithAgentOperationLock
27
+ operationLease?: AgentOperationLease
25
28
  }
26
29
 
27
- export async function stop({
30
+ export async function stop(options: StopOptions): Promise<StopResult> {
31
+ const operationLock = options.operationLock ?? withAgentOperationLock
32
+ try {
33
+ const locked = await operationLock(
34
+ { agentDir: options.cwd, operation: 'stop', lease: options.operationLease },
35
+ async () => await runStop(options),
36
+ )
37
+ return locked.ok ? locked.value : { ok: false, reason: locked.reason }
38
+ } catch (error) {
39
+ return { ok: false, reason: error instanceof Error ? error.message : String(error) }
40
+ }
41
+ }
42
+
43
+ async function runStop({
28
44
  cwd,
29
45
  exec = defaultDockerExec,
30
46
  archiveLogs = archiveContainerLogs,
@@ -527,7 +527,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
527
527
  ? await opts.restartPreflight({ containerName: req.containerName, cwd, build: req.build })
528
528
  : null
529
529
  if (preflight) return preflight
530
- const ack = supervisor.scheduleRestart({
530
+ const ack = await supervisor.scheduleRestart({
531
531
  containerName: req.containerName,
532
532
  cwd,
533
533
  build: req.build,
@@ -8,6 +8,7 @@ import { isWindows } from '@/shared'
8
8
  const SOCKET_FILE = 'hostd.sock'
9
9
  const REGISTRATIONS_DIR = 'registrations'
10
10
  const KEYS_DIR = 'keys'
11
+ const AGENT_OPERATION_LOCKS_DIR = 'agent-locks'
11
12
 
12
13
  // Defense-in-depth: containerName arrives from RPC payloads (some of which
13
14
  // originate inside the container). Docker already forbids slashes and most
@@ -67,6 +68,17 @@ export function registrationsDir(): string {
67
68
  return join(runDir(), REGISTRATIONS_DIR)
68
69
  }
69
70
 
71
+ export function agentOperationLocksDir(): string {
72
+ return join(runDir(), AGENT_OPERATION_LOCKS_DIR)
73
+ }
74
+
75
+ export function agentOperationLockPath(containerName: string): string {
76
+ if (!SAFE_NAME.test(containerName)) {
77
+ throw new Error(`invalid container name for agent operation lock: ${JSON.stringify(containerName)}`)
78
+ }
79
+ return join(agentOperationLocksDir(), `${containerName}.lock`)
80
+ }
81
+
70
82
  export function keysDir(): string {
71
83
  return join(homeRoot(), KEYS_DIR)
72
84
  }
@@ -101,3 +113,8 @@ export async function ensureDirs(): Promise<void> {
101
113
  await chmod(keysDir(), 0o700).catch(() => {})
102
114
  await chmod(modelsDir(), 0o700).catch(() => {})
103
115
  }
116
+
117
+ export async function ensureAgentOperationLocksDir(): Promise<void> {
118
+ await mkdir(agentOperationLocksDir(), { recursive: true })
119
+ await chmod(agentOperationLocksDir(), 0o700).catch(() => {})
120
+ }
@@ -1,4 +1,9 @@
1
1
  import type { CurrentHostDaemon } from '@/container'
2
+ import {
3
+ type AgentOperationLease,
4
+ reserveAgentOperationLock,
5
+ type ReserveAgentOperationLock,
6
+ } from '@/container/agent-operation-lock'
2
7
 
3
8
  import type { Response } from './protocol'
4
9
 
@@ -13,6 +18,7 @@ export type SupervisorRestart = (input: {
13
18
  // Injected by the daemon so the restart registers the container in-process
14
19
  // instead of over the socket — see CurrentHostDaemon docs in src/container.
15
20
  currentHostDaemon?: CurrentHostDaemon
21
+ operationLease?: AgentOperationLease
16
22
  }) => Promise<{ ok: true } | { ok: false; reason: string }>
17
23
 
18
24
  export type SupervisorOptions = {
@@ -30,34 +36,43 @@ export type Supervisor = {
30
36
  cwd: string
31
37
  build?: boolean
32
38
  currentHostDaemon?: CurrentHostDaemon
33
- }) => Response
39
+ }) => Promise<Response>
34
40
  }
35
41
 
36
42
  export type SupervisorBuildOptions = {
37
43
  restart: SupervisorRestart
38
44
  onLog: (event: SupervisorLogEvent) => void
39
45
  isStopped: () => boolean
46
+ reserveLock?: ReserveAgentOperationLock
40
47
  }
41
48
 
42
- // The daemon ACKs the agent immediately so the container can exit cleanly,
43
- // then runs stop+start in the background. If we ran them inline the agent's
44
- // own RPC connection would die when its container stopped — guaranteed to
45
- // race because `docker stop` is the very thing we're about to do. Errors are
46
- // surfaced via the log channel; there is no connected client to receive them.
47
- export function buildSupervisor({ restart, onLog, isStopped }: SupervisorBuildOptions): Supervisor {
49
+ // The daemon reserves the lifecycle lock before ACKing, then runs stop+start in
50
+ // the background so the agent's RPC connection can close before `docker stop`.
51
+ // The requester exits about 500 ms after the ACK, so a lock failure discovered
52
+ // later would be invisible; rejecting the ACK lets the agent surface and retry
53
+ // the error. Failures after scheduling are still surfaced through the log channel.
54
+ export function buildSupervisor({
55
+ restart,
56
+ onLog,
57
+ isStopped,
58
+ reserveLock = reserveAgentOperationLock,
59
+ }: SupervisorBuildOptions): Supervisor {
48
60
  return {
49
- scheduleRestart: ({ containerName, cwd, build = false, currentHostDaemon }): Response => {
61
+ scheduleRestart: async ({ containerName, cwd, build = false, currentHostDaemon }): Promise<Response> => {
50
62
  if (isStopped()) return { ok: false, reason: 'daemon stopping' }
63
+ const reservation = await reserveLock({ agentDir: cwd, operation: 'restart' })
64
+ if (!reservation.ok) return { ok: false, reason: reservation.reason }
51
65
  onLog({ kind: 'restart-scheduled', containerName, build })
52
- void runRestart()
66
+ void runRestart(reservation.lease, reservation.release)
53
67
  return { ok: true }
54
68
 
55
- async function runRestart(): Promise<void> {
69
+ async function runRestart(lease: AgentOperationLease, release: () => Promise<void>): Promise<void> {
56
70
  try {
57
71
  const result = await restart({
58
72
  containerName,
59
73
  cwd,
60
74
  build,
75
+ operationLease: lease,
61
76
  ...(currentHostDaemon ? { currentHostDaemon } : {}),
62
77
  })
63
78
  if (result.ok) onLog({ kind: 'restart-completed', containerName })
@@ -68,6 +83,8 @@ export function buildSupervisor({ restart, onLog, isStopped }: SupervisorBuildOp
68
83
  containerName,
69
84
  reason: error instanceof Error ? error.message : String(error),
70
85
  })
86
+ } finally {
87
+ await release().catch(() => {})
71
88
  }
72
89
  }
73
90
  },
@@ -1,3 +1,4 @@
1
+ import { CONTAINER_AGENT_MESSENGER_CONFIG_DIR } from '@/agent-messenger/config-dir'
1
2
  import { ASKPASS_SCRIPT, TYPECLAW_GIT_ASKPASS_PATH } from '@/bundled-plugins/github-cli-auth/git-askpass'
2
3
  import { validateDockerfileAppendLine } from '@/config/config'
3
4
  import type { DockerfileConfig, DockerfileFeatureToggle } from '@/config/config'
@@ -1380,11 +1381,11 @@ ENV NODE_ENV=production
1380
1381
  # Persist first-party GWS config without changing global XDG/git config lookup.
1381
1382
  ENV GWS_CONFIG_HOME=/agent/workspace/.config/gws
1382
1383
 
1383
- # Keep agent-messenger's fallback config dir inside workspace/ for any future
1384
- # SDK fallback paths. TypeClaw's KakaoTalk adapter does not write there:
1385
- # credentials live in secrets.json#channels.kakaotalk and container writes go
1386
- # through hostd's secrets-patch RPC.
1387
- ENV AGENT_MESSENGER_CONFIG_DIR=/agent/workspace/.agent-messenger
1384
+ # Keep agent-messenger runtime storage inside workspace/. LINE writes E2EE key
1385
+ # material under line-storage/ here; Instagram stores its live session here.
1386
+ # KakaoTalk credentials instead live in secrets.json#channels.kakaotalk and
1387
+ # container writes go through hostd's secrets-patch RPC.
1388
+ ENV AGENT_MESSENGER_CONFIG_DIR=${CONTAINER_AGENT_MESSENGER_CONFIG_DIR}
1388
1389
 
1389
1390
  ${customLines}ENTRYPOINT ["${TYPECLAW_ENTRYPOINT_PATH}"]
1390
1391
  CMD ["run"]