Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/desktop-replay.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ on:
- packages/flow/tests/test_check_it_dry_run_apps.py
- packages/flow/tests/native_apps/check_it_gtk.py
- packages/flow/tests/test_approval_form_window.py
- packages/flow/tests/test_binary_file_descriptors.py
- packages/flow/tests/_binary_descriptor_contract.py
- packages/flow/benchmark/rdp_pool/**
- packages/flow/pyproject.toml
- packages/flow/uv.lock
Expand Down Expand Up @@ -88,7 +90,7 @@ jobs:
run: |
mkdir -p runs
pytest -v -rs -p no:cacheprovider tests/test_windows_attempt_ledger.py \
tests/test_approval_form_window.py --junitxml=runs/windows-guards-junit.xml
tests/test_approval_form_window.py tests/test_binary_file_descriptors.py --junitxml=runs/windows-guards-junit.xml
- name: Record, compile and replay, judged from the record
env:
OAFLOW_WINDOWS_HOSTED_E2E: "1"
Expand Down
9 changes: 7 additions & 2 deletions packages/flow/docs/design/DECOMPOSITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,13 @@ PR #2280 raised `replayer.py` by 12 lines: the call sites of the clock-text chec
| File | Line budget |
|---|---:|
| `packages/flow/openadapt_flow/runtime/replayer.py` | 20094 |
| `packages/flow/openadapt_flow/__main__.py` | 14570 |
| `packages/flow/openadapt_flow/__main__.py` | 14571 |
| `packages/flow/openadapt_flow/qualification.py` | 5963 |
| `packages/flow/openadapt_flow/ir.py` | 6711 |
| `packages/desktop/openadapt_desktop/dispatch.py` | 6524 |
| `packages/capture/openadapt_capture/recorder.py` | 4836 |
| `packages/flow/openadapt_flow/interactive_recorder.py` | 4620 |
| `packages/flow/openadapt_flow/runtime/durable/attended.py` | 4367 |
| `packages/flow/openadapt_flow/runtime/durable/attended.py` | 4368 |
| `packages/flow/openadapt_flow/terminal_verification_v2.py` | 3673 |
| `packages/flow/openadapt_flow/backends/playwright_backend.py` | 6727 |
<!-- size-ratchet:end -->
Expand Down Expand Up @@ -665,3 +665,8 @@ Decided in review on 2026-10-24:
4. **Resolution rungs:** no second ladder is planned, so `resolver.resolve()`
stays one ladder. Checking identity against the system of record is a check
source (S5), not a ladder.

The binary descriptor policy adds one shared-helper import to `__main__.py`
and `runtime/durable/attended.py`. Their budgets increase by one line each;
byte-mode handling remains in the small private-file helper and the source
contract checks every raw-reader/writer entry point.
9 changes: 5 additions & 4 deletions packages/flow/openadapt_flow/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
from urllib.parse import urlsplit
from uuid import UUID

from openadapt_flow.private_file import open_binary
from openadapt_flow.vision_hint import vision_hint

if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -6640,7 +6641,7 @@ def _read_qualification_case_inputs(path: Path, *, workflow):
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
try:
descriptor = os.open(path, flags)
descriptor = open_binary(path, flags)
except OSError as exc:
raise ValueError(
"qualification case inputs could not be opened safely"
Expand Down Expand Up @@ -6723,7 +6724,7 @@ def private_directory(path: Path, *, migrate_owned_root: bool = False) -> Path:
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
try:
descriptor = os.open(path, flags)
descriptor = open_binary(path, flags)
except OSError as exc:
raise ValueError(
"qualification attempt ledger is not a private directory"
Expand Down Expand Up @@ -6811,7 +6812,7 @@ def private_directory(path: Path, *, migrate_owned_root: bool = False) -> Path:
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
try:
descriptor = os.open(marker, flags, 0o600)
descriptor = open_binary(marker, flags, 0o600)
except FileExistsError:
print(
"run REFUSED: this qualification case attempt already started in this "
Expand Down Expand Up @@ -6840,7 +6841,7 @@ def private_directory(path: Path, *, migrate_owned_root: bool = False) -> Path:
directory_flags |= os.O_DIRECTORY
if hasattr(os, "O_NOFOLLOW"):
directory_flags |= os.O_NOFOLLOW
directory = os.open(ledger_dir, directory_flags)
directory = open_binary(ledger_dir, directory_flags)
try:
os.fsync(directory)
finally:
Expand Down
9 changes: 6 additions & 3 deletions packages/flow/openadapt_flow/approval_mode/item_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
verify_grant,
)
from openadapt_flow.approval_mode.switch import canonical_json
from openadapt_flow.private_file import open_binary

REQUEST_SCHEMA: Literal["openadapt.item-approval-request/v1"] = (
"openadapt.item-approval-request/v1"
Expand Down Expand Up @@ -595,7 +596,7 @@ def _open_flags(base: int) -> int:


def _read_regular(path: Path, *, limit: int = _MAX_FILE_BYTES) -> bytes:
descriptor = os.open(path, _open_flags(os.O_RDONLY))
descriptor = open_binary(path, _open_flags(os.O_RDONLY))
try:
info = os.fstat(descriptor)
if not stat.S_ISREG(info.st_mode):
Expand All @@ -622,7 +623,9 @@ def _write_exclusive(path: Path, payload: bytes) -> None:
"""Create ``path`` once (0600), never replacing anything already there."""

tmp = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
descriptor = os.open(tmp, _open_flags(os.O_WRONLY | os.O_CREAT | os.O_EXCL), 0o600)
descriptor = open_binary(
tmp, _open_flags(os.O_WRONLY | os.O_CREAT | os.O_EXCL), 0o600
)
try:
view = memoryview(payload)
while view:
Expand All @@ -648,7 +651,7 @@ def _fsync_dir(path: Path) -> None:
if hasattr(os, "O_DIRECTORY"):
flags |= os.O_DIRECTORY
try:
descriptor = os.open(path, flags)
descriptor = open_binary(path, flags)
except OSError:
return
try:
Expand Down
5 changes: 3 additions & 2 deletions packages/flow/openadapt_flow/approval_mode/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
OffReason,
SwitchThresholds,
)
from openadapt_flow.private_file import open_binary

SWITCH_RECORD_SCHEMA: Literal["openadapt.approval-mode-switch/v1"] = (
"openadapt.approval-mode-switch/v1"
Expand Down Expand Up @@ -615,7 +616,7 @@ def _write_event(self, fields: dict[str, Any]) -> SwitchLogEvent:
# O_BINARY: on Windows a plain os.open writes in text mode and turns
# each "\n" into "\r\n", so the line would no longer be canonical.
flags = os.O_CREAT | os.O_WRONLY | os.O_APPEND | getattr(os, "O_BINARY", 0)
descriptor = os.open(self.path, flags, 0o600)
descriptor = open_binary(self.path, flags, 0o600)
try:
written = os.write(descriptor, line)
if written != len(line):
Expand All @@ -629,7 +630,7 @@ def _write_event(self, fields: dict[str, Any]) -> SwitchLogEvent:
def _lock(self) -> Iterator[None]:
self.path.parent.mkdir(parents=True, exist_ok=True)
lock_path = self.path.with_name(self.path.name + ".lock")
descriptor = os.open(
descriptor = open_binary(
lock_path, os.O_CREAT | os.O_RDWR | getattr(os, "O_BINARY", 0), 0o600
)
try:
Expand Down
6 changes: 4 additions & 2 deletions packages/flow/openadapt_flow/at_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
from pathlib import Path
from typing import Final, Optional

from openadapt_flow.private_file import open_binary

OPT_OUT_ENV: Final[str] = "OPENADAPT_FLOW_PLAINTEXT_AT_REST"
MODE_ENV: Final[str] = "OPENADAPT_FLOW_AT_REST"
LEDGER_NAME: Final[str] = "plaintext-at-rest.jsonl"
Expand Down Expand Up @@ -237,7 +239,7 @@ def _record(
},
sort_keys=True,
)
fd = os.open(ledger, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
fd = open_binary(ledger, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
try:
os.write(fd, (line + "\n").encode("utf-8"))
finally:
Expand Down Expand Up @@ -322,7 +324,7 @@ def _local_key(path: Path) -> str:
# fails if another process got there first: no reader ever sees a
# half-written key, and no key is ever replaced.
tmp = path.with_name(f".{KEY_NAME}.{os.getpid()}.{secrets.token_hex(4)}")
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
fd = open_binary(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
try:
os.write(fd, (text + "\n").encode("ascii"))
os.fsync(fd)
Expand Down
5 changes: 4 additions & 1 deletion packages/flow/openadapt_flow/backends/xfreerdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from typing import Any, Callable, Iterator, Literal, Optional, Sequence, TypeVar

from openadapt_flow.backends.rdp_backend import Framebuffer
from openadapt_flow.private_file import open_binary

_SCALES = (100, 140, 180)
_DISPLAY_LOCK = threading.Lock()
Expand Down Expand Up @@ -269,7 +270,9 @@ def _claim_display() -> int:
if n in _CLAIMED or not _display_free(n):
continue
try:
fd = os.open(_marker(n), os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
fd = open_binary(
_marker(n), os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600
)
except FileExistsError:
continue
os.write(fd, str(os.getpid()).encode())
Expand Down
4 changes: 3 additions & 1 deletion packages/flow/openadapt_flow/console/json_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from pathlib import Path
from typing import Any, Literal, Optional

from openadapt_flow.private_file import open_binary

MAX_ARTIFACT_BYTES = 5 * 1024 * 1024
MAX_DOCUMENT_NODES = 20_000
MAX_DOCUMENT_DEPTH = 100
Expand Down Expand Up @@ -323,7 +325,7 @@ def _read_bounded(artifact: JsonArtifact) -> bytes:
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
try:
fd = os.open(safe, flags)
fd = open_binary(safe, flags)
except OSError as exc:
raise JsonArtifactError(
"artifact is no longer available", status_code=404
Expand Down
3 changes: 2 additions & 1 deletion packages/flow/openadapt_flow/decision/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
JudgmentFactType,
LocalEvidenceRefV1,
)
from openadapt_flow.private_file import open_binary

LABEL_SCHEMA: Literal["openadapt.decision-label/v1"] = "openadapt.decision-label/v1"
#: Where an item sat in the plan. ``unscored`` is every item when triage is
Expand Down Expand Up @@ -150,7 +151,7 @@ def append(self, label: ApprovalLabelV1) -> None:
flags = os.O_WRONLY | os.O_CREAT | os.O_APPEND
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
descriptor = os.open(self.path, flags, 0o600)
descriptor = open_binary(self.path, flags, 0o600)
try:
os.write(descriptor, line)
os.fsync(descriptor)
Expand Down
3 changes: 2 additions & 1 deletion packages/flow/openadapt_flow/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
DeclaredSurface,
declared_surface_problem,
)
from openadapt_flow.private_file import open_binary
from openadapt_flow.remote_frame_contract import RemoteFrameContract

# Import-light (pydantic only): the effect CONTRACT types double as the
Expand Down Expand Up @@ -1493,7 +1494,7 @@ def pin_deployment_bytes(source: str | Path, sha256: str) -> bytes:
# O_BINARY: on Windows a plain os.open reads in text mode, turning
# CRLF into LF, so the bytes hashed wouldn't be the bytes written.
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_BINARY", 0)
fd = os.open(str(source), flags)
fd = open_binary(str(source), flags)
except OSError as exc:
raise ValueError(
f"deployment config {source} can't be opened as a plain file: {exc}"
Expand Down
7 changes: 4 additions & 3 deletions packages/flow/openadapt_flow/eligibility/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
eligibility_request_sha256,
parse_271,
)
from openadapt_flow.private_file import open_binary
from openadapt_flow.runtime.effects.document_hash import DocumentHashVerifier
from openadapt_flow.runtime.effects.effect import (
Effect,
Expand Down Expand Up @@ -180,7 +181,7 @@ def _write_exclusive(path: Path, payload: bytes) -> None:
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
fd = os.open(path, flags, 0o600)
fd = open_binary(path, flags, 0o600)
try:
view = memoryview(payload)
while view:
Expand All @@ -197,7 +198,7 @@ def _read_regular(path: Path) -> bytes:
flags = os.O_RDONLY
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
fd = os.open(path, flags)
fd = open_binary(path, flags)
try:
opened = os.fstat(fd)
if not stat.S_ISREG(opened.st_mode):
Expand All @@ -215,7 +216,7 @@ def _read_regular(path: Path) -> bytes:
def _fsync_dir(path: Path) -> None:
if os.name == "nt":
return
fd = os.open(path, os.O_RDONLY)
fd = open_binary(path, os.O_RDONLY)
try:
os.fsync(fd)
finally:
Expand Down
4 changes: 3 additions & 1 deletion packages/flow/openadapt_flow/fleet/_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from pathlib import Path
from typing import Iterator

from openadapt_flow.private_file import open_binary


@contextmanager
def exclusive_lock(path: Path) -> Iterator[None]:
"""Hold an exclusive lock on ``path`` (created if missing) while inside."""
path.parent.mkdir(parents=True, exist_ok=True)
descriptor = os.open(path, os.O_CREAT | os.O_RDWR, 0o600)
descriptor = open_binary(path, os.O_CREAT | os.O_RDWR, 0o600)
try:
if os.name == "nt":
import msvcrt
Expand Down
8 changes: 5 additions & 3 deletions packages/flow/openadapt_flow/hosted.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import httpx
import idna

from openadapt_flow.private_file import open_binary

__all__ = [
"DEFAULT_HOST",
"HostedError",
Expand Down Expand Up @@ -1953,7 +1955,7 @@ def read_existing(metadata: os.stat_result) -> str:
# opened descriptor back to this exact directory entry before reading
# any bytes; O_NOFOLLOW is an added symlink refusal where available.
try:
fd = os.open(id_path, flags)
fd = open_binary(id_path, flags)
except FileNotFoundError as exc:
raise HostedError(
"run report id changed while it was being opened"
Expand Down Expand Up @@ -2050,7 +2052,7 @@ def is_canonical_prefix(raw: bytes) -> bool:
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
try:
fd = os.open(id_path, flags, 0o600)
fd = open_binary(id_path, flags, 0o600)
except FileExistsError:
try:
existing = id_path.lstat()
Expand Down Expand Up @@ -2092,7 +2094,7 @@ def is_canonical_prefix(raw: bytes) -> bool:
directory_flags = os.O_RDONLY
if hasattr(os, "O_DIRECTORY"):
directory_flags |= os.O_DIRECTORY
directory_fd = os.open(run_path, directory_flags)
directory_fd = open_binary(run_path, directory_flags)
try:
os.fsync(directory_fd)
finally:
Expand Down
4 changes: 3 additions & 1 deletion packages/flow/openadapt_flow/no_outbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
import time
from typing import Any, Mapping, Optional, Sequence, Union

from openadapt_flow.private_file import open_binary

ENV_MODE = "OPENADAPT_NO_OUTBOUND"
ENV_ALLOW = "OPENADAPT_NO_OUTBOUND_ALLOW"
ENV_LOG = "OPENADAPT_NO_OUTBOUND_LOG"
Expand Down Expand Up @@ -269,7 +271,7 @@ def _record(event: str, host: Any, port: Any) -> None:
sort_keys=True,
)
try:
fd = os.open(_log_path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
fd = open_binary(_log_path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
try:
os.write(fd, (line + "\n").encode("utf-8"))
finally:
Expand Down
4 changes: 3 additions & 1 deletion packages/flow/openadapt_flow/onprem_reboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from pathlib import Path
from typing import Any, Callable, Mapping, Optional, Sequence

from openadapt_flow.private_file import open_binary

SETTINGS_SCHEMA = "openadapt.onprem-service-settings/v1"
NOTICE_SCHEMA = "openadapt.onprem-notice/v1"
EVENT_SCHEMA = "openadapt.onprem-event/v1"
Expand Down Expand Up @@ -160,7 +162,7 @@ def record_event(root: Path, event: str, **detail: Any) -> dict[str, Any]:
line = {"schema": EVENT_SCHEMA, "event": event, "at": _utc(), **detail}
path = events_path(root)
path.parent.mkdir(parents=True, exist_ok=True)
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
fd = open_binary(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
try:
os.write(fd, (json.dumps(line, sort_keys=True) + "\n").encode("utf-8"))
finally:
Expand Down
Loading
Loading