repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

perf(modernize): resolve installed Pi without spawning

67357a22faf33af5b219bb96ff6f25cfad444432

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (5)

.pi/skills/modernize/scripts/audit.bench.mjs

Added
new file mode 100644
1 +#!/usr/bin/env node
3 +import { spawnSync } from "node:child_process";
4 +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
5 +import { tmpdir } from "node:os";
6 +import path from "node:path";
7 +import { performance } from "node:perf_hooks";
8 +import { fileURLToPath, pathToFileURL } from "node:url";
9 +import { executableOnPath, resolvePiInstallation } from "./audit.mjs";
11 +const RUNS = 20;
12 +const WARMUPS = 3;
13 +const script = fileURLToPath(import.meta.url);
15 +export function checkedSpawn(command, args, options = {}) {
16 + const result = spawnSync(command, args, {
17 + encoding: "utf8",
18 + windowsHide: true,
19 + ...options,
20 + });
21 + if (result.error) throw result.error;
22 + if (result.status !== 0) {
23 + const outcome = result.signal
24 + ? `signal ${result.signal}`
25 + : `code ${result.status}`;
26 + const detail = (result.stderr || result.stdout || "").trim();
27 + throw new Error(
28 + `${command} exited with ${outcome}${detail ? `: ${detail}` : ""}`,
29 + );
30 + }
31 + return result;
34 +function installation() {
35 + return resolvePiInstallation(executableOnPath("pi"));
38 +function spawnedVersion() {
39 + const { cli } = installation();
40 + return checkedSpawn(process.execPath, [cli, "--version"]).stdout.trim();
43 +function manifestVersion() {
44 + return installation().version;
47 +function command(mode) {
48 + return [process.execPath, script, mode]
49 + .map((argument) => JSON.stringify(argument.replaceAll("\\", "/")))
50 + .join(" ");
53 +function writeBenchstat(pathname, result) {
54 + const lines = [
55 + `goos: ${process.platform}`,
56 + `goarch: ${process.arch}`,
57 + "pkg: pi-ext/modernize",
58 + ...result.times.map(
59 + (seconds) =>
60 + `BenchmarkLocalPiVersion 1 ${Math.round(seconds * 1e9)} ns/op`,
61 + ),
62 + ];
63 + writeFileSync(pathname, `${lines.join("\n")}\n`);
66 +function trace() {
67 + const samples = [];
68 + for (let index = 0; index < 10; index++) {
69 + const started = performance.now();
70 + const executable = executableOnPath("pi");
71 + const foundExecutable = performance.now();
72 + const { cli } = resolvePiInstallation(executable);
73 + const resolvedPackage = performance.now();
74 + checkedSpawn(process.execPath, [cli, "--version"]);
75 + const readVersion = performance.now();
76 + samples.push({
77 + executable: foundExecutable - started,
78 + package: resolvedPackage - foundExecutable,
79 + spawn: readVersion - resolvedPackage,
80 + });
81 + }
82 + for (const key of ["executable", "package", "spawn"]) {
83 + const mean =
84 + samples.reduce((total, sample) => total + sample[key], 0) /
85 + samples.length;
86 + console.log(`${key}: ${mean.toFixed(2)} ms mean`);
87 + }
90 +function compare() {
91 + const baselineVersion = spawnedVersion();
92 + const candidateVersion = manifestVersion();
93 + if (baselineVersion !== candidateVersion)
94 + throw new Error(
95 + `version mismatch: subprocess ${baselineVersion}, manifest ${candidateVersion}`,
96 + );
98 + const directory = mkdtempSync(path.join(tmpdir(), "pi-modernize-bench-"));
99 + try {
100 + const json = path.join(directory, "hyperfine.json");
101 + checkedSpawn(
102 + "hyperfine",
104 + "--shell=none",
105 + "--style",
106 + "basic",
107 + "--warmup",
108 + String(WARMUPS),
109 + "--runs",
110 + String(RUNS),
111 + "--export-json",
112 + json,
113 + "--command-name",
114 + "subprocess",
115 + command("spawn"),
116 + "--command-name",
117 + "manifest",
118 + command("manifest"),
119 + ],
120 + { stdio: "inherit" },
121 + );
122 + const results = JSON.parse(readFileSync(json, "utf8")).results;
123 + const baseline = path.join(directory, "subprocess.txt");
124 + const candidate = path.join(directory, "manifest.txt");
125 + writeBenchstat(baseline, results[0]);
126 + writeBenchstat(candidate, results[1]);
127 + checkedSpawn("benchstat", [baseline, candidate], { stdio: "inherit" });
128 + } finally {
129 + rmSync(directory, { recursive: true, force: true });
133 +function main() {
134 + switch (process.argv[2]) {
135 + case "spawn":
136 + spawnedVersion();
137 + break;
138 + case "manifest":
139 + manifestVersion();
140 + break;
141 + case "trace":
142 + trace();
143 + break;
144 + case undefined:
145 + compare();
146 + break;
147 + default:
148 + throw new Error(`unknown benchmark mode: ${process.argv[2]}`);
152 +const invoked = process.argv[1]
153 + ? import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href
154 + : false;
155 +if (invoked) main();

.pi/skills/modernize/scripts/audit.mjs

Modified
1 1 #!/usr/bin/env node
3 -import { spawnSync } from "node:child_process";
4 3 import {
5 4 accessSync,
6 5 constants,
139 -function run(command, args, options = {}) {
140 - const result = spawnSync(command, args, {
141 - encoding: "utf8",
142 - maxBuffer: 64 * 1024 * 1024,
143 - ...options,
144 - });
145 - if (result.error) throw result.error;
146 - if (result.status !== 0) {
147 - throw new Error(
148 - `${command} ${args.join(" ")} failed: ${(result.stderr || result.stdout).trim()}`,
149 - );
150 - }
151 - return result.stdout;
154 -function executableOnPath(name) {
138 +export function executableOnPath(name) {
155 139 const extensions =
156 140 process.platform === "win32"
157 141 ? (process.env.PATHEXT ?? ".EXE;.CMD;.BAT").split(";")
171 155 throw new Error(`${name} executable not found on PATH`);
174 -function installedChangelog(executable) {
175 - let directory = path.dirname(executable);
176 - for (let depth = 0; depth < 6; depth++) {
177 - const candidate = path.join(directory, "CHANGELOG.md");
178 - if (existsSync(candidate)) return candidate;
158 +function packageAt(directory) {
159 + const manifestPath = path.join(directory, "package.json");
160 + if (!existsSync(manifestPath)) return undefined;
161 + const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
162 + return manifest?.name === PACKAGE ? manifest : undefined;
165 +function findPiPackage(executable) {
166 + let directory = path.dirname(realpathSync(executable));
167 + while (true) {
168 + for (const packageRoot of [
169 + directory,
170 + path.join(directory, "node_modules", ...PACKAGE.split("/")),
171 + ]) {
172 + const manifest = packageAt(packageRoot);
173 + if (manifest) return { packageRoot, manifest };
179 175 const parent = path.dirname(directory);
180 - if (parent === directory) break;
176 + if (parent === directory)
177 + throw new Error(
178 + `installed ${PACKAGE} package not found near ${executable}`,
179 + );
181 180 directory = parent;
183 - throw new Error(`installed Pi changelog not found near ${executable}`);
184 +export function resolvePiInstallation(executable) {
185 + const { packageRoot, manifest } = findPiPackage(executable);
186 + const bin =
187 + typeof manifest.bin === "string" ? manifest.bin : manifest.bin?.pi;
188 + if (typeof bin !== "string")
189 + throw new Error(`installed ${PACKAGE} package has no pi bin entry`);
190 + const cli = path.resolve(packageRoot, bin);
191 + const relativeCli = path.relative(packageRoot, cli);
192 + if (relativeCli.startsWith("..") || path.isAbsolute(relativeCli))
193 + throw new Error(`installed ${PACKAGE} pi bin points outside its package`);
194 + accessSync(cli, constants.R_OK);
195 + const changelog = path.join(packageRoot, "CHANGELOG.md");
196 + accessSync(changelog, constants.R_OK);
197 + if (typeof manifest.version !== "string")
198 + throw new Error(`installed ${PACKAGE} package has no version`);
199 + return { cli, changelog, version: manifest.version };
186 202 async function fetchJson(url) {
249 265 async function main() {
250 266 const repo = process.cwd();
251 - const executable = executableOnPath("pi");
252 - const localVersion = run(executable, ["--version"]).trim();
267 + const installation = resolvePiInstallation(executableOnPath("pi"));
268 + // Package resolution already reads the authoritative installed manifest;
269 + // spawning another Node process only to print the same version costs ~770 ms.
270 + const localVersion = installation.version;
253 271 const metadata = await fetchJson(REGISTRY_URL);
254 272 if (!metadata || typeof metadata.version !== "string")
255 273 throw new Error(`registry response for ${PACKAGE} has no version`);
256 274 const latestVersion = metadata.version;
257 275 assertLatest(localVersion, latestVersion);
259 - const localChangelogPath = installedChangelog(executable);
277 + const localChangelogPath = installation.changelog;
260 278 const localChangelog = readFileSync(localChangelogPath, "utf8");
261 279 const remoteChangelogUrl = `https://raw.githubusercontent.com/earendil-works/pi/v${latestVersion}/packages/coding-agent/CHANGELOG.md`;
262 280 const remoteChangelog = await fetchText(remoteChangelogUrl);

.pi/skills/modernize/scripts/audit.test.mjs

Modified
1 1 import assert from "node:assert/strict";
2 +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3 +import { tmpdir } from "node:os";
4 +import path from "node:path";
2 5 import test from "node:test";
6 +import { checkedSpawn } from "./audit.bench.mjs";
3 7 import {
4 8 assertLatest,
5 9 collectPackageVersions,
7 11 createReport,
8 12 extractCandidates,
9 13 parseChangelog,
14 + resolvePiInstallation,
10 15 } from "./audit.mjs";
12 17 const changelog = `# Changelog
74 79 assert.throws(() => assertLatest("0.80.5", "0.80.6"), /not latest/);
82 +test("resolves the installed Pi package from launcher and CLI paths", (context) => {
83 + const directory = mkdtempSync(path.join(tmpdir(), "pi-modernize-"));
84 + context.after(() => rmSync(directory, { recursive: true, force: true }));
85 + const packageRoot = path.join(
86 + directory,
87 + "node_modules",
88 + "@earendil-works",
89 + "pi-coding-agent",
90 + );
91 + const cli = path.join(packageRoot, "dist", "cli.js");
92 + const changelog = path.join(packageRoot, "CHANGELOG.md");
93 + const launcher = path.join(directory, "pi.CMD");
94 + mkdirSync(path.dirname(cli), { recursive: true });
95 + writeFileSync(
96 + path.join(packageRoot, "package.json"),
97 + JSON.stringify({
98 + name: "@earendil-works/pi-coding-agent",
99 + version: "0.85.0",
100 + bin: { pi: "dist/cli.js" },
101 + }),
102 + );
103 + writeFileSync(cli, "");
104 + writeFileSync(changelog, "");
105 + writeFileSync(launcher, "");
107 + const expected = { cli, changelog, version: "0.85.0" };
108 + assert.deepEqual(resolvePiInstallation(launcher), expected);
109 + assert.deepEqual(resolvePiInstallation(cli), expected);
110 +});
112 +test("fails when no installed Pi package is near the launcher", (context) => {
113 + const directory = mkdtempSync(path.join(tmpdir(), "pi-modernize-"));
114 + context.after(() => rmSync(directory, { recursive: true, force: true }));
115 + const launcher = path.join(directory, "pi");
116 + writeFileSync(launcher, "");
118 + assert.throws(
119 + () => resolvePiInstallation(launcher),
120 + /installed @earendil-works\/pi-coding-agent package not found/,
121 + );
122 +});
124 +test("reports inherited-stdio child failures by exit code", () => {
125 + assert.throws(
126 + () =>
127 + checkedSpawn(process.execPath, ["-e", "process.exit(7)"], {
128 + stdio: "inherit",
129 + }),
130 + /exited with code 7/,
131 + );
132 +});
77 134 test("collects resolved earendil Pi package versions", () => {
78 135 assert.deepEqual(
79 136 collectPackageVersions({

CHANGELOG.md

Modified
140 140 - Fix Ultra tool-step cleanup so teardown errors do not hide a completed workflow step
141 141 - Remove Ultra's exported authoring skill; `/ultra exec` now sends the workflow authoring prompt internally
142 142 - Fix Pulse TAB completion
143 +- Fix `/modernize` on Windows by resolving Pi behind platform launchers, and speed up audits by reading the installed package version without launching Pi
143 144 - Fix Notes so `/tree` restores notes from the selected branch and `/new` asks whether to carry pending notes into the new session
144 145 - Disable all mise-managed tools on Android to avoid platform-specific tool failures
145 146 - Speed up Mise workflows with direct task references and parallel CI check and lint stages

mise.toml

Modified
50 50 usage = '''
51 51 arg "[args]" var=#true
53 -run = '''
54 -npm run test --{% for arg in usage.args %} "{{ arg }}"{% endfor %}
55 -'''
53 +run = [
54 + '''npm run test --{% for arg in usage.args %} "{{ arg }}"{% endfor %}''',
55 + "node --test .pi/skills/modernize/scripts/audit.test.mjs",
57 58 [tasks.fuzz]
58 59 description = "Run property tests"
64 65 depends = ["install"]
65 66 run = "mise run --jobs 1 //extensions/...:bench"
68 +[tasks.modernize-bench]
69 +description = "Benchmark modernize audit"
70 +tools.hyperfine = "latest"
71 +tools."go:golang.org/x/perf/cmd/benchstat" = "latest"
72 +usage = '''
73 +arg "[args]" var=#true
74 +'''
75 +run = '''
76 +node .pi/skills/modernize/scripts/audit.bench.mjs{% for arg in usage.args %} "{{ arg }}"{% endfor %}
77 +'''
67 79 [tasks.e2e]
68 80 description = "Run all extension browser E2E tests"
69 81 depends = ["install"]