Skip to content

feat(labeler): normalized finding contract (W8.1) - #1991

Merged
ascorbic merged 2 commits into
feat/plugin-registry-labelling-servicefrom
feat/labeler-finding-contracts
Jul 12, 2026
Merged

ascorbic merged 2 commits into
feat/plugin-registry-labelling-servicefrom
feat/labeler-finding-contracts

Conversation

@ascorbic

@ascorbic ascorbic commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Adds the labeler's normalized finding contract — plan item W8.1. This is the single validated shape every analysis stage (deterministic, capability, model, image, history) produces, and the gate that enforces it before an assessment finalizes. It's the foundation the W7 (deterministic + capability) and W8 (AI code + image) analyses populate and the operator console (W9.3) reads.

  • findings.ts — NormalizedFinding (source, policy-validated category, severity, optional confidence, public/private summaries, evidence refs, optional affected files/images, and a discriminated sourceMetadata — tool for deterministic/capability/history, model for model/image). validateFinding / validateFindings throw FindingValidationError on an unknown category, a source/severity outside the enums, a confidence outside [0,1], an evidence ref that doesn't resolve to a recorded evidence object, or malformed/oversized strings — and return an object built solely from validated locals (unexpected input fields are dropped, never passed through).
  • Policy-derived category allowlist — allowedFindingCategories(policy) = the automated-block ∪ warning label values (via the new warningCategories, mirroring automatedBlockCategories), derived from the versioned policy rather than hardcoded. Eligibility and manual-system label values are not finding-derivable.
  • Public boundary preserved — toPublicFindingView routes through evidence.ts's existing never-typed PrivateFindingRecord → PublicFindingView narrowing rather than constructing a public view itself, so a private field can't leak by drifting out of sync. PublicFindingView is unchanged; PrivateFindingRecord gains the new optional fields but still carries the non-optional privateDetail/evidenceRefs that make it non-assignable to the public view.
  • Enforced in the pipeline — the (test-only, behind the production boundary) orchestrator's StageFinding is now NormalizedFinding, and it validates collected findings against the policy allowlist + the run's recorded evidence ids before finalizing. An invalid finding is a non-retryable error (a stage-adapter bug), so it aborts the run rather than retrying.

No migration (the findings table already exists), no lexicon change, no public-API change (findings are internal evidence — the public publicAssessment has no findings array).

Reviewed with a focused adversarial (opus) pass targeting the private→public boundary, validation bypass, and the orchestrator wiring before opening.

Targets feat/plugin-registry-labelling-service. Related to #1909 and RFC #694.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion: RFC RFC: Decentralized Plugin Registry #694

Admin i18n and changeset are n/a — this touches only apps/labeler, a private, unpublished Worker app (no admin UI, not on npm).

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Code with Claude Opus 4.8 (design, review) and Sonnet/Opus subagents (implementation, adversarial review)

Screenshots / test output

  • pnpm --filter @emdash-cms/labeler test: 150 pass (130 pre-existing + 20 new — 18 for the finding contract/validation/boundary, 2 for the orchestrator abort-on-invalid path)
  • Typecheck clean; pnpm lint:json diagnostics unchanged at 19 (all pre-existing, 0 in apps/labeler)

Try this PR

Open a fresh playground →

A full working EmDash site, deployed from this branch. Each visit gets its own session-scoped sandbox: no login needed and no shared state. Try the admin, edit content, hit the public site.

Tracks feat/labeler-finding-contracts. Updated automatically when the playground redeploys.

@changeset-bot

changeset-bot Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 15d92da

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added review/needs-review No maintainer or bot review yet size/XL labels Jul 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Scope check

This PR changes 655 lines across 7 files. Large PRs are harder to review and more likely to be closed without review.

If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs.

See CONTRIBUTING.md for contribution guidelines.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-do 15d92da Jul 12 2026, 09:20 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-playground 15d92da Jul 12 2026, 09:21 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-cache 15d92da Jul 12 2026, 09:21 PM

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right change for W8.1. It introduces a clear, policy-derived normalized finding contract, validates it against the allowlist and the run’s recorded evidence, and gates finalization with a non-retryable error — all consistent with the labeler architecture and the stated production-boundary scope.

I read the diff and full changed files, traced the new validator through the orchestrator, checked the public/private boundary in evidence.ts, verified SQL parameterization in listEvidenceObjectIds, and confirmed the type-only import cycle is runtime-safe under verbatimModuleSyntax. Tests cover the validation surface and abort path.

Headline conclusion: clean overall, with one minor suggestion. The validateFinding contract claims it returns an object built solely from validated locals and never passes input through, but the array helper returns the original array reference for evidenceRefs, affectedFiles, and affectedImages. Arrays are element-wise validated, so the current orchestrator path is not unsafe, but a caller that mutates the input array after validation would mutate the supposedly immutable validated finding. Copying the array in requireStringArray aligns the implementation with the contract claim.

