Skip to content

fix(search): bound retrieval and avoid repeated turn admission - #7888

Merged
icecrasher321 merged 7 commits into
stagingfrom
codex/fix-search-and-usage-query-cost
Sep 16, 2026
Merged

icecrasher321 merged 7 commits into
stagingfrom
codex/fix-search-and-usage-query-cost

Conversation

@icecrasher321

@icecrasher321 icecrasher321 commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Bound document-scoped vector probes with an indexed lookup and disable JIT inside deadline-bound search transactions.
  • Admit Assistant spending once per logical turn. Continuations recheck current access and the original payer's account standing without repeating usage aggregation; cancellation remains available after usage exhaustion.
  • Keep fresh pooled/member usage checks and existing workflow reservations. Remove duplicate organization reads without adding a billing projection, cache, or shared write lock.
  • Build the search lookup index concurrently with interrupted-build recovery through the existing migration machinery.

Companion: simstudioai/mothership#505

Deploy this Sim change before the companion. Missing validation purpose preserves the existing new-turn behavior, so the Sim change is compatible with the previous Assistant server.

Type of Change

  • Bug fix

Testing

  • Billing and background workflow regression suite: 916 tests passed.
  • Callback, ownership, continuation, cancellation and chat lifecycle suites: 123 tests passed.
  • Real PostgreSQL concurrent accounting, charge freshness, migration, concurrent index build and replay tests; real Redis pooled/member admission and duplicate reservation tests passed.
  • Search query/budget unit tests and application/database type checks passed.
  • Synthetic 300,000-chunk integration fixture on 2 CPU / 1 GiB: 24 tests passed, all 32 normal sampled searches completed, and 7 nearest-neighbor recall comparisons matched the exact baseline. Cache-pressured keyword retrieval can still dominate end-to-end latency.
  • Real Drizzle push creates the lookup index concurrently and leaves it unchanged on replay.
  • Full repository lint, all 46 audits, generated-contract checks and migration safety checks passed.
  • Companion: complete Go test suites, race detector, vet, generated contracts, encrypted prompts and license checks passed.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Sep 16, 2026 8:25pm UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no new actionable correctness, security, migration, or repository-rule violations remain.

Summary

This PR bounds document-scoped search retrieval, adds replay-safe concurrent lookup-index creation, and changes Copilot billing admission so spending is admitted once per logical turn while continuations revalidate access and payer standing. It also extends Credential Groups with integration-specific workspace grants and applies those grants across credential, MCP, trigger, and Copilot discovery paths.

  • Adds bounded search probes and transaction-level deadline protections.
  • Separates new-turn admission, continuation validation, and cancellation behavior.
  • Preserves fresh pooled/member billing checks and duplicate-reservation safety.
  • Adds concurrent, recoverable search lookup-index migration handling.
  • Introduces per-workspace Credential Group integration grants with optimistic concurrency.
  • Rechecks credential visibility and runtime resolution against the selected integration grant.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Request[Copilot lifecycle request] --> Purpose{Validation purpose}
  Purpose -->|New turn| Admission[Resolve payer and check fresh usage]
  Purpose -->|Continuation| Restore[Restore original payer snapshot]
  Restore --> Access[Recheck current resource access]
  Access --> Standing[Recheck actor and payer standing]
  Purpose -->|Cancellation| CancelAccess[Recheck resource access]
  CancelAccess --> Cancel[Allow cancellation without spend admission]
  Admission --> Run[Continue logical turn]
  Standing --> Run
  Search[Document-scoped search] --> Lookup[Indexed document lookup]
  Lookup --> Budget[Bound semantic and keyword probes]
  Budget --> Results[Rank bounded results]
Loading

Reviews (3) · Last reviewed commit: "fix(test): align session policy fixture ..."

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 20 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread scripts/test-knowledge-acls.ts Outdated
Comment thread packages/db/script-migrations/index.ts Outdated
Comment thread packages/db/schema.ts
Comment thread packages/db/scripts/push.ts Outdated
@icecrasher321 icecrasher321 changed the title fix(search): bound retrieval and billing query cost fix(search): bound retrieval and avoid repeated turn admission Sep 16, 2026
@github-actions github-actions Bot added the requires-mothership-merge Has a companion PR on the mothership/copilot side — merge in lockstep label Sep 16, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Cross-repo companion check

One or more companion PRs aren't merged into staging yet. Merging this without them will leave copilot and sim out of sync — merge them in lockstep.

  • ❌ simstudioai/mothership#505 — OPEN, not merged (targets staging) — fix(billing): admit assistant spend once per logical turn

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@icecrasher321 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 36 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

Comment thread apps/sim/lib/billing/calculations/usage-reservation.test.ts
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@icecrasher321 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 37 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

@icecrasher321
icecrasher321 merged commit 95b5b76 into staging Sep 16, 2026
33 checks passed
@icecrasher321
icecrasher321 deleted the codex/fix-search-and-usage-query-cost branch September 16, 2026 20:28

This branch was successfully deployed

1 active deployment
Preview — 93934868 Deployed Sep 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

requires-mothership-merge Has a companion PR on the mothership/copilot side — merge in lockstep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant