fix(factory): treat 404 agent_not_found on release as terminal success - #365
Merged
Conversation
A `FleetClient.release()` that comes back with a Relay 404
`agent_not_found` IS the successful end state release itself aims for:
the agent is already gone from Relay's view. Treating it as a failure
re-arms `#abandonStuckDispatch` → `#releaseAndTerminateAgents` at ~1 Hz
forever. That is what wedged cloud dispatch: an instrumented `/evidence`
stream captured 1434 stderr lines in 13 minutes, 100% of them the same
three ghost agents (`ar-9-impl-sandbox`, `ar-1540-babysit-relay`,
`ar-1540-impl-relay`) cycling through:
[factory] releasing agents held past deadline
{issue:"9", heldForMs:373458303, holdTimeoutMs:14400000,
waitingForTerminalState:"human-review",
reason:"held-past-deadline",
agents:["ar-9-impl-sandbox"]}
[factory] no pid available to terminate ar-9-impl-sandbox during
completion
[factory] failed to release ar-9-impl-sandbox during completion
{name:"RelayError", message:'Agent "ar-9-impl-sandbox" not found',
code:"not_found", retryable:false, statusCode:404,
rawCode:"agent_not_found"}
Sweep discovery never got a slot because every cycle was consumed by
this loop; the canary issue and every real work unit sat untouched.
The fix classifies a release error as "already gone" when either the
canonical `rawCode === 'agent_not_found'` field is set, or the fallback
combination `statusCode === 404 && code === 'not_found'` is present (a
middleware may re-throw with the HTTP status preserved but drop the
SDK-specific rawCode; the fix depends on catching both shapes). On the
already-gone path, the code performs the exact same bookkeeping the
success path does — `batch.recordRelease` and the `agent.released`
report — so the lifecycle marches on to `abandoned` and the sweep tick
frees up. Anything else keeps going through the failure path: a
retryable 503 host-unavailable, a real 5xx, an unrelated 404 on a
different resource all still add the agent to `failed`, drive
`cleanupComplete = false`, and schedule a legitimate retry.
Isolated the classifier into its own module so the reasoning lives
next to the rule and the tests are focused (rather than adding a case
to the 20k-line factory.test.ts, which already carries known flakes).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Session-Id: a4adc660-7d8a-4fff-b674-2e3931afd6cb
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Contributor
|
@coderabbitai review Requested for exact head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the runaway release loop that has kept cloud Factory from dispatching any work. When
FleetClient.release()returns a Relay 404agent_not_found, that IS the successful end state release aims for — the agent is already gone from Relay's view. Treating it as a failure re-arms#abandonStuckDispatch→#releaseAndTerminateAgentsat ~1 Hz forever.Evidence
An instrumented
/evidencestream in AgentWorkforce/factory-cloud (fc#77) captured 1434 stderr lines in 13 minutes, 100% of them the same three ghost agents (ar-9-impl-sandbox,ar-1540-babysit-relay,ar-1540-impl-relay) cycling:readinessReconcile.state=stalledfor 30+ minutes,dispatched=0, canary #350 untouched — because every sweep cycle was consumed by this loop and discovery never got a slot.Change
src/orchestrator/release-error.ts— a smallisAgentAlreadyGoneOnRelease(error)classifier that recognises both the canonicalrawCode === 'agent_not_found'and the fallbackstatusCode === 404 && code === 'not_found'(a middleware may re-throw with HTTP status preserved but drop the SDK-specific rawCode).src/orchestrator/factory.ts:#releaseAndTerminateAgents— on an already-gone catch, run the samebatch.recordRelease+agent.releasedbookkeeping the success path does, so the lifecycle marches on toabandonedand the sweep frees up. Anything else — retryable 503 host-unavailable, real 5xx, unrelated 404 on a different resource — keeps going through the failure path and drives a legitimate retry.Test plan
src/orchestrator/release-error.test.ts— 7 cases: canonical shape, fallback shape, foreign 404, retryable 503, generic 500, primitives, missing fields.npx vitest run src/orchestrator/release-error.test.ts— 7/7 pass.factoryOutput.stderrLastNstops showing the release-retry pattern andreadinessReconciletransitions fromstalledback tohealthywith realdispatched > 0.🤖 Generated with Claude Code