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
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: test

# The first check surface this repo has had (#5 review, 2026-09-25). Ranking and migration
# are where a memory store fails silently -- nothing throws, the store just stops surfacing
# what it holds -- so they are pinned in tests/ and run on every PR.
on:
pull_request:
push:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npm test
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "npm run build && node --test tests/*.test.mjs",
"postinstall": "npm run build || true",
"prepublishOnly": "npm run build"
},
Expand Down
26 changes: 24 additions & 2 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ CREATE TABLE IF NOT EXISTS observations (
cwd TEXT,
tags TEXT,
content_hash TEXT,
shape_hash TEXT,
scored_by TEXT,
event_session_id TEXT
);
Expand Down Expand Up @@ -477,6 +478,18 @@ export function openDatabase(path?: string): Database.Database {
db.exec(`UPDATE patterns SET last_seen = COALESCE(updated_at, created_at, datetime('now')) WHERE last_seen IS NULL`);
} catch { /* column already exists */ }

// observations.shape_hash — the repetition discount's key (see memory.ts `shapeHash`).
// The warning above is not historical: adding the column to SCHEMA alone does nothing for
// an existing database, because CREATE TABLE IF NOT EXISTS will not widen a table it
// already made, and insertObservation names the column — so prepareStatements throws
// `no such column: shape_hash` on every existing db and capture dies silently, exactly as
// it did for four days in 2026-06/07. Nullable ADD COLUMN, no backfill: rows written before
// the discount existed keep the salience they were given, and NULL is the honest value for
// "this row's shape was never computed".
try {
db.exec(`ALTER TABLE observations ADD COLUMN shape_hash TEXT`);
} catch { /* column already exists */ }

