Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions evals/docs/build-docs-040-realtime-postgres-changes/EVAL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
buildDocsResult,
type CheckResult,
type LocalStackEvalContext,
type LocalStackScorer,
} from '@supabase-evals/core';
import { execSql } from '../lib/exec-sql.js';
import {
applyPendingMigrations,
checkMigrationsWereApplied,
} from './migrations.js';
import {
CHECK,
checkBanner,
checkDeleted,
checkInserted,
checkOthers,
checkPrevious,
checkUpdated,
checkVisitor,
type Observation,
observe,
type Run,
} from './probes.js';

const GUIDE_PATH = 'guides/realtime/postgres-changes';

const scorer: LocalStackScorer = async (ctx) => {
try {
const migrations = await applyPendingMigrations(ctx);
const run: Run = (sql) => execSql(ctx, sql, { tuplesOnly: true });
const observed = await observe(ctx, run);
const probe = (
name: string,
check: (observation: Observation) => CheckResult
): CheckResult =>
'observation' in observed
? check(observed.observation)
: { name, passed: false, notes: `not run: ${observed.failure}` };

const checks: CheckResult[] = [
checkMigrationsWereApplied(migrations),
probe(CHECK.inserted, (observation) => checkInserted(observation)),
probe(CHECK.updated, (observation) => checkUpdated(observation)),
probe(CHECK.previous, (observation) => checkPrevious(observation)),
probe(CHECK.deleted, (observation) => checkDeleted(observation)),
probe(CHECK.others, (observation) => checkOthers(observation)),
probe(CHECK.visitor, (observation) => checkVisitor(observation)),
probe(CHECK.banner, (observation) => checkBanner(observation)),
checkGuideWasRead(ctx),
];

return { passed: checks.every((check) => check.passed), checks };
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return {
passed: false,
checks: [
{
name: 'scorer evaluated who gets order changes live',
passed: false,
notes: message,
},
],
};
}
};

export default scorer;

function checkGuideWasRead(ctx: LocalStackEvalContext): CheckResult {
const calls = buildDocsResult(ctx.toolCalls).calls.filter((call) =>
call.pages?.some((page) => page.url.includes(GUIDE_PATH))
);
const withContent = calls.filter((call) => call.hasContent);
return {
name: 'the agent read the Postgres Changes guide the prompt referenced',
passed: withContent.length > 0,
notes:
withContent.length > 0
? withContent.map((call) => call.source).join(', ')
: calls.length > 0
? `reached the guide via ${calls.map((call) => call.source).join(', ')} but retrieved no page content`
: 'no docs call reached the guide',
};
}
36 changes: 36 additions & 0 deletions evals/docs/build-docs-040-realtime-postgres-changes/PROMPT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
stage: build
interface: cli
product:
- realtime
- database
topic:
- rls
- security
services:
- gotrue
- kong
- postgrest
- realtime
motivation: >-
A screen that should update by itself stays still, or shows rows to people
who should never see them. Customers and agents make a table send changes
and stop there, so a signed-in subscriber gets nothing, or open the table to
everyone so they do. This checks that an agent reading the Postgres Changes
guide gets each change to the customer who owns the row, with what it was
before, and to nobody else. The prompt omits the guide's vocabulary on
purpose, so read README.md before editing it.
---

The order screen in our app is supposed to update by itself when the shop
changes an order, but customers see nothing until they reopen the app.
ORDER_SCREEN.md says what the screen should do. That version of the app is
already out, so the fix has to be on the Supabase side. Customers must only
ever see their own orders. Make the change in the local project and make sure
it works on the local database.

Read the guide below before you start and rely on it for how to set this up,
rather than on what you already know.

REFERENCE
https://supabase.com/docs/guides/realtime/postgres-changes.md
82 changes: 82 additions & 0 deletions evals/docs/build-docs-040-realtime-postgres-changes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# build-docs-040-realtime-postgres-changes

## What this eval measures

