feat(prompt-hook): graph-derived gate tier + confidence-tiered injection + gate telemetry (#1136)

The keyword gate (#1126) can never know a repo's domain nouns. This adds
the graph-derived tier the design discussion converged on: symbol names
are split into prose segments at index time (name_segment_vocab, riding
the insertNode write path), and the hook verifies a prompt's plain words
against them — "the state machine des commandes" → OrderStateMachine, in
any language whose technical nouns are Latin script.

Confidence now decides HOW MUCH to inject, not just whether:
- HIGH (keyword, or index-verified code token): full explore injection,
  unchanged — the validated adoption lever.
- MEDIUM (segment matches only): a ~500-byte pointer naming the matching
  symbols; the AGENT writes the explore query. Never runs explore, so a
  fuzzy match can't inject 16KB of wrong-feature context.
- Silent otherwise, as before.

Precision is derived from the repo's own naming statistics plus measured
FP fixes: co-occurrence (≥2 words on one name) always qualifies; a single
word must be ≥5 chars, cluster across 2–25 names (singletons are prose
coincidence: "deploy to production" → matchesNonProductionDir), match a
multi-segment name, and not be an English function/filler word (the one
place a word list is honest: identifiers are English, so only English
prose collides). Every candidate is re-verified against nodes before
being surfaced — vocab rows are proposals, deletions leave orphans by
design, a full index rebuilds from scratch, and sync heals pre-upgrade
databases (batched + yielding; emptiness captured at sync ENTRY so the
sync's own writes can't mask the backfill).

Schema v7 migration is DDL-only (instant; none of the #1067 row-churn
hazards). Gate outcomes roll up as anonymous usage counters
(prompt-hook-gate-<outcome>, names only, never content) through the
existing telemetry pipeline — recall becomes measurable, and the counters
are the agreed kill-criterion data for ever revisiting a local classifier.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-07-02 14:35:38 -05:00
committed by GitHub
co-authored by Claude Fable 5
parent 317e7f4d3d
commit e699ee9686
15 changed files with 771 additions and 37 deletions
+20 -1
View File
@@ -9,7 +9,7 @@ import { SqliteDatabase } from './sqlite-adapter';
/**
* Current schema version
*/
export const CURRENT_SCHEMA_VERSION = 6;
export const CURRENT_SCHEMA_VERSION = 7;
/**
* Migration definition
@@ -100,6 +100,25 @@ const migrations: Migration[] = [
`);
},
},
{
version: 7,
description:
'Add name_segment_vocab — prose-word → symbol-name lookup for the prompt hooks graph-derived gate',
up: (db) => {
// DDL only — instant on any size database (the row-churn hazards of #1067
// don't apply). The table starts EMPTY on migrated databases; `sync`
// detects that over a populated graph and backfills batched+yielding
// (CodeGraph.rebuildNameSegmentVocab), and any full index rebuilds it
// from scratch. Keep the definition in lockstep with schema.sql.
db.exec(`
CREATE TABLE IF NOT EXISTS name_segment_vocab (
segment TEXT NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (segment, name)
) WITHOUT ROWID;
`);
},
},
];
/**
+113
View File
@@ -21,6 +21,7 @@ import { safeJsonParse } from '../utils';
import { kindBonus, nameMatchBonus, scorePathRelevance } from '../search/query-utils';
import { parseQuery, boundedEditDistance } from '../search/query-parser';
import { isGeneratedFile } from '../extraction/generated-detection';
import { splitIdentifierSegments } from '../search/identifier-segments';
/**
* Path-only heuristic for files that should not be candidates for
@@ -219,8 +220,16 @@ export class QueryBuilder {
getDominantFile?: SqliteStatement;
getTopRouteFile?: SqliteStatement;
getRoutingManifest?: SqliteStatement;
insertNameSegment?: SqliteStatement;
} = {};
// Names whose segments were already written this session — skips re-splitting
// and re-inserting for the same-named nodes that repeat across files ("get",
// "render", …). Purely a write-path fast path; INSERT OR IGNORE is the
// correctness backstop. Bounded so a pathological repo can't grow it forever.
private segmentedNames: Set<string> = new Set();
private static readonly MAX_SEGMENTED_NAMES = 65536;
constructor(db: SqliteDatabase) {
this.db = db;
}
@@ -303,6 +312,30 @@ export class QueryBuilder {
returnType: node.returnType ?? null,
updatedAt: node.updatedAt ?? Date.now(),
});
// Segment vocabulary rides the same write path (and transaction) so it can
// never drift ahead of the nodes it describes. Deletes intentionally leave
// orphans behind — vocab rows are proposals re-verified against nodes
// before use, and a full index clears the table at its start. File nodes
// are excluded: a file's basename duplicates the symbols inside it
// (state-machine.ts / OrderStateMachine), which double-counts every
// concept and defeats the singleton-vs-cluster rarity statistics.
if (node.kind !== 'file') this.insertNameSegments(node.name);
}
/** Write `name`'s segments into name_segment_vocab (idempotent). */
private insertNameSegments(name: string): void {
if (this.segmentedNames.has(name)) return;
if (this.segmentedNames.size >= QueryBuilder.MAX_SEGMENTED_NAMES) this.segmentedNames.clear();
this.segmentedNames.add(name);
if (!this.stmts.insertNameSegment) {
this.stmts.insertNameSegment = this.db.prepare(
'INSERT OR IGNORE INTO name_segment_vocab (segment, name) VALUES (?, ?)',
);
}
for (const segment of splitIdentifierSegments(name)) {
this.stmts.insertNameSegment.run(segment, name);
}
}
/**
@@ -409,6 +442,86 @@ export class QueryBuilder {
this.stmts.deleteNodesByFile.run(filePath);
}
// ===========================================================================
// Name-segment vocabulary (prompt-hook graph-derived gate)
// ===========================================================================
/** Wipe the segment vocabulary. A full index calls this at its start; the
* node write path repopulates it as files (re-)index, so the end state is
* exactly the current names with no orphan rows. */
clearNameSegmentVocab(): void {
this.db.exec('DELETE FROM name_segment_vocab');
this.segmentedNames.clear();
}
/** True when the vocab has no rows — an index built before the table existed.
* `sync` uses this to heal such databases (see rebuildNameSegmentVocabFrom). */
isNameSegmentVocabEmpty(): boolean {
const row = this.db.prepare('SELECT 1 FROM name_segment_vocab LIMIT 1').get();
return row === undefined;
}
/** One page of distinct non-file node names, for batched vocab rebuilds
* (file basenames are excluded from the vocab — see insertNode). */
getDistinctNodeNames(limit: number, offset: number): string[] {
const rows = this.db
.prepare("SELECT DISTINCT name FROM nodes WHERE kind != 'file' ORDER BY name LIMIT ? OFFSET ?")
.all(limit, offset) as Array<{ name: string }>;
return rows.map((r) => r.name);
}
/** Insert segments for a batch of names in one transaction (vocab heal path). */
insertNameSegmentsBatch(names: string[]): void {
this.db.transaction(() => {
for (const name of names) this.insertNameSegments(name);
})();
}
/**
* Names whose segments cover at least `minSegments` of the given segments —
* the co-occurrence probe behind the prompt hook's medium tier: the words
* "state" and "machine" both being segments of `OrderStateMachine` is strong
* evidence the prompt names that symbol in prose. Ordered by coverage.
*/
getSegmentCoOccurrence(segments: string[], minSegments: number, limit: number): Array<{ name: string; matches: number }> {
if (segments.length === 0) return [];
const placeholders = segments.map(() => '?').join(', ');
const rows = this.db
.prepare(
`SELECT name, COUNT(DISTINCT segment) AS matches
FROM name_segment_vocab
WHERE segment IN (${placeholders})
GROUP BY name
HAVING matches >= ?
ORDER BY matches DESC, length(name) ASC
LIMIT ?`,
)
.all(...segments, minSegments, limit) as Array<{ name: string; matches: number }>;
return rows;
}
/** How many distinct names each segment appears in — the rarity signal that
* separates a discriminative word ("checkout") from a ubiquitous one ("state"). */
getSegmentNameCounts(segments: string[]): Map<string, number> {
if (segments.length === 0) return new Map();
const placeholders = segments.map(() => '?').join(', ');
const rows = this.db
.prepare(
`SELECT segment, COUNT(*) AS n FROM name_segment_vocab
WHERE segment IN (${placeholders}) GROUP BY segment`,
)
.all(...segments) as Array<{ segment: string; n: number }>;
return new Map(rows.map((r) => [r.segment, r.n]));
}
/** Names containing the given segment (rare-single-word tier). */
getNamesForSegment(segment: string, limit: number): string[] {
const rows = this.db
.prepare('SELECT name FROM name_segment_vocab WHERE segment = ? ORDER BY length(name) ASC LIMIT ?')
.all(segment, limit) as Array<{ name: string }>;
return rows.map((r) => r.name);
}
/**
* Get a node by ID
*/
+19
View File
@@ -123,6 +123,25 @@ CREATE TRIGGER IF NOT EXISTS nodes_au AFTER UPDATE ON nodes BEGIN
VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature);
END;
-- Prose-word → symbol-name lookup for the prompt hook's graph-derived gate.
-- One row per (segment, name): segment is a lowercased word of a symbol name
-- ("OrderStateMachine" → order, state, machine — see identifier-segments.ts),
-- which lets natural-language prompt words be verified against the graph in
-- any language whose technical nouns are Latin script. File nodes are
-- excluded — a file's basename duplicates the symbols inside it and skews the
-- singleton-vs-cluster rarity statistics. FTS can't serve this lookup (its
-- tokenizer keeps camelCase names as single tokens), so segments are
-- materialized on the node write path.
-- Deletions leave orphan rows ON PURPOSE: rows are PROPOSALS, always
-- re-verified against nodes before being surfaced (CodeGraph.getSegmentMatches),
-- and a full index clears the table at its start. Populated lazily on old
-- databases (empty until the next index/sync heals it).
CREATE TABLE IF NOT EXISTS name_segment_vocab (
segment TEXT NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (segment, name)
) WITHOUT ROWID;
-- Edge indexes.
-- idx_edges_source / idx_edges_target are intentionally omitted —
-- the (source, kind) and (target, kind) composites below cover the