@ai-matrx/records 0.82.0 → 0.82.1

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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # Changelog — @ai-matrx/records
2
2
 
3
- ## Unreleased — lane DESCRIBE-BENCH2: a rollup or lookup in the wrong shape is read, not refused
3
+ ## 0.82.1
4
+ ### lane DESCRIBE-BENCH2 (sweep): the mechanical wrong shapes are read, not refused
5
+
6
+ - `autofixDescribeSpec` also rewrites, each with an auto-fix note and only when unambiguous: view `filters` written as a list of `{field, op, value}` (→ the store's map; several values → `anyOf`; a condition the store cannot express is dropped and SAID), `sort`/`orderBy` → `sorts`, `group_by`/`date_field`/… → `groupBy`/`dateField`/…, an exact duplicate view dropped; a field typed with `kind`/`type` (or by a plain label) → `parityType`; a formula over bare field names → `{Label}`; a relationship `inverseKey` that is already a field of the target (the store then read the rollup's link as a plain formula) renamed; a rollup named for a status with no filter → the filter when exactly one far-side choice/tick box is that word; a real-looking phone → a fictional one; a copy of the shared team_member block → an ordinary table (name, role, notes).
7
+ - `describeVocabulary()` gains `view_filters_shape`, `field_shape`, `formula_shape`, `relationship_shape`, `phone_rule`, `team_rule`.
8
+
9
+ ## Earlier in this release — a rollup or lookup in the wrong shape is read, not refused
4
10
 
5
11
  - `autofixDescribeSpec` (so `describeCheck` and the live box) rewrites a rollup or lookup written as `{rollup:{relation,field,fn}}`, `{lookup:{relationField,targetField}}`, `rollupVia/rollupField/rollupFn`, `through/aggregate` and ~20 other spellings into the validator's `via` / `pick` / `agg`, with an auto-fix note, when the relation field, the far field and the aggregate word are unambiguous (sum/total, count, avg/average/mean, min, max …). A rollup through a single link makes that link a list (and its relationship `many`). An unknown relation or aggregate is left for the check to name — nothing is invented.
6
12
  - `describeVocabulary()` gains `rollup_shape` and `lookup_shape`: the rule in one sentence and one tiny example in the validator's keys, handed to every describe arm.
@@ -2087,7 +2087,7 @@ function validateUseCase(useCase) {
2087
2087
  `${primary.rows.length} rows makes the screen look empty. Seed at least ${MIN_ROWS_IN_PRIMARY_TABLE} \u2014 dozens is better.`
2088
2088
  );
2089
2089
  }
2090
- const resolveField = (tableToken, fieldKey) => tablesByToken.get(tableToken)?.fields.find((f) => f.key === fieldKey);
2090
+ const resolveField = (tableToken, fieldKey2) => tablesByToken.get(tableToken)?.fields.find((f) => f.key === fieldKey2);
2091
2091
  useCase.relationships?.forEach((rel, i) => {
2092
2092
  const at = `relationships[${i}]`;
2093
2093
  if (!tablesByToken.has(rel.fromTable)) {
@@ -4566,8 +4566,8 @@ function validateTemplate(raw, options = {}) {
4566
4566
  for (const k of rule.filled ?? []) if (!filled(val(k))) say(at, `"${row.key}" is ${st}, so "${fieldOf(table, k)?.label ?? k}" is filled; it is empty.`);
4567
4567
  for (const [k, v] of Object.entries(rule.equals ?? {})) {
4568
4568
  const got = val(k) ?? null;
4569
- const same3 = sameJson(got, v) || typeof v === "number" && parseNumeric(storeText(got) ?? "") !== null && qCmp(parseNumeric(storeText(got) ?? ""), parseNumeric(String(v))) === 0;
4570
- if (!same3) say(at, `"${row.key}" is ${st}, so "${fieldOf(table, k)?.label ?? k}" is ${JSON.stringify(v)}; it is ${JSON.stringify(got)}.`);
4569
+ const same4 = sameJson(got, v) || typeof v === "number" && parseNumeric(storeText(got) ?? "") !== null && qCmp(parseNumeric(storeText(got) ?? ""), parseNumeric(String(v))) === 0;
4570
+ if (!same4) say(at, `"${row.key}" is ${st}, so "${fieldOf(table, k)?.label ?? k}" is ${JSON.stringify(v)}; it is ${JSON.stringify(got)}.`);
4571
4571
  }
4572
4572
  }
4573
4573
  };
@@ -5905,7 +5905,7 @@ ${e.body}`.matchAll(MERGE_TOKEN)].map((m) => m[1]));
5905
5905
  }
5906
5906
  }
5907
5907
  const rows = t.rows;
5908
- const same3 = (x, y) => {
5908
+ const same4 = (x, y) => {
5909
5909
  const a = storeText(x);
5910
5910
  const b = storeText(y);
5911
5911
  if (a === null || b === null) return false;
@@ -5923,7 +5923,7 @@ ${e.body}`.matchAll(MERGE_TOKEN)].map((m) => m[1]));
5923
5923
  };
