Repository navigation
fix(cli): restart via stop/start when CLI is newer than proxy #6548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
agentHits
wants to merge
7
commits into
lidge-jun:dev
from
agentHits:agentHits/cli-restart-update-on-skew
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
99b3061
fix(cli): restart via stop/start when CLI is newer than proxy
agentHits a903fb5
fix(cli): carry attested restart identity through guarded update restart
agentHits 4186bb7
Merge remote-tracking branch 'upstream/dev' into agentHits/cli-restar…
agentHits 6df30d4
Merge remote-tracking branch 'upstream/dev' into agentHits/cli-restar…
agentHits f47cb93
Merge remote-tracking branch 'upstream/dev' into agentHits/cli-restar…
agentHits 8b3b8dd
Merge remote-tracking branch 'upstream/dev' into agentHits/cli-restar…
agentHits f9e916d
Merge remote-tracking branch 'upstream/dev' into agentHits/cli-restar…
agentHits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /** | ||
| * Identity-carrying coordinator for the CLI-newer update restart. | ||
| * | ||
| * An in-place restart respawns the live process from its own installation, so a | ||
| * restart accepted from a newer CLI would keep serving the old build. Instead of | ||
| * refusing (or stopping whatever happens to be live), this coordinator carries | ||
| * the attested restart identity (PID/port) through every step and revalidates it | ||
| * under the ownership lease through the existing guarded-stop mechanism | ||
| * (runApprovedStop): a changed, unknown, or non-runtime owner refuses before any | ||
| * stop is attempted, the start runs only after the intended target shows | ||
| * stopped, and the replacement is accepted only when it serves this exact CLI | ||
| * version on the expected port. Every boundary is injected for tests; no real | ||
| * service is touched by the regression suite. | ||
| */ | ||
| import type { LiveProxy } from "../server/proxy-liveness"; | ||
| import { runResolve, type ResolveJson } from "./resolve"; | ||
| import type { StopApproval } from "./stop-approval"; | ||
| import type { StopOutcome } from "./stop-report"; | ||
| import type { ProxyRestartStartOutcome } from "./tray-proxy"; | ||
|
|
||
| /** Attested restart identity observed before the CLI-newer refusal. */ | ||
| export interface RestartUpdateTarget { | ||
| pid: number; | ||
| port: number; | ||
| } | ||
|
|
||
| export type RestartUpdateRefusal = | ||
| | "target-changed" | ||
| | "stop-refused" | ||
| | "stop-failed" | ||
| | "target-not-confirmed-stopped" | ||
| | "start-failed" | ||
| | "replacement-unverified"; | ||
|
|
||
| export type RestartUpdateResult = | ||
| | { ok: true; live: LiveProxy } | ||
| | { ok: false; reason: RestartUpdateRefusal; detail?: string }; | ||
|
|
||
| export interface RestartUpdateIo { | ||
| findLive: () => Promise<LiveProxy | null>; | ||
| resolve: () => Promise<ResolveJson | null>; | ||
| stopGuarded: (approval: StopApproval) => Promise<StopOutcome>; | ||
| startWhenStopped: (recoveringLiveRestart: boolean) => Promise<ProxyRestartStartOutcome>; | ||
| now?: () => number; | ||
| sleep?: (ms: number) => Promise<void>; | ||
| cliVersion: string; | ||
| } | ||
|
|
||
| /** Bounded re-observation shared by the stopped and replacement waits. */ | ||
| async function pollLive( | ||
| findLive: () => Promise<LiveProxy | null>, | ||
| deadlineAt: number, | ||
| now: () => number, | ||
| sleep: (ms: number) => Promise<void>, | ||
| accept: (live: LiveProxy | null) => boolean, | ||
| ): Promise<LiveProxy | null> { | ||
| for (;;) { | ||
| const live = await findLive(); | ||
| if (accept(live)) return live; | ||
| const remaining = deadlineAt - now(); | ||
| if (remaining <= 0) return null; | ||
| await sleep(Math.min(250, remaining)); | ||
| } | ||
| } | ||
|
|
||
| /** Production resolve reader: one JSON document, null on any other outcome. */ | ||
| export async function readResolveJson(): Promise<ResolveJson | null> { | ||
| const lines: string[] = []; | ||
| const code = await runResolve({ json: true }, { | ||
| stdout: { log: (line: string) => { lines.push(line); } }, | ||
| stderr: { error: () => {} }, | ||
| }); | ||
| if (code !== 0 || lines.length !== 1) return null; | ||
| try { | ||
| return JSON.parse(lines[0] as string) as ResolveJson; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| const STOPPED_CONFIRM_BUDGET_MS = 10_000; | ||
|
|
||
| export async function runRestartUpdate( | ||
| target: RestartUpdateTarget, | ||
| deadlineAt: number, | ||
| io: RestartUpdateIo, | ||
| ): Promise<RestartUpdateResult> { | ||
| const now = io.now ?? Date.now; | ||
| const sleep = io.sleep ?? Bun.sleep; | ||
| const fail = (reason: RestartUpdateRefusal, detail?: string): RestartUpdateResult => | ||
| ({ ok: false, reason, ...(detail === undefined ? {} : { detail }) }); | ||
| const resolved = await io.resolve(); | ||
| if (resolved === null) return fail("target-changed", "the runtime could not be re-resolved"); | ||
| const liveness = resolved.liveness; | ||
| if (liveness.status !== "live" || liveness.pid === null || liveness.port === null) { | ||
| return fail("target-changed", "the approved runtime is no longer live"); | ||
| } | ||
| if (liveness.pid !== target.pid || liveness.port !== target.port) { | ||
| return fail("target-changed", "the live PID/port no longer match the attested restart target"); | ||
| } | ||
| if (liveness.source !== "runtime") { | ||
| return fail("target-changed", "the live runtime is no longer attested"); | ||
| } | ||
| if (liveness.role === "client") { | ||
| return fail("target-changed", "the runtime now routes as a client"); | ||
| } | ||
| const approval: StopApproval = { | ||
| pid: target.pid, | ||
| port: target.port, | ||
| hostname: liveness.hostname ?? "", | ||
| configHome: resolved.configHome, | ||
| cliVersion: io.cliVersion, | ||
| compatibilityToken: resolved.takeover.kind === "supported" ? resolved.takeover.token : "", | ||
| }; | ||
| const stopped = await io.stopGuarded(approval); | ||
| if (!stopped.ok) { | ||
| const refused = stopped.summary.outcome === "approval-changed"; | ||
| return fail(refused ? "stop-refused" : "stop-failed", stopped.summary.message); | ||
| } | ||
| const stoppedDeadline = Math.min(deadlineAt, now() + STOPPED_CONFIRM_BUDGET_MS); | ||
| const gone = await pollLive(io.findLive, stoppedDeadline, now, sleep, | ||
| live => live === null || live.port !== target.port); | ||
| if (gone === null && now() >= stoppedDeadline) { | ||
| return fail("target-not-confirmed-stopped", "the expected port still answers after the stop"); | ||
| } | ||
| const started = await io.startWhenStopped(true); | ||
| if (started.status !== "started") { | ||
| return fail("start-failed", "the replacement proxy did not start"); | ||
| } | ||
| const replacement = await pollLive(io.findLive, deadlineAt, now, sleep, | ||
| live => live !== null && live.source === "runtime" && live.port === target.port | ||
| && live.pid !== null && live.version === io.cliVersion); | ||
| if (replacement === null) { | ||
| return fail("replacement-unverified", "no healthy replacement serving this CLI version appeared in time"); | ||
| } | ||
| return { ok: true, live: replacement }; | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
rg -n 'restartViaStopStartForUpdate|restart_version_skew_cli_newer|handleProxyRestart|stop.start|version mismatch' tests/cli src/cli/index.tsRepository: lidge-jun/opencodex
Length of output: 1202
🏁 Script executed:
Repository: lidge-jun/opencodex
Length of output: 42139
🤖 get_repo_knowledge executed:
get_repo_knowledge lidge-jun/opencodex /tmp/coderabbit-repo-knowledge/lidge-jun-opencodex-7afea732/conventions /tmp/coderabbit-repo-knowledge/lidge-jun-opencodex-7afea732/learningsLength of output: 38270
🏁 Script executed:
Repository: lidge-jun/opencodex
Length of output: 42422
Add focused coverage for the CLI update-restart path.
tests/cli/system-restart-client.test.ts:150-178checks the skew error code and that the client sends one request. It does not exercise the CLI fallback insrc/cli/index.ts:923-981or verify the stop/start and post-start checks. Add cases for stop failure, a non-startedoutcome, no live proxy, a version mismatch, and success.🤖 Prompt for AI Agents