@synap-core/workspace-templates 0.38.1 → 0.39.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.
package/src/validate.ts CHANGED
@@ -804,6 +804,7 @@ export const LIFECYCLE_OPTION_FIELDS = [
804
804
  "stuckAfterDays",
805
805
  "dateBinding",
806
806
  "waits",
807
+ "requires",
807
808
  ] as const;
808
809
  export const LIFECYCLE_OPTION_WAITS_FIELDS = ["on", "waitKind", "refFrom", "dateBinding"] as const;
809
810
 
@@ -909,6 +910,90 @@ export function lifecycleOptionWaitsProblems(option: {
909
910
  return out;
910
911
  }
911
912
 
913
+ /** MIRROR of `LIFECYCLE_REQUIREMENT_OWNERS` / `_DIRECTIONS` (`@synap-core/types/lifecycle`, `option.ts`). */
914
+ export const LIFECYCLE_REQUIREMENT_OWNERS = ["you", "agent"] as const;
915
+ export const LIFECYCLE_REQUIREMENT_DIRECTIONS = ["out", "in"] as const;
916
+ /** MIRROR of `REQUIREMENTS_PER_OPTION_MAX` (the pod's `lifecycle-model.ts`). */
917
+ export const REQUIREMENTS_PER_OPTION_MAX = 10;
918
+ /** MIRROR of the pod's `BLOCKER_KEY`: lowercase letters, digits and `_`. */
919
+ const BLOCKER_KEY = /^[a-z][a-z0-9_]{0,63}$/;
920
+
921
+ /**
922
+ * The problem (words) with one option's `requires`, or null: a MIRROR of the
923
+ * pod's `parseOptionRequires` (`packages/database/src/utils/lifecycle-model.ts`),
924
+ * same rules in the same order, so the catalog accepts a `requires` exactly when
925
+ * the pod does. Names are checked for SHAPE only (the pod does not check that a
926
+ * relation type or a property exists: a role may name the property of the kind
927
+ * wearing it). Pinned to the pod by `lifecycle-requires.parity.test.ts`.
928
+ */
929
+ export function lifecycleOptionRequiresProblem(option: {
930
+ key?: unknown;
931
+ category?: unknown;
932
+ requires?: unknown;
933
+ }): string | null {
934
+ const raw = option.requires;
935
+ if (raw === undefined) return null;
936
+ const key = String(option.key);
937
+ const at = (i: number) => `option "${key}" requires[${i}]`;
938
+ if (!Array.isArray(raw)) return `option "${key}" requires must be a list of { kind: "link" | "property", blockerKey, ... }`;
939
+ if (raw.length > 0 && (option.category === "done" || option.category === "lost"))
940
+ return `option "${key}" is ${String(option.category)}: a finished thing requires nothing (requires is for open, active or paused options)`;
941
+ if (raw.length > REQUIREMENTS_PER_OPTION_MAX) return `option "${key}" requires at most ${REQUIREMENTS_PER_OPTION_MAX} things`;
942
+ const seen = new Set<string>();
943
+ for (const [i, item] of raw.entries()) {
944
+ if (!item || typeof item !== "object" || Array.isArray(item)) return `${at(i)} must be an object`;
945
+ const r = item as Record<string, unknown>;
946
+ if (typeof r.blockerKey !== "string" || !BLOCKER_KEY.test(r.blockerKey))
947
+ return `${at(i)}.blockerKey must be a lowercase name (letters, digits, _), e.g. "no_contact"`;
948
+ if (seen.has(r.blockerKey)) return `${at(i)}.blockerKey "${r.blockerKey}" is declared twice`;
949
+ seen.add(r.blockerKey);
950
+ if (r.label !== undefined && (typeof r.label !== "string" || !r.label.trim())) return `${at(i)}.label must be words`;
951
+ if (r.owner !== undefined && !(LIFECYCLE_REQUIREMENT_OWNERS as readonly unknown[]).includes(r.owner))
952
+ return `${at(i)}.owner must be one of ${LIFECYCLE_REQUIREMENT_OWNERS.join(" | ")}`;
953
+ if (r.clearedBy !== undefined && !requirementClearedByOk(r.clearedBy))
954
+ return `${at(i)}.clearedBy must be one of { answer: true } | { intent } | { playbookId } | { capability }`;
955
+ if (
956
+ r.forKinds !== undefined &&
957
+ (!Array.isArray(r.forKinds) ||
958
+ r.forKinds.length === 0 ||
959
+ !r.forKinds.every((k) => typeof k === "string" && LIFECYCLE_PROPERTY_SLUG.test(k)))
960
+ )
961
+ return `${at(i)}.forKinds must be a list of kind slugs (e.g. ["person"])`;
962
+ const common = ["kind", "blockerKey", "label", "owner", "clearedBy", "forKinds"];
963
+ if (r.kind === "link") {
964
+ if (typeof r.relation !== "string" || !LIFECYCLE_PROPERTY_SLUG.test(r.relation))
965
+ return `${at(i)}.relation must name a relation type (its slug, e.g. "works_at")`;
966
+ if (!(LIFECYCLE_REQUIREMENT_DIRECTIONS as readonly unknown[]).includes(r.direction))
967
+ return `${at(i)}.direction must be "out" (the thing is the link's source) or "in" (its target)`;
968
+ if (r.min !== undefined && !(typeof r.min === "number" && Number.isInteger(r.min) && r.min >= 1 && r.min <= 1000))
969
+ return `${at(i)}.min must be a whole number from 1 to 1000`;
970
+ const unknown = Object.keys(r).filter((k) => ![...common, "relation", "direction", "min"].includes(k));
971
+ if (unknown.length > 0) return `${at(i)} has unknown field(s): ${unknown.join(", ")}`;
972
+ continue;
973
+ }
974
+ if (r.kind === "property") {
975
+ if (typeof r.property !== "string" || !LIFECYCLE_PROPERTY_SLUG.test(r.property))
976
+ return `${at(i)}.property must name a property (its slug, e.g. "linkedinUrl")`;
977
+ if (r.in !== undefined && !(Array.isArray(r.in) && r.in.length > 0 && r.in.every((v) => typeof v === "string" && v.trim())))
978
+ return `${at(i)}.in must be a non-empty list of values`;
979
+ const unknown = Object.keys(r).filter((k) => ![...common, "property", "in"].includes(k));
980
+ if (unknown.length > 0) return `${at(i)} has unknown field(s): ${unknown.join(", ")}`;
981
+ continue;
982
+ }
983
+ return `${at(i)}.kind must be "link" or "property"`;
984
+ }
985
+ return null;
986
+ }
987
+
988
+ /** Exactly one of `{answer: true}`, `{intent}`, `{playbookId}`, `{capability}` (the pod's `parseRequirementClearedBy`). */
989
+ function requirementClearedByOk(raw: unknown): boolean {
990
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return false;
991
+ const c = raw as Record<string, unknown>;
992
+ if (Object.keys(c).length !== 1) return false;
993
+ if (c.answer === true) return true;
994
+ return (["intent", "playbookId", "capability"] as const).some((k) => typeof c[k] === "string" && (c[k] as string).trim() !== "");
995
+ }
996
+
912
997
  /** Key-sorted JSON, so two equal declarations compare equal whatever the YAML key order. */
913
998
  function stableJson(v: unknown): string {
914
999
  if (Array.isArray(v)) return `[${v.map(stableJson).join(",")}]`;
@@ -994,6 +1079,8 @@ function checkLifecycleDeclarations(
994
1079
  }
995
1080
  for (const p of lifecycleOptionWaitsProblems(o ?? {}))
996
1081
  add("lifecycle-option-shape", `${at}.options[${i}].${p.field}`, p.message);
1082
+ const requiresProblem = lifecycleOptionRequiresProblem(o ?? {});
1083
+ if (requiresProblem) add("lifecycle-option-shape", `${at}.options[${i}].requires`, requiresProblem);
997
1084
  });
998
1085
  }
999
1086
  }