Skip to content

fix(bindings,codegen): scope ctx.vectors reads by the DO shard namespace - #294

Closed
prisis wants to merge 2 commits into
alphafrom
advisor/255-vector-tenant-isolation
Closed

prisis wants to merge 2 commits into
alphafrom
advisor/255-vector-tenant-isolation

Conversation

@prisis

@prisis prisis commented Aug 2, 2026 •

Copy link
Copy Markdown
Member

Branched before #288/#289 landed, so it needs a rebase onto current alpha before merge.

Commits

  • fix(bindings,codegen): scope ctx.vectors reads by the DO shard namespace
  • fix(bindings,codegen,ai): close the root-shard read leak, honor RAG's namespace

Files this branch still changes relative to alpha

packages/ai/__tests__/rag.test.ts
packages/bindings/__tests__/vectors/ctx.test.ts
packages/bindings/docs/index.mdx
packages/bindings/src/vectors/context.ts
packages/codegen/__tests__/run-codegen.test.ts
packages/codegen/src/emit.ts
packages/platform/src/capabilities.ts

Summary by CodeRabbit

  • New Features

    • Added namespace-aware vector search, upsert, retrieval, and deletion operations.
    • Sharded vector indexes now automatically scope operations to the appropriate namespace.
    • Added optional namespace support for ID-based reads and deletes.
    • Added namespace information to vector records.
  • Bug Fixes

    • Prevented unsafe root-level access to sharded indexes without an explicit namespace.
    • Enforced namespace filtering for ID-based vector operations.
  • Documentation

    • Documented tenant isolation and namespace behavior for vectorized tables.

prisis and others added 2 commits August 1, 2026 16:45
Vectorize indexes are account-global, so a sharded app's ctx.vectors.query
searched every tenant's vectors even though the auto-sync write hook was
already namespace-scoped (plan 251) — the read side never got the same
treatment. getByIds/deleteByIds had no namespace at all, so a guessable
row id could read or delete another tenant's vector record.

createContextVectors now accepts an optional default namespace: query and
upsert/upsertNow default to it when the caller passes none (an explicit
namespace always wins), and getByIds/deleteByIds enforce it client-side
since Vectorize's id operations take no remote namespace filter — getByIds
drops non-matching records (fail closed on a record with no namespace at
all), deleteByIds resolves ids first and only deletes the matching subset.
Codegen threads the DO's own shard key into the ctx adapter for a
`.shardBy()`'d vectorized table, reusing the same sentinel-mapped
expression already computed for the write hook; unsharded schemas keep
emitting the bare call, byte-identical.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
… namespace

Thermo-nuclear review found two problems with the ctx.vectors namespace
scoping this plan added:

