Skip to content

feat(agent): variant spec-clarity LLM judge [STORY-039d] - #75

Merged
khoks merged 7 commits into
mainfrom
story/039d-variant-spec-clarity-judge
May 11, 2026
Merged

khoks merged 7 commits into
mainfrom
story/039d-variant-spec-clarity-judge

Conversation

@khoks

@khoks khoks commented May 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Closes AC feat(product): Path A scope — new Stories, NOVEL_IDEAS, DECISIONS_LOG #3 of STORY-039 — adds a Haiku LLM-judge that scores LLM-generated problem variants on a 1-5 spec-clarity rubric (instruction_clarity / example_quality / concept_match) and drops the variant when min(scores) < 3.
  • New versioned prompt variant-spec-clarity-v1 in @learnpro/prompts. New pure judge runVariantSpecClarityJudge in @learnpro/agent.
  • generateProblemVariant accepts an optional judge parameter; Fastify route wires it behind LEARNPRO_VARIANT_SPEC_CLARITY_JUDGE=1 (default ON when ANTHROPIC_API_KEY is set).

Test plan

  • pnpm --filter @learnpro/agent test variant-spec-clarity-judge passes
  • pnpm --filter @learnpro/agent test problem-variants passes (new + existing tests)
  • pnpm --filter @learnpro/prompts test variant-spec-clarity-prompt passes (forbidden-phrase test)
  • pnpm format:check passes
  • CI green

🤖 Generated with Claude Code

@khoks
khoks marked this pull request as ready for review May 11, 2026 09:48
@khoks
khoks force-pushed the story/039d-variant-spec-clarity-judge branch from 6ac5df1 to 99e6c44 Compare May 11, 2026 10:00
khoks and others added 7 commits May 11, 2026 07:47
…039d]

Closes AC #3 of STORY-039 — adds a second LLM call (Haiku) after the structural
Zod gate that scores variants 1-5 on instruction_clarity / example_quality /
concept_match and drops the variant when min(scores) < 3.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds a Haiku LLM-judge that scores LLM-generated problem variants on a 1-5
spec-clarity rubric with three criteria — instruction_clarity / example_quality /
concept_match. Computes pass = min(scores) >= 3. Pure / never throws / single
retry on parse failure / synthetic { pass: false } on persistent failure so the
caller can use a single drop-if-not-pass pattern.

- packages/prompts/src/variant-spec-clarity-prompt.ts — versioned v1 prompt with
  rubric anchors at 1/3/5 per criterion + forbidden-phrase guards on the system
  prompt itself
- packages/agent/src/variant-spec-clarity-judge.ts — pure judge with Zod-validated
  response + parseVariantSpecClarityResponse exported for direct use
- 12 prompt tests + 25 judge tests pass

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…039d]

Wires `runVariantSpecClarityJudge` into `generateProblemVariant` as a second
gate (alongside STORY-039's Zod structural gate and STORY-039a's Piston
self-validation gate). The judge runs after sandbox validation; a `pass:false`
result drops the variant, fires `variants_dropped_spec_clarity` telemetry, and
counts toward the same retry budget. The retry call carries an augmented
prompt that points the model at the lowest-scoring rubric criterion.

- `judge?: SpecClarityJudge` parameter on `GenerateProblemVariantInput`
- new `variants_dropped_spec_clarity` counter on `ProblemVariantsTelemetry`
- `PreviousFailure` is now a discriminated union (`self_validation` |
  `spec_clarity`) so the retry prompt picks the right corrective phrasing
- 9 new tests covering happy path / drop+retry / drop-twice / sandbox+judge
  ordering / throwing-judge / throwing-sink

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…-039d]

- New `buildVariantSpecClarityJudgeFromEnv` reads `LEARNPRO_VARIANT_SPEC_CLARITY_JUDGE`
  and returns a `SpecClarityJudge` only when enabled. Default is ON when
  `ANTHROPIC_API_KEY` is set, OFF otherwise (cost-safe default — judge costs
  ~$0.01 per variant). Disable values: 0 / false / off / no.
- New optional `variantSpecClarityJudge` field on `BuildServerOptions`; the
  problem-variants route passes it through to `generateProblemVariant`.
- `defaultsFromEnv` wires the env builder alongside the existing route. The
  wiring uses the same `llm` instance the rest of the API uses (no extra
  transport).
- 8 new route tests cover env-flag enable/disable cases + judge-pass-through
  on a passing stub + drop-on-failing-stub + no-judge-default.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- STORY-039d status → done, AC #1-6 → [x], activity log appended
- STORY-039 AC #3 (LLM-judge spec-clarity rubric) → [x], activity log appended
- BOARD.md: Last updated header leads with STORY-039d; new row in Recently
  Done table before STORY-039a

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
CI typecheck (tsconfig.test.json) caught two issues in the stub-judge helpers:

1. `(async () => {...}) as SpecClarityJudge` was a non-overlapping cast — the
   arrow function ignored the `variant` parameter so its inferred type didn't
   match `(variant: ProblemDef) => Promise<...>`. Rewrote the helpers to return
   an explicit `{ judge: SpecClarityJudge; calls: () => number }` tuple via a
   closure over `let calls = 0`, so the function literal carries the right
   signature.
2. `failingJudge(scoreOverride: number = 2)` widened `instruction_clarity` to
   `number`, breaking the `1 | 2 | 3 | 4 | 5` literal union. Removed the
   override; the helper now always returns `instruction_clarity: 2`, which is
   what every existing call used.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Empty commit to force GitHub Actions to pick up the branch.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@khoks
