From 0ef430a39ffbd1d2fd06f53c9c8cf980ae8a9313 Mon Sep 17 00:00:00 2001 From: roboomp Date: Tue, 30 Jun 2026 12:47:07 +0000 Subject: [PATCH] fix(debug): guarded v8.setFlagsFromString call so CPU profiler works on Bun MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CPU profiler called node:v8 setFlagsFromString("--allow-natives-syntax") unconditionally and crashed on Bun (oven-sh/bun#1702), surfacing as "Failed to start profiler: node:v8 setFlagsFromString is not yet implemented in Bun". The flag is only needed for ad-hoc V8 natives such as %GetOptimizationStatus, not for the CDP Profiler session that actually collects samples — so swallow the error and let the inspector path run. Added a Bun-runtime regression test that would have caught the crash: without the guard, startCpuProfile() throws before returning a session. Fixes #3897 --- packages/coding-agent/CHANGELOG.md | 4 ++++ packages/coding-agent/src/debug/profiler.ts | 8 ++++++- .../coding-agent/test/debug/profiler.test.ts | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 packages/coding-agent/test/debug/profiler.test.ts diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 46831be05..3ca3513c3 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fixed + +- Fixed `omp debug --profile` failing on Bun with "node:v8 setFlagsFromString is not yet implemented in Bun" by treating the optional `--allow-natives-syntax` flag as best-effort, so the CPU profiler proceeds even on runtimes that don't expose `v8.setFlagsFromString`. ([#3897](https://github.com/can1357/oh-my-pi/issues/3897)) + ## [16.2.8] - 2026-06-30 ### Added diff --git a/packages/coding-agent/src/debug/profiler.ts b/packages/coding-agent/src/debug/profiler.ts index 242cb2a2f..c81a7064a 100644 --- a/packages/coding-agent/src/debug/profiler.ts +++ b/packages/coding-agent/src/debug/profiler.ts @@ -114,7 +114,13 @@ function formatProfileAsMarkdown(profileJson: string): string { */ export async function startCpuProfile(): Promise { const v8 = await import("node:v8"); - v8.setFlagsFromString("--allow-natives-syntax"); + try { + // Enables `%GetOptimizationStatus` and friends when V8 natives are needed + // for ad-hoc profiling. Best-effort: Bun does not implement + // `setFlagsFromString` (oven-sh/bun#1702) but the CPU profiler itself + // works without it, so swallow the error and continue. + v8.setFlagsFromString("--allow-natives-syntax"); + } catch {} const { Session } = await import("node:inspector/promises"); const session = new Session(); diff --git a/packages/coding-agent/test/debug/profiler.test.ts b/packages/coding-agent/test/debug/profiler.test.ts new file mode 100644 index 000000000..afbd7a60c --- /dev/null +++ b/packages/coding-agent/test/debug/profiler.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from "bun:test"; +import { startCpuProfile } from "@oh-my-pi/pi-coding-agent/debug/profiler"; + +describe("startCpuProfile", () => { + // Regression: `node:v8` `setFlagsFromString` throws on Bun + // (oven-sh/bun#1702). The profiler used to call it unconditionally and + // crash before connecting the inspector session. Running this test under + // Bun guarantees the guard is in place — without it the call below would + // reject with "node:v8 setFlagsFromString is not yet implemented in Bun". + it("starts and stops successfully even when v8.setFlagsFromString is unavailable", async () => { + const session = await startCpuProfile(); + // Run a tiny bit of work so the profile has at least one sample. + let acc = 0; + for (let i = 0; i < 10_000; i++) acc += i; + expect(acc).toBeGreaterThan(0); + + const profile = await session.stop(); + const parsed = JSON.parse(profile.data) as { nodes: unknown[] }; + expect(Array.isArray(parsed.nodes)).toBe(true); + expect(parsed.nodes.length).toBeGreaterThan(0); + expect(typeof profile.markdown).toBe("string"); + expect(profile.markdown.length).toBeGreaterThan(0); + }); +});