Skip to content

Commit 7b64b14

Browse files
authored
Use mounted workspace backend in sandbox runs (#182)
* fix: use mounted workspace backend in sandbox * fix: preserve recipe run diagnostics
1 parent e961e5f commit 7b64b14

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

packages/cli/src/agent-code.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ if (!defined('DATAMACHINE_WORKSPACE_PATH')) {
6464
define('DATAMACHINE_WORKSPACE_PATH', ${JSON.stringify(SANDBOX_WORKSPACE_ROOT)});
6565
}
6666
67+
add_filter('datamachine_code_remote_workspace_backend_should_handle', '__return_false', 100);
68+
6769
$sandbox_workspace_adoptions = array();
6870
if (function_exists('wp_get_ability')) {
6971
$sandbox_adopt_callback = static function () use (&$sandbox_workspace_adoptions): void {
@@ -257,6 +259,8 @@ if (!defined('DATAMACHINE_WORKSPACE_PATH')) {
257259
define('DATAMACHINE_WORKSPACE_PATH', ${JSON.stringify(SANDBOX_WORKSPACE_ROOT)});
258260
}
259261
262+
add_filter('datamachine_code_remote_workspace_backend_should_handle', '__return_false', 100);
263+
260264
add_filter('datamachine_should_load_full_runtime', '__return_true', 1);
261265
262266
$plugins = array_merge(array(

packages/runtime-playground/src/index.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ class PlaygroundCommandCrashError extends Error {
8080
}
8181
}
8282

83+
class PlaygroundCliExitError extends Error {
84+
readonly code = "wp-codebox-playground-cli-exited"
85+
86+
constructor(readonly exitCode: number) {
87+
super(`WordPress Playground CLI exited while booting the runtime with exit code ${exitCode}.`)
88+
this.name = "PlaygroundCliExitError"
89+
}
90+
}
91+
8392
class PlaygroundPreviewPortUnavailableError extends Error {
8493
readonly code = "wp-codebox-preview-port-in-use"
8594

@@ -115,6 +124,20 @@ function errorMessage(error: unknown): string {
115124
return error instanceof Error ? error.message : String(error)
116125
}
117126

127+
async function runPlaygroundCliWithoutProcessExit<T>(callback: () => Promise<T>): Promise<T> {
128+
const exit = process.exit
129+
process.exit = ((code?: string | number | null | undefined): never => {
130+
const exitCode = typeof code === "number" ? code : 1
131+
throw new PlaygroundCliExitError(exitCode)
132+
}) as typeof process.exit
133+
134+
try {
135+
return await callback()
136+
} finally {
137+
process.exit = exit
138+
}
139+
}
140+
118141
interface PlaygroundCliServer {
119142
playground: {
120143
run(options: { code: string } | { scriptPath: string }): Promise<PlaygroundRunResponse>
@@ -1292,7 +1315,7 @@ echo json_encode(array('command' => 'inspect-mounted-inputs', 'mounts' => $inspe
12921315
}
12931316

12941317
try {
1295-
const server = await runCLI({
1318+
const server = await runPlaygroundCliWithoutProcessExit(() => runCLI({
12961319
command: "server",
12971320
port: 0,
12981321
quiet: true,
@@ -1304,7 +1327,7 @@ echo json_encode(array('command' => 'inspect-mounted-inputs', 'mounts' => $inspe
13041327
wp: this.spec.environment.version,
13051328
"site-url": this.spec.preview?.siteUrl,
13061329
blueprint: playgroundBlueprint(this.spec.environment.blueprint, this.spec.policy, this.spec.preview?.siteUrl),
1307-
})
1330+
}))
13081331

13091332
if (!this.spec.preview?.port) {
13101333
return server

scripts/agent-sandbox-code-smoke.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async function main() {
1414
assert.match(code, /\\"agent_modes\\":\[\\"sandbox\\",\\"pipeline\\"\]/, "client context should report additive sandbox modes")
1515
assert.match(code, /\\"tool_policy\\":\{\\"mode\\":\\"allow\\",\\"tools\\":\[.*\\"workspace_read\\"/, "sandbox agent should allow workspace tools")
1616
assert.match(code, /datamachine_agent_mode_sandbox/, "sandbox mode should inject tool guidance")
17+
assert.match(code, /datamachine_code_remote_workspace_backend_should_handle/, "sandbox mode should use the mounted workspace backend")
1718
assert.match(code, /Do not invent alternate tool names such as read_file/, "sandbox guidance should prevent pseudo-tool aliases")
1819
assert.match(code, /workspace_apply_patch/, "sandbox tool policy should include patch application")
1920
}

0 commit comments

Comments
 (0)