@sigloch/graph-api-core 5.8.0 → 5.9.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.
@@ -137,6 +137,14 @@ export class FormatECodec {
137
137
  }
138
138
  continue;
139
139
  }
140
+ // CR-SM-369: a `### <TYPE>` header outside `## Nodes` was swallowed by the `#` skip
141
+ // below — the node lines after it then failed as a bare "Unrecognized line". One
142
+ // canonical form (the serializer writes `## Nodes`), so the header is an error that
143
+ // names the section, not a second accepted spelling.
144
+ if (typeSection) {
145
+ errors.push(`Type section "### ${typeSection[1]}" outside "## Nodes" — "### <TYPE>" and its node lines (+ ~ - !) belong under "## Nodes", edges under "## Edges"`);
146
+ continue;
147
+ }
140
148
  if (line.startsWith('#'))
141
149
  continue;
142
150
  // Merge operations
@@ -168,8 +176,13 @@ export class FormatECodec {
168
176
  }
169
177
  continue;
170
178
  }
171
- if (line.length > 0)
172
- errors.push(`Unrecognized line: "${line}"`);
179
+ // CR-SM-369: every fall-through names the section the line belongs in.
180
+ if (section === 'edges') {
181
+ errors.push(`Line under "## Edges" is not an edge (expected "<source> -<arrow>-> <target>"): "${line}" — node lines (+ ~ - !) belong under "## Nodes" + "### <TYPE>"`);
182
+ }
183
+ else {
184
+ errors.push(`Line outside any section: "${line}" — node lines (+ ~ - !) belong under "## Nodes" + "### <TYPE>", edges under "## Edges"`);
185
+ }
173
186
  }
174
187
  return { operations, errors };
175
188
  }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * commandsToFormatE — MutateCommands als Format-E-Text, die Rueckrichtung der Tuer (ITEM-2026-604).
