Skip to content

fix(ops-mission-control): prune the stalest of tied ledger entries - #21825

Merged
bolichen97 merged 1 commit into
mainfrom
fix/ledger-prune-tie-21719
Oct 18, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/ledger-prune-tie-21719

Conversation

@bolichen97

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: When the ledger hygiene prune must drop one of two otherwise-equal entries, it drops the one used longest ago and keeps the freshest.

The comment on MAX_LEDGER_ENTRIES says the prune drops "least used, weakest confidence, oldest" first. The code did the opposite on a tie: two entries equal in net uses, trust and confidence, cap reached, and the recently used one was pruned while the stale one stayed.

Why it matters

Once a team ledger reaches its cap, every hygiene pass threw away the lesson someone had just relied on and kept one nobody had needed for weeks. That is silent knowledge loss in the part of the ledger that is most current.

Not a goal

What changed (motivation → approach → change)

Symptom: on a tie, the prune kept the stalest entry and dropped the freshest.

Root cause: the last key of the prune sort in backend/ledger.py was last_used ascending, so the oldest timestamp sorted first and was kept by deduped[:MAX_LEDGER_ENTRIES].

Change: the pass sorts by last_used newest first, then runs the existing value sort (now without last_used), which is stable. Ties keep their newest-first order, so the stalest tied entry sits last and is pruned. last_used is an ISO string, so it cannot be negated inside one key. The code now matches the MAX_LEDGER_ENTRIES comment, so no comment or SOP text changes.

Backwards compatibility

Compatible: the summary keys and the cap are the same. Only which entry is dropped on an exact value tie changes, and tied entries are written newest first instead of oldest first.

Reader: src/kiro_crew/apps/builtins/ops_mission_control/backend/ledger.py:match -- entry: crew page, cron (investigations) -- ranks by its own key and keeps file order only on an exact tie there, so after a pruning pass tied matches list the most recently used first; the existing ops-mission-control match tests are green.

Tests

test_prune_drops_the_stalest_of_tied_entries in tests/test_store_and_gate_ledger.py: two entries equal in everything but last_used (20 and 2 days old, both inside the decay window), cap patched to 1, run in both file orders. Only the newer one survives, pruned is 1, decayed is 0. Red on origin/main in both orders (['older'] != ['newer']). The same test pins that recency only breaks ties: an older entry with 3 uses still beats a fresh entry with none.

Determinism: the test file ran 5 times in a row serially and once under -n 2, all green. The ops-mission-control tests (1119 passed) and the local gate's related backend set (22501 passed) are green.

Manual verification

N/A — unit coverage sufficient: the prune is a pure pass over a real ledger file under an isolated KIROCREW_HOME, and the test drives it end to end.

Related Issues

Closes #21719

Pattern harvest

Not generalizable: a single tie-break key written in the wrong direction against its own documented contract; the regression test pins this one.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 3a52ce1b3b10c5d3f3818b76df4b0548731568e0 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 18, 2026
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 3a52ce1b3b10c5d3f3818b76df4b0548731568e0 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that every writer stores last_used in the same ISO format, since the newest-first sort compares the strings directly.

[DESIGN-REVIEWED] 3a52ce1

Description read

[DESCRIPTION-READ] 5b368056ce773397d4c3dbd22f57202a9bd44b59f52dbdbb1be4cf3f6e191fc0

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 3a52ce1b3b10c5d3f3818b76df4b0548731568e0 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Check that nothing reads the ledger's file order as its age. Tied entries are now written newest first instead of oldest first.

What this change ships

Inventory (3 items) — 3 justified

Intent: when the ledger is full and two entries are equally valuable, keep the one someone just used and drop the stale one. This is a FIX (#21719).

  1. On an exact value tie, the hygiene prune now drops the entry used longest ago instead of the freshest one. — justified
  2. After a prune, the ledger file lists tied entries newest first; the description says so under Backwards compatibility. — justified
  3. A regression test covers the tie in both file orders, and checks that recency only breaks ties and never beats net uses. — justified

[FIRST-PRINCIPLES-REVIEWED] 3a52ce1

Description read

[DESCRIPTION-READ] 3d04a9a5f0bfe2b98ba147f98853c8baf356b8306faf1f9225bc9c24af68272a

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 3a52ce1b3b10c5d3f3818b76df4b0548731568e0 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 3a52ce1

Verdict parsed from the review's SHA-scoped output markers for commit 3a52ce1b3b10c5d3f3818b76df4b0548731568e0.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 3a52ce1b3b10c5d3f3818b76df4b0548731568e0: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 3a52ce1b3b10c5d3f3818b76df4b0548731568e0 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 3a52ce1

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 3a52ce1b3b10c5d3f3818b76df4b0548731568e0: <one-sentence reason>

@bolichen97
bolichen97 enabled auto-merge (squash) October 18, 2026 09:35
The hygiene prune sorted `last_used` ascending, so among entries tied on
net uses, trust and confidence it kept the one used longest ago and
pruned the freshest. That contradicts the `MAX_LEDGER_ENTRIES` contract
(least used, weakest confidence, oldest go first).

Sort most recently used first, then stably by value, so ties keep the
freshest entry and the stalest is pruned.

Closes #21719
@bolichen97

Copy link
Copy Markdown
Contributor Author

[operator: bolichen97] Rebased: main moved in src/kiro_crew/apps/builtins/ops_mission_control/backend/ledger.py and its tests since this head went green (#21787 merged; 63cf7aa -> 462c24f, head 2d3f9a4 -> 3a52ce1). Same single commit; CI re-running.

@bolichen97
bolichen97 force-pushed the fix/ledger-prune-tie-21719 branch from 2d3f9a4 to 3a52ce1 Compare October 18, 2026 09:36
@bolichen97

Copy link
Copy Markdown
Contributor Author

[operator: bolichen97]

outcome: rebutted (checked, no change). Every writer of last_used uses one format: LedgerEntry.create and record_use/upsert set it from models.utc_now_iso() (%Y-%m-%dT%H:%M:%SZ), and _reconcile takes the string max of two such values.
The decay pass already parses that exact format. A fixed-width UTC ISO string sorts the same as the time it names, and main's prune key already compared these strings, so this PR adds no new string comparison. Only the direction changed.

@bolichen97

Copy link
Copy Markdown
Contributor Author

[operator: bolichen97]

outcome: rebutted (checked, no change). No reader treats the ledger's file order as age. match() and handover sort by their own keys (match keeps file order only on an exact tie in its key). ledger_index only uses order to batch which entries it indexes first, and all of them get indexed. dispatch builds a dict keyed by text. stats counts entries.
The file was already written in prune-sort order on main, so its order was never age order. Only exact value ties flip, from oldest-first to newest-first.

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 18, 2026

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tier 1 auto-approve: fix (2 files). Criteria: no conflict, no requested changes, security path denylist clean, design-doc gate clean, security checklist all-NO, no reviewer-lane findings, readiness signal fresh for this head SHA, declared category matches the diff. Category: the ledger prune sorts by recency first so the stalest of tied entries is pruned; test covers both file orders.

@bolichen97
bolichen97 merged commit 51a43d9 into main Oct 18, 2026
104 checks passed
@bolichen97
bolichen97 deleted the fix/ledger-prune-tie-21719 branch October 18, 2026 11:26
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 18, 2026
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.

ops-mission-control: ledger prune keeps the stalest of tied entries, contradicting its cap comment

2 participants