Skip to content

feat(stats): surface failureCategory breakdown from metrics.json #783

Closed

Description

@admarble

Context

#761 (PR #781) added a bounded-enum failureCategory to MetricRunSchema (rate_limit, billing, timeout, api_error, build_error, hook_failure, context_overflow, unknown), recorded on failed runs in .sequant/metrics.json. Nothing surfaces it yet: sequant stats shows outcome counts and bars from metrics (src/commands/stats.ts:441-443, display ~:608+) and a log-derived "Common Failures" list of raw error strings (:354), but the new privacy-safe why did it fail dimension is invisible.

Surfacing it answers "what's killing my runs — rate limits or real failures?" directly from metrics, without needing run logs to still exist (logs rotate; metrics don't).

Proposal

Add a failure-category breakdown to the metrics-based analytics display, next to the outcome bars — e.g.:

  Failure categories (12 failed runs)
    rate_limit   5 (42%)
    build_error  4 (33%)
    timeout      2 (17%)
    unclassified 1 (8%)

Acceptance Criteria

Out of scope

Changing what gets recorded (#761 owns the recording path); the log-derived "Common Failures" section (raw strings stay as-is — different data source, different purpose).

References

Activity

  1. added
    enhancement
    cli
    on Jul 18, 2026
  2. admarble commented on Jul 18, 2026

    @admarble
    ContributorAuthor

    → PROCEED — Clean spec; src/commands/stats.ts matches the referenced outcome-bars code; no overlap with other in-flight work.

    Workflow: spec → testgen → exec → qa (--testgen — AC-6 mandates unit tests: mixed corpus + zero-failure cases)

    Run: sequant run 783 -Q --testgen

  3. admarble commented on Jul 18, 2026

    @admarble
    ContributorAuthor

    Plan — /spec

    Complexity: Standard (6 ACs, 1 directory)

    AC Quality Check

    All 6 ACs passed lint (0 flagged).

    Scope Assessment

    Non-Goals: Present ("Out of scope": recording path #761, log-derived Common Failures section).

    Metric Value Status
    Feature count 2 ⚠️
    AC items 6 ⚠️
    Directory spread 1 ✅

    Verdict: ⚠️ SCOPE_WARNING → quality loop auto-enabled.

    Acceptance Criteria

    • AC-1: Failure-category breakdown rendered in the metrics-based display, adjacent to outcome bars — Unit Test: mixed corpus → section renders with counts/percentages
    • AC-2: Records without failureCategory bucketed as unclassified, distinct from enum unknown — Unit Test: pre-feat: detect rate-limit failures mid-chain and halt cleanly instead of timeout-cascading #761 record + unknown-classified record produce two separate buckets
    • AC-3: Section hidden when zero failed runs — Unit Test: success/partial-only corpus → no "Failure categories" header
    • AC-4: Computed after cohort filters — Unit Test: --label/--since force log-mode (feat(stats): add --label and --since filters for cohort measurement #640 bypasses metrics entirely), so the metrics breakdown never renders on a filtered cohort; breakdown is computed inside calculateMetricsAnalytics from the passed run set, post-filter by construction
    • AC-5: JSON mode (--json, metrics source) includes the breakdown; CSV is log-sourced only → N/A for CSV — Unit Test: JSON output has failureCategories + per-run failureCategory already present via runs
    • AC-6: Mixed corpus + zero-failure corpus tests — Unit Test (covers AC-1/2/3)

    Implementation Plan

    Phase 1: Compute (src/commands/stats.ts)

    1. Extend MetricsAnalytics with failureCategories: { category: string; count: number }[] (sorted desc by count, ties stable by enum order; unclassified bucket for absent field).
    2. Compute in calculateMetricsAnalytics over runs with outcome === "failed" — pure function of the passed runs array (AC-4 by construction).

    Phase 2: Display
    3. In displayMetricsAnalytics, render Failure categories (N failed runs) section immediately after the outcome bars; per-category count (pct%); skip entirely when failedCount === 0 (AC-3).
    4. Add failureCategories to the --json metrics-source output object (AC-5).

    Phase 3: Tests + docs
    5. src/commands/stats.test.ts: mixed corpus (categorized failed, uncategorized failed, unknown-categorized failed, success, partial), zero-failure corpus, JSON inclusion.
    6. docs/reference/analytics.md: document the breakdown + unclassified semantics.

    Design Review

    1. Where does this logic belong? src/commands/stats.ts — same module that owns calculateMetricsAnalytics/displayMetricsAnalytics; no new files.
    2. Simplest correct approach? Aggregate in the existing analytics pass; no schema change, no new data source.
    3. Existing pattern? Mirrors the Token Usage section (#AC-10): computed in analytics, conditionally rendered, included in JSON.
    4. Senior pushback? "Why not include partial runs that carry a category?" — see Open Questions.

    Open Questions

    1. Partial runs can carry failureCategory (run-orchestrator.ts:1557 records it whenever ≥1 issue failed, so a partial run may have one). AC-1/AC-3 say "failed runs" — implementing literally (outcome === "failed" only). If partial-run categories should count, that's a follow-up; noted here so the signal loss is a decision, not an accident.

    Recommended Workflow

    Phases: spec → testgen → exec → qa | Quality Loop: enabled (SCOPE_WARNING) — matches prior /assess recommendation.

    Label Review

    Current: enhancement, cli — adequate. No browser testing needed (CLI output only); testgen recommended (unit-test ACs).

  4. added
    planned
    on Jul 18, 2026
  5. admarble commented on Jul 18, 2026

    @admarble
    ContributorAuthor

    Test Stubs Generated — /testgen

    AC Verification Happy Path Failure Paths
    AC-1 Unit Test 1 test —
    AC-2 Unit Test 2 tests —
    AC-3 Unit Test 1 test —
    AC-4 Unit Test 1 test —
    AC-5 Unit Test 1 test —
    AC-6 Meta (this suite) — 2 edge cases

    Unit Test Stubs

    • src/commands/stats-failure-categories.test.ts (worktree) — 6 AC stubs + 2 edge-case stubs (all-unclassified corpus; partial-run-with-category exclusion), following the existing stats.test.ts mock conventions.

    Edge cases baked in:

    • All-unclassified failed corpus (no NaN%, single bucket at 100%)
    • Partial runs carrying failureCategory are excluded from the failed-run breakdown (AC-1 literal — see Open Question 1 in the plan)

    Next: /exec implements the feature and fills these stubs.

  6. admarble commented on Jul 18, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #783

    AC Coverage

    AC Source Description Status Notes
    AC-1 Original Breakdown rendered from MetricRun.failureCategory over failed runs, adjacent to outcome section MET Rendered immediately after the outcome bars in displayMetricsAnalytics (src/commands/stats.ts); dogfooded against the real 102-run corpus (25 failed → 23 unclassified, 2 billing)
    AC-2 Original Absent field bucketed as unclassified, distinct from enum unknown MET run.failureCategory ?? "unclassified"; test asserts both buckets appear simultaneously for an unknown-classified + field-absent pair
    AC-3 Original Section hidden with zero failed runs MET Guarded on failedCount > 0; test asserts no header on success/partial-only corpus
    AC-4 Original Respects cohort filters — computed after filtering MET --label/--since force log-mode (#640), bypassing metrics entirely — asserted for both flags; the breakdown is a pure function of the runs array passed to calculateMetricsAnalytics, so it is post-filter by construction
    AC-5 Original Machine-readable output includes breakdown MET --json (metrics source) emits failureCategories: [{category, count}]; per-run field already in runs[]. CSV is log-sourced → N/A per the AC's own "otherwise N/A" branch
    AC-6 Original Unit tests: mixed corpus + zero-failure corpus MET 10 new tests in stats-failure-categories.test.ts incl. edge cases (all-unclassified, partial-run-with-category exclusion)

    Coverage: 6/6 AC items fully met. Derived ACs: none in spec.

    Quality Metrics

    Metric Value Status
    Type issues (any) 0 OK
    Deleted tests 0 OK
    Files changed 4 OK
    Lines added/deleted +451 / −2 OK
    Build / Lint / Tests tsc ✅ · eslint ✅ · 30/30 ✅ (both stats suites) OK

    Precheck Findings

    Section Status Surfaced
    Fixtures pass 1 (issue's example output block — see adversarial re-read)
    Sibling-grep pass 2 new identifiers, 0 sibling sites
    AC literal-diff not_applicable no PR at precheck time

    Test Coverage Analysis

    Changed File Tier Has Tests?
    src/commands/stats.ts Standard ✅ stats-failure-categories.test.ts + existing stats.test.ts
    docs/reference/analytics.md, CHANGELOG.md Optional — (docs)

    Execution Evidence

    Test Command Result
    Happy path stats (real 102-run corpus) ✅ section renders: unclassified 23 (92%), billing 2 (8%)
    JSON stats --json ✅ failureCategories present, counts match corpus
    Filter bypass stats --label docs ✅ log-mode display, no metrics breakdown

    Evidence status: Complete

    Risk Assessment

    • Likely failure mode: Rounded percentages may not sum to exactly 100 (cosmetic, matches existing display style). Partial runs carrying a failureCategory are excluded by design (AC text says "over failed runs") — that signal loss is documented in the plan's Open Question 1, not accidental.
    • Not tested: CSV path (unchanged, log-sourced — out of scope per issue).
    • Sibling sites considered: log-based "Common Failures" section shares the purpose but is explicitly out of scope per the issue's Out-of-scope section; no cross-file siblings.
    • Sibling-line audit: the log-mode JSON/CSV emitters were audited — they never touch failureCategory (different schema), so no stale sibling.

    Adversarial Re-Read

    Findings: No gaps found because: the issue's motivating example block (the one precheck fixture) was compared against real output — semantics match exactly (bucket names incl. unclassified, counts, percentages, failed-run header count); only the header casing differs ("Failure Categories" via ui.sectionHeader vs the example's "Failure categories"), which follows the file's existing section-header convention and the example is prefixed "e.g.". Evidence claims are post-fix validations on the real corpus, not bug repros. Process state inspected: worktree clean, 2 commits, no stash. Non-Goals checked: recording path and Common Failures section untouched. One improvement (bucket typed string) was found and fixed in-review (8372ef4 — now FailureCategory | "unclassified").

    Status: Clean

    CHANGELOG Verification

    [Unreleased] → Added entry present with issue reference (#783). Docs updated (docs/reference/analytics.md: example output + semantics of unclassified).

    Verdict: READY_FOR_MERGE

    All 6 ACs met literally; quality gates (execution evidence, changelog, docs, adversarial re-read) all pass; no improvement suggestions remain open.

  7. admarble commented on Jul 18, 2026

    @admarble
    ContributorAuthor

    /fullsolve Complete

    Issue: #783
    Status: READY_FOR_MERGE
    PR: #786

    Summary

    • AC: 6/6 met (verified literally — see QA review above)
    • Tests: 30/30 passing (10 new + 20 existing stats tests); build + lint green
    • Dogfooded on the real 102-run metrics corpus: unclassified 23 (92%), billing 2 (8%); hidden under cohort filters; present in --json

    Progress

    Phase Iterations Status
    Spec 1/1 Complete (SCOPE_WARNING → quality loop armed, unused)
    Testgen 1/1 Complete (10 stubs → all implemented)
    Exec 1/1 Complete (2 commits)
    QA 1/2 Complete — READY_FOR_MERGE on first pass; one typing improvement fixed in-review (8372ef4)

    CI dispatched (5 checks), PR is MERGEABLE. Ready for human review and merge.

  8. added a commit that references this issue on Jul 19, 2026
  9. admarble commented on Jul 19, 2026

    @admarble
    ContributorAuthor

    Update — breakdown now counts partial runs

    Following review, the failure-category breakdown was widened from failed-only to any run that recorded a failure (outcome failed OR partial).

    Why: a partial run always had ≥1 issue fail and carries a failureCategory, so it is equally the answer to "what's killing my runs?" Success runs never carry the field and stay excluded.

    Deliberate deviation from AC-1's literal "over failed runs" wording — flagged here for the record. Concretely:

    • Header relabeled (N runs with a failure) (was (N failed runs)).
    • Hide condition (AC-3) is now "no run recorded a failure" — a partial-only corpus now shows the section (bucketed unclassified when uncategorized).
    • AC-2 unclassified bucketing preserved for uncategorized partials.

    Dogfooded on the real corpus (102 runs): now 42 runs with a failure (25 failed + 17 partial) → unclassified 40 (95%), billing 2 (5%). All 17 partials were uncategorized (pre-#761), so they land in unclassified.

    Verification: 32 tests pass (added counts categories carried by partial runs, DOES show when only non-success run is a partial, excludes success runs in mixed corpus); build + lint green; merged origin/main (resolved a CHANGELOG conflict with #784); all 5 CI checks pass; PR #786 is MERGEABLE / CLEAN.

  10. added a commit that references this issue on Jul 19, 2026
  11. added 3 commits that reference this issue on Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clienhancementplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions