pi-mega-compact 0.19.1 → 0.19.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.
- package/dist/config/vector-cortex.js +21 -0
- package/dist/config.js +1 -1
- package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +2 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-query.js +46 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-topology.js +145 -5
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +1 -0
- package/dist/extensions/dashboard-server/routes.js +1 -1
- package/dist/extensions/dashboard-server/server.js +3 -1
- package/dist/src/config/vector-cortex.js +21 -0
- package/dist/src/config.js +1 -1
- package/dist/src/tieredRouter.js +8 -0
- package/dist/src/vector-cortex/migrations/router-generation-v2.js +252 -0
- package/dist/src/vector-cortex/topology/build.js +232 -0
- package/dist/src/vector-cortex/topology/index.js +113 -0
- package/dist/src/vector-cortex/topology/query.js +342 -0
- package/dist/src/vector-cortex/topology/types.js +54 -0
- package/dist/src/vector-cortex/vc3b-support.js +125 -0
- package/dist/vector-cortex/migrations/router-generation-v2.js +252 -0
- package/dist/vector-cortex/topology/build.js +232 -0
- package/dist/vector-cortex/topology/index.js +113 -0
- package/dist/vector-cortex/topology/query.js +342 -0
- package/dist/vector-cortex/topology/types.js +54 -0
- package/dist/vector-cortex/vc3b-support.js +125 -0
- package/extensions/dashboard-client/dist/assets/{AreaChart-B9KYKrUE.js → AreaChart-DqiFSnDx.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-B9KYKrUE.js.map → AreaChart-DqiFSnDx.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-D1sWkR6j.js → BarChart-Cqg-PQMK.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-D1sWkR6j.js.map → BarChart-Cqg-PQMK.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-BR8MYcJq.js → CacheTab-BPNs0xIF.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-BR8MYcJq.js.map → CacheTab-BPNs0xIF.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-BRNpDMvP.js → EventsTab-BQf7OGkf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-BRNpDMvP.js.map → EventsTab-BQf7OGkf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-rr5nXpjq.js → HealthTab-B14TaSPN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-rr5nXpjq.js.map → HealthTab-B14TaSPN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtjPWnzN.js → MaintenanceTab-D8lkRoa1.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtjPWnzN.js.map → MaintenanceTab-D8lkRoa1.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-5SnFDIYc.js → MemoryMapTab-BOWtGfpx.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-5SnFDIYc.js.map → MemoryMapTab-BOWtGfpx.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-4MZfwRhq.js → MetricsTab-7XI9cWaX.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-4MZfwRhq.js.map → MetricsTab-7XI9cWaX.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BAKENnbk.js → OverviewTab-C9KaGYvh.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BAKENnbk.js.map → OverviewTab-C9KaGYvh.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-7-z_OmZo.js → ReposTab-BwESNhox.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-7-z_OmZo.js.map → ReposTab-BwESNhox.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-D_r2ki0y.js → SessionsTab-DKH55Rda.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-D_r2ki0y.js.map → SessionsTab-DKH55Rda.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-DPujtYgv.js → SetupTab-zDxf-nJd.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-DPujtYgv.js.map → SetupTab-zDxf-nJd.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CLHy30t6.js → TimeSavedCard-C9isog4L.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CLHy30t6.js.map → TimeSavedCard-C9isog4L.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Coul6CHc.js → TurnsTab-ciI6ydvj.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Coul6CHc.js.map → TurnsTab-ciI6ydvj.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-hryG5oyY.js → WikiTab-DcYHc734.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-hryG5oyY.js.map → WikiTab-DcYHc734.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-Dxo95lRI.js → button-DF8lx4bt.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-Dxo95lRI.js.map → button-DF8lx4bt.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-FXwhLF_u.js → card-DIoDheSZ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-FXwhLF_u.js.map → card-DIoDheSZ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-M-GeRM8U.js → generateCategoricalChart-BFRzeYX3.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-M-GeRM8U.js.map → generateCategoricalChart-BFRzeYX3.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-BIZZXvoy.js → index-DSZON0Yi.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-BIZZXvoy.js.map → index-DSZON0Yi.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{switch-C-d4tkN1.js → switch-l4qo22mU.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-C-d4tkN1.js.map → switch-l4qo22mU.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-Bfmucdcl.js → toggle-DPn6td1_.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-Bfmucdcl.js.map → toggle-DPn6td1_.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-CxK_wdmk.js → useSSE-BLmolqTJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-CxK_wdmk.js.map → useSSE-BLmolqTJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/api/vector-cortex.ts +9 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +82 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +25 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +35 -0
- package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
- package/extensions/dashboard-server/routes-vector-cortex-query.ts +54 -0
- package/extensions/dashboard-server/routes-vector-cortex-topology.ts +166 -5
- package/extensions/dashboard-server/routes-vector-cortex.ts +1 -0
- package/extensions/dashboard-server/routes.ts +1 -0
- package/extensions/dashboard-server/server.ts +2 -0
- package/package.json +1 -1
- package/src/config/vector-cortex.ts +23 -0
- package/src/config.ts +2 -0
- package/src/tieredRouter.ts +9 -0
- package/src/vector-cortex/migrations/router-generation-v2.ts +317 -0
- package/src/vector-cortex/topology/build.ts +259 -0
- package/src/vector-cortex/topology/index.ts +143 -0
- package/src/vector-cortex/topology/query.ts +485 -0
- package/src/vector-cortex/topology/types.ts +156 -0
- package/src/vector-cortex/vc3b-support.ts +142 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js.map +0 -1
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/topology/index.ts — topology barrel + stable graph digest (VC3B).
|
|
3
|
+
*
|
|
4
|
+
* Typical role: a thin barrel + a few derived helpers. The build algorithm lives
|
|
5
|
+
* in build.ts; this file:
|
|
6
|
+
* - re-exports the TopologyV1 / EdgeV1 contracts;
|
|
7
|
+
* - computes ONE deterministic graph digest over the canonical, order-
|
|
8
|
+
* independent serialization of nodes + edges (task 4);
|
|
9
|
+
* - compose `buildTopologyGraph` = build + digest + emit on one seam.
|
|
10
|
+
*
|
|
11
|
+
* Ordinal-order independence: the digest is computed over nodes sorted by id
|
|
12
|
+
* bytes and edges sorted by (source, target, head, direction) then score, so
|
|
13
|
+
* any permutation of the build input yields an identical digest (verified by the
|
|
14
|
+
* acceptance consumer across 1,000 distinct input orderings — Q02).
|
|
15
|
+
*
|
|
16
|
+
* Pure deterministic helpers: no I/O, no console, no network (PREVENT-PI-004),
|
|
17
|
+
* no `any` (PREVENT-011).
|
|
18
|
+
*/
|
|
19
|
+
import { createHash } from "node:crypto";
|
|
20
|
+
import { buildTopology } from "./build.js";
|
|
21
|
+
import {
|
|
22
|
+
type TopologyBuildResult,
|
|
23
|
+
type TopologyEdgeV1,
|
|
24
|
+
type TopologyInput,
|
|
25
|
+
type TopologyNodeV1,
|
|
26
|
+
type TopologyV1,
|
|
27
|
+
} from "./types.js";
|
|
28
|
+
|
|
29
|
+
export type {
|
|
30
|
+
TopologyBuildResult,
|
|
31
|
+
TopologyEdgeV1,
|
|
32
|
+
TopologyInput,
|
|
33
|
+
TopologyNodeV1,
|
|
34
|
+
TopologyV1,
|
|
35
|
+
TopologyCandidate,
|
|
36
|
+
TopologyRejection,
|
|
37
|
+
TopologyRejectCode,
|
|
38
|
+
} from "./types.js";
|
|
39
|
+
export { TOP_IDS, TOP_NAMED_IDS, TOP_K } from "./types.js";
|
|
40
|
+
export { buildTopology } from "./build.js";
|
|
41
|
+
|
|
42
|
+
/** Emitter shape for the two VC3B topology events. */
|
|
43
|
+
export type TopologyEmit = (
|
|
44
|
+
event: "vector_cortex_topology_built" | "vector_cortex_topology_edge_rejected",
|
|
45
|
+
fields: Record<string, unknown>,
|
|
46
|
+
) => void;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Deterministic topological-construction seam (task 5). Runs the build, fills
|
|
50
|
+
* the stable generationDigest, and optionally emits:
|
|
51
|
+
* - `vector_cortex_topology_built` — once, when a graph is produced;
|
|
52
|
+
* - `vector_cortex_topology_edge_rejected`— once per rejected edge with code.
|
|
53
|
+
* Emission is best-effort and non-fatal (never breaks the agent loop).
|
|
54
|
+
*/
|
|
55
|
+
export function buildTopologyGraph(
|
|
56
|
+
input: TopologyInput,
|
|
57
|
+
emit?: TopologyEmit,
|
|
58
|
+
): TopologyBuildResult {
|
|
59
|
+
const result = buildTopology(input);
|
|
60
|
+
if (!result.ok) return result;
|
|
61
|
+
const topology: TopologyV1 = {
|
|
62
|
+
...result.topology,
|
|
63
|
+
generationDigest: graphDigest(result.topology),
|
|
64
|
+
};
|
|
65
|
+
const out: TopologyBuildResult = { ok: true, topology, rejected: result.rejected };
|
|
66
|
+
if (emit) {
|
|
67
|
+
try {
|
|
68
|
+
emit("vector_cortex_topology_built", {
|
|
69
|
+
sessionId: topology.sessionId,
|
|
70
|
+
sourceHighWater: topology.sourceHighWater.toString(),
|
|
71
|
+
nodeCount: topology.nodeCount,
|
|
72
|
+
edgeCount: topology.edgeCount,
|
|
73
|
+
generationDigest: topology.generationDigest,
|
|
74
|
+
});
|
|
75
|
+
for (const r of result.rejected) {
|
|
76
|
+
emit("vector_cortex_topology_edge_rejected", {
|
|
77
|
+
sessionId: topology.sessionId,
|
|
78
|
+
source: r.source,
|
|
79
|
+
target: r.target,
|
|
80
|
+
head: r.head,
|
|
81
|
+
code: r.code,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
/* non-fatal observability — never break the agent loop */
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* ONE deterministic SHA-256 (hex) over the canonical serialization of the graph.
|
|
93
|
+
* Canonical form is a compact stream: `topology-v1|` + sorted nodes + `|` +
|
|
94
|
+
* sorted edges, where nodes are sorted by id bytes and edges by
|
|
95
|
+
* (source, target, head, direction) then score. Order-independence is achieved
|
|
96
|
+
* by that canonical sort, never by input order.
|
|
97
|
+
*
|
|
98
|
+
* Printable framing (no control bytes): FIELD_SEP (`|`) separates fields and
|
|
99
|
+
* RECORD_SEP (`~`) separates records. The project-controlled field values (ids,
|
|
100
|
+
* kinds, heads, directions, scores) contain neither `|` nor `~`, so the framing
|
|
101
|
+
* never collides, and keeping the file control-byte-free lets git treat this
|
|
102
|
+
* core sprint file as text (prevents a spurious "binary file" classification).
|
|
103
|
+
*/
|
|
104
|
+
// Printable, unambiguous framing delimiters for the canonical serialization.
|
|
105
|
+
const FIELD_SEP = "|";
|
|
106
|
+
const RECORD_SEP = "~";
|
|
107
|
+
|
|
108
|
+
export function graphDigest(topology: TopologyV1): string {
|
|
109
|
+
const nodes = canonicalNodes(topology.nodes);
|
|
110
|
+
const edges = canonicalEdges(topology.edges);
|
|
111
|
+
const hash = createHash("sha256");
|
|
112
|
+
hash.update(`topology-v1${FIELD_SEP}`);
|
|
113
|
+
hash.update(nodes);
|
|
114
|
+
hash.update(FIELD_SEP);
|
|
115
|
+
hash.update(edges);
|
|
116
|
+
return `sha256:${hash.digest("hex")}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function canonicalNodes(nodes: readonly TopologyNodeV1[]): Buffer {
|
|
120
|
+
const sorted = [...nodes]
|
|
121
|
+
.map((n) => `${n.id}${FIELD_SEP}${n.kind}`)
|
|
122
|
+
.sort(unsignedBytesCompare)
|
|
123
|
+
.join(RECORD_SEP);
|
|
124
|
+
return Buffer.from(sorted, "utf8");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function canonicalEdges(edges: readonly TopologyEdgeV1[]): Buffer {
|
|
128
|
+
const parts = edges
|
|
129
|
+
.map((e) => `${e.source}${FIELD_SEP}${e.target}${FIELD_SEP}${e.head}${FIELD_SEP}${e.direction}${FIELD_SEP}${e.score}`)
|
|
130
|
+
.sort(unsignedBytesCompare)
|
|
131
|
+
.join(RECORD_SEP);
|
|
132
|
+
return Buffer.from(parts, "utf8");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function unsignedBytesCompare(a: string, b: string): number {
|
|
136
|
+
const ba = Buffer.from(a, "utf8");
|
|
137
|
+
const bb = Buffer.from(b, "utf8");
|
|
138
|
+
const n = Math.min(ba.length, bb.length);
|
|
139
|
+
for (let i = 0; i < n; i++) {
|
|
140
|
+
if (ba[i] !== bb[i]) return ba[i] < bb[i] ? -1 : 1;
|
|
141
|
+
}
|
|
142
|
+
return ba.length < bb.length ? -1 : ba.length > bb.length ? 1 : 0;
|
|
143
|
+
}
|
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/topology/query.ts — topology query + router invalidation (VC3C).
|
|
3
|
+
*
|
|
4
|
+
* Owns `RouterKeyV2` (the structured router key) and `TopologyQueryV1` (the
|
|
5
|
+
* generation query/index surface that serves graphs and invalidates stale
|
|
6
|
+
* generations). This is the degradation seam of the sprint's failure triad:
|
|
7
|
+
*
|
|
8
|
+
* A = topology index at current generation (serve the active graph);
|
|
9
|
+
* B = fresh linear scan forced when a key targets a STALE generation;
|
|
10
|
+
* C = authority sequence scan forced when the derived store is unavailable.
|
|
11
|
+
*
|
|
12
|
+
* Key encoding is length-delimited with unsigned-byte (prefix) ordering and a
|
|
13
|
+
* FIXED five-field arity, so no field value can ambiguously prefix another and
|
|
14
|
+
* two sessions `a` / `aa` can never prefix-collide at the key level
|
|
15
|
+
* (M6-KEY-001). Invalidation matches the exact `(session,generation)` identity
|
|
16
|
+
* bytes — never a string-prefix — and a query whose key carries a generation
|
|
17
|
+
* older than the session's active one is rejected with `TOP_GENERATION_STALE`
|
|
18
|
+
* (M6-STALE-002), which forces mode B.
|
|
19
|
+
*
|
|
20
|
+
* Pure src/ logic over an injected `TopologyQueryHost` (the source of graphs +
|
|
21
|
+
* linear/authority scans) and an optional `TopologyQueryEmit`. Deterministic,
|
|
22
|
+
* pi-agnostic, no console, no network (PREVENT-PI-004), no `any`
|
|
23
|
+
* (PREVENT-011). The caller owns durability; every store write on the caller
|
|
24
|
+
* side is best-effort and never breaks the agent loop.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { createHash } from "node:crypto";
|
|
28
|
+
|
|
29
|
+
/** Query rejected: the key's generation is stale relative to the active one. */
|
|
30
|
+
export const TOP_GENERATION_STALE = "TOP_GENERATION_STALE";
|
|
31
|
+
/** Query rejected: a key could not be decoded (malformed length-delimited bytes). */
|
|
32
|
+
export const TOP_KEY_DECODE_FAILED = "TOP_KEY_DECODE_FAILED";
|
|
33
|
+
/** Query rejected: the derived store is unavailable and no authority scan is possible. */
|
|
34
|
+
export const TOP_AUTHORITY_UNAVAILABLE = "TOP_AUTHORITY_UNAVAILABLE";
|
|
35
|
+
|
|
36
|
+
/** Frozen router key schema version carried in every encoded key. */
|
|
37
|
+
export const ROUTER_KEY_VERSION = 2;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The structured router key. Every field is explicit — there is no string
|
|
41
|
+
* concatenation of `session`+`generation` anywhere; identity and ordering are
|
|
42
|
+
* derived from the length-delimited encoding below, so `a` and `aa` can never
|
|
43
|
+
* collide (M6-KEY-001).
|
|
44
|
+
*/
|
|
45
|
+
export interface RouterKeyV2 {
|
|
46
|
+
readonly session: string;
|
|
47
|
+
readonly sourceStart: bigint;
|
|
48
|
+
readonly sourceEnd: bigint;
|
|
49
|
+
readonly generation: bigint;
|
|
50
|
+
readonly algorithm: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Descriptive result of decoding a RouterKeyV2. */
|
|
54
|
+
export type RouterKeyDecode =
|
|
55
|
+
| { ok: true; key: RouterKeyV2 }
|
|
56
|
+
| { ok: false; code: "TOP_KEY_DECODE_FAILED" };
|
|
57
|
+
|
|
58
|
+
/** The three query result modes, mirroring the sprint failure triad. */
|
|
59
|
+
export type TopologyQueryMode = "A" | "B" | "C";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A served query result. `mode` records which triad arm produced it:
|
|
63
|
+
* A — served from the topology index at the current generation;
|
|
64
|
+
* B — a fresh linear scan, forced because the A key was stale;
|
|
65
|
+
* C — an authority sequence scan, forced because the derived store was
|
|
66
|
+
* unavailable.
|
|
67
|
+
* `matchedKey` is the EXACT encoded key that hit — the invariant is that a hit
|
|
68
|
+
* always matches every structured key field, never a prefix subset (the
|
|
69
|
+
* dashboard/consumers read only `matchedKey`, never a raw concatenation).
|
|
70
|
+
*/
|
|
71
|
+
export interface TopologyQueryResult {
|
|
72
|
+
readonly ok: true;
|
|
73
|
+
readonly mode: TopologyQueryMode;
|
|
74
|
+
readonly session: string;
|
|
75
|
+
readonly generation: bigint;
|
|
76
|
+
readonly algorithm: string;
|
|
77
|
+
/** EXACT router key that produced the hit (all five fields). */
|
|
78
|
+
readonly matchedKey: string;
|
|
79
|
+
/** Byte-level digest of the served graph/scan (the A graph digest). */
|
|
80
|
+
readonly digest: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A rejected query (stale key, undecodable key, or authority unavailable). */
|
|
84
|
+
export interface TopologyQueryReject {
|
|
85
|
+
readonly ok: false;
|
|
86
|
+
readonly code: string;
|
|
87
|
+
readonly mode: TopologyQueryMode | null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type TopologyQueryOutcome = TopologyQueryResult | TopologyQueryReject;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The data sources the query layer reads from. Capability-shaped: the index only
|
|
94
|
+
* asks for what it needs. `activeGeneration(session)` is the source of authority
|
|
95
|
+
* for staleness; `graphAt` is the mode-A derived store (the same graph that the
|
|
96
|
+
* CortexReader exposes at the active generation).
|
|
97
|
+
*/
|
|
98
|
+
export interface TopologyQueryHost {
|
|
99
|
+
/** The current (active) generation for a session, or undefined when absent. */
|
|
100
|
+
readonly activeGeneration: (session: string) => bigint | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* The topology graph at an exact (session, generation, algorithm) key, or
|
|
103
|
+
* undefined when that generation is not indexed. Mode-A source.
|
|
104
|
+
*/
|
|
105
|
+
readonly graphAt: (
|
|
106
|
+
session: string,
|
|
107
|
+
generation: bigint,
|
|
108
|
+
algorithm: string,
|
|
109
|
+
) => { readonly digest: string } | undefined;
|
|
110
|
+
/**
|
|
111
|
+
* Fresh linear scan over `[sourceStart, sourceEnd]`. Mode-B source, forced
|
|
112
|
+
* when a key targets a stale generation.
|
|
113
|
+
*/
|
|
114
|
+
readonly linearScan: (
|
|
115
|
+
session: string,
|
|
116
|
+
sourceStart: bigint,
|
|
117
|
+
sourceEnd: bigint,
|
|
118
|
+
) => { readonly digest: string };
|
|
119
|
+
/**
|
|
120
|
+
* Authority sequence scan over `[sourceStart, sourceEnd]`. Mode-C source,
|
|
121
|
+
* forced when the derived store is unavailable; returns undefined when the
|
|
122
|
+
* authority itself is unavailable (authority outage freezes the derived
|
|
123
|
+
* high-water).
|
|
124
|
+
*/
|
|
125
|
+
readonly authorityScan: (
|
|
126
|
+
session: string,
|
|
127
|
+
sourceStart: bigint,
|
|
128
|
+
sourceEnd: bigint,
|
|
129
|
+
) => { readonly digest: string } | undefined;
|
|
130
|
+
/** Whether the derived (mode-A) store is available at all. */
|
|
131
|
+
readonly derivedAvailable: (session: string) => boolean;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Optional structured-event emitter (same shape as the other VC seams). */
|
|
135
|
+
export type TopologyQueryEmit = (event: string, fields: Record<string, unknown>) => void;
|
|
136
|
+
|
|
137
|
+
/** The two VC3C events the query seam can emit. */
|
|
138
|
+
export type TopologyQueryEvent =
|
|
139
|
+
| "vector_cortex_router_generation_invalidated"
|
|
140
|
+
| "vector_cortex_topology_query_demoted";
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Reader/writer surface of the query index. This file owns the PURE logic; the
|
|
144
|
+
* production wiring (which host + emitter to use, and how invalidation is made
|
|
145
|
+
* durable) lives at the delegate seam in tieredRouter.ts so src/ stays
|
|
146
|
+
* pi-agnostic.
|
|
147
|
+
*/
|
|
148
|
+
export interface TopologyQueryV1 {
|
|
149
|
+
/** Serve a query; rejects with TOP_GENERATION_STALE when the key is stale. */
|
|
150
|
+
readonly query: (key: RouterKeyV2) => TopologyQueryOutcome;
|
|
151
|
+
/**
|
|
152
|
+
* Invalidate an EXACT (session,generation). Emits
|
|
153
|
+
* `vector_cortex_router_generation_invalidated`. Cross-session keys never
|
|
154
|
+
* evict each other (M6 enforcement: exact byte match).
|
|
155
|
+
*/
|
|
156
|
+
readonly invalidate: (session: string, generation: bigint) => void;
|
|
157
|
+
/** Number of distinct generations currently indexed (diagnostic). */
|
|
158
|
+
readonly generationalCount: (session: string) => number;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Create the query surface. `emit` is optional and flag-gated (the caller passes
|
|
163
|
+
* a reporter produced by the same pattern VC3A/VC3B use — zero emissions when
|
|
164
|
+
* `MEGACOMPACT_VC3C=0`, byte-identical to the predecessor). Pure in-memory index
|
|
165
|
+
* over the injected host; durability of invalidation is the caller's job.
|
|
166
|
+
*/
|
|
167
|
+
export function createTopologyQuery(
|
|
168
|
+
host: TopologyQueryHost,
|
|
169
|
+
emit?: TopologyQueryEmit,
|
|
170
|
+
): TopologyQueryV1 {
|
|
171
|
+
// session -> Set of indexed generation strings (exact, byte-identity).
|
|
172
|
+
const generations = new Map<string, Set<string>>();
|
|
173
|
+
|
|
174
|
+
const fire = (event: TopologyQueryEvent, fields: Record<string, unknown>): void => {
|
|
175
|
+
if (!emit) return;
|
|
176
|
+
try {
|
|
177
|
+
emit(event, fields);
|
|
178
|
+
} catch {
|
|
179
|
+
/* non-fatal observability — never break the agent loop */
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const query = (key: RouterKeyV2): TopologyQueryOutcome => {
|
|
184
|
+
const active = host.activeGeneration(key.session);
|
|
185
|
+
// Mode C: derived store unavailable -> authority sequence scan.
|
|
186
|
+
if (!host.derivedAvailable(key.session)) {
|
|
187
|
+
const cDigest = host.authorityScan(key.session, key.sourceStart, key.sourceEnd);
|
|
188
|
+
if (cDigest === undefined) {
|
|
189
|
+
return { ok: false, code: TOP_AUTHORITY_UNAVAILABLE, mode: "C" };
|
|
190
|
+
}
|
|
191
|
+
fire("vector_cortex_topology_query_demoted", {
|
|
192
|
+
session: key.session,
|
|
193
|
+
generation: key.generation.toString(),
|
|
194
|
+
code: "DERIVED_UNAVAILABLE",
|
|
195
|
+
toMode: "C",
|
|
196
|
+
});
|
|
197
|
+
return {
|
|
198
|
+
ok: true,
|
|
199
|
+
mode: "C",
|
|
200
|
+
session: key.session,
|
|
201
|
+
generation: key.generation,
|
|
202
|
+
algorithm: key.algorithm,
|
|
203
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
204
|
+
digest: cDigest.digest,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A key is stale when its generation is below the active one (or the active
|
|
209
|
+
// generation is unset and the requested generation is non-zero).
|
|
210
|
+
const stale =
|
|
211
|
+
active === undefined
|
|
212
|
+
? key.generation !== 0n
|
|
213
|
+
: key.generation < active;
|
|
214
|
+
|
|
215
|
+
if (stale) {
|
|
216
|
+
// Mode B: fresh linear scan forced by a stale A key.
|
|
217
|
+
const bDigest = host.linearScan(key.session, key.sourceStart, key.sourceEnd);
|
|
218
|
+
fire("vector_cortex_topology_query_demoted", {
|
|
219
|
+
session: key.session,
|
|
220
|
+
generation: key.generation.toString(),
|
|
221
|
+
activeGeneration: active === undefined ? null : active.toString(),
|
|
222
|
+
code: "STALE_GENERATION",
|
|
223
|
+
toMode: "B",
|
|
224
|
+
});
|
|
225
|
+
return {
|
|
226
|
+
ok: true,
|
|
227
|
+
mode: "B",
|
|
228
|
+
session: key.session,
|
|
229
|
+
generation: key.generation,
|
|
230
|
+
algorithm: key.algorithm,
|
|
231
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
232
|
+
digest: bDigest.digest,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Mode A: topology index at the current generation. If the derived store is
|
|
237
|
+
// up but has not indexed this exact (generation, algorithm) yet, that is a
|
|
238
|
+
// mode-A miss (no graph to serve) — force the fresh linear scan rather than
|
|
239
|
+
// fabricate a graph or report a false staleness (Q03: the index cannot be
|
|
240
|
+
// trusted to hold every current generation; B recomputes from source).
|
|
241
|
+
const graph = host.graphAt(key.session, key.generation, key.algorithm);
|
|
242
|
+
if (graph === undefined) {
|
|
243
|
+
const bDigest = host.linearScan(key.session, key.sourceStart, key.sourceEnd);
|
|
244
|
+
fire("vector_cortex_topology_query_demoted", {
|
|
245
|
+
session: key.session,
|
|
246
|
+
generation: key.generation.toString(),
|
|
247
|
+
code: "GENERATION_MISS",
|
|
248
|
+
toMode: "B",
|
|
249
|
+
});
|
|
250
|
+
return {
|
|
251
|
+
ok: true,
|
|
252
|
+
mode: "B",
|
|
253
|
+
session: key.session,
|
|
254
|
+
generation: key.generation,
|
|
255
|
+
algorithm: key.algorithm,
|
|
256
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
257
|
+
digest: bDigest.digest,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return {
|
|
261
|
+
ok: true,
|
|
262
|
+
mode: "A",
|
|
263
|
+
session: key.session,
|
|
264
|
+
generation: key.generation,
|
|
265
|
+
algorithm: key.algorithm,
|
|
266
|
+
matchedKey: encodeRouterKeyV2(key),
|
|
267
|
+
digest: graph.digest,
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const invalidate = (session: string, generation: bigint): void => {
|
|
272
|
+
const invalidationIdentity = invalidationKey(session, generation);
|
|
273
|
+
const set = generations.get(session);
|
|
274
|
+
if (set) set.delete(invalidationIdentity);
|
|
275
|
+
fire("vector_cortex_router_generation_invalidated", {
|
|
276
|
+
session,
|
|
277
|
+
generation: generation.toString(),
|
|
278
|
+
key: invalidationIdentity,
|
|
279
|
+
});
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const generationalCount = (session: string): number => {
|
|
283
|
+
return generations.get(session)?.size ?? 0;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
return { query, invalidate, generationalCount };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ---------------------------------------------------------------------------
|
|
290
|
+
// Length-delimited, unsigned-byte-ordered key encoding (M6-KEY-001)
|
|
291
|
+
// ---------------------------------------------------------------------------
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Encode one field: `u32BE(byteLength) || bytes`. Non-negative numbers are
|
|
295
|
+
* encoded as their unsigned big-endian byte form (leading zeros trimmed). This
|
|
296
|
+
* is the standard big-endian length-prefixed encoding: two fields compare in
|
|
297
|
+
* unsigned-byte order exactly as their values do, and no value is an ambiguous
|
|
298
|
+
* prefix of another.
|
|
299
|
+
*/
|
|
300
|
+
function encodeLengthDelimited(bytes: Uint8Array): Uint8Array {
|
|
301
|
+
const out = new Uint8Array(4 + bytes.length);
|
|
302
|
+
const view = new DataView(out.buffer);
|
|
303
|
+
view.setUint32(0, bytes.length, false);
|
|
304
|
+
out.set(bytes, 4);
|
|
305
|
+
return out;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Big-endian unsigned bytes of a non-negative bigint (length-prefixed below). */
|
|
309
|
+
function bigintBytes(n: bigint): Uint8Array {
|
|
310
|
+
let v = n;
|
|
311
|
+
const out: number[] = [];
|
|
312
|
+
if (v === 0n) return new Uint8Array([0]);
|
|
313
|
+
while (v > 0n) {
|
|
314
|
+
out.unshift(Number(v & 0xffn));
|
|
315
|
+
v >>= 8n;
|
|
316
|
+
}
|
|
317
|
+
return Uint8Array.from(out);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** UTF-8 bytes of a string. */
|
|
321
|
+
function strBytes(s: string): Uint8Array {
|
|
322
|
+
return new TextEncoder().encode(s);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function concat(parts: readonly Uint8Array[]): Uint8Array {
|
|
326
|
+
let len = 0;
|
|
327
|
+
for (const p of parts) len += p.length;
|
|
328
|
+
const out = new Uint8Array(len);
|
|
329
|
+
let off = 0;
|
|
330
|
+
for (const p of parts) {
|
|
331
|
+
out.set(p, off);
|
|
332
|
+
off += p.length;
|
|
333
|
+
}
|
|
334
|
+
return out;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Encode a RouterKeyV2 into its canonical, length-delimited, unsigned-byte-ordered
|
|
339
|
+
* byte form. Version-prefixed (1 byte) + five length-delimited fields in fixed
|
|
340
|
+
* order (session, sourceStart, sourceEnd, generation, algorithm). Fixed arity +
|
|
341
|
+
* length-delimiting means no key is a strict prefix of another and sessions `a`
|
|
342
|
+
* and `aa` never collide (M6-KEY-001).
|
|
343
|
+
*/
|
|
344
|
+
export function encodeRouterKeyV2(key: RouterKeyV2): string {
|
|
345
|
+
const version = Uint8Array.of(ROUTER_KEY_VERSION);
|
|
346
|
+
const session = encodeLengthDelimited(strBytes(key.session));
|
|
347
|
+
const sourceStart = encodeLengthDelimited(bigintBytes(key.sourceStart));
|
|
348
|
+
const sourceEnd = encodeLengthDelimited(bigintBytes(key.sourceEnd));
|
|
349
|
+
const generation = encodeLengthDelimited(bigintBytes(key.generation));
|
|
350
|
+
const algorithm = encodeLengthDelimited(strBytes(key.algorithm));
|
|
351
|
+
return "rk2:" + Buffer.from(concat([version, session, sourceStart, sourceEnd, generation, algorithm])).toString("hex");
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Decode a RouterKeyV2 from its canonical byte form. Returns
|
|
356
|
+
* TOP_KEY_DECODE_FAILED on malformed bytes (bad version, truncated length, or
|
|
357
|
+
* trailing garbage). Never parses by string prefix.
|
|
358
|
+
*/
|
|
359
|
+
export function decodeRouterKeyV2(encoded: string): RouterKeyDecode {
|
|
360
|
+
if (!encoded.startsWith("rk2:")) return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
361
|
+
const hex = encoded.slice(4);
|
|
362
|
+
if (!/^[0-9a-f]*$/.test(hex) || hex.length % 2 !== 0) {
|
|
363
|
+
return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
364
|
+
}
|
|
365
|
+
const buf = Buffer.from(hex, "hex");
|
|
366
|
+
const bytes = new Uint8Array(buf);
|
|
367
|
+
const reader = new Reader(bytes);
|
|
368
|
+
const version = reader.u8();
|
|
369
|
+
if (version !== ROUTER_KEY_VERSION) return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
370
|
+
const sessionBytes = reader.lenPrefixed();
|
|
371
|
+
const ssBytes = reader.lenPrefixed();
|
|
372
|
+
const seBytes = reader.lenPrefixed();
|
|
373
|
+
const genBytes = reader.lenPrefixed();
|
|
374
|
+
const algoBytes = reader.lenPrefixed();
|
|
375
|
+
if (reader.remaining() !== 0) return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
376
|
+
if (sessionBytes === null || ssBytes === null || seBytes === null || genBytes === null || algoBytes === null) {
|
|
377
|
+
return { ok: false, code: "TOP_KEY_DECODE_FAILED" };
|
|
378
|
+
}
|
|
379
|
+
const session = new TextDecoder().decode(sessionBytes);
|
|
380
|
+
const sourceStart = bytesToBigint(ssBytes);
|
|
381
|
+
const sourceEnd = bytesToBigint(seBytes);
|
|
382
|
+
const generation = bytesToBigint(genBytes);
|
|
383
|
+
const algorithm = new TextDecoder().decode(algoBytes);
|
|
384
|
+
return { ok: true, key: { session, sourceStart, sourceEnd, generation, algorithm } };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Sequential length-delimited byte reader. */
|
|
388
|
+
class Reader {
|
|
389
|
+
private readonly bytes: Uint8Array;
|
|
390
|
+
private off = 0;
|
|
391
|
+
constructor(bytes: Uint8Array) {
|
|
392
|
+
this.bytes = bytes;
|
|
393
|
+
}
|
|
394
|
+
u8(): number {
|
|
395
|
+
const v = this.bytes[this.off];
|
|
396
|
+
this.off += 1;
|
|
397
|
+
return v;
|
|
398
|
+
}
|
|
399
|
+
lenPrefixed(): Uint8Array | null {
|
|
400
|
+
if (this.off + 4 > this.bytes.length) return null;
|
|
401
|
+
const view = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.off, 4);
|
|
402
|
+
const len = view.getUint32(0, false);
|
|
403
|
+
this.off += 4;
|
|
404
|
+
if (this.off + len > this.bytes.length) return null;
|
|
405
|
+
const out = this.bytes.slice(this.off, this.off + len);
|
|
406
|
+
this.off += len;
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
remaining(): number {
|
|
410
|
+
return this.bytes.length - this.off;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Non-negative bigint from its unsigned big-endian bytes. */
|
|
415
|
+
function bytesToBigint(bytes: Uint8Array): bigint {
|
|
416
|
+
let n = 0n;
|
|
417
|
+
for (const b of bytes) n = (n << 8n) | BigInt(b);
|
|
418
|
+
return n;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Canonical invalidation identity for an exact (session,generation) pair, using
|
|
423
|
+
* the SAME length-delimited scheme so two pairs can only match by exact byte
|
|
424
|
+
* equality (never by string prefix — `a` never matches `aa`). Returned as hex
|
|
425
|
+
* for use as a registry key.
|
|
426
|
+
*/
|
|
427
|
+
export function invalidationKey(session: string, generation: bigint): string {
|
|
428
|
+
const sessionF = encodeLengthDelimited(strBytes(session));
|
|
429
|
+
const genF = encodeLengthDelimited(bigintBytes(generation));
|
|
430
|
+
return "inv:" + Buffer.from(concat([sessionF, genF])).toString("hex");
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** One deterministic SHA-256 (hex) digest over the canonical key bytes. */
|
|
434
|
+
export function keyDigest(key: RouterKeyV2): string {
|
|
435
|
+
return `sha256:${createHash("sha256").update(Buffer.from(encodeRouterKeyV2(key).slice(4), "hex")).digest("hex")}`;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Shared invalidation-capability seam (narrow delegate, no routing rewrite).
|
|
440
|
+
* Records the EXACT (session,generation) identity and, when `enabled`, best-effort
|
|
441
|
+
* emits `vector_cortex_router_generation_invalidated`. Falsy `enabled` (flag OFF)
|
|
442
|
+
* is a strict no-op so the predecessor keying is byte-identical to pre-sprint.
|
|
443
|
+
* Exactness is enforced by `invalidationKey` (length-delimited, never a string
|
|
444
|
+
* prefix — `a` never matches `aa`), the same identity M6 validates.
|
|
445
|
+
*/
|
|
446
|
+
export function routerGenerationInvalidationSeam(
|
|
447
|
+
key: RouterKeyV2,
|
|
448
|
+
enabled: boolean,
|
|
449
|
+
emit?: TopologyQueryEmit,
|
|
450
|
+
): void {
|
|
451
|
+
if (!enabled) return;
|
|
452
|
+
const identity = invalidationKey(key.session, key.generation);
|
|
453
|
+
if (emit) {
|
|
454
|
+
try {
|
|
455
|
+
emit("vector_cortex_router_generation_invalidated", {
|
|
456
|
+
session: key.session,
|
|
457
|
+
generation: key.generation.toString(),
|
|
458
|
+
key: identity,
|
|
459
|
+
});
|
|
460
|
+
} catch {
|
|
461
|
+
/* non-fatal observability — never break the agent loop */
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Registered TOP query conformance ID range (TOP-021..030). */
|
|
467
|
+
export const TOP_QUERY_IDS = [
|
|
468
|
+
"TOP-021",
|
|
469
|
+
"TOP-022",
|
|
470
|
+
"TOP-023",
|
|
471
|
+
"TOP-024",
|
|
472
|
+
"TOP-025",
|
|
473
|
+
"TOP-026",
|
|
474
|
+
"TOP-027",
|
|
475
|
+
"TOP-028",
|
|
476
|
+
"TOP-029",
|
|
477
|
+
"TOP-030",
|
|
478
|
+
] as const;
|
|
479
|
+
|
|
480
|
+
/** Registered named TOP-query conformance IDs. */
|
|
481
|
+
export const TOP_QUERY_NAMED_IDS = [
|
|
482
|
+
"TOP-QUERY-003",
|
|
483
|
+
"M6-KEY-001",
|
|
484
|
+
"M6-STALE-002",
|
|
485
|
+
] as const;
|