typeclaw 0.48.9 → 0.48.11
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.
- package/package.json +1 -1
- package/scripts/apparmor/typeclaw-bwrap +43 -0
- package/scripts/verify-apparmor-signals.sh +161 -0
- package/scripts/verify-mask-fd-sandbox.sh +176 -0
- package/src/agent/index.ts +9 -0
- package/src/agent/live-subagents.ts +98 -0
- package/src/agent/session-origin.ts +5 -1
- package/src/agent/tools/channel-reply.ts +30 -2
- package/src/agent/tools/channel-send.ts +19 -2
- package/src/agent/tools/spawn-subagent.ts +37 -1
- package/src/agent/tools/subagent-cancel.ts +14 -2
- package/src/bundled-plugins/backup/README.md +4 -3
- package/src/bundled-plugins/backup/runner.ts +125 -32
- package/src/bundled-plugins/github-cli-auth/gh-command.ts +106 -9
- package/src/bundled-plugins/github-cli-auth/git-command.ts +94 -9
- package/src/bundled-plugins/github-cli-auth/index.ts +45 -5
- package/src/channels/adapters/github/inbound.ts +210 -27
- package/src/channels/adapters/github/index.ts +16 -18
- package/src/channels/adapters/github/reconcile-cooldown-store.ts +15 -1
- package/src/channels/adapters/github/recover-failed-deliveries.ts +7 -2
- package/src/channels/adapters/github/review-submitter.ts +28 -0
- package/src/channels/adapters/instagram-continuity-store.ts +141 -0
- package/src/channels/adapters/instagram.ts +149 -4
- package/src/channels/github-repo.ts +11 -0
- package/src/channels/github-review-verdict-coordinator.ts +179 -29
- package/src/channels/github-verdict-activity.ts +12 -6
- package/src/channels/manager.ts +10 -0
- package/src/channels/persistence.ts +28 -2
- package/src/channels/router.ts +371 -33
- package/src/channels/schema.ts +18 -0
- package/src/channels/types.ts +11 -0
- package/src/cli/compose.ts +7 -4
- package/src/compose/restart.ts +26 -16
- package/src/compose/start.ts +24 -14
- package/src/config/config.ts +9 -1
- package/src/container/restart.ts +4 -0
- package/src/container/shared.ts +30 -3
- package/src/container/start.ts +137 -56
- package/src/doctor/apparmor.ts +101 -0
- package/src/doctor/checks.ts +2 -0
- package/src/hostd/daemon.ts +40 -17
- package/src/init/dockerfile.ts +7 -6
- package/src/init/windows-dev-link.ts +34 -2
- package/src/run/channel-session-factory.ts +35 -15
- package/src/run/index.ts +1 -0
- package/src/sandbox/availability.ts +7 -5
- package/src/skills/typeclaw-channel-github/SKILL.md +4 -2
- package/typeclaw.schema.json +7 -0
package/src/container/start.ts
CHANGED
|
@@ -119,19 +119,24 @@ export type HostDaemonRegisterPayload = {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
// Injected only on the daemon-owned restart path (reuseCurrentHostDaemon).
|
|
122
|
-
// The daemon
|
|
123
|
-
// so
|
|
124
|
-
// can time out under IPC congestion and silently drop the
|
|
125
|
-
//
|
|
122
|
+
// The daemon updates the container registration in-process and reports its own
|
|
123
|
+
// HTTP port, so restart lifecycle changes skip socket self-RPCs — those
|
|
124
|
+
// round-trips can time out under IPC congestion and silently drop the
|
|
125
|
+
// TYPECLAW_HOSTD_* env triple or leave stale broker state behind.
|
|
126
126
|
export type CurrentHostDaemon = {
|
|
127
127
|
httpPort: number
|
|
128
128
|
register: (payload: HostDaemonRegisterPayload) => Promise<{ ok: true } | { ok: false; reason: string }>
|
|
129
|
+
deregister: (containerName: string) => Promise<void>
|
|
129
130
|
}
|
|
130
131
|
|
|
131
132
|
export type StartOptions = {
|
|
132
133
|
cwd: string
|
|
133
134
|
preferredHostPort: number
|
|
134
135
|
forceBuild?: boolean
|
|
136
|
+
// A parent live renderer must be the terminal's only cursor owner. Compose
|
|
137
|
+
// disables process output so child progress and warnings cannot corrupt it.
|
|
138
|
+
streamOutput?: boolean
|
|
139
|
+
onWarning?: (warning: string) => void
|
|
135
140
|
exec?: DockerExec
|
|
136
141
|
// Test seam: allows tests to inject a deterministic port allocator. In
|
|
137
142
|
// production we go through the real kernel via `findFreePort`.
|
|
@@ -140,9 +145,10 @@ export type StartOptions = {
|
|
|
140
145
|
// Hostd's supervisor restart callback already runs inside the daemon process.
|
|
141
146
|
// Reusing that daemon avoids a self-shutdown when disk source has drifted.
|
|
142
147
|
reuseCurrentHostDaemon?: boolean
|
|
143
|
-
// Set by hostd's supervisor restart wrapper. When present, registration
|
|
144
|
-
// through the daemon in-process (no socket round-trips),
|
|
145
|
-
//
|
|
148
|
+
// Set by hostd's supervisor restart wrapper. When present, registration
|
|
149
|
+
// lifecycle changes go through the daemon in-process (no socket round-trips),
|
|
150
|
+
// so known-good control values and rollback survive IPC congestion.
|
|
151
|
+
// See type docs.
|
|
146
152
|
currentHostDaemon?: CurrentHostDaemon
|
|
147
153
|
ensureDeps?: (cwd: string, opts?: { force?: boolean }) => Promise<EnsureDepsResult>
|
|
148
154
|
// Test seam for host embedding model provisioning. Production callers use
|
|
@@ -242,6 +248,8 @@ async function runStart({
|
|
|
242
248
|
cwd,
|
|
243
249
|
preferredHostPort,
|
|
244
250
|
forceBuild = false,
|
|
251
|
+
streamOutput = true,
|
|
252
|
+
onWarning,
|
|
245
253
|
exec = defaultDockerExec,
|
|
246
254
|
allocatePort = findFreePort,
|
|
247
255
|
cliEntry,
|
|
@@ -327,7 +335,11 @@ async function runStart({
|
|
|
327
335
|
}
|
|
328
336
|
|
|
329
337
|
const githubCliDeniedRoots = resolveGithubCliDeniedRoots(cwd, await loadTypeclawConfig(cwd))
|
|
330
|
-
await refreshGithubCliStore(cwd, githubCliDeniedRoots, provisionGithubCliStoreForAgent)
|
|
338
|
+
const githubCliWarning = await refreshGithubCliStore(cwd, githubCliDeniedRoots, provisionGithubCliStoreForAgent)
|
|
339
|
+
if (githubCliWarning !== null) {
|
|
340
|
+
if (streamOutput) process.stderr.write(githubCliWarning)
|
|
341
|
+
else onWarning?.(githubCliWarning.trim())
|
|
342
|
+
}
|
|
331
343
|
|
|
332
344
|
if (agentMessengerPolicy.migrate) {
|
|
333
345
|
const agentMessengerMigration = await migrateAgentMessengerConfigDir(cwd)
|
|
@@ -551,16 +563,23 @@ async function runStart({
|
|
|
551
563
|
|
|
552
564
|
let built = false
|
|
553
565
|
if (plan.needsBuild) {
|
|
554
|
-
const
|
|
566
|
+
const buildResult = await runImageBuild({
|
|
555
567
|
exec,
|
|
556
568
|
cwd,
|
|
557
569
|
imageTag: plan.imageTag,
|
|
558
570
|
buildContext: plan.buildContext,
|
|
559
571
|
hasBuildx,
|
|
572
|
+
streamOutput,
|
|
560
573
|
})
|
|
561
|
-
if (!
|
|
574
|
+
if (!buildResult.ok) {
|
|
562
575
|
await cleanupHostDaemonRegistration(containerName, hostd)
|
|
563
|
-
|
|
576
|
+
const retryDetail = buildResult.credentialHelperRetried
|
|
577
|
+
? ' The build was also retried without the configured Docker credential helper.'
|
|
578
|
+
: ''
|
|
579
|
+
const detail = streamOutput
|
|
580
|
+
? ''
|
|
581
|
+
: ` (exit code ${buildResult.exitCode}). Run \`typeclaw start --build\` from this agent directory to view the full build output.${retryDetail}`
|
|
582
|
+
return { ok: false, reason: `docker build failed${detail}` }
|
|
564
583
|
}
|
|
565
584
|
built = true
|
|
566
585
|
}
|
|
@@ -575,6 +594,44 @@ async function runStart({
|
|
|
575
594
|
}
|
|
576
595
|
}
|
|
577
596
|
|
|
597
|
+
// The build and model provisioning above can outlive a hostd registration.
|
|
598
|
+
// Refresh both the registry entry and the launch plan at the run boundary:
|
|
599
|
+
// a broker restart may have invalidated the initial control tokens while
|
|
600
|
+
// those pre-run steps were in flight. Keep that early registration because
|
|
601
|
+
// it remains the cleanup target for build/model failures.
|
|
602
|
+
const initialHostd = hostd
|
|
603
|
+
try {
|
|
604
|
+
if (cliEntry) {
|
|
605
|
+
const refreshedHostd = await registerWithDaemon({
|
|
606
|
+
cwd,
|
|
607
|
+
containerName,
|
|
608
|
+
cliEntry,
|
|
609
|
+
hostPort,
|
|
610
|
+
reuseCurrentHostDaemon,
|
|
611
|
+
currentHostDaemon,
|
|
612
|
+
})
|
|
613
|
+
if (refreshedHostd.state !== 'registered') {
|
|
614
|
+
await cleanupHostDaemonRegistration(containerName, initialHostd)
|
|
615
|
+
}
|
|
616
|
+
hostd = refreshedHostd
|
|
617
|
+
hostdControl = refreshedHostd.state === 'registered' ? refreshedHostd.control : undefined
|
|
618
|
+
plan = await planStart({
|
|
619
|
+
cwd,
|
|
620
|
+
hostPort,
|
|
621
|
+
imageExists: true,
|
|
622
|
+
forceBuild: false,
|
|
623
|
+
hostdControl,
|
|
624
|
+
publishHost,
|
|
625
|
+
tuiToken,
|
|
626
|
+
platform,
|
|
627
|
+
hostIdentity,
|
|
628
|
+
})
|
|
629
|
+
}
|
|
630
|
+
} catch (error) {
|
|
631
|
+
await cleanupHostDaemonRegistration(containerName, hostd.state === 'registered' ? hostd : initialHostd)
|
|
632
|
+
return { ok: false, reason: error instanceof Error ? error.message : String(error) }
|
|
633
|
+
}
|
|
634
|
+
|
|
578
635
|
let run = await execRunWithConflictRetry(exec, plan.runArgs, cwd, containerName, archiveBeforeRemove)
|
|
579
636
|
|
|
580
637
|
// TOCTOU: another process may have grabbed the port between our probe and
|
|
@@ -616,7 +673,8 @@ async function runStart({
|
|
|
616
673
|
}
|
|
617
674
|
hostPort = await allocatePort(0)
|
|
618
675
|
if (cliEntry) {
|
|
619
|
-
|
|
676
|
+
const previousHostd = hostd
|
|
677
|
+
const retriedHostd = await registerWithDaemon({
|
|
620
678
|
cwd,
|
|
621
679
|
containerName,
|
|
622
680
|
cliEntry,
|
|
@@ -624,7 +682,11 @@ async function runStart({
|
|
|
624
682
|
reuseCurrentHostDaemon,
|
|
625
683
|
currentHostDaemon,
|
|
626
684
|
})
|
|
627
|
-
|
|
685
|
+
if (retriedHostd.state !== 'registered') {
|
|
686
|
+
await cleanupHostDaemonRegistration(containerName, previousHostd)
|
|
687
|
+
}
|
|
688
|
+
hostd = retriedHostd
|
|
689
|
+
hostdControl = retriedHostd.state === 'registered' ? retriedHostd.control : undefined
|
|
628
690
|
}
|
|
629
691
|
plan = await planStart({
|
|
630
692
|
cwd,
|
|
@@ -674,19 +736,19 @@ async function refreshGithubCliStore(
|
|
|
674
736
|
agentDir: string,
|
|
675
737
|
deniedRoots: readonly string[],
|
|
676
738
|
provision: (options: ProvisionGithubCliStoreOptions) => GithubCliProvisionResult | Promise<GithubCliProvisionResult>,
|
|
677
|
-
): Promise<
|
|
739
|
+
): Promise<string | null> {
|
|
678
740
|
let refreshed = false
|
|
679
741
|
try {
|
|
680
742
|
refreshed = (await provision({ agentDir, deniedRoots })).ok
|
|
681
743
|
} catch {
|
|
682
744
|
refreshed = false
|
|
683
745
|
}
|
|
684
|
-
if (refreshed) return
|
|
746
|
+
if (refreshed) return null
|
|
685
747
|
|
|
686
|
-
|
|
748
|
+
return (
|
|
687
749
|
'typeclaw: warning: Could not refresh GitHub CLI credentials from the host. ' +
|
|
688
|
-
|
|
689
|
-
|
|
750
|
+
'Keeping the previously persisted credential store. Run `gh auth login --hostname github.com` on the host, ' +
|
|
751
|
+
'then restart TypeClaw.\n'
|
|
690
752
|
)
|
|
691
753
|
}
|
|
692
754
|
|
|
@@ -808,21 +870,28 @@ export async function planStart({
|
|
|
808
870
|
// Operators must `typeclaw restart` (removes and recreates), not
|
|
809
871
|
// `docker restart` (reuses the old HostConfig).
|
|
810
872
|
//
|
|
811
|
-
//
|
|
812
|
-
//
|
|
813
|
-
//
|
|
814
|
-
//
|
|
815
|
-
//
|
|
816
|
-
//
|
|
817
|
-
//
|
|
818
|
-
//
|
|
819
|
-
//
|
|
820
|
-
//
|
|
821
|
-
//
|
|
822
|
-
//
|
|
823
|
-
//
|
|
824
|
-
//
|
|
825
|
-
//
|
|
873
|
+
// These two security options are a load-bearing pair for `bwrap(1)`
|
|
874
|
+
// (installed in baseline; see BASELINE_APT_PACKAGES in
|
|
875
|
+
// src/init/dockerfile.ts). Docker's default seccomp profile rejects the
|
|
876
|
+
// namespace syscalls, while moby's docker-default AppArmor profile has an
|
|
877
|
+
// explicit `deny mount,` rule that rejects bwrap's opening
|
|
878
|
+
// mount(NULL, "/", MS_SLAVE|MS_REC). Selecting `apparmor=unconfined` removes
|
|
879
|
+
// that mount denial; it selects a profile, it does NOT grant a capability.
|
|
880
|
+
// On stock Ubuntu 23.10+ the host's `unprivileged_userns` catch-all then
|
|
881
|
+
// denies bwrap's /proc/<pid>/uid_map write when
|
|
882
|
+
// kernel.apparmor_restrict_unprivileged_userns=1. The complete path there is
|
|
883
|
+
// the shipped host-loaded `typeclaw-bwrap` profile plus
|
|
884
|
+
// sandbox.apparmorProfile="typeclaw-bwrap"; doctor reports the exact install
|
|
885
|
+
// and restart commands.
|
|
886
|
+
//
|
|
887
|
+
// CAP_SYS_ADMIN is not an alternative: the entrypoint drops to the host's
|
|
888
|
+
// non-root UID with an empty capability set, so Docker's --cap-add grant is
|
|
889
|
+
// void in the production process. These outer defaults are appropriate
|
|
890
|
+
// because the container is a single-tenant trust boundary — the user trusts
|
|
891
|
+
// everything inside it equally, and the .env and agent folder are already
|
|
892
|
+
// mounted in. Seccomp/AppArmor's multi-tenant protections are therefore not
|
|
893
|
+
// load-bearing for this threat model; the per-tool bwrap sandbox is the real
|
|
894
|
+
// boundary against prompt-injected commands. See docs/internals/sandbox.mdx.
|
|
826
895
|
const runArgs = [
|
|
827
896
|
'run',
|
|
828
897
|
'-d',
|
|
@@ -832,6 +901,8 @@ export async function planStart({
|
|
|
832
901
|
'--shm-size=2g',
|
|
833
902
|
'--security-opt',
|
|
834
903
|
'seccomp=unconfined',
|
|
904
|
+
'--security-opt',
|
|
905
|
+
`apparmor=${cfg.sandbox.apparmorProfile}`,
|
|
835
906
|
'-p',
|
|
836
907
|
`${publishHost}:${hostPort}:${CONTAINER_PORT}`,
|
|
837
908
|
]
|
|
@@ -857,17 +928,17 @@ export async function planStart({
|
|
|
857
928
|
// sandbox.realProc (default FALSE) opts into the per-tool bwrap sandbox's
|
|
858
929
|
// 'real-proc' strategy (src/sandbox/build.ts), which prefixes the sandbox with
|
|
859
930
|
// `unshare --pid --fork --mount --mount-proc`. Mounting a fresh procfs for the
|
|
860
|
-
// new PID namespace needs real CAP_SYS_ADMIN
|
|
861
|
-
//
|
|
862
|
-
//
|
|
863
|
-
//
|
|
931
|
+
// new PID namespace needs real CAP_SYS_ADMIN, so the Docker grant remains
|
|
932
|
+
// gated on the flag and is OFF by default. The entrypoint's production
|
|
933
|
+
// non-root UID clears that cap before the agent starts today, and the runtime
|
|
934
|
+
// capability probe consequently falls back to proc-bind. External-package
|
|
935
|
+
// execution (`bunx agent-*`)
|
|
864
936
|
// no longer needs it — the default 'proc-bind' strategy gives the runner real
|
|
865
937
|
// /proc without any outer capability (see docs/internals/sandbox.mdx). Setting
|
|
866
|
-
// realProc:true
|
|
867
|
-
//
|
|
868
|
-
//
|
|
869
|
-
//
|
|
870
|
-
// is necessary-but-not-sufficient by design. Placed before the image tag (like
|
|
938
|
+
// realProc:true requests the stricter PID-isolation posture. The
|
|
939
|
+
// container-side strategy resolution probes whether the mount actually works
|
|
940
|
+
// (canMountRealProc) and falls back to proc-bind when the cap is absent or the
|
|
941
|
+
// runtime rejects the mount (e.g. OrbStack). Placed before the image tag (like
|
|
871
942
|
// --cap-add=NET_ADMIN) so docker applies it at run time.
|
|
872
943
|
if (cfg.sandbox.realProc) {
|
|
873
944
|
runArgs.push('--cap-add=SYS_ADMIN')
|
|
@@ -1042,8 +1113,9 @@ async function runImageBuild(args: {
|
|
|
1042
1113
|
imageTag: string
|
|
1043
1114
|
buildContext: string
|
|
1044
1115
|
hasBuildx: boolean
|
|
1045
|
-
|
|
1046
|
-
|
|
1116
|
+
streamOutput: boolean
|
|
1117
|
+
}): Promise<{ ok: true } | { ok: false; exitCode: number; credentialHelperRetried: boolean }> {
|
|
1118
|
+
const { exec, cwd, imageTag, buildContext, hasBuildx, streamOutput } = args
|
|
1047
1119
|
const buildArgv = (frontend: 'buildx' | 'legacy'): string[] =>
|
|
1048
1120
|
frontend === 'buildx'
|
|
1049
1121
|
? ['buildx', 'build', '--load', '-t', imageTag, buildContext]
|
|
@@ -1051,12 +1123,17 @@ async function runImageBuild(args: {
|
|
|
1051
1123
|
|
|
1052
1124
|
let sanitizedConfig: SanitizedDockerConfig | null = null
|
|
1053
1125
|
const attempt = async (frontend: 'buildx' | 'legacy'): Promise<DockerExecResult> =>
|
|
1054
|
-
exec(buildArgv(frontend), {
|
|
1126
|
+
exec(buildArgv(frontend), {
|
|
1127
|
+
cwd,
|
|
1128
|
+
...(streamOutput ? { inheritStdio: true, captureStderr: true } : {}),
|
|
1129
|
+
...(streamOutput ? {} : { captureStdout: false, maxCapturedStderrBytes: 32 * 1024 }),
|
|
1130
|
+
env: sanitizedConfig?.env,
|
|
1131
|
+
})
|
|
1055
1132
|
|
|
1056
1133
|
try {
|
|
1057
1134
|
let frontend: 'buildx' | 'legacy' = hasBuildx ? 'buildx' : 'legacy'
|
|
1058
1135
|
let result = await attempt(frontend)
|
|
1059
|
-
if (result.exitCode === 0) return true
|
|
1136
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1060
1137
|
|
|
1061
1138
|
// Same-frontend retry: a broken credential helper aborts the pull before
|
|
1062
1139
|
// the builder ever matters, so strip it and retry the identical build.
|
|
@@ -1064,7 +1141,7 @@ async function runImageBuild(args: {
|
|
|
1064
1141
|
sanitizedConfig = await createSanitizedDockerConfig()
|
|
1065
1142
|
if (sanitizedConfig) {
|
|
1066
1143
|
result = await attempt(frontend)
|
|
1067
|
-
if (result.exitCode === 0) return true
|
|
1144
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1068
1145
|
}
|
|
1069
1146
|
}
|
|
1070
1147
|
|
|
@@ -1075,23 +1152,23 @@ async function runImageBuild(args: {
|
|
|
1075
1152
|
await refreshDockerfile(cwd, { buildKit: false })
|
|
1076
1153
|
frontend = 'legacy'
|
|
1077
1154
|
result = await attempt(frontend)
|
|
1078
|
-
if (result.exitCode === 0) return true
|
|
1155
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1079
1156
|
if (sanitizedConfig === null && isMissingDockerCredentialHelper(result.stderr)) {
|
|
1080
1157
|
sanitizedConfig = await createSanitizedDockerConfig()
|
|
1081
1158
|
if (sanitizedConfig) {
|
|
1082
1159
|
result = await attempt(frontend)
|
|
1083
|
-
if (result.exitCode === 0) return true
|
|
1160
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1084
1161
|
}
|
|
1085
1162
|
}
|
|
1086
1163
|
}
|
|
1087
1164
|
|
|
1088
|
-
if (sanitizedConfig !== null) {
|
|
1089
|
-
|
|
1165
|
+
if (sanitizedConfig !== null && streamOutput) {
|
|
1166
|
+
const guidance =
|
|
1090
1167
|
'typeclaw: docker build still failed after retrying public image pulls without the configured ' +
|
|
1091
|
-
|
|
1092
|
-
)
|
|
1168
|
+
'credential helper. Your ~/.docker/config.json credsStore/credHelpers may be broken.'
|
|
1169
|
+
process.stderr.write(`${guidance}\n`)
|
|
1093
1170
|
}
|
|
1094
|
-
return false
|
|
1171
|
+
return { ok: false, exitCode: result.exitCode, credentialHelperRetried: sanitizedConfig !== null }
|
|
1095
1172
|
} finally {
|
|
1096
1173
|
await sanitizedConfig?.cleanup()
|
|
1097
1174
|
}
|
|
@@ -1448,6 +1525,7 @@ async function registerWithDaemon({
|
|
|
1448
1525
|
return {
|
|
1449
1526
|
state: 'registered',
|
|
1450
1527
|
control: { url: `http://${CONTAINER_HOSTD_HOST}:${currentHostDaemon.httpPort}`, token, brokerToken },
|
|
1528
|
+
deregister: (registeredContainerName) => currentHostDaemon.deregister(registeredContainerName),
|
|
1451
1529
|
}
|
|
1452
1530
|
}
|
|
1453
1531
|
|
|
@@ -1458,6 +1536,9 @@ async function registerWithDaemon({
|
|
|
1458
1536
|
return {
|
|
1459
1537
|
state: 'registered',
|
|
1460
1538
|
control: { url: `http://${CONTAINER_HOSTD_HOST}:${prepared.httpPort}`, token, brokerToken },
|
|
1539
|
+
deregister: async (registeredContainerName) => {
|
|
1540
|
+
await sendToDaemon({ kind: 'deregister', containerName: registeredContainerName })
|
|
1541
|
+
},
|
|
1461
1542
|
}
|
|
1462
1543
|
}
|
|
1463
1544
|
|
|
@@ -1492,7 +1573,7 @@ async function useCurrentHostDaemon(): Promise<{ ok: true; httpPort: number } |
|
|
|
1492
1573
|
}
|
|
1493
1574
|
|
|
1494
1575
|
type PreparedHostDaemonStatus =
|
|
1495
|
-
| { state: 'registered'; control: HostDaemonControl }
|
|
1576
|
+
| { state: 'registered'; control: HostDaemonControl; deregister: (containerName: string) => Promise<void> }
|
|
1496
1577
|
| { state: 'unavailable'; reason: string }
|
|
1497
1578
|
| { state: 'disabled' }
|
|
1498
1579
|
|
|
@@ -1503,7 +1584,7 @@ function stripHostDaemonControl(status: PreparedHostDaemonStatus): HostDaemonSta
|
|
|
1503
1584
|
|
|
1504
1585
|
async function cleanupHostDaemonRegistration(containerName: string, status: PreparedHostDaemonStatus): Promise<void> {
|
|
1505
1586
|
if (status.state !== 'registered') return
|
|
1506
|
-
await
|
|
1587
|
+
await status.deregister(containerName).catch(() => {})
|
|
1507
1588
|
}
|
|
1508
1589
|
|
|
1509
1590
|
// process.env.TZ is honored first because users who explicitly set it (e.g.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
import { loadConfigSync } from '@/config'
|
|
4
|
+
|
|
5
|
+
import type { CheckResult, DoctorCheck } from './types'
|
|
6
|
+
|
|
7
|
+
const APPARMOR_ENABLED_PATH = '/sys/module/apparmor/parameters/enabled'
|
|
8
|
+
const RESTRICT_UNPRIVILEGED_USERNS_PATH = '/proc/sys/kernel/apparmor_restrict_unprivileged_userns'
|
|
9
|
+
|
|
10
|
+
const SHIPPED_PROFILE = 'typeclaw-bwrap'
|
|
11
|
+
const DOCKER_DEFAULT_PROFILE = 'docker-default'
|
|
12
|
+
|
|
13
|
+
const INSTALL_SHIPPED_PROFILE_FIX =
|
|
14
|
+
'Install the shipped scripts/apparmor/typeclaw-bwrap profile as /etc/apparmor.d/typeclaw-bwrap, run `sudo apparmor_parser -r -W /etc/apparmor.d/typeclaw-bwrap`, set `sandbox.apparmorProfile` to `typeclaw-bwrap`, then run `typeclaw restart`.'
|
|
15
|
+
|
|
16
|
+
export type ApparmorSandboxDeps = {
|
|
17
|
+
readHostFile?: (path: string) => Promise<string | null>
|
|
18
|
+
loadApparmorProfile?: (cwd: string) => string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function apparmorSandbox(deps: ApparmorSandboxDeps = {}): DoctorCheck {
|
|
22
|
+
const readHostFile = deps.readHostFile ?? safeReadHostFile
|
|
23
|
+
const loadApparmorProfile = deps.loadApparmorProfile ?? ((cwd) => loadConfigSync(cwd).sandbox.apparmorProfile)
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
name: 'container.apparmor-bwrap',
|
|
27
|
+
category: 'container',
|
|
28
|
+
description: 'host AppArmor permits the per-tool bwrap sandbox',
|
|
29
|
+
applies: (ctx) => ctx.hasAgentFolder,
|
|
30
|
+
async run(ctx) {
|
|
31
|
+
const [enabled, restricted] = await Promise.all([
|
|
32
|
+
readHostFile(APPARMOR_ENABLED_PATH),
|
|
33
|
+
readHostFile(RESTRICT_UNPRIVILEGED_USERNS_PATH),
|
|
34
|
+
])
|
|
35
|
+
if (enabled?.trim() !== 'Y') {
|
|
36
|
+
return { status: 'ok', message: 'host AppArmor is not enabled; no container profile applies' }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let profile: string
|
|
40
|
+
try {
|
|
41
|
+
profile = loadApparmorProfile(ctx.cwd)
|
|
42
|
+
} catch {
|
|
43
|
+
return { status: 'ok', message: 'AppArmor profile check deferred until typeclaw.json is valid' }
|
|
44
|
+
}
|
|
45
|
+
return classifyProfile(profile, restricted?.trim() === '1')
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Profile FIRST, host sysctl second. docker-default's `deny mount,` rejects
|
|
51
|
+
// bwrap's opening mount(NULL, "/", MS_SLAVE|MS_REC) on any AppArmor-enabled
|
|
52
|
+
// host, so gating that verdict on kernel.apparmor_restrict_unprivileged_userns
|
|
53
|
+
// would report a dead bash surface as healthy. Only the `unconfined` verdict
|
|
54
|
+
// actually depends on the sysctl.
|
|
55
|
+
function classifyProfile(profile: string, restrictedUserns: boolean): CheckResult {
|
|
56
|
+
if (profile === SHIPPED_PROFILE) {
|
|
57
|
+
return { status: 'ok', message: `sandbox uses the shipped ${SHIPPED_PROFILE} AppArmor profile` }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// `error`, not `warning`: an unavailable sandbox makes every model bash call
|
|
61
|
+
// throw SandboxUnavailableError (src/sandbox/availability.ts) — the surface is
|
|
62
|
+
// dead, not degraded.
|
|
63
|
+
if (profile === DOCKER_DEFAULT_PROFILE) {
|
|
64
|
+
return {
|
|
65
|
+
status: 'error',
|
|
66
|
+
message: `AppArmor profile ${DOCKER_DEFAULT_PROFILE} denies the mount that sandboxed bash needs, so every bash call fails`,
|
|
67
|
+
fix: { description: INSTALL_SHIPPED_PROFILE_FIX },
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (profile === 'unconfined') {
|
|
72
|
+
if (!restrictedUserns) {
|
|
73
|
+
return { status: 'ok', message: 'host AppArmor does not require a custom bwrap profile' }
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
status: 'error',
|
|
77
|
+
message:
|
|
78
|
+
'stock Ubuntu AppArmor blocks the user namespace required by sandboxed bash when sandbox.apparmorProfile is unconfined',
|
|
79
|
+
fix: { description: INSTALL_SHIPPED_PROFILE_FIX },
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// A deliberate operator-authored profile. Doctor cannot read
|
|
84
|
+
// /sys/kernel/security/apparmor/profiles unprivileged, so it can neither
|
|
85
|
+
// confirm nor refute this one — say so rather than assert it works.
|
|
86
|
+
return {
|
|
87
|
+
status: 'info',
|
|
88
|
+
message: `sandbox uses custom AppArmor profile ${profile}; typeclaw cannot verify it permits userns, mount, and signal delivery`,
|
|
89
|
+
details: [
|
|
90
|
+
`Compare it against the shipped ${SHIPPED_PROFILE} profile, and run scripts/verify-apparmor-signals.sh against this host to confirm cancellation and container stop/restart still work.`,
|
|
91
|
+
],
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function safeReadHostFile(path: string): Promise<string | null> {
|
|
96
|
+
try {
|
|
97
|
+
return await readFile(path, 'utf8')
|
|
98
|
+
} catch {
|
|
99
|
+
return null
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/doctor/checks.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { detectMissingDeps } from '@/init/ensure-deps'
|
|
|
22
22
|
import { buildGitignore, GITIGNORE_FILE } from '@/init/gitignore'
|
|
23
23
|
import { detectWsl, isWindows, isWindowsDriveMount, type WslInfo } from '@/shared'
|
|
24
24
|
|
|
25
|
+
import { apparmorSandbox } from './apparmor'
|
|
25
26
|
import { buildChannelChecks } from './channel-checks'
|
|
26
27
|
import { agentFileOwnership, type FileOwnershipDeps } from './file-ownership'
|
|
27
28
|
import { buildOperationalIncidentChecks } from './operational-incidents'
|
|
@@ -40,6 +41,7 @@ export function buildStaticChecks(opts: { dockerExec?: DockerExec } & FileOwners
|
|
|
40
41
|
agentFolderGitRepo(),
|
|
41
42
|
agentFileOwnership(opts),
|
|
42
43
|
configValid(),
|
|
44
|
+
apparmorSandbox(),
|
|
43
45
|
hostdHomeWritable(),
|
|
44
46
|
wslDriveMount(),
|
|
45
47
|
windowsSecretPerms(),
|
package/src/hostd/daemon.ts
CHANGED
|
@@ -323,6 +323,8 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
323
323
|
const cwds = new Map<string, string>()
|
|
324
324
|
const restartTokens = new Map<string, string>()
|
|
325
325
|
const brokerTokens = new Map<string, string>()
|
|
326
|
+
const registrationGenerations = new Map<string, number>()
|
|
327
|
+
let nextRegistrationGeneration = 0
|
|
326
328
|
const perContainerSerial = new Map<string, Promise<unknown>>()
|
|
327
329
|
const gcMisses = new Map<string, number>()
|
|
328
330
|
let stopped = false
|
|
@@ -398,6 +400,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
398
400
|
brokerTokens.delete(containerName)
|
|
399
401
|
cwds.delete(containerName)
|
|
400
402
|
restartTokens.delete(containerName)
|
|
403
|
+
registrationGenerations.delete(containerName)
|
|
401
404
|
gcMisses.delete(containerName)
|
|
402
405
|
if (opts.portbroker)
|
|
403
406
|
await withCleanupTimeout(opts.portbroker.stop(containerName, 'fatal-auth'), cleanupStepTimeoutMs)
|
|
@@ -410,6 +413,8 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
410
413
|
|
|
411
414
|
const applyRegistration = async (payload: RegisterPayload): Promise<void> => {
|
|
412
415
|
const alreadyRegistered = cwds.has(payload.containerName)
|
|
416
|
+
registrationGenerations.set(payload.containerName, ++nextRegistrationGeneration)
|
|
417
|
+
gcMisses.delete(payload.containerName)
|
|
413
418
|
cwds.set(payload.containerName, payload.cwd)
|
|
414
419
|
if (payload.restartToken) restartTokens.set(payload.containerName, payload.restartToken)
|
|
415
420
|
else restartTokens.delete(payload.containerName)
|
|
@@ -471,6 +476,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
471
476
|
const hadCwd = cwds.delete(req.containerName)
|
|
472
477
|
restartTokens.delete(req.containerName)
|
|
473
478
|
brokerTokens.delete(req.containerName)
|
|
479
|
+
registrationGenerations.delete(req.containerName)
|
|
474
480
|
gcMisses.delete(req.containerName)
|
|
475
481
|
if (opts.portbroker)
|
|
476
482
|
await withCleanupTimeout(opts.portbroker.stop(req.containerName, 'deregistered'), cleanupStepTimeoutMs)
|
|
@@ -500,9 +506,9 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
500
506
|
return { ok: true, result }
|
|
501
507
|
}
|
|
502
508
|
|
|
503
|
-
// Lets the supervisor's restart
|
|
504
|
-
// over the socket.
|
|
505
|
-
// invariant the socket dispatcher
|
|
509
|
+
// Lets the supervisor's restart update child registration lifecycle
|
|
510
|
+
// in-process instead of over the socket. Both callbacks await restore for
|
|
511
|
+
// the same no-mutation-before-restore invariant as the socket dispatcher.
|
|
506
512
|
const registerCurrentChild = async (
|
|
507
513
|
payload: HostDaemonRegisterPayload,
|
|
508
514
|
): Promise<{ ok: true } | { ok: false; reason: string }> => {
|
|
@@ -511,6 +517,11 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
511
517
|
return reply.ok ? { ok: true } : { ok: false, reason: reply.reason }
|
|
512
518
|
}
|
|
513
519
|
|
|
520
|
+
const deregisterCurrentChild = async (containerName: string): Promise<void> => {
|
|
521
|
+
await awaitRestored()
|
|
522
|
+
await handleDeregister({ containerName })
|
|
523
|
+
}
|
|
524
|
+
|
|
514
525
|
// Auth: only restart containers that registered with this daemon. The
|
|
515
526
|
// socket is 0o600 + UID-bound, but inside a container any process that
|
|
516
527
|
// reaches the mounted socket could otherwise restart any peer container on
|
|
@@ -531,7 +542,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
531
542
|
containerName: req.containerName,
|
|
532
543
|
cwd,
|
|
533
544
|
build: req.build,
|
|
534
|
-
currentHostDaemon: { httpPort, register: registerCurrentChild },
|
|
545
|
+
currentHostDaemon: { httpPort, register: registerCurrentChild, deregister: deregisterCurrentChild },
|
|
535
546
|
})
|
|
536
547
|
if (!ack.ok) return ack
|
|
537
548
|
const result: RestartResult = { containerName: req.containerName, scheduled: true }
|
|
@@ -790,7 +801,11 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
790
801
|
}
|
|
791
802
|
httpPort = httpServer.port ?? 0
|
|
792
803
|
log({ kind: 'daemon-http-listening', host: httpHostname, port: httpPort })
|
|
793
|
-
opts.currentHostDaemonHolder?.set({
|
|
804
|
+
opts.currentHostDaemonHolder?.set({
|
|
805
|
+
httpPort,
|
|
806
|
+
register: registerCurrentChild,
|
|
807
|
+
deregister: deregisterCurrentChild,
|
|
808
|
+
})
|
|
794
809
|
|
|
795
810
|
const sockets = new Set<NetSocket>()
|
|
796
811
|
const listener = createServer((socket) => {
|
|
@@ -812,28 +827,36 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
812
827
|
|
|
813
828
|
const runGc = async (): Promise<void> => {
|
|
814
829
|
for (const name of Array.from(cwds.keys())) {
|
|
830
|
+
const generation = registrationGenerations.get(name)
|
|
831
|
+
if (generation === undefined) continue
|
|
815
832
|
const status = await probeContainerAlive(name)
|
|
816
|
-
if (status === 'alive') {
|
|
817
|
-
gcMisses.delete(name)
|
|
818
|
-
continue
|
|
819
|
-
}
|
|
820
|
-
if (status === 'unknown') continue
|
|
821
|
-
const misses = (gcMisses.get(name) ?? 0) + 1
|
|
822
|
-
if (misses < gcMissesToDeregister) {
|
|
823
|
-
gcMisses.set(name, misses)
|
|
824
|
-
continue
|
|
825
|
-
}
|
|
826
|
-
gcMisses.delete(name)
|
|
827
833
|
void runSerially(name, async () => {
|
|
834
|
+
// A probe is only authoritative for the registration it observed before
|
|
835
|
+
// awaiting Docker. Re-registration installs a new generation before any
|
|
836
|
+
// later async registration work, so stale probes cannot spend its miss
|
|
837
|
+
// budget or remove it.
|
|
838
|
+
if (registrationGenerations.get(name) !== generation) return
|
|
839
|
+
if (status === 'alive') {
|
|
840
|
+
gcMisses.delete(name)
|
|
841
|
+
return
|
|
842
|
+
}
|
|
843
|
+
if (status === 'unknown') return
|
|
844
|
+
const misses = (gcMisses.get(name) ?? 0) + 1
|
|
845
|
+
if (misses < gcMissesToDeregister) {
|
|
846
|
+
gcMisses.set(name, misses)
|
|
847
|
+
return
|
|
848
|
+
}
|
|
849
|
+
gcMisses.delete(name)
|
|
828
850
|
const hadCwd = cwds.delete(name)
|
|
829
851
|
restartTokens.delete(name)
|
|
852
|
+
brokerTokens.delete(name)
|
|
853
|
+
registrationGenerations.delete(name)
|
|
830
854
|
if (opts.portbroker) await withCleanupTimeout(opts.portbroker.stop(name, 'deregistered'), cleanupStepTimeoutMs)
|
|
831
855
|
if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(name), cleanupStepTimeoutMs)
|
|
832
856
|
if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(name), cleanupStepTimeoutMs)
|
|
833
857
|
if (opts.teamsRenewal) await withCleanupTimeout(opts.teamsRenewal.stop(name), cleanupStepTimeoutMs)
|
|
834
858
|
await removeRegistrationFile(name)
|
|
835
859
|
if (hadCwd) log({ kind: 'deregister', containerName: name, reason: 'gone' })
|
|
836
|
-
return { ok: true }
|
|
837
860
|
})
|
|
838
861
|
}
|
|
839
862
|
}
|
package/src/init/dockerfile.ts
CHANGED
|
@@ -56,12 +56,13 @@ export type BuildDockerfileOptions = {
|
|
|
56
56
|
// agent author. See `src/sandbox/` for the bwrap command builder, and
|
|
57
57
|
// `docs/internals/sandbox.mdx` for why bwrap is the right
|
|
58
58
|
// shape for per-call isolation inside an already-containerized agent. The
|
|
59
|
-
// outer container's
|
|
60
|
-
//
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
//
|
|
64
|
-
//
|
|
59
|
+
// outer container's `seccomp=unconfined` plus configured AppArmor profile
|
|
60
|
+
// (see `src/container/start.ts:planStart`) let bwrap create user/pid/mount
|
|
61
|
+
// namespaces from inside Docker. Docker's seccomp default blocks userns and
|
|
62
|
+
// docker-default AppArmor explicitly denies mount; on stock Ubuntu 23.10+ the
|
|
63
|
+
// operator must load the shipped typeclaw-bwrap profile because the host's
|
|
64
|
+
// unprivileged_userns catch-all also denies uid_map writes to unconfined
|
|
65
|
+
// processes. The package, options, and host profile are load-bearing together.
|
|
65
66
|
//
|
|
66
67
|
// `jq` is in baseline (not behind a toggle) so it is available to the
|
|
67
68
|
// per-tool bwrap sandbox that wraps agent bash calls. The sandbox only
|