Repository navigation
Roll back both archive-managed runtime launchers #9007
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
Open
loopx-agent
wants to merge
7
commits into
main
Choose a base branch
from
codex/c1-paired-runtime-rollback-20261118
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+319
−48
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e556a65
fix(update): roll back both archive runtime launchers
loopx-agent 98e9e86
Merge commit '1083771794744d663eb8c025788bd6c116f8c113' into codex/c1…
loopx-agent 9dda43f
fix(update): recover archive launcher renames on interrupt
loopx-agent 6718868
Merge commit '7bc56e74687a93ff4d5bcccb532c454bf2379c29' into codex/c1…
loopx-agent 26aedee
fix(update): preserve launchers changed during archive rollback
loopx-agent 7fa1e68
Merge commit '45c015ffd9c9065b3af533e0abf103170627d15f' into codex/c1…
loopx-agent 52efe8d
fix(update): preserve concurrent launchers during rollback recovery
loopx-agent 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,11 @@ | |
| import os | ||
| import re | ||
| import shlex | ||
| import stat | ||
| import subprocess | ||
| import sys | ||
| import time | ||
| from tempfile import TemporaryDirectory | ||
| import zipimport | ||
| from typing import Any | ||
| from urllib.parse import quote | ||
|
|
@@ -1450,6 +1452,44 @@ def _verify_rollback_todos( | |
| raise RuntimeError(refusal) | ||
|
|
||
|
|
||
| def _rollback_launcher_stat(launcher: Path) -> os.stat_result | None: | ||
| try: | ||
| return launcher.lstat() | ||
| except FileNotFoundError: | ||
| return None | ||
|
|
||
|
|
||
| def _verify_rollback_launcher(launcher: Path, original: os.stat_result | None) -> None: | ||
| current = _rollback_launcher_stat(launcher) | ||
| if original is None: | ||
| unchanged = current is None | ||
| else: | ||
| unchanged = current is not None and os.path.samestat(original, current) | ||
| if not unchanged: | ||
| raise RuntimeError( | ||
| f"LoopX launcher changed during rollback checks ({launcher.name}). " | ||
| "Read it back before retrying." | ||
| ) | ||
|
|
||
|
|
||
| def _managed_rollback_helper_target( | ||
| helper: Path, releases_dir: Path, original: os.stat_result | None, | ||
| ) -> str | None: | ||
| if original is None or not stat.S_ISLNK(original.st_mode): | ||
| return None | ||
| previous = os.readlink(helper) | ||
| # Allow a prior partial rollback: either release can own this sibling. | ||
| # Check the archive launcher path even if that release was pruned. | ||
| script = helper.parent / previous | ||
| if ( | ||
| script.name == helper.name | ||
| and script.parent.name == "scripts" | ||
| and script.parent.parent.parent.resolve() == releases_dir.resolve() | ||
| ): | ||
| return previous | ||
| return None | ||
|
|
||
|
|
||
| def execute_rollback_plan( | ||
| payload: dict[str, Any], | ||
| *, | ||
|
|
@@ -1478,17 +1518,32 @@ def execute_rollback_plan( | |
| target_script = _release_script(release_root) | ||
| route_arguments = _runtime_route_arguments(registry_path, runtime_root) | ||
| loopx_bin = _user_loopx_bin(home) | ||
| previous_link_target = os.readlink(loopx_bin) if loopx_bin.is_symlink() else None | ||
| primary_stat = _rollback_launcher_stat(loopx_bin) | ||
| previous_link_target = ( | ||
| os.readlink(loopx_bin) if primary_stat is not None and stat.S_ISLNK(primary_stat.st_mode) else None | ||
| ) | ||
| primary_was_absent = primary_stat is None | ||
| original_launchers = {loopx_bin: primary_stat} | ||
| previous_targets = {loopx_bin: previous_link_target} | ||
| target_scripts = {loopx_bin: target_script} | ||
| helper = loopx_bin.with_name("loopx-apply-rrule") | ||
| execution: dict[str, Any] = { | ||
| "target_script": str(target_script), | ||
| "loopx_command": str(loopx_bin), | ||
| "previous_link_target": previous_link_target, | ||
| "restored_previous_on_failure": False, | ||
| } | ||
| updated = dict(payload) | ||
| switched = False | ||
| staged_launchers: dict[Path, os.stat_result] = {} | ||
| temporary: TemporaryDirectory[str] | None = None | ||
| promotion_guard = None | ||
| try: | ||
| helper_stat = _rollback_launcher_stat(helper) | ||
| previous_helper = _managed_rollback_helper_target(helper, release_root.parent, helper_stat) | ||
| if previous_helper is not None: | ||
| original_launchers[helper] = helper_stat | ||
| previous_targets[helper] = previous_helper | ||
| target_scripts[helper] = target_script.with_name(helper.name) | ||
| compatible = subprocess.run( | ||
| [str(target_script), *route_arguments, "--format", "json", "authority-archive", "upgrade", | ||
| "--all-known", "--require-current"], | ||
|
|
@@ -1517,17 +1572,29 @@ def execute_rollback_plan( | |
| time.sleep(min(0.1, remaining)) | ||
| if registry_path is not None: | ||
| _verify_rollback_todos(target_script, registry_path, runtime_root, timeout_seconds=timeout_seconds) | ||
| observed_link = os.readlink(loopx_bin) if loopx_bin.is_symlink() else None | ||
| if observed_link != previous_link_target: | ||
| raise RuntimeError("LoopX launcher changed during rollback checks. Read it back before retrying.") | ||
| if previous_helper is not None: | ||
| helper_script = target_scripts[helper] | ||
| if not helper_script.is_file() or not os.access(helper_script, os.X_OK): | ||
| raise RuntimeError( | ||
| "Rollback target lacks an executable loopx-apply-rrule. " | ||
| "Choose a release containing both managed launchers." | ||
| ) | ||
| if previous_link_target is None and not primary_was_absent: | ||
| raise RuntimeError( | ||
| "Cannot roll back the managed launcher pair over a regular loopx command." | ||
| ) | ||
| loopx_bin.parent.mkdir(parents=True, exist_ok=True) | ||
| temp_link = loopx_bin.with_name(f".{loopx_bin.name}.rollback.{os.getpid()}") | ||
| if temp_link.exists() or temp_link.is_symlink(): | ||
| temp_link.unlink() | ||
| temp_link.symlink_to(target_script) | ||
| promoted_link_stat = temp_link.lstat() | ||
| os.replace(temp_link, loopx_bin) | ||
| switched = True | ||
| temporary = TemporaryDirectory(prefix=".loopx-rollback-", dir=loopx_bin.parent) | ||
| staging = Path(temporary.name) | ||
| for launcher, script in target_scripts.items(): | ||
| _verify_rollback_launcher(launcher, original_launchers[launcher]) | ||
| staged = staging / launcher.name | ||
| staged.symlink_to(script) | ||
| staged_launchers[launcher] = staged.lstat() | ||
| # A signal can arrive immediately after the rename. Record ownership first. | ||
| for launcher in staged_launchers: | ||
| _verify_rollback_launcher(launcher, original_launchers[launcher]) | ||
| os.replace(staging / launcher.name, launcher) | ||
| doctor_result = subprocess.run( | ||
| [str(loopx_bin), *route_arguments, "--format", "json", "doctor"], | ||
| text=True, encoding="utf-8", errors="replace", | ||
|
|
@@ -1574,37 +1641,58 @@ def execute_rollback_plan( | |
| updated["ok"] = False | ||
| if doctor_result.returncode != 0: | ||
| raise RuntimeError("Rollback target failed runtime checks; inspect the Doctor output.") | ||
| except Exception as exc: | ||
| for launcher, staged_stat in staged_launchers.items(): | ||
| if not os.path.samestat(staged_stat, launcher.lstat()): | ||
| raise RuntimeError(f"LoopX launcher changed during rollback validation ({launcher.name}).") | ||
| except BaseException as exc: | ||
| execution["error"] = str(exc) | ||
| updated["ok"] = False | ||
| if switched and previous_link_target: | ||
| restore_link = loopx_bin.with_name( | ||
| f".{loopx_bin.name}.rollback.restore.{os.getpid()}" | ||
| ) | ||
| try: | ||
| if os.path.samestat(promoted_link_stat, loopx_bin.lstat()): | ||
| if restore_link.exists() or restore_link.is_symlink(): | ||
| restore_link.unlink() | ||
| restore_link.symlink_to(previous_link_target) | ||
| os.replace(restore_link, loopx_bin) | ||
| execution["restored_previous_on_failure"] = True | ||
| except Exception as restore_exc: | ||
| execution["restore_error"] = str(restore_exc) | ||
| restore_errors = [] | ||
| if temporary is not None: | ||
| for launcher, staged_stat in reversed(staged_launchers.items()): | ||
| try: | ||
| try: | ||
| current_stat = launcher.lstat() | ||
| except FileNotFoundError: | ||
| if launcher == loopx_bin and primary_was_absent: | ||
| continue | ||
| raise | ||
| if not os.path.samestat(staged_stat, current_stat): | ||
| continue | ||
| previous = previous_targets[launcher] | ||
| if previous is not None: | ||
| staged = Path(temporary.name) / launcher.name | ||
| staged.symlink_to(previous) | ||
| # Preparing the restore link can overlap another publisher. | ||
| current_stat = _rollback_launcher_stat(launcher) | ||
| if current_stat is None or not os.path.samestat(staged_stat, current_stat): | ||
| continue | ||
| os.replace(staged, launcher) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] 请在恢复暂存链接后再次核对目标 inode。File 和 SQLite 上,核心 Doctor 失败后,回退先读到自己发布的助手 inode 并暂存旧链接。此时独立进程原子替换现行助手,随后这次 rename 又覆盖了较新的 inode。请将第二次归属校验放在恢复写入前,并补上这个恢复窗口的测试。 |
||
| if launcher == loopx_bin: | ||
| execution["restored_previous_on_failure"] = True | ||
| elif primary_was_absent: | ||
| launcher.unlink() | ||
| except OSError as restore_exc: | ||
| restore_errors.append(f"{launcher.name}: {restore_exc}") | ||
| if restore_errors: | ||
| execution["restore_error"] = "\n".join(restore_errors) | ||
| if not isinstance(exc, Exception): | ||
| raise | ||
| finally: | ||
| if "temp_link" in locals() and (temp_link.exists() or temp_link.is_symlink()): | ||
| temp_link.unlink() | ||
| if "restore_link" in locals() and ( | ||
| restore_link.exists() or restore_link.is_symlink() | ||
| ): | ||
| restore_link.unlink() | ||
| if promotion_guard is not None: | ||
| promotion_guard.close() | ||
| try: | ||
| if temporary is not None: | ||
| temporary.cleanup() | ||
| finally: | ||
| if promotion_guard is not None: | ||
| promotion_guard.close() | ||
| updated["execution"] = execution | ||
| if updated["ok"]: | ||
| updated["recommended_action"] = "rollback complete; review doctor output" | ||
| elif execution.get("doctor_returncode") == 0 and execution.get( | ||
| "extension_doctor_returncode" | ||
| ) not in {None, 0}: | ||
| elif ( | ||
| "error" not in execution | ||
| and execution.get("doctor_returncode") == 0 | ||
| and execution.get("extension_doctor_returncode") not in {None, 0} | ||
| ): | ||
| updated["recommended_action"] = ( | ||
| "rollback activated, but one or more enabled extensions remain fail " | ||
| "closed; repair the provider and run `loopx extension doctor " | ||
|
|
@@ -1660,6 +1748,8 @@ def render_update_plan_markdown(payload: dict[str, Any]) -> str: | |
| lines.extend(["", f"### {label} {stream}", "", "```text", str(output), "```"]) | ||
| if execution.get("error"): | ||
| lines.extend(["", f"Error: {execution['error']}"]) | ||
| if execution.get("restore_error"): | ||
| lines.extend(["", f"Recovery error: {execution['restore_error']}"]) | ||
| return "\n".join(lines) + "\n" | ||
|
|
||
| source = payload.get("source") if isinstance(payload.get("source"), dict) else {} | ||
|
|
||
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.
[P2] 第二次 rename 前复检 helper 的当前归属。 这里使用的是暂存时的观察。实际切换主 CLI 后,让独立程序替换尚未写过的 helper。下一次 rename 覆盖了较新的 inode,随后 SIGINT 使恢复写回早先捕获的链接。File 和 SQLite 都可复现。请在每个入口自己的 rename 前复检原观察,发现变化时中止并只恢复本次仍拥有的写入。