feat(plugins): extend the output allowlist to child rows and whole-row read models

The output contract cut only the top level of an entity result. A derived
key's value went out untouched, so the children that are whole rows still
carried every column: a day's notes_items are full day_notes entities and
a reservation's endpoints are SELECT e.* on reservation_endpoints. About
fifty methods were also left as readModel and passed through whole, many
of them backed by SELECT * reads (day_accommodations, day_notes, todos,
tags, packing bags, journal entries, collab, collections, vacay plans).
A new column on any of those tables reached every plugin by default,
which is what the contract was meant to stop and what the SDK README and
the wiki already claimed it did.

PLUGIN_ENTITY_NESTED now names, per entity, the derived keys that hold
rows of another entity, and the shaper cuts those rows down to their own
field list, recursively. dayNote and reservationEndpoint are the child
tables that were whole rows; day.assignments and place.tags are held to
the assignment and tag lists too, although their reads already name
their columns. The remaining derived children (travelers, participants,
ratings, budget members, payers and receipts, packing recipients,
attachments, voters, bag members) are built field by field, which the
contract comment now states as the rule for a derived key without a
nested entity.

The whole-row read models get entity contracts: accommodation, dayNote,
todo, tag, category, packingBag, fileLink, collabNote, collabPoll,
collabMessage, journey, journalEntry, bucketItem and collection, and
vacayPlan with its holiday calendars. collections.listMine,
collections.get and vacay.mine return envelopes around rows; an entity
with a null table describes those, so their rows are reached through the
same nesting. Every list holds exactly the keys each read returned
before, so no plugin loses a field. What stays readModel is built field
by field from named columns, and each such entry says what it returns:
setBagMembers, atlas.visited, collections.savePlace and copyToTrip, the
two vacay toggles and journal.addEntryPhoto.

