Files
Elydora-Python-SDK/elydora/plugins/_transaction_payload.py
T
ZacharyZhang-NY e37418c489 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.
2026-07-21 18:53:06 -04:00

81 lines
2.6 KiB
Python

"""Identity-bound population of journaled transaction reservations."""
from __future__ import annotations
import os
import stat
from ._managed_files import FileSnapshot
from ._pinned_directory import PinnedDirectory
def _write_all(descriptor: int, payload: bytes, label: str) -> None:
offset = 0
while offset < len(payload):
written = os.write(descriptor, payload[offset:])
if written <= 0:
raise OSError(f"Stage {label} made no progress")
offset += written
def populate_reserved_text(
directory: PinnedDirectory,
file_path: str,
reservation: FileSnapshot,
content: str,
mode: int,
label: str,
) -> FileSnapshot:
"""Fill one journal-owned empty file without changing its identity."""
name = directory.name_for(file_path)
current = directory.read_file(name, f"{label} reservation", 0)
if current != reservation or current.contents:
raise OSError(f"{label} reservation changed before staging: {file_path}")
flags = os.O_WRONLY | getattr(os, "O_BINARY", 0)
flags |= getattr(os, "O_CLOEXEC", 0) | getattr(os, "O_NOFOLLOW", 0)
descriptor = -1
payload = content.encode("utf-8")
try:
descriptor = directory.open_file(name, flags)
opened = os.fstat(descriptor)
if (
not stat.S_ISREG(opened.st_mode)
or stat.S_ISLNK(opened.st_mode)
or (opened.st_dev, opened.st_ino)
!= (reservation.device, reservation.inode)
or opened.st_size != 0
):
raise OSError(f"{label} reservation changed while opening: {file_path}")
os.ftruncate(descriptor, 0)
_write_all(descriptor, payload, label)
os.fsync(descriptor)
directory.chmod_descriptor(descriptor, mode)
os.fsync(descriptor)
finished = os.fstat(descriptor)
if (
not stat.S_ISREG(finished.st_mode)
or (finished.st_dev, finished.st_ino)
!= (reservation.device, reservation.inode)
or finished.st_size != len(payload)
):
raise OSError(f"{label} changed while staging: {file_path}")
finally:
if descriptor >= 0:
os.close(descriptor)
directory.sync()
snapshot = directory.read_file(name, label, len(payload))
if (
snapshot is None
or snapshot.contents != content
or (snapshot.device, snapshot.inode)
!= (reservation.device, reservation.inode)
or (os.name != "nt" and snapshot.mode != mode)
):
raise OSError(f"{label} changed after staging: {file_path}")
return snapshot
__all__ = ["populate_reserved_text"]