Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cli/src/agent-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ if (!defined('DATAMACHINE_WORKSPACE_PATH')) {
define('DATAMACHINE_WORKSPACE_PATH', ${JSON.stringify(SANDBOX_WORKSPACE_ROOT)});
}

add_filter('datamachine_code_remote_workspace_backend_should_handle', '__return_false', 100);

$sandbox_workspace_adoptions = array();
if (function_exists('wp_get_ability')) {
$sandbox_adopt_callback = static function () use (&$sandbox_workspace_adoptions): void {
Expand Down Expand Up @@ -257,6 +259,8 @@ if (!defined('DATAMACHINE_WORKSPACE_PATH')) {
define('DATAMACHINE_WORKSPACE_PATH', ${JSON.stringify(SANDBOX_WORKSPACE_ROOT)});
}

add_filter('datamachine_code_remote_workspace_backend_should_handle', '__return_false', 100);

add_filter('datamachine_should_load_full_runtime', '__return_true', 1);

$plugins = array_merge(array(
Expand Down
27 changes: 25 additions & 2 deletions packages/runtime-playground/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ class PlaygroundCommandCrashError extends Error {
}
}

class PlaygroundCliExitError extends Error {
readonly code = "wp-codebox-playground-cli-exited"

constructor(readonly exitCode: number) {
super(`WordPress Playground CLI exited while booting the runtime with exit code ${exitCode}.`)
this.name = "PlaygroundCliExitError"
}
}

class PlaygroundPreviewPortUnavailableError extends Error {
readonly code = "wp-codebox-preview-port-in-use"

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

async function runPlaygroundCliWithoutProcessExit<T>(callback: () => Promise<T>): Promise<T> {
const exit = process.exit
process.exit = ((code?: string | number | null | undefined): never => {
const exitCode = typeof code === "number" ? code : 1
throw new PlaygroundCliExitError(exitCode)
}) as typeof process.exit

try {
return await callback()
} finally {
process.exit = exit
}
}

interface PlaygroundCliServer {
playground: {
run(options: { code: string } | { scriptPath: string }): Promise<PlaygroundRunResponse>
Expand Down Expand Up @@ -1290,7 +1313,7 @@ echo json_encode(array('command' => 'inspect-mounted-inputs', 'mounts' => $inspe
}

try {
const server = await runCLI({
const server = await runPlaygroundCliWithoutProcessExit(() => runCLI({
command: "server",
port: 0,
quiet: true,
Expand All @@ -1302,7 +1325,7 @@ echo json_encode(array('command' => 'inspect-mounted-inputs', 'mounts' => $inspe
wp: this.spec.environment.version,
"site-url": this.spec.preview?.siteUrl,
blueprint: playgroundBlueprint(this.spec.environment.blueprint, this.spec.policy, this.spec.preview?.siteUrl),
})
}))

if (!this.spec.preview?.port) {
return server
Expand Down
1 change: 1 addition & 0 deletions scripts/agent-sandbox-code-smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async function main() {
assert.match(code, /\\"agent_modes\\":\[\\"sandbox\\",\\"pipeline\\"\]/, "client context should report additive sandbox modes")
assert.match(code, /\\"tool_policy\\":\{\\"mode\\":\\"allow\\",\\"tools\\":\[.*\\"workspace_read\\"/, "sandbox agent should allow workspace tools")
assert.match(code, /datamachine_agent_mode_sandbox/, "sandbox mode should inject tool guidance")
assert.match(code, /datamachine_code_remote_workspace_backend_should_handle/, "sandbox mode should use the mounted workspace backend")
assert.match(code, /Do not invent alternate tool names such as read_file/, "sandbox guidance should prevent pseudo-tool aliases")
assert.match(code, /workspace_apply_patch/, "sandbox tool policy should include patch application")
}
Expand Down