@mulmoclaude/core 3.11.0 → 3.13.0

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.
@@ -4,6 +4,7 @@ import { n as toPosixRelPath } from "./relPath-DW8MC8VO.js";
4
4
  import { B as fieldText, I as embedTargetId, L as isFieldDrivenEvery, N as COMPUTED_TYPES, V as fieldTextOrNull, d as parseIsoDate, f as parseIsoDateTime, x as isValidCollectionName, z as storageKindFor } from "./itemId-DfTm08jm.js";
5
5
  import { C as actionVisible, _ as uniqueRefTargets, b as projectBacklinkRow, c as selectDynamicRecord, f as itemIsDone, g as uniqueEmbedTargets, h as uniqueBacklinkSources, i as ownProp, n as deriveAll, o as firstDateField, s as resolveIcon, t as defangForPrompt, v as backlinkRows, x as rollupValue, y as coerceNumeric } from "./promptSafety-CSdyp7RF.js";
6
6
  import { B as SCHEMA_FILE$1, C as CollectionQueryZ, I as parseAppManifest, J as isBackendUnavailable, K as safeSlugName, O as isRegularFile, S as compileJsonlQuery, U as resolveDataDir, V as isContainedInRoot, W as resolveTemplatePath, X as archiveDir, b as queryCsv, c as resolveMutateSet, d as storeFor, f as checkpointSqliteDatabase, ht as stagingSkillDir, i as resolvePrimaryField, it as isPresetSlug$1, j as resolveCreateItemId, m as cacheDir, n as discoverCollections, nt as getWorkspaceRoot, ot as log, r as loadCollection, s as CollectionSchemaZ, u as readOnlyRefusal, y as normalizeCsvValue } from "./discovery-B9zdNkgW.js";
7
+ import { r as isSafeCustomViewPath } from "./templatePath-k_WNbL_Q.js";
7
8
  import { ingestStatePath } from "./feeds/paths.js";
8
9
  import { mirrorSkillWrite } from "./skill-bridge/index.js";
9
10
  import path from "node:path";
