@synap-core/workspace-templates 0.74.1 → 0.76.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/types.ts CHANGED
@@ -140,6 +140,20 @@ export interface TemplateProperty {
140
140
  enumValues?: string[];
141
141
  constraints?: Record<string, unknown>;
142
142
  targetProfileSlug?: string;
143
+ /**
144
+ * The relation type (a relation_def slug) this `entity_id` property creates:
145
+ * setting a value links the entity to the target through the property sync,
146
+ * so the reference is a graph edge a view or traversal can follow. Both pod
147
+ * provisioning doors write it to `property_defs.relation_def_id`
148
+ * (`resolvePropertyRelationDefs`, backend#1498). The slug must be one this
149
+ * template's `entityLinks` / `relationDefs` (or a dependency, or a pod-wide
150
+ * default) defines; never an exposure relation (`visible_to`,
151
+ * `belongs_to_project`); ONE property per relation per template. Only entity
152
+ * values written after the template applies link: rows that already hold a
153
+ * value link through `relations.backfill`. Validated by
154
+ * `checkPropertyRelations` in validate.ts.
155
+ */
156
+ relation?: string;
143
157
  }
144
158
 
145
159
  export interface TemplateProfile {
package/src/validate.ts CHANGED
@@ -682,6 +682,74 @@ function goalNamesStatus(
682
682
  );
683
683
  }
684
684
 
