Skip to content

fix(codegen): scope vector write-sync by the shard key so .vectorize() can't cross-tenant-leak - #278

Merged
prisis merged 2 commits into
alphafrom
advisor/251-codegen-vector-sync-namespace
Aug 1, 2026
Merged

prisis merged 2 commits into
alphafrom
advisor/251-codegen-vector-sync-namespace

Conversation

@prisis

@prisis prisis commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

What

Closes a silent cross-tenant leak: .vectorize()'s auto write-sync was emitted unscoped. emit.ts called createVectorSyncHook({ schema, vectors }) with no namespace, so in a multi-tenant sharded app every tenant's vectors landed in one shared, unpartitioned Vectorize namespace — any tenant's query without a namespace filter could then read another's vectors. The only prior mitigation was a one-time dev console.warn, which is not a security control.

The fix

The hook already supported scoping (createVectorSyncHook's optional namespace, whose docblock says the caller MUST pass the shard/tenant key) — the gap was purely in the emit. And the shard key is reachable at the emission site with zero plumbing: vectorsBuild is inlined in the emitted ShardDO subclass's buildCtx(), so this.currentShardKey() (already protected on ShardDOBase, returning runner.shardKey ?? ROOT_SHARD_NAME) is directly callable — no ShardHost/ctx contract change needed.

emit.ts now threads namespace: this.currentShardKey() === ROOT_SHARD_NAME ? undefined : this.currentShardKey() into the createVectorSyncHook(...) call, gated on a new codegen-time hasShardedVectors flag (a vector-indexed table and .shardBy()).

Single-tenant stays byte-identical

An unsharded/single-tenant app emits the bare (namespace-less) call exactly as before — define-rag.ts documents namespace-less single-tenant as a legitimate mode. Proven two ways: a codegen test that negative-asserts namespace:/ROOT_SHARD_NAME/currentShardKey for an unsharded vectorized table, and zero golden/example delta on regeneration (the only vectorized example, examples/blog, is unsharded, so it exercises exactly the unchanged branch).

Verification

  • @lunora/codegen 975 tests, no golden drift; @lunora/bindings 222. lint:types/lint:eslint clean; build:packages green.
  • Goldens + all examples/*/_generated regenerated via the documented flow → zero delta everywhere (confirmed correct, not coincidental — see above).
  • Cross-shard isolation test (bindings/__tests__/vectors/end-to-end.test.ts): two hooks (namespace shard-a/shard-b) against one shared namespace-partitioning Vectorize fake; asserts each shard's query returns only its own row, plus an unnamespaced query sees both (proving isolation comes from the filter, not the fake's layout) — a real cross-namespace assertion, not just "a namespace was passed."
  • The sharded+vectorized combo has no real example (none combines .shardBy() + .vectorize()), so it's covered by the new unit test rather than a golden.

Context

This is plan 238's shippable prerequisite — a scoped .withVectorIndex() reader (#274) over an unscoped write path buys nothing; this makes the write side agree.

Merge-order note

packages/codegen/src/emit.ts is also edited by #243 (alarm codegen). My change is localized to the vector-index block (~lines 4123–4460); expect a trivial keep-both merge.

Plan: plans/251-*.md.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved vector synchronization for tables configured with .shardBy().
    • Generated shard contexts now use the appropriate shard key for vector namespaces.
    • Root shards continue to operate without a namespace.

…) can't cross-tenant-leak

Vectorize indexes are account-global, so a .vectorize() table that is also
.shardBy()'d had every shard's auto-synced vectors land in one shared,
unpartitioned namespace — any tenant's query could see every other tenant's
vectors. The hook already accepted a namespace option; the codegen emit just
never passed one, so the built-in dev warning fired and was ignored while
production shipped unscoped.

Pass the owning DO's shard key as the namespace when the vectorized table is
.shardBy()'d; map the ROOT_SHARD_NAME sentinel back to undefined so a
root-mode write stays namespace-less. A schema with no sharded vector table
emits the bare call, unchanged — goldens and every examples/*/_generated stay
byte-identical.
@netlify

netlify Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for lunorash ready!

Name Link
🔨 Latest commit fd41490
🔍 Latest deploy log https://app.netlify.com/projects/lunorash/deploys/6a6db16ea611ae000703be21
😎 Deploy Preview https://deploy-preview-278--lunorash.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@prisis, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 39 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 820447e5-9ab7-4ff4-b34c-a683c5600234

📥 Commits

Reviewing files that changed from the base of the PR and between 1231f4f and fd41490.

⛔ Files ignored due to path filters (1)
  • packages/codegen/__tests__/run-codegen.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
📒 Files selected for processing (1)
  • packages/codegen/src/emit.ts

Walkthrough

Vector synchronization now detects vectorized tables configured with .shardBy(). Generated shard contexts pass the current shard key as the Vectorize namespace and preserve namespace-less behavior for root shards and non-sharded vectors.

Changes

Vectorize namespace generation

Layer / File(s) Summary
Detect sharded vector indexes
packages/codegen/src/emit.ts
The generator detects vectorized tables using .shardBy() and imports ROOT_SHARD_NAME when needed.
Emit shard-aware namespace
packages/codegen/src/emit.ts
Generated context setup passes the current shard key to Vectorize. It maps the root-shard sentinel to undefined and preserves the existing behavior for non-sharded vectors.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the fix for shard-scoped vector synchronization and cross-tenant leakage.
Description check ✅ Passed The description clearly explains the problem, fix, scope, testing, regeneration results, and merge considerations, but omits several template headings and checklist items.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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 advisor/251-codegen-vector-sync-namespace

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.

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Thank you for confirming the Contributor License Agreement! 🙏

@codspeed

codspeed Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 152 untouched benchmarks
⏩ 109 skipped benchmarks1


Comparing advisor/251-codegen-vector-sync-namespace (fd41490) with alpha (a540991)2

Open in CodSpeed

Footnotes

  1. 109 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on alpha (d9791ea) during the generation of this report, so a540991 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

… inline .vectorize()

hasShardedVectors only checked table.vectorIndexes, which holds inline
.vectorize() (Shape A) indexes only. Standalone defineVectorIndex(...)
(Shape B, incl. extension-contributed) never get hoisted onto their
owning table and live only in the schema-level vectorIndexes array. A
.shardBy()'d table indexed via Shape B was silently gated out, so its
sync hook stayed wired but namespace-less -- every tenant's vectors
landing in one unpartitioned Vectorize namespace.

Gate across schema.vectorIndexes instead, keyed back to each index's
owning table via VectorIndexIR.table, and discriminate the shard-mode
union on its kind tag rather than a bare typeof check.

Also de-brittles a test assertion that pinned the entire shared-import
line instead of just the ROOT_SHARD_NAME fragment this test owns.
@prisis
prisis merged commit 2c48d58 into alpha Aug 1, 2026
42 of 44 checks passed
@prisis
prisis deleted the advisor/251-codegen-vector-sync-namespace branch August 1, 2026 10:56
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using our GitHub Discussions tab for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 8, 2026

This branch was previously deployed

1 inactive deployment
benchmarks — fd414902 Deployed Aug 1, 2026 by prisis via Benchmarks #1304
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant