Skip to content

fix(pi): make the session roots fingerprint independent of discovery order - #4618

Merged
steipete merged 3 commits into
steipete:mainfrom
dhruva77:fix/pi-session-roots-fingerprint-order
Oct 19, 2026
Merged

steipete merged 3 commits into
steipete:mainfrom
dhruva77:fix/pi-session-roots-fingerprint-order

Conversation

@dhruva77

Copy link
Copy Markdown
Contributor

Summary

PiSessionCostScanner.sessionRootsFingerprint identifies the set of Pi/OMP session roots behind a cached cost scan. The fingerprint is stored in the cache as sessionRootsFingerprint and exposed as the report's scopeFingerprint.

The function sorts each root's retention keys, but it joins the roots in discovery order. That order comes from the order of workingDirectories, from live process contexts, and from previously retained roots. None of these is part of the scope.

Concrete scenario: two Pi projects with their own .pi/settings.json sessionDir are both known to the scanner. If one refresh discovers them as [alpha, beta] and the next as [beta, alpha], the fingerprint changes even though the roots are identical. When that happens:

  • sessionRootsChanged becomes true, which forces a refresh and rewrites the cache scope;
  • cached reads that compare scopeFingerprint reject a report that covers exactly the same roots.

Fix

The per-root fingerprint components are sorted before they are joined, which is a one-line .sorted() call. Each component already carries the root path and all of its flags, so sorting is lossless.

The parser that restores retained roots splits the fingerprint into components and doesn't depend on their order.

Traversal order is unchanged: the scanner still walks defaultSessionRoots in discovery order. Two side effects:

  • An existing cache whose stored fingerprint is not already in sorted order will see one scope change on its first refresh after upgrading. After that the fingerprint is stable.
  • Roots that are retained only from a previous fingerprint are re-added in sorted order instead of their earlier order. Roots are deduplicated by path, so this doesn't change which files are scanned.

Tests

New test in PiSessionProcessContextTests: pi cost scope ignores the discovery order of identical session roots. It sets up three synthetic projects, each with a project-level sessionDir under a temporary HOME, and checks three things through PiSessionCostScanner.scopeFingerprint(options:):

  1. [alpha, beta] and [beta, alpha] give the same fingerprint;
  2. [alpha, gamma] gives a different one;
  3. the fingerprint still names both configured roots.

Without the fix, the reordering check fails: scope([projects[1], projects[0]]) == forward.

taskpolicy -b nice -n 20 swift build --build-tests --jobs 2
taskpolicy -b nice -n 20 swift test --skip-build --filter 'PiSessionProcessContextTests|PiSessionCostScannerTests|PiSessionCostV8UpgradeTests|PiSessionCostRefreshReliabilityTests|PiProviderTests'
# 66 tests in 5 suites passed

.build/lint-tools/bin/swiftformat --lint <touched files>   # 0/2 files require formatting

Notes

The fix is small and isolated: two production lines (one call plus a comment) and one focused test. Root discovery, traversal, the cache format and pricing are unchanged.

…order

The fingerprint sorted each root's retention keys but joined the roots in discovery order. That order follows working directories and process contexts, so the same roots found in another order invalidated the cached scope and forced a rescan. Sort the components before joining; traversal order is unchanged.
@clawsweeper

clawsweeper Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-18T19:10:49.863974Z b626f10 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal priority bug or improvement with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 18, 2026
@clawsweeper

clawsweeper Bot commented Oct 18, 2026

Copy link
Copy Markdown

Codex review: needs real behavior proof before merge.

What this changes

Sort Pi/OMP session-root fingerprint components so discovery order does not invalidate an unchanged cache scope.

