@kairos-es/core 1.0.0-alpha.3 → 1.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,71 @@
1
- import { Effect, Layer, PubSub, Ref, Stream } from 'effect';
1
+ import { DateTime, Effect, Layer, PubSub, Ref, Stream } from 'effect';
2
2
  import { makeContractStore } from "./ContractStore.js";
3
3
  import { DcbEventStore } from "./DcbEventStore.js";
4
4
  import { ORIGIN } from "./Event.js";
5
5
  import { AppendConditionFailed } from "./errors.js";
6
6
  import { matchesSequenced } from "./Query.js";
7
7
  import { DEFAULT_CATCH_UP_PAGE_SIZE, DEFAULT_POLL_INTERVAL_MILLIS, subscribeStream } from "./SubscribeMachine.js";
8
+ /**
9
+ * A copy of `event` that shares no mutable structure with it: fresh `data` bytes, a
10
+ * fresh `tags` array, a fresh `metadata` bag and a fresh `occurredAt`. Only `type`
11
+ * and `uuid`, which are strings, are carried as they are.
12
+ *
13
+ * Each copy is chosen for what it does on EVERY input the type admits, not on the
14
+ * common one. `data` is copied through the `Uint8Array` constructor and not with
15
+ * `slice`, because `DcbEvent` admits any `Uint8Array` subclass and a subclass may
16
+ * answer `slice` with a VIEW of the same memory — Node's `Buffer` does. A
17
+ * `DateTime.Utc` is an ordinary object whose fields can be written past its
18
+ * `readonly` type, so `occurredAt` is rebuilt from its instant rather than shared.
19
+ *
20
+ * ## Why the oracle copies at BOTH edges
21
+ *
22
+ * This store is the reference the SQL engines are judged against, and a SQL engine
23
+ * shares nothing with its caller: it serialises on the way in and builds fresh objects
24
+ * on the way out. A store that kept the caller's objects instead would let a caller
25
+ * rewrite the log AFTER the append — mutate a `Uint8Array`, push to a `tags` array,
26
+ * assign into a `metadata` bag — and every later read would see the rewritten event
27
+ * with no append, no position and no wake behind it. A reference that can be edited
28
+ * after the fact judges nothing.
29
+ *
30
+ * So `append` stores a copy (the log never holds a caller's object) and every read
31
+ * path hands out a copy (a reader cannot reach the log through what it was given).
32
+ * Copying on the way in alone would close the first route and leave the second open:
33
+ * the same object would then be shared by every reader of the log.
34
+ */
35
+ const copyEvent = event => ({
36
+ type: event.type,
37
+ data: new Uint8Array(event.data),
38
+ tags: [...event.tags],
39
+ uuid: event.uuid,
40
+ occurredAt: DateTime.unsafeMake(DateTime.toEpochMillis(event.occurredAt)),
41
+ metadata: {
42
+ ...event.metadata
43
+ }
44
+ });
45
+ /**
46
+ * A copy of `query` that shares no array with it, taking only the keys the grammar
47
+ * defines.
48
+ *
49
+ * `subscribe` is the one verb that holds a query beyond its own call: the fetch seam
50
+ * judges it again on every page for as long as the subscription runs. Each SQL engine
51
+ * compiles its plan once, when the subscription starts, so an edit the caller makes
52
+ * to its own object afterwards reaches nothing there. The oracle has to answer the
53
+ * same, for the reason it copies events (above): a reference that the caller can
54
+ * change mid-stream judges nothing. Only `types` and `tags` are carried because they
55
+ * are all `matchesQueryItem` reads, and a key the grammar does not define was
56
+ * already refused at the contract decorator before this runs.
57
+ */
58
+ const copyQuery = query => ({
59
+ items: query.items.map(item => ({
60
+ types: [...item.types],
61
+ tags: [...item.tags]
62
+ }))
63
+ });
64
+ /** A `SequencedEvent` whose event is a `copyEvent` of the stored one. */
65
+ const copySequenced = sequenced => ({
66
+ event: copyEvent(sequenced.event),
67
+ position: sequenced.position
68
+ });
8
69
  /** Filter a snapshot by `query` and `after` (strict `position > after`). */
