mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 12:24:39 +00:00
* feat(runner): replace server-side bash sandbox with remote runner * fix(runner): address review findings for output bounds, key rotation, defaults, and generated uploads * chore(docker): remove sandbox privileges and env vars from remaining compose files * feat(cli): add runner start with bundled SRT sandboxing (#535) * refactor(agent): expose runner bash without skill gating * feat(cli): add runner start with optional SRT sandboxing * refactor(cli): remove runner Python dependency * feat(cli): bundle SRT and use platform-native runner shells * refactor(cli): delegate runner processes to Execa * chore(cli): upgrade @anthropic-ai/sandbox-runtime to 0.0.83 * feat(cli): discover Bash for Windows runners Use Pi-style Bash discovery and launch behavior, exercise the packaged CLI shell, and wait for pending ZIP metadata reads before closing cancelled downloads. * fix(runner): enforce ownership and safe reconnects Gate dispatch on reconciliation, retain cancellations across disconnects, and stop replaced clients. Guard the single-server runner topology with a database lease. Restage missing generated inputs, report signal exits, and bound generated-file cleanup. * feat(webui): add Korean translations for agent runners * feat(cli): add runner event logging and --verbose flag (#536) * feat(cli): add runner event logging and --verbose flag * fix(cli): log runner capacity rejections accurately and use monotonic duration
233 lines
8.6 KiB
TypeScript
233 lines
8.6 KiB
TypeScript
import { randomUUID } from 'node:crypto'
|
|
import { execa } from 'execa'
|
|
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
|
import {
|
|
runnerEnvironmentSchema,
|
|
type RunnerEnvironment,
|
|
type RunnerExecResultMessage,
|
|
type RunnerExecStartMessage,
|
|
} from '@shumai/dtos'
|
|
import { commandEnv } from './config'
|
|
import { RunnerFiles } from './files'
|
|
import { resolveRunnerShell } from './shell-config'
|
|
import { loadSandbox } from './srt'
|
|
|
|
const MAX_OUTPUT_BYTES = 64 * 1024 * 1024
|
|
const MAX_TIMEOUT_SEC = 3600
|
|
|
|
export class RunnerShell {
|
|
readonly environment: RunnerEnvironment
|
|
private sandbox?: Awaited<ReturnType<typeof loadSandbox>>
|
|
private initialized = false
|
|
private active = 0
|
|
|
|
constructor(
|
|
private readonly files: RunnerFiles,
|
|
private readonly srtPath?: string,
|
|
private readonly config = resolveRunnerShell(),
|
|
) {
|
|
this.environment = runnerEnvironmentSchema.parse({
|
|
platform: process.platform,
|
|
shell: config.executable,
|
|
})
|
|
}
|
|
|
|
async initialize() {
|
|
if (!this.srtPath) return
|
|
if (this.config.commandTransport === 'stdin')
|
|
throw new Error(
|
|
'SRT cannot run the legacy WSL Bash launcher. Install Git for Windows to use Bash with SRT.',
|
|
)
|
|
// Read and validate once; commands cannot weaken the policy by editing the file.
|
|
this.sandbox = await loadSandbox(this.srtPath)
|
|
try {
|
|
await SandboxManager.initialize(this.sandbox.config)
|
|
this.initialized = true
|
|
let diagnostic = ''
|
|
const probe = await this.run(
|
|
{ type: 'exec.start', executionId: randomUUID(), command: 'exit 0', timeoutSec: 15 },
|
|
new AbortController().signal,
|
|
(_stream, data) => {
|
|
diagnostic = (diagnostic + data).slice(-4096)
|
|
},
|
|
)
|
|
if (probe.exitCode !== 0 || probe.error || probe.timedOut)
|
|
throw new Error(
|
|
`SRT preflight failed: ${probe.error || diagnostic.trim() || 'Check settings and local SRT prerequisites.'}`,
|
|
)
|
|
} catch (error) {
|
|
await this.close()
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async close() {
|
|
if (!this.sandbox) return
|
|
try {
|
|
await SandboxManager.reset()
|
|
} finally {
|
|
await this.sandbox.cleanup()
|
|
this.sandbox = undefined
|
|
this.initialized = false
|
|
}
|
|
}
|
|
|
|
async run(
|
|
message: RunnerExecStartMessage,
|
|
signal: AbortSignal,
|
|
output: (stream: 'stdout' | 'stderr', data: string) => void,
|
|
): Promise<RunnerExecResultMessage> {
|
|
const result: RunnerExecResultMessage = {
|
|
type: 'exec.result',
|
|
executionId: message.executionId,
|
|
exitCode: null,
|
|
signal: null,
|
|
timedOut: false,
|
|
aborted: false,
|
|
}
|
|
if (this.srtPath) this.active++
|
|
try {
|
|
if (signal.aborted) return { ...result, aborted: true }
|
|
const timeout = message.timeoutSec ?? 120
|
|
if (!Number.isFinite(timeout) || timeout <= 0 || timeout > MAX_TIMEOUT_SEC)
|
|
throw new Error(`Command timeout must be between 0 and ${MAX_TIMEOUT_SEC} seconds.`)
|
|
const logPath = `.pi/logs/bash-${message.executionId}.log`
|
|
const log = await this.files.createWriter(logPath)
|
|
try {
|
|
const windows = process.platform === 'win32'
|
|
const { commandTransport } = this.config
|
|
const useShell = commandTransport === 'shell'
|
|
const sendCommandOnStdin = commandTransport === 'stdin'
|
|
let executable = useShell ? message.command : this.config.executable
|
|
let args = useShell ? [] : sendCommandOnStdin ? ['-s'] : ['-c', message.command]
|
|
let shell: string | false = useShell && this.config.executable
|
|
let env = commandEnv(message.env)
|
|
if (this.srtPath) {
|
|
if (!this.initialized) throw new Error('SRT has not been initialized.')
|
|
const skillEnv = commandEnv(message.env, false)
|
|
const command = windows
|
|
? message.command
|
|
: [
|
|
...Object.entries(skillEnv).map(
|
|
([key, value]) => `export ${key}='${value!.replaceAll("'", "'\\''")}'`,
|
|
),
|
|
message.command,
|
|
].join('\n')
|
|
const wrapped = await SandboxManager.wrapWithSandboxArgv(
|
|
command,
|
|
this.config.executable,
|
|
undefined,
|
|
signal,
|
|
this.files.cwd,
|
|
{ commandId: message.executionId, commandText: message.command },
|
|
)
|
|
;[executable, ...args] = wrapped.argv
|
|
shell = false
|
|
if (windows) {
|
|
// SRT's broker starts a fresh sandbox-user environment. Explicit skill
|
|
// values belong in its child overlay, never in the broker's startup env.
|
|
const separator = args.indexOf('--')
|
|
if (separator < 0) throw new Error('SRT returned an invalid Windows launch command.')
|
|
args.splice(
|
|
separator,
|
|
0,
|
|
...Object.entries(skillEnv).flatMap(([key, value]) => ['--env', `${key}=${value}`]),
|
|
)
|
|
env = wrapped.env
|
|
} else env = commandEnv()
|
|
}
|
|
if (signal.aborted) return { ...result, aborted: true }
|
|
const deadline = Date.now() + Math.ceil(timeout * 1000)
|
|
const child = execa(executable, args, {
|
|
cwd: this.files.cwd,
|
|
env,
|
|
extendEnv: false,
|
|
shell,
|
|
stdin: sendCommandOnStdin ? [Buffer.from(message.command)] : 'ignore',
|
|
buffer: false,
|
|
reject: false,
|
|
stripFinalNewline: false,
|
|
verbose: 'none',
|
|
windowsHide: true,
|
|
killDescendants: true,
|
|
killSignal: 'SIGKILL',
|
|
cancelSignal: signal,
|
|
timeout: Math.ceil(timeout * 1000),
|
|
})
|
|
let bytes = 0
|
|
let writes = Promise.resolve()
|
|
let drainTimer: ReturnType<typeof setTimeout> | undefined
|
|
let drainExpired = false
|
|
let exitedWithinDeadline = false
|
|
child.nodeChildProcess.once('exit', (exitCode) => {
|
|
exitedWithinDeadline = exitCode !== null && Date.now() < deadline
|
|
// Execa cleans up descendants where the OS still permits it. Bound
|
|
// draining if a background child keeps inherited output pipes open.
|
|
child.kill('SIGKILL')
|
|
drainTimer = setTimeout(() => {
|
|
drainExpired = true
|
|
child.stdout.destroy()
|
|
child.stderr.destroy()
|
|
}, 1000)
|
|
})
|
|
try {
|
|
const readers = (['stdout', 'stderr'] as const).map(async (stream) => {
|
|
child[stream].setEncoding('utf8')
|
|
try {
|
|
for await (const data of child[stream]) {
|
|
bytes += Buffer.byteLength(data)
|
|
if (bytes > MAX_OUTPUT_BYTES)
|
|
throw new Error('Command exceeded the 64 MiB output limit.')
|
|
await (writes = writes.then(async () => {
|
|
await log.write(data)
|
|
output(stream, data)
|
|
}))
|
|
}
|
|
} catch (error) {
|
|
if (
|
|
drainExpired &&
|
|
error instanceof Error &&
|
|
'code' in error &&
|
|
error.code === 'ERR_STREAM_PREMATURE_CLOSE'
|
|
)
|
|
return
|
|
result.error ??= error instanceof Error ? error.message : 'Output streaming failed.'
|
|
child.kill('SIGKILL')
|
|
}
|
|
})
|
|
const [completed] = await Promise.all([child, Promise.all(readers)])
|
|
await writes
|
|
result.exitCode = completed.exitCode ?? null
|
|
result.signal = completed.signal ?? null
|
|
// Execa's timeout includes stream draining. Report the command's
|
|
// runtime, so a timely exit does not become a timeout while draining.
|
|
result.timedOut = completed.timedOut && !exitedWithinDeadline
|
|
result.aborted = completed.isCanceled
|
|
if (
|
|
completed.failed &&
|
|
completed.exitCode === undefined &&
|
|
!result.timedOut &&
|
|
!result.aborted &&
|
|
!completed.signal
|
|
)
|
|
result.error = `Command execution failed (${completed.code ?? 'unknown error'}).`
|
|
result.fullOutputPath = logPath
|
|
} finally {
|
|
clearTimeout(drainTimer)
|
|
}
|
|
} finally {
|
|
await log.close()
|
|
}
|
|
} catch (error) {
|
|
result.aborted = signal.aborted
|
|
result.error = error instanceof Error ? error.message : 'Command execution failed.'
|
|
} finally {
|
|
// SRT's temporary mount resources are shared: clean up only after every
|
|
// concurrent command has finished, and keep the proxies until shutdown.
|
|
if (this.srtPath && --this.active === 0 && this.initialized)
|
|
SandboxManager.cleanupAfterCommand()
|
|
}
|
|
return result
|
|
}
|
|
}
|