Repository navigation
feat(agent): variant spec-clarity LLM judge [STORY-039d] - #75
Merged
Merged
Conversation
khoks
marked this pull request as ready for review
May 11, 2026 09:48
khoks
force-pushed
the
story/039d-variant-spec-clarity-judge
branch
from
May 11, 2026 10:00
6ac5df1 to
99e6c44
Compare
…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
force-pushed
the
story/039d-variant-spec-clarity-judge
branch
from
May 11, 2026 14:49
99e6c44 to
071b97c
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
min(scores) < 3.variant-spec-clarity-v1in@learnpro/prompts. New pure judgerunVariantSpecClarityJudgein@learnpro/agent.generateProblemVariantaccepts an optionaljudgeparameter; Fastify route wires it behindLEARNPRO_VARIANT_SPEC_CLARITY_JUDGE=1(default ON when ANTHROPIC_API_KEY is set).Test plan
pnpm --filter @learnpro/agent test variant-spec-clarity-judgepassespnpm --filter @learnpro/agent test problem-variantspasses (new + existing tests)pnpm --filter @learnpro/prompts test variant-spec-clarity-promptpasses (forbidden-phrase test)pnpm format:checkpasses🤖 Generated with Claude Code