Example: Two projects use /tmp/pi-sessions-alpha and /tmp/pi-sessions-beta, discovered in opposite orders on successive refreshes.

  • Before: The fingerprint changes, forcing a refresh and rejecting otherwise matching cached reports.
  • After: Both discovery orders produce the same fingerprint.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The implementation is focused and source-supported, but required real-behavior and existing-cache upgrade evidence remain unprovided.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🦐 gold shrimp (3/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Pi/OMP cost history can be rescanned or rejected from cache when the same project roots are discovered in a different order.
Reason: The change restores set-based cache identity within the existing accounting design without adding options or parallel machinery.

Merge readiness

⛔ Blocked before merge - 2 items remain

Keep open: this PR fixes a source-confirmed cache identity defect still present on main and in v0.77.0, with no concrete introduced bug found.

Priority: P2
Reviewed head: b626f10206ecc503ad174ef6da488dbe0d5baa5f

Before merge

  • Add real behavior proof - The supplied evidence exercises scopeFingerprint through synthetic project settings in tests, but provides no after-fix shipped CLI or app output for reordered roots; add redacted terminal output or logs showing stable cache reuse and the latest-release cache transition, removing private paths and credentials. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.
  • Add data-model compatibility proof - The review found that existing stored data may not work after upgrade. Show that existing data still loads and works with this change.

Findings

None.

Tests

  • Missing end-to-end proof: Show the shipped CLI or app reusing the same scope after root-order reversal, including a v0.77.0 cache upgrade with totals and retained roots intact; the reported base-failure/head-pass test result was not independently executed.
Agent review details

How this fits together

The Pi cost scanner resolves project and process session roots, reads local transcripts, and publishes cached token and cost reports whose scope is identified by the root fingerprint.

flowchart TD
    A[Project settings and process contexts] --> B[Session root discovery]
    B --> C[Sorted scope fingerprint]
    B --> D[Transcript scanner]
    C --> E[Cache scope validation]
    D --> E
    E --> F[App and CLI cost reports]
Loading

Technical review

Best possible solution:

Keep the existing fingerprint owner and canonicalize its complete root components before joining them.

Do we have a high-confidence way to reproduce the issue?

Source confirms that reversing two configured project roots changes main's fingerprint and reaches cache invalidation; no target code was executed during this read-only review.

Is this the best way to solve the issue?

Sorting the existing complete components is the smallest complete fix for order-sensitive scope identity, preserving flags, retention keys, and live discovery traversal.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 6480977b47f6.

Provenance checked

  • Pi root-scope fingerprint and retained-root identity keeps the original intent (81c8a1c: The merged feature ties cached reports to selected session roots and preserves explicit-selector provenance across process exit.)

Testing

Proof path: unit tests only.

Security

None.

Evidence

What I checked:

Likely related people:

  • Yuxin Qiao: Raw commit 81c8a1c adds Sources/CodexBarCore/PiSessionCostScanner.swift:569 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 81c8a1cbf9bb; files: Sources/CodexBarCore/PiSessionCostScanner.swift)
  • Peter Steinberger: Raw commit 26ee8be adds Sources/CodexBarCore/PiSessionCostScanner.swift:105 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 26ee8be34254; files: Sources/CodexBarCore/PiSessionCostScanner.swift)

Review metrics

Metric Value Why it matters
Patch footprint Production +2 lines; tests +31 lines A minimal production fix with focused coverage through real project-root resolution.

Labels

Label changes:

  • add P2: Unchanged local-history roots can cause unnecessary rescans and cached-report rejection, with limited provider-specific impact.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • P2: Unchanged local-history roots can cause unnecessary rescans and cached-report rejection, with limited provider-specific impact.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Provide a shipped CLI or app trace showing reordered roots retain one scope and a v0.77.0 cache refresh preserves history.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 18, 2026, 3:20 PM ET / 19:20 UTC.

Refs steipete#4618.

Co-authored-by: Dhruva Sahrawat <21278423+dhruva77@users.noreply.github.com>
steipete added a commit that referenced this pull request Oct 19, 2026
@steipete
steipete merged commit 317bbe3 into steipete:main Oct 19, 2026
3 checks passed
@steipete

Copy link
Copy Markdown
Owner

Merged in 317bbe3, thanks @dhruva77! The Pi session-roots fingerprint no longer depends on discovery order, so reordered roots keep their cached totals. We kept your sorted fingerprint and added coverage for cached reuse and the one-time upgrade from older fingerprints.

@dhruva77
dhruva77 deleted the fix/pi-session-roots-fingerprint-order branch October 20, 2026 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal priority bug or improvement with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants