Skip to content

fix(service): make subprocess PATH deterministic across reconnect election (fixes #38897) - #46126

Closed
optamus-ai wants to merge 2 commits into
anomalyco:v2from
optamus-ai:optamus/fix-path-determinism-38897-v2
Closed

optamus-ai wants to merge 2 commits into
anomalyco:v2from
optamus-ai:optamus/fix-path-determinism-38897-v2

Conversation

@optamus-ai

Copy link
Copy Markdown

Fixes #38897

The V2 managed service inherits the environment of whichever reconnecting client wins election. When several clients reconnect after a restart they race to spawn contenders with different PATH values; the winner's PATH becomes the server's global PATH and shell commands like curl flip between found/missing (reproduced 10/10).

Derive the service's PATH from the user's login shell (bounded -l/-il probe, cached, no secrets) instead of the winning client's inherited PATH. spawnServiceContender now sets PATH to the login shell's canonical PATH unless the caller explicitly provides PATH, preserving test overrides. Fallback is a fixed system PATH when the probe is unavailable.

Add deterministic regression tests proving repeated elections with different client PATHs cannot change command resolution.

Preserves normal reconnect/ensure behavior and does not persist secrets.

Optamus added 2 commits August 29, 2026 13:45
…bent (fixes anomalyco#37795)

opencode2 service restart could silently fail to stop a temporarily unresponsive managed service (health probe timeout 2s, find returns undefined) and then ensure() would reuse the original process when it recovered, so restart had a success path where it did not replace the service at all.

Fix: Service.stop now returns { stopped, reason } with distinct outcomes for missing registration, stale-registration, still-registered, timeout, and error, preserving stale-registration and reused-PID safety via same() checks and process.kill(pid, 0). Service.restart now checks stopResult.stopped and fails with actionable error 'Service restart failed to stop incumbent (reason: ...). Explicit restart never proceeds to ensure() after an unconfirmed stop.' instead of silently continuing to ensure(). Successful restart now verifies the new instance has a different PID/identity.

Preserves ordinary ensure/reconnect idempotent reuse semantics; only explicit restart requires replacement. Covers missing registration, corrupt registration, unreachable health endpoint, rejected/unsupported stop, graceful timeout/escalation, successful replacement, and stale PID protection. Logs whether stop was accepted, unsupported, rejected, timed out, escalated, or skipped.

Add deterministic regression test service-restart-race.test.ts proving explicit restart does not silently reuse unresponsive incumbent.

Closes anomalyco#37795
…ction (fixes anomalyco#38897)

The V2 managed service inherits the environment of whichever reconnecting client wins election. When several clients reconnect after a restart they race to spawn contenders with different PATH values; the winner's PATH becomes the server's global PATH and shell commands like curl flip between found/missing.

Derive the service's PATH from the user's login shell (bounded -l/-il probe, cached, no secrets) instead of the winning client's inherited PATH. spawnServiceContender now sets PATH to the login shell's canonical PATH (via getDeterministicPath) unless the caller explicitly provides PATH, preserving test overrides. Fallback is a fixed system PATH when the probe is unavailable.

Add deterministic regression tests proving repeated elections with different client PATHs cannot change command resolution (10 elections with/without fake curl in PATH all give same result, and explicit PATH is still respected).

Preserves normal reconnect/ensure behavior and does not persist secrets.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Aug 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Aug 29, 2026
@github-actions github-actions Bot closed this Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant