-
Notifications
You must be signed in to change notification settings - Fork 0
fix: make update promotion crash-safe and trim speculative machinery #21
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
Merged
aorumbayev
merged 5 commits into
fix/test-hermeticity-structural
from
fix/update-manager-crash-safety
Jul 10, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4fa884a
fix: make update promotion crash-safe and trim speculative machinery
aorumbayev e8ed41e
fix: harden update cleanup against unvalidated deletes and false ready
aorumbayev 9335921
merge: pick up merge-notice race fix from base branch
aorumbayev 55ffd7e
test: skip the host-dedupe pin when the vendored source is absent
aorumbayev 2ac586d
merge: pick up CI merge-notice timeout fix
aorumbayev 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
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,82 @@ | ||
| import type { TuiPluginMeta } from "@opencode-ai/plugin/tui" | ||
| import { basename, dirname, join } from "node:path" | ||
| import { isAutomaticUpdateInstall, parseRelease } from "./updates" | ||
| import { | ||
| defaultFileSystem, | ||
| type FileSystem, | ||
| markerMatches, | ||
| stat, | ||
| type UpdateMarker, | ||
| type UpdatePaths, | ||
| updatePaths, | ||
| validateWrapper, | ||
| } from "./update-paths" | ||
|
|
||
| async function readMarker(fs: FileSystem, markerPath: string): Promise<UpdateMarker | undefined> { | ||
| const info = await stat(fs, markerPath) | ||
| if (!info?.isFile()) return | ||
| try { | ||
| return JSON.parse(await fs.readFile(markerPath, "utf8")) as UpdateMarker | ||
| } catch { | ||
| await fs.rm(markerPath, { force: true }) | ||
| } | ||
| } | ||
|
|
||
| async function removeOrphanBackup( | ||
| fs: FileSystem, | ||
| paths: UpdatePaths, | ||
| target: string, | ||
| currentVersion: string, | ||
| marker: UpdateMarker | undefined, | ||
| matchesCurrent: boolean, | ||
| ) { | ||
| if (!(await stat(fs, paths.backup))) return | ||
| if (marker && matchesCurrent) return | ||
| await validateWrapper(fs, paths.current, target, currentVersion) | ||
| await fs.rm(paths.backup, { recursive: true }) | ||
| } | ||
|
|
||
| // Guard against a corrupted marker naming an arbitrary path: only sweep prepared dirs Kagan itself | ||
| // could have created — a `kagan@<x.y.z>` sibling of the current wrapper in the same scope cache. | ||
| function preparedInCache(preparedPath: string, paths: UpdatePaths): boolean { | ||
| if (dirname(preparedPath) !== dirname(paths.current)) return false | ||
| const name = basename(preparedPath) | ||
| if (!name.startsWith("kagan@")) return false | ||
| return parseRelease(name.slice("kagan@".length)) !== undefined | ||
| } | ||
|
|
||
| async function removeStaleMarker(fs: FileSystem, paths: UpdatePaths, marker: UpdateMarker) { | ||
| if (marker.prepared && preparedInCache(marker.prepared, paths)) { | ||
| await fs.rm(marker.prepared, { recursive: true, force: true }).catch(() => {}) | ||
| } | ||
| await fs.rm(paths.marker, { force: true }) | ||
| } | ||
|
|
||
| export async function cleanupPreparedUpdate( | ||
| meta: TuiPluginMeta, | ||
| currentVersion: string, | ||
| fs: FileSystem = defaultFileSystem, | ||
| ): Promise<void> { | ||
| if (!isAutomaticUpdateInstall({ source: meta.source, spec: meta.spec, version: currentVersion })) return | ||
| const paths = updatePaths(meta.target, currentVersion) | ||
| const marker = await readMarker(fs, paths.marker) | ||
| const matches = Boolean(marker && markerMatches(marker, paths, currentVersion)) | ||
|
|
||
| await removeOrphanBackup(fs, paths, meta.target, currentVersion, marker, matches) | ||
|
|
||
| if (marker && !matches) { | ||
| await removeStaleMarker(fs, paths, marker) | ||
| } | ||
|
|
||
| const markerForCleanup = await readMarker(fs, paths.marker) | ||
| if (!markerForCleanup || !markerMatches(markerForCleanup, paths, currentVersion)) return | ||
|
|
||
| await validateWrapper(fs, paths.current, meta.target, currentVersion) | ||
| if (await stat(fs, paths.backup)) { | ||
| await validateWrapper(fs, paths.backup, join(paths.backup, "node_modules", "@kagan-sh", "kagan")) | ||
| await fs.rm(paths.backup, { recursive: true }) | ||
| } | ||
| // Interrupted promotion + host re-download leaves the prepared dir behind; it is self-computed, so sweep it. | ||
| if (await stat(fs, paths.prepared)) await fs.rm(paths.prepared, { recursive: true }) | ||
| await fs.rm(paths.marker) | ||
| } | ||
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,60 @@ | ||
| import type { TuiPluginApi, TuiPluginMeta } from "@opencode-ai/plugin/tui" | ||
| import { cleanupPreparedUpdate } from "./update-cleanup" | ||
| import { prepareUpdate } from "./update-manager" | ||
| import { checkForUpdate, type UpdateStatus } from "./updates" | ||
| import { defaultFileSystem, type FileSystem } from "./update-paths" | ||
|
|
||
| export function showUpdateToast(api: TuiPluginApi, currentVersion: string, status: Exclude<UpdateStatus, undefined>) { | ||
| if (status.kind === "broken") return | ||
| if (api.route.current.name !== "home" && api.route.current.name !== "session") return | ||
| api.ui.toast({ | ||
| variant: status.kind === "ready" ? "success" : "warning", | ||
| title: "Kagan", | ||
| message: | ||
| status.kind === "ready" | ||
| ? `Kagan v${status.version} is ready. Restart OpenCode to apply.` | ||
| : `Kagan v${currentVersion} remains active. Kagan v${status.version} requires OpenCode ${status.requiredOpenCode}.`, | ||
| }) | ||
| } | ||
|
|
||
| export async function runAutomaticUpdateLaunch(input: { | ||
| api: TuiPluginApi | ||
| meta: TuiPluginMeta | ||
| currentVersion: string | ||
| now: number | ||
| setUpdateStatus: (status: UpdateStatus) => void | ||
| showToast?: typeof showUpdateToast | ||
| fetchImpl?: typeof fetch | ||
| fs?: FileSystem | ||
| }): Promise<void> { | ||
| const { api, meta, currentVersion, now, setUpdateStatus } = input | ||
| const showToast = input.showToast ?? showUpdateToast | ||
| const fs = input.fs ?? defaultFileSystem | ||
|
|
||
| try { | ||
| await cleanupPreparedUpdate(meta, currentVersion, fs) | ||
| } catch { | ||
| setUpdateStatus({ kind: "broken" }) | ||
| return | ||
| } | ||
|
|
||
| const status = await checkForUpdate({ | ||
| kv: api.kv, | ||
| currentVersion, | ||
| openCodeVersion: api.app.version, | ||
| source: meta.source, | ||
| spec: meta.spec, | ||
| now, | ||
| fetchImpl: input.fetchImpl, | ||
| }) | ||
|
|
||
| if (!status || api.lifecycle.signal.aborted) return | ||
|
|
||
| if (status.kind === "ready" && !(await prepareUpdate({ api, meta, currentVersion, status, fs }))) { | ||
| setUpdateStatus({ kind: "broken" }) | ||
| return | ||
| } | ||
|
|
||
| setUpdateStatus(status) | ||
| showToast(api, currentVersion, status) | ||
| } |
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.
If the process exits after
currentwas renamed tobackupand the marker is left malformed, this catch deletes the only recovery marker and returnsundefined. The interrupted-promotion branch never runs,currentstays missing, and each launch can remain stuck inupdates unavailableinstead of restoring from the validated backup.Context Used: AGENTS.md (source)
Artifacts
Repro: focused Bun harness for corrupt marker crash-recovery state
Repro: command output showing marker deletion with current still missing and backup still present
Prompt To Fix With AI