The schema test now covers every child and new table, and calls each
newly shaped method on the real services, comparing nested rows key for
key so the only field allowed to disappear is a withheld one. A further
case proves the nested rows it compares are not empty. gen:plugin-facts
publishes PLUGIN_ENTITY_NESTED to the SDK, and the SDK README, the plugin
wiki pages and server/CLAUDE.md describe the nested rows and list the new
entities.
This commit is contained in:
Maurice
2026-10-08 18:02:39 +02:00
parent 0c9e1cd639
commit 9dec36995e
12 changed files with 973 additions and 192 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ Because the package ships standalone it cannot import server code, so several fi
| SDK copy | Source of truth | Parity test |
|---|---|---|
| `src/generated/host-facts.ts` (permissions, RPC methods, hooks, event catalog, `KNOWN_ADDONS`, the output contract `PLUGIN_ENTITY_FIELDS` / `PLUGIN_METHOD_RESULT`) | `server/src/nest/plugins/protocol/envelope.ts`, `protocol/output-contract.ts` and `server/src/addons.ts` via `server/scripts/gen-plugin-facts.ts` — **machine-written, never hand-edit** | server `check:plugin-facts` CI gate; the entity interfaces in `src/index.ts` are type-checked against `PLUGIN_ENTITY_FIELDS` |
| `src/generated/host-facts.ts` (permissions, RPC methods, hooks, event catalog, `KNOWN_ADDONS`, the output contract `PLUGIN_ENTITY_FIELDS` / `PLUGIN_ENTITY_NESTED` / `PLUGIN_METHOD_RESULT`) | `server/src/nest/plugins/protocol/envelope.ts`, `protocol/output-contract.ts` and `server/src/addons.ts` via `server/scripts/gen-plugin-facts.ts` (**machine-written, never hand-edit**) | server `check:plugin-facts` CI gate; the entity interfaces in `src/index.ts` are type-checked against `PLUGIN_ENTITY_FIELDS` |
| `src/egress-policy.ts` (pure helpers, kept **byte-identical**) | `server/src/nest/plugins/runtime/egress-policy.ts` | `test/permissions-parity.test.ts` |
| `src/manifest.ts` (validation rules incl. the `trek` version range) | `server/src/nest/plugins/install/` | kept in sync by hand |
| `src/cli/checks/*` (registry gates) | TREK-Plugins registry repo (`scripts/validate-entry.mjs`, `check-readme.mjs`) | `test/checks-parity.test.ts` (runs the registry's real script; `TREK_PLUGINS_REPO` points at a checkout) |
+34 -20
View File
@@ -132,25 +132,39 @@ ungranted call throws `PERMISSION_DENIED`.
Every ctx method falls into one of three kinds, listed per method in the exported
`PLUGIN_METHOD_RESULT`:
- **An entity** (`trip`, `place`, `day`, `reservation`, `packingItem`, `tripFile`,
`budgetItem`, `assignment`, `user`; a list where the entry ends in `[]`). Each row
carries exactly the fields in `PLUGIN_ENTITY_FIELDS[entity]` and no others: the
table's published columns in snake_case, then what TREK adds on top (for a trip
`day_count`, `place_count`, `is_owner`, `owner_username` and `shared_count`; for a
place `category`, `tags` and `ratings`; for a reservation `endpoints`, `travelers`
and `day_positions`; for a day its `assignments` and `notes_items`). A field is only
present when the method's read produces it, so `trips.getById` has no `day_count`
while `trips.listMine` does. Credentials are never part of a row (a trip's
calendar `feed_token`, any account data beyond a user's id, username, display name
and avatar). A column TREK adds later is not delivered until TREK publishes it in
this list, so a row never changes shape behind your back. The `Trip`, `Place`, ...
interfaces name the commonly used fields and are checked against these lists when
the SDK is built; `Place.day_id` is deprecated and never delivered, since a place's
days are the `assignments` of `ctx.trips.getDays()`.
- **`readModel`**: the app's own read model for that data (tags, to-dos, collab
notes, journal entries, bags, collections, accommodations and the like), passed on
as TREK's REST route returns it. These are typed `unknown`; treat a field you did
not check for as optional.
- **An entity** (a list where the entry ends in `[]`). The trip data: `trip`,
`place`, `day`, `reservation`, `accommodation`, `packingItem`, `packingBag`,
`tripFile`, `fileLink`, `budgetItem`, `assignment`, `dayNote`, `todo` and `user`;
the collab board: `collabNote`, `collabPoll` and `collabMessage`; the acting
user's own data: `tag`, `category`, `journey`, `journalEntry`, `bucketItem` and
`collection`. Each row carries exactly the fields in `PLUGIN_ENTITY_FIELDS[entity]`
and no others: the table's published columns in snake_case, then what TREK adds on
top (for a trip `day_count`, `place_count`, `is_owner`, `owner_username` and
`shared_count`; for a place `category`, `tags` and `ratings`; for a reservation
`endpoints`, `travelers` and `day_positions`; for a day its `assignments` and
`notes_items`). A field is only present when the method's read produces it, so
`trips.getById` has no `day_count` while `trips.listMine` does. Credentials are
never part of a row (a trip's calendar `feed_token`, any account data beyond a
user's id, username, display name and avatar). A column TREK adds later is not
delivered until TREK publishes it in this list, so a row never changes shape behind
your back.
The same holds for the rows inside a row. `PLUGIN_ENTITY_NESTED` names, per entity,
the fields that hold rows of another entity (a day's `notes_items` are `dayNote`
rows, a reservation's `endpoints` are `reservationEndpoint` rows), and those carry
that entity's fields and no others, however deep they sit. Every other added field
(counts, `{ user_id, username }` lists, id lists, a place's `category`) is a value
TREK builds field by field. Three results are envelopes around rows rather than a
row: `collections.listMine` (`collectionListing`), `collections.get`
(`collectionDetail`) and `vacay.mine` (`vacayPlanData`).
The `Trip`, `Place`, ... interfaces name the commonly used fields and are checked
against these lists when the SDK is built; `Place.day_id` is deprecated and never
delivered, since a place's days are the `assignments` of `ctx.trips.getDays()`.
- **`readModel`**: a small result TREK builds field by field from named columns, so
no stored row passes through it whole: a bag's members, the Atlas `visited` codes,
what a Vacay toggle did, a collection's saved place or copy summary, and a journal
photo. These are typed `unknown`; treat a field you did not check for as optional.
- **`host`**: a value the host builds itself (`{ deleted }`, `{ sent }`, a model's
answer, an access token) or data you own (`ctx.db`, `ctx.meta`, another plugin's
answer), returned as it is.
@@ -375,7 +389,7 @@ npx trek-plugin-sdk publish --repo you/repo --tag v1.1.0 --sign # or just answ
- `definePlugin(def)` + all the plugin types (`PluginContext`, `PluginRoute`, `PluginJob`, `PhotoProvider`, `CalendarSource`).
- `PLUGIN_API_VERSION` — embed as `apiVersion` in your manifest.
- `PLUGIN_ENTITY_FIELDS`, `PLUGIN_METHOD_RESULT`: what each ctx method returns and which fields each entity row carries (see [What a ctx call returns](#what-a-ctx-call-returns)).
- `PLUGIN_ENTITY_FIELDS`, `PLUGIN_ENTITY_NESTED`, `PLUGIN_METHOD_RESULT`: what each ctx method returns, which fields each entity row carries and which of them hold rows of another entity (see [What a ctx call returns](#what-a-ctx-call-returns)).
- `validateManifest(json)` — the manifest rules the server loader uses.
- `settingDefaults(manifest, scope)` — the `default`s of one settings scope, keyed by field: what the host resolves for an unset field, and what `dev` seeds `ctx.config` / `ctx.settings` with. Spread it under your own fixtures when calling `createMockHost` to mirror the host. `SETTING_FIELD_KEYS` is the attribute list the host stores.
- `createMockHost(opts)` (from `trek-plugin-sdk/testing`).
+319 -36
View File
@@ -622,8 +622,291 @@ export const PLUGIN_ENTITY_FIELDS = {
'display_name',
'avatar',
],
dayNote: [
'id',
'day_id',
'trip_id',
'text',
'time',
'icon',
'sort_order',
'created_at',
'color',
],
reservationEndpoint: [
'id',
'reservation_id',
'role',
'sequence',
'name',
'code',
'lat',
'lng',
'timezone',
'local_time',
'local_date',
'created_at',
],
tag: [
'id',
'user_id',
'name',
'color',
'created_at',
],
todo: [
'id',
'trip_id',
'name',
'checked',
'category',
'sort_order',
'due_date',
'description',
'assigned_user_id',
'priority',
'created_at',
'reminded_at',
],
accommodation: [
'id',
'trip_id',
'place_id',
'start_day_id',
'end_day_id',
'check_in',
'check_in_end',
'check_out',
'confirmation',
'notes',
'created_at',
'place_name',
'place_address',
'place_image',
'place_lat',
'place_lng',
'reservation_title',
],
packingBag: [
'id',
'trip_id',
'name',
'color',
'weight_limit_grams',
'sort_order',
'created_at',
'user_id',
'assigned_username',
'members',
'total_weight_grams',
],
fileLink: [
'id',
'file_id',
'reservation_id',
'assignment_id',
'place_id',
'created_at',
'budget_item_id',
],
collabNote: [
'id',
'trip_id',
'user_id',
'category',
'title',
'content',
'color',
'pinned',
'created_at',
'updated_at',
'website',
'username',
'avatar',
'avatar_url',
'attachments',
],
collabPoll: [
'id',
'trip_id',
'user_id',
'question',
'options',
'multiple',
'closed',
'deadline',
'created_at',
'username',
'avatar',
'avatar_url',
'is_closed',
'multiple_choice',
],
collabMessage: [
'id',
'trip_id',
'user_id',
'text',
'reply_to',
'created_at',
'deleted',
'username',
'avatar',
'reply_text',
'reply_username',
'user_avatar',
'avatar_url',
'reactions',
'attachments',
],
journey: [
'id',
'user_id',
'title',
'subtitle',
'cover_gradient',
'status',
'created_at',
'updated_at',
'cover_image',
'show_trip_tracks',
'show_verdict',
'show_mood',
'show_weather',
'status_override',
'photo_location',
'entry_count',
'photo_count',
'place_count',
'trip_date_min',
'trip_date_max',
],
journalEntry: [
'id',
'journey_id',
'source_trip_id',
'source_place_id',
'author_id',
'type',
'title',
'story',
'entry_date',
'entry_time',
'location_name',
'location_lat',
'location_lng',
'mood',
'weather',
'tags',
'visibility',
'sort_order',
'created_at',
'updated_at',
'pros_cons',
'stats_excluded',
'dismissed',
'country_code',
'source_assignment_id',
'is_draft',
'photos',
'source_trip_name',
],
category: [
'id',
'name',
'color',
'icon',
'user_id',
'created_at',
],
bucketItem: [
'id',
'user_id',
'name',
'lat',
'lng',
'country_code',
'notes',
'created_at',
'target_date',
'visited_at',
'visited_source',
'region_code',
],
collection: [
'id',
'owner_id',
'name',
'description',
'color',
'icon',
'cover_image',
'links',
'sort_order',
'created_at',
'updated_at',
'place_count',
'members',
'is_owner',
'labels',
],
collectionListing: [
'collections',
'incomingInvites',
],
collectionDetail: [
'collection',
'places',
],
vacayPlan: [
'id',
'owner_id',
'block_weekends',
'holidays_enabled',
'holidays_region',
'school_holidays_enabled',
'company_holidays_enabled',
'carry_over_enabled',
'created_at',
'weekend_days',
'week_start',
'holiday_calendars',
],
vacayHolidayCalendar: [
'id',
'plan_id',
'type',
'region',
'label',
'color',
'sort_order',
],
vacayPlanData: [
'plan',
'users',
'pendingInvites',
'incomingInvites',
'isOwner',
'isFused',
],
} as const;
/**
* The fields of an entity that hold rows of another entity (one row or a list),
* and which one. Those rows carry that entity's PLUGIN_ENTITY_FIELDS and no others,
* however deep they sit, so a column TREK adds to a child table is held back the
* same way. A field not listed here holds a value TREK builds field by field.
*/
export const PLUGIN_ENTITY_NESTED: Readonly<Record<string, Readonly<Record<string, string>>>> = {
place: { tags: 'tag' },
day: { assignments: 'assignment', notes_items: 'dayNote' },
reservation: { endpoints: 'reservationEndpoint' },
collectionListing: { collections: 'collection' },
collectionDetail: { collection: 'collection' },
vacayPlan: { holiday_calendars: 'vacayHolidayCalendar' },
vacayPlanData: { plan: 'vacayPlan' },
};
/**
* What each ctx method returns: an entity from PLUGIN_ENTITY_FIELDS (`trip`, or
* `trip[]` for a list), `host` for a value the host builds itself or data the
@@ -639,54 +922,54 @@ export const PLUGIN_METHOD_RESULT: Readonly<Record<string, string>> = {
'trips.getPlaces': 'place[]',
'trips.getReservations': 'reservation[]',
'trips.getDays': 'day[]',
'trips.getAccommodations': 'readModel',
'trips.getAccommodations': 'accommodation[]',
'trips.listMine': 'trip[]',
'reservations.listMine': 'reservation[]',
'reservations.create': 'reservation',
'reservations.update': 'reservation',
'reservations.delete': 'host',
'accommodations.create': 'readModel',
'accommodations.update': 'readModel',
'accommodations.create': 'accommodation',
'accommodations.update': 'accommodation',
'accommodations.delete': 'host',
'packing.list': 'packingItem[]',
'packing.create': 'packingItem',
'packing.update': 'packingItem',
'packing.delete': 'host',
'packing.listBags': 'readModel',
'packing.createBag': 'readModel',
'packing.updateBag': 'readModel',
'packing.listBags': 'packingBag[]',
'packing.createBag': 'packingBag',
'packing.updateBag': 'packingBag',
'packing.deleteBag': 'host',
'packing.setBagMembers': 'readModel',
'files.list': 'tripFile[]',
'files.getContent': 'host',
'files.create': 'tripFile',
'files.createLink': 'readModel',
'files.createLink': 'fileLink[]',
'files.update': 'tripFile',
'files.softDelete': 'host',
'collab.listNotes': 'readModel',
'collab.listPolls': 'readModel',
'collab.listMessages': 'readModel',
'collab.createNote': 'readModel',
'collab.createPoll': 'readModel',
'collab.votePoll': 'readModel',
'collab.createMessage': 'readModel',
'collab.listNotes': 'collabNote[]',
'collab.listPolls': 'collabPoll[]',
'collab.listMessages': 'collabMessage[]',
'collab.createNote': 'collabNote',
'collab.createPoll': 'collabPoll',
'collab.votePoll': 'collabPoll',
'collab.createMessage': 'collabMessage',
'trips.addMember': 'host',
'trips.removeMember': 'host',
'trips.create': 'trip',
'journal.listMine': 'readModel',
'journal.getEntries': 'readModel',
'journal.listMine': 'journey[]',
'journal.getEntries': 'journalEntry[]',
'atlas.visited': 'readModel',
'atlas.bucketList': 'readModel',
'atlas.bucketList': 'bucketItem[]',
'rates.get': 'host',
'vacay.mine': 'readModel',
'daynotes.list': 'readModel',
'daynotes.create': 'readModel',
'daynotes.update': 'readModel',
'vacay.mine': 'vacayPlanData',
'daynotes.list': 'dayNote[]',
'daynotes.create': 'dayNote',
'daynotes.update': 'dayNote',
'daynotes.delete': 'host',
'collections.listMine': 'readModel',
'collections.get': 'readModel',
'collections.create': 'readModel',
'collections.update': 'readModel',
'collections.listMine': 'collectionListing',
'collections.get': 'collectionDetail',
'collections.create': 'collection',
'collections.update': 'collection',
'collections.savePlace': 'readModel',
'collections.copyToTrip': 'readModel',
'collections.deletePlace': 'host',
@@ -694,26 +977,26 @@ export const PLUGIN_METHOD_RESULT: Readonly<Record<string, string>> = {
'atlas.unmarkCountry': 'host',
'atlas.markRegion': 'host',
'atlas.unmarkRegion': 'host',
'atlas.createBucketItem': 'readModel',
'atlas.createBucketItem': 'bucketItem',
'atlas.deleteBucketItem': 'host',
'vacay.toggleEntry': 'readModel',
'vacay.toggleCompanyHoliday': 'readModel',
'journal.createEntry': 'readModel',
'journal.createEntry': 'journalEntry',
'journal.addEntryPhoto': 'readModel',
'journal.updateEntry': 'readModel',
'journal.updateEntry': 'journalEntry',
'journal.deleteEntry': 'host',
'journal.createJourney': 'readModel',
'journal.createJourney': 'journey',
'journal.deleteJourney': 'host',
'weather.get': 'host',
'categories.list': 'readModel',
'tags.list': 'readModel',
'tags.create': 'readModel',
'tags.update': 'readModel',
'categories.list': 'category[]',
'tags.list': 'tag[]',
'tags.create': 'tag',
'tags.update': 'tag',
'tags.delete': 'host',
'trips.members': 'user[]',
'todos.list': 'readModel',
'todos.create': 'readModel',
'todos.update': 'readModel',
'todos.list': 'todo[]',
'todos.create': 'todo',
'todos.update': 'todo',
'todos.delete': 'host',
'costs.getByTrip': 'budgetItem[]',
'costs.listMine': 'budgetItem[]',
+3 -2
View File
@@ -1007,8 +1007,9 @@ export {
export {
EVENT_FAMILIES, EVENT_SNAPSHOT_GRANT, KNOWN_PERMISSIONS,
} from './generated/host-facts.js';
// What ctx results carry: the published fields of each entity, and what each method returns.
export { PLUGIN_ENTITY_FIELDS, PLUGIN_METHOD_RESULT } from './generated/host-facts.js';
// What ctx results carry: the published fields of each entity, the fields holding rows
// of another entity, and what each method returns.
export { PLUGIN_ENTITY_FIELDS, PLUGIN_ENTITY_NESTED, PLUGIN_METHOD_RESULT } from './generated/host-facts.js';
// The lucide icons a `capabilities.poiCategories` entry may use, and the per-plugin cap.
export { POI_CATEGORY_ICONS, POI_CATEGORY_MAX } from './generated/host-facts.js';
+17 -1
View File
@@ -5,7 +5,7 @@
* runtime side: what the exports hold and that they cover the method vocabulary.
*/
import { describe, it, expect } from 'vitest';
import { PLUGIN_ENTITY_FIELDS, PLUGIN_METHOD_RESULT } from '../src/index.js';
import { PLUGIN_ENTITY_FIELDS, PLUGIN_ENTITY_NESTED, PLUGIN_METHOD_RESULT } from '../src/index.js';
import { KNOWN_ADDONS } from '../src/manifest.js';
import { KNOWN_METHODS } from '../src/generated/host-facts.js';
@@ -26,6 +26,20 @@ describe('PLUGIN_ENTITY_FIELDS', () => {
});
});
describe('PLUGIN_ENTITY_NESTED', () => {
it('points the fields that hold rows at entities with a field list', () => {
const fields: Readonly<Record<string, readonly string[]>> = PLUGIN_ENTITY_FIELDS;
for (const [entity, nested] of Object.entries(PLUGIN_ENTITY_NESTED)) {
for (const [field, child] of Object.entries(nested)) {
expect(fields[entity]).toContain(field);
expect(fields[child]).toBeDefined();
}
}
expect(PLUGIN_ENTITY_NESTED.day).toEqual({ assignments: 'assignment', notes_items: 'dayNote' });
expect(PLUGIN_ENTITY_NESTED.reservation).toEqual({ endpoints: 'reservationEndpoint' });
});
});
describe('PLUGIN_METHOD_RESULT', () => {
it('describes every wire method plus the three unconditional ones', () => {
for (const method of KNOWN_METHODS) expect(PLUGIN_METHOD_RESULT[method]).toBeDefined();
@@ -41,6 +55,8 @@ describe('PLUGIN_METHOD_RESULT', () => {
expect(PLUGIN_METHOD_RESULT['trips.getById']).toBe('trip');
expect(PLUGIN_METHOD_RESULT['trips.getPlaces']).toBe('place[]');
expect(PLUGIN_METHOD_RESULT['db.query']).toBe('host');
expect(PLUGIN_METHOD_RESULT['tags.list']).toBe('tag[]');
expect(PLUGIN_METHOD_RESULT['atlas.visited']).toBe('readModel');
});
});