Skip to content

fix(android): guard AlphaLocalAgentRunner phase transitions against stop races - #2398

Open
NubsCarson wants to merge 1 commit into
developfrom
fix/local-agent-runner-stop-race
Open

NubsCarson wants to merge 1 commit into
developfrom
fix/local-agent-runner-stop-race

Conversation

@NubsCarson

Copy link
Copy Markdown
Collaborator

Summary

Fixes the thread race in AlphaLocalAgentRunner caught on main (Resident Android exact source run 47400748049, AlphaAgentServiceLocalHandoffTest.localRunnerLifecycleTransitionsAreSynchronousAndRaceFree at AlphaAgentServiceLocalHandoffTest.java:113):

  • Root cause: start() queued launchLocked(targetEpoch, ...) on serial, which mutated phase (Phase.STARTING / Phase.FAILED via failLocked) and activeEpoch without holding lifecycleLock or re-checking desiredRunning && activeEpoch == epoch after stop() synchronously set desiredRunning = false; phase = Phase.STOPPED. statusJson() also read phase without holding lifecycleLock and lacked a !desiredRunning guard, so a racing launchLocked/failLocked caused isActive() to return false while statusJson().optString("state") reverted to "starting" or "failed".
  • Lifecycle synchronization (apps/app/android/app/src/main/java/ai/alphacompute/alphaphone/AlphaLocalAgentRunner.java):
    • Guard all phase, activeEpoch, failureReason, and errorDetail reads/writes across launchLocked, failLocked, markReadyLocked, watchExit, failOrScheduleRestartLocked, stop, isRunning, onReady, and statusJson under lifecycleLock, verifying desiredRunning && activeEpoch == epoch before mutating state or spawning a child process.
    • Ensure statusJson() captures an atomic snapshot under lifecycleLock and always reports state: "stopped" and epoch: 0 (with no failure/error fields) whenever !desiredRunning.
    • Track childEpoch separately on serial so teardownChildLocked() retires only the torn-down child's epoch instead of a newer activeEpoch reserved by a subsequent start().
    • Prevent failOrScheduleRestartLocked from calling failLocked when !desiredRunning or when superseded by a newer epoch, and reserve retry epochs atomically under lifecycleLock.
  • Regression tests (apps/app/android/app/src/test/java/ai/alphacompute/alphaphone/AlphaAgentServiceLocalHandoffTest.java):
    • Add stopDuringAsyncLaunchFailureNeverRevertsStatusFromStopped (stops immediately upon the async launch observer callback right before Robolectric's async launch failure path runs, draining serial via a FIFO barrier and verifying state == "stopped", epoch == 0, and no failure/error).
    • Add repeatedStartRestartStopCyclesKeepStoppedStateAndMonotonicEpochs (60 rapid start/restart/stop cycles with varying micro-delays and a linearizable seqlock observer verifying atomic snapshots and monotonic epochs).

Verification

  • ./gradlew --no-daemon -PelizaParadeeApiOnly=true -PelizaSpeechBridgeCapacitor=true :app:testLauncherDebugUnitTest --rerun against pinned eliza@8dc9b2db6def: 33 test suites, 133/133 tests PASS (0 failures, 0 errors, 0 skipped)
  • AlphaAgentServiceLocalHandoffTest (6/6 tests) repeated 3 consecutive --rerun runs: 18/18 PASS (0 flakes)
  • pnpm no-bun (via Boat vibestack-remote) and git diff --check: PASS

…top races

Guard lifecycle phase, epoch, and failure-detail transitions in AlphaLocalAgentRunner under lifecycleLock so an in-flight async launchLocked(), failLocked(), markReadyLocked(), watchExit(), or failOrScheduleRestartLocked() task on the serial executor cannot flip statusJson() back to starting, running, or failed after stop() sets desiredRunning = false or restart() advances activeEpoch.

Make statusJson() and isRunning() read lifecycle state atomically under lifecycleLock (always reporting stopped with epoch 0 whenever !desiredRunning), track childEpoch separately so queued stop teardown retires only the child it tears down, and add deterministic async-stop and stress-cycle regression tests in AlphaAgentServiceLocalHandoffTest.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 20:05
@NubsCarson
NubsCarson requested a deployment to turbo-cache-read December 4, 2026 20:05 — with GitHub Actions In progress
@NubsCarson
NubsCarson deployed to turbo-cache-read December 4, 2026 20:05 — with GitHub Actions Active

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

This branch was successfully deployed

1 active deployment
turbo-cache-read — 45d05506 Deployed Dec 4, 2026 by NubsCarson via build #5835
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants