mdld-parse 0.9.1 โ†’ 0.9.3

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.
Files changed (59) hide show
  1. package/README.md +34 -1
  2. package/dist/.nojekyll +0 -0
  3. package/dist/CNAME +1 -0
  4. package/dist/README.md +425 -0
  5. package/dist/docs/API.md +397 -0
  6. package/dist/docs/Architecture.md +190 -0
  7. package/dist/docs/Guide.md +322 -0
  8. package/dist/docs/LLM-MDLD-Inducer.md +550 -0
  9. package/dist/docs/Parser.md +654 -0
  10. package/dist/docs/Performance.md +238 -0
  11. package/dist/docs/Spec-render.md +1439 -0
  12. package/dist/docs/Subject.md +180 -0
  13. package/dist/docs/Syntax.md +463 -0
  14. package/dist/docs/TodoList.md +215 -0
  15. package/dist/docs/Use-Cases.md +354 -0
  16. package/dist/docs/index.html +108 -0
  17. package/dist/docs/index.md +56 -0
  18. package/dist/docs/origin.md +459 -0
  19. package/dist/docs/polarity.md +586 -0
  20. package/dist/docs/primary-subject.md +199 -0
  21. package/dist/docs/statements.md +226 -0
  22. package/dist/docs/tokenization-performance-report.md +164 -0
  23. package/dist/examples/Cookbook.md +32 -0
  24. package/dist/examples/Status-SHACL.md +33 -0
  25. package/dist/examples/demo/index.md +35 -0
  26. package/dist/examples/demo/journal.md +18 -0
  27. package/dist/examples/demo/minimal.md +11 -0
  28. package/dist/examples/demo/project.md +15 -0
  29. package/dist/examples/demo/recipe.md +31 -0
  30. package/dist/examples/demo/research.md +46 -0
  31. package/dist/examples/demo/space-mission.md +48 -0
  32. package/dist/examples/dogfood.md +288 -0
  33. package/dist/examples/few-shot.md +309 -0
  34. package/dist/examples/graph/rdf-graph.html +473 -0
  35. package/dist/examples/graph/rdf_force_graph_web_component.html +506 -0
  36. package/dist/examples/index.md +45 -0
  37. package/dist/examples/llm-time-workflow.md +173 -0
  38. package/dist/examples/prov-o.md +2367 -0
  39. package/dist/examples/rdf.md +827 -0
  40. package/dist/examples/shacl.md +3399 -0
  41. package/dist/examples/statements.md +43 -0
  42. package/dist/examples/workflow.md +150 -0
  43. package/dist/index.html +2827 -0
  44. package/dist/index.js +1548 -0
  45. package/dist/logo.svg +7 -0
  46. package/dist/spec/Spec-compact.md +363 -0
  47. package/dist/spec/Spec-ultra.md +221 -0
  48. package/dist/spec/Spec.md +1068 -0
  49. package/dist/spec/index.md +46 -0
  50. package/package.json +13 -8
  51. package/src/constants.js +0 -31
  52. package/src/generate.js +0 -245
  53. package/src/index.js +0 -13
  54. package/src/locate.js +0 -23
  55. package/src/merge.js +0 -157
  56. package/src/parse.js +0 -765
  57. package/src/render.js +0 -576
  58. package/src/shared.js +0 -577
  59. package/src/utils.js +0 -430
@@ -0,0 +1,46 @@
1
+ # Specification Hub
2
+
3
+ ## ๐Ÿ“‹ [Formal Specification](./Spec.md)
4
+ Complete MD-LD specification with grammar and formal definitions
5
+
6
+ ## ๐Ÿ“„ [Compact Spec](./Spec-compact.md)
7
+ Concise specification reference for quick lookup
8
+
9
+ ## ๐Ÿ“š [CHANGELOG](../CHANGELOG.md)
10
+ Version history and breaking changes
11
+
12
+ ## ๐Ÿ—๏ธ [Architecture](../docs/Architecture.md)
13
+ Design principles, processing pipeline, and performance characteristics
14
+
15
+ ## ๐Ÿงช [Test Suite](../tests/)
16
+ Comprehensive test coverage (105 tests) with examples and validation
17
+
18
+ ## Specification Overview
19
+
20
+ MD-LD is a **markdown-linked data** format that combines:
21
+
22
+ - **๐Ÿ“ Markdown syntax** - Familiar authoring experience
23
+ - **๐Ÿ”— RDF semantics** - Standard linked data model
24
+ - **โšก Polarity system** - Built-in diff authoring capabilities
25
+ - **๐Ÿ“ Origin tracking** - Complete provenance for every quad
26
+ - **๐Ÿท๏ธ Context management** - Subject declarations and prefix folding
27
+
28
+ ### Key Concepts
29
+
30
+ - **Subject declarations** - `{=IRI}` sets current subject
31
+ - **Predicate forms** - `p`, `?p`, `!p` for different relationship types
32
+ - **Value carriers** - `[text]`, `**bold**`, `code blocks`, etc.
33
+ - **Polarity** - `+` (default) and `-` prefixes for diff authoring
34
+ - **Origin tracking** - Lean mapping from quads to source locations
35
+
36
+ ### Grammar Highlights
37
+
38
+ ```
39
+ Document ::= Prefix* Subject* Annotation*
40
+ Subject ::= '=IRI' | '=#fragment' | '='
41
+ Annotation ::= Carrier Predicate*
42
+ Predicate ::= 'p' | '?p' | '!p' | '.Class'
43
+ Carrier ::= InlineCarrier | BlockCarrier
44
+ ```
45
+
46
+ See [Formal Specification](./Spec.md) for complete grammar and semantics.
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "mdld-parse",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "description": "A standards-compliant parser for **MD-LD (Markdown-Linked Data)** โ€” a human-friendly RDF authoring format that extends Markdown with semantic annotations.",
5
5
  "type": "module",
6
+ "packageManager": "pnpm@10.33.3",
6
7
  "main": "index.js",
7
8
  "exports": {
8
- ".": "./src/index.js"
9
+ ".": "./dist/index.js"
9
10
  },
10
11
  "files": [
11
- "index.js",
12
- "src"
12
+ "dist"
13
13
  ],
14
14
  "scripts": {
15
15
  "test": "node tests/index.js",
16
16
  "dev": "vite dev",
17
- "build": "vite build && vite build --config vite.config.lib.js && cp -r docs/ dist/docs/ && cp -r examples/ dist/examples/ && cp -r spec/ dist/spec/"
17
+ "build": "vite build && vite build --config vite.config.lib.js && cp -r docs/ dist/docs/ && cp -r examples/ dist/examples/ && cp -r spec/ dist/spec/ && cp -r README.md dist/README.md"
18
18
  },
