feat(python): durable transaction subsystem and Kiro IDE 1.0 adapter

Land the crash-recoverable managed-file transaction engine (journal,
staging, commit, restart recovery, opaque snapshots, private artifacts,
Windows DACL hardening) and the Kiro IDE 1.0 workspace hook plugin with
legacy migration.

Repair five defects that kept the suite red (426 failures to zero):

- Anchor the transaction state directory at ~/.elydora/transactions
  instead of the shared system temp directory. The recovery journal now
  shares fate with the runtimes it repairs, survives temp cleaners, and
  isolates naturally under per-test fake homes.
- Skip pending-transaction recovery when no state directory exists so
  rejected installs never create runtime directories.
- Create the runtime root's parent before hardening it, matching the Go
  SDK's expand behavior for fresh homes.
- Stop using file timestamps as identity evidence: Windows exposes
  mtime/ctime with delayed cross-handle visibility. Reads now prove
  stability through a verification re-read; snapshots compare device,
  inode, size, and SHA-256.
- Stop comparing synthetic Windows permission bits across stat
  boundaries: path stats derive execute bits from file extensions while
  handle stats do not, which falsely flagged every managed .cmd wrapper.
  Snapshots store raw modes for restoration and compare portable bits.

Reservation cleanup now verifies content before discarding so a
concurrent writer's file at a rollback path is preserved, and the
install-failure tests assert the precise contract: no agent runtime
residue, with the durable state directory allowed to remain.

Gates: pytest 807 passed, mypy clean, pip check clean, wheel builds.
This commit is contained in:
ZacharyZhang-NY committed 2026-07-21 18:53:06 -04:00
1 parent dce7d1288a
commit e37418c489
53 files changed
+11855 -531

No files matched your search

+38 -18
View File
@@ -73,17 +73,21 @@ def test_install_restores_config_and_four_runtimes_when_final_commit_fails(
fixture = prepare_fixture(
monkeypatch, tmp_path, existing_config=original
)
real_replace = _transaction.os.replace
real_replace = _transaction._replace_physical
failed = False
def fail_config_once(source: str, destination: str) -> None:
def fail_config_once(
source: str,
destination: str,
directory: _transaction.PinnedDirectory,
) -> None:
nonlocal failed
if os.path.abspath(destination) == str(fixture.config_path) and not failed:
failed = True
raise OSError("injected Grok config failure")
real_replace(source, destination)
real_replace(source, destination, directory)
monkeypatch.setattr(_transaction.os, "replace", fail_config_once)
monkeypatch.setattr(_transaction, "_replace_physical", fail_config_once)
with pytest.raises(OSError, match="injected Grok config failure"):
fixture.install()
@@ -132,10 +136,14 @@ def test_uninstall_preserves_config_when_commit_fails(
)
changes = grok_installation.prepare_grok_uninstall(rendered)
def fail_replace(_source: str, _destination: str) -> None:
def fail_replace(
_source: str,
_destination: str,
_directory: _transaction.PinnedDirectory,
) -> None:
raise OSError("injected Grok uninstall failure")
monkeypatch.setattr(_transaction.os, "replace", fail_replace)
monkeypatch.setattr(_transaction, "_replace_physical", fail_replace)
with pytest.raises(OSError, match="injected Grok uninstall failure"):
grok_installation.commit_grok_uninstall(changes)
@@ -152,20 +160,24 @@ def test_recovery_preserves_original_after_committed_file_changes(
original = fixture.runtime_config_path.read_text(encoding="utf-8")
fixture.config["org_id"] = "org-updated"
changes = prepare_installation(fixture)
real_replace = _transaction.os.replace
real_replace = _transaction._replace_physical
calls = 0
def change_first_then_fail(source: str, destination: str) -> None:
def change_first_then_fail(
source: str,
destination: str,
directory: _transaction.PinnedDirectory,
) -> None:
nonlocal calls
calls += 1
if calls == 1:
real_replace(source, destination)
real_replace(source, destination, directory)
write_text(Path(destination), "external change\n")
return
raise OSError("injected later commit failure")
monkeypatch.setattr(
_transaction.os, "replace", change_first_then_fail
_transaction, "_replace_physical", change_first_then_fail
)
with pytest.raises(OSError, match="original content preserved at"):
@@ -189,21 +201,29 @@ def test_recovery_preserves_original_after_restore_failure(
bytes(range(31, -1, -1))
)
changes = prepare_installation(fixture)
real_replace = _transaction.os.replace
real_replace = _transaction._replace_physical
commits = 0
def fail_commit_and_restore(source: str, destination: str) -> None:
def fail_later_commit(
source: str,
destination: str,
directory: _transaction.PinnedDirectory,
) -> None:
nonlocal commits
if source.endswith(".rollback"):
raise OSError("injected rollback failure")
commits += 1
if commits == 2:
raise OSError("injected later commit failure")
real_replace(source, destination)
real_replace(source, destination, directory)
monkeypatch.setattr(
_transaction.os, "replace", fail_commit_and_restore
)
def fail_restore(
_source: str,
_destination: str,
_directory: _transaction.PinnedDirectory,
) -> None:
raise OSError("injected rollback failure")
monkeypatch.setattr(_transaction, "_replace_physical", fail_later_commit)
monkeypatch.setattr(_transaction, "_restore_physical", fail_restore)
with pytest.raises(OSError, match="original content preserved at"):
grok_installation.commit_grok_installation(changes)