auth-yes/.forum/src/agents/adversary_performance.ts

68 lines
1.9 KiB
TypeScript

import { resolveGitPath } from "../core/git_path_resolver.ts";
import { join } from "jsr:@std/path@0.224.0";
import { writeNote } from "../core/git_storage.ts";
export async function analyzePerformance(cwd: string) {
console.log(" [Performance] Ingesting Execution Traces...");
// deno-lint-ignore no-explicit-any
const performanceBottlenecks: any[] = [];
let traceFile = "";
try {
traceFile = await resolveGitPath("forum_trace.jsonl", cwd);
} catch {
traceFile = join(cwd || Deno.cwd(), ".git", "forum_trace.jsonl");
}
try {
const content = await Deno.readTextFile(traceFile);
const lines = content.split("\n").filter((line) => line.trim() !== "");
// deno-lint-ignore no-explicit-any
const slowTraces: any[] = [];
for (const line of lines) {
try {
const trace = JSON.parse(line);
if (trace.duration_ms > 100) {
slowTraces.push(trace);
}
} catch {
// Ignore malformed trace lines
}
}
if (slowTraces.length > 0) {
performanceBottlenecks.push({
source: "execution-telemetry",
slowTraces: slowTraces,
});
await writeNote("otel_traces", slowTraces, "HEAD", cwd);
}
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) {
throw new Error(
`Failed to process execution traces: ${
e instanceof Error ? e.message : String(e)
}`,
);
}
// If NotFound, just return empty bottlenecks
} finally {
// Explicitly delete the trace file to prevent trace leakage between runs
try {
if (traceFile) {
await Deno.remove(traceFile);
}
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) {
throw new Error(
`Failed to cleanup execution trace file: ${
e instanceof Error ? e.message : String(e)
}`,
);
}
}
}
return performanceBottlenecks;
}