@@ -30,6 +30,7 @@ import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";
3030import { validReview , writeReviewArtifacts } from "./pr-review-artifact-fixture.js" ;
3131
3232const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
33+ const freshMainTemplateDirs = useAutoCleanupTempDirTracker ( afterAll ) ;
3334const escapedPipeHolderPidFiles = new Set < string > ( ) ;
3435afterEach ( async ( ) => {
3536 const failures : unknown [ ] = [ ] ;
@@ -55,6 +56,7 @@ const lockRef = "refs/openclaw/pr-operation-locks/42";
5556const detachedChildren = new WeakSet < ChildProcess > ( ) ;
5657const goneProcessGroups = new Set < number > ( ) ;
5758let templateRepo = "" ;
59+ let freshMainTemplate : ReturnType < typeof createFreshMainTemplate > | undefined ;
5860
5961// Direct preload affects only the supervisor; operation fixtures keep real clocks.
6062// The source assertions below pin the production safety durations being accelerated.
@@ -278,6 +280,67 @@ function installPrCliFixture(repoDir: string, env?: NodeJS.ProcessEnv) {
278280 return { binDir, cli } ;
279281}
280282
283+ function createFreshMainTemplate ( ) {
284+ // Freeze only the committed wrapper/tool prefix. Origins, FETCH_HEAD,
285+ // linked worktrees, and failure proxies are created in each private copy.
286+ const repoDir = freshMainTemplateDirs . make ( "openclaw-pr-fresh-main-template-" ) ;
287+ cpSync ( templateRepo , repoDir , { recursive : true } ) ;
288+ const stateDir = join ( repoDir , "fixture-state" ) ;
289+ const homeDir = join ( stateDir , "home" ) ;
290+ const tmpDir = join ( stateDir , "tmp" ) ;
291+ mkdirSync ( homeDir , { recursive : true } ) ;
292+ mkdirSync ( tmpDir , { recursive : true } ) ;
293+ const setupEnv = createPrFixtureEnv ( homeDir , process . env . PATH ?? "" ) ;
294+ const { binDir } = installPrCliFixture ( repoDir , setupEnv ) ;
295+ const realGit = realpathSync ( join ( binDir , "git" ) ) ;
296+ // Only these real tools are reachable. rg/pnpm are required by the CLI
297+ // preflight; these cases must stop before invoking either of them.
298+ for ( const command of [
299+ "awk" ,
300+ "cat" ,
301+ "date" ,
302+ "env" ,
303+ "jq" ,
304+ "mkdir" ,
305+ "mktemp" ,
306+ "mv" ,
307+ "ps" ,
308+ "rm" ,
309+ "seq" ,
310+ "sh" ,
311+ "sleep" ,
312+ ] ) {
313+ symlinkSync (
314+ execFileSync ( "which" , [ command ] , { encoding : "utf8" , env : setupEnv } ) . trim ( ) ,
315+ join ( binDir , command ) ,
316+ ) ;
317+ }
318+ symlinkSync ( process . execPath , join ( binDir , "node" ) ) ;
319+ for ( const command of [ "rg" , "pnpm" ] ) {
320+ const stub = writeFixtureFile ( binDir , command , [
321+ "#!/bin/sh" ,
322+ "echo 'unexpected command in review-init fixture' >&2" ,
323+ "exit 99" ,
324+ ] ) ;
325+ chmodSync ( stub , 0o755 ) ;
326+ }
327+ const env : NodeJS . ProcessEnv = {
328+ ...createPrFixtureEnv ( homeDir , binDir ) ,
329+ TMPDIR : tmpDir ,
330+ } ;
331+ const git = ( ...args : string [ ] ) =>
332+ execFileSync ( realGit , args , { cwd : repoDir , env, encoding : "utf8" , timeout : 5000 } ) . trim ( ) ;
333+ writeFileSync (
334+ join ( repoDir , ".git/info/exclude" ) ,
335+ "/.local/\n/.worktrees/\n/isolated-bin/\n/fixture-state/\n" ,
336+ ) ;
337+ git ( "add" , "scripts" , ".github" ) ;
338+ git ( "commit" , "-qm" , "test: unmodified public PR wrapper fixture" ) ;
339+ const cachedMain = git ( "rev-parse" , "HEAD" ) ;
340+ const canonicalTree = git ( "rev-parse" , "HEAD^{tree}" ) ;
341+ return { repoDir, cachedMain, canonicalTree } ;
342+ }
343+
281344function installRequiredPrCommandStubs ( binDir : string ) {
282345 for ( const command of [ "gh" , "jq" , "pnpm" , "rg" ] ) {
283346 const stub = join ( binDir , command ) ;
@@ -738,60 +801,22 @@ describePosix("scripts/pr per-PR operation lock", () => {
738801 ] ) (
739802 "$command requires fresh main before replacing PR artifacts ($failure, existing=$existing)" ,
740803 async ( { command, failure, existing } ) => {
741- const repoDir = createRepo ( ) ;
804+ const template = ( freshMainTemplate ??= createFreshMainTemplate ( ) ) ;
805+ const repoDir = tempDirs . make ( "openclaw-pr-fresh-main-" ) ;
806+ cpSync ( template . repoDir , repoDir , { recursive : true } ) ;
807+ const { cachedMain, canonicalTree } = template ;
742808 const stateDir = join ( repoDir , "fixture-state" ) ;
743809 const homeDir = join ( stateDir , "home" ) ;
744810 const tmpDir = join ( stateDir , "tmp" ) ;
745- mkdirSync ( homeDir , { recursive : true } ) ;
746- mkdirSync ( tmpDir , { recursive : true } ) ;
747- const setupEnv = createPrFixtureEnv ( homeDir , process . env . PATH ?? "" ) ;
748- const { binDir, cli } = installPrCliFixture ( repoDir , setupEnv ) ;
811+ const binDir = join ( repoDir , "isolated-bin" ) ;
812+ const cli = join ( repoDir , "scripts/pr" ) ;
749813 const realGit = realpathSync ( join ( binDir , "git" ) ) ;
750- // Only these real tools are reachable. rg/pnpm are required by the CLI
751- // preflight; these cases must stop before invoking either of them.
752- for ( const command of [
753- "awk" ,
754- "cat" ,
755- "date" ,
756- "env" ,
757- "jq" ,
758- "mkdir" ,
759- "mktemp" ,
760- "mv" ,
761- "ps" ,
762- "rm" ,
763- "seq" ,
764- "sh" ,
765- "sleep" ,
766- ] ) {
767- symlinkSync (
768- execFileSync ( "which" , [ command ] , { encoding : "utf8" , env : setupEnv } ) . trim ( ) ,
769- join ( binDir , command ) ,
770- ) ;
771- }
772- symlinkSync ( process . execPath , join ( binDir , "node" ) ) ;
773- for ( const command of [ "rg" , "pnpm" ] ) {
774- const stub = writeFixtureFile ( binDir , command , [
775- "#!/bin/sh" ,
776- "echo 'unexpected command in review-init fixture' >&2" ,
777- "exit 99" ,
778- ] ) ;
779- chmodSync ( stub , 0o755 ) ;
780- }
781814 const env : NodeJS . ProcessEnv = {
782815 ...createPrFixtureEnv ( homeDir , binDir ) ,
783816 TMPDIR : tmpDir ,
784817 } ;
785818 const git = ( ...args : string [ ] ) =>
786819 execFileSync ( realGit , args , { cwd : repoDir , env, encoding : "utf8" , timeout : 5000 } ) . trim ( ) ;
787- writeFileSync (
788- join ( repoDir , ".git/info/exclude" ) ,
789- "/.local/\n/.worktrees/\n/isolated-bin/\n/fixture-state/\n" ,
790- ) ;
791- git ( "add" , "scripts" , ".github" ) ;
792- git ( "commit" , "-qm" , "test: unmodified public PR wrapper fixture" ) ;
793- const cachedMain = git ( "rev-parse" , "HEAD" ) ;
794- const canonicalTree = git ( "rev-parse" , "HEAD^{tree}" ) ;
795820 const newCommit = ( message : string ) =>
796821 execFileSync ( realGit , [ "commit-tree" , canonicalTree , "-p" , cachedMain ] , {
797822 cwd : repoDir ,
0 commit comments