Skip to content

Commit e8bb2db

Browse files
committed
test(guardrails): Windows環境を群で切り分ける
2変数の復元では原因を説明できなかったため、既知の非秘密Windows環境群を一度戻してから適応的に最小集合へ絞る。Get-Aclは最大30回・各5秒・全体170秒で制限し、全群復元も失敗する場合はそこで停止する。 値はowner-onlyの一時JSONに限定し、ログは既知の存在有無・選択名・終了結果だけとする。production、実ACL制約、full unitのstrict実行は変更しない。 Refs: #366 Agent-Lane: codex
1 parent 7cc7d1b commit e8bb2db

3 files changed

Lines changed: 170 additions & 16 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ jobs:
131131
if: runner.os == 'Windows' && steps.acl-snapshot.outcome == 'success'
132132
continue-on-error: true
133133
run: |
134-
node -e 'require("node:fs").writeFileSync(require("node:path").join(process.env.RUNNER_TEMP, "opencode-acl-environment.json"), JSON.stringify({PSModulePath: process.env.PSModulePath, LOCALAPPDATA: process.env.LOCALAPPDATA}), {flag:"r+"})'
134+
node --input-type=module -e 'import {captureEnvironment} from "./packages/guardrails/bin/windows-acl-transport.mjs"; captureEnvironment()'
135135
136136
- name: Diagnose ACL transport directly
137137
if: runner.os == 'Windows'
@@ -149,10 +149,11 @@ jobs:
149149
continue-on-error: true
150150
run: GITHUB_ACTIONS=false bun turbo test --env-mode=loose --filter=@opencode-ai/guardrails -- --test-name-pattern 'Windows ACL transport'
151151

152-
- name: Diagnose ACL environment factors under strict mode
152+
- name: Diagnose ACL environment adaptively under strict mode
153153
if: runner.os == 'Windows' && steps.acl-environment.outcome == 'success'
154154
continue-on-error: true
155-
run: GITHUB_ACTIONS=false bun turbo test --filter=@opencode-ai/guardrails -- bin/windows-acl-environment.test.ts --test-name-pattern 'Windows ACL environment factor'
155+
timeout-minutes: 3
156+
run: GITHUB_ACTIONS=false bun turbo test --filter=@opencode-ai/guardrails -- bin/windows-acl-adaptive.test.ts --test-name-pattern 'Windows ACL adaptive probe'
156157