3
+ *
4
+ * Wer einen Batch als Kommandos baut (GVE-Editor, graphify, aise), schreibt ihn hiermit als den
5
+ * Text, den `graph_mutate` annimmt. EIN Serialisierer neben dem Codec, nicht einer je Konsument.
6
+ * Knotenzeilen schreibt der Codec selbst (`SE_FORMAT_E_CODEC.serialize`, samt `@key`-Folgezeilen
7
+ * und `[__name:…]`); was ein Zustand nicht ausdrueckt, steht als Zeile der Sprache:
8
+ *
9
+ * update-node `~ uid[|beschreibung]` — ein Patch: ohne Pipe bleibt die Beschreibung, wie sie ist
10
+ * delete-node `- uid` unter dem Typabschnitt (die Sprache verlangt ihn fuer jede Knotenzeile)
11
+ * delete-edge `- A -t-> B`
12
+ * update-edge Loeschen plus Anlegen: Typwechsel, Flip (Richtung), Attribute aus `set`
13
+ * merge-nodes `M quelle + ziel`
14
+ *
15
+ * Den Typ eines Knotens, den der Batch nicht anlegt, kennt nur der Bestand — `typeOf` liefert ihn.
16
+ * Ohne Typ wirft die Funktion, statt einen Abschnitt zu raten.
17
+ * @author andreas@siglochconsulting
18
+ */
19
+ import type { MutateCommand } from '@sigloch/contracts/harness';
20
+ type TypeOf = (uid: string) => string | undefined;
21
+ export declare function commandsToFormatE(commands: readonly MutateCommand[], typeOf?: TypeOf): string;
22
+ export {};
@@ -0,0 +1,81 @@
1
+ import { SE_FORMAT_E_CODEC } from './format-e-door.js';
2
+ /** Die Zeilen EINES Knotens, wie der Codec sie schreibt — ohne `## Nodes` und `### TYPE`. */
3
+ function knotenZeilen(node, roundTrip) {
4
+ return SE_FORMAT_E_CODEC.serialize({ nodes: [node], edges: [] }, { roundTrip }).split('\n').slice(2);
5
+ }
6
+ export function commandsToFormatE(commands, typeOf = () => undefined) {
7
+ const angelegt = new Map();
8
+ for (const c of commands)
9
+ if (c.op === 'add-node')
10
+ angelegt.set(c.node.uid, c.node.type);
11
+ const typ = (uid, gegeben) => {
12
+ const t = gegeben ?? angelegt.get(uid) ?? typeOf(uid);
13
+ if (!t)
14
+ throw new Error(`commandsToFormatE: Typ von "${uid}" unbekannt — typeOf liefert ihn aus dem Bestand`);
15
+ return t;
16
+ };
17
+ const jeTyp = new Map();
18
+ const zeilenFuer = (t) => {
19
+ const z = jeTyp.get(t) ?? [];
20
+ jeTyp.set(t, z);
21
+ return z;
22
+ };
23
+ const loeschKanten = [];
24
+ const neueKanten = [];
25
+ const merges = [];
26
+ for (const c of commands) {
27
+ switch (c.op) {
28
+ case 'add-node':
29
+ zeilenFuer(c.node.type).push(...knotenZeilen({ ...c.node, attributes: c.node.attributes ?? {} }, true));
30
+ break;
31
+ case 'update-node': {
32
+ const { uid, name, description } = c.node;
33
+ const attributes = { ...(c.node.attributes ?? {}), ...(name !== undefined ? { __name: name } : {}) };
34
+ const [kopf, ...folge] = knotenZeilen({ uid, type: typ(uid, c.node.type), name: name ?? uid, description: description ?? '', attributes }, false);
35
+ // Eine explizit leere Beschreibung braucht den Pipe — ohne ihn hiesse die Zeile „unveraendert".
36
+ const zeile = description === '' ? kopf.replace(`+ ${uid}`, `~ ${uid}|`) : kopf.replace(/^\+ /, '~ ');
37
+ zeilenFuer(typ(uid, c.node.type)).push(zeile, ...folge);
38
+ break;
39
+ }
40
+ case 'delete-node':
41
+ zeilenFuer(typ(c.uid)).push(`- ${c.uid}`);
42
+ break;
43
+ case 'add-edge':
44
+ neueKanten.push({ ...c.edge, attributes: c.edge.attributes ?? {} });
45
+ break;
46
+ case 'delete-edge':
47
+ loeschKanten.push(`- ${c.edge.sourceId} -${c.edge.edgeType}-> ${c.edge.targetId}`);
48
+ break;
49
+ case 'update-edge': {
50
+ const { sourceId, targetId, edgeType } = c.edge;
51
+ loeschKanten.push(`- ${sourceId} -${edgeType}-> ${targetId}`);
52
+ const flip = c.set.flip === true;
53
+ neueKanten.push({
54
+ sourceId: flip ? targetId : sourceId,
55
+ targetId: flip ? sourceId : targetId,
56
+ edgeType: c.set.edgeType ?? edgeType,
57
+ attributes: c.set.attributes ?? {},
58
+ });
59
+ break;
60
+ }
61
+ case 'merge-nodes':
62
+ merges.push(`M ${c.sourceUid} + ${c.targetUid}`);
63
+ break;
64
+ }
65
+ }
66
+ const teile = [];
67
+ if (jeTyp.size > 0) {
68
+ teile.push(['## Nodes', ...[...jeTyp.keys()].sort().flatMap((t) => [`### ${t}`, ...(jeTyp.get(t) ?? [])])].join('\n'));
69
+ }
70
+ // OHNE roundTrip: dessen Validierung verlangt jeden Endpunkt als Knoten im selben Graphen —
71
+ // ein Batch haengt aber an Knoten, die im Bestand stehen.
72
+ const anlegen = neueKanten.length
73
+ ? SE_FORMAT_E_CODEC.serialize({ nodes: [], edges: neueKanten }).trim().split('\n').filter((z) => z !== '## Edges')
74
+ : [];
75
+ // Loeschen vor Anlegen: ein Typwechsel legt dieselbe Kante mit neuem Typ an.
76
+ if (loeschKanten.length + anlegen.length > 0)
77
+ teile.push(['## Edges', ...loeschKanten, ...anlegen].join('\n'));
78
+ if (merges.length > 0)
79
+ teile.push(['## Merges', ...merges].join('\n'));
80
+ return teile.join('\n\n');
81
+ }
package/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export { GraphNodeSchema, GraphEdgeSchema, GraphSchema, GraphScopeSchema, GraphQ
14
14
  export { GraphService } from './graph-service.js';
15
15
  export { FormatECodec } from './format-e-codec.js';
16
16
  export { SE_FORMAT_E_CODEC, FormatEInputSchema } from './format-e-door.js';
17
+ export { commandsToFormatE } from './format-e-commands.js';
17
18
  export type { Rule, RuleViolation, RuleEngine } from './rule-engine.js';
18
19
  export { DefaultRuleEngine } from './rule-engine.js';
19
20
  export type { AuditEntry, AuditLog, OperationsLog } from './audit.js';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export { GraphService } from './graph-service.js';
15
15
  export { FormatECodec } from './format-e-codec.js';
16
16
  // CR-SM-359: die Tuer des Format-E-Vertrags und die eine SE-Codec-Instanz — Konsumenten bauen keine eigene.
17
17
  export { SE_FORMAT_E_CODEC, FormatEInputSchema } from './format-e-door.js';
18
+ export { commandsToFormatE } from './format-e-commands.js';
18
19
  export { DefaultRuleEngine } from './rule-engine.js';
19
20
  export { InMemoryAuditLog, FileOperationsLog, AUDIT_FILE, AUDIT_BASENAME, DEFAULT_COMPACT_BYTES } from './audit.js';
20
21
  export { MemoryAdapter } from './memory-adapter.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sigloch/graph-api-core",
3
- "version": "5.8.0",
3
+ "version": "5.9.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",