suppa-mcp-2 1.43.4 → 1.43.5

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/dist/server.js CHANGED
@@ -2713,9 +2713,9 @@ export function buildToolTable() {
2713
2713
  " FULL state (an omitted variable is deleted), so an empty one is never sent and one that\n" +
2714
2714
  " drops the form's default variable is refused. mobileSchema, code, localizations, entity,\n" +
2715
2715
  " alias and parent are never sent; a child form's `schema.overrides` is preserved.\n\n" +
2716
- " Refuses while someone else has the form open in the builder (lockedBy). Without\n" +
2717
- " confirm=true it returns the plan and a `revision`; pass it back as expected_revision and\n" +
2718
- " a form that moved since the preview is refused, not overwritten.",
2716
+ " Refuses: someone else's lock (lockedBy), another form's file, a child's layer over a root,\n" +
2717
+ " a merged tree over a child, an emptied form, a published read. The file's `revision` (or\n" +
2718
+ " expected_revision) guards it: a form that moved is refused, not overwritten.",
2719
2719
  inputSchema: {
2720
2720
  form_id: z.number().describe("The Forms row id — the `form=` in the builder URL."),
2721
2721
  path: z
@@ -885,13 +885,81 @@ function staleAnswer(message, id) {
885
885
  current_revision: current,
886
886
  };
887
887
  }
888
+ const isFileObj = (v) => Boolean(v) && typeof v === "object" && !Array.isArray(v);
889
+ /** The one record of a `records` answer (suppa_search_records), else the file itself. */
890
+ const fileBody = (v) => {
891
+ if (!isFileObj(v))
892
+ return null;
893
+ const recs = v.records;
894
+ return Array.isArray(recs) && recs.length === 1 && isFileObj(recs[0]) ? recs[0] : v;
895
+ };
896
+ // The marker alone, wherever the read sits: an agent that copied the answer without `generation`, or kept it inside
897
+ // `records`, still holds a published read (review of 1.43.4).
898
+ const isPublishedRead = (v) => fileBody(v)?.read === "published";
899
+ const isRefId = (v) => Number.isInteger(v) && v > 0 ? v : isFileObj(v) && Number.isInteger(v.id) && v.id > 0 ? v.id : null;
900
+ /** The form a file was read from, when it says: a suppa_get_form answer's `id`, a previous-draft file's `form_id`, a
901
+ * one-record Forms read's row id. A FormVersions row's `id` is a VERSION, so a snapshot says nothing here. */
902
+ function fileFormId(v) {
903
+ if (!isFileObj(v))
904
+ return null;
905
+ if (typeof v.generation === "string")
906
+ return isRefId(v.id);
907
+ if (Number.isInteger(v.form_id))
908
+ return v.form_id;
909
+ const body = fileBody(v);
910
+ return body && body !== v && !("snapshot" in body) ? isRefId(body.id) : null;
911
+ }
912
+ /** The parent a file declares — a real reference only (parent / parentForm / parentVersion). */
913
+ function fileParent(v) {
914
+ const body = fileBody(v);
915
+ return body ? isRefId(body.parentForm) ?? isRefId(body.parent) : null;
916
+ }
917
+ /** The revision a suppa_get_form answer was read at — the guard of the write it becomes. */
918
+ const fileRevision = (v) => (isFileObj(v) && typeof v.generation === "string" && typeof v.revision === "string" ? v.revision : null);
919
+ const elementIds = (list) => Array.isArray(list) ? list.map((e) => (isFileObj(e) && typeof e.id === "string" ? e.id : null)).filter((x) => Boolean(x)) : [];
888
920
  /**
889
- * A suppa_get_form version='published' answer is what this user sees, not the draft. For a child form it
890
- * is the MERGED tree with no `parent` (the published read carries none), so writing it as the draft would
891
- * turn the parent's elements and variables into the child's own layer; for any form it would revert the
892
- * draft's later edits. It is read-only — the published version comes back through rollback.
921
+ * What a draft must not do to the form it is written to, judged against the LIVE row — each a refusal:
922
+ * another form's file; a child's layer over a form that is no child; a child's draft that carries elements it
923
+ * INHERITS (a merged tree — the published read, a FormVersions snapshot, or one copied out of either: written as the
924
+ * child's own layer they detach those elements from the parent); every element of a populated form removed.
925
+ * The inherited ids are the parent's published elements; when that read fails the check is said to be skipped.
893
926
  */
894
- const isPublishedRead = (v) => Boolean(v && typeof v === "object" && !Array.isArray(v) && v.generation === "2.x" && v.read === "published");
927
+ async function writeRefusals(id, file, row, draftComponents) {
928
+ // (a published read is refused before this — in the preview even when the row cannot be read)
929
+ const refusals = [];
930
+ const notes = [];
931
+ const fromForm = fileFormId(file);
932
+ if (fromForm !== null && fromForm !== id) {
933
+ refusals.push(`this file was read from form ${fromForm}, not form ${id}: written here it would replace form ${id}'s draft with another form's. Read form ${id} (suppa_get_form) and edit that.`);
934
+ }
935
+ const rowParent = isRefId(row.parent);
936
+ const declared = fileParent(file);
937
+ if (declared !== null && rowParent === null) {
938
+ refusals.push(`this file is a child form's layer (its parent is form ${declared}), and form ${id} is not a child: a child's own layer over a root form would remove the elements it has.`);
939
+ }
940
+ const own = elementIds(isFileObj(row.schema) ? row.schema.components : null);
941
+ const draftIds = elementIds(draftComponents);
942
+ if (draftIds.length === 0 && own.length > 0) {
943
+ refusals.push(`the draft has no elements and form ${id} has ${own.length}: saving it would delete every one. Remove elements in the builder if that is really meant.`);
944
+ }
945
+ if (rowParent !== null && draftIds.length) {
946
+ try {
947
+ const parent = (await makeRequest("GET", `/api/core/forms/${rowParent}/schema`, null, { query: { mode: "published" } }));
948
+ const inherited = new Set(elementIds(isFileObj(parent?.schema) ? parent.schema.components : null));
949
+ const ownSet = new Set(own);
950
+ const carried = draftIds.filter((x) => inherited.has(x) && !ownSet.has(x));
951
+ if (carried.length) {
952
+ refusals.push(`the draft carries ${carried.length} element(s) form ${id} INHERITS from its parent, form ${rowParent} (${carried.slice(0, 5).join(", ")}${carried.length > 5 ? ", …" : ""}): ` +
953
+ "a merged tree — a published read, a FormVersions snapshot, or one copied out of either. A child's draft is its OWN layer: " +
954
+ "read it with suppa_get_form (the draft) and edit that.");
955
+ }
956
+ }
957
+ catch (e) {
958
+ notes.push(`the inherited-element check was skipped: form ${rowParent}'s published elements could not be read (${e instanceof Error ? e.message : String(e)}).`);
959
+ }
960
+ }
961
+ return { refusals, notes };
962
+ }
895
963
  const PUBLISHED_READ_REFUSAL = "this file is a suppa_get_form version='published' read — the version users see (a child form's MERGED tree, " +
896
964
  "without its own layer), not the draft. Saved as the draft it would revert the draft's later edits and turn a " +
897
965
  "child's inherited elements into its own. Read the draft (suppa_get_form without version) to edit it; to bring a " +
@@ -1040,11 +1108,30 @@ export async function writeFormDraft(formId, path, confirm = false, normalize =
1040
1108
  variables = `the form's variables could not be read (${e instanceof Error ? e.message : String(e)}); the confirmed call reads them again and refuses if that fails`;
1041
1109
  }
1042
1110
  }
