Skip to content

feat(flow): read-only local-state check for files and OS settings - #2420

Open
abrichr wants to merge 12 commits into
mainfrom
claude/local-state-check
Open

abrichr wants to merge 12 commits into
mainfrom
claude/local-state-check

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member

Why

Desktop examples like "turn off Night Shift" don't work today. The released runnable example is a sample web app in a hidden browser. A desktop task also needs a check after the run, and an OS setting or a saved file has no app record to read back. Without that check, a replay ends COMPLETED_UNVERIFIED; the Notepad lab runs below show exactly that.

What

A new record-check kind, effects.kind: local-state, for state that lives on this computer rather than in an app's database. It reads typed, closed sources through the operating system's own read-only interfaces:

  • file: contents (UTF-8, up to 1 MiB) and SHA-256, through read-only file I/O;
  • macos_defaults: one preference key, through /usr/bin/defaults export <domain> - (goes through cfprefsd);
  • windows_registry: one value under HKCU or HKLM, through winreg with KEY_READ.

There is no free-form command source: a source with any other type or an extra key is refused when the config loads. A missing file, key or value is a readable state (present: False), so "Light Mode" (no AppleInterfaceStyle) can be expected. Any other failed read (wrong OS, permission denied, defaults failing, a missing folder) makes the read unreadable, and the verdict is INDETERMINATE. The tier is independent-system (tier 1): the read goes to the store the state lives in, not the screen.

Examples are data in packages/flow/examples/local_state/ (notepad-save, windows-dark-mode). scripts/desktop/qualify_local_state.py is the lab harness. Docs: docs/LOCAL_STATE_CHECKS.md, EFFECT_KIT.md, and a CAPABILITIES row.

One finding for anyone writing a verify.yaml: verify-run also checks that nothing else in the read changed (the frame). A save moves sha256 and size along with text, so a file example must list them in frame.target_fields, or the receipt says reconciliation_required. The examples do; a test pins the behavior.

Evidence

