Skip to content
Closed
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
96 changes: 96 additions & 0 deletions packages/flow/docs/EFFECT_LOCATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Effect locations

A careful person who has just entered a form checks their work. They read the
review page before they press submit, and afterwards they look again where the
value should now appear: the summary, the saved record, a list row, a second
session or an export. An effect location records one of those places as data,
so the engine can do the same check for any application without knowing which
application it is.

The record is `openadapt.effect-location/v1`
(`openadapt_flow/effect_location/record.py`). A pack stores one per place,
compiled from the demonstration. The evaluator
(`openadapt_flow/effect_location/evaluator.py`) judges it from a reading that
an adapter (a browser, an accessibility tree, a terminal screen, an export
reader) takes when it goes back.

## The record

| Field | What it holds |
| --- | --- |
| `keys` | The intended values the location shows. Usually one. A summary row such as "Name (DOB)" shows two. |
| `path` | The demonstration steps that reach the location. |
| `start` | Where the re-observation starts: `same_screen`, `fresh_navigation`, `new_session` or `other_channel`. |
| `locator` | How to find it: `label`, `row_key` (a row whose `column` cell equals `value`) or `region`. The adapter reads it; the evaluator does not. |
| `expected` | The value each key should show, in the codec's canonical form. The run's plan fills it in. |
| `codec` | How displayed text maps to the value (below). |
| `expected_count` | How many times the value shows. Required; usually 1. |
| `baseline` | Optional. What the location showed before the action. |

## Codecs

Every codec reads the whole displayed text. A prefix, a longer text or a value
with extra words never matches, so "Jane" is not "Jane Doe" and "250" is not
"$1,250.00".

| Kind | Reads | Canonical value |
| --- | --- | --- |
| `text`, `fold` | Text, whitespace collapsed (`fold` also ignores case) | string |
| `number`, `unit` | A decimal number, for `unit` followed by one of `units` | decimal |
| `money` | An amount with optional `affixes` (currency signs), `group_marks` and `decimal_mark` | integer minor units |
| `date` | ISO, or numeric in `date_order` (`YMD`, `MDY`, `DMY`), or with `month_names` | ISO date |
| `bool` | One of `true_tokens` or `false_tokens` | bool |
| `enum` | An entry of `table` (shown text to value) | string |
| `split_date` | Three displayed parts, `year`, `month` and `day` | ISO date |
| `template` | One text matched in full by `pattern`; each of `parts` reads one named group as one key with its own codec | one value per key |

The engine has no default words. A bool codec with no tokens, a date codec
with no month names or a money codec with no currency sign accepts only the
bare form. The pack supplies the rest from the demonstration.

## How a value is judged

The obligations are the run's intended values, fixed before any reading is
judged. Each one ends in one of the verdict kernel's three statuses.

- `not_checked`: no location shows the value, or the location was not read,
was not found, or was found a different number of times than expected. Also
when the read failed or came from another run, the start condition was not
met, a kind is unknown, the text can't be read, or the read was partial and
showed no conflict. Also when the expected value equals the baseline, so
the read can't tell the change from what was already there.
- `refuted`: the location shows a different value. A partial read can refute.
- `held`: every location for the value was read in full and showed exactly
the expected value the expected number of times.

The run passes only when every planned value held and no reading is left
over. When several locations show one value, a refutation at any of them
refutes it, and it holds only when all of them hold, at the weakest level.

## Check level and receipt word

The check level comes from the start condition the adapter achieved:

| Start | Check level |
| --- | --- |
| `same_screen` | 1, `same_screen` |
| `fresh_navigation` | 2, `reopened` |
| `new_session` | 3, `separate_session`, basis `separate_session` |
| `other_channel` | 4, `system_of_record`, with the basis the adapter names (`system_api`, `activity_log`, `db_frame` or `owned_connection`) |

The receipt word comes from `openadapt_types.check_level.outcome_word`, so
this package never decides what `VERIFIED` means. A same-screen read or a
fresh navigation is `SHOWN`. A new session is `VERIFIED`. Another channel is
`VERIFIED` only with a basis, otherwise `RECORDED`. Until `outcome_word` is on
main, the word is `None`.