9
70
  const filterLog = (log, query, after) => log.filter(sequenced => (after === undefined || sequenced.position > after) && matchesSequenced(sequenced, query));
10
71
  /** The position of the last event in a slice, or `undefined` when empty. */
@@ -22,7 +83,7 @@ const readSnapshot = (log, query, options) => {
22
83
  // when there is nothing to report.
23
84
  const head = (options?.limit === undefined ? lastPosition(log) : lastPosition(limited)) ?? ORIGIN;
24
85
  return {
25
- events: Stream.fromIterable(limited),
86
+ events: Stream.fromIterable(limited.map(copySequenced)),
26
87
  head
27
88
  };
28
89
  };
@@ -74,7 +135,7 @@ const fetchPage = (log, query, from, limit) => {
74
135
  for (let index = firstIndexAfter(log, from); index < log.length && page.length < limit; index += 1) {
75
136
  const sequenced = log[index];
76
137
  if (matchesSequenced(sequenced, query)) {
77
- page.push(sequenced);
138
+ page.push(copySequenced(sequenced));
78
139
  }
79
140
  }
80
141
  return page;
@@ -99,45 +160,51 @@ const make = options => Effect.gen(function* () {
99
160
  // is reentrant, so append's inner read re-enters. Here append inlines the
100
161
  // read logic instead, so a plain lock suffices.
101
162
  mutex.withPermits(1)(Effect.map(Ref.get(log), snapshot => readSnapshot(snapshot, query, options)));
102
- const append = (events, condition) =>
103
- // Critical section: check + commit are atomic (the exactly-one-wins guarantee).
104
- mutex.withPermits(1)(Effect.gen(function* () {
105
- const snapshot = yield* Ref.get(log);
106
- if (condition !== undefined) {
107
- const conflicts = filterLog(snapshot, condition.failIfEventsMatch, condition.after);
108
- // Detect by presence: any matching event after `condition.after`
109
- // fails the append. The error carries only the condition (see
110
- // `AppendConditionFailed`); a correct retry re-reads to the new head.
111
- if (conflicts.length > 0) {
112
- return yield* new AppendConditionFailed({
113
- condition
114
- });
163
+ const append = (events, condition) => Effect.suspend(() => {
164
+ // The events are copied when the append STARTS, before it waits for the
165
+ // permit, as each SQL engine serialises them before its first suspension:
166
+ // an edit the caller makes while this append is queued must not reach the
167
+ // log here when it could not reach theirs.
168
+ const owned = events.map(copyEvent);
169
+ // Critical section: check + commit are atomic (the exactly-one-wins guarantee).
170
+ return mutex.withPermits(1)(Effect.gen(function* () {
171
+ const snapshot = yield* Ref.get(log);
172
+ if (condition !== undefined) {
173
+ const conflicts = filterLog(snapshot, condition.failIfEventsMatch, condition.after);
174
+ // Detect by presence: any matching event after `condition.after`
175
+ // fails the append. The error carries only the condition (see
176
+ // `AppendConditionFailed`); a correct retry re-reads to the new head.
177
+ if (conflicts.length > 0) {
178
+ return yield* new AppendConditionFailed({
179
+ condition
180
+ });
181
+ }
115
182
  }
116
- }
117
- // Optional test seam between the conflict-check and the commit (see
118
- // `InMemoryStoreOptions.beforeCommit`). Defaults to `Effect.void`, a
119
- // synchronous no-op, so the shipped critical section stays atomic by
120
- // construction; the concurrency contract test injects `Effect.yieldNow()`
121
- // here to make the exactly-one-wins guard genuinely depend on the mutex.
122
- yield* beforeCommit;
123
- const start = yield* Ref.get(nextPosition);
124
- const appended = events.map((event, index) => ({
125
- event,
126
- position: start + BigInt(index)
183
+ // Optional test seam between the conflict-check and the commit (see
184
+ // `InMemoryStoreOptions.beforeCommit`). Defaults to `Effect.void`, a
185
+ // synchronous no-op, so the shipped critical section stays atomic by
186
+ // construction; the concurrency contract test injects `Effect.yieldNow()`
187
+ // here to make the exactly-one-wins guard genuinely depend on the mutex.
188
+ yield* beforeCommit;
189
+ const start = yield* Ref.get(nextPosition);
190
+ const appended = owned.map((event, index) => ({
191
+ event,
192
+ position: start + BigInt(index)
193
+ }));
194
+ const end = start + BigInt(events.length);
195
+ yield* Ref.set(log, [...snapshot, ...appended]);
196
+ yield* Ref.set(nextPosition, end);
197
+ // The hub is a WAKE SOURCE and nothing else: the subscribe machine never
198
+ // reads what it carries, and its poll delivers with or without one. That
199
+ // is why suppressing the publish is a legitimate test seam rather than a
200
+ // broken store (see `InMemoryStoreOptions.suppressWake`).
201
+ if (!suppressWake) {
202
+ yield* PubSub.publishAll(hub, appended);
203
+ }
204
+ // `events` is non-empty, so the last new position is `end - 1`.
205
+ return end - 1n;
127
206
  }));
128
- const end = start + BigInt(events.length);
129
- yield* Ref.set(log, [...snapshot, ...appended]);
130
- yield* Ref.set(nextPosition, end);
131
- // The hub is a WAKE SOURCE and nothing else: the subscribe machine never
132
- // reads what it carries, and its poll delivers with or without one. That
133
- // is why suppressing the publish is a legitimate test seam rather than a
134
- // broken store (see `InMemoryStoreOptions.suppressWake`).
135
- if (!suppressWake) {
136
- yield* PubSub.publishAll(hub, appended);
137
- }
138
- // `events` is non-empty, so the last new position is `end - 1`.
139
- return end - 1n;
140
- }));
207
+ });
141
208
  const subscribe = (query, after) =>
142
209
  // Take the hub seat HERE, before handing the machine its wake source, so the
143
210
  // subscription is established no later than the machine's first `fetch`. That
@@ -153,9 +220,12 @@ const make = options => Effect.gen(function* () {
153
220
  // is nothing left here for a critical section to protect. `append` keeps its
154
221
  // permit, which is the whole exactly-one-wins guarantee.
155
222
  Stream.unwrapScoped(Effect.gen(function* () {
223
+ // Taken when the subscription starts, as the SQL engines compile theirs
224
+ // (see `copyQuery`).
225
+ const judged = copyQuery(query);
156
226
  const wakes = yield* PubSub.subscribe(hub);
157
227
  return subscribeStream({
158
- fetch: (from, limit) => Effect.map(Ref.get(log), snapshot => fetchPage(snapshot, query, from, limit)),
228
+ fetch: (from, limit) => Effect.map(Ref.get(log), snapshot => fetchPage(snapshot, judged, from, limit)),
159
229
  listen: Stream.fromQueue(wakes),
160
230
  pollInterval,
161
231
  catchUpPageSize
@@ -1 +1 @@
1
- {"version":3,"file":"inMemory.js","names":["Effect","Layer","PubSub","Ref","Stream","makeContractStore","DcbEventStore","ORIGIN","AppendConditionFailed","matchesSequenced","DEFAULT_CATCH_UP_PAGE_SIZE","DEFAULT_POLL_INTERVAL_MILLIS","subscribeStream","filterLog","log","query","after","filter","sequenced","undefined","position","lastPosition","events","at","readSnapshot","options","matched","limited","limit","slice","head","fromIterable","firstIndexAfter","low","high","length","mid","fetchPage","from","page","index","push","make","gen","beforeCommit","void","suppressWake","catchUpPageSize","pollInterval","mutex","makeSemaphore","hub","unbounded","nextPosition","read","withPermits","map","get","snapshot","append","condition","conflicts","failIfEventsMatch","start","appended","event","BigInt","end","set","publishAll","subscribe","unwrapScoped","wakes","fetch","listen","fromQueue","DcbEventStoreInMemory","effect","makeInMemoryTestLayer"],"sources":["../../src/inMemory.ts"],"sourcesContent":[null],"mappings":"AA2BA,SAASA,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,GAAG,EAAEC,MAAM,QAAQ,QAAQ;AAE3D,SAASC,iBAAiB,QAA+B,oBAAiB;AAE1E,SAASC,aAAa,QAAQ,oBAAiB;AAE/C,SAASC,MAAM,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MAAMM,qBAAqB,gBAA+B3E,KAAK,CAAC4E,MAAM,CAC3EvE,aAAa,eACbN,MAAM,CAACwD,GAAG,cAACd,IAAI,EAAE,EAAErC,iBAAiB,CAAC,CACtC;AAED;;;;;;;;;;;AAWA,OAAO,MAAMyE,qBAAqB,GAChCrD,OAA6B,IAE7BxB,KAAK,CAAC4E,MAAM,CAACvE,aAAa,EAAEN,MAAM,CAACwD,GAAG,CAACd,IAAI,CAACjB,OAAO,CAAC,EAAEpB,iBAAiB,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"inMemory.js","names":["DateTime","Effect","Layer","PubSub","Ref","Stream","makeContractStore","DcbEventStore","ORIGIN","AppendConditionFailed","matchesSequenced","DEFAULT_CATCH_UP_PAGE_SIZE","DEFAULT_POLL_INTERVAL_MILLIS","subscribeStream","copyEvent","event","type","data","Uint8Array","tags","uuid","occurredAt","unsafeMake","toEpochMillis","metadata","copyQuery","query","items","map","item","types","copySequenced","sequenced","position","filterLog","log","after","filter","undefined","lastPosition","events","at","readSnapshot","options","matched","limited","limit","slice","head","fromIterable","firstIndexAfter","low","high","length","mid","fetchPage","from","page","index","push","make","gen","beforeCommit","void","suppressWake","catchUpPageSize","pollInterval","mutex","makeSemaphore","hub","unbounded","nextPosition","read","withPermits","get","snapshot","append","condition","suspend","owned","conflicts","failIfEventsMatch","start","appended","BigInt","end","set","publishAll","subscribe","unwrapScoped","judged","wakes","fetch","listen","fromQueue","DcbEventStoreInMemory","effect","makeInMemoryTestLayer"],"sources":["../../src/inMemory.ts"],"sourcesContent":[null],"mappings":"AA2BA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,GAAG,EAAEC,MAAM,QAAQ,QAAQ;AAErE,SAASC,iBAAiB,QAA+B,oBAAiB;AAE1E,SAASC,aAAa,QAAQ,oBAAiB;AAE/C,SAASC,MAAM,QAAQ,YAAS;AAChC,SAASC,qBAAqB,QAAQ,aAAU;AAEhD,SAASC,gBA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qBAAqB,GAChCtD,OAA6B,IAE7BzC,KAAK,CAAC8F,MAAM,CAACzF,aAAa,EAAEN,MAAM,CAAC2B,GAAG,CAACgC,IAAI,CAACjB,OAAO,CAAC,EAAErC,iBAAiB,CAAC,CAAC","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kairos-es/core",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "DCB-first event-sourcing contract and in-memory store for Effect v3",
5
5
  "license": "BSD-3-Clause",
6
6
  "repository": {
@@ -11,7 +11,9 @@
11
11
  * needing a bespoke `catchAll`/`instanceof`:
12
12
  *
13
13
  * - `assertServableQuery` at every verb's entry (read, the append condition's
14
- * guard query, subscribe) — a non-servable query is a DEFECT in every engine;
14
+ * guard query, subscribe) — a non-servable query, or one carrying a key the
15
+ * grammar does not define, is a DEFECT in every engine, and an append condition
16
+ * carrying a key `AppendCondition` does not define is refused alongside it;
15
17
  * - `ReadLimit` decode on `read`'s `limit` — a negative/non-integer limit is a
16
18
  * DEFECT (so `read` keeps `E = never`), validated through the branded schema
17
19
  * even at an untyped boundary that reaches the store as `unknown`;
@@ -34,7 +36,7 @@
34
36
  */
35
37
  import type { Array as Arr, Scope } from 'effect'
36
38
  import { Effect, Predicate, Schema, Stream } from 'effect'
37
- import type { AppendCondition } from './AppendCondition'
39
+ import { AppendCondition } from './AppendCondition'
38
40
  import { assertAppendNotOwned } from './AppendIsOwned'
39
41
  import type { DcbEventStore, ReadOptions, ReadResponse } from './DcbEventStore'
40
42
  import type { DcbEvent, Position, SequencedEvent } from './Event'
@@ -87,7 +89,7 @@ export const makeContractStore = <E>(
87
89
 
88
90
  append: (events, condition) =>
89
91
  // The ownership refusal runs FIRST and sits OUTSIDE the suspend, because it is
90
- // the only one of append's four checks that is EFFECTFUL: it consults a
92
+ // the only one of append's checks that is EFFECTFUL: it consults a
91
93
  // `Context.Tag` rather than inspecting the arguments. `Effect.flatMap` over an
92
94
  // already-lazy effect defers it exactly as the suspend defers the throws below,
93
95
  // so nothing runs at the call site either way; what it buys is that the
@@ -113,8 +115,22 @@ export const makeContractStore = <E>(
113
115
  'plain-data caller bypassing the type — a defect, not a conflict.',
114
116
  )
115
117
  }
116
- // A non-servable guard query is a defect in every engine.
118
+ // A non-servable guard query is a defect in every engine, and so is a
119
+ // condition carrying a key `AppendCondition` does not define: an author
120
+ // who wrote one believes they added a condition, and every engine would
121
+ // ignore it. Known keys come off the schema, as `servableQueryFault`
122
+ // reads its own (`Query.ts` owns the argument).
117
123
  if (condition !== undefined) {
124
+ const unknown = Object.keys(condition).find(
125
+ (key) => !Object.hasOwn(AppendCondition.fields, key),
126
+ )
127
+ if (unknown !== undefined) {
128
+ throw new Error(
129
+ `makeContractStore.append: the append condition carries the key ${JSON.stringify(unknown)}, ` +
130
+ 'which `AppendCondition` does not define; an unknown key would be ' +
131
+ 'silently ignored by every engine rather than honoured, so it is refused',
132
+ )
133
+ }
118
134
  assertServableQuery(condition.failIfEventsMatch)
119
135
  }
120
136
  // Only `AppendConditionFailed` belongs on the contract error channel;
package/src/Event.ts CHANGED
@@ -110,13 +110,74 @@ export const EventType = Schema.String.pipe(
110
110
  )
111
111
  export type EventType = typeof EventType.Type
112
112
 
113
+ /**
114
+ * An event's metadata: a flat bag of strings carrying the circumstances under
115
+ * which the event was RECORDED, never anything the business asserts (ADR-0017).
116
+ * Payload is what happened; metadata is the context of the recording — a
117
+ * `traceparent`, a correlation or causation id, a deployment revision.
118
+ *
119
+ * Values are strings, not `unknown`, for cross-engine parity rather than taste:
120
+ * both SQL engines carry the bag as a JSON document, and `JSON.stringify` drops
121
+ * `undefined`, throws on `bigint` and turns `NaN` into `null`, none of which the
122
+ * in-memory oracle does.
123
+ *
124
+ * The key schema carries no refinement, deliberately: a refinement on a
125
+ * `Schema.Record` key acts as a FILTER on decode, silently dropping a key that
126
+ * fails it rather than failing the decode. Constraining keys, if ever wanted,
127
+ * has to be a validating step of its own.
128
+ *
129
+ * Two engine limits a consumer must know, because the bag is typically filled
130
+ * from outside the application at a request edge. Postgres refuses a NUL
131
+ * character (U+0000) and an unpaired surrogate in any string it stores, so an
132
+ * event whose bag holds one fails to append there while the in-memory and
133
+ * SQLite engines accept it; sanitise untrusted values before they reach the
134
+ * bag. And key ORDER is not preserved: Postgres stores an object's keys in an
135
+ * order of its own, so compare bags as unordered and never rely on the order
136
+ * `Object.keys` returns.
137
+ *
138
+ * Metadata is never part of a `Query` or an `AppendCondition` — a store refuses
139
+ * either when it carries a key the grammar does not define, so a metadata
140
+ * predicate cannot be written and silently ignored — never reaches a codec's
141
+ * `tagsFor`, and no store offers a way to rewrite it once appended. ADR-0017
142
+ * owns why each of those is mechanical rather than preferential.
143
+ *
144
+ * ## The one home of the shape rule
145
+ *
146
+ * The bag is a JSON OBJECT whose every value is a STRING, and this schema is
147
+ * where that rule is stated. At runtime it is checked in ONE place, at write: the
148
+ * codec's `toEvent` decodes every bag through this schema and refuses one that
149
+ * fails on its own error channel, so a value smuggled past the type (through an
150
+ * `any`) stops there. The schema judges a bag's OWN enumerable keys, so an object
151
+ * that keeps its entries anywhere else — a `Map`, a `Date` — reads as the empty
152
+ * bag rather than failing. A store checks no values: an event built by hand and handed
153
+ * to `append` directly carries whatever it carries, and what each engine then does
154
+ * with a non-string value differs — one may store it and read it back differently
155
+ * from another, and a `bigint` makes a SQL engine's append fail. Each
156
+ * SQL engine keeps a container check of its own on read — the column must hold a
157
+ * JSON object — because that defends against a writer outside the library and is
158
+ * about the column, not the values. A VALUE check must never be added on read
159
+ * alone: it would make an event the store accepted unreadable for ever, so a
160
+ * value check belongs at write or nowhere.
161
+ */
162
+ export const Metadata = Schema.Record({
163
+ key: Schema.String,
164
+ value: Schema.String,
165
+ })
166
+ export type Metadata = typeof Metadata.Type
167
+
113
168
  /**
114
169
  * An event as stored: an opaque payload plus its type, tags, a
115
- * producer-supplied `uuid`, and producer-supplied `occurredAt` domain time.
170
+ * producer-supplied `uuid`, producer-supplied `occurredAt` domain time, and its
171
+ * `metadata` bag.
116
172
  *
117
173
  * `data` is `Uint8Array` (opaque bytes); the store does not interpret it.
118
174
  * `uuid` is producer-supplied to keep `core` deterministic (no RNG in core).
119
175
  * `occurredAt` is informational only; ordering is by position alone.
176
+ * `metadata` mirrors `tags` rather than `data`: REQUIRED, with an empty bag
177
+ * legal. Required because an absent-versus-empty divergence between engines is
178
+ * invisible to `toEqual`, which ignores `undefined` properties, so an optional
179
+ * field could drift across the three engines with the round-trip suite green.
180
+ * Core only carries it; the codec's `toEvent` is where it is resolved.
120
181
  */
121
182
  export const DcbEvent = Schema.Struct({
122
183
  type: EventType,
@@ -124,6 +185,7 @@ export const DcbEvent = Schema.Struct({
124
185
  tags: Schema.Array(Tag),
125
186
  uuid: Schema.UUID,
126
187
  occurredAt: Schema.DateTimeUtc,
188
+ metadata: Metadata,
127
189
  })
128
190
  export type DcbEvent = typeof DcbEvent.Type
129
191
 
package/src/Projection.ts CHANGED
@@ -38,7 +38,7 @@ const WILDCARD: Query = { items: [] }
38
38
 
39
39
  /**
40
40
  * An already-decoded event handed to a projection's fold: the sequenced wire
41
- * envelope (position, type, tags, timings) plus the decoded `payload`.
41
+ * envelope (position, type, tags, timings, metadata) plus the decoded `payload`.
42
42
  *
43
43
  * Core does not decode — it only folds events another layer has already decoded
44
44
  * — so `payload` is `unknown` at this layer. `@kairos-es/codec`'s builder emits
@@ -110,13 +110,24 @@ export type CompositeState<T extends Record<string, AnyProjection>> = {
110
110
  readonly [K in keyof T]: StateOf<T[K]>
111
111
  }
112
112
 
113
- /** Merge two queries by OR-ing their items; either wildcard yields a wildcard. */
114
- const mergeQuery = (left: Query, right: Query): Query => {
115
- if (left.items.length === 0 || right.items.length === 0) {
116
- return WILDCARD
117
- }
118
- return { items: [...left.items, ...right.items] }
119
- }
113
+ /**
114
+ * Merge two queries by OR-ing their items; either wildcard yields a wildcard.
115
+ *
116
+ * Every other key on either envelope is carried into the result. The grammar's
117
+ * envelope has no key but `items`, so anything else is a key the store boundary
118
+ * refuses (`servableQueryFault`); a merge that rebuilt the envelope from `items`
119
+ * alone would drop that key before the boundary saw it, and a slice composed with
120
+ * another would then have its smuggled predicate ignored in silence — the outcome
121
+ * the refusal exists to prevent.
122
+ */
123
+ const mergeQuery = (left: Query, right: Query): Query => ({
124
+ ...left,
125
+ ...right,
126
+ items:
127
+ left.items.length === 0 || right.items.length === 0
128
+ ? WILDCARD.items
129
+ : [...left.items, ...right.items],
130
+ })
120
131
 
121
132
  /**
122
133
  * OR-merge a list of queries into one. The empty list is the wildcard.
package/src/Query.ts CHANGED
@@ -79,6 +79,9 @@ export const matchesSequenced = (
79
79
  * (`Query.ts` module docs) but which plain-data assembly — e.g. decision-model
80
80
  * composition, `Projection.ts:31-36` — can still bypass.
81
81
  *
82
+ * A query, or any of its items, carrying a key the grammar does not define is
83
+ * refused before anything else is judged (`unknownKeyFault` below says why).
84
+ *
82
85
  * The servable grammar is exactly one of:
83
86
  * - the wildcard (`items.length === 0`), which matches every event;
84
87
  * - a *single* item (`items.length === 1`), any types/tags, including a
@@ -105,6 +108,72 @@ export const assertServableQuery = (query: Query): void => {
105
108
  }
106
109
  }
107
110
 
111
+ /**
112
+ * The refusal of a key `schemaFields` does not define, or `undefined` when `value`
113
+ * carries none. `where` names the level for the message.
114
+ *
115
+ * ## Why an unknown key is refused rather than ignored
116
+ *
117
+ * A `Query` is plain data, so nothing at runtime stops a caller adding a key the
118
+ * grammar does not have — a `metadata` predicate on an item is the one this repo
119
+ * expects, since metadata is the field an author most plausibly wants to filter on
120
+ * and must not (ADR-0017). Ignored, that key is a predicate the author believes they
121
+ * wrote and no engine honours, and on the append side it is worse: the Postgres
122
+ * guard embeds its match as literal plpgsql text that no type reaches, so a key a
123
+ * read path one day learned to honour would still be dropped by the guard, and the
124
+ * optimistic-concurrency check would escape in silence. Refusing every key the
125
+ * grammar does not define closes that route — a SMUGGLED key — at the one boundary
126
+ * every engine shares, before any engine could disagree. "Every key" means every
127
+ * key `Object.keys` returns: an own, enumerable, string key, which is what a plain
128
+ * object literal or a spread builds. A symbol key, a non-enumerable key or an
129
+ * inherited one is not judged, and no engine reads one either. Core's own query
130
+ * merge (`unionQueries`, `composeProjections`) carries an envelope's unknown key
131
+ * into the merged query rather than dropping it, so composing slices cannot launder
132
+ * a smuggled key past this check.
133
+ *
134
+ * It does not close the neighbouring one, and cannot: a field ADDED to the grammar's
135
+ * schema is a known key from that edit on and passes here, while the plpgsql guard
136
+ * still has to be taught it by hand. That exposure is the Postgres guard's, and its
137
+ * own comment in that engine's DDL says so.
138
+ *
139
+ * The known keys are read off the SCHEMA's own `fields` rather than listed here, so
140
+ * a field added to the grammar is admitted by the same edit that adds it. Only key
141
+ * NAMES are judged: an exact-optional key written as `undefined` is a known key and
142
+ * passes, because which values a key admits is the type's business, not this rule's.
143
+ */
144
+ const unknownKeyFault = (
145
+ where: string,
146
+ value: object,
147
+ schemaFields: object,
148
+ ): string | undefined => {
149
+ const unknown = Object.keys(value).find(
150
+ (key) => !Object.hasOwn(schemaFields, key),
151
+ )
152
+ return unknown === undefined
153
+ ? undefined
154
+ : `assertServableQuery: ${where} carries the key ${JSON.stringify(unknown)}, which ` +
155
+ 'the query grammar does not define; an unknown key would be silently ignored ' +
156
+ 'by every engine rather than honoured, so it is refused'
157
+ }
158
+
159
+ /**
160
+ * The unknown-key half of `servableQueryFault`, on its own: the refusal of a key the
161
+ * grammar does not define on the envelope or on any item, or `undefined`.
162
+ *
163
+ * It is the same rule and not a second statement of it — `servableQueryFault` calls
164
+ * this first. It is exported because ONE caller has to tell the two halves apart:
165
+ * the codec's read ledger refuses an unservable SHAPE on its own error channel, and
166
+ * must not report an unknown key as one. An unknown key is not something a slice's
167
+ * tags can fix, so the ledger leaves it to the store, which refuses it as a defect.
168
+ */
169
+ export const unknownQueryKeyFault = (query: Query): string | undefined =>
170
+ unknownKeyFault('a query', query, Query.fields) ??
171
+ query.items.reduce<string | undefined>(
172
+ (fault, item) =>
173
+ fault ?? unknownKeyFault('a query item', item, QueryItem.fields),
174
+ undefined,
175
+ )
176
+
108
177
  /**
109
178
  * The servable grammar as a TOTAL function: the reason `query` cannot be served,
110
179
  * or `undefined` when it can.
@@ -126,6 +195,10 @@ export const assertServableQuery = (query: Query): void => {
126
195
  * reason), so a fault is one exit route and one return type.
127
196
  */
128
197
  export const servableQueryFault = (query: Query): string | undefined => {
198
+ const unknownKey = unknownQueryKeyFault(query)
199
+ if (unknownKey !== undefined) {
200
+ return unknownKey
201
+ }
129
202
  const { items } = query
130
203
  // Wildcard and single-item queries are always servable.
131
204
  if (items.length <= 1) {