Windows 11 Notepad, end to end, on the zenbook. Each trial: record the demonstration with the desktop recorder (record_desktop_demo over the lab's win_agent backend; the recorder types through the agent, as in the other Windows lab harnesses), compile, reset, take the checker's baseline, replay with a new note, then run openadapt-flow verify-run inside the guest, where the file lives.

Trials Harness Result
1-3 early Replay didn't complete (findings below); record-time checks confirmed
4 status bar off verified
5, 9 Stopped at a lab SSH timeout during setup, before replay
6 no focus setup Keys didn't reach Notepad; replay halted, check refuted
7, 8, 10 final verified, all three

Per-trial evidence (receipts, replay reports, recordings and frames for 7, 8, 10) is in openadapt-internal qualification/local-state-checks-20261121/ (branch claude/local-state-check-evidence).

Each accepted trial also has a skipped-replay control that comes back REFUTED and an unreadable path (missing folder) that comes back INDETERMINATE. Replay on its own reports COMPLETED_UNVERIFIED; verify-run turns that into verified (tier 1, independent_readback).

What it took to get there (lab setup, not engine changes):

  • Notepad's status bar must be off. Notepad's editor exposes no readable UIA value, so replay's typed-echo check falls back to whole-screen OCR, and the status bar's "28 characters / Ln 1, Col 29" counts as other added text. Replay then halts as unconfirmed. That's a replay limitation for full-window editors, not a checker problem; widening the agent's value reader is a separate change.
  • The demonstration doesn't click in the editor. A click in blank editor space has no UIA locator, and the visual resolver anchored it to the nearest text, the Edit menu.
  • The harness clears Notepad's restored tabs on reset (backed up first) and stops the ASUS OLED screensaver, which blocks screen capture.

Windows dark mode: check only. On the zenbook's real registry, verify-run with examples/local_state/windows-dark-mode/verify.yaml returns verified; the same contract expecting light returns REFUTED. The lab Windows is not activated, and Settings shows "You need to activate Windows before you can personalize your PC", with the mode control disabled. So it can't be demonstrated there, and there's no end-to-end run.

Not built, and why (also in the doc): Night Shift (CoreBrightness is private; no supported read of the current state), Notes (AppleScript or Shortcuts only, plus a consent prompt; the database is private), Calendar (EventKit is supported but needs a permission prompt and PyObjC, which isn't shipped today).

Tests: tests/test_local_state_verifier.py (stand-ins for defaults and winreg, so it runs on any OS and never reads this machine's settings), plus test_cli_deployment.py, test_effect_kit_config.py, test_verify_run.py, test_engine_generality_check.py, test_execution_profiles.py: 234 passed on the WSL test host at the merged head. The engine generality check passes.

Generality

  1. No per-app code in the engine. Yes. local_state.py names OS interfaces (file, preferences domain, registry), never an app or a setting. Apps and settings appear only in examples/ data and the lab harness.
  2. Fix the class, not the instance. Yes. One source model, judged by the shared judge_records, replaces a per-example checker. Files, preferences and registry values are three typed sources of one read.
  3. Held-out evidence. Partly. The registry source read a value that didn't motivate the design (HKCU\\Control Panel\\International\\sShortDate, CONFIRMED) and an absent value under an existing key (present: False, CONFIRMED). The file source was shown only on the Notepad example that motivated it. The macOS source has no real-OS run yet.

Not in this PR

  • No quickstart wiring. Nothing here is user-runnable yet: notepad-save is lab-qualified with a scripted demonstration through the lab agent, and the user path (openadapt-flow record --backend windows, a person typing) is untested. openadapt quickstart --own (feat(quickstart): openadapt quickstart --own, your own task from recording to a tool #2396) is a draft owned by the quickstart agent; an --example hook is proposed to them separately.
  • Encryption at rest for desktop recordings is another agent's work.
  • After the trials, one fail-closed change: a missing registry key and an empty preferences domain (what defaults export prints for a domain that doesn't exist) are unreadable, not absent. Unit-tested; no trial depended on either.
  • TextEdit and macOS Dark Mode examples are not included: the macOS VM needs 30 GiB free on the host, which had 25-30 GiB. The macos_defaults source is unit-tested only.

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: 5e516a7

First review of this PR. Author is GitHub abrichr, not Grok. The PR is a draft. Review only; I am not merging.

Fail-closed absence is implemented as claimed, and the tests cover it: a missing file is present: False only when its parent folder exists; a missing preferences key is absence only inside a non-empty exported domain (defaults export of an unknown domain is empty and is unreadable); a missing registry value is absence only under a key that OpenKey could open. Wrong OS, permission errors, a non-zero defaults exit, and unparsable output all make the whole read unreadable and cannot confirm absence.

The typed sources are closed and have no command string. defaults is invoked as /usr/bin/defaults export <domain> - with a domain that cannot start with -. The key is selected from the parsed plist, never passed on the command line. Registry access is KEY_READ on HKCU/HKLM only.

Please refuse non-regular files before reading. _read_file opens the path and then reads to EOF while hashing. A source (or a symlink) that points at /dev/zero, /dev/urandom, or a FIFO never ends, so the verifier can hang the run. stat first, and treat anything other than a regular file as unreadable. A size or time cap on huge regular files would also be wise.

The INDEPENDENT_SYSTEM claim is justified for macos_defaults (a separate defaults process through cfprefsd) and for windows_registry (the registry API, not the screen). File reads are same-process filesystem I/O of the actual store; that is still not the screen, and I am not asking for a tier change.

frame.target_fields: [present, text, sha256, size] on the Notepad example is the right fix for a save that moves hash and size with the text. The test that a hash-only change is collateral without those fields belongs here.

I am not merging.

…d budgets

Review blocker (fail-open class: unbounded-read-as-evidence): a file source
that is a FIFO, a device or a link to one could block the open or never
reach EOF and hang the run. A file source is now read only when it is a
regular file: it is checked before the open, opened with O_NONBLOCK, and
fstat-checked after. Anything else is unreadable, as is a file over 256 MiB
(also while it grows during the read). Tests cover a FIFO, a link to it,
/dev/null and the size cap.

CI: deployment imported up into runtime.effects.local_state, and the eager
runtime.effects export loaded one more module for ir and receipt than
their budgets allow. The typed source models move to
openadapt_flow/local_state_sources.py (policy layer, and added to the
protected-path manifest beside runtime/effects/**). The verifier is a lazy
export of openadapt_flow.runtime, like Replayer. local_state reads
sys.platform without binding the sys namespace, so the lazy export is no new
import-layer debt site. No budget or debt row changed.

The deployment schema snapshot is regenerated on Pydantic 2.13.4 for the new
effects.local_state field (#2424).
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: f4db48f

Gemini cross-vendor review of the protected delta (it addresses the earlier reviewer's request to refuse non-regular files) and of the full PR diff vs main. The summary below is from Gemini's own output; it reported no findings.

  • Open-time race and blocking. The file is stat-checked, opened with O_NONBLOCK, then fstat-checked on the opened descriptor. A regular file swapped for a FIFO cannot hang open(), and getattr(os, "O_NONBLOCK", 0) degrades safely on Windows.
  • Unbounded read. FILE_LIMIT (256 MiB) is enforced after the open and again during the chunked read, so a growing file can neither exhaust memory nor hang the run.
  • Model move. The validators (_check_id, _check_domain, _check_path) are preserved in openadapt_flow/local_state_sources.py. That file is added to the protected-path manifest and to the import-layer contract.
  • Lazy export. LocalStateVerifier is a lazy export of openadapt_flow.runtime. sys.platform is bound as a constant, so the import-time budgets hold.
  • Fail-closed. Every new refusal (not a regular file, too large) raises _Unreadable, so the read returns None and the verdict is INDETERMINATE. It never guesses a value or confirms absence.

@abrichr
abrichr marked this pull request as ready for review November 22, 2026 05:04
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: f5f4b42

Review only. I am not merging this PR. This is a delta stamp versus Gemini's APPROVE at f4db48fddd77acd54de985e412933f608d92b783 (my earlier REQUEST CHANGES was at 5e516a7130ec35a4b01caa5bd5e49d0386f4ce23).

The new unique work on top of the main merge is:

  • EffectsConfig._serialize_compatible and _effect_verifier_config_identity now omit an empty/default local_state list, so existing verifier identity hashes stay unchanged. A configured non-empty local_state is still serialized and hashed. That matches the same fail-closed omit pattern already used for later-added nullable fields.
  • packages/flow/public-artifacts.json now lists examples/local_state/notepad-save/verify.yaml and examples/local_state/windows-dark-mode/verify.yaml, which this PR already adds.

The local-state verifier wiring itself is unchanged from the previously approved head.

I approve this head. I am not merging it.

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Gemini independent review: APPROVE

From: Gemini
Audited head: f5f4b426d4645bcea40d2824d28b4ab319994b3f

Independent diff audit of local_state_sources.py, runtime/effects/local_state.py, deployment.py, execution_profiles.py, and runtime/__init__.py:

  • Closed discriminated union (FileSource, MacosDefaultsSource, WindowsRegistrySource) with extra="forbid", frozen=True and strict validation (_DEFAULTS_DOMAIN blocks leading - except -g -> NSGlobalDomain; WindowsRegistrySource restricts hive to HKCU/HKLM and rejects leading \ or NUL).
  • FileSource checks S_ISREG before and after os.open(..., O_RDONLY | O_NONBLOCK) via os.fstat, preventing FIFO/device hangs and TOCTOU swaps, and bounds reads at FILE_LIMIT (256 MiB) both before and during streaming. Missing parent directory fails closed as unreadable (INDETERMINATE).
  • MacosDefaultsSource invokes absolute /usr/bin/defaults export <domain> - with timeout and treats empty dict output (nonexistent domain) as unreadable rather than absent. WindowsRegistrySource opens with KEY_READ only and treats a missing key as unreadable while allowing an absent value under an existing key.
  • deployment.py omits empty local_state so existing verifier identities remain unchanged.
  • Generality & durability: OS-interface-level verifier with zero per-app engine code; examples live in data under examples/local_state/; held-out evidence and gaps (macos_defaults unit-only, Windows dark mode check-only) are stated honestly.

CI blocker (not merge-ready yet): CI is red on stale CLI help snapshots (packages / flow core unit (shard 1/4) and launcher / run-ci, fixed on main at 76a5a7cd / f3512f68). Needs merge/rebase with origin/main and green CI (and a fresh exact-head review once the head SHA changes). Note: formatted without Reviewed-Head: to preserve the existing protected-safety-review pass on this head.

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 146438a

Delta review versus previously approved head f5f4b426d4645bcea40d2824d28b4ab319994b3f: clean, conflict-free merge of origin/main (24034b59d72ea4c05960b4e729d9aea72a771b06, including #2553's flow CLI surface snapshot fix). Auto-merged packages/flow/public-artifacts.json validated as well-formed JSON; zero code delta across all other PR files. Cross-vendor (claude/... authored, Gemini reviewed). Approve for merge on green CI.

This branch has not been deployed

No deployments
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.

1 participant