1043
- if (revision) {
1111
+ // What the confirmed call will refuse outright, said now — the same guards, against the same row.
1112
+ const fileValue = peeked.error ? null : peeked.value;
1113
+ const published = isPublishedRead(fileValue);
1114
+ let refused = published;
1115
+ if (row && fileValue && !published) {
1116
+ const guards = await writeRefusals(id, fileValue, row, draftComponents);
1117
+ for (const r of guards.refusals)
1118
+ warnings.push(`the confirmed call will REFUSE this file: ${r}`);
1119
+ warnings.push(...guards.notes);
1120
+ refused = guards.refusals.length > 0;
1121
+ }
1122
+ const fromFile = fileRevision(fileValue);
1123
+ if (fromFile && revision && fromFile !== revision) {
1124
+ warnings.push(`the form changed since this file was read (read at ${fromFile}, now ${revision}): the confirmed call sends the file's revision and the ` +
1125
+ "platform refuses it, so the change made since is not overwritten — read the form again (suppa_get_form) and redo the edit on it.");
1126
+ }
1127
+ else if (fromFile) {
1128
+ warnings.push(`the confirmed call is guarded by the revision this file was read at (${fromFile}): it is refused, not written, if the form changed since.`);
1129
+ }
1130
+ else if (revision) {
1044
1131
  warnings.push(`pass expected_revision: "${revision}" on the confirmed call — it is then refused, not written, if the form changed since this preview.`);
1045
1132
  }
1046
1133
  return confirmRequired("save a form DRAFT (it never publishes)", { form_id: id, path: target }, {
1047
- writes: ["schema", "settings", ...(draftVars ? ["variables"] : [])],
1134
+ writes: refused ? [] : ["schema", "settings", ...(draftVars ? ["variables"] : [])],
1048
1135
  never_sent: NEVER_SENT,
1049
1136
  publishes: false,
1050
1137
  draft: preview,
@@ -1146,6 +1233,12 @@ export async function writeFormDraft(formId, path, confirm = false, normalize =
1146
1233
  }
1147
1234
  if (gate.refused || !gate.ok)
1148
1235
  return refusal(gate);
1236
+ // 3b. WHAT THE DRAFT MUST NOT DO TO THIS FORM — another form's file, a child's layer over a root form, a merged tree
1237
+ // over a child, every element removed. Judged against the live row, because the file can say anything.
1238
+ const guards = await writeRefusals(id, read.value, row, gate.draft?.schema);
1239
+ if (guards.refusals.length) {
1240
+ return jsonResponse({ written: false, error: guards.refusals.join(" "), refusals: guards.refusals, note: "Nothing was written." });
1241
+ }
1149
1242
  // 4. SOMEONE ELSE HAS IT OPEN. The builder holds `lockedBy` while a form is being edited; writing
1150
1243
  // under them means one of the two of you loses work without being told.
1151
1244
  const lockedBy = row.lockedBy;
@@ -1215,7 +1308,11 @@ export async function writeFormDraft(formId, path, confirm = false, normalize =
1215
1308
  if (existingSettings.isFormBuilderV2 === true && settings.isFormBuilderV2 !== true) {
1216
1309
  settings.isFormBuilderV2 = true; // the host's routing flag — an edit must not drop it
1217
1310
  }
1218
- const revision = expectedRevision || (typeof row.updatedAt === "string" ? row.updatedAt : null);
1311
+ // The guard is the revision the edit was BASED on: the caller's, else the one the file was read at (a suppa_get_form
1312
+ // answer carries it), and only else the row as read just now — which guards nothing about a save made in between.
1313
+ const fromFile = fileRevision(read.value);
1314
+ const revision = expectedRevision || fromFile || (typeof row.updatedAt === "string" ? row.updatedAt : null);
1315
+ const revisionFrom = expectedRevision ? "expected_revision" : fromFile ? "the file (the revision suppa_get_form read)" : "the row as read for this write";
1219
1316
  const body = { schema: schemaColumn, settings };
1220
1317
  if (draftVars)
1221
1318
  body.variables = draftVars;
@@ -1233,8 +1330,9 @@ export async function writeFormDraft(formId, path, confirm = false, normalize =
1233
1330
  // difference between a setting you decided to keep and one you did not know was still there.
1234
1331
  settings_kept_because_the_draft_is_silent_about_them: Object.keys(existingSettings).filter((k) => !(k in (draft.settings ?? {}))),
1235
1332
  guarded_by_revision: revision,
1333
+ revision_from: revisionFrom,
1236
1334
  warnings: gate.warnings,
1237
- notes: gate.notes,
1335
+ notes: [...(gate.notes ?? []), ...guards.notes],
1238
1336
  ...(gate.already_live.length ? { already_on_the_live_form: alreadyLive(gate.already_live) } : {}),
1239
1337
  ...(() => {
1240
1338
  if (row.parent?.id)
@@ -396,7 +396,7 @@ const nonEmpty = (v) => Array.isArray(v) && v.length > 0;
396
396
  /**
397
397
  * What only a 2.x form has: elements in the schema column, a Code Form or its build, a mobile layout, the
398
398
  * builder's flag, a parent (a child's own layer can be empty — its settings, elements and code inherited),
399
- * or overrides of a parent's elements, settings, connections or texts.
399
+ * overrides of a parent's elements, settings, connections or texts, or FormVariables rows.
400
400
  */
401
401
  function hasV2Evidence(a) {
402
402
  const schema = isObj(a.schema) ? a.schema : {};
@@ -409,7 +409,9 @@ function hasV2Evidence(a) {
409
409
  a.parentForm != null ||
410
410
  nonEmpty(a.settingsOverrides) ||
411
411
  nonEmpty(a.connectionOverrides) ||
412
- nonEmpty(a.localizationOverrides));
412
+ nonEmpty(a.localizationOverrides) ||
413
+ // FormVariables rows are 2.x only: measured on dev 2026-10-22, none of the 77 Suppa 1.0 forms has one
414
+ nonEmpty(a.variables));
413
415
  }
414
416
  /** An FB2 kind alone is weaker evidence: `Forms.type` is free text, and a 1.0 form can carry one. */
415
417
  const hasV2Kind = (a) => typeof a.type === "string" && V2_FORM_TYPES.has(a.type);
@@ -426,7 +428,8 @@ function artifactSummary(art) {
426
428
  * Empty lists and nulls are dropped; the compiled bundle is summarized unless asked for.
427
429
  */
428
430
  function v2FormAnswer(a, version, includeArtifact) {
429
- const { schema, mobileSchema, settings, variables, localizations, code, codeArtifact, codeArtifacts, dataSources, updatedAt, ...meta } = a;
431
+ // dataSources stays in `meta`: always [] today («Increment 2»), dropped as empty — but kept the day it is not.
432
+ const { schema, mobileSchema, settings, variables, localizations, code, codeArtifact, codeArtifacts, updatedAt, ...meta } = a;
430
433
  const notes = [];
431
434
  const parent = isObj(meta.parentForm) ? meta.parentForm : null;
432
435
  if (parent && version === "draft") {
@@ -475,14 +478,25 @@ function v2FormAnswer(a, version, includeArtifact) {
475
478
  * (formShema / formSettings / formModul).
476
479
  */
477
480
  export async function getForm(formId, version = "draft", includeArtifact = false) {
481
+ const where = `GET /core/forms/${formId}/schema${version === "published" ? "?mode=published" : ""}`;
478
482
  let assembled = null;
479
483
  try {
480
484
  const got = await makeRequest("GET", `/api/core/forms/${formId}/schema`, null, version === "published" ? { query: { mode: "published" } } : undefined);
481
- assembled = isObj(got) ? got : null;
485
+ // A 200 that is not a form is not «no builder read»: answering the row read instead would call a 2.x form 1.0.
486
+ if (!isObj(got))
487
+ throw new Error(`${where} answered 200 with ${got === null ? "null" : Array.isArray(got) ? "a list" : typeof got}, not a form`);
488
+ assembled = got;
482
489
  }
483
490
  catch (e) {
484
491
  if (e?.status !== 404)
485
492
  throw e;
493
+ // Only the platform's own «Form N not found» (data.not_found) means there is no such form for the builder; a
494
+ // 404 from anything else (a route this stand does not have, a proxy) is said, never read as «a 1.0 form».
495
+ const message = e instanceof Error ? e.message : String(e);
496
+ // (every 404's text starts «HTTP 404 Not Found» — it is the platform's body that says which one this is)
497
+ if (!/data\.not_found|Form \d+ not found/.test(message)) {
498
+ throw new Error(`${where} answered a 404 that is not the platform's «form not found»: ${message}`);
499
+ }
486
500
  if (version === "published") {
487
501
  return jsonResponse({
488
502
  error: `Form ${formId} has no published version this user sees (or does not exist). version='draft' reads the draft.`,
@@ -500,8 +514,18 @@ export async function getForm(formId, version = "draft", includeArtifact = false
500
514
  if (assembled && hasV2Kind(assembled) && !nonEmpty(formShema)) {
501
515
  return jsonResponse(v2FormAnswer(assembled, version, includeArtifact));
502
516
  }
503
- if (result === null)
517
+ if (result === null) {
518
+ // The builder read found the form, the row read did not: it is no deleted form — the row (a 1.0 form's layout)
519
+ // is what this user cannot read.
520
+ if (assembled) {
521
+ return jsonResponse({
522
+ error: `Form ${formId} exists (the builder read answered) but its Forms row could not be read — no access to the ` +
523
+ "row, most likely. The builder read holds no 2.x layout, so this is a Suppa 1.0 form whose `data` is in that row.",
524
+ type: assembled.type ?? null,
525
+ });
526
+ }
504
527
  return notFoundAnswer(ENTITY_FORMS, formId, "form");
528
+ }
505
529
  return jsonResponse({ generation: "1.0", ...result });
506
530
  }
507
531
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "suppa-mcp-2",
3
- "version": "1.43.4",
3
+ "version": "1.43.5",
4
4
  "description": "MCP server for the Suppa platform (modern.suppa.me) — Tasks, Docs/Pages, Entities & Schema, Forms, Automations, and file attachments, with multi-tenant Google auth. Node port of PyPI suppa-mcp.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -230,14 +230,19 @@ export function normalizeEnvelope(input) {
230
230
  // is not optional.
231
231
  // An EMPTY `overrides` array does not make a child: measured on dev, 3 parentless forms carry
232
232
  // `schema.overrides: []` while 12 of the 16 real children carry a populated one. Only a declared
233
- // parent, or overrides with something in them, is evidence.
233
+ // parent, or overrides with something in them, is evidence — and a parent counts only as a REAL reference: a
234
+ // positive id, or an object holding one. `parentForm: {}` / `{ id: null }` / `0` / `"none"` declared nothing, and
235
+ // taking them for a parent let an empty layer through (review of 1.43.4).
236
+ const isRef = (v) =>
237
+ (Number.isInteger(v) && v > 0) ||
238
+ (v !== null && typeof v === "object" && !Array.isArray(v) && Number.isInteger(v.id) && v.id > 0);
234
239
  const childForm = Boolean(
235
240
  (Array.isArray(overrides) && overrides.length) ||
236
241
  (col && Array.isArray(col.overrides) && col.overrides.length) ||
237
- (input.parent !== undefined && input.parent !== null) ||
242
+ isRef(input.parent) ||
238
243
  // suppa_get_form names the parent `parentForm`, as GET /core/forms/:id/schema does
239
- (input.parentForm !== undefined && input.parentForm !== null) ||
240
- (input.parentVersion !== undefined && input.parentVersion !== null),
244
+ isRef(input.parentForm) ||
245
+ isRef(input.parentVersion),
241
246
  );
242
247
 
243
248
  if (!schema && emptyAt.length && !childForm) {