11import { spawn } from "node:child_process" ;
22import path from "node:path" ;
33import { ensureDir } from "../util/fs.js" ;
4- import type { VllmAgentConfig } from "../types.js" ;
4+ import type { VllmAgentConfig , WorkspaceIdentity } from "../types.js" ;
55import type { SessionStore } from "../session/store.js" ;
66import { resolveRtkStatus , rtkDbPath , rtkEnv , type RtkRuntime } from "./manager.js" ;
77import { readRtkCommandStats , type RtkCommandStats } from "./stats.js" ;
8+ import { runSandboxedProcess } from "../sandbox/runner.js" ;
9+ import type { SandboxExecutionInfo } from "../sandbox/types.js" ;
810
911export interface RtkShellCommandOptions {
1012 config : VllmAgentConfig ;
@@ -15,6 +17,7 @@ export interface RtkShellCommandOptions {
1517 tool_name : string ;
1618 command : string ;
1719 cwd : string ;
20+ workspace : WorkspaceIdentity ;
1821 env ?: NodeJS . ProcessEnv ;
1922 timeout_ms : number ;
2023}
@@ -27,6 +30,7 @@ export interface RtkShellCommandResult {
2730 command : string ;
2831 rewritten_command ?: string ;
2932 rtk ?: RtkCommandStats ;
33+ sandbox ?: SandboxExecutionInfo ;
3034}
3135
3236interface PreparedRtkCommand {
@@ -40,7 +44,7 @@ interface PreparedRtkCommand {
4044
4145export async function runRtkAwareShellCommand ( options : RtkShellCommandOptions ) : Promise < RtkShellCommandResult > {
4246 const prepared = await prepareCommand ( options ) ;
43- const result = await runShell ( prepared . command , options . cwd , prepared . env , options . timeout_ms ) ;
47+ const result = await runShell ( prepared . command , options . cwd , prepared . env , options . timeout_ms , options . config , options . workspace , prepared . original_command , prepared . rewritten_command ) ;
4448 const rtk = await recordRtkSavings ( options , prepared ) ;
4549 return {
4650 ...result ,
@@ -146,39 +150,26 @@ async function recordRtkSavings(options: RtkShellCommandOptions, prepared: Prepa
146150 return stats ;
147151}
148152
149- function runShell ( command : string , cwd : string , env : NodeJS . ProcessEnv , timeoutMs : number ) : Promise < Omit < RtkShellCommandResult , "command" > > {
150- return new Promise ( ( resolve ) => {
151- const child = spawn ( command , {
152- cwd,
153- env,
154- shell : true ,
155- } ) ;
156- let stdout = "" ;
157- let stderr = "" ;
158- let timedOut = false ;
159- const timeout = setTimeout ( ( ) => {
160- timedOut = true ;
161- child . kill ( "SIGTERM" ) ;
162- setTimeout ( ( ) => {
163- if ( ! child . killed ) {
164- child . kill ( "SIGKILL" ) ;
165- }
166- } , 2000 ) . unref ( ) ;
167- } , timeoutMs ) ;
168- child . stdout . on ( "data" , ( chunk ) => {
169- stdout += String ( chunk ) ;
170- } ) ;
171- child . stderr . on ( "data" , ( chunk ) => {
172- stderr += String ( chunk ) ;
173- } ) ;
174- child . on ( "close" , ( code ) => {
175- clearTimeout ( timeout ) ;
176- resolve ( { code, stdout, stderr, timed_out : timedOut } ) ;
177- } ) ;
178- child . on ( "error" , ( error ) => {
179- clearTimeout ( timeout ) ;
180- resolve ( { code : 127 , stdout, stderr : error . message , timed_out : timedOut } ) ;
181- } ) ;
153+ async function runShell (
154+ command : string ,
155+ cwd : string ,
156+ env : NodeJS . ProcessEnv ,
157+ timeoutMs : number ,
158+ config : VllmAgentConfig ,
159+ workspace : WorkspaceIdentity ,
160+ originalCommand : string ,
161+ rewrittenCommand ?: string ,
162+ ) : Promise < Omit < RtkShellCommandResult , "command" > > {
163+ return await runSandboxedProcess ( {
164+ config,
165+ workspace,
166+ command,
167+ shell : true ,
168+ cwd,
169+ env,
170+ timeoutMs,
171+ originalCommand,
172+ rewrittenCommand,
182173 } ) ;
183174}
184175
0 commit comments