Skip to content

Cover concurrent Gemini tier-fetch coalescing - #150

Merged
HemSoft merged 3 commits into
mainfrom
fix/issue-149-gemini-tier-coalescing
Aug 8, 2026
Merged

Cover concurrent Gemini tier-fetch coalescing#150
HemSoft merged 3 commits into
mainfrom
fix/issue-149-gemini-tier-coalescing

Conversation

@HemSoft

@HemSoft HemSoft commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Closes #149

Summary

  • gate the first Gemini tier response and start a second usage refresh against the same credential
  • prove the second refresh joins the in-flight tier request, waits before quota routing, and uses the returned project and tier label
  • extend isolated URLSession test support with async handlers and cancellation-safe loading tasks
  • record the coverage improvement under Unreleased / Developer Experience

Verification

  • ./test.sh (changelog, release-artifact, and app-runner smoke tests; 352 XCTest tests passed)
  • xcodebuild ... -enableThreadSanitizer YES test (352 tests passed; no Thread Sanitizer diagnostics)
  • targeted coverage-enabled XCTest plus xccov (the existing fetchInProgress false return and continuation enqueue each executed once)
  • xcodebuild ... analyze
  • git diff --check

Residual risk

  • The new observation closure is package-internal and nil in the public initializer, so production behavior is unchanged. Async isolated URL loading is test-target-only and the full standard and sanitizer suites cover its existing and new users.

Summary by cubic

Adds deterministic coverage for concurrent Gemini tier-fetch coalescing and confirms a second usage refresh waits on the in-flight request and uses the tier-derived project for quota routing. No production behavior change.

  • Refactors
    • Added internal tierWaiterEnqueued hook to GeminiUsageProvider and forwarded it to GeminiTierCache.waitForInFlightFetchIfNeeded via a new package-internal initializer.
    • Upgraded isolated test URLSession to async handlers and let cancellation interrupt and stop callbacks once stopLoading begins.
    • Added XCTest verifying a single tier request, coalesced waiters, and correct project IDs; updated CHANGELOG.

Written for commit 712a5cf. Summary will update on new commits.

Review in cubic

Note

Add deterministic test coverage for concurrent Gemini tier-fetch coalescing

  • Adds testGeminiUsageProviderCoalescesConcurrentTierFetches to verify that two concurrent fetches share a single tier HTTP request and derive the same quota project.
  • Introduces a tierWaiterEnqueued callback on GeminiUsageProvider and threads it through GeminiTierCache.waitForInFlightFetchIfNeeded so the test can synchronize precisely when the second fetch joins the waiter queue.
  • Reworks IsolatedTestURLProtocol in NetworkTestSupport.swift to support async handlers with safe cancellation, allowing the test to gate the tier response until both fetches are in-flight.

Macroscope summarized 712a5cf.

Greptile Summary

The PR adds deterministic coverage for concurrent Gemini tier-fetch coalescing and extends isolated URL loading to support asynchronous handlers.

  • Adds an internal waiter-observation hook and verifies concurrent usage refreshes share one tier request.
  • Verifies quota routing uses the project returned by the coalesced tier response.
  • Introduces asynchronous isolated test handlers with task cancellation and stop-state tracking.
  • Documents the concurrency coverage in the changelog.

Confidence Score: 4/5

The PR is not yet safe to merge because isolated URL protocol callbacks can still execute after cancellation begins.

The reply stating the callback race was fixed is contradicted by the current implementation: activeClient() releases its lock before invoking each client callback, so stopLoading() can begin after the check while the callback still proceeds.

Files Needing Attention: CodexBarMacTests/NetworkTestSupport.swift

Important Files Changed

Filename Overview
CodexBarMac/Services/GeminiUsageProvider.swift Adds a package-internal waiter-observation callback while preserving the public initializer’s production behavior.
CodexBarMacTests/GeminiProviderTests.swift Adds deterministic concurrent-fetch coverage proving tier-request coalescing and tier-derived quota routing.
CodexBarMacTests/NetworkTestSupport.swift Adds async isolated handlers and cancellation state, but the previously reported check-to-callback cancellation race remains.
CHANGELOG.md Records the new Gemini concurrency coverage under Developer Experience.

Sequence Diagram

sequenceDiagram
    participant F1 as First usage refresh
    participant Cache as Gemini tier cache
    participant Tier as Tier endpoint
    participant F2 as Second usage refresh
    participant Quota as Quota endpoint
    F1->>Cache: Fetch tier
    Cache->>Tier: Start request
    F2->>Cache: Request tier
    Cache-->>F2: Enqueue waiter
    Tier-->>Cache: Project and tier
    Cache-->>F1: Complete fetch
    Cache-->>F2: Resume waiter
    F1->>Quota: Request using returned project
    F2->>Quota: Request using returned project
Loading

Reviews (3): Last reviewed commit: "Let cancellation interrupt test callback..." | Re-trigger Greptile

@cursor

cursor Bot commented Aug 8, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Reliability

    • Improved handling of simultaneous Gemini usage requests.
    • Concurrent requests now share in-progress tier lookups, reducing duplicate network activity.
    • Quota requests continue to use the project associated with the resolved Gemini tier.
  • Tests

    • Added coverage for concurrent usage requests, request coalescing, and cancellation-safe network handling.

Walkthrough

