tribunal-kit 4.3.0 → 4.3.1

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.
Files changed (98) hide show
  1. package/.agent/scripts/case_law_manager.js +684 -684
  2. package/.agent/scripts/graph_builder.js +199 -0
  3. package/.agent/scripts/graph_zoom.js +154 -0
  4. package/.agent/skills/agent-organizer/SKILL.md +9 -1
  5. package/.agent/skills/agentic-patterns/SKILL.md +9 -1
  6. package/.agent/skills/ai-prompt-injection-defense/SKILL.md +9 -1
  7. package/.agent/skills/api-patterns/SKILL.md +206 -198
  8. package/.agent/skills/api-security-auditor/SKILL.md +9 -1
  9. package/.agent/skills/app-builder/SKILL.md +9 -1
  10. package/.agent/skills/app-builder/templates/SKILL.md +77 -69
  11. package/.agent/skills/appflow-wireframe/SKILL.md +9 -1
  12. package/.agent/skills/architecture/SKILL.md +9 -1
  13. package/.agent/skills/authentication-best-practices/SKILL.md +9 -1
  14. package/.agent/skills/bash-linux/SKILL.md +9 -1
  15. package/.agent/skills/behavioral-modes/SKILL.md +9 -1
  16. package/.agent/skills/brainstorming/SKILL.md +9 -1
  17. package/.agent/skills/building-native-ui/SKILL.md +9 -1
  18. package/.agent/skills/clean-code/SKILL.md +9 -1
  19. package/.agent/skills/code-review-checklist/SKILL.md +9 -1
  20. package/.agent/skills/config-validator/SKILL.md +9 -1
  21. package/.agent/skills/csharp-developer/SKILL.md +9 -1
  22. package/.agent/skills/data-validation-schemas/SKILL.md +287 -279
  23. package/.agent/skills/database-design/SKILL.md +199 -191
  24. package/.agent/skills/deployment-procedures/SKILL.md +9 -1
  25. package/.agent/skills/devops-engineer/SKILL.md +9 -1
  26. package/.agent/skills/devops-incident-responder/SKILL.md +9 -1
  27. package/.agent/skills/documentation-templates/SKILL.md +9 -1
  28. package/.agent/skills/edge-computing/SKILL.md +9 -1
  29. package/.agent/skills/error-resilience/SKILL.md +387 -379
  30. package/.agent/skills/extract-design-system/SKILL.md +9 -1
  31. package/.agent/skills/framer-motion-expert/SKILL.md +203 -195
  32. package/.agent/skills/frontend-design/SKILL.md +160 -152
  33. package/.agent/skills/game-design-expert/SKILL.md +9 -1
  34. package/.agent/skills/game-engineering-expert/SKILL.md +9 -1
  35. package/.agent/skills/geo-fundamentals/SKILL.md +9 -1
  36. package/.agent/skills/github-operations/SKILL.md +9 -1
  37. package/.agent/skills/gsap-core/SKILL.md +54 -46
  38. package/.agent/skills/gsap-frameworks/SKILL.md +54 -46
  39. package/.agent/skills/gsap-performance/SKILL.md +54 -46
  40. package/.agent/skills/gsap-plugins/SKILL.md +54 -46
  41. package/.agent/skills/gsap-react/SKILL.md +54 -46
  42. package/.agent/skills/gsap-scrolltrigger/SKILL.md +54 -46
  43. package/.agent/skills/gsap-timeline/SKILL.md +54 -46
  44. package/.agent/skills/gsap-utils/SKILL.md +54 -46
  45. package/.agent/skills/i18n-localization/SKILL.md +9 -1
  46. package/.agent/skills/intelligent-routing/SKILL.md +38 -30
  47. package/.agent/skills/knowledge-graph/SKILL.md +36 -0
  48. package/.agent/skills/lint-and-validate/SKILL.md +9 -1
  49. package/.agent/skills/llm-engineering/SKILL.md +9 -1
  50. package/.agent/skills/local-first/SKILL.md +9 -1
  51. package/.agent/skills/mcp-builder/SKILL.md +9 -1
  52. package/.agent/skills/mobile-design/SKILL.md +222 -214
  53. package/.agent/skills/monorepo-management/SKILL.md +293 -285
  54. package/.agent/skills/motion-engineering/SKILL.md +193 -185
  55. package/.agent/skills/nextjs-react-expert/SKILL.md +193 -185
  56. package/.agent/skills/nodejs-best-practices/SKILL.md +9 -1
  57. package/.agent/skills/observability/SKILL.md +9 -1
  58. package/.agent/skills/parallel-agents/SKILL.md +9 -1
  59. package/.agent/skills/performance-profiling/SKILL.md +9 -1
  60. package/.agent/skills/plan-writing/SKILL.md +9 -1
  61. package/.agent/skills/platform-engineer/SKILL.md +9 -1
  62. package/.agent/skills/playwright-best-practices/SKILL.md +9 -1
  63. package/.agent/skills/powershell-windows/SKILL.md +9 -1
  64. package/.agent/skills/project-idioms/SKILL.md +9 -1
  65. package/.agent/skills/python-patterns/SKILL.md +9 -1
  66. package/.agent/skills/python-pro/SKILL.md +282 -274
  67. package/.agent/skills/react-specialist/SKILL.md +236 -228
  68. package/.agent/skills/readme-builder/SKILL.md +9 -1
  69. package/.agent/skills/realtime-patterns/SKILL.md +9 -1
  70. package/.agent/skills/red-team-tactics/SKILL.md +9 -1
  71. package/.agent/skills/rust-pro/SKILL.md +9 -1
  72. package/.agent/skills/seo-fundamentals/SKILL.md +9 -1
  73. package/.agent/skills/server-management/SKILL.md +9 -1
  74. package/.agent/skills/shadcn-ui-expert/SKILL.md +9 -1
  75. package/.agent/skills/skill-creator/SKILL.md +9 -1
  76. package/.agent/skills/sql-pro/SKILL.md +9 -1
  77. package/.agent/skills/supabase-postgres-best-practices/SKILL.md +9 -1
  78. package/.agent/skills/swiftui-expert/SKILL.md +9 -1
  79. package/.agent/skills/systematic-debugging/SKILL.md +9 -1
  80. package/.agent/skills/tailwind-patterns/SKILL.md +9 -1
  81. package/.agent/skills/tdd-workflow/SKILL.md +9 -1
  82. package/.agent/skills/test-result-analyzer/SKILL.md +9 -1
  83. package/.agent/skills/testing-patterns/SKILL.md +9 -1
  84. package/.agent/skills/trend-researcher/SKILL.md +9 -1
  85. package/.agent/skills/typescript-advanced/SKILL.md +294 -286
  86. package/.agent/skills/ui-ux-pro-max/SKILL.md +9 -1
  87. package/.agent/skills/ui-ux-researcher/SKILL.md +9 -1
  88. package/.agent/skills/vue-expert/SKILL.md +234 -226
  89. package/.agent/skills/vulnerability-scanner/SKILL.md +9 -1
  90. package/.agent/skills/web-accessibility-auditor/SKILL.md +9 -1
  91. package/.agent/skills/web-design-guidelines/SKILL.md +9 -1
  92. package/.agent/skills/webapp-testing/SKILL.md +9 -1
  93. package/.agent/skills/whimsy-injector/SKILL.md +9 -1
  94. package/.agent/skills/workflow-optimizer/SKILL.md +9 -1
  95. package/README.md +242 -242
  96. package/bin/tribunal-kit.js +30 -22
  97. package/package.json +81 -80
  98. package/scripts/validate-payload.js +73 -0