1. Root-shard fail-open. codegen mapped ROOT_SHARD_NAME to `undefined` for
   BOTH the write hook and the read facade — correct for writes (a write
   event only ever fires for a table this DO instance owns, so a root
   instance never processes a sharded table's write), but wrong for reads:
   `ctx.vectors` is one flat facade over every declared index, reachable
   from any DO instance, so a namespace-less call from the root instance
   against a SHARDED index would search/mutate every tenant's vectors — the
   exact leak this plan closes, just reached from the read/explicit-write
   side. Fixed by threading `shardedIndexNames` (which vector indexes are
   actually sourced from a `.shardBy()`'d table) into `createContextVectors`:
   an index outside that set always stays namespace-less (a root-scoped
   index has no tenant identity to begin with); an index inside it, called
   from the root instance with no explicit override, now throws instead of
   silently defaulting to "every tenant" or masquerading the problem as "no
   data". A per-tenant instance's own shard key remains the default for a
   sharded index, unchanged.

2. `@lunora/ai/rag` breakage. `RagVectors.getByIds`/`deleteByIds` declare a
   third `namespace` parameter that `defineRag` already calls with — the
   facade's 2-arg implementation silently dropped it, so RAG's own
   `effectiveNamespace` never reached Vectorize: `readHead()` always missed
   (re-embedding every document on every `index()`), and `remove()` silently
   no-opped, orphaning vectors. `getByIds`/`deleteByIds` now accept an
   optional trailing `namespace` that overrides the default exactly like
   `query`'s `input.namespace` does — a pure, backward-compatible addition
   (extra optional parameters keep the type assignable to
   `@lunora/server`'s unchanged two-argument `VectorSearchReader`/
   `VectorSearch`).

Also tightened the docblocks to state plainly that an explicit namespace is
a deliberate override (not a hole) and that the id path's silent partial
delete has no per-call signal today.

New tests: bindings' ctx.test.ts covers the per-index default, the
root-instance throw (query/upsert/getByIds/deleteByIds), and the third-arg
override; codegen's run-codegen.test.ts covers a mixed schema (one sharded,
one root-scoped vectorized table) asserting `shardedIndexNames` lists only
the sharded one; @lunora/ai's rag.test.ts adds a `namespaceStrictVectors`
double (unlike the existing `memoryVectors`, it actually enforces the third
argument) proving `readHead`/`deleteChunkRange` thread `effectiveNamespace`
correctly — @lunora/ai has no dependency on @lunora/bindings by design, so
this and the bindings-side third-arg tests together cover the contract from
both sides. Goldens byte-identical — no committed fixture or example has a
sharded vector table.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@netlify

netlify Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for lunorash failed.

Name Link
🔨 Latest commit c88b6e0
🔍 Latest deploy log https://app.netlify.com/projects/lunorash/deploys/6a6f0b305783a6000805934e

@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Vector contexts now support namespace-aware operations for sharded indexes. Generated code passes shard namespaces. Root access requires explicit namespaces. ID-based reads and deletes apply client-side namespace filtering.

Changes

Vector namespace isolation

Layer / File(s) Summary
Context namespace operations
packages/bindings/src/vectors/context.ts
Vector context types accept namespace options. Upserts and queries resolve namespaces. ID-based reads and deletes filter records by namespace.
Generated namespace wiring
packages/codegen/src/emit.ts, packages/bindings/docs/index.mdx, packages/platform/src/capabilities.ts
Generated contexts receive shard-derived namespace configuration. Documentation describes namespace isolation and Vectorize capability behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • anolilab/lunora#278: Updates code generation to pass shard-derived namespaces for sharded vector indexes.

Sequence Diagram(s)

sequenceDiagram
  participant GeneratedDO
  participant ContextVectors
  participant Vectorize
  GeneratedDO->>ContextVectors: Create context with shard namespace
  ContextVectors->>Vectorize: Query or upsert using resolved namespace
  ContextVectors->>Vectorize: Fetch IDs for namespace verification
  ContextVectors-->>GeneratedDO: Return namespace-filtered records
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description lists commits and affected files but does not provide the required summary, test plan, checklist, linked issues, notes, or CLA confirmation. Complete the required template sections, especially the change summary, test plan with results, checklist, migration notes if applicable, and CLA confirmation.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: scoping vector reads by the Durable Object shard namespace.
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 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch advisor/255-vector-tenant-isolation

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 2, 2026

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Thank you for confirming the Contributor License Agreement! 🙏

@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.

🧹 Nitpick comments (2)
packages/bindings/src/vectors/context.ts (1)

175-177: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider throwing a typed error instead of bare Error.

The sibling facade in packages/bindings/src/vectors/create-vectors.ts throws TypeError, RangeError, or LunoraError("INTERNAL", ...) for facade-level failures. This throw uses a bare Error, so callers cannot classify it by code or type. A typed error also keeps the HTTP/status mapping consistent for app code that catches Lunora errors.

This is a consistency improvement only. The fail-closed behavior is correct.

🤖 Prompt for 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.

In `@packages/bindings/src/vectors/context.ts` around lines 175 - 177, Replace the
bare Error thrown in the sharded-table namespace validation path with the
established typed error used for facade-level failures, preferably the
appropriate LunoraError code or error type consistent with create-vectors.ts.
Preserve the existing fail-closed condition and descriptive message while making
the failure classifiable by callers and HTTP/status mapping.
packages/codegen/src/emit.ts (1)

4480-4485: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Extract the sharded index names once and reuse them.

The predicate shardedTableNames.has(index.table) is now applied twice over schema.vectorIndexes: once at line 4151 to compute hasShardedVectors, and again here to build the emitted list. Derive both from one array so the two can never disagree, and so the template expression stays readable.

The gating and emitted output are otherwise correct: vectorNamespaceField and vectorsContextNamespaceOption share the same hasShardedVectors gate, so vectorShardKey is always declared before use, and ROOT_SHARD_NAME is imported under the same condition.

♻️ Proposed refactor

Add near line 4151:

-    const hasShardedVectors = schema.vectorIndexes.some((index) => shardedTableNames.has(index.table));
+    const shardedVectorIndexNames = schema.vectorIndexes.filter((index) => shardedTableNames.has(index.table)).map((index) => index.name);
+    const hasShardedVectors = shardedVectorIndexNames.length > 0;

Then at lines 4480-4485:

     const vectorsContextNamespaceOption = hasShardedVectors
-        ? `, { namespace: vectorShardKey === ROOT_SHARD_NAME ? undefined : vectorShardKey, shardedIndexNames: [${schema.vectorIndexes
-              .filter((index) => shardedTableNames.has(index.table))
-              .map((index) => JSON.stringify(index.name))
-              .join(", ")}] }`
+        ? `, { namespace: vectorShardKey === ROOT_SHARD_NAME ? undefined : vectorShardKey, shardedIndexNames: [${shardedVectorIndexNames.map((name) => JSON.stringify(name)).join(", ")}] }`
         : "";
🤖 Prompt for 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.

In `@packages/codegen/src/emit.ts` around lines 4480 - 4485, Extract the filtered
sharded vector indexes into a shared array near the existing hasShardedVectors
calculation, derive hasShardedVectors from that array, and reuse it when mapping
emitted index names in vectorsContextNamespaceOption. Keep the existing gating
and generated output unchanged while removing the duplicate
shardedTableNames.has predicate and simplifying the template expression.
🤖 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.

Nitpick comments:
In `@packages/bindings/src/vectors/context.ts`:
- Around line 175-177: Replace the bare Error thrown in the sharded-table
namespace validation path with the established typed error used for facade-level
failures, preferably the appropriate LunoraError code or error type consistent
with create-vectors.ts. Preserve the existing fail-closed condition and
descriptive message while making the failure classifiable by callers and
HTTP/status mapping.

In `@packages/codegen/src/emit.ts`:
- Around line 4480-4485: Extract the filtered sharded vector indexes into a
shared array near the existing hasShardedVectors calculation, derive
hasShardedVectors from that array, and reuse it when mapping emitted index names
in vectorsContextNamespaceOption. Keep the existing gating and generated output
unchanged while removing the duplicate shardedTableNames.has predicate and
simplifying the template expression.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 47c41bc9-1cce-4413-acd4-59eb8251d455

📥 Commits

Reviewing files that changed from the base of the PR and between 6a4dd72 and c88b6e0.

⛔ Files ignored due to path filters (3)
  • packages/ai/__tests__/rag.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/bindings/__tests__/vectors/ctx.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/codegen/__tests__/run-codegen.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
📒 Files selected for processing (4)
  • packages/bindings/docs/index.mdx
  • packages/bindings/src/vectors/context.ts
  • packages/codegen/src/emit.ts
  • packages/platform/src/capabilities.ts

@codspeed

codspeed Bot commented Aug 2, 2026

Copy link
Copy Markdown

This PR has no benchmarks

⚠️ No benchmarks were detected in both the base of the PR and the PR.
Please ensure that your benchmarks are correctly instrumented with CodSpeed.

Check out the benchmarks creation guide


Comparing advisor/255-vector-tenant-isolation (c88b6e0) with alpha (6a4dd72)

Open in CodSpeed

@prisis

prisis commented Aug 2, 2026

Copy link
Copy Markdown
Member Author

Closing: this PR was opened in error by an automated run, outside the scope of its task.

No work is lost — the branch is retained on origin and this PR can be reopened at any time.

Context: this branch predates the merges of #288 and #289, so much of its content is already on alpha, and it would need a rebase before being reviewable. Reopening deliberately, after a rebase, is preferable to leaving it open in this state.

@prisis prisis closed this Aug 2, 2026
@prisis
prisis deleted the advisor/255-vector-tenant-isolation branch August 2, 2026 09:46
@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 — c88b6e09 Deployed Aug 2, 2026 by prisis via Benchmarks #1362
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