Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,62 @@ describe('resolveAgentIdentity', () => {
userId: 'local-user'
})
})

it('stops reporting once the report budget is spent, so a flapping socket cannot flood telemetry', async () => {
const getIdentity = failing()
const identity = harness(getIdentity)

await vi.advanceTimersByTimeAsync(10 * 60_000)
expect(identity.onFailure).toHaveBeenCalledTimes(8)

for (let reopen = 0; reopen < 3; reopen += 1) {
identity.connect()
await vi.advanceTimersByTimeAsync(10 * 60_000)
}

// 8 reports for the first window, then exactly one marker saying the rest are
// suppressed -- without it this is indistinguishable from a cycle that ended.
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
expect(getIdentity).toHaveBeenCalledTimes(32)
expect(identity.onFailure).toHaveBeenCalledTimes(9)
// The ninth failure, not the first of the second window: `failures` restarts on
// every reopen, so the marker has to count cumulatively or it reads "after 1".
expect(identity.onFailure).toHaveBeenLastCalledWith(
expect.objectContaining({
message: expect.stringContaining('after 9 attempts')
})
)
})

it('reports a terminal failure even after the retryable budget is spent', async () => {
const transient = new Error('agent still starting')
const terminal = new AgentApiError('forbidden', 403, undefined)
const getIdentity = vi
.fn<() => Promise<{ userId: string }>>()
.mockRejectedValue(transient)
const identity = harness(getIdentity, (error) => error !== terminal)

await vi.advanceTimersByTimeAsync(10 * 60_000)
identity.onFailure.mockClear()

// The budget is spent. A 401/403 or a missing route is the most actionable
// failure there is, so it must not be swallowed by the transient pool.
getIdentity.mockRejectedValue(terminal)
identity.connect()
await vi.advanceTimersByTimeAsync(10 * 60_000)

expect(identity.onFailure).toHaveBeenCalledWith(terminal)
expect(identity.state.value).toEqual({ status: 'rejected' })
})

it('reports again for a new account, because reset refreshes the report budget', async () => {
const getIdentity = failing()
const identity = harness(getIdentity)
await vi.advanceTimersByTimeAsync(10 * 60_000)
expect(identity.onFailure).toHaveBeenCalledTimes(8)

identity.reset()
await vi.advanceTimersByTimeAsync(10 * 60_000)

expect(identity.onFailure).toHaveBeenCalledTimes(16)
})
})
38 changes: 37 additions & 1 deletion src/workbench/extensions/agent/services/agent/agentIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ const RETRY_BASE_MS = 1_000
const RETRY_MAX_MS = 30_000
/** Lookups tried before a transient failure is treated as a rejection. */
const MAX_ATTEMPTS = 8
/**
* Retryable failures reported per identity. The attempt budget restarts every time
* the socket reopens, so without a separate report budget a flapping socket would
* re-emit the same warning for as long as the panel stays mounted. Derived from
* MAX_ATTEMPTS so one full attempt window always reports: pinning it to a literal
* meant raising MAX_ATTEMPTS would silently drop the tail of the first window,
* including the attempt that exhausts retries.
*/
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
const MAX_REPORTED_FAILURES = MAX_ATTEMPTS

Comment thread
christian-byrne marked this conversation as resolved.
Resolved
interface AgentIdentityDeps {
getIdentity(): Promise<{ userId: string }>
Expand Down Expand Up @@ -35,6 +44,11 @@ export function resolveAgentIdentity(
let inFlight = false
let stopped = false
let failures = 0
let reported = 0
let suppressed = false
// `failures` restarts whenever the socket reopens, so it cannot describe how long
// the lookup has been failing. Only `reset()` clears this one.
let totalFailures = 0
let retryTimer: ReturnType<typeof setTimeout> | null = null
let generation = 0
let exhausted = false
Expand All @@ -59,11 +73,30 @@ export function resolveAgentIdentity(

function settleFailure(error: unknown): void {
failures += 1
deps.onFailure(error)
totalFailures += 1
// A terminal failure -- a 401/403, or a 404 for a route that is not there -- is
// the most actionable one there is and ends the lookup for good, so it always
// reports. Charging it against the same budget as the transient failures meant an
// exhausted retry cycle could spend the budget first and leave it silent.
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
if (!deps.isRetryable(error)) {
deps.onFailure(error)
state.value = { status: 'rejected' }
return
}
if (reported < MAX_REPORTED_FAILURES) {
reported += 1
deps.onFailure(error)
} else if (!suppressed) {
// Without this marker a socket flapping for hours looks exactly like a single
// failure cycle that ended, because both stop reporting at the same count.
suppressed = true
deps.onFailure(
new Error(
`Agent identity lookup still failing after ${totalFailures} attempts; further failure reports suppressed`,
{ cause: error }
)
)
}
if (failures >= MAX_ATTEMPTS) {
exhausted = true
state.value = { status: 'rejected' }
Expand Down Expand Up @@ -106,6 +139,9 @@ export function resolveAgentIdentity(
generation += 1
state.value = { status: 'pending' }
failures = 0
totalFailures = 0
reported = 0
suppressed = false
exhausted = false
inFlight = false
cancelRetry()
Expand Down
Loading