Skip to content

Commit 45cd08b

Browse files
committed
Run the handlesteps generator outside of quickjs sandbox
1 parent 4db7fd6 commit 45cd08b

File tree

1 file changed

+22
-56
lines changed

1 file changed

+22
-56
lines changed

packages/agent-runtime/src/run-programmatic-step.ts

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { getErrorObject } from '@codebuff/common/util/error'
33
import { cloneDeep } from 'lodash'
44

55
import { executeToolCall } from './tools/tool-executor'
6-
import { SandboxManager } from './util/quickjs-sandbox'
76

87
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
98
import type {
@@ -17,34 +16,24 @@ import type {
1716
} from '@codebuff/common/types/contracts/client'
1817
import type { AddAgentStepFn } from '@codebuff/common/types/contracts/database'
1918
import type { Logger } from '@codebuff/common/types/contracts/logger'
20-
import type {
21-
ParamsExcluding,
22-
ParamsOf,
23-
} from '@codebuff/common/types/function-params'
19+
import type { ParamsExcluding } from '@codebuff/common/types/function-params'
2420
import type {
2521
ToolResultOutput,
2622
ToolResultPart,
2723
} from '@codebuff/common/types/messages/content-part'
2824
import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
2925
import type { AgentState } from '@codebuff/common/types/session-state'
3026

31-
// Global sandbox manager for QuickJS contexts
32-
const sandboxManager = new SandboxManager()
33-
3427
// Maintains generator state for all agents. Generator state can't be serialized, so we store it in memory.
3528
const runIdToGenerator: Record<string, StepGenerator | undefined> = {}
3629
export const runIdToStepAll: Set<string> = new Set()
3730

3831
// Function to clear the generator cache for testing purposes
39-
export function clearAgentGeneratorCache(
40-
params: ParamsOf<typeof sandboxManager.dispose>,
41-
) {
32+
export function clearAgentGeneratorCache() {
4233
for (const key in runIdToGenerator) {
4334
delete runIdToGenerator[key]
4435
}
4536
runIdToStepAll.clear()
46-
// Clean up QuickJS sandboxes
47-
sandboxManager.dispose(params)
4837
}
4938

5039
// Function to handle programmatic agents
@@ -129,10 +118,9 @@ export async function runProgrammaticStep(
129118

130119
// Run with either a generator or a sandbox.
131120
let generator = runIdToGenerator[agentState.runId]
132-
let sandbox = sandboxManager.getSandbox({ runId: agentState.runId })
133121

134122
// Check if we need to initialize a generator
135-
if (!generator && !sandbox) {
123+
if (!generator) {
136124
const createLogMethod =
137125
(level: 'debug' | 'info' | 'warn' | 'error') =>
138126
(data: any, msg?: string) => {
@@ -153,31 +141,19 @@ export async function runProgrammaticStep(
153141
error: createLogMethod('error'),
154142
}
155143

156-
if (typeof template.handleSteps === 'string') {
157-
// Initialize QuickJS sandbox for string-based generator
158-
sandbox = await sandboxManager.getOrCreateSandbox({
159-
runId: agentState.runId,
160-
generatorCode: template.handleSteps,
161-
initialInput: {
162-
agentState,
163-
prompt,
164-
params: toolCallParams,
165-
logger: streamingLogger,
166-
},
167-
config: undefined, // config
168-
sandboxLogger: streamingLogger, // pass the streaming logger instance for internal use
169-
logger,
170-
})
171-
} else {
172-
// Initialize native generator
173-
generator = template.handleSteps({
174-
agentState,
175-
prompt,
176-
params,
177-
logger: streamingLogger,
178-
})
179-
runIdToGenerator[agentState.runId] = generator
180-
}
144+
const generatorFn =
145+
typeof template.handleSteps === 'string'
146+
? eval(`(${template.handleSteps})`)
147+
: template.handleSteps
148+
149+
// Initialize native generator
150+
generator = generatorFn({
151+
agentState,
152+
prompt,
153+
params: toolCallParams,
154+
logger: streamingLogger,
155+
})
156+
runIdToGenerator[agentState.runId] = generator
181157
}
182158

183159
// Check if we're in STEP_ALL mode
@@ -239,17 +215,11 @@ export async function runProgrammaticStep(
239215
creditsBefore = state.agentState.directCreditsUsed
240216
childrenBefore = state.agentState.childRunIds.length
241217

242-
const result = sandbox
243-
? await sandbox.executeStep({
244-
agentState: getPublicAgentState(state.agentState),
245-
toolResult,
246-
stepsComplete,
247-
})
248-
: generator!.next({
249-
agentState: getPublicAgentState(state.agentState),
250-
toolResult,
251-
stepsComplete,
252-
})
218+
const result = generator!.next({
219+
agentState: getPublicAgentState(state.agentState),
220+
toolResult,
221+
stepsComplete,
222+
})
253223

254224
if (result.done) {
255225
endTurn = true
@@ -263,7 +233,7 @@ export async function runProgrammaticStep(
263233
break
264234
}
265235

266-
if (result.value.type === 'STEP_TEXT') {
236+
if ('type' in result.value && result.value.type === 'STEP_TEXT') {
267237
textOverride = result.value.text
268238
break
269239
}
@@ -464,10 +434,6 @@ export async function runProgrammaticStep(
464434
}
465435
} finally {
466436
if (endTurn) {
467-
if (sandbox) {
468-
// Clean up QuickJS sandbox if execution is complete
469-
sandboxManager.removeSandbox({ runId: agentState.runId, logger })
470-
}
471437
delete runIdToGenerator[agentState.runId]
472438
runIdToStepAll.delete(agentState.runId)
473439
}

0 commit comments

Comments
 (0)