@mneme-ai/core 2.62.0 → 2.64.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/agent_manifest.d.ts.map +1 -1
- package/dist/agent_manifest.js +8 -0
- package/dist/agent_manifest.js.map +1 -1
- package/dist/diff_arena/adapters.d.ts +84 -0
- package/dist/diff_arena/adapters.d.ts.map +1 -0
- package/dist/diff_arena/adapters.js +172 -0
- package/dist/diff_arena/adapters.js.map +1 -0
- package/dist/diff_arena/consensus.d.ts +73 -0
- package/dist/diff_arena/consensus.d.ts.map +1 -0
- package/dist/diff_arena/consensus.js +138 -0
- package/dist/diff_arena/consensus.js.map +1 -0
- package/dist/diff_arena/index.d.ts +100 -0
- package/dist/diff_arena/index.d.ts.map +1 -0
- package/dist/diff_arena/index.js +230 -0
- package/dist/diff_arena/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/time_crystal/gotcha_detector.d.ts +31 -0
- package/dist/time_crystal/gotcha_detector.d.ts.map +1 -0
- package/dist/time_crystal/gotcha_detector.js +81 -0
- package/dist/time_crystal/gotcha_detector.js.map +1 -0
- package/dist/time_crystal/index.d.ts +158 -0
- package/dist/time_crystal/index.d.ts.map +1 -0
- package/dist/time_crystal/index.js +289 -0
- package/dist/time_crystal/index.js.map +1 -0
- package/dist/time_crystal/problem_fingerprint.d.ts +43 -0
- package/dist/time_crystal/problem_fingerprint.d.ts.map +1 -0
- package/dist/time_crystal/problem_fingerprint.js +101 -0
- package/dist/time_crystal/problem_fingerprint.js.map +1 -0
- package/dist/time_crystal/ranking.d.ts +60 -0
- package/dist/time_crystal/ranking.d.ts.map +1 -0
- package/dist/time_crystal/ranking.js +90 -0
- package/dist/time_crystal/ranking.js.map +1 -0
- package/dist/truth_gate/claims.d.ts.map +1 -1
- package/dist/truth_gate/claims.js +38 -0
- package/dist/truth_gate/claims.js.map +1 -1
- package/dist/truth_gate/probes.d.ts.map +1 -1
- package/dist/truth_gate/probes.js +115 -0
- package/dist/truth_gate/probes.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v2.63.0 — TIME-CRYSTAL: federated agent wisdom.
|
|
3
|
+
*
|
|
4
|
+
* User-roadmap (continuing from v2.60-v2.62): conscience + **memory** +
|
|
5
|
+
* diplomat + bodyguard + time machine. v2.63 ships the **memory**
|
|
6
|
+
* primitive.
|
|
7
|
+
*
|
|
8
|
+
* When agent A encounters problem P (e.g. "Cannot find module
|
|
9
|
+
* @types/node"), Mneme replies "342 agents saw this same problem in
|
|
10
|
+
* the last 7 days. 89% used `npm i -D @types/node` (304 trials, Wilson
|
|
11
|
+
* lower-bound 0.85). 11% tried `delete node_modules` (38 trials, but
|
|
12
|
+
* 17 failed on pnpm projects)."
|
|
13
|
+
*
|
|
14
|
+
* Pre-MIRROR data sources for AI tooling are static (Stack Overflow,
|
|
15
|
+
* GitHub Issues, docs). TIME-CRYSTAL aggregates **actual AI agent
|
|
16
|
+
* behavior** — every contribution = a real agent ran a real fix and
|
|
17
|
+
* got a real outcome. Every agent using Mneme MCP contributes back.
|
|
18
|
+
*
|
|
19
|
+
* 5 wild innovations:
|
|
20
|
+
*
|
|
21
|
+
* 1. CANONICAL PROBLEM FINGERPRINTING — entity slotting (`<PKG>`,
|
|
22
|
+
* `<VER>`, `<PATH>`, `<HASH>`, `<TSERR>`) + stop-word filter +
|
|
23
|
+
* token sort + SHA-256 = stable 16-hex cluster key. Two agents
|
|
24
|
+
* typing "Cannot find module '@types/node'" and "TypeScript Error
|
|
25
|
+
* TS2307: Cannot find module @types/node" cluster to ONE bucket.
|
|
26
|
+
*
|
|
27
|
+
* 2. WILSON LOWER-BOUND RANKING — small-sample approaches don't
|
|
28
|
+
* dominate. 2/2 success looks great until Wilson LB says 0.20.
|
|
29
|
+
* Surfaces statistically credible recommendations.
|
|
30
|
+
*
|
|
31
|
+
* 3. RECENCY DECAY — exponential half-life (30 days default).
|
|
32
|
+
* Old wisdom may be stale (package versions move, framework
|
|
33
|
+
* defaults change). Recent wins weigh more.
|
|
34
|
+
*
|
|
35
|
+
* 4. ENVIRONMENT-AWARE GROUNDING — caller passes current env
|
|
36
|
+
* (`{ node: "22.13.0", pnpm: "10" }`); ranking boosts approaches
|
|
37
|
+
* tested on matching env. Prevents "this works on Yarn but you're
|
|
38
|
+
* on pnpm" mismatches.
|
|
39
|
+
*
|
|
40
|
+
* 5. GOTCHA AUTO-DETECTION — when an approach has mixed outcomes,
|
|
41
|
+
* compute the env-key/value where failure-rate gain ≥ 30%.
|
|
42
|
+
* Auto-extracts "X fails on Y" patterns without anyone writing them.
|
|
43
|
+
* Plus folds free-text failure notes ("ฟังก์ชั่น break ถ้ามี
|
|
44
|
+
* pnpm-lock.yaml").
|
|
45
|
+
*
|
|
46
|
+
* Plus HMAC-chained ledger (same convention as PASSPORT + MIRRAGE)
|
|
47
|
+
* for tamper-evident contribution history; opt-in cross-host gossip
|
|
48
|
+
* via CONSENT FABRIC (wire pending v2.64+).
|
|
49
|
+
*
|
|
50
|
+
* Pure ESM. Defensive — never throws.
|
|
51
|
+
*/
|
|
52
|
+
import { type ApproachGroup, type Outcome } from "./ranking.js";
|
|
53
|
+
import { type Gotcha } from "./gotcha_detector.js";
|
|
54
|
+
export interface ContributionInput {
|
|
55
|
+
/** Free-text problem description (will be fingerprinted). */
|
|
56
|
+
problem: string;
|
|
57
|
+
/** Free-text description of what the agent tried. */
|
|
58
|
+
approach: string;
|
|
59
|
+
/** Outcome — success/failure/partial. */
|
|
60
|
+
outcome: Outcome;
|
|
61
|
+
/** Reporting agent id (for distinct-contributor count). */
|
|
62
|
+
agent: string;
|
|
63
|
+
/** Optional environment map (node, pnpm, framework versions). */
|
|
64
|
+
env?: Record<string, string>;
|
|
65
|
+
/** Optional free-text gotcha hint. */
|
|
66
|
+
note?: string;
|
|
67
|
+
/** Working directory. */
|
|
68
|
+
cwd?: string;
|
|
69
|
+
}
|
|
70
|
+
export interface ContributionResult {
|
|
71
|
+
ok: boolean;
|
|
72
|
+
recordId: string;
|
|
73
|
+
problemFingerprint: string;
|
|
74
|
+
hmac: string;
|
|
75
|
+
hint: string;
|
|
76
|
+
}
|
|
77
|
+
export interface LookupInput {
|
|
78
|
+
problem: string;
|
|
79
|
+
/** Optional env to filter / boost ranking. */
|
|
80
|
+
env?: Record<string, string>;
|
|
81
|
+
/** Max approaches to return. */
|
|
82
|
+
topN?: number;
|
|
83
|
+
/** Working directory. */
|
|
84
|
+
cwd?: string;
|
|
85
|
+
/** Recency half-life override (ms). Default 30 days. */
|
|
86
|
+
halfLifeMs?: number;
|
|
87
|
+
/** Reference "now" for deterministic tests. */
|
|
88
|
+
now?: number;
|
|
89
|
+
}
|
|
90
|
+
export interface LookupResult {
|
|
91
|
+
problem: string;
|
|
92
|
+
problemFingerprint: string;
|
|
93
|
+
totalContributors: number;
|
|
94
|
+
distinctAgents: number;
|
|
95
|
+
approaches: ApproachGroup[];
|
|
96
|
+
gotchas: Gotcha[];
|
|
97
|
+
/** Similar (but not identical) problem fingerprints with cross-bucket overlap. */
|
|
98
|
+
related: Array<{
|
|
99
|
+
fingerprint: string;
|
|
100
|
+
canonical: string;
|
|
101
|
+
similarity: number;
|
|
102
|
+
sampleSize: number;
|
|
103
|
+
}>;
|
|
104
|
+
/** Plain-English summary suitable for an agent to read. */
|
|
105
|
+
summary: string;
|
|
106
|
+
at: string;
|
|
107
|
+
hmac: string;
|
|
108
|
+
}
|
|
109
|
+
interface LedgerEntry {
|
|
110
|
+
kind: "contribution";
|
|
111
|
+
at: string;
|
|
112
|
+
recordId: string;
|
|
113
|
+
agent: string;
|
|
114
|
+
problemFingerprint: string;
|
|
115
|
+
canonical: string;
|
|
116
|
+
approach: string;
|
|
117
|
+
outcome: Outcome;
|
|
118
|
+
env?: Record<string, string>;
|
|
119
|
+
note?: string;
|
|
120
|
+
prevHmac: string;
|
|
121
|
+
hmac: string;
|
|
122
|
+
}
|
|
123
|
+
export declare function readLedger(cwd: string): LedgerEntry[];
|
|
124
|
+
export declare function verifyLedgerChain(cwd: string): {
|
|
125
|
+
ok: boolean;
|
|
126
|
+
rows: number;
|
|
127
|
+
brokenAt?: number;
|
|
128
|
+
};
|
|
129
|
+
export declare function contribute(input: ContributionInput): ContributionResult;
|
|
130
|
+
export declare function lookupWisdom(input: LookupInput): LookupResult;
|
|
131
|
+
export declare function verifyLookup(r: LookupResult): boolean;
|
|
132
|
+
export interface ContributorStats {
|
|
133
|
+
totalContributions: number;
|
|
134
|
+
distinctAgents: number;
|
|
135
|
+
distinctProblems: number;
|
|
136
|
+
/** Top 10 agents by contribution count. */
|
|
137
|
+
topAgents: Array<{
|
|
138
|
+
agent: string;
|
|
139
|
+
count: number;
|
|
140
|
+
}>;
|
|
141
|
+
/** Top 10 most-discussed problem fingerprints. */
|
|
142
|
+
topProblems: Array<{
|
|
143
|
+
fingerprint: string;
|
|
144
|
+
canonical: string;
|
|
145
|
+
count: number;
|
|
146
|
+
}>;
|
|
147
|
+
/** Distribution of outcomes. */
|
|
148
|
+
outcomes: Record<Outcome, number>;
|
|
149
|
+
}
|
|
150
|
+
export declare function contributorStats(cwd: string): ContributorStats;
|
|
151
|
+
export declare function renderLookupBanner(r: LookupResult): string;
|
|
152
|
+
export { normalizeProblem, similarity } from "./problem_fingerprint.js";
|
|
153
|
+
export type { NormalizationResult } from "./problem_fingerprint.js";
|
|
154
|
+
export { rankApproaches, approachKey } from "./ranking.js";
|
|
155
|
+
export type { WisdomRecord, ApproachGroup, RankOpts, Outcome } from "./ranking.js";
|
|
156
|
+
export { detectGotchas } from "./gotcha_detector.js";
|
|
157
|
+
export type { Gotcha } from "./gotcha_detector.js";
|
|
158
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/time_crystal/index.ts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 ships the **memory**
|
|
6
|
+
* primitive.
|
|
7
|
+
*
|
|
8
|
+
* When agent A encounters problem P (e.g. "Cannot find module
|
|
9
|
+
* @types/node"), Mneme replies "342 agents saw this same problem in
|
|
10
|
+
* the last 7 days. 89% used `npm i -D @types/node` (304 trials, Wilson
|
|
11
|
+
* lower-bound 0.85). 11% tried `delete node_modules` (38 trials, but
|
|
12
|
+
* 17 failed on pnpm projects)."
|
|
13
|
+
*
|
|
14
|
+
* Pre-MIRROR data sources for AI tooling are static (Stack Overflow,
|
|
15
|
+
* GitHub Issues, docs). TIME-CRYSTAL aggregates **actual AI agent
|
|
16
|
+
* behavior** — every contribution = a real agent ran a real fix and
|
|
17
|
+
* got a real outcome. Every agent using Mneme MCP contributes back.
|
|
18
|
+
*
|
|
19
|
+
* 5 wild innovations:
|
|
20
|
+
*
|
|
21
|
+
* 1. CANONICAL PROBLEM FINGERPRINTING — entity slotting (`<PKG>`,
|
|
22
|
+
* `<VER>`, `<PATH>`, `<HASH>`, `<TSERR>`) + stop-word filter +
|
|
23
|
+
* token sort + SHA-256 = stable 16-hex cluster key. Two agents
|
|
24
|
+
* typing "Cannot find module '@types/node'" and "TypeScript Error
|
|
25
|
+
* TS2307: Cannot find module @types/node" cluster to ONE bucket.
|
|
26
|
+
*
|
|
27
|
+
* 2. WILSON LOWER-BOUND RANKING — small-sample approaches don't
|
|
28
|
+
* dominate. 2/2 success looks great until Wilson LB says 0.20.
|
|
29
|
+
* Surfaces statistically credible recommendations.
|
|
30
|
+
*
|
|
31
|
+
* 3. RECENCY DECAY — exponential half-life (30 days default).
|
|
32
|
+
* Old wisdom may be stale (package versions move, framework
|
|
33
|
+
* defaults change). Recent wins weigh more.
|
|
34
|
+
*
|
|
35
|
+
* 4. ENVIRONMENT-AWARE GROUNDING — caller passes current env
|
|
36
|
+
* (`{ node: "22.13.0", pnpm: "10" }`); ranking boosts approaches
|
|
37
|
+
* tested on matching env. Prevents "this works on Yarn but you're
|
|
38
|
+
* on pnpm" mismatches.
|
|
39
|
+
*
|
|
40
|
+
* 5. GOTCHA AUTO-DETECTION — when an approach has mixed outcomes,
|
|
41
|
+
* compute the env-key/value where failure-rate gain ≥ 30%.
|
|
42
|
+
* Auto-extracts "X fails on Y" patterns without anyone writing them.
|
|
43
|
+
* Plus folds free-text failure notes ("ฟังก์ชั่น break ถ้ามี
|
|
44
|
+
* pnpm-lock.yaml").
|
|
45
|
+
*
|
|
46
|
+
* Plus HMAC-chained ledger (same convention as PASSPORT + MIRRAGE)
|
|
47
|
+
* for tamper-evident contribution history; opt-in cross-host gossip
|
|
48
|
+
* via CONSENT FABRIC (wire pending v2.64+).
|
|
49
|
+
*
|
|
50
|
+
* Pure ESM. Defensive — never throws.
|
|
51
|
+
*/
|
|
52
|
+
import { createHmac, randomBytes } from "node:crypto";
|
|
53
|
+
import { appendFileSync, mkdirSync, readFileSync } from "node:fs";
|
|
54
|
+
import { dirname, join } from "node:path";
|
|
55
|
+
import { normalizeProblem, similarity } from "./problem_fingerprint.js";
|
|
56
|
+
import { rankApproaches } from "./ranking.js";
|
|
57
|
+
import { detectGotchas } from "./gotcha_detector.js";
|
|
58
|
+
const KEY_ENV = "MNEME_TIME_CRYSTAL_KEY";
|
|
59
|
+
const DEFAULT_KEY = "mneme-time-crystal-v1";
|
|
60
|
+
function keyOf() { return process.env[KEY_ENV] ?? DEFAULT_KEY; }
|
|
61
|
+
/* ── Canonical JSON HMAC ────────────────────────────────────────── */
|
|
62
|
+
function canonicalJson(o) {
|
|
63
|
+
if (o === undefined)
|
|
64
|
+
return "null";
|
|
65
|
+
if (o === null || typeof o !== "object")
|
|
66
|
+
return JSON.stringify(o);
|
|
67
|
+
if (Array.isArray(o))
|
|
68
|
+
return "[" + o.map((x) => canonicalJson(x === undefined ? null : x)).join(",") + "]";
|
|
69
|
+
const entries = Object.entries(o).filter(([, v]) => v !== undefined);
|
|
70
|
+
entries.sort(([a], [b]) => a.localeCompare(b));
|
|
71
|
+
return "{" + entries.map(([k, v]) => JSON.stringify(k) + ":" + canonicalJson(v)).join(",") + "}";
|
|
72
|
+
}
|
|
73
|
+
function signHmac(body) {
|
|
74
|
+
return createHmac("sha256", keyOf()).update(canonicalJson(body)).digest("hex");
|
|
75
|
+
}
|
|
76
|
+
function ledgerPath(cwd) {
|
|
77
|
+
return join(cwd, ".mneme", "time_crystal", "wisdom.jsonl");
|
|
78
|
+
}
|
|
79
|
+
function readLedgerLines(cwd) {
|
|
80
|
+
try {
|
|
81
|
+
return readFileSync(ledgerPath(cwd), "utf8").trim().split(/\n/).filter((l) => l.trim().length > 0);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function lastLedgerHmac(cwd) {
|
|
88
|
+
const lines = readLedgerLines(cwd);
|
|
89
|
+
if (lines.length === 0)
|
|
90
|
+
return "";
|
|
91
|
+
try {
|
|
92
|
+
return JSON.parse(lines[lines.length - 1]).hmac;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return "";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export function readLedger(cwd) {
|
|
99
|
+
return readLedgerLines(cwd).map((l) => {
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(l);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}).filter((x) => x !== null);
|
|
107
|
+
}
|
|
108
|
+
export function verifyLedgerChain(cwd) {
|
|
109
|
+
const lines = readLedger(cwd);
|
|
110
|
+
let prevHmac = "";
|
|
111
|
+
for (let i = 0; i < lines.length; i++) {
|
|
112
|
+
const row = lines[i];
|
|
113
|
+
if (row.prevHmac !== prevHmac)
|
|
114
|
+
return { ok: false, rows: i, brokenAt: i };
|
|
115
|
+
const { hmac, ...body } = row;
|
|
116
|
+
const expected = createHmac("sha256", keyOf()).update(prevHmac).update(canonicalJson(body)).digest("hex");
|
|
117
|
+
if (expected !== hmac)
|
|
118
|
+
return { ok: false, rows: i, brokenAt: i };
|
|
119
|
+
prevHmac = hmac;
|
|
120
|
+
}
|
|
121
|
+
return { ok: true, rows: lines.length };
|
|
122
|
+
}
|
|
123
|
+
/* ── Contribute ─────────────────────────────────────────────────── */
|
|
124
|
+
export function contribute(input) {
|
|
125
|
+
const cwd = input.cwd ?? process.cwd();
|
|
126
|
+
const norm = normalizeProblem(input.problem);
|
|
127
|
+
const recordId = randomBytes(8).toString("hex");
|
|
128
|
+
const at = new Date().toISOString();
|
|
129
|
+
const prevHmac = lastLedgerHmac(cwd);
|
|
130
|
+
const body = {
|
|
131
|
+
kind: "contribution",
|
|
132
|
+
at, recordId, agent: input.agent,
|
|
133
|
+
problemFingerprint: norm.fingerprint,
|
|
134
|
+
canonical: norm.canonical,
|
|
135
|
+
approach: input.approach,
|
|
136
|
+
outcome: input.outcome,
|
|
137
|
+
env: input.env,
|
|
138
|
+
note: input.note,
|
|
139
|
+
prevHmac,
|
|
140
|
+
};
|
|
141
|
+
const hmac = createHmac("sha256", keyOf()).update(prevHmac).update(canonicalJson(body)).digest("hex");
|
|
142
|
+
const entry = { ...body, hmac };
|
|
143
|
+
try {
|
|
144
|
+
mkdirSync(dirname(ledgerPath(cwd)), { recursive: true });
|
|
145
|
+
appendFileSync(ledgerPath(cwd), JSON.stringify(entry) + "\n");
|
|
146
|
+
}
|
|
147
|
+
catch { /* noop */ }
|
|
148
|
+
return {
|
|
149
|
+
ok: true,
|
|
150
|
+
recordId,
|
|
151
|
+
problemFingerprint: norm.fingerprint,
|
|
152
|
+
hmac,
|
|
153
|
+
hint: `contributed: agent=${input.agent} outcome=${input.outcome} fingerprint=${norm.fingerprint}`,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/* ── Lookup ─────────────────────────────────────────────────────── */
|
|
157
|
+
export function lookupWisdom(input) {
|
|
158
|
+
const cwd = input.cwd ?? process.cwd();
|
|
159
|
+
const norm = normalizeProblem(input.problem);
|
|
160
|
+
const all = readLedger(cwd);
|
|
161
|
+
const exact = all.filter((e) => e.problemFingerprint === norm.fingerprint);
|
|
162
|
+
// Build records for ranking.
|
|
163
|
+
const records = exact.map((e) => ({
|
|
164
|
+
approach: e.approach,
|
|
165
|
+
outcome: e.outcome,
|
|
166
|
+
at: e.at,
|
|
167
|
+
env: e.env,
|
|
168
|
+
note: e.note,
|
|
169
|
+
}));
|
|
170
|
+
const approaches = rankApproaches(records, {
|
|
171
|
+
env: input.env,
|
|
172
|
+
halfLifeMs: input.halfLifeMs,
|
|
173
|
+
now: input.now,
|
|
174
|
+
}).slice(0, input.topN ?? 5);
|
|
175
|
+
const gotchas = detectGotchas(records);
|
|
176
|
+
// Related: distinct fingerprints with >0.30 token-set similarity.
|
|
177
|
+
const byFingerprint = new Map();
|
|
178
|
+
for (const e of all) {
|
|
179
|
+
if (e.problemFingerprint === norm.fingerprint)
|
|
180
|
+
continue;
|
|
181
|
+
const list = byFingerprint.get(e.problemFingerprint) ?? [];
|
|
182
|
+
list.push(e);
|
|
183
|
+
byFingerprint.set(e.problemFingerprint, list);
|
|
184
|
+
}
|
|
185
|
+
const related = [];
|
|
186
|
+
for (const [fp, list] of byFingerprint) {
|
|
187
|
+
const sim = similarity(norm.canonical, list[0].canonical);
|
|
188
|
+
if (sim >= 0.30) {
|
|
189
|
+
related.push({ fingerprint: fp, canonical: list[0].canonical, similarity: +sim.toFixed(3), sampleSize: list.length });
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
related.sort((a, b) => b.similarity - a.similarity);
|
|
193
|
+
const distinctAgents = new Set(exact.map((e) => e.agent)).size;
|
|
194
|
+
const at = new Date().toISOString();
|
|
195
|
+
// Plain-English summary suitable for the agent to read inline.
|
|
196
|
+
const summaryLines = [];
|
|
197
|
+
if (exact.length === 0) {
|
|
198
|
+
summaryLines.push(`No prior wisdom on this exact problem fingerprint (${norm.fingerprint}).`);
|
|
199
|
+
if (related.length > 0)
|
|
200
|
+
summaryLines.push(`But ${related.length} similar bucket(s) exist — top: "${related[0].canonical.slice(0, 80)}".`);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
summaryLines.push(`${exact.length} contribution(s) from ${distinctAgents} distinct agent(s) on this problem.`);
|
|
204
|
+
for (const a of approaches.slice(0, 3)) {
|
|
205
|
+
const pct = (a.successRate * 100).toFixed(0);
|
|
206
|
+
const lb = (a.wilsonLB * 100).toFixed(0);
|
|
207
|
+
summaryLines.push(` • ${a.approach}: ${pct}% success (n=${a.sampleSize}, Wilson-LB ${lb}%)`);
|
|
208
|
+
}
|
|
209
|
+
if (gotchas.length > 0) {
|
|
210
|
+
summaryLines.push(`gotchas: ${gotchas.slice(0, 2).map((g) => g.approach + (g.triggerConditions.length > 0 ? ` (fails on ${g.triggerConditions[0].key}=${g.triggerConditions[0].value})` : "")).join("; ")}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const summary = summaryLines.join("\n");
|
|
214
|
+
const bodyForHmac = {
|
|
215
|
+
problem: input.problem,
|
|
216
|
+
problemFingerprint: norm.fingerprint,
|
|
217
|
+
totalContributors: exact.length,
|
|
218
|
+
distinctAgents,
|
|
219
|
+
approaches, gotchas, related, summary, at,
|
|
220
|
+
};
|
|
221
|
+
const hmac = signHmac(bodyForHmac);
|
|
222
|
+
return { ...bodyForHmac, hmac };
|
|
223
|
+
}
|
|
224
|
+
export function verifyLookup(r) {
|
|
225
|
+
if (!r || typeof r.hmac !== "string")
|
|
226
|
+
return false;
|
|
227
|
+
const { hmac, ...body } = r;
|
|
228
|
+
return signHmac(body) === hmac;
|
|
229
|
+
}
|
|
230
|
+
export function contributorStats(cwd) {
|
|
231
|
+
const rows = readLedger(cwd);
|
|
232
|
+
const agentCount = new Map();
|
|
233
|
+
const problemCount = new Map();
|
|
234
|
+
const outcomes = { success: 0, failure: 0, partial: 0 };
|
|
235
|
+
for (const r of rows) {
|
|
236
|
+
agentCount.set(r.agent, (agentCount.get(r.agent) ?? 0) + 1);
|
|
237
|
+
const p = problemCount.get(r.problemFingerprint) ?? { canonical: r.canonical, count: 0 };
|
|
238
|
+
p.count++;
|
|
239
|
+
problemCount.set(r.problemFingerprint, p);
|
|
240
|
+
outcomes[r.outcome]++;
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
totalContributions: rows.length,
|
|
244
|
+
distinctAgents: agentCount.size,
|
|
245
|
+
distinctProblems: problemCount.size,
|
|
246
|
+
topAgents: Array.from(agentCount.entries()).sort((a, b) => b[1] - a[1]).slice(0, 10).map(([agent, count]) => ({ agent, count })),
|
|
247
|
+
topProblems: Array.from(problemCount.entries()).sort((a, b) => b[1].count - a[1].count).slice(0, 10).map(([fingerprint, { canonical, count }]) => ({ fingerprint, canonical, count })),
|
|
248
|
+
outcomes,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
/* ── Render ─────────────────────────────────────────────────────── */
|
|
252
|
+
export function renderLookupBanner(r) {
|
|
253
|
+
const lines = [
|
|
254
|
+
`🌌 TIME-CRYSTAL · fingerprint ${r.problemFingerprint}`,
|
|
255
|
+
` ${r.totalContributors} contribution(s) · ${r.distinctAgents} distinct agent(s)`,
|
|
256
|
+
"",
|
|
257
|
+
];
|
|
258
|
+
for (const a of r.approaches) {
|
|
259
|
+
const pct = (a.successRate * 100).toFixed(0);
|
|
260
|
+
const lb = (a.wilsonLB * 100).toFixed(0);
|
|
261
|
+
lines.push(` ★ rank=${a.rankScore.toFixed(3)} ${pct}% success Wilson-LB ${lb}% n=${a.sampleSize}`);
|
|
262
|
+
lines.push(` ${a.approach}`);
|
|
263
|
+
if (a.notes.length > 0)
|
|
264
|
+
lines.push(` note: ${a.notes[0]}`);
|
|
265
|
+
}
|
|
266
|
+
if (r.gotchas.length > 0) {
|
|
267
|
+
lines.push("");
|
|
268
|
+
lines.push(` GOTCHAS (${r.gotchas.length}):`);
|
|
269
|
+
for (const g of r.gotchas.slice(0, 3)) {
|
|
270
|
+
const cond = g.triggerConditions.length > 0
|
|
271
|
+
? `fails on ${g.triggerConditions.map((t) => `${t.key}=${t.value}`).join(", ")}`
|
|
272
|
+
: g.notes.join("; ");
|
|
273
|
+
lines.push(` ⚠ ${g.approach} — ${cond} (severity ${(g.severity * 100).toFixed(0)}%)`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (r.related.length > 0) {
|
|
277
|
+
lines.push("");
|
|
278
|
+
lines.push(` RELATED (${r.related.length}):`);
|
|
279
|
+
for (const rel of r.related.slice(0, 3)) {
|
|
280
|
+
lines.push(` ~ ${(rel.similarity * 100).toFixed(0)}% match ${rel.canonical.slice(0, 80)} (n=${rel.sampleSize})`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return lines.join("\n");
|
|
284
|
+
}
|
|
285
|
+
/* ── Re-exports ─────────────────────────────────────────────────── */
|
|
286
|
+
export { normalizeProblem, similarity } from "./problem_fingerprint.js";
|
|
287
|
+
export { rankApproaches, approachKey } from "./ranking.js";
|
|
288
|
+
export { detectGotchas } from "./gotcha_detector.js";
|
|
289
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/time_crystal/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,cAAc,EAAc,SAAS,EAAE,YAAY,EAAiB,MAAM,SAAS,CAAC;AAC7F,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACxE,OAAO,EAAE,cAAc,EAAsE,MAAM,cAAc,CAAC;AAClH,OAAO,EAAE,aAAa,EAAe,MAAM,sBAAsB,CAAC;AAElE,MAAM,OAAO,GAAG,wBAAwB,CAAC;AACzC,M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AAC;AAE3D,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v2.63.0 — TIME-CRYSTAL problem fingerprinting.
|
|
3
|
+
*
|
|
4
|
+
* Two agents hitting the same problem will type it slightly differently:
|
|
5
|
+
* "Cannot find module '@types/node'"
|
|
6
|
+
* "TypeScript Error TS2307: Cannot find module @types/node"
|
|
7
|
+
* "error: cannot find module @types/node when building"
|
|
8
|
+
*
|
|
9
|
+
* All three should cluster to ONE wisdom bucket. The fingerprint is a
|
|
10
|
+
* canonical hash of the SHAPE of the problem after normalization.
|
|
11
|
+
*
|
|
12
|
+
* Pipeline:
|
|
13
|
+
* 1. Lowercase
|
|
14
|
+
* 2. Strip stack-trace prefixes (file:line:col, "at Function.foo (path)")
|
|
15
|
+
* 3. Normalize entities to slot tokens:
|
|
16
|
+
* "@types/node" → "<PKG>"
|
|
17
|
+
* "5.6.3" → "<VER>"
|
|
18
|
+
* "src/file.ts" → "<PATH>"
|
|
19
|
+
* "2026-05-26" → "<DATE>"
|
|
20
|
+
* "abc1234" → "<HASH>"
|
|
21
|
+
* numbers 3+ digits → "<N>"
|
|
22
|
+
* 4. Strip filler / TypeScript error codes (TS2307 etc.)
|
|
23
|
+
* 5. Tokenize, remove common stop-words, sort, join
|
|
24
|
+
* 6. SHA-256 → 16 hex char fingerprint
|
|
25
|
+
*
|
|
26
|
+
* The slot tokens are KEPT in the fingerprint (just normalized) so
|
|
27
|
+
* "Cannot find module @types/node" and "Cannot find module foo" share
|
|
28
|
+
* structure but NOT specifically — they collapse to the same shape.
|
|
29
|
+
*
|
|
30
|
+
* Pure deterministic.
|
|
31
|
+
*/
|
|
32
|
+
export interface NormalizationResult {
|
|
33
|
+
/** Canonical text after entity slotting + stop-word removal. */
|
|
34
|
+
canonical: string;
|
|
35
|
+
/** Stable 16-hex fingerprint hash. */
|
|
36
|
+
fingerprint: string;
|
|
37
|
+
/** Slot counts (informational). */
|
|
38
|
+
slots: Record<string, number>;
|
|
39
|
+
}
|
|
40
|
+
export declare function normalizeProblem(text: string): NormalizationResult;
|
|
41
|
+
/** Jaccard similarity between two canonical-tokens sets. */
|
|
42
|
+
export declare function similarity(a: string, b: string): number;
|
|
43
|
+
//# sourceMappingURL=problem_fingerprint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"problem_fingerprint.d.ts","sourceRoot":"","sources":["../../src/time_crystal/problem_fingerprint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AA2BH,MAAM,WAAW,mBAAmB;IAClC,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,sCAAsC;IACtC,WAAW,EAAE,MAAM,CAAC;IACpB,mCAAmC;IACnC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC/B;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,mBAAmB,CAkClE;AAED,4DAA4D;AAC5D,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAQvD"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v2.63.0 — TIME-CRYSTAL problem fingerprinting.
|
|
3
|
+
*
|
|
4
|
+
* Two agents hitting the same problem will type it slightly differently:
|
|
5
|
+
* "Cannot find module '@types/node'"
|
|
6
|
+
* "TypeScript Error TS2307: Cannot find module @types/node"
|
|
7
|
+
* "error: cannot find module @types/node when building"
|
|
8
|
+
*
|
|
9
|
+
* All three should cluster to ONE wisdom bucket. The fingerprint is a
|
|
10
|
+
* canonical hash of the SHAPE of the problem after normalization.
|
|
11
|
+
*
|
|
12
|
+
* Pipeline:
|
|
13
|
+
* 1. Lowercase
|
|
14
|
+
* 2. Strip stack-trace prefixes (file:line:col, "at Function.foo (path)")
|
|
15
|
+
* 3. Normalize entities to slot tokens:
|
|
16
|
+
* "@types/node" → "<PKG>"
|
|
17
|
+
* "5.6.3" → "<VER>"
|
|
18
|
+
* "src/file.ts" → "<PATH>"
|
|
19
|
+
* "2026-05-26" → "<DATE>"
|
|
20
|
+
* "abc1234" → "<HASH>"
|
|
21
|
+
* numbers 3+ digits → "<N>"
|
|
22
|
+
* 4. Strip filler / TypeScript error codes (TS2307 etc.)
|
|
23
|
+
* 5. Tokenize, remove common stop-words, sort, join
|
|
24
|
+
* 6. SHA-256 → 16 hex char fingerprint
|
|
25
|
+
*
|
|
26
|
+
* The slot tokens are KEPT in the fingerprint (just normalized) so
|
|
27
|
+
* "Cannot find module @types/node" and "Cannot find module foo" share
|
|
28
|
+
* structure but NOT specifically — they collapse to the same shape.
|
|
29
|
+
*
|
|
30
|
+
* Pure deterministic.
|
|
31
|
+
*/
|
|
32
|
+
import { createHash } from "node:crypto";
|
|
33
|
+
const STOP_WORDS = new Set([
|
|
34
|
+
"a", "an", "the", "and", "or", "but", "of", "to", "in", "on", "at", "by",
|
|
35
|
+
"for", "with", "from", "is", "was", "are", "were", "be", "been", "being",
|
|
36
|
+
"do", "does", "did", "doing", "have", "has", "had", "having", "will",
|
|
37
|
+
"would", "should", "could", "may", "might", "must", "this", "that",
|
|
38
|
+
"these", "those", "it", "its", "as", "if", "then", "than", "so", "such",
|
|
39
|
+
"very", "just", "only", "also", "again", "ever", "still", "yet",
|
|
40
|
+
"error", "warning", "exception", "failure", "problem", "issue",
|
|
41
|
+
]);
|
|
42
|
+
const TS_ERROR_CODE = /\bTS\d{4,5}\b/g;
|
|
43
|
+
const STACK_LINE = /^\s*at\s.+(\(.+\))?$/gm;
|
|
44
|
+
const FILE_LINE_COL = /\b[A-Za-z_][A-Za-z0-9_./\\-]*:\d+(:\d+)?\b/g;
|
|
45
|
+
const PKG_SCOPED = /@[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*/gi;
|
|
46
|
+
const PKG_BARE = /\b[a-z][a-z0-9_-]*(?:-[a-z0-9_-]+){0,3}\b(?=@\d)/gi; // pkg before @version
|
|
47
|
+
const SEMVER = /\bv?\d+\.\d+(?:\.\d+)?(?:-[a-z0-9.]+)?(?:\+[a-z0-9.]+)?\b/gi;
|
|
48
|
+
const PATH_RX = /\b[A-Za-z_][A-Za-z0-9_/\\.-]*\.(?:ts|tsx|js|jsx|cjs|mjs|py|rs|go|java|kt|swift|md|json|yaml|yml|toml|sh|sql|html|css|tex)\b/g;
|
|
49
|
+
const ISO_DATE = /\b\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?)?\b/g;
|
|
50
|
+
const SHA_HASH = /\b[a-f0-9]{7,40}\b/g;
|
|
51
|
+
const URL_RX = /https?:\/\/\S+/g;
|
|
52
|
+
const BIG_NUM = /\b\d{3,}(?:,\d{3})*\b/g;
|
|
53
|
+
const QUOTES = /[`'"]/g;
|
|
54
|
+
export function normalizeProblem(text) {
|
|
55
|
+
if (typeof text !== "string" || text.length === 0) {
|
|
56
|
+
return { canonical: "", fingerprint: "0".repeat(16), slots: {} };
|
|
57
|
+
}
|
|
58
|
+
let s = text.toLowerCase();
|
|
59
|
+
// Strip stack-trace + file:line:col patterns first.
|
|
60
|
+
s = s.replace(STACK_LINE, " ");
|
|
61
|
+
s = s.replace(URL_RX, " <URL> ");
|
|
62
|
+
s = s.replace(ISO_DATE, " <DATE> ");
|
|
63
|
+
// Order matters: scoped packages BEFORE bare-pkg-before-version BEFORE semver.
|
|
64
|
+
s = s.replace(PKG_SCOPED, " <PKG> ");
|
|
65
|
+
s = s.replace(SEMVER, " <VER> ");
|
|
66
|
+
s = s.replace(PATH_RX, " <PATH> ");
|
|
67
|
+
s = s.replace(SHA_HASH, " <HASH> ");
|
|
68
|
+
s = s.replace(FILE_LINE_COL, " <LOC> ");
|
|
69
|
+
s = s.replace(TS_ERROR_CODE, " <TSERR> ");
|
|
70
|
+
s = s.replace(BIG_NUM, " <N> ");
|
|
71
|
+
s = s.replace(QUOTES, " ");
|
|
72
|
+
// Tokenize on non-word boundaries (but keep angle brackets for slots).
|
|
73
|
+
const tokens = s.split(/[^a-z0-9_<>]+/i).map((t) => t.trim()).filter((t) => t.length > 0);
|
|
74
|
+
const filtered = tokens.filter((t) => !STOP_WORDS.has(t) && t.length >= 2);
|
|
75
|
+
// Sort for canonical form so order doesn't matter.
|
|
76
|
+
filtered.sort();
|
|
77
|
+
const canonical = filtered.join(" ");
|
|
78
|
+
// Slot counts.
|
|
79
|
+
const slots = {};
|
|
80
|
+
for (const t of filtered) {
|
|
81
|
+
if (/^<.+>$/.test(t))
|
|
82
|
+
slots[t] = (slots[t] ?? 0) + 1;
|
|
83
|
+
}
|
|
84
|
+
const fingerprint = createHash("sha256").update(canonical).digest("hex").slice(0, 16);
|
|
85
|
+
return { canonical, fingerprint, slots };
|
|
86
|
+
}
|
|
87
|
+
/** Jaccard similarity between two canonical-tokens sets. */
|
|
88
|
+
export function similarity(a, b) {
|
|
89
|
+
if (!a || !b)
|
|
90
|
+
return 0;
|
|
91
|
+
const sa = new Set(a.split(/\s+/).filter(Boolean));
|
|
92
|
+
const sb = new Set(b.split(/\s+/).filter(Boolean));
|
|
93
|
+
if (sa.size === 0 || sb.size === 0)
|
|
94
|
+
return 0;
|
|
95
|
+
let intersect = 0;
|
|
96
|
+
for (const t of sa)
|
|
97
|
+
if (sb.has(t))
|
|
98
|
+
intersect++;
|
|
99
|
+
return intersect / (sa.size + sb.size - intersect);
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=problem_fingerprint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"problem_fingerprint.js","sourceRoot":"","sources":["../../src/time_crystal/problem_fingerprint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,UAAU,GAAG,IAAI,GAA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