A future tightening opportunity (not a finding): validateFinding does not enforce that source and sourceMetadata.kind are consistent (e.g., source: "deterministic" with sourceMetadata.kind: "model"). The spec phrasing suggests a relationship, so this may be worth codifying once the stage adapters are real.

Comment thread apps/labeler/src/findings.ts Outdated
`${field} entries must be at most ${MAX_ARRAY_ENTRY_LENGTH} characters`,
);
}
return value;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] requireStringArray returns the original input array reference after validating it, so the arrays assigned to evidenceRefs, affectedFiles, and affectedImages in validateFinding are the caller’s arrays passed through rather than freshly built. The PR description says validated findings are “built solely from validated locals” and that unexpected input fields are “never passed through,” but these arrays are passed through.

In the current orchestrator path this is harmless because the stage adapter cannot mutate a finding after returning it, but it makes the contract fragile for other callers and means a mutated array could change a validated finding later.

Suggested change
return value;
return [...value];

This single change fulfills the stated contract without affecting behavior elsewhere.

@github-actions github-actions Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed review/needs-review No maintainer or bot review yet labels Jul 12, 2026
Add the canonical NormalizedFinding shape every analysis stage produces,
with validateFinding/validateFindings enforcing it against the
policy-derived allowed-category set (automated-block union warning) and a
run's recorded evidence objects before an assessment finalizes.

toPublicFindingView routes through evidence.ts's never-typed
PrivateFindingRecord -> PublicFindingView narrowing so private fields
can't leak. The orchestrator now validates collected findings before
resolving policy outcome; an invalid finding aborts the run as a
non-retryable stage-adapter bug.
@ascorbic
ascorbic force-pushed the feat/labeler-finding-contracts branch from ae8055d to f4a547f Compare July 12, 2026 21:14
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Jul 12, 2026
…nput

requireStringArray returned the caller's array by reference, so mutating
the input array after validation would mutate the validated finding.
Return a copy so the finding matches the contract's immutability claim.
@ascorbic

Copy link
Copy Markdown
Member Author

Applied your suggestion. requireStringArray now returns a copy ([...value]), so evidenceRefs/affectedFiles/affectedImages on a validated finding are decoupled from the caller's input array — matches the contract's "built solely from validated locals" claim. Added a test that mutates the input array post-validation and asserts the finding is unaffected.

On the future tightening (source ↔ sourceMetadata.kind consistency): agreed it's worth codifying, and I'm deferring it exactly as you framed — to when the stage adapters are real. Enforcing it now would bake in a mapping the adapters haven't pinned down (e.g. whether history reports tool or a distinct kind), so it belongs with the W8 stage-adapter wiring, not this contract PR.

Also FYI: rebased onto the freshly-merged #1989 (clean; the only overlap was non-adjacent additions in assessment-store.ts). Re-triggering for a pass against current HEAD (15d92da).

@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Jul 12, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of the W8.1 normalized finding contract follow-up.

The one item from the previous review — validateFinding returning the original input array references for evidenceRefs/affectedFiles/affectedImages despite claiming validated-locals-only output — is now fixed. requireStringArray returns a shallow copy ([...value]) and apps/labeler/test/findings.test.ts adds a regression test confirming the validated finding is decoupled from caller mutations.

I re-read the full changed files (findings.ts, evidence.ts, policy.ts, assessment-orchestrator.ts, assessment-store.ts, and the tests) and found no new issues:

  • The public/private boundary is still enforced by routing through evidence.ts’s never-typed toPublicFinding; toPublicFindingView never constructs a public view literal itself.
  • Validation covers the enumerated source/severity values, policy-derived category allowlist, confidence range, evidence-ref resolution, string bounds, array bounds, and typed sourceMetadata.
  • The orchestrator validates collected findings before finalization, aborts (non-retryable) on the first validation failure, and remains behind the documented production boundary (AssessmentOrchestrator is test-only per production-boundary.test.ts).
  • SQL in listEvidenceObjectIds is parameterized; no migration, no admin UI, no public API change, and no changeset are needed for this private Worker app.

No new findings. The only remaining tightening opportunity is the source/sourceMetadata.kind consistency check already noted as future work in the prior review.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Jul 12, 2026
@ascorbic
ascorbic merged commit 14402a3 into feat/plugin-registry-labelling-service Jul 12, 2026
10 checks passed
@ascorbic
ascorbic deleted the feat/labeler-finding-contracts branch July 12, 2026 21:42
@ascorbic ascorbic mentioned this pull request Jul 14, 2026
60 of 81 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:review Trigger an emdashbot code review on this PR cla: signed review/approved Approved; no new commits since size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant