Repository navigation
Conversation
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>
❌ Deploy Preview for lunorash failed.
|
WalkthroughVector 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. ChangesVector namespace isolation
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
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
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
Thank you for following the naming conventions! 🙏 |
|
Thank you for confirming the Contributor License Agreement! 🙏 |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/bindings/src/vectors/context.ts (1)
175-177: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider throwing a typed error instead of bare
Error.The sibling facade in
packages/bindings/src/vectors/create-vectors.tsthrowsTypeError,RangeError, orLunoraError("INTERNAL", ...)for facade-level failures. This throw uses a bareError, 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 valueExtract the sharded index names once and reuse them.
The predicate
shardedTableNames.has(index.table)is now applied twice overschema.vectorIndexes: once at line 4151 to computehasShardedVectors, 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:
vectorNamespaceFieldandvectorsContextNamespaceOptionshare the samehasShardedVectorsgate, sovectorShardKeyis always declared before use, andROOT_SHARD_NAMEis 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
⛔ Files ignored due to path filters (3)
packages/ai/__tests__/rag.test.tsis excluded by!**/__tests__/**,!**/*.test.tsand included bypackages/**packages/bindings/__tests__/vectors/ctx.test.tsis excluded by!**/__tests__/**,!**/*.test.tsand included bypackages/**packages/codegen/__tests__/run-codegen.test.tsis excluded by!**/__tests__/**,!**/*.test.tsand included bypackages/**
📒 Files selected for processing (4)
packages/bindings/docs/index.mdxpackages/bindings/src/vectors/context.tspackages/codegen/src/emit.tspackages/platform/src/capabilities.ts
|
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 |
|
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. |
Branched before #288/#289 landed, so it needs a rebase onto current alpha before merge.
Commits
Files this branch still changes relative to alpha
Summary by CodeRabbit
New Features
Bug Fixes
Documentation