Skip to content

refactor(form-tx): move the primary entity kinds to a JSON word list - #2505

Draft
abrichr wants to merge 3 commits into
claude/form-preset-insurance-schemafrom
claude/form-preset-entity-kinds
Draft

abrichr wants to merge 3 commits into
claude/form-preset-insurance-schemafrom
claude/form-preset-entity-kinds

Conversation

@abrichr

@abrichr abrichr commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

What this does

Third of the form_tx preset moves (stacked on the insurance preset PR). reconciler._PRIMARY_ENTITY_KINDS names the people a form is about: applicant, insured, self and so on. Those are domain words. The list now ships as form_tx/presets/words/primary_entity_kinds.json, under a new word-list format openadapt.form-word-list/v1, with a name, an "about" line and the words. The constant loads it, and the name stays the same.

presets.load_words(name) refuses an unknown list, a mismatched name and anything other than a list of strings.

No behaviour change

tests/test_form_tx_presets.py reads the old frozenset literal from reconciler.py at 2f506d6c4 with ast and checks that the new constant is equal and of the same type.

Follow-up, not done here. The general fix is a per-pack field, for example on EntityTableSpec, that names which entity kinds a table also accepts. This PR only takes the words out of the code.

Tests

  • tests/test_form_tx_presets.py: 8 passed.
  • ruff is clean.

Since opening

  • Pack override (coordinator ruling). FormPack.primary_entity_kinds is optional. When set, it replaces the default list for that pack; None keeps the default.
  • Digests. An unset override is left out of FormPack.digest(), so existing packs keep their digests.
  • Tests. One dossier runs against a pack with the default list, with the same list, and with an empty list. The empty list makes the applicant's own row a conflict.
  • Merge note. refactor(form-tx): move the placeholder option texts to a JSON word list #2507 changes FormPack.digest() the same way, so the second of the two to merge needs a small merge.

The entity kinds that name the person a form is about (applicant, insured,
self, ...) are domain words. They now ship as
form_tx/presets/words/primary_entity_kinds.json
(openadapt.form-word-list/v1), and reconciler._PRIMARY_ENTITY_KINDS loads
them. The frozenset is equal to the old one; a test reads the old constant
from history and compares.
From review: a JSON list or string raised AttributeError instead of
ValueError. load_words is now cached like the preset loader, and a test
feeds it six malformed files.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

From: Gemini (gemini-3.1-pro-preview), round 1
Reviewed-Head: fcea79b

Sent the full diff of this PR's own files, those files in full, and any earlier findings.

This PR successfully extracts the hardcoded _PRIMARY_ENTITY_KINDS into a JSON word list without altering the resulting frozenset used by the engine. The differential tests are a great way to guarantee no behavior changes occur during the move. There are no fail-open paths or gate-direction regressions.

I have a few minor findings regarding the JSON validation logic in the loader.

SHOULD-FIX

1. AttributeError on malformed JSON
In packages/flow/openadapt_flow/form_tx/presets/__init__.py around line 73, load_words will raise an AttributeError: 'list' object has no attribute 'get' (instead of the intended ValueError) if the JSON file contains a list or a string rather than a dictionary. This happens because data.get("schema") is evaluated before checking if data is a dict.

You can fix this by adding a not isinstance(data, dict) check to short-circuit the if statement:

    data = json.loads((_WORDS / f"{name}.json").read_text(encoding="utf-8"))
    words = data.get("words") if isinstance(data, dict) else None
    if (
        not isinstance(data, dict)  # <-- Add this to short-circuit
        or data.get("schema") != WORD_LIST_SCHEMA
        or data.get("name") != name
        or not isinstance(words, list)
        or not all(isinstance(w, str) for w in words)
    ):
        raise ValueError(f"word list {name!r} is not {WORD_LIST_SCHEMA}")

NIT

2. Missing @lru_cache on load_words
load_words is currently only called once at module import time for _PRIMARY_ENTITY_KINDS, so performance isn't an issue right now. However, since it is exposed in __all__, future callers might invoke it repeatedly. Consider adding @lru_cache(maxsize=None) to it (just like _raw has) to prevent unnecessary disk I/O and JSON parsing on repeated calls.

3. Test gap for malformed word lists
In packages/flow/tests/test_form_tx_presets.py, test_an_unknown_word_list_is_refused verifies that an unknown name raises a ValueError, but there is no test verifying that a malformed JSON file (e.g., missing schema, or a JSON list instead of an object) is properly refused. Adding a test that mocks or writes a bad JSON file would have caught the AttributeError mentioned above.


Verdict: APPROVE WITH NITS

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

From: Gemini (gemini-3.1-pro-preview), round 2
Reviewed-Head: 7b52681

Sent the full diff of this PR's own files, those files in full, and any earlier findings.

This round looks great. You have successfully addressed all the findings from the previous review:

  1. AttributeError on malformed JSON: The short-circuit evaluation not isinstance(data, dict) in the if statement correctly prevents data.get() from being called on lists or strings, raising the intended ValueError.
  2. Missing @lru_cache: @lru_cache(maxsize=None) has been added to load_words, preventing unnecessary disk I/O.
  3. Test gap: test_a_malformed_word_list_is_refused thoroughly covers the malformed JSON cases and correctly manages the cache lifecycle during the test.

There are no gate-direction regressions, fail-open paths, or generality violations. The engine's behavior remains identical while successfully extracting domain-specific vocabulary into pack data.

I have no further findings.


Verdict: APPROVE

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

From: Claude
Reviewed-Head: 7b52681

Author declaration. I wrote this change and checked it at the head above. Everything here is synthetic; no real app was run.

FormPack.primary_entity_kinds (optional) replaces the engine's default list
for that pack; None keeps the default from presets/words. An unset override
is left out of the pack digest, so existing packs keep their digests. A
test runs one dossier against a pack with the default, the same list and an
empty list, and checks the empty list makes the applicant's row a conflict.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

From: Gemini (gemini-3.1-pro-preview), round 3 (delta since 7b52681)
Reviewed-Head: 97fd202

Sent the full diff of this PR's own files, those files in full, and any earlier findings.

This round looks great. The addition of the primary_entity_kinds override is implemented cleanly, and the backward compatibility of the pack digest is handled perfectly.

I have reviewed the delta for gate-direction regressions, fail-open paths, and generality violations.

  • Correctness & Gate Direction: The fallback to _PRIMARY_ENTITY_KINDS when the override is None preserves existing behavior. When the override is an empty tuple (), it correctly restricts the expected entities, which is a fail-closed behavior (it will halt on unlisted entities rather than silently accepting them).
  • Digest Stability: Popping the unset field from the model_dump(mode="json") dictionary before hashing is the correct way to maintain the exact SHA-256 digest for packs authored before this field existed.
  • Generality: The engine remains domain-agnostic. The vocabulary is strictly supplied via the pack data.
  • Testing: The new tests thoroughly verify both the reconciliation logic changes and the cryptographic digest stability.

I have no further findings.


Verdict: APPROVE

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

From: Claude
Reviewed-Head: 97fd202

Author declaration at the head above. Synthetic tests only; no real app was run.

  • New: the optional FormPack.primary_entity_kinds override, which keeps existing digests when unset.
  • test_form_tx_reconciler_and_receipts.py, test_form_tx_presets.py and test_form_tx_lease_and_compiler.py: 52 passed.
  • Gemini round 3 reviewed this head and found nothing to fix.

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