19
19
  "keywords": [
20
20
  "mdld",
@@ -43,10 +43,15 @@
43
43
  "devDependencies": {
44
44
  "@unocss/extractor-pug": "^66.6.8",
45
45
  "@vitejs/plugin-vue": "^6.0.6",
46
+ "@vueuse/core": "^14.3.0",
47
+ "codejar": "^4.3.0",
48
+ "markdown-it": "^14.1.1",
49
+ "n3": "^2.0.3",
50
+ "shiki": "^4.0.2",
46
51
  "unocss": "^66.6.8",
47
52
  "vite": "^8.0.10",
48
53
  "vite-plugin-singlefile": "^2.3.3",
49
- "markdown-it": "^14.1.1",
50
- "n3": "^2.0.3"
54
+ "vscode-textmate": "^9.3.2",
55
+ "vue": "^3.6.0-beta.10"
51
56
  }
52
- }
57
+ }
package/src/constants.js DELETED
@@ -1,31 +0,0 @@
1
- /**
2
- * Shared utilities for MD-LD Parser and Renderer
3
- * Ensures DRY code and consistent CommonMark processing
4
- */
5
-
6
-
7
-
8
- export const DEFAULT_CONTEXT = {
9
- '@vocab': "http://www.w3.org/2000/01/rdf-schema#",
10
- rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
11
- rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
12
- xsd: 'http://www.w3.org/2001/XMLSchema#',
13
- sh: "http://www.w3.org/ns/shacl#",
14
- prov: 'http://www.w3.org/ns/prov#'
15
- };
16
-
17
- // CommonMark patterns - shared between parser and renderer
18
- export const URL_REGEX = /^(https?|ftp|mailto|tag|nih|urn|uuid|did|web|ipfs|ipns|data|file|urn:uuid):/;
19
- export const FENCE_REGEX = /^(`{3,}|~{3,})(.*)/;
20
- export const PREFIX_REGEX = /^\[([^\]]+)\]\s*<([^>]+)>/;
21
- export const HEADING_REGEX = /^(#{1,6})\s+(.+?)(?:\s*(\{[^}]+\}))?$/;
22
- export const UNORDERED_LIST_REGEX = /^(\s*)([-*+]|\d+\.)\s+(.+?)(?:\s*(\{[^}]+\}))?\s*$/;
23
- export const BLOCKQUOTE_REGEX = /^>\s+(.+?)(?:\s*(\{[^}]+\}))?$/;
24
- export const STANDALONE_SUBJECT_REGEX = /^\s*\{=(.*?)\}\s*$/;
25
-
26
- // Pre-compiled carrier patterns for performance
27
- export const CARRIER_PATTERN_ARRAY = [
28
- ['EMPHASIS', /[*__]+(.+?)[*__]+\s*\{([^}]+)\}/y],
29
- ['CODE_SPAN_SINGLE', /`(.+?)`\s*\{([^}]+)\}/y],
30
- ['CODE_SPAN_DOUBLE', /``(.+?)``\s*\{([^}]+)\}/y]
31
- ];
package/src/generate.js DELETED
@@ -1,245 +0,0 @@
1
- import { shortenIRI, expandIRI, DataFactory } from './utils.js';
2
- import { DEFAULT_CONTEXT } from './constants.js';
3
- import {
4
- isLiteral,
5
- collectUsedPrefixes,
6
- sortQuadsByPredicate,
7
- generatePrefixDeclaration,
8
- generateLiteralText,
9
- generateObjectText,
10
- filterQuadsByType
11
- } from './shared.js';
12
-
13
- export function extractLocalName(iri, ctx = {}) {
14
- if (!iri) return iri;
15
-
16
- // Check for exact prefix matches first
17
- for (const [prefix, namespace] of Object.entries(ctx)) {
18
- if (iri.startsWith(namespace) || iri.startsWith(namespace.slice(0, -1))) {
19
- return iri.substring(namespace.length);
20
- }
21
- }
22
-
23
- // Fallback to original logic for local names
24
- const separators = ['#', '/', ':'];
25
- for (const sep of separators) {
26
- const lastSep = iri.lastIndexOf(sep);
27
- if (lastSep !== -1 && lastSep < iri.length - 1) {
28
- return iri.substring(lastSep + 1);
29
- }
30
- }
31
- return iri;
32
- }
33
-
34
- /**
35
- * Generate deterministic MDLD from RDF quads
36
- * Purpose: TTLโ†’MDLD conversion with canonical structure
37
- * Input: RDF quads + context + optional primarySubject (string IRI)
38
- * Output: MDLD text
39
- */
40
- export function generate({ quads, context = {}, primarySubject = null }) {
41
- const fullContext = { ...DEFAULT_CONTEXT, ...context };
42
-
43
- const normalizedQuads = normalizeAndSortQuads(quads);
44
-
45
- const subjectGroups = groupQuadsBySubject(normalizedQuads);
46
-
47
- // Fallback: if no primarySubject provided, use first subject from quads
48
- let effectivePrimary = primarySubject;
49
- if (!effectivePrimary && normalizedQuads.length > 0) {
50
- effectivePrimary = normalizedQuads[0].subject.value;
51
- }
52
-
53
- const { text } = buildDeterministicMDLD(subjectGroups, fullContext, effectivePrimary);
54
-
55
- return { text, context: fullContext };
56
- }
57
-
58
- /**
59
- * Generate node-centric MDLD showing all quads where a specific IRI appears
60
- * in any position: subject, object, predicate, type, or datatype.
61
- * Perfect for exploring individual nodes and their complete relationship graph.
62
- */
63
- export function generateNode({ quads, focusIRI, context = {} }) {
64
- // Validate: must have quads and a focus IRI
65
- if (!quads?.length || !focusIRI) {
66
- return { text: '', context: { ...DEFAULT_CONTEXT, ...context } };
67
- }
68
-
69
- const fullContext = { ...DEFAULT_CONTEXT, ...context };
70
- const normalizedQuads = normalizeAndSortQuads(quads);
71
- const nodeGroups = groupQuadsByNode(normalizedQuads);
72
-
73
- // SAFETY: If focusIRI not in graph, return empty - NEVER fall back to all data
74
- // This prevents accidental rendering of entire databases on misspelled IRIs
75
- if (!nodeGroups.has(focusIRI)) {
76
- return { text: '', context: fullContext };
77
- }
78
-
79
- const { text } = buildDeterministicMDLD(nodeGroups, fullContext, focusIRI);
80
-
81
- return { text, context: fullContext };
82
- }
83
-
84
- function normalizeAndSortQuads(quads) {
85
- return quads
86
- .map(quad => {
87
- // Use DataFactory.fromTerm to ensure proper RDF/JS compatibility
88
- const normSubject = DataFactory.fromTerm(quad.subject);
89
- const normPredicate = DataFactory.fromTerm(quad.predicate);
90
- const normObject = DataFactory.fromTerm(quad.object);
91
-
92
- return {
93
- subject: normSubject,
94
- predicate: normPredicate,
95
- object: normObject
96
- };
97
- })
98
- .sort((a, b) => {
99
- // Deterministic sorting: subject -> predicate -> object
100
- const sComp = a.subject.value.localeCompare(b.subject.value);
101
- if (sComp !== 0) return sComp;
102
- const pComp = a.predicate.value.localeCompare(b.predicate.value);
103
- if (pComp !== 0) return pComp;
104
- const oA = isLiteral(a.object) ? a.object.value : a.object.value;
105
- const oB = isLiteral(b.object) ? b.object.value : b.object.value;
106
- return oA.localeCompare(oB);
107
- });
108
- }
109
-
110
- function groupQuadsBySubject(quads) {
111
- const groups = new Map();
112
- for (const quad of quads) {
113
- if (!groups.has(quad.subject.value)) {
114
- groups.set(quad.subject.value, []);
115
- }
116
- groups.get(quad.subject.value).push(quad);
117
- }
118
- return groups;
119
- }
120
-
121
- function groupQuadsByNode(quads) {
122
- const groups = new Map();
123
- const RDFS_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
124
-
125
- const ensure = (key) => {
126
- if (!groups.has(key)) groups.set(key, []);
127
- return groups.get(key);
128
- };
129
-
130
- for (const quad of quads) {
131
- const { subject, predicate, object } = quad;
132
-
133
- // 1. Subject (direct properties)
134
- ensure(subject.value).push(quad);
135
-
136
- // 2. Object (reverse relations - where this IRI is pointed to)
137
- if (object.termType === 'NamedNode') {
138
- ensure(object.value).push(quad);
139
- }
140
-
141
- // 3. Predicate (where this IRI is used as a property)
142
- ensure(predicate.value).push(quad);
143
-
144
- // 4. Type (instances - where this IRI is the class/type)
145
- if (predicate.value === RDFS_TYPE && object.termType === 'NamedNode') {
146
- ensure(object.value).push(quad);
147
- }
148
-
149
- // 5. Datatype (literals using this as their type)
150
- if (object.termType === 'Literal' && object.datatype) {
151
- ensure(object.datatype.value || object.datatype).push(quad);
152
- }
153
- }
154
- return groups;
155
- }
156
-
157
- function buildDeterministicMDLD(subjectGroups, context, primarySubject = null) {
158
- let text = '';
159
- const usedPrefixes = collectUsedPrefixes(subjectGroups, context);
160
-
161
- // Build label lookup map for all IRIs that have rdfs:label
162
- const labelLookup = buildLabelLookup(subjectGroups);
163
-
164
- // Add prefixes first (deterministic order), but exclude default context prefixes
165
- const sortedPrefixes = Object.entries(context).sort(([a], [b]) => a.localeCompare(b));
166
- for (const [prefix, namespace] of sortedPrefixes) {
167
- // Skip default context prefixes - they're implicit in MDLD
168
- if (prefix !== '@vocab' && !prefix.startsWith('@') && !DEFAULT_CONTEXT[prefix] && usedPrefixes.has(prefix)) {
169
- text += generatePrefixDeclaration(prefix, namespace);
170
- }
171
- }
172
-
173
- if (sortedPrefixes.length > 0) {
174
- text += '\n';
175
- }
176
-
177
- // Process subjects in deterministic order, with primary subject first
178
- const sortedSubjects = Array.from(subjectGroups.keys()).sort();
179
- const primarySubjectIRI = primarySubject; // Already a string IRI
180
-
181
- // If primary subject exists, place it first
182
- const orderedSubjects = primarySubjectIRI
183
- ? [primarySubjectIRI, ...sortedSubjects.filter(s => s !== primarySubjectIRI)]
184
- : sortedSubjects;
185
-
186
- for (const subjectIRI of orderedSubjects) {
187
- const subjectQuads = subjectGroups.get(subjectIRI);
188
- // Skip if subject not found in groups (e.g., primarySubject provided but no quads for it)
189
- if (!subjectQuads) continue;
190
-
191
- const shortSubject = shortenIRI(subjectIRI, context);
192
-
193
- // Separate types, literals, and objects using shared utility
194
- const { types, literals, objects } = filterQuadsByType(subjectQuads);
195
-
196
- // Check if this subject has a label
197
- const hasLabel = labelLookup.has(subjectIRI);
198
- const displayName = hasLabel ? labelLookup.get(subjectIRI) : extractLocalName(subjectIRI, context);
199
-
200
- // Build annotations: types + label indicator if present
201
- let annotations = types.length > 0
202
- ? types.map(t => '.' + shortenIRI(t.object.value, context)).sort().join(' ')
203
- : '';
204
- if (hasLabel) {
205
- annotations += (annotations ? ' ' : '') + 'label';
206
- }
207
-
208
- const annotationStr = annotations ? ' ' + annotations : '';
209
- text += `# ${displayName} {=${shortSubject}${annotationStr}}\n\n`;
210
-
211
- // Add literals (excluding the label used in heading) and objects
212
- const rdfsLabelIRI = 'http://www.w3.org/2000/01/rdf-schema#label';
213
- const headingLabel = hasLabel ? labelLookup.get(subjectIRI) : null;
214
- sortQuadsByPredicate(literals).forEach(quad => {
215
- // Skip only the label that matches the heading display, render additional labels
216
- if (quad.predicate.value === rdfsLabelIRI && quad.object.value === headingLabel) {
217
- return; // Skip the heading label
218
- }
219
- text += generateLiteralText(quad, context);
220
- });
221
-
222
- sortQuadsByPredicate(objects).forEach(quad => {
223
- text += generateObjectText(quad, context, labelLookup);
224
- });
225
-
226
- text += '\n';
227
- }
228
-
229
- return { text };
230
- }
231
-
232
- function buildLabelLookup(subjectGroups) {
233
- const labelLookup = new Map();
234
- const rdfsLabelIRI = 'http://www.w3.org/2000/01/rdf-schema#label';
235
-
236
- for (const subjectQuads of subjectGroups.values()) {
237
- for (const quad of subjectQuads) {
238
- if (quad.predicate.value === rdfsLabelIRI && quad.object.termType === 'Literal') {
239
- labelLookup.set(quad.subject.value, quad.object.value);
240
- }
241
- }
242
- }
243
-
244
- return labelLookup;
245
- }
package/src/index.js DELETED
@@ -1,13 +0,0 @@
1
- export { parse } from './parse.js';
2
- export { merge } from './merge.js';
3
- export { generate, generateNode } from './generate.js';
4
- export { locate } from './locate.js';
5
- export { render } from './render.js';
6
- export { DEFAULT_CONTEXT } from './constants.js';
7
- export {
8
- DataFactory,
9
- hash,
10
- expandIRI,
11
- shortenIRI,
12
- parseSemanticBlock
13
- } from './utils.js';
package/src/locate.js DELETED
@@ -1,23 +0,0 @@
1
- import { quadToKeyForOrigin } from './utils.js';
2
-
3
- /**
4
- * Locate the origin entry for a quad using the lean origin system
5
- *
6
- * @param {Object} quad - The quad to locate (subject, predicate, object)
7
- * @param {Object} origin - Origin object containing quadIndex
8
- * @returns {Object|null} Origin entry or null if not found
9
- */
10
- export function locate(quad, origin) {
11
- if (!quad || !origin || !origin.quadIndex) {
12
- return null;
13
- }
14
-
15
- // Generate the quad key to lookup in quadIndex
16
- const quadKey = quadToKeyForOrigin(quad);
17
- if (!quadKey) {
18
- return null;
19
- }
20
-
21
- // Return the origin entry directly - no need to create new object
22
- return origin.quadIndex.get(quadKey) || null;
23
- }
package/src/merge.js DELETED
@@ -1,157 +0,0 @@
1
- import { parse } from './parse.js';
2
- import { quadToKeyForOrigin } from './utils.js';
3
- import { DEFAULT_CONTEXT } from './constants.js';
4
-
5
- /**
6
- * Creates a unique key for quad identity matching - using shared utility
7
- * @param {Quad} quad
8
- * @returns {string}
9
- */
10
- function quadKey(quad) {
11
- return quadToKeyForOrigin(quad);
12
- }
13
-
14
- /**
15
- * Normalizes merge input to ParseResult format
16
- * @param {string|ParseResult} input
17
- * @param {Object} options
18
- * @param {Object} docContext
19
- * @returns {ParseResult}
20
- */
21
- function normalizeInput(input, options, docContext) {
22
- if (typeof input === 'string') {
23
- return parse({
24
- text: input,
25
- ...options,
26
- context: { ...docContext, ...options.context }
27
- });
28
- }
29
- // ParseResult passthrough - no re-parse
30
- return input;
31
- }
32
-
33
- /**
34
- * Merges multiple MDLD documents with diff polarity resolution
35
- * @param {Array<string|ParseResult>} docs
36
- * @param {Object} options
37
- * @returns {Object} Merge result with quads, remove, statements, origin, and context
38
- */
39
- export function merge(docs, options = {}) {
40
- const sessionBuffer = new Map(); // Use Map instead of Set for proper quad storage
41
- const sessionRemoveSet = new Set();
42
- const allDocuments = [];
43
- const quadIndex = new Map();
44
- const allStatements = []; // Collect statements from all documents
45
- const accumulatedContext = new Map(); // Track all unique prefixes across documents
46
- const primarySubjects = []; // Collect primary subjects from all documents
47
-
48
- // Process each document in order
49
- for (let i = 0; i < docs.length; i++) {
50
- const input = docs[i];
51
-
52
- // Each document gets the same context (no inheritance)
53
- const docContext = { ...DEFAULT_CONTEXT, ...options.context };
54
-
55
- // Normalize input to ParseResult
56
- const doc = normalizeInput(input, options, docContext);
57
-
58
- // Accumulate context from this document
59
- if (doc.context) {
60
- for (const [prefix, namespace] of Object.entries(doc.context)) {
61
- // Don't override default context entries unless explicitly provided in options
62
- if (!accumulatedContext.has(prefix) && !DEFAULT_CONTEXT[prefix]) {
63
- accumulatedContext.set(prefix, namespace);
64
- }
65
- }
66
- }
67
-
68
- // Create document origin
69
- const documentOrigin = {
70
- index: i,
71
- input: typeof input === 'string' ? 'string' : 'ParseResult',
72
- origin: doc.origin,
73
- context: doc.context,
74
- statementsCount: doc.statements?.length || 0 // Track statements count
75
- };
76
- allDocuments.push(documentOrigin);
77
-
78
- // Collect statements from this document
79
- if (doc.statements && doc.statements.length > 0) {
80
- allStatements.push(...doc.statements);
81
- }
82
-
83
- // Collect primary subject from this document (already a string IRI)
84
- if (doc.primarySubject) {
85
- primarySubjects.push(doc.primarySubject);
86
- }
87
-
88
- // Fold assertions into session buffer
89
- for (const quad of doc.quads) {
90
- const key = quadKey(quad);
91
- sessionBuffer.set(key, quad);
92
-
93
- // Create quad origin with document index and polarity
94
- const existingOrigin = doc.origin.quadIndex.get(key);
95
- quadIndex.set(key, {
96
- ...(existingOrigin || {}),
97
- documentIndex: i,
98
- polarity: '+'
99
- });
100
- }
101
-
102
- // Fold retractions
103
- for (const quad of doc.remove) {
104
- const key = quadKey(quad);
105
-
106
- if (sessionBuffer.has(key)) {
107
- // Inter-document cancel - remove from buffer
108
- sessionBuffer.delete(key);
109
- } else {
110
- // External retract - add to remove set
111
- sessionRemoveSet.add(quad);
112
- }
113
-
114
- // Create quad origin for remove quads
115
- const existingOrigin = doc.origin.quadIndex.get(key);
116
- quadIndex.set(key, {
117
- ...(existingOrigin || {}),
118
- documentIndex: i,
119
- polarity: '-'
120
- });
121
- }
122
- }
123
-
124
- // Build final result
125
- const finalQuads = Array.from(sessionBuffer.values());
126
- const finalRemove = Array.from(sessionRemoveSet);
127
-
128
- // Build merge origin
129
- const mergeOrigin = {
130
- documents: allDocuments,
131
- quadIndex: quadIndex
132
- };
133
-
134
- // Build final context (union of all contexts)
135
- const finalContext = {
136
- ...DEFAULT_CONTEXT,
137
- ...options.context,
138
- ...Object.fromEntries(accumulatedContext)
139
- };
140
-
141
- // Enforce hard invariant
142
- const quadKeys = new Set(finalQuads.map(quadKey));
143
- const removeKeys = new Set(finalRemove.map(quadKey));
144
-
145
- // Filter out any overlaps (shouldn't happen with correct implementation)
146
- const filteredQuads = finalQuads.filter(quad => !removeKeys.has(quadKey(quad)));
147
- const filteredRemove = finalRemove.filter(quad => !quadKeys.has(quadKey(quad)));
148
-
149
- return {
150
- quads: filteredQuads,
151
- remove: filteredRemove,
152
- statements: allStatements, // Include all collected statements
153
- origin: mergeOrigin,
154
- context: finalContext,
155
- primarySubjects: primarySubjects // Include all collected primary subjects
156
- };
157
- }