@peer2peer/ext-ix 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +75 -2
- package/SPEC.md +695 -0
- package/dist/conflicts.d.ts +64 -0
- package/dist/conflicts.js +188 -0
- package/dist/extension.d.ts +31 -0
- package/dist/extension.js +552 -0
- package/dist/footprint-command.d.ts +57 -0
- package/dist/footprint-command.js +398 -0
- package/dist/footprint-compute.d.ts +106 -0
- package/dist/footprint-compute.js +559 -0
- package/dist/footprint.d.ts +162 -0
- package/dist/footprint.js +451 -0
- package/dist/hmac.d.ts +18 -0
- package/dist/hmac.js +42 -0
- package/dist/impact.d.ts +66 -0
- package/dist/impact.js +208 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +20 -0
- package/dist/ix.d.ts +20 -0
- package/dist/ix.js +76 -0
- package/dist/note.d.ts +40 -0
- package/dist/note.js +82 -0
- package/dist/overlap-command.d.ts +37 -0
- package/dist/overlap-command.js +81 -0
- package/dist/overlap.d.ts +83 -0
- package/dist/overlap.js +227 -0
- package/dist/repo.d.ts +36 -0
- package/dist/repo.js +116 -0
- package/dist/subgraph.d.ts +59 -0
- package/dist/subgraph.js +140 -0
- package/dist/text.d.ts +83 -0
- package/dist/text.js +187 -0
- package/dist/which.d.ts +1 -0
- package/dist/which.js +45 -0
- package/package.json +61 -4
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { type NameKey } from "./hmac.js";
|
|
2
|
+
import { type RepoRef } from "./repo.js";
|
|
3
|
+
import { type RiskLevel } from "./text.js";
|
|
4
|
+
export declare const FOOTPRINT_KIND: "x:ix.footprint";
|
|
5
|
+
/** Seconds a footprint stays valid at the receiver without a newer one (it is republished every 5 min while unchanged). */
|
|
6
|
+
export declare const FOOTPRINT_TTL = 600;
|
|
7
|
+
/** The body's cap: `{v, body}` must fit p2p's 16 KiB state value (EXT_STATE_MAX_BYTES). */
|
|
8
|
+
export declare const MAX_FOOTPRINT_BYTES: number;
|
|
9
|
+
export declare const FP_LIMITS: {
|
|
10
|
+
readonly actors: 9;
|
|
11
|
+
readonly files: 25;
|
|
12
|
+
readonly changed: 40;
|
|
13
|
+
readonly newSymbols: 10;
|
|
14
|
+
readonly ring1Files: 60;
|
|
15
|
+
readonly ring1Symbols: 60;
|
|
16
|
+
readonly ring2Files: 100;
|
|
17
|
+
readonly components: 25;
|
|
18
|
+
/** A symbol's qualified key, after `path#`. */
|
|
19
|
+
readonly qualifiedKey: 200;
|
|
20
|
+
readonly counts: 1000000;
|
|
21
|
+
};
|
|
22
|
+
export declare const FP_GRAPHS: readonly ["base", "worktree", "branch", "none"];
|
|
23
|
+
export type FpGraph = (typeof FP_GRAPHS)[number];
|
|
24
|
+
export declare const FP_RELS: readonly ["call", "use", "test", "import"];
|
|
25
|
+
export type FpRel = (typeof FP_RELS)[number];
|
|
26
|
+
export declare const FP_SYMBOL_RELS: readonly ["call", "use"];
|
|
27
|
+
export type FpSymbolRel = (typeof FP_SYMBOL_RELS)[number];
|
|
28
|
+
export declare const FP_NAMES: readonly ["plain", "hmac"];
|
|
29
|
+
export type FpNames = (typeof FP_NAMES)[number];
|
|
30
|
+
export interface FpComponent {
|
|
31
|
+
/** A region name, or its hmac token. */
|
|
32
|
+
n: string;
|
|
33
|
+
kind?: string;
|
|
34
|
+
count: number;
|
|
35
|
+
}
|
|
36
|
+
export interface FpActor {
|
|
37
|
+
/** "main", or the status agent id ("a1", …). */
|
|
38
|
+
a: string;
|
|
39
|
+
graph: FpGraph;
|
|
40
|
+
wt?: true;
|
|
41
|
+
branch?: string;
|
|
42
|
+
/** The merge base a worktree agent's diff was taken against. */
|
|
43
|
+
base?: string;
|
|
44
|
+
risk: RiskLevel;
|
|
45
|
+
stale?: true;
|
|
46
|
+
coarse?: true;
|
|
47
|
+
partial?: true;
|
|
48
|
+
changed: {
|
|
49
|
+
files: string[];
|
|
50
|
+
symbols: {
|
|
51
|
+
k: string;
|
|
52
|
+
kind: string;
|
|
53
|
+
}[];
|
|
54
|
+
new?: string[];
|
|
55
|
+
};
|
|
56
|
+
ring1: {
|
|
57
|
+
files: {
|
|
58
|
+
f: string;
|
|
59
|
+
rel: FpRel;
|
|
60
|
+
}[];
|
|
61
|
+
symbols: {
|
|
62
|
+
k: string;
|
|
63
|
+
rel: FpSymbolRel;
|
|
64
|
+
}[];
|
|
65
|
+
};
|
|
66
|
+
ring2?: {
|
|
67
|
+
files: string[];
|
|
68
|
+
};
|
|
69
|
+
components: FpComponent[];
|
|
70
|
+
}
|
|
71
|
+
export interface FootprintBody {
|
|
72
|
+
repo: RepoRef;
|
|
73
|
+
at: number;
|
|
74
|
+
seq: number;
|
|
75
|
+
names: FpNames;
|
|
76
|
+
keyId?: string;
|
|
77
|
+
actors: FpActor[];
|
|
78
|
+
merged: {
|
|
79
|
+
components: FpComponent[];
|
|
80
|
+
risk: RiskLevel;
|
|
81
|
+
counts: {
|
|
82
|
+
changedSymbols: number;
|
|
83
|
+
ring1Files: number;
|
|
84
|
+
ring2Files: number;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
truncated?: true;
|
|
88
|
+
}
|
|
89
|
+
/** A qualified key as the wire allows it: disallowed characters become "_" (deterministic), at most 200. */
|
|
90
|
+
export declare function wireQualifiedKey(qk: string): string | undefined;
|
|
91
|
+
/** `path#qualifiedKey`, or undefined when either part can't be sent. A path containing "#" has no symbol keys. */
|
|
92
|
+
export declare function symbolKey(path: string, qualifiedKey: string): string | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Receiving (strict, SPEC.md §8.5): every known field is checked against its
|
|
95
|
+
* type, pattern and cap, and one bad field makes the whole body invalid.
|
|
96
|
+
* Unknown fields are dropped (SPEC §1). Paths are repo-relative, symbol keys
|
|
97
|
+
* `path#qualifiedKey`, or `h:` tokens in hmac mode, never both.
|
|
98
|
+
*/
|
|
99
|
+
export declare function parseFootprintBody(v: unknown): FootprintBody;
|
|
100
|
+
/** One actor as measured locally, with plain names (footprint-compute.ts). */
|
|
101
|
+
export interface RawActor {
|
|
102
|
+
a: string;
|
|
103
|
+
graph: FpGraph;
|
|
104
|
+
wt?: boolean;
|
|
105
|
+
branch?: string;
|
|
106
|
+
base?: string;
|
|
107
|
+
risk: RiskLevel;
|
|
108
|
+
stale?: boolean;
|
|
109
|
+
coarse?: boolean;
|
|
110
|
+
partial?: boolean;
|
|
111
|
+
/** A finished agent (done or failed): trimmed before the running ones. */
|
|
112
|
+
done?: boolean;
|
|
113
|
+
files: string[];
|
|
114
|
+
symbols: {
|
|
115
|
+
k: string;
|
|
116
|
+
kind: string;
|
|
117
|
+
}[];
|
|
118
|
+
newSymbols?: string[];
|
|
119
|
+
ring1Files: {
|
|
120
|
+
f: string;
|
|
121
|
+
rel: FpRel;
|
|
122
|
+
}[];
|
|
123
|
+
ring1Symbols: {
|
|
124
|
+
k: string;
|
|
125
|
+
rel: FpSymbolRel;
|
|
126
|
+
}[];
|
|
127
|
+
ring2Files?: string[];
|
|
128
|
+
components: FpComponent[];
|
|
129
|
+
}
|
|
130
|
+
export declare const maxRisk: (rs: Iterable<RiskLevel>) => RiskLevel;
|
|
131
|
+
/** Components summed by (name, kind), largest first, then by name. */
|
|
132
|
+
export declare function mergeComponents(lists: FpComponent[][]): FpComponent[];
|
|
133
|
+
export interface AssembleInput {
|
|
134
|
+
repo: RepoRef;
|
|
135
|
+
at: number;
|
|
136
|
+
seq: number;
|
|
137
|
+
actors: RawActor[];
|
|
138
|
+
/** Set for hmac mode. */
|
|
139
|
+
key?: NameKey;
|
|
140
|
+
/** Test seam: the byte cap (default MAX_FOOTPRINT_BYTES). */
|
|
141
|
+
maxBytes?: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Builds the body: names (plain or hmac), per-actor caps, the merge (from the
|
|
145
|
+
* untrimmed actors), then trimming until it fits, in this order (SPEC §8.4):
|
|
146
|
+
* ring 2, ring-1 symbols, ring-1 files, finished agents, changed symbols,
|
|
147
|
+
* per-actor components, the merged components, running agents (last first).
|
|
148
|
+
* Each step takes from the longest list (ties: the later actor) a quarter of
|
|
149
|
+
* it from the end, so what is kept is the head of each sorted list, and the
|
|
150
|
+
* same input always gives the same body.
|
|
151
|
+
*/
|
|
152
|
+
export declare function assembleFootprint(i: AssembleInput): {
|
|
153
|
+
body: FootprintBody;
|
|
154
|
+
fallback: string;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* The line a state-aware client without ext-ix shows (≤200 characters). Area
|
|
158
|
+
* names only in plain mode.
|
|
159
|
+
*/
|
|
160
|
+
export declare function footprintFallback(b: Pick<FootprintBody, "names" | "merged" | "actors">): string;
|
|
161
|
+
/** What decides republishing: the body without its time and sequence number. */
|
|
162
|
+
export declare function footprintDigest(b: FootprintBody): string;
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// x:ix.footprint (SPEC.md §8): a session's code footprint as its local Ix
|
|
4
|
+
// sees it: per actor (the main loop and each subagent), the symbols it
|
|
5
|
+
// changed, the code one hop downstream of them (ring 1, optionally ring 2)
|
|
6
|
+
// and the architecture regions involved. It is a state kind (protocol §4.3
|
|
7
|
+
// "Extension state kinds"): published to the session's status audience,
|
|
8
|
+
// kept latest-wins by the receiver, never an event, never shown to a model.
|
|
9
|
+
//
|
|
10
|
+
// This file is the wire: the body's types, strict validation on receive, and
|
|
11
|
+
// assembling a body from what footprint-compute.ts measured: stable names,
|
|
12
|
+
// hashed names (hmac.ts), per-actor caps, the per-session merge and the
|
|
13
|
+
// deterministic trimming that keeps a body within 16 KiB.
|
|
14
|
+
import { createHash } from "node:crypto";
|
|
15
|
+
import { isSafeRelativePath } from "@peer2peer/core/v3";
|
|
16
|
+
import { hashFile, hashRegion, hashSymbol, HMAC_TOKEN_RE, KEY_ID_RE } from "./hmac.js";
|
|
17
|
+
import { parseRepo } from "./repo.js";
|
|
18
|
+
import { cps, fail, isObj, jsonBytes, LIMITS, line, list, oneOf, optInt, relPath, RISK_LEVELS } from "./text.js";
|
|
19
|
+
export const FOOTPRINT_KIND = "x:ix.footprint";
|
|
20
|
+
/** Seconds a footprint stays valid at the receiver without a newer one (it is republished every 5 min while unchanged). */
|
|
21
|
+
export const FOOTPRINT_TTL = 600;
|
|
22
|
+
/** The body's cap: `{v, body}` must fit p2p's 16 KiB state value (EXT_STATE_MAX_BYTES). */
|
|
23
|
+
export const MAX_FOOTPRINT_BYTES = 16 * 1024 - 64;
|
|
24
|
+
export const FP_LIMITS = {
|
|
25
|
+
actors: 9,
|
|
26
|
+
files: 25,
|
|
27
|
+
changed: 40,
|
|
28
|
+
newSymbols: 10,
|
|
29
|
+
ring1Files: 60,
|
|
30
|
+
ring1Symbols: 60,
|
|
31
|
+
ring2Files: 100,
|
|
32
|
+
components: 25,
|
|
33
|
+
/** A symbol's qualified key, after `path#`. */
|
|
34
|
+
qualifiedKey: 200,
|
|
35
|
+
counts: 1_000_000,
|
|
36
|
+
};
|
|
37
|
+
export const FP_GRAPHS = ["base", "worktree", "branch", "none"];
|
|
38
|
+
export const FP_RELS = ["call", "use", "test", "import"];
|
|
39
|
+
export const FP_SYMBOL_RELS = ["call", "use"];
|
|
40
|
+
export const FP_NAMES = ["plain", "hmac"];
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Names (§8.3): repo-relative `path`, `path#qualifiedKey`, region names.
|
|
43
|
+
const QK_RE = /^[A-Za-z_$][A-Za-z0-9_$.:#<>,]{0,199}$/;
|
|
44
|
+
const KIND_RE = /^[A-Za-z][A-Za-z0-9_-]{0,31}$/;
|
|
45
|
+
const AGENT_RE = /^(?:main|a[1-9][0-9]{0,3})$/;
|
|
46
|
+
const BRANCH_RE = /^[A-Za-z0-9._/+@-]{1,128}$/;
|
|
47
|
+
const BASE_RE = /^[0-9a-f]{7,64}$/;
|
|
48
|
+
/** A qualified key as the wire allows it: disallowed characters become "_" (deterministic), at most 200. */
|
|
49
|
+
export function wireQualifiedKey(qk) {
|
|
50
|
+
let s = qk.trim().replace(/[^A-Za-z0-9_$.:#<>,]/g, "_");
|
|
51
|
+
if (s === "")
|
|
52
|
+
return undefined;
|
|
53
|
+
if (!/^[A-Za-z_$]/.test(s))
|
|
54
|
+
s = `_${s}`;
|
|
55
|
+
s = s.slice(0, FP_LIMITS.qualifiedKey);
|
|
56
|
+
return QK_RE.test(s) ? s : undefined;
|
|
57
|
+
}
|
|
58
|
+
/** `path#qualifiedKey`, or undefined when either part can't be sent. A path containing "#" has no symbol keys. */
|
|
59
|
+
export function symbolKey(path, qualifiedKey) {
|
|
60
|
+
if (path.includes("#") || !isSafeRelativePath(path) || cps(path) > LIMITS.path)
|
|
61
|
+
return undefined;
|
|
62
|
+
const qk = wireQualifiedKey(qualifiedKey);
|
|
63
|
+
return qk ? `${path}#${qk}` : undefined;
|
|
64
|
+
}
|
|
65
|
+
function parseFile(v, names, what) {
|
|
66
|
+
if (names === "hmac") {
|
|
67
|
+
if (typeof v !== "string" || !HMAC_TOKEN_RE.test(v))
|
|
68
|
+
return fail(`${what} must be an h: token`);
|
|
69
|
+
return v;
|
|
70
|
+
}
|
|
71
|
+
return relPath(v, what);
|
|
72
|
+
}
|
|
73
|
+
function parseSymbolKey(v, names, what) {
|
|
74
|
+
if (typeof v !== "string")
|
|
75
|
+
return fail(`${what} must be a string`);
|
|
76
|
+
if (names === "hmac") {
|
|
77
|
+
if (!HMAC_TOKEN_RE.test(v))
|
|
78
|
+
return fail(`${what} must be an h: token`);
|
|
79
|
+
return v;
|
|
80
|
+
}
|
|
81
|
+
const i = v.indexOf("#");
|
|
82
|
+
if (i <= 0)
|
|
83
|
+
return fail(`${what} must be path#qualifiedKey`);
|
|
84
|
+
relPath(v.slice(0, i), `${what} (its path)`);
|
|
85
|
+
if (!QK_RE.test(v.slice(i + 1)))
|
|
86
|
+
return fail(`${what} has a qualified key the wire doesn't allow`);
|
|
87
|
+
return v;
|
|
88
|
+
}
|
|
89
|
+
function parseRegion(v, names, what) {
|
|
90
|
+
if (typeof v !== "string")
|
|
91
|
+
return fail(`${what} must be a string`);
|
|
92
|
+
if (names === "hmac") {
|
|
93
|
+
if (!HMAC_TOKEN_RE.test(v))
|
|
94
|
+
return fail(`${what} must be an h: token`);
|
|
95
|
+
return v;
|
|
96
|
+
}
|
|
97
|
+
if (v.trim() !== v || v === "" || cps(v) > LIMITS.region || line(v, LIMITS.region) !== v)
|
|
98
|
+
return fail(`${what} must be a plain region name of 1-${LIMITS.region}`);
|
|
99
|
+
return v;
|
|
100
|
+
}
|
|
101
|
+
function parseKind(o, k, what, required) {
|
|
102
|
+
const v = o[k];
|
|
103
|
+
if (v === undefined && !required)
|
|
104
|
+
return undefined;
|
|
105
|
+
if (typeof v !== "string" || !KIND_RE.test(v))
|
|
106
|
+
return fail(`${what}.${k} must be an entity kind`);
|
|
107
|
+
return v;
|
|
108
|
+
}
|
|
109
|
+
function optTrue(o, k, what) {
|
|
110
|
+
const v = o[k];
|
|
111
|
+
if (v === undefined)
|
|
112
|
+
return undefined;
|
|
113
|
+
if (v !== true)
|
|
114
|
+
return fail(`${what}.${k} may only be true`);
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
const int = (o, k, what, max = FP_LIMITS.counts) => {
|
|
118
|
+
const n = optInt(o, k, what, max);
|
|
119
|
+
if (n === undefined)
|
|
120
|
+
return fail(`${what}.${k} is required`);
|
|
121
|
+
return n;
|
|
122
|
+
};
|
|
123
|
+
function parseComponents(o, names, what) {
|
|
124
|
+
if (o.components === undefined)
|
|
125
|
+
return fail(`${what}.components is required`);
|
|
126
|
+
return list(o, "components", FP_LIMITS.components, what, (x, i) => {
|
|
127
|
+
const w = `${what}.components[${i}]`;
|
|
128
|
+
if (!isObj(x))
|
|
129
|
+
return fail(`${w} must be an object`);
|
|
130
|
+
const n = parseRegion(x.n, names, `${w}.n`);
|
|
131
|
+
const kind = parseKind(x, "kind", w, false);
|
|
132
|
+
return { n, ...(kind ? { kind } : {}), count: int(x, "count", w) };
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function obj(v, what) {
|
|
136
|
+
if (!isObj(v))
|
|
137
|
+
return fail(`${what} must be an object`);
|
|
138
|
+
return v;
|
|
139
|
+
}
|
|
140
|
+
function parseActor(x, i, names) {
|
|
141
|
+
const w = `footprint.actors[${i}]`;
|
|
142
|
+
const o = obj(x, w);
|
|
143
|
+
if (typeof o.a !== "string" || !AGENT_RE.test(o.a))
|
|
144
|
+
return fail(`${w}.a must be "main" or an agent id`);
|
|
145
|
+
const graph = oneOf(o, "graph", FP_GRAPHS, w);
|
|
146
|
+
const wt = optTrue(o, "wt", w);
|
|
147
|
+
const branch = o.branch === undefined ? undefined : typeof o.branch === "string" && BRANCH_RE.test(o.branch) ? o.branch : fail(`${w}.branch has characters a branch can't`);
|
|
148
|
+
const base = o.base === undefined ? undefined : typeof o.base === "string" && BASE_RE.test(o.base) ? o.base : fail(`${w}.base must be a commit id`);
|
|
149
|
+
const risk = oneOf(o, "risk", RISK_LEVELS, w);
|
|
150
|
+
const c = obj(o.changed, `${w}.changed`);
|
|
151
|
+
const files = list(c, "files", FP_LIMITS.files, `${w}.changed`, (f, j) => parseFile(f, names, `${w}.changed.files[${j}]`));
|
|
152
|
+
const symbols = list(c, "symbols", FP_LIMITS.changed, `${w}.changed`, (s, j) => {
|
|
153
|
+
const sw = `${w}.changed.symbols[${j}]`;
|
|
154
|
+
const so = obj(s, sw);
|
|
155
|
+
return { k: parseSymbolKey(so.k, names, `${sw}.k`), kind: parseKind(so, "kind", sw, true) };
|
|
156
|
+
});
|
|
157
|
+
const fresh = c.new === undefined ? undefined : list(c, "new", FP_LIMITS.newSymbols, `${w}.changed`, (s, j) => parseSymbolKey(s, names, `${w}.changed.new[${j}]`));
|
|
158
|
+
const r1 = obj(o.ring1, `${w}.ring1`);
|
|
159
|
+
const r1files = list(r1, "files", FP_LIMITS.ring1Files, `${w}.ring1`, (f, j) => {
|
|
160
|
+
const fw = `${w}.ring1.files[${j}]`;
|
|
161
|
+
const fo = obj(f, fw);
|
|
162
|
+
return { f: parseFile(fo.f, names, `${fw}.f`), rel: oneOf(fo, "rel", FP_RELS, fw) };
|
|
163
|
+
});
|
|
164
|
+
const r1syms = list(r1, "symbols", FP_LIMITS.ring1Symbols, `${w}.ring1`, (s, j) => {
|
|
165
|
+
const sw = `${w}.ring1.symbols[${j}]`;
|
|
166
|
+
const so = obj(s, sw);
|
|
167
|
+
return { k: parseSymbolKey(so.k, names, `${sw}.k`), rel: oneOf(so, "rel", FP_SYMBOL_RELS, sw) };
|
|
168
|
+
});
|
|
169
|
+
let ring2;
|
|
170
|
+
if (o.ring2 !== undefined) {
|
|
171
|
+
const r2 = obj(o.ring2, `${w}.ring2`);
|
|
172
|
+
ring2 = { files: list(r2, "files", FP_LIMITS.ring2Files, `${w}.ring2`, (f, j) => parseFile(f, names, `${w}.ring2.files[${j}]`)) };
|
|
173
|
+
}
|
|
174
|
+
const components = parseComponents(o, names, w);
|
|
175
|
+
return {
|
|
176
|
+
a: o.a,
|
|
177
|
+
graph,
|
|
178
|
+
...(wt ? { wt } : {}),
|
|
179
|
+
...(branch ? { branch } : {}),
|
|
180
|
+
...(base ? { base } : {}),
|
|
181
|
+
risk,
|
|
182
|
+
...(optTrue(o, "stale", w) ? { stale: true } : {}),
|
|
183
|
+
...(optTrue(o, "coarse", w) ? { coarse: true } : {}),
|
|
184
|
+
...(optTrue(o, "partial", w) ? { partial: true } : {}),
|
|
185
|
+
changed: { files, symbols, ...(fresh && fresh.length ? { new: fresh } : {}) },
|
|
186
|
+
ring1: { files: r1files, symbols: r1syms },
|
|
187
|
+
...(ring2 ? { ring2 } : {}),
|
|
188
|
+
components,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Receiving (strict, SPEC.md §8.5): every known field is checked against its
|
|
193
|
+
* type, pattern and cap, and one bad field makes the whole body invalid.
|
|
194
|
+
* Unknown fields are dropped (SPEC §1). Paths are repo-relative, symbol keys
|
|
195
|
+
* `path#qualifiedKey`, or `h:` tokens in hmac mode, never both.
|
|
196
|
+
*/
|
|
197
|
+
export function parseFootprintBody(v) {
|
|
198
|
+
const o = obj(v, "footprint body");
|
|
199
|
+
if (jsonBytes(o) > MAX_FOOTPRINT_BYTES)
|
|
200
|
+
fail(`footprint body is larger than ${MAX_FOOTPRINT_BYTES} bytes`);
|
|
201
|
+
const repo = parseRepo(o.repo, "footprint.repo");
|
|
202
|
+
const at = int(o, "at", "footprint", 8.64e15);
|
|
203
|
+
const seq = int(o, "seq", "footprint", Number.MAX_SAFE_INTEGER);
|
|
204
|
+
const names = oneOf(o, "names", FP_NAMES, "footprint");
|
|
205
|
+
let keyId;
|
|
206
|
+
if (names === "hmac") {
|
|
207
|
+
if (typeof o.keyId !== "string" || !KEY_ID_RE.test(o.keyId))
|
|
208
|
+
fail("footprint.keyId is required in hmac mode (k: + 8 hex)");
|
|
209
|
+
keyId = o.keyId;
|
|
210
|
+
}
|
|
211
|
+
else if (o.keyId !== undefined)
|
|
212
|
+
fail("footprint.keyId is only for hmac mode");
|
|
213
|
+
if (!Array.isArray(o.actors) || o.actors.length === 0)
|
|
214
|
+
fail("footprint.actors must be a non-empty list");
|
|
215
|
+
const actors = list(o, "actors", FP_LIMITS.actors, "footprint", (x, i) => parseActor(x, i, names));
|
|
216
|
+
const ids = new Set();
|
|
217
|
+
for (const a of actors) {
|
|
218
|
+
if (ids.has(a.a))
|
|
219
|
+
fail(`footprint.actors has ${a.a} twice`);
|
|
220
|
+
ids.add(a.a);
|
|
221
|
+
}
|
|
222
|
+
const m = obj(o.merged, "footprint.merged");
|
|
223
|
+
const counts = obj(m.counts, "footprint.merged.counts");
|
|
224
|
+
const merged = {
|
|
225
|
+
components: parseComponents(m, names, "footprint.merged"),
|
|
226
|
+
risk: oneOf(m, "risk", RISK_LEVELS, "footprint.merged"),
|
|
227
|
+
counts: { changedSymbols: int(counts, "changedSymbols", "footprint.merged.counts"), ring1Files: int(counts, "ring1Files", "footprint.merged.counts"), ring2Files: int(counts, "ring2Files", "footprint.merged.counts") },
|
|
228
|
+
};
|
|
229
|
+
const truncated = optTrue(o, "truncated", "footprint");
|
|
230
|
+
return { repo, at, seq, names, ...(keyId ? { keyId } : {}), actors, merged, ...(truncated ? { truncated } : {}) };
|
|
231
|
+
}
|
|
232
|
+
const RISK_ORDER = { unknown: 0, low: 1, medium: 2, high: 3, critical: 4 };
|
|
233
|
+
export const maxRisk = (rs) => {
|
|
234
|
+
let best = "unknown";
|
|
235
|
+
for (const r of rs)
|
|
236
|
+
if (RISK_ORDER[r] > RISK_ORDER[best])
|
|
237
|
+
best = r;
|
|
238
|
+
return best;
|
|
239
|
+
};
|
|
240
|
+
const REL_ORDER = { call: 0, use: 1, import: 2, test: 3 };
|
|
241
|
+
const uniq = (xs, key) => {
|
|
242
|
+
const seen = new Set();
|
|
243
|
+
return xs.filter((x) => {
|
|
244
|
+
const k = key(x);
|
|
245
|
+
if (seen.has(k))
|
|
246
|
+
return false;
|
|
247
|
+
seen.add(k);
|
|
248
|
+
return true;
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
/** Components summed by (name, kind), largest first, then by name. */
|
|
252
|
+
export function mergeComponents(lists) {
|
|
253
|
+
const by = new Map();
|
|
254
|
+
for (const l of lists) {
|
|
255
|
+
for (const c of l) {
|
|
256
|
+
const k = `${c.n}\u0000${c.kind ?? ""}`;
|
|
257
|
+
const cur = by.get(k);
|
|
258
|
+
if (cur)
|
|
259
|
+
cur.count = Math.min(FP_LIMITS.counts, cur.count + c.count);
|
|
260
|
+
else
|
|
261
|
+
by.set(k, { ...c });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return [...by.values()].sort((a, b) => b.count - a.count || (a.n < b.n ? -1 : a.n > b.n ? 1 : 0)).slice(0, FP_LIMITS.components);
|
|
265
|
+
}
|
|
266
|
+
/** Whether `f` would pass the receiver's check (§8.5), before any hashing: invalid entries are dropped, not sent. */
|
|
267
|
+
const ok = (f) => {
|
|
268
|
+
try {
|
|
269
|
+
f();
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const okFile = (p) => ok(() => parseFile(p, "plain", "file"));
|
|
277
|
+
const okKey = (k) => ok(() => parseSymbolKey(k, "plain", "symbol"));
|
|
278
|
+
const okRegion = (n) => ok(() => parseRegion(n, "plain", "region"));
|
|
279
|
+
const okKind = (k) => k === undefined || KIND_RE.test(k);
|
|
280
|
+
/** A raw actor with every entry the receiver would refuse dropped. */
|
|
281
|
+
function validEntries(r) {
|
|
282
|
+
return {
|
|
283
|
+
...r,
|
|
284
|
+
files: r.files.filter(okFile),
|
|
285
|
+
symbols: r.symbols.filter((s) => okKey(s.k) && okKind(s.kind)),
|
|
286
|
+
...(r.newSymbols ? { newSymbols: r.newSymbols.filter(okKey) } : {}),
|
|
287
|
+
ring1Files: r.ring1Files.filter((x) => okFile(x.f)),
|
|
288
|
+
ring1Symbols: r.ring1Symbols.filter((x) => okKey(x.k)),
|
|
289
|
+
...(r.ring2Files ? { ring2Files: r.ring2Files.filter(okFile) } : {}),
|
|
290
|
+
components: r.components.filter((c) => okRegion(c.n) && okKind(c.kind) && Number.isSafeInteger(c.count) && c.count >= 0),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/** A raw actor capped, sorted deterministically and named (plain or hashed). */
|
|
294
|
+
function wireActor(r, name) {
|
|
295
|
+
const files = uniq(r.files.map(name.file), (x) => x).slice(0, FP_LIMITS.files);
|
|
296
|
+
const symbols = uniq(r.symbols.map((s) => ({ k: name.symbol(s.k), kind: s.kind })), (s) => s.k).slice(0, FP_LIMITS.changed);
|
|
297
|
+
const fresh = uniq((r.newSymbols ?? []).map(name.symbol), (x) => x).slice(0, FP_LIMITS.newSymbols);
|
|
298
|
+
const r1f = uniq([...r.ring1Files].sort((x, y) => REL_ORDER[x.rel] - REL_ORDER[y.rel] || (x.f < y.f ? -1 : x.f > y.f ? 1 : 0)), (x) => x.f)
|
|
299
|
+
.map((x) => ({ f: name.file(x.f), rel: x.rel }))
|
|
300
|
+
.slice(0, FP_LIMITS.ring1Files);
|
|
301
|
+
const r1s = uniq([...r.ring1Symbols].sort((x, y) => REL_ORDER[x.rel] - REL_ORDER[y.rel] || (x.k < y.k ? -1 : x.k > y.k ? 1 : 0)), (x) => x.k)
|
|
302
|
+
.map((x) => ({ k: name.symbol(x.k), rel: x.rel }))
|
|
303
|
+
.slice(0, FP_LIMITS.ring1Symbols);
|
|
304
|
+
const r2 = r.ring2Files ? uniq([...r.ring2Files].sort(), (x) => x).map(name.file).slice(0, FP_LIMITS.ring2Files) : undefined;
|
|
305
|
+
return {
|
|
306
|
+
a: r.a,
|
|
307
|
+
graph: r.graph,
|
|
308
|
+
...(r.wt ? { wt: true } : {}),
|
|
309
|
+
...(r.branch && BRANCH_RE.test(r.branch) ? { branch: r.branch } : {}),
|
|
310
|
+
...(r.base && BASE_RE.test(r.base) ? { base: r.base } : {}),
|
|
311
|
+
risk: r.risk,
|
|
312
|
+
...(r.stale ? { stale: true } : {}),
|
|
313
|
+
...(r.coarse ? { coarse: true } : {}),
|
|
314
|
+
...(r.partial ? { partial: true } : {}),
|
|
315
|
+
changed: { files, symbols, ...(fresh.length ? { new: fresh } : {}) },
|
|
316
|
+
ring1: { files: r1f, symbols: r1s },
|
|
317
|
+
...(r2 && r2.length ? { ring2: { files: r2 } } : {}),
|
|
318
|
+
components: mergeComponents([r.components.map((c) => ({ ...c, n: name.region(c.n) }))]),
|
|
319
|
+
...(r.done ? { done: true } : {}),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
const plainNamer = { file: (p) => p, symbol: (k) => k, region: (n) => n };
|
|
323
|
+
const hmacNamer = (k) => ({ file: (p) => hashFile(k, p), symbol: (s) => hashSymbol(k, s), region: (n) => hashRegion(k, n) });
|
|
324
|
+
/**
|
|
325
|
+
* Builds the body: names (plain or hmac), per-actor caps, the merge (from the
|
|
326
|
+
* untrimmed actors), then trimming until it fits, in this order (SPEC §8.4):
|
|
327
|
+
* ring 2, ring-1 symbols, ring-1 files, finished agents, changed symbols,
|
|
328
|
+
* per-actor components, the merged components, running agents (last first).
|
|
329
|
+
* Each step takes from the longest list (ties: the later actor) a quarter of
|
|
330
|
+
* it from the end, so what is kept is the head of each sorted list, and the
|
|
331
|
+
* same input always gives the same body.
|
|
332
|
+
*/
|
|
333
|
+
export function assembleFootprint(i) {
|
|
334
|
+
const name = i.key ? hmacNamer(i.key) : plainNamer;
|
|
335
|
+
const actors = i.actors.slice(0, FP_LIMITS.actors).map((r) => wireActor(validEntries(r), name));
|
|
336
|
+
const ring1 = new Set();
|
|
337
|
+
const ring2 = new Set();
|
|
338
|
+
let changedSymbols = 0;
|
|
339
|
+
for (const a of actors) {
|
|
340
|
+
changedSymbols += a.changed.symbols.length + (a.changed.new?.length ?? 0);
|
|
341
|
+
for (const f of a.ring1.files)
|
|
342
|
+
ring1.add(f.f);
|
|
343
|
+
for (const f of a.ring2?.files ?? [])
|
|
344
|
+
ring2.add(f);
|
|
345
|
+
}
|
|
346
|
+
for (const f of ring1)
|
|
347
|
+
ring2.delete(f);
|
|
348
|
+
const merged = {
|
|
349
|
+
components: mergeComponents(actors.map((a) => a.components)),
|
|
350
|
+
risk: maxRisk(actors.map((a) => a.risk)),
|
|
351
|
+
counts: { changedSymbols: Math.min(changedSymbols, FP_LIMITS.counts), ring1Files: ring1.size, ring2Files: ring2.size },
|
|
352
|
+
};
|
|
353
|
+
const body = {
|
|
354
|
+
repo: i.repo,
|
|
355
|
+
at: i.at,
|
|
356
|
+
seq: i.seq,
|
|
357
|
+
names: i.key ? "hmac" : "plain",
|
|
358
|
+
...(i.key ? { keyId: i.key.keyId } : {}),
|
|
359
|
+
actors,
|
|
360
|
+
merged,
|
|
361
|
+
};
|
|
362
|
+
const max = i.maxBytes ?? MAX_FOOTPRINT_BYTES;
|
|
363
|
+
const size = () => jsonBytes({ ...body, actors: body.actors.map(stripDone), truncated: true });
|
|
364
|
+
let truncated = false;
|
|
365
|
+
const lists = [
|
|
366
|
+
(a) => a.ring2?.files,
|
|
367
|
+
(a) => a.ring1.symbols,
|
|
368
|
+
(a) => a.ring1.files,
|
|
369
|
+
];
|
|
370
|
+
const shrink = (get, among) => {
|
|
371
|
+
let pick;
|
|
372
|
+
for (const a of among) {
|
|
373
|
+
const l = get(a)?.length ?? 0;
|
|
374
|
+
if (l > 0 && (!pick || l >= (get(pick)?.length ?? 0)))
|
|
375
|
+
pick = a;
|
|
376
|
+
}
|
|
377
|
+
if (!pick)
|
|
378
|
+
return false;
|
|
379
|
+
const l = get(pick);
|
|
380
|
+
l.splice(l.length - Math.max(1, Math.ceil(l.length / 4)));
|
|
381
|
+
return true;
|
|
382
|
+
};
|
|
383
|
+
for (const get of lists)
|
|
384
|
+
while (size() > max && shrink(get, body.actors))
|
|
385
|
+
truncated = true;
|
|
386
|
+
// Finished agents, the last first.
|
|
387
|
+
while (size() > max) {
|
|
388
|
+
const j = body.actors.map((a) => a.done === true).lastIndexOf(true);
|
|
389
|
+
if (j < 0)
|
|
390
|
+
break;
|
|
391
|
+
body.actors.splice(j, 1);
|
|
392
|
+
truncated = true;
|
|
393
|
+
}
|
|
394
|
+
for (const get of [(a) => a.changed.new, (a) => a.changed.symbols, (a) => a.components]) {
|
|
395
|
+
while (size() > max && shrink(get, body.actors))
|
|
396
|
+
truncated = true;
|
|
397
|
+
}
|
|
398
|
+
while (size() > max && body.merged.components.length > 0) {
|
|
399
|
+
body.merged.components.splice(body.merged.components.length - Math.max(1, Math.ceil(body.merged.components.length / 4)));
|
|
400
|
+
truncated = true;
|
|
401
|
+
}
|
|
402
|
+
while (size() > max && body.actors.length > 1) {
|
|
403
|
+
body.actors.pop();
|
|
404
|
+
truncated = true;
|
|
405
|
+
}
|
|
406
|
+
while (size() > max && shrink((a) => a.changed.files, body.actors))
|
|
407
|
+
truncated = true;
|
|
408
|
+
for (const a of body.actors)
|
|
409
|
+
if (a.ring2 && a.ring2.files.length === 0)
|
|
410
|
+
delete a.ring2;
|
|
411
|
+
const out = { ...body, actors: body.actors.map(stripDone), ...(truncated ? { truncated: true } : {}) };
|
|
412
|
+
if (jsonBytes(out) > max)
|
|
413
|
+
throw new Error("footprint body doesn't fit even trimmed");
|
|
414
|
+
// The sender holds itself to the receiver's check: what goes out is exactly what validates.
|
|
415
|
+
const checked = max <= MAX_FOOTPRINT_BYTES ? parseFootprintBody(out) : out;
|
|
416
|
+
return { body: checked, fallback: footprintFallback(checked) };
|
|
417
|
+
}
|
|
418
|
+
function stripDone(a) {
|
|
419
|
+
const { done: _d, ...rest } = a;
|
|
420
|
+
return rest;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* The line a state-aware client without ext-ix shows (≤200 characters). Area
|
|
424
|
+
* names only in plain mode.
|
|
425
|
+
*/
|
|
426
|
+
export function footprintFallback(b) {
|
|
427
|
+
const n = b.merged.counts.changedSymbols;
|
|
428
|
+
const files = new Set(b.actors.flatMap((a) => a.changed.files)).size;
|
|
429
|
+
const areas = b.merged.components.length;
|
|
430
|
+
const risk = b.merged.risk === "unknown" ? "" : `, ${b.merged.risk} risk`;
|
|
431
|
+
const down = b.merged.counts.ring1Files + b.merged.counts.ring2Files;
|
|
432
|
+
const downstream = down > 0 && b.names === "plain" ? `, ~${down} files downstream` : "";
|
|
433
|
+
const tail = "; enable ext-ix to compare";
|
|
434
|
+
const plural = (k, w) => `${k} ${w}${k === 1 ? "" : "s"}`;
|
|
435
|
+
const where = areas > 0 ? `in ${plural(areas, "area")}` : `in ${plural(files, "file")}`;
|
|
436
|
+
const head = `Ix footprint: ${plural(n, "symbol")} ${where}`;
|
|
437
|
+
if (b.names === "plain" && areas > 0) {
|
|
438
|
+
const all = b.merged.components.map((c) => line(c.n, 40));
|
|
439
|
+
for (let k = Math.min(all.length, 5); k > 0; k--) {
|
|
440
|
+
const s = `${head} (${all.slice(0, k).join(", ")}${k < all.length ? ", …" : ""})${risk}${downstream}${tail}`;
|
|
441
|
+
if (cps(s) <= 200)
|
|
442
|
+
return s;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return `${head}${risk}${downstream}${tail}`.slice(0, 200);
|
|
446
|
+
}
|
|
447
|
+
/** What decides republishing: the body without its time and sequence number. */
|
|
448
|
+
export function footprintDigest(b) {
|
|
449
|
+
const { at: _a, seq: _s, ...rest } = b;
|
|
450
|
+
return createHash("sha256").update(JSON.stringify(rest)).digest("hex").slice(0, 32);
|
|
451
|
+
}
|
package/dist/hmac.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const HMAC_TOKEN_RE: RegExp;
|
|
2
|
+
export declare const KEY_ID_RE: RegExp;
|
|
3
|
+
export interface NameKey {
|
|
4
|
+
/** 32 bytes: sha256("p2p/ix-footprint/v1|" + remote + "|" + rootCommit). */
|
|
5
|
+
key: Buffer;
|
|
6
|
+
/** "k:" + the first 8 hex of sha256(key): lets a receiver tell it holds the same key. */
|
|
7
|
+
keyId: string;
|
|
8
|
+
}
|
|
9
|
+
/** The repository's key. `rootCommit` is the lexicographically smallest parentless commit. */
|
|
10
|
+
export declare function deriveNameKey(remote: string, rootCommit: string): NameKey;
|
|
11
|
+
/** Picks the root commit from `git rev-list --max-parents=0 HEAD` output (several when histories were merged). */
|
|
12
|
+
export declare function pickRootCommit(revList: string): string | undefined;
|
|
13
|
+
/** A file: "f|" + lower(relPath). Case-folded, as Ix compares paths. */
|
|
14
|
+
export declare const hashFile: (k: NameKey, relPath: string) => string;
|
|
15
|
+
/** A symbol key `path#qualifiedKey`: "s|" + lower(path) + "#" + qualifiedKey. */
|
|
16
|
+
export declare function hashSymbol(k: NameKey, key: string): string;
|
|
17
|
+
/** A region (component) name: "c|" + name. */
|
|
18
|
+
export declare const hashRegion: (k: NameKey, name: string) => string;
|
package/dist/hmac.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// Hashed names for x:ix.footprint (SPEC.md §8.6): by default a footprint
|
|
4
|
+
// names files, symbols and regions by keyed hashes instead of by name
|
|
5
|
+
// (readable names are the person's opt-in, per circle). The key comes from the
|
|
6
|
+
// repository itself (its normalized remote and its root commit), so only
|
|
7
|
+
// people who have the repository can compute it, and two checkouts of the
|
|
8
|
+
// same repository agree on it with no exchange.
|
|
9
|
+
//
|
|
10
|
+
// What this hides and what it doesn't is spelled out in SPEC.md §8.6: it is
|
|
11
|
+
// a speed bump against casual reading by people without the code, not
|
|
12
|
+
// encryption. Anyone with the repository (and, for a public one, anyone at
|
|
13
|
+
// all) can hash every path and symbol and read a footprint back.
|
|
14
|
+
import { createHash, createHmac } from "node:crypto";
|
|
15
|
+
export const HMAC_TOKEN_RE = /^h:[A-Za-z0-9_-]{22}$/;
|
|
16
|
+
export const KEY_ID_RE = /^k:[0-9a-f]{8}$/;
|
|
17
|
+
/** The repository's key. `rootCommit` is the lexicographically smallest parentless commit. */
|
|
18
|
+
export function deriveNameKey(remote, rootCommit) {
|
|
19
|
+
const key = createHash("sha256").update(`p2p/ix-footprint/v1|${remote}|${rootCommit}`, "utf8").digest();
|
|
20
|
+
return { key, keyId: `k:${createHash("sha256").update(key).digest("hex").slice(0, 8)}` };
|
|
21
|
+
}
|
|
22
|
+
/** Picks the root commit from `git rev-list --max-parents=0 HEAD` output (several when histories were merged). */
|
|
23
|
+
export function pickRootCommit(revList) {
|
|
24
|
+
const ids = revList
|
|
25
|
+
.split(/\s+/)
|
|
26
|
+
.map((s) => s.trim().toLowerCase())
|
|
27
|
+
.filter((s) => /^[0-9a-f]{40}(?:[0-9a-f]{24})?$/.test(s))
|
|
28
|
+
.sort();
|
|
29
|
+
return ids[0];
|
|
30
|
+
}
|
|
31
|
+
function token(k, msg) {
|
|
32
|
+
return `h:${createHmac("sha256", k.key).update(msg, "utf8").digest("base64url").slice(0, 22)}`;
|
|
33
|
+
}
|
|
34
|
+
/** A file: "f|" + lower(relPath). Case-folded, as Ix compares paths. */
|
|
35
|
+
export const hashFile = (k, relPath) => token(k, `f|${relPath.toLowerCase()}`);
|
|
36
|
+
/** A symbol key `path#qualifiedKey`: "s|" + lower(path) + "#" + qualifiedKey. */
|
|
37
|
+
export function hashSymbol(k, key) {
|
|
38
|
+
const i = key.indexOf("#");
|
|
39
|
+
return token(k, `s|${key.slice(0, i).toLowerCase()}#${key.slice(i + 1)}`);
|
|
40
|
+
}
|
|
41
|
+
/** A region (component) name: "c|" + name. */
|
|
42
|
+
export const hashRegion = (k, name) => token(k, `c|${name}`);
|