@@ -19,13 +20,30 @@ var NameZ = z.string().refine(isValidCollectionName, { message: "is not a valid
19
20
  * the rules compare it to `request.auth.token.email` verbatim, so any
20
21
  * narrowing here would refuse addresses Firebase itself accepts. */
21
22
  var EmailZ = z.string().trim().min(3).includes("@");
22
- /** The four roles the deployed rules understand. `participant` is the layer
23
- * that is NAMED but reads only its own rows — see `readerOf` vs `listedIn`. */
23
+ /** The roles the deployed rules understand.
24
+ *
25
+ * Two of them are row-scoped, in opposite directions, and the pair is what
26
+ * the four-way split could not express:
27
+ *
28
+ * `participant` — the layer that is NAMED but reads only its OWN rows (the
29
+ * rows it submitted). See `readerOf` vs `listedIn`.
30
+ *
31
+ * `assignee` — reads EVERY row and writes only the rows ASSIGNED to it. The
32
+ * stylist who approves their own bookings and not a colleague's; the marker
33
+ * who grades their own students. Which rows are theirs is
34
+ * `collections[cid].assigneeField`, a field on the record holding the
35
+ * member's address. Reads are deliberately unscoped: a stylist needs the
36
+ * whole day's schedule, and scoping the read makes the app unusable.
37
+ *
38
+ * The names are permanent. The deployed rules compare these strings directly
39
+ * and they are written into `app.json` files people commit, so a rename is a
40
+ * migration over published apps rather than an edit. */
24
41
  var APP_ROLES = [
25
42
  "owner",
26
43
  "editor",
27
44
  "viewer",
28
- "participant"
45
+ "participant",
46
+ "assignee"
29
47
  ];
30
48
  var RoleZ = z.enum(APP_ROLES);
31
49
  /** `{ email: { "*" | cid: role } }`. The `"*"` key is the app-wide role; a
@@ -51,6 +69,29 @@ var CollectionConfigZ = z.object({
51
69
  transitions: z.record(z.string().trim().min(1), z.array(z.string().trim().min(1))).optional(),
52
70
  immutable: z.boolean().optional(),
53
71
  submitOnly: z.boolean().optional(),
72
+ /** The field naming the member a row belongs to, for the `assignee` role.
73
+ *
74
+ * Holds an ADDRESS, because that is the only thing the rules can compare
75
+ * a member against (`request.auth.token.email`). A `ref` to a staff
76
+ * collection stores the target's primary-key slug, not an address, so it
77
+ * cannot be this field — declare a plain field beside the ref and let the
78
+ * ref stay the thing the UI renders. The alternative, having the rules
79
+ * `get()` the staff record to read an address off it, costs a document
80
+ * access on every write and puts a second document between an
81
+ * authorization decision and its answer.
82
+ *
83
+ * Only meaningful with a member holding `assignee` on this cid; a
84
+ * declaration with the role and no field is refused (`assigneeProblems`),
85
+ * because that member would silently hold nothing. */
86
+ assigneeField: z.string().trim().min(1).optional(),
87
+ /** This collection is the public projection of `mirrorOf` — the other
88
+ * half of `public.submit[...].mirror`, declared here because the rules
89
+ * read it when the PROJECTION is written rather than when the record is.
90
+ *
91
+ * What it buys: `state` may be written by anybody, and only to the value
92
+ * the authority actually says, so a visitor who was refused a slot can
93
+ * repair the stale row that offered it to them. */
94
+ mirrorOf: NameZ.optional(),
54
95
  peerVisibility: z.enum(["public", "hidden"]).optional(),
55
96
  revealGated: z.boolean().optional(),
56
97
  gatedFrom: NameZ.optional(),
@@ -69,9 +110,60 @@ var CollectionConfigZ = z.object({
69
110
  * the rules do not coerce strings, so an ISO string reaching Firestore is a
70
111
  * type error that fails CLOSED (`inWindow` refuses every submission and the
71
112
  * author sees "nobody can submit", not an error). */
113
+ /** A window bound that lives on ANOTHER record, read at write time.
114
+ *
115
+ * `window.from` is one absolute instant for the whole collection, which is
116
+ * enough for a survey and useless for anything recurring: "each class opens
117
+ * three days before it starts, at 08:00" is a bound PER RECORD. So the bound
118
+ * is not computed in the rules — they have no usable date arithmetic and
119
+ * `request.time` is UTC, which is the wrong answer for "08:00" — it is
120
+ * computed by whoever schedules the class, stored on the class record as
121
+ * epoch millis, and merely COMPARED here.
122
+ *
123
+ * `ref` is the field on the record being written that names the target
124
+ * (`classId`); `collection` is the cid the target lives in, fixed in the
125
+ * declaration so that a path is never built out of a value a submitter wrote;
126
+ * `field` is the epoch-millis field on the target.
127
+ *
128
+ * Not spelled `in` — that is an operator in the rules language, and
129
+ * `w.fromField.in` does not parse there. */
130
+ var WindowRefZ = z.object({
131
+ ref: z.string().trim().min(1),
132
+ collection: NameZ,
133
+ field: z.string().trim().min(1)
134
+ }).strict();
135
+ /** The closing bound's per-record twin, and it ships WITH `fromField` rather
136
+ * than as a symmetric extra: a booking desk that opens per slot and never
137
+ * closes is not a booking desk. Same shape, opposite comparison — and
138
+ * EXCLUSIVE where `fromField` is inclusive, so one slot's closing instant and
139
+ * the next one's opening instant may be the same number. */
72
140
  var WindowZ = z.object({
73
141
  from: z.iso.datetime().optional(),
74
- until: z.iso.datetime().optional()
142
+ until: z.iso.datetime().optional(),
143
+ fromField: WindowRefZ.optional(),
144
+ untilField: WindowRefZ.optional()
145
+ }).strict();
146
+ /** Which record a `field` document id must name, and what state it must be in.
147
+ *
148
+ * `idFrom: "field"` alone only stops the same string being written twice —
149
+ * nothing stops a client bypassing the page and inventing a slot, so the
150
+ * rules check the referenced record themselves. `exists()` is a FLOOR: a
151
+ * cancelled slot and a slot nobody may book any more exist too, which is what
152
+ * `where` is for.
153
+ *
154
+ * Always the object form, never a bare collection name. Two shapes for one
155
+ * key is the kind of thing a generator gets right once and wrong afterwards,
156
+ * and the rules read `s.idIn.collection` either way. */
157
+ var IdInZ = z.object({
158
+ collection: NameZ,
159
+ where: z.object({
160
+ field: z.string().trim().min(1),
161
+ equals: z.union([
162
+ z.string(),
163
+ z.number(),
164
+ z.boolean()
165
+ ])
166
+ }).strict().optional()
75
167
  }).strict();
76
168
  var ValidateZ = z.object({
77
169
  required: z.array(z.string().trim().min(1)).optional(),
@@ -96,14 +188,46 @@ var SubmitZ = z.object({
96
188
  emailField: z.string().trim().min(1).optional(),
97
189
  createFields: z.array(z.string().trim().min(1)).min(1),
98
190
  initialStatus: z.string().trim().min(1).optional(),
191
+ /** `field` is the mode that makes a CONTESTED resource exclusive: the
192
+ * booking's document id IS the slot's id, so the second person to want
193
+ * that slot is writing a document that already exists — an update, which
194
+ * the public submission path never allows. Firestore decides that
195
+ * atomically, so unlike a countable capacity (see `stampField`) this is
196
+ * first-come ENFORCED rather than first-come read off a rank. */
99
197
  idFrom: z.enum([
100
198
  "auto",
101
199
  "auth.uid",
102
- "auth.uid+field"
200
+ "auth.uid+field",
201
+ "field"
103
202
  ]).optional(),
104
203
  idField: z.string().trim().min(1).optional(),
204
+ /** Required by `idFrom: "field"` — see {@link IdInZ}. */
205
+ idIn: IdInZ.optional(),
206
+ /** The collection holding this record's PUBLIC PROJECTION, one row per
207
+ * contested thing, sharing its document id.
208
+ *
209
+ * A booking carries a name, an address and a phone number, and Firestore
210
+ * rules cannot hide a field, so the public page must not read bookings at
211
+ * all. It reads the projection instead, whose `state` is a copy of "does
212
+ * a booking with this id exist" — and the rules accept the two writes
213
+ * only as one batch, in both directions, so the copy cannot drift into
214
+ * advertising a slot that is gone. */
215
+ mirror: NameZ.optional(),
105
216
  validate: ValidateZ.optional(),
106
217
  window: WindowZ.optional(),
218
+ /** A field the rules PIN to the server clock on create: the record must
219
+ * carry `request.time` in it, and may never change it afterwards.
220
+ *
221
+ * What it buys is an order nobody can jump. A first-come app takes its
222
+ * capacity from rank rather than from a count — the rules cannot count
223
+ * documents, so "the first 8" can only ever be a reading of the rows —
224
+ * and a rank is only as honest as the timestamp it sorts by. `idFrom`
225
+ * stops a person holding two places; nothing else stops them writing
226
+ * yesterday's date into the field that decides who got there first.
227
+ *
228
+ * Binds EVERY create, the writer branch included, so a staff-entered row
229
+ * cannot be back-dated into the queue either. */
230
+ stampField: z.string().trim().min(1).optional(),
107
231
  /** Per CURRENT STATUS, never a flat list: a flat list lets a customer move
108
232
  * an approved booking's `startAt` without anyone re-approving it. */
109
233
  selfUpdate: z.record(z.string().trim().min(1), z.array(z.string().trim().min(1))).optional(),
@@ -120,6 +244,24 @@ var PublicZ = z.object({
120
244
  * well as its own declaration. */
121
245
  enabled: z.boolean().optional(),
122
246
  read: z.array(NameZ).optional(),
247
+ /** The page the public sees, instead of the generated form.
248
+ *
249
+ * A form is enough to ANSWER something and not enough to CHOOSE from
250
+ * what is available — a stylist-by-hour grid is not the far end of a
251
+ * table. So the app may name one HTML file, which the host publishes to
252
+ * `config/view` and the public page renders in a sandboxed iframe.
253
+ *
254
+ * `submit` stays declared alongside: the view sends an INTENT, and the
255
+ * page it is embedded in performs the write against these rules.
256
+ *
257
+ * `collections` is declared rather than inferred from `read`. Inferring
258
+ * it produces the worst failure this feature has — the view renders, the
259
+ * data it wanted was never sent, and it draws an empty grid with no error
260
+ * anywhere. */
261
+ view: z.object({
262
+ path: z.string().trim().min(1),
263
+ collections: z.array(NameZ).min(1)
264
+ }).strict().optional(),
123
265
  submit: z.record(NameZ, SubmitZ).optional()
124
266
  }).strict();
125
267
  /** The URL name an app is handed out under: `https://<host>/{slug}`.
@@ -224,7 +366,12 @@ function windowMillis(window) {
224
366
  if (window.from !== void 0) out.fromMs = Date.parse(window.from);
225
367
  if (window.until !== void 0) out.untilMs = Date.parse(window.until);
226
368
  if (Object.values(out).some((value) => !Number.isFinite(value))) throw new Error(`publish: window bound is not a parseable timestamp (${JSON.stringify(window)})`);
227
- return Object.keys(out).length > 0 ? out : void 0;
369
+ const projected = {
370
+ ...out,
371
+ ...window.fromField === void 0 ? {} : { fromField: window.fromField },
372
+ ...window.untilField === void 0 ? {} : { untilField: window.untilField }
373
+ };
374
+ return Object.keys(projected).length > 0 ? projected : void 0;
228
375
  }
229
376
  /** One `public.submit[cid]`, with its window lowered. Everything else passes
230
377
  * through: the rules read these keys by the names the author wrote. */
@@ -298,6 +445,7 @@ function projectApp(authored, schemas, stamp, existing) {
298
445
  enabled: authored.public?.enabled === true,
299
446
  read: authored.public?.read ?? [],
300
447
  submit,
448
+ ...authored.public?.view === void 0 ? {} : { view: { collections: authored.public.view.collections } },
301
449
  publishedAt: stamp.publishedAt
302
450
  };
303
451
  if (authored.name !== void 0) config.name = authored.name;
@@ -595,7 +743,7 @@ function ruleReadFields(submit) {
595
743
  field: submit.emailField,
596
744
  why: `public.submit.<cid>.emailField — the rules compare it to the submitter's verified address`
597
745
  });
598
- if (submit.idFrom === "auth.uid+field" && submit.idField !== void 0) fields.push({
746
+ if ((submit.idFrom === "auth.uid+field" || submit.idFrom === "field") && submit.idField !== void 0) fields.push({
599
747
  field: submit.idField,
600
748
  why: `public.submit.<cid>.idField — the rules rebuild the document id from it`
601
749
  });
@@ -618,10 +766,58 @@ function submitCoherenceProblems(app, cid, submit) {
618
766
  const collection = app.collections?.[cid];
619
767
  const problems = [...statusCoherenceProblems(cid, submit, collection), ...createFieldProblems(cid, submit)];
620
768
  if (submit.idFrom === "auth.uid+field" && submit.idField === void 0) problems.push(`public.submit.${cid}.idFrom is "auth.uid+field" but no idField is declared: the rules rebuild the document id from that field and refuse every create.`);
769
+ problems.push(...fieldIdProblems(cid, submit));
621
770
  if ((submit.selfUpdate !== void 0 || submit.selfTransitions !== void 0) && !collection?.statusField) problems.push(`public.submit.${cid}.selfUpdate / selfTransitions are declared per CURRENT STATUS, but collections.${cid} declares no statusField: the rules read the current status first and refuse every self-edit without it.`);
622
771
  if (submit.audience === "participant" && Object.keys(app.members).length === 0) problems.push(`public.submit.${cid}.audience is "participant" but the roster is empty: the rules resolve the submitter's role from members, so every submission is refused.`);
623
772
  return problems;
624
773
  }
774
+ /** `idFrom: "field"` makes the document id a CLAIM ABOUT ANOTHER RECORD, and
775
+ * the claim is only worth what is checked.
776
+ *
777
+ * `idIn` is required rather than optional, and that is the whole point of
778
+ * refusing here: without it the id is any string a stranger likes, so the app
779
+ * quietly accepts bookings for slots that do not exist. Nothing downstream
780
+ * ever notices — the booking is real, its slot is not — which is exactly the
781
+ * kind of hole a gate is for and a rule cannot state.
782
+ *
783
+ * `idIn` without the mode is refused for the opposite reason: the rules read
784
+ * it only in that branch, so an author who wrote it believes a check is
785
+ * running that is not. */
786
+ function fieldIdProblems(cid, submit) {
787
+ const problems = [];
788
+ if (submit.idFrom === "field") {
789
+ if (submit.idField === void 0) problems.push(`public.submit.${cid}.idFrom is "field" but no idField is declared: the rules take the document id from that field and refuse every create.`);
790
+ if (submit.idIn === void 0) problems.push(`public.submit.${cid}.idFrom is "field" but no idIn is declared: the document id is then any string a submitter chooses, so the app accepts records pointing at things that do not exist. Name the collection the id must be found in — and, when only some of those records may be claimed, the state they must be in: "idIn": { "collection": "slots", "where": { "field": "state", "equals": "open" } }.`);
791
+ } else if (submit.idIn !== void 0) {
792
+ const mode = submit.idFrom === void 0 ? "absent" : JSON.stringify(submit.idFrom);
793
+ problems.push(`public.submit.${cid}.idIn is declared but idFrom is ${mode}: the rules read idIn only for idFrom "field", so as written nothing checks the referenced record and the declaration promises a check it does not perform.`);
794
+ }
795
+ return problems;
796
+ }
797
+ /** Every `idIn` target, checked against the collections this repository has.
798
+ *
799
+ * Separate from {@link fieldIdProblems} for the reason the file is split at
800
+ * all: that one reads the declaration alone, this one needs to know what
801
+ * exists. */
802
+ function idTargetProblems(app, collections) {
803
+ const known = new Set(collections.map((collection) => collection.cid));
804
+ const names = known.size > 0 ? [...known].sort().join(", ") : "(none)";
805
+ return Object.entries(app.public?.submit ?? {}).flatMap(([cid, submit]) => idInTargetProblems(cid, submit, known, names));
806
+ }
807
+ /** Where a `field` id says its record must be found.
808
+ *
809
+ * A typo passes every other check: the rules look the record up in a
810
+ * collection that does not exist, the lookup can never succeed, and every
811
+ * submission is refused with no explanation anywhere. A collection pointing
812
+ * at ITSELF is worse than a typo — on a create the document being written
813
+ * does not exist yet, so it is a declaration that can never accept anything. */
814
+ function idInTargetProblems(cid, submit, known, names) {
815
+ const target = submit.idIn?.collection;
816
+ if (target === void 0) return [];
817
+ if (target === cid) return [`public.submit.${cid}.idIn.collection names '${cid}' itself: a create writes a document that does not exist yet, so the record can never be found and every submission is refused. Name the collection of the thing being claimed (the slots, the seats, the assets).`];
818
+ if (!known.has(target)) return [`public.submit.${cid}.idIn.collection names '${target}', which is not a shared collection in this repository. The rules look the record up there, so nothing can ever be submitted. Shared collections here: ${names}.`];
819
+ return [];
820
+ }
625
821
  /** The staged reveal reads its flag off the PARENT record, so the path to that
626
822
  * parent is not optional decoration — without it the gate never opens. */
627
823
  function gateCoherenceProblems(cid, collection) {
@@ -651,7 +847,8 @@ function unknownCidProblems(app, collections) {
651
847
  ["collections", Object.keys(app.collections ?? {})],
652
848
  ["public.read", app.public?.read ?? []],
653
849
  ["public.submit", Object.keys(app.public?.submit ?? {})],
654
- ["participantRead", app.participantRead ?? []]
850
+ ["participantRead", app.participantRead ?? []],
851
+ ["members", [...new Set(Object.values(app.members).flatMap((roles) => Object.keys(roles)))].filter((key) => key !== "*")]
655
852
  ].flatMap(([where, cids]) => cids.filter((cid) => !known.has(cid)).map((cid) => `${where} names '${cid}', which is not a shared collection in this repository. Shared collections here: ${known.size > 0 ? [...known].sort().join(", ") : "(none - a schema needs storage.type \"firestore\")"}.`));
656
853
  }
657
854
  /** Everything publish refuses, as lines the author can act on.
@@ -668,7 +865,13 @@ function publishProblems(app, collections, publisherEmail) {
668
865
  ...mailProblems(app),
669
866
  ...submitShapeProblems(app),
670
867
  ...coherenceProblems(app),
671
- ...primaryKeyProblems(app, collections)
868
+ ...primaryKeyProblems(app, collections),
869
+ ...assigneeProblems(app),
870
+ ...stampProblems(app),
871
+ ...windowRefProblems(app, collections),
872
+ ...idTargetProblems(app, collections),
873
+ ...mirrorProblems(app, collections),
874
+ ...publicViewProblems(app, collections)
672
875
  ];
673
876
  }
674
877
  /** A public submission must NOT be allowed to name its own primary key.
@@ -698,6 +901,293 @@ function primaryKeyProblems(app, collections) {
698
901
  return [`public.submit.${cid}.createFields must NOT include "${primaryKey}", the schema's primaryKey: the rules can pin the document id but not the value of a field, so a submitter could write at their own id while claiming another record's. A shared record's identity is its document id — the store fills the field from it, and a submitted value is either the same thing or a lie that is thrown away.`];
699
902
  });
700
903
  }
904
+ /** `assignee` without the field that says which rows are theirs.
905
+ *
906
+ * A FAIL-CLOSED trap of the worst kind, because it fails closed for one
907
+ * person and nobody else: the rules ask `collections[cid].assigneeField` for
908
+ * the field to compare, find nothing, and refuse every write that member
909
+ * makes. The app works for the owner who set it up, and the member it was set
910
+ * up for is told only "permission denied".
911
+ *
912
+ * `'*': "assignee"` is refused outright rather than checked against every
913
+ * collection. The role means "the rows assigned to you", and what counts as
914
+ * assigned is per collection — an app-wide one would need the same field name
915
+ * to be right everywhere, and where it is missing it silently means "no
916
+ * access to this collection" rather than "no scoping here".
917
+ */
918
+ function assigneeProblems(app) {
919
+ return Object.entries(app.members).flatMap(([email, roles]) => Object.entries(roles).flatMap(([cid, role]) => {
920
+ if (role !== "assignee") return [];
921
+ if (cid === "*") return [`members["${email}"] holds "assignee" under "*", and the role cannot be app-wide: which rows are yours is declared per collection (\`collections.<cid>.assigneeField\`). Name the collections instead — { "bookings": "assignee" }.`];
922
+ if (app.collections?.[cid]?.assigneeField !== void 0) return [];
923
+ return [`members["${email}"] holds "assignee" on '${cid}', but collections.${cid}.assigneeField does not say which field names the member a row belongs to. Add it (assigneeField: "<a field holding an address>"), or give a role that is not row-scoped. Without it the rules have nothing to compare and refuse every write that member makes, while the app keeps working for everybody else.`];
924
+ }));
925
+ }
926
+ /** A server-stamped field the submitter cannot write, or can rewrite later.
927
+ *
928
+ * Both failures are silent in opposite directions. Left out of
929
+ * `createFields`, the rules refuse every submission (`hasOnly(createFields)`
930
+ * rejects the key the stamp check requires) — an app nobody can use. Left IN
931
+ * a `selfUpdate` list, the field the queue is ordered by becomes editable by
932
+ * the person standing in the queue. */
933
+ function stampProblems(app) {
934
+ return Object.entries(app.public?.submit ?? {}).flatMap(([cid, submit]) => {
935
+ const stamp = submit.stampField;
936
+ if (stamp === void 0) return [];
937
+ const problems = [];
938
+ if (!submit.createFields.includes(stamp)) problems.push(`public.submit.${cid}.stampField names '${stamp}', which is not in createFields. The rules require the record to CARRY the server time in that field, and refuse any key outside createFields — so every submission is denied. Add it to createFields; the page fills it in, not the person.`);
939
+ for (const [status, fields] of Object.entries(submit.selfUpdate ?? {})) {
940
+ if (!fields.includes(stamp)) continue;
941
+ problems.push(`public.submit.${cid}.selfUpdate.${status} lets the submitter write '${stamp}', which is the field stampField pins to the server clock. Whatever that field orders — a first-come queue, an audit trail — could then be rewritten by the person it ranks. Remove it from selfUpdate.`);
942
+ }
943
+ return problems;
944
+ });
945
+ }
946
+ /** A per-record window bound pointing at a collection or a field the submitter
947
+ * never writes.
948
+ *
949
+ * `fromField` makes the rules read another record, and every part of that
950
+ * read is fail-closed: an unknown collection, or a `ref` the submission does
951
+ * not carry, means the bound can never be satisfied and the form is shut for
952
+ * good. */
953
+ function windowRefProblems(app, collections) {
954
+ const known = new Set(collections.map((collection) => collection.cid));
955
+ return Object.entries(app.public?.submit ?? {}).flatMap(([cid, submit]) => [...windowBoundProblems(cid, submit, known, "fromField", submit.window?.fromField, "opening"), ...windowBoundProblems(cid, submit, known, "untilField", submit.window?.untilField, "closing")]);
956
+ }
957
+ /** Both bounds, checked identically. `untilField` arrived with the booking
958
+ * desk and reads exactly like its twin, so a check that knew only about
959
+ * `fromField` would let the closing half through unchecked — and a closing
960
+ * bound that names nothing does not leave the door ajar, it refuses every
961
+ * submission with no explanation. */
962
+ function windowBoundProblems(cid, submit, known, key, ref, which) {
963
+ if (ref === void 0) return [];
964
+ const problems = [];
965
+ if (!known.has(ref.collection)) problems.push(`public.submit.${cid}.window.${key}.collection names '${ref.collection}', which is not a shared collection in this repository. The rules read the ${which} time off a record there, so nothing can ever be submitted. Shared collections here: ${known.size > 0 ? [...known].sort().join(", ") : "(none)"}.`);
966
+ if (!submit.createFields.includes(ref.ref)) problems.push(`public.submit.${cid}.window.${key}.ref names '${ref.ref}', which is not in createFields. The rules take the target record's id from that field ON THE SUBMISSION — if the submitter never writes it, there is nothing to look up and every submission is refused.`);
967
+ return problems;
968
+ }
969
+ /** The two halves of a mirror, checked as the pair they only work as.
970
+ *
971
+ * `mirror` on the submission and `mirrorOf` on the projection are separate
972
+ * keys in separate places, and each is inert without the other: a booking
973
+ * whose slot declares no `mirrorOf` can never be created (the rules demand a
974
+ * paired write that the projection's own rule will refuse), and a projection
975
+ * whose authority declares no `mirror` drifts unbounded because nothing makes
976
+ * the two move together. Both failures are silent, and one of them —
977
+ * advertising a slot somebody already holds — is the exact thing the mirror
978
+ * exists to prevent.
979
+ *
980
+ * Also refuses a collection mirroring ITSELF, which reads as a typo and
981
+ * behaves as an unwritable collection: every create would have to prove its
982
+ * own document is simultaneously taken and open. */
983
+ function mirrorProblems(app, collections) {
984
+ const known = new Set(collections.map((collection) => collection.cid));
985
+ const names = known.size > 0 ? [...known].sort().join(", ") : "(none)";
986
+ return [...Object.entries(app.public?.submit ?? {}).flatMap(([cid, submit]) => mirrorClaimProblems(app, cid, submit, known, names)), ...Object.entries(app.collections ?? {}).flatMap(([cid, collection]) => mirrorOfProblems(app, cid, collection, known, names))];
987
+ }
988
+ /** The submission side: `public.submit[cid].mirror`. */
989
+ function mirrorClaimProblems(app, cid, submit, known, names) {
990
+ const { mirror } = submit;
991
+ if (mirror === void 0) return [];
992
+ if (mirror === cid) return [`public.submit.${cid}.mirror names its own collection: the projection is a SEPARATE record, and as written no create can satisfy the rules.`];
993
+ if (!known.has(mirror)) return [`public.submit.${cid}.mirror names '${mirror}', which is not a shared collection in this repository. The rules require the projection to move in the same write, so every submission is refused. Shared collections here: ${names}.`];
994
+ if (app.collections?.[mirror]?.mirrorOf !== cid) return [`public.submit.${cid}.mirror names '${mirror}', but collections.${mirror} does not declare mirrorOf: "${cid}". The two halves only work as a pair — the submission side demands the projection move with it, and the projection side is what allows that move — so as written every submission is refused.`];
995
+ return [];
996
+ }
997
+ /** The projection side: `collections[cid].mirrorOf`. */
998
+ function mirrorOfProblems(app, cid, collection, known, names) {
999
+ const authority = collection.mirrorOf;
1000
+ if (authority === void 0) return [];
1001
+ if (!known.has(authority)) return [`collections.${cid}.mirrorOf names '${authority}', which is not a shared collection in this repository. Nothing can then be true of it, so the projection's state may never be written. Shared collections here: ${names}.`];
1002
+ if (app.public?.submit?.[authority]?.mirror !== cid) return [`collections.${cid}.mirrorOf names '${authority}', but public.submit.${authority} does not declare mirror: "${cid}". Only the pair keeps the projection honest: without the other half a record can be created without moving this one, and the public page goes on offering something that is already taken.`];
1003
+ return [];
1004
+ }
1005
+ /** What the public view is handed. Declared, never inferred — a view whose
1006
+ * datasets were guessed from `public.read` renders perfectly and draws an
1007
+ * empty grid, with nothing in the page, the rules or the log to say why. */
1008
+ function publicViewProblems(app, collections) {
1009
+ const view = app.public?.view;
1010
+ if (view === void 0) return [];
1011
+ const known = new Set(collections.map((collection) => collection.cid));
1012
+ const readable = new Set(app.public?.read ?? []);
1013
+ const problems = [];
1014
+ if (!isSafeCustomViewPath(view.path) || view.path.split("/").length !== 2) problems.push(`public.view.path is '${view.path}': a published view is exactly one HTML file directly inside the collection's own views/ directory (e.g. views/booking.html) — no sub-directories, and no segments that climb out of it. The host reads this as a file to publish, and what it publishes is world-readable.`);
1015
+ for (const cid of view.collections) {
1016
+ if (!known.has(cid)) {
1017
+ problems.push(`public.view.collections names '${cid}', which is not a shared collection in this repository. Shared collections here: ${known.size > 0 ? [...known].sort().join(", ") : "(none)"}.`);
1018
+ continue;
1019
+ }
1020
+ if (!readable.has(cid)) problems.push(`public.view.collections names '${cid}', which is not in public.read: the page reads these with the VISITOR's permissions, so the rules refuse the read and the view draws an empty page. Nothing errors — this is the failure that looks like a working view with no data.`);
1021
+ }
1022
+ return problems;
1023
+ }
1024
+ /** What publish will actually promote, checked as the PAIR it becomes.
1025
+ *
1026
+ * `publishProblems` reads the manifest, where `members` and `collections` sit
1027
+ * side by side and agree. Publish does not write that pair. It writes the
1028
+ * roster from the manifest and the collection configuration from what DEPLOY
1029
+ * staged, so the app that lands is one half of each — and no check has ever
1030
+ * looked at that combination.
1031
+ *
1032
+ * The sequence that gets through: deploy revision A with no `assigneeField`,
1033
+ * add the field AND the member in revision B, publish without redeploying.
1034
+ * Every manifest-level check passes on a declaration that is internally sound,
1035
+ * while what lands is A's field-less configuration beside B's roster — an
1036
+ * assignee with nothing to be compared against, refused every write, in an app
1037
+ * that keeps working for everybody else. That is the precise trap
1038
+ * `assigneeProblems` exists to prevent, reached by the one route it cannot
1039
+ * see.
1040
+ *
1041
+ * Separate from `publishProblems` because it needs what deploy staged, which
1042
+ * is a Firestore read the host makes and this package does not. It is checked
1043
+ * against `stagedRuleConfig` — the same function the projection uses — rather
1044
+ * than against a re-derivation, so the value validated is the value written.
1045
+ *
1046
+ * Only the staged half can be stale, so only that half is named and the fix is
1047
+ * "deploy again" rather than "fix the declaration". */
1048
+ function promotedRoleProblems(app, staged) {
1049
+ const promoted = stagedRuleConfig(staged).collections ?? {};
1050
+ const stagedCids = new Set(staged.map((entry) => entry.cid));
1051
+ return [
1052
+ ...promotedAssigneeProblems(app, promoted, stagedCids),
1053
+ ...promotedMirrorProblems(app, promoted, stagedCids),
1054
+ ...promotedRefFieldProblems(app, staged)
1055
+ ];
1056
+ }
1057
+ /** The FIELDS a rule reads off another record — `idIn.where.field` and the two
1058
+ * window bounds — checked against the schema publish is about to promote.
1059
+ *
1060
+ * These are checked here rather than in `publishProblems` because that gate
1061
+ * is given a cid and a primary key per collection and nothing else, on
1062
+ * purpose: it reads the DECLARATION. A field name can only be judged against
1063
+ * a schema, and the schema that matters is the STAGED one — the version
1064
+ * publish promotes — not whatever the working tree says now.
1065
+ *
1066
+ * What a typo costs: `where: { field: "staet" }` publishes cleanly, the
1067
+ * rules' comparison can never match, and every submission is denied with no
1068
+ * message. The author's own app looks broken with nothing to read.
1069
+ *
1070
+ * Only fields the schema DECLARES are accepted. A record may carry more than
1071
+ * its schema does, but a shared collection's records are written through it,
1072
+ * and "the field exists on some rows" is not something a gate can promise. */
1073
+ function promotedRefFieldProblems(app, staged) {
1074
+ const schemaOf = new Map(staged.map((entry) => [entry.cid, entry.doc.publishedSchema]));
1075
+ return Object.entries(app.public?.submit ?? {}).flatMap(([cid, submit]) => submitRefProblems(schemaOf, cid, submit));
1076
+ }
1077
+ function submitRefProblems(schemaOf, cid, submit) {
1078
+ return [
1079
+ ...idInRefProblems(schemaOf, cid, submit),
1080
+ ...boundRefProblems(schemaOf, cid, "fromField", submit.window?.fromField),
1081
+ ...boundRefProblems(schemaOf, cid, "untilField", submit.window?.untilField)
1082
+ ];
1083
+ }
1084
+ function idInRefProblems(schemaOf, cid, submit) {
1085
+ const where = submit.idIn?.where;
1086
+ if (where === void 0) return [];
1087
+ return [...refFieldProblem(schemaOf, cid, "idIn.where.field", submit.idIn?.collection, where.field), ...comparableProblem(schemaOf, cid, submit.idIn?.collection, where)];
1088
+ }
1089
+ function boundRefProblems(schemaOf, cid, key, ref) {
1090
+ if (ref === void 0) return [];
1091
+ return [...refFieldProblem(schemaOf, cid, `window.${key}.field`, ref.collection, ref.field), ...millisProblem(schemaOf, cid, `window.${key}.field`, ref)];
1092
+ }
1093
+ /** The field spec a reference points at, or undefined when there is nothing
1094
+ * staged to judge it against (the host refuses that separately, naming every
1095
+ * missing collection at once). */
1096
+ function referencedField(schemaOf, target, field) {
1097
+ if (target === void 0 || field === void 0) return void 0;
1098
+ return schemaOf.get(target)?.fields?.[field];
1099
+ }
1100
+ /** An enum's domain, or undefined for every other kind. Narrowed by the key
1101
+ * rather than asserted: `fields` is a discriminated union and only some of
1102
+ * its members carry `values`. */
1103
+ function enumValues(spec) {
1104
+ return spec.type === "enum" ? spec.values : void 0;
1105
+ }
1106
+ function refFieldProblem(schemaOf, cid, key, target, field) {
1107
+ if (target === void 0 || field === void 0) return [];
1108
+ const schema = schemaOf.get(target);
1109
+ if (schema === void 0 || referencedField(schemaOf, target, field) !== void 0) return [];
1110
+ const known = Object.keys(schema.fields ?? {}).sort().join(", ");
1111
+ return [`public.submit.${cid}.${key} names '${field}', which the STAGED schema of '${target}' — the one publish promotes — does not declare. The rules read that field off the record and compare it, so as written every submission is refused with nothing to explain it. Fields on '${target}': ${known.length > 0 ? known : "(none)"}.`];
1112
+ }
1113
+ /** A comparison the rules can never satisfy is as dead as a missing field, and
1114
+ * looks even more correct on the page: an `enum` whose domain does not contain
1115
+ * the value, or a boolean field compared with a string. */
1116
+ function comparableProblem(schemaOf, cid, target, where) {
1117
+ const spec = referencedField(schemaOf, target, where.field);
1118
+ if (spec === void 0) return [];
1119
+ const said = JSON.stringify(where.equals);
1120
+ const values = enumValues(spec);
1121
+ if (values !== void 0) {
1122
+ if (values.includes(String(where.equals))) return [];
1123
+ return [`public.submit.${cid}.idIn.where.equals is ${said}, which is not one of the values '${where.field}' can hold on '${String(target)}' (${values.join(", ") || "(none)"}). The comparison can never be true, so every submission is refused.`];
1124
+ }
1125
+ const wanted = spec.type === "number" ? "number" : spec.type === "boolean" ? "boolean" : "string";
1126
+ if (typeof where.equals === wanted) return [];
1127
+ return [`public.submit.${cid}.idIn.where.equals is ${said}, and '${where.field}' on '${String(target)}' is a ${spec.type} field. The rules compare the stored value with this one and never coerce, so the comparison can never be true and every submission is refused.`];
1128
+ }
1129
+ /** A per-record window bound is EPOCH MILLIS, because the rules have no date
1130
+ * arithmetic and do not coerce: they compare `request.time.toMillis()` with
1131
+ * whatever is stored. A `datetime` field holds an ISO string, which is a type
1132
+ * error that fails closed — the window never opens, and nothing says so. */
1133
+ function millisProblem(schemaOf, cid, key, ref) {
1134
+ const spec = referencedField(schemaOf, ref?.collection, ref?.field);
1135
+ if (spec === void 0 || spec.type === "number") return [];
1136
+ return [`public.submit.${cid}.${key} names '${String(ref?.field)}' on '${String(ref?.collection)}', which is a ${spec.type} field. A per-record bound is EPOCH MILLIS: the rules compare it with request.time.toMillis() and never coerce, so anything else is a type error that refuses every submission — the window simply never opens. Store the instant as a number.`];
1137
+ }
1138
+ function promotedAssigneeProblems(app, promoted, stagedCids) {
1139
+ return Object.entries(app.members).flatMap(([email, roles]) => Object.entries(roles).flatMap(([cid, role]) => {
1140
+ if (role !== "assignee" || cid === "*" || !stagedCids.has(cid)) return [];
1141
+ if (promoted[cid]?.assigneeField !== void 0) return [];
1142
+ return [`members["${email}"] holds "assignee" on '${cid}', and the STAGED version of '${cid}' — the one publish promotes — carries no assigneeField, even if app.json declares one now. Publish writes the roster from app.json and the collection configuration from the deploy, so that member would land with nothing to be compared against: refused every write, while the app keeps working for everybody else. Run deploy again, so the version being published is the one the declaration describes.`];
1143
+ }));
1144
+ }
1145
+ /** The mirror's other half, checked against what publish will actually
1146
+ * promote — the same trap as the assignee's field, reached by the same route.
1147
+ *
1148
+ * `mirror` is published from the MANIFEST (it lives in `public.submit`) while
1149
+ * `mirrorOf` is promoted from what DEPLOY staged. Add both halves to
1150
+ * `app.json` and publish without redeploying, and what lands is a submission
1151
+ * demanding a paired projection write beside a projection whose rule config
1152
+ * does not allow it: every booking is refused, and the declaration on disk
1153
+ * looks perfectly sound.
1154
+ *
1155
+ * Refused in the reverse direction too. Removing `mirrorOf` from a live app
1156
+ * and publishing without a deploy leaves the projection accepting nothing —
1157
+ * and removing it FROM the staged side while the submission still demands it
1158
+ * is the drift this pair exists to prevent. */
1159
+ function promotedMirrorProblems(app, promoted, stagedCids) {
1160
+ return [...addedMirrorProblems(app, promoted, stagedCids), ...strandedMirrorProblems(app, promoted)];
1161
+ }
1162
+ /** The manifest asks for a projection the promoted configuration will not
1163
+ * allow: every submission denied, and nothing on the page to say why. */
1164
+ function addedMirrorProblems(app, promoted, stagedCids) {
1165
+ return Object.entries(app.public?.submit ?? {}).flatMap(([cid, submit]) => {
1166
+ const { mirror } = submit;
1167
+ if (mirror === void 0 || !stagedCids.has(mirror)) return [];
1168
+ if (promoted[mirror]?.mirrorOf === cid) return [];
1169
+ return [`public.submit.${cid}.mirror names '${mirror}', and the STAGED version of '${mirror}' — the one publish promotes — does not declare mirrorOf: "${cid}", even if app.json declares it now. Publish writes the submission side from app.json and the collection side from the deploy, so what lands is a booking that must move its projection beside a projection that refuses to move: every submission is denied, with nothing on the page to say why. Run deploy again, so the version being published is the one the declaration describes.`];
1170
+ });
1171
+ }
1172
+ /** The other direction, and the DANGEROUS one.
1173
+ *
1174
+ * Take a live pair, delete BOTH halves from `app.json`, and publish without
1175
+ * redeploying. The submission side comes from the manifest, so nothing
1176
+ * requires the projection to move any more; the collection side comes from
1177
+ * staging, which still says `mirrorOf`, so the projection stays writable.
1178
+ * Bookings are then created while the public row goes on saying `open` — the
1179
+ * precise failure the pair exists to prevent, arrived at by removing it.
1180
+ *
1181
+ * Refused rather than tolerated because the app keeps WORKING: submissions
1182
+ * succeed. Only the public page is wrong, and only to the people reading it. */
1183
+ function strandedMirrorProblems(app, promoted) {
1184
+ return Object.entries(promoted).flatMap(([cid, config]) => {
1185
+ const authority = config.mirrorOf;
1186
+ if (authority === void 0) return [];
1187
+ if (app.public?.submit?.[authority]?.mirror === cid) return [];
1188
+ return [`the STAGED version of '${cid}' — the one publish promotes — declares mirrorOf: "${authority}", and app.json no longer declares public.submit.${authority}.mirror: "${cid}". What lands is a projection that is still writable beside submissions that no longer have to move it, so records can be created while '${cid}' goes on advertising them as available. Nothing fails: the app works and the public page lies. Run deploy again, so the version being published is the one the declaration describes.`];
1189
+ });
1190
+ }
701
1191
  //#endregion
702
1192
  //#region src/collection/server/skillAssets.ts
703
1193
  /** Read a collection's custom-view HTML, path-safely. `viewFile` is a
@@ -2768,6 +3258,6 @@ function makeManageCollectionTool(deps = {}) {
2768
3258
  };
2769
3259
  }
2770
3260
  //#endregion
2771
- export { parseAuthoredApp as $, runQueryOverRows as A, APP_SLUGS_COLLECTION as B, STORE_UNREADABLE as C, recordFieldProblem as D, compileRecordZ as E, readCustomViewI18n as F, appStagingPath as G, appConfigPath as H, readSkillTemplate as I, projectPublish as J, projectApp as K, bindsSubmitterIdentity as L, buildCollectionActionSeedPrompt as M, promptPathsFor as N, runCollectionQuery as O, readCustomViewHtml as P, AuthoredAppZ as Q, publishProblems as R, MAX_RECORD_ISSUES as S, validateRecordObject as T, appSchemasPath as U, PUBLIC_CONFIG_DOC as V, appSlugDoc as W, stagedRuleConfig as X, promoteSchema as Y, APP_ROLES as Z, computeCollectionIcon as _, deleteCollection as a, applyMutateAction as b, computeSuccessor as c, isTriggerDue as d, maybeSpawnSuccessor as f, ONE_SECOND_MS as g, successorId as h, deleteCustomView as i, buildActionSeedPrompt as j, enrichItems as k, daysInMonth as l, resolveEvery as m, MAX_UNSELECTIVE_ITEMS as n, deleteCollectionRefusalMessage as o, parseCivil as p, projectDeploy as q, makeManageCollectionTool as r, advanceTriggerDate as s, MAX_SCHEMA_ISSUES as t, formatCivil as u, buildWorkspaceOntology as v, validateCollectionRecords as w, firstMutateParamProblem as x, schemaRelations as y, APPS_COLLECTION as z };
3261
+ export { AuthoredAppZ as $, runQueryOverRows as A, APPS_COLLECTION as B, STORE_UNREADABLE as C, recordFieldProblem as D, compileRecordZ as E, readCustomViewI18n as F, appSlugDoc as G, PUBLIC_CONFIG_DOC as H, readSkillTemplate as I, projectDeploy as J, appStagingPath as K, bindsSubmitterIdentity as L, buildCollectionActionSeedPrompt as M, promptPathsFor as N, runCollectionQuery as O, readCustomViewHtml as P, APP_ROLES as Q, promotedRoleProblems as R, MAX_RECORD_ISSUES as S, validateRecordObject as T, appConfigPath as U, APP_SLUGS_COLLECTION as V, appSchemasPath as W, promoteSchema as X, projectPublish as Y, stagedRuleConfig as Z, computeCollectionIcon as _, deleteCollection as a, applyMutateAction as b, computeSuccessor as c, isTriggerDue as d, parseAuthoredApp as et, maybeSpawnSuccessor as f, ONE_SECOND_MS as g, successorId as h, deleteCustomView as i, buildActionSeedPrompt as j, enrichItems as k, daysInMonth as l, resolveEvery as m, MAX_UNSELECTIVE_ITEMS as n, deleteCollectionRefusalMessage as o, parseCivil as p, projectApp as q, makeManageCollectionTool as r, advanceTriggerDate as s, MAX_SCHEMA_ISSUES as t, formatCivil as u, buildWorkspaceOntology as v, validateCollectionRecords as w, firstMutateParamProblem as x, schemaRelations as y, publishProblems as z };
2772
3262
 
2773
- //# sourceMappingURL=server-zcM1ViCE.js.map
3263
+ //# sourceMappingURL=server-DGjmK9vU.js.map