@@ -31,6 +31,7 @@ import {
3131import { sleepWithAbort } from "../../infra/backoff.js" ;
3232import { freezeDiagnosticTraceContext } from "../../infra/diagnostic-trace-context.js" ;
3333import { formatErrorMessage , toErrorObject } from "../../infra/errors.js" ;
34+ import { redactIdentifier } from "../../logging/redact-identifier.js" ;
3435import { buildAgentHookContextChannelFields } from "../../plugins/hook-agent-context.js" ;
3536import { getGlobalHookRunner } from "../../plugins/hook-runner-global.js" ;
3637import { 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+
602605export 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