Skip to content

feat: enforce free-tier node ceiling and MCP gate#265

Merged
cameronapak merged 1 commit into
mainfrom
claude/wayfinder-151-next-5b0145
Jul 15, 2026
Merged

feat: enforce free-tier node ceiling and MCP gate#265
cameronapak merged 1 commit into
mainfrom
claude/wayfinder-151-next-5b0145

Conversation

@cameronapak

@cameronapak cameronapak commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Summary

Free outlines are now capped at 2,000 live nodes and MCP agent access is a paid capability, so the free/paid line is enforced server-side rather than by convention.

Fixes #170

Changes

  1. A free user who would push past 2,000 nodes gets a "limit reached" upgrade toast and the insert rolls back; edits, moves, deletes, and net-reducing batches always apply, and an over-cap (downgraded) outline is never locked.
  2. A free MCP token can still handshake and list tools, but every tools/call is refused with an in-protocol upgrade error — no read or write reaches the outline.
  3. The DO gates writes at commit time (applyBatchGated/upsertNodesGated): it counts net node growth by distinct id inside the write transaction and refuses only batches that grow the outline past the cap it's told.
  4. The refusal decision is a pure, exhaustively unit-tested batchExceedsNodeLimit; the Worker resolves the caller's plan once per node-creating write (skipping pure-delete batches) and maps a refusal to 403 {error:"node_limit",limit}.
  5. An operator comps a user by inserting an active subscription row — getPlan already treats it as paid, so no Stripe round-trip and no new override path.
  6. Review hardening: the plan lookup on the node path uses the billing user.id (not the DO-routing id, which collapses the owner to default), and a pure countNetGrowth closes a delete+reinsert count bypass.

Start here: change 6 — the owner billing-id fix and the delete+reinsert bypass, both found by adversarial review; the growth count now takes each id's last op.

Flow

flowchart LR
  A[structural insert] --> B[Worker: getPlan]
  B --> C[DO applyBatchGated]
  C -->|within cap| D[commit + echo]
  C -->|would exceed| E[403 node_limit]
  E --> F[toast + rollback]
Loading

Breaking / Migration

None. Additive: no schema change, no new Node field. Existing alpha accounts (no subscription row) are free and unaffected until they cross the cap.

Test plan

  • bun run typecheck / typecheck:worker / typecheck:test — green
  • bun test — passing, incl. batchExceedsNodeLimit boundary cases (under/at/over cap, downgraded outline never locked, net-reducing batch allowed), countNetGrowth (the delete+reinsert bypass case), and the MCP free-token gate (tools/call refused, discovery open)
  • bun run lint, bun run fmt:check — clean
  • Adversarial review: two Opus reviewers (correctness + security/paywall). No bypass; gate fails closed. Two findings fixed (owner billing-id on the node path; delete+reinsert cap-count bypass). Every node-creating path (batch, legacy upsert, PATCH, kv-as-nodes, ungated MCP write) traced and closed.
  • Needs manual check: the live cap toast + optimistic rollback in a signed-in free session, and an MCP client hitting the refusal — needs a running Worker + D1 + a free account; e2e can't reach it (seedOutline mocks the Worker, so the real getPlan/DO cap never runs).

Free outlines are capped at 2,000 live nodes and MCP agent access is a
paid capability (#170).

- DO: applyBatchGated/upsertNodesGated count net growth (distinct new ids)
  inside the write transaction and refuse only batches that would grow the
  outline past the cap. Edits, moves, deletes, and net-reducing batches
  always apply; an over-cap (downgraded) outline is never locked. Decision
  is the pure, unit-tested batchExceedsNodeLimit.
- Worker: resolves the caller's plan (getPlan), skips the query for
  pure-delete batches, maps a refusal to 403 {error:"node_limit",limit}.
- MCP: a free token still handshakes + lists tools, but every tools/call
  is refused with an in-protocol -32001 upgrade error.
- Client: the 403 becomes a distinct NodesLimitError; the structural funnel
  shows one upgrade toast then rolls the optimistic insert back.

Operator override is free: getPlan treats a hand-inserted active
subscription row as paid.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Warning

Review limit reached

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

Next review available in: 37 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: c3abf7fd-2ef3-4a83-86e2-26b42644ae92

📥 Commits

Reviewing files that changed from the base of the PR and between 9d6c1aa and ec51859.

📒 Files selected for processing (9)
  • .changeset/free-tier-entitlements.md
  • src/data/nodes-client-effect.ts
  • src/data/structural.ts
  • worker/index.ts
  • worker/mcp.test.ts
  • worker/mcp.ts
  • worker/outline-do.ts
  • worker/plan.test.ts
  • worker/plan.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/wayfinder-151-next-5b0145

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.

@cameronapak cameronapak merged commit bc79155 into main Jul 15, 2026
2 checks passed
cameronapak added a commit that referenced this pull request Jul 15, 2026
Two issues surfaced by adversarial review of the entitlement change:

- Owner account was misclassified as free on the node-write path: getPlan
  received the DO-resolved id (owner collapses to 'default'), missing the
  owner's comp/subscription row keyed on the real user.id. Pass the billing
  id (session.user.id) to the plan lookup while the DO stub keeps routing on
  the resolved id — mirroring the MCP branch.
- Node-cap growth count could be bypassed by a batch that deletes and
  re-inserts the same id (double-subtracted the survivor). Replaced the
  independent insert/delete sets with a pure, unit-tested countNetGrowth
  (last-op-per-distinct-id), so an id that ends the batch present is one
  insert and a delete+reinsert nets zero.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
cameronapak added a commit that referenced this pull request Jul 15, 2026
Two issues surfaced by adversarial review of the entitlement change:

- Owner account was misclassified as free on the node-write path: getPlan
  received the DO-resolved id (owner collapses to 'default'), missing the
  owner's comp/subscription row keyed on the real user.id. Pass the billing
  id (session.user.id) to the plan lookup while the DO stub keeps routing on
  the resolved id — mirroring the MCP branch.
- Node-cap growth count could be bypassed by a batch that deletes and
  re-inserts the same id (double-subtracted the survivor). Replaced the
  independent insert/delete sets with a pure, unit-tested countNetGrowth
  (last-op-per-distinct-id), so an id that ends the batch present is one
  insert and a delete+reinsert nets zero.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Entitlements: enforce the free-tier node ceiling + MCP gate

1 participant