This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
feat(harness): expose native Pi runtime #3391
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { RpcClient } from "@earendil-works/pi-coding-agent"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { createPiRpcClient } from "./rpc-client"; | ||
|
|
||
| describe("createPiRpcClient", () => { | ||
| it("does not put provider credentials in the child environment", () => { | ||
| const client = createPiRpcClient({ | ||
| cwd: "/workspace", | ||
| model: "claude-opus-4-8", | ||
| providerOptions: { apiKey: "token", region: "us" }, | ||
| }); | ||
|
|
||
| expect(client).toBeInstanceOf(RpcClient); | ||
| expect(client).toMatchObject({ | ||
| options: { | ||
| cwd: "/workspace", | ||
| model: "claude-opus-4-8", | ||
| provider: "posthog", | ||
| }, | ||
| }); | ||
| expect( | ||
| (client as unknown as { options: { env?: Record<string, string> } }) | ||
| .options.env, | ||
| ).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { type ChildProcess, spawn } from "node:child_process"; | ||
| import type { Writable } from "node:stream"; | ||
| import { StringDecoder } from "node:string_decoder"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { | ||
| RpcClient, | ||
| type RpcClientOptions, | ||
| } from "@earendil-works/pi-coding-agent"; | ||
| import type { PosthogProviderOptions } from "@posthog/harness/extensions/posthog-provider/provider"; | ||
| import { safePiEnvironment } from "./rpc-environment"; | ||
|
|
||
| export type PiRpcClient = RpcClient; | ||
|
|
||
| interface RpcClientInternals { | ||
| process?: ChildProcess; | ||
| stopReadingStdout?: () => void; | ||
| stderr: string; | ||
| exitError: Error | null; | ||
| handleLine(line: string): void; | ||
| createProcessExitError( | ||
| code: number | null, | ||
| signal: NodeJS.Signals | null, | ||
| ): Error; | ||
| rejectPendingRequests(error: Error): void; | ||
| } | ||
|
|
||
| function attachJsonlReader( | ||
| stream: NodeJS.ReadableStream, | ||
| onLine: (line: string) => void, | ||
| ): () => void { | ||
| const decoder = new StringDecoder("utf8"); | ||
| let buffer = ""; | ||
| const onData = (chunk: Buffer | string) => { | ||
| buffer += typeof chunk === "string" ? chunk : decoder.write(chunk); | ||
| let newlineIndex = buffer.indexOf("\n"); | ||
| while (newlineIndex !== -1) { | ||
| const line = buffer.slice(0, newlineIndex); | ||
| onLine(line.endsWith("\r") ? line.slice(0, -1) : line); | ||
| buffer = buffer.slice(newlineIndex + 1); | ||
| newlineIndex = buffer.indexOf("\n"); | ||
| } | ||
| }; | ||
| stream.on("data", onData); | ||
| return () => stream.off("data", onData); | ||
| } | ||
|
|
||
| class SecurePiRpcClient extends RpcClient { | ||
| constructor( | ||
| private readonly secureOptions: RpcClientOptions, | ||
| private readonly providerOptions?: PosthogProviderOptions, | ||
| ) { | ||
| super(secureOptions); | ||
| } | ||
|
|
||
| override async start(): Promise<void> { | ||
| const internals = this as unknown as RpcClientInternals; | ||
| if (internals.process) { | ||
| throw new Error("Pi RPC client is already started"); | ||
| } | ||
|
|
||
| internals.exitError = null; | ||
| const args = ["--mode", "rpc"]; | ||
| if (this.secureOptions.provider) { | ||
| args.push("--provider", this.secureOptions.provider); | ||
| } | ||
| if (this.secureOptions.model) { | ||
| args.push("--model", this.secureOptions.model); | ||
| } | ||
| if (this.secureOptions.args) { | ||
| args.push(...this.secureOptions.args); | ||
| } | ||
|
|
||
| const child = spawn( | ||
| process.execPath, | ||
| [this.secureOptions.cliPath ?? "dist/cli.js", ...args], | ||
| { | ||
| cwd: this.secureOptions.cwd, | ||
| env: safePiEnvironment(process.env), | ||
| stdio: ["pipe", "pipe", "pipe", "pipe"], | ||
| }, | ||
| ); | ||
| internals.process = child; | ||
|
|
||
| child.stderr?.on("data", (data: Buffer) => { | ||
| internals.stderr += data.toString(); | ||
| process.stderr.write(data); | ||
| }); | ||
| child.once("exit", (code, signal) => { | ||
| if (internals.process !== child) { | ||
| return; | ||
| } | ||
| const error = internals.createProcessExitError(code, signal); | ||
| internals.exitError = error; | ||
| internals.rejectPendingRequests(error); | ||
| }); | ||
| child.once("error", (error) => { | ||
| if (internals.process !== child) { | ||
| return; | ||
| } | ||
| const processError = new Error( | ||
| `Agent process error: ${error.message}. Stderr: ${internals.stderr}`, | ||
| ); | ||
| internals.exitError = processError; | ||
| internals.rejectPendingRequests(processError); | ||
| }); | ||
| child.stdin?.on("error", (error) => { | ||
| const stdinError = | ||
| internals.exitError ?? | ||
| new Error( | ||
| `Agent process stdin error: ${error.message}. Stderr: ${internals.stderr}`, | ||
| ); | ||
| internals.exitError = stdinError; | ||
| internals.rejectPendingRequests(stdinError); | ||
| }); | ||
| if (child.stdout) { | ||
| internals.stopReadingStdout = attachJsonlReader(child.stdout, (line) => | ||
| internals.handleLine(line), | ||
| ); | ||
| } | ||
|
|
||
| const bootstrapPipe = child.stdio[3] as Writable | null; | ||
| bootstrapPipe?.end( | ||
| JSON.stringify({ providerOptions: this.providerOptions }), | ||
| ); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||
| if (child.exitCode !== null) { | ||
| throw ( | ||
| internals.exitError ?? | ||
| internals.createProcessExitError(child.exitCode, child.signalCode) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export type PiRpcClientOptions = Pick<RpcClientOptions, "cwd" | "model"> & { | ||
| providerOptions?: PosthogProviderOptions; | ||
| }; | ||
|
|
||
| export function createPiRpcClient( | ||
| options: PiRpcClientOptions = {}, | ||
| ): PiRpcClient { | ||
| const { providerOptions, ...rpcOptions } = options; | ||
| return new SecurePiRpcClient( | ||
| { | ||
| ...rpcOptions, | ||
| cliPath: fileURLToPath(new URL("./rpc-host.js", import.meta.url)), | ||
| provider: "posthog", | ||
| }, | ||
| providerOptions, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| const SAFE_ENVIRONMENT_KEYS = [ | ||
| "APPDATA", | ||
| "COLORTERM", | ||
| "ComSpec", | ||
| "FORCE_COLOR", | ||
| "HOME", | ||
| "LANG", | ||
| "LC_ALL", | ||
| "LOCALAPPDATA", | ||
| "LOGNAME", | ||
| "NO_COLOR", | ||
| "PATH", | ||
| "PATHEXT", | ||
| "SHELL", | ||
| "SystemRoot", | ||
| "TEMP", | ||
| "TERM", | ||
| "TMP", | ||
| "TMPDIR", | ||
| "USER", | ||
| "USERPROFILE", | ||
| "WINDIR", | ||
| "XDG_CACHE_HOME", | ||
| "XDG_CONFIG_HOME", | ||
| "XDG_DATA_HOME", | ||
| "XDG_RUNTIME_DIR", | ||
| "XDG_STATE_HOME", | ||
| ] as const; | ||
|
|
||
| export function safePiEnvironment( | ||
| source: NodeJS.ProcessEnv, | ||
| ): Record<string, string> { | ||
| const environment: Record<string, string> = {}; | ||
| for (const key of SAFE_ENVIRONMENT_KEYS) { | ||
| const value = source[key]; | ||
| if (value !== undefined) { | ||
| environment[key] = value; | ||
| } | ||
| } | ||
| return environment; | ||
| } | ||
|
|
||
| export function sanitizePiHostEnvironment(): void { | ||
| const environment = safePiEnvironment(process.env); | ||
| for (const key of Object.keys(process.env)) { | ||
| delete process.env[key]; | ||
| } | ||
| Object.assign(process.env, environment); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would we need
ELECTRON_RUN_AS_NODE=1here?