Skip to content

feat(tags): reserved system tags, per-tag permission, tags in export (#805) - #875

Merged
athvin merged 6 commits into
mainfrom
issue/805-system-tags
Nov 8, 2026
Merged

athvin merged 6 commits into
mainfrom
issue/805-system-tags

Conversation

@athvin

@athvin athvin commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

Closes #805

What

Three residue items from spec unit 1.29: reserved system tag names (merge, manual_merge, unmatch, link, split) that users can neither create (422) nor retire (kind column + 422), a per-tag apply_min_role enforced as a 403 on apply/unapply, and a tags column in the segment CSV export joined from a new bulk taggings read.

Why

The tag service is live but has no system/user distinction — a user can squat a name the system will need — no per-tag permission finer than the route, and the export drops the vocabulary entirely.

Proof

make check green (entities-check regen byte-clean; pg_tags 9 phases green against real Postgres, incl. the reserved-name 422s, the 403-not-404 ladder, and the system-tag guards). make frontend green — 549 passed; one datasets.spec.ts mobile-chrome flake on the first run passed in isolation on all three browsers and on the rerun.

Re-verified after a rebase onto a main that had moved four PRs (incl. #519's audit-store schema change): make check green end-to-end, pg_tags / pg_schema_replay / fixups green, frontend types + 17 unit tests green. One conflict (generated entities-index.md statement count) resolved by regen.

pr-review round (four fresh-context reviewers, findings verifier-scored):

  • Schema/differ — one real BLOCKER found and fixed before merge: the pg differ reads a missing NOT NULL column as breaking regardless of its default, so every existing store would have refused to boot on tags.kind. Fixed with the 0005_tag_kind BeforeReconcile fixup (fixups.rs) that adds the column with its default on old stores, plus pg_schema_replay phase 5 which ages a store past the fixup and proves the heal. VERIFIED merge-ready by the same reviewer.
  • Security — clean; two in-PR hardening items taken: #[serde(deny_unknown_fields)] on TagIn/TagApplyIn (phase-6 pin), and the unreadable-apply_min_role path now fails CLOSED with a 500 instead of silently lifting the floor.
  • API contract — clean; filed MCP manifest cannot represent required query params #878 (MCP manifest lacks query params) as a pre-existing gap.
  • BFF — no blockers; filed Export 502s on a Postgres-less er-server (the 501 shape meets the tags prequery) #896 (export 502s when er-server has no Postgres) with the decision deferred; docstrings now say the tags column is a pg point-in-time read with unspecified cell order, and grow.spec pins watchlist in an actual CSV row.

make check-e2e: not owed — no lake statement changed (the new read is Postgres-side).

Risk & rollback

Additive schema columns with defaults; new 422s only on previously-accepted-but-wrong calls. git revert is the rollback.

Post-merge ledger

Closed by: the post-merge watch on 350cd213 and its successor 85de6958

Specific to this change

Context — plan, discoveries, measurements, files

Plan

  1. entities/tag: +kind (user|system), +apply_min_role (nullable); create statement gains both params; regen.
  2. er-server: reserved-name const + 422 on create (case-insensitive), 422 on retiring a system tag, pg::create_system_tag for system writers (tests use it).
  3. er-server: apply_min_role on TagIn (values steward|admin), enforced in tag_application as 403 naming the required role, after the 404 existence check (which leaks nothing — viewers can list the whole vocabulary).
  4. Export: tagging.for_dataset declared read (cap+1 truncation probe), GET .../taggings?subject_type=entity (Viewer+), BFF segment export joins and gains a tags column; truncation reported in x-export-summary.
  5. Mock parity, pg_tags coverage (reserved 422s, system-retire 422, 403-not-404 ladder), frontend export tests, OpenAPI + schema.d.ts regen.

Discoveries

  • The tag service never distinguished system from user — no kind column anywhere.
  • Only ONE export exists: the segment CSV of golden records. A groups-shaped export is E11 — Public resolution results, lineage and bulk export #521 (E11) territory; noted on the issue.
  • The entity framework has no array params, so a per-page ANY($ids) join is not declarable; the export prequery is one dataset-scoped capped read instead, with truncation detected (cap+1) and reported.
  • Existence-before-permission ordering in tag_application leaks nothing: Viewer+ already lists the entire vocabulary.
  • Tag/Tagging are not OpenAPI components (they are in 15 entities declare wire: [json] with no OpenAPI component schema — an in-code backlog with no ticket #762's fifteen), so no openapi.json/schema.d.ts regen was owed; the contract surfaces that DID move are the MCP manifest (tools.json: new taggings tool + apply_min_role body field) and the envelope ledger (envelopes.json: {taggings, truncated}), both regenerated by their --ignored regenerate arms.
  • pg_serial keys one database per test FILE, so the new coverage landed as phases 6–9 of pg_tags' existing test rather than a second #[tokio::test] — and the fold exposed a real ordering fact: a steward's retire attempt on a system tag is the route's 403, not the guard's 422, because the admin floor runs first.
  • The pg schema differ treats a missing NOT NULL column as a breaking change even when the DDL carries a default — additive NOT NULL columns need a BeforeReconcile fixup, and pg_schema_replay is the gate that proves the heal. Plain er-entity gen does not write entities-index.md; scripts/skills-check.sh regen does (caught by skills-check after the rebase).
  • The er-entity type for a limit param is bigint, not int8; list<varchar> exists as a param type (unused here — the capped prequery is the right shape for the export regardless).

Measurements

  • Taggings prequery cap: 100,000 per dataset per request, cap+1 probe (lib.rs taggings_index).
  • make frontend: 549 passed, 117 skipped (device/release tiers), 2.5 min.

Files

  • entities/tag/entity.yaml, create/*, read/* — +kind, +apply_min_role, regenerated er-entities + Tag.ts binding + DDL.
  • entities/tagging/read/for_dataset.{sql,yaml} — the new capped bulk read.
  • rust/crates/er-server/src/fixups.rs — 0005_tag_kind BeforeReconcile fixup.
  • rust/crates/er-server/tests/pg_schema_replay.rs — phase 5: aged store heals through the fixup.
  • rust/crates/er-server/src/pg.rs — RESERVED_TAG_NAMES, create_tag +apply_min_role, create_system_tag, tag_by_id, taggings_for_dataset.
  • rust/crates/er-server/src/lib.rs — reserved-name 422, system-tag retire/apply guards, per-tag role floor (403), taggings_index + route.
  • rust/crates/er-server/tests/pg_tags.rs — phases 6–9.
  • rust/crates/er-mcp/tools.json, frontend/src/lib/api/envelopes.json — regenerated ledgers.
  • frontend/src/lib/bff/export-tags.ts + tests/unit/export-tags.test.ts — the join and its unit coverage.
  • frontend/src/app/api/.../segments/export/route.ts — prequery, tags column, truncation note.
  • frontend/src/lib/bff/tags.ts — apply_min_role passthrough.
  • frontend/tests/mock-erserver/server.mjs — reserved names, new fields, the taggings endpoint.
  • frontend/tests/functional/grow.spec.ts — header pins the tags column last.

…he export (#805)

- tags gain kind (user|system) and apply_min_role (steward|admin, null =
  the steward route default); the five spec-reserved names are a 422 on
  create, case-insensitively, and a system tag is a 422 to retire and a
  422 to hand-apply (operator excepted — the platform's own credential)
- apply_min_role is enforced on apply AND unapply as a 403 naming the
  required role — 403 on role, never 404, per the house rule
- GET .../taggings?subject_type= is the bulk read the segment export
  prequeries (cap+1 truncation probe, reported in the envelope); the CSV
  gains a tags column joined through the active vocabulary, and a
  truncated prequery is named in x-export-summary
- pg::create_system_tag is the internal path the platform's writers will
  use; the pg test drives it to prove the guards
…e MCP + envelope ledgers (#805)

pg_serial keys one database per FILE, so the new assertions land as
phases 6-9 of the existing test rather than a second #[tokio::test].
tools.json gains the taggings read and the apply_min_role body field;
envelopes.json records the {taggings, truncated} envelope.
…ning (#805)

pr-review findings:
- BLOCKER (entity-framework): the differ reads a missing NOT NULL column
  as breaking regardless of default, so every store that already had
  'tags' would refuse to boot. 0005_tag_kind (BeforeReconcile, Safe)
  adds kind idempotently; pg_schema_replay phase 5 ages a store and
  proves the boot heals with the default filling pre-existing rows.
- taggings_dataset_subject (dataset_id, subject_type, subject_id)
  serves for_dataset's filter AND order with no sort.
- security hardening taken in-PR: TagIn/TagApplyIn now
  deny_unknown_fields (seven house precedents), so kind:'system' in a
  body is a rejected 422 rather than silently ignored; phase 6 pins it.
- export docstrings: the tags column is a pg point-in-time read, not
  snapshot-pinned; the join has an end-to-end assertion (watchlist in
  the CSV), not just the header.
@athvin
athvin force-pushed the issue/805-system-tags branch from 4fbb9f8 to 97422a3 Compare November 8, 2026 19:24
@athvin
athvin marked this pull request as ready for review November 8, 2026 19:33
@athvin
athvin merged commit 350cd21 into main Nov 8, 2026
48 checks passed
@athvin

athvin commented Nov 8, 2026

Copy link
Copy Markdown
Owner Author

Flagging a post-merge failure that belongs to this PR — found while landing #895, whose own merge SHA the run is misleadingly labelled with.

deploy run 42516648310 failed, and it is deploying 350cd213 (this PR), not the commit it is attributed to. gh run list reports that run as headSha: 9583e470 — the head of main when the workflow_run fired — while DEPLOY_SHA inside the run is 350cd21379f73526b9af7a1e383ffde95a424368. Worth knowing in general: for a workflow_run-triggered deploy, the run list's headSha is not the commit being deployed, so a later merge appears to own an earlier one's broken deploy.

Root cause

The helm upgrade rolled back atomically on job er-schema-migrate failed: BackoffLimitExceeded. The workflow log stops there; the pod itself is specific:

verdict: breaking (catalog 73637fbf2edd)

1 statement(s) would be applied:
  public.tags: ALTER TABLE public.tags ADD COLUMN apply_min_role text;

1 difference(s) the reconciler REFUSES to auto-apply:
  public.tags: column kind is absent; add column

This release cannot be rolled out underneath a running older version.
Removing or retyping a column takes TWO releases: N stops reading it,
N+1 changes it once no older pod is left. Where that is genuinely not
possible, the operation belongs in a named fixup (er-server/src/fixups.rs)
whose doc comment argues why it is safe.

So of the two columns this PR adds to public.tags, apply_min_role reconciles fine and kind is refused. That is the reconciler's breaking-change refusal working as designed, not a flake — a rerun will fail identically.

Where the fix goes

public.tags.kind needs a named fixup in rust/crates/er-server/src/fixups.rs whose doc comment argues why it is safe, which is the same path the Salesforce extract's NOT-NULL dataset_id columns took. Making it nullable or giving it a default is the other route, if the column's semantics allow it.

What is NOT broken

Dev is still serving. The upgrade was atomic, so it rolled back; web-api-green (the live colour) has been Running 31m and the Services still select it:

$ kubectl --context er-dev -n ci get pods
pod/er-dispatcher-68fcf97b86-qddq7   1/1   Running   31m
pod/er-schema-migrate-56fjv          0/1   Error     2m18s
pod/web-api-blue-7bd4777d6-4lbc5     2/2   Running   2m6s
pod/web-api-green-86cb9d4bb7-tv7tj   2/2   Running   31m

The consequence is that main has not deployed since b12514c0 — every merge after this one inherits the same red deploy until the fixup lands, which is how this would otherwise be mistaken for the next PR's fault. (It already was: I had to rule it out of #895's post-merge watch.)

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.

feat(tags): reserved system tags, per-tag permission, and tags in the export

2 participants