@@ -1,684 +1,684 @@
1
- #!/usr/bin/env node
2
- /**
3
- * case_law_manager.js — Tribunal Kit Case Law Engine
4
- * =====================================================
5
- * Records rejected code patterns as "Cases" and surfaces them as
6
- * binding Legal Precedence during future Tribunal reviews.
7
- *
8
- * Usage:
9
- * node .agent/scripts/case_law_manager.js add-case
10
- * node .agent/scripts/case_law_manager.js search-cases --query "forEach side effects"
11
- * node .agent/scripts/case_law_manager.js list
12
- * node .agent/scripts/case_law_manager.js show --id 7
13
- * node .agent/scripts/case_law_manager.js export
14
- * node .agent/scripts/case_law_manager.js stats
15
- *
16
- * Storage:
17
- * .agent/history/case-law/index.json ← master index of all cases
18
- * .agent/history/case-law/cases/ ← one JSON file per case
19
- */
20
-
21
- 'use strict';
22
-
23
- const fs = require('fs');
24
- const path = require('path');
25
- const crypto = require('crypto');
26
- const readline = require('readline');
27
-
28
- // ── Colours ──────────────────────────────────────────────────────────────────
29
- const GREEN = '\x1b[92m';
30
- const YELLOW = '\x1b[93m';
31
- const CYAN = '\x1b[96m';
32
- const RED = '\x1b[91m';
33
- const BLUE = '\x1b[94m';
34
- const BOLD = '\x1b[1m';
35
- const DIM = '\x1b[2m';
36
- const RESET = '\x1b[0m';
37
-
38
- // ── Find .agent directory ─────────────────────────────────────────────────────
39
- function findAgentDir() {
40
- let current = path.resolve(process.cwd());
41
- const root = path.parse(current).root;
42
- while (current !== root) {
43
- const candidate = path.join(current, '.agent');
44
- if (fs.existsSync(candidate) && fs.statSync(candidate).isDirectory()) return candidate;
45
- current = path.dirname(current);
46
- }
47
- console.error(`${RED}✖ Error: '.agent' directory not found. Please run 'npx tribunal-kit init' first.${RESET}`);
48
- process.exit(1);
49
- }
50
-
51
- const AGENT_DIR = findAgentDir();
52
- const HISTORY_DIR = path.join(AGENT_DIR, 'history', 'case-law');
53
- const CASES_DIR = path.join(HISTORY_DIR, 'cases');
54
- const INDEX_FILE = path.join(HISTORY_DIR, 'index.json');
55
-
56
- const VALID_DOMAINS = new Set(['backend', 'frontend', 'database', 'security', 'performance', 'mobile', 'testing', 'devops', 'general']);
57
- const VALID_VERDICTS = new Set(['REJECTED', 'APPROVED_WITH_CONDITIONS', 'PRECEDENT_SET', 'OVERRULED']);
58
-
59
- // ── Noise filter ────────────────────────────────────────────────────────────
60
- const NOISE_PATTERNS = [
61
- /\bformatting\b/i, /\bwhitespace\b/i, /\bindent(ation)?\b/i,
62
- /\bimport\s+order\b/i, /\btrailing\s+(comma|space|whitespace)\b/i,
63
- /\bsemicolon\b/i, /\bprettier\b/i, /\beslint.*fix\b/i, /\blint.*only\b/i,
64
- ];
65
-
66
- function isNoiseRejection(reason) {
67
- const lower = reason.toLowerCase();
68
- return NOISE_PATTERNS.some(p => p.test(lower));
69
- }
70
-
71
- // ── Trivial-change filter (Semantic Delta) ────────────────────────────────────
72
- const TRIVIAL_PATTERNS = [
73
- /^\s*$/, // blank lines
74
- /^\s*\/\/.*$/, // comment-only lines
75
- /^\s*#.*$/, // python comments
76
- /^\s*\*.*$/, // JSDoc lines
77
- /^\s*(import\s+\{[^}]+\}|from\s+['"])/, // import reorders
78
- ];
79
-
80
- function isTrivialLine(line) {
81
- return TRIVIAL_PATTERNS.some(p => p.test(line));
82
- }
83
-
84
- function semanticDelta(diffText) {
85
- const lines = diffText.split('\n');
86
- const meaningful = [];
87
- for (const line of lines) {
88
- if (line.startsWith('+++') || line.startsWith('---') || line.startsWith('@@')) {
89
- meaningful.push(line);
90
- continue;
91
- }
92
- if (line.startsWith('+') || line.startsWith('-')) {
93
- const codePart = line.slice(1);
94
- if (!isTrivialLine(codePart)) meaningful.push(line);
95
- } else {
96
- meaningful.push(line);
97
- }
98
- }
99
- let filtered = meaningful.join('\n');
100
- filtered = filtered.replace(/(\n[ ]?\n){3,}/g, '\n\n');
101
- return filtered.trim();
102
- }
103
-
104
- function contentHash(text) {
105
- const cleaned = semanticDelta(text);
106
- return crypto.createHash('sha256').update(cleaned).digest('hex').slice(0, 8);
107
- }
108
-
109
- // ── Index helpers ─────────────────────────────────────────────────────────────
110
- function ensureDirs() {
111
- fs.mkdirSync(HISTORY_DIR, { recursive: true });
112
- fs.mkdirSync(CASES_DIR, { recursive: true });
113
- }
114
-
115
- function loadIndex() {
116
- ensureDirs();
117
- if (fs.existsSync(INDEX_FILE)) {
118
- try { return JSON.parse(fs.readFileSync(INDEX_FILE, 'utf8')); } catch { /* fallthrough */ }
119
- }
120
- return { version: '1.0', cases: [], next_id: 1 };
121
- }
122
-
123
- function saveIndex(index) {
124
- ensureDirs();
125
- const tmp = INDEX_FILE + '.tmp';
126
- fs.writeFileSync(tmp, JSON.stringify(index, null, 2), 'utf8');
127
- fs.renameSync(tmp, INDEX_FILE);
128
- }
129
-
130
- function loadCase(caseId) {
131
- const p = path.join(CASES_DIR, `case-${String(caseId).padStart(4, '0')}.json`);
132
- if (!fs.existsSync(p)) return null;
133
- try { return JSON.parse(fs.readFileSync(p, 'utf8')); } catch { return null; }
134
- }
135
-
136
- function saveCase(caseRecord) {
137
- const p = path.join(CASES_DIR, `case-${String(caseRecord.id).padStart(4, '0')}.json`);
138
- fs.writeFileSync(p, JSON.stringify(caseRecord, null, 2), 'utf8');
139
- }
140
-
141
- // ── Keyword/tag extraction ─────────────────────────────────────────────────────
142
- function extractTags(text) {
143
- const tokens = text.match(/\b[a-zA-Z_][a-zA-Z0-9_]{2,}\b/g) || [];
144
- const stopWords = new Set([
145
- 'the', 'and', 'for', 'was', 'this', 'with', 'that',
146
- 'from', 'are', 'not', 'use', 'but', 'also', 'code',
147
- 'have', 'will', 'should', 'must', 'can', 'may', 'any',
148
- 'all', 'new', 'old', 'add', 'get', 'set', 'var', 'let',
149
- 'const', 'function', 'return', 'import', 'export', 'class',
150
- 'async', 'await', 'true', 'false', 'null', 'undefined',
151
- ]);
152
- const seen = new Set();
153
- const tags = [];
154
- for (const token of tokens) {
155
- const lower = token.toLowerCase();
156
- if (!stopWords.has(lower) && !seen.has(lower)) {
157
- seen.add(lower);
158
- tags.push(lower);
159
- }
160
- if (tags.length >= 20) break;
161
- }
162
- return tags;
163
- }
164
-
165
- // ── Version-Aware Case Filtering ──────────────────────────────────────────────
166
- // FIX: Prevents stale cases (e.g., React 17 rejections) from blocking valid code
167
- // once the project has upgraded frameworks. Uses simple numeric comparison.
168
- //
169
- // Version filter string format: "react=19,node=22,next=15"
170
- // Case stack_version format: "react>=18,node>=20" (>=, >, =, <, <=)
171
-
172
- /**
173
- * Parse a version filter string into a map of { lib -> number }.
174
- * Input: "react=19,node=22"
175
- * Output: { react: 19, node: 22 }
176
- * @param {string} filterStr
177
- * @returns {Object<string, number>}
178
- */
179
- function parseVersionFilter(filterStr) {
180
- const result = {};
181
- if (!filterStr) return result;
182
- for (const segment of filterStr.split(',')) {
183
- const m = segment.trim().match(/^([a-zA-Z0-9_.-]+)\s*(?:>=|>|<=|<|=)?\s*(\d+(?:\.\d+)?)/);
184
- if (m) result[m[1].toLowerCase()] = parseFloat(m[2]);
185
- }
186
- return result;
187
- }
188
-
189
- /**
190
- * Returns true if the case either has no stack_version constraint,
191
- * OR all of its version constraints are satisfied by the provided filter.
192
- *
193
- * A case with stack_version "react>=18,node>=20" will be SKIPPED (return false)
194
- * when the version filter says react=19,node=22 → both constraints met → case IS relevant.
195
- *
196
- * The logic: if a case's constraint is NOT met by the current version filter,
197
- * that case is from a different version context → skip it.
198
- *
199
- * @param {{ stack_version?: string }} caseEntry
200
- * @param {Object<string, number>} versionFilter
201
- * @returns {boolean} true = case is eligible for this version context
202
- */
203
- function caseMatchesVersionFilter(caseEntry, versionFilter) {
204
- if (!caseEntry.stack_version) return true; // No constraint → always eligible
205
- if (!versionFilter || !Object.keys(versionFilter).length) return true;
206
-
207
- // Parse the case's own version constraint
208
- const caseConstraints = [];
209
- for (const segment of caseEntry.stack_version.split(',')) {
210
- const m = segment.trim().match(/^([a-zA-Z0-9_.-]+)\s*(>=|>|<=|<|=)\s*(\d+(?:\.\d+)?)/);
211
- if (m) caseConstraints.push({ lib: m[1].toLowerCase(), op: m[2], ver: parseFloat(m[3]) });
212
- }
213
-
214
- // Check each constraint against the version filter
215
- for (const { lib, op, ver } of caseConstraints) {
216
- const projectVer = versionFilter[lib];
217
- if (projectVer === undefined) continue; // Unknown lib → don't filter on it
218
-
219
- const satisfied = (
220
- op === '>=' ? projectVer >= ver :
221
- op === '>' ? projectVer > ver :
222
- op === '<=' ? projectVer <= ver :
223
- op === '<' ? projectVer < ver :
224
- /* = */ projectVer === ver
225
- );
226
- if (!satisfied) return false; // This case's version context doesn't match
227
- }
228
- return true;
229
- }
230
-
231
- // ── Similarity scoring (TF-IDF Cosine — token-free) ──────────────────────────
232
-
233
- function buildIdf(corpus) {
234
- const n = corpus.length;
235
- if (n === 0) return {};
236
- const docFreq = {};
237
- for (const tags of corpus) {
238
- const unique = new Set(tags);
239
- for (const tag of unique) {
240
- docFreq[tag] = (docFreq[tag] || 0) + 1;
241
- }
242
- }
243
- const idf = {};
244
- for (const [term, df] of Object.entries(docFreq)) {
245
- idf[term] = Math.log((n + 1) / (df + 1)) + 1.0;
246
- }
247
- return idf;
248
- }
249
-
250
- function tfidfCosineSimilarity(queryTags, caseTags, idf) {
251
- if (!queryTags.length || !caseTags.length) return 0.0;
252
- const tfQ = {};
253
- for (const t of queryTags) tfQ[t] = (tfQ[t] || 0) + 1;
254
- const tfC = {};
255
- for (const t of caseTags) tfC[t] = (tfC[t] || 0) + 1;
256
- const allTerms = new Set([...Object.keys(tfQ), ...Object.keys(tfC)]);
257
- let dot = 0, magQ = 0, magC = 0;
258
- for (const term of allTerms) {
259
- const wQ = (tfQ[term] || 0) * (idf[term] || 1.0);
260
- const wC = (tfC[term] || 0) * (idf[term] || 1.0);
261
- dot += wQ * wC;
262
- magQ += wQ * wQ;
263
- magC += wC * wC;
264
- }
265
- if (magQ === 0 || magC === 0) return 0.0;
266
- return dot / (Math.sqrt(magQ) * Math.sqrt(magC));
267
- }
268
-
269
- // ── Input helpers ─────────────────────────────────────────────────────────────
270
- function createRl() {
271
- return readline.createInterface({ input: process.stdin, output: process.stdout });
272
- }
273
-
274
- function ask(rl, prompt) {
275
- return new Promise(resolve => rl.question(` ${BOLD}${prompt}${RESET} `, resolve));
276
- }
277
-
278
- function askMultiline(rl, prompt, sentinel) {
279
- return new Promise(resolve => {
280
- console.log(` ${BOLD}${prompt}${RESET}`);
281
- console.log(` ${DIM}(Type or paste content. Type '${sentinel}' on its own line when done.)${RESET}`);
282
- const lines = [];
283
- const listener = (line) => {
284
- if (line.trim() === sentinel) {
285
- rl.removeListener('line', listener);
286
- resolve(lines.join('\n'));
287
- } else {
288
- lines.push(line);
289
- }
290
- };
291
- rl.on('line', listener);
292
- });
293
- }
294
-
295
- function askChoice(rl, label, choices, defaultVal) {
296
- return new Promise(resolve => {
297
- const opts = choices.map(c => c === defaultVal ? `${BOLD}${c}${RESET}` : c).join(' / ');
298
- rl.question(` ${BOLD}${label}${RESET} [${opts}] (default: ${defaultVal}): `, answer => {
299
- const val = (answer || '').trim().toLowerCase();
300
- resolve(val && choices.includes(val) ? val : defaultVal);
301
- });
302
- });
303
- }
304
-
305
- // ── Commands ──────────────────────────────────────────────────────────────────
306
- async function cmdAddCase() {
307
- console.log(`\n${BOLD}${CYAN}━━━ Recording New Case ━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
308
- const rl = createRl();
309
-
310
- const diffText = await askMultiline(rl, 'Paste the REJECTED diff (code snippet):', 'END_DIFF');
311
- if (!diffText.trim()) { console.log(`${RED}✖ Diff cannot be empty. Aborting.${RESET}`); rl.close(); process.exit(1); }
312
-
313
- const reason = await ask(rl, 'Rejection reason (1-2 sentences):');
314
- if (!reason.trim()) { console.log(`${RED}✖ Reason cannot be empty. Aborting.${RESET}`); rl.close(); process.exit(1); }
315
-
316
- const domain = await askChoice(rl, 'Domain', [...VALID_DOMAINS].sort(), 'general');
317
- const verdict = await askChoice(rl, 'Verdict', [...VALID_VERDICTS].sort(), 'REJECTED');
318
- const prRef = (await ask(rl, 'PR / commit reference (optional, e.g. PR-404):')).trim() || null;
319
- const reviewer = (await ask(rl, 'Reviewer agent (optional, e.g. security-auditor):')).trim() || null;
320
-
321
- // FIX: stack_version — prevents stale cases from blocking valid code after
322
- // framework upgrades. Format: "react>=19, node>=20" or leave blank for all.
323
- const stackVersionRaw = (await ask(rl, 'Stack version constraint (optional, e.g. react>=18, node>=20):')).trim();
324
- const stackVersion = stackVersionRaw || null;
325
- rl.close();
326
-
327
- const delta = semanticDelta(diffText);
328
- const fingerprint = contentHash(diffText);
329
- const tags = extractTags(diffText + ' ' + reason);
330
-
331
- const index = loadIndex();
332
- const caseId = index.next_id;
333
- const now = new Date().toISOString().slice(0, 19);
334
-
335
- const caseRecord = {
336
- id: caseId, fingerprint, timestamp: now, domain, verdict,
337
- reason: reason.trim(), pr_ref: prRef, reviewer, tags,
338
- stack_version: stackVersion,
339
- diff_raw: diffText.trim(), diff_delta: delta,
340
- };
341
-
342
- saveCase(caseRecord);
343
- index.cases.push({
344
- id: caseId, fingerprint, domain, verdict, tags,
345
- timestamp: now, reason_summary: reason.trim().slice(0, 120),
346
- stack_version: stackVersion,
347
- });
348
- index.next_id = caseId + 1;
349
- saveIndex(index);
350
-
351
- console.log(`\n${GREEN}✔ Case #${String(caseId).padStart(4, '0')} recorded${RESET}`);
352
- console.log(` ${DIM}Fingerprint : ${fingerprint}${RESET}`);
353
- console.log(` ${DIM}Domain : ${domain}${RESET}`);
354
- console.log(` ${DIM}Tags : ${tags.slice(0, 8).join(', ')}${RESET}`);
355
- if (stackVersion) console.log(` ${DIM}Stack version : ${stackVersion}${RESET}`);
356
- console.log();
357
- }
358
-
359
- function cmdSearchCases(args) {
360
- let query = args.filter(a => !a.startsWith('--')).join(' ');
361
- if (!query) {
362
- const qi = process.argv.indexOf('--query');
363
- if (qi !== -1) query = process.argv.slice(qi + 1).filter(a => !a.startsWith('--')).join(' ');
364
- }
365
- if (!query) { console.log(`${RED}✖ Provide a search query: search-cases --query "forEach side effects"${RESET}`); process.exit(1); }
366
-
367
- // FIX: --version-filter skips cases whose stack_version constraint doesn't
368
- // match the specified versions. Prevents stale React 17/18 cases blocking React 19 code.
369
- // Usage: search-cases "useEffect" --version-filter "react=19,node=22"
370
- const vfIdx = args.indexOf('--version-filter');
371
- const versionFilter = vfIdx !== -1 && args[vfIdx + 1]
372
- ? parseVersionFilter(args[vfIdx + 1])
373
- : null;
374
-
375
- const queryTags = extractTags(query);
376
- const index = loadIndex();
377
- if (!index.cases.length) { console.log(`${YELLOW}No cases recorded yet. Use 'add-case' to record your first rejection.${RESET}`); return; }
378
-
379
- // Apply version filter before scoring
380
- const eligibleCases = versionFilter
381
- ? index.cases.filter(e => caseMatchesVersionFilter(e, versionFilter))
382
- : index.cases;
383
-
384
- const skipped = index.cases.length - eligibleCases.length;
385
-
386
- const corpus = eligibleCases.map(e => e.tags || []);
387
- const idf = buildIdf(corpus);
388
-
389
- const scored = [];
390
- for (const entry of eligibleCases) {
391
- const score = tfidfCosineSimilarity(queryTags, entry.tags || [], idf);
392
- if (score > 0.0) scored.push({ score, entry });
393
- }
394
- scored.sort((a, b) => b.score - a.score);
395
- const top = scored.slice(0, 5);
396
-
397
- if (!top.length) {
398
- console.log(`${YELLOW}No matching cases found for: "${query}"${RESET}`);
399
- if (skipped > 0) console.log(` ${DIM}${skipped} case(s) skipped by version filter: ${args[vfIdx + 1]}${RESET}`);
400
- console.log(` ${DIM}Try broader terms or check 'list' for available cases.${RESET}`);
401
- return;
402
- }
403
-
404
- console.log(`\n${BOLD}${CYAN}━━━ Case Law Search Results ━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
405
- console.log(` Query : ${BOLD}${query}${RESET}`);
406
- console.log(` Matches: ${top.length} of ${index.cases.length} cases` +
407
- (skipped > 0 ? ` ${DIM}(${skipped} skipped by version filter)${RESET}` : '') + '\n');
408
-
409
- for (const { score, entry } of top) {
410
- const vc = entry.verdict === 'REJECTED' ? RED : YELLOW;
411
- console.log(` ${BOLD}Case #${String(entry.id).padStart(4, '0')}${RESET} ${vc}[${entry.verdict}]${RESET} ${DIM}${(entry.timestamp || '').slice(0, 10)}${RESET} score=${score.toFixed(2)}`);
412
- console.log(` ${DIM}Domain: ${entry.domain}${RESET}`);
413
- console.log(` ${entry.reason_summary}`);
414
- console.log(` ${DIM}Tags: ${(entry.tags || []).slice(0, 8).join(', ')}${RESET}\n`);
415
- }
416
- console.log(` ${DIM}Run 'show --id <N>' to see the full diff for any case.${RESET}`);
417
- console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
418
- }
419
-
420
- function cmdList(args) {
421
- const index = loadIndex();
422
- const cases = index.cases || [];
423
- if (!cases.length) { console.log(`${YELLOW}No cases recorded yet.${RESET}`); return; }
424
-
425
- let domainFilter = null;
426
- const di = args.indexOf('--domain');
427
- if (di !== -1 && args[di + 1]) domainFilter = args[di + 1].toLowerCase();
428
-
429
- const filtered = domainFilter ? cases.filter(c => c.domain === domainFilter) : cases;
430
- const total = filtered.length;
431
-
432
- console.log(`\n${BOLD}${CYAN}━━━ Case Law Index (${total} cases) ━━━━━━━━━━━━━━━━━━━━${RESET}`);
433
- if (domainFilter) console.log(` ${DIM}Filtered by domain: ${domainFilter}${RESET}\n`);
434
-
435
- const last20 = filtered.slice(-20).reverse();
436
- for (const entry of last20) {
437
- const vc = entry.verdict === 'REJECTED' ? RED : YELLOW;
438
- console.log(` ${BOLD}#${String(entry.id).padStart(4, '0')}${RESET} ${vc}[${entry.verdict}]${RESET} ${DIM}${(entry.domain || '').toUpperCase()}${RESET} ${(entry.timestamp || '').slice(0, 10)}`);
439
- console.log(` ${(entry.reason_summary || '').slice(0, 80)}`);
440
- }
441
- if (total > 20) console.log(`\n ${YELLOW}... showing last 20 of ${total}. Use 'export' for full history.${RESET}`);
442
- console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
443
- }
444
-
445
- function cmdShow(args) {
446
- let caseId = null;
447
- const ii = args.indexOf('--id');
448
- if (ii !== -1 && args[ii + 1]) caseId = parseInt(args[ii + 1], 10);
449
- if (caseId == null || isNaN(caseId)) { console.log(`${RED}✖ Provide a case ID: show --id 7${RESET}`); process.exit(1); }
450
-
451
- const caseRecord = loadCase(caseId);
452
- if (!caseRecord) { console.log(`${RED}✖ Case #${String(caseId).padStart(4, '0')} not found.${RESET}`); process.exit(1); }
453
-
454
- const vc = caseRecord.verdict === 'REJECTED' ? RED : YELLOW;
455
- console.log(`\n${BOLD}${CYAN}━━━ Case #${String(caseRecord.id).padStart(4, '0')} ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
456
- console.log(` Verdict : ${vc}${BOLD}${caseRecord.verdict}${RESET}`);
457
- console.log(` Domain : ${caseRecord.domain}`);
458
- console.log(` Recorded : ${caseRecord.timestamp}`);
459
- if (caseRecord.pr_ref) console.log(` PR / Ref : ${caseRecord.pr_ref}`);
460
- if (caseRecord.reviewer) console.log(` Reviewer : ${caseRecord.reviewer}`);
461
- console.log(`\n ${BOLD}Reason:${RESET}`);
462
- console.log(` ${caseRecord.reason}`);
463
- console.log(`\n ${BOLD}Semantic Delta (meaningful changes only):${RESET}`);
464
- console.log(` ${DIM}─────────────────────────────────────────${RESET}`);
465
- const deltaLines = (caseRecord.diff_delta || caseRecord.diff_raw).split('\n').slice(0, 40);
466
- for (const line of deltaLines) {
467
- if (line.startsWith('+')) console.log(` ${GREEN}${line}${RESET}`);
468
- else if (line.startsWith('-')) console.log(` ${RED}${line}${RESET}`);
469
- else console.log(` ${DIM}${line}${RESET}`);
470
- }
471
- console.log(`\n ${BOLD}Tags:${RESET} ${(caseRecord.tags || []).join(', ')}`);
472
- console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
473
- }
474
-
475
- function cmdExport(args) {
476
- const toStdout = args.includes('--stdout');
477
- const index = loadIndex();
478
- const cases = index.cases || [];
479
- if (!cases.length) { console.log(`${YELLOW}No cases to export.${RESET}`); return; }
480
-
481
- const now = new Date().toISOString().slice(0, 19);
482
- const lines = [
483
- '# Tribunal Case Law — Full Export\n',
484
- `Generated: ${now}`, `Total Cases: ${cases.length}\n`, '---\n',
485
- ];
486
- for (const entry of cases) {
487
- const caseRecord = loadCase(entry.id) || entry;
488
- const badge = `[${caseRecord.verdict || 'REJECTED'}]`;
489
- lines.push(`## Case #${String(entry.id).padStart(4, '0')} ${badge}`);
490
- lines.push(`**Domain:** ${entry.domain} `);
491
- lines.push(`**Recorded:** ${(entry.timestamp || '').slice(0, 10)} `);
492
- if (caseRecord.pr_ref) lines.push(`**PR/Ref:** ${caseRecord.pr_ref} `);
493
- lines.push(`\n**Reason:** ${entry.reason_summary}\n`);
494
- lines.push(`**Tags:** \`${(entry.tags || []).slice(0, 8).join(', ')}\`\n`);
495
- lines.push('---\n');
496
- }
497
- const content = lines.join('\n');
498
- if (toStdout) { console.log(content); return; }
499
-
500
- const outPath = path.join(HISTORY_DIR, 'case-law-export.md');
501
- fs.writeFileSync(outPath, content, 'utf8');
502
- console.log(`${GREEN}✔ Exported ${cases.length} cases to ${outPath}${RESET}`);
503
- }
504
-
505
- function cmdStats() {
506
- const index = loadIndex();
507
- const cases = index.cases || [];
508
- const domainCounts = {};
509
- const verdictCounts = {};
510
- for (const c of cases) {
511
- domainCounts[c.domain] = (domainCounts[c.domain] || 0) + 1;
512
- verdictCounts[c.verdict] = (verdictCounts[c.verdict] || 0) + 1;
513
- }
514
-
515
- console.log(`\n${BOLD}${CYAN}━━━ Case Law Statistics ━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
516
- console.log(` Total cases: ${BOLD}${cases.length}${RESET}`);
517
- console.log(`\n ${BOLD}By Verdict:${RESET}`);
518
- for (const v of Object.keys(verdictCounts).sort()) {
519
- const color = v === 'REJECTED' ? RED : YELLOW;
520
- console.log(` ${color}${v.padEnd(30)}${RESET} ${verdictCounts[v]}`);
521
- }
522
- console.log(`\n ${BOLD}By Domain:${RESET}`);
523
- for (const [d, c] of Object.entries(domainCounts).sort((a, b) => b[1] - a[1])) {
524
- console.log(` ${CYAN}${d.padEnd(20)}${RESET} ${c}`);
525
- }
526
- console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
527
- }
528
-
529
- function cmdAutoRecord() {
530
- function getFlag(name) {
531
- const flag = `--${name}`;
532
- const idx = process.argv.indexOf(flag);
533
- return (idx !== -1 && process.argv[idx + 1]) ? process.argv[idx + 1] : '';
534
- }
535
-
536
- const diffText = getFlag('diff');
537
- const reason = getFlag('reason');
538
- let domain = getFlag('domain') || 'general';
539
- let verdict = getFlag('verdict') || 'REJECTED';
540
- const reviewer = getFlag('reviewer') || null;
541
- const prRef = getFlag('pr-ref') || null;
542
- // FIX: --stack-version persists version context with the case record so
543
- // future searches can skip it when the project has moved past that version.
544
- // Example: --stack-version "react=18,next=14"
545
- const stackVersion = getFlag('stack-version') || null;
546
-
547
- if (!diffText || !reason) {
548
- console.log(`${RED}✖ auto-record requires --diff and --reason flags.${RESET}`);
549
- console.log(` Usage: auto-record --diff "code" --reason "why" --domain security --reviewer agent-name --stack-version "react=18"`);
550
- process.exit(1);
551
- }
552
-
553
- if (isNoiseRejection(reason)) { console.log(`${DIM}⊘ Skipped: trivial rejection (noise filter matched).${RESET}`); return; }
554
- if (!VALID_DOMAINS.has(domain)) domain = 'general';
555
- if (!VALID_VERDICTS.has(verdict)) verdict = 'REJECTED';
556
-
557
- const fingerprint = contentHash(diffText);
558
- const index = loadIndex();
559
- for (const existing of index.cases) {
560
- if (existing.fingerprint === fingerprint) {
561
- console.log(`${YELLOW}⊘ Duplicate: Case #${String(existing.id).padStart(4, '0')} already records this pattern.${RESET}`);
562
- return;
563
- }
564
- }
565
-
566
- const delta = semanticDelta(diffText);
567
- const tags = extractTags(diffText + ' ' + reason);
568
- const caseId = index.next_id;
569
- const now = new Date().toISOString().slice(0, 19);
570
-
571
- const caseRecord = {
572
- id: caseId, fingerprint, timestamp: now, domain, verdict,
573
- reason: reason.trim(), pr_ref: prRef, reviewer, tags,
574
- stack_version: stackVersion,
575
- diff_raw: diffText.trim(), diff_delta: delta, auto_recorded: true,
576
- };
577
-
578
- saveCase(caseRecord);
579
- index.cases.push({
580
- id: caseId, fingerprint, domain, verdict, tags,
581
- timestamp: now, reason_summary: reason.trim().slice(0, 120),
582
- stack_version: stackVersion,
583
- });
584
- index.next_id = caseId + 1;
585
- saveIndex(index);
586
- console.log(`${GREEN}✔ Auto-recorded Case #${String(caseId).padStart(4, '0')}${RESET} [${verdict}] domain=${domain}`);
587
- console.log(` ${DIM}Reason: ${reason.slice(0, 80)}${RESET}`);
588
- if (stackVersion) console.log(` ${DIM}Stack version: ${stackVersion}${RESET}`);
589
- }
590
-
591
- async function cmdOverrule(args) {
592
- let caseId = null;
593
- const ii = args.indexOf('--id');
594
- if (ii !== -1 && args[ii + 1]) caseId = parseInt(args[ii + 1], 10);
595
- if (caseId == null || isNaN(caseId)) { console.log(`${RED}✖ Provide a case ID: overrule --id 7${RESET}`); process.exit(1); }
596
-
597
- const caseRecord = loadCase(caseId);
598
- if (!caseRecord) { console.log(`${RED}✖ Case #${String(caseId).padStart(4, '0')} not found.${RESET}`); process.exit(1); }
599
- if (caseRecord.verdict === 'OVERRULED') { console.log(`${YELLOW}Case #${String(caseId).padStart(4, '0')} is already OVERRULED.${RESET}`); return; }
600
-
601
- let reason = null;
602
- const ri = args.indexOf('--reason');
603
- if (ri !== -1 && args[ri + 1]) reason = args[ri + 1];
604
-
605
- if (!reason) {
606
- const rl = createRl();
607
- reason = await ask(rl, 'Reason for overruling this precedent:');
608
- rl.close();
609
- }
610
- if (!reason || !reason.trim()) { console.log(`${RED}✖ An overrule reason is required.${RESET}`); process.exit(1); }
611
-
612
- const oldVerdict = caseRecord.verdict;
613
- caseRecord.verdict = 'OVERRULED';
614
- caseRecord.overruled_at = new Date().toISOString().slice(0, 19);
615
- caseRecord.overrule_reason = reason.trim();
616
- caseRecord.previous_verdict = oldVerdict;
617
- saveCase(caseRecord);
618
-
619
- const index = loadIndex();
620
- for (const entry of index.cases) {
621
- if (entry.id === caseId) { entry.verdict = 'OVERRULED'; break; }
622
- }
623
- saveIndex(index);
624
-
625
- console.log(`\n${GREEN}✔ Case #${String(caseId).padStart(4, '0')} OVERRULED${RESET}`);
626
- console.log(` ${DIM}Previous verdict : ${oldVerdict}${RESET}`);
627
- console.log(` ${DIM}Overrule reason : ${reason.trim()}${RESET}`);
628
- console.log(` ${DIM}The case is preserved in history but no longer blocks reviews.${RESET}\n`);
629
- }
630
-
631
- // ── Main ──────────────────────────────────────────────────────────────────────
632
- const COMMANDS = {
633
- 'add-case': cmdAddCase,
634
- 'auto-record': cmdAutoRecord,
635
- 'search-cases': cmdSearchCases,
636
- 'list': cmdList,
637
- 'show': cmdShow,
638
- 'overrule': cmdOverrule,
639
- 'export': cmdExport,
640
- 'stats': cmdStats,
641
- };
642
-
643
- async function main() {
644
- const argv = process.argv.slice(2);
645
- if (!argv.length || ['-h', '--help', 'help'].includes(argv[0])) {
646
- console.log(`
647
- ${BOLD}case_law_manager.js${RESET} — Tribunal Case Law Engine
648
-
649
- ${BOLD}Commands:${RESET}
650
- add-case Record a new rejected pattern (interactive)
651
- auto-record --diff --reason Record a rejection (non-interactive, for AI agents)
652
- search-cases --query <text> Find relevant precedents (TF-IDF cosine, token-free)
653
- list [--domain <domain>] List all recorded cases
654
- show --id <N> Show full diff for a case
655
- overrule --id <N> Formally overrule a past precedent
656
- export [--stdout] Export all cases to Markdown
657
- stats Show breakdown by domain/verdict
658
-
659
- ${BOLD}Domains:${RESET} ${[...VALID_DOMAINS].sort().join(', ')}
660
- ${BOLD}Verdicts:${RESET} ${[...VALID_VERDICTS].sort().join(', ')}
661
- `);
662
- return;
663
- }
664
-
665
- const cmd = argv[0];
666
- const rest = argv.slice(1);
667
- if (!COMMANDS[cmd]) {
668
- console.log(`${RED}✖ Unknown command: '${cmd}'${RESET}`);
669
- console.log(` Valid: ${Object.keys(COMMANDS).join(', ')}`);
670
- process.exit(1);
671
- }
672
- await COMMANDS[cmd](rest);
673
- }
674
-
675
- // ── Exports ──────────────────────────────────────────────────────────────────
676
- module.exports = {
677
- contentHash, semanticDelta, extractTags, loadIndex, saveIndex,
678
- loadCase, saveCase, tfidfCosineSimilarity, buildIdf, findAgentDir,
679
- isNoiseRejection, isTrivialLine,
680
- };
681
-
682
- if (require.main === module) {
683
- main().catch(err => { console.error(err); process.exit(1); });
684
- }
1
+ #!/usr/bin/env node
2
+ /**
3
+ * case_law_manager.js — Tribunal Kit Case Law Engine
4
+ * =====================================================
5
+ * Records rejected code patterns as "Cases" and surfaces them as
6
+ * binding Legal Precedence during future Tribunal reviews.
7
+ *
8
+ * Usage:
9
+ * node .agent/scripts/case_law_manager.js add-case
10
+ * node .agent/scripts/case_law_manager.js search-cases --query "forEach side effects"
11
+ * node .agent/scripts/case_law_manager.js list
12
+ * node .agent/scripts/case_law_manager.js show --id 7
13
+ * node .agent/scripts/case_law_manager.js export
14
+ * node .agent/scripts/case_law_manager.js stats
15
+ *
16
+ * Storage:
17
+ * .agent/history/case-law/index.json ← master index of all cases
18
+ * .agent/history/case-law/cases/ ← one JSON file per case
19
+ */
20
+
21
+ 'use strict';
22
+
23
+ const fs = require('fs');
24
+ const path = require('path');
25
+ const crypto = require('crypto');
26
+ const readline = require('readline');
27
+
28
+ // ── Colours ──────────────────────────────────────────────────────────────────
29
+ const GREEN = '\x1b[92m';
30
+ const YELLOW = '\x1b[93m';
31
+ const CYAN = '\x1b[96m';
32
+ const RED = '\x1b[91m';
33
+ const BLUE = '\x1b[94m';
34
+ const BOLD = '\x1b[1m';
35
+ const DIM = '\x1b[2m';
36
+ const RESET = '\x1b[0m';
37
+
38
+ // ── Find .agent directory ─────────────────────────────────────────────────────
39
+ function findAgentDir() {
40
+ let current = path.resolve(process.cwd());
41
+ const root = path.parse(current).root;
42
+ while (current !== root) {
43
+ const candidate = path.join(current, '.agent');
44
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isDirectory()) return candidate;
45
+ current = path.dirname(current);
46
+ }
47
+ console.error(`${RED}✖ Error: '.agent' directory not found. Please run 'npx tribunal-kit init' first.${RESET}`);
48
+ process.exit(1);
49
+ }
50
+
51
+ const AGENT_DIR = findAgentDir();
52
+ const HISTORY_DIR = path.join(AGENT_DIR, 'history', 'case-law');
53
+ const CASES_DIR = path.join(HISTORY_DIR, 'cases');
54
+ const INDEX_FILE = path.join(HISTORY_DIR, 'index.json');
55
+
56
+ const VALID_DOMAINS = new Set(['backend', 'frontend', 'database', 'security', 'performance', 'mobile', 'testing', 'devops', 'general']);
57
+ const VALID_VERDICTS = new Set(['REJECTED', 'APPROVED_WITH_CONDITIONS', 'PRECEDENT_SET', 'OVERRULED']);
58
+
59
+ // ── Noise filter ────────────────────────────────────────────────────────────
60
+ const NOISE_PATTERNS = [
61
+ /\bformatting\b/i, /\bwhitespace\b/i, /\bindent(ation)?\b/i,
62
+ /\bimport\s+order\b/i, /\btrailing\s+(comma|space|whitespace)\b/i,
63
+ /\bsemicolon\b/i, /\bprettier\b/i, /\beslint.*fix\b/i, /\blint.*only\b/i,
64
+ ];
65
+
66
+ function isNoiseRejection(reason) {
67
+ const lower = reason.toLowerCase();
68
+ return NOISE_PATTERNS.some(p => p.test(lower));
69
+ }
70
+
71
+ // ── Trivial-change filter (Semantic Delta) ────────────────────────────────────
72
+ const TRIVIAL_PATTERNS = [
73
+ /^\s*$/, // blank lines
74
+ /^\s*\/\/.*$/, // comment-only lines
75
+ /^\s*#.*$/, // python comments
76
+ /^\s*\*.*$/, // JSDoc lines
77
+ /^\s*import\b.*$/, // imports
78
+ ];
79
+
80
+ function isTrivialLine(line) {
81
+ return TRIVIAL_PATTERNS.some(p => p.test(line));
82
+ }
83
+
84
+ function semanticDelta(diffText) {
85
+ const lines = diffText.split('\n');
86
+ const meaningful = [];
87
+ for (const line of lines) {
88
+ if (line.startsWith('+++') || line.startsWith('---') || line.startsWith('@@')) {
89
+ meaningful.push(line);
90
+ continue;
91
+ }
92
+ if (line.startsWith('+') || line.startsWith('-')) {
93
+ const codePart = line.slice(1);
94
+ if (!isTrivialLine(codePart)) meaningful.push(line);
95
+ } else {
96
+ meaningful.push(line);
97
+ }
98
+ }
99
+ let filtered = meaningful.join('\n');
100
+ filtered = filtered.replace(/(\n[ ]?\n){3,}/g, '\n\n');
101
+ return filtered.trim();
102
+ }
103
+
104
+ function contentHash(text) {
105
+ const cleaned = semanticDelta(text);
106
+ return crypto.createHash('sha256').update(cleaned).digest('hex').slice(0, 8);
107
+ }
108
+
109
+ // ── Index helpers ─────────────────────────────────────────────────────────────
110
+ function ensureDirs() {
111
+ fs.mkdirSync(HISTORY_DIR, { recursive: true });
112
+ fs.mkdirSync(CASES_DIR, { recursive: true });
113
+ }
114
+
115
+ function loadIndex() {
116
+ ensureDirs();
117
+ if (fs.existsSync(INDEX_FILE)) {
118
+ try { return JSON.parse(fs.readFileSync(INDEX_FILE, 'utf8')); } catch { /* fallthrough */ }
119
+ }
120
+ return { version: '1.0', cases: [], next_id: 1 };
121
+ }
122
+
123
+ function saveIndex(index) {
124
+ ensureDirs();
125
+ const tmp = INDEX_FILE + '.tmp';
126
+ fs.writeFileSync(tmp, JSON.stringify(index, null, 2), 'utf8');
127
+ fs.renameSync(tmp, INDEX_FILE);
128
+ }
129
+
130
+ function loadCase(caseId) {
131
+ const p = path.join(CASES_DIR, `case-${String(caseId).padStart(4, '0')}.json`);
132
+ if (!fs.existsSync(p)) return null;
133
+ try { return JSON.parse(fs.readFileSync(p, 'utf8')); } catch { return null; }
134
+ }
135
+
136
+ function saveCase(caseRecord) {
137
+ const p = path.join(CASES_DIR, `case-${String(caseRecord.id).padStart(4, '0')}.json`);
138
+ fs.writeFileSync(p, JSON.stringify(caseRecord, null, 2), 'utf8');
139
+ }
140
+
141
+ // ── Keyword/tag extraction ─────────────────────────────────────────────────────
142
+ function extractTags(text) {
143
+ const tokens = text.match(/\b[a-zA-Z_][a-zA-Z0-9_]{2,}\b/g) || [];
144
+ const stopWords = new Set([
145
+ 'the', 'and', 'for', 'was', 'this', 'with', 'that', 'has', 'a', 'an',
146
+ 'from', 'are', 'not', 'use', 'but', 'also', 'code',
147
+ 'have', 'will', 'should', 'must', 'can', 'may', 'any',
148
+ 'all', 'new', 'old', 'add', 'get', 'set', 'var', 'let',
149
+ 'const', 'function', 'return', 'import', 'export', 'class',
150
+ 'async', 'await', 'true', 'false', 'null', 'undefined',
151
+ ]);
152
+ const seen = new Set();
153
+ const tags = [];
154
+ for (const token of tokens) {
155
+ const lower = token.toLowerCase();
156
+ if (!stopWords.has(lower) && !seen.has(lower)) {
157
+ seen.add(lower);
158
+ tags.push(lower);
159
+ }
160
+ if (tags.length >= 20) break;
161
+ }
162
+ return tags;
163
+ }
164
+
165
+ // ── Version-Aware Case Filtering ──────────────────────────────────────────────
166
+ // FIX: Prevents stale cases (e.g., React 17 rejections) from blocking valid code
167
+ // once the project has upgraded frameworks. Uses simple numeric comparison.
168
+ //
169
+ // Version filter string format: "react=19,node=22,next=15"
170
+ // Case stack_version format: "react>=18,node>=20" (>=, >, =, <, <=)
171
+
172
+ /**
173
+ * Parse a version filter string into a map of { lib -> number }.
174
+ * Input: "react=19,node=22"
175
+ * Output: { react: 19, node: 22 }
176
+ * @param {string} filterStr
177
+ * @returns {Object<string, number>}
178
+ */
179
+ function parseVersionFilter(filterStr) {
180
+ const result = {};
181
+ if (!filterStr) return result;
182
+ for (const segment of filterStr.split(',')) {
183
+ const m = segment.trim().match(/^([a-zA-Z0-9_.-]+)\s*(?:>=|>|<=|<|=)?\s*(\d+(?:\.\d+)?)/);
184
+ if (m) result[m[1].toLowerCase()] = parseFloat(m[2]);
185
+ }
186
+ return result;
187
+ }
188
+
189
+ /**
190
+ * Returns true if the case either has no stack_version constraint,
191
+ * OR all of its version constraints are satisfied by the provided filter.
192
+ *
193
+ * A case with stack_version "react>=18,node>=20" will be SKIPPED (return false)
194
+ * when the version filter says react=19,node=22 → both constraints met → case IS relevant.
195
+ *
196
+ * The logic: if a case's constraint is NOT met by the current version filter,
197
+ * that case is from a different version context → skip it.
198
+ *
199
+ * @param {{ stack_version?: string }} caseEntry
200
+ * @param {Object<string, number>} versionFilter
201
+ * @returns {boolean} true = case is eligible for this version context
202
+ */
203
+ function caseMatchesVersionFilter(caseEntry, versionFilter) {
204
+ if (!caseEntry.stack_version) return true; // No constraint → always eligible
205
+ if (!versionFilter || !Object.keys(versionFilter).length) return true;
206
+
207
+ // Parse the case's own version constraint
208
+ const caseConstraints = [];
209
+ for (const segment of caseEntry.stack_version.split(',')) {
210
+ const m = segment.trim().match(/^([a-zA-Z0-9_.-]+)\s*(>=|>|<=|<|=)\s*(\d+(?:\.\d+)?)/);
211
+ if (m) caseConstraints.push({ lib: m[1].toLowerCase(), op: m[2], ver: parseFloat(m[3]) });
212
+ }
213
+
214
+ // Check each constraint against the version filter
215
+ for (const { lib, op, ver } of caseConstraints) {
216
+ const projectVer = versionFilter[lib];
217
+ if (projectVer === undefined) continue; // Unknown lib → don't filter on it
218
+
219
+ const satisfied = (
220
+ op === '>=' ? projectVer >= ver :
221
+ op === '>' ? projectVer > ver :
222
+ op === '<=' ? projectVer <= ver :
223
+ op === '<' ? projectVer < ver :
224
+ /* = */ projectVer === ver
225
+ );
226
+ if (!satisfied) return false; // This case's version context doesn't match
227
+ }
228
+ return true;
229
+ }
230
+
231
+ // ── Similarity scoring (TF-IDF Cosine — token-free) ──────────────────────────
232
+
233
+ function buildIdf(corpus) {
234
+ const n = corpus.length;
235
+ if (n === 0) return {};
236
+ const docFreq = {};
237
+ for (const tags of corpus) {
238
+ const unique = new Set(tags);
239
+ for (const tag of unique) {
240
+ docFreq[tag] = (docFreq[tag] || 0) + 1;
241
+ }
242
+ }
243
+ const idf = {};
244
+ for (const [term, df] of Object.entries(docFreq)) {
245
+ idf[term] = Math.log((n + 1) / (df + 1)) + 1.0;
246
+ }
247
+ return idf;
248
+ }
249
+
250
+ function tfidfCosineSimilarity(queryTags, caseTags, idf) {
251
+ if (!queryTags.length || !caseTags.length) return 0.0;
252
+ const tfQ = {};
253
+ for (const t of queryTags) tfQ[t] = (tfQ[t] || 0) + 1;
254
+ const tfC = {};
255
+ for (const t of caseTags) tfC[t] = (tfC[t] || 0) + 1;
256
+ const allTerms = new Set([...Object.keys(tfQ), ...Object.keys(tfC)]);
257
+ let dot = 0, magQ = 0, magC = 0;
258
+ for (const term of allTerms) {
259
+ const wQ = (tfQ[term] || 0) * (idf[term] || 1.0);
260
+ const wC = (tfC[term] || 0) * (idf[term] || 1.0);
261
+ dot += wQ * wC;
262
+ magQ += wQ * wQ;
263
+ magC += wC * wC;
264
+ }
265
+ if (magQ === 0 || magC === 0) return 0.0;
266
+ return dot / (Math.sqrt(magQ) * Math.sqrt(magC));
267
+ }
268
+
269
+ // ── Input helpers ─────────────────────────────────────────────────────────────
270
+ function createRl() {
271
+ return readline.createInterface({ input: process.stdin, output: process.stdout });
272
+ }
273
+
274
+ function ask(rl, prompt) {
275
+ return new Promise(resolve => rl.question(` ${BOLD}${prompt}${RESET} `, resolve));
276
+ }
277
+
278
+ function askMultiline(rl, prompt, sentinel) {
279
+ return new Promise(resolve => {
280
+ console.log(` ${BOLD}${prompt}${RESET}`);
281
+ console.log(` ${DIM}(Type or paste content. Type '${sentinel}' on its own line when done.)${RESET}`);
282
+ const lines = [];
283
+ const listener = (line) => {
284
+ if (line.trim() === sentinel) {
285
+ rl.removeListener('line', listener);
286
+ resolve(lines.join('\n'));
287
+ } else {
288
+ lines.push(line);
289
+ }
290
+ };
291
+ rl.on('line', listener);
292
+ });
293
+ }
294
+
295
+ function askChoice(rl, label, choices, defaultVal) {
296
+ return new Promise(resolve => {
297
+ const opts = choices.map(c => c === defaultVal ? `${BOLD}${c}${RESET}` : c).join(' / ');
298
+ rl.question(` ${BOLD}${label}${RESET} [${opts}] (default: ${defaultVal}): `, answer => {
299
+ const val = (answer || '').trim().toLowerCase();
300
+ resolve(val && choices.includes(val) ? val : defaultVal);
301
+ });
302
+ });
303
+ }
304
+
305
+ // ── Commands ──────────────────────────────────────────────────────────────────
306
+ async function cmdAddCase() {
307
+ console.log(`\n${BOLD}${CYAN}━━━ Recording New Case ━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
308
+ const rl = createRl();
309
+
310
+ const diffText = await askMultiline(rl, 'Paste the REJECTED diff (code snippet):', 'END_DIFF');
311
+ if (!diffText.trim()) { console.log(`${RED}✖ Diff cannot be empty. Aborting.${RESET}`); rl.close(); process.exit(1); }
312
+
313
+ const reason = await ask(rl, 'Rejection reason (1-2 sentences):');
314
+ if (!reason.trim()) { console.log(`${RED}✖ Reason cannot be empty. Aborting.${RESET}`); rl.close(); process.exit(1); }
315
+
316
+ const domain = await askChoice(rl, 'Domain', [...VALID_DOMAINS].sort(), 'general');
317
+ const verdict = await askChoice(rl, 'Verdict', [...VALID_VERDICTS].sort(), 'REJECTED');
318
+ const prRef = (await ask(rl, 'PR / commit reference (optional, e.g. PR-404):')).trim() || null;
319
+ const reviewer = (await ask(rl, 'Reviewer agent (optional, e.g. security-auditor):')).trim() || null;
320
+
321
+ // FIX: stack_version — prevents stale cases from blocking valid code after
322
+ // framework upgrades. Format: "react>=19, node>=20" or leave blank for all.
323
+ const stackVersionRaw = (await ask(rl, 'Stack version constraint (optional, e.g. react>=18, node>=20):')).trim();
324
+ const stackVersion = stackVersionRaw || null;
325
+ rl.close();
326
+
327
+ const delta = semanticDelta(diffText);
328
+ const fingerprint = contentHash(diffText);
329
+ const tags = extractTags(diffText + ' ' + reason);
330
+
331
+ const index = loadIndex();
332
+ const caseId = index.next_id;
333
+ const now = new Date().toISOString().slice(0, 19);
334
+
335
+ const caseRecord = {
336
+ id: caseId, fingerprint, timestamp: now, domain, verdict,
337
+ reason: reason.trim(), pr_ref: prRef, reviewer, tags,
338
+ stack_version: stackVersion,
339
+ diff_raw: diffText.trim(), diff_delta: delta,
340
+ };
341
+
342
+ saveCase(caseRecord);
343
+ index.cases.push({
344
+ id: caseId, fingerprint, domain, verdict, tags,
345
+ timestamp: now, reason_summary: reason.trim().slice(0, 120),
346
+ stack_version: stackVersion,
347
+ });
348
+ index.next_id = caseId + 1;
349
+ saveIndex(index);
350
+
351
+ console.log(`\n${GREEN}✔ Case #${String(caseId).padStart(4, '0')} recorded${RESET}`);
352
+ console.log(` ${DIM}Fingerprint : ${fingerprint}${RESET}`);
353
+ console.log(` ${DIM}Domain : ${domain}${RESET}`);
354
+ console.log(` ${DIM}Tags : ${tags.slice(0, 8).join(', ')}${RESET}`);
355
+ if (stackVersion) console.log(` ${DIM}Stack version : ${stackVersion}${RESET}`);
356
+ console.log();
357
+ }
358
+
359
+ function cmdSearchCases(args) {
360
+ let query = args.filter(a => !a.startsWith('--')).join(' ');
361
+ if (!query) {
362
+ const qi = process.argv.indexOf('--query');
363
+ if (qi !== -1) query = process.argv.slice(qi + 1).filter(a => !a.startsWith('--')).join(' ');
364
+ }
365
+ if (!query) { console.log(`${RED}✖ Provide a search query: search-cases --query "forEach side effects"${RESET}`); process.exit(1); }
366
+
367
+ // FIX: --version-filter skips cases whose stack_version constraint doesn't
368
+ // match the specified versions. Prevents stale React 17/18 cases blocking React 19 code.
369
+ // Usage: search-cases "useEffect" --version-filter "react=19,node=22"
370
+ const vfIdx = args.indexOf('--version-filter');
371
+ const versionFilter = vfIdx !== -1 && args[vfIdx + 1]
372
+ ? parseVersionFilter(args[vfIdx + 1])
373
+ : null;
374
+
375
+ const queryTags = extractTags(query);
376
+ const index = loadIndex();
377
+ if (!index.cases.length) { console.log(`${YELLOW}No cases recorded yet. Use 'add-case' to record your first rejection.${RESET}`); return; }
378
+
379
+ // Apply version filter before scoring
380
+ const eligibleCases = versionFilter
381
+ ? index.cases.filter(e => caseMatchesVersionFilter(e, versionFilter))
382
+ : index.cases;
383
+
384
+ const skipped = index.cases.length - eligibleCases.length;
385
+
386
+ const corpus = eligibleCases.map(e => e.tags || []);
387
+ const idf = buildIdf(corpus);
388
+
389
+ const scored = [];
390
+ for (const entry of eligibleCases) {
391
+ const score = tfidfCosineSimilarity(queryTags, entry.tags || [], idf);
392
+ if (score > 0.0) scored.push({ score, entry });
393
+ }
394
+ scored.sort((a, b) => b.score - a.score);
395
+ const top = scored.slice(0, 5);
396
+
397
+ if (!top.length) {
398
+ console.log(`${YELLOW}No matching cases found for: "${query}"${RESET}`);
399
+ if (skipped > 0) console.log(` ${DIM}${skipped} case(s) skipped by version filter: ${args[vfIdx + 1]}${RESET}`);
400
+ console.log(` ${DIM}Try broader terms or check 'list' for available cases.${RESET}`);
401
+ return;
402
+ }
403
+
404
+ console.log(`\n${BOLD}${CYAN}━━━ Case Law Search Results ━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
405
+ console.log(` Query : ${BOLD}${query}${RESET}`);
406
+ console.log(` Matches: ${top.length} of ${index.cases.length} cases` +
407
+ (skipped > 0 ? ` ${DIM}(${skipped} skipped by version filter)${RESET}` : '') + '\n');
408
+
409
+ for (const { score, entry } of top) {
410
+ const vc = entry.verdict === 'REJECTED' ? RED : YELLOW;
411
+ console.log(` ${BOLD}Case #${String(entry.id).padStart(4, '0')}${RESET} ${vc}[${entry.verdict}]${RESET} ${DIM}${(entry.timestamp || '').slice(0, 10)}${RESET} score=${score.toFixed(2)}`);
412
+ console.log(` ${DIM}Domain: ${entry.domain}${RESET}`);
413
+ console.log(` ${entry.reason_summary}`);
414
+ console.log(` ${DIM}Tags: ${(entry.tags || []).slice(0, 8).join(', ')}${RESET}\n`);
415
+ }
416
+ console.log(` ${DIM}Run 'show --id <N>' to see the full diff for any case.${RESET}`);
417
+ console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
418
+ }
419
+
420
+ function cmdList(args) {
421
+ const index = loadIndex();
422
+ const cases = index.cases || [];
423
+ if (!cases.length) { console.log(`${YELLOW}No cases recorded yet.${RESET}`); return; }
424
+
425
+ let domainFilter = null;
426
+ const di = args.indexOf('--domain');
427
+ if (di !== -1 && args[di + 1]) domainFilter = args[di + 1].toLowerCase();
428
+
429
+ const filtered = domainFilter ? cases.filter(c => c.domain === domainFilter) : cases;
430
+ const total = filtered.length;
431
+
432
+ console.log(`\n${BOLD}${CYAN}━━━ Case Law Index (${total} cases) ━━━━━━━━━━━━━━━━━━━━${RESET}`);
433
+ if (domainFilter) console.log(` ${DIM}Filtered by domain: ${domainFilter}${RESET}\n`);
434
+
435
+ const last20 = filtered.slice(-20).reverse();
436
+ for (const entry of last20) {
437
+ const vc = entry.verdict === 'REJECTED' ? RED : YELLOW;
438
+ console.log(` ${BOLD}#${String(entry.id).padStart(4, '0')}${RESET} ${vc}[${entry.verdict}]${RESET} ${DIM}${(entry.domain || '').toUpperCase()}${RESET} ${(entry.timestamp || '').slice(0, 10)}`);
439
+ console.log(` ${(entry.reason_summary || '').slice(0, 80)}`);
440
+ }
441
+ if (total > 20) console.log(`\n ${YELLOW}... showing last 20 of ${total}. Use 'export' for full history.${RESET}`);
442
+ console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
443
+ }
444
+
445
+ function cmdShow(args) {
446
+ let caseId = null;
447
+ const ii = args.indexOf('--id');
448
+ if (ii !== -1 && args[ii + 1]) caseId = parseInt(args[ii + 1], 10);
449
+ if (caseId == null || isNaN(caseId)) { console.log(`${RED}✖ Provide a case ID: show --id 7${RESET}`); process.exit(1); }
450
+
451
+ const caseRecord = loadCase(caseId);
452
+ if (!caseRecord) { console.log(`${RED}✖ Case #${String(caseId).padStart(4, '0')} not found.${RESET}`); process.exit(1); }
453
+
454
+ const vc = caseRecord.verdict === 'REJECTED' ? RED : YELLOW;
455
+ console.log(`\n${BOLD}${CYAN}━━━ Case #${String(caseRecord.id).padStart(4, '0')} ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
456
+ console.log(` Verdict : ${vc}${BOLD}${caseRecord.verdict}${RESET}`);
457
+ console.log(` Domain : ${caseRecord.domain}`);
458
+ console.log(` Recorded : ${caseRecord.timestamp}`);
459
+ if (caseRecord.pr_ref) console.log(` PR / Ref : ${caseRecord.pr_ref}`);
460
+ if (caseRecord.reviewer) console.log(` Reviewer : ${caseRecord.reviewer}`);
461
+ console.log(`\n ${BOLD}Reason:${RESET}`);
462
+ console.log(` ${caseRecord.reason}`);
463
+ console.log(`\n ${BOLD}Semantic Delta (meaningful changes only):${RESET}`);
464
+ console.log(` ${DIM}─────────────────────────────────────────${RESET}`);
465
+ const deltaLines = (caseRecord.diff_delta || caseRecord.diff_raw).split('\n').slice(0, 40);
466
+ for (const line of deltaLines) {
467
+ if (line.startsWith('+')) console.log(` ${GREEN}${line}${RESET}`);
468
+ else if (line.startsWith('-')) console.log(` ${RED}${line}${RESET}`);
469
+ else console.log(` ${DIM}${line}${RESET}`);
470
+ }
471
+ console.log(`\n ${BOLD}Tags:${RESET} ${(caseRecord.tags || []).join(', ')}`);
472
+ console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
473
+ }
474
+
475
+ function cmdExport(args) {
476
+ const toStdout = args.includes('--stdout');
477
+ const index = loadIndex();
478
+ const cases = index.cases || [];
479
+ if (!cases.length) { console.log(`${YELLOW}No cases to export.${RESET}`); return; }
480
+
481
+ const now = new Date().toISOString().slice(0, 19);
482
+ const lines = [
483
+ '# Tribunal Case Law — Full Export\n',
484
+ `Generated: ${now}`, `Total Cases: ${cases.length}\n`, '---\n',
485
+ ];
486
+ for (const entry of cases) {
487
+ const caseRecord = loadCase(entry.id) || entry;
488
+ const badge = `[${caseRecord.verdict || 'REJECTED'}]`;
489
+ lines.push(`## Case #${String(entry.id).padStart(4, '0')} ${badge}`);
490
+ lines.push(`**Domain:** ${entry.domain} `);
491
+ lines.push(`**Recorded:** ${(entry.timestamp || '').slice(0, 10)} `);
492
+ if (caseRecord.pr_ref) lines.push(`**PR/Ref:** ${caseRecord.pr_ref} `);
493
+ lines.push(`\n**Reason:** ${entry.reason_summary}\n`);
494
+ lines.push(`**Tags:** \`${(entry.tags || []).slice(0, 8).join(', ')}\`\n`);
495
+ lines.push('---\n');
496
+ }
497
+ const content = lines.join('\n');
498
+ if (toStdout) { console.log(content); return; }
499
+
500
+ const outPath = path.join(HISTORY_DIR, 'case-law-export.md');
501
+ fs.writeFileSync(outPath, content, 'utf8');
502
+ console.log(`${GREEN}✔ Exported ${cases.length} cases to ${outPath}${RESET}`);
503
+ }
504
+
505
+ function cmdStats() {
506
+ const index = loadIndex();
507
+ const cases = index.cases || [];
508
+ const domainCounts = {};
509
+ const verdictCounts = {};
510
+ for (const c of cases) {
511
+ domainCounts[c.domain] = (domainCounts[c.domain] || 0) + 1;
512
+ verdictCounts[c.verdict] = (verdictCounts[c.verdict] || 0) + 1;
513
+ }
514
+
515
+ console.log(`\n${BOLD}${CYAN}━━━ Case Law Statistics ━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}`);
516
+ console.log(` Total cases: ${BOLD}${cases.length}${RESET}`);
517
+ console.log(`\n ${BOLD}By Verdict:${RESET}`);
518
+ for (const v of Object.keys(verdictCounts).sort()) {
519
+ const color = v === 'REJECTED' ? RED : YELLOW;
520
+ console.log(` ${color}${v.padEnd(30)}${RESET} ${verdictCounts[v]}`);
521
+ }
522
+ console.log(`\n ${BOLD}By Domain:${RESET}`);
523
+ for (const [d, c] of Object.entries(domainCounts).sort((a, b) => b[1] - a[1])) {
524
+ console.log(` ${CYAN}${d.padEnd(20)}${RESET} ${c}`);
525
+ }
526
+ console.log(`${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}\n`);
527
+ }
528
+
529
+ function cmdAutoRecord() {
530
+ function getFlag(name) {
531
+ const flag = `--${name}`;
532
+ const idx = process.argv.indexOf(flag);
533
+ return (idx !== -1 && process.argv[idx + 1]) ? process.argv[idx + 1] : '';
534
+ }
535
+
536
+ const diffText = getFlag('diff');
537
+ const reason = getFlag('reason');
538
+ let domain = getFlag('domain') || 'general';
539
+ let verdict = getFlag('verdict') || 'REJECTED';
540
+ const reviewer = getFlag('reviewer') || null;
541
+ const prRef = getFlag('pr-ref') || null;
542
+ // FIX: --stack-version persists version context with the case record so
543
+ // future searches can skip it when the project has moved past that version.
544
+ // Example: --stack-version "react=18,next=14"
545
+ const stackVersion = getFlag('stack-version') || null;
546
+
547
+ if (!diffText || !reason) {
548
+ console.log(`${RED}✖ auto-record requires --diff and --reason flags.${RESET}`);
549
+ console.log(` Usage: auto-record --diff "code" --reason "why" --domain security --reviewer agent-name --stack-version "react=18"`);
550
+ process.exit(1);
551
+ }
552
+
553
+ if (isNoiseRejection(reason)) { console.log(`${DIM}⊘ Skipped: trivial rejection (noise filter matched).${RESET}`); return; }
554
+ if (!VALID_DOMAINS.has(domain)) domain = 'general';
555
+ if (!VALID_VERDICTS.has(verdict)) verdict = 'REJECTED';
556
+
557
+ const fingerprint = contentHash(diffText);
558
+ const index = loadIndex();
559
+ for (const existing of index.cases) {
560
+ if (existing.fingerprint === fingerprint) {
561
+ console.log(`${YELLOW}⊘ Duplicate: Case #${String(existing.id).padStart(4, '0')} already records this pattern.${RESET}`);
562
+ return;
563
+ }
564
+ }
565
+
566
+ const delta = semanticDelta(diffText);
567
+ const tags = extractTags(diffText + ' ' + reason);
568
+ const caseId = index.next_id;
569
+ const now = new Date().toISOString().slice(0, 19);
570
+
571
+ const caseRecord = {
572
+ id: caseId, fingerprint, timestamp: now, domain, verdict,
573
+ reason: reason.trim(), pr_ref: prRef, reviewer, tags,
574
+ stack_version: stackVersion,
575
+ diff_raw: diffText.trim(), diff_delta: delta, auto_recorded: true,
576
+ };
577
+
578
+ saveCase(caseRecord);
579
+ index.cases.push({
580
+ id: caseId, fingerprint, domain, verdict, tags,
581
+ timestamp: now, reason_summary: reason.trim().slice(0, 120),
582
+ stack_version: stackVersion,
583
+ });
584
+ index.next_id = caseId + 1;
585
+ saveIndex(index);
586
+ console.log(`${GREEN}✔ Auto-recorded Case #${String(caseId).padStart(4, '0')}${RESET} [${verdict}] domain=${domain}`);
587
+ console.log(` ${DIM}Reason: ${reason.slice(0, 80)}${RESET}`);
588
+ if (stackVersion) console.log(` ${DIM}Stack version: ${stackVersion}${RESET}`);
589
+ }
590
+
591
+ async function cmdOverrule(args) {
592
+ let caseId = null;
593
+ const ii = args.indexOf('--id');
594
+ if (ii !== -1 && args[ii + 1]) caseId = parseInt(args[ii + 1], 10);
595
+ if (caseId == null || isNaN(caseId)) { console.log(`${RED}✖ Provide a case ID: overrule --id 7${RESET}`); process.exit(1); }
596
+
597
+ const caseRecord = loadCase(caseId);
598
+ if (!caseRecord) { console.log(`${RED}✖ Case #${String(caseId).padStart(4, '0')} not found.${RESET}`); process.exit(1); }
599
+ if (caseRecord.verdict === 'OVERRULED') { console.log(`${YELLOW}Case #${String(caseId).padStart(4, '0')} is already OVERRULED.${RESET}`); return; }
600
+
601
+ let reason = null;
602
+ const ri = args.indexOf('--reason');
603
+ if (ri !== -1 && args[ri + 1]) reason = args[ri + 1];
604
+
605
+ if (!reason) {
606
+ const rl = createRl();
607
+ reason = await ask(rl, 'Reason for overruling this precedent:');
608
+ rl.close();
609
+ }
610
+ if (!reason || !reason.trim()) { console.log(`${RED}✖ An overrule reason is required.${RESET}`); process.exit(1); }
611
+
612
+ const oldVerdict = caseRecord.verdict;
613
+ caseRecord.verdict = 'OVERRULED';
614
+ caseRecord.overruled_at = new Date().toISOString().slice(0, 19);
615
+ caseRecord.overrule_reason = reason.trim();
616
+ caseRecord.previous_verdict = oldVerdict;
617
+ saveCase(caseRecord);
618
+
619
+ const index = loadIndex();
620
+ for (const entry of index.cases) {
621
+ if (entry.id === caseId) { entry.verdict = 'OVERRULED'; break; }
622
+ }
623
+ saveIndex(index);
624
+
625
+ console.log(`\n${GREEN}✔ Case #${String(caseId).padStart(4, '0')} OVERRULED${RESET}`);
626
+ console.log(` ${DIM}Previous verdict : ${oldVerdict}${RESET}`);
627
+ console.log(` ${DIM}Overrule reason : ${reason.trim()}${RESET}`);
628
+ console.log(` ${DIM}The case is preserved in history but no longer blocks reviews.${RESET}\n`);
629
+ }
630
+
631
+ // ── Main ──────────────────────────────────────────────────────────────────────
632
+ const COMMANDS = {
633
+ 'add-case': cmdAddCase,
634
+ 'auto-record': cmdAutoRecord,
635
+ 'search-cases': cmdSearchCases,
636
+ 'list': cmdList,
637
+ 'show': cmdShow,
638
+ 'overrule': cmdOverrule,
639
+ 'export': cmdExport,
640
+ 'stats': cmdStats,
641
+ };
642
+
643
+ async function main() {
644
+ const argv = process.argv.slice(2);
645
+ if (!argv.length || ['-h', '--help', 'help'].includes(argv[0])) {
646
+ console.log(`
647
+ ${BOLD}case_law_manager.js${RESET} — Tribunal Case Law Engine
648
+
649
+ ${BOLD}Commands:${RESET}
650
+ add-case Record a new rejected pattern (interactive)
651
+ auto-record --diff --reason Record a rejection (non-interactive, for AI agents)
652
+ search-cases --query <text> Find relevant precedents (TF-IDF cosine, token-free)
653
+ list [--domain <domain>] List all recorded cases
654
+ show --id <N> Show full diff for a case
655
+ overrule --id <N> Formally overrule a past precedent
656
+ export [--stdout] Export all cases to Markdown
657
+ stats Show breakdown by domain/verdict
658
+
659
+ ${BOLD}Domains:${RESET} ${[...VALID_DOMAINS].sort().join(', ')}
660
+ ${BOLD}Verdicts:${RESET} ${[...VALID_VERDICTS].sort().join(', ')}
661
+ `);
662
+ return;
663
+ }
664
+
665
+ const cmd = argv[0];
666
+ const rest = argv.slice(1);
667
+ if (!COMMANDS[cmd]) {
668
+ console.log(`${RED}✖ Unknown command: '${cmd}'${RESET}`);
669
+ console.log(` Valid: ${Object.keys(COMMANDS).join(', ')}`);
670
+ process.exit(1);
671
+ }
672
+ await COMMANDS[cmd](rest);
673
+ }
674
+
675
+ // ── Exports ──────────────────────────────────────────────────────────────────
676
+ module.exports = {
677
+ contentHash, semanticDelta, extractTags, loadIndex, saveIndex,
678
+ loadCase, saveCase, tfidfCosineSimilarity, buildIdf, findAgentDir,
679
+ isNoiseRejection, isTrivialLine,
680
+ };
681
+
682
+ if (require.main === module) {
683
+ main().catch(err => { console.error(err); process.exit(1); });
684
+ }