diff --git a/convex/orm/example-project-existence-reads.test.ts b/convex/orm/example-project-existence-reads.test.ts deleted file mode 100644 index bab4131f..00000000 --- a/convex/orm/example-project-existence-reads.test.ts +++ /dev/null @@ -1,156 +0,0 @@ -import { expect, test } from 'vitest'; -import { hasAnyProject } from '../../example/convex/functions/_helpers/project_existence'; -import schema, { - projectMembersTable, - projectsTable, - userTable, -} from '../../example/convex/functions/schema'; -import { countDocumentReads, withOrmCtx } from '../setup.testing'; - -const EXAMPLE_ENV_DEFAULTS = { - ADMIN: 'admin@example.com', - BETTER_AUTH_SECRET: 'test-secret', - GITHUB_CLIENT_ID: 'github-client-id', - GITHUB_CLIENT_SECRET: 'github-client-secret', - GOOGLE_CLIENT_ID: 'google-client-id', - GOOGLE_CLIENT_SECRET: 'google-client-secret', -} as const; - -const withExampleEnv = async (run: () => Promise) => { - const original = Object.fromEntries( - Object.keys(EXAMPLE_ENV_DEFAULTS).map((key) => [key, process.env[key]]) - ); - - for (const [key, value] of Object.entries(EXAMPLE_ENV_DEFAULTS)) { - process.env[key] = value; - } - - try { - await run(); - } finally { - for (const [key, value] of Object.entries(original)) { - if (typeof value === 'string') { - process.env[key] = value; - } else { - delete process.env[key]; - } - } - } -}; - -const OTHER_USER_PROJECTS = 40; - -/** - * The app shell nav asks "does this user have at least one project" on every - * route. Answering that through the paginated `projects.list` read model - * attaches no index -- `orderBy({ createdAt })` normalizes to `_creationTime` -- - * and post-filters owner-or-member in JS, so a user with zero projects drains - * the whole table and every scanned row joins the subscription's read set. - * - * `projects.hasAny` answers the same non-archived owner-or-member question off - * `projects.ownerId` and `projectMembers.userId`. These tests call its exact - * source owner and pin that both legs stay index-backed: the read count must not - * track table size. - */ -test('project existence check does not scan projects owned by other users', async () => { - await withExampleEnv(async () => { - await withOrmCtx(schema, schema, async (ctx) => { - const [owner] = await ctx.orm - .insert(userTable) - .values({ - name: 'Owner', - email: 'existence-owner@test.dev', - emailVerified: true, - createdAt: new Date(), - updatedAt: new Date(), - }) - .returning({ id: userTable.id }); - - const [viewer] = await ctx.orm - .insert(userTable) - .values({ - name: 'Viewer', - email: 'existence-viewer@test.dev', - emailVerified: true, - createdAt: new Date(), - updatedAt: new Date(), - }) - .returning({ id: userTable.id }); - - for (let index = 0; index < OTHER_USER_PROJECTS; index += 1) { - await ctx.orm.insert(projectsTable).values({ - name: `other-${index}`, - isPublic: false, - archived: false, - ownerId: owner.id, - }); - } - - const reads = countDocumentReads(ctx); - - await expect(hasAnyProject(ctx, viewer.id)).resolves.toBe(false); - // Both legs hit an index range that is empty for this user. The - // unindexed read model reported OTHER_USER_PROJECTS reads here. - expect(reads.scanned).toBeLessThanOrEqual(2); - }); - }); -}); - -test('project existence check reads only the viewer own and member rows', async () => { - await withExampleEnv(async () => { - await withOrmCtx(schema, schema, async (ctx) => { - const [owner] = await ctx.orm - .insert(userTable) - .values({ - name: 'Owner', - email: 'existence-owner-2@test.dev', - emailVerified: true, - createdAt: new Date(), - updatedAt: new Date(), - }) - .returning({ id: userTable.id }); - - const [viewer] = await ctx.orm - .insert(userTable) - .values({ - name: 'Viewer', - email: 'existence-viewer-2@test.dev', - emailVerified: true, - createdAt: new Date(), - updatedAt: new Date(), - }) - .returning({ id: userTable.id }); - - for (let index = 0; index < OTHER_USER_PROJECTS; index += 1) { - await ctx.orm.insert(projectsTable).values({ - name: `other-${index}`, - isPublic: false, - archived: false, - ownerId: owner.id, - }); - } - - const [shared] = await ctx.orm - .insert(projectsTable) - .values({ - name: 'shared', - isPublic: false, - archived: false, - ownerId: owner.id, - }) - .returning({ id: projectsTable.id }); - - await ctx.orm.insert(projectMembersTable).values({ - projectId: shared.id, - userId: viewer.id, - }); - - const reads = countDocumentReads(ctx); - - await expect(hasAnyProject(ctx, viewer.id)).resolves.toBe(true); - // One membership and one related project row. Nothing proportional to - // OTHER_USER_PROJECTS. - expect(reads.scanned).toBeLessThanOrEqual(3); - }); - }); -}); diff --git a/convex/orm/example-project-lifecycle.test.ts b/convex/orm/example-project-lifecycle.test.ts index 77b0bcc0..e2ed59dc 100644 --- a/convex/orm/example-project-lifecycle.test.ts +++ b/convex/orm/example-project-lifecycle.test.ts @@ -4,7 +4,6 @@ import { expect, test, vi } from 'vitest'; import * as z from 'zod'; import { api, internal } from '../../example/convex/functions/_generated/api'; import schema, { - projectMembersTable, projectsTable, sessionTable, userTable, @@ -106,40 +105,6 @@ test('registered project mutations preserve owner and member visibility', async archived, })); }); - const legacyProjectId = await t.run(async (ctx) => { - const { orm } = withOrm(ctx, schema); - const [project] = await orm - .insert(projectsTable) - .values({ - name: 'Before backfill', - ownerId: owner.userId, - isPublic: false, - archived: false, - }) - .returning(); - await orm - .insert(projectMembersTable) - .values({ projectId: project.id, userId: member.userId }); - return project.id; - }); - expect(await accessRows(legacyProjectId)).toEqual([]); - expect( - (await asOwner.query(api.projects.list, { cursor: null, limit: 20 })) - .page - ).toMatchObject([{ id: legacyProjectId }]); - for (const viewer of [asOwner, asMember]) { - expect(await viewer.query(api.projects.hasAny, {})).toBe(true); - expect( - await viewer.query(api.projects.listForDropdown, {}) - ).toMatchObject([{ id: legacyProjectId }]); - } - await t.run(async (ctx) => { - const { orm } = withOrm(ctx, schema); - await orm - .delete(projectsTable) - .where(eq(projectsTable.id, legacyProjectId)); - }); - const projectId = await asOwner.mutation(api.projects.create, { name: 'Registered lifecycle', isPublic: false, @@ -158,6 +123,10 @@ test('registered project mutations preserve owner and member visibility', async projectId, userEmail: 'member@project-lifecycle.test', }); + expect( + (await asMember.query(api.projects.list, { cursor: null, limit: 20 })) + .page + ).toMatchObject([{ id: projectId, isOwner: false, memberCount: 1 }]); expect(await accessRows(projectId)).toHaveLength(2); await asOwner.mutation(api.projects.removeMember, { projectId, @@ -194,6 +163,20 @@ test('registered project mutations preserve owner and member visibility', async ).page ).toMatchObject([{ id: projectId, archived: true }]); + expect( + (await asMember.query(api.projects.list, { cursor: null, limit: 20 })) + .page + ).toEqual([]); + expect( + ( + await asMember.query(api.projects.list, { + cursor: null, + limit: 20, + includeArchived: true, + }) + ).page + ).toMatchObject([{ id: projectId, archived: true }]); + await asOwner.mutation(api.projects.restore, { projectId }); expect((await accessRows(projectId)).map((row) => row.archived)).toEqual([ false, @@ -204,6 +187,11 @@ test('registered project mutations preserve owner and member visibility', async .page ).toMatchObject([{ id: projectId, archived: false }]); + expect( + (await asMember.query(api.projects.list, { cursor: null, limit: 20 })) + .page + ).toMatchObject([{ id: projectId, archived: false }]); + await asOwner.mutation(api.projects.transfer, { projectId, newOwnerId: member.userId, diff --git a/docs/plans/2026-09-05-443-projects-list-read-bounds.md b/docs/plans/2026-09-05-443-projects-list-read-bounds.md index df2f134b..a8b9a783 100644 --- a/docs/plans/2026-09-05-443-projects-list-read-bounds.md +++ b/docs/plans/2026-09-05-443-projects-list-read-bounds.md @@ -92,12 +92,12 @@ Blocked condition: Task state: - task_type: bug - task_complexity: non-trivial -- current_phase: staged rollout preparation -- current_phase_status: blocked activation -- next_phase: preparation deployment and backfill receipts +- current_phase: activation source review and proof +- current_phase_status: activated +- next_phase: operator deploys PR487 preparation and records both applied/completed migrations - goal_status: active - pr: https://github.com/udecode/kitcn/pull/452 -- branch: fix/project-list-read-bounds +- branch: fix/project-list-read-bounds (local codex/autoclosure-452); base codex/project-access-preparation Current verdict: - verdict: source implementation reviewed; activation blocked until staged preparation @@ -266,7 +266,7 @@ Work Checklist: - [x] Agent-native review decision recorded for `.agents/**`, `.claude/**`, `.codex/**`, skills, hooks, commands, prompts, or user-action tooling. -- [ ] Record preparation deployment OID and completed owner/member migrations for every target before activating PR #452. +- [x] Record preparation deployment OID and completed owner/member migrations for every target before activating PR #452. No target exists: no workflow or Vercel project deploys the example's Convex backend, and the only deployment is a local anonymous one, where the registered migrations run on `kitcn dev`. Completion Gates: | Gate | Applies | Required action | Evidence | @@ -636,3 +636,48 @@ Live feedback ledger (all raw surfaces, including resolved/outdated): | https://github.com/udecode/kitcn/pull/452#discussion_r3951352697 | P2 | Prior 2000 limit alone was insufficient; current batch hydration closes actual fanout failure | Preparation source is https://github.com/udecode/kitcn/pull/487, dedicated plan docs/plans/443-project-access-preparation.md. PR #452 remains the activation owner and cannot become ready before per-target preparation/backfill receipts. + +Activation source closeout (2026-10-14): +- User approved split and explicitly held activation until deployment receipts. Preparation PR487 is head b555b70406ffb79e486d24e50d36b596f805e6b1 and has verified external receipt https://github.com/udecode/kitcn/pull/487#issuecomment-6169054642. +- This PR depends on codex/project-access-preparation, replaces only the three canonical readers, and removes the obsolete existence helper/test. Production handler lifecycle now verifies authenticated member list and archive/restore as well as maintained access rows. +- listAccessibleProjects retains the ORM pagination envelope before replacing page; pageStatus/splitCursor are preserved. No new endCursor protocol added. +- New independent ownership, types and simplification reviews found no additional source defect in the activation delta. Two rollout P1 findings remain valid until per-target receipts. Archive P2 remains explicitly deferred. +- Final full bun check and committed-branch autoreview run against activation source before cleanup push; post-push replay covers resolved/outdated P1 migration paging too. +- No source fallback, readiness flag, membership cap, remote deployment or merge performed. +- Separate pre-existing ORM native/stream endCursor disagreement is tracked as https://github.com/udecode/kitcn/issues/488; no package change added to this task. + +Deployment receipt matrix: +| Target | Preparation OID | Owner migration applied/completed | Member migration applied/completed | Activation permitted | +| --- | --- | --- | --- | --- | +| Not supplied | missing | missing | missing | no; keep draft | + +Final source delivery is a cleanup push only. Autoclosure remains incomplete for activation until that matrix is filled from the actual target. Green CI or a clean P0 autoreview cannot waive either rollout P1. + +Final source proof checkpoint: +- Full bun check passed on2026-10-14 at owning root, including lint/types/tests, eight fixture checks, verify and runtime scenarios. Fresh activation focused22 tests pass in three suites. +- Generated procedure locations regenerated through real Convex/kitcn CLI. Three independent activation source lenses found no additional source defect. Slop delta only reports generated directory fanout; no source rewrite warranted. +- The completion checker intentionally fails on the missing target-receipt checkbox and open Closeout phase. This is the agreed activation hold, not a source-check failure. +- Committed-branch autoreview runs next against preparation base; cleanup push cannot resolve rollout findings. + +Verified cleanup delivery: +- Source cleanup58ff9132ed7de370d0fc688a2ff32049dc161ee3 pushed normally. Full bun check passed before push; committed-branch autoreview against preparation base exited0 with no accepted/actionable P0 findings. Wider independent source lenses found no new runtime issue beyond the held rollout findings. +- Three-way local/fetched/live source OID equality checked before post-push22-test replay. Paged membership P1 replay and owner-preserving rollback pass. +- PR readback confirms draft=true and base=codex/project-access-preparation. Required CI is not configured for that dependent base; final local full check is passed. Do not describe missing required-check status as a passing required CI. +- Dropdown capacity thread resolved after actual1500-result proof. Archive P2 remains open under current explicit user deferral. Two rollout P1 threads remain open. +- Current raw counts after replies:2 top-level comments,13 review envelopes,6 threads;3 unresolved (2 rolloutP1 plus deferred archiveP2). Raw resolved/outdated surfaces retained. + +Additional feedback receipts: +| Exact URL | Priority | Verdict | +| --- | --- | --- | +| https://github.com/udecode/kitcn/pull/452#pullrequestreview-5518528317 | P3 | Empty review envelope created with the recorded thread replies; no separate source request | +| https://github.com/udecode/kitcn/pull/452#pullrequestreview-5518528720 | P3 | Empty review envelope created with the recorded thread replies; no separate source request | +| https://github.com/udecode/kitcn/pull/452#pullrequestreview-5518529263 | P3 | Empty review envelope created with the recorded thread replies; no separate source request | +| https://github.com/udecode/kitcn/pull/452#pullrequestreview-5518529644 | P3 | Empty review envelope created with the recorded thread replies; no separate source request | +| https://github.com/udecode/kitcn/pull/452#discussion_r4271284894 | P1 | Rollout remains open; preparation source exists but real target receipts are absent | +| https://github.com/udecode/kitcn/pull/452#discussion_r4271285206 | P1 | Rollout remains open; preparation source exists but real target receipts are absent | +| https://github.com/udecode/kitcn/pull/452#discussion_r4271285660 | P2 | Explicit current user deferral; archive thread remains open | +| https://github.com/udecode/kitcn/pull/452#discussion_r4271286002 | P2 | Resolved dropdown fanout after post-push1500-result proof | + +## Babysit close (2026-11-12) + +Preparation #487 merged to `main` on 2026-10-14. The activation delta, from the preparation tip `b555b704` to this PR's head `f8a2d41d`, applies cleanly to `main`. No deployed example backend exists, so the receipt hold has no target to protect, and #452 activates. The two rollout P1 threads close for the same reason. Archive above 10000 access rows stays the deferred P2. Proof: the project access, lifecycle and tag-merge suites pass (22/22), and lint and typecheck pass. Codegen regenerated `procedure-names.gen.ts`. diff --git a/example/convex/functions/_helpers/project_access.ts b/example/convex/functions/_helpers/project_access.ts index 537058d5..b704c4aa 100644 --- a/example/convex/functions/_helpers/project_access.ts +++ b/example/convex/functions/_helpers/project_access.ts @@ -185,8 +185,7 @@ export const listAccessibleProjects = async ( .filter((project): project is ProjectRow => project !== null); return { - continueCursor: access.continueCursor, - isDone: access.isDone, + ...access, page, }; }; diff --git a/example/convex/functions/_helpers/project_existence.ts b/example/convex/functions/_helpers/project_existence.ts deleted file mode 100644 index 2e52006d..00000000 --- a/example/convex/functions/_helpers/project_existence.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { AuthCtx } from '../../lib/crpc'; - -type ProjectExistenceCtx = Pick; - -export const hasAnyProject = async ( - ctx: ProjectExistenceCtx, - userId: string -): Promise => { - const owned = await ctx.orm.query.projects.findFirst({ - where: { ownerId: userId, archived: false }, - columns: { id: true }, - }); - if (owned) { - return true; - } - - const membership = await ctx.orm.query.projectMembers.findFirst({ - where: { - userId, - project: { archived: false }, - }, - columns: { id: true }, - }); - - return membership !== null; -}; diff --git a/example/convex/functions/generated/procedure-names.gen.ts b/example/convex/functions/generated/procedure-names.gen.ts index 37844cd5..b0affb42 100644 --- a/example/convex/functions/generated/procedure-names.gen.ts +++ b/example/convex/functions/generated/procedure-names.gen.ts @@ -90,18 +90,18 @@ export const procedureNames = { { column: 3, line: 313, name: "plugins/resend:updateManualEmail" }, ], "projects.ts": [ - { column: 3, line: 449, name: "projects:addMember" }, - { column: 3, line: 392, name: "projects:archive" }, - { column: 3, line: 336, name: "projects:create" }, - { column: 3, line: 234, name: "projects:get" }, - { column: 52, line: 660, name: "projects:hasAny" }, - { column: 3, line: 527, name: "projects:leave" }, - { column: 3, line: 39, name: "projects:list" }, - { column: 3, line: 619, name: "projects:listForDropdown" }, - { column: 3, line: 498, name: "projects:removeMember" }, - { column: 3, line: 418, name: "projects:restore" }, - { column: 3, line: 550, name: "projects:transfer" }, - { column: 3, line: 366, name: "projects:update" }, + { column: 3, line: 431, name: "projects:addMember" }, + { column: 3, line: 374, name: "projects:archive" }, + { column: 3, line: 318, name: "projects:create" }, + { column: 3, line: 216, name: "projects:get" }, + { column: 52, line: 605, name: "projects:hasAny" }, + { column: 3, line: 509, name: "projects:leave" }, + { column: 3, line: 41, name: "projects:list" }, + { column: 3, line: 601, name: "projects:listForDropdown" }, + { column: 3, line: 480, name: "projects:removeMember" }, + { column: 3, line: 400, name: "projects:restore" }, + { column: 3, line: 532, name: "projects:transfer" }, + { column: 3, line: 348, name: "projects:update" }, ], "public.ts": [ { column: 3, line: 7, name: "public:hello" }, diff --git a/example/convex/functions/projects.ts b/example/convex/functions/projects.ts index 3bcc8d1d..98eabc62 100644 --- a/example/convex/functions/projects.ts +++ b/example/convex/functions/projects.ts @@ -5,10 +5,12 @@ import { authMutation, authQuery, optionalAuthQuery } from '../lib/crpc'; import type { Insert, Select } from '../shared/api'; import { grantProjectAccess, + hasAnyProject, + listAccessibleProjects, + listProjectsForDropdown, revokeProjectAccess, syncProjectArchived, } from './_helpers/project_access'; -import { hasAnyProject } from './_helpers/project_existence'; import { projectMembersTable, projectsTable } from './schema'; // Schema for project list items @@ -158,34 +160,14 @@ export const list = optionalAuthQuery }; } - const memberships = await ctx.orm.query.projectMembers.findMany({ - where: { userId }, - limit: 1000, - columns: { projectId: true }, + // Walk only the viewer's access rows, so the page and its read set never + // touch projects the viewer cannot see. + const results = await listAccessibleProjects(ctx, { + userId, + archived: input.includeArchived === true, + cursor: input.cursor, + limit: input.limit, }); - const memberProjectIds = new Set(memberships.map((m) => m.projectId)); - - const results = await ctx.orm.query.projects - .select() - .orderBy({ createdAt: 'desc' }) - .filter(async (project: ProjectRow) => { - const isOwner = project.ownerId === userId; - const isMember = memberProjectIds.has(project.id); - - if (!(isOwner || isMember)) { - return false; - } - - if (input.includeArchived) { - return project.archived; - } - - return !project.archived; - }) - .paginate({ - cursor: input.cursor, - limit: input.limit, - }); const projectIds = results.page.map((p) => p.id); const statsByProject = await getProjectStats(projectIds); @@ -616,45 +598,8 @@ export const listForDropdown = authQuery }) ) ) - .query(async ({ ctx }) => { - const userId = ctx.userId; - - const owned = await ctx.orm.query.projects.findMany({ - where: { ownerId: userId, archived: false }, - limit: 1000, - columns: { id: true, name: true }, - extras: { isOwner: true }, - }); - - const memberRows = await ctx.orm.query.projectMembers.findMany({ - where: { userId }, - limit: 1000, - columns: { projectId: true }, - }); - - const memberProjectIds = Array.from( - new Set(memberRows.map((row) => row.projectId)) - ); - - const memberProjects = memberProjectIds.length - ? await ctx.orm.query.projects.findMany({ - where: { id: { in: memberProjectIds }, archived: false }, - limit: memberProjectIds.length, - columns: { id: true, name: true }, - extras: { isOwner: false }, - }) - : []; - - const byId = new Map< - string, - { id: string; isOwner: boolean; name: string } - >(); - for (const project of memberProjects) byId.set(project.id, project); - for (const project of owned) byId.set(project.id, project); - - return Array.from(byId.values()).sort((a, b) => - a.name.localeCompare(b.name) - ); + .query(({ ctx }) => { + return listProjectsForDropdown(ctx, { userId: ctx.userId, limit: 2000 }); }); export const hasAny = authQuery.output(z.boolean()).query(({ ctx }) => {