157158
- name: Remove private ACL diagnostic snapshot
158159
if: always() && runner.os == 'Windows'
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Temporary bounded ddmin experiment. Normal test directories do not include this file.
2+
import { expect, test } from "bun:test"
3+
import { diagnose, snapshotNames } from "./windows-acl-transport.mjs"
4+
5+
const all = process.platform === "win32" ? snapshotNames() : []
6+
const deadline = performance.now() + 170_000
7+
let current = all
8+
let granularity = 2
9+
let phase = "full"
10+
let queue: string[][] = []
11+
let probes = 0
12+
let withoutFailed = false
13+
const confirmations: boolean[] = []
14+
15+
function complements() {
16+
const width = Math.ceil(current.length / granularity)
17+
queue = Array.from({ length: Math.ceil(current.length / width) }, (_, index) =>
18+
current.filter((_, position) => position < index * width || position >= (index + 1) * width),
19+
)
20+
}
21+
22+
function next() {
23+
if (phase === "full") return { keys: all, stage: "Get-Acl", transport: "bun" }
24+
if (phase === "search") {
25+
if (current.length === 1) phase = "without"
26+
else if (!queue.length) complements()
27+
if (phase === "search") {
28+
const keys = queue.shift()
29+
if (!keys) {
30+
phase = "no-candidate"
31+
return
32+
}
33+
return { keys, stage: "Get-Acl", transport: "bun" }
34+
}
35+
}
36+
if (phase === "without")
37+
return { keys: all.filter((key) => !current.includes(key)), stage: "Get-Acl", transport: "bun" }
38+
if (phase === "confirm") {
39+
const index = confirmations.length
40+
return { keys: current, stage: index % 2 ? "Get-Acl" : "Set-Acl", transport: index < 2 ? "bun" : "node24" }
41+
}
42+
}
43+
44+
function advance(keys: string[], ok: boolean) {
45+
if (phase === "full") {
46+
phase = ok ? "search" : "full-group-failed"
47+
return
48+
}
49+
if (phase === "search") {
50+
if (ok) {
51+
current = keys
52+
granularity = Math.max(2, granularity - 1)
53+
queue = []
54+
if (!current.length) phase = "empty-group-succeeded"
55+
return
56+
}
57+
if (queue.length) return
58+
if (granularity >= current.length) phase = "without"
59+
else granularity = Math.min(current.length, granularity * 2)
60+
return
61+
}
62+
if (phase === "without") {
63+
withoutFailed = !ok
64+
phase = "confirm"
65+
return
66+
}
67+
if (phase === "confirm") {
68+
confirmations.push(ok)
69+
if (confirmations.length === 4) phase = "done"
70+
}
71+
}
72+
73+
for (let slot = 0; slot < 30; slot++) {
74+
test.skipIf(process.platform !== "win32")("Windows ACL adaptive probe " + slot, () => {
75+
if (performance.now() >= deadline || (phase === "search" && probes >= 24)) phase = "budget-exhausted"
76+
const plan = next()
77+
if (plan) {
78+
const result = diagnose(plan.stage, plan.transport, plan.keys)
79+
probes++
80+
console.log("ACL_ADAPTIVE " + JSON.stringify({ phase, transport: plan.transport, ...result }))
81+
const ok = !result.errorCode && result.status === 0
82+
advance(plan.keys, ok)
83+
expect(result.status).toBe(0)
84+
}
85+
if (slot === 29)
86+
console.log(
87+
"ACL_ADAPTIVE_SUMMARY " +
88+
JSON.stringify({ phase, probes, unusedSlots: 30 - probes, candidate: current, withoutFailed, confirmations }),
89+
)
90+
})
91+
}

‎packages/guardrails/bin/windows-acl-transport.mjs‎

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,72 @@ import path from "node:path"
77
import { fileURLToPath } from "node:url"
88

