Skip to content

Commit 68b533d

Browse files
Avoid post-run auth success lane delay (#85829)
* fix: avoid post-run auth success lane delay * fix: redact post-run auth profile logs * Fix embedded runner auth-success test rename
1 parent 180a960 commit 68b533d

2 files changed

Lines changed: 63 additions & 12 deletions

File tree

src/agents/embedded-agent-runner/run.overflow-compaction.loop.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
mockedIsCompactionFailureError,
1515
mockedIsLikelyContextOverflowError,
1616
mockedLog,
17+
mockedMarkAuthProfileSuccess,
1718
mockedResolveModelAsync,
1819
mockedRunEmbeddedAttempt,
1920
mockedSessionLikelyHasOversizedToolResults,
@@ -187,6 +188,22 @@ describe("overflow compaction in run loop", () => {
187188
expect(requireMockCallArg(mockedRunEmbeddedAttempt, 0).thinkLevel).toBe("adaptive");
188189
});
189190

191+
it("does not wait for post-run auth-profile success bookkeeping before returning", async () => {
192+
let resolveSuccess!: () => void;
193+
const successPromise = new Promise<void>((resolve) => {
194+
resolveSuccess = resolve;
195+
});
196+
mockedMarkAuthProfileSuccess.mockReturnValueOnce(successPromise);
197+
mockedRunEmbeddedAttempt.mockResolvedValueOnce(makeAttemptResult());
198+
199+
const result = await runEmbeddedAgent(baseParams);
200+
201+
expect(result.meta.error).toBeUndefined();
202+
expect(mockedMarkAuthProfileSuccess).toHaveBeenCalledTimes(1);
203+
resolveSuccess();
204+
await successPromise;
205+
});
206+
190207
it("continues from transcript after compaction when the current inbound message was persisted", async () => {
191208
const overflowError = makeOverflowError();
192209

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

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import { sleepWithAbort } from "../../infra/backoff.js";
3232
import { freezeDiagnosticTraceContext } from "../../infra/diagnostic-trace-context.js";
3333
import { formatErrorMessage, toErrorObject } from "../../infra/errors.js";
34+
import { redactIdentifier } from "../../logging/redact-identifier.js";
3435
import { buildAgentHookContextChannelFields } from "../../plugins/hook-agent-context.js";
3536
import { getGlobalHookRunner } from "../../plugins/hook-runner-global.js";
3637
import { resolveProviderAuthProfileId } from "../../plugins/provider-runtime.js";
@@ -599,6 +600,8 @@ function resolveInitialEmbeddedRunModel(params: {
599600
};
600601
}
601602

603+
const POST_RUN_AUTH_PROFILE_SUCCESS_SLOW_MS = 1_000;
604+
602605
export function runEmbeddedAgent(
603606
paramsInput: RunEmbeddedAgentParams,
604607
): Promise<EmbeddedAgentRunResult> {
@@ -1733,6 +1736,48 @@ async function runEmbeddedAgentInternal(
17331736
modelId: failure.modelId,
17341737
});
17351738
};
1739+
const markAuthProfileSuccessAfterRun = () => {
1740+
if (!lastProfileId) {
1741+
return;
1742+
}
1743+
const successProfileId = lastProfileId;
1744+
const safeSuccessProfileId = redactIdentifier(successProfileId, { len: 12 });
1745+
const successProvider = resolveAuthProfileStateProvider(
1746+
profileFailureStore,
1747+
successProfileId,
1748+
provider,
1749+
);
1750+
const successStarted = Date.now();
1751+
void markAuthProfileSuccess({
1752+
store: profileFailureStore,
1753+
provider: successProvider,
1754+
profileId: successProfileId,
1755+
agentDir: params.agentDir,
1756+
})
1757+
.then(() => {
1758+
const durationMs = Date.now() - successStarted;
1759+
if (durationMs >= POST_RUN_AUTH_PROFILE_SUCCESS_SLOW_MS) {
1760+
log.warn(
1761+
`post-run auth-profile success bookkeeping completed after ${durationMs}ms: ` +
1762+
`runId=${params.runId} sessionId=${params.sessionId} ` +
1763+
`provider=${sanitizeForLog(successProvider)} profileId=${safeSuccessProfileId}`,
1764+
);
1765+
} else if (log.isEnabled("trace")) {
1766+
log.trace(
1767+
`post-run auth-profile success bookkeeping completed: ` +
1768+
`runId=${params.runId} sessionId=${params.sessionId} durationMs=${durationMs}`,
1769+
);
1770+
}
1771+
})
1772+
.catch((err: unknown) => {
1773+
log.warn(
1774+
`post-run auth-profile success bookkeeping failed: ` +
1775+
`runId=${params.runId} sessionId=${params.sessionId} ` +
1776+
`provider=${sanitizeForLog(successProvider)} profileId=${safeSuccessProfileId} ` +
1777+
`error=${formatErrorMessage(err)}`,
1778+
);
1779+
});
1780+
};
17361781
const resolveRunAuthProfileFailureReason = (
17371782
failoverReason: FailoverReason | null,
17381783
opts?: { providerStarted?: boolean; transientRateLimit?: boolean },
@@ -4036,18 +4081,7 @@ async function runEmbeddedAgentInternal(
40364081
log.debug(
40374082
`embedded run done: runId=${params.runId} sessionId=${params.sessionId} durationMs=${Date.now() - started} aborted=${aborted}`,
40384083
);
4039-
if (lastProfileId) {
4040-
await markAuthProfileSuccess({
4041-
store: profileFailureStore,
4042-
provider: resolveAuthProfileStateProvider(
4043-
profileFailureStore,
4044-
lastProfileId,
4045-
provider,
4046-
),
4047-
profileId: lastProfileId,
4048-
agentDir: params.agentDir,
4049-
});
4050-
}
4084+
markAuthProfileSuccessAfterRun();
40514085
const replayInvalid = resolveReplayInvalidForAttempt(null);
40524086
const livenessState = attempt.yieldDetected
40534087
? "paused"

0 commit comments

Comments
 (0)