pi-mega-compact 0.19.2 → 0.20.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/config/vector-cortex.js +24 -0
- package/dist/config.js +1 -1
- package/dist/extensions/dashboard-server/routes-cache.test/_helpers.js +2 -0
- package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +2 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-helpers.js +100 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-query.js +46 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-shards.js +48 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +2 -0
- package/dist/extensions/dashboard-server/routes.js +1 -1
- package/dist/extensions/dashboard-server/server.js +41 -16
- package/dist/extensions/dashboard-server/tailscale.js +32 -0
- package/dist/src/config/vector-cortex.js +24 -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/shards/exact.js +134 -0
- package/dist/src/vector-cortex/shards/manifest.js +237 -0
- package/dist/src/vector-cortex/shards/semantic.js +137 -0
- package/dist/src/vector-cortex/shards/types.js +51 -0
- package/dist/src/vector-cortex/topology/query.js +342 -0
- package/dist/vector-cortex/migrations/router-generation-v2.js +252 -0
- package/dist/vector-cortex/shards/exact.js +134 -0
- package/dist/vector-cortex/shards/manifest.js +237 -0
- package/dist/vector-cortex/shards/semantic.js +137 -0
- package/dist/vector-cortex/shards/types.js +51 -0
- package/dist/vector-cortex/topology/query.js +342 -0
- package/extensions/dashboard-client/dist/assets/{AreaChart-CSaJZXru.js → AreaChart-n4z5w9Ox.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-CSaJZXru.js.map → AreaChart-n4z5w9Ox.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-CQouNj1g.js → BarChart-BLGjWWBr.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-CQouNj1g.js.map → BarChart-BLGjWWBr.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-m_eiCYoL.js → CacheTab-CAnrKKVK.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-m_eiCYoL.js.map → CacheTab-CAnrKKVK.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-DRyOwjYN.js → EventsTab-D-GL7Ngq.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-DRyOwjYN.js.map → EventsTab-D-GL7Ngq.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-Dj9k40TJ.js → HealthTab-C4x5qQ8s.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-Dj9k40TJ.js.map → HealthTab-C4x5qQ8s.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-Bk9mhYfD.js → MaintenanceTab-BSPs0eq5.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-Bk9mhYfD.js.map → MaintenanceTab-BSPs0eq5.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-C7ve49VG.js → MemoryMapTab-BWEUtTSu.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-C7ve49VG.js.map → MemoryMapTab-BWEUtTSu.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-DzjhqZA9.js → MetricsTab-Dhm1c3Cx.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-DzjhqZA9.js.map → MetricsTab-Dhm1c3Cx.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-CFQdfIm_.js → OverviewTab-Cm6cySiQ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-CFQdfIm_.js.map → OverviewTab-Cm6cySiQ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-DyjwEFUr.js → ReposTab-DxLN1N4K.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-DyjwEFUr.js.map → ReposTab-DxLN1N4K.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-njLIy7ab.js → SessionsTab-CZuI4ISW.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-njLIy7ab.js.map → SessionsTab-CZuI4ISW.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-UHIs8-Lh.js → SetupTab-C_0VOomm.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-UHIs8-Lh.js.map → SetupTab-C_0VOomm.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-inWHjCxg.js → TimeSavedCard-BhbaRLmi.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-inWHjCxg.js.map → TimeSavedCard-BhbaRLmi.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-CXJYugBu.js → TurnsTab-CXJmbjje.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-CXJYugBu.js.map → TurnsTab-CXJmbjje.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-BGe9hu3r.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-BGe9hu3r.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-BJ4A2oi9.js → WikiTab-C5uvMCzp.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-BJ4A2oi9.js.map → WikiTab-C5uvMCzp.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-BWl-moyn.js → button-Cl_fxJcg.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-BWl-moyn.js.map → button-Cl_fxJcg.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-C58RVPOk.js → card-BH8Le5YM.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-C58RVPOk.js.map → card-BH8Le5YM.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-vgeOQU3L.js → generateCategoricalChart-sZ6ZNtBi.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-vgeOQU3L.js.map → generateCategoricalChart-sZ6ZNtBi.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-lVrWQjpu.js → index-CX9zv8cJ.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-lVrWQjpu.js.map → index-CX9zv8cJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{switch-CSeplFQT.js → switch-CXJhcjhM.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-CSeplFQT.js.map → switch-CXJhcjhM.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-CQRxgVRi.js → toggle-CoLtIc6y.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-CQRxgVRi.js.map → toggle-CoLtIc6y.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-BUz6IOI-.js → useSSE-fSXho606.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-BUz6IOI-.js.map → useSSE-fSXho606.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/api/vector-cortex.ts +18 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +62 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +25 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +41 -0
- package/extensions/dashboard-server/routes-cache.test/_helpers.ts +2 -0
- package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
- package/extensions/dashboard-server/routes-vector-cortex-helpers.ts +110 -0
- package/extensions/dashboard-server/routes-vector-cortex-query.ts +54 -0
- package/extensions/dashboard-server/routes-vector-cortex-shards.ts +57 -0
- package/extensions/dashboard-server/routes-vector-cortex.ts +2 -0
- package/extensions/dashboard-server/routes.ts +2 -0
- package/extensions/dashboard-server/server.ts +46 -17
- package/extensions/dashboard-server/tailscale.ts +32 -0
- package/package.json +1 -1
- package/src/config/vector-cortex.ts +26 -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/shards/exact.ts +141 -0
- package/src/vector-cortex/shards/manifest.ts +276 -0
- package/src/vector-cortex/shards/semantic.ts +156 -0
- package/src/vector-cortex/shards/types.ts +212 -0
- package/src/vector-cortex/topology/query.ts +485 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DkS0Azx9.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DkS0Azx9.js.map +0 -1
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/migrations/router-generation-v2.ts — M6 copy/validate/switch
|
|
3
|
+
* migration (VC3C).
|
|
4
|
+
*
|
|
5
|
+
* M6 versions the tiered router's query set: the predecessor routed on ad-hoc
|
|
6
|
+
* per-session keys (no generation identity); VC3C routes on the structured,
|
|
7
|
+
* length-delimited `RouterKeyV2`. M6 copies the old per-session query set into
|
|
8
|
+
* v2 `RouterGenV2Row`s BESIDE the legacy state, validates, and only then
|
|
9
|
+
* atomically switches the active version pointer — the same copy/validate/
|
|
10
|
+
* switch + resume contract as M4.
|
|
11
|
+
*
|
|
12
|
+
* - copy: resumable per (session, old key) — an interrupted run resumes
|
|
13
|
+
* without duplicate rows or active-pointer drift.
|
|
14
|
+
* - validate: every old query key has exactly one v2 row, every row's key is
|
|
15
|
+
* canonical + re-hashes, and — the VC3C-specific invariant — the new query
|
|
16
|
+
* set is partition-preserving: a row derived from an old key of session S
|
|
17
|
+
* MUST carry session S. A row whose session differs is rejected as
|
|
18
|
+
* cross-session eviction (`M6_CROSS_SESSION_EVICTION`): one session may
|
|
19
|
+
* never lose or steal another session's query identity.
|
|
20
|
+
* - switch: atomically flips the ACTIVE VERSION pointer from 1 (legacy) to 2
|
|
21
|
+
* (router-generation-v2) only after a verified copy; interruption keeps the
|
|
22
|
+
* old active pointer.
|
|
23
|
+
*
|
|
24
|
+
* Pure logic over an injected M6Host (deterministic + testable; no console).
|
|
25
|
+
* PREVENT-002/011/PI-004 honored.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { createHash } from "node:crypto";
|
|
29
|
+
import {
|
|
30
|
+
encodeRouterKeyV2,
|
|
31
|
+
decodeRouterKeyV2,
|
|
32
|
+
type RouterKeyV2,
|
|
33
|
+
} from "../topology/query.js";
|
|
34
|
+
|
|
35
|
+
/** The active-version value the v2 router generation pointer is switched to. */
|
|
36
|
+
export const ROUTER_GEN_V2_VERSION = 2;
|
|
37
|
+
/** The legacy (predecessor) active-version value before any verified switch. */
|
|
38
|
+
export const ROUTER_GEN_LEGACY_VERSION = 1;
|
|
39
|
+
|
|
40
|
+
/** M6 failure codes. */
|
|
41
|
+
export const M6_FAIL = {
|
|
42
|
+
VERSION_MISMATCH: "M6_VERSION_MISMATCH",
|
|
43
|
+
COPY_PARTIAL: "M6_COPY_PARTIAL",
|
|
44
|
+
COUNT_MISMATCH: "M6_COUNT_MISMATCH",
|
|
45
|
+
DIGEST_MISMATCH: "M6_DIGEST_MISMATCH",
|
|
46
|
+
BAD_OLD_KEY: "M6_BAD_OLD_KEY",
|
|
47
|
+
CROSS_SESSION_EVICTION: "M6_CROSS_SESSION_EVICTION",
|
|
48
|
+
SWITCH_PRECONDITION: "M6_SWITCH_PRECONDITION",
|
|
49
|
+
} as const;
|
|
50
|
+
export type M6MigrationCode = (typeof M6_FAIL)[keyof typeof M6_FAIL];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A v2 router-generation row derived from one predecessor query key. Identity is
|
|
54
|
+
* exact: `session` + the canonical length-delimited `key` (all five structured
|
|
55
|
+
* fields). `digest` is the authoritative sha256 over the canonical key bytes.
|
|
56
|
+
*/
|
|
57
|
+
export interface RouterGenV2Row {
|
|
58
|
+
readonly session: string;
|
|
59
|
+
readonly generation: bigint;
|
|
60
|
+
readonly sourceStart: bigint;
|
|
61
|
+
readonly sourceEnd: bigint;
|
|
62
|
+
readonly algorithm: string;
|
|
63
|
+
/** Canonical `rk2:` hex encoding of the full structured key. */
|
|
64
|
+
readonly key: string;
|
|
65
|
+
/** Authoritative sha256 hex over the canonical key bytes. */
|
|
66
|
+
readonly digest: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The host the migration reads from / writes to (capability-shaped). */
|
|
70
|
+
export interface M6Host {
|
|
71
|
+
/** Every session whose query set is being migrated. */
|
|
72
|
+
readonly sessions: () => readonly string[];
|
|
73
|
+
/** The predecessor (legacy) query keys for one session. */
|
|
74
|
+
readonly oldKeysOf: (session: string) => readonly string[];
|
|
75
|
+
/**
|
|
76
|
+
* Decode one predecessor query key into the structured RouterKeyV2 it maps to,
|
|
77
|
+
* or a `M6_BAD_OLD_KEY` rejection when the legacy key cannot be interpreted.
|
|
78
|
+
*/
|
|
79
|
+
readonly parseOldKey: (
|
|
80
|
+
session: string,
|
|
81
|
+
oldKey: string,
|
|
82
|
+
) => { ok: true; key: RouterKeyV2 } | { ok: false };
|
|
83
|
+
/** Persisted v2 rows already written (for resume + active pointer). */
|
|
84
|
+
readonly existingV2: () => readonly RouterGenV2Row[];
|
|
85
|
+
/** Idempotent write of v2 rows (never mutates legacy rows). */
|
|
86
|
+
readonly putV2: (rows: readonly RouterGenV2Row[]) => void;
|
|
87
|
+
/** The currently ACTIVE version (1 = legacy until a verified switch). */
|
|
88
|
+
readonly activeVersion: () => number;
|
|
89
|
+
/** Atomically switch the active version pointer to 2. */
|
|
90
|
+
readonly switchToV2: () => void;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** An M6 migration validation result. */
|
|
94
|
+
export interface M6ValidateResult {
|
|
95
|
+
readonly ok: boolean;
|
|
96
|
+
readonly codes: readonly M6MigrationCode[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The identity of a query-set slot, for copy/resume dedup. */
|
|
100
|
+
interface SlotId {
|
|
101
|
+
readonly session: string;
|
|
102
|
+
readonly oldKey: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Derive one v2 row from an old key (authoritative bytes). */
|
|
106
|
+
export function deriveRouterGenRow(
|
|
107
|
+
host: M6Host,
|
|
108
|
+
session: string,
|
|
109
|
+
oldKey: string,
|
|
110
|
+
): { ok: true; row: RouterGenV2Row } | { ok: false; session: string; oldKey: string } {
|
|
111
|
+
const parsed = host.parseOldKey(session, oldKey);
|
|
112
|
+
if (!parsed.ok) return { ok: false, session, oldKey };
|
|
113
|
+
const key = parsed.key;
|
|
114
|
+
// Cross-session guard at COPY time: the structured key's session must equal
|
|
115
|
+
// the slot session. A key that claims a different session is never copied.
|
|
116
|
+
if (key.session !== session) return { ok: false, session, oldKey };
|
|
117
|
+
const encoded = encodeRouterKeyV2(key);
|
|
118
|
+
return {
|
|
119
|
+
ok: true,
|
|
120
|
+
row: {
|
|
121
|
+
session,
|
|
122
|
+
generation: key.generation,
|
|
123
|
+
sourceStart: key.sourceStart,
|
|
124
|
+
sourceEnd: key.sourceEnd,
|
|
125
|
+
algorithm: key.algorithm,
|
|
126
|
+
key: encoded,
|
|
127
|
+
digest: sha256Hex(Buffer.from(encoded.slice(4), "hex")),
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function sha256Hex(bytes: Buffer): string {
|
|
133
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Copy the full old query set into v2 rows (resumable): rows already persisted
|
|
138
|
+
* with a matching canonical key + digest are left untouched (no duplicates);
|
|
139
|
+
* absent or corrupt (wrong session / wrong key / stale digest / bad old key)
|
|
140
|
+
* slots are (re)written from the authoritative derivation. Returns the delta
|
|
141
|
+
* written this call. The active pointer stays at the legacy version until a
|
|
142
|
+
* verified switch.
|
|
143
|
+
*/
|
|
144
|
+
export function m6Copy(host: M6Host): { readonly written: RouterGenV2Row[]; readonly all: RouterGenV2Row[] } {
|
|
145
|
+
const existing = new Map(host.existingV2().map((r) => [`${r.session}::${r.key}`, r]));
|
|
146
|
+
const wanted: RouterGenV2Row[] = [];
|
|
147
|
+
for (const session of host.sessions()) {
|
|
148
|
+
for (const oldKey of host.oldKeysOf(session)) {
|
|
149
|
+
const derived = deriveRouterGenRow(host, session, oldKey);
|
|
150
|
+
if (!derived.ok) continue; // bad old key surfaced by verify, not lost silently
|
|
151
|
+
const stored = existing.get(`${session}::${derived.row.key}`);
|
|
152
|
+
if (stored !== undefined && rowMatches(stored, derived.row)) continue; // healthy
|
|
153
|
+
wanted.push(derived.row);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (wanted.length > 0) host.putV2(wanted);
|
|
157
|
+
return { written: wanted, all: [...host.existingV2()] };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** True when a persisted row already matches the authoritative derivation. */
|
|
161
|
+
function rowMatches(stored: RouterGenV2Row, fresh: RouterGenV2Row): boolean {
|
|
162
|
+
return (
|
|
163
|
+
stored.session === fresh.session &&
|
|
164
|
+
stored.generation === fresh.generation &&
|
|
165
|
+
stored.sourceStart === fresh.sourceStart &&
|
|
166
|
+
stored.sourceEnd === fresh.sourceEnd &&
|
|
167
|
+
stored.algorithm === fresh.algorithm &&
|
|
168
|
+
stored.key === fresh.key &&
|
|
169
|
+
stored.digest === fresh.digest
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Verify the migrated query set: every old query key across every session has a
|
|
175
|
+
* v2 row (exact-once), every row's key is canonical + re-hashes, and the new set
|
|
176
|
+
* is partition-preserving — every row's session is exactly the session of the
|
|
177
|
+
* old key it was derived from (rejects cross-session eviction). Never mutates.
|
|
178
|
+
*
|
|
179
|
+
* Comparison of old/new query sets is exact-by-identity (structured key bytes),
|
|
180
|
+
* never by string prefix.
|
|
181
|
+
*/
|
|
182
|
+
export function m6Verify(host: M6Host): M6ValidateResult {
|
|
183
|
+
const codes: M6MigrationCode[] = [];
|
|
184
|
+
const stored = host.existingV2();
|
|
185
|
+
|
|
186
|
+
// Expected slot inventory: (session, canonical key) for every derivable old key.
|
|
187
|
+
const expectedSlots: SlotId[] = [];
|
|
188
|
+
const badOldKeys: SlotId[] = [];
|
|
189
|
+
for (const session of host.sessions()) {
|
|
190
|
+
for (const oldKey of host.oldKeysOf(session)) {
|
|
191
|
+
const parsed = host.parseOldKey(session, oldKey);
|
|
192
|
+
if (!parsed.ok) {
|
|
193
|
+
badOldKeys.push({ session, oldKey });
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (parsed.key.session !== session) {
|
|
197
|
+
// A structured key that claims a different session is a cross-session
|
|
198
|
+
// eviction attempt — never migrated, never evicted.
|
|
199
|
+
codes.push(M6_FAIL.CROSS_SESSION_EVICTION);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
expectedSlots.push({ session, oldKey });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (badOldKeys.length > 0) codes.push(M6_FAIL.BAD_OLD_KEY);
|
|
206
|
+
|
|
207
|
+
// Map stored rows by their (session,key) identity.
|
|
208
|
+
const byIdentity = new Map<string, RouterGenV2Row>();
|
|
209
|
+
const duplicateCheck = new Map<string, number>();
|
|
210
|
+
for (const r of stored) {
|
|
211
|
+
const ident = `${r.session}::${r.key}`;
|
|
212
|
+
byIdentity.set(ident, r);
|
|
213
|
+
duplicateCheck.set(ident, (duplicateCheck.get(ident) ?? 0) + 1);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Each expected slot must be present exactly once, with a healthy row.
|
|
217
|
+
for (const slot of expectedSlots) {
|
|
218
|
+
const parsed = host.parseOldKey(slot.session, slot.oldKey);
|
|
219
|
+
if (!parsed.ok) continue;
|
|
220
|
+
const expectedKey = encodeRouterKeyV2(parsed.key);
|
|
221
|
+
const ident = `${slot.session}::${expectedKey}`;
|
|
222
|
+
const count = duplicateCheck.get(ident) ?? 0;
|
|
223
|
+
if (count === 0) codes.push(M6_FAIL.COPY_PARTIAL);
|
|
224
|
+
if (count > 1) codes.push(M6_FAIL.COUNT_MISMATCH);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Row health: digest re-hashes + session partition-preserving.
|
|
228
|
+
for (const r of stored) {
|
|
229
|
+
if (!r.key.startsWith("rk2:")) codes.push(M6_FAIL.DIGEST_MISMATCH);
|
|
230
|
+
const expectedDigest = sha256Hex(Buffer.from(r.key.slice(4), "hex"));
|
|
231
|
+
if (r.digest !== expectedDigest) codes.push(M6_FAIL.DIGEST_MISMATCH);
|
|
232
|
+
// The row's own structured key must decode and agree with its declared fields.
|
|
233
|
+
const dec = decodeRow(r);
|
|
234
|
+
if (!dec) codes.push(M6_FAIL.DIGEST_MISMATCH);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// No stored row may be an orphan (no corresponding old key anywhere).
|
|
238
|
+
const expectedIdentities = new Set<string>();
|
|
239
|
+
for (const slot of expectedSlots) {
|
|
240
|
+
const parsed = host.parseOldKey(slot.session, slot.oldKey);
|
|
241
|
+
if (!parsed.ok) continue;
|
|
242
|
+
expectedIdentities.add(`${slot.session}::${encodeRouterKeyV2(parsed.key)}`);
|
|
243
|
+
}
|
|
244
|
+
for (const ident of byIdentity.keys()) {
|
|
245
|
+
if (!expectedIdentities.has(ident)) codes.push(M6_FAIL.COUNT_MISMATCH);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const ok = codes.length === 0;
|
|
249
|
+
return { ok, codes: dedupe(codes) };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Self-consistent decode of a stored row: its canonical key decodes to a
|
|
254
|
+
* RouterKeyV2 whose five fields equal the row's declared fields. A row whose key
|
|
255
|
+
* doesn't round-trip against its own declared session/generation/range/algorithm
|
|
256
|
+
* is corrupt (cannot be trusted as the query set's identity).
|
|
257
|
+
*/
|
|
258
|
+
function decodeRow(r: RouterGenV2Row): boolean {
|
|
259
|
+
if (!r.key.startsWith("rk2:")) return false;
|
|
260
|
+
const hex = r.key.slice(4);
|
|
261
|
+
if (!/^[0-9a-f]+$/.test(hex) || hex.length % 2 !== 0) return false;
|
|
262
|
+
const dec = decodeRouterKeyV2(r.key);
|
|
263
|
+
if (!dec.ok) return false;
|
|
264
|
+
return (
|
|
265
|
+
dec.key.session === r.session &&
|
|
266
|
+
dec.key.generation === r.generation &&
|
|
267
|
+
dec.key.sourceStart === r.sourceStart &&
|
|
268
|
+
dec.key.sourceEnd === r.sourceEnd &&
|
|
269
|
+
dec.key.algorithm === r.algorithm
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Switch the active version to 2. Only call after `m6Verify` reports ok; a
|
|
275
|
+
* crash/return before switch leaves the legacy pointer active (interruption
|
|
276
|
+
* keeps old authority, resumable idempotently).
|
|
277
|
+
*/
|
|
278
|
+
export function m6Switch(host: M6Host): void {
|
|
279
|
+
if (host.activeVersion() !== ROUTER_GEN_LEGACY_VERSION) return;
|
|
280
|
+
host.switchToV2();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Full M6 lifecycle: copy -> verify -> switch; returns the verify result. */
|
|
284
|
+
export function migrateRouterGenerationV2(host: M6Host): M6ValidateResult {
|
|
285
|
+
m6Copy(host);
|
|
286
|
+
const v = m6Verify(host);
|
|
287
|
+
if (v.ok) m6Switch(host);
|
|
288
|
+
return v;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Registered M6 conformance ID range (M6-001..012). */
|
|
292
|
+
export const M6_IDS = [
|
|
293
|
+
"M6-001",
|
|
294
|
+
"M6-002",
|
|
295
|
+
"M6-003",
|
|
296
|
+
"M6-004",
|
|
297
|
+
"M6-005",
|
|
298
|
+
"M6-006",
|
|
299
|
+
"M6-007",
|
|
300
|
+
"M6-008",
|
|
301
|
+
"M6-009",
|
|
302
|
+
"M6-010",
|
|
303
|
+
"M6-011",
|
|
304
|
+
"M6-012",
|
|
305
|
+
] as const;
|
|
306
|
+
|
|
307
|
+
/** Registered named M6 conformance IDs. */
|
|
308
|
+
export const M6_NAMED_IDS = [
|
|
309
|
+
"M6-KEY-001",
|
|
310
|
+
"M6-STALE-002",
|
|
311
|
+
] as const;
|
|
312
|
+
|
|
313
|
+
function dedupe(codes: readonly M6MigrationCode[]): M6MigrationCode[] {
|
|
314
|
+
const out: M6MigrationCode[] = [];
|
|
315
|
+
for (const c of codes) if (!out.includes(c)) out.push(c);
|
|
316
|
+
return out;
|
|
317
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vector-cortex/shards/exact.ts — exact tier partition (VC4A).
|
|
3
|
+
*
|
|
4
|
+
* Carves the session's PROTECTED spans — every tool call/result pair, every
|
|
5
|
+
* anchor, every invalid UTF-8 event — into `ExactShardV1` payloads of the
|
|
6
|
+
* ORIGINAL bytes, verbatim and never normalized (SHD-UTF8-002: invalid bytes
|
|
7
|
+
* are exact-only and preserved unchanged — the codec's strict-fatal classifier
|
|
8
|
+
* means an invalid event has no lossy text to re-encode; we copy `originalBytes`
|
|
9
|
+
* directly). Each protected span is ATOMIC: a tool call/result pair is one
|
|
10
|
+
* `ProtectedSpan`, so no exact shard EVER splits a pair, even when the pair
|
|
11
|
+
* straddles the target-size boundary (SHD-PAIR-001).
|
|
12
|
+
*
|
|
13
|
+
* The partition walks the protected spans in canonical stream order (ascending
|
|
14
|
+
* first-seq), accrues each span's bytes, and closes an exact shard the moment
|
|
15
|
+
* adding the next atomic span would exceed `targetSize`. A single span over the
|
|
16
|
+
* budget still occupies exactly one shard — the pair case never splits.
|
|
17
|
+
*
|
|
18
|
+
* Triad: A = used with the semantic tier; B = the same exact spans preserved
|
|
19
|
+
* alongside extractive-derived semantic content; C = exact anchors/current
|
|
20
|
+
* transcript ONLY (this partition alone, with zero semantic shards).
|
|
21
|
+
*
|
|
22
|
+
* Pure/deterministic: hashes + array walking only, no storage, no console, no
|
|
23
|
+
* network (PREVENT-PI-004 / PREVENT-011).
|
|
24
|
+
*/
|
|
25
|
+
import { createHash } from "node:crypto";
|
|
26
|
+
import type { EventV2 } from "../ledger/types.js";
|
|
27
|
+
import { cumulativeOffsets } from "./semantic.js";
|
|
28
|
+
import type { ExactPartitionInput, ExactPartitionResult, ExactShardCase, ExactShardV1, ProtectedSpan } from "./types.js";
|
|
29
|
+
|
|
30
|
+
/** Deterministic exact-shard digest over the verbatim original bytes. */
|
|
31
|
+
function sealExactDigest(sessionId: string, bytes: Uint8Array): string {
|
|
32
|
+
const h = createHash("sha256");
|
|
33
|
+
h.update(`exact-shard-v1|${sessionId}|`);
|
|
34
|
+
h.update(bytes);
|
|
35
|
+
return `sha256:${h.digest("hex")}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Combine per-span cases into one shard case (invalid dominates, then tool-pair). */
|
|
39
|
+
function combineCase(span: ProtectedSpan, acc: ExactShardCase): ExactShardCase {
|
|
40
|
+
if (span.case === "invalid-utf8" || acc === "invalid-utf8") return "invalid-utf8";
|
|
41
|
+
if (span.case === "tool-pair" || acc === "tool-pair") return "tool-pair";
|
|
42
|
+
return "anchor";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Concatenate an ordered group of events' original bytes, verbatim. */
|
|
46
|
+
function concatBytes(events: readonly EventV2[]): Uint8Array {
|
|
47
|
+
let len = 0;
|
|
48
|
+
for (const e of events) len += e.originalBytes.length;
|
|
49
|
+
const out = new Uint8Array(len);
|
|
50
|
+
let off = 0;
|
|
51
|
+
for (const e of events) {
|
|
52
|
+
out.set(e.originalBytes, off);
|
|
53
|
+
off += e.originalBytes.length;
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Partition the exact tier (task 3). Returns `ok:true` with a list of
|
|
60
|
+
* `ExactShardV1` whose (sorted) ranges are contiguous, non-overlapping and
|
|
61
|
+
* jointly cover every protected span exactly once, each preserving the original
|
|
62
|
+
* bytes verbatim. Defensive failures mirror the semantic tier: a bad target size
|
|
63
|
+
* rejects with `SHD_INVALID_TARGET_SIZE`; a protected span referencing an event
|
|
64
|
+
* outside the session's stream, or a mixed-session span, rejects with
|
|
65
|
+
* `SHD_CROSS_SESSION`.
|
|
66
|
+
*/
|
|
67
|
+
export function partitionExact(input: ExactPartitionInput): ExactPartitionResult {
|
|
68
|
+
const { sessionId, events, protectedSpans, targetSize } = input;
|
|
69
|
+
if (!Number.isFinite(targetSize) || targetSize <= 0) {
|
|
70
|
+
return { ok: false, code: "SHD_INVALID_TARGET_SIZE" };
|
|
71
|
+
}
|
|
72
|
+
const stream = new Set(events.map((e) => e.seq.toString()));
|
|
73
|
+
for (const span of protectedSpans) {
|
|
74
|
+
if (span.events.some((e) => e.sessionId !== sessionId || !stream.has(e.seq.toString()))) {
|
|
75
|
+
return { ok: false, code: "SHD_CROSS_SESSION" };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const offsets = cumulativeOffsets(events);
|
|
79
|
+
if (protectedSpans.length === 0) return { ok: true, shards: [] };
|
|
80
|
+
|
|
81
|
+
// Process protected spans in canonical stream order (ascending first-seq;
|
|
82
|
+
// equal first-seq preserves input order for determinism).
|
|
83
|
+
const ordered = [...protectedSpans].sort((a, b) => {
|
|
84
|
+
const as = a.events.length ? a.events[0].seq : 0n;
|
|
85
|
+
const bs = b.events.length ? b.events[0].seq : 0n;
|
|
86
|
+
return as < bs ? -1 : as > bs ? 1 : 0;
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const shards: ExactShardV1[] = [];
|
|
90
|
+
let batch: ProtectedSpan[] = [];
|
|
91
|
+
let batchBytes = 0;
|
|
92
|
+
let batchCase: ExactShardCase = "anchor";
|
|
93
|
+
|
|
94
|
+
const spanBytes = (s: ProtectedSpan): number =>
|
|
95
|
+
s.events.reduce((n, e) => n + e.originalBytes.length, 0);
|
|
96
|
+
|
|
97
|
+
const flush = (): void => {
|
|
98
|
+
if (batch.length === 0) return;
|
|
99
|
+
const first = batch[0];
|
|
100
|
+
const last = batch[batch.length - 1];
|
|
101
|
+
const firstEv = first.events[0];
|
|
102
|
+
const lastEv = last.events[last.events.length - 1];
|
|
103
|
+
const start = offsets.of(firstEv.seq);
|
|
104
|
+
const end = offsets.of(lastEv.seq);
|
|
105
|
+
const byteStart = start ? start.byteStart : 0;
|
|
106
|
+
const byteEnd = end ? end.byteEnd : byteStart;
|
|
107
|
+
const bytes = concatBytes(batch.flatMap((s) => s.events));
|
|
108
|
+
shards.push({
|
|
109
|
+
schema: "exact-shard-v1",
|
|
110
|
+
sessionId,
|
|
111
|
+
range: {
|
|
112
|
+
sessionId,
|
|
113
|
+
seqStart: firstEv.seq,
|
|
114
|
+
seqEnd: lastEv.seq,
|
|
115
|
+
byteStart,
|
|
116
|
+
byteEnd,
|
|
117
|
+
},
|
|
118
|
+
kind: "exact",
|
|
119
|
+
originalBytes: bytes,
|
|
120
|
+
digest: sealExactDigest(sessionId, bytes),
|
|
121
|
+
byteCount: batchBytes,
|
|
122
|
+
case: batchCase,
|
|
123
|
+
});
|
|
124
|
+
batch = [];
|
|
125
|
+
batchBytes = 0;
|
|
126
|
+
batchCase = "anchor";
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
for (const span of ordered) {
|
|
130
|
+
const sz = spanBytes(span);
|
|
131
|
+
// Close at a complete-span boundary when adding this atomic span would
|
|
132
|
+
// exceed the budget and we already hold at least one complete span.
|
|
133
|
+
if (batch.length > 0 && batchBytes + sz > targetSize) flush();
|
|
134
|
+
batch.push(span);
|
|
135
|
+
batchBytes += sz;
|
|
136
|
+
batchCase = combineCase(span, batchCase);
|
|
137
|
+
}
|
|
138
|
+
flush();
|
|
139
|
+
|
|
140
|
+
return { ok: true, shards };
|
|
141
|
+
}
|