The adapter states the start condition it achieved and binds the reading to
the run. The evaluator can't see whether a navigation really fetched from the
server; the adapter must prove it before it says `fresh_navigation`.

## Fail-open testing

`evaluate_all` is a registered verdict site (`effect_location.evaluate_all`).
Its spec, `tests/failopen/sites/effect_location.py`, runs the generic
mutations against a plan with a label, a summary row, a split date and an
amount.
53 changes: 53 additions & 0 deletions packages/flow/openadapt_flow/effect_location/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Effect locations: where a careful person would look again to see a value.

Echo before commit, re-observe after. For each intended value, a pack stores
an effect location compiled from the demonstration: how to get there (the path
and a start condition), what to look for (the locator), what it should show
(the expected value, read through a codec), how many times (usually once) and,
optionally, what it showed before. A review page is one effect location; a
summary row, a lookup-table row, a reopened record, a separate session and an
export are others.

:func:`evaluate` judges one location from one reading. :func:`evaluate_all`
judges every intended value of a run. A location that can't be reached, found
or read is ``not_checked``, never a silent pass. The engine holds no
vocabulary for any application or field: every word, pattern and token table
comes from the pack. See ``docs/EFFECT_LOCATION.md``.
"""

from __future__ import annotations

from openadapt_flow.effect_location.codecs import CodecError, decode
from openadapt_flow.effect_location.evaluator import (
KeyResult,
Report,
evaluate,
evaluate_all,
)
from openadapt_flow.effect_location.record import (
CODEC_KINDS,
LOCATOR_KINDS,
SCHEMA,
Codec,
EffectLocation,
Locator,
Reading,
Start,
)

__all__ = [
"CODEC_KINDS",
"LOCATOR_KINDS",
"SCHEMA",
"Codec",
"CodecError",
"EffectLocation",
"KeyResult",
"Locator",
"Reading",
"Report",
"Start",
"decode",
"evaluate",
"evaluate_all",
]
67 changes: 67 additions & 0 deletions packages/flow/openadapt_flow/effect_location/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""The two interfaces this package codes against before they reach main.

- ``openadapt_flow.verdict_sites.verdict_site`` (#2476) registers a verdict
site with the fail-open harness. Until it is on main, the decorator here
records the id on the function and changes nothing else.
- ``openadapt_types.check_level.outcome_word`` and ``level_for_basis``
(#2495) choose the receipt word and say which level a basis reaches. Until
they are on main, no word is chosen (``None``) and no basis is accepted
for another channel. Nothing here defines what ``VERIFIED`` means.

Delete this module once both are on main and import them directly.
"""

from __future__ import annotations

from collections.abc import Callable
from typing import Any, Optional, TypeVar

from openadapt_types import check_level as _check_level

F = TypeVar("F", bound=Callable[..., Any])

try: # pragma: no cover - which branch runs depends on what is merged
from openadapt_flow.verdict_sites import verdict_site as verdict_site
except ImportError: # pragma: no cover

def verdict_site(id: str, *, classes: str) -> Callable[[F], F]: # type: ignore[no-redef]
"""Record ``id`` on the function, as #2476's decorator does."""

def mark(fn: F) -> F:
setattr(fn, "__verdict_site__", id)
return fn

return mark


_level_for_basis: Optional[Callable[..., Any]] = getattr(
_check_level, "level_for_basis", None
)
_outcome_word: Optional[Callable[..., Any]] = getattr(
_check_level, "outcome_word", None
)


def has_ladder_words() -> bool:
"""True when ``check_level.outcome_word`` is available."""

return _outcome_word is not None


def reaches(basis: Optional[str], level: Any) -> bool:
"""True when ``basis`` is a known basis that reaches exactly ``level``."""

if basis is None or _level_for_basis is None:
return False
try:
return bool(_level_for_basis(basis) == level)
except ValueError:
return False


def word(level: Any, basis: Optional[str], *, confirmed: bool) -> Optional[str]:
"""The receipt word for ``level`` and ``basis``, or None before #2495."""

if _outcome_word is None:
return None
return str(_outcome_word(level, basis, confirmed=confirmed))
Loading
Loading