@altopelago/sansa 0.9.1 → 0.10.1
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/CHANGELOG.md +59 -1
- package/README.md +13 -5
- package/conformance/cts-claims.json +4 -4
- package/docs/api-contract.md +41 -22
- package/docs/capabilities.json +40 -2
- package/docs/instruction-tool.md +4 -0
- package/docs/query-tool.md +28 -8
- package/fixtures/address-v1.json +9 -9
- package/fixtures/query-inventory.aeon +2 -2
- package/fixtures/query-inventory.json +4 -4
- package/fixtures/query-inventory.telex.aes +571 -0
- package/package.json +1 -1
- package/scripts/instruction.mjs +3 -3
- package/scripts/query-web.mjs +2 -1
- package/scripts/query.mjs +14 -9
- package/scripts/validate-cts-claims.mjs +22 -11
- package/src/index.d.ts +171 -2
- package/src/index.js +517 -22
- package/stress-tests/address/valid.sansa-cases +3 -3
- package/tools/mutate-web/app.js +26 -6
- package/tools/mutate-web/index.html +6 -1
- package/tools/mutate-web/runtime.mjs +314 -16
- package/tools/query-web/app.js +24 -5
- package/tools/query-web/index.html +4 -0
- package/tools/query-web/runtime.mjs +407 -5
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
2
|
-
const
|
|
2
|
+
const QUALIFIER_NUMBER_RE = /^[+-]?(?:(?:0|[1-9][0-9]*)(?:\.[0-9]+)?|\.[0-9]+)$/;
|
|
3
3
|
export const SANSA_MAX_POSITION_INDEX = 999_999;
|
|
4
4
|
export const SANSA_MAX_QUERY_INTEGER = Number.MAX_SAFE_INTEGER;
|
|
5
5
|
|
|
@@ -36,7 +36,7 @@ const VALUE_SEMANTICS_METADATA_CATEGORIES = [
|
|
|
36
36
|
'date',
|
|
37
37
|
'time',
|
|
38
38
|
'datetime',
|
|
39
|
-
'
|
|
39
|
+
'wtc',
|
|
40
40
|
'temporal',
|
|
41
41
|
'lexicalStructuredScalar',
|
|
42
42
|
'container',
|
|
@@ -1670,6 +1670,9 @@ function resolveMutationTargetSurface(targetSurface) {
|
|
|
1670
1670
|
if (targetSurface === 'json' || targetSurface === 'json-compatible') {
|
|
1671
1671
|
return { ok: true, surface: jsonMutationTargetSurface };
|
|
1672
1672
|
}
|
|
1673
|
+
if (targetSurface === 'telex' || targetSurface === 'telex.aes') {
|
|
1674
|
+
return { ok: true, surface: telexMutationTargetSurface };
|
|
1675
|
+
}
|
|
1673
1676
|
if (targetSurface && typeof targetSurface === 'object' && typeof targetSurface.validateOperation === 'function') {
|
|
1674
1677
|
return {
|
|
1675
1678
|
ok: true,
|
|
@@ -1683,7 +1686,7 @@ function resolveMutationTargetSurface(targetSurface) {
|
|
|
1683
1686
|
ok: false,
|
|
1684
1687
|
error: mutationTargetSurfaceError(
|
|
1685
1688
|
'SANSA_MUTATE_TARGET_UNSUPPORTED_FEATURE',
|
|
1686
|
-
'Mutation target surface must be "aeon", "json", or an object with validateOperation',
|
|
1689
|
+
'Mutation target surface must be "aeon", "json", "json-compatible", "telex", "telex.aes", or an object with validateOperation',
|
|
1687
1690
|
{ targetFormat: targetSurfaceId(targetSurface) },
|
|
1688
1691
|
),
|
|
1689
1692
|
};
|
|
@@ -1724,6 +1727,159 @@ const jsonMutationTargetSurface = Object.freeze({
|
|
|
1724
1727
|
},
|
|
1725
1728
|
});
|
|
1726
1729
|
|
|
1730
|
+
const TELEX_SCALAR_KINDS = new Set([
|
|
1731
|
+
'StringLiteral',
|
|
1732
|
+
'NumberLiteral',
|
|
1733
|
+
'InfinityLiteral',
|
|
1734
|
+
'NaNLiteral',
|
|
1735
|
+
'NullLiteral',
|
|
1736
|
+
'BooleanLiteral',
|
|
1737
|
+
'ToggleLiteral',
|
|
1738
|
+
'HexLiteral',
|
|
1739
|
+
'RadixLiteral',
|
|
1740
|
+
'EncodingLiteral',
|
|
1741
|
+
'SeparatorLiteral',
|
|
1742
|
+
'SansaAddressLiteral',
|
|
1743
|
+
'DateLiteral',
|
|
1744
|
+
'TimeLiteral',
|
|
1745
|
+
'DateTimeLiteral',
|
|
1746
|
+
'WTCDateTimeLiteral',
|
|
1747
|
+
]);
|
|
1748
|
+
|
|
1749
|
+
const TELEX_KIND_ALIASES = new Map([
|
|
1750
|
+
['string', 'StringLiteral'], ['trimtick', 'StringLiteral'], ['prose', 'StringLiteral'],
|
|
1751
|
+
['number', 'NumberLiteral'], ['n', 'NumberLiteral'], ['integer', 'NumberLiteral'],
|
|
1752
|
+
['boolean', 'BooleanLiteral'], ['bool', 'BooleanLiteral'], ['toggle', 'ToggleLiteral'],
|
|
1753
|
+
['null', 'NullLiteral'], ['nan', 'NaNLiteral'], ['infinity', 'InfinityLiteral'],
|
|
1754
|
+
['hex', 'HexLiteral'], ['radix', 'RadixLiteral'], ['encoding', 'EncodingLiteral'],
|
|
1755
|
+
['base64', 'EncodingLiteral'], ['embed', 'EncodingLiteral'], ['inline', 'EncodingLiteral'],
|
|
1756
|
+
['separator', 'SeparatorLiteral'], ['sep', 'SeparatorLiteral'], ['csv', 'SeparatorLiteral'],
|
|
1757
|
+
['sansa', 'SansaAddressLiteral'], ['sansaaddress', 'SansaAddressLiteral'],
|
|
1758
|
+
['date', 'DateLiteral'], ['time', 'TimeLiteral'], ['datetime', 'DateTimeLiteral'], ['wtc', 'WTCDateTimeLiteral'],
|
|
1759
|
+
]);
|
|
1760
|
+
|
|
1761
|
+
const telexMutationTargetSurface = Object.freeze({
|
|
1762
|
+
id: 'telex',
|
|
1763
|
+
validateOperation(operation, context) {
|
|
1764
|
+
if (operation.op !== 'replace') {
|
|
1765
|
+
return {
|
|
1766
|
+
ok: false,
|
|
1767
|
+
error: mutationTargetSurfaceError(
|
|
1768
|
+
'SANSA_MUTATE_TARGET_UNSUPPORTED_OPERATION',
|
|
1769
|
+
`Target 'telex' currently supports scalar replacement only; '${operation.op}' requires a portable path-rewrite contract`,
|
|
1770
|
+
{ operationIndex: context.operationIndex, targetFormat: 'telex' },
|
|
1771
|
+
),
|
|
1772
|
+
};
|
|
1773
|
+
}
|
|
1774
|
+
const exposedKind = operation.target?.binding?.representationKind;
|
|
1775
|
+
const kind = TELEX_SCALAR_KINDS.has(exposedKind)
|
|
1776
|
+
? exposedKind
|
|
1777
|
+
: TELEX_KIND_ALIASES.get(String(exposedKind ?? '').toLowerCase());
|
|
1778
|
+
if (!TELEX_SCALAR_KINDS.has(kind)) {
|
|
1779
|
+
return {
|
|
1780
|
+
ok: false,
|
|
1781
|
+
error: mutationTargetSurfaceError(
|
|
1782
|
+
'SANSA_MUTATE_TARGET_UNSUPPORTED_FEATURE',
|
|
1783
|
+
`Target 'telex' cannot replace '${exposedKind ?? 'unknown'}' bindings in the scalar-replacement profile`,
|
|
1784
|
+
{ operationIndex: context.operationIndex, targetFormat: 'telex' },
|
|
1785
|
+
),
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1788
|
+
if (operation.value !== null && typeof operation.value === 'object') {
|
|
1789
|
+
return {
|
|
1790
|
+
ok: false,
|
|
1791
|
+
error: mutationTargetSurfaceError(
|
|
1792
|
+
'SANSA_MUTATE_TARGET_UNSUPPORTED_VALUE',
|
|
1793
|
+
"Target 'telex' scalar replacement requires a scalar mutation value",
|
|
1794
|
+
{ operationIndex: context.operationIndex, targetFormat: 'telex', valuePath: `operations[${context.operationIndex}].value` },
|
|
1795
|
+
),
|
|
1796
|
+
};
|
|
1797
|
+
}
|
|
1798
|
+
const proposedKind = telexProposedScalarKind(operation, kind);
|
|
1799
|
+
if (!proposedKind.ok) {
|
|
1800
|
+
return {
|
|
1801
|
+
ok: false,
|
|
1802
|
+
error: mutationTargetSurfaceError(
|
|
1803
|
+
proposedKind.code,
|
|
1804
|
+
proposedKind.message,
|
|
1805
|
+
{ operationIndex: context.operationIndex, targetFormat: 'telex', ...(operation.datatype === undefined ? {} : { datatype: operation.datatype }) },
|
|
1806
|
+
),
|
|
1807
|
+
};
|
|
1808
|
+
}
|
|
1809
|
+
const valueResult = validateTelexScalarMutationValue(proposedKind.kind, operation.value, kind);
|
|
1810
|
+
if (!valueResult.ok) {
|
|
1811
|
+
return {
|
|
1812
|
+
ok: false,
|
|
1813
|
+
error: mutationTargetSurfaceError(
|
|
1814
|
+
'SANSA_MUTATE_TARGET_UNSUPPORTED_VALUE',
|
|
1815
|
+
valueResult.message,
|
|
1816
|
+
{ operationIndex: context.operationIndex, targetFormat: 'telex', valuePath: `operations[${context.operationIndex}].value` },
|
|
1817
|
+
),
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1820
|
+
return { ok: true };
|
|
1821
|
+
},
|
|
1822
|
+
});
|
|
1823
|
+
|
|
1824
|
+
function telexProposedScalarKind(operation, fallback) {
|
|
1825
|
+
if (operation.kind !== undefined) {
|
|
1826
|
+
if (TELEX_SCALAR_KINDS.has(operation.kind)) return { ok: true, kind: operation.kind };
|
|
1827
|
+
const alias = TELEX_KIND_ALIASES.get(String(operation.kind).toLowerCase());
|
|
1828
|
+
if (alias !== undefined) return { ok: true, kind: alias };
|
|
1829
|
+
return {
|
|
1830
|
+
ok: false,
|
|
1831
|
+
code: 'SANSA_MUTATE_TARGET_UNSUPPORTED_FEATURE',
|
|
1832
|
+
message: `Target 'telex' scalar replacement cannot represent kind '${operation.kind}'`,
|
|
1833
|
+
};
|
|
1834
|
+
}
|
|
1835
|
+
if (operation.datatype !== undefined) {
|
|
1836
|
+
const base = datatypeBaseName(operation.datatype)?.toLowerCase();
|
|
1837
|
+
const alias = TELEX_KIND_ALIASES.get(base);
|
|
1838
|
+
if (alias !== undefined) return { ok: true, kind: alias };
|
|
1839
|
+
if (/^(?:u?int|float|decimal)\d*$/u.test(base ?? '')) return { ok: true, kind: 'NumberLiteral' };
|
|
1840
|
+
if (/^radix\d+$/u.test(base ?? '')) return { ok: true, kind: 'RadixLiteral' };
|
|
1841
|
+
if (['object', 'list', 'tuple', 'node', 'clonereference', 'pointerreference'].includes(base)) {
|
|
1842
|
+
return {
|
|
1843
|
+
ok: false,
|
|
1844
|
+
code: 'SANSA_MUTATE_TARGET_UNSUPPORTED_DATATYPE',
|
|
1845
|
+
message: `Target 'telex' scalar replacement cannot represent datatype '${operation.datatype}'`,
|
|
1846
|
+
};
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
return { ok: true, kind: fallback };
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
function validateTelexScalarMutationValue(kind, value, currentKind) {
|
|
1853
|
+
if (kind === 'NumberLiteral') {
|
|
1854
|
+
return typeof value === 'number' && Number.isFinite(value)
|
|
1855
|
+
? { ok: true }
|
|
1856
|
+
: { ok: false, message: "Target 'telex' NumberLiteral replacement requires a finite JSON number" };
|
|
1857
|
+
}
|
|
1858
|
+
if (kind === 'BooleanLiteral') {
|
|
1859
|
+
return typeof value === 'boolean'
|
|
1860
|
+
? { ok: true }
|
|
1861
|
+
: { ok: false, message: "Target 'telex' BooleanLiteral replacement requires a JSON boolean" };
|
|
1862
|
+
}
|
|
1863
|
+
if (kind === 'NullLiteral') {
|
|
1864
|
+
return (typeof value === 'string' && value.length > 0) || (value === null && currentKind === 'NullLiteral')
|
|
1865
|
+
? { ok: true }
|
|
1866
|
+
: { ok: false, message: "Target 'telex' NullLiteral replacement requires a non-empty reason string, or null for an existing null event" };
|
|
1867
|
+
}
|
|
1868
|
+
if (kind === 'NaNLiteral') {
|
|
1869
|
+
return value === null || value === 'NaN'
|
|
1870
|
+
? { ok: true }
|
|
1871
|
+
: { ok: false, message: "Target 'telex' NaNLiteral replacement requires null or 'NaN'" };
|
|
1872
|
+
}
|
|
1873
|
+
if (kind === 'InfinityLiteral') {
|
|
1874
|
+
return value === Infinity || value === -Infinity || ['Infinity', '+Infinity', '-Infinity'].includes(value)
|
|
1875
|
+
? { ok: true }
|
|
1876
|
+
: { ok: false, message: "Target 'telex' InfinityLiteral replacement requires an infinity token" };
|
|
1877
|
+
}
|
|
1878
|
+
return typeof value === 'string'
|
|
1879
|
+
? { ok: true }
|
|
1880
|
+
: { ok: false, message: `Target 'telex' ${kind} replacement requires a JSON string` };
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1727
1883
|
function normalizeMutationTargetSurfaceOperationResult(result, context) {
|
|
1728
1884
|
if (result === undefined || result === true || result?.ok === true) return { ok: true };
|
|
1729
1885
|
if (result === false) {
|
|
@@ -1925,7 +2081,7 @@ function validateAeonTargetScalarValue(value, representation, path, operationInd
|
|
|
1925
2081
|
case 'date':
|
|
1926
2082
|
case 'time':
|
|
1927
2083
|
case 'datetime':
|
|
1928
|
-
case '
|
|
2084
|
+
case 'wtc':
|
|
1929
2085
|
return typeof value === 'string' && isQueryTemporalLiteral(value, representation)
|
|
1930
2086
|
? { ok: true }
|
|
1931
2087
|
: invalidAeonTargetValue(`${representation} literals must use valid AEON temporal text`, path, operationIndex);
|
|
@@ -2021,7 +2177,7 @@ function validateAeonSeparatorDatatypeSurface(datatype, base, operationIndex) {
|
|
|
2021
2177
|
),
|
|
2022
2178
|
};
|
|
2023
2179
|
}
|
|
2024
|
-
if (!/^(?:sep|separator)
|
|
2180
|
+
if (!/^(?:sep|separator)\[\s*"[A-Za-z0-9!#$%&*+\-.:;=?@^_|~<>]"\s*(?:,\s*"[A-Za-z0-9!#$%&*+\-.:;=?@^_|~<>]"\s*)*\]$/i.test(source)) {
|
|
2025
2181
|
return {
|
|
2026
2182
|
ok: false,
|
|
2027
2183
|
error: mutationTargetSurfaceError(
|
|
@@ -2115,7 +2271,7 @@ function isReservedAeonDatatypeBase(base) {
|
|
|
2115
2271
|
'date',
|
|
2116
2272
|
'time',
|
|
2117
2273
|
'datetime',
|
|
2118
|
-
'
|
|
2274
|
+
'wtc',
|
|
2119
2275
|
'sep',
|
|
2120
2276
|
'separator',
|
|
2121
2277
|
'kadot',
|
|
@@ -2334,7 +2490,7 @@ function representationKindFromMutationName(name, { allowUnknown = false } = {})
|
|
|
2334
2490
|
if (base === 'sep' || base === 'separator' || base === 'kadot') return 'separator';
|
|
2335
2491
|
if (base === 'sansa') return 'sansa';
|
|
2336
2492
|
if (base === 'encoding' || ['base64', 'embed', 'inline'].includes(base)) return 'encoding';
|
|
2337
|
-
if (['date', 'time', 'datetime', '
|
|
2493
|
+
if (['date', 'time', 'datetime', 'wtc'].includes(base)) return base;
|
|
2338
2494
|
if (['cloneReference', 'pointerReference', 'referenceForm'].includes(base)) return base;
|
|
2339
2495
|
return allowUnknown ? base : undefined;
|
|
2340
2496
|
}
|
|
@@ -2515,6 +2671,342 @@ export function resolveAddress(input, namespace, options = {}) {
|
|
|
2515
2671
|
return { ok: true, bindings: current, diagnostics: [] };
|
|
2516
2672
|
}
|
|
2517
2673
|
|
|
2674
|
+
export function traverseGraph(starts, namespace, declarations, options = {}) {
|
|
2675
|
+
const invalid = validateGraphInput(starts, namespace, declarations, options);
|
|
2676
|
+
if (invalid) return { ok: false, bindings: [], paths: [], errors: [invalid] };
|
|
2677
|
+
|
|
2678
|
+
const paths = [];
|
|
2679
|
+
const bindings = [];
|
|
2680
|
+
const bindingAddresses = new Set();
|
|
2681
|
+
let visitedEdges = 0;
|
|
2682
|
+
|
|
2683
|
+
for (const start of starts) {
|
|
2684
|
+
const startAddress = graphBindingAddress(start, namespace);
|
|
2685
|
+
if (!startAddress) {
|
|
2686
|
+
return graphFailure('SANSA_GRAPH_START_ADDRESS_MISSING', 'Graph start binding has no canonical address');
|
|
2687
|
+
}
|
|
2688
|
+
for (const declaration of declarations) {
|
|
2689
|
+
if (!declaration.schemaVersions.includes(options.schemaVersion)) {
|
|
2690
|
+
return graphFailure('SANSA_GRAPH_SCHEMA_VERSION_UNSUPPORTED', `Relationship '${declaration.id}' does not apply to schema version '${options.schemaVersion}'`, {
|
|
2691
|
+
declarationId: declaration.id,
|
|
2692
|
+
startAddress,
|
|
2693
|
+
});
|
|
2694
|
+
}
|
|
2695
|
+
const sourceSemanticType = graphBindingSemanticType(start, namespace);
|
|
2696
|
+
if (!declaration.sourceSemanticTypes.includes(sourceSemanticType)) {
|
|
2697
|
+
return graphFailure('SANSA_GRAPH_SOURCE_TYPE_MISMATCH', `Relationship '${declaration.id}' does not admit source semantic type '${sourceSemanticType || '<unknown>'}'`, {
|
|
2698
|
+
declarationId: declaration.id,
|
|
2699
|
+
startAddress,
|
|
2700
|
+
});
|
|
2701
|
+
}
|
|
2702
|
+
const edges = resolveAddress(declaration.edgeAddress, namespace, {
|
|
2703
|
+
contextualRoot: start,
|
|
2704
|
+
maxBindings: options.budget.maxVisitedEdges,
|
|
2705
|
+
});
|
|
2706
|
+
if (!edges.ok) return { ok: false, bindings: [], paths: [], errors: edges.errors };
|
|
2707
|
+
if (edges.bindings.length === 0 && declaration.missingEdge === 'error') {
|
|
2708
|
+
return graphFailure('SANSA_GRAPH_EDGE_MISSING', `Relationship '${declaration.id}' has no edge at '${declaration.edgeAddress}'`, {
|
|
2709
|
+
declarationId: declaration.id,
|
|
2710
|
+
startAddress,
|
|
2711
|
+
});
|
|
2712
|
+
}
|
|
2713
|
+
if (!graphCardinalityMatches(declaration.edgeCardinality, edges.bindings.length)) {
|
|
2714
|
+
return graphFailure('SANSA_GRAPH_EDGE_CARDINALITY_MISMATCH', `Relationship '${declaration.id}' edge cardinality '${declaration.edgeCardinality}' rejected ${edges.bindings.length} resolved edges`, {
|
|
2715
|
+
declarationId: declaration.id,
|
|
2716
|
+
startAddress,
|
|
2717
|
+
observed: edges.bindings.length,
|
|
2718
|
+
});
|
|
2719
|
+
}
|
|
2720
|
+
for (const edge of edges.bindings) {
|
|
2721
|
+
visitedEdges += 1;
|
|
2722
|
+
if (visitedEdges > options.budget.maxVisitedEdges) {
|
|
2723
|
+
return graphBudgetFailure('maxVisitedEdges', options.budget.maxVisitedEdges, visitedEdges);
|
|
2724
|
+
}
|
|
2725
|
+
const edgeAddress = graphBindingAddress(edge, namespace);
|
|
2726
|
+
if (!edgeAddress) {
|
|
2727
|
+
return graphFailure('SANSA_GRAPH_EDGE_ADDRESS_MISSING', 'Graph relationship edge has no canonical address', {
|
|
2728
|
+
declarationId: declaration.id,
|
|
2729
|
+
startAddress,
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2732
|
+
const targetAddress = graphReferenceTarget(graphBindingValue(edge, namespace));
|
|
2733
|
+
if (!targetAddress) {
|
|
2734
|
+
return graphFailure('SANSA_GRAPH_INVALID_REFERENCE_TARGET', `Relationship edge '${edgeAddress}' does not contain an absolute SANSA address reference`, {
|
|
2735
|
+
declarationId: declaration.id,
|
|
2736
|
+
startAddress,
|
|
2737
|
+
edgeAddress,
|
|
2738
|
+
});
|
|
2739
|
+
}
|
|
2740
|
+
const target = resolveAddress(targetAddress, namespace, { maxBindings: 2 });
|
|
2741
|
+
if (!target.ok) return { ok: false, bindings: [], paths: [], errors: target.errors };
|
|
2742
|
+
if (target.bindings.length === 0) {
|
|
2743
|
+
if (declaration.danglingTarget === 'error') {
|
|
2744
|
+
return graphFailure('SANSA_GRAPH_DANGLING_TARGET', `Relationship edge '${edgeAddress}' targets missing binding '${targetAddress}'`, {
|
|
2745
|
+
declarationId: declaration.id,
|
|
2746
|
+
startAddress,
|
|
2747
|
+
edgeAddress,
|
|
2748
|
+
targetAddress,
|
|
2749
|
+
});
|
|
2750
|
+
}
|
|
2751
|
+
continue;
|
|
2752
|
+
}
|
|
2753
|
+
const end = target.bindings[0];
|
|
2754
|
+
const endAddress = graphBindingAddress(end, namespace);
|
|
2755
|
+
if (!endAddress) {
|
|
2756
|
+
return graphFailure('SANSA_GRAPH_END_ADDRESS_MISSING', 'Graph relationship target has no canonical address', {
|
|
2757
|
+
declarationId: declaration.id,
|
|
2758
|
+
startAddress,
|
|
2759
|
+
edgeAddress,
|
|
2760
|
+
targetAddress,
|
|
2761
|
+
});
|
|
2762
|
+
}
|
|
2763
|
+
const targetSemanticType = graphBindingSemanticType(end, namespace);
|
|
2764
|
+
if (!declaration.targetSemanticTypes.includes(targetSemanticType)) {
|
|
2765
|
+
return graphFailure('SANSA_GRAPH_TARGET_TYPE_MISMATCH', `Relationship '${declaration.id}' does not admit target semantic type '${targetSemanticType || '<unknown>'}'`, {
|
|
2766
|
+
declarationId: declaration.id,
|
|
2767
|
+
startAddress,
|
|
2768
|
+
edgeAddress,
|
|
2769
|
+
targetAddress,
|
|
2770
|
+
});
|
|
2771
|
+
}
|
|
2772
|
+
if (options.authorizeStep && options.authorizeStep({ start, edge, end, declaration }) !== true) {
|
|
2773
|
+
return graphFailure('SANSA_GRAPH_STEP_UNAUTHORIZED', `Traversal step through relationship '${declaration.id}' was not authorized`, {
|
|
2774
|
+
declarationId: declaration.id,
|
|
2775
|
+
startAddress,
|
|
2776
|
+
edgeAddress,
|
|
2777
|
+
targetAddress,
|
|
2778
|
+
});
|
|
2779
|
+
}
|
|
2780
|
+
paths.push({
|
|
2781
|
+
declarationId: declaration.id,
|
|
2782
|
+
start,
|
|
2783
|
+
startAddress,
|
|
2784
|
+
edge,
|
|
2785
|
+
edgeAddress,
|
|
2786
|
+
end,
|
|
2787
|
+
endAddress,
|
|
2788
|
+
});
|
|
2789
|
+
if (paths.length > options.budget.maxResults) {
|
|
2790
|
+
return graphBudgetFailure('maxResults', options.budget.maxResults, paths.length);
|
|
2791
|
+
}
|
|
2792
|
+
if (!bindingAddresses.has(endAddress)) {
|
|
2793
|
+
bindingAddresses.add(endAddress);
|
|
2794
|
+
bindings.push(end);
|
|
2795
|
+
if (bindings.length > options.budget.maxVisitedNodes) {
|
|
2796
|
+
return graphBudgetFailure('maxVisitedNodes', options.budget.maxVisitedNodes, bindings.length);
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
}
|
|
2800
|
+
}
|
|
2801
|
+
}
|
|
2802
|
+
|
|
2803
|
+
return {
|
|
2804
|
+
ok: true,
|
|
2805
|
+
bindings,
|
|
2806
|
+
paths,
|
|
2807
|
+
metrics: { visitedNodes: bindings.length, visitedEdges },
|
|
2808
|
+
diagnostics: [],
|
|
2809
|
+
};
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
export function traverseGraphSequence(starts, namespace, hops, options = {}) {
|
|
2813
|
+
if (!Array.isArray(hops) || hops.length === 0 || !['omit', 'error'].includes(options.cyclePolicy)) {
|
|
2814
|
+
return graphFailure('SANSA_GRAPH_INVALID_INPUT', 'Graph sequence traversal requires one or more declaration hops and an explicit cycle policy');
|
|
2815
|
+
}
|
|
2816
|
+
if (!options.budget || !Number.isSafeInteger(options.budget.maxDepth) || options.budget.maxDepth < hops.length) {
|
|
2817
|
+
return graphBudgetFailure('maxDepth', options.budget?.maxDepth, hops.length);
|
|
2818
|
+
}
|
|
2819
|
+
if (!Array.isArray(starts) || starts.length > options.budget.maxStartBindings) {
|
|
2820
|
+
return graphBudgetFailure('maxStartBindings', options.budget.maxStartBindings, Array.isArray(starts) ? starts.length : undefined);
|
|
2821
|
+
}
|
|
2822
|
+
for (const declarations of hops) {
|
|
2823
|
+
const invalid = validateGraphInput(starts, namespace, declarations, options);
|
|
2824
|
+
if (invalid) return { ok: false, bindings: [], paths: [], errors: [invalid] };
|
|
2825
|
+
}
|
|
2826
|
+
|
|
2827
|
+
let frontier = [];
|
|
2828
|
+
for (const start of starts) {
|
|
2829
|
+
const startAddress = graphBindingAddress(start, namespace);
|
|
2830
|
+
if (!startAddress) return graphFailure('SANSA_GRAPH_START_ADDRESS_MISSING', 'Graph start binding has no canonical address');
|
|
2831
|
+
frontier.push({ start, startAddress, current: start, steps: [], seen: new Set([startAddress]) });
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
const visitedNodeAddresses = new Set();
|
|
2835
|
+
let visitedEdges = 0;
|
|
2836
|
+
for (const declarations of hops) {
|
|
2837
|
+
const next = [];
|
|
2838
|
+
for (const path of frontier) {
|
|
2839
|
+
const traversed = traverseGraph([path.current], namespace, declarations, options);
|
|
2840
|
+
if (!traversed.ok) return traversed;
|
|
2841
|
+
visitedEdges += traversed.metrics.visitedEdges;
|
|
2842
|
+
if (visitedEdges > options.budget.maxVisitedEdges) {
|
|
2843
|
+
return graphBudgetFailure('maxVisitedEdges', options.budget.maxVisitedEdges, visitedEdges);
|
|
2844
|
+
}
|
|
2845
|
+
for (const step of traversed.paths) {
|
|
2846
|
+
if (path.seen.has(step.endAddress)) {
|
|
2847
|
+
if (options.cyclePolicy === 'error') {
|
|
2848
|
+
return graphFailure('SANSA_GRAPH_CYCLE_DETECTED', `Graph sequence revisited binding '${step.endAddress}'`, {
|
|
2849
|
+
declarationId: step.declarationId,
|
|
2850
|
+
startAddress: path.startAddress,
|
|
2851
|
+
edgeAddress: step.edgeAddress,
|
|
2852
|
+
targetAddress: step.endAddress,
|
|
2853
|
+
});
|
|
2854
|
+
}
|
|
2855
|
+
continue;
|
|
2856
|
+
}
|
|
2857
|
+
visitedNodeAddresses.add(step.endAddress);
|
|
2858
|
+
if (visitedNodeAddresses.size > options.budget.maxVisitedNodes) {
|
|
2859
|
+
return graphBudgetFailure('maxVisitedNodes', options.budget.maxVisitedNodes, visitedNodeAddresses.size);
|
|
2860
|
+
}
|
|
2861
|
+
next.push({
|
|
2862
|
+
...path,
|
|
2863
|
+
current: step.end,
|
|
2864
|
+
steps: [...path.steps, step],
|
|
2865
|
+
seen: new Set([...path.seen, step.endAddress]),
|
|
2866
|
+
});
|
|
2867
|
+
if (next.length > options.budget.maxResults) {
|
|
2868
|
+
return graphBudgetFailure('maxResults', options.budget.maxResults, next.length);
|
|
2869
|
+
}
|
|
2870
|
+
}
|
|
2871
|
+
}
|
|
2872
|
+
frontier = next;
|
|
2873
|
+
if (frontier.length === 0) break;
|
|
2874
|
+
}
|
|
2875
|
+
|
|
2876
|
+
const bindings = [];
|
|
2877
|
+
const finalAddresses = new Set();
|
|
2878
|
+
const paths = frontier.map((path) => {
|
|
2879
|
+
const endAddress = graphBindingAddress(path.current, namespace);
|
|
2880
|
+
if (!finalAddresses.has(endAddress)) {
|
|
2881
|
+
finalAddresses.add(endAddress);
|
|
2882
|
+
bindings.push(path.current);
|
|
2883
|
+
}
|
|
2884
|
+
return {
|
|
2885
|
+
start: path.start,
|
|
2886
|
+
startAddress: path.startAddress,
|
|
2887
|
+
end: path.current,
|
|
2888
|
+
endAddress,
|
|
2889
|
+
steps: path.steps,
|
|
2890
|
+
};
|
|
2891
|
+
});
|
|
2892
|
+
return {
|
|
2893
|
+
ok: true,
|
|
2894
|
+
bindings,
|
|
2895
|
+
paths,
|
|
2896
|
+
metrics: { visitedNodes: visitedNodeAddresses.size, visitedEdges },
|
|
2897
|
+
diagnostics: [],
|
|
2898
|
+
};
|
|
2899
|
+
}
|
|
2900
|
+
|
|
2901
|
+
function validateGraphInput(starts, namespace, declarations, options) {
|
|
2902
|
+
if (!Array.isArray(starts) || !namespace || typeof namespace !== 'object' || !Array.isArray(declarations)) {
|
|
2903
|
+
return graphError('SANSA_GRAPH_INVALID_INPUT', 'Graph traversal requires start bindings, a namespace, and relationship declarations');
|
|
2904
|
+
}
|
|
2905
|
+
if (!options.budget || typeof options.budget !== 'object') {
|
|
2906
|
+
return graphError('SANSA_GRAPH_BUDGET_REQUIRED', 'Graph traversal requires an explicit budget');
|
|
2907
|
+
}
|
|
2908
|
+
if (typeof options.schemaVersion !== 'string' || options.schemaVersion.length === 0) {
|
|
2909
|
+
return graphError('SANSA_GRAPH_INVALID_INPUT', 'Graph traversal requires an explicit schema version');
|
|
2910
|
+
}
|
|
2911
|
+
for (const field of ['maxDepth', 'maxStartBindings', 'maxVisitedNodes', 'maxVisitedEdges', 'maxResults']) {
|
|
2912
|
+
const value = options.budget[field];
|
|
2913
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
2914
|
+
return graphError('SANSA_GRAPH_INVALID_BUDGET', `Graph budget '${field}' must be a non-negative safe integer`, { budget: field });
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
if (declarations.length > 0 && options.budget.maxDepth < 1) {
|
|
2918
|
+
return graphError('SANSA_GRAPH_BUDGET_EXCEEDED', 'Graph depth budget exceeded', {
|
|
2919
|
+
budget: 'maxDepth',
|
|
2920
|
+
limit: options.budget.maxDepth,
|
|
2921
|
+
observed: 1,
|
|
2922
|
+
});
|
|
2923
|
+
}
|
|
2924
|
+
if (starts.length > options.budget.maxStartBindings) {
|
|
2925
|
+
return graphError('SANSA_GRAPH_BUDGET_EXCEEDED', 'Graph start binding budget exceeded', {
|
|
2926
|
+
budget: 'maxStartBindings',
|
|
2927
|
+
limit: options.budget.maxStartBindings,
|
|
2928
|
+
observed: starts.length,
|
|
2929
|
+
});
|
|
2930
|
+
}
|
|
2931
|
+
const ids = new Set();
|
|
2932
|
+
for (const declaration of declarations) {
|
|
2933
|
+
if (!declaration || typeof declaration !== 'object'
|
|
2934
|
+
|| typeof declaration.id !== 'string' || declaration.id.length === 0
|
|
2935
|
+
|| declaration.direction !== 'directed'
|
|
2936
|
+
|| !isNonEmptyUniqueStringArray(declaration.schemaVersions)
|
|
2937
|
+
|| declaration.namespaceTransition !== 'same-namespace'
|
|
2938
|
+
|| declaration.referenceForm !== 'absolute-address-reference'
|
|
2939
|
+
|| !isNonEmptyUniqueStringArray(declaration.sourceSemanticTypes)
|
|
2940
|
+
|| !isNonEmptyUniqueStringArray(declaration.targetSemanticTypes)
|
|
2941
|
+
|| typeof declaration.edgeAddress !== 'string' || !declaration.edgeAddress.startsWith('?')
|
|
2942
|
+
|| !['zero-or-one', 'exactly-one', 'zero-or-more', 'one-or-more'].includes(declaration.edgeCardinality)
|
|
2943
|
+
|| (declaration.missingEdge !== 'omit' && declaration.missingEdge !== 'error')
|
|
2944
|
+
|| (declaration.danglingTarget !== 'omit' && declaration.danglingTarget !== 'error')) {
|
|
2945
|
+
return graphError('SANSA_GRAPH_INVALID_DECLARATION', 'Graph relationship declaration is invalid');
|
|
2946
|
+
}
|
|
2947
|
+
if (ids.has(declaration.id)) {
|
|
2948
|
+
return graphError('SANSA_GRAPH_DUPLICATE_DECLARATION', `Graph relationship declaration '${declaration.id}' is duplicated`, {
|
|
2949
|
+
declarationId: declaration.id,
|
|
2950
|
+
});
|
|
2951
|
+
}
|
|
2952
|
+
ids.add(declaration.id);
|
|
2953
|
+
}
|
|
2954
|
+
return null;
|
|
2955
|
+
}
|
|
2956
|
+
|
|
2957
|
+
function graphBindingAddress(binding, namespace) {
|
|
2958
|
+
if (typeof binding?.address === 'string') return binding.address;
|
|
2959
|
+
const handle = namespace.bindingHandle?.(binding);
|
|
2960
|
+
return typeof handle === 'string' ? handle : undefined;
|
|
2961
|
+
}
|
|
2962
|
+
|
|
2963
|
+
function graphBindingValue(binding, namespace) {
|
|
2964
|
+
return namespace.value ? namespace.value(binding) : binding?.value;
|
|
2965
|
+
}
|
|
2966
|
+
|
|
2967
|
+
function graphBindingSemanticType(binding, namespace) {
|
|
2968
|
+
const value = typeof namespace.semanticType === 'function'
|
|
2969
|
+
? namespace.semanticType(binding)
|
|
2970
|
+
: binding?.semanticType ?? binding?.datatype;
|
|
2971
|
+
return typeof value === 'string' ? value : '';
|
|
2972
|
+
}
|
|
2973
|
+
|
|
2974
|
+
function graphCardinalityMatches(cardinality, observed) {
|
|
2975
|
+
if (cardinality === 'zero-or-one') return observed <= 1;
|
|
2976
|
+
if (cardinality === 'exactly-one') return observed === 1;
|
|
2977
|
+
if (cardinality === 'one-or-more') return observed >= 1;
|
|
2978
|
+
return true;
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2981
|
+
function isNonEmptyUniqueStringArray(value) {
|
|
2982
|
+
return Array.isArray(value)
|
|
2983
|
+
&& value.length > 0
|
|
2984
|
+
&& value.every((item) => typeof item === 'string' && item.length > 0)
|
|
2985
|
+
&& new Set(value).size === value.length;
|
|
2986
|
+
}
|
|
2987
|
+
|
|
2988
|
+
function graphReferenceTarget(value) {
|
|
2989
|
+
if (typeof value === 'string' && value.startsWith('$')) return value;
|
|
2990
|
+
if (!value || typeof value !== 'object') return undefined;
|
|
2991
|
+
if ((value.type === 'PointerReference' || value.type === 'CloneReference') && typeof value.target === 'string' && value.target.startsWith('$')) {
|
|
2992
|
+
return value.target;
|
|
2993
|
+
}
|
|
2994
|
+
if (value.type === 'StringLiteral' && typeof value.value === 'string' && value.value.startsWith('$')) return value.value;
|
|
2995
|
+
return undefined;
|
|
2996
|
+
}
|
|
2997
|
+
|
|
2998
|
+
function graphBudgetFailure(budget, limit, observed) {
|
|
2999
|
+
return graphFailure('SANSA_GRAPH_BUDGET_EXCEEDED', `Graph budget '${budget}' exceeded`, { budget, limit, observed });
|
|
3000
|
+
}
|
|
3001
|
+
|
|
3002
|
+
function graphFailure(code, message, details = {}) {
|
|
3003
|
+
return { ok: false, bindings: [], paths: [], errors: [graphError(code, message, details)] };
|
|
3004
|
+
}
|
|
3005
|
+
|
|
3006
|
+
function graphError(code, message, details = {}) {
|
|
3007
|
+
return { code, message, ...details };
|
|
3008
|
+
}
|
|
3009
|
+
|
|
2518
3010
|
export function renderAddress(address) {
|
|
2519
3011
|
let output = address.root.kind === 'absolute' ? '$' : '?';
|
|
2520
3012
|
|
|
@@ -4135,7 +4627,7 @@ function queryLiteralMetadata(expression) {
|
|
|
4135
4627
|
case 'date':
|
|
4136
4628
|
case 'time':
|
|
4137
4629
|
case 'datetime':
|
|
4138
|
-
case '
|
|
4630
|
+
case 'wtc':
|
|
4139
4631
|
return { kind: expression.kind, category: 'temporal', semanticType: expression.kind };
|
|
4140
4632
|
case 'null':
|
|
4141
4633
|
return { kind: 'null', category: 'explicitNull', nullReason: expression.nullReason };
|
|
@@ -4867,7 +5359,7 @@ function compareMinimumOrdering(left, right, profile) {
|
|
|
4867
5359
|
function normalizeValueSemanticsCategory(category) {
|
|
4868
5360
|
if (category === 'sansa') return 'sansaAddress';
|
|
4869
5361
|
if (category === 'cloneReference' || category === 'pointerReference') return 'referenceForm';
|
|
4870
|
-
if (['date', 'time', 'datetime', '
|
|
5362
|
+
if (['date', 'time', 'datetime', 'wtc'].includes(category)) return 'temporal';
|
|
4871
5363
|
return category;
|
|
4872
5364
|
}
|
|
4873
5365
|
|
|
@@ -5479,7 +5971,7 @@ function matchesRepresentationKind(namespace, binding, expected) {
|
|
|
5479
5971
|
const actual = typeof namespace.representationKind === 'function'
|
|
5480
5972
|
? namespace.representationKind(binding)
|
|
5481
5973
|
: binding.representationKind ?? binding.kind ?? binding.type;
|
|
5482
|
-
return typeof actual === 'string' && lowerFirst(actual) === expected;
|
|
5974
|
+
return typeof actual === 'string' && (actual === expected || lowerFirst(actual) === expected);
|
|
5483
5975
|
}
|
|
5484
5976
|
|
|
5485
5977
|
function resolveBindingLimitError(limit, observed, selectorIndex) {
|
|
@@ -5526,14 +6018,15 @@ export function renderQualifierTerm(term) {
|
|
|
5526
6018
|
for (const group of parameterGroups) {
|
|
5527
6019
|
output += `<${group.map(renderQualifierTerm).join(',')}>`;
|
|
5528
6020
|
}
|
|
5529
|
-
|
|
5530
|
-
output += `[${renderQualifierArgument(
|
|
6021
|
+
if (term.arguments.length > 0) {
|
|
6022
|
+
output += `[${term.arguments.map(renderQualifierArgument).join(',')}]`;
|
|
5531
6023
|
}
|
|
5532
6024
|
return output;
|
|
5533
6025
|
}
|
|
5534
6026
|
|
|
5535
6027
|
function renderQualifierArgument(argument) {
|
|
5536
6028
|
if (argument.kind === 'token') return argument.value;
|
|
6029
|
+
if (argument.kind === 'number') return String(argument.value);
|
|
5537
6030
|
return quotePayload(argument.value);
|
|
5538
6031
|
}
|
|
5539
6032
|
|
|
@@ -5712,9 +6205,15 @@ class AddressParser {
|
|
|
5712
6205
|
parameters.push(...group);
|
|
5713
6206
|
}
|
|
5714
6207
|
|
|
5715
|
-
|
|
6208
|
+
if (this.match('[')) {
|
|
5716
6209
|
args.push(this.parseQualifierArgument());
|
|
6210
|
+
while (this.match(',')) {
|
|
6211
|
+
args.push(this.parseQualifierArgument());
|
|
6212
|
+
}
|
|
5717
6213
|
this.consume(']');
|
|
6214
|
+
if (this.peek() === '[') {
|
|
6215
|
+
this.fail('Qualifier clarifiers must use a single bracketed list', 'SANSA_INVALID_QUALIFIER');
|
|
6216
|
+
}
|
|
5718
6217
|
}
|
|
5719
6218
|
|
|
5720
6219
|
const next = this.peek();
|
|
@@ -5731,17 +6230,13 @@ class AddressParser {
|
|
|
5731
6230
|
}
|
|
5732
6231
|
|
|
5733
6232
|
const start = this.index;
|
|
5734
|
-
while (!this.atEnd() && this.peek() !== ']') {
|
|
5735
|
-
const char = this.peek();
|
|
5736
|
-
if (!isQualifierArgumentChar(char)) {
|
|
5737
|
-
this.fail(`Invalid unquoted qualifier argument character '${char}'`, 'SANSA_INVALID_QUALIFIER_ARGUMENT_CHAR');
|
|
5738
|
-
}
|
|
6233
|
+
while (!this.atEnd() && this.peek() !== ']' && this.peek() !== ',') {
|
|
5739
6234
|
this.index += 1;
|
|
5740
6235
|
}
|
|
5741
6236
|
if (this.index === start) this.fail('Expected qualifier argument', 'SANSA_EXPECTED_QUALIFIER_ARGUMENT');
|
|
5742
6237
|
const value = this.input.slice(start, this.index);
|
|
5743
|
-
if (!
|
|
5744
|
-
return { kind: '
|
|
6238
|
+
if (!QUALIFIER_NUMBER_RE.test(value)) this.fail('Invalid qualifier argument', 'SANSA_INVALID_QUALIFIER_ARGUMENT');
|
|
6239
|
+
return { kind: 'number', value: Number(value) };
|
|
5745
6240
|
}
|
|
5746
6241
|
|
|
5747
6242
|
parseIdentifier(context) {
|
|
@@ -6840,7 +7335,7 @@ class QueryExpressionParser {
|
|
|
6840
7335
|
const source = this.readSimpleLiteralPayload();
|
|
6841
7336
|
const kind = source.includes('T')
|
|
6842
7337
|
? source.includes('&')
|
|
6843
|
-
? '
|
|
7338
|
+
? 'wtc'
|
|
6844
7339
|
: 'datetime'
|
|
6845
7340
|
: source.includes(':')
|
|
6846
7341
|
? 'time'
|
|
@@ -7721,7 +8216,7 @@ function isQueryTemporalLiteral(source, kind) {
|
|
|
7721
8216
|
&& isValidDateParts(match[1], match[2], match[3])
|
|
7722
8217
|
&& isValidTimeMatch(match, 4);
|
|
7723
8218
|
}
|
|
7724
|
-
if (kind === '
|
|
8219
|
+
if (kind === 'wtc') {
|
|
7725
8220
|
const match = new RegExp(`^${date}T${datetimeTime}&(${zone})$`).exec(source);
|
|
7726
8221
|
return Boolean(match)
|
|
7727
8222
|
&& isValidDateParts(match[1], match[2], match[3])
|