The PR adds deterministic concurrent Gemini usage coverage. It instruments tier-cache waiter enrollment, updates isolated URL protocol support for asynchronous cancellation, and verifies shared tier fetching and tier-derived quota project routing.

Changes

Gemini concurrency coverage

Layer / File(s) Summary
Asynchronous test network handling
CodexBarMacTests/NetworkTestSupport.swift
The isolated URL protocol now runs asynchronous sendable handlers, tracks tasks, suppresses cancellation failures, and prevents callbacks after stopping or deallocation.
Tier waiter observation
CodexBarMac/Services/GeminiUsageProvider.swift
GeminiUsageProvider accepts an internal waiter callback and passes it to GeminiTierCache, which reports only successful in-flight waiter enrollment.
Concurrent Gemini request validation
CodexBarMacTests/GeminiProviderTests.swift, CHANGELOG.md
The test verifies one tier request, two quota requests using the tier-derived project, consistent tier-aware results, and successful completion. The changelog records the coverage.
Estimated code review effort: 3 (Moderate) ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant RequestA
  participant RequestB
  participant GeminiUsageProvider
  participant GeminiTierCache
  participant TestURLProtocol
  RequestA->>GeminiUsageProvider: Start usage fetch
  RequestB->>GeminiUsageProvider: Start overlapping usage fetch
  GeminiUsageProvider->>GeminiTierCache: Fetch or await tier
  GeminiTierCache->>TestURLProtocol: Send one tier request
  TestURLProtocol-->>GeminiTierCache: Return tier and project
  GeminiTierCache-->>GeminiUsageProvider: Release both callers
  GeminiUsageProvider->>TestURLProtocol: Send quota request for resolved project
  GeminiUsageProvider->>TestURLProtocol: Send quota request for resolved project
  TestURLProtocol-->>RequestA: Return usage
  TestURLProtocol-->>RequestB: Return usage
Loading

Possibly related issues

  • HemSoft/codexbar-ios issue 235 — Both changes add deterministic concurrent provider tests and isolated URL-session synchronization, but target different providers.

Possibly related PRs

Suggested labels: refactor

Poem

A rabbit watched two fetches race,
One tier request held its place.
The project path stayed true and bright,
Two quota calls returned right.
Async gates kept paws in line.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes and reported verification address the linked issue's concurrency, routing, waiter-path, support, changelog, and test-suite objectives.
Out of Scope Changes check ✅ Passed All changes support the linked objective, including async test support and changelog documentation; no unrelated code changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly and concisely describes the main change: deterministic coverage for concurrent Gemini tier-fetch coalescing.
Description check ✅ Passed The description directly explains the concurrency test coverage, async test support, verification, and changelog update.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-149-gemini-tier-coalescing

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@HemSoft

HemSoft commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@macroscope-app review

@HemSoft

HemSoft commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@macroscopeapp

macroscopeapp Bot commented Aug 8, 2026

Copy link
Copy Markdown

Manual reviews triggered for commit 712a5cf:

All prior checks · these links stay valid even if you push more commits.

@macroscopeapp

macroscopeapp Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review is in progress. Results will be posted as check runs when complete.

@coderabbitai coderabbitai Bot added the refactor label Aug 8, 2026
Comment thread CodexBarMacTests/NetworkTestSupport.swift Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@CodexBarMacTests/NetworkTestSupport.swift`:
- Around line 71-80: Add explicit empty deinit declarations to both
IsolatedTestURLProtocol and its nested LoadContext class, matching the existing
test-target convention and satisfying SwiftLint’s required_deinit rule.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 6add99d8-2b83-4635-966d-5b7d87fd8389

📥 Commits

Reviewing files that changed from the base of the PR and between f97038b and 9baeec9.

📒 Files selected for processing (4)
  • CHANGELOG.md
  • CodexBarMac/Services/GeminiUsageProvider.swift
  • CodexBarMacTests/GeminiProviderTests.swift
  • CodexBarMacTests/NetworkTestSupport.swift

Comment thread CodexBarMacTests/NetworkTestSupport.swift
@macroscopeapp

macroscopeapp Bot commented Aug 8, 2026

Copy link
Copy Markdown

Approvability

Verdict: Would Approve

This PR adds test coverage for concurrent Gemini tier-fetch coalescing. Production code changes are limited to adding an optional test synchronization hook that's always nil in production, with no runtime behavior impact. The bulk of changes are test code and test infrastructure.

Macroscope would have approved this PR. Enable approvability here.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 9baeec9b5c

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@HemSoft

HemSoft commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@macroscope-app review

@HemSoft

HemSoft commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@codex review

Comment thread CodexBarMacTests/NetworkTestSupport.swift Outdated
@macroscopeapp

macroscopeapp Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review is in progress. Results will be posted as check runs when complete.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: b3f25cf1ad

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@HemSoft

HemSoft commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@macroscope-app review

@HemSoft

HemSoft commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@macroscopeapp

macroscopeapp Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review is in progress. Results will be posted as check runs when complete.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 712a5cffaf

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@HemSoft
HemSoft merged commit 8bed175 into main Aug 8, 2026
6 checks passed
@HemSoft
HemSoft deleted the fix/issue-149-gemini-tier-coalescing branch August 8, 2026 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cover concurrent Gemini tier-fetch coalescing

1 participant