Skip to content

fix(local): replace install state atomically - #934

Open
rudycelekli wants to merge 2 commits into
Tencent:mainfrom
rudycelekli:fix/local-atomic-install-state
Open

rudycelekli wants to merge 2 commits into
Tencent:mainfrom
rudycelekli:fix/local-atomic-install-state

Conversation

@rudycelekli

@rudycelekli rudycelekli commented Nov 16, 2026 •

Copy link
Copy Markdown

What changed

The Local installer previously wrote .devflow/install-state.json directly, so an interrupted write could leave its previous inventory empty or incomplete. Save now stages complete JSON beside the inventory, flushes and fsyncs the staged file, then publishes it with os.replace.

The replacement preserves an existing inventory's POSIX permission bits. A new inventory uses the original creation behavior (0666 filtered by the process umask), allowing shared-workspace readers to retain access. Failed replacement keeps the previous bytes intact and removes the staged file. The fsync covers staged file contents; this does not claim directory-entry durability after power loss.

Scope and compatibility

  • Shared Local installer / CLI behavior for Classic and Portable; unchanged JSON schema and user-file conflict rules.
  • Regression coverage checks failed replacement, existing mode 0640, and new files under umasks 022, 027, and 077.

Current validation

Python 3.12.11 and Node 24.19.0, using disposable task-local packaging tools and caches:

  • python3 scripts/build-classic-hosts.py --check — passed.
  • bash scripts/validate.sh — passed.
  • bash scripts/smoke-install.sh — passed, including 27 installer tests, Portable tests, wheel installation and permission-hook regression checks.
  • bash scripts/smoke-npm.sh — passed.
  • bash scripts/smoke-curl-install.sh — passed against localhost fixtures.
  • bash scripts/e2e.sh — passed (34 tests, one browser module skipped because Playwright is unavailable).
  • bash scripts/test-observability.sh — passed for all three Classic bundles.
  • git diff --check — passed.

bash scripts/scan-secrets.sh could not run: its pinned helper supports Linux x86_64 only, while this environment is macOS ARM64. The public-content/credential-pattern checks in validate.sh passed; they do not substitute for that scanner. Full platform and hosted required CI have not been rerun locally and must be verified before merge. Initial sandbox/cache and missing-pip failures were resolved with disposable tooling; no shared Python environment was modified and no model/provider calls were made.

Safety

  • No credentials, private infrastructure, production data, or organization-specific configuration included.
  • Existing user files and unrelated work preserved.

@Yanami-2K Yanami-2K left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

改成临时文件 + os.replace 的方向是对的,测试也确实断言了替换失败时旧清单逐字节保留、没有残留临时文件。但有一处行为变化没有在描述里说明(描述写的是 "This changes only publication of the inventory"):

  1. local/src/devflow_cli/core.py save_state:tempfile.mkstemp 创建的文件权限固定是 0600,os.replace 后 .devflow/install-state.json 也变成 0600;原来的 path.write_text 按 umask 创建(通常是 0644)。项目目录被别的用户或容器内不同 uid 读取时(例如共享工作区、CI 以另一用户执行 loopforge status),升级后会读不到清单。建议在 os.replace 前对临时文件 os.chmod:已有清单就沿用它的 st_mode,没有就用 0o666 & ~umask;并在 test_failed_state_replace_preserves_previous_records 旁加一个断言:正常保存后权限与改动前一致。
  2. (非阻塞)os.replace 前没有 handle.flush() + os.fsync(),掉电时仍可能得到空文件。描述只覆盖进程中断,可以接受,但如果目标包括崩溃安全,建议一起补上。

另外,本 PR 只跑了开源扫描检查,validate / smoke-install 等 CI 没有触发,合入前请维护者触发完整 CI。

@rudycelekli
rudycelekli force-pushed the fix/local-atomic-install-state branch from 62137cc to b01aca4 Compare November 20, 2026 02:34
@rudycelekli

Copy link
Copy Markdown
Author

Addressed the permission regression and the optional flush/fsync suggestion in b01aca4 (rebased onto current main). Existing inventory permissions are preserved; new files use 0666 filtered by the process umask, without changing the process-wide umask. The staged file is flushed and fsynced before replacement; failure cleanup preserves the original inventory. This does not claim directory-entry durability after power loss.

Fresh local validation passed: generated-host check, full validate.sh, install smoke (27 installer tests plus Portable, wheel/CLI and permission-hook checks), npm smoke, localhost curl-install smoke, fake-host E2E (34 tests, one browser module skipped without Playwright), and observability tests for all three Classic bundles. git diff --check passed. The new regressions cover existing 0640 and new files under umasks 022, 027, and 077.

scan-secrets.sh remains blocked locally because its helper supports Linux x86_64 only; this environment is macOS ARM64. The content checks inside validate.sh passed. Full platform/hosted required CI still needs verification before merge; I have updated the PR description with the exact passed, skipped and blocked checks.

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