typeclaw 0.48.8 → 0.48.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/scripts/verify-mask-fd-sandbox.sh +106 -0
- package/src/agent/index.ts +9 -0
- 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 +44 -5
- package/src/channels/adapters/github/inbound.ts +15 -2
- package/src/channels/adapters/instagram-continuity-store.ts +141 -0
- package/src/channels/adapters/instagram.ts +149 -4
- package/src/channels/adapters/slack-bot.ts +13 -3
- package/src/channels/adapters/teams.ts +8 -6
- package/src/channels/github-verdict-activity.ts +4 -2
- package/src/channels/manager.ts +18 -1
- package/src/channels/router.ts +117 -2
- package/src/channels/types.ts +10 -0
- package/src/cli/compose.ts +7 -4
- package/src/compose/restart.ts +26 -16
- package/src/compose/start.ts +24 -14
- package/src/container/restart.ts +4 -0
- package/src/container/shared.ts +30 -3
- package/src/container/start.ts +104 -32
- package/src/container/stop.ts +2 -1
- package/src/hostd/daemon.ts +40 -17
- package/src/run/channel-session-factory.ts +35 -15
- package/src/sandbox/build.ts +20 -7
- package/src/sandbox/policy.ts +5 -2
package/src/container/shared.ts
CHANGED
|
@@ -20,6 +20,10 @@ export type DockerExecOptions = {
|
|
|
20
20
|
// stay visible to the user while start() still inspects stderr to detect the
|
|
21
21
|
// missing-credential-helper failure and decide whether to retry.
|
|
22
22
|
captureStderr?: boolean
|
|
23
|
+
// Piped commands normally retain complete output. Long-running callers can
|
|
24
|
+
// still drain without retaining stdout and keep only stderr's diagnostic tail.
|
|
25
|
+
captureStdout?: boolean
|
|
26
|
+
maxCapturedStderrBytes?: number
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
export type DockerExec = (args: string[], options?: DockerExecOptions) => Promise<DockerExecResult>
|
|
@@ -83,9 +87,9 @@ export const defaultDockerExec: DockerExec = async (args, options) => {
|
|
|
83
87
|
signal: options?.signal,
|
|
84
88
|
env,
|
|
85
89
|
})
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
const stderr = await
|
|
90
|
+
const stdoutPromise = readCapturedStream(proc.stdout, options?.captureStdout === false ? 0 : undefined)
|
|
91
|
+
const stderrPromise = readCapturedStream(proc.stderr, options?.maxCapturedStderrBytes)
|
|
92
|
+
const [exitCode, stdout, stderr] = await Promise.all([proc.exited, stdoutPromise, stderrPromise])
|
|
89
93
|
return { exitCode, stdout, stderr }
|
|
90
94
|
} catch (error) {
|
|
91
95
|
if ((error as NodeJS.ErrnoException)?.code === 'ENOENT') {
|
|
@@ -95,6 +99,29 @@ export const defaultDockerExec: DockerExec = async (args, options) => {
|
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
101
|
|
|
102
|
+
export async function readCapturedStream(stream: ReadableStream<Uint8Array>, maxBytes?: number): Promise<string> {
|
|
103
|
+
if (maxBytes === undefined) return await new Response(stream).text()
|
|
104
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0) throw new RangeError('maxBytes must be a non-negative integer')
|
|
105
|
+
|
|
106
|
+
const reader = stream.getReader()
|
|
107
|
+
let tail = Buffer.alloc(0)
|
|
108
|
+
try {
|
|
109
|
+
for (;;) {
|
|
110
|
+
const { done, value } = await reader.read()
|
|
111
|
+
if (done) break
|
|
112
|
+
if (maxBytes === 0) continue
|
|
113
|
+
const chunk = Buffer.from(value)
|
|
114
|
+
tail =
|
|
115
|
+
chunk.length >= maxBytes
|
|
116
|
+
? chunk.subarray(chunk.length - maxBytes)
|
|
117
|
+
: Buffer.concat([tail, chunk]).subarray(-maxBytes)
|
|
118
|
+
}
|
|
119
|
+
} finally {
|
|
120
|
+
reader.releaseLock()
|
|
121
|
+
}
|
|
122
|
+
return tail.toString('utf8')
|
|
123
|
+
}
|
|
124
|
+
|
|
98
125
|
// Streams a docker child's stdout straight to the parent terminal while teeing
|
|
99
126
|
// stderr to BOTH the terminal and a buffer. A plain `stderr: 'inherit'` would
|
|
100
127
|
// keep the build output live but leave start() blind to the failure reason;
|
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
|
|
|
@@ -1042,8 +1104,9 @@ async function runImageBuild(args: {
|
|
|
1042
1104
|
imageTag: string
|
|
1043
1105
|
buildContext: string
|
|
1044
1106
|
hasBuildx: boolean
|
|
1045
|
-
|
|
1046
|
-
|
|
1107
|
+
streamOutput: boolean
|
|
1108
|
+
}): Promise<{ ok: true } | { ok: false; exitCode: number; credentialHelperRetried: boolean }> {
|
|
1109
|
+
const { exec, cwd, imageTag, buildContext, hasBuildx, streamOutput } = args
|
|
1047
1110
|
const buildArgv = (frontend: 'buildx' | 'legacy'): string[] =>
|
|
1048
1111
|
frontend === 'buildx'
|
|
1049
1112
|
? ['buildx', 'build', '--load', '-t', imageTag, buildContext]
|
|
@@ -1051,12 +1114,17 @@ async function runImageBuild(args: {
|
|
|
1051
1114
|
|
|
1052
1115
|
let sanitizedConfig: SanitizedDockerConfig | null = null
|
|
1053
1116
|
const attempt = async (frontend: 'buildx' | 'legacy'): Promise<DockerExecResult> =>
|
|
1054
|
-
exec(buildArgv(frontend), {
|
|
1117
|
+
exec(buildArgv(frontend), {
|
|
1118
|
+
cwd,
|
|
1119
|
+
...(streamOutput ? { inheritStdio: true, captureStderr: true } : {}),
|
|
1120
|
+
...(streamOutput ? {} : { captureStdout: false, maxCapturedStderrBytes: 32 * 1024 }),
|
|
1121
|
+
env: sanitizedConfig?.env,
|
|
1122
|
+
})
|
|
1055
1123
|
|
|
1056
1124
|
try {
|
|
1057
1125
|
let frontend: 'buildx' | 'legacy' = hasBuildx ? 'buildx' : 'legacy'
|
|
1058
1126
|
let result = await attempt(frontend)
|
|
1059
|
-
if (result.exitCode === 0) return true
|
|
1127
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1060
1128
|
|
|
1061
1129
|
// Same-frontend retry: a broken credential helper aborts the pull before
|
|
1062
1130
|
// the builder ever matters, so strip it and retry the identical build.
|
|
@@ -1064,7 +1132,7 @@ async function runImageBuild(args: {
|
|
|
1064
1132
|
sanitizedConfig = await createSanitizedDockerConfig()
|
|
1065
1133
|
if (sanitizedConfig) {
|
|
1066
1134
|
result = await attempt(frontend)
|
|
1067
|
-
if (result.exitCode === 0) return true
|
|
1135
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1068
1136
|
}
|
|
1069
1137
|
}
|
|
1070
1138
|
|
|
@@ -1075,23 +1143,23 @@ async function runImageBuild(args: {
|
|
|
1075
1143
|
await refreshDockerfile(cwd, { buildKit: false })
|
|
1076
1144
|
frontend = 'legacy'
|
|
1077
1145
|
result = await attempt(frontend)
|
|
1078
|
-
if (result.exitCode === 0) return true
|
|
1146
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1079
1147
|
if (sanitizedConfig === null && isMissingDockerCredentialHelper(result.stderr)) {
|
|
1080
1148
|
sanitizedConfig = await createSanitizedDockerConfig()
|
|
1081
1149
|
if (sanitizedConfig) {
|
|
1082
1150
|
result = await attempt(frontend)
|
|
1083
|
-
if (result.exitCode === 0) return true
|
|
1151
|
+
if (result.exitCode === 0) return { ok: true }
|
|
1084
1152
|
}
|
|
1085
1153
|
}
|
|
1086
1154
|
}
|
|
1087
1155
|
|
|
1088
|
-
if (sanitizedConfig !== null) {
|
|
1089
|
-
|
|
1156
|
+
if (sanitizedConfig !== null && streamOutput) {
|
|
1157
|
+
const guidance =
|
|
1090
1158
|
'typeclaw: docker build still failed after retrying public image pulls without the configured ' +
|
|
1091
|
-
|
|
1092
|
-
)
|
|
1159
|
+
'credential helper. Your ~/.docker/config.json credsStore/credHelpers may be broken.'
|
|
1160
|
+
process.stderr.write(`${guidance}\n`)
|
|
1093
1161
|
}
|
|
1094
|
-
return false
|
|
1162
|
+
return { ok: false, exitCode: result.exitCode, credentialHelperRetried: sanitizedConfig !== null }
|
|
1095
1163
|
} finally {
|
|
1096
1164
|
await sanitizedConfig?.cleanup()
|
|
1097
1165
|
}
|
|
@@ -1448,6 +1516,7 @@ async function registerWithDaemon({
|
|
|
1448
1516
|
return {
|
|
1449
1517
|
state: 'registered',
|
|
1450
1518
|
control: { url: `http://${CONTAINER_HOSTD_HOST}:${currentHostDaemon.httpPort}`, token, brokerToken },
|
|
1519
|
+
deregister: (registeredContainerName) => currentHostDaemon.deregister(registeredContainerName),
|
|
1451
1520
|
}
|
|
1452
1521
|
}
|
|
1453
1522
|
|
|
@@ -1458,6 +1527,9 @@ async function registerWithDaemon({
|
|
|
1458
1527
|
return {
|
|
1459
1528
|
state: 'registered',
|
|
1460
1529
|
control: { url: `http://${CONTAINER_HOSTD_HOST}:${prepared.httpPort}`, token, brokerToken },
|
|
1530
|
+
deregister: async (registeredContainerName) => {
|
|
1531
|
+
await sendToDaemon({ kind: 'deregister', containerName: registeredContainerName })
|
|
1532
|
+
},
|
|
1461
1533
|
}
|
|
1462
1534
|
}
|
|
1463
1535
|
|
|
@@ -1492,7 +1564,7 @@ async function useCurrentHostDaemon(): Promise<{ ok: true; httpPort: number } |
|
|
|
1492
1564
|
}
|
|
1493
1565
|
|
|
1494
1566
|
type PreparedHostDaemonStatus =
|
|
1495
|
-
| { state: 'registered'; control: HostDaemonControl }
|
|
1567
|
+
| { state: 'registered'; control: HostDaemonControl; deregister: (containerName: string) => Promise<void> }
|
|
1496
1568
|
| { state: 'unavailable'; reason: string }
|
|
1497
1569
|
| { state: 'disabled' }
|
|
1498
1570
|
|
|
@@ -1503,7 +1575,7 @@ function stripHostDaemonControl(status: PreparedHostDaemonStatus): HostDaemonSta
|
|
|
1503
1575
|
|
|
1504
1576
|
async function cleanupHostDaemonRegistration(containerName: string, status: PreparedHostDaemonStatus): Promise<void> {
|
|
1505
1577
|
if (status.state !== 'registered') return
|
|
1506
|
-
await
|
|
1578
|
+
await status.deregister(containerName).catch(() => {})
|
|
1507
1579
|
}
|
|
1508
1580
|
|
|
1509
1581
|
// process.env.TZ is honored first because users who explicitly set it (e.g.
|
package/src/container/stop.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
containerNameFromCwd,
|
|
9
9
|
defaultDockerExec,
|
|
10
10
|
type DockerExec,
|
|
11
|
+
isGenuineMissingContainer,
|
|
11
12
|
parseContainerInspectOutput,
|
|
12
13
|
sanitizeDockerStderr,
|
|
13
14
|
waitForRemoval,
|
|
@@ -58,7 +59,7 @@ async function runStop({
|
|
|
58
59
|
// `docker inspect` exits non-zero both when the container does not
|
|
59
60
|
// exist AND when it exists but is in a transient state docker cannot
|
|
60
61
|
// inspect (Removal In Progress, Dead, daemon hiccup). Discriminate by
|
|
61
|
-
if (inspect.stderr
|
|
62
|
+
if (isGenuineMissingContainer(inspect.stderr)) {
|
|
62
63
|
return { ok: true, containerName, running: false }
|
|
63
64
|
}
|
|
64
65
|
return {
|
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
|
}
|
|
@@ -89,6 +89,21 @@ function isValidSessionFileBasename(name: string): boolean {
|
|
|
89
89
|
return name.endsWith('.jsonl')
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
function errorChainHasCode(error: unknown, expectedCode: string): boolean {
|
|
93
|
+
const seen = new Set<object>()
|
|
94
|
+
let current = error
|
|
95
|
+
while (typeof current === 'object' && current !== null && !seen.has(current)) {
|
|
96
|
+
seen.add(current)
|
|
97
|
+
try {
|
|
98
|
+
if ('code' in current && current.code === expectedCode) return true
|
|
99
|
+
current = 'cause' in current ? current.cause : undefined
|
|
100
|
+
} catch {
|
|
101
|
+
return false
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return false
|
|
105
|
+
}
|
|
106
|
+
|
|
92
107
|
// The production wiring for channel-routed sessions. Channel inbounds arrive
|
|
93
108
|
// at the router, the router calls this factory to get an AgentSession, and
|
|
94
109
|
// the agent uses `channel_send` to reply. If `channelRouter` is missing here
|
|
@@ -181,41 +196,46 @@ function tryReopenOrCreate(
|
|
|
181
196
|
logger: FactoryLogger,
|
|
182
197
|
): SessionManager {
|
|
183
198
|
if (existingSessionFile === undefined) {
|
|
184
|
-
logger.warn(
|
|
185
|
-
`[channels] session ${existingSessionId} has no sessionFile (v2 mapping not yet migrated); creating new`,
|
|
186
|
-
)
|
|
199
|
+
logger.warn('[channels] persisted session has no session file; creating new')
|
|
187
200
|
return SessionManager.create(cwd, sessionDir)
|
|
188
201
|
}
|
|
189
202
|
if (!isValidSessionFileBasename(existingSessionFile)) {
|
|
190
|
-
logger.warn(
|
|
191
|
-
`[channels] session ${existingSessionId} has invalid sessionFile (${JSON.stringify(existingSessionFile)}); creating new`,
|
|
192
|
-
)
|
|
203
|
+
logger.warn('[channels] persisted session file is invalid; creating new')
|
|
193
204
|
return SessionManager.create(cwd, sessionDir)
|
|
194
205
|
}
|
|
195
206
|
const path = join(sessionDir, existingSessionFile)
|
|
207
|
+
let missingDuringCap = false
|
|
196
208
|
if (capOptions !== null) {
|
|
197
209
|
try {
|
|
198
210
|
const stats = capJsonlFileInPlace(path, capOptions)
|
|
199
211
|
if (stats.entriesMutated > 0) {
|
|
200
212
|
logger.info(
|
|
201
|
-
`[channels] rehydrate-cap
|
|
213
|
+
`[channels] rehydrate-cap complete: entriesMutated=${stats.entriesMutated} imagesReplaced=${stats.imagesReplaced} textsTruncated=${stats.textsTruncated} bytesElided=${stats.bytesElided}`,
|
|
202
214
|
)
|
|
203
215
|
}
|
|
204
216
|
} catch (err) {
|
|
205
217
|
// Capping is best-effort: if the rewrite fails, fall through to the
|
|
206
218
|
// regular open path so the session still rehydrates uncapped rather
|
|
207
|
-
// than being killed by a transient FS error.
|
|
208
|
-
|
|
209
|
-
|
|
219
|
+
// than being killed by a transient FS error. Missing files are expected
|
|
220
|
+
// during concurrent cleanup; open either creates a fresh transcript or
|
|
221
|
+
// reaches the fallback below.
|
|
222
|
+
if (errorChainHasCode(err, 'ENOENT')) {
|
|
223
|
+
missingDuringCap = true
|
|
224
|
+
} else {
|
|
225
|
+
logger.warn('[channels] rehydrate-cap unavailable; continuing with open')
|
|
226
|
+
}
|
|
210
227
|
}
|
|
211
228
|
}
|
|
212
229
|
try {
|
|
213
|
-
|
|
230
|
+
const sessionManager = SessionManager.open(path)
|
|
231
|
+
if (sessionManager.getSessionId() !== existingSessionId && !missingDuringCap) {
|
|
232
|
+
logger.warn('[channels] persisted session was replaced during rehydrate')
|
|
233
|
+
}
|
|
234
|
+
return sessionManager
|
|
214
235
|
} catch (err) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
)
|
|
236
|
+
if (!errorChainHasCode(err, 'ENOENT')) {
|
|
237
|
+
logger.warn('[channels] persisted session rehydrate failed; creating new')
|
|
238
|
+
}
|
|
219
239
|
return SessionManager.create(cwd, sessionDir)
|
|
220
240
|
}
|
|
221
241
|
}
|
package/src/sandbox/build.ts
CHANGED
|
@@ -24,10 +24,22 @@ export type SandboxedCommand = {
|
|
|
24
24
|
spawnEnv: Record<string, string>
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
//
|
|
27
|
+
// First fd the rendered commandString opens to /dev/null for --ro-bind-data
|
|
28
28
|
// file masks. 3 is the first fd above stdio; the bash tool's spawn does not
|
|
29
29
|
// inherit it, so the redirect is part of the command string itself.
|
|
30
|
-
|
|
30
|
+
//
|
|
31
|
+
// Each masked file gets its OWN fd counting up from here. bwrap copies a
|
|
32
|
+
// --ro-bind-data op's content out of the fd and then close()s it, so a second
|
|
33
|
+
// op naming an already-consumed fd aborts the whole invocation with
|
|
34
|
+
// "Can't write data to file <dest>: Bad file descriptor" — taking every
|
|
35
|
+
// sandboxed bash call down, not just the mask. Upstream defines no reuse
|
|
36
|
+
// semantics for the operand fd (bwrap.xml: "Copy from the file descriptor FD"),
|
|
37
|
+
// and bubblewrap's own demo plus flatpak both allocate a distinct fd per op.
|
|
38
|
+
const FIRST_MASK_DATA_FD = 3
|
|
39
|
+
|
|
40
|
+
function maskDataFd(index: number): number {
|
|
41
|
+
return FIRST_MASK_DATA_FD + index
|
|
42
|
+
}
|
|
31
43
|
|
|
32
44
|
// Pure: no I/O, no bwrap availability probe (that is `ensureBwrapAvailable`'s
|
|
33
45
|
// job). Given a bash command and a policy, returns the bwrap-wrapped argv plus
|
|
@@ -40,8 +52,9 @@ export function buildSandboxedCommand(command: string, policy: SandboxPolicy = {
|
|
|
40
52
|
applyCommandFilter(command, policy.commandFilter)
|
|
41
53
|
}
|
|
42
54
|
const argv = buildArgv(command, policy)
|
|
43
|
-
const
|
|
44
|
-
const commandString =
|
|
55
|
+
const maskRedirects = (policy.masks?.files ?? []).map((_, i) => `${maskDataFd(i)}</dev/null`)
|
|
56
|
+
const commandString =
|
|
57
|
+
maskRedirects.length > 0 ? `${formatCommand(argv)} ${maskRedirects.join(' ')}` : formatCommand(argv)
|
|
45
58
|
return { argv, commandString, spawnEnv: resolveSpawnEnv(policy.env) }
|
|
46
59
|
}
|
|
47
60
|
|
|
@@ -222,9 +235,9 @@ function appendMasks(argv: string[], policy: SandboxPolicy): void {
|
|
|
222
235
|
for (const dir of policy.masks?.dirs ?? []) {
|
|
223
236
|
argv.push('--tmpfs', dir)
|
|
224
237
|
}
|
|
225
|
-
|
|
226
|
-
argv.push('--ro-bind-data', String(
|
|
227
|
-
}
|
|
238
|
+
;(policy.masks?.files ?? []).forEach((file, i) => {
|
|
239
|
+
argv.push('--ro-bind-data', String(maskDataFd(i)), file)
|
|
240
|
+
})
|
|
228
241
|
}
|
|
229
242
|
|
|
230
243
|
function appendWritable(argv: string[], policy: SandboxPolicy): void {
|
package/src/sandbox/policy.ts
CHANGED
|
@@ -60,8 +60,11 @@ export type SandboxProcessPolicy = {
|
|
|
60
60
|
// bwrap primitive that masks a single FILE (--tmpfs is dir-only). --ro-bind-data
|
|
61
61
|
// reads its empty content from a file descriptor, and the bash tool spawns with
|
|
62
62
|
// stdio ["ignore","pipe","pipe"] — no inherited extra fds — so the rendered
|
|
63
|
-
// commandString self-opens fd
|
|
64
|
-
//
|
|
63
|
+
// commandString self-opens ONE fd PER masked file (counting up from
|
|
64
|
+
// FIRST_MASK_DATA_FD) via `<fd></dev/null` redirections appended after
|
|
65
|
+
// `bash -c <command>`. The fds cannot be shared: bwrap close()s each after
|
|
66
|
+
// copying it, so a reused fd aborts the entire invocation with EBADF and kills
|
|
67
|
+
// every sandboxed bash call. Masks MUST render after the broad parent
|
|
65
68
|
// mounts: bwrap applies mount ops in command-line order and the last op on a
|
|
66
69
|
// path wins, so a mask emitted before its parent bind would be re-exposed.
|
|
67
70
|
export type SandboxMaskPolicy = {
|