Skip to content

Bulk-tag works from an exact subject (#13755) - #67

Draft
mekarpeles wants to merge 6 commits into
13929/convert-tag-refsfrom
13755/bulk-retag-subject
Draft

mekarpeles wants to merge 6 commits into
13929/convert-tag-refsfrom
13755/bulk-retag-subject

Conversation

@mekarpeles

Copy link
Copy Markdown
Collaborator

ada-status — DRAFT, not yet run against production.

  • Branch 13755/bulk-retag-subject @ 9934d6a, stacked on PR A (13929/convert-tag-refs @ 49a65de). Merge/rebase A first.
  • Tests: 367 passed / 1 skipped. Compiles on 3.12. Three adversarial-review rounds, all PASS.
  • Open question for Mek: keep or strip the matched subject string after tagging? Lead's lean: KEEP (the default).

Bulk-tag works from an exact subject (#13755)

Stacked on 13929/convert-tag-refs. Given an exact subject string and a target Tag,
finds the works carrying that subject and adds the Tag ref to the correct plural
work field
(#13755).
Nothing here has been run against production.

Tool — scripts/bulk_retag_subject.py

  • Target resolution checks the live Tag's singular tag_type against the plural
    field (genre ↔ genres); a tag_type/field mismatch is refused up front; a 404 /
    unknown Tag skips and reports (never a silent drop).
  • Shares PR A's safety model: dry-run default, live re-fetch before write, only the
    target field changed, atomic save_many → bisect, audit → conditional undo, and the
    same --creds bot-identity assertion before any write.

keep-vs-strip (needs Mek)

After a work is tagged from its matched subject, its free-text subjects entry can
stay or be removed:

  • KEEP (default, lead's lean): add the ref, leave subjects untouched — reversible, non-destructive.
  • STRIP (--strip-subject): also remove the matched string — cleaner data, destructive.

Testing

367 passed / 1 skipped; compiles on Python 3.12. Covers target resolution
(singular tag_type vs plural field), the null-field fix, and the undo tests.

Order

Depends on PR A — merge/rebase A first. Same Solr deploy-order gates as PR A.

🤖 Generated with Claude Code

Maps one EXACT subject string to a canonical Tag and adds it as a ref to matching
works, in batches of 250. Part of the Tags epic (#13923, pillar #13755).

- Exact match by default on the live work's subjects; --ignore-case /
  --ignore-whitespace are opt-in and the dry-run diff still shows the exact stored
  string that matched.
- Target Tag resolved against production (the authority): --tag OLID, or
  --name + --tag-type that must resolve to exactly ONE live Tag (fail on 0/>1);
  its tag_type must equal the target field.
- Add-only merge (dedupe, order-preserving, both shapes); subject KEPT by default,
  --strip-subject removes it (audited as its own row).
- Same safety model as convert_tag_refs and reuses its primitives (save_with_bisect,
  make_saver/is_transient_failure, require_audit_for_write, conditional apply_undo):
  dry-run default, --write requires --audit/--done, atomic-save bisect, audit JSONL
  with base revision, conditional reversible undo.

Refactor: convert_tag_refs.cmd_undo now delegates to a shared apply_undo().

21 new tests; full suite green (303 passed, 1 skipped).
- [HIGH] resolve_target_tag --name path now enforces the resolved tag's
  tag_type == field AND name == requested name (previously only checked it was
  live, so --tag-type different from --field could merge a wrong-type tag into the
  field, and a loose query.json name filter could resolve a wrong-named tag).
- [HIGH] a work with "subjects": null (or a null/non-dict work body) no longer
  crashes the run: work_matches_subject/apply_retag tolerate null subjects and
  run_retag skips a non-dict work (counted as "malformed").
- enumerate_subject_works tolerates a non-list (error) query.json body.
- Documented _merge_tag's intended ref-normalization (accepted, matching the
  conversion tool's already-ref handling).

5 new regression tests; full suite green (308 passed, 1 skipped).
- [MED] apply_retag now resolves EXISTING field entries through the shared
  TagResolver with the conversion tool's rules (follow redirects, drop a definitive
  /type/delete, audited; an unresolvable value skips the whole work). Retag and
  convert now leave a field in the same shape, and retag never saves a field that
  still holds a dead/merged ref (which would 4xx once the type is declared).
- [LOW] documented that the target tag_type must equal the field, so
  --field literary_forms/audiences refuses today (prod tag_types are singular until
  #13814); genres/subgenres work now.
- [LOW] documented (docstring + --help) that enumerate matches the EXACT subject,
  so --ignore-case/--ignore-whitespace only relax matching among already-enumerated
  works.

3 new regression tests; full suite green (310 passed, 1 skipped).
Mek's convention (2026-11-02, #14229 indexer flip): a Tag's tag_type is
SINGULAR (genre) and the work field is its plural stem (genres), so the
target-tag check becomes tag_type + 's' == field instead of tag_type == field.

- Add _expected_tag_type(field) = field without trailing 's'.
- resolve_target_tag now accepts a tag whose singular tag_type pluralizes to
  the field; this also resolves audiences/literary_forms targets that the old
  plural==field check could never match.
- --tag-type now defaults to the singular stem of --field (was the plural field).
- Reject a not-yet-retyped plural tag_type (correct until the retype + #14229 deploy).
- Tests: singular->plural resolves (all types), old plural now rejected, helper.

backfill_tags/convert_tag_refs (branch A) are unaffected: their tag_type axis is
the local classifier dir name (tag_types/<dir>, via vocabulary.json), decoupled
from the Tag document's tag_type property this convention concerns.
…-7 review, #13755)

B1 (MINOR): apply_retag used work.get(field, []), so a present-but-null target field
(genres: null) returned None -> normalize_field reported it unclassifiable -> the whole
work was skipped, silently missing a taggable work. This was asymmetric with the
subjects handling, which already tolerates null. Use `work.get(field) or []` so null
counts as empty and the tag is added.

Test (watched fail first): a work carrying the subject with genres=null is tagged
("changed"), not skipped. Suite 329 passed, 1 skipped.
…se-7 review, #13755)

B2 (MINOR): apply_undo restored row["before"] unconditionally, so undoing a tag added
to a previously-absent field left genres: [] rather than removing the key. [] and an
absent key are equivalent, but removing matches the original shape. When before is
falsy, pop the key instead of writing []. convert never writes an empty "before"
(it refuses to empty a field), so this only affects bulk_retag's add-to-absent path.

Test (watched fail first): undo of a row with before=[] removes the key; a non-empty
before still restores its value. Suite 342 passed, 1 skipped.
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