import * as fs from "node:fs"; import * as path from "node:path"; import { fileURLToPath } from "node:url"; import { createJiti } from "jiti"; import * as piCore from "@earendil-works/pi-agent-core"; import * as piAi from "@earendil-works/pi-ai/compat"; import * as piTui from "@earendil-works/pi-tui"; import * as sdk from "@selesai/code"; import * as typebox from "typebox"; import * as typeboxCompile from "typebox/compile"; import * as typeboxValue from "typebox/value"; import { installRunnerHttpDispatcher } from "./runner-http-dispatcher.ts"; import { runConfiguredSubagent, validateSubagentRunConfig } from "./subagent-runner-bootstrap.ts"; import { getAgentDir } from "../../shared/utils.ts"; /** * Pi's extension loader supplies the embedded SDK. Bare Bun/Node cannot replace * it: an apparent import success can be an auto-installed, different SDK. * Await the configured runner inside extension initialization so the outer Pi * session/RPC loop never starts. Exit only after shared disposal/lease release. * Derived from xz-dev's PR #2049, commit 910807bfefcf9ee41d73fa25ec86dcd75ab8f4b2. */ export default async function runBinaryBootstrap(): Promise { const configPath = process.env.SELESAI_SUBAGENT_RUNNER_CONFIG; delete process.env.SELESAI_SUBAGENT_RUNNER_CONFIG; try { if (!configPath || !path.isAbsolute(configPath)) throw new Error("Missing absolute SELESAI_SUBAGENT_RUNNER_CONFIG path"); const rawConfig: unknown = JSON.parse(fs.readFileSync(configPath, "utf8")); validateSubagentRunConfig(rawConfig); const config = rawConfig; try { fs.unlinkSync(configPath); } catch { // Temp-config cleanup is best effort, as in the Node entrypoint. } // Pi applies httpIdleTimeoutMs to its dispatcher only after extension // factories return; this factory never does, so install the runner's own. installRunnerHttpDispatcher({ agentDir: getAgentDir(), cwd: process.cwd() }); await runConfiguredSubagent(config, { loadPiCodingAgent: async () => sdk, loadExecutionModule: async () => { // Native Bun imports bypass Pi's Jiti virtual peers in the compiled host. const jiti = createJiti(import.meta.url, { tryNative: false, moduleCache: false, virtualModules: { "@earendil-works/pi-agent-core": piCore, "@earendil-works/pi-ai": piAi, "@earendil-works/pi-ai/compat": piAi, "@earendil-works/pi-tui": piTui, "@selesai/code": sdk, typebox, "typebox/compile": typeboxCompile, "typebox/value": typeboxValue, }, }); const runner = `./subagent-runner${path.extname(fileURLToPath(import.meta.url))}`; return jiti.import(runner); }, }); process.exit(0); } catch (error) { console.error("Subagent binary runner error:", error); process.exit(1); } }