Skip to content

Commit 9e13abc

Browse files
galinilievGalin Iliev
authored andcommitted
fix(agents): split embedded attempt dispatch timing
1 parent 549a0ea commit 9e13abc

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

src/agents/pi-embedded-runner/run.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import { createEmbeddedRunReplayState, observeReplayMetadata } from "./replay-st
114114
import { handleAssistantFailover } from "./run/assistant-failover.js";
115115
import {
116116
createEmbeddedRunStageTracker,
117+
EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE,
117118
formatEmbeddedRunStageSummary,
118119
shouldWarnEmbeddedRunStageSummary,
119120
} from "./run/attempt-stage-timing.js";
@@ -194,6 +195,16 @@ const COMPACTION_CONTINUATION_RETRY_INSTRUCTION =
194195
"The previous attempt compacted the conversation context before producing a final user-visible answer. Continue from the compacted transcript and produce the final answer now. Do not restart from scratch, do not repeat completed work, and do not rerun tools unless the transcript clearly lacks required evidence.";
195196
type EmbeddedRunAttemptForRunner = Awaited<ReturnType<typeof runEmbeddedAttemptWithBackend>>;
196197

198+
function resolveAttemptDispatchApiKey(params: {
199+
apiKeyInfo: ApiKeyInfo | null;
200+
runtimeAuthState: RuntimeAuthState | null;
201+
}): string | undefined {
202+
if (params.runtimeAuthState) {
203+
return undefined;
204+
}
205+
return params.apiKeyInfo?.apiKey;
206+
}
207+
197208
function resolveEmbeddedRunLaneTimeoutMs(timeoutMs: number): number | undefined {
198209
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
199210
return undefined;
@@ -1271,6 +1282,9 @@ export async function runEmbeddedPiAgent(
12711282
authRetryPending = false;
12721283
attemptedThinking.add(thinkLevel);
12731284
await fs.mkdir(resolvedWorkspace, { recursive: true });
1285+
if (!startupStagesEmitted) {
1286+
startupStages.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.workspace);
1287+
}
12741288

12751289
const basePrompt =
12761290
nextAttemptPromptOverride ??
@@ -1289,9 +1303,12 @@ export async function runEmbeddedPiAgent(
12891303
promptAdditions.length > 0
12901304
? `${basePrompt}\n\n${promptAdditions.join("\n\n")}`
12911305
: basePrompt;
1292-
let resolvedStreamApiKey: string | undefined;
1293-
if (!runtimeAuthState && apiKeyInfo) {
1294-
resolvedStreamApiKey = (apiKeyInfo as ApiKeyInfo).apiKey;
1306+
const resolvedStreamApiKey = resolveAttemptDispatchApiKey({
1307+
apiKeyInfo,
1308+
runtimeAuthState,
1309+
});
1310+
if (!startupStagesEmitted) {
1311+
startupStages.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.prompt);
12951312
}
12961313
const runtimePlan = buildAgentRuntimePlan({
12971314
provider,
@@ -1323,9 +1340,10 @@ export async function runEmbeddedPiAgent(
13231340
},
13241341
});
13251342
if (!startupStagesEmitted) {
1326-
startupStages.mark("attempt-dispatch");
1343+
startupStages.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.runtimePlan);
1344+
startupStages.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.dispatch);
13271345
notifyExecutionPhase("attempt_dispatch", { provider, model: modelId });
1328-
emitStartupStageSummary("attempt-dispatch");
1346+
emitStartupStageSummary(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.dispatch);
13291347
startupStagesEmitted = true;
13301348
}
13311349

src/agents/pi-embedded-runner/run/attempt-stage-timing.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vitest";
22
import {
33
createEmbeddedRunStageTracker,
4+
EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE,
45
formatEmbeddedRunStageSummary,
56
shouldWarnEmbeddedRunStageSummary,
67
} from "./attempt-stage-timing.js";
@@ -66,4 +67,22 @@ describe("embedded run stage timing", () => {
6667
"embedded run startup stages: runId=r1 totalMs=80 stages=workspace:25ms@25ms,tools:55ms@80ms",
6768
);
6869
});
70+
71+
it("names first-attempt dispatch subspans for slow startup summaries", () => {
72+
let clock = 0;
73+
const tracker = createEmbeddedRunStageTracker({ now: () => clock });
74+
75+
clock = 10;
76+
tracker.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.workspace);
77+
clock = 40;
78+
tracker.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.prompt);
79+
clock = 90;
80+
tracker.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.runtimePlan);
81+
clock = 91;
82+
tracker.mark(EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE.dispatch);
83+
84+
expect(formatEmbeddedRunStageSummary("startup", tracker.snapshot())).toBe(
85+
"startup totalMs=91 stages=attempt-workspace:10ms@10ms,attempt-prompt:30ms@40ms,attempt-runtime-plan:50ms@90ms,attempt-dispatch:1ms@91ms",
86+
);
87+
});
6988
});

src/agents/pi-embedded-runner/run/attempt-stage-timing.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export type EmbeddedRunStageTracker = {
1414
snapshot: () => EmbeddedRunStageSummary;
1515
};
1616

17+
export const EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE = {
18+
workspace: "attempt-workspace",
19+
prompt: "attempt-prompt",
20+
runtimePlan: "attempt-runtime-plan",
21+
dispatch: "attempt-dispatch",
22+
} as const;
23+
1724
const EMBEDDED_RUN_STAGE_WARN_TOTAL_MS = 10_000;
1825
const EMBEDDED_RUN_STAGE_WARN_STAGE_MS = 5_000;
1926

0 commit comments

Comments
 (0)