openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
79 lines (78 loc) • 2.66 kB
JavaScript
import { n as createQueuedDiagnosticPhaseEmitter } from "./diagnostic-events-DT4ejwoa.mjs";
import { t as createSubsystemLogger } from "./subsystem-4NzR43uz.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
import { performance } from "node:perf_hooks";
//#region src/agents/worktrees/preparation-timing.ts
const log = createSubsystemLogger("agents/worktrees");
const preparation = new AsyncLocalStorage();
function markManagedWorktreePreparation() {
const current = preparation.getStore();
if (current) current.enabled = true;
}
function setWorktreePreparationTemplate(template) {
const current = preparation.getStore();
if (current) current.template = template;
}
/** Nested phases are inclusive; only total measures the complete preparation once. */
function startWorktreePreparationPhase(phase) {
const current = preparation.getStore();
const startedAt = performance.now();
let finished = false;
return () => {
if (current && !finished) {
current.phases[phase] = (current.phases[phase] ?? 0) + performance.now() - startedAt;
finished = true;
}
};
}
async function timeWorktreePreparationPhase(phase, run) {
const finish = startWorktreePreparationPhase(phase);
try {
return await run();
} finally {
finish();
}
}
async function withWorktreePreparationTiming(kind, run) {
const startedAt = Date.now();
const start = performance.now();
const emit = createQueuedDiagnosticPhaseEmitter();
const current = {
enabled: kind === "managed",
template: "unavailable",
phases: {}
};
let outcome = "threw";
try {
const result = await preparation.run(current, run);
outcome = "returned";
return result;
} finally {
if (current.enabled) try {
const durationMs = Math.round(performance.now() - start);
const phaseDurationsMs = Object.fromEntries(Object.entries(current.phases).map(([phase, duration]) => [phase, Math.round(duration)]));
log.info("managed worktree preparation", {
consoleMessage: `managed worktree preparation kind=${kind} template=${current.template} outcome=${outcome} durationMs=${durationMs} phaseDurationsMs=${JSON.stringify(phaseDurationsMs)}`,
kind,
template: current.template,
outcome,
durationMs,
phaseDurationsMs
});
emit?.({
name: "worktree.preparation",
startedAt,
endedAt: Date.now(),
durationMs,
details: {
kind,
template: current.template,
outcome,
...phaseDurationsMs
}
});
} catch {}
}
}
//#endregion
export { withWorktreePreparationTiming as a, timeWorktreePreparationPhase as i, setWorktreePreparationTemplate as n, startWorktreePreparationPhase as r, markManagedWorktreePreparation as t };