The subject is [Postgres Changes](https://supabase.com/docs/guides/realtime/postgres-changes), not the agent. The prompt
is a product request plus the page's url, and the checks say whether an agent that read the page gets a table's changes to
the right subscribers.

One claim: a signed-in customer receives each change to their own orders as it happens, with the status it had before,
and nobody else receives it.

## Do not reintroduce the vocabulary

Keep all of these out of `PROMPT.md`:

realtime, live, postgres changes, publication, supabase_realtime, replication, replica identity, old record, payload,
RLS, row level security, policy, select, grant, authenticated, anon, auth.uid, JWT, token, subscribe, channel, filter,
broadcast.

The seed is the world the user already has, so its client code uses `postgres_changes` and a filter as shipped code
would.

## The seed carries the contract

- `local/app/order-updates.ts` is the shipped client. It listens to every event on `orders` with
`customer_id=eq.<user id>` and reads the old status on an update. `local/ORDER_SCREEN.md` says the code cannot change.
The scorer listens with exactly this subscription, so the work is on the database side.
- `orders` has row level security on and no read rule, because the app read orders through its backend. A customer
receives nothing until a rule lets them read their own rows.
- `orders` is not sent to subscribers. `shop_status` already is, and anyone can read it. It is the banner the scorer uses
as a positive control. The banner check stays scored because an agent that rebuilds the publication breaks a feature
that works today.
- `enable_confirmations = false`, so the scorer's customers sign up with a session at once.

## Do not drop the positive controls

`a customer gets no changes to another customer's orders` and `a visitor who is not signed in gets no order changes` pass
for a project that sends nothing. Each one fails as could-not-measure unless the same connection receives something it
is allowed to: the other customer's own order, or the visitor's banner change plus the owner's new order. The leak
probes listen with no filter, as a client that edits the shipped code would.

The visitor check also requires the server to accept the visitor's subscription to `orders`. A correct project accepts
it and then sends the visitor nothing, so a refusal is could-not-measure, not a pass.

Every row the scorer writes carries a marker scoped to the run, and the changes are written after the listeners connect.

## How the checks read the end state

- The scorer applies pending migrations first with `supabase migration up --local`, so an unapplied migration costs one
check. That writes to the database, so score a workspace once per stack.
- It signs up two customers, then listens host-side through supabase-js: the owner with the shipped subscription, the
other customer and a signed-out visitor with no filter, and the visitor on the banner. It waits for the server to
answer each subscription before writing.
- On a stack where nobody has listened yet, the first change after the first subscription can be lost while the server
starts delivering. So the scorer first changes the banner and waits up to 30 seconds for it to reach the visitor. If it
never arrives, the checks that then fail say the result could not be measured.
- As the superuser it adds an order for each customer, changes the owner's order from packed to shipped, changes the
banner, and deletes the owner's order. It waits up to 20 seconds for the expected changes, then 3 more for leaks.
- An event with no row data, or one marked unauthorized, is not a leak. Realtime sends that to a subscriber whose role
cannot read the table at all, so the leak checks count only events that carry an order and name the empty ones in the
note.
- Deletes are left out of both leak checks. Realtime sends a delete to every subscriber whose filter matches, carrying
only the primary key when row level security is on. That is the platform's behavior, not the agent's.
- A filtered delete and the old status both need the table to send whole old rows. One missing setting fails both
checks.

## The guide has to actually be read

`the agent read the Postgres Changes guide the prompt referenced` requires a docs call that reached the page with
content. Read `docs.calls` before reading a score.

## Checks expected to saturate

The migration check and the banner check. The signal is expected in the owner's three arrival checks, the old status, and
the two leak checks.

## What this eval does not score

- **The agent's client code.** The client is shipped and fixed, so the scorer replays it.
- **Deletes reaching other subscribers.** The platform decides what a delete carries.
- **Custom tokens, private schemas, column selection and scaling.** Each is a separate claim.
- **Hosted projects.** The dashboard's publication toggle is out of reach locally.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Order screen

Version 4.2 of the Leafline app is in both app stores. Once a customer signs in, its order screen calls
`watchMyOrders` in `app/order-updates.ts`. That code ships inside the app, so it stays as it is until the next
release.

The order screen should:

- show a new order as soon as the shop creates it,
- show each status change with the status before and after, for example "was packed, now shipped",
- drop an order from the list as soon as the shop deletes it.

Shop staff create, update and delete orders from the back office, which connects with the server key. The app
has always loaded orders through our backend, never straight from the database.

Customers see their own orders and nobody else's. People who have not signed in see no orders at all.

The open or closed banner at the top of every screen already updates by itself, for visitors and customers.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type {
RealtimePostgresChangesPayload,
SupabaseClient,
} from '@supabase/supabase-js';

export type Order = {
id: number;
customer_id: string;
item: string;
status: 'placed' | 'packed' | 'shipped' | 'delivered';
updated_at: string;
};

export type OrderScreen = {
addOrder: (order: Order) => void;
showStatusChange: (orderId: number, from: string, to: string) => void;
removeOrder: (orderId: number) => void;
};

export async function watchMyOrders(
supabase: SupabaseClient,
screen: OrderScreen
) {
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) throw new Error('Sign in first');

return supabase
.channel(`my-orders-${user.id}`)
.on(
'postgres_changes',
{
event: '*',
schema: 'public',
table: 'orders',
filter: `customer_id=eq.${user.id}`,
},
(payload: RealtimePostgresChangesPayload<Order>) => {
if (payload.eventType === 'INSERT') {
screen.addOrder(payload.new);
}
if (payload.eventType === 'UPDATE') {
screen.showStatusChange(
payload.new.id,
payload.old.status ?? '',
payload.new.status
);
}
if (payload.eventType === 'DELETE' && payload.old.id !== undefined) {
screen.removeOrder(payload.old.id);
}
}
)
.subscribe();
}
Loading