@motebit/protocol 0.7.0 → 0.8.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/dist/__tests__/branded-ids.test.d.ts +2 -0
- package/dist/__tests__/branded-ids.test.d.ts.map +1 -0
- package/dist/__tests__/branded-ids.test.js +70 -0
- package/dist/__tests__/branded-ids.test.js.map +1 -0
- package/dist/__tests__/semiring-laws.test.d.ts +25 -0
- package/dist/__tests__/semiring-laws.test.d.ts.map +1 -0
- package/dist/__tests__/semiring-laws.test.js +231 -0
- package/dist/__tests__/semiring-laws.test.js.map +1 -0
- package/dist/__tests__/semiring.test.d.ts +2 -0
- package/dist/__tests__/semiring.test.d.ts.map +1 -0
- package/dist/__tests__/semiring.test.js +201 -0
- package/dist/__tests__/semiring.test.js.map +1 -0
- package/dist/__tests__/traversal.test.d.ts +2 -0
- package/dist/__tests__/traversal.test.d.ts.map +1 -0
- package/dist/__tests__/traversal.test.js +331 -0
- package/dist/__tests__/traversal.test.js.map +1 -0
- package/dist/__tests__/trust-algebra.test.d.ts +2 -0
- package/dist/__tests__/trust-algebra.test.d.ts.map +1 -0
- package/dist/__tests__/trust-algebra.test.js +105 -0
- package/dist/__tests__/trust-algebra.test.js.map +1 -0
- package/dist/credential-anchor.d.ts +86 -0
- package/dist/credential-anchor.d.ts.map +1 -0
- package/dist/credential-anchor.js +8 -0
- package/dist/credential-anchor.js.map +1 -0
- package/dist/graph.d.ts +50 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +95 -0
- package/dist/graph.js.map +1 -0
- package/dist/index.d.ts +123 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/semiring.d.ts +88 -0
- package/dist/semiring.d.ts.map +1 -0
- package/dist/semiring.js +166 -0
- package/dist/semiring.js.map +1 -0
- package/dist/traversal.d.ts +60 -0
- package/dist/traversal.d.ts.map +1 -0
- package/dist/traversal.js +183 -0
- package/dist/traversal.js.map +1 -0
- package/dist/trust-algebra.d.ts +34 -0
- package/dist/trust-algebra.d.ts.map +1 -0
- package/dist/trust-algebra.js +58 -0
- package/dist/trust-algebra.js.map +1 -0
- package/package.json +6 -5
package/dist/semiring.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Semiring — the algebraic foundation for agent network computation.
|
|
3
|
+
*
|
|
4
|
+
* A semiring (S, ⊕, ⊗, 0, 1) satisfies:
|
|
5
|
+
* (S, ⊕, 0) — commutative monoid (aggregation of parallel alternatives)
|
|
6
|
+
* (S, ⊗, 1) — monoid (sequential composition)
|
|
7
|
+
* ⊗ distributes over ⊕: a ⊗ (b ⊕ c) = (a ⊗ b) ⊕ (a ⊗ c)
|
|
8
|
+
* 0 annihilates: a ⊗ 0 = 0 ⊗ a = 0
|
|
9
|
+
*
|
|
10
|
+
* Different semirings model different routing concerns over the same graph:
|
|
11
|
+
* Trust: (max, ×, 0, 1) → most trusted delegation chain
|
|
12
|
+
* Cost: (min, +, ∞, 0) → cheapest agent pipeline
|
|
13
|
+
* Latency: (min, +, ∞, 0) → fastest sequential path
|
|
14
|
+
* Bottleneck: (max, min, 0, 1) → widest bottleneck path (capacity)
|
|
15
|
+
* Reliability: (max, ×, 0, 1) → most reliable chain
|
|
16
|
+
* Boolean: (∨, ∧, ⊥, ⊤) → reachability
|
|
17
|
+
*
|
|
18
|
+
* One graph. One algorithm. Swap the semiring. Different answer.
|
|
19
|
+
*/
|
|
20
|
+
// ── Concrete Semirings ──────────────────────────────────────────────
|
|
21
|
+
/** (max, ×, 0, 1) — most trusted delegation chain through the network. */
|
|
22
|
+
export const TrustSemiring = {
|
|
23
|
+
zero: 0,
|
|
24
|
+
one: 1,
|
|
25
|
+
add: (a, b) => Math.max(a, b),
|
|
26
|
+
mul: (a, b) => a * b,
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* (min, +, ∞, 0) — cheapest path through the agent network.
|
|
30
|
+
* Tropical semiring. Same algebra as Dijkstra / Bellman-Ford.
|
|
31
|
+
*/
|
|
32
|
+
export const CostSemiring = {
|
|
33
|
+
zero: Infinity,
|
|
34
|
+
one: 0,
|
|
35
|
+
add: (a, b) => Math.min(a, b),
|
|
36
|
+
mul: (a, b) => a + b,
|
|
37
|
+
};
|
|
38
|
+
/** (min, +, ∞, 0) — fastest sequential path (sum of edge latencies). */
|
|
39
|
+
export const LatencySemiring = {
|
|
40
|
+
zero: Infinity,
|
|
41
|
+
one: 0,
|
|
42
|
+
add: (a, b) => Math.min(a, b),
|
|
43
|
+
mul: (a, b) => a + b,
|
|
44
|
+
};
|
|
45
|
+
/** (max, min, 0, ∞) — widest bottleneck path (capacity-limited routing). */
|
|
46
|
+
export const BottleneckSemiring = {
|
|
47
|
+
zero: 0,
|
|
48
|
+
one: Infinity,
|
|
49
|
+
add: (a, b) => Math.max(a, b),
|
|
50
|
+
mul: (a, b) => Math.min(a, b),
|
|
51
|
+
};
|
|
52
|
+
/** (max, ×, 0, 1) — most reliable chain (probability product). */
|
|
53
|
+
export const ReliabilitySemiring = {
|
|
54
|
+
zero: 0,
|
|
55
|
+
one: 1,
|
|
56
|
+
add: (a, b) => Math.max(a, b),
|
|
57
|
+
mul: (a, b) => a * b,
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* (min, +, ∞, 0) — lowest regulatory risk path.
|
|
61
|
+
* Risk accumulates along delegation chains (additive composition),
|
|
62
|
+
* parallel alternatives pick the lowest-risk route (min choice).
|
|
63
|
+
*
|
|
64
|
+
* Edge weights represent risk scores: 0 = no risk, ∞ = impossible.
|
|
65
|
+
* Jurisdictional data handling, compliance requirements, audit depth —
|
|
66
|
+
* all accumulate when one agent delegates to another.
|
|
67
|
+
*/
|
|
68
|
+
export const RegulatoryRiskSemiring = {
|
|
69
|
+
zero: Infinity,
|
|
70
|
+
one: 0,
|
|
71
|
+
add: (a, b) => Math.min(a, b),
|
|
72
|
+
mul: (a, b) => a + b,
|
|
73
|
+
};
|
|
74
|
+
/** (∨, ∧, false, true) — can agent A reach agent B? */
|
|
75
|
+
export const BooleanSemiring = {
|
|
76
|
+
zero: false,
|
|
77
|
+
one: true,
|
|
78
|
+
add: (a, b) => a || b,
|
|
79
|
+
mul: (a, b) => a && b,
|
|
80
|
+
};
|
|
81
|
+
// ── Semiring Combinators ────────────────────────────────────────────
|
|
82
|
+
/**
|
|
83
|
+
* Product semiring: optimize multiple concerns simultaneously.
|
|
84
|
+
*
|
|
85
|
+
* (A × B, ⊕_A × ⊕_B, ⊗_A × ⊗_B, (0_A, 0_B), (1_A, 1_B))
|
|
86
|
+
*
|
|
87
|
+
* One graph traversal computes trust × cost × latency in a single pass.
|
|
88
|
+
*/
|
|
89
|
+
export function productSemiring(sa, sb) {
|
|
90
|
+
const saEq = sa.eq;
|
|
91
|
+
const sbEq = sb.eq;
|
|
92
|
+
const eqA = (a, b) => (saEq ? saEq(a, b) : a === b);
|
|
93
|
+
const eqB = (a, b) => (sbEq ? sbEq(a, b) : a === b);
|
|
94
|
+
return {
|
|
95
|
+
zero: [sa.zero, sb.zero],
|
|
96
|
+
one: [sa.one, sb.one],
|
|
97
|
+
add: (x, y) => [sa.add(x[0], y[0]), sb.add(x[1], y[1])],
|
|
98
|
+
mul: (x, y) => [sa.mul(x[0], y[0]), sb.mul(x[1], y[1])],
|
|
99
|
+
eq: (x, y) => eqA(x[0], y[0]) && eqB(x[1], y[1]),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Lift a scalar semiring into a named-fields record semiring.
|
|
104
|
+
* Useful when you want labeled dimensions instead of nested tuples.
|
|
105
|
+
*
|
|
106
|
+
* const Multi = recordSemiring({ trust: TrustSemiring, cost: CostSemiring });
|
|
107
|
+
* // Semiring<{ trust: number; cost: number }>
|
|
108
|
+
*/
|
|
109
|
+
export function recordSemiring(fields) {
|
|
110
|
+
const keys = Object.keys(fields);
|
|
111
|
+
const zero = {};
|
|
112
|
+
const one = {};
|
|
113
|
+
const eqs = {};
|
|
114
|
+
for (const k of keys) {
|
|
115
|
+
const f = fields[k];
|
|
116
|
+
zero[k] = f.zero;
|
|
117
|
+
one[k] = f.one;
|
|
118
|
+
const fEq = f.eq;
|
|
119
|
+
eqs[k] = fEq
|
|
120
|
+
? (a, b) => fEq(a, b)
|
|
121
|
+
: (a, b) => a === b;
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
zero,
|
|
125
|
+
one,
|
|
126
|
+
add(a, b) {
|
|
127
|
+
const r = {};
|
|
128
|
+
for (const k of keys) {
|
|
129
|
+
const f = fields[k];
|
|
130
|
+
r[k] = f.add(a[k], b[k]);
|
|
131
|
+
}
|
|
132
|
+
return r;
|
|
133
|
+
},
|
|
134
|
+
mul(a, b) {
|
|
135
|
+
const r = {};
|
|
136
|
+
for (const k of keys) {
|
|
137
|
+
const f = fields[k];
|
|
138
|
+
r[k] = f.mul(a[k], b[k]);
|
|
139
|
+
}
|
|
140
|
+
return r;
|
|
141
|
+
},
|
|
142
|
+
eq(a, b) {
|
|
143
|
+
for (const k of keys) {
|
|
144
|
+
if (!eqs[k](a[k], b[k]))
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Map a semiring through an isomorphism.
|
|
153
|
+
* Useful for wrapping/unwrapping branded types or unit conversions.
|
|
154
|
+
*/
|
|
155
|
+
export function mappedSemiring(base, to, from) {
|
|
156
|
+
const bEq = base.eq;
|
|
157
|
+
const baseEq = (a, b) => (bEq ? bEq(a, b) : a === b);
|
|
158
|
+
return {
|
|
159
|
+
zero: to(base.zero),
|
|
160
|
+
one: to(base.one),
|
|
161
|
+
add: (a, b) => to(base.add(from(a), from(b))),
|
|
162
|
+
mul: (a, b) => to(base.mul(from(a), from(b))),
|
|
163
|
+
eq: (a, b) => baseEq(from(a), from(b)),
|
|
164
|
+
};
|
|
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CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7C,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;KACvC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic graph traversal over arbitrary semirings.
|
|
3
|
+
*
|
|
4
|
+
* The core insight: shortest path, most-trusted path, cheapest path,
|
|
5
|
+
* most reliable path, and reachability are ALL the same algorithm.
|
|
6
|
+
* The semiring determines the answer.
|
|
7
|
+
*
|
|
8
|
+
* Bellman-Ford generalized: relax edges using ⊕ (choice) and ⊗ (composition).
|
|
9
|
+
* Floyd-Warshall generalized: all-pairs transitive closure.
|
|
10
|
+
*
|
|
11
|
+
* These are the only two traversal algorithms the system needs.
|
|
12
|
+
* Every routing query is a semiring instantiation of one of them.
|
|
13
|
+
*/
|
|
14
|
+
import type { WeightedDigraph } from "./graph.js";
|
|
15
|
+
/**
|
|
16
|
+
* Single-source optimal paths via generalized Bellman-Ford.
|
|
17
|
+
*
|
|
18
|
+
* Returns a map from each reachable node to the optimal semiring value
|
|
19
|
+
* of the best path from `source` to that node.
|
|
20
|
+
*
|
|
21
|
+
* - Over TrustSemiring: most trusted delegation chain from source
|
|
22
|
+
* - Over CostSemiring: cheapest pipeline from source
|
|
23
|
+
* - Over BooleanSemiring: reachable set from source
|
|
24
|
+
* - Over product semiring: all of the above simultaneously
|
|
25
|
+
*
|
|
26
|
+
* Complexity: O(V × E) — safe for graphs with negative-weight analogs
|
|
27
|
+
* (semirings where ⊕ is not monotone). For monotone semirings
|
|
28
|
+
* (trust, cost), could be optimized to Dijkstra-like O((V+E) log V).
|
|
29
|
+
*/
|
|
30
|
+
export declare function optimalPaths<T>(graph: WeightedDigraph<T>, source: string): Map<string, T>;
|
|
31
|
+
/**
|
|
32
|
+
* Optimal path between two specific nodes.
|
|
33
|
+
* Convenience wrapper around optimalPaths.
|
|
34
|
+
*/
|
|
35
|
+
export declare function optimalPath<T>(graph: WeightedDigraph<T>, source: string, target: string): T;
|
|
36
|
+
/**
|
|
37
|
+
* All-pairs transitive closure via generalized Floyd-Warshall.
|
|
38
|
+
*
|
|
39
|
+
* Computes the optimal semiring value between every pair of nodes.
|
|
40
|
+
* Returns a nested map: closure.get(from)?.get(to) → optimal value.
|
|
41
|
+
*
|
|
42
|
+
* Use cases:
|
|
43
|
+
* - Pre-compute all trust relationships in a network
|
|
44
|
+
* - Find all cheapest routes for capacity planning
|
|
45
|
+
* - Detect isolated subgraphs (boolean semiring)
|
|
46
|
+
*
|
|
47
|
+
* Complexity: O(V³). Use optimalPaths for single-source queries on large graphs.
|
|
48
|
+
*/
|
|
49
|
+
export declare function transitiveClosure<T>(graph: WeightedDigraph<T>): Map<string, Map<string, T>>;
|
|
50
|
+
/**
|
|
51
|
+
* Reconstruct the actual optimal path (sequence of node IDs).
|
|
52
|
+
*
|
|
53
|
+
* Returns null if no path exists (value equals semiring zero).
|
|
54
|
+
* Runs a modified Bellman-Ford that tracks predecessors.
|
|
55
|
+
*/
|
|
56
|
+
export declare function optimalPathTrace<T>(graph: WeightedDigraph<T>, source: string, target: string): {
|
|
57
|
+
value: T;
|
|
58
|
+
path: string[];
|
|
59
|
+
} | null;
|
|
60
|
+
//# sourceMappingURL=traversal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traversal.d.ts","sourceRoot":"","sources":["../src/traversal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAsCzF;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,CAAC,CAE3F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CA6C3F;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EACzB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb;IAAE,KAAK,EAAE,CAAC,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAA;CAAE,GAAG,IAAI,CAiDrC"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic graph traversal over arbitrary semirings.
|
|
3
|
+
*
|
|
4
|
+
* The core insight: shortest path, most-trusted path, cheapest path,
|
|
5
|
+
* most reliable path, and reachability are ALL the same algorithm.
|
|
6
|
+
* The semiring determines the answer.
|
|
7
|
+
*
|
|
8
|
+
* Bellman-Ford generalized: relax edges using ⊕ (choice) and ⊗ (composition).
|
|
9
|
+
* Floyd-Warshall generalized: all-pairs transitive closure.
|
|
10
|
+
*
|
|
11
|
+
* These are the only two traversal algorithms the system needs.
|
|
12
|
+
* Every routing query is a semiring instantiation of one of them.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Single-source optimal paths via generalized Bellman-Ford.
|
|
16
|
+
*
|
|
17
|
+
* Returns a map from each reachable node to the optimal semiring value
|
|
18
|
+
* of the best path from `source` to that node.
|
|
19
|
+
*
|
|
20
|
+
* - Over TrustSemiring: most trusted delegation chain from source
|
|
21
|
+
* - Over CostSemiring: cheapest pipeline from source
|
|
22
|
+
* - Over BooleanSemiring: reachable set from source
|
|
23
|
+
* - Over product semiring: all of the above simultaneously
|
|
24
|
+
*
|
|
25
|
+
* Complexity: O(V × E) — safe for graphs with negative-weight analogs
|
|
26
|
+
* (semirings where ⊕ is not monotone). For monotone semirings
|
|
27
|
+
* (trust, cost), could be optimized to Dijkstra-like O((V+E) log V).
|
|
28
|
+
*/
|
|
29
|
+
export function optimalPaths(graph, source) {
|
|
30
|
+
const sr = graph.sr;
|
|
31
|
+
const srEq = sr.eq;
|
|
32
|
+
const eq = (a, b) => (srEq ? srEq(a, b) : a === b);
|
|
33
|
+
const dist = new Map();
|
|
34
|
+
// Initialize: source = 1 (identity), everything else = 0 (worst)
|
|
35
|
+
for (const node of graph.nodes()) {
|
|
36
|
+
dist.set(node, sr.zero);
|
|
37
|
+
}
|
|
38
|
+
dist.set(source, sr.one);
|
|
39
|
+
const nodes = [...graph.nodes()];
|
|
40
|
+
const nodeCount = nodes.length;
|
|
41
|
+
// Relax all edges V-1 times
|
|
42
|
+
for (let i = 0; i < nodeCount - 1; i++) {
|
|
43
|
+
let changed = false;
|
|
44
|
+
for (const node of nodes) {
|
|
45
|
+
const dNode = dist.get(node);
|
|
46
|
+
// Skip unreachable nodes — mul(zero, w) = zero (annihilation), so no update possible
|
|
47
|
+
if (eq(dNode, sr.zero))
|
|
48
|
+
continue;
|
|
49
|
+
for (const [neighbor, weight] of graph.neighbors(node)) {
|
|
50
|
+
// New candidate: path-to-node ⊗ edge-weight
|
|
51
|
+
const candidate = sr.mul(dNode, weight);
|
|
52
|
+
const current = dist.get(neighbor);
|
|
53
|
+
const combined = sr.add(current, candidate);
|
|
54
|
+
// Only update if the value actually changed
|
|
55
|
+
if (!eq(combined, current)) {
|
|
56
|
+
dist.set(neighbor, combined);
|
|
57
|
+
changed = true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!changed)
|
|
62
|
+
break; // Early termination — converged
|
|
63
|
+
}
|
|
64
|
+
return dist;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Optimal path between two specific nodes.
|
|
68
|
+
* Convenience wrapper around optimalPaths.
|
|
69
|
+
*/
|
|
70
|
+
export function optimalPath(graph, source, target) {
|
|
71
|
+
return optimalPaths(graph, source).get(target) ?? graph.sr.zero;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* All-pairs transitive closure via generalized Floyd-Warshall.
|
|
75
|
+
*
|
|
76
|
+
* Computes the optimal semiring value between every pair of nodes.
|
|
77
|
+
* Returns a nested map: closure.get(from)?.get(to) → optimal value.
|
|
78
|
+
*
|
|
79
|
+
* Use cases:
|
|
80
|
+
* - Pre-compute all trust relationships in a network
|
|
81
|
+
* - Find all cheapest routes for capacity planning
|
|
82
|
+
* - Detect isolated subgraphs (boolean semiring)
|
|
83
|
+
*
|
|
84
|
+
* Complexity: O(V³). Use optimalPaths for single-source queries on large graphs.
|
|
85
|
+
*/
|
|
86
|
+
export function transitiveClosure(graph) {
|
|
87
|
+
const sr = graph.sr;
|
|
88
|
+
const nodes = [...graph.nodes()];
|
|
89
|
+
const n = nodes.length;
|
|
90
|
+
const idx = new Map();
|
|
91
|
+
for (let i = 0; i < n; i++)
|
|
92
|
+
idx.set(nodes[i], i);
|
|
93
|
+
// Initialize distance matrix
|
|
94
|
+
const dist = [];
|
|
95
|
+
for (let i = 0; i < n; i++) {
|
|
96
|
+
dist.push([]);
|
|
97
|
+
for (let j = 0; j < n; j++) {
|
|
98
|
+
dist[i].push(i === j ? sr.one : sr.zero);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Load edges
|
|
102
|
+
for (const node of nodes) {
|
|
103
|
+
const i = idx.get(node);
|
|
104
|
+
for (const [neighbor, weight] of graph.neighbors(node)) {
|
|
105
|
+
const j = idx.get(neighbor);
|
|
106
|
+
dist[i][j] = sr.add(dist[i][j], weight);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Floyd-Warshall relaxation
|
|
110
|
+
for (let k = 0; k < n; k++) {
|
|
111
|
+
for (let i = 0; i < n; i++) {
|
|
112
|
+
for (let j = 0; j < n; j++) {
|
|
113
|
+
const through_k = sr.mul(dist[i][k], dist[k][j]);
|
|
114
|
+
dist[i][j] = sr.add(dist[i][j], through_k);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Convert to nested map
|
|
119
|
+
const result = new Map();
|
|
120
|
+
for (let i = 0; i < n; i++) {
|
|
121
|
+
const row = new Map();
|
|
122
|
+
for (let j = 0; j < n; j++) {
|
|
123
|
+
row.set(nodes[j], dist[i][j]);
|
|
124
|
+
}
|
|
125
|
+
result.set(nodes[i], row);
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Reconstruct the actual optimal path (sequence of node IDs).
|
|
131
|
+
*
|
|
132
|
+
* Returns null if no path exists (value equals semiring zero).
|
|
133
|
+
* Runs a modified Bellman-Ford that tracks predecessors.
|
|
134
|
+
*/
|
|
135
|
+
export function optimalPathTrace(graph, source, target) {
|
|
136
|
+
const sr = graph.sr;
|
|
137
|
+
const dist = new Map();
|
|
138
|
+
const pred = new Map();
|
|
139
|
+
for (const node of graph.nodes()) {
|
|
140
|
+
dist.set(node, sr.zero);
|
|
141
|
+
pred.set(node, null);
|
|
142
|
+
}
|
|
143
|
+
dist.set(source, sr.one);
|
|
144
|
+
const nodes = [...graph.nodes()];
|
|
145
|
+
const srEq = sr.eq;
|
|
146
|
+
const eq = (a, b) => (srEq ? srEq(a, b) : a === b);
|
|
147
|
+
for (let i = 0; i < nodes.length - 1; i++) {
|
|
148
|
+
let changed = false;
|
|
149
|
+
for (const node of nodes) {
|
|
150
|
+
const dNode = dist.get(node);
|
|
151
|
+
if (eq(dNode, sr.zero))
|
|
152
|
+
continue;
|
|
153
|
+
for (const [neighbor, weight] of graph.neighbors(node)) {
|
|
154
|
+
const candidate = sr.mul(dNode, weight);
|
|
155
|
+
const current = dist.get(neighbor);
|
|
156
|
+
const combined = sr.add(current, candidate);
|
|
157
|
+
if (!eq(combined, current)) {
|
|
158
|
+
dist.set(neighbor, combined);
|
|
159
|
+
pred.set(neighbor, node);
|
|
160
|
+
changed = true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (!changed)
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
const value = dist.get(target) ?? sr.zero;
|
|
168
|
+
if (value === sr.zero && source !== target)
|
|
169
|
+
return null;
|
|
170
|
+
// Reconstruct path
|
|
171
|
+
const path = [];
|
|
172
|
+
let current = target;
|
|
173
|
+
const visited = new Set();
|
|
174
|
+
while (current != null && !visited.has(current)) {
|
|
175
|
+
visited.add(current);
|
|
176
|
+
path.unshift(current);
|
|
177
|
+
current = pred.get(current) ?? null;
|
|
178
|
+
}
|
|
179
|
+
if (path[0] !== source)
|
|
180
|
+
return null;
|
|
181
|
+
return { value, path };
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=traversal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traversal.js","sourceRoot":"","sources":["../src/traversal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CAAI,KAAyB,EAAE,MAAc;IACvE,MAAM,EAAE,GAAG,KAAK,CAAC,EAAE,CAAC;IACpB,MAAM,IAAI,GAAG,EAAE,CAAC,EAAE,CAAC;IACnB,MAAM,EAAE,GAAG,CAAC,CAAI,EAAE,CAAI,EAAW,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAClE,MAAM,IAAI,GAAG,IAAI,GAAG,EAAa,CAAC;IAElC,iEAAiE;IACjE,KAAK,MAAM,IAA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IAAI,GAAG,EAAU,CAAC;IAClC,OAAO,OAAO,IAAI,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;QAChD,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACrB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACtB,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;IACtC,CAAC;IAED,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM;QAAE,OAAO,IAAI,CAAC;IACpC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trust algebra: concrete semiring operations for agent trust scoring.
|
|
3
|
+
*
|
|
4
|
+
* (TrustScores, max, ×, 0, 1) — standard algebraic path problem.
|
|
5
|
+
* Multiplicative discount for serial chains, max for parallel paths.
|
|
6
|
+
*
|
|
7
|
+
* These are protocol-level primitives: any compatible implementation
|
|
8
|
+
* must compute trust the same way for interoperable routing.
|
|
9
|
+
*/
|
|
10
|
+
import type { TrustTransitionThresholds, AgentTrustLevel } from "./index.js";
|
|
11
|
+
/**
|
|
12
|
+
* Canonical AgentTrustLevel → [0,1] mapping (single source of truth).
|
|
13
|
+
*
|
|
14
|
+
* Uses string literals instead of enum computed keys to avoid circular
|
|
15
|
+
* initialization between trust-algebra.ts ↔ index.ts. The values match
|
|
16
|
+
* AgentTrustLevel enum values exactly.
|
|
17
|
+
*/
|
|
18
|
+
export declare const TRUST_LEVEL_SCORES: Record<string, number>;
|
|
19
|
+
/** Convert a trust level to its numeric score. */
|
|
20
|
+
export declare function trustLevelToScore(level: AgentTrustLevel | string): number;
|
|
21
|
+
/** Semiring zero — annihilator for ⊗, identity for ⊕. */
|
|
22
|
+
export declare const TRUST_ZERO = 0;
|
|
23
|
+
/** Semiring one — identity for ⊗. */
|
|
24
|
+
export declare const TRUST_ONE = 1;
|
|
25
|
+
/** ⊕: parallel paths — pick the best route. */
|
|
26
|
+
export declare function trustAdd(a: number, b: number): number;
|
|
27
|
+
/** ⊗: serial chain — discount per hop. */
|
|
28
|
+
export declare function trustMultiply(a: number, b: number): number;
|
|
29
|
+
/** Fold a chain of trust scores with ⊗. Empty chain → 1.0 (identity). */
|
|
30
|
+
export declare function composeTrustChain(scores: number[]): number;
|
|
31
|
+
/** Fold parallel route scores with ⊕. No routes → 0.0 (identity). */
|
|
32
|
+
export declare function joinParallelRoutes(scores: number[]): number;
|
|
33
|
+
export declare const DEFAULT_TRUST_THRESHOLDS: TrustTransitionThresholds;
|
|
34
|
+
//# sourceMappingURL=trust-algebra.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trust-algebra.d.ts","sourceRoot":"","sources":["../src/trust-algebra.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAI7E;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAMrD,CAAC;AAEF,kDAAkD;AAClD,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,GAAG,MAAM,CAEzE;AAED,yDAAyD;AACzD,eAAO,MAAM,UAAU,IAAI,CAAC;AAE5B,qCAAqC;AACrC,eAAO,MAAM,SAAS,IAAI,CAAC;AAE3B,+CAA+C;AAC/C,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,0CAA0C;AAC1C,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED,yEAAyE;AACzE,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAE1D;AAED,qEAAqE;AACrE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAE3D;AAID,eAAO,MAAM,wBAAwB,EAAE,yBAOtC,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trust algebra: concrete semiring operations for agent trust scoring.
|
|
3
|
+
*
|
|
4
|
+
* (TrustScores, max, ×, 0, 1) — standard algebraic path problem.
|
|
5
|
+
* Multiplicative discount for serial chains, max for parallel paths.
|
|
6
|
+
*
|
|
7
|
+
* These are protocol-level primitives: any compatible implementation
|
|
8
|
+
* must compute trust the same way for interoperable routing.
|
|
9
|
+
*/
|
|
10
|
+
// ── Trust Semiring Algebra ──────────────────────────────────────────
|
|
11
|
+
/**
|
|
12
|
+
* Canonical AgentTrustLevel → [0,1] mapping (single source of truth).
|
|
13
|
+
*
|
|
14
|
+
* Uses string literals instead of enum computed keys to avoid circular
|
|
15
|
+
* initialization between trust-algebra.ts ↔ index.ts. The values match
|
|
16
|
+
* AgentTrustLevel enum values exactly.
|
|
17
|
+
*/
|
|
18
|
+
export const TRUST_LEVEL_SCORES = {
|
|
19
|
+
unknown: 0.1,
|
|
20
|
+
first_contact: 0.3,
|
|
21
|
+
verified: 0.6,
|
|
22
|
+
trusted: 0.9,
|
|
23
|
+
blocked: 0.0,
|
|
24
|
+
};
|
|
25
|
+
/** Convert a trust level to its numeric score. */
|
|
26
|
+
export function trustLevelToScore(level) {
|
|
27
|
+
return TRUST_LEVEL_SCORES[level] ?? 0.1;
|
|
28
|
+
}
|
|
29
|
+
/** Semiring zero — annihilator for ⊗, identity for ⊕. */
|
|
30
|
+
export const TRUST_ZERO = 0;
|
|
31
|
+
/** Semiring one — identity for ⊗. */
|
|
32
|
+
export const TRUST_ONE = 1;
|
|
33
|
+
/** ⊕: parallel paths — pick the best route. */
|
|
34
|
+
export function trustAdd(a, b) {
|
|
35
|
+
return Math.max(a, b);
|
|
36
|
+
}
|
|
37
|
+
/** ⊗: serial chain — discount per hop. */
|
|
38
|
+
export function trustMultiply(a, b) {
|
|
39
|
+
return a * b;
|
|
40
|
+
}
|
|
41
|
+
/** Fold a chain of trust scores with ⊗. Empty chain → 1.0 (identity). */
|
|
42
|
+
export function composeTrustChain(scores) {
|
|
43
|
+
return scores.reduce(trustMultiply, TRUST_ONE);
|
|
44
|
+
}
|
|
45
|
+
/** Fold parallel route scores with ⊕. No routes → 0.0 (identity). */
|
|
46
|
+
export function joinParallelRoutes(scores) {
|
|
47
|
+
return scores.reduce(trustAdd, TRUST_ZERO);
|
|
48
|
+
}
|
|
49
|
+
// ── Default Thresholds ─────────────────────────────────────────────
|
|
50
|
+
export const DEFAULT_TRUST_THRESHOLDS = {
|
|
51
|
+
promoteToVerified_minTasks: 5,
|
|
52
|
+
promoteToVerified_minRate: 0.8,
|
|
53
|
+
promoteToTrusted_minTasks: 20,
|
|
54
|
+
promoteToTrusted_minRate: 0.9,
|
|
55
|
+
demote_belowRate: 0.5,
|
|
56
|
+
demote_minTasks: 3,
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=trust-algebra.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trust-algebra.js","sourceRoot":"","sources":["../src/trust-algebra.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,uEAAuE;AAEvE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAA2B;IACxD,OAAO,EAAE,GAAG;IACZ,aAAa,EAAE,GAAG;IAClB,QAAQ,EAAE,GAAG;IACb,OAAO,EAAE,GAAG;IACZ,OAAO,EAAE,GAAG;CACb,CAAC;AAEF,kDAAkD;AAClD,MAAM,UAAU,iBAAiB,CAAC,KAA+B;IAC/D,OAAO,kBAAkB,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC;AAC1C,CAAC;AAED,yDAAyD;AACzD,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAE5B,qCAAqC;AACrC,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,CAAC;AAE3B,+CAA+C;AAC/C,MAAM,UAAU,QAAQ,CAAC,CAAS,EAAE,CAAS;IAC3C,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACxB,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,aAAa,CAAC,CAAS,EAAE,CAAS;IAChD,OAAO,CAAC,GAAG,CAAC,CAAC;AACf,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,iBAAiB,CAAC,MAAgB;IAChD,OAAO,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;AACjD,CAAC;AAED,qEAAqE;AACrE,MAAM,UAAU,kBAAkB,CAAC,MAAgB;IACjD,OAAO,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;AAC7C,CAAC;AAED,sEAAsE;AAEtE,MAAM,CAAC,MAAM,wBAAwB,GAA8B;IACjE,0BAA0B,EAAE,CAAC;IAC7B,yBAAyB,EAAE,GAAG;IAC9B,yBAAyB,EAAE,EAAE;IAC7B,wBAAwB,EAAE,GAAG;IAC7B,gBAAgB,EAAE,GAAG;IACrB,eAAe,EAAE,CAAC;CACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motebit/protocol",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Motebit network protocol types — identity, receipts, credentials, settlement, trust algebra. MIT, zero dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
|
-
"dist
|
|
16
|
-
"dist
|
|
17
|
-
"dist
|
|
18
|
-
"dist
|
|
15
|
+
"dist/**/*.js",
|
|
16
|
+
"dist/**/*.js.map",
|
|
17
|
+
"dist/**/*.d.ts",
|
|
18
|
+
"dist/**/*.d.ts.map",
|
|
19
19
|
"LICENSE",
|
|
20
20
|
"README.md"
|
|
21
21
|
],
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "^22.0.0",
|
|
50
50
|
"typescript": "^5.6.0",
|
|
51
|
+
"fast-check": "^4.6.0",
|
|
51
52
|
"vitest": "^2.1.0"
|
|
52
53
|
},
|
|
53
54
|
"engines": {
|