@@ -17,22 +17,39 @@ const sources = {
1717 ) ?. [ 1 ] ,
1818}
1919
20- function presence ( ) {
21- const names = new Set ( Object . keys ( process . env ) . map ( ( key ) => key . toUpperCase ( ) ) )
20+ function presence ( env = process . env ) {
21+ const names = new Set ( Object . keys ( env ) . map ( ( key ) => key . toUpperCase ( ) ) )
2222 return {
2323 ...Object . fromEntries (
2424 [ "SYSTEMROOT" , "WINDIR" , "PSMODULEPATH" , "USERPROFILE" , "TEMP" , "TMP" , "PATH" , "LOCALAPPDATA" , "COMSPEC" ] . map (
2525 ( key ) => [ key , names . has ( key ) ] ,
2626 ) ,
2727 ) ,
28- SystemRootLookup : Boolean ( process . env . SystemRoot ) ,
29- SYSTEMROOTLookup : Boolean ( process . env . SYSTEMROOT ) ,
28+ SystemRootLookup : Boolean ( env . SystemRoot ) ,
29+ SYSTEMROOTLookup : Boolean ( env . SYSTEMROOT ) ,
3030 }
3131}
3232
33- function probe ( stage ) {
33+ function probe ( stage , restore = "none" ) {
3434 const script = sources [ stage ]
3535 if ( ! script ) throw new Error ( "Unknown ACL diagnostic stage" )
36+ const choices = {
37+ none : [ ] ,
38+ PSModulePath : [ "PSModulePath" ] ,
39+ LOCALAPPDATA : [ "LOCALAPPDATA" ] ,
40+ both : [ "PSModulePath" , "LOCALAPPDATA" ] ,
41+ }
42+ if ( ! Object . hasOwn ( choices , restore ) ) throw new Error ( "Unknown ACL environment factor" )
43+ const env = { ...process . env }
44+ if ( restore !== "none" ) {
45+ const snapshot = JSON . parse (
46+ readFileSync ( path . join ( process . env . RUNNER_TEMP , "opencode-acl-environment.json" ) , "utf8" ) ,
47+ )
48+ for ( const key of choices [ restore ] ) {
49+ if ( typeof snapshot [ key ] !== "string" || ! snapshot [ key ] ) throw new Error ( "ACL environment factor unavailable" )
50+ env [ key ] = snapshot [ key ]
51+ }
52+ }
3653 const dir = mkdtempSync ( path . join ( tmpdir ( ) , "opencode-acl-transport-" ) )
3754 const file = path . join ( dir , "fixture [key]'s.key" )
3855 writeFileSync ( file , "diagnostic-placeholder\n" )
@@ -43,7 +60,7 @@ function probe(stage) {
4360 [ "-NoLogo" , "-NoProfile" , "-NonInteractive" , "-Command" , script ] ,
4461 {
4562 env : {
46- ...process . env ,
63+ ...env ,
4764 OPENCODE_COR_LOCAL_TEST_PATH : file ,
4865 OPENCODE_COR_LOCAL_TEST_PUBLIC : "0" ,
4966 OPENCODE_COR_LOCAL_ACL_PATH : file ,
@@ -59,7 +76,8 @@ function probe(stage) {
5976 return {
6077 stage,
6178 runtime : process . versions . bun ? `Bun ${ process . versions . bun } ` : `Node ${ process . versions . node } ` ,
62- envPresence : presence ( ) ,
79+ restore,
80+ envPresence : presence ( env ) ,
6381 scriptSha256 : createHash ( "sha256" ) . update ( script ) . digest ( "hex" ) ,
6482 status : result . status ,
6583 signal : result . signal ,
@@ -73,9 +91,9 @@ function probe(stage) {
7391 }
7492}
7593
76- export function diagnose ( stage , transport ) {
77- if ( transport === "bun" ) return probe ( stage )
78- const result = spawnSync ( "node" , [ fileURLToPath ( import . meta. url ) , stage ] , {
94+ export function diagnose ( stage , transport , restore = "none" ) {
95+ if ( transport === "bun" ) return probe ( stage , restore )
96+ const result = spawnSync ( "node" , [ fileURLToPath ( import . meta. url ) , stage , restore ] , {
7997 env : process . env ,
8098 encoding : "utf8" ,
8199 stdio : [ "ignore" , "pipe" , "pipe" ] ,
@@ -88,6 +106,7 @@ export function diagnose(stage, transport) {
88106 return {
89107 stage,
90108 runtime : "Node child did not report" ,
109+ restore,
91110 envPresence : presence ( ) ,
92111 status : result . status ,
93112 signal : result . signal ,
@@ -100,5 +119,5 @@ export function diagnose(stage, transport) {
100119if ( process . argv [ 1 ] && path . resolve ( process . argv [ 1 ] ) === fileURLToPath ( import . meta. url ) ) {
101120 if ( process . platform !== "win32" || process . versions . node . split ( "." ) [ 0 ] !== "24" )
102121 throw new Error ( "ACL diagnostic child requires Windows Node 24" )
103- console . log ( JSON . stringify ( probe ( process . argv [ 2 ] ) ) )
122+ console . log ( JSON . stringify ( probe ( process . argv [ 2 ] , process . argv [ 3 ] ) ) )
104123}
0 commit comments