// Migration: base_salience — immutable IMPORTANCE, decoupled from the decaying `salience`.
// decayObservations drives `salience` toward 0 for old rows, but search ranks by salience, so
// important old memories became unfindable. Search now ranks by base_salience (importance);
Expand Down Expand Up @@ -533,6 +546,9 @@ export function openDatabase(path?: string): Database.Database {
// Index AFTER the ALTER, for the same reason base_salience's index is here: on a
// pre-migration db the SCHEMA block runs before the column exists.
db.exec(`CREATE INDEX IF NOT EXISTS idx_obs_content_hash ON observations(content_hash)`);
// Same rule, fourth time in this file: an index cannot precede its column. idx_obs_shape
// is created here, after the shape_hash ALTER above, never in SCHEMA.
db.exec(`CREATE INDEX IF NOT EXISTS idx_obs_shape ON observations(shape_hash)`);

// Migration: seen_set.last_seen — enables recency-windowed novelty (prune stale tokens so novelty
// doesn't saturate to 0 as the set grows). Backfill from first_seen.
Expand Down Expand Up @@ -566,8 +582,14 @@ export function prepareStatements(db: Database.Database) {
insertObservation: db.prepare(`
INSERT INTO observations (session_id, tool_name, input_summary, output_summary,
surprise, novelty, arousal, reward, conflict, salience, base_salience, cwd, tags,
content_hash, scored_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
content_hash, scored_by, shape_hash)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`),

// How many observations already share this SHAPE. Drives the repetition discount:
// the 684th copy of a wake prompt is not 684 times as important as the first.
countShape: db.prepare(`
SELECT COUNT(*) AS n FROM observations WHERE shape_hash = ?
`),

// Same insert WITH the event's own timestamp. Kept as a second statement rather
Expand Down
113 changes: 105 additions & 8 deletions src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ import { createHash } from 'node:crypto';
/** Stable content hash for dedup — backs the observations.content_hash column (Kimi #4). */
const contentHash = (s: string): string => createHash('sha256').update(s).digest('hex');

/**
* A hash of an observation's SHAPE — its text with the volatile parts masked.
*
* content_hash is exact, and exactness is useless against boilerplate that carries an id:
* measured 2026-09-25, the hestia wake prompt appears 684 times in one store with 684
* DISTINCT content hashes, because each carries a fresh notice id and timestamp. The scorer
* saw novelty 0.7 on every one of them and filed all 684 at base_salience 0.9 — 37% of the
* whole store, one prompt, all of it ranked as if newly informative.
*
* Masking ids, hashes, timestamps and bare numbers gives repeated boilerplate a stable
* identity, so the discount below can see that it is the same thing again.
*/
const shapeHash = (toolName: string, input: string): string => {
const norm = (input || '')
.replace(/\b[0-9a-f]{8,}\b/gi, '#') // ids, shas, uuids
.replace(/\d{4}-\d{2}-\d{2}[T ][\d:.]+Z?/g, '#') // timestamps
.replace(/\b\d+\b/g, '#') // bare numbers
.replace(/\s+/g, ' ')
.trim()
.slice(0, 400);
return contentHash(toolName + '\x00' + norm);
};

/**
* Which scorer generation wrote a row — stamped into observations.scored_by.
*
Expand Down Expand Up @@ -183,6 +206,18 @@ export class SNARCMemory {
const stored = scores.salience >= this.scorer.threshold;
if (stored) {
const tags = extractTags(toolName, inputSummary, outputSummary);
// REPETITION DISCOUNT. The Nth identical-shaped observation carries less information
// than the first; base_salience is what retrieval ranks by, so that is what falls.
// log2 rather than 1/n: the 2nd copy still matters, the 700th does not, and nothing
// is ever discounted to zero because the event did happen.
const shape = shapeHash(toolName, inputSummary);
let priorShapes = 0;
try {
priorShapes = (this.stmts.countShape.get(shape) as any)?.n ?? 0;
} catch { /* pre-migration database — no discount, same as before */ }
const discounted = priorShapes > 0
? Math.max(0.05, scores.salience / (1 + Math.log2(1 + priorShapes)))
: scores.salience;
this.stmts.insertObservation.run(
this.sessionId,
toolName,
Expand All @@ -194,11 +229,12 @@ export class SNARCMemory {
scores.reward,
scores.conflict,
scores.salience,
scores.salience, // base_salience — immutable importance; `salience` (prev col) decays, this doesn't
discounted, // base_salience — importance. Discounted by how often this SHAPE recurred.
cwd,
JSON.stringify(tags),
contentHash(toolName + '\x00' + inputSummary + '\x00' + outputSummary),
SCORER_VERSION,
shape,
);
}

Expand Down Expand Up @@ -331,10 +367,24 @@ export class SNARCMemory {
}
} catch { /* FTS query syntax error — skip */ }

// Sort: patterns first (higher value), then by salience
// Sort: patterns and identity ABOVE raw observations, then by strength.
//
// The intent above ("patterns first") was right and the comparator did the opposite:
// `a.tier - b.tier` ascending puts Tier 1 first, and because Tier 1 holds 1,861 rows
// against Tier 2's 86, observations always filled `limit` and the slice below then
// discarded every pattern. Measured 2026-09-25: `search('prior art')` returned 5 results,
// 0 of them patterns, while the prepared statement for Tier 2 returned the "check for
// prior art: look for open PRs" pattern on the same query. The tier was fetched, ranked
// last, and thrown away — every time, for every query, while the tool advertised
// "search across all tiers".
const rank = (t: number) => (t === 2 ? 0 : t === 3 ? 1 : 2);
results.sort((a, b) => {
if (a.tier !== b.tier) return a.tier - b.tier; // lower tier = higher value
return (b.salience || 0) - (a.salience || 0);
const ra = rank(a.tier), rb = rank(b.tier);
if (ra !== rb) return ra - rb;
// Patterns carry `confidence`, observations carry `salience`; compare like with like.
const sa = a.salience ?? a.confidence ?? 0;
const sb = b.salience ?? b.confidence ?? 0;
return sb - sa;
});

