Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/mistakes/import-seed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// src/mistakes/import-seed.js — import the October 2026 catalogue of mined mistakes (KB #3557) as weak-evidence records.
import { readFileSync } from 'fs';
import { recordMistake } from './store.js';
import { createHash } from 'crypto';
import { currentMistakes, recordMistake } from './store.js';

const hash12 = (s) => createHash('sha256').update(s).digest('hex').slice(0, 12);

const part = (label, value) => (typeof value === 'string' && value.trim() ? `${label}: ${value.trim()}` : null);

Expand All @@ -15,7 +18,8 @@ export function seedRecord(row) {
source: 'seed', project: typeof row.project === 'string' && row.project.trim() ? row.project.trim() : 'unknown',
task: row.title.trim(), task_text_ref: `kb:${row.kb_id}`, model: null, caught_by: 'operator', finding,
tags: [row.kind, row.pattern].filter(v => typeof v === 'string' && v.trim()).map(v => v.split(/\s+/).slice(0, 6).join(' ')),
client_ok: true, key: `seed:${row.kb_id}`,
// One KB document can hold several mistakes, so the key names the document and the mistake.
client_ok: true, key: `seed:${String(row.kb_id).replace(/[^A-Za-z0-9_.-]+/g, '-')}:${hash12(row.title.trim())}`,
};
}

Expand All @@ -24,9 +28,15 @@ export function importSeed(file, { now = () => new Date(), mirror = null } = {})
try { rows = JSON.parse(readFileSync(file, 'utf8')); } catch (e) { throw new Error(`cannot read the catalogue ${file}: ${e.message}`); }
if (!Array.isArray(rows)) throw new Error('the catalogue must be a JSON array');
const result = { rows: rows.length, added: 0, duplicates: 0, skipped: [], ...(mirror ? { unmirrored: 0 } : {}) };
// Rows already in the ledger under the older key (one per document) are recognised by their document and title.
// Such a row is sent again under the key it already has, so it counts as a repeat and a missing KB copy is retried.
const known = new Map(currentMistakes().mistakes.filter(m => m.source === 'seed' && m.key).map(m => [`${m.task_text_ref}|${m.task}`, m.key]));
rows.forEach((row, i) => {
try {
const r = recordMistake(seedRecord(row), { now: now(), mirror });
const fresh = seedRecord(row);
const record = { ...fresh, key: known.get(`${fresh.task_text_ref}|${fresh.task}`) ?? fresh.key };
const r = recordMistake(record, { now: now(), mirror });
known.set(`${record.task_text_ref}|${record.task}`, record.key);
if (r.duplicate) result.duplicates += 1; else result.added += 1;
if (mirror && !r.mirrored) result.unmirrored += 1;
} catch (e) {
Expand Down
25 changes: 23 additions & 2 deletions tests/mistakes-import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { commitEvidence, findingsInBrief, fixCommit, importSdd, ledgerDirs, parseSddLedger } from '../src/mistakes/import-sdd.js';
import { importSeed, seedRecord } from '../src/mistakes/import-seed.js';
import { currentMistakes } from '../src/mistakes/store.js';
import { currentMistakes, recordMistake } from '../src/mistakes/store.js';

const PROGRESS = [
'# SDD ledger — plan: docs/plans/demo.md',
Expand Down Expand Up @@ -181,7 +181,8 @@ describe('the seed catalogue', () => {

it('maps a row to a weak record keyed by its KB id', () => {
const r = seedRecord(row);
assert.deepStrictEqual([r.source, r.project, r.caught_by, r.key, r.task_text_ref, r.client_ok], ['seed', 'cod3x/kalshi', 'operator', 'seed:1510', 'kb:1510', true]);
assert.deepStrictEqual([r.source, r.project, r.caught_by, r.task_text_ref, r.client_ok], ['seed', 'cod3x/kalshi', 'operator', 'kb:1510', true]);
assert.match(r.key, /^seed:1510:[0-9a-f]{12}$/);
assert.match(r.finding, /^GTC orders were really FOK\nSymptom: every order got 409\nWrong first guess: maintenance noise\nRoot cause: .*\nFix: 7f2634b/);
assert.strictEqual(r.fix_commit, undefined);
assert.deepStrictEqual(r.tags, ['bug-in-code', 'assumed server default differs from reality']);
Expand All @@ -203,3 +204,23 @@ describe('the seed catalogue', () => {
assert.throws(() => importSeed(join(work, 'obj.json')), /must be a JSON array/);
});
});

describe('several catalogue rows from the same KB document', () => {
const base = { kb_id: 1510, project: 'p', kind: 'bug-in-code' };

it('are all imported, once each', () => {
const file = join(work, 'shared.json');
writeFileSync(file, JSON.stringify([{ ...base, title: 'First mistake' }, { ...base, title: 'Second mistake' }, { ...base, kb_id: 7, title: 'Other doc' }]));
assert.deepStrictEqual([importSeed(file).added, importSeed(file).added], [3, 0]);
assert.strictEqual(currentMistakes().mistakes.length, 3);
});

it('a ledger imported with the old one-key-per-document rule is completed, not doubled', () => {
recordMistake({ ...seedRecord({ ...base, title: 'First mistake' }), key: 'seed:1510' }); // what the old importer wrote
const file = join(work, 'shared.json');
writeFileSync(file, JSON.stringify([{ ...base, title: 'First mistake' }, { ...base, title: 'Second mistake' }]));
const r = importSeed(file);
assert.deepStrictEqual([r.added, r.duplicates], [1, 1]);
assert.deepStrictEqual(currentMistakes().mistakes.map(m => m.task).sort(), ['First mistake', 'Second mistake']);
});
});
Loading