Skip to content

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

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

abrichr wants to merge 4 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.

@abrichr
abrichr marked this pull request as ready for review November 23, 2026 22:38
@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

From: Claude
Reviewed-Head: 2861a2b

Author declaration for the new head.

  • Merged the updated parent branch claude/form-preset-insurance-schema (now 48ff3b3f5, which has origin/main merged in) into this branch. There were no conflicts. I did not rebase or force-push.
  • The net diff against the parent is unchanged from before the merge: 6 files, +219/-17. It covers presets/words/primary_entity_kinds.json, load_words in presets/__init__.py, the reconciler reading the list, the optional FormPack.primary_entity_kinds override, and tests.
  • FormPack.digest() leaves out primary_entity_kinds while it is unset, so a pack written before this field existed keeps its digest. refactor(form-tx): move the placeholder option texts to a JSON word list #2507 touches digest() later; this PR's version stands on its own.
  • Checks run locally: ruff check and ruff format --check on the changed files; mypy on the three changed source files (clean); pytest -p no:xdist on every test_form_tx_*.py except the e2e and Playwright suites (263 passed).

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: 2861a2b

I checked the live head with gh and reviewed this PR's own diff against its base claude/form-preset-insurance-schema (48ff3b3).

Blocking

  1. The new word list is not in the public artifact inventory. python scripts/check_release_consistency.py (from packages/flow) lists openadapt_flow/form_tx/presets/words/primary_entity_kinds.json as unregistered. The same applies to refactor(form-tx): move the property insurance quote preset to JSON #2504's file. This step gates flow core lint → packages-gate → ci.
  2. Packs written by this version can't be read by any older Flow. FormPack is extra="forbid" (form_tx/types.py:384). The new primary_entity_kinds: Optional[...] = None (:404) is always dumped, so form_tx induce (cli.py:173, report.pack.model_dump(mode="json")) now writes "primary_entity_kinds": null into every pack, even when there is no override.
    • Reproduced: main's FormPack.model_validate() on a default pack dumped at this head fails with primary_entity_kinds: Extra inputs are not permitted.
    • The digest special-case (_UNHASHED_WHEN_UNSET, :417) keeps hashes stable but not the file format.
    • Fix: refactor(form-tx): move the yes/no words to JSON word lists, with a codec override #2511 already uses the right pattern on FieldCodecSpec: a model_serializer(mode="wrap") that drops unset overrides. Apply it here and the digest special-case becomes unnecessary. A test that main's model accepts a default dump would pin it.

Non-blocking

Behavior parity: OK

  • _PRIMARY_ENTITY_KINDS (reconciler.py:76) holds the same 6 words. It is used only as a frozenset membership test, so order doesn't matter.
  • An unset override takes the old path.
  • load_words only accepts names that exist under presets/words/ and checks the schema, the name and that every entry is a string. It is cached and returns a tuple.

Packaging and generality

Tests (at this head, on the box)

  • pytest tests/test_form_tx*.py: 274 passed.
  • ruff and format: clean.

CI at review time

1 failed (Evaluate protected-path evidence), 3 pending, 11 skipped. The packages jobs have not run.

@abrichr

abrichr commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Architectural pivot (founder Option 1, 2026-11-23): freezing the hand-written form-tx DOM heuristic and word-list stack to focus on openadapt wrap + demonstration-derived CheckpointSpec + Cross-Tab Entity Ledger + GUI-to-API/MCP promotion. Branch preserved for reference.

@abrichr abrichr closed this Nov 24, 2026
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