Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330import type { WebContents } from "electron"import { Browser } from "@opencode/plugin-browser/rpc"import { gzipSync, gunzipSync } from "node:zlib"import { Schema } from "effect"import type { Cdp } from "./cdp"import type { BrowserFiles } from "./files"import { analyzeCpu, analyzeTrace, parseHeap } from "./analysis"
export type Recording = { owner: WebContents pid: number started: number timer?: ReturnType<typeof setTimeout> finish: () => Promise<{ id: Browser.FileID; durationMs: number; incomplete: boolean }>}
/** Only one trace records across every tab of the app; `shared` holds it. */export function createProfiling( contents: WebContents, cdp: Cdp, files: BrowserFiles, source: () => readonly string[], shared: { recording?: Recording },) { let trace: Promise<{ id: Browser.FileID; durationMs: number; incomplete: boolean }> | undefined let traceResources = new Set<string>() let traceID = ""
let cpu: | { started: number id: string resources: Set<string> timer?: ReturnType<typeof setTimeout> result?: Promise<{ id: Browser.FileID; durationMs: number }> } | undefined
let takingHeap = false cdp.on("Page.frameNavigated", ({ frame }) => { if (shared.recording?.owner === contents) traceResources.add(frame.url)
if (cpu && !cpu.result) cpu.resources.add(frame.url) })
const json = async (id: Browser.FileID) => { const file = await files.transfer(id)
try { const data = Buffer.from(file.data)
return Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown))( (file.name.endsWith(".gz") ? gunzipSync(data, { maxOutputLength: 128 * 1024 * 1024 }) : data).toString("utf8"), ) } catch (error) { throw new Error( "Selected file cannot be decoded as a JSON capture, or expands beyond the 128 MiB analysis limit. Call browser.files.list({tabID}) and choose the fileID from the matching trace, CPU, or heap capture, not a screenshot/download. Do not retry the same invalid file.", { cause: error }, ) } }
const stopCpu = () => { if (!cpu) return Promise.reject( new Error( "No CPU profile has been started in this tab. Call browser.cpu.start({tabID}), perform the interaction to inspect, then browser.cpu.stop({tabID}).", ), )
if (cpu.result) return cpu.result const resources = cpu.resources clearTimeout(cpu.timer) cpu.result = cdp.send("Profiler.stop").then(async ({ profile }) => ({ id: await files.save("profile.cpuprofile", "application/json", Buffer.from(JSON.stringify(profile)), [ ...resources, ]), durationMs: (profile.endTime - profile.startTime) / 1000, }))
return cpu.result }
return { target(type: "trace" | "cpu"): Browser.Target { return { resources: (type === "trace" ? [...traceResources] : [...(cpu?.resources ?? [])]).sort(), key: type === "trace" ? traceID : (cpu?.id ?? ""), } }, async startTrace(durationMs = 10_000) { if (shared.recording) throw new Error( shared.recording.owner === contents ? "A performance trace is already active in this tab. Use browser.trace.stop({tabID}) to finish it before starting another." : "Another tab owns the active performance trace. Wait for its owner to finish; do not stop or replace another tab's recording.", ) const complete = Promise.withResolvers<{ stream?: string; dataLossOccurred: boolean }>() const off = cdp.on("Tracing.tracingComplete", (event) => complete.resolve(event))
const owner = { owner: contents, pid: contents.getOSProcessId(), started: performance.now(), timer: undefined as ReturnType<typeof setTimeout> | undefined, finish: () => { if (trace) return trace trace = (async () => { clearTimeout(owner.timer) const durationMs = performance.now() - owner.started const deadline = Promise.withResolvers<never>()
const timeout = setTimeout( () => deadline.reject( new Error( "Chromium did not finish flushing the trace within 10 seconds. No complete export is confirmed. Check browser.files.list({tabID}); do not start another recording until the current trace has finished or the user resolves the failure.", ), ), 10_000, )
try { const result = await Promise.race([ cdp.send("Tracing.end").then(() => complete.promise), deadline.promise, ])
if (!result.stream) throw new Error( "Chromium stopped tracing without returning a trace stream. No export is available. Check desktop/plugin compatibility and report the failure; repeating trace.stop cannot recover a missing stream.", ) const chunks: Buffer[] = [] let bytes = 0
try { while (true) { const part = await cdp.send("IO.read", { handle: result.stream, size: 256 * 1024 }) const buffer = Buffer.from(part.data, part.base64Encoded ? "base64" : "utf8") bytes += buffer.byteLength
if (bytes > 64 * 1024 * 1024) throw new Error( "Trace exceeded its 64 MiB desktop capture limit. Record a shorter interaction with a smaller durationMs in browser.trace.start; do not repeat the same recording unchanged.", ) chunks.push(buffer)
if (part.eof) break } } finally { await cdp.send("IO.close", { handle: result.stream }) }
const raw = Schema.decodeUnknownSync( Schema.fromJsonString( Schema.Struct({ traceEvents: Schema.Array(Schema.Record(Schema.String, Schema.Json)) }), ), )(Buffer.concat(chunks).toString("utf8"))
// A CDP trace is browser-wide. Export only this renderer process, never // the application renderer or another session's process metadata. const traceEvents = raw.traceEvents.filter((event) => event.pid === owner.pid)
const id = await files.save( "trace.json.gz", "application/gzip", gzipSync( JSON.stringify({ traceEvents, metadata: { source: "opencode", scope: "renderer-process", processId: owner.pid }, }), ), [...traceResources], )
return { id, durationMs, incomplete: result.dataLossOccurred || contents.isDestroyed() || contents.getOSProcessId() !== owner.pid, } } finally { clearTimeout(timeout) off()
if (shared.recording === owner) shared.recording = undefined } })()
return trace }, }
shared.recording = owner trace = undefined traceResources = new Set(source()) traceID = crypto.randomUUID()
try { await cdp.send("Tracing.start", { transferMode: "ReturnAsStream", traceConfig: { recordMode: "recordUntilFull", traceBufferSizeInKb: 8192, includedCategories: [ "devtools.timeline", "disabled-by-default-devtools.timeline", "disabled-by-default-devtools.timeline.stack", "v8.execute", "blink.user_timing", "disabled-by-default-v8.cpu_profiler", ], excludedCategories: ["*"], }, }) owner.timer = setTimeout(() => { void owner.finish().catch(() => undefined) }, durationMs) } catch (error) { off()
if (shared.recording === owner) shared.recording = undefined throw error } }, stopTrace() { if (shared.recording?.owner === contents) return shared.recording.finish()
if (trace) return trace
return Promise.reject( new Error( "This tab has no performance trace to stop. Call browser.trace.start({tabID}), perform the interaction to inspect, then browser.trace.stop({tabID}).", ), ) }, async startCpu() { if (cpu && !cpu.result) throw new Error( "A CPU profile is already active in this tab. Use browser.cpu.stop({tabID}) before starting another profile.", ) await cdp.send("Profiler.enable") await cdp.send("Profiler.start") cpu = { started: Date.now(), id: crypto.randomUUID(), resources: new Set(source()) } cpu.timer = setTimeout(() => { void stopCpu().catch(() => undefined) }, 30_000) }, stopCpu, async heap() { if (takingHeap) throw new Error( "A heap snapshot is already being captured in this tab. Await that call before taking another; do not capture the same tab's heaps in parallel.", ) takingHeap = true const resources = source() const chunks: string[] = [] let size = 0 let overflow = false
const off = cdp.on("HeapProfiler.addHeapSnapshotChunk", ({ chunk }) => { size += Buffer.byteLength(chunk)
if (size > 128 * 1024 * 1024) overflow = true
if (!overflow) chunks.push(chunk) })
try { await cdp.send("HeapProfiler.enable") await cdp.send("HeapProfiler.takeHeapSnapshot", { reportProgress: false }) } finally { takingHeap = false off() }
if (overflow) throw new Error( "Heap snapshot exceeded the 128 MiB desktop capture limit. Use a smaller page/test case or ask the user to inspect the heap with desktop developer tools; this tool has no size override.", )
return files.save("heap.heapsnapshot.gz", "application/gzip", gzipSync(chunks.join("")), [ ...resources, ...source(), ]) }, async analyze( action: Extract< Browser.Action, { type: "trace.analyze" | "cpu.analyze" | "heap.summary" | "heap.query" | "heap.object" | "heap.compare" } >, ) { if (action.type === "heap.compare") { const before = parseHeap(await json(action.before)).classes const after = parseHeap(await json(action.after)).classes
return { classes: Array.from(new Set([...before.keys(), ...after.keys()])) .map((name) => ({ name, countDelta: (after.get(name)?.count ?? 0) - (before.get(name)?.count ?? 0), bytesDelta: (after.get(name)?.bytes ?? 0) - (before.get(name)?.bytes ?? 0), })) .filter((item) => item.countDelta || item.bytesDelta) .sort((a, b) => Math.abs(b.bytesDelta) - Math.abs(a.bytesDelta)) .slice(0, action.limit ?? 100), } }
const value = await json(action.fileID)
if (action.type === "trace.analyze") return analyzeTrace(value, action.limit)
if (action.type === "cpu.analyze") return analyzeCpu(value, action.limit) const heap = parseHeap(value)
if (action.type === "heap.summary") return heap.summary(action.limit)
if (action.type === "heap.query") return heap.query(action.name, action.limit)
return heap.object(action.id, action.limit) }, async dispose() { if (shared.recording?.owner === contents) await shared.recording.finish().catch(() => undefined)
if (cpu && !cpu.result) await stopCpu().catch(() => undefined) }, }}