99
const here = path.dirname(fileURLToPath(import.meta.url))
10+
export const DIAGNOSTIC_ENV_KEYS = [
11+
"PSExecutionPolicyPreference",
12+
"PSModuleAnalysisCachePath",
13+
"PSModulePath",
14+
"PSHOME",
15+
"PSDisableModuleAnalysisCacheCleanup",
16+
"__PSLockdownPolicy",
17+
"APPDATA",
18+
"LOCALAPPDATA",
19+
"ProgramData",
20+
"ProgramFiles",
21+
"ProgramFiles(x86)",
22+
"ProgramW6432",
23+
"CommonProgramFiles",
24+
"CommonProgramFiles(x86)",
25+
"CommonProgramW6432",
26+
"USERDOMAIN",
27+
"USERDOMAIN_ROAMINGPROFILE",
28+
"USERDNSDOMAIN",
29+
"USERNAME",
30+
"LOGONSERVER",
31+
"COMPUTERNAME",
32+
"ALLUSERSPROFILE",
33+
"PUBLIC",
34+
"OS",
35+
"PROCESSOR_ARCHITECTURE",
36+
"PROCESSOR_IDENTIFIER",
37+
"PROCESSOR_LEVEL",
38+
"PROCESSOR_REVISION",
39+
"NUMBER_OF_PROCESSORS",
40+
"SystemRoot",
41+
"WINDIR",
42+
"SYSTEMDRIVE",
43+
"HOMEDRIVE",
44+
"HOMEPATH",
45+
"USERPROFILE",
46+
"HOME",
47+
"USER",
48+
"TEMP",
49+
"TMP",
50+
"PATH",
51+
"COMSPEC",
52+
"PATHEXT",
53+
"SESSIONNAME",
54+
]
55+
56+
function snapshotPath() {
57+
return path.join(process.env.RUNNER_TEMP, "opencode-acl-environment.json")
58+
}
59+
60+
export function captureEnvironment() {
61+
writeFileSync(
62+
snapshotPath(),
63+
JSON.stringify(
64+
Object.fromEntries(DIAGNOSTIC_ENV_KEYS.filter((key) => process.env[key]).map((key) => [key, process.env[key]])),
65+
),
66+
{ flag: "r+" },
67+
)
68+
}
69+
70+
export function snapshotNames() {
71+
return Object.keys(JSON.parse(readFileSync(snapshotPath(), "utf8"))).filter((key) =>
72+
DIAGNOSTIC_ENV_KEYS.includes(key),
73+
)
74+
}
75+
1076
const sources = {
1177
"exit 0": "exit 0",
1278
"Set-Acl": readFileSync(path.join(here, "../profile/src/cor-local-bin-defaults.test.ts"), "utf8").match(
@@ -20,11 +86,7 @@ const sources = {
2086
function presence(env = process.env) {
2187
const names = new Set(Object.keys(env).map((key) => key.toUpperCase()))
2288
return {
23-
...Object.fromEntries(
24-
["SYSTEMROOT", "WINDIR", "PSMODULEPATH", "USERPROFILE", "TEMP", "TMP", "PATH", "LOCALAPPDATA", "COMSPEC"].map(
25-
(key) => [key, names.has(key)],
26-
),
27-
),
89+
...Object.fromEntries(DIAGNOSTIC_ENV_KEYS.map((key) => [key.toUpperCase(), names.has(key.toUpperCase())])),
2890
SystemRootLookup: Boolean(env.SystemRoot),
2991
SYSTEMROOTLookup: Boolean(env.SYSTEMROOT),
3092
}
@@ -39,13 +101,13 @@ function probe(stage, restore = "none") {
39101
LOCALAPPDATA: ["LOCALAPPDATA"],
40102
both: ["PSModulePath", "LOCALAPPDATA"],
41103
}
42-
if (!Object.hasOwn(choices, restore)) throw new Error("Unknown ACL environment factor")
104+
const keys = Array.isArray(restore) ? restore : Object.hasOwn(choices, restore) ? choices[restore] : undefined
105+
if (!keys || !keys.every((key) => DIAGNOSTIC_ENV_KEYS.includes(key)))
106+
throw new Error("Unknown ACL environment factor")
43107
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]) {
108+
if (keys.length) {
109+
const snapshot = JSON.parse(readFileSync(snapshotPath(), "utf8"))
110+
for (const key of keys) {
49111
if (typeof snapshot[key] !== "string" || !snapshot[key]) throw new Error("ACL environment factor unavailable")
50112
env[key] = snapshot[key]
51113
}
@@ -93,7 +155,7 @@ function probe(stage, restore = "none") {
93155

94156
export function diagnose(stage, transport, restore = "none") {
95157
if (transport === "bun") return probe(stage, restore)
96-
const result = spawnSync("node", [fileURLToPath(import.meta.url), stage, restore], {
158+
const result = spawnSync("node", [fileURLToPath(import.meta.url), stage, JSON.stringify(restore)], {
97159
env: process.env,
98160
encoding: "utf8",
99161
stdio: ["ignore", "pipe", "pipe"],
@@ -119,5 +181,5 @@ export function diagnose(stage, transport, restore = "none") {
119181
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
120182
if (process.platform !== "win32" || process.versions.node.split(".")[0] !== "24")
121183
throw new Error("ACL diagnostic child requires Windows Node 24")
122-
console.log(JSON.stringify(probe(process.argv[2], process.argv[3])))
184+
console.log(JSON.stringify(probe(process.argv[2], process.argv[3] ? JSON.parse(process.argv[3]) : "none")))
123185
}

0 commit comments

Comments
 (0)