@jsb188/mday 1.1.8 → 1.1.11

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.
Files changed (123) hide show
  1. package/package.json +105 -35
  2. package/src/constants/agentDoc.ts +12 -0
  3. package/src/constants/agentMemory.ts +154 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/campaign.ts +127 -0
  6. package/src/constants/dataTable.ts +48 -0
  7. package/src/constants/dataTableFieldTypes.generated.ts +63 -0
  8. package/src/constants/docAccess.ts +32 -0
  9. package/src/constants/email.ts +44 -0
  10. package/src/constants/fieldDoc.ts +34 -0
  11. package/src/constants/graphqlReactivity.ts +113 -0
  12. package/src/constants/inboundContact.ts +8 -0
  13. package/src/constants/integration.ts +13 -0
  14. package/src/constants/log.ts +7 -366
  15. package/src/constants/logActivityGroupColors.generated.ts +19 -0
  16. package/src/constants/logTaxonomy.generated.ts +1103 -0
  17. package/src/constants/noteDoc.ts +77 -0
  18. package/src/constants/organization.ts +18 -11
  19. package/src/constants/pageRoute.ts +23 -0
  20. package/src/constants/schedule.ts +220 -0
  21. package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
  22. package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
  23. package/src/constants/sheet.ts +168 -0
  24. package/src/constants/sheetRegionSources.generated.ts +578 -0
  25. package/src/constants/square.ts +16 -0
  26. package/src/constants/storage.ts +61 -0
  27. package/src/constants/usage.ts +79 -0
  28. package/src/schemas/editLogEntry.ts +1017 -738
  29. package/src/schemas/fieldDocsQuery.ts +12 -0
  30. package/src/schemas/inboundContact.ts +77 -0
  31. package/src/schemas/logsQuery.ts +57 -316
  32. package/src/schemas/organization.ts +53 -213
  33. package/src/types/account.d.ts +81 -0
  34. package/src/types/agentDoc.d.ts +64 -0
  35. package/src/types/agentMemory.d.ts +74 -0
  36. package/src/types/campaign.d.ts +683 -0
  37. package/src/types/dataTable.d.ts +366 -0
  38. package/src/types/docAccess.d.ts +96 -0
  39. package/src/types/email.d.ts +142 -0
  40. package/src/types/fieldDoc.d.ts +273 -0
  41. package/src/types/inboundContact.d.ts +57 -0
  42. package/src/types/integration.d.ts +42 -0
  43. package/src/types/log.d.ts +169 -190
  44. package/src/types/noteDoc.d.ts +86 -0
  45. package/src/types/organization.d.ts +175 -76
  46. package/src/types/pageRoute.d.ts +48 -0
  47. package/src/types/schedule.d.ts +391 -0
  48. package/src/types/sheet.d.ts +1130 -0
  49. package/src/types/storage.d.ts +242 -0
  50. package/src/types/usage.d.ts +106 -0
  51. package/src/types/workflow.d.ts +305 -0
  52. package/src/utils/agentDoc.ts +109 -0
  53. package/src/utils/aiChat.ts +26 -0
  54. package/src/utils/campaign.ts +741 -0
  55. package/src/utils/campaignFormEditor.ts +871 -0
  56. package/src/utils/dataTable.ts +849 -0
  57. package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
  58. package/src/utils/dataTableRealtimeAuthority.ts +209 -0
  59. package/src/utils/dataTableRealtimeSelection.ts +134 -0
  60. package/src/utils/dataTableRowDocument.ts +508 -0
  61. package/src/utils/dataTableSource.ts +230 -0
  62. package/src/utils/docAccess.ts +383 -0
  63. package/src/utils/documentName.ts +53 -0
  64. package/src/utils/email.ts +172 -0
  65. package/src/utils/fieldDoc.ts +113 -0
  66. package/src/utils/log.ts +173 -778
  67. package/src/utils/logRegistryValue.ts +90 -0
  68. package/src/utils/logStoreRead.ts +266 -0
  69. package/src/utils/logTaxonomy.ts +76 -0
  70. package/src/utils/logTaxonomyDisplay.ts +439 -0
  71. package/src/utils/noteDoc.ts +827 -0
  72. package/src/utils/organization.ts +683 -81
  73. package/src/utils/pageRoute.ts +71 -0
  74. package/src/utils/schedule.ts +799 -0
  75. package/src/utils/scheduleEvaluation.ts +1994 -0
  76. package/src/utils/scheduleICS.ts +246 -0
  77. package/src/utils/scheduleRuleTemplates.ts +188 -0
  78. package/src/utils/sheet.ts +4973 -0
  79. package/src/utils/sheetAxisMove.ts +830 -0
  80. package/src/utils/sheetBootstrap.ts +1769 -0
  81. package/src/utils/sheetCommand.ts +423 -0
  82. package/src/utils/sheetExport.ts +238 -0
  83. package/src/utils/sheetFormulaEvaluation.ts +914 -0
  84. package/src/utils/sheetFormulaEvaluator.ts +852 -0
  85. package/src/utils/sheetJournal.ts +78 -0
  86. package/src/utils/sheetNumberFormat.ts +1211 -0
  87. package/src/utils/sheetRegion.ts +374 -0
  88. package/src/utils/sheetRegionFilter.ts +1259 -0
  89. package/src/utils/sheetRegionSourceRegistry.ts +594 -0
  90. package/src/utils/sheetSequence.ts +148 -0
  91. package/src/utils/sheetSnapshot.ts +992 -0
  92. package/src/utils/sheetStructureEdit.ts +1284 -0
  93. package/src/utils/square.ts +32 -0
  94. package/src/utils/storage.ts +152 -0
  95. package/src/utils/table-route.ts +262 -0
  96. package/src/utils/workflow.ts +380 -0
  97. package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
  98. package/tests/docAccess.test.ts +525 -0
  99. package/tests/editLogEntry.test.ts +112 -0
  100. package/tests/noteDoc.test.ts +669 -0
  101. package/tests/schedule.test.ts +693 -0
  102. package/tests/scheduleEvaluation.test.ts +1970 -0
  103. package/tests/scheduleICS.test.ts +208 -0
  104. package/tests/scheduleRuleTemplates.test.ts +176 -0
  105. package/tests/scheduleSweepInvariants.test.ts +428 -0
  106. package/tests/sheetAxisMove.test.ts +343 -0
  107. package/tests/sheetFormulaEvaluator.test.ts +575 -0
  108. package/tests/sheetRegionFilter.test.ts +1135 -0
  109. package/tests/sheetSequence.test.ts +41 -0
  110. package/tests/sheetStructureEdit.test.ts +82 -0
  111. package/tsconfig.json +12 -0
  112. package/vitest.config.ts +8 -0
  113. package/src/constants/log-activityTexts.ts +0 -209
  114. package/src/constants/product.ts +0 -66
  115. package/src/constants/report.ts +0 -25
  116. package/src/schemas/childOrganizationsQuery.ts +0 -44
  117. package/src/schemas/productsQuery.ts +0 -145
  118. package/src/schemas/reportsQuery.ts +0 -107
  119. package/src/types/action.d.ts +0 -42
  120. package/src/types/product.d.ts +0 -173
  121. package/src/types/report.d.ts +0 -203
  122. package/src/utils/product-calEvent.ts +0 -508
  123. package/src/utils/route-helpers.ts +0 -583
