Skip to content

Self-improving AI Employees for everyone

fix(ci): drain packaged runtime extraction output on Windows

trohitg730ee4717a3e
diff --git a/server/pi/tests/distribution.test.ts b/server/pi/tests/distribution.test.ts
index 82f56cdb..b7140967 100644
--- a/server/pi/tests/distribution.test.ts
+++ b/server/pi/tests/distribution.test.ts
@@ -1,35 +1,43 @@
-import {expect,test} from "bun:test";
+import {afterAll,beforeAll,describe,expect,test} from "bun:test";
import {createHash} from "node:crypto";
-import {readFile,mkdir,mkdtemp,realpath,rm} from "node:fs/promises";
+import {readFile,mkdtemp,realpath,rm} from "node:fs/promises";
import {dirname,join,resolve,sep} from "node:path";
import {tmpdir} from "node:os";
import {fileURLToPath} from "node:url";
-import {smokeHost} from "./process.test.ts";
+import {captureProcess,smokeHost,within} from "./process-helpers.ts";
const root=resolve(dirname(fileURLToPath(import.meta.url)),"..");
const manifestPath=join(root,"dist","runtime-manifest.json");
-const distributionTest=await Bun.file(manifestPath).exists() ? test : test.skip;
-distributionTest("production archive runs offline without source or root dependency resolution",async()=>{
- const manifest=JSON.parse(await readFile(manifestPath,"utf8"));
- const archive=join(root,"dist",manifest.archive);
- const bytes=await readFile(archive);
- expect(createHash("sha256").update(bytes).digest("hex")).toBe(manifest.sha256);
- expect(manifest.sdk_version).toBe("1.1.0");
- expect(manifest.universal).toBe(true);
- // Outside the repository so missing runtime packages cannot resolve from
- // server/pi/node_modules, root node_modules or any development workspace.
- const buildRoot=await realpath(tmpdir());
- const isolated=await realpath(await mkdtemp(join(buildRoot,"opencompany-pi-offline-")));
- try {
- const unpack=Bun.spawn(["tar","-xzf",archive,"-C",isolated],{stdout:"pipe",stderr:"pipe"});
- expect(await unpack.exited).toBe(0);
+describe.skipIf(!await Bun.file(manifestPath).exists())("production archive",()=>{
+ let manifest:{archive:string;sha256:string;sdk_version:string;universal:boolean;entrypoint:string;sdk_package_dir:string};
+ let buildRoot:string;let isolated:string|undefined;
+ // Cold Windows extraction and removal can dominate this large dependency
+ // tree. Keep fixture work separate from the bounded host startup contract.
+ beforeAll(async()=>{
+ manifest=JSON.parse(await readFile(manifestPath,"utf8"));
+ const archive=join(root,"dist",manifest.archive);
+ const bytes=await readFile(archive);
+ expect(createHash("sha256").update(bytes).digest("hex")).toBe(manifest.sha256);
+ expect(manifest.sdk_version).toBe("1.1.0");
+ expect(manifest.universal).toBe(true);
+ // Outside the repository so missing runtime packages cannot resolve from
+ // server/pi/node_modules, root node_modules or any development workspace.
+ buildRoot=await realpath(tmpdir());
+ isolated=await realpath(await mkdtemp(join(buildRoot,"opencompany-pi-offline-")));
+ const unpack=await captureProcess(["tar","-xzf",archive,"-C",isolated],"Offline runtime archive extraction",25000);
+ expect(unpack.exitCode,`Archive extraction failed: ${unpack.stderr}`).toBe(0);
for (const platform of ["darwin-arm64","darwin-x64","linux-x64","win32-x64"]) {
const binary=join(isolated,"node_modules","@esbuild",platform,platform.startsWith("win32-") ? "esbuild.exe" : "bin/esbuild");
expect(await Bun.file(binary).exists()).toBe(true);
}
- await smokeHost(join(isolated,manifest.entrypoint),{NODE_PATH:"",PI_PACKAGE_DIR:join(isolated,manifest.sdk_package_dir),PI_OFFLINE:"1"});
- } finally {
+ },30000);
+ afterAll(async()=>{
+ if(!isolated)return;
if (!resolve(isolated).startsWith(resolve(buildRoot)+sep)) throw new Error("Invalid offline test scratch path");
- await rm(isolated,{recursive:true,force:true});
- }
-},30000);
+ await within(rm(isolated,{recursive:true,force:true,maxRetries:3,retryDelay:100}),25000,"Offline runtime scratch cleanup");
+ },30000);
+ test("runs offline without source or root dependency resolution",async()=>{
+ if(!isolated)throw new Error("Offline runtime extraction did not create a fixture");
+ await smokeHost(join(isolated,manifest.entrypoint),{NODE_PATH:"",PI_PACKAGE_DIR:join(isolated,manifest.sdk_package_dir),PI_OFFLINE:"1"});
+ },15000);
+});
diff --git a/server/pi/tests/process-helpers.ts b/server/pi/tests/process-helpers.ts
new file mode 100644
index 00000000..d8a84632
--- /dev/null
+++ b/server/pi/tests/process-helpers.ts
@@ -0,0 +1,62 @@
+import {expect} from "bun:test";
+import {encodeFrame} from "../src/protocol.ts";
+import {message,run,toolResult,call} from "./fixtures.ts";
+
+export async function within<T>(promise:Promise<T>,milliseconds:number,phase:string):Promise<T> {
+ let timer:ReturnType<typeof setTimeout>|undefined;
+ try {return await Promise.race([promise,new Promise<never>((_resolve,reject)=>{
+ timer=setTimeout(()=>reject(new Error(`${phase} exceeded ${milliseconds} ms`)),milliseconds);
+ })]);} finally {if(timer)clearTimeout(timer);}
+}
+
+/** Drain both pipes immediately: awaiting exit first can block a verbose child. */
+export async function captureProcess(command:string[],phase:string,milliseconds:number):Promise<{exitCode:number;stdout:string;stderr:string}> {
+ const child=Bun.spawn(command,{stdin:"ignore",stdout:"pipe",stderr:"pipe"});
+ let stdout="";let stderr="";
+ const drain=async(stream:ReadableStream<Uint8Array>,append:(text:string)=>void)=>{
+ const decoder=new TextDecoder();
+ for await(const chunk of stream) append(decoder.decode(chunk,{stream:true}));
+ append(decoder.decode());
+ };
+ const drains=Promise.all([drain(child.stdout,text=>{stdout+=text;}),drain(child.stderr,text=>{stderr+=text;})]);
+ try {
+ const [exitCode]=await within(Promise.all([child.exited,drains]),milliseconds,phase);
+ return {exitCode,stdout,stderr};
+ } catch(error) {
+ throw new Error(`${phase}: ${error instanceof Error ? error.message : String(error)}; stderr: ${stderr.slice(-2000)}`);
+ } finally {
+ if(child.exitCode===null)child.kill("SIGKILL");
+ await within(Promise.allSettled([child.exited,drains]),5000,`${phase} process cleanup`);
+ }
+}
+
+export async function smokeHost(entrypoint:string,env:Record<string,string|undefined>={}):Promise<void> {
+ const child=Bun.spawn([process.execPath,entrypoint],{stdin:"pipe",stdout:"pipe",stderr:"pipe",env:{...process.env,...env}});
+ let buffer="";let ended=false;let stderr="";const observed:string[]=[];
+ const stderrDrain=(async()=>{const decoder=new TextDecoder();for await(const chunk of child.stderr)stderr+=decoder.decode(chunk,{stream:true});stderr+=decoder.decode();})();
+ const stdoutDrain=(async()=>{
+ const decoder=new TextDecoder();
+ for await(const chunk of child.stdout) {
+ buffer+=decoder.decode(chunk,{stream:true});let lf:number;
+ while((lf=buffer.indexOf("\n"))>=0) {
+ const event=JSON.parse(buffer.slice(0,lf));buffer=buffer.slice(lf+1);observed.push(event.type);
+ if(event.type==="operation") child.stdin.write(encodeFrame({type:"operation_result",id:event.id,
+ result:event.kind==="model"?{assistant_message:message("assistant","read",[call()])}:{message:toolResult()}}));
+ if(event.type==="turn_checkpoint") {expect(event.snapshot.entries.some((entry:{type:string;message?:{role:string}})=>entry.type==="message" && entry.message?.role==="toolResult")).toBe(true);ended=true;}
+ if(event.type==="error")throw new Error(`${event.code}: ${event.message}`);
+ }
+ }
+ })();
+ const drains=Promise.all([stdoutDrain,stderrDrain]);
+ try {
+ child.stdin.write(encodeFrame(run()));
+ const [exitCode]=await within(Promise.all([child.exited,drains]),10000,"Pi host checkpoint and exit");
+ expect(ended).toBe(true);expect(observed).toEqual(["operation","assistant_ready","operation","turn_checkpoint"]);
+ expect(exitCode).toBe(0);expect(stderr).toBe("");
+ } catch(error) {
+ throw new Error(`${error instanceof Error ? error.message : String(error)}; observed frames: ${observed.join(",") || "none"}; stderr: ${stderr.slice(-2000)}`);
+ } finally {
+ if(child.exitCode===null)child.kill("SIGKILL");
+ await within(Promise.allSettled([child.exited,drains]),5000,"Pi host process cleanup");
+ }
+}
diff --git a/server/pi/tests/process.test.ts b/server/pi/tests/process.test.ts
index a9f2af16..fbdaf9d6 100644
--- a/server/pi/tests/process.test.ts
+++ b/server/pi/tests/process.test.ts
@@ -6,7 +6,8 @@ import {ScopedProtocolBlobs} from "../src/transport.ts";
import {INLINE_LIMIT_BYTES,MAX_FRAME_BYTES} from "../src/types.ts";
import {mkdtemp,realpath,rm} from "node:fs/promises";
import {tmpdir} from "node:os";
-import {message,run,toolResult,call} from "./fixtures.ts";
+import {message,run} from "./fixtures.ts";
+import {captureProcess,smokeHost} from "./process-helpers.ts";
const source=join(dirname(fileURLToPath(import.meta.url)),"..","src","index.ts");
const fixtures=join(dirname(fileURLToPath(import.meta.url)),"runtime-fixtures");
@@ -50,24 +51,21 @@ test("bootstrap protocol failures still emit a bounded terminal error",async()=>
expect(await stderr).toBe("");
} finally {if(child.exitCode===null){child.kill();await child.exited;}}
},15000);
-export async function smokeHost(entrypoint:string,env:Record<string,string|undefined>={}):Promise<void> {
- const child=Bun.spawn([process.execPath,entrypoint],{stdin:"pipe",stdout:"pipe",stderr:"pipe",env:{...process.env,...env}});
- child.stdin.write(encodeFrame(run()));
- let buffer="";let ended=false;const observed:string[]=[];
- for await(const chunk of child.stdout) {
- buffer+=new TextDecoder().decode(chunk);
- let lf:number;
- while((lf=buffer.indexOf("\n"))>=0) {
- const event=JSON.parse(buffer.slice(0,lf));buffer=buffer.slice(lf+1);observed.push(event.type);
- if(event.type==="operation") child.stdin.write(encodeFrame({type:"operation_result",id:event.id,
- result:event.kind==="model"?{assistant_message:message("assistant","read",[call()])}:{message:toolResult()}}));
- if(event.type==="turn_checkpoint") {expect(event.snapshot.entries.some((entry:{type:string;message?:{role:string}})=>entry.type==="message" && entry.message?.role==="toolResult")).toBe(true);ended=true;}
- if(event.type==="error") throw new Error(`${event.code}: ${event.message}`);
- }
- }
- expect(ended).toBe(true);expect(observed).toEqual(["operation","assistant_ready","operation","turn_checkpoint"]);
- expect(await child.exited).toBe(0);expect(await new Response(child.stderr).text()).toBe("");
-}
+test("subprocess capture drains full stdout and stderr pipes before waiting for exit",async()=>{
+ const size=1024*1024;
+ const child=await captureProcess([process.execPath,"-e",`process.stdout.write("o".repeat(${size}));process.stderr.write("e".repeat(${size}));`],"Verbose archive command",10000);
+ expect(child.exitCode).toBe(0);expect(child.stdout).toBe("o".repeat(size));expect(child.stderr).toBe("e".repeat(size));
+},15000);
+test("subprocess capture reports its phase and reaps a child that exceeds its bound",async()=>{
+ let failure:Error|undefined;
+ try {
+ await captureProcess([process.execPath,"-e","process.stderr.write(`pid=${process.pid}`);setInterval(()=>{},1000);"],"Stalled archive command",2000);
+ } catch(error) {failure=error as Error;}
+ expect(failure?.message).toContain("Stalled archive command exceeded 2000 ms");
+ const pid=Number(failure?.message.match(/pid=(\d+)/)?.[1]);
+ expect(Number.isSafeInteger(pid) && pid>0).toBe(true);
+ expect(()=>process.kill(pid,0)).toThrow();
+},10000);
test("isolated JSONL host completes and exits without stdin EOF",()=>smokeHost(source),15000);
test("stdin EOF aborts and reaps a waiting SDK host",async()=>{
const child=Bun.spawn([process.execPath,source],{stdin:"pipe",stdout:"pipe",stderr:"pipe"});