Skip to content

fix(store): close 3 aggregate-query tiebreaker sites flagged in #36 §3#38

Merged
yakuphanycl merged 1 commit into
masterfrom
fix/store-aggregate-tiebreakers-2026-04-25
Apr 25, 2026
Merged

fix(store): close 3 aggregate-query tiebreaker sites flagged in #36 §3#38
yakuphanycl merged 1 commit into
masterfrom
fix/store-aggregate-tiebreakers-2026-04-25

Conversation

@yakuphanycl
Copy link
Copy Markdown
Contributor

§0 TL;DR

Closes the 3 aggregate-query tiebreaker sites that PR #36 §3 reported but did not silently fix per the "report, don't silently fix" hard rule from #33. Same SQLite tiebreaker bug class as #30 / #33 / #34 / #36 but DIFFERENT fix shape — these are GROUP BY queries where the deterministic tiebreaker is the GROUP key (alphabetical ASC), not rowid.

Authored by Codex (OpenAI), PR opened by Claude A — Codex's PR creation was blocked by the same `gh` 401 / GitHub connector 403 auth issue we hit 5 times already today. 6-of-6 rescue today per `feedback_codex_pr_creation_blocked.md`. Branch pushed cleanly; PR-creation rescue only.

+3 tests, 131 → 134 passing. After this PR, the SQLite-tiebreaker class is FULLY closed in the instinct repo (per-row + LIMIT-1 + aggregate, all sites covered).

§1 Sites fixed (all src/instinct/store.py)

Function Line Pre-fix ORDER BY Group key Fix
`trending()` real-delta path ~635 `... GROUP BY pattern ORDER BY observations DESC LIMIT ?` `pattern` append `, pattern ASC`
`effectiveness()` ~735 `... GROUP BY pattern ORDER BY confirmed DESC, suggested DESC` `pattern` append `, pattern ASC`
`stats()` by_category ~829 `... GROUP BY category ORDER BY count DESC` `category` append `, category ASC`

§2 Why ASC on group key (different from #34/#36)

PR Mental model Tiebreaker Why
#30, #33, #34, #36 Per-row queries on `instincts` / `confidence_log` / `suggest_log` `rowid DESC` (or `id DESC` where integer PK) Per-row queries return individual records; insertion-order surrogate is the natural deterministic tiebreaker
This PR Aggregate (GROUP BY) queries ` ASC` GROUP BY collapses rows into aggregate values; the only stable tiebreaker is the GROUP key itself, alphabetically ASC

ASC chosen over DESC because aggregate display benefits from alphabetical-stable order on ties (predictable, scannable). DESC would also work but ASC matches the natural "first alphabetically when values tie" convention.

§3 Tests added (3 new)

Each test creates ≥2 groups producing identical aggregate values (tie scenario), calls the function twice, asserts same order both calls AND that the order matches the alphabetical group-key tiebreaker.

  • `test_trending_real_delta_deterministic_under_observations_collision`
  • `test_effectiveness_deterministic_under_confirmed_suggested_collision`
  • `test_stats_by_category_deterministic_under_count_collision`

§4 Validation

```
$ PYTHONPATH=src python -m pytest -q
134 passed in [time] (was 131 pre-fix; +3 new)

$ PYTHONPATH=src ruff check src/instinct/store.py tests/test_store.py
All checks passed!
```

Baseline note: task brief expected 113→116, but current master already includes #37 (PR merged after the brief was drafted), so this clone verifies the live 131→134 delta. +3 strict increase confirmed.

§5 No 4th aggregate site found (decision gate clean)

Audit during this PR: `_promote_universal()` has `GROUP BY cl.pattern` but no aggregate `ORDER BY` tie to stabilize. No 4th aggregate site discovered. Scope stays at exactly the 3 sites #36 §3 flagged.

No other decision-gate triggers. No surprises.

§6 Maintainer checklist

§7 Hard rule self-quote

§8 References — full SQLite-tiebreaker class lineage (instinct)

PR Surface Sites Tiebreaker shape
#30 confidence_log per-row detect_chains `id ASC` (integer PK)
#33 confidence_log per-row + indices history + idx_conflog_*_ts `id ASC` + DESC→ASC index migration
#34 instincts per-row recent / list / trending fallback `rowid DESC` (no integer PK)
#36 instincts per-row + suggest_log LIMIT-1 suggest / search / export_rules / find_duplicates / export_all + _confirm_suggestion `rowid DESC` (instincts) + `id DESC` (suggest_log)
This PR aggregate (GROUP BY) trending real-delta / effectiveness / stats by_category ` ASC`

After this PR merges, all SQLite ORDER BY sites in `src/instinct/store.py` have a deterministic tiebreaker matching their semantic model. The cross-repo rule `feedback_sqlite_rowid_tiebreaker.md` is fully realised in the instinct repo.

🤖 Generated with Claude Code on behalf of Codex (OpenAI)

§0 TL;DR: add deterministic group-key ASC tiebreakers for the 3 aggregate GROUP BY sites flagged in PR #36 §3.

§1 Evidence: adds tests for trending real-delta, effectiveness, and stats by_category tie order; full suite reports 134 passed on current master.

§4 Hard-rule self-quote: did NOT touch per-row tiebreaker sites, server.py, tests/mcp, SKILL.md, .cursor/rules, README, CHANGELOG, or pyproject.
@yakuphanycl yakuphanycl merged commit 26b5435 into master Apr 25, 2026
11 checks passed
@yakuphanycl yakuphanycl deleted the fix/store-aggregate-tiebreakers-2026-04-25 branch April 25, 2026 08:48
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