Skip to content

feat(stats): add --label and --since filters for cohort measurement #640

Closed

Description

@admarble

Problem

sequant stats --detailed already shows label segmentation, but there's no way to filter the report to a single label or date window. This blocks automatic measurement of cohort success rates — e.g. AC-1 from #462 wanted "docs-labeled issues post-#451," which today requires a one-off jq script over .sequant/logs/run-*.json (see #462 closeout comment).

Acceptance Criteria

  • AC-1: sequant stats --label <name> filters all output (table, --json, --csv, --detailed) to runs whose issues carry the given label
  • AC-2: sequant stats --since <YYYY-MM-DD> filters by run startTime (UTC)
  • AC-3: --label and --since compose (both filters apply)
  • AC-4: When filters yield zero matches, output is a clear "no matching runs" message in the same format as the existing empty-data branch (no crash)
  • AC-5: src/commands/stats.test.ts covers: label match, label miss, date filter, combined filter, zero-match case
  • AC-6: bin/cli.ts registers both flags with .option(); README stats section documents them with a one-line example

Method

  • Filter happens after parseLogFile, before calculateStats / calculateDetailedAnalytics
  • A run is included if any issue in log.issues[] carries the label (matches existing label-segment logic in calculateDetailedAnalytics)
  • For per-issue stats with --label, count only the matching issues, not all issues in qualifying runs

References

Activity

  1. added
    enhancement
    cli
    on May 14, 2026
  2. admarble commented on May 14, 2026

    @admarble
    ContributorAuthor

    Plan Complete

    AC Checklist

    • AC-1: stats --label <name> filters all output (table, --json, --csv, --detailed) to runs whose issues carry the given label
    • AC-2: stats --since <YYYY-MM-DD> filters by run startTime (UTC)
    • AC-3: --label and --since compose (both filters apply)
    • AC-4: Zero-match yields a clear "no matching runs" message in the same format as the existing empty branch (no crash)
    • AC-5: src/commands/stats.test.ts covers: label match, label miss, date filter, combined, zero-match
    • AC-6: bin/cli.ts registers both flags; README + docs/reference/analytics.md document them with one-line examples

    Implementation Plan

    Phase 1 — Filter helper + CLI wiring

    1. bin/cli.ts:360-370 — register .option("--label <name>", ...) and .option("--since <date>", ...) on the stats command.
    2. src/commands/stats.ts —
      • Extend StatsOptions with label?: string; since?: string.
      • Add filterLogs(logs, { label, since }): RunLog[] helper (runs after parseLogFile, before calculateStats).
      • Per-run inclusion: --label keeps run iff any issue in log.issues[] carries the label (matches stats.ts:744-761 shape); --since keeps run iff log.startTime >= <since>T00:00:00Z.
      • Force log-mode when either filter is set — metrics schema has no per-issue labels (metrics-schema.ts:84 is issues: number[]), so bypass the metrics short-circuit at stats.ts:941.
      • Validate --since: Date.parse NaN → throw Invalid --since date: <value>. Expected YYYY-MM-DD.
      • Reuse existing empty-branch messages (no new format) when filtering yields zero.

    Phase 2 — Tests
    3. src/commands/stats.test.ts — new describe("--label and --since filters") block with 5 cases: label match, label miss (zero-match), --since cutoff, combined filters, invalid date.

    Phase 3 — Docs
    4. README.md §CLI Commands — append one-line example.
    5. docs/reference/analytics.md §Viewing Analytics — short "Filtering" subsection with both flags.

    Key Decisions

    • Filter at the run-log layer, not metrics. Metrics has no per-issue labels — the only way to satisfy AC-1 is to use .sequant/logs/run-*.json. When --label/--since are set, skip the metrics-first path.
    • Single helper applied at three call sites (JSON / CSV / human). Avoids threading filters into every analytics function.
    • Empty-result reuses existing empty branches — no new "no matching runs" string format; the existing "No data found" / {error, runs: []} / header-only-CSV outputs already satisfy AC-4 verbatim.

    Open Questions

    1. --until upper bound? Recommendation: defer; composable later without breaking change.
    2. Repeated --label for OR-of-labels? Recommendation: defer; AC-1 says single label.

    Recommended Workflow

    Phases: spec → exec → qa (no /test: backend CLI; no /testgen: AC-5 covered by the unit-test work in exec).

  3. added
    planned
    on May 14, 2026
  4. added a commit that references this issue on May 14, 2026
  5. admarble commented on May 14, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #640

    Verdict: READY_FOR_MERGE

    All 6 ACs literally verified, 18/18 unit tests pass, build + lint clean, CLI flags wired and smoke-verified end-to-end.

    AC Coverage

    AC Status Evidence
    AC-1: --label filters all output ✅ MET filterLogs applied at JSON, CSV, human, and --detailed sites. log.issues.some(i => i.labels.includes(label)) matches the AC text.
    AC-2: --since YYYY-MM-DD filters by run startTime UTC ✅ MET Parsed as <since>T00:00:00Z; compared against log.startTime.
    AC-3: Filters compose ✅ MET filterLogs ANDs both predicates; smoke-tested in both positive and negative composed cases.
    AC-4: Zero-match → "no matching runs" in existing empty-branch format ✅ MET emitNoMatchingRuns mirrors existing empty shapes — JSON {error,runs:[]}, header-only CSV, headerBox + warning for human.
    AC-5: 5 test cases ✅ MET label match, label miss/zero-match, --since cutoff, composed AND, invalid date.
    AC-6: CLI registration + README docs ✅ MET bin/cli.ts registers both .option() calls; README + docs/reference/analytics.md both updated.

    Quality Metrics

    Metric Value
    Files changed 6 (stats.ts, bin/cli.ts, stats.test.ts, README.md, analytics.md, CHANGELOG.md)
    Lines +255 / −8
    Type issues (any) 0
    Deleted tests 0
    Tests 18/18 pass (5 new)

    Smoke Test (end-to-end)

    stats --help                           → both --label and --since visible
    stats --label docs --since 2026-02-01  → AND-composed, 1 surviving run
    stats --label bug                      → single-filter match
    stats --label nope --csv               → header-only output
    stats --since not-a-date               → "Invalid --since date" + exit 1
    

    CHANGELOG

    Added entry under ## [Unreleased] / ### Added referencing (#640).

    Risk Assessment

    • Likely failure mode: User passes a --since that excludes all their runs and assumes the filter is broken. Mitigation: unambiguous "No matching runs" message; syntactically invalid dates exit with code 1.
    • Not tested: Positive-result combined filter (where both --label and --since survive). The AND-composition test covers the zero-match path; smoke-verified manually with Jan/Mar fixture. Low risk — filterLogs is two independent if checks chained, both single-filter positive paths are tested.
    • Sibling sites: N/A — new feature; precheck siblingGrep found 0 cross-file siblings for filterLogs / emitNoMatchingRuns.

    Documentation

    • ✅ README — one-line example in §CLI Commands
    • ✅ docs/reference/analytics.md — new §"Filtering by Label or Date" subsection
    • ✅ CHANGELOG — [Unreleased] / Added entry

    Next Steps

    PR is ready. Closes #640.

  6. added 2 commits that reference this issue on May 14, 2026
  7. added a commit that references this issue on Jul 19, 2026
  8. added a commit that references 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