5924
5924
  for (const sf of t.fields.filter((ff) => !isComputed(ff) && !isRelation(ff) && ff.key !== l.key)) {
5925
5925
  const have = rows.filter((r) => filled(r.values?.[sf.key]));
5926
- if (have.length >= 3 && have.every((r) => same3(r.values?.[sf.key], far(r)))) {
5926
+ if (have.length >= 3 && have.every((r) => same4(r.values?.[sf.key], far(r)))) {
5927
5927
  say(`tables (${t.token}).fields (${sf.key})`, `"${sf.label}" repeats "${l.label}" on every row; drop it and use the lookup, which follows the linked record.`);
5928
5928
  }
5929
5929
  }
@@ -6951,6 +6951,370 @@ function normalizeComputedFields(spec, fixes) {
6951
6951
  }
6952
6952
  }
6953
6953
 
6954
+ // src/templates/describe/mechanical-shapes.ts
6955
+ var squash2 = (s) => String(s ?? "").toLowerCase().replace(/[^a-z0-9]/g, "");
6956
+ var singular2 = (s) => s.replace(/ies$/, "y").replace(/s$/, "");
6957
+ var same3 = (a, b) => squash2(a) !== "" && (squash2(a) === squash2(b) || singular2(squash2(a)) === singular2(squash2(b)));
6958
+ var tableOf = (spec, token) => allTables(spec).find((t) => t.token === token);
6959
+ function fieldKey(t, name) {
6960
+ if (!t || typeof name !== "string") return void 0;
6961
+ return t.fields.find((f) => f.key === name)?.key ?? t.fields.find((f) => same3(f.key, name))?.key ?? t.fields.find((f) => same3(f.label, name))?.key;
6962
+ }
6963
+ var VIEW_ALIASES = {
6964
+ groupBy: ["group_by", "groupby", "groupField", "group_field", "columnField", "column_field"],
6965
+ dateField: ["date_field", "datefield", "dateColumn", "date_column", "calendarField"],
6966
+ startField: ["start_field", "startDate", "start_date", "start"],
6967
+ endField: ["end_field", "endDate", "end_date", "end"],
6968
+ coverField: ["cover_field", "coverImage", "cover_image", "imageField", "image_field", "photoField", "photo_field"],
6969
+ swimlaneBy: ["swimlane_by", "swimlaneField", "swimlane_field", "lanes"]
6970
+ };
6971
+ var EQ_OPS = /* @__PURE__ */ new Set(["eq", "is", "equals", "equal", "=", "==", "===", "equal_to", "is_equal"]);
6972
+ var NEQ_OPS = /* @__PURE__ */ new Set(["neq", "ne", "is_not", "isnot", "not_equal", "!=", "<>", "not_equals", "not_in", "nin", "not_any_of"]);
6973
+ var IN_OPS = /* @__PURE__ */ new Set(["in", "is_any_of", "any_of", "one_of", "in_list", "is_one_of"]);
6974
+ var EMPTY_OPS = /* @__PURE__ */ new Set(["is_empty", "empty", "is_null", "null", "unset", "is_blank", "blank", "is_unset"]);
6975
+ function sortDirection(v) {
6976
+ const s = String(v ?? "").toLowerCase().replace(/[^a-z]/g, "");
6977
+ if (["asc", "ascending", "up", "az", "oldest", "lowest", "first"].includes(s)) return "asc";
6978
+ if (["desc", "descending", "down", "za", "newest", "highest", "last"].includes(s)) return "desc";
6979
+ return void 0;
6980
+ }
6981
+ function fixViewShapes(spec, fixes) {
6982
+ for (const view of spec.views ?? []) {
6983
+ const t = tableOf(spec, view.table);
6984
+ const at = `views.${view.token}`;
6985
+ for (const [canon, alts] of Object.entries(VIEW_ALIASES)) {
6986
+ if (view[canon] == null) {
6987
+ const alt = alts.find((a) => view[a] != null && typeof view[a] === "string");
6988
+ if (alt) {
6989
+ view[canon] = view[alt];
6990
+ delete view[alt];
6991
+ fixes.push(`${at}.${canon}: written as "${alt}"`);
6992
+ }
6993
+ }
6994
+ if (typeof view[canon] === "string") {
6995
+ const k = fieldKey(t, view[canon]);
6996
+ if (k && k !== view[canon]) {
6997
+ fixes.push(`${at}.${canon}: "${view[canon]}" is the field "${k}"`);
6998
+ view[canon] = k;
6999
+ }
7000
+ }
7001
+ }
7002
+ const rawSorts = view.sorts ?? view.sort ?? view.orderBy ?? view.order_by;
7003
+ if (rawSorts !== void 0) {
7004
+ const list = Array.isArray(rawSorts) ? rawSorts : typeof rawSorts === "string" ? rawSorts.split(",") : rawSorts && typeof rawSorts === "object" ? [rawSorts] : [];
7005
+ const out = [];
7006
+ for (const s of list) {
7007
+ let name, dir;
7008
+ if (typeof s === "string") {
7009
+ const m = /^\s*(-?)\s*([^\s]+)(?:\s+(asc|desc|ascending|descending))?\s*$/i.exec(s);
7010
+ if (m) {
7011
+ name = m[2];
7012
+ dir = m[3] ?? (m[1] ? "desc" : "asc");
7013
+ }
7014
+ } else if (s && typeof s === "object") {
7015
+ name = s.field ?? s.key ?? s.column ?? s.by ?? s.name;
7016
+ dir = s.direction ?? s.dir ?? s.order ?? s.sort ?? "asc";
7017
+ }
7018
+ const key = fieldKey(t, name) ?? (typeof name === "string" ? name : void 0);
7019
+ const d = sortDirection(dir);
7020
+ if (key && d) out.push({ field: key, direction: d });
7021
+ }
7022
+ const changed = JSON.stringify(rawSorts) !== JSON.stringify(out) || view.sort !== void 0 || view.orderBy !== void 0 || view.order_by !== void 0;
7023
+ delete view.sort;
7024
+ delete view.orderBy;
7025
+ delete view.order_by;
7026
+ if (out.length) view.sorts = out;
7027
+ else delete view.sorts;
7028
+ if (changed) fixes.push(`${at}.sorts: written in the store's shape [{field, direction}]`);
7029
+ }
7030
+ if (Array.isArray(view.filters)) {
7031
+ const filters = {};
7032
+ const anyOf = { ...view.anyOf ?? {} };
7033
+ const dropped = [];
7034
+ for (const c of view.filters) {
7035
+ if (!c || typeof c !== "object") continue;
7036
+ const name = c.field ?? c.key ?? c.column ?? c.name;
7037
+ const key = fieldKey(t, name) ?? (typeof name === "string" ? name : void 0);
7038
+ if (!key) continue;
7039
+ const f = t?.fields.find((x) => x.key === key);
7040
+ const op = String(c.op ?? c.operator ?? c.comparison ?? "eq").toLowerCase().trim().replace(/[\s-]+/g, "_");
7041
+ const has = "value" in c || "values" in c;
7042
+ const val = "value" in c ? c.value : c.values;
7043
+ const setEq = (v) => {
7044
+ if (key in filters || key in anyOf) dropped.push(`${key} (two conditions on one field)`);
7045
+ else filters[key] = v;
7046
+ };
7047
+ const setAny = (vals) => {
7048
+ if (key in filters || key in anyOf) dropped.push(`${key} (two conditions on one field)`);
7049
+ else if (vals.length === 1) filters[key] = vals[0];
7050
+ else anyOf[key] = vals;
7051
+ };
7052
+ const universe = f?.parityType === "checkbox" ? [true, false] : f?.choices?.length ? [...f.choices] : void 0;
7053
+ if (EMPTY_OPS.has(op)) setEq(null);
7054
+ else if (EQ_OPS.has(op) && has && !Array.isArray(val)) setEq(val);
7055
+ else if ((IN_OPS.has(op) || EQ_OPS.has(op)) && Array.isArray(val) && val.length) setAny(val);
7056
+ else if (NEQ_OPS.has(op) && has && universe) {
7057
+ const gone = new Set(Array.isArray(val) ? val : [val]);
7058
+ const left = universe.filter((x) => !gone.has(x));
7059
+ if (left.length) setAny(left);
7060
+ else dropped.push(`${key} ${op}`);
7061
+ } else dropped.push(`${key} ${op}`);
7062
+ }
7063
+ view.filters = filters;
7064
+ if (Object.keys(anyOf).length) view.anyOf = anyOf;
7065
+ fixes.push(`${at}.filters: an array of conditions written as the store's map (field \u2192 value; several values \u2192 anyOf)${dropped.length ? `; not expressible and dropped, so the view shows more rows than asked: ${dropped.join(", ")}` : ""}`);
7066
+ }
7067
+ }
7068
+ }
7069
+ function dropDuplicateViews(spec, fixes) {
7070
+ const seen = /* @__PURE__ */ new Map();
7071
+ const kept = [];
7072
+ for (const v of spec.views ?? []) {
7073
+ const { token: _t, name: _n, isDefault: _d, ...shape } = v;
7074
+ const sig = JSON.stringify(Object.keys(shape).sort().map((k) => [k, shape[k]]));
7075
+ const twin = seen.get(sig);
7076
+ if (!twin) {
7077
+ seen.set(sig, v);
7078
+ kept.push(v);
7079
+ continue;
7080
+ }
7081
+ if (v.isDefault) twin.isDefault = true;
7082
+ fixes.push(`views.${v.token}: "${v.name}" showed exactly what "${twin.name}" shows; dropped`);
7083
+ }
7084
+ if (kept.length !== (spec.views ?? []).length) spec.views = kept;
7085
+ }
7086
+ var KIND_KEYS = ["kind", "type", "fieldType", "field_type", "dataType", "data_type", "parity_type"];
7087
+ var KIND_SYNONYMS = {
7088
+ string: "text",
7089
+ shorttext: "text",
7090
+ singlelinetext: "text",
7091
+ varchar: "text",
7092
+ longtext: "long_text",
7093
+ textarea: "long_text",
7094
+ paragraph: "long_text",
7095
+ multiline: "long_text",
7096
+ richtext: "long_text",
7097
+ integer: "number",
7098
+ int: "number",
7099
+ decimal: "number",
7100
+ float: "number",
7101
+ numeric: "number",
7102
+ money: "currency",
7103
+ price: "currency",
7104
+ amount: "currency",
7105
+ boolean: "checkbox",
7106
+ bool: "checkbox",
7107
+ tick: "checkbox",
7108
+ yesno: "checkbox",
7109
+ date: "datetime",
7110
+ timestamp: "datetime",
7111
+ datetime: "datetime",
7112
+ time: "datetime",
7113
+ dropdown: "select",
7114
+ enum: "select",
7115
+ choice: "select",
7116
+ singleselect: "select",
7117
+ multiselect: "multi_select",
7118
+ tags: "multi_select",
7119
+ tel: "phone",
7120
+ telephone: "phone",
7121
+ mobile: "phone",
7122
+ mail: "email",
7123
+ emailaddress: "email",
7124
+ website: "url",
7125
+ link: "url",
7126
+ uri: "url"
7127
+ };
7128
+ function inferKind(label, key) {
7129
+ const s = `${key.replace(/_/g, " ")} ${label}`.toLowerCase();
7130
+ if (/\be-?mail\b/.test(s)) return "email";
7131
+ if (/\b(phone|mobile|cell|telephone|fax)\b/.test(s)) return "phone";
7132
+ if (/\b(url|website|web site|link)\b/.test(s)) return "url";
7133
+ if (/\b(amount|fee|price|cost|total|balance|budget|salary|wage|deposit|revenue|payment)\b/.test(s)) return "currency";
7134
+ if (/\b(date|when|due|deadline|birthday|dob|start|end|time|at)\b/.test(s) || /_(on|at)$/.test(key)) return "datetime";
7135
+ if (/\b(notes?|description|details|comments?|summary|instructions)\b/.test(s)) return "long_text";
7136
+ if (/\b(count|quantity|qty|hours|score|age|number of|size|weight|rating)\b/.test(s)) return "number";
7137
+ if (/\b(name|title|owner|label|code|id|reference)\b/.test(s)) return "text";
7138
+ return void 0;
7139
+ }
7140
+ var wordOf = (v) => {
7141
+ if (typeof v !== "string") return void 0;
7142
+ if (RESOLVABLE_FIELD_TYPE_WORDS.includes(v)) return v;
7143
+ const syn = KIND_SYNONYMS[squash2(v)];
7144
+ return syn && RESOLVABLE_FIELD_TYPE_WORDS.includes(syn) ? syn : void 0;
7145
+ };
7146
+ function fixFieldKinds(spec, fixes) {
7147
+ for (const t of spec.tables) {
7148
+ for (const f of t.fields) {
7149
+ const raw = f;
7150
+ if (typeof raw.parityType === "string" && RESOLVABLE_FIELD_TYPE_WORDS.includes(raw.parityType)) continue;
7151
+ const at = `tables.${t.token}.fields.${f.key}`;
7152
+ const alias = KIND_KEYS.find((k) => raw[k] != null && typeof raw[k] === "string");
7153
+ let kind = wordOf(raw.parityType) ?? (alias ? wordOf(raw[alias]) : void 0);
7154
+ let how = alias ? `its "${alias}" says "${raw[alias]}"` : raw.parityType ? `"${raw.parityType}" is not a type word` : "it names no type";
7155
+ if (!kind && !alias && !raw.parityType) {
7156
+ kind = inferKind(String(f.label ?? ""), String(f.key ?? ""));
7157
+ how = `it names no type; "${f.label}" plainly is one`;
7158
+ }
7159
+ if (!kind) continue;
7160
+ for (const k of KIND_KEYS) if (k !== "parityType" && raw[k] === raw[alias ?? ""] && alias) delete raw[k];
7161
+ f.parityType = kind;
7162
+ if ((kind === "phone" || kind === "email") && !raw.format) raw.format = kind;
7163
+ fixes.push(`${at}: type ${kind} (${how})`);
7164
+ }
7165
+ }
7166
+ }
7167
+ function fixFormulaRefs(spec, fixes) {
7168
+ for (const t of spec.tables) {
7169
+ for (const f of t.fields) {
7170
+ if (f.parityType !== "formula" || typeof f.formula !== "string") continue;
7171
+ const others = t.fields.filter((x) => x.key !== f.key);
7172
+ const labelFor = (name) => others.find((x) => x.label === name)?.label ?? others.find((x) => x.key === name)?.label ?? others.find((x) => same3(x.key, name))?.label ?? others.find((x) => same3(x.label, name))?.label;
7173
+ const text = f.formula.replace(/^\s*=\s*/, "");
7174
+ let out = "";
7175
+ let i = 0;
7176
+ while (i < text.length) {
7177
+ const c = text[i];
7178
+ if (c === "{") {
7179
+ const j = text.indexOf("}", i);
7180
+ if (j < 0) {
7181
+ out += text.slice(i);
7182
+ break;
7183
+ }
7184
+ const name = text.slice(i + 1, j).trim();
7185
+ out += `{${labelFor(name) ?? name}}`;
7186
+ i = j + 1;
7187
+ } else if (c === "'" || c === '"') {
7188
+ const j = text.indexOf(c, i + 1);
7189
+ if (j < 0) {
7190
+ out += text.slice(i);
7191
+ break;
7192
+ }
7193
+ out += text.slice(i, j + 1);
7194
+ i = j + 1;
7195
+ } else if (/[A-Za-z_]/.test(c)) {
7196
+ const m = /^[A-Za-z_][A-Za-z_0-9]*/.exec(text.slice(i))[0];
7197
+ const after = text.slice(i + m.length).trimStart()[0];
7198
+ const label = after === "(" ? void 0 : labelFor(m);
7199
+ out += label ? `{${label}}` : m;
7200
+ i += m.length;
7201
+ } else {
7202
+ out += c;
7203
+ i += 1;
7204
+ }
7205
+ }
7206
+ if (out !== f.formula) {
7207
+ fixes.push(`tables.${t.token}.fields.${f.key}.formula: fields named by label in braces ("${f.formula}" \u2192 "${out}")`);
7208
+ f.formula = out;
7209
+ }
7210
+ }
7211
+ }
7212
+ }
7213
+ function fixInverseKeys(spec, fixes) {
7214
+ const rels = spec.relationships ?? [];
7215
+ const taken = /* @__PURE__ */ new Set();
7216
+ for (const t of allTables(spec)) for (const f of t.fields) taken.add(`${t.token}.${f.key}`);
7217
+ for (const r of rels) if (r.inverseKey) taken.add(`${r.toTable}.${r.inverseKey}`);
7218
+ for (const r of rels) {
7219
+ if (!r.inverseKey) continue;
7220
+ const target = tableOf(spec, r.toTable);
7221
+ if (!target?.fields.some((f) => f.key === r.inverseKey)) continue;
7222
+ let k = `${r.fromTable}_${r.fromField}_back`;
7223
+ for (let n = 2; taken.has(`${r.toTable}.${k}`); n++) k = `${r.fromTable}_${r.fromField}_back_${n}`;
7224
+ fixes.push(`relationships: ${r.fromTable}.${r.fromField} \u2192 ${r.toTable}: inverseKey "${r.inverseKey}" is already a field of ${r.toTable}; renamed "${k}"`);
7225
+ taken.add(`${r.toTable}.${k}`);
7226
+ r.inverseKey = k;
7227
+ }
7228
+ }
7229
+ var STATUS_WORDS = /\b(open|booked|upcoming|unpaid|outstanding|active|ordered|pending|overdue|scheduled|confirmed|paid|completed|current|remaining)\b/i;
7230
+ function fixRollupFilters(spec, fixes) {
7231
+ for (const t of spec.tables) {
7232
+ for (const f of t.fields) {
7233
+ if (f.parityType !== "rollup" || f.filter && Object.keys(f.filter).length || !f.via) continue;
7234
+ const hit = STATUS_WORDS.exec(`${f.label} ${f.key.replace(/_/g, " ")}`);
7235
+ const via = t.fields.find((x) => x.key === f.via);
7236
+ const far = via?.relationTarget ? tableOf(spec, via.relationTarget) : void 0;
7237
+ if (!hit || !far) continue;
7238
+ const word = hit[1].toLowerCase();
7239
+ const cands = [];
7240
+ for (const x of far.fields) {
7241
+ if (["select", "multi_select"].includes(x.parityType)) {
7242
+ for (const c of x.choices ?? []) if (c.toLowerCase() === word) cands.push({ key: x.key, value: c });
7243
+ }
7244
+ if (x.parityType === "checkbox" && new RegExp(`(^|_)${word}($|_)`).test(x.key)) cands.push({ key: x.key, value: true });
7245
+ if (x.parityType === "checkbox" && word === "unpaid" && /(^|_)paid($|_)/.test(x.key)) cands.push({ key: x.key, value: false });
7246
+ }
7247
+ if (cands.length !== 1) continue;
7248
+ f.filter = { [cands[0].key]: cands[0].value };
7249
+ fixes.push(`tables.${t.token}.fields.${f.key}: "${hit[1]}" in its name now picks those records: filter ${JSON.stringify(f.filter)}`);
7250
+ }
7251
+ }
7252
+ }
7253
+ function fictional(p) {
7254
+ const h = [...p].reduce((n, ch) => (n * 31 + ch.charCodeAt(0)) % 1e3, 7);
7255
+ const nn = String(h % 100).padStart(2, "0");
7256
+ const uk = String(h).padStart(3, "0");
7257
+ if (/^\s*\+/.test(p) && !/^\s*\+1\b/.test(p)) return `+44 20 7946 0${uk}`;
7258
+ if (/^\s*0\d/.test(p)) return `020 7946 0${uk}`;
7259
+ return p.replace(/(\d{3})([\s.-]?)(\d{4})\s*$/, (_m, _a, sep) => `555${sep}01${nn}`);
7260
+ }
7261
+ function fixFictionalPhones(spec, fixes) {
7262
+ const json = JSON.stringify(spec);
7263
+ const hits = /* @__PURE__ */ new Set();
7264
+ const isPhoneField = (f) => f.parityType === "phone" || /(^|_)(phone|mobile|cell|fax|tel)(_|$)/.test(f.key);
7265
+ const biz = spec.business?.phone;
7266
+ if (typeof biz === "string") for (const p of phonesOutsideFictionalRange(biz, true)) hits.add(p);
7267
+ for (const t of spec.tables) for (const r of t.rows ?? []) for (const f of t.fields) {
7268
+ const v = r.values?.[f.key];
7269
+ if (typeof v === "string") for (const p of phonesOutsideFictionalRange(v, isPhoneField(f))) hits.add(p);
7270
+ }
7271
+ for (const p of phonesOutsideFictionalRange(json)) hits.add(p);
7272
+ let out = json;
7273
+ let n = 0;
7274
+ for (const p of hits) {
7275
+ const escaped = JSON.stringify(p).slice(1, -1);
7276
+ if (!out.includes(escaped)) continue;
7277
+ out = out.split(escaped).join(JSON.stringify(fictional(p)).slice(1, -1));
7278
+ n += 1;
7279
+ }
7280
+ if (!n) return;
7281
+ const next = JSON.parse(out);
7282
+ for (const k of Object.keys(spec)) delete spec[k];
7283
+ Object.assign(spec, next);
7284
+ fixes.push(`phones: ${n} number(s) that could reach a real phone replaced with fictional ones (555-0100\u20260199, UK Ofcom drama range)`);
7285
+ }
7286
+ function fixTeamBlockCopy(spec, fixes) {
7287
+ const block = SHARED_BLOCKS.team_member;
7288
+ const used = /* @__PURE__ */ new Set();
7289
+ for (const v of spec.views ?? []) for (const k of ["groupBy", "dateField", "startField", "endField", "coverField", "swimlaneBy", "measure"]) if (v.table && v[k]) used.add(`${v.table}.${v[k]}`);
7290
+ for (const fm of spec.forms ?? []) for (const q2 of fm.fields ?? []) used.add(`${fm.table}.${q2.fieldKey}`);
7291
+ for (const e of spec.extras ?? []) for (const q2 of e.questions ?? []) used.add(`${e.table}.${q2.fieldKey}`);
7292
+ for (const t of spec.tables) {
7293
+ const meaning = (f) => block.fields.find((bf) => bf.parityType === f.parityType && bf.sameAs.some((w) => same3(w, f.label) || same3(w, f.key)));
7294
+ const hits = t.fields.map((f) => ({ f, bf: meaning(f) })).filter((x) => x.bf);
7295
+ if (hits.length < block.copyThreshold) continue;
7296
+ const hired = hits.some((h) => h.bf.key === "start_date");
7297
+ const hasRole = hits.some((h) => h.bf.key === "role");
7298
+ const contactOfSomeone = t.fields.some((f) => f.parityType === "relation" && f.relationTarget && f.relationTarget !== block.table && spec.tables.some((o) => o.token === f.relationTarget));
7299
+ if (!(hired || hasRole && !contactOfSomeone)) continue;
7300
+ const order = ["full_name", "role", "work_email", "mobile", "start_date"];
7301
+ const keep = new Set(hits.filter((h) => ["full_name", "role"].includes(h.bf.key)).map((h) => h.f.key));
7302
+ const dropped = [];
7303
+ let left = hits.length;
7304
+ for (const h of [...hits].sort((a, b) => order.indexOf(b.bf.key) - order.indexOf(a.bf.key))) {
7305
+ if (left < block.copyThreshold) break;
7306
+ if (keep.has(h.f.key) || used.has(`${t.token}.${h.f.key}`) || t.titleField === h.f.key) continue;
7307
+ dropped.push(h.f.label);
7308
+ t.fields = t.fields.filter((x) => x.key !== h.f.key);
7309
+ for (const r of t.rows ?? []) if (r.values) delete r.values[h.f.key];
7310
+ left -= 1;
7311
+ }
7312
+ if (left >= block.copyThreshold) continue;
7313
+ if (!t.fields.some((f) => f.key === "notes")) t.fields.push({ key: "notes", label: "Notes", parityType: "long_text", sensitivity: "internal", contextPolicy: "include" });
7314
+ fixes.push(`tables.${t.token}: copied the shared team_member block; kept as an ordinary table (${t.fields.map((f) => f.label).join(", ")}); dropped ${dropped.join(", ") || "nothing"}`);
7315
+ }
7316
+ }
7317
+
6954
7318
  // src/templates/describe/autofix.ts