return results.slice(0, limit);
Expand Down Expand Up @@ -391,19 +441,66 @@ export class SNARCMemory {

// Tier 2 patterns — INFERRED, only high-confidence (>= 0.6)
// Exclude proposed_identity — those need human review before injection
//
// RANKED BY WHAT A PATTERN TEACHES, NOT BY HOW OFTEN ITS SHAPE RECURRED. getAllPatterns
// orders by `frequency DESC`, and the most frequent shapes are the conversational
// scaffolding: "Conversation → user_prompt → Conversation" at frequency 434. Every
// consolidated engineering lesson has frequency 1. So before this, the three briefing
// slots were structurally guaranteed to hold tautologies — measured on this database
// 2026-09-25, all three were Conversation/user_prompt cycles — while 80 deep_* patterns
// ("check for prior art: look for open PRs that touch it", "take a letter's date from
// the file's mtime") were never once surfaced. The seat then re-derived several of them
// at cost in a single day. Frequency is a measure of repetition, not of information.
const briefingRank = (p: any): number => {
const byKind: Record<string, number> = {
deep_insight: 0, deep_error_fix: 0, deep_decision: 1, deep_workflow: 1,
concept_cluster: 3, tool_sequence: 4,
};
return byKind[p.kind] ?? 2;
};
// A tool_sequence made only of conversational turns encodes nothing: it says that talking
// is followed by talking. Drop it rather than rank it, so it cannot crowd a slot.
const isEmptySequence = (p: any): boolean => {
if (p.kind !== 'tool_sequence') return false;
const steps = String(p.summary || '').replace(/^.*workflow:\s*/i, '').split('→');
return steps.every((s: string) => /^(conversation|user_prompt)$/i.test(s.trim()));
};
const patterns = this.getPatterns()
.filter((p: any) => p.confidence >= 0.6 && p.kind !== 'proposed_identity');
.filter((p: any) => p.confidence >= 0.6 && p.kind !== 'proposed_identity')
.filter((p: any) => !isEmptySequence(p))
.sort((a: any, b: any) => briefingRank(a) - briefingRank(b) || b.confidence - a.confidence);
if (patterns.length > 0) {
lines.push('Inferred patterns (heuristic — may not be accurate):');
for (const p of patterns.slice(0, 3)) {
for (const p of patterns.slice(0, 5)) {
lines.push(` - [${p.kind}] ${p.summary} (confidence: ${p.confidence.toFixed(2)})`);
this.logRetrieval(cwd, 'briefing', 'pattern', p.confidence, `${p.summary} ${p.detail || ''}`);
}
}

// Tier 1 observations — OBSERVED, above median salience (>= 0.35)
const recent = this.stmts.getRecentObservations.all(20) as any[];
const highSalience = recent.filter((o: any) => o.salience >= 0.35);
// DEDUPED, because the loudest observations are the ones the harness repeats. A hestia
// mesh wake injects a near-identical prompt every time and it records at salience 1.000,
// so without this the three observation slots hold three copies of the same wake text —
// measured 2026-09-25. Keep the first of each near-duplicate and let the next distinct
// thing have the slot.
const recent = this.stmts.getRecentObservations.all(40) as any[];
const seen = new Set<string>();
const highSalience = recent
.filter((o: any) => o.salience >= 0.35)
.filter((o: any) => {
// Normalise before comparing: the SAME wake text is recorded once as a user_prompt
// and again as a Conversation carrying a "[Human] " role tag, so a raw prefix key
// sees two distinct strings and keeps both.
const key = String(o.input_summary || '')
.replace(/^\s*\[[^\]]{1,20}\]\s*/, '')
.replace(/\s+/g, ' ')
.trim()
.slice(0, 60)
.toLowerCase();
if (seen.has(key)) return false;
seen.add(key);
return true;
});
if (highSalience.length > 0) {
lines.push('Recent observations (directly recorded):');
for (const o of highSalience.slice(0, 3)) {
Expand Down
Loading
Loading