-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.mjs
More file actions
239 lines (228 loc) · 12.9 KB
/
Copy pathrun.mjs
File metadata and controls
239 lines (228 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// run.mjs - Python wheel/sdist clean install and packed npm product integration gate.
import { generateKeyPairSync } from "node:crypto";
import { spawn } from "node:child_process";
import { createServer } from "node:http";
import { existsSync } from "node:fs";
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { delimiter, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { binPath, installPackedPyProc, ROOT, run } from "../packageHarness.mjs";
import { publishVerifiedEffectPack } from "../effectTransactionFixtures.mjs";
const TIMEOUT_MS = Number(process.env.PYPROC_GATE_TIMEOUT || 300000);
const PYTHON = process.env.PYPROC_PYTHON || "python";
const HERE = fileURLToPath(new URL(".", import.meta.url));
const approvalPair = generateKeyPairSync("ed25519");
let createInstalledApprovalGrant = null;
let publishInstalledEffectPack = null;
let committedEffects = 0;
function venvPython(directory) {
return process.platform === "win32" ? join(directory, "Scripts", "python.exe") : join(directory, "bin", "python");
}
function runAsync(command, args, { cwd, timeoutMs = TIMEOUT_MS, env = process.env } = {}) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { cwd, stdio: ["ignore", "pipe", "pipe"], env: { ...env } });
let stdout = "";
let stderr = "";
child.stdout.on("data", (chunk) => { stdout = (stdout + String(chunk)).slice(-16000); });
child.stderr.on("data", (chunk) => { stderr = (stderr + String(chunk)).slice(-16000); });
const timer = setTimeout(() => {
child.kill("SIGTERM");
reject(new Error(`${command} timed out\n${stderr}`));
}, timeoutMs);
child.once("error", (error) => { clearTimeout(timer); reject(error); });
child.once("exit", (code) => {
clearTimeout(timer);
if (code === 0) resolve({ stdout, stderr });
else reject(new Error(`${command} failed with ${code}\n${stdout}\n${stderr}`));
});
});
}
const frameBridge = await readFile(join(ROOT, "scripts", "automationSpace", "frameSpaceTarget.js"));
const targetServer = createServer((req, res) => {
if (req.url === "/effect" && req.method === "POST") {
committedEffects += 1;
res.writeHead(201, { "Content-Type": "application/json", "Cache-Control": "no-store" });
res.end(JSON.stringify({ committedEffects }));
return;
}
if (req.url === "/approval" && req.method === "POST") {
let body = "";
req.setEncoding("utf8");
req.on("data", (chunk) => { body = (body + chunk).slice(-1024 * 1024); });
req.on("end", () => {
try {
const input = JSON.parse(body);
const grant = createInstalledApprovalGrant({ intent: input.intent, authorityId: "operator:python-product",
trustDomainSha256: input.trustDomainSha256,
expiresAt: new Date(Date.now() + 60 * 60 * 1000).toISOString(), nonce: "nonce:python-product",
policyVersion: "python-product/1" }, approvalPair.privateKey);
res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-store" });
res.end(JSON.stringify(grant));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json", "Cache-Control": "no-store" });
res.end(JSON.stringify({ error: String(error?.message || error) }));
}
});
return;
}
if (req.url === "/effect-evidence" && req.method === "POST") {
let body = "";
req.setEncoding("utf8");
req.on("data", (chunk) => { body = (body + chunk).slice(-1024 * 1024); });
req.on("end", async () => {
try {
const publication = await publishInstalledEffectPack(JSON.parse(body));
res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-store" });
res.end(JSON.stringify({ outputDir: publication.outputDir }));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json", "Cache-Control": "no-store" });
res.end(JSON.stringify({ error: String(error?.message || error) }));
}
});
return;
}
if (req.url === "/frameSpaceTarget.js") {
res.writeHead(200, { "Content-Type": "text/javascript; charset=utf-8", "Cache-Control": "no-store" });
res.end(frameBridge);
return;
}
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(`<!doctype html><html><body><h1>python-sdk-ready</h1>
<button id="commit">Commit</button><output id="committed" role="status">not committed</output>
<script>document.getElementById("commit").addEventListener("click", async () => {
const response = await fetch("/effect", { method: "POST" });
document.getElementById("committed").textContent = response.ok ? "effect committed" : "effect failed";
});</script>
${req.url?.startsWith("/frame") ? '<script src="/frameSpaceTarget.js"></script>' : ""}</body></html>`);
});
await new Promise((resolve) => targetServer.listen(0, "127.0.0.1", resolve));
const targetOrigin = `http://127.0.0.1:${targetServer.address().port}`;
let passed = 0;
let failed = 0;
const check = (name, pass, info = "") => {
if (pass) { passed += 1; console.log(` PASS ${name}${info ? ` (${info})` : ""}`); }
else { failed += 1; console.log(` FAIL ${name}${info ? ` (${info})` : ""}`); }
};
const installed = await installPackedPyProc("pyprocPythonSdk-");
const distDir = join(installed.tmp, "pythonDist");
const wheelVenv = join(installed.tmp, "wheelVenv");
const sourceVenv = join(installed.tmp, "sourceVenv");
const configPath = join(installed.appDir, ".pyproc-python", "manifest.json");
const memoryRoot = join(installed.appDir, ".pyproc-python-memory");
const approvalKeyFile = join(memoryRoot, "approval-public.pem");
const frameConfigPath = join(installed.appDir, "pyproc-python-frame.json");
await mkdir(distDir, { recursive: true });
await mkdir(memoryRoot, { recursive: true });
await writeFile(approvalKeyFile, approvalPair.publicKey.export({ type: "spki", format: "pem" }));
const installedControlEntry = join(installed.appDir, "node_modules", "pyproc", "scripts", "controlProtocol",
"controlApi.js");
({ createApprovalGrant: createInstalledApprovalGrant } = await import(pathToFileURL(installedControlEntry).href));
const { createEvidencePack, publishEvidencePack } = await import(pathToFileURL(join(installed.appDir,
"node_modules", "pyproc", "scripts", "verification", "evidencePack.js")).href);
const pythonProjectIdentity = { workspaceId: "installed:python", commit: "fixture",
treeSha256: `sha256:${"1".repeat(64)}`, diffSha256: `sha256:${"2".repeat(64)}`, untracked: false };
publishInstalledEffectPack = (transaction) => publishVerifiedEffectPack({ createEvidencePack, publishEvidencePack,
repositoryRoot: memoryRoot, outputDir: "packs/effect-python-product", repository: pythonProjectIdentity,
projectId: "python-product", transaction });
const browser = process.env.PYPROC_BROWSER || undefined;
const mcpCli = binPath(installed.appDir, "pyproc-mcp");
run(mcpCli, ["init", "--recipe", "authorizedBrowser", "--project-root", installed.appDir,
"--out", ".pyproc-python", "--engine-root", join(ROOT, "src", "runtime", "engines", "wasi", "owned", "core"),
"--timeout-ms", String(TIMEOUT_MS), "--origin", targetOrigin, "--max-risk", "externalEffect",
"--purpose", "Python-SDK-product-gate", "--acknowledge-effects",
"--action", "snapshot", "--action", "screenshot",
"--artifact-max-bytes", String(8 * 1024 * 1024), "--artifact-total-bytes", String(16 * 1024 * 1024),
"--artifact-max-count", "8", "--artifact-inline-bytes", String(4 * 1024 * 1024),
"--artifact-ttl-ms", "120000", "--execution-memory-root", memoryRoot,
"--enable-effect-transactions", "--effect-approval-authority", `operator:python-product=${approvalKeyFile}`,
"--action", "click",
...(browser ? ["--browser", browser] : [])], { cwd: installed.appDir });
await writeFile(frameConfigPath, JSON.stringify({
schemaVersion: 1,
engine: { root: join(ROOT, "src", "runtime", "engines", "wasi", "owned", "core") },
timeoutMs: TIMEOUT_MS,
browser: {
enabled: true,
provider: "frame",
...(browser ? { executable: browser } : {}),
allowedOrigins: [targetOrigin],
maxRisk: "externalEffect",
actions: ["snapshot", "screenshot"],
methods: [],
externalEffects: "acknowledged",
purpose: "Python SDK FrameSpace product gate",
artifacts: { maxArtifactBytes: 8 * 1024 * 1024, maxTotalBytes: 16 * 1024 * 1024,
maxArtifacts: 8, inlineMaxBytes: 4 * 1024 * 1024, ttlMs: 120000 },
},
}, null, 2));
console.log("pyproc Python SDK product gate");
try {
run(PYTHON, ["-m", "build", join(ROOT, "pythonSdk"), "--outdir", distDir], { cwd: ROOT });
const distributions = await readdir(distDir);
const wheel = distributions.find((file) => file.endsWith(".whl"));
const source = distributions.find((file) => file.endsWith(".tar.gz"));
check("wheel과 source distribution을 격리 backend로 빌드",
!!wheel && !!source && distributions.length === 2, distributions.join(","));
run(PYTHON, ["-m", "venv", wheelVenv], { cwd: installed.tmp });
run(PYTHON, ["-m", "venv", sourceVenv], { cwd: installed.tmp });
const wheelPython = venvPython(wheelVenv);
const sourcePython = venvPython(sourceVenv);
if (!existsSync(wheelPython) || !existsSync(sourcePython)) throw new Error("clean virtual environment Python is missing");
run(wheelPython, ["-m", "pip", "install", "--disable-pip-version-check", "--no-deps", join(distDir, wheel)],
{ cwd: installed.tmp });
run(sourcePython, ["-m", "pip", "install", "--disable-pip-version-check", "--no-deps", join(distDir, source)],
{ cwd: installed.tmp });
const wheelMetadata = run(wheelPython, ["-m", "pip", "show", "pyproc-control"], { cwd: installed.tmp }).stdout;
const sourceMetadata = run(sourcePython, ["-m", "pip", "show", "pyproc-control"], { cwd: installed.tmp }).stdout;
check("서로 다른 clean venv에 wheel과 source distribution 설치",
wheelMetadata.includes("Version: 0.0.22") && sourceMetadata.includes("Version: 0.0.22"));
const protocol = run(sourcePython, [join(HERE, "protocolContract.py")], { cwd: installed.appDir });
check("source 설치본 Python codec과 transport outcome 음성 fixture", protocol.stdout.includes("22 fixtures"));
const productPath = join(installed.appDir, "node_modules", ".bin");
const journey = await runAsync(wheelPython, [join(HERE, "productJourney.py"), configPath,
`${targetOrigin}/product`, `${targetOrigin}/approval`, `${targetOrigin}/effect-evidence`], { cwd: installed.appDir,
env: { ...process.env, PATH: `${productPath}${delimiter}${process.env.PATH || ""}` } });
const report = JSON.parse(journey.stdout.trim().split(/\r?\n/).at(-1));
check("wheel 설치본이 Python, checkpoint, cancel, permission, screenshot 여정을 완주",
report.ok === true && report.operations === 34
&& Number.isSafeInteger(report.checkpoint) && report.checkpoint >= 0
&& report.attachmentBytes > 0 && report.cancelOutcome === "outcomeUnknown"
&& report.cancelTerminal === "outcomeUnknown" && report.timeoutOutcome === "outcomeUnknown"
&& report.timeoutTerminal === "outcomeUnknown" && report.permissionTerminal === "rejected"
&& report.successTerminal === "completed" && report.perceptionEntityRef?.startsWith("entity:")
&& report.situationRef?.startsWith("situation:") && report.executionMemory === true
&& report.effectTerminal === "confirmed" && report.effectSealed === true && committedEffects === 1,
JSON.stringify({
operations: report.operations,
checkpoint: report.checkpoint,
attachmentBytes: report.attachmentBytes,
cancelOutcome: report.cancelOutcome,
cancelTerminal: report.cancelTerminal,
timeoutOutcome: report.timeoutOutcome,
timeoutTerminal: report.timeoutTerminal,
permissionTerminal: report.permissionTerminal,
successTerminal: report.successTerminal,
perceptionEntityRef: report.perceptionEntityRef,
situationRef: report.situationRef,
executionMemory: report.executionMemory,
effectTerminal: report.effectTerminal,
effectSealed: report.effectSealed,
committedEffects,
}));
const frameJourney = await runAsync(wheelPython, [join(HERE, "frameJourney.py"), frameConfigPath,
`${targetOrigin}/frame`], { cwd: installed.appDir,
env: { ...process.env, PATH: `${productPath}${delimiter}${process.env.PATH || ""}` } });
const frameReport = JSON.parse(frameJourney.stdout.trim().split(/\r?\n/).at(-1));
check("wheel 설치본이 FrameSpace Python, 격리, screenshot 여정을 완주",
frameReport.ok === true && frameReport.operations === 17 && frameReport.attachmentBytes > 0
&& frameReport.perceptionEntityRef?.startsWith("entity:")
&& frameReport.situationRef?.startsWith("situation:"),
`${frameReport.attachmentBytes} bytes`);
} catch (error) {
check("Python SDK 제품 흐름 예외 없음", false, String(error?.stack || error).slice(-1200));
} finally {
targetServer.close();
await rm(installed.tmp, { recursive: true, force: true });
}
console.log(`\n결과: ${failed === 0 ? "GREEN" : "RED"} (${passed}/${passed + failed})`);
process.exit(failed === 0 ? 0 : 1);