685
+ /**
686
+ * A property's `relation` (the relation type its entity_id value creates) must
687
+ * be one the pod provisioning door can apply: `resolvePropertyRelationDefs`
688
+ * (synap-backend) REFUSES, at apply time and with only a report entry, a slug
689
+ * that resolves nowhere, an exposure relation, a relation declared by a second
690
+ * property of the SAME kind, and a property that is not an `entity_id` (the
691
+ * property sync is keyed on the source entity's own profile, so two kinds may
692
+ * both declare `part_of_campaign`; two properties of one kind may not). Catch
693
+ * the same four here so a refusal never reaches a pod.
694
+ */
695
+ const EXPOSURE_RELATIONS = new Set(["visible_to", "belongs_to_project"]);
696
+
697
+ function checkPropertyRelations(
698
+ tpl: WorkspaceYaml,
699
+ resolveDep: (slug: string) => WorkspaceYaml | undefined,
700
+ corpus: WorkspaceYaml[] | undefined,
701
+ add: (rule: string, path: string, message: string) => void
702
+ ): void {
703
+ let known: Set<string> | null = null;
704
+ for (const p of tpl.profiles || []) {
705
+ const claimed = new Map<string, string>();
706
+ for (const pr of p.properties || []) {
707
+ if (pr.relation === undefined) continue;
708
+ const at = `profiles.${p.slug}.${pr.slug}.relation`;
709
+ const rel = pr.relation;
710
+ if (typeof rel !== "string" || !rel) {
711
+ add(
712
+ "property-relation-invalid",
713
+ at,
714
+ `property ${p.slug}.${pr.slug} has an empty or non-string relation`
715
+ );
716
+ continue;
717
+ }
718
+ if (EXPOSURE_RELATIONS.has(rel)) {
719
+ add(
720
+ "property-relation-exposure",
721
+ at,
722
+ `property ${p.slug}.${pr.slug} declares "${rel}", an exposure relation set through its own door (sharing or project filing), never from a property`
723
+ );
724
+ continue;
725
+ }
726
+ if (pr.valueType !== "entity_id") {
727
+ add(
728
+ "property-relation-not-entity-id",
729
+ at,
730
+ `property ${p.slug}.${pr.slug} declares relation "${rel}" but is "${pr.valueType}", not entity_id: only an entity reference can link`
731
+ );
732
+ }
733
+ known ??= knownRelationTypes(tpl, resolveDep, corpus);
734
+ if (!known.has(rel)) {
735
+ add(
736
+ "property-relation-unknown",
737
+ at,
738
+ `property ${p.slug}.${pr.slug} declares relation "${rel}", which no entityLinks[].type / relationDefs[].slug of this template (or its dependencies) defines`
739
+ );
740
+ }
741
+ const first = claimed.get(rel);
742
+ if (first) {
743
+ add(
744
+ "property-relation-duplicate",
745
+ at,
746
+ `property ${p.slug}.${pr.slug} declares relation "${rel}", already created by ${first}: one property per relation per kind`
747
+ );
748
+ } else claimed.set(rel, `${p.slug}.${pr.slug}`);
749
+ }
750
+ }
751
+ }
752
+
685
753
  function checkProcessContract(
686
754
  tpl: WorkspaceYaml,
687
755
  resolveDep: (slug: string) => WorkspaceYaml | undefined,
@@ -1471,7 +1539,10 @@ function checkPresentationDeclarations(
1471
1539
  * pod's triggers strip them from any other write, so a default or an option
1472
1540
  * list would be a promise the pod refuses (rule `property-server-derived-value`).
1473
1541
  */
1474
- export const SERVER_DERIVED_PROPERTY_KEYS: readonly string[] = ["workedIn", "workedInAll"];
1542
+ export const SERVER_DERIVED_PROPERTY_KEYS: readonly string[] = [
1543
+ "workedIn",
1544
+ "workedInAll",
1545
+ ];
1475
1546
 
1476
1547
  /**
1477
1548
  * The server-derived keys that hold ONE string, so a kanban can group by them:
@@ -1489,7 +1560,9 @@ function checkServerDerivedProperties(
1489
1560
  if (!SERVER_DERIVED_PROPERTY_KEYS.includes(pr.slug)) continue;
1490
1561
  const c = (pr.constraints ?? {}) as Record<string, unknown>;
1491
1562
  const valued = [
1492
- ...(Array.isArray(pr.enumValues) && pr.enumValues.length > 0 ? ["enumValues"] : []),
1563
+ ...(Array.isArray(pr.enumValues) && pr.enumValues.length > 0
1564
+ ? ["enumValues"]
1565
+ : []),
1493
1566
  ...("defaultValue" in c ? ["constraints.defaultValue"] : []),
1494
1567
  ...("enum" in c ? ["constraints.enum"] : []),
1495
1568
  ];
@@ -1965,6 +2038,37 @@ function hasCorpus(
1965
2038
 
1966
2039
  // ── The validator ────────────────────────────────────────────────────────────
1967
2040
 
2041
+ /**
2042
+ * The declared-request caps and shape. MIRRORS of `SPACE_INTENT_COUNT_CAP`,
2043
+ * `SPACE_INTENT_PHRASE_CAP` and the locale pattern in
2044
+ * `@synap-core/types/space-brief`, and of the glue-word list of the pod's
2045
+ * matcher (api `services/discover/space-intents.ts`, accents folded). The
2046
+ * pod's matcher drops these words before it counts a phrase's words, so a
2047
+ * phrase made of them routes nothing. `onboarding-intents.validate.test.ts`
2048
+ * pins the values; the sources are in another repo.
2049
+ */
2050
+ export const SPACE_INTENT_COUNT_CAP = 8;
2051
+ export const SPACE_INTENT_PHRASE_CAP = 80;
2052
+ const INTENT_LOCALE = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})?$/;
2053
+ const INTENT_GLUE_WORDS: ReadonlySet<string> = new Set(
2054
+ (
2055
+ "a about an and are as at be by can could do does for from help how i in into is it like me my need of on or our " +
2056
+ "please should some that the this to us want we what when where which with would you your " +
2057
+ "au aux avec ce ces cet cette ci dans de des du elle en es est et il ils c d j je m l la le les leur leurs ma mes " +
2058
+ "merci mon n nos notre nous on ou par peut peux peuvent plait pour qu que qui s sa ses son stp sur svp t ta te tes " +
2059
+ "toi ton tu un une voudrais veux votre vos vous y aimerais aimerai faut falloir besoin aide moi pouvez pourrais pourriez"
2060
+ ).split(" ")
2061
+ );
2062
+ /** The words of a phrase that carry a request: accents folded, glue words out. */
2063
+ export function intentMeaningfulWords(phrase: string): number {
2064
+ return phrase
2065
+ .normalize("NFD")
2066
+ .replace(/\p{M}+/gu, "")
2067
+ .toLowerCase()
2068
+ .split(/[^\p{L}\p{N}]+/u)
2069
+ .filter((w) => w !== "" && !INTENT_GLUE_WORDS.has(w)).length;
2070
+ }
2071
+
1968
2072
  /**
1969
2073
  * Validate ONE template. Pure: returns `{ ok, errors }`, never exits. Covers the
1970
2074
  * per-template rules formerly split across build.mjs `validate()` (required
@@ -2472,6 +2576,80 @@ export function validateTemplate(
2472
2576
  );
2473
2577
  }
2474
2578
 
2579
+ // Declared requests (`onboarding.intents`, space contract L3): the same
2580
+ // caps and shape as the pod's reader and matcher. A phrase the pod would
2581
+ // clip, drop or never match is an authoring error HERE, not a silent loss
2582
+ // there. Mirrored, not imported, for the reason the lifecycle constants
2583
+ // above are: this validator also runs from the published dist on the CP
2584
+ // and the CLI, whose resolved `@synap-core/types` may predate `intents`.
2585
+ // Sources: `SpaceBrief.intents` / `SPACE_INTENT_*_CAP` / `readIntents`
2586
+ // (@synap-core/types/space-brief) and `meaningfulWords` (api
2587
+ // services/discover/space-intents.ts). "One language per phrase" is the
2588
+ // author's to keep (nothing can read it mechanically); `locale` says which.
2589
+ const intents = (onboarding as { intents?: unknown }).intents;
2590
+ if (intents !== undefined) {
2591
+ if (!Array.isArray(intents)) {
2592
+ add(
2593
+ "onboarding-intents-invalid",
2594
+ "onboarding.intents",
2595
+ "onboarding.intents must be a list of { phrase, locale? }"
2596
+ );
2597
+ } else {
2598
+ if (intents.length > SPACE_INTENT_COUNT_CAP)
2599
+ add(
2600
+ "onboarding-intents-too-many",
2601
+ "onboarding.intents",
2602
+ `onboarding.intents has ${intents.length} phrases; the pod keeps the first ${SPACE_INTENT_COUNT_CAP} and drops the rest`
2603
+ );
2604
+ const seenIntent = new Set<string>();
2605
+ intents.forEach((raw, i) => {
2606
+ const where = `onboarding.intents.${i}`;
2607
+ const phrase =
2608
+ raw && typeof raw === "object"
2609
+ ? (raw as { phrase?: unknown }).phrase
2610
+ : undefined;
2611
+ if (typeof phrase !== "string" || phrase.trim() === "") {
2612
+ add(
2613
+ "onboarding-intent-invalid",
2614
+ where,
2615
+ "an intent is an object with a non-empty `phrase` (a bare string is read by the pod but never authored)"
2616
+ );
2617
+ return;
2618
+ }
2619
+ if (Array.from(phrase).length > SPACE_INTENT_PHRASE_CAP)
2620
+ add(
2621
+ "onboarding-intent-too-long",
2622
+ `${where}.phrase`,
2623
+ `intent "${phrase.slice(0, 30)}…" is over ${SPACE_INTENT_PHRASE_CAP} characters: the pod clips it`
2624
+ );
2625
+ if (intentMeaningfulWords(phrase) < 2)
2626
+ add(
2627
+ "onboarding-intent-words",
2628
+ `${where}.phrase`,
2629
+ `intent "${phrase}" has fewer than two meaningful words (glue words like "the", "to", "for", "le", "de" do not count): one word routes nothing`
2630
+ );
2631
+ const locale = (raw as { locale?: unknown }).locale;
2632
+ if (
2633
+ locale !== undefined &&
2634
+ (typeof locale !== "string" || !INTENT_LOCALE.test(locale))
2635
+ )
2636
+ add(
2637
+ "onboarding-intent-locale",
2638
+ `${where}.locale`,
2639
+ `intent locale ${JSON.stringify(locale)} is not a BCP 47 language (en, fr, pt-BR): the pod drops it`
2640
+ );
2641
+ const key = phrase.trim().toLowerCase();
2642
+ if (seenIntent.has(key))
2643
+ add(
2644
+ "onboarding-intent-duplicate",
2645
+ `${where}.phrase`,
2646
+ `intent "${phrase}" repeats an earlier one: the pod keeps the first`
2647
+ );
2648
+ seenIntent.add(key);
2649
+ });
2650
+ }
2651
+ }
2652
+
2475
2653
  if (
2476
2654
  onboarding.openingQuestions !== undefined &&
2477
2655
  (!Array.isArray(onboarding.openingQuestions) ||
@@ -2785,6 +2963,7 @@ export function validateTemplate(
2785
2963
  checkSharedRoleReferences(tpl, resolveDepFrom(opts), add);
2786
2964
  checkStageDomains(tpl, resolveDepFrom(opts), add);
2787
2965
  checkProcessContract(tpl, resolveDepFrom(opts), opts.corpus, add);
2966
+ checkPropertyRelations(tpl, resolveDepFrom(opts), opts.corpus, add);
2788
2967
  checkKanbanGrouping(tpl, resolveDepFrom(opts), opts.corpus, add);
2789
2968
  checkServerDerivedProperties(tpl, add);
2790
2969
  checkLifecycleDeclarations(tpl, resolveDepFrom(opts), opts.corpus, add);