khoks force-pushed the story/039d-variant-spec-clarity-judge branch from 99e6c44 to 071b97c Compare May 11, 2026 14:49
@khoks
khoks merged commit b74268f into main May 11, 2026
2 checks passed
khoks added a commit that referenced this pull request May 11, 2026
…e) [STORY-039e]

Squashed rebase of the STORY-039e branch onto current main (which already
includes STORY-039d's spec-clarity judge merged via PR #75).

Lands:
- Migration 0025_variant_gate_failures: CHECK-constrained failure_reason
  enum (parse_error / identity_mismatch / spec_clarity_judge /
  self_validation / retry_exhausted), failure_detail jsonb, model_id,
  attempt_number; indexed on (source_problem_id, attempted_at desc).
- Migration 0026_users_is_admin: boolean default false.
- @learnpro/db variant-gate-failures helpers + tests.
- apps/api admin route GET /v1/admin/variant-failures with 401/403/200
  gates + filter + pagination.
- apps/web admin page at /admin/variant-failures (server component).
- is_admin: false added to apps/api test session factories (data-controls,
  export, index, onboarding, profile-insights, session-plan, today-plan,
  tutor, weekly-plan, autonomy, bug-finding-scores, cheatsheet,
  cheatsheet-cron, email-digest, email-digest-cron, install-eligible,
  llm-mode, notifications, portfolio, profile-insights-cron, quiet-hours,
  recommendation, sandbox*, tutor-factory, etc).
- is_admin: false in db data-export.test.ts makeUser factory.

Known follow-up: the agent-side failureLogger wiring (passing
failureLogger to generateProblemVariant + safeLogFailure calls in
tryGenerateOne) is NOT included in this commit because it conflicted
non-trivially with STORY-039d's spec-clarity judge runtime integration
that landed on main first. The variant_gate_failures table is set up
to receive failure entries; wiring the agent to write to it is filed
as a STORY-039g follow-up.
khoks added a commit that referenced this pull request May 11, 2026
…e) [STORY-039e]

Squashed rebase of the STORY-039e branch onto current main (which already
includes STORY-039d's spec-clarity judge merged via PR #75).

Lands:
- Migration 0025_variant_gate_failures: CHECK-constrained failure_reason
  enum (parse_error / identity_mismatch / spec_clarity_judge /
  self_validation / retry_exhausted), failure_detail jsonb, model_id,
  attempt_number; indexed on (source_problem_id, attempted_at desc).
- Migration 0026_users_is_admin: boolean default false.
- @learnpro/db variant-gate-failures helpers + tests.
- apps/api admin route GET /v1/admin/variant-failures with 401/403/200
  gates + filter + pagination.
- apps/web admin page at /admin/variant-failures (server component).
- is_admin: false added to apps/api test session factories (data-controls,
  export, index, onboarding, profile-insights, session-plan, today-plan,
  tutor, weekly-plan, autonomy, bug-finding-scores, cheatsheet,
  cheatsheet-cron, email-digest, email-digest-cron, install-eligible,
  llm-mode, notifications, portfolio, profile-insights-cron, quiet-hours,
  recommendation, sandbox*, tutor-factory, etc).
- is_admin: false in db data-export.test.ts makeUser factory.

Known follow-up: the agent-side failureLogger wiring (passing
failureLogger to generateProblemVariant + safeLogFailure calls in
tryGenerateOne) is NOT included in this commit because it conflicted
non-trivially with STORY-039d's spec-clarity judge runtime integration
that landed on main first. The variant_gate_failures table is set up
to receive failure entries; wiring the agent to write to it is filed
as a STORY-039g follow-up.
khoks added a commit that referenced this pull request May 11, 2026
…e) [STORY-039e] (#76)

Squashed rebase of the STORY-039e branch onto current main (which already
includes STORY-039d's spec-clarity judge merged via PR #75).

Lands:
- Migration 0025_variant_gate_failures: CHECK-constrained failure_reason
  enum (parse_error / identity_mismatch / spec_clarity_judge /
  self_validation / retry_exhausted), failure_detail jsonb, model_id,
  attempt_number; indexed on (source_problem_id, attempted_at desc).
- Migration 0026_users_is_admin: boolean default false.
- @learnpro/db variant-gate-failures helpers + tests.
- apps/api admin route GET /v1/admin/variant-failures with 401/403/200
  gates + filter + pagination.
- apps/web admin page at /admin/variant-failures (server component).
- is_admin: false added to apps/api test session factories (data-controls,
  export, index, onboarding, profile-insights, session-plan, today-plan,
  tutor, weekly-plan, autonomy, bug-finding-scores, cheatsheet,
  cheatsheet-cron, email-digest, email-digest-cron, install-eligible,
  llm-mode, notifications, portfolio, profile-insights-cron, quiet-hours,
  recommendation, sandbox*, tutor-factory, etc).
- is_admin: false in db data-export.test.ts makeUser factory.

Known follow-up: the agent-side failureLogger wiring (passing
failureLogger to generateProblemVariant + safeLogFailure calls in
tryGenerateOne) is NOT included in this commit because it conflicted
non-trivially with STORY-039d's spec-clarity judge runtime integration
that landed on main first. The variant_gate_failures table is set up
to receive failure entries; wiring the agent to write to it is filed
as a STORY-039g follow-up.
@khoks
khoks deleted the story/039d-variant-spec-clarity-judge branch May 14, 2026 22:55
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