@@ -0,0 +1,594 @@
1
+ import type { DataTableFieldTypeEnum } from '../types/dataTable.d.ts';
2
+ import {
3
+ SHEET_CUSTOM_REGION_SOURCE_CHILD_ORGANIZATION_COLUMNS,
4
+ SHEET_CUSTOM_REGION_SOURCE_CHILD_ORGANIZATIONS,
5
+ SHEET_CUSTOM_REGION_SOURCE_LOG_COLUMNS,
6
+ SHEET_CUSTOM_REGION_SOURCE_LOGS,
7
+ } from '../constants/sheet.ts';
8
+ import { DATA_TABLE_FIELD_TYPE_ENUMS } from '../constants/dataTable.ts';
9
+
10
+ /* Reach descriptor: the CONSTRAINED vocabulary a registry row may use to bind
11
+ * one field to SQL. Rows never carry free-form SQL; the server compiler quotes
12
+ * every identifier and rejects anything outside this grammar (error 20077). */
13
+ export type SheetRegionSourceReach =
14
+ | {
15
+ kind: 'COLUMN';
16
+ column: string;
17
+ render: SheetRegionSourceReachRender;
18
+ }
19
+ | {
20
+ kind: 'JOINED_COLUMN';
21
+ join: string;
22
+ column: string;
23
+ render: SheetRegionSourceReachRender;
24
+ }
25
+ | {
26
+ kind: 'PERSON_NAME';
27
+ join: string;
28
+ firstNameColumn: string;
29
+ lastNameColumn: string;
30
+ };
31
+
32
+ /* Render vocabulary compiled by code (adding a render kind is a code change):
33
+ * RAW → "a"."col"; TEXT_ID → "a"."col"::text; COALESCE_TEXT →
34
+ * COALESCE("a"."col", ''); DATE/DATETIME → reserved, compile like RAW. */
35
+ export type SheetRegionSourceReachRender =
36
+ | 'RAW'
37
+ | 'TEXT_ID'
38
+ | 'COALESCE_TEXT'
39
+ | 'DATE'
40
+ | 'DATETIME';
41
+
42
+ /* One queryable field of a region source kind (registry row facade). */
43
+ export type SheetRegionSourceFieldDescriptor = {
44
+ key: string;
45
+ labelKey: string;
46
+ fieldType: DataTableFieldTypeEnum | null;
47
+ formulaValueSource: string | null;
48
+ relatedTable: string | null;
49
+ width: number;
50
+ ordinal: number;
51
+ filterable: boolean;
52
+ sortable: boolean;
53
+ reach: SheetRegionSourceReach | null;
54
+ };
55
+
56
+ /* One join of a region source kind. Compilation is always equality-only
57
+ * LEFT OUTER JOIN; fromJoinKey null anchors on the kind's baseTable. */
58
+ export type SheetRegionSourceJoinDescriptor = {
59
+ key: string;
60
+ table: string;
61
+ fromJoinKey: string | null;
62
+ fromColumn: string;
63
+ toColumn: string;
64
+ };
65
+
66
+ /* One region source kind: identity, fetch strategy, client metadata keys,
67
+ * joins, and fields. Everything here is registry DATA; handlers, semantics,
68
+ * and label resolution stay code. */
69
+ export type SheetRegionSourceKindDescriptor = {
70
+ key: string;
71
+ fetchStrategy: 'SQL' | 'DIRECTORY';
72
+ baseTable: string | null;
73
+ titleKey: string;
74
+ appRoute: string | null;
75
+ revisionSourceKey: string;
76
+ /* Staged-launch gate: a disabled kind is fully recognized by durable data
77
+ * paths (revision types, event validation, existing-region rendering) but
78
+ * refuses NEW region creation until the flip + regenerate + deploy. The
79
+ * contract checker freshness-normalizes this flag, so a live flip alone
80
+ * never fails the contract. */
81
+ enabled: boolean;
82
+ joins: SheetRegionSourceJoinDescriptor[];
83
+ fields: SheetRegionSourceFieldDescriptor[];
84
+ };
85
+
86
+ const FILE_CONSTANTS = {
87
+ identRegex: /^[A-Za-z_][A-Za-z0-9_]{0,62}$/,
88
+ renderKinds: ['RAW', 'TEXT_ID', 'COALESCE_TEXT', 'DATE', 'DATETIME'],
89
+ maxJoinsPerKind: 12,
90
+ maxFieldsPerKind: 40,
91
+ maxJoinChainDepth: 2,
92
+
93
+ /* Base-table columns that are immutable/PK id-like: they can never change
94
+ * on an existing row, so invalidation-map derivation emits NO entry for
95
+ * them (matches the deliberate absence of an `id` key in the historical
96
+ * hand-written realtime map). */
97
+ immutableBaseColumns: ['id'],
98
+
99
+ /* LOGS joins, mirroring the historical getLogRegionSourcePageJoinStatement
100
+ * order (site, counterparty, childOrg, inboundContact, fieldDoc,
101
+ * fieldDocSubmission, accountProfile). */
102
+ logsJoins: [
103
+ {
104
+ key: 'site',
105
+ table: 'organization_sites',
106
+ fromJoinKey: null,
107
+ fromColumn: 'siteId',
108
+ toColumn: 'id',
109
+ },
110
+ {
111
+ key: 'counterparty',
112
+ table: 'counterparties',
113
+ fromJoinKey: null,
114
+ fromColumn: 'counterpartyId',
115
+ toColumn: 'id',
116
+ },
117
+ {
118
+ key: 'childOrg',
119
+ table: 'organizations',
120
+ fromJoinKey: 'counterparty',
121
+ fromColumn: 'linkedOrgId',
122
+ toColumn: 'id',
123
+ },
124
+ {
125
+ key: 'inboundContact',
126
+ table: 'inbound_contacts',
127
+ fromJoinKey: 'counterparty',
128
+ fromColumn: 'inboundContactId',
129
+ toColumn: 'id',
130
+ },
131
+ {
132
+ key: 'fieldDoc',
133
+ table: 'field_docs',
134
+ fromJoinKey: null,
135
+ fromColumn: 'fieldDocId',
136
+ toColumn: 'id',
137
+ },
138
+ {
139
+ key: 'fieldDocSubmission',
140
+ table: 'field_doc_submissions',
141
+ fromJoinKey: null,
142
+ fromColumn: 'fieldDocSubmissionId',
143
+ toColumn: 'id',
144
+ },
145
+ {
146
+ key: 'accountProfile',
147
+ table: 'account_profiles',
148
+ fromJoinKey: null,
149
+ fromColumn: 'accountId',
150
+ toColumn: 'accountId',
151
+ },
152
+ ] as SheetRegionSourceJoinDescriptor[],
153
+
154
+ /* LOGS reach per field key, mirroring the historical per-cellKey SQL
155
+ * expressions (getLogRegionSourceSQLExpression). */
156
+ logsReachByKey: {
157
+ id: { kind: 'COLUMN', column: 'id', render: 'TEXT_ID' },
158
+ summary: { kind: 'COLUMN', column: 'summary', render: 'COALESCE_TEXT' },
159
+ date: { kind: 'COLUMN', column: 'date', render: 'RAW' },
160
+ createdAt: { kind: 'COLUMN', column: 'createdAt', render: 'RAW' },
161
+ childOrgId: {
162
+ kind: 'JOINED_COLUMN',
163
+ join: 'counterparty',
164
+ column: 'linkedOrgId',
165
+ render: 'TEXT_ID',
166
+ },
167
+ childOrgName: {
168
+ kind: 'JOINED_COLUMN',
169
+ join: 'childOrg',
170
+ column: 'name',
171
+ render: 'COALESCE_TEXT',
172
+ },
173
+ siteId: { kind: 'COLUMN', column: 'siteId', render: 'TEXT_ID' },
174
+ siteName: {
175
+ kind: 'JOINED_COLUMN',
176
+ join: 'site',
177
+ column: 'name',
178
+ render: 'COALESCE_TEXT',
179
+ },
180
+ inboundContactId: {
181
+ kind: 'JOINED_COLUMN',
182
+ join: 'counterparty',
183
+ column: 'inboundContactId',
184
+ render: 'TEXT_ID',
185
+ },
186
+ inboundContactEmail: {
187
+ kind: 'JOINED_COLUMN',
188
+ join: 'inboundContact',
189
+ column: 'email',
190
+ render: 'COALESCE_TEXT',
191
+ },
192
+ inboundContactPhone: {
193
+ kind: 'JOINED_COLUMN',
194
+ join: 'inboundContact',
195
+ column: 'phone',
196
+ render: 'COALESCE_TEXT',
197
+ },
198
+ fieldDocId: { kind: 'COLUMN', column: 'fieldDocId', render: 'TEXT_ID' },
199
+ fieldDocName: {
200
+ kind: 'JOINED_COLUMN',
201
+ join: 'fieldDoc',
202
+ column: 'title',
203
+ render: 'COALESCE_TEXT',
204
+ },
205
+ fieldDocSubmissionId: {
206
+ kind: 'COLUMN',
207
+ column: 'fieldDocSubmissionId',
208
+ render: 'TEXT_ID',
209
+ },
210
+ fieldDocSubmissionPeriod: {
211
+ kind: 'JOINED_COLUMN',
212
+ join: 'fieldDocSubmission',
213
+ column: 'period',
214
+ render: 'RAW',
215
+ },
216
+ accountId: { kind: 'COLUMN', column: 'accountId', render: 'TEXT_ID' },
217
+ accountPersonName: {
218
+ kind: 'PERSON_NAME',
219
+ join: 'accountProfile',
220
+ firstNameColumn: 'firstName',
221
+ lastNameColumn: 'lastName',
222
+ },
223
+ } as Record<string, SheetRegionSourceReach>,
224
+ };
225
+
226
+ /* Loose column shape shared by the two hand-maintained constants arrays. */
227
+ type SheetRegionSourceConstantColumn = {
228
+ key: string;
229
+ labelKey: string;
230
+ fieldType?: DataTableFieldTypeEnum | null;
231
+ formulaValueSource?: string | null;
232
+ relatedTable?: string | null;
233
+ width: number;
234
+ };
235
+
236
+ /*
237
+ * Map one constants column entry onto a registry field descriptor.
238
+ */
239
+ function buildSheetRegionSourceFieldDescriptor(
240
+ column: SheetRegionSourceConstantColumn,
241
+ ordinal: number,
242
+ options: {
243
+ filterable: boolean;
244
+ sortable: boolean;
245
+ reach: SheetRegionSourceReach | null;
246
+ },
247
+ ): SheetRegionSourceFieldDescriptor {
248
+ return {
249
+ key: column.key,
250
+ labelKey: column.labelKey,
251
+ fieldType: column.fieldType || null,
252
+ formulaValueSource: column.formulaValueSource || null,
253
+ relatedTable: column.relatedTable || null,
254
+ width: column.width,
255
+ ordinal,
256
+ filterable: options.filterable,
257
+ sortable: options.sortable,
258
+ reach: options.reach,
259
+ };
260
+ }
261
+
262
+ /*
263
+ * Derive the registry kind descriptors from the current hand-maintained
264
+ * constants plus the code-side reach/join tables above. This is the seed
265
+ * source of truth until the registry flips; the contract checker compares it
266
+ * against the live database rows and the committed generated modules.
267
+ */
268
+ export function buildSheetRegionSourceDescriptorsFromConstants(): SheetRegionSourceKindDescriptor[] {
269
+ return [
270
+ {
271
+ key: SHEET_CUSTOM_REGION_SOURCE_CHILD_ORGANIZATIONS,
272
+ fetchStrategy: 'DIRECTORY',
273
+ baseTable: null,
274
+ titleKey: 'form.vendors',
275
+ appRoute: '/app/vendors',
276
+ revisionSourceKey: SHEET_CUSTOM_REGION_SOURCE_CHILD_ORGANIZATIONS,
277
+ // Both legacy kinds are live in production, so they derive enabled
278
+ enabled: true,
279
+ joins: [],
280
+ // DIRECTORY hydration ignores region filter/sort today; the seeded
281
+ // flags preserve that asymmetry (normalization forces null filter/sort)
282
+ fields: SHEET_CUSTOM_REGION_SOURCE_CHILD_ORGANIZATION_COLUMNS.map(
283
+ (column, index) =>
284
+ buildSheetRegionSourceFieldDescriptor(
285
+ column as SheetRegionSourceConstantColumn,
286
+ index,
287
+ {
288
+ filterable: false,
289
+ sortable: false,
290
+ reach: null,
291
+ },
292
+ ),
293
+ ),
294
+ },
295
+ {
296
+ key: SHEET_CUSTOM_REGION_SOURCE_LOGS,
297
+ fetchStrategy: 'SQL',
298
+ baseTable: 'logs',
299
+ titleKey: 'log.logs',
300
+ appRoute: '/app/logs',
301
+ revisionSourceKey: SHEET_CUSTOM_REGION_SOURCE_LOGS,
302
+ enabled: true,
303
+ joins: FILE_CONSTANTS.logsJoins.map((join) => ({ ...join })),
304
+ fields: SHEET_CUSTOM_REGION_SOURCE_LOG_COLUMNS.map((column, index) =>
305
+ buildSheetRegionSourceFieldDescriptor(
306
+ column as SheetRegionSourceConstantColumn,
307
+ index,
308
+ {
309
+ filterable: true,
310
+ sortable: true,
311
+ reach: FILE_CONSTANTS.logsReachByKey[column.key] || null,
312
+ },
313
+ ),
314
+ ),
315
+ },
316
+ ];
317
+ }
318
+
319
+ /*
320
+ * Return whether one value is a valid quoted-identifier candidate.
321
+ */
322
+ function isSheetRegionSourceIdent(value: unknown): value is string {
323
+ return typeof value === 'string' && FILE_CONSTANTS.identRegex.test(value);
324
+ }
325
+
326
+ /*
327
+ * Collect grammar issues for one field's reach descriptor.
328
+ */
329
+ function collectSheetRegionSourceReachIssues(
330
+ kind: SheetRegionSourceKindDescriptor,
331
+ fieldKey: string,
332
+ reach: SheetRegionSourceReach,
333
+ ): string[] {
334
+ const issues: string[] = [];
335
+ const joinKeys = new Set(kind.joins.map((join) => join.key));
336
+ const label = `${kind.key}.${fieldKey}`;
337
+
338
+ if (reach.kind === 'COLUMN') {
339
+ if (!isSheetRegionSourceIdent(reach.column)) {
340
+ issues.push(`${label}: COLUMN reach has an invalid column identifier`);
341
+ }
342
+ if (!FILE_CONSTANTS.renderKinds.includes(reach.render)) {
343
+ issues.push(`${label}: COLUMN reach has an unknown render kind`);
344
+ }
345
+ } else if (reach.kind === 'JOINED_COLUMN') {
346
+ if (!joinKeys.has(reach.join)) {
347
+ issues.push(
348
+ `${label}: JOINED_COLUMN reach references unknown join ${reach.join}`,
349
+ );
350
+ }
351
+ if (!isSheetRegionSourceIdent(reach.column)) {
352
+ issues.push(
353
+ `${label}: JOINED_COLUMN reach has an invalid column identifier`,
354
+ );
355
+ }
356
+ if (!FILE_CONSTANTS.renderKinds.includes(reach.render)) {
357
+ issues.push(`${label}: JOINED_COLUMN reach has an unknown render kind`);
358
+ }
359
+ } else if (reach.kind === 'PERSON_NAME') {
360
+ if (!joinKeys.has(reach.join)) {
361
+ issues.push(
362
+ `${label}: PERSON_NAME reach references unknown join ${reach.join}`,
363
+ );
364
+ }
365
+ if (
366
+ !isSheetRegionSourceIdent(reach.firstNameColumn) ||
367
+ !isSheetRegionSourceIdent(reach.lastNameColumn)
368
+ ) {
369
+ issues.push(
370
+ `${label}: PERSON_NAME reach has an invalid name column identifier`,
371
+ );
372
+ }
373
+ } else {
374
+ issues.push(`${label}: unknown reach kind`);
375
+ }
376
+
377
+ return issues;
378
+ }
379
+
380
+ /*
381
+ * Collect join-graph issues (identifiers, chain anchoring, chain depth) for
382
+ * one kind descriptor.
383
+ */
384
+ function collectSheetRegionSourceJoinIssues(
385
+ kind: SheetRegionSourceKindDescriptor,
386
+ ): string[] {
387
+ const issues: string[] = [];
388
+ const joinKeys = new Set<string>();
389
+
390
+ if (kind.joins.length > FILE_CONSTANTS.maxJoinsPerKind) {
391
+ issues.push(
392
+ `${kind.key}: exceeds the ${FILE_CONSTANTS.maxJoinsPerKind}-join budget`,
393
+ );
394
+ }
395
+
396
+ kind.joins.forEach((join) => {
397
+ if (joinKeys.has(join.key)) {
398
+ issues.push(`${kind.key}: duplicate join key ${join.key}`);
399
+ }
400
+ joinKeys.add(join.key);
401
+
402
+ if (
403
+ !isSheetRegionSourceIdent(join.key) ||
404
+ !isSheetRegionSourceIdent(join.table)
405
+ ) {
406
+ issues.push(`${kind.key}.${join.key}: invalid join/table identifier`);
407
+ }
408
+ if (
409
+ !isSheetRegionSourceIdent(join.fromColumn) ||
410
+ !isSheetRegionSourceIdent(join.toColumn)
411
+ ) {
412
+ issues.push(`${kind.key}.${join.key}: invalid join column identifier`);
413
+ }
414
+ });
415
+
416
+ kind.joins.forEach((join) => {
417
+ let depth = 1;
418
+ let current: SheetRegionSourceJoinDescriptor | undefined = join;
419
+ const visited = new Set<string>([join.key]);
420
+ while (current?.fromJoinKey) {
421
+ const parent = kind.joins.find(
422
+ (candidate) => candidate.key === current?.fromJoinKey,
423
+ );
424
+ if (!parent) {
425
+ issues.push(
426
+ `${kind.key}.${join.key}: chains off unknown join ${current.fromJoinKey}`,
427
+ );
428
+ break;
429
+ }
430
+ if (visited.has(parent.key)) {
431
+ issues.push(`${kind.key}.${join.key}: join chain cycle`);
432
+ break;
433
+ }
434
+ visited.add(parent.key);
435
+ depth += 1;
436
+ current = parent;
437
+ }
438
+ if (depth > FILE_CONSTANTS.maxJoinChainDepth) {
439
+ issues.push(
440
+ `${kind.key}.${join.key}: join chain deeper than ${FILE_CONSTANTS.maxJoinChainDepth}`,
441
+ );
442
+ }
443
+ });
444
+
445
+ return issues;
446
+ }
447
+
448
+ /*
449
+ * Validate one descriptor set: grammar, identifier regex, join budgets,
450
+ * strategy/reach coherence, unique keys, and fieldType vocabulary. Returns
451
+ * human-readable issues; an empty array means the set is valid.
452
+ */
453
+ export function collectSheetRegionSourceDescriptorIssues(
454
+ kinds: SheetRegionSourceKindDescriptor[],
455
+ ): string[] {
456
+ const issues: string[] = [];
457
+ const kindKeys = new Set<string>();
458
+
459
+ kinds.forEach((kind) => {
460
+ if (kindKeys.has(kind.key)) {
461
+ issues.push(`duplicate kind key ${kind.key}`);
462
+ }
463
+ kindKeys.add(kind.key);
464
+
465
+ if (!isSheetRegionSourceIdent(kind.key)) {
466
+ issues.push(`${kind.key}: invalid kind key identifier`);
467
+ }
468
+ if (kind.fetchStrategy !== 'SQL' && kind.fetchStrategy !== 'DIRECTORY') {
469
+ issues.push(`${kind.key}: unknown fetchStrategy`);
470
+ }
471
+ if ((kind.fetchStrategy === 'SQL') !== Boolean(kind.baseTable)) {
472
+ issues.push(
473
+ `${kind.key}: baseTable is required exactly for SQL-strategy kinds`,
474
+ );
475
+ }
476
+ if (kind.baseTable && !isSheetRegionSourceIdent(kind.baseTable)) {
477
+ issues.push(`${kind.key}: invalid baseTable identifier`);
478
+ }
479
+ if (!kind.titleKey) {
480
+ issues.push(`${kind.key}: missing titleKey`);
481
+ }
482
+ if (!kind.revisionSourceKey) {
483
+ issues.push(`${kind.key}: missing revisionSourceKey`);
484
+ }
485
+ if (kind.fields.length > FILE_CONSTANTS.maxFieldsPerKind) {
486
+ issues.push(
487
+ `${kind.key}: exceeds the ${FILE_CONSTANTS.maxFieldsPerKind}-field budget`,
488
+ );
489
+ }
490
+
491
+ issues.push(...collectSheetRegionSourceJoinIssues(kind));
492
+
493
+ const fieldKeys = new Set<string>();
494
+ kind.fields.forEach((field) => {
495
+ if (fieldKeys.has(field.key)) {
496
+ issues.push(`${kind.key}: duplicate field key ${field.key}`);
497
+ }
498
+ fieldKeys.add(field.key);
499
+
500
+ if (!isSheetRegionSourceIdent(field.key)) {
501
+ issues.push(`${kind.key}.${field.key}: invalid field key identifier`);
502
+ }
503
+ if (!field.labelKey) {
504
+ issues.push(`${kind.key}.${field.key}: missing labelKey`);
505
+ }
506
+ if (
507
+ field.fieldType !== null &&
508
+ !DATA_TABLE_FIELD_TYPE_ENUMS.includes(field.fieldType)
509
+ ) {
510
+ issues.push(
511
+ `${kind.key}.${field.key}: unknown fieldType ${field.fieldType}`,
512
+ );
513
+ }
514
+
515
+ // Reach descriptors bind fields to EXISTING fieldType semantics; they
516
+ // are required exactly for SQL-strategy kinds (DIRECTORY fields are
517
+ // client metadata hydrated by a code-registered strategy)
518
+ if (kind.fetchStrategy === 'SQL' && !field.reach) {
519
+ issues.push(
520
+ `${kind.key}.${field.key}: SQL-strategy field is missing its reach descriptor`,
521
+ );
522
+ }
523
+ if (kind.fetchStrategy === 'DIRECTORY' && field.reach) {
524
+ issues.push(
525
+ `${kind.key}.${field.key}: DIRECTORY-strategy field must not carry a reach descriptor`,
526
+ );
527
+ }
528
+ if (field.reach) {
529
+ issues.push(
530
+ ...collectSheetRegionSourceReachIssues(kind, field.key, field.reach),
531
+ );
532
+ }
533
+ });
534
+ });
535
+
536
+ return issues;
537
+ }
538
+
539
+ /*
540
+ * Return the base-table column one field's reach anchors on: its own column
541
+ * for COLUMN reach, otherwise the join-chain root's fromColumn.
542
+ */
543
+ export function getSheetRegionSourceReachBaseColumn(
544
+ kind: SheetRegionSourceKindDescriptor,
545
+ reach: SheetRegionSourceReach,
546
+ ): string | null {
547
+ if (reach.kind === 'COLUMN') {
548
+ return reach.column;
549
+ }
550
+
551
+ let joinKey: string | null = reach.join;
552
+ let rootJoin: SheetRegionSourceJoinDescriptor | undefined;
553
+ const visited = new Set<string>();
554
+ while (joinKey && !visited.has(joinKey)) {
555
+ visited.add(joinKey);
556
+ rootJoin = kind.joins.find((join) => join.key === joinKey);
557
+ joinKey = rootJoin?.fromJoinKey || null;
558
+ }
559
+
560
+ return rootJoin?.fromColumn || null;
561
+ }
562
+
563
+ /*
564
+ * Derive the base-table column → derived cellKeys realtime invalidation map
565
+ * for one kind (replaces the historical hand-written map). Immutable/PK
566
+ * id-like base columns emit NO entry: they can never change on an existing
567
+ * row, so no write to them can invalidate a rendered cell.
568
+ */
569
+ export function getSheetRegionSourceCellKeysByBaseColumn(
570
+ kind: SheetRegionSourceKindDescriptor,
571
+ ): Record<string, string[]> {
572
+ const cellKeysByBaseColumn: Record<string, string[]> = {};
573
+
574
+ kind.fields.forEach((field) => {
575
+ if (!field.reach) {
576
+ return;
577
+ }
578
+
579
+ const baseColumn = getSheetRegionSourceReachBaseColumn(kind, field.reach);
580
+ if (
581
+ !baseColumn ||
582
+ FILE_CONSTANTS.immutableBaseColumns.includes(baseColumn)
583
+ ) {
584
+ return;
585
+ }
586
+
587
+ cellKeysByBaseColumn[baseColumn] = [
588
+ ...(cellKeysByBaseColumn[baseColumn] || []),
589
+ field.key,
590
+ ];
591
+ });
592
+
593
+ return cellKeysByBaseColumn;
594
+ }