6955
7319
  var clone2 = (v) => JSON.parse(JSON.stringify(v));
6956
7320
  var SENSITIVITY_RANK2 = ["public", "internal", "confidential", "restricted"];
@@ -7272,7 +7636,15 @@ function switchJob(spec, fixes) {
7272
7636
  function autofixDescribeSpec(input, ctx = {}) {
7273
7637
  const spec = describeSpec(clone2(input));
7274
7638
  const autoFixes = [];
7639
+ fixFieldKinds(spec, autoFixes);
7275
7640
  normalizeComputedFields(spec, autoFixes);
7641
+ fixFormulaRefs(spec, autoFixes);
7642
+ fixViewShapes(spec, autoFixes);
7643
+ dropDuplicateViews(spec, autoFixes);
7644
+ fixInverseKeys(spec, autoFixes);
7645
+ fixRollupFilters(spec, autoFixes);
7646
+ fixTeamBlockCopy(spec, autoFixes);
7647
+ fixFictionalPhones(spec, autoFixes);
7276
7648
  fixMissingQuestionFields(spec, autoFixes);
7277
7649
  raiseSensitivity(spec, autoFixes);
7278
7650
  flagAskedOfSubject(spec, ctx, autoFixes);
@@ -7504,6 +7876,22 @@ var LOOKUP_SHAPE_FACTS = {
7504
7876
  rule: 'a lookup is a field that shows one value of the record a relation field points at: parityType "lookup", via = the key of a relation field of THIS table, pick = the key of a field on the far table. Write exactly these keys on the field itself \u2014 never a nested {lookup:{\u2026}} object.',
7505
7877
  example: { key: "client_phone", label: "Client phone", parityType: "lookup", via: "client", pick: "phone" }
7506
7878
  };
7879
+ var VIEW_FILTERS_SHAPE_FACTS = {
7880
+ rule: "a view's `filters` is ONE object of field key \u2192 value (a plain value is equality, null means empty); several allowed values of one field go in `anyOf` as field key \u2192 [values]; `sorts` is a list of {field, direction: asc|desc}; groupBy, dateField, startField, endField, coverField are field KEYS. Never write filters or sort as a list of {field, op, value}. The store cannot filter on 'not', 'contains', 'less than' or 'is not empty' \u2014 leave such a condition out.",
7881
+ example: { token: "open_requests", name: "Open requests", table: "request", kind: "kanban", groupBy: "stage", filters: { priority: "High" }, anyOf: { stage: ["New", "In progress"] }, sorts: [{ field: "due_on", direction: "asc" }] }
7882
+ };
7883
+ var FIELD_SHAPE_FACTS = {
7884
+ rule: "every field is {key, label, parityType, sensitivity, contextPolicy}; the type word goes in `parityType` (never `kind` or `type`) and is one of field_kinds.",
7885
+ example: { key: "visit_fee", label: "Visit fee", parityType: "currency", sensitivity: "internal", contextPolicy: "include" }
7886
+ };
7887
+ var FORMULA_SHAPE_FACTS = {
7888
+ rule: "a formula names every field by its LABEL in braces ({Quantity}), never by key; operators are + - * / % only (no >, <, = or comparisons), functions are IF, ROUND, SUM, DATEDIFF and the other store functions; it never reads a lookup or rollup, nor itself.",
7889
+ example: { key: "line_total", label: "Line total", parityType: "formula", formula: "{Quantity} * {Unit price}" }
7890
+ };
7891
+ var RELATIONSHIP_SHAPE_FACTS = {
7892
+ rule: "a relationship's inverseKey is the name the OTHER table sees this link under; it must not be a key that table already has as a field. A parent that lists its children owns ONE multi relation field (multi true); the child's single link to the parent is a second field, and their inverseKeys differ from every field key.",
7893
+ example: { fromTable: "request", fromField: "client", toTable: "client", flavor: "referenced", cardinality: "one", onDelete: "restrict", inverseKey: "requests_from" }
7894
+ };
7507
7895
  function describeVocabulary() {
7508
7896
  return {
7509
7897
  field_kinds: RESOLVABLE_FIELD_TYPE_WORDS,
@@ -7529,6 +7917,12 @@ function describeVocabulary() {
7529
7917
  view_shape: VIEW_SHAPE_FACTS,
7530
7918
  rollup_shape: ROLLUP_SHAPE_FACTS,
7531
7919
  lookup_shape: LOOKUP_SHAPE_FACTS,
7920
+ view_filters_shape: VIEW_FILTERS_SHAPE_FACTS,
7921
+ field_shape: FIELD_SHAPE_FACTS,
7922
+ formula_shape: FORMULA_SHAPE_FACTS,
7923
+ relationship_shape: RELATIONSHIP_SHAPE_FACTS,
7924
+ phone_rule: "seed phone numbers are fictional: 555-0100 to 555-0199 in North America (e.g. (813) 555-0142) or a UK Ofcom drama number (e.g. 020 7946 0123); never a real-looking number.",
7925
+ team_rule: "the team (name, role, work email, mobile, start date) is the shared team_member block; do not declare a table with those fields \u2014 keep a table of your own to just name and role, or point a relation field at team_member.",
7532
7926
  title_field: "a table's titleField is the key of a plain text-like field of that same table (a name, a title) \u2014 never a link to another table, a formula or a timed date"
7533
7927
  };
7534
7928
  }