loopx icon indicating copy to clipboard operation
loopx copied to clipboard

[Bug]: native Windows: loopx backup-state --execute always fails because both durability fsync calls use read-only file handles

Open JasonBuildAI opened this issue 1 month ago • 0 comments

Preflight

  • [x] I searched existing issues and discussions for this behavior.
  • [x] This report is not an unpatched security vulnerability.
  • [x] The report and attachments contain no credentials or private/internal material.

Issue origin

Observed or reproduced in a real environment (native Windows 11, Python 3.12, Node.js 24.19.0, commit aedb376db).

LoopX version or commit

aedb376db (main).

Host or runtime surface

Codex CLI.

LoopX area

Control plane (goals, todos, quota, scheduler, registry, runtime).

Problem

Who is affected: every native-Windows user who runs the documented state-protection command loopx backup-state --execute (for example before a destructive reconnect, where the bootstrap command pack tells the operator to run backup-state first). On Windows the command cannot complete at all, and the failure message is not actionable.

The backup path performs two deliberate durability flushes. Both are correct on POSIX, and both fail on Windows only because the file being flushed is opened read-only:

  1. SQLite snapshot (hits every real state tree). When any SQLite file is in backup scope - every real LoopX state root has one - snapshotSqliteDatabase in loopx/control_plane/coordination/sqlite_backup.ts writes the snapshot and then flushes it through open(destination, "r") + handle.sync(). On Windows the flush (FlushFileBuffers) requires a handle with write access, so Node raises EPERM: operation not permitted, fsync (errno -4048) and the whole archive aborts. The user sees only "error": "Effect runtime filesystem operation failed", with no hint that fsync/Windows is involved.
  2. Archive and manifest durability (hits even without SQLite). At the end of execute_state_backup_plan in loopx/state_backup.py, the staged archive and manifest are reopened read-only (path.open("rb")) to os.fsync(handle.fileno()). On Windows that raises OSError: [Errno 9] Bad file descriptor, so even a backup with no SQLite file in scope fails.

Severity: backup-state --execute - the command the product itself recommends before destructive reconnect/upgrade - is unusable on Windows, and the only feedback is a generic effect-runtime error. Backups silently do not happen while the operator believes they were told to rely on that step.

Related precedent in the same tree: the TypeScript directory flush owners (syncAuthorityDirectory and syncDirectory) already skip the unsupported flush on win32, so the codebase treats Windows flush semantics as something to handle explicitly rather than assume POSIX behavior.

Minimal reproduction

On a native Windows host with a qualified Node.js (>= 22.22.3) on PATH and a source checkout:

  1. Create a runtime root that contains one SQLite file: python -c "import sqlite3, sys; db = sqlite3.connect(sys.argv[1] + r'\authority.sqlite'); db.execute('CREATE TABLE facts(value TEXT)'); db.commit(); db.close()" <runtime>
  2. Run the command with synthetic project/output directories: python -m loopx.cli --format json --runtime-root <runtime> backup-state --project <project> --output-dir <backups> --no-automations --no-skills --current-project-only --execute

The same reproduction without any SQLite file in the runtime root (only a text file) reaches the second defect instead of the first.

Expected behavior

The command writes the archive and manifest and reports {"ok": true, ...} on native Windows, as it already does on Linux/macOS.

Actual behavior

With an SQLite file in scope:

{"ok": false, "schema_version": "loopx_state_backup_v0", "mode": "state_backup",
 "dry_run": false, "execute_requested": true,
 "error": "Effect runtime filesystem operation failed",
 "recommended_action": "fix backup planning before retrying"}
exit=1

With no SQLite file in scope:

{"ok": false, ..., "error": "[Errno 9] Bad file descriptor", ...}
exit=1

Sanitized diagnostics

Direct probe of the two flush primitives on the same Windows host:

# Node.js (the SQLite snapshot flush)
mode=r:  EPERM -4048 EPERM: operation not permitted, fsync
mode=r+: sync OK

# Python (the archive/manifest flush)
mode=rb:  OSError errno=9 winerror=None Bad file descriptor
mode=r+b: fsync OK
mode=ab:  fsync OK

The repository's own regression coverage reproduces both failures on this host (they pass on Linux/macOS CI):

$ python -m pytest -q tests/test_state_backup.py
E  loopx.control_plane.effect_runtime.EffectRuntimePermanentIOError: Effect runtime filesystem operation failed   (2 x SQLite snapshot)
E  OSError: [Errno 9] Bad file descriptor                                                                        (junction/plain-file path)
E  subprocess.CalledProcessError: ... returned non-zero exit status 1                                            (real CLI path)
5 failed, 1 passed in 35.89s

$ node --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/sqlite_backup.test.ts
Error: EPERM: operation not permitted, fsync
    at async snapshotSqliteDatabase (loopx/control_plane/coordination/sqlite_backup.ts:32:11)

The Windows CI lane (windows-powershell in .github/workflows/python-tests.yml) does not run either suite, which is why this is invisible upstream.

Additional context

A fix will follow as a pull request: open the flush handles with update access ("r+" in the TypeScript snapshot owner, "rb+" in the Python archive owner) so the flush is permitted on Windows, and keep the existing durability guarantee unchanged on every platform. No platform branch or behavior change for POSIX hosts is needed. The two changed lines are already covered by the existing suites above, which fail before the change and pass after it on Windows.

AI disclosure: this report and the follow-up change were drafted with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 11 '26 17:10 JasonBuildAI