New:Socket for Asana Is Now Available.Learn more
Get Started

@lubab/madar

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lubab/madar - npm Package Compare versions

Comparing version
0.40.0-beta.4
to
0.40.0-beta.5
+16
dist/src/adapters/typescript/execution.d.ts
import ts from 'typescript';
import type { IndexChannelNode, IndexDiagnostic, IndexEdge, IndexSymbol } from '../../domain/index/model.js';
export type CollectExecutionInput = {
program: ts.Program;
sourceFiles: readonly ts.SourceFile[];
checker: ts.TypeChecker;
pathToFileId: ReadonlyMap<string, string>;
symbols: IndexSymbol[];
symbolsByFile: ReadonlyMap<string, readonly IndexSymbol[]>;
};
export type CollectExecutionResult = {
channels: readonly IndexChannelNode[];
edges: readonly IndexEdge[];
diagnostics: readonly IndexDiagnostic[];
};
export declare function collectExecutionSemantics(h: CollectExecutionInput): CollectExecutionResult;

Sorry, the diff of this file is too big to display

import type { QueryIndex } from '../domain/query/index-status.js';
import type { EvidenceHydrationTargets, HydratedEvidenceResult } from '../domain/query/types.js';
export declare function hydrateEvidence(index: QueryIndex, input: EvidenceHydrationTargets): HydratedEvidenceResult;
import { isUtf8 } from 'node:buffer';
import { createHash } from 'node:crypto';
import { readFileSync, realpathSync } from 'node:fs';
import { isAbsolute, relative, resolve, sep } from 'node:path';
class Halt {
value;
constructor(value) {
this.value = value;
}
}
function halt(state, key) {
throw new Halt({ state, subject: key });
}
function bad(key) { halt('corrupt', key); }
const same = (a, b) => a.start.line === b.start.line && a.start.column === b.start.column
&& a.end.line === b.end.line && a.end.column === b.end.column;
function lines(text) {
const starts = [0], ends = [];
for (const match of text.matchAll(/\r\n|[\n\r\u2028\u2029]/g)) {
ends.push(match.index);
starts.push(match.index + match[0].length);
}
ends.push(text.length);
return [starts, ends];
}
function clip(src, r) {
const { start, end } = r ?? {};
if (!start || !end)
return null;
const a = src[3][start.line - 1], z = src[4][end.line - 1];
if (a === undefined || z === undefined)
return null;
const from = a + start.column - 1, to = src[3][end.line - 1] + end.column - 1;
return from <= z && to <= z && from <= to ? src[2].slice(from, to) : null;
}
function ready(i, q) {
const ids = (xs) => [...new Set(xs)].sort();
const nodes = ids(q.symbolIds), decls = ids(q.declarationSymbolIds), ops = ids(q.operationIds), checks = ids(q.validationOperationIds ?? []);
if (decls.some((id) => !nodes.includes(id)))
bad('declaration targets');
const edges = [...q.edges]
.sort((a, b) => a.id < b.id ? -1 : Number(a.id > b.id));
const ds = new Set(decls), srcs = new Map(), fs = new Map(), ctrls = new Map(), ents = new Map(), cuts = new Map(), refs = new Map(), used = new Set();
const node = (id) => {
if (!i.graph.hasNode(id))
bad(id);
return i.graph.nodeAttributes(id);
};
const load = (path) => {
const old = srcs.get(path);
if (old)
return old;
const hash = i.file_hashes.get(path);
if (hash === undefined)
halt('stale', path);
let file, buf;
try {
const root = realpathSync(i.root_path);
file = realpathSync(resolve(root, path));
const rel = relative(root, file);
if (isAbsolute(path) || rel === '..'
|| rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
halt('unavailable', path);
}
buf = readFileSync(file);
}
catch (err) {
if (err instanceof Halt)
throw err;
halt('unavailable', path);
}
if (createHash('sha256').update(buf).digest('hex') !== hash)
halt('stale', path);
if (!isUtf8(buf))
bad(path);
const text = buf.toString('utf8'), id = `f${fs.size}`;
const row = [path, hash, text, ...lines(text), id];
srcs.set(path, row);
fs.set(path, [id, hash]);
return row;
};
const auth = (src, r, hash, key = src[0], keep = true) => {
const text = clip(src, r);
if (text === null)
bad(key);
const sum = createHash('sha256').update(text).digest('hex');
if (hash !== undefined && sum !== hash)
bad(key);
if (!keep)
return '';
const sig = `${src[1]}\0${r.start.line}:${r.start.column}:${r.end.line}:${r.end.column}\0${sum}`;
const old = cuts.get(sig);
if (old)
return old[0];
const id = `x${cuts.size}`;
cuts.set(sig, [id, src[5], r, sum, text]);
return id;
};
const ent = (id) => {
const old = ents.get(id);
if (old)
return old[0];
const a = node(id), ref = `e${ents.size}`, ch = i.channels_by_id.get(id);
if (ch) {
ents.set(id, [ref, 'channel', ch.channel_kind, ch.transport,
ch.key, ch.parent_channel_id, ch.scope]);
if (ch.parent_channel_id)
ent(ch.parent_channel_id);
return ref;
}
const path = a.source_file, label = a.label, kind = a.node_kind, decl = a.declaration_range;
if (kind === 'file')
bad(id);
const src = load(path), proof = ds.has(id) ? auth(src, decl, undefined, id) : undefined;
ents.set(id, [ref, 'symbol', label, kind, src[5]]);
if (proof) {
refs.set(id, [`p${refs.size}`, 'declaration', ref, proof]);
used.add(ref);
}
return ref;
};
const fact = (v, exact, keep = true) => {
const owner = v.owner_symbol_id, own = ents.get(owner)?.[0]
?? (exact && keep ? ent(owner) : '');
if (v.kind === 'call' && v.target_symbol_id) {
node(v.target_symbol_id);
if (ents.has(v.target_symbol_id))
used.add(ent(v.target_symbol_id));
}
const src = load(node(owner).source_file), ctrl = ['condition', 'loop', 'parallel'].includes(v.kind);
const stmt = auth(src, v.evidence.statement_range, v.evidence.excerpt_sha256, v.id, keep && !ctrl);
return [own, ctrl ? auth(src, v.evidence.range, undefined, v.id, keep) : stmt];
};
const callAt = (edge, owner, ok, keep = true) => {
const hits = (i.operations_by_owner.get(owner) ?? [])
.filter((v) => v.kind === 'call'
&& v.owner_symbol_id === owner && ok(v));
if (hits.length !== 1)
bad(edge);
const hit = hits[0];
if (i.operation_by_id.get(hit.id) !== hit)
bad(edge);
return fact(hit, false, keep)[1];
};
const link = (id, from, to, rel, cut) => {
refs.set(id, [`p${refs.size}`, 'edge', from, to, rel, cut]);
used.add(from);
used.add(to);
};
const ranged = (id, from, to, rel, file, range) => {
refs.set(id, [`p${refs.size}`, 'edge_range', from, to, rel, file, range]);
used.add(from);
used.add(to);
};
for (const id of nodes) {
if (i.channels_by_id.has(id))
bad(id);
ent(id);
}
for (const id of checks) {
const value = i.operation_by_id.get(id);
if (!value)
bad(id);
fact(value, true, false);
if (['condition', 'loop', 'parallel'].includes(value.kind)) {
const src = load(node(value.owner_symbol_id).source_file);
ctrls.set(id, [src[5], value.evidence.range]);
}
}
for (const id of ops) {
const value = i.operation_by_id.get(id);
if (!value)
bad(id);
const [owner, excerpt] = fact(value, true), ref = `e${ents.size}`;
ents.set(id, [ref, 'operation', owner, value]);
refs.set(id, [`p${refs.size}`, 'operation', ref, excerpt]);
used.add(owner);
}
for (const edge of edges) {
const hits = i.graph.edgesBetween(edge.fromId, edge.toId)
.filter((hit) => hit.id === edge.id);
if (hits.length !== 1)
bad(edge.id);
const a = hits[0].attributes, rel = a.relation;
if (edge.relation !== undefined && edge.relation !== rel)
bad(edge.id);
const from = ent(edge.fromId), to = ent(edge.toId);
const ev = a.evidence;
if (rel === 'calls') {
const cut = callAt(edge.id, edge.fromId, (call) => call.target_symbol_id === edge.toId && same(call.evidence.range, ev.range)
&& ev.source === (call.source === 'framework'
? 'framework-decorator' : call.source));
link(edge.id, from, to, rel, cut);
continue;
}
const owner = a.execution_owner_id, stmt = ev.statement_range, sum = ev.excerpt_sha256, path = a.source_file;
if (rel === 'consumed_by' && owner !== edge.toId) {
callAt(edge.id, owner, (call) => same(stmt, call.evidence.statement_range)
&& call.evidence.excerpt_sha256 === sum, false);
}
const src = load(path);
auth(src, stmt, sum, edge.id, false);
auth(src, ev.range, undefined, edge.id, false);
ranged(edge.id, from, to, rel, src[5], ev.range);
}
for (const [id, entry] of ents) {
if (entry[1] === 'symbol' && !used.has(entry[0]))
bad(id);
}
return {
state: 'ready', files: fs, controls: ctrls,
excerpts: cuts, entities: ents, proofs: refs,
};
}
export function hydrateEvidence(index, input) {
try {
return index.state === 'ready' ? ready(index, input)
: { state: index.state, subject: index.subject };
}
catch (error) {
return error instanceof Halt ? error.value
: { state: 'corrupt', subject: 'evidence hydration' };
}
}
import type { NormalizedRetrieveRequest, QuestionPlanResult } from './types.js';
export declare function lexicalTokens(value: string): string[];
export declare function planQuestion(request: NormalizedRetrieveRequest): QuestionPlanResult;
const OWNER = 'file|module|class|function|method|service|handler|worker|component|controller|repository';
const sets = (...values) => values.map((value) => new Set(value.split(' ')));
const [FLOW, LOCATE, EXPLAIN, COMMON] = sets('flow workflow pipeline lifecycle generate run execute create build produce process work', 'locate find define declare implement contain handle own write read save set update persist publish consume store use live', 'explain describe work behave operate handle process validate resolve compute calculate score select update apply evaluate mean control do use choose return reject allow call invoke', `a an the this that these those it its they them their we our you your i me my he she what which who where when why how does do did is are was were be been being can could would should will may might must get of for with without by in into on at as and or but if then than from through via to after before during while all every any some each please show trace explain describe end complete initial final full entire code ${OWNER.replaceAll('|', ' ')} definition declaration implementation behavior happen`);
const ACTIONS = new Set([...FLOW, ...LOCATE, ...EXPLAIN, 'complete', 'get', 'happen', 'plan']);
const BEHAVIOR = new Set('apply allow calculate choose compute consume control evaluate persist publish read reject resolve return save score select store update validate write'.split(' '));
const IRREGULAR = new Map('built=build generation=generate got=get getting=get persistence=persist planned=plan planning=plan ran=run running=run setting=set written=write wrote=write'.split(' ').map((pair) => pair.split('=')));
function canonical(value) {
const mapped = IRREGULAR.get(value);
if (mapped)
return mapped;
const ing = value.endsWith('ing') ? value.slice(0, -3) : '';
const past = value.endsWith('ed') ? value.slice(0, -2) : '';
const forms = [value,
/i(?:es|ed)$/u.test(value) ? `${value.slice(0, -3)}y` : '',
ing, ing ? `${ing}e` : '', past, past ? `${past}e` : '',
value.endsWith('es') ? value.slice(0, -2) : '',
value.endsWith('s') ? value.slice(0, -1) : ''];
const action = forms.find((candidate) => ACTIONS.has(candidate));
if (action)
return action;
if (value.length <= 4)
return value;
if (value.endsWith('ies'))
return `${value.slice(0, -3)}y`;
return /(?<!s|u|i)s$/u.test(value) ? value.slice(0, -1) : value;
}
export function lexicalTokens(value) {
const raw = value.normalize('NFKC')
.replace(/([\p{Ll}\p{N}])([\p{Lu}])/gu, '$1 $2')
.replace(/[’']s\b/giu, '')
.toLowerCase();
return (raw.match(/[\p{L}\p{N}]+/gu) ?? []).map(canonical);
}
const [FW, LW, EW] = [FLOW, LOCATE, EXPLAIN].map((set) => [...set].join('|'));
const AUX = 'is|are|was|were|does|do|did|can|could|would|should|will';
const CLAUSE = 'when|after|before|on|during|if|from|through|via';
const FN = 'flow|workflow|pipeline|lifecycle';
const isNoise = (token, mode) => COMMON.has(token) || (mode === 'workflow' ? FLOW.has(token)
: mode === 'locate' ? LOCATE.has(token)
: EXPLAIN.has(token) && !BEHAVIOR.has(token));
const content = (value, mode, plain = false) => [...new Set(lexicalTokens(value).filter((token) => !(plain ? COMMON.has(token) : isNoise(token, mode))))];
function pick(text, mode, rules, plain = false) {
for (const rule of rules) {
const topic = content(rule.exec(text)?.slice(1).join(' ') ?? '', mode, plain).join(' ');
if (topic)
return topic;
}
return '';
}
function coordinatedFlow(text) {
const entry = /\b(?:accept|receive|submit|handle) (.+?)(?= (?:schedule|enqueue|queue|dispatch|research|process|compose|assemble|render|write|save|store|persist)\b)/u
.exec(text), end = /\b(?:write|save|store|persist) (.+)$/u.exec(text), composed = /\b(compose|assemble|render) (.+?)(?= (?:and )?(?:write|save|store|persist)\b|$)/u
.exec(text);
if (!entry || !end)
return undefined;
const input = content(entry[1], 'workflow');
let output = content(composed?.[2] ?? end[1], 'workflow');
if (composed && /^(?:model|output|result)$/u.test(output.at(-1) ?? ''))
output = content(end[1], 'workflow');
const first = input[0], raw = output.at(-1), last = /^(?:model|output|result)$/u.test(raw ?? '') ? first : raw;
if (!first || !last)
return undefined;
const handoff = /\b(?:schedule|enqueue|queue|dispatch|publish|emit)\b/u.test(text)
? 'schedule' : undefined, stages = [
/\b(?:research|investigate|discover)\b/u.test(text) ? 'research' : '',
/\b(?:compose|assemble|render|synthesize|merge)\b/u.test(text)
? 'assemble' : '',
].filter(Boolean);
return {
subject: first === last ? first : `${first} ${last}`,
entry: `request ${first}`, ...(stages.length ? { stage: stages.join(' ') } : {}),
...(handoff ? { handoff } : {}), terminal: 'persistence',
terms: [...new Set([first, last, ...stages, ...(handoff ? [handoff] : [])])],
};
}
function flowSubject(text) {
const direct = pick(text, 'workflow', [
/\bwhat happen when (?:(?:a|an|the) )?(?:user|client|caller) (?:request|submit) (.+)$/,
]) || pick(text, 'workflow', [
/\bwalk (?:me )?through (.+?)(?= from\b| via\b| to\b|$)/,
]) || pick(text, 'workflow', [
/\btrace (?:the )?(.+)(?= from .+ (?:to|through|via)\b)/,
/\bwhat \w+ (?:the )?(.+?) that/,
], true) || pick(text, 'workflow', [
RegExp(`\\bhow (?:is|are|was|were) (.+?) (?:${FW})\\b`),
RegExp(`\\bhow (?:does|do|did) (.+?) get (?:${FW})\\b`),
]);
if (direct)
return [direct, []];
const active = RegExp(`\\bhow (?:(?:${AUX}) )?(.+?) (${FW}) (.+?)(?= (?:${CLAUSE})\\b| end to end\\b|$)`).exec(text);
if (active) {
const object = content(active[3], 'workflow').join(' ');
if (object)
return [object, content(active[1], 'workflow', true)];
}
const topic = pick(text, 'workflow', [
/^follow (?:\S+ )*?(\S+) from .+ until (?:\S+ )*?(\S+) is/,
/for (.+?) generate/,
/\btrace (?:the )?(.+?)(?= through\b| via\b| to\b|$)/,
RegExp(`\\b(?:${FN}) (?:of|for) (.+?)(?= from\\b|$)`),
RegExp(`(.+?) (?:${FN})\\b`),
RegExp(`\\bhow (?:(?:${AUX}) )?(.+?) (?:${FW})\\b`),
/\w+ (?:an?|the) (\S+)/,
]);
return [topic || content(text, 'workflow').join(' '), []];
}
function flowBounds(text) {
if (/^follow /.test(text)
|| !(/\b(?:from|via)\b/u.test(text) || /^(?:trace|walk)\b.*\bthrough\b/u
.test(text)))
return {};
const read = (rule) => {
const value = lexicalTokens(rule.exec(text)?.[1] ?? '')
.filter((token) => !COMMON.has(token)).join(' ');
return value || undefined;
};
const walked = /^walk (?:me )?through\b/u.test(text);
return {
entry: read(/\bfrom (.+?)(?= (?:through(?: to)?|via|to)\b| how\b|$)/u),
stage: read(/\bvia (.+?)(?= to\b| how\b|$)/u)
?? read(/\bfrom .+?\bthrough (?!to\b)(.+?)(?= to\b| how\b|$)/u)
?? (walked ? undefined
: read(/\bthrough (?!to\b)(.+?)(?= to\b| how\b|$)/u)),
terminal: read(/\b(?:through to|to) (.+?)(?= how\b|$)/u),
};
}
function simpleSubject(text, mode) {
const locate = mode === 'locate';
const rules = locate ? [
RegExp(`\\bwhere (?:(?:${AUX}) )?(.+?)(?= (?:${LW})\\b| (?:${CLAUSE})\\b|$)`),
RegExp(`\\b(?:(?:which|what) (?:${OWNER}) |what )(?:${LW}) (.+?)(?= (?:${CLAUSE})\\b|$)`),
/\b(?:locate|find)(?: the)? (.+?)(?= (?:definition|declaration|implementation)\b|$)/,
/\b(?:definition|declaration|implementation) (?:of|for) (.+)$/,
] : [
RegExp(`\\bwhich (?:${OWNER}) \\S+ (.+)$`),
RegExp(`\\bwhat (?:${FW}|${EW}) (.+)$`),
RegExp(`\\bhow (?:${AUX}) (.+?) (?:create|build|produce)\\b`),
RegExp(`\\b(?:how|why|what) (?:(?:${AUX}) )?(.+?) (?:${EW})\\b`),
/\b(?:explain|describe)(?: how)?(?: the)? (.+)$/,
];
return pick(text, mode, rules, locate || /\b(?:call|invoke)\b/u.test(text))
|| content(text, mode).join(' ');
}
export function planQuestion(request) {
const raw = request.question.normalize('NFKC'), text = lexicalTokens(request.question).join(' '), tokens = text.split(' '), names = [...raw.matchAll(/(?<![\p{L}\p{N}_$])([\p{L}_$][\p{L}\p{N}_$]*(?:\.[\p{L}_$][\p{L}\p{N}_$]*)+)/gu)]
.map((match) => match[1]), ident = /\bwhere\s+(?:is|are|was|were)\s+[`'"]?([\p{L}_$][\p{L}\p{N}_$.-]*)[`'"]?\s+(?:defined|declared|implemented)\b/iu
.exec(raw)?.[1], ownerQuery = RegExp(`\\b(?:which|what) (?:${OWNER}) (?:${LW})\\b`).test(text);
const mode = ownerQuery ? 'locate'
: /\b(?:end to end|what happen when)\b|\bfrom\b.+\b(?:through|via)\b.+\bto\b|\btrace\b.+\bfrom\b.+\bto\b/.test(text)
|| /^follow /.test(text)
|| /^which .+\b(?:save|write)\b/.test(text)
|| /\bhow\b[\s\S]*\bgenerat(?:e|ed|es|ing)\b/iu.test(raw)
|| /\bhow\s+(?:(?:does|do|did|can|could|would|should|will)\s+)?(?!(?:does|do|did|can|could|would|should|will)\b)[\p{L}_$][\p{L}\p{N}_$]*\s+(?:generate|run|execute|create|build|produce|process)\b/iu.test(raw)
? 'workflow'
: /\b(?:where|locate|find|definition|declaration|implementation)\b/.test(text)
|| RegExp(`\\bwhat (?:${LW})\\b`).test(text) ? 'locate'
: /^trace\b|\b(?:flow|workflow|pipeline|lifecycle)\b/.test(text) ? 'workflow'
: /\b(?:explain|describe|how|why|behavior)\b|\bwhat (?:does|do|is|are)\b/
.test(text)
|| RegExp(`\\b(?:what (?:${FW}|${EW})|which (?:${OWNER}))\\b`)
.test(text) ? 'explain' : undefined;
if (!mode) {
return {
status: 'unsupported', reason: 'unsupported_intent',
terms: [...new Set(tokens.filter((token) => !COMMON.has(token)))].sort(),
};
}
const coordinated = mode === 'workflow' ? coordinatedFlow(text) : undefined;
const [topic, ignored] = mode === 'workflow'
? coordinated ? [coordinated.subject, []] : flowSubject(text)
: [names[0]
? content(names[0], mode, true).join(' ')
: mode === 'locate' && ident
? content(ident, 'locate', true).join(' ')
: simpleSubject(text, mode), []];
const span = coordinated
?? (mode === 'workflow' ? flowBounds(text) : {});
if (mode === 'workflow' && names.length) {
span.stage = names.flatMap(lexicalTokens).join(' ');
}
const skip = new Set(ignored);
const terms = new Set((coordinated?.terms ?? tokens).filter((token) => !isNoise(token, mode) && !skip.has(token)));
lexicalTokens(topic).forEach((token) => terms.add(token));
const sorted = [...terms].sort();
if (!topic || sorted.length === 0) {
return { status: 'unsupported', reason: 'missing_subject', terms: sorted };
}
const words = new Set(tokens);
const access = mode !== 'locate' ? undefined
: ['read', 'find'].some((word) => words.has(word)) ? 'read'
: ['write', 'save', 'set', 'update', 'persist', 'store']
.some((word) => words.has(word)) ? 'write' : undefined;
const kinds = mode === 'locate' ? ['subject']
: mode === 'explain' ? ['subject', 'behavior']
: ['subject', 'entry', 'stage', 'handoff', 'behavior', 'ordering', 'terminal'];
const rest = RegExp(`^(?:what (?:${FW})|how (?:is|are|was|were) .+ (?:${FW}))\\b`)
.test(text) ? []
: sorted.filter((token) => !lexicalTokens(topic).includes(token));
return {
status: 'supported',
plan: {
intent: mode, subject: topic, terms: sorted,
obligations: kinds.map((kind, index) => ({
id: `o${index + 1}`, kind,
target: kind === 'entry' ? span.entry ?? topic
: kind === 'stage' ? span.stage ?? topic
: kind === 'handoff' ? span.handoff ?? topic
: kind === 'terminal' ? span.terminal ?? topic
: kind === 'behavior' && mode === 'explain' && rest.length
? rest.join(' ') : topic,
mandatory: true,
})),
...(access ? { access } : {}),
},
};
}
import type { ReadyQueryIndex } from './index-status.js';
import { type QueryPlan, type WorkflowSelection } from './types.js';
export declare function selectWorkflow(i: ReadyQueryIndex, plan: QueryPlan): WorkflowSelection;
import { compareCodeUnits as cmp } from '../graph/canonical-json.js';
import { lexicalTokens as words } from './plan.js';
import { sourceDomainOf as domainOf } from './source-domain.js';
import { valueHas, } from './types.js';
const [CANDIDATES, NODES, HOPS, RECOVERY] = [32, 512, 24, 64];
const FAILURE_WORD = /^(?:abort|cancel|error|fail(?:ed|ure)?|refund|reject(?:ed)?|retry|rollback)$/u, READ = /^(?:read|file_read|object_read)$/u, GENERIC_TERMINAL = /^(?:data|output|persist|persistence|record|report|result|storage|store|write)$/u, DATABASE = /^(?:database|db|mongo|mongodb|repository|sql)$/u;
const MISSING_CODES = {
handoff: 'adjacent_handoff_unproven', behavior: 'behavior_unproven',
subject: 'subject_unproven', entry: 'entrypoint_unproven',
terminal: 'terminal_persistence_unproven',
};
const cache = new WeakMap();
function append(map, key, value) {
map.get(key)?.push(value) ?? map.set(key, [value]);
}
const text = (attrs, key) => typeof attrs[key] === 'string' ? attrs[key] : '';
const factText = (fact) => fact.kind === 'call' ? fact.callee
: fact.kind === 'persistence'
? `${fact.receiver_type} ${JSON.stringify(fact.resource ?? '')}`
: fact.kind === 'mutation' ? fact.target
: fact.kind === 'literal' ? JSON.stringify(fact.value)
: fact.kind === 'return' || fact.kind === 'throw'
? `${fact.kind} ${JSON.stringify(fact.value ?? '')}`
: fact.kind;
const behavior = (fact) => fact.kind !== 'literal';
const adverse = (value) => words(value).some((word) => word !== 'retry' && FAILURE_WORD.test(word));
const terminal = (fact) => fact.kind === 'persistence' && !READ.test(fact.operation);
function bad(fact) {
return !!fact && (fact.control.some((frame) => frame.kind === 'exception' && frame.arm === 'catch')
|| fact.kind === 'call'
&& (adverse(fact.callee)
|| fact.arguments.some((argument) => valueHas(argument, (value) => value.kind === 'literal'
&& typeof value.value === 'string'
&& adverse(value.value)))));
}
const hasTerminal = (v, id, adverse) => v[1].get(id)?.[5].some((fact) => terminal(fact)
&& (adverse || !bad(fact) && !bad(v[6].get(fact.call_fact_id)))) ?? false;
function rangeKey(value) {
return `${value?.start?.line}:${value?.start?.column}:${value?.end?.line}:${value?.end?.column}`;
}
const idsOf = (arc) => arc[3].map((edge) => edge[0]);
const penalty = (domain) => domain === 'production' ? 0 : domain === 'unknown' ? 4 : 32;
function isRequest(attrs) {
const role = text(attrs, 'framework_role'), kind = text(attrs, 'node_kind');
return kind === 'route'
|| /(?:_route|_api|_server_action|router_(?:loader|action)|trpc_procedure_)/u
.test(role);
}
function buildView(i) {
const prior = cache.get(i);
if (prior)
return prior;
const nodes = [];
for (const [id, attrs] of i.graph.nodeEntries()) {
if (['channel', 'file'].includes(text(attrs, 'node_kind')))
continue;
const facts = i.operations_by_owner.get(id) ?? [], file = text(attrs, 'source_file'), name = [
text(attrs, 'label'), text(attrs, 'qualified_name'),
text(attrs, 'node_kind'), text(attrs, 'framework_role'),
].join(' '), nameWords = words(name), lexicon = words([name, file, ...facts.map(factText)].join(' '));
nodes.push([id, lexicon.join(''), nameWords.join(''),
new Set(lexicon), new Set(nameWords), facts, facts.some(terminal),
domainOf(attrs.source_domain, file, i.root_path), isRequest(attrs)]);
}
const byId = new Map(nodes.map((symbol) => [symbol[0], symbol])), exact = new Map(), routes = new Map(), subs = new Map(), pubs = [], nonEntries = new Set();
for (const [from, to, attrs, id] of i.graph.edgeEntries()) {
const relation = String(attrs.relation);
const evidence = attrs.evidence;
if (!['calls', 'publishes_to', 'routes_through', 'consumed_by'].includes(relation)
|| !/^(?:typescript-(?:semantic|syntactic)|framework-decorator|wrapper-summary)$/u
.test(String(evidence?.source)))
continue;
const owner = text(attrs, 'execution_owner_id'), edgeRange = rangeKey(evidence?.range), statement = rangeKey(evidence?.statement_range), bind = relation === 'publishes_to' ? from
: relation === 'consumed_by' && owner !== to ? owner : '', calls = bind ? (i.operations_by_owner.get(bind) ?? []).filter((fact) => fact.kind === 'call'
&& (relation === 'consumed_by' || rangeKey(fact.evidence.range)
=== edgeRange)
&& rangeKey(fact.evidence.statement_range) === statement
&& fact.evidence.excerpt_sha256 === evidence?.excerpt_sha256)
: [], payload = attrs.dispatch_payload_argument, edge = [id, from, to, relation,
`${edgeRange}\0${statement}`, owner, calls.length === 1 ? calls[0].id : '',
typeof payload === 'number' && Number.isSafeInteger(payload) && payload >= 0
? payload : undefined];
if (byId.has(to) && (relation === 'consumed_by'
|| relation === 'calls' && byId.get(from)?.[7] === 'production'))
nonEntries.add(to);
if (relation === 'calls') {
const key = `c\0${from}\0${to}\0${edgeRange}`;
if (!exact.has(key))
exact.set(key, edge);
}
else if (relation === 'routes_through')
append(routes, `${from}\0${to}`, edge);
else if (relation === 'consumed_by')
append(subs, from, edge);
else
pubs.push(edge);
}
let arcs = [];
for (const owner of nodes) {
for (const fact of owner[5]) {
if (fact.kind !== 'call' || !fact.target_symbol_id
|| !byId.has(fact.target_symbol_id))
continue;
const edge = exact.get(`c\0${owner[0]}\0${fact.target_symbol_id}\0${rangeKey(fact.evidence.range)}`);
if (edge)
arcs.push([owner[0], fact.target_symbol_id, 'direct', [edge], [fact.id]]);
}
}
for (const pub of pubs) {
if (!byId.has(pub[1]) || !i.channels_by_id.has(pub[2]))
continue;
const channel = i.channels_by_id.get(pub[2]), routed = channel.channel_kind === 'job'
? (routes.get(`${channel.id}\0${channel.parent_channel_id}`) ?? [])
.filter((edge) => edge[5] === pub[1] && edge[4] === pub[4])
: [], route = routed.length === 1 ? routed[0] : undefined;
if (channel.channel_kind === 'job' && !route)
continue;
const dest = route?.[2] ?? channel.id;
for (const sub of subs.get(dest) ?? []) {
if (!byId.has(sub[2]))
continue;
const binding = !sub[5] || sub[5] === sub[2]
? [] : sub[6] ? [sub[6]] : undefined;
if (!binding)
continue;
const edges = route ? [pub, route, sub] : [pub, sub];
arcs.push([pub[1], sub[2], 'channel', edges,
pub[6] ? [pub[6], ...binding] : []]);
}
}
const d = new Set(), c = new Set();
for (const a of arcs) {
const k = `${a[0]}\0${a[1]}`;
if (a[2] === 'channel')
c.add(k);
else
a[4].forEach((id) => d.add(`${k}\0${id}`));
}
const h = new Set();
arcs = arcs.filter((arc) => arc[2] !== 'channel' || !arc[4].some((id) => {
const fact = i.operation_by_id.get(id);
if (fact?.kind !== 'call' || !fact.target_symbol_id)
return false;
const r = d.has(`${arc[0]}\0${fact.target_symbol_id}\0${id}`)
&& c.has(`${fact.target_symbol_id}\0${arc[1]}`);
if (r)
arc[3].forEach((edge) => h.add(edge[0]));
return r;
}));
arcs.sort((a, b) => cmp(a[0], b[0]) || cmp(a[1], b[1])
|| cmp(a[3][0][0], b[3][0][0]));
const outgoing = new Map(), incoming = new Map();
for (const arc of arcs) {
append(outgoing, arc[0], arc);
append(incoming, arc[1], arc);
}
const used = new Set([...arcs.flatMap(idsOf), ...h]), blocked = new Set(pubs.filter((edge) => byId.has(edge[1])
&& !used.has(edge[0])).map((edge) => edge[1])), v = [
nodes, byId, outgoing, incoming, blocked, nonEntries, i.operation_by_id,
];
cache.set(i, v);
return v;
}
function score(symbol, goals) {
let result = 0;
for (const target of goals) {
const terms = words(target), compact = terms.join('');
if (symbol[2].includes(compact))
result += 128;
if (terms.every((term) => symbol[4].has(term)))
result += 64;
if (symbol[1].includes(compact))
result += 32;
for (const term of terms) {
if (symbol[3].has(term))
result += 8;
if (symbol[4].has(term))
result += 8;
}
}
return result;
}
function rootRank(v, symbol, lexical) {
const degree = (v[3].get(symbol[0])?.length ?? 0)
+ (v[2].get(symbol[0])?.length ?? 0)
+ (v[4].has(symbol[0]) ? 1 : 0);
return lexical - penalty(symbol[7]) - Math.min(24, Math.max(0, degree - 8) * 2)
- (degree === 0 && !symbol[6] ? 12 : 0)
- (symbol[6] ? 16 : 0);
}
function adverseArc(v, arc) {
return arc[4].some((id) => {
const fact = v[6].get(id);
if (bad(fact))
return true;
if (arc[2] !== 'channel' || fact?.kind !== 'call' || !fact.target_symbol_id) {
return false;
}
const matching = (v[2].get(fact.target_symbol_id) ?? []).filter((inner) => inner[2] === 'channel' && inner[1] === arc[1]);
return matching.length > 0 && matching.every((inner) => inner[4].some((operation) => bad(v[6].get(operation))));
});
}
function reach(v, seeds, back, cap, allow, allowed, stops) {
const dist = new Map(seeds.map((seed) => [seed, 0]));
const actual = new Set(seeds), queue = [...seeds];
const prev = new Map(), extra = new Set();
let bounded = false;
while (queue.length > 0) {
queue.sort((left, right) => dist.get(left) - dist.get(right)
|| cmp(left, right));
const at = queue.shift();
if (stops?.has(at))
continue;
const base = dist.get(at), arcs = (back ? v[3] : v[2]).get(at) ?? [];
for (const arc of arcs) {
if (allow && !allow(arc))
continue;
const next = back ? arc[0] : arc[1], hops = base + arc[3].length;
if (hops > HOPS) {
extra.add(next);
continue;
}
if ((allowed && !allowed.has(next)) || (dist.get(next) ?? Infinity) <= hops)
continue;
const added = [...new Set(arc[3].flatMap((edge) => [edge[1], edge[2]]))]
.filter((id) => !actual.has(id));
if (actual.size + added.length > cap) {
bounded = true;
continue;
}
added.forEach((id) => actual.add(id));
dist.set(next, hops);
prev.set(next, arc);
if (!queue.includes(next))
queue.push(next);
}
}
return [
dist, actual, bounded || [...extra].some((id) => !dist.has(id)),
prev,
];
}
function orderBy(left, right) {
let i = 0;
while (i < left.length && i < right.length && left[i] === right[i])
i += 1;
return (left[i] ?? 0) - (right[i] ?? 0) || left.length - right.length;
}
function corridor(v, root, cap, goals, fail, endNeed, channelEnd = false) {
const allow = fail ? undefined : (arc) => !adverseArc(v, arc), fwd = reach(v, [root], false, cap, allow);
const found = [...fwd[0].keys()].filter((id) => hasTerminal(v, id, fail)), wanted = endNeed
? pickIds(v, found, endNeed, 'terminal') : [], options = endNeed ? wanted : found, sinceChannel = (id) => {
let direct = 0;
for (let at = id; at !== root;) {
const arc = fwd[3].get(at);
if (!arc)
return Infinity;
if (arc[2] === 'channel')
return direct;
direct += 1;
at = arc[0];
}
return Infinity;
}, originals = options.filter((id) => v[1].get(id)?.[5].some((fact) => terminal(fact) && fact.source !== 'wrapper-summary')), terminalOptions = originals.length > 0 ? originals : options, channelDistance = channelEnd
? Math.min(...terminalOptions.map(sinceChannel)) : Infinity, channelOptions = Number.isFinite(channelDistance)
? terminalOptions.filter((id) => sinceChannel(id) === channelDistance) : [], eligible = channelOptions.length > 0 ? channelOptions : terminalOptions;
const exact = pickIds(v, eligible, goals[0] ?? '', 'exact'), related = exact.length > 0 ? exact
: eligible.filter((id) => score(v[1].get(id), goals) > 0), zone = related.length > 0
? reach(v, related, false, cap, allow, fwd[0])[0] : undefined, pool = (zone ? eligible.filter((id) => zone.has(id)) : eligible)
.sort((left, right) => fwd[0].get(right) - fwd[0].get(left)
|| score(v[1].get(right), goals) - score(v[1].get(left), goals)
|| cmp(left, right));
const allowed = fwd[0];
let ends = pool.filter((id) => {
const below = reach(v, [id], false, cap, allow, allowed)[0];
return !pool.some((other) => other !== id && below.has(other));
});
if (ends.length === 0)
ends = pool.slice(0, 1);
const exactEnds = ends.filter((id) => exact.includes(id));
if (exactEnds.length > 0)
ends = exactEnds;
const chosen = ends.length > 0 ? fwd
: reach(v, [root], false, cap, allow, undefined, v[4]), backward = reach(v, ends, true, cap, allow, allowed);
let nodes = new Set([...chosen[0].keys()].filter((id) => ends.length === 0 || backward[0].has(id)));
let arcs = [...v[2].values()].flat().filter((arc) => nodes.has(arc[0]) && nodes.has(arc[1]) && (!allow || allow(arc)));
const hopCount = new Set(arcs.flatMap(idsOf)).size;
const pruned = hopCount > HOPS;
if (pruned) {
const walks = ends.map((terminal) => {
const path = [];
for (let id = terminal; id !== root;) {
const arc = fwd[3].get(id);
if (!arc)
return [];
path.unshift(arc);
id = arc[0];
}
return path;
});
const need = [...new Map(walks.flat().map((arc) => [idsOf(arc).join('\0'), arc])).values()];
arcs = new Set(need.flatMap(idsOf)).size <= HOPS ? need : walks[0] ?? [];
nodes = new Set([root, ...arcs.flatMap((arc) => [arc[0], arc[1]])]);
}
arcs.sort((left, right) => (fwd[0].get(left[0]) ?? Infinity) - (fwd[0].get(right[0]) ?? Infinity)
|| (fwd[0].get(left[1]) ?? Infinity) - (fwd[0].get(right[1]) ?? Infinity)
|| cmp(left[0], right[0]) || cmp(left[1], right[1])
|| cmp(left[3][0][0], right[3][0][0]));
return [nodes, arcs, ends.filter((id) => nodes.has(id)),
fwd[1], ends.length === 0 && (fwd[2] || chosen[2]) || pruned];
}
function incomingArm(i, a, c) {
if (c.test?.kind !== 'template')
return;
const [root, ...path] = c.test.parts;
if (root?.kind !== 'parameter' || root.position !== 0
|| !path.every((p) => p.kind === 'literal' && typeof p.value === 'string'))
return;
const pub = a[3][0], at = pub[7], call = i.operation_by_id.get(a[4][0]), ch = i.channels_by_id.get(pub[2]);
if (at === undefined || call?.kind !== 'call')
return;
let v = call.arguments[at];
if (ch.transport.startsWith('bull')) {
const key = path.shift()?.value;
if (key === 'name' && ch.channel_kind === 'job' && !path[0])
v = { kind: 'literal', value: ch.key };
else if (key !== 'data')
return;
}
for (const p of path)
v = v?.kind === 'object'
? v.entries.find((e) => e.key === p.value)?.value : undefined;
if (v?.kind !== 'literal')
return;
return `case:${Buffer.from(JSON.stringify([
typeof v.value, v.value,
])).toString('base64url')}`;
}
function endFacts(i, arc, facts) {
const cases = (i.operations_by_owner.get(arc[1]) ?? []).filter((fact) => fact.kind === 'condition'
&& fact.condition_kind === 'switch');
if (!cases[0])
return facts.filter((fact) => !fact.control.length);
const hits = cases.flatMap((cond) => {
const arm = incomingArm(i, arc, cond);
if (!arm)
return [];
const valid = facts.filter((fact) => fact.control.some((frame) => frame.kind === 'branch' && frame.controller_fact_id === cond.id
&& frame.arm === arm));
return valid[0] ? [valid] : [];
});
return hits.length === 1 ? hits[0] : [];
}
function controls(i, arcs, ends, seeds) {
const ops = i.operation_by_id;
const ok = (a, b) => b[4].every((id) => {
const op = ops.get(id);
return !op || op.owner_symbol_id !== b[0]
|| op.control.every((f) => {
if (f.kind !== 'branch')
return true;
const ctl = ops.get(f.controller_fact_id);
if (ctl?.kind !== 'condition' || ctl.condition_kind !== 'switch')
return true;
const arm = incomingArm(i, a, ctl);
return arm === f.arm;
});
});
const factIds = [...new Set(arcs.flatMap((arc) => arc[4]))], core = new Set([...seeds, ...factIds]);
const endOps = new Set();
for (const id of ends) {
const options = (i.operations_by_owner.get(id) ?? []).filter(terminal), incoming = arcs.filter((arc) => arc[1] === id && arc[2] === 'channel'), groups = incoming.length > 0
? incoming.map((arc) => endFacts(i, arc, options)) : [options], chosen = groups.map((group) => group.filter((entry) => !bad(entry)
&& !bad(ops.get(entry.call_fact_id))).at(-1));
if (chosen.some((fact) => !fact))
continue;
for (const fact of chosen) {
core.add(fact.id);
endOps.add(fact.id);
}
}
const groupsBy = new Map(), seqs = new Map();
const need = new Set(core);
let proven = true;
const ins = arcs.filter((arc) => arc[2] === 'channel');
for (const left of ins) {
const outs = arcs.filter((right) => right[0] === left[1]);
if (outs.length > 0 && !outs.some((right) => ok(left, right)))
proven = false;
}
for (const right of arcs) {
const froms = ins.filter((left) => left[1] === right[0]);
if (froms.length > 0 && !froms.some((left) => ok(left, right)))
proven = false;
}
for (const id of need) {
const fact = ops.get(id);
if (!fact) {
proven = false;
continue;
}
if (factIds.includes(id) && fact.kind === 'call'
&& !fact.control.some((frame) => frame.kind === 'parallel')) {
append(seqs, `${fact.owner_symbol_id}\0${JSON.stringify(fact.control)}`, fact);
}
for (const frame of fact.control) {
if (frame.kind === 'exception')
continue;
need.add(frame.controller_fact_id);
const arm = frame.kind === 'branch' ? frame.arm : undefined;
const key = `${frame.kind}\0${frame.controller_fact_id}\0${arm ?? ''}`;
const group = groupsBy.get(key)
?? [frame.kind, frame.controller_fact_id, arm, new Set(), new Set()];
groupsBy.set(key, group);
group[3].add(fact.id);
group[4].add(fact.owner_symbol_id);
}
if (fact.kind === 'parallel') {
fact.member_fact_ids.forEach((member) => need.add(member));
}
if (fact.kind === 'persistence')
need.add(fact.call_fact_id);
}
const ordered = [];
for (const calls of seqs.values())
if (calls.length > 1) {
calls.sort((a, b) => orderBy(a.order, b.order) || cmp(a.id, b.id));
ordered.push({ kind: 'sequence',
operationIds: calls.map((fact) => fact.id),
symbolIds: calls.flatMap((fact) => fact.target_symbol_id ? [fact.target_symbol_id] : []) });
}
const groups = [...groupsBy.values()]
.map(([kind, controllerOperationId, arm, ops, nodes,]) => ({
kind, controllerOperationId, ...(arm ? { arm } : {}),
operationIds: [...ops].sort(cmp), symbolIds: [...nodes].sort(cmp),
})).concat(ordered);
return [[...need].sort(cmp), groups, proven, [...endOps].sort(cmp)];
}
function cycles(nodes, arcs) {
const walks = new Map([...nodes].map((id) => [id, new Set()]));
for (const arc of arcs)
walks.get(arc[0])?.add(arc[1]);
for (const through of nodes)
for (const from of nodes) {
if (!walks.get(from)?.has(through))
continue;
for (const to of walks.get(through) ?? [])
walks.get(from).add(to);
}
const groups = [];
for (const symbol of nodes) {
const members = [...nodes].filter((cand) => walks.get(symbol)?.has(cand) && walks.get(cand)?.has(symbol)).sort(cmp);
if (members[0] !== symbol)
continue;
groups.push({ kind: 'cycle', operationIds: [], symbolIds: members });
}
return groups;
}
function pickIds(v, ids, target, role) {
const lexical = words(target), names = role === 'names', tokens = (id) => v[1].get(id)[names ? 4 : 3], full = (id) => v[1].get(id)[names ? 2 : 1]
.includes(lexical.join('')) || lexical.every((term) => tokens(id).has(term));
if (role === 'entry' && lexical.includes('request')) {
const entries = ids.filter((id) => v[1].get(id)[8]), rest = lexical.filter((token) => token !== 'request');
return rest.length === 0 ? entries : entries.filter((id) => rest.every((token) => v[1].get(id)[3].has(token)));
}
if (role !== 'terminal') {
const exact = ids.filter(full);
if (exact.length > 0 || role === 'exact' || names)
return exact;
const related = ids.filter((id) => lexical.some((term) => tokens(id).has(term)));
return lexical.every((term) => related.some((id) => tokens(id).has(term)))
? related : [];
}
const specific = lexical.filter((token) => !GENERIC_TERMINAL.test(token));
if (specific.length === 0)
return [...ids];
const exact = ids.filter(full);
if (exact.length > 0)
return exact;
return ids.filter((id) => specific.every((token) => {
const lexicon = v[1].get(id)[3];
return lexicon.has(token) || /^(?:database|db)$/u.test(token)
&& [...lexicon].some((cand) => DATABASE.test(cand));
}));
}
function channelFit(i, id, target) {
const channel = i.channels_by_id.get(id);
if (!channel)
return false;
const expected = words(target), key = words(channel.key), actual = words(`${channel.channel_kind} ${channel.transport}`).concat(key), compact = expected.join(''), suffix = key.join(''), forms = `${suffix}\0${actual[0]}${actual[1]}${suffix}\0${actual[1]}${actual[0]}${suffix}`;
return expected.every((token) => actual.includes(token))
|| forms.includes(compact);
}
function stageMatch(i, v, selection, target, omitted) {
const nodes = pickIds(v, [...selection[0]].filter((id) => !omitted.includes(id)), target, 'stage');
if (nodes.length > 0)
return [nodes, []];
const arcs = selection[1].filter((arc) => arc[3].some((edge) => channelFit(i, edge[1], target) || channelFit(i, edge[2], target)));
return [[...new Set(arcs.flatMap((arc) => [arc[0], arc[1]]))].sort(cmp), arcs];
}
function scanRoots(v, ranks, goals) {
const traversal = reach(v, ranks.map((entry) => entry[0][0]), true, RECOVERY);
const rank = (id) => {
const symbol = v[1].get(id);
return rootRank(v, symbol, score(symbol, goals));
};
const ids = [...traversal[0].keys()].filter((id) => !v[5].has(id)
&& !v[1].get(id)?.[6])
.sort((left, right) => penalty(v[1].get(left)[7]) - penalty(v[1].get(right)[7])
|| rank(right) - rank(left)
|| (v[2].get(right)?.length ?? 0) - (v[2].get(left)?.length ?? 0)
|| cmp(left, right));
return [ids, traversal[1], traversal[2]];
}
export function selectWorkflow(i, plan) {
const v = buildView(i), ops = i.operation_by_id, { intent, subject: target, terms, obligations, access } = plan, isFlow = intent === 'workflow', bound = (kind) => obligations.find((entry) => entry.kind === kind
&& entry.target !== target)?.target, goals = [...new Set([
target, ...terms, ...obligations.map((entry) => entry.target),
])], lastBound = bound('terminal'), stageNeed = bound('stage'), bNeed = bound('behavior'), asyncNeed = words(bound('handoff') ?? '').some((word) => /^(?:async|dispatch|emit|enqueue|event|job|publish|queue|schedule)$/u.test(word)), fail = goals.some((entry) => words(entry).some((word) => FAILURE_WORD.test(word)));
const cand = (symbol) => {
const lexical = score(symbol, goals), outgoing = v[2].get(symbol[0]) ?? [], affinity = isFlow
? outgoing.some((arc) => arc[2] === 'channel') ? 2
: Number(outgoing.some((arc) => arc[2] === 'direct'
&& v[2].get(arc[1])?.some((next) => next[2] === 'channel')))
: intent === 'explain'
? Number(v[2].has(symbol[0]) || v[3].has(symbol[0]))
+ Number(symbol[5].some((fact) => ['condition', 'loop', 'parallel'].includes(fact.kind)))
: intent !== 'locate' || !access ? 0
: access === 'write' ? Number(symbol[6]
|| symbol[5].some((fact) => fact.kind === 'mutation'))
: Number(symbol[5].some((fact) => fact.kind === 'persistence'
&& READ.test(fact.operation))), exact = isFlow ? Number(!v[5].has(symbol[0]))
: Number(pickIds(v, [symbol[0]], target, 'names').length > 0);
return [symbol, lexical, rootRank(v, symbol, lexical), affinity, exact];
};
const ranks = v[0].map(cand)
.filter((entry) => entry[1] > 0 && (!isFlow
|| v[2].has(entry[0][0]) || v[3].has(entry[0][0])
|| v[4].has(entry[0][0])))
.sort((a, b) => isFlow
? b[4] - a[4] || b[3] - a[3] || b[2] - a[2]
|| cmp(a[0][0], b[0][0])
: intent === 'locate'
? b[3] - a[3] || b[4] - a[4]
|| penalty(a[0][7]) - penalty(b[0][7])
|| b[1] - a[1] || cmp(a[0][0], b[0][0])
: b[4] - a[4] || b[2] - a[2]
|| b[3] - a[3] || b[1] - a[1] || cmp(a[0][0], b[0][0]))
.slice(0, CANDIDATES), focus = ranks[0]?.[0][0];
const entryNeed = bound('entry'), eligibleRoots = (ids) => entryNeed
? pickIds(v, ids, entryNeed, 'entry') : ids, entryPool = isFlow ? ranks.filter((entry) => !v[5].has(entry[0][0])) : [], entryIds = entryNeed
? new Set(eligibleRoots(entryPool.map((entry) => entry[0][0]))) : undefined;
let entries = entryPool.filter((entry) => !entryIds || entryIds.has(entry[0][0])).slice(0, 3);
let scan;
if (isFlow && ranks.length > 0 && (entries.length === 0
|| entries.every((entry) => entry[0][7] !== 'production'))) {
scan = scanRoots(v, ranks, goals);
const recovered = eligibleRoots(scan[0])
.map((id) => cand(v[1].get(id)));
entries = [...new Map([...recovered, ...entries].map((entry) => [entry[0][0], entry])).values()].slice(0, 3);
}
let roots = [];
const subjectTerms = new Set(words(target)), callTarget = terms.filter((term) => !subjectTerms.has(term)).join(' '), direct = focus && intent === 'explain'
? (v[2].get(focus) ?? []).filter((arc) => arc[2] === 'direct') : [], wanted = callTarget ? direct.find((arc) => pickIds(v, [arc[1]], callTarget, 'names').length > 0) : undefined, callArcs = direct.filter((arc) => arc === wanted || score(v[1].get(arc[1]), goals) > 0)
.sort((a, b) => Number(b === wanted) - Number(a === wanted)
|| score(v[1].get(b[1]), goals) - score(v[1].get(a[1]), goals)
|| cmp(a[1], b[1])).slice(0, 3), ids = focus ? [focus, ...callArcs.map((arc) => arc[1])] : [];
let flow = isFlow ? [new Set(), [], [], new Set(), false]
: [new Set(ids), callArcs, [], new Set(ids), false];
const seen = new Set();
let tries = isFlow ? 0 : focus ? 1 : 0;
let locked = false, bestStage = !stageNeed;
for (const entry of entries) {
const id = entry[0][0];
const room = NODES - RECOVERY - seen.size;
if (room <= 0)
break;
const trial = corridor(v, id, room, goals, fail, lastBound, asyncNeed);
trial[3].forEach((cand) => seen.add(cand));
tries += 1;
const stageFit = (!stageNeed
|| stageMatch(i, v, trial, stageNeed, [id])[0].length > 0)
&& (!asyncNeed || trial[1].some((arc) => arc[2] === 'channel'));
const domain = penalty(entry[0][7]), prior = roots[0]
? penalty(v[1].get(roots[0])[7]) : Infinity;
if (entryNeed && domain === prior && roots.length > 0 && [...trial[0]].some((node) => node !== id && flow[0].has(node))) {
const merged = [...new Set([...flow[1], ...trial[1]])];
if (new Set(merged.flatMap(idsOf)).size > HOPS) {
flow = [flow[0], flow[1], flow[2], flow[3], true];
}
else {
flow = [new Set([...flow[0], ...trial[0]]), merged,
[...new Set([...flow[2], ...trial[2]])],
new Set([...flow[3], ...trial[3]]), flow[4] || trial[4]];
roots.push(id);
}
continue;
}
if (roots.length === 0 || !locked
&& (domain < prior || domain === prior
&& (Number(stageFit) > Number(bestStage)
|| stageFit === bestStage
&& (Number(trial[2].length > 0) > Number(flow[2].length > 0)
|| Boolean(trial[2].length) === Boolean(flow[2].length)
&& flow[4] && !trial[4])))) {
roots = [id];
flow = trial;
bestStage = stageFit;
locked = !lastBound && !stageNeed && !asyncNeed && trial[2].length === 0
&& pickIds(v, [id], target, 'names').length > 0
&& (trial[1].length > 0 || v[4].has(id));
}
}
const rec = new Set();
scan?.[1].forEach((id) => rec.add(id));
flow[3].forEach((id) => seen.add(id));
let bounded = flow[4];
let passes = scan ? 1 : 0;
if (isFlow && (flow[2].length === 0 || flow[4])
&& ranks.length > 0) {
scan ??= scanRoots(v, ranks, goals);
scan[1].forEach((id) => { rec.add(id); seen.add(id); });
bounded ||= scan[2];
passes = 1;
const alternates = flow[2].length === 0 && !locked
? eligibleRoots(scan[0]).filter((id) => id !== roots[0])
.slice(0, 3 - tries) : [];
if (alternates.length > 0)
passes = 2;
for (const id of alternates) {
tries += 1;
const room = RECOVERY - rec.size + 1;
if (room <= 0) {
bounded = true;
break;
}
const trial = corridor(v, id, room, goals, fail, lastBound, asyncNeed);
bounded ||= trial[4];
trial[3].forEach((entry) => { rec.add(entry); seen.add(entry); });
if (roots[0] && v[4].has(roots[0])
&& pickIds(v, [roots[0]], target, 'names').length > 0)
continue;
roots = [id];
flow = trial;
break;
}
}
else if (intent === 'explain' && focus
&& !(v[1].get(focus)?.[5].some(behavior) ?? false)) {
const alternate = ranks.slice(1, 4).find((entry) => entry[0][5].some(behavior));
if (alternate) {
passes = 1;
tries += 1;
const id = alternate[0][0];
rec.add(id);
seen.add(id);
flow = [new Set([id]), [], [], new Set([id]), false];
}
}
const [nodes, links, ends] = flow;
const rootIds = isFlow
? roots.filter((id) => nodes.has(id)).sort(cmp)
: callArcs.length > 0 && focus ? [focus] : [];
const causal = [...new Set([
...rootIds, ...ends, ...links.flatMap((arc) => [arc[0], arc[1]]),
])].sort(cmp);
const symbolIds = [...nodes].sort(cmp), edges = [...new Map(links.flatMap((arc) => arc[3].map((edge) => [edge[0], edge]))).values()]
.map(([id, fromId, toId, relation]) => ({ id, fromId, toId, relation })).sort((a, b) => cmp(a.id, b.id)), subjects = pickIds(v, symbolIds, target, intent === 'locate' && !access ? 'names' : 'subject'), behaviors = isFlow ? causal : subjects, owners = new Set(links.flatMap((arc) => [arc[0], arc[1]]));
const seeds = intent === 'locate' ? [] : behaviors
.filter((id) => !owners.has(id) && !ends.includes(id))
.flatMap((id) => {
const facts = v[1].get(id)?.[5].filter((fact) => behavior(fact) && (fail || !bad(fact))) ?? [];
return [...new Map(facts.map((fact) => [fact.kind, fact.id])).values()];
});
const locateOps = intent === 'locate' && access
? subjects.flatMap((id) => {
const expected = words(target);
return (v[1].get(id)?.[5] ?? [])
.filter((fact) => {
const compatible = fact.kind === 'persistence'
? (access === 'read')
=== READ.test(fact.operation)
: access === 'write' && fact.kind === 'mutation';
const actual = new Set(words(factText(fact)));
return compatible && expected.length > 0
&& expected.every((word) => actual.has(word));
})
.map((fact) => fact.id);
})
: [];
const ctl = intent === 'locate'
? locateOps.length > 0 ? controls(i, [], [], locateOps) : [[], [], true, []]
: controls(i, links, ends, seeds);
const steps = isFlow ? causal : symbolIds, edgeIds = edges.map((edge) => edge.id), chosenOps = new Set(ctl[0]), terminalIds = [...new Set(ctl[3].map((id) => ops.get(id)?.owner_symbol_id).filter((id) => id !== undefined))].sort(cmp), arcOps = [...new Set(links.flatMap((arc) => arc[4]))]
.filter((id) => chosenOps.has(id));
const related = (ids, calls = false) => ctl[0].filter((id) => {
const fact = ops.get(id);
return !!fact && (ids.includes(fact.owner_symbol_id) || calls
&& fact.kind === 'call' && !!fact.target_symbol_id
&& ids.includes(fact.target_symbol_id));
});
const stage = stageNeed
? stageMatch(i, v, flow, stageNeed, rootIds)
: undefined, stageNodes = stage?.[0] ?? steps, stageOps = stage ? stage[1].length > 0
? [...new Set(stage[1].flatMap((arc) => arc[4]))]
.filter((id) => chosenOps.has(id)).sort(cmp)
: related(stageNodes, true) : ctl[0], stageEdges = stage ? [...new Set(stage[1].flatMap(idsOf))].sort(cmp) : edgeIds, aOps = related(behaviors), bTerms = words(bNeed ?? ''), bOps = bTerms[0] ? aOps.filter((id) => {
const fact = ops.get(id);
return fact?.kind === 'call' && bTerms.every((word) => words(fact.callee).includes(word)
|| v[1].get(fact.target_symbol_id ?? '')?.[3].has(word));
}) : aOps, bReady = !bTerms[0] || !!bOps[0];
const inert = behaviors.filter((id) => !owners.has(id)
&& !aOps.some((operation) => ops.get(operation)?.owner_symbol_id === id));
const gaps = causal.filter((id) => v[4].has(id));
const data = {
subject: [subjects, intent === 'locate' && access ? locateOps : related(subjects),
subjects.length > 0 && (!access || locateOps.length > 0)],
entry: [rootIds, related(rootIds), rootIds.length > 0
&& (!entryIds || rootIds.some((id) => entryIds.has(id)))],
stage: [stageNodes, stageOps,
steps.length > 0 && (!stageNeed || stageNodes.length > 0)],
handoff: [causal, isFlow ? arcOps : related(causal),
links.length > 0 && ctl[2] && (!isFlow || gaps.length === 0)
&& (!asyncNeed || links.some((arc) => arc[2] === 'channel'))],
behavior: [bReady ? behaviors : [], bOps,
behaviors.length > 0 && inert.length === 0
&& bReady],
ordering: [steps, arcOps,
links.length > 0 && gaps.length === 0 && ctl[2]
&& links.every((arc) => arc[4].length > 0)],
terminal: [terminalIds, ctl[3], terminalIds.length > 0],
};
const missing = [];
const proofs = obligations.map((obligation) => {
const [symbolIds, operationIds, proven] = data[obligation.kind], proofEdges = obligation.kind === 'stage' ? stageEdges
: /^(?:handoff|behavior|ordering)$/u.test(obligation.kind) ? edgeIds : [];
const proof = {
...obligation, proven, symbolIds, operationIds, edgeIds: proofEdges,
};
if (proof.mandatory && !proof.proven) {
missing.push({ code: MISSING_CODES[proof.kind] ?? 'obligation_target_unproven',
target: proof.kind === 'handoff' && gaps.length > 0
? gaps.join(',') : proof.target,
obligationId: proof.id });
}
return proof;
});
if (!ctl[2])
missing.push({ code: 'controller_dependency_unproven', target });
if (bounded)
missing.push({ code: 'selection_bound_reached', target });
return {
complete: missing.length === 0,
symbolIds,
operationIds: ctl[0],
rootSymbolIds: rootIds,
terminalSymbolIds: terminalIds,
edges,
links: links.map((arc) => ({
fromId: arc[0], toId: arc[1], kind: arc[2],
edgeIds: idsOf(arc),
operationIds: arc[4].filter((id) => chosenOps.has(id) && ops.get(id)?.owner_symbol_id === arc[0]),
})),
controlGroups: [...ctl[1], ...cycles(new Set(causal), links)],
obligations: proofs,
missing,
metrics: {
candidateCount: ranks.length, rootCandidateCount: tries,
actualNodeCount: seen.size,
causalRelationHops: edges.length, recoveryPasses: passes,
recoveryFrontierCount: rec.size, bounded,
},
};
}
+1
-0
import type { KnowledgeGraph } from '../../domain/graph/directed-multigraph.js';
export declare function graphArtifactIdentity(graphPath: string): string;
export declare function readBoundedUtf8(descriptor: number, maxBytes: number, tooLarge: string): string;

@@ -3,0 +4,0 @@ export interface GraphArtifactReceipt {

@@ -9,2 +9,14 @@ import { createHash } from 'node:crypto';

const descriptorIdentity = (stats) => `${stats.dev}:${stats.ino}:${stats.ctimeMs}:${stats.mtimeMs}:${stats.size}`;
export function graphArtifactIdentity(graphPath) {
const descriptor = openSync(validateGraphPath(graphPath), 'r');
try {
const stats = fstatSync(descriptor);
if (stats.size > MAX_GRAPH_BYTES)
throw new Error(`Graph file too large: ${graphPath}`);
return descriptorIdentity(stats);
}
finally {
closeSync(descriptor);
}
}
export function readBoundedUtf8(descriptor, maxBytes, tooLarge) {

@@ -11,0 +23,0 @@ const chunks = [];

+3
-3

@@ -13,3 +13,3 @@ import { DEFAULT_RETRIEVE_BUDGET, MAX_RETRIEVE_BUDGET, MAX_RETRIEVE_QUESTION_LENGTH, normalizeRetrieveRequest, } from '../../domain/query/types.js';

name: 'retrieve',
description: 'Return the smallest deterministic evidence path for a TypeScript or JavaScript codebase question.',
description: 'Return an authenticated answer dossier or exact gaps.',
inputSchema: Object.freeze({

@@ -24,3 +24,3 @@ type: 'object',

maxLength: MAX_RETRIEVE_QUESTION_LENGTH,
description: 'The codebase question to answer from authenticated graph evidence.',
description: 'A locate, explain, or workflow question.',
}),

@@ -93,3 +93,3 @@ budget: Object.freeze({

},
instructions: 'Call retrieve once with the codebase question. Madar returns one deterministic authenticated evidence path or a terminal evidence boundary.',
instructions: 'Call once. Ready is complete; otherwise report its exact condition.',
});

@@ -96,0 +96,0 @@ case 'ping':

@@ -6,3 +6,3 @@ import { realpathSync } from 'node:fs';

import { setTimeout as delay } from 'node:timers/promises';
import { readGraphArtifactReceipt } from '../filesystem/graph-artifact.js';
import { graphArtifactIdentity, readGraphArtifactReceipt, } from '../filesystem/graph-artifact.js';
import { readBuildState } from '../../domain/index/build-state.js';

@@ -80,20 +80,28 @@ import { failedQueryIndex, inspectQueryIndex, } from '../../domain/query/index-status.js';

}
function readAcceptedQueryIndex(workspace, controller) {
function readAcceptedQueryIndex(workspace, controller, cached) {
try {
const acceptedBuildId = controller.acceptedBuildId();
if (cached
&& cached.acceptedBuildId === acceptedBuildId
&& cached.identity === graphArtifactIdentity(workspace.graphPath))
return cached;
const receipt = readGraphArtifactReceipt(workspace.graphPath);
const index = inspectQueryIndex(receipt.graph);
if (index.state !== 'ready')
return index;
return { index };
const build = readBuildState(receipt.graph);
if (!build
|| build.build_id !== controller.acceptedBuildId()
|| !acceptedBuildId
|| build.build_id !== acceptedBuildId
|| !samePath(index.root_path, workspace.rootPath)) {
return unavailableIndex();
return { index: unavailableIndex() };
}
return index;
return { identity: receipt.identity, acceptedBuildId, index };
}
catch (error) {
return failedQueryIndex(unavailableGraphError(error) ? 'unavailable' : 'corrupt', unavailableGraphError(error)
? UNAVAILABLE_SUBJECT
: 'canonical graph artifact');
return {
index: failedQueryIndex(unavailableGraphError(error) ? 'unavailable' : 'corrupt', unavailableGraphError(error)
? UNAVAILABLE_SUBJECT
: 'canonical graph artifact'),
};
}

@@ -145,2 +153,3 @@ }

#controller = null;
#queryIndexCache = null;
#startFailure = null;

@@ -196,3 +205,5 @@ #startPromise = null;

if (controller.startupComplete() && state === 'idle') {
return readAcceptedQueryIndex(this.#workspace, controller);
const result = readAcceptedQueryIndex(this.#workspace, controller, this.#queryIndexCache);
this.#queryIndexCache = 'identity' in result ? result : null;
return result.index;
}

@@ -199,0 +210,0 @@ if (state === 'failed' || state === 'stopped')

@@ -1,4 +0,4 @@

import { type QueryIndex } from '../domain/query/index-status.js';
import type { QueryIndex } from '../domain/query/index-status.js';
import { type RetrieveContextResult } from '../domain/query/types.js';
export declare function retrieveContext(index: QueryIndex, input: unknown): RetrieveContextResult;
export declare function serializeRetrieveContextResult(result: RetrieveContextResult): string;

@@ -1,238 +0,587 @@

import { createHash } from 'node:crypto';
import { readFileSync, realpathSync } from 'node:fs';
import { isAbsolute, relative, resolve, sep } from 'node:path';
import { TextDecoder } from 'node:util';
import { canonicalJsonString } from '../domain/graph/canonical-json.js';
import {} from '../domain/query/index-status.js';
import { rankQueryAnchors } from '../domain/query/rank.js';
import { sliceEvidence } from '../domain/query/slice.js';
import { traverseEvidencePaths } from '../domain/query/traverse.js';
import { normalizeRetrieveRequest, } from '../domain/query/types.js';
const utf8 = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
function isPositiveLine(value) {
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
import { countTokens as tokens } from 'gpt-tokenizer/encoding/cl100k_base';
import { hydrateEvidence, } from './evidence-hydrator.js';
import { canonicalJsonString as json, compareCodeUnits as cmp } from '../domain/graph/canonical-json.js';
import { lexicalTokens, planQuestion } from '../domain/query/plan.js';
import { selectWorkflow, } from '../domain/query/workflow.js';
import { MAX_RETRIEVE_EXCERPTS as EXCERPTS, MAX_RETRIEVE_FILES as FILES, RETRIEVE_RESULT_SCHEMA, RETRIEVE_RESULT_VERSION, normalizeRetrieveRequest, valueHas, } from '../domain/query/types.js';
const uniq = (values) => [...new Set(values)].sort(cmp);
const cap = (code, required, limit) => [{ code, required, limit }];
function stat(req, flow, auth, gaps = new Set(), plan) {
const selected = flow?.obligations ?? [];
const must = plan?.obligations.filter(({ mandatory }) => mandatory)
?? selected.filter(({ mandatory }) => mandatory);
const data = flow?.metrics;
const roots = data?.rootCandidateCount ?? 0;
return {
budget_tokens: req.budget,
serialized_tokens: 0,
selected_files: auth?.files.size ?? 0,
authenticated_excerpts: auth?.excerpts.size ?? 0,
required_obligations: must.length,
proven_obligations: must.filter((required) => !gaps.has(required.id)
&& selected.some((candidate) => candidate.mandatory && candidate.proven
&& candidate.id === required.id && candidate.kind === required.kind
&& candidate.target === required.target)).length,
optional_bundles_omitted: 0,
root_candidates: roots,
initial_candidates: data?.candidateCount ?? 0,
explored_nodes: data?.actualNodeCount ?? 0,
causal_hops: data?.causalRelationHops ?? 0,
recovery_passes: data?.recoveryPasses ?? 0,
recovery_frontier_nodes: data?.recoveryFrontierCount ?? 0,
alternate_seeds: Math.max(0, roots - 1),
};
}
function stringFact(attributes, key) {
const value = attributes[key];
return typeof value === 'string' && value.length > 0 ? value : null;
function seal(out) {
out.metrics.serialized_tokens = 0;
const body = tokens(json(out)) - 1;
out.metrics.serialized_tokens = body + tokens(String(body + tokens(String(body))));
return out;
}
function sourceIsBeneathRoot(root, source) {
const path = relative(root, source);
return path !== '..' && !path.startsWith(`..${sep}`) && !isAbsolute(path);
}
function readAuthenticatedSource(index, sourceFile, cache) {
const cached = cache.get(sourceFile);
if (cached)
return cached;
const remember = (result) => {
cache.set(sourceFile, result);
return result;
};
const expectedHash = index.file_hashes.get(sourceFile);
if (!expectedHash)
return remember({ state: 'stale', subject: sourceFile });
try {
const root = realpathSync(index.root_path);
const candidate = realpathSync(resolve(root, sourceFile));
if (isAbsolute(sourceFile) || !sourceIsBeneathRoot(root, candidate)) {
return remember({ state: 'unavailable', subject: sourceFile });
const base = (state, req, flow, auth, gaps, plan) => ({
schema: RETRIEVE_RESULT_SCHEMA, version: RETRIEVE_RESULT_VERSION,
state, metrics: stat(req, flow, auth, gaps, plan),
});
const ask = (plan) => ({ intent: plan.intent, subject: plan.subject, terms: plan.terms });
function fit(out, max) {
seal(out);
if (out.metrics.serialized_tokens <= max)
return out;
if (out.state === 'incomplete') {
out.query.subject = out.query.subject.slice(0, 32);
out.query.terms = [];
for (const row of out.missing)
delete row.target;
}
else if (out.state === 'unsupported')
out.terms = [];
else
for (const failure of out.failures) {
failure.subject = failure.subject.slice(0, 32);
}
const bytes = readFileSync(candidate);
const text = utf8.decode(bytes);
const hash = createHash('sha256').update(bytes).digest('hex');
return remember(hash === expectedHash
? { state: 'ready', text }
: { state: 'stale', subject: sourceFile });
if (seal(out).metrics.serialized_tokens <= max)
return out;
if (out.state === 'incomplete') {
out.query.subject = '';
if (seal(out).metrics.serialized_tokens <= max)
return out;
}
catch {
return remember({ state: 'unavailable', subject: sourceFile });
}
return seal({
schema: out.schema, version: out.version, state: 'corrupt', metrics: out.metrics,
failures: [{ state: 'corrupt', subject: 'terminal result budget' }],
});
}
function offsetOf(text, position) {
if (!Number.isSafeInteger(position.line) || position.line < 1
|| !Number.isSafeInteger(position.column) || position.column < 1)
return null;
const starts = [0], ends = [];
for (let index = 0; index < text.length; index += 1) {
const code = text.charCodeAt(index);
if (![10, 13, 0x2028, 0x2029].includes(code))
function select(plan, flow, index) {
const need = plan.intent === 'locate' ? flow.symbolIds.slice(0, 1) : [];
const focus = flow.obligations.find(({ kind, proven }) => kind === 'subject' && proven);
if (plan.intent === 'explain' && focus)
need.push(...focus.symbolIds.slice(0, 1));
const incident = new Set(flow.edges.flatMap(({ fromId, toId }) => [fromId, toId])), path = new Set([
...flow.rootSymbolIds, ...flow.terminalSymbolIds,
...flow.links.flatMap(({ fromId, toId }) => [fromId, toId]),
]), linked = new Set(flow.links.flatMap(({ operationIds }) => operationIds)), facts = new Set();
for (const id of flow.operationIds) {
const fact = index.operation_by_id.get(id);
if (!fact)
continue;
ends.push(index);
if (code === 13 && text.charCodeAt(index + 1) === 10)
index += 1;
starts.push(index + 1);
incident.add(fact.owner_symbol_id);
if (!['condition', 'loop', 'parallel'].includes(fact.kind)
&& !linked.has(id)
&& (fact.kind !== 'call' || path.has(fact.owner_symbol_id)))
facts.add(id);
}
ends.push(text.length);
const start = starts[position.line - 1], end = ends[position.line - 1];
if (start === undefined || end === undefined)
return null;
const offset = start + position.column - 1;
return offset <= end ? offset : null;
need.push(...flow.symbolIds.filter((id) => !incident.has(id)));
for (const id of facts) {
const fact = index.operation_by_id.get(id);
if (fact?.kind === 'persistence')
facts.add(fact.call_fact_id);
if (fact?.kind === 'parallel')
fact.member_fact_ids.forEach((member) => facts.add(member));
}
return {
symbolIds: flow.symbolIds, edges: flow.edges,
declarationSymbolIds: uniq(need),
operationIds: uniq(facts),
validationOperationIds: flow.operationIds.filter((id) => !facts.has(id)),
};
}
function validRange(value) {
if (!value || typeof value !== 'object')
return false;
const range = value;
return offsetPosition(range.start) <= offsetPosition(range.end);
function miss(req, plan, missing, flow, auth) {
const gaps = new Set(missing.flatMap((entry) => entry.obligation_id ? [entry.obligation_id] : []));
return fit({
...base('incomplete', req, flow, auth, gaps, plan),
query: ask(plan),
missing,
}, req.budget);
}
function offsetPosition(position) {
return position && Number.isSafeInteger(position.line) && position.line > 0
&& Number.isSafeInteger(position.column) && position.column > 0
? position.line * 0x1_0000_0000 + position.column
: Number.NaN;
function mandatoryObligationGaps(plan, flow) {
const required = plan.obligations.filter(({ mandatory }) => mandatory);
const selected = flow.obligations.filter(({ mandatory, proven }) => mandatory && proven);
const unmatched = new Set(selected.map((_, index) => index));
const missing = [];
for (const obligation of required) {
const match = selected.findIndex((candidate, index) => unmatched.has(index)
&& candidate.id === obligation.id && candidate.kind === obligation.kind
&& candidate.target === obligation.target);
if (match >= 0)
unmatched.delete(match);
else
missing.push({
code: 'required_proof_missing', obligation_id: obligation.id,
target: obligation.target,
});
}
for (const index of unmatched) {
const obligation = selected[index];
missing.push({
code: 'required_reference_missing', obligation_id: obligation.id,
target: obligation.target,
});
}
return missing;
}
function exactRange(text, range) {
const start = offsetOf(text, range.start), end = offsetOf(text, range.end);
return start === null || end === null || end < start ? null : text.slice(start, end);
function unsupportedSubjectTerms(index, plan, flow) {
if (!flow.missing.some(({ code }) => code === 'subject_unproven'))
return undefined;
const subject = lexicalTokens(plan.subject);
if (subject.length === 0)
return undefined;
const matched = index.unsupported_sources.some(({ path }) => {
const tokens = new Set(lexicalTokens(path));
return subject.every((term) => tokens.has(term));
});
return matched ? [...new Set(subject)].sort(cmp) : undefined;
}
function authenticateNode(index, nodeId, sourceCache) {
if (!index.graph.hasNode(nodeId))
return { state: 'corrupt', subject: nodeId };
const attributes = index.graph.nodeAttributes(nodeId);
const label = stringFact(attributes, 'label');
const nodeKind = stringFact(attributes, 'node_kind');
const sourceFile = stringFact(attributes, 'source_file');
const sourceLocation = stringFact(attributes, 'source_location');
const provenance = attributes.provenance;
const contentHash = sourceFile ? index.file_hashes.get(sourceFile) : undefined;
if (!label || !nodeKind || !sourceFile
|| !Array.isArray(provenance) || provenance.length === 0 || !contentHash) {
return { state: 'corrupt', subject: nodeId };
const fail = (req, state, subject, flow) => fit({
...base(state, req, flow),
failures: [{ state, subject: subject.slice(0, 96) }],
}, req.budget);
function view(value, ref, brief) {
const nested = (entry) => view(entry, ref, brief);
if (value.kind === 'literal')
return brief ? value.value : value;
if (value.kind === 'symbol') {
const entity = ref(value.symbol_id);
return brief ? entity ? { entity } : { unknown: 'outside_dossier' }
: entity ? { kind: 'symbol', entity }
: { kind: 'unknown', reason: 'outside_dossier' };
}
const source = readAuthenticatedSource(index, sourceFile, sourceCache);
if (source.state !== 'ready')
return source;
const sourceDomain = stringFact(attributes, 'source_domain');
const common = {
node_id: nodeId, label, source_file: sourceFile, provenance,
content_hash: contentHash,
...(sourceDomain ? { source_domain: sourceDomain } : {}),
if (value.kind === 'array')
return brief
? value.elements.map(nested)
: { kind: 'array', elements: value.elements.map(nested) };
if (value.kind === 'object')
return brief ? {
object: value.entries.map(({ key, value: entry }) => [key, nested(entry)]),
} : { kind: 'object', entries: value.entries.map(({ key, value: entry }) => ({
key, value: nested(entry),
})) };
if (value.kind === 'template')
return brief
? { template: value.parts.map(nested) }
: { kind: 'template', parts: value.parts.map(nested) };
if (!brief)
return value;
if (value.kind === 'parameter')
return {
parameter: value.position, ...(value.scope ? { scope: value.scope } : {}),
};
if (value.kind === 'redacted')
return {
redacted: value.sha256, bytes: value.byte_length,
};
return { unknown: value.reason };
}
const KEYS = {
literal: ['role'], condition: ['condition_kind'], loop: ['loop_kind'],
parallel: ['combinator', 'completion', 'lane_count'],
return: [], throw: [], mutation: ['operation', 'target'],
persistence: ['operation', 'receiver_type'],
};
function displayArm(arm) {
if (!arm.startsWith('case:'))
return arm;
const encoded = arm.slice(5);
try {
const value = JSON.parse(Buffer.from(encoded, 'base64url').toString('utf8'));
if (!Array.isArray(value) || value.length !== 2)
return arm;
const [rawKind, scalar] = value;
const kind = rawKind === 'object' && scalar === null ? 'null' : rawKind;
const valid = kind === 'null' && scalar === null
|| kind === 'string' && typeof scalar === 'string'
|| kind === 'boolean' && typeof scalar === 'boolean'
|| kind === 'number' && typeof scalar === 'number' && Number.isFinite(scalar);
if (!valid || Buffer.from(json(value)).toString('base64url') !== encoded)
return arm;
return `case:${String(kind)}:${json(scalar)}`;
}
catch {
return arm;
}
}
function info(fact, ref) {
const value = (entry) => entry === undefined ? undefined : view(entry, ref, false);
if (fact.kind === 'call') {
const target = fact.target_symbol_id ? ref(fact.target_symbol_id) : undefined;
const args = fact.arguments.some((entry) => valueHas(entry, (candidate) => candidate.kind === 'literal')) ? fact.arguments.map((entry) => view(entry, ref, false)) : undefined;
return { callee: fact.callee,
...(target ? { target } : {}), ...(args ? { arguments: args } : {}) };
}
if (fact.kind === 'condition')
return {
kind: fact.condition_kind,
...(fact.test === undefined ? {} : { test: view(fact.test, ref, true) }),
};
if (fact.kind === 'loop')
return {
kind: fact.loop_kind,
...(fact.test === undefined ? {} : { test: view(fact.test, ref, true) }),
};
if (fact.kind === 'parallel')
return {
combinator: fact.combinator, completion: fact.completion,
lanes: fact.lane_count,
...(fact.input === undefined ? {} : { input: view(fact.input, ref, true) }),
};
const row = {};
const raw = fact;
for (const key of KEYS[fact.kind])
row[key] = raw[key];
if (fact.kind === 'persistence') {
row.call = ref(fact.call_fact_id);
const resource = value(fact.resource);
if (resource && !(resource.kind === 'symbol'
&& resource.entity === ref(fact.owner_symbol_id)))
row.resource = resource;
}
else if (['literal', 'return', 'throw', 'mutation'].includes(fact.kind)) {
const item = raw.value;
if (item !== undefined)
row.value = value(item);
}
return row;
}
function pack(plan, flow, auth, index) {
const { rootSymbolIds: roots, terminalSymbolIds: ends, links: paths, controlGroups: groups, obligations: claims, } = flow;
const get = (id) => auth.entities.get(id)?.[0];
const ref = get;
const coords = (range) => [
range.start.line, range.start.column, range.end.line, range.end.column,
];
const refs = [];
for (const proof of auth.proofs.values()) {
if (proof[1] === 'edge')
refs.push({
id: proof[0], from: proof[2], to: proof[3],
relation: proof[4], excerpt: proof[5],
});
else if (proof[1] === 'edge_range')
refs.push({
id: proof[0], from: proof[2], to: proof[3], relation: proof[4],
file: proof[5], range: coords(proof[6]),
});
}
const tied = new Map();
const pathProofs = paths.map((link) => {
const proofs = [...new Set(link.edgeIds.map((id) => auth.proofs.get(id)[0]))];
link.operationIds.forEach((id) => tied.set(id, uniq([...(tied.get(id) ?? []), ...proofs])));
return proofs;
});
const folded = new Map(), consumed = new Set();
paths.forEach((path, index) => {
if (path.kind !== 'direct')
return;
const incoming = paths.flatMap((candidate, candidateIndex) => candidate.toId === path.toId ? [candidateIndex] : []);
const outgoing = paths.flatMap((candidate, candidateIndex) => candidate.fromId === path.toId ? [candidateIndex] : []);
if (incoming.length !== 1 || outgoing.length !== 1)
return;
const nextIndex = outgoing[0], next = paths[nextIndex];
if (next.kind !== 'channel')
return;
const alreadyPublished = paths.some((candidate, candidateIndex) => candidateIndex !== index && candidateIndex !== nextIndex
&& candidate.fromId === path.fromId && candidate.toId === next.toId);
if (alreadyPublished)
return;
folded.set(index, nextIndex);
consumed.add(nextIndex);
});
const links = [];
paths.forEach((path, index) => {
if (consumed.has(index))
return;
const nextIndex = folded.get(index), next = nextIndex === undefined
? undefined : paths[nextIndex];
links.push({
id: `l${links.length + 1}`,
kind: next ? 'channel' : path.kind,
from: ref(path.fromId), to: ref(next?.toId ?? path.toId),
proofs: nextIndex === undefined ? pathProofs[index]
: [...new Set([...pathProofs[index], ...pathProofs[nextIndex]])],
});
});
const opRefs = (ids) => uniq(ids.flatMap((id) => tied.get(id) ?? (get(id) ? [ref(id)] : [])));
const resolve = (id) => tied.get(id)?.[0] ?? get(id);
const owned = new Map();
const ents = [...auth.entities].flatMap(([id, item,]) => {
const alias = item[0];
const proof = auth.proofs.get(id);
const excerpt = proof?.[1] === 'declaration' || proof?.[1] === 'operation'
? proof[3] : undefined;
if (item[1] === 'symbol') {
return [{
id: alias, kind: 'symbol', label: item[2],
...(/^(?:function|method|class)$/u.test(item[3])
? {} : { node_kind: item[3] }),
file: item[4],
...(excerpt ? { excerpt } : {}),
}];
}
if (item[1] === 'channel') {
const parent = item[5] ? ref(item[5]) : undefined;
return [{
id: alias, kind: 'channel', channel_kind: item[2],
transport: item[3], key: item[4],
...(parent ? { parent } : {}),
...(item[6] ? { scope: item[6] } : {}),
}];
}
const fact = item[3];
const repl = tied.get(id);
owned.set(item[2], fact.kind === 'persistence'
? alias : owned.get(item[2]) ?? repl?.[0] ?? alias);
return repl ? [] : [{
id: alias, kind: 'operation', operation_kind: fact.kind,
owner: item[2], excerpt: excerpt, detail: info(fact, resolve),
}];
});
const cover = (ids, behavior = false) => {
const result = ids.flatMap((id) => {
const subject = ref(id);
const hydrated = auth.proofs.get(id);
const proof = behavior
? refs.find((entry) => entry.from === subject)?.id ?? owned.get(subject)
: hydrated && (hydrated[1] === 'declaration' || hydrated[1] === 'operation')
? subject : owned.get(subject)
?? refs.find((entry) => entry.from === subject || entry.to === subject)?.id;
return proof ? [proof] : [];
});
return behavior || result.length === ids.length ? uniq(result) : [];
};
if (nodeKind === 'file') {
return { state: 'ready', node: { ...common, evidence_kind: 'structural_file', node_kind: 'file' } };
const chains = [];
for (const group of groups) {
const id = group.controllerOperationId;
if (!id || !['branch', 'loop', 'parallel'].includes(group.kind))
continue;
const fact = index.operation_by_id.get(id), proof = auth.controls.get(id);
if (!fact || !proof || !['condition', 'loop', 'parallel'].includes(fact.kind)) {
return { code: 'required_proof_missing', target: plan.subject };
}
const members = opRefs(group.operationIds);
if (members.length === 0)
continue;
const detail = info(fact, resolve), owner = ref(fact.owner_symbol_id);
const parent = [...fact.control].reverse().find((frame) => frame.kind === group.kind
&& (frame.kind !== 'branch' || frame.arm === group.arm));
const prior = parent && 'controller_fact_id' in parent
? [...chains].reverse().find((chain) => chain[4].at(-1) === parent.controller_fact_id
&& chain[0] === group.kind && chain[1] === group.arm
&& chain[2] === owner && chain[3] === proof[0]
&& json(chain[7]) === json(detail)
&& members.every((member) => chain[6].at(-1).includes(member)))
: undefined;
if (prior) {
prior[4].push(id);
prior[5].push(coords(proof[1]));
prior[6].push(members);
}
else {
chains.push([group.kind, group.arm, owner, proof[0],
[id], [coords(proof[1])], [members], detail]);
}
}
const startLine = attributes.line_number;
const endLine = attributes.end_line_number;
const definitionRange = attributes.definition_range;
const declarationRange = attributes.declaration_range;
if (!sourceLocation || !isPositiveLine(startLine) || !isPositiveLine(endLine))
return { state: 'corrupt', subject: nodeId };
if (!validRange(definitionRange) || !validRange(declarationRange)
|| offsetPosition(declarationRange.start) < offsetPosition(definitionRange.start)
|| offsetPosition(declarationRange.end) > offsetPosition(definitionRange.end)) {
return { state: 'stale', subject: sourceFile };
const byFile = new Map();
for (const chain of chains) {
const ranges = byFile.get(chain[3]) ?? [];
for (const range of chain[5]) {
if (!ranges.some((candidate) => json(candidate) === json(range))) {
ranges.push(range);
}
}
byFile.set(chain[3], ranges);
}
const expectedLocation = definitionRange.end.line > definitionRange.start.line
? `L${definitionRange.start.line}-L${definitionRange.end.line}`
: `L${definitionRange.start.line}`;
if (startLine !== definitionRange.start.line || endLine !== definitionRange.end.line
|| sourceLocation !== expectedLocation)
return { state: 'stale', subject: sourceFile };
const snippet = exactRange(source.text, declarationRange);
if (snippet === null || exactRange(source.text, definitionRange) === null) {
return { state: 'stale', subject: sourceFile };
const controls = [...byFile].sort(([left], [right]) => cmp(left, right)).map(([file, ranges,], index) => ({
id: `c${index + 1}`, file,
ranges: ranges.sort((left, right) => {
for (let part = 0; part < left.length; part += 1) {
const order = left[part] - right[part];
if (order !== 0)
return order;
}
return 0;
}),
}));
const control = (chain) => {
const catalog = controls.find(({ file }) => file === chain[3]);
const indexes = chain[5].map((range) => catalog.ranges.findIndex((candidate) => json(candidate) === json(range)));
const sequential = indexes.every((index, offset) => index === indexes[0] + offset);
const selector = sequential && indexes.length > 1
? `${indexes[0]}-${indexes.at(-1)}` : indexes.join('.');
return `${catalog.id}:${selector}`;
};
const order = chains.map((chain) => {
const controller = control(chain);
const layers = chain[6].map((set, layer) => set.filter((member) => !(chain[6][layer + 1] ?? []).includes(member)));
const members = layers.flat();
return {
id: '', kind: chain[0], controller,
...(chain[1] ? { arm: displayArm(chain[1]) } : {}), detail: chain[7],
...(layers.length > 1 ? {
depths: layers.flatMap((layer, depth) => layer.map(() => depth)),
} : {}),
members,
};
});
for (const group of groups) {
if (group.controllerOperationId)
continue;
const ops = opRefs(group.operationIds);
const nodes = group.kind === 'cycle' ? group.symbolIds.map(ref) : ops;
const proofs = group.kind === 'cycle'
? [...ops, ...links.filter((link) => nodes.includes(link.from)
&& nodes.includes(link.to)).flatMap((link) => link.proofs)] : ops;
if (nodes.length === 0 || proofs.length === 0
|| group.kind === 'sequence' && nodes.length < 2)
continue;
order.push({
id: '', kind: group.kind,
members: group.kind === 'sequence' ? nodes : uniq(nodes),
...(group.kind === 'cycle' ? { proofs: uniq(proofs) } : {}),
});
}
order.forEach((group, index) => { group.id = `g${index + 1}`; });
const collapse = (raw, bundles) => {
const wanted = new Set(raw);
const used = bundles.filter(({ proofs }) => proofs.some((id) => wanted.has(id)));
const covered = new Set(used.flatMap(({ proofs }) => proofs));
const packed = [...used.map(({ id }) => id), ...raw.filter((id) => !covered.has(id))];
return packed.length < raw.length ? packed : [...raw];
};
const linkBundles = links.map(({ id, proofs }) => ({ id, proofs }));
const orderBundles = order.map(({ id, members, proofs = [] }) => ({
id, proofs: [...members, ...proofs],
}));
const claimsOut = [];
for (const claim of claims) {
const useOps = claim.kind === 'ordering' || claim.kind === 'terminal'
|| claim.kind === 'subject' && plan.intent === 'locate' && !!plan.access;
const raw = claim.kind === 'handoff'
? claim.edgeIds.map((id) => auth.proofs.get(id)[0])
: claim.kind === 'behavior'
? uniq([...cover(claim.symbolIds, true), ...opRefs(claim.operationIds)])
: useOps ? opRefs(claim.operationIds) : cover(claim.symbolIds);
const refs = uniq(claim.kind === 'ordering'
? collapse(raw, orderBundles)
: ['stage', 'handoff', 'behavior'].includes(claim.kind)
? collapse(raw, linkBundles) : raw);
if (claim.mandatory && refs.length === 0) {
return {
code: 'required_proof_missing', target: claim.target,
obligation_id: claim.id,
};
}
claimsOut.push({
id: claim.id, kind: claim.kind,
statement: claim.kind === 'subject' ? `${plan.subject}.` : `${claim.kind} proven.`,
proofs: refs,
});
}
return {
state: 'ready',
node: {
...common, evidence_kind: 'symbol_declaration', node_kind: nodeKind,
source_location: sourceLocation, line_number: startLine, end_line_number: endLine,
definition_range: definitionRange, declaration_range: declarationRange, snippet,
query: ask(plan), obligations: claimsOut,
flow: { roots: roots.map(ref), terminals: ends.map(ref), links, order },
evidence: {
digest_algorithm: 'sha256-base64url',
files: [...auth.files].map(([path, [id, sha256]]) => ({
id, path, digest: Buffer.from(sha256, 'hex').toString('base64url'),
})),
excerpts: [...auth.excerpts.values()].map(([id, file, range, , text]) => ({
id, file, range: [range.start.line, range.start.column,
range.end.line, range.end.column], text,
})),
controls, entities: ents, proofs: refs,
},
};
}
function relationshipFromEdge(edge) {
const sourceFile = edge.attributes.source_file;
const sourceLocation = edge.attributes.source_location;
const provenance = edge.attributes.provenance;
if (!Array.isArray(provenance) || provenance.length === 0)
return null;
return {
id: edge.id,
from_id: edge.from,
to_id: edge.to,
relation: edge.relation,
...(typeof sourceFile === 'string' && sourceFile.length > 0 ? { source_file: sourceFile } : {}),
...(typeof sourceLocation === 'string' && sourceLocation.length > 0 ? { source_location: sourceLocation } : {}),
provenance,
};
}
function outcomeFrom(nodes, boundaries) {
if (nodes.length > 0)
return 'evidence';
for (const state of ['corrupt', 'unavailable', 'stale', 'unsupported', 'missing']) {
if (boundaries.some((boundary) => boundary.kind === state))
return state;
}
return 'missing';
}
function boundary(kind, subject) {
return { kind, subject };
}
function emptyResult(request, outcome, boundaries) {
return sliceEvidence({
request, outcome,
matchedNodes: [],
relationships: [],
boundaries, priorityNodeIds: [], closurePasses: 0,
});
}
export function retrieveContext(index, input) {
const request = normalizeRetrieveRequest(input);
if (index.state !== 'ready') {
return emptyResult(request, index.state, [boundary(index.state, index.subject)]);
const req = normalizeRetrieveRequest(input);
const planned = planQuestion(req);
if (planned.status === 'unsupported') {
return fit({
...base('unsupported', req),
reason: planned.reason,
terms: planned.terms.slice(0, 8).map((term) => term.slice(0, 32)),
}, req.budget);
}
const ranking = rankQueryAnchors(index, request);
if (ranking.anchors.length === 0) {
const boundaries = ranking.boundaries.length > 0
? ranking.boundaries
: [boundary('missing', request.question)];
return emptyResult(request, outcomeFrom([], boundaries), boundaries);
const plan = planned.plan;
if (index.state !== 'ready')
return fail(req, index.state, index.subject);
let flow;
try {
flow = selectWorkflow(index, plan);
}
const traversal = traverseEvidencePaths(index, ranking);
const sourceCache = new Map();
let matchedNodes = [];
const boundaries = [...ranking.boundaries, ...traversal.boundaries];
for (const nodeId of traversal.nodeIds) {
const authenticated = authenticateNode(index, nodeId, sourceCache);
if (authenticated.state === 'ready') {
matchedNodes.push(authenticated.node);
}
else {
boundaries.push(boundary(authenticated.state, authenticated.subject));
}
catch {
return fail(req, 'corrupt', 'workflow selection');
}
const selectedNodeIds = new Set(matchedNodes.map((node) => node.node_id));
const relationships = [];
for (const edge of traversal.edges) {
if (!selectedNodeIds.has(edge.from) || !selectedNodeIds.has(edge.to))
continue;
const relationship = relationshipFromEdge(edge);
if (relationship)
relationships.push(relationship);
else
boundaries.push(boundary('corrupt', edge.id));
const unsupportedTerms = unsupportedSubjectTerms(index, plan, flow);
if (unsupportedTerms) {
return fit({
...base('unsupported', req, flow),
reason: 'unsupported_source', terms: unsupportedTerms,
}, req.budget);
}
const related = new Set(relationships.flatMap((edge) => [edge.from_id, edge.to_id]));
const orphanFiles = matchedNodes.filter((node) => node.evidence_kind === 'structural_file' && !related.has(node.node_id));
for (const node of orphanFiles)
boundaries.push(boundary('unavailable', node.source_file));
const orphanIds = new Set(orphanFiles.map((node) => node.node_id));
matchedNodes = matchedNodes.filter((node) => !orphanIds.has(node.node_id));
return sliceEvidence({
request,
outcome: outcomeFrom(matchedNodes, boundaries),
matchedNodes,
relationships,
boundaries,
priorityNodeIds: ranking.priorityAnchorIds
? [...ranking.priorityAnchorIds]
: ranking.anchors.map((anchor) => anchor.id),
closurePasses: traversal.closurePasses,
structuralRequired: ranking.structuralRequired === true,
structuralCoverageComplete: ranking.structuralCoverageComplete !== false,
});
let auth;
try {
auth = hydrateEvidence(index, select(plan, flow, index));
}
catch {
return fail(req, 'corrupt', 'evidence hydration', flow);
}
if (auth.state !== 'ready') {
return fail(req, auth.state, auth.subject, flow);
}
if (!flow.complete) {
return miss(req, plan, flow.missing.map((entry) => ({
code: entry.code,
...(entry.obligationId ? { obligation_id: entry.obligationId } : {}),
...(entry.target.length <= 96 ? { target: entry.target } : {}),
})), flow, auth);
}
const obligationGaps = mandatoryObligationGaps(plan, flow);
if (obligationGaps.length > 0) {
return miss(req, plan, obligationGaps, flow, auth);
}
const over = auth.files.size > FILES
? ['required_file_limit', auth.files.size, FILES]
: auth.excerpts.size > EXCERPTS
? ['required_excerpt_limit', auth.excerpts.size, EXCERPTS]
: undefined;
if (over)
return miss(req, plan, cap(over[0], over[1], over[2]), flow, auth);
try {
const built = pack(plan, flow, auth, index);
if ('code' in built)
return miss(req, plan, [built], flow, auth);
const ready = seal({
...base('ready', req, flow, auth, undefined, plan),
dossier: built,
});
if (ready.metrics.serialized_tokens > req.budget)
return miss(req, plan, cap('required_token_budget', ready.metrics.serialized_tokens, req.budget), flow, auth);
return ready;
}
catch {
return fail(req, 'corrupt', 'dossier packing', flow);
}
}
export function serializeRetrieveContextResult(result) {
return canonicalJsonString(result);
return json(result);
}
import type { KnowledgeGraph } from '../graph/directed-multigraph.js';
export declare const CANONICAL_INDEX_FORMAT_VERSION: 3;
export declare const CANONICAL_INDEX_FORMAT_VERSION: 4;
export declare const GENERATION_POLICY_VERSION: 4;
export declare const INDEX_BUILD_STATE_VERSION: 3;
export declare const INDEX_ENGINE_ID: "madar-typescript-index-v3";
export declare const INDEX_ENGINE_ID: "madar-typescript-index-v4-execution-1";
export declare const INDEXING_OUTCOME_STATUSES: readonly ["indexed", "indexed_with_warnings", "skipped_by_policy", "unsupported", "failed"];

@@ -7,0 +7,0 @@ export declare const INDEXING_REASON_CODES: readonly ["indexed", "environment_file", "private_key", "credential_store", "secret_config", "sensitive_directory", "unreadable_path", "unreadable_directory", "hidden_path", "hard_ignored", "madarignore", "gitignored", "noise_path", "symlink_disabled", "symlink_outside_root", "symlink_cycle", "unsupported_file_type", "canonical_diagnostic", "canonical_file_missing"];

@@ -5,6 +5,6 @@ import { createHash } from 'node:crypto';

import { hasExactKeys, isRecord } from '../../shared/guards.js';
export const CANONICAL_INDEX_FORMAT_VERSION = 3;
export const CANONICAL_INDEX_FORMAT_VERSION = 4;
export const GENERATION_POLICY_VERSION = 4;
export const INDEX_BUILD_STATE_VERSION = 3;
export const INDEX_ENGINE_ID = 'madar-typescript-index-v3';
export const INDEX_ENGINE_ID = 'madar-typescript-index-v4-execution-1';
export const INDEXING_OUTCOME_STATUSES = [

@@ -11,0 +11,0 @@ 'indexed', 'indexed_with_warnings', 'skipped_by_policy', 'unsupported', 'failed',

@@ -18,2 +18,133 @@ export type IndexLanguage = 'typescript' | 'javascript' | 'tsx' | 'jsx';

};
export type IndexSha256 = string;
type Immutable<T> = {
readonly [K in keyof T]: T[K];
};
export type IndexFactConfidence = 'high' | 'medium' | 'low';
export type IndexFactSource = 'typescript-semantic' | 'typescript-syntactic' | 'framework' | 'wrapper-summary';
export type IndexCallScheduling = 'sync' | 'awaited' | 'fire_and_forget';
export type IndexLiteralRole = 'argument' | 'initializer' | 'condition' | 'return' | 'channel' | 'configuration';
type IndexConditionKind = 'if' | 'switch' | 'ternary' | 'logical_and' | 'logical_or' | 'nullish' | 'guard';
type IndexLoopKind = 'for' | 'for_in' | 'for_of' | 'while' | 'do_while' | 'array_iteration';
export type IndexPromiseCombinator = 'all' | 'allSettled' | 'any' | 'race';
export type IndexParallelCompletion = 'all_or_first_rejection' | 'all_settled' | 'first_fulfilled' | 'first_settled';
export type IndexMutationOperation = 'assign' | 'increment' | 'decrement' | 'append' | 'remove' | 'delete';
export type IndexPersistenceOperation = 'read' | 'create' | 'update' | 'delete' | 'upsert' | 'transaction' | 'file_read' | 'file_write' | 'object_read' | 'object_write';
type IndexUnknownReason = 'dynamic' | 'ambiguous' | 'unsupported';
export type IndexFactEvidence = Immutable<{
file_id: string;
range: IndexRange;
statement_range: IndexRange;
excerpt_sha256: IndexSha256;
}>;
export type IndexScalarValue = string | number | boolean | null;
export type IndexObjectEntry = Immutable<{
key: string;
value: IndexValue;
}>;
export type IndexValue = Immutable<{
kind: 'literal';
value: IndexScalarValue;
}> | Immutable<{
kind: 'symbol';
symbol_id: string;
}> | Immutable<{
kind: 'parameter';
position: number;
scope?: 'iteration';
}> | Immutable<{
kind: 'array';
elements: readonly IndexValue[];
}> | Immutable<{
kind: 'object';
entries: readonly IndexObjectEntry[];
}> | Immutable<{
kind: 'template';
parts: readonly IndexValue[];
}> | Immutable<{
kind: 'redacted';
sha256: IndexSha256;
byte_length: number;
}> | Immutable<{
kind: 'unknown';
reason: IndexUnknownReason;
}>;
export type IndexBranchArm = 'then' | 'else' | 'truthy' | 'falsy' | 'nullish' | 'default' | `case:${string}`;
export type IndexControlFrame = Immutable<{
kind: 'branch';
controller_fact_id: string;
arm: IndexBranchArm;
}> | Immutable<{
kind: 'loop';
controller_fact_id: string;
}> | Immutable<{
kind: 'parallel';
controller_fact_id: string;
lane: number | 'each';
}> | Immutable<{
kind: 'exception';
arm: 'try' | 'catch' | 'finally';
}>;
export type IndexBodyFactBase = Immutable<{
id: string;
owner_symbol_id: string;
order: readonly number[];
evidence: IndexFactEvidence;
control: readonly IndexControlFrame[];
confidence: IndexFactConfidence;
source: IndexFactSource;
}>;
type Fact<K extends string, T extends object = object> = IndexBodyFactBase & Immutable<{
kind: K;
} & T>;
export type IndexCallFact = Fact<'call', {
callee: string;
target_symbol_id?: string;
arguments: readonly IndexValue[];
scheduling: IndexCallScheduling;
}>;
export type IndexLiteralFact = Fact<'literal', {
value: IndexValue;
role: IndexLiteralRole;
}>;
export type IndexConditionFact = Fact<'condition', {
condition_kind: IndexConditionKind;
test?: IndexValue;
}>;
export type IndexLoopFact = Fact<'loop', {
loop_kind: IndexLoopKind;
test?: IndexValue;
}>;
export type IndexParallelFact = Fact<'parallel', {
combinator: IndexPromiseCombinator;
completion: IndexParallelCompletion;
lane_count: number;
input?: IndexValue;
member_fact_ids: readonly string[];
}>;
export type IndexReturnFact = Fact<'return', {
value?: IndexValue;
}>;
export type IndexThrowFact = Fact<'throw', {
value?: IndexValue;
}>;
export type IndexMutationFact = Fact<'mutation', {
operation: IndexMutationOperation;
target: string;
value?: IndexValue;
}>;
export type IndexPersistenceFact = Fact<'persistence', {
operation: IndexPersistenceOperation;
call_fact_id: string;
resource?: IndexValue;
receiver_type: string;
}>;
export type IndexBodyFact = IndexCallFact | IndexLiteralFact | IndexConditionFact | IndexLoopFact | IndexParallelFact | IndexReturnFact | IndexThrowFact | IndexMutationFact | IndexPersistenceFact;
export declare function indexBodyFactId(ownerSymbolId: string, kind: IndexBodyFact['kind'], order: readonly number[], excerptSha256: IndexSha256, semantics?: readonly unknown[]): string;
export type IndexBodyFactTable = readonly [version: 1, rows: readonly string[]];
export declare const INDEX_BODY_FACT_CONTROL_LIMIT = 64;
export declare class IndexBodyFactBoundsError extends Error {
}
export declare function encodeIndexBodyFactTable(facts: readonly IndexBodyFact[]): IndexBodyFactTable;
export declare function decodeIndexBodyFactTable(value: unknown, owner: string, file: string): readonly IndexBodyFact[] | null;
export type IndexFrameworkRole = 'nest_module' | 'nest_controller' | 'nest_route' | 'nest_provider' | 'nest_guard' | 'nest_pipe' | 'nest_interceptor' | 'express_app' | 'express_router' | 'express_route' | 'express_middleware' | 'nextjs_app_page' | 'nextjs_app_route' | 'nextjs_app_layout' | 'nextjs_app_loading' | 'nextjs_app_error' | 'nextjs_app_template' | 'nextjs_pages_page' | 'nextjs_pages_api' | 'nextjs_middleware' | 'nextjs_client_component' | 'nextjs_server_action' | 'react_router_router' | 'react_router_loader' | 'react_router_action' | 'hono_app' | 'hono_route' | 'hono_middleware' | 'fastify_app' | 'fastify_route' | 'fastify_plugin' | 'trpc_router' | 'trpc_procedure_query' | 'trpc_procedure_mutation' | 'trpc_procedure_subscription' | 'prisma_client' | 'prisma_model_reader' | 'prisma_model_writer' | 'prisma_model_access';

@@ -37,9 +168,24 @@ export type IndexStorageOperation = 'create' | 'createMany' | 'update' | 'updateMany' | 'delete' | 'deleteMany' | 'upsert' | 'findUnique' | 'findUniqueOrThrow' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'count' | 'aggregate' | 'groupBy' | '$transaction';

framework_metadata?: IndexFrameworkMetadata;
body_facts?: readonly IndexBodyFact[];
};
export type IndexEdgeKind = 'imports' | 'reexports' | 'declares' | 'calls' | 'enqueues_job' | 'extends' | 'implements' | 'param_type' | 'return_type' | 'module_provides' | 'module_imports' | 'module_exports' | 'controller_route' | 'route_handler' | 'registers_controller' | 'injects' | 'guards' | 'intercepts' | 'pipes';
export type IndexChannelKind = 'queue' | 'job' | 'event';
export type IndexChannelTransport = 'bull' | 'bullmq' | 'node-event-emitter' | 'nestjs-event-emitter';
export type IndexChannelNode = Immutable<{
id: string;
node_kind: 'channel';
channel_kind: IndexChannelKind;
transport: IndexChannelTransport;
key: string;
scope?: string;
parent_channel_id?: string;
}>;
export declare function indexChannelId(input: Omit<IndexChannelNode, 'id' | 'node_kind'>): string;
export type IndexEdgeKind = 'imports' | 'reexports' | 'declares' | 'calls' | 'enqueues_job' | 'publishes_to' | 'routes_through' | 'consumed_by' | 'extends' | 'implements' | 'param_type' | 'return_type' | 'module_provides' | 'module_imports' | 'module_exports' | 'controller_route' | 'route_handler' | 'registers_controller' | 'injects' | 'guards' | 'intercepts' | 'pipes';
export type IndexEdgeConfidence = 'high' | 'medium' | 'low';
export type IndexEdgeSource = 'typescript-semantic' | 'typescript-syntactic' | 'framework-decorator' | 'heuristic';
export type IndexEdgeSource = 'typescript-semantic' | 'typescript-syntactic' | 'framework-decorator' | 'wrapper-summary' | 'heuristic';
export type IndexEdgeEvidence = {
file_id: string;
range: IndexRange;
statement_range?: IndexRange;
excerpt_sha256?: IndexSha256;
};

@@ -66,1 +212,2 @@ export type IndexEdge = {

};
export {};

@@ -1,1 +0,483 @@

export {};
import { createHash } from 'node:crypto';
const KS = ['condition', 'loop', 'parallel', 'call', 'literal',
'mutation', 'persistence', 'return', 'throw'];
const LS = ['high', 'medium', 'low'];
const SS = ['typescript-semantic', 'typescript-syntactic', 'framework', 'wrapper-summary'];
const TM = ['sync', 'awaited', 'fire_and_forget'];
const RS = ['argument', 'initializer', 'condition', 'return', 'channel', 'configuration'];
const CS = ['if', 'switch', 'ternary', 'logical_and', 'logical_or', 'nullish', 'guard'];
const LP = ['for', 'for_in', 'for_of', 'while', 'do_while', 'array_iteration'];
const PM = ['all', 'allSettled', 'any', 'race'];
const CP = ['all_or_first_rejection', 'all_settled', 'first_fulfilled', 'first_settled'];
const MU = ['assign', 'increment', 'decrement', 'append', 'remove', 'delete'];
const ST = ['read', 'create', 'update', 'delete', 'upsert', 'transaction',
'file_read', 'file_write', 'object_read', 'object_write'];
const UK = ['dynamic', 'ambiguous', 'unsupported'];
const SHA256 = /^[a-f0-9]{64}$/;
const MR = 8_192, MB = 262_144, MT = 8_388_608;
const MD = 5, ME = 32, MX = 512;
const aa = Array.isArray, bl = Buffer.byteLength, js = JSON.stringify;
export function indexBodyFactId(ownerSymbolId, kind, order, excerptSha256, semantics) {
const identity = semantics
? JSON.stringify([ownerSymbolId, ...semantics])
: [ownerSymbolId, kind, order.join('.'), excerptSha256].join('\u0000');
return `operation:${createHash('sha256').update(identity, 'utf8')
.digest('hex').slice(0, 32)}`;
}
export const INDEX_BODY_FACT_CONTROL_LIMIT = 64;
export class IndexBodyFactBoundsError extends Error {
}
function ep(a, b) { const i = a.indexOf(b); if (i < 0)
throw new Error(`Unsupported execution value ${b}`); return i; }
function oc(a, b) { for (let i = 0; i < Math.min(a.length, b.length); i += 1) {
const d = a[i] - b[i];
if (d !== 0)
return d;
} return a.length - b.length; }
function dn(a) { for (let i = 0; i < a.length; i += 1)
if (!Object.hasOwn(a, i))
return false; return true; }
const sc = (a) => (a === null || ['string', 'number', 'boolean'].includes(typeof a)) && !(typeof a === 'number' && (!Number.isFinite(a) || Object.is(a, -0))) && !(typeof a === 'string' && bl(a) > MX);
function pv(a, d = 0) {
const n = a.kind === 'array' ? a.elements.length
: a.kind === 'object' ? a.entries.length
: a.kind === 'template' ? a.parts.length : 0;
if (d > MD || (d === MD && n > 0))
return [7, ep(UK, 'unsupported')];
switch (a.kind) {
case 'literal':
if (!sc(a.value))
throw new Error('Execution literal is not JSON-lossless');
return [0, a.value];
case 'symbol':
if (!vt(a.symbol_id, 1_024))
throw new Error('Execution symbol reference is invalid');
return [1, a.symbol_id];
case 'parameter':
if (!si(a.position)
|| (a.scope !== undefined && a.scope !== 'iteration'))
throw new Error('Execution parameter position is invalid');
return a.scope === 'iteration'
? [2, a.position, 1]
: [2, a.position];
case 'array':
if (a.elements.length > ME || !dn(a.elements))
throw new Error('Execution array exceeds its element bound');
return [3, a.elements.map((e) => pv(e, d + 1))];
case 'object': {
const k = new Set();
if (a.entries.length > ME || !dn(a.entries))
throw new Error('Execution object exceeds its element bound');
for (const e of a.entries) {
if (bl(e.key) > MX || e.key.includes('\0') || k.has(e.key))
throw new Error('Execution object key is invalid');
k.add(e.key);
}
return [4, a.entries.map((e) => [
e.key, pv(e.value, d + 1),
])];
}
case 'template':
if (a.parts.length > ME || !dn(a.parts))
throw new Error('Execution template exceeds its element bound');
return [5, a.parts.map((e) => pv(e, d + 1))];
case 'redacted':
if (!SHA256.test(a.sha256) || !si(a.byte_length))
throw new Error('Execution redaction is invalid');
return [6, a.sha256, a.byte_length];
case 'unknown': return [7, ep(UK, a.reason)];
}
throw new Error('Unsupported execution value');
}
const pe = (a) => [a.range.start.line, a.range.start.column, a.range.end.line, a.range.end.column, a.statement_range.start.line, a.statement_range.start.column, a.statement_range.end.line, a.statement_range.end.column, a.excerpt_sha256];
export function encodeIndexBodyFactTable(facts) {
if (facts.length === 0 || facts.length > MR) {
throw new IndexBodyFactBoundsError('Execution fact table is outside its row bound');
}
if (!dn(facts))
throw new Error('Execution fact table is sparse');
const a = [...facts].sort((l, r) => oc(l.order, r.order) || (l.id < r.id ? -1 : l.id > r.id ? 1 : 0));
const m = new Map(a.map((f, i) => [f.id, i]));
if (m.size !== a.length)
throw new Error('Execution fact IDs are not unique');
const oi = (id) => {
const v = m.get(id);
if (v === undefined)
throw new Error(`Missing execution fact reference ${id}`);
return v;
};
const cf = (f) => {
if (f.kind === 'branch') {
if (!vt(f.arm, 96)
|| (!['then', 'else', 'truthy', 'falsy', 'nullish', 'default'].includes(f.arm)
&& !(f.arm.startsWith('case:') && f.arm.length > 5))) {
throw new Error('Execution branch arm is invalid');
}
return [0, oi(f.controller_fact_id), f.arm];
}
if (f.kind === 'loop')
return [1, oi(f.controller_fact_id)];
if (f.kind === 'parallel') {
if (f.lane !== 'each' && !si(f.lane))
throw new Error('Execution parallel lane is invalid');
return [2, oi(f.controller_fact_id), f.lane];
}
if (f.kind === 'exception')
return [3, ep(['try', 'catch', 'finally'], f.arm)];
throw new Error('Unsupported execution control frame');
};
let b = 0;
const k = new Set();
const r = a.map((f) => {
const o = f.order.join('.');
if (f.order.length !== 4
|| !dn(f.order) || !f.order.every((v) => si(v))
|| !dn(f.control)
|| f.control.length > INDEX_BODY_FACT_CONTROL_LIMIT
|| f.order[1] !== ep(KS, f.kind)
|| k.has(o)) {
throw new Error(`Invalid execution fact identity ${f.id}`);
}
k.add(o);
let w;
switch (f.kind) {
case 'call':
if (!dn(f.arguments))
throw new Error(`Sparse call arguments for ${f.id}`);
w = [
f.callee, f.target_symbol_id ?? null,
f.arguments.map(pv), ep(TM, f.scheduling),
];
break;
case 'literal':
w = [pv(f.value), ep(RS, f.role)];
break;
case 'condition':
w = [
ep(CS, f.condition_kind),
f.test ? pv(f.test) : null,
];
break;
case 'loop':
w = [
ep(LP, f.loop_kind),
f.test ? pv(f.test) : null,
];
break;
case 'parallel': {
const c = ep(PM, f.combinator);
if (f.completion !== CP[c]
|| !si(f.lane_count)
|| !dn(f.member_fact_ids)
|| new Set(f.member_fact_ids).size !== f.member_fact_ids.length)
throw new Error(`Invalid parallel completion ${f.id}`);
w = [
c, f.input ? pv(f.input) : null,
f.member_fact_ids.map(oi), f.lane_count,
];
break;
}
case 'return':
case 'throw':
w = [f.value ? pv(f.value) : null];
break;
case 'mutation':
w = [
ep(MU, f.operation), f.target,
f.value ? pv(f.value) : null,
];
break;
case 'persistence':
if (!vt(f.receiver_type))
throw new Error(`Persistence proof is missing for ${f.id}`);
w = [
ep(ST, f.operation), oi(f.call_fact_id),
f.resource ? pv(f.resource) : null,
f.receiver_type,
];
break;
}
const s = [
ep(KS, f.kind),
f.order[0], f.order[2], f.order[3], pe(f.evidence),
f.control.map(cf), ep(LS, f.confidence),
ep(SS, f.source), w,
];
const id = indexBodyFactId(f.owner_symbol_id, f.kind, f.order, f.evidence.excerpt_sha256, s);
if (f.id !== id && f.id !== indexBodyFactId(f.owner_symbol_id, f.kind, f.order, f.evidence.excerpt_sha256))
throw new Error(`Invalid execution fact identity ${f.id}`);
const x = js([id, ...s]);
const z = bl(x);
b += z;
if (z > MB || b > MT)
throw new IndexBodyFactBoundsError(`Execution fact table exceeds its byte bound at ${f.id}`);
return x;
});
return [1, r];
}
const si = (a, m = 0) => typeof a === 'number' && Number.isSafeInteger(a) && !Object.is(a, -0) && a >= m;
const vt = (a, m = MX) => typeof a === 'string' && a.length > 0 && !a.includes('\0') && bl(a) <= m;
const tu = (a, l) => aa(a) && a.length === l ? a : null;
const ev = (a, b) => si(b) && b < a.length ? a[b] : null;
function rv(a, d = 0) {
if (!aa(a) || !si(a[0]) || a[0] > 7)
return null;
if (d > MD)
return null;
if (d === MD && [3, 4, 5].includes(a[0])
&& (!aa(a[1]) || a[1].length > 0))
return null;
switch (a[0]) {
case 0: {
return a.length === 2 && sc(a[1])
? { kind: 'literal', value: a[1] } : null;
}
case 1:
return a.length === 2 && vt(a[1], 1_024)
? { kind: 'symbol', symbol_id: a[1] }
: null;
case 2:
return (a.length === 2 || (a.length === 3 && a[2] === 1))
&& si(a[1])
? {
kind: 'parameter',
position: a[1],
...(a[2] === 1 ? { scope: 'iteration' } : {}),
}
: null;
case 3:
case 5: {
if (a.length !== 2 || !aa(a[1]) || a[1].length > ME)
return null;
const v = a[1].map((e) => rv(e, d + 1));
if (!v.every((e) => e !== null))
return null;
return a[0] === 3
? { kind: 'array', elements: v }
: { kind: 'template', parts: v };
}
case 4: {
if (a.length !== 2 || !aa(a[1]) || a[1].length > ME)
return null;
const k = new Set(), e = [];
for (const x of a[1]) {
const r = tu(x, 2), v = r ? rv(r[1], d + 1) : null;
if (!r || typeof r[0] !== 'string' || r[0].includes('\0')
|| bl(r[0]) > MX || k.has(r[0]) || !v)
return null;
k.add(r[0]);
e.push({ key: r[0], value: v });
}
return { kind: 'object', entries: e };
}
case 6:
return a.length === 3 && typeof a[1] === 'string'
&& SHA256.test(a[1]) && si(a[2])
? { kind: 'redacted', sha256: a[1], byte_length: a[2] }
: null;
case 7: {
const r = ev(UK, a[1]);
return a.length === 2 && r ? { kind: 'unknown', reason: r } : null;
}
}
return null;
}
function re(a, f) {
const r = tu(a, 9);
if (!r || !r.slice(0, 8).every((e) => si(e, 1))
|| typeof r[8] !== 'string' || !SHA256.test(r[8]))
return null;
const g = { start: { line: r[0], column: r[1] },
end: { line: r[2], column: r[3] } };
const s = { start: { line: r[4], column: r[5] },
end: { line: r[6], column: r[7] } };
const c = (a, b) => a.line - b.line || a.column - b.column;
return c(g.start, g.end) <= 0 && c(s.start, s.end) <= 0
&& c(s.start, g.start) <= 0 && c(g.end, s.end) <= 0
? { file_id: f, range: g, statement_range: s, excerpt_sha256: r[8] }
: null;
}
function dr(a, o, f) {
if (bl(a) > MB)
return null;
let p;
try {
p = JSON.parse(a);
}
catch {
return null;
}
if (js(p) !== a)
return null;
const r = tu(p, 10);
if (!r || !vt(r[0], 64)
|| !si(r[1]) || r[1] >= KS.length
|| !si(r[2]) || !si(r[3]) || !si(r[4])
|| !aa(r[6])
|| r[6].length > INDEX_BODY_FACT_CONTROL_LIMIT)
return null;
const k = KS[r[1]], e = re(r[5], f), c = ev(LS, r[7]), s = ev(SS, r[8]);
const q = [r[2], r[1], r[3], r[4]];
if (!e || !c || !s || r[0] !== indexBodyFactId(o, k, q, e.excerpt_sha256, r.slice(1)))
return null;
return { id: r[0], kind: k, order: q, evidence: e, control: r[6],
confidence: c, source: s, payload: r[9] };
}
export function decodeIndexBodyFactTable(value, owner, file) {
const t = tu(value, 2);
if (!vt(owner, 1_024) || !vt(file, 128)
|| !t || t[0] !== 1 || !aa(t[1])
|| t[1].length === 0 || t[1].length > MR)
return null;
const d = [];
let b = 0;
for (const x of t[1]) {
if (typeof x !== 'string')
return null;
b += bl(x);
if (b > MT)
return null;
const r = dr(x, owner, file);
if (!r)
return null;
d.push(r);
}
const i = d.map((r) => r.id);
if (new Set(i).size !== i.length
|| d.some((r, n) => n > 0 && oc(d[n - 1].order, r.order) >= 0)) {
return null;
}
const ia = (v) => si(v) && v < i.length ? i[v] : null;
const cf = (v) => {
if (!aa(v) || !si(v[0]))
return null;
const id = ia(v[1]);
if (v[0] === 0) {
return v.length === 3 && id
&& vt(v[2], 96)
&& (['then', 'else', 'truthy', 'falsy', 'nullish', 'default'].includes(v[2])
|| (v[2].startsWith('case:') && v[2].length > 5))
? { kind: 'branch', controller_fact_id: id, arm: v[2] }
: null;
}
if (v[0] === 1)
return v.length === 2 && id
? { kind: 'loop', controller_fact_id: id } : null;
if (v[0] === 2)
return v.length === 3 && id
&& (v[2] === 'each' || si(v[2]))
? { kind: 'parallel', controller_fact_id: id, lane: v[2] } : null;
const a = ev(['try', 'catch', 'finally'], v[1]);
return v[0] === 3 && v.length === 2 && a
? { kind: 'exception', arm: a } : null;
};
const f = [];
for (const r of d) {
const c = r.control.map(cf);
if (!c.every((x) => x !== null))
return null;
const z = {
id: r.id, owner_symbol_id: owner, order: r.order,
evidence: r.evidence, control: c,
confidence: r.confidence, source: r.source,
};
const w = aa(r.payload) ? r.payload : null;
let x = null;
if (r.kind === 'call' && w?.length === 4) {
const s = ev(TM, w[3]);
const a = aa(w[2])
? w[2].map((e) => rv(e))
: [];
if (vt(w[0]) && s
&& (w[1] === null || vt(w[1], 1_024))
&& aa(w[2])
&& a.every((e) => e !== null)) {
x = {
...z, kind: 'call', callee: w[0],
...(typeof w[1] === 'string' ? { target_symbol_id: w[1] } : {}),
arguments: a, scheduling: s,
};
}
}
else if (r.kind === 'literal' && w?.length === 2) {
const v = rv(w[0]), o = ev(RS, w[1]);
if (v && o)
x = { ...z, kind: 'literal', value: v, role: o };
}
else if (r.kind === 'condition' && w?.length === 2) {
const k = ev(CS, w[0]), t = w[1] === null ? undefined : rv(w[1]);
if (k && (w[1] === null || t)) {
x = { ...z, kind: 'condition', condition_kind: k, ...(t ? { test: t } : {}) };
}
}
else if (r.kind === 'loop' && w?.length === 2) {
const k = ev(LP, w[0]), t = w[1] === null ? undefined : rv(w[1]);
if (k && (w[1] === null || t)) {
x = { ...z, kind: 'loop', loop_kind: k, ...(t ? { test: t } : {}) };
}
}
else if (r.kind === 'parallel' && w?.length === 4) {
const q = ev(PM, w[0]), n = w[1] === null ? undefined : rv(w[1]);
const m = aa(w[2])
? w[2].map(ia)
: [];
if (q && (w[1] === null || n)
&& aa(w[2])
&& m.every((id) => id !== null)
&& new Set(m).size === m.length
&& si(w[3])) {
x = {
...z, kind: 'parallel', combinator: q,
completion: CP[PM.indexOf(q)],
lane_count: w[3],
...(n ? { input: n } : {}),
member_fact_ids: m,
};
}
}
else if ((r.kind === 'return' || r.kind === 'throw')
&& w?.length === 1) {
const v = w[0] === null ? undefined : rv(w[0]);
if (w[0] === null || v) {
x = { ...z, kind: r.kind, ...(v ? { value: v } : {}) };
}
}
else if (r.kind === 'mutation' && w?.length === 3) {
const o = ev(MU, w[0]), v = w[2] === null ? undefined : rv(w[2]);
if (o && vt(w[1]) && (w[2] === null || v)) {
x = {
...z, kind: 'mutation', operation: o, target: w[1],
...(v ? { value: v } : {}),
};
}
}
else if (r.kind === 'persistence' && w?.length === 4) {
const o = ev(ST, w[0]), id = ia(w[1]);
const v = w[2] === null ? undefined : rv(w[2]);
if (o && id && (w[2] === null || v) && vt(w[3])) {
x = {
...z, kind: 'persistence', operation: o, call_fact_id: id,
...(v ? { resource: v } : {}),
receiver_type: w[3],
};
}
}
if (!x)
return null;
f.push(x);
}
return f;
}
export function indexChannelId(input) {
const descriptor = {
channel_kind: input.channel_kind,
transport: input.transport,
key: input.key,
...(input.parent_channel_id
? { parent_channel_id: input.parent_channel_id }
: {}),
...(input.scope ? { scope: input.scope } : {}),
};
return `channel:${createHash('sha256')
.update(JSON.stringify(descriptor), 'utf8').digest('hex').slice(0, 32)}`;
}
import { KnowledgeGraph, type GraphAttributes, type GraphEdge } from '../graph/directed-multigraph.js';
import { type SourceSnapshotEntry } from '../index/build-state.js';
import type { IndexBodyFact, IndexChannelNode } from '../index/model.js';
export interface QueryGraph {

@@ -19,2 +20,6 @@ hasNode(id: string): boolean;

unsupported_sources: readonly SourceSnapshotEntry[];
operation_by_id: ReadonlyMap<string, IndexBodyFact>;
operations_by_owner: ReadonlyMap<string, readonly IndexBodyFact[]>;
channels_by_id: ReadonlyMap<string, IndexChannelNode>;
channels_by_key: ReadonlyMap<string, readonly IndexChannelNode[]>;
}

@@ -21,0 +26,0 @@ export interface FailedQueryIndex {

import { KnowledgeGraph, } from '../graph/directed-multigraph.js';
import { CANONICAL_INDEX_FORMAT_VERSION, readBuildState } from '../index/build-state.js';
function immutableMap(entries) {
const values = new Map(entries);
return Object.freeze({
get size() { return values.size; },
get(key) { return values.get(key); },
has(key) { return values.has(key); },
entries() { return values.entries(); },
keys() { return values.keys(); },
values() { return values.values(); },
forEach(callback, thisArg) {
values.forEach((value, key) => callback.call(thisArg, value, key, this));
},
[Symbol.iterator]() { return values[Symbol.iterator](); },
});
import { compareCodeUnits as cc } from '../graph/canonical-json.js';
import { CANONICAL_INDEX_FORMAT_VERSION, readBuildState, } from '../index/build-state.js';
import { decodeIndexBodyFactTable, indexChannelId } from '../index/model.js';
import { isRecord } from '../../shared/guards.js';
const SHA256 = /^[a-f0-9]{64}$/;
const MAX_TEXT = 512;
const KINDS = new Set(['queue', 'job', 'event']);
const TRANSPORTS = new Set([
'bull', 'bullmq', 'node-event-emitter', 'nestjs-event-emitter'
]);
const RELATIONS = new Set(['publishes_to', 'routes_through', 'consumed_by']);
const EDGE_SOURCES = new Set([
'typescript-semantic', 'typescript-syntactic', 'framework-decorator', 'wrapper-summary'
]);
const CH = 'channel_kind', PH = 'parent_channel_id', CO = 'condition_kind', SR = 'statement_range', EH = 'excerpt_sha256', CF = 'controller_fact_id', OW = 'owner_symbol_id', NK = 'node_kind', EV = 'evidence', TR = 'transport', SF = 'source_file', MF = 'member_fact_ids', LC = 'lane_count', EO = 'execution_owner_id', DR = 'definition_range', PL = 'parallel', CD = 'condition', CR = 'corrupt', CA = 'call';
const oh = Object.hasOwn, of = Object.freeze;
class IntegrityError extends Error {
}
function graphSnapshot(source) {
const snapshot = new KnowledgeGraph(source.graph);
for (const [id, attributes] of source.nodeEntries())
snapshot.addNode(id, attributes);
for (const [from, to, attributes, expectedId] of source.edgeEntries()) {
const id = snapshot.addEdge(from, to, attributes);
if (id !== expectedId)
throw new Error('Canonical graph edge identity changed while sealing query index');
function fl(s) { throw new IntegrityError(`canonical ${s}`); }
const ne = (v) => typeof v === 'string' && v.length > 0 && !v.includes('\0');
const bd = (v, m) => ne(v) && Buffer.byteLength(v, 'utf8') <= m;
const si = (v, m = 0) => typeof v === 'number' && Number.isSafeInteger(v) && !Object.is(v, -0) && v >= m;
const ex = (v, k) => isRecord(v) && Object.keys(v).length === k.length && k.every((x) => oh(v, x)) ? v : null;
const pc = (a, b) => a.line - b.line || a.column - b.column;
function ro(v) {
const r = ex(v, ['start', 'end']), s = ex(r?.start, ['line', 'column']), e = ex(r?.end, ['line', 'column']);
if (!r || !s || !e || !si(s.line, 1) || !si(s.column, 1)
|| !si(e.line, 1) || !si(e.column, 1))
return null;
const p = { start: { line: s.line, column: s.column },
end: { line: e.line, column: e.column } };
return pc(p.start, p.end) <= 0 ? p : null;
}
const ct = (a, b) => pc(a.start, b.start) <= 0 && pc(b.end, a.end) <= 0;
const ss = (a, b) => pc(a.start, b.start) === 0 && pc(a.end, b.end) === 0;
function va(c, a) {
if (c[CO] === 'if')
return ['then', 'else'].includes(a);
if (c[CO] === 'switch')
return a === 'default'
|| (a.startsWith('case:') && a.length > 5);
if (c[CO] === 'logical_and')
return a === 'truthy';
if (c[CO] === 'logical_or')
return a === 'falsy';
if (c[CO] === 'nullish')
return a === 'nullish';
return (c[CO] === 'ternary' ? ['truthy', 'falsy'] : ['then', 'else']).includes(a);
}
function ep(a, f, n, t, o) {
const s = a[SF], i = a[EO], w = typeof i === 'string' ? n.get(i) : undefined;
const p = ro(w?.[DR]), r = ex(a[EV], ['source', 'range', 'statement_range', 'excerpt_sha256']);
const g = ro(r?.range), m = ro(r?.[SR]), d = a.dispatch_payload_argument;
const q = !oh(a, 'dispatch_payload_argument')
|| a.relation === 'publishes_to' && t?.[CH] !== 'event'
&& si(d) && (o.get(String(i)) ?? []).filter((x) => x.kind === CA && d < x.arguments.length
&& g !== null && ss(x[EV].range, g)
&& m !== null && ss(x[EV][SR], m)
&& x[EV][EH] === r?.[EH]).length === 1;
return typeof s === 'string' && f.has(s) && typeof i === 'string'
&& w?.[SF] === s && w?.[NK] !== 'file' && w?.[NK] !== 'channel'
&& p !== null && r !== null && EDGE_SOURCES.has(String(r.source))
&& g !== null && m !== null && ct(p, m) && ct(m, g)
&& typeof r[EH] === 'string' && SHA256.test(r[EH]) && q;
}
const vh = (v, t) => t(v) || v.kind === 'array' && v.elements.some((e) => vh(e, t)) || v.kind === 'object' && v.entries.some((e) => vh(e.value, t)) || v.kind === 'template' && v.parts.some((e) => vh(e, t));
function fh(f, t) {
let x;
switch (f.kind) {
case CA:
x = f.arguments;
break;
case 'literal':
x = [f.value];
break;
case CD:
case 'loop':
x = f.test ? [f.test] : [];
break;
case PL:
x = f.input ? [f.input] : [];
break;
case 'return':
case 'throw':
case 'mutation':
x = f.value ? [f.value] : [];
break;
case 'persistence': x = f.resource ? [f.resource] : [];
}
return snapshot;
return x.some((v) => vh(v, t));
}
function immutableQueryGraph(snapshot) {
return Object.freeze({
hasNode: (id) => snapshot.hasNode(id),
hasEdge: (source, target) => snapshot.hasEdge(source, target),
nodeEntries: () => snapshot.nodeEntries(),
edgeEntries: () => snapshot.edgeEntries(),
predecessors: (id) => snapshot.predecessors(id),
successors: (id) => snapshot.successors(id),
edgesBetween: (source, target) => snapshot.edgesBetween(source, target),
nodeAttributes: (id) => snapshot.nodeAttributes(id),
});
function rc(i, a) {
if (!ne(i) || !KINDS.has(a[CH])
|| !TRANSPORTS.has(a[TR]) || !bd(a.key, MAX_TEXT)
|| oh(a, 'parent_channel_id') && !ne(a[PH])
|| oh(a, 'scope') && !bd(a.scope, 512))
return null;
const c = {
id: i, node_kind: 'channel', channel_kind: a[CH],
transport: a[TR], key: a.key,
...(typeof a[PH] === 'string' ? { parent_channel_id: a[PH] } : {}),
...(typeof a.scope === 'string' ? { scope: a.scope } : {}),
};
return i === indexChannelId(c) ? c : null;
}
export function failedQueryIndex(state, subject) {
return { state, subject };
function oc(a, b) { for (let i = 0; i < Math.min(a.length, b.length); i += 1) {
const d = a[i] - b[i];
if (d !== 0)
return d;
} return a.length - b.length; }
function fr(v) { if (v !== null && typeof v === 'object' && !Object.isFrozen(v)) {
for (const e of Object.values(v))
fr(e);
of(v);
} return v; }
function sm(e) {
const x = new Map(e);
let v;
v = {
get size() { return x.size; }, get(k) { return x.get(k); },
has(k) { return x.has(k); }, entries() { return x.entries(); },
keys() { return x.keys(); }, values() { return x.values(); },
forEach(c, t) { x.forEach((a, b) => c.call(t, a, b, v)); },
[Symbol.iterator]() { return x[Symbol.iterator](); },
};
return of(v);
}
const se = (x) => [...x.entries()].sort(([a], [b]) => cc(a, b));
function bm(v, l) {
const n = v.nodeEntries();
const b = new Map(n);
const s = new Set();
const f = new Map();
const o = new Map();
const c = new Map();
const k = new Map();
const q = new Map();
for (const [i, a] of n) {
if (a[NK] === 'channel') {
if (oh(a, 'body_facts')) {
fl('channel body facts');
}
const h = rc(i, a);
if (!h)
fl('channel node');
c.set(i, h);
continue;
}
if (a[NK] === 'file') {
if (oh(a, 'body_facts')
|| oh(a, 'channel_kind')
|| oh(a, 'parent_channel_id')) {
fl('file-node execution metadata');
}
continue;
}
if (oh(a, 'channel_kind')
|| oh(a, 'parent_channel_id')) {
fl('channel discriminator');
}
s.add(i);
if (!oh(a, 'body_facts'))
continue;
const u = a[SF];
const d = typeof u === 'string'
? l.get(u)
: undefined;
const p = ro(a[DR]);
const w = d ? b.get(d) : undefined;
if (!d || !p || !w
|| w[NK] !== 'file') {
fl('operation owner');
}
const x = decodeIndexBodyFactTable(a.body_facts, i, d);
if (!x)
fl('symbol body facts');
const y = x.filter((t) => t.kind === 'persistence');
const z = new Set(y.map((t) => t.order[3]));
if (z.size !== y.length || y.some((_, j) => !z.has(j + 1)))
fl('persistence order');
const e = q.get(i) ?? new Set();
q.set(i, e);
for (const t of x) {
if (!ct(p, t[EV][SR])
|| f.has(t.id)) {
fl('operation fact');
}
const g = t.order.join('.');
if (e.has(g))
fl('operation order');
e.add(g);
f.set(t.id, t);
const m = o.get(i) ?? [];
m.push(t);
o.set(i, m);
}
}
for (const h of c.values()) {
if (h[CH] === 'job') {
const p = h[PH]
? c.get(h[PH])
: undefined;
if (!p || p[CH] !== 'queue'
|| p[TR] !== h[TR]) {
fl('job parent channel');
}
}
else if (h[PH] !== undefined) {
fl('non-job parent channel');
}
if (h[CH] === 'event') {
if (!bd(h.scope, 512)) {
fl('event channel scope');
}
}
else if (h.scope !== undefined) {
fl('non-event channel scope');
}
const y = k.get(h.key) ?? [];
y.push(h);
k.set(h.key, y);
}
for (const t of f.values()) {
if (fh(t, (v) => v.kind === 'symbol' && !s.has(v.symbol_id))) {
fl('operation value reference');
}
if (t.kind === CA && t.target_symbol_id
&& !s.has(t.target_symbol_id)) {
fl('call target');
}
const i = new Set();
for (const d of t.control) {
if (d.kind === 'exception')
continue;
if (i.has(d[CF])) {
fl('duplicate control reference');
}
i.add(d[CF]);
const c = f.get(d[CF]);
const k = d.kind === 'branch'
? CD
: d.kind;
const g = d.kind === 'branch'
&& c?.kind === CD
&& c[CO] === 'guard';
if (!c || c[OW] !== t[OW]
|| c.kind !== k
|| oc(c.order, t.order) >= 0
|| (!g && !ct(d.kind === PL
? c[EV].range
: c[EV][SR], t[EV].range))
|| (d.kind === 'branch' && c.kind === CD
&& !va(c, d.arm))
|| (d.kind === PL && c.kind === PL
&& (d.lane === 'each'
? c[LC] === 0
: d.lane >= c[LC]))
|| (t.kind === CA && d.kind === PL
&& c.kind === PL
&& !c[MF].includes(t.id))) {
fl('operation control reference');
}
}
if (fh(t, (v) => v.kind === 'parameter' && v.scope === 'iteration')
&& !t.control.some((d) => {
const c = d.kind === 'loop'
? f.get(d[CF])
: undefined;
return c?.kind === 'loop'
&& c.loop_kind === 'array_iteration';
})) {
fl('iteration parameter');
}
if (t.kind === PL) {
const l = t.input?.kind === 'array'
? t.input.elements.length
: 0;
if (t[MF].some((i) => {
const m = f.get(i);
const r = m?.control.find((d) => d.kind === PL
&& d[CF] === t.id);
const p = r?.lane === 'each'
? m?.control.some((d) => {
const c = d.kind === 'loop'
? f.get(d[CF])
: undefined;
return c?.kind === 'loop'
&& c.loop_kind === 'array_iteration';
})
: true;
return !m || m.kind !== CA || !r || !p
|| m[OW] !== t[OW];
}) || t[LC] !== l) {
fl('parallel member reference');
}
}
if (t.kind === 'persistence') {
const a = f.get(t.call_fact_id);
if (!a || a.kind !== CA
|| a[OW] !== t[OW]
|| !ss(a[EV].range, t[EV].range)
|| !ss(a[EV][SR], t[EV][SR])
|| a[EV][EH] !== t[EV][EH]
|| a.order[0] !== t.order[0]
|| a.order[2] !== t.order[2]
|| JSON.stringify(a.control) !== JSON.stringify(t.control)
|| !bd(t.receiver_type, MAX_TEXT)) {
fl('persistence call reference');
}
}
}
const r = new Map();
for (const [u, t, a] of v.edgeEntries()) {
const e = a.relation;
const x = c.get(u);
const y = c.get(t);
const g = x !== undefined || y !== undefined;
if (oh(a, 'dispatch_payload_argument')
&& (!g || e !== 'publishes_to'))
fl('dispatch payload relation');
if (!g && !RELATIONS.has(String(e)))
continue;
if (!RELATIONS.has(String(e))) {
fl('channel relation');
}
if (!ep(a, l, b, y, o)) {
fl('channel evidence');
}
const w = a[EO];
if (e === 'publishes_to') {
if (!s.has(u) || !y
|| u !== w
|| !['queue', 'job', 'event'].includes(y[CH])) {
fl('publishes_to endpoints');
}
}
else if (e === 'routes_through') {
if (!x || x[CH] !== 'job'
|| !y || y[CH] !== 'queue'
|| x[PH] !== t
|| x[TR] !== y[TR]) {
fl('routes_through endpoints');
}
r.set(u, (r.get(u) ?? 0) + 1);
}
else if (e === 'consumed_by') {
if (!x || !s.has(t) || y) {
fl('consumed_by endpoints');
}
}
}
for (const h of c.values()) {
if (h[CH] === 'job'
&& r.get(h.id) !== 1) {
fl('job routing');
}
}
for (const x of o.values()) {
x.sort((a, b) => oc(a.order, b.order) || cc(a.id, b.id));
x.forEach(fr);
of(x);
}
for (const x of k.values()) {
x.sort((a, b) => cc(a.id, b.id));
x.forEach(fr);
of(x);
}
f.forEach(fr);
c.forEach(fr);
return {
operation_by_id: sm(se(f)),
operations_by_owner: sm(se(o)),
channels_by_id: sm(se(c)),
channels_by_key: sm(se(k)),
};
}
function cg(s) {
const v = new KnowledgeGraph(s.graph);
for (const [i, a] of s.nodeEntries()) {
v.addNode(i, a);
}
for (const [f, t, a, e] of s.edgeEntries()) {
const i = v.addEdge(f, t, a);
if (i !== e) {
throw new Error('Canonical graph edge identity changed while sealing query index');
}
}
return v;
}
const sg = (v) => of({
hasNode: (i) => v.hasNode(i), hasEdge: (s, t) => v.hasEdge(s, t),
nodeEntries: () => v.nodeEntries(), edgeEntries: () => v.edgeEntries(),
predecessors: (i) => v.predecessors(i), successors: (i) => v.successors(i),
edgesBetween: (s, t) => v.edgesBetween(s, t),
nodeAttributes: (i) => v.nodeAttributes(i),
});
export function failedQueryIndex(state, subject) { return { state, subject }; }
export function inspectQueryIndex(graph) {
let snapshot;
let v;
try {
snapshot = graphSnapshot(graph);
v = cg(graph);
}
catch {
return failedQueryIndex('corrupt', 'canonical graph snapshot');
return failedQueryIndex(CR, 'canonical graph snapshot');
}
const build = readBuildState(snapshot);
const root = snapshot.graph.root_path;
if (!build || snapshot.graph.canonical_typescript_index !== true
|| snapshot.graph.schema_version !== CANONICAL_INDEX_FORMAT_VERSION
|| typeof root !== 'string' || root.trim().length === 0
|| build.source_root.root_path !== root) {
return failedQueryIndex('corrupt', 'canonical TypeScript index metadata');
const b = readBuildState(v);
const r = v.graph.root_path;
if (!b || v.graph.canonical_typescript_index !== true
|| v.graph.schema_version !== CANONICAL_INDEX_FORMAT_VERSION
|| typeof r !== 'string' || r.trim().length === 0
|| b.source_root.root_path !== r) {
return failedQueryIndex(CR, 'canonical TypeScript index metadata');
}
if (build.completeness.summary.state !== 'complete'
|| build.completeness.supported_failures.length > 0) {
if (b.completeness.summary.state !== 'complete'
|| b.completeness.supported_failures.length > 0) {
return failedQueryIndex('unavailable', 'canonical TypeScript index incomplete');
}
const hashes = new Map();
for (const [, attributes] of snapshot.nodeEntries()) {
if (attributes.node_kind !== 'file')
const h = new Map();
const f = new Map();
for (const [i, a] of v.nodeEntries()) {
if (a[NK] !== 'file')
continue;
const sourceFile = attributes.source_file;
const contentHash = attributes.content_hash;
if (typeof sourceFile !== 'string' || typeof contentHash !== 'string'
|| !/^[a-f0-9]{64}$/.test(contentHash)) {
return failedQueryIndex('corrupt', 'canonical file-node hash');
const s = a[SF];
const x = a.content_hash;
if (typeof s !== 'string' || typeof x !== 'string'
|| !SHA256.test(x)) {
return failedQueryIndex(CR, 'canonical file-node hash');
}
if (hashes.has(sourceFile)) {
return failedQueryIndex('corrupt', sourceFile);
if (h.has(s) || f.has(s)) {
return failedQueryIndex(CR, s);
}
hashes.set(sourceFile, contentHash);
h.set(s, x);
f.set(s, i);
}
if (hashes.size !== build.sources.supported.length
|| build.sources.supported.some((source) => hashes.get(source.path) !== source.hash)) {
return failedQueryIndex('corrupt', 'canonical file-node coverage');
if (h.size !== b.sources.supported.length
|| b.sources.supported.some((s) => h.get(s.path) !== s.hash)) {
return failedQueryIndex(CR, 'canonical file-node coverage');
}
return Object.freeze({
state: 'ready', graph: immutableQueryGraph(snapshot), root_path: root,
file_hashes: immutableMap(hashes),
unsupported_sources: Object.freeze(build.sources.unsupported.map((source) => Object.freeze({ ...source }))),
let e;
try {
e = bm(v, f);
}
catch (x) {
return failedQueryIndex(CR, x instanceof IntegrityError
? x.message
: 'canonical execution index');
}
for (const [i, a] of v.nodeEntries()) {
if (!oh(a, 'body_facts'))
continue;
const { body_facts: _, ...t } = a;
v.replaceNodeAttributes(i, t);
}
return of({
state: 'ready',
graph: sg(v),
root_path: r,
file_hashes: sm(h),
unsupported_sources: of(b.sources.unsupported.map((s) => of({ ...s }))),
...e,
});
}

@@ -1,11 +0,4 @@

import type { GraphAttributes } from '../graph/directed-multigraph.js';
import type { IndexRange } from '../index/model.js';
export declare const RETRIEVE_RESULT_SCHEMA: "madar.retrieve";
export declare const RETRIEVE_RESULT_VERSION: 1;
export declare const DEFAULT_RETRIEVE_BUDGET = 4000;
export declare const MIN_RETRIEVE_BUDGET = 256;
export declare const MAX_RETRIEVE_BUDGET = 4000;
export declare const MAX_RETRIEVE_QUESTION_LENGTH = 512;
export declare const MAX_RETRIEVE_FILES = 12;
export declare const MAX_RETRIEVE_SNIPPETS = 25;
import type { IndexBodyFact, IndexRange, IndexValue } from '../index/model.js';
export declare const RETRIEVE_RESULT_SCHEMA: "madar.retrieve", RETRIEVE_RESULT_VERSION: 2, DEFAULT_RETRIEVE_BUDGET = 4000, MIN_RETRIEVE_BUDGET = 256, MAX_RETRIEVE_BUDGET = 4000, MAX_RETRIEVE_QUESTION_LENGTH = 512, MAX_RETRIEVE_FILES = 12, MAX_RETRIEVE_EXCERPTS = 25;
export declare function valueHas(value: IndexValue, test: (candidate: IndexValue) => boolean): boolean;
export interface NormalizedRetrieveRequest {

@@ -15,90 +8,218 @@ question: string;

}
export type EvidenceBoundaryKind = 'missing' | 'disconnected' | 'unsupported' | 'stale' | 'unavailable' | 'corrupt' | 'truncated';
export interface EvidenceBoundary {
kind: EvidenceBoundaryKind;
export type RetrieveIntent = 'locate' | 'explain' | 'workflow';
type L<T> = readonly T[];
type M<T> = ReadonlyMap<string, T>;
type FS = 'stale' | 'unavailable' | 'corrupt';
type EF = {
state: FS;
subject: string;
detail?: string;
};
export type RetrieveObligationKind = 'subject' | 'entry' | 'stage' | 'handoff' | 'behavior' | 'ordering' | 'terminal';
export type RetrieveState = 'ready' | 'incomplete' | 'unsupported' | FS;
type MK = `${'budget' | 'serialized'}_tokens` | 'selected_files' | 'authenticated_excerpts' | `${'required' | 'proven'}_obligations` | 'optional_bundles_omitted' | `${'root' | 'initial'}_candidates` | 'explored_nodes' | 'causal_hops' | 'recovery_frontier_nodes' | 'alternate_seeds';
export type RetrieveMetrics = Record<MK, number> & {
recovery_passes: 0 | 1 | 2;
};
export interface QuerySummary {
intent: RetrieveIntent;
subject: string;
terms: L<string>;
}
export interface RankedQueryNode {
id: string;
attributes: GraphAttributes;
score: number;
matchedTerms: string[];
firstMatch: number;
export type QueryIntent = RetrieveIntent;
export type LocateAccess = 'read' | 'write';
export type ObligationKind = RetrieveObligationKind;
export interface QueryObligation {
id: `o${number}`;
kind: ObligationKind;
target: string;
mandatory: boolean;
}
export interface RankQueryResult {
anchors: RankedQueryNode[];
boundaries: EvidenceBoundary[];
queryTerms: string[];
flow: boolean;
branch: readonly string[];
sequential?: boolean;
priorityAnchorIds?: readonly string[];
coveredTerms?: readonly string[];
structuralRequired?: boolean;
structuralCoverageComplete?: boolean;
export interface QueryPlan {
intent: QueryIntent;
subject: string;
terms: L<string>;
obligations: L<QueryObligation>;
access?: LocateAccess;
}
export interface QueryPathEdge {
id: string;
from: string;
to: string;
relation: string;
attributes: GraphAttributes;
export type QuestionPlanResult = {
status: 'supported';
plan: QueryPlan;
} | {
status: 'unsupported';
reason: 'unsupported_intent' | 'missing_subject';
terms: L<string>;
};
export type RetrieveMissingCode = `${'subject' | 'entrypoint' | 'terminal_persistence' | 'corridor' | 'obligation_target' | 'adjacent_handoff' | 'behavior' | 'controller_dependency'}_unproven` | 'selection_bound_reached' | `required_${'file_limit' | 'excerpt_limit' | 'token_budget' | 'proof_missing' | 'reference_missing'}`;
export interface MissingRequirement {
code: RetrieveMissingCode;
obligation_id?: string;
target?: string;
required?: number;
limit?: number;
}
export interface QuerySlice {
nodeIds: string[];
edges: QueryPathEdge[];
boundaries: EvidenceBoundary[];
closurePasses: 0 | 1;
type SF<K extends PropertyKey> = Record<K, string>;
type Tag<K extends string> = {
kind: K;
};
type DR<K extends PropertyKey = never> = SF<'id' | K>;
type PR<K extends PropertyKey = never> = DR<K> & {
proofs: L<string>;
};
type OR = {
operationIds: L<string>;
};
type WR = OR & {
symbolIds: L<string>;
};
export type WorkflowRelation = 'calls' | 'publishes_to' | 'routes_through' | 'consumed_by';
export type WorkflowMissingCode = Extract<RetrieveMissingCode, `${string}_unproven` | 'selection_bound_reached'>;
export type WorkflowEdge = SF<'id' | 'fromId' | 'toId'> & {
relation: WorkflowRelation;
};
export type WorkflowHandoff = OR & SF<'fromId' | 'toId'> & {
kind: 'direct' | 'channel';
edgeIds: L<string>;
};
export type WorkflowControlGroup = WR & {
kind: 'branch' | 'loop' | 'parallel' | 'cycle' | 'sequence';
controllerOperationId?: string;
arm?: string;
};
export type WorkflowObligationProof = WR & {
id: `o${number}`;
kind: RetrieveObligationKind;
target: string;
mandatory: boolean;
proven: boolean;
edgeIds: L<string>;
};
export type WorkflowMissingReason = {
code: WorkflowMissingCode;
obligationId?: string;
target: string;
};
export type WorkflowSelection = WR & {
complete: boolean;
rootSymbolIds: L<string>;
terminalSymbolIds: L<string>;
edges: L<WorkflowEdge>;
links: L<WorkflowHandoff>;
controlGroups: L<WorkflowControlGroup>;
obligations: L<WorkflowObligationProof>;
missing: L<WorkflowMissingReason>;
metrics: {
candidateCount: number;
rootCandidateCount: number;
actualNodeCount: number;
causalRelationHops: number;
recoveryPasses: 0 | 1 | 2;
recoveryFrontierCount: number;
bounded: boolean;
};
};
export type ProvenObligation = PR<'statement'> & {
kind: RetrieveObligationKind;
};
export type DossierFile = DR<'path' | 'digest'>;
export type DossierExcerpt = DR<'file' | 'text'> & {
range: readonly [number, number, number, number];
};
export type DossierControl = DR<'file'> & {
ranges: L<readonly [number, number, number, number]>;
};
export type DossierEntity = DR & (Tag<'symbol'> & SF<'label' | 'file'> & {
node_kind?: string;
excerpt?: string;
} | Tag<'channel'> & SF<'transport' | 'key'> & {
channel_kind: 'queue' | 'job' | 'event';
parent?: string;
scope?: string;
} | Tag<'operation'> & {
excerpt: string;
} & SF<'operation_kind' | 'owner'> & {
detail: Readonly<Record<string, unknown>>;
});
export type DossierProof = DR<'from' | 'to' | 'relation'> & (SF<'excerpt'> | {
file: string;
range: readonly [number, number, number, number];
});
export type DossierLink = PR<'from' | 'to'> & {
kind: 'direct' | 'channel';
};
export type DossierOrderGroup = DR & {
kind: 'branch' | 'loop' | 'parallel' | 'cycle' | 'sequence';
controller?: string;
arm?: string;
detail?: Readonly<Record<string, unknown>>;
depths?: L<number>;
members: L<string>;
proofs?: L<string>;
};
export interface AnswerDossier {
query: QuerySummary;
obligations: L<ProvenObligation>;
flow: {
roots: L<string>;
terminals: L<string>;
links: L<DossierLink>;
order: L<DossierOrderGroup>;
};
evidence: {
digest_algorithm: 'sha256-base64url';
files: L<DossierFile>;
excerpts: L<DossierExcerpt>;
controls: L<DossierControl>;
entities: L<DossierEntity>;
proofs: L<DossierProof>;
};
}
interface EvidenceNodeBase {
node_id: string;
label: string;
source_file: string;
source_domain?: string;
provenance: unknown[];
content_hash: string;
export type SelectedEvidenceEdge = DR<'fromId' | 'toId'> & {
relation?: string;
};
export interface EvidenceHydrationTargets {
symbolIds: L<string>;
declarationSymbolIds: L<string>;
operationIds: L<string>;
validationOperationIds?: L<string>;
edges: L<SelectedEvidenceEdge>;
}
export type EvidenceNode = EvidenceNodeBase & ({
evidence_kind: 'structural_file';
node_kind: 'file';
snippet?: undefined;
definition_range?: undefined;
declaration_range?: undefined;
} | {
evidence_kind: 'symbol_declaration';
node_kind: string;
source_location: string;
line_number: number;
end_line_number: number;
definition_range: IndexRange;
declaration_range: IndexRange;
snippet: string;
});
export interface EvidenceRelationship {
id: string;
from_id: string;
to_id: string;
relation: string;
source_file?: string;
source_location?: string;
provenance: unknown[];
}
export type RetrieveOutcome = 'evidence' | 'missing' | 'unsupported' | 'stale' | 'unavailable' | 'corrupt';
export interface RetrieveContextResult {
export type HydratedFile = readonly [string, string];
export type HydratedExcerpt = readonly [string, string, IndexRange, string, string];
export type HydratedControl = readonly [string, IndexRange];
export type HydratedEntity = readonly [string, 'symbol', string, string, string] | readonly [
string,
'channel',
'queue' | 'job' | 'event',
string,
string,
string | undefined,
string | undefined
] | readonly [string, 'operation', string, IndexBodyFact];
export type HydratedProof = readonly [string, 'declaration' | 'operation', string, string] | readonly [string, 'edge', string, string, string, string] | readonly [string, 'edge_range', string, string, string, string, IndexRange];
export type HydratedEvidenceResult = {
state: 'ready';
files: M<HydratedFile>;
controls: M<HydratedControl>;
entities: M<HydratedEntity>;
excerpts: M<HydratedExcerpt>;
proofs: M<HydratedProof>;
} | EF;
interface RB<S extends RetrieveState> {
schema: typeof RETRIEVE_RESULT_SCHEMA;
version: typeof RETRIEVE_RESULT_VERSION;
outcome: RetrieveOutcome;
matched_nodes: EvidenceNode[];
relationships: EvidenceRelationship[];
boundaries: EvidenceBoundary[];
metrics: {
selected_files: number;
snippets: number;
closure_passes: 0 | 1;
serialized_tokens: number;
truncated: boolean;
};
state: S;
metrics: RetrieveMetrics;
}
export type RetrieveContextResult = RB<'ready'> & {
dossier: AnswerDossier;
} | RB<'incomplete'> & {
query: QuerySummary;
missing: L<MissingRequirement>;
} | RB<'unsupported'> & {
reason: 'unsupported_intent' | 'missing_subject' | 'unsupported_source';
terms: L<string>;
} | RB<FS> & {
failures: L<EF>;
};
export declare function normalizeRetrieveRequest(value: unknown): NormalizedRetrieveRequest;
export {};

@@ -1,9 +0,8 @@

export const RETRIEVE_RESULT_SCHEMA = 'madar.retrieve';
export const RETRIEVE_RESULT_VERSION = 1;
export const DEFAULT_RETRIEVE_BUDGET = 4000;
export const MIN_RETRIEVE_BUDGET = 256;
export const MAX_RETRIEVE_BUDGET = 4000;
export const MAX_RETRIEVE_QUESTION_LENGTH = 512;
export const MAX_RETRIEVE_FILES = 12;
export const MAX_RETRIEVE_SNIPPETS = 25;
export const RETRIEVE_RESULT_SCHEMA = 'madar.retrieve', RETRIEVE_RESULT_VERSION = 2, DEFAULT_RETRIEVE_BUDGET = 4000, MIN_RETRIEVE_BUDGET = 256, MAX_RETRIEVE_BUDGET = 4000, MAX_RETRIEVE_QUESTION_LENGTH = 512, MAX_RETRIEVE_FILES = 12, MAX_RETRIEVE_EXCERPTS = 25;
export function valueHas(value, test) {
return test(value)
|| value.kind === 'array' && value.elements.some((entry) => valueHas(entry, test))
|| value.kind === 'object' && value.entries.some((entry) => valueHas(entry.value, test))
|| value.kind === 'template' && value.parts.some((entry) => valueHas(entry, test));
}
export function normalizeRetrieveRequest(value) {

@@ -26,3 +25,6 @@ if (value === null || typeof value !== 'object' || Array.isArray(value)) {

}
return { question, budget: Math.max(MIN_RETRIEVE_BUDGET, Math.min(budget ?? DEFAULT_RETRIEVE_BUDGET, MAX_RETRIEVE_BUDGET)) };
return {
question,
budget: Math.max(MIN_RETRIEVE_BUDGET, Math.min(budget ?? DEFAULT_RETRIEVE_BUDGET, MAX_RETRIEVE_BUDGET)),
};
}
{
"name": "@lubab/madar",
"version": "0.40.0-beta.4",
"version": "0.40.0-beta.5",
"mcpName": "io.github.mohanagy/madar",

@@ -15,4 +15,2 @@ "description": "Give AI coding agents a small, authenticated evidence path through large TypeScript and JavaScript repositories.",

"examples/sample-workspace/",
"examples/why-madar.md",
"CHANGELOG.md",
"README.md",

@@ -19,0 +17,0 @@ "LICENSE"

@@ -11,7 +11,7 @@ # Madar

The result is a small set of exact source excerpts and directed relationships, or an explicit boundary explaining why evidence could not be returned. There are no tool profiles or alternate retrieval modes to choose.
The result is a complete, ordered answer dossier backed by exact source evidence, or an exact non-ready state naming what could not be proven. There are no tool profiles, fallback searches, or alternate retrieval modes to choose.
MCP advertises only the tools capability. It exposes no resources or prompts.
[![npm next](https://img.shields.io/npm/v/%40lubab%2Fmadar/next?label=npm%20next)](https://www.npmjs.com/package/@lubab/madar/v/0.40.0-beta.4)
[![npm next](https://img.shields.io/npm/v/%40lubab%2Fmadar/next?label=npm%20next)](https://www.npmjs.com/package/@lubab/madar/v/0.40.0-beta.5)
[![node >=20](https://img.shields.io/badge/node-%E2%89%A520-3c873a)](https://nodejs.org/)

@@ -23,3 +23,3 @@ [![local first](https://img.shields.io/badge/local--first-no%20cloud%20required-0f766e)](#local-by-design)

See [beta.4 changes](https://github.com/mohanagy/madar/blob/next/CHANGELOG.md#0400-beta4---2026-07-30). No comparative performance or retention claim.
See [beta.5 changes](https://github.com/mohanagy/madar/blob/next/CHANGELOG.md#0400-beta5---2026-08-02). This is a manual-test candidate before #631 qualification; it makes no comparative performance or retention claim.

@@ -65,5 +65,5 @@ ## Start in three steps

Results contain authenticated nodes and excerpts, directed relationships, explicit boundaries, and size metrics. `evidence` means the returned path is usable; other outcomes name the focused verification needed instead of implying a path Madar did not prove.
`ready` contains a non-truncated dossier: the normalized query, proven obligations, roots and terminals, direct or channel links, partial-order groups, and SHA-256-authenticated files, excerpts, controls, entities, and proofs. `incomplete`, `unsupported`, `stale`, `unavailable`, and `corrupt` name the exact condition instead of implying a path Madar did not prove.
Results include at most 12 files, 25 snippets, one directional closure pass, and 4,000 serialized tokens. See [MCP response shape](https://github.com/mohanagy/madar/blob/next/docs/mcp-response-shape.md) for the exact envelope.
Results include at most 12 files, 25 authenticated excerpts, two bounded recovery passes, and 4,000 serialized tokens. See [MCP response shape](https://github.com/mohanagy/madar/blob/next/docs/mcp-response-shape.md) for the exact envelope.

@@ -82,3 +82,3 @@ ## How it works

v
exact excerpts + directed relationships
ordered claims + authenticated proof
```

@@ -85,0 +85,0 @@

Sorry, the diff of this file is too big to display

import type { ReadyQueryIndex } from './index-status.js';
import type { NormalizedRetrieveRequest, RankQueryResult } from './types.js';
export declare function rankQueryAnchors(index: ReadyQueryIndex, request: NormalizedRetrieveRequest): RankQueryResult;
import { compareCodeUnits as compare } from '../graph/canonical-json.js';
import { classifySourceDomain, isPollutedSourcePath, sourceDomainOf, } from './source-domain.js';
import { MAX_RETRIEVE_FILES as FILE_CAP, MAX_RETRIEVE_SNIPPETS as SNIPPET_CAP, } from './types.js';
const CAUSAL = new Set(['calls', 'enqueues_job']);
const RELATIONS = ['calls', 'contains', 'enqueues_job', 'imports_from'];
const LAST = Number.MAX_SAFE_INTEGER;
const STOP = new Set('a actual an and any applicabl are as at be by bas being can do does exist final for from get gett handl explain how in initial is it its me new of on operat or specific tell that the then through to trace what when which with work you'.split(' '));
const UNSUPPORTED = /^(?:bash|c|cc|cljs|clj|cpp|cs|cxx|dart|elm|ex|exs|fs|fsx|go|groovy|h|hpp|hs|java|jl|kt|kts|lua|m|mm|php|ps1|py|r|rb|rs|scala|sh|sol|sql|svelte|swift|vue|zig)$/u;
const DOMAIN_TERMS = {
test: ['test', 'spec', 'e2e'],
benchmark: ['benchmark', 'bench', 'performance'],
fixture: ['fixture', 'mock'],
generated: ['generated'],
docs: ['doc', 'documentation', 'readme'],
config: ['config', 'configuration', 'setting'],
};
const STEMS = [
[7, 'ization', 'ize'], [5, 'ies', 'y'], [6, 'ence', ''], [6, 'ance', ''],
[8, 'ment', ''], [5, 'ions', ''], [4, 'ion', ''], [5, 'ing', ''],
[4, 'ery', 'er'], [4, 'ed', ''], [4, 's', ''], [5, 'e', ''],
];
const corpusCache = new WeakMap();
function stem(value) {
if (/^\d+$/.test(value))
return value;
let result = value;
for (let pass = 0; pass < 2; pass += 1) {
const rule = STEMS.find(([minimum, suffix]) => result.length > minimum && result.endsWith(suffix)
&& (suffix !== 's' || !result.endsWith('ss')));
if (!rule)
break;
result = `${result.slice(0, -rule[1].length)}${rule[2]}`;
}
return result;
}
function tokens(value) {
const separated = value
.replace(/([a-z\d])([A-Z])/g, '$1 $2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
.toLowerCase();
return (separated.match(/[a-z][a-z0-9]*|\d+/g) ?? [])
.flatMap((token) => {
const parts = token.match(/[a-z]+|\d+/g) ?? [];
return parts.length >= 3 ? parts : [token];
})
.map(stem)
.filter((token) => token.length > 1 || /^\d+$/.test(token));
}
function words(value) {
return tokens(value).filter((token) => !STOP.has(token));
}
function matches(values, t) {
if (values.has(t))
return true;
if (t.length < 3)
return false;
const singular = t.length >= 4 && t.endsWith('s') && !t.endsWith('ss')
? t.slice(0, -1) : `${t}s`;
const bare = t.endsWith('e') ? t.slice(0, -1) : `${t}e`;
for (const form of [t, singular, bare]) {
if (values.has(form))
return true;
for (const prefix of ['en', 're', 'un']) {
if (values.has(`${prefix}${form}`)
|| (form.startsWith(prefix) && values.has(form.slice(prefix.length))))
return true;
}
}
return false;
}
function text(attributes, key) {
const value = attributes[key];
return typeof value === 'string' ? value : '';
}
function field(value, weight) {
const lexical = tokens(value);
return lexical.length === 0 ? null : {
compact: lexical.join(''), tokens: new Set(lexical), weight,
};
}
function buildCorpus(index) {
const cached = corpusCache.get(index)?.deref();
if (cached)
return cached;
const nodes = [];
const paths = new Map();
for (const [id, attributes] of index.graph.nodeEntries()) {
const file = text(attributes, 'source_file');
const kind = text(attributes, 'node_kind');
if (!paths.has(file))
paths.set(file, field(file, 7));
const pathField = paths.get(file) ?? null;
const fields = [
field(text(attributes, 'label'), 12),
field(text(attributes, 'qualified_name'), 12),
field(`${text(attributes, 'framework')} ${text(attributes, 'framework_role')}`, 5),
field(JSON.stringify(attributes.framework_metadata) ?? '', 5),
field(kind, 3),
pathField,
].filter((field) => !!field);
const eligible = !isPollutedSourcePath(file, index.root_path)
&& (kind === 'file'
? !!file && Array.isArray(attributes.provenance)
&& attributes.provenance.length > 0 && index.file_hashes.has(file)
: !!attributes.definition_range && !!attributes.declaration_range
&& !(attributes.framework_metadata
&& typeof attributes.framework_metadata === 'object'
&& 'external_call' in attributes.framework_metadata
&& attributes.framework_metadata.external_call === true));
nodes.push({
id, attributes, file, kind, fields,
domain: sourceDomainOf(attributes.source_domain, file, index.root_path),
tokens: new Set(fields.flatMap((field) => [...field.tokens])),
pathTokens: pathField?.tokens ?? new Set(),
ins: [], outs: [], eligible,
defined: kind === 'file'
|| JSON.stringify(attributes.declaration_range)
!== JSON.stringify(attributes.definition_range),
});
}
const byId = new Map(nodes.map((n) => [n.id, n]));
const members = new Map();
for (const [from, to, attributes] of index.graph.edgeEntries()) {
const source = byId.get(from);
const target = byId.get(to);
const relation = text(attributes, 'relation');
if (source && target && CAUSAL.has(relation)
&& source.kind !== 'file' && target.kind !== 'file') {
source.outs.push(to);
target.ins.push(from);
}
if (source?.kind === 'class' && target
&& (relation === 'contains' || relation === 'method')) {
target.owner = from;
const owned = members.get(from) ?? [];
owned.push(to);
members.set(from, owned);
}
}
for (const n of nodes) {
n.ins = [...new Set(n.ins)].sort(compare);
n.outs = [...new Set(n.outs)].sort(compare);
}
for (const [owner, owned] of members) {
members.set(owner, [...new Set(owned)].sort(compare));
}
const docs = new Map();
const files = new Map();
for (const n of nodes) {
const siblings = files.get(n.file) ?? [];
siblings.push(n);
files.set(n.file, siblings);
const document = docs.get(n.file || n.id) ?? new Set();
for (const field of n.fields)
for (const token of field.tokens)
document.add(token);
docs.set(n.file || n.id, document);
}
const freq = new Map();
for (const document of docs.values()) {
for (const token of document) {
freq.set(token, (freq.get(token) ?? 0) + 1);
}
}
const c = {
nodes, byId, members, files, freq, docs: docs.size,
};
corpusCache.set(index, new WeakRef(c));
return c;
}
function scopes(question) {
const result = [];
const seen = new Set();
const patterns = [
[/`([A-Za-z_$][A-Za-z0-9_$.:]*)`/g, false],
[/\b([A-Za-z_$][A-Za-z0-9_$]*[A-Z][A-Za-z0-9_$]*)\b/g, false],
[/\b(?:[A-Za-z0-9_$.[\]-]+\/)+[A-Za-z0-9_$.[\]-]+\.(?:[cm]?[jt]sx?)\b/g, true],
[/\b(?=[a-z0-9-]*\d)[a-z][a-z0-9]*(?:-[a-z0-9]+)+\b/g, true],
];
for (const [pattern, hard] of patterns) {
for (const match of question.matchAll(pattern)) {
const subject = (match[1] ?? match[0]).trim();
if (subject === match[0] && /^[A-Z]+$/u.test(subject))
continue;
const lexical = tokens(subject);
const compact = lexical.join('');
if (!compact || seen.has(compact))
continue;
seen.add(compact);
result.push({
subject, tokens: lexical, compact,
first: match.index ?? LAST, hard,
});
}
}
const qualified = result.filter((s) => !s.hard && s.subject.includes('.'));
return result.filter((s) => !qualified.some((parent) => parent !== s
&& s.first > parent.first
&& s.first <= parent.first + parent.subject.length + 1
&& parent.subject.split('.').includes(s.subject)))
.sort((a, b) => a.first - b.first || compare(a.subject, b.subject));
}
function vocabulary(question) {
const task = question.replace(/\.\s+(?:cite|use|report)\b[\s\S]*$/iu, '');
const raw = tokens(task);
const terms = [];
const pos = new Map();
for (const [position, t] of raw.entries()) {
if (!STOP.has(t) && !pos.has(t)) {
terms.push(t);
pos.set(t, position);
}
}
const lower = task.toLowerCase();
for (const relation of RELATIONS) {
const variants = [relation, relation.replaceAll('_', ' ')];
const found = variants.map((variant) => lower.indexOf(variant))
.filter((position) => position >= 0);
if (found.length > 0 && !pos.has(relation)) {
terms.push(relation);
pos.set(relation, Math.min(...found));
}
}
const explicit = scopes(task);
const clauses = task
.split(/[,;:\u2013\u2014]+|[!?]+|\.(?=\s+[A-Z])|\b(?:[Aa][Nn][Dd]\s+)?[Tt][Hh][Ee][Nn]\b/u)
.map(words).filter((part) => part.length > 0);
const parts = (clauses.length > 0 ? clauses : [terms])
.map((part) => new Set(part));
const connector = raw.findIndex((t, index) => index > 0 && index < raw.length - 1
&& (t === 'through' || t === 'until'));
const directed = (raw.includes('from')
&& raw.some((t) => t === 'to' || t === 'through'))
|| connector >= 0
|| raw.some((t, index) => t === 'end' && raw[index + 1] === 'to' && raw[index + 2] === 'end');
const from = raw.indexOf('from');
const to = raw.indexOf('to', from + 1);
const parallel = from >= 0 && to > from && raw.includes('and');
const explainsProcess = raw.includes('how') && terms.length > 1
&& explicit.every((s) => s.hard);
const structural = directed || parts.length > 1
|| explainsProcess
|| terms.some((t) => [
'flow', 'handoff', 'journey', 'lifecycl', 'orchestrat',
'pipelin', 'process', 'queue', 'sequenc', 'stag',
].includes(t));
const expand = directed || parts.length > 1
|| (terms.some((t) => t === 'stag' || t === 'stage')
&& terms.some((t) => ['job', 'jobs', 'queue', 'enqueu', 'orchestrat'].includes(t)));
const domains = new Set(Object.entries(DOMAIN_TERMS)
.filter(([, variants]) => variants.some((variant) => terms.includes(variant)))
.map(([domain]) => domain));
return {
terms, pos, scopes: explicit,
limits: explicit.filter((s) => s.hard),
parts, mentions: new Set(task.split(/[^A-Za-z0-9_$]+/u)), domains,
structural, expand,
sequential: connector >= 0 || raw.includes('then') || (directed && !parallel),
};
}
function inScope(n, s) {
return s.tokens.every((token) => n.tokens.has(token))
&& n.fields.some((field) => field.compact.includes(s.compact));
}
function rarity(c, t) {
const seen = c.freq.get(t) ?? 0;
return Math.max(1, Math.round((1 + Math.log2((c.docs + 1) / (seen + 1))) * 64));
}
function exactLabel(n, q) {
const label = text(n.attributes, 'label');
const identifier = label.replace(/^\./u, '').replace(/\(\)$/u, '');
return (label.endsWith('()') || /[A-Z_$\d.:]/u.test(identifier))
&& words(identifier).length > 0 && q.mentions.has(identifier);
}
function termsOf(n, q, semantic = false) {
return q.terms.filter((t) => n.fields.some((field) => (!semantic || field.weight !== 7) && matches(field.tokens, t)));
}
function score(c, n, q) {
const hits = termsOf(n, q);
const context = q.terms.filter((t) => !hits.includes(t)
&& [...n.ins, ...n.outs].some((id) => {
const adjacent = c.byId.get(id);
return !!adjacent?.eligible && matches(adjacent.tokens, t);
}));
if (hits.length === 0 && context.length === 0
&& !q.scopes.some((s) => inScope(n, s)))
return null;
let value = n.domain === 'production' ? 500
: n.domain === 'test' ? -250
: n.domain === 'unknown' ? 0 : -500;
for (const t of hits) {
const weight = Math.max(0, ...n.fields
.filter((field) => matches(field.tokens, t)).map((field) => field.weight));
value += rarity(c, t) * weight;
if (n.fields.some((field) => field.weight !== 7 && matches(field.tokens, t))) {
value += rarity(c, t) * 12;
}
if (matches(n.pathTokens, t))
value += rarity(c, t) * 7;
}
for (const t of context)
value += rarity(c, t) * 5;
for (const s of q.scopes.filter((s) => !s.hard)) {
if (n.fields.some((field) => field.compact === s.compact))
value += 2_000_000;
else if (inScope(n, s))
value += 1_000_000;
}
if (exactLabel(n, q))
value += 2_000_000;
const firstMatch = hits.reduce((first, t) => Math.min(first, q.pos.get(t) ?? LAST), LAST);
return {
id: n.id, attributes: n.attributes, score: value,
matchedTerms: [...hits, ...context], firstMatch,
};
}
function byRank(a, b) {
return b.rank.score - a.rank.score
|| a.rank.firstMatch - b.rank.firstMatch
|| b.n.outs.length - a.n.outs.length
|| compare(a.n.file, b.n.file)
|| compare(a.n.id, b.n.id);
}
function inDomain(n, q) {
return q.domains.size > 0
? q.domains.has(n.domain)
: n.domain === 'production' || n.domain === 'unknown';
}
function scoredNodes(c, q, keep) {
const items = c.nodes.flatMap((n) => {
if (!n.eligible || !keep(n)
|| (q.limits.length > 0
&& !q.limits.some((s) => inScope(n, s))))
return [];
if (n.kind === 'file' && !q.terms.includes('imports_from')
&& !q.limits.some((s) => s.subject.includes('/') && inScope(n, s)))
return [];
if ((!n.defined || n.kind === 'interface' || n.kind === 'type-alias')
&& !q.terms.some((t) => tokens(n.kind).includes(t))
&& !q.terms.some((t) => t === 'defin' || t === 'declar')
&& !q.scopes.some((s) => inScope(n, s))
&& !exactLabel(n, q))
return [];
if (!inDomain(n, q) && !q.scopes.some((s) => inScope(n, s)))
return [];
const rank = score(c, n, q);
if (!rank)
return [];
return [{ n, rank }];
}).sort(byRank);
if (q.parts.length > 1 || q.scopes.length > 0)
return items;
const locator = q.terms[0] === 'where';
const byFile = new Map();
for (const item of items) {
const current = byFile.get(item.n.file || item.n.id) ?? [];
current.push(item);
byFile.set(item.n.file || item.n.id, current);
}
return [...byFile.values()].flatMap((entries) => entries.sort((a, b) => {
if (locator)
return byRank(a, b);
const ids = new Set(entries.map((entry) => entry.n.id));
return b.n.outs.filter((id) => ids.has(id)).length
- a.n.outs.filter((id) => ids.has(id)).length
|| byRank(a, b);
}).slice(0, 2)).sort(byRank);
}
function usable(n, q) {
return !!n?.eligible && n.kind !== 'file' && n.kind !== 'class'
&& (inDomain(n, q) || n.domain === 'production' || n.domain === 'unknown');
}
function path(c, from, to, q, backwards = false, maximumDepth = 16) {
if (from === to)
return [from];
const previous = new Map();
const depth = new Map([[from, 0]]);
const queue = [from];
for (let cursor = 0; cursor < queue.length && queue.length < 512; cursor += 1) {
const id = queue[cursor];
const distance = depth.get(id) ?? 0;
if (distance >= maximumDepth)
continue;
const adjacent = backwards
? c.byId.get(id)?.ins ?? []
: c.byId.get(id)?.outs ?? [];
for (const next of adjacent) {
if (depth.has(next) || !usable(c.byId.get(next), q))
continue;
depth.set(next, distance + 1);
previous.set(next, id);
if (next === to) {
const result = [to];
while (result.at(-1) !== from)
result.push(previous.get(result.at(-1)));
return result.reverse();
}
queue.push(next);
}
}
return null;
}
function toposort(c, input) {
const allowed = new Set(input);
const index = new Map();
const low = new Map();
const stack = [];
const active = new Set();
const groups = [];
let ordinal = 0;
const visit = (id) => {
index.set(id, ordinal);
low.set(id, ordinal++);
stack.push(id);
active.add(id);
for (const next of c.byId.get(id)?.outs ?? []) {
if (!allowed.has(next))
continue;
if (!index.has(next)) {
visit(next);
low.set(id, Math.min(low.get(id), low.get(next)));
}
else if (active.has(next)) {
low.set(id, Math.min(low.get(id), index.get(next)));
}
}
if (low.get(id) !== index.get(id))
return;
const group = [];
while (stack.length > 0) {
const member = stack.pop();
active.delete(member);
group.push(member);
if (member === id)
break;
}
groups.push(group);
};
for (const id of input)
if (!index.has(id))
visit(id);
const groupOf = new Map(groups.flatMap((group, groupIndex) => group.map((id) => [id, groupIndex])));
const outs = new Map();
const indegree = new Map();
for (const id of input) {
const from = groupOf.get(id);
for (const next of c.byId.get(id)?.outs ?? []) {
if (!allowed.has(next))
continue;
const to = groupOf.get(next);
if (from === to)
continue;
const targets = outs.get(from) ?? new Set();
if (targets.has(to))
continue;
targets.add(to);
outs.set(from, targets);
indegree.set(to, (indegree.get(to) ?? 0) + 1);
}
}
const pos = new Map(input.map((id, order) => [id, order]));
const ready = groups.map((_, group) => group)
.filter((group) => !indegree.has(group));
const depths = new Map(ready.map((group) => [
group, Math.max(0, groups[group].length - 1),
]));
const ids = [];
let depth = 0;
while (ready.length > 0) {
ready.sort((a, b) => Math.min(...groups[a].map((id) => pos.get(id) ?? LAST))
- Math.min(...groups[b].map((id) => pos.get(id) ?? LAST)));
const group = ready.shift();
const level = depths.get(group) ?? 0;
depth = Math.max(depth, level);
ids.push(...groups[group].sort((a, b) => (pos.get(a) ?? LAST)
- (pos.get(b) ?? LAST)
|| compare(a, b)));
for (const next of outs.get(group) ?? []) {
depths.set(next, Math.max(depths.get(next) ?? 0, level + 1 + Math.max(0, groups[next].length - 1)));
const left = (indegree.get(next) ?? 0) - 1;
if (left > 0)
indegree.set(next, left);
else {
indegree.delete(next);
ready.push(next);
}
}
}
return { ids: ids.length === input.length ? ids : [...input], depth };
}
function rootPath(c, target, seedMap, forbidden, q) {
const next = new Map();
const depth = new Map([[target, 0]]);
const queue = [target];
const roots = [];
for (let cursor = 0; cursor < queue.length && queue.length < 512; cursor += 1) {
const id = queue[cursor];
const n = c.byId.get(id);
const distance = depth.get(id) ?? 0;
if (!usable(n, q) || distance >= 16)
continue;
const parents = n.ins.filter((parent) => usable(c.byId.get(parent), q));
if (id !== target && parents.length === 0)
roots.push(id);
for (const parent of parents) {
if (depth.has(parent))
continue;
depth.set(parent, distance + 1);
next.set(parent, id);
queue.push(parent);
}
}
const route = (root) => {
const result = [root];
while (result.at(-1) !== target)
result.push(next.get(result.at(-1)));
return result;
};
const choices = roots.flatMap((root) => {
const ids = route(root);
if (ids.slice(0, -1).some((id) => forbidden.has(id)))
return [];
return [{
ids,
parts: new Set(ids.flatMap((id) => seedMap.get(id)?.parts ?? [])).size,
terms: new Set(ids.flatMap((id) => seedMap.get(id)?.hits ?? [])).size,
}];
});
return choices.sort((a, b) => a.ids.length - b.ids.length
|| b.parts - a.parts
|| b.terms - a.terms
|| (seedMap.get(b.ids[0])?.rank.score ?? 0)
- (seedMap.get(a.ids[0])?.rank.score ?? 0)
|| compare(a.ids[0], b.ids[0]))[0]?.ids ?? null;
}
function connect(c, seeds, q) {
const seedMap = new Map(seeds.map((seed) => [seed.n.id, seed]));
const byId = c.byId;
const hubs = c.nodes.filter((hub) => usable(hub, q) && hub.ins.length > 0 && hub.outs.length > 0);
const shapes = new Map();
for (const file of new Set(hubs.map((hub) => hub.file))) {
shapes.set(file, (c.files.get(file) ?? []).flatMap((registry) => {
if (!usable(registry, q) || registry.ins.length < 2)
return [];
const pairs = registry.ins.flatMap((hookId) => {
const registrar = byId.get(hookId);
if (!usable(registrar, q) || !registrar.owner)
return [];
const workers = (c.members.get(registrar.owner) ?? [])
.filter((id) => id !== hookId && registrar.outs.includes(id)
&& usable(byId.get(id), q));
return workers.length === 1
? [{ hookId, workerId: workers[0] }] : [];
});
const hooks = [...new Set(pairs.map((pair) => pair.hookId))];
const workers = [...new Set(pairs.map((pair) => pair.workerId))];
const role = (id) => {
const n = byId.get(id);
return `${n.kind}\0${text(n.attributes, 'label').replace(/^\./u, '')}`;
};
return hooks.length >= 2 && hooks.length === workers.length
&& new Set(hooks.map(role)).size === 1
&& new Set(workers.map(role)).size === 1
&& role(hooks[0]) !== role(workers[0])
? [{ registryId: registry.id, hooks, workers }] : [];
}));
}
const choices = hubs.flatMap((hub) => (shapes.get(hub.file) ?? [])
.filter(({ registryId }) => registryId !== hub.id)
.flatMap(({ hooks, workers }) => {
const hits = workers.filter((id) => hub.outs.includes(id)).length;
if (hits * 2 >= workers.length)
return [];
const entry = rootPath(c, hub.id, seedMap, new Set([...hooks, ...workers]), q);
if (!entry)
return [];
const relevant = [...entry, ...workers];
const scopes = q.scopes.filter((s) => !s.hard);
const matchesQuery = scopes.length > 0
? scopes.every((s) => c.nodes.some((n) => usable(n, q) && inScope(n, s)
&& relevant.some((id) => !!path(c, id, n.id, q, false, 3))))
: relevant.some((id) => seedMap.has(id));
return matchesQuery ? [{ hub, workers, entry }] : [];
}))
.sort((a, b) => Number(seedMap.has(b.hub.id)) - Number(seedMap.has(a.hub.id))
|| b.hub.ins.length - a.hub.ins.length
|| b.workers.length - a.workers.length
|| compare(a.hub.id, b.hub.id));
const pick = choices[0];
if (!pick)
return null;
const branches = pick.workers.map((workerId) => {
const worker = byId.get(workerId);
const services = worker.outs.filter((id) => usable(byId.get(id), q));
const service = [...services].sort((a, b) => Number(!!path(c, b, pick.hub.id, q))
- Number(!!path(c, a, pick.hub.id, q))
|| Number(byId.get(b)?.file !== worker.file)
- Number(byId.get(a)?.file !== worker.file)
|| (seedMap.get(b)?.rank.score ?? 0)
- (seedMap.get(a)?.rank.score ?? 0)
|| compare(a, b))[0];
return {
workerId, service,
hits: pick.hub.outs.includes(workerId),
returns: !!service && !!path(c, service, pick.hub.id, q),
};
}).sort((a, b) => Number(b.hits) - Number(a.hits)
|| Number(b.returns) - Number(a.returns)
|| (byId.get(a.service ?? '')?.outs.length
?? LAST)
- (byId.get(b.service ?? '')?.outs.length
?? LAST)
|| (seedMap.get(b.workerId)?.rank.score ?? 0)
- (seedMap.get(a.workerId)?.rank.score ?? 0)
|| compare(a.workerId, b.workerId));
const ids = [];
const files = new Set();
const append = (id) => {
if (!id || ids.includes(id))
return true;
const n = byId.get(id);
if (!usable(n, q))
return true;
if (ids.length >= SNIPPET_CAP
|| (n.file && !files.has(n.file) && files.size >= FILE_CAP))
return false;
ids.push(id);
if (n.file)
files.add(n.file);
return true;
};
let complete = true;
for (const id of pick.entry)
if (!append(id))
complete = false;
const terminal = branches.filter((branch) => !branch.returns);
for (const branch of branches) {
if (branch.returns
|| (!!branch.service
&& (termsOf(byId.get(branch.service), q, true).length > 0
|| (q.parts.length > 1 && terminal.length === 1
&& q.scopes.every((s) => s.hard))))) {
if (!append(branch.workerId))
complete = false;
}
if (branch.returns && !append(branch.service))
complete = false;
}
const sideBranches = [];
for (const s of q.scopes.filter((s) => !s.hard)) {
if (ids.some((id) => inScope(byId.get(id), s)))
continue;
const side = c.nodes.filter((n) => usable(n, q) && !ids.includes(n.id) && inScope(n, s)
&& n.ins.some((id) => ids.includes(id)))
.sort((a, b) => (seedMap.get(b.id)?.rank.score ?? 0)
- (seedMap.get(a.id)?.rank.score ?? 0)
|| compare(a.id, b.id))[0];
if (!side) {
complete = false;
continue;
}
const parent = Math.max(...side.ins.map((id) => ids.indexOf(id)));
const length = ids.length;
if (parent < 0 || !append(side.id) || ids.length === length) {
complete = false;
continue;
}
ids.pop();
ids.splice(parent + 1, 0, side.id);
sideBranches.push(side.id);
}
if (!q.expand && !q.terms.every((t) => t === 'flow'
|| ids.some((id) => termsOf(byId.get(id), q, true).includes(t))))
return null;
return {
ids, flow: true,
complete: complete && ids.length > 1,
structuralRequired: true,
branch: sideBranches,
};
}
function causal(c, seeds, q) {
if (seeds.length === 0)
return null;
const named = q.scopes.filter((s) => !s.hard);
const scoped = new Set(named.flatMap((s) => s.tokens));
const picked = (named.length > 0
? seeds.filter((seed) => named.some((s) => inScope(seed.n, s))
|| seed.hits.some((t) => !scoped.has(t)))
: seeds).slice(0, 32);
const ids = new Set(picked.map((seed) => seed.n.id));
for (const n of c.nodes) {
if (!usable(n, q))
continue;
const children = n.outs.filter((id) => ids.has(id)).length;
if (children < 2)
continue;
ids.add(n.id);
if (ids.size >= 256)
break;
}
const ordered = [...new Set(q.scopes.flatMap((s) => picked.filter((seed) => inScope(seed.n, s))))];
const filtered = [...new Set([
...ordered.map(({ n }) => n.id), ...ids,
])].filter((id) => usable(c.byId.get(id), q));
const files = new Set();
const bounded = toposort(c, filtered).ids.filter((id) => {
const n = c.byId.get(id);
if (files.size >= FILE_CAP && n.file && !files.has(n.file))
return false;
if (files.size < FILE_CAP && n.file)
files.add(n.file);
return true;
}).slice(0, SNIPPET_CAP);
const retained = new Set(bounded);
const edges = bounded.reduce((count, id) => count + c.byId.get(id).outs.filter((to) => retained.has(to)).length, 0);
const depth = toposort(c, bounded).depth;
const reachable = ordered.some((from, index) => ordered.slice(index + 1).some((to) => !!path(c, from.n.id, to.n.id, q)));
if (edges === 0 && (!(q.scopes.length > 1
|| q.terms.filter((t) => /^\d+$/.test(t)).length > 1)
|| (q.limits.length === 0 && !reachable)))
return {
ids: [], flow: false, complete: false,
structuralRequired: true,
};
if (depth < 2 && edges >= 3 && files.size <= 1)
return {
ids: [], flow: false, complete: false,
structuralRequired: true,
};
const conceptCoverage = new Set(picked
.filter((seed) => retained.has(seed.n.id))
.flatMap((seed) => seed.parts));
return {
ids: bounded,
flow: bounded.length > 1,
complete: q.parts.every((concept, index) => concept.size === 0 || conceptCoverage.has(index)),
structuralRequired: true,
};
}
function selectStructure(c, scored, q) {
if (!q.structural || q.terms[0] === 'where'
|| (q.scopes.some((s) => !s.hard) && !q.expand)
|| q.limits.some((s) => s.subject.includes('/')))
return null;
if (!q.expand) {
const matches = scored.map(({ n }) => [n, termsOf(n, q)]);
const coverable = new Set(matches.flatMap(([, terms]) => terms));
if (matches.some(([n, found]) => found.length * 5 >= coverable.size * 3
&& found.some((t) => matches.filter(([, terms]) => terms.includes(t)).length <= 2)
&& !n.ins.concat(n.outs).some((id) => {
const adjacent = c.byId.get(id);
return !!adjacent && termsOf(adjacent, q)
.some((t) => coverable.has(t) && !found.includes(t));
})))
return null;
}
const seeds = scored.flatMap((item) => {
if (!usable(item.n, q))
return [];
const hits = termsOf(item.n, q);
if (hits.length === 0)
return [];
return [{
...item, hits,
parts: q.parts.flatMap((concept, index) => hits.some((t) => concept.has(t)) ? [index] : []),
}];
}).sort((a, b) => Number(b.n.ins.length + b.n.outs.length > 0)
- Number(a.n.ins.length + a.n.outs.length > 0)
|| b.hits.length - a.hits.length
|| byRank(a, b));
return connect(c, seeds, q)
?? causal(c, seeds, q);
}
function fallback(index, c, scored, q) {
const ids = [];
const files = new Set();
const covered = new Set();
const add = (item) => {
if (ids.includes(item.n.id))
return;
if (item.n.file && !files.has(item.n.file)
&& files.size >= FILE_CAP)
return;
ids.push(item.n.id);
if (item.n.file)
files.add(item.n.file);
for (const t of item.rank.matchedTerms)
covered.add(t);
};
if (q.limits.some((s) => s.subject.includes('/'))) {
for (const s of q.limits.filter((item) => item.subject.includes('/'))) {
const matching = scored.filter((item) => inScope(item.n, s));
const file = matching.find((item) => item.n.kind === 'file');
const symbol = matching.find((item) => item.n.kind !== 'file'
&& (!file || index.graph.edgesBetween(file.n.id, item.n.id)
.some(({ attributes }) => text(attributes, 'relation') === 'contains')));
if (file)
add(file);
if (symbol)
add(symbol);
}
}
else if (q.parts.length > 1) {
for (const concept of q.parts) {
const next = scored.filter((item) => item.rank.matchedTerms.some((t) => concept.has(t)))
.sort((a, b) => Number(!files.has(b.n.file)) - Number(!files.has(a.n.file))
|| byRank(a, b))[0];
if (next)
add(next);
}
}
else {
const loc = q.terms[0] === 'where';
const pos = (item) => termsOf(item.n, q, true).map((t) => q.pos.get(t) ?? LAST);
const start = loc ? [...scored].sort((a, b) => b.rank.matchedTerms.length - a.rank.matchedTerms.length
|| Math.min(LAST, ...pos(a)) - Math.min(LAST, ...pos(b))
|| pos(b).length - pos(a).length
|| byRank(a, b))[0] : scored[0];
const exact = start && start.n.kind !== 'class' && start.n.kind !== 'file'
&& exactLabel(start.n, q);
const first = start && (exact ? start : scored.find((item) => item.n.file === start.n.file
&& start.n.outs.includes(item.n.id)
&& start.rank.matchedTerms.every((t) => item.rank.matchedTerms.includes(t))
&& termsOf(item.n, q, true).length
>= termsOf(start.n, q, true).length) ?? start);
if (first)
add(first);
while (ids.length < (exact && loc ? 1 : loc ? 2 : SNIPPET_CAP)) {
const next = scored.filter((item) => !ids.includes(item.n.id)
&& (files.has(item.n.file) || files.size < FILE_CAP))
.sort((a, b) => {
const link = (item) => Number(ids.some((id) => index.graph.edgesBetween(id, item.n.id).some(({ attributes }) => CAUSAL.has(text(attributes, 'relation')))));
const novelty = (item) => item.rank.matchedTerms.filter((t) => !covered.has(t)).length;
return (loc
? link(b) - link(a)
|| Math.max(-1, ...pos(b)) - Math.max(-1, ...pos(a))
|| pos(b).length - pos(a).length
|| novelty(b) - novelty(a)
: novelty(b) - novelty(a) || link(b) - link(a))
|| a.rank.firstMatch - b.rank.firstMatch
|| byRank(a, b);
})[0];
if (!next)
break;
const novel = next.rank.matchedTerms.some((t) => !covered.has(t));
const connected = termsOf(next.n, q).length > 0 && ids.some((id) => index.graph.edgesBetween(id, next.n.id).some(({ attributes }) => CAUSAL.has(text(attributes, 'relation'))));
if (loc ? !connected : !novel && !connected)
break;
add(next);
}
}
const ordered = toposort(c, ids).ids;
return {
ids: ordered, flow: false, complete: true, structuralRequired: false,
};
}
function unsupportedCandidates(index, q) {
return index.unsupported_sources.flatMap((source) => {
const extension = source.path.toLowerCase().match(/\.([a-z0-9]+)$/)?.[1] ?? '';
const domain = classifySourceDomain(source.path, index.root_path);
if (!UNSUPPORTED.test(extension)
|| isPollutedSourcePath(source.path, index.root_path)
|| (domain !== 'production' && domain !== 'unknown'))
return [];
const pathTokens = new Set(words(source.path));
const basename = new Set(words(source.path.split('/').at(-1) ?? source.path));
const matched = q.terms.filter((t) => !t.includes('_') && !t.includes('-') && pathTokens.has(t));
const weights = new Map(matched.map((t) => [t, basename.has(t) ? 4 : 1]));
const s = q.scopes.some((scope) => scope.tokens.every((t) => pathTokens.has(t))
&& tokens(source.path).join('').includes(scope.compact));
if (!s && (matched.length === 0
|| matched.every((t) => t.length < 4)))
return [];
return [{
path: source.path, terms: matched, weights,
first: matched.reduce((first, t) => Math.min(first, q.pos.get(t) ?? LAST), LAST),
score: matched.reduce((total, t) => total + t.length ** 2 * (weights.get(t) ?? 1) * 100, s ? 1_000_000 : 0),
}];
}).sort((a, b) => b.score - a.score || a.first - b.first
|| compare(a.path, b.path));
}
function unsupportedBoundaries(choices) {
const picked = [];
const covered = new Set();
const rest = [...choices];
while (rest.length > 0 && picked.length < 4) {
rest.sort((a, b) => {
const novelty = (item) => item.terms.filter((t) => !covered.has(t))
.reduce((total, t) => total + t.length ** 2 * (item.weights.get(t) ?? 1), 0);
return novelty(b) - novelty(a)
|| b.score - a.score || compare(a.path, b.path);
});
const next = rest.shift();
if (picked.length > 0
&& next.terms.every((t) => covered.has(t))
&& next.score * 3 < picked[0].score)
break;
picked.push(next);
for (const t of next.terms)
covered.add(t);
}
const boundaries = picked
.map((item) => ({
kind: 'unsupported', subject: item.path,
}))
.sort((a, b) => compare(a.subject, b.subject));
return picked.length >= 4 && rest.length > 0
? [...boundaries, { kind: 'truncated', subject: 'unsupported sources' }]
: boundaries;
}
export function rankQueryAnchors(index, request) {
const c = buildCorpus(index);
const q = vocabulary(request.question);
const active = (s) => !s.hard
|| s.subject.includes('/') || c.nodes.some((n) => {
const prefix = s.tokens.filter((t) => !/^\d+$/.test(t));
return prefix.every((t) => n.tokens.has(t))
&& [...n.tokens].some((t) => /^\d+$/.test(t));
});
for (const s of q.scopes.filter((s) => !active(s))) {
const outside = new Set(tokens(request.question.replaceAll(s.subject, '')));
q.terms = q.terms.filter((t) => !/^\d+$/.test(t) || !s.tokens.includes(t) || outside.has(t));
}
q.scopes = q.scopes.filter(active);
q.limits = q.scopes.filter((s) => s.hard);
const outside = unsupportedCandidates(index, q);
const unsupportedFacts = unsupportedBoundaries(outside);
const missing = q.scopes.flatMap((s) => {
const graphMatches = c.nodes.filter((n) => inScope(n, s));
if (graphMatches.some((n) => n.eligible)
|| outside.some((item) => tokens(item.path).join('').includes(s.compact)))
return [];
return [{
kind: graphMatches.length > 0 ? 'unavailable' : 'missing',
subject: s.subject,
}];
});
const found = q.scopes.filter((s) => c.nodes.some((n) => n.eligible && inScope(n, s)));
const limits = q.limits.filter((s) => found.includes(s));
const scoped = new Set(found.flatMap((s) => s.tokens));
const allScopedTerms = new Set(q.scopes.flatMap((s) => s.tokens));
const unscopedTerms = new Set(q.terms.filter((t) => !allScopedTerms.has(t)));
const outsideTerms = new Set(q.terms.filter((t) => !scoped.has(t)));
const has = (n, terms) => [...terms].some((t) => matches(n.tokens, t)
|| n.ins.concat(n.outs).some((id) => {
const adjacent = c.byId.get(id);
return !!adjacent?.eligible && matches(adjacent.tokens, t);
}));
const keep = (n) => q.limits.length > 0
? limits.some((s) => inScope(n, s))
: q.scopes.length === 0
|| (found.length === 0
? has(n, unscopedTerms)
: found.some((s) => inScope(n, s))
|| has(n, outsideTerms));
const pool = scoredNodes(c, q, keep);
const structural = selectStructure(c, pool, q);
const choice = structural ?? fallback(index, c, pool, q);
const anchors = choice.ids.flatMap((id, ordinal) => {
const existing = pool.find((item) => item.n.id === id)?.rank;
if (existing)
return [existing];
const n = c.byId.get(id);
if (!n?.eligible)
return [];
const matchedTerms = termsOf(n, q);
return [{
id, attributes: n.attributes,
score: Math.max(0, (pool[0]?.rank.score ?? 0) - ordinal),
matchedTerms,
firstMatch: matchedTerms.reduce((first, t) => Math.min(first, q.pos.get(t) ?? LAST), LAST),
}];
});
const picked = new Set(anchors.map((anchor) => anchor.id));
const selectedFiles = new Set(anchors.map((anchor) => text(anchor.attributes, 'source_file')));
const truncated = pool.some(({ n }) => !picked.has(n.id))
&& (anchors.length >= SNIPPET_CAP
|| selectedFiles.size >= FILE_CAP)
? [{ kind: 'truncated', subject: 'query anchors' }]
: [];
const boundaries = anchors.length === 0
&& unsupportedFacts.length === 0 && missing.length === 0
? [{ kind: 'missing', subject: request.question }]
: [...unsupportedFacts, ...missing, ...truncated];
return {
anchors, boundaries,
queryTerms: q.terms, flow: choice.flow, branch: choice.branch ?? [],
sequential: q.sequential,
priorityAnchorIds: choice.ids,
structuralRequired: choice.structuralRequired,
structuralCoverageComplete: choice.complete
&& (!choice.structuralRequired || missing.length === 0),
};
}
import { type EvidenceBoundary, type EvidenceNode, type EvidenceRelationship, type NormalizedRetrieveRequest, type RetrieveContextResult, type RetrieveOutcome } from './types.js';
export interface SliceEvidenceInput {
request: NormalizedRetrieveRequest;
outcome: RetrieveOutcome;
matchedNodes: readonly EvidenceNode[];
relationships: readonly EvidenceRelationship[];
boundaries: readonly EvidenceBoundary[];
priorityNodeIds: readonly string[];
closurePasses: 0 | 1;
structuralRequired?: boolean;
structuralCoverageComplete?: boolean;
}
export declare function sliceEvidence(input: SliceEvidenceInput): RetrieveContextResult;
import { countTokens } from 'gpt-tokenizer/encoding/cl100k_base';
import { canonicalJsonString as json, compareCodeUnits as compare, } from '../graph/canonical-json.js';
import { MAX_RETRIEVE_FILES, MAX_RETRIEVE_SNIPPETS, RETRIEVE_RESULT_SCHEMA, RETRIEVE_RESULT_VERSION, } from './types.js';
const CAUSAL_RELATIONS = new Set(['calls', 'enqueues_job']);
function causal(edge) {
return CAUSAL_RELATIONS.has(edge.relation);
}
function truncation(target) {
if (!target)
return { kind: 'truncated', subject: 'retrieve', detail: 'Omitted by limit.' };
return {
kind: 'truncated',
subject: target.evidence_kind === 'symbol_declaration'
? `${target.source_file}:${target.source_location}`
: target.source_file,
};
}
function edgeOrder(left, right) {
return compare(left.from_id, right.from_id)
|| compare(left.relation, right.relation)
|| compare(left.to_id, right.to_id)
|| compare(left.id, right.id);
}
function edgeSlot(edges, edge) {
let low = 0;
let high = edges.length;
while (low < high) {
const middle = (low + high) >>> 1;
if (edgeOrder(edges[middle], edge) < 0)
low = middle + 1;
else
high = middle;
}
const before = [edges[low - 1], edges[low]]
.filter((value) => Boolean(value));
const after = [edges[low - 1], edge, edges[low]]
.filter((value) => Boolean(value));
return {
at: low,
delta: countTokens(json(after)) - countTokens(json(before)),
};
}
function addEdgeTokens(current, delta) {
const body = current - countTokens(String(current)) + delta;
let tokens = body;
for (let pass = 0; pass < 16; pass += 1) {
const observed = body + countTokens(String(tokens));
if (observed === tokens)
return tokens;
tokens = observed;
}
throw new Error('Unable to stabilize retrieve serialized token count');
}
function factOrder(left, right) {
return compare(left.kind, right.kind)
|| compare(left.subject, right.subject)
|| compare(left.detail ?? '', right.detail ?? '');
}
function unique(values, identityOf, name) {
const facts = new Map();
for (const value of values) {
const identity = identityOf(value);
const serialized = json(value);
const previous = facts.get(identity);
if (previous && previous.serialized !== serialized) {
throw new TypeError(`Conflicting ${name} facts share identity ${JSON.stringify(identity)}`);
}
if (!previous)
facts.set(identity, { serialized, value });
}
return [...facts.values()].map(({ value }) => value);
}
function uniqueFacts(facts) {
return unique(facts, json, 'boundary').sort(factOrder);
}
function handoffEnds(fact) {
if (fact.kind !== 'disconnected')
return null;
const separator = ' -> ';
const at = fact.subject.indexOf(separator);
if (at <= 0 || fact.subject.indexOf(separator, at + separator.length) >= 0)
return null;
return [fact.subject.slice(0, at), fact.subject.slice(at + separator.length)];
}
function pruneFiles(nodes, relationships) {
const related = new Set(relationships.flatMap(({ from_id, to_id }) => [from_id, to_id]));
return nodes.filter((node) => node.evidence_kind !== 'structural_file' || related.has(node.node_id));
}
function finalize(input, nodes, edges, facts) {
const sortedEdges = [...edges].sort(edgeOrder);
const kept = pruneFiles(nodes, sortedEdges);
const ids = new Set(kept.map(({ node_id }) => node_id));
const handoff = facts.some((fact) => {
const ends = handoffEnds(fact);
return ends !== null && ends.every((id) => ids.has(id));
});
const hasEdge = sortedEdges.some(causal);
const ready = !input.structuralRequired
|| (input.structuralCoverageComplete !== false
&& (hasEdge || handoff));
const missing = input.outcome === 'evidence' && !ready;
const outputFacts = uniqueFacts([
...facts,
...missing ? [{
kind: 'missing',
subject: 'structural coverage',
}] : [],
]);
const files = new Set([
...kept.map(({ source_file }) => source_file),
...sortedEdges.flatMap(({ source_file }) => source_file ? [source_file] : []),
]).size;
const snippets = kept.filter(({ snippet }) => Boolean(snippet)).length;
const result = (tokenCount) => ({
schema: RETRIEVE_RESULT_SCHEMA,
version: RETRIEVE_RESULT_VERSION,
outcome: missing
|| (kept.length === 0 && input.outcome === 'evidence')
? 'missing'
: input.outcome,
matched_nodes: kept,
relationships: sortedEdges,
boundaries: outputFacts,
metrics: {
selected_files: files,
snippets,
closure_passes: input.closurePasses,
serialized_tokens: tokenCount,
truncated: outputFacts.some(({ kind }) => kind === 'truncated'),
},
});
let tokens = 0;
for (let pass = 0; pass < 16; pass += 1) {
const value = result(tokens);
const seen = countTokens(json(value));
if (seen === tokens)
return value;
tokens = seen;
}
for (tokens = 0; tokens <= 10_000; tokens += 1) {
const value = result(tokens);
if (countTokens(json(value)) === tokens)
return value;
}
throw new Error('Unable to stabilize retrieve serialized token count');
}
function pack(input, nodes, edges, facts, budget) {
const byId = new Map(nodes.map((node) => [node.node_id, node]));
const priorityIds = [...new Set(input.priorityNodeIds)];
const prioritySet = new Set(priorityIds);
const ordered = [
...priorityIds.flatMap((id) => {
const node = byId.get(id);
return node ? [node] : [];
}),
...nodes.filter(({ node_id }) => !prioritySet.has(node_id)),
];
const ordinals = new Map(priorityIds.map((id, index) => [id, index]));
const priority = (ids) => {
const ranks = ids.map((id) => ordinals.get(id) ?? Number.POSITIVE_INFINITY);
return [Math.max(...ranks), Math.min(...ranks)];
};
const queue = [];
const loose = [];
let omitted = false;
for (const edge of edges) {
const ids = [...new Set([edge.from_id, edge.to_id])];
const ends = ids.map((id) => byId.get(id));
if (ends.some((node) => !node)) {
omitted = true;
continue;
}
queue.push({
nodes: ends,
edge,
rank: priority(ids),
order: causal(edge) ? 0 : 2,
key: json(edge),
});
}
for (const fact of facts) {
if (fact.kind !== 'disconnected') {
if (budget === undefined || fact.kind !== 'truncated') {
loose.push(fact);
}
continue;
}
const ids = handoffEnds(fact);
const ends = ids?.map((id) => byId.get(id));
if (!ids || !ends || ends.some((node) => !node)) {
omitted = true;
continue;
}
queue.push({
nodes: ends,
fact,
rank: priority(ids),
order: 1,
key: json(fact),
});
}
const rank = (left, right) => left === right ? 0 : left < right ? -1 : 1;
queue.sort((left, right) => Number(Number.isFinite(right.rank[0]))
- Number(Number.isFinite(left.rank[0]))
|| left.order - right.order
|| rank(left.rank[0], right.rank[0])
|| rank(left.rank[1], right.rank[1])
|| compare(left.key, right.key));
const chosen = new Set();
const keptEdges = [];
let keptFacts = budget === undefined ? [] : [truncation()];
const files = new Set();
const blocked = new Set();
let snippets = 0;
let tokenCount;
const selectedNodes = (ids = chosen) => ordered.filter(({ node_id }) => ids.has(node_id));
const tryAdd = (item) => {
const missing = item.nodes.filter(({ node_id }) => !chosen.has(node_id));
const addedFiles = new Set(missing.map(({ source_file }) => source_file).filter((file) => !files.has(file)));
const edgeFile = item.edge?.source_file;
if (edgeFile && !files.has(edgeFile))
addedFiles.add(edgeFile);
const addedSnippets = missing.filter(({ snippet }) => Boolean(snippet)).length;
if (files.size + addedFiles.size > MAX_RETRIEVE_FILES
|| snippets + addedSnippets > MAX_RETRIEVE_SNIPPETS)
return false;
const candidateIds = new Set(chosen);
for (const { node_id } of missing)
candidateIds.add(node_id);
const candidateEdges = item.edge
? [...keptEdges, item.edge]
: keptEdges;
const candidateFacts = item.fact
? uniqueFacts([...keptFacts, item.fact])
: keptFacts;
let insertion;
let nextTokens;
const edge = item.edge;
const stableStructure = !input.structuralRequired
|| !edge || !causal(edge)
|| keptEdges.some(causal);
if (budget !== undefined) {
if (tokenCount !== undefined && edge && !item.fact
&& missing.length === 0 && addedFiles.size === 0 && stableStructure) {
insertion = edgeSlot(keptEdges, edge);
nextTokens = addEdgeTokens(tokenCount, insertion.delta);
}
else {
nextTokens = finalize(input, selectedNodes(candidateIds), candidateEdges, candidateFacts).metrics.serialized_tokens;
}
if (nextTokens > budget)
return false;
}
for (const node of missing)
chosen.add(node.node_id);
for (const file of addedFiles)
files.add(file);
snippets += addedSnippets;
if (edge) {
const at = insertion?.at
?? edgeSlot(keptEdges, edge).at;
keptEdges.splice(at, 0, edge);
}
if (item.fact)
keptFacts = candidateFacts;
tokenCount = nextTokens;
return true;
};
for (const item of queue) {
if (tryAdd(item))
continue;
omitted = true;
if (item.fact) {
for (const { node_id } of item.nodes) {
if (!chosen.has(node_id))
blocked.add(node_id);
}
}
}
for (const node of ordered) {
if (chosen.has(node.node_id) || blocked.has(node.node_id))
continue;
if (node.evidence_kind === 'structural_file' || !tryAdd({ nodes: [node] })) {
omitted = true;
}
}
for (const fact of loose.sort(factOrder)) {
if (!tryAdd({ nodes: [], fact }))
omitted = true;
}
return {
nodes: selectedNodes(),
relationships: keptEdges,
boundaries: keptFacts,
omitted,
};
}
export function sliceEvidence(input) {
const nodes = unique(input.matchedNodes, ({ node_id }) => node_id, 'node');
const relationships = unique(input.relationships, ({ id }) => id, 'relationship').sort(edgeOrder);
const boundaries = uniqueFacts(input.boundaries);
const capped = pack(input, nodes, relationships, boundaries);
if (capped.omitted && !capped.boundaries.some(({ kind }) => kind === 'truncated')) {
capped.boundaries = uniqueFacts([...capped.boundaries, truncation()]);
}
const cappedResult = finalize(input, capped.nodes, capped.relationships, capped.boundaries);
if (cappedResult.metrics.serialized_tokens <= input.request.budget)
return cappedResult;
const retained = pack(input, capped.nodes, capped.relationships, capped.boundaries, input.request.budget);
const omittedTarget = capped.nodes.find(({ node_id }) => !retained.nodes.some((node) => node.node_id === node_id));
if (omittedTarget) {
const targeted = retained.boundaries.map((boundary) => boundary.kind === 'truncated' ? truncation(omittedTarget) : boundary);
if (finalize(input, retained.nodes, retained.relationships, targeted).metrics.serialized_tokens <= input.request.budget)
retained.boundaries = targeted;
}
return finalize(input, retained.nodes, retained.relationships, retained.boundaries);
}
import type { ReadyQueryIndex } from './index-status.js';
import type { QuerySlice, RankQueryResult } from './types.js';
export declare function traverseEvidencePaths(index: ReadyQueryIndex, ranking: RankQueryResult): QuerySlice;
import { compareCodeUnits as compare } from '../graph/canonical-json.js';
import { sourceDomainOf as domainOf } from './source-domain.js';
function relationWords(value) {
const normalized = value.toLowerCase();
return [normalized, ...normalized.split(/[^a-z0-9]+/u)]
.filter((term, index, terms) => term.length > 0 && terms.indexOf(term) === index);
}
function mentions(relation, queryTerms) {
const terms = relationWords(relation);
return queryTerms.has(terms[0])
|| (terms.length > 1 && terms.slice(1).every((term) => queryTerms.has(term)));
}
function pathEdge(edge) {
const relation = edge.attributes.relation;
if (typeof relation !== 'string' || relation.length === 0) {
throw new Error(`Graph edge ${edge.id} has no relation`);
}
return { id: edge.id, from: edge.source, to: edge.target, relation, attributes: edge.attributes };
}
function allowed(graph, edge) {
const from = graph.nodeAttributes(edge.from);
const to = graph.nodeAttributes(edge.to);
const fromFile = from.node_kind === 'file';
const toFile = to.node_kind === 'file';
if ((!fromFile && (!from.definition_range || !from.declaration_range))
|| (!toFile && (!to.definition_range || !to.declaration_range)))
return false;
if (edge.relation === 'contains') {
return fromFile && !toFile && from.source_file === to.source_file;
}
if (fromFile || toFile)
return edge.relation === 'imports_from' && fromFile && toFile;
return edge.relation === 'calls' || edge.relation === 'enqueues_job';
}
function outgoing(graph, nodeId, terms) {
return graph.successors(nodeId)
.flatMap((targetId) => graph.edgesBetween(nodeId, targetId))
.map(pathEdge)
.filter((edge) => allowed(graph, edge))
.sort((left, right) => {
const leftMentioned = mentions(left.relation, terms);
const rightMentioned = mentions(right.relation, terms);
if (leftMentioned !== rightMentioned)
return leftMentioned ? -1 : 1;
const line = (edge) => Number(String(edge.attributes.source_location ?? '').match(/\d+/)?.[0] ?? Number.MAX_SAFE_INTEGER);
return line(left) - line(right)
|| compare(left.to, right.to)
|| compare(left.relation, right.relation)
|| compare(left.id, right.id);
});
}
function direct(graph, from, to) {
return graph.edgesBetween(from, to)
.map(pathEdge)
.some((edge) => allowed(graph, edge));
}
function rebuild(sourceId, targetId, parents) {
const reversed = [];
let currentId = targetId;
while (currentId !== sourceId) {
const predecessor = parents.get(currentId);
if (!predecessor) {
throw new Error(`Traversal predecessor missing for ${sourceId} -> ${targetId}`);
}
reversed.push(predecessor.edge);
currentId = predecessor.nodeId;
}
return reversed.reverse();
}
function unique(boundaries) {
const seen = new Set();
return boundaries.filter((boundary) => {
const key = `${boundary.kind}\u0000${boundary.subject}\u0000${boundary.detail ?? ''}`;
if (seen.has(key))
return false;
seen.add(key);
return true;
});
}
function verify(graph, nodeId) {
const attributes = graph.nodeAttributes(nodeId);
return [attributes.source_file, attributes.source_location]
.filter((value) => typeof value === 'string' && value.length > 0)
.join(':') || nodeId;
}
function valid(graph, ranking, boundaries) {
const seen = new Set();
return ranking.anchors.filter((anchor) => {
if (!graph.hasNode(anchor.id)) {
boundaries.push({
kind: 'corrupt',
subject: anchor.id,
detail: 'ranked anchor is absent from the authoritative graph',
});
return false;
}
if (seen.has(anchor.id))
return false;
seen.add(anchor.id);
return true;
});
}
export function traverseEvidencePaths(index, ranking) {
const facts = [...ranking.boundaries];
const anchors = valid(index.graph, ranking, facts);
if (anchors.length <= 1) {
return {
nodeIds: anchors.map((anchor) => anchor.id),
edges: [],
boundaries: unique(facts),
closurePasses: 0,
};
}
const branches = new Set(ranking.branch);
const chain = anchors.filter(({ id }) => !branches.has(id));
const sources = chain.slice(0, -1).map((source, origin) => ({
source,
targets: ranking.flow
? [
chain[origin + 1],
...anchors.slice(anchors.indexOf(source) + 1, anchors.indexOf(chain[origin + 1])).filter(({ id }) => branches.has(id)),
]
: anchors.slice(anchors.indexOf(source) + 1),
}));
const visited = sources.map(({ source }) => new Set([source.id]));
const parents = sources.map(() => new Map());
const paths = sources.map(() => new Map());
const queue = sources.map(({ source }, origin) => ({
origin,
nodeId: source.id,
}));
const terms = new Set(ranking.queryTerms.map((term) => term.toLowerCase()));
const forest = !ranking.sequential;
const domain = (id) => {
const a = index.graph.nodeAttributes(id);
return domainOf(a.source_domain, String(a.source_file ?? ''), index.root_path);
};
const domains = new Set([
'production', 'unknown', ...anchors.map(({ id }) => domain(id)),
]);
for (let cursor = 0; cursor < queue.length; cursor += 1) {
const state = queue[cursor];
const search = sources[state.origin];
const found = paths[state.origin];
if (found.size === search.targets.length)
continue;
const seen = visited[state.origin];
const nextEdges = outgoing(index.graph, state.nodeId, terms)
.filter(({ to }) => domains.has(domain(to)));
const previous = parents[state.origin];
for (const edge of nextEdges) {
if (seen.has(edge.to))
continue;
seen.add(edge.to);
previous.set(edge.to, { nodeId: state.nodeId, edge });
if (search.targets.some((target) => target.id === edge.to)) {
found.set(edge.to, rebuild(search.source.id, edge.to, previous));
}
queue.push({ origin: state.origin, nodeId: edge.to });
}
}
const nodeIds = [];
const edges = [];
const nodes = new Set();
const edgeIds = new Set();
const include = (nodeId) => {
if (nodes.has(nodeId))
return;
nodes.add(nodeId);
nodeIds.push(nodeId);
};
for (const anchor of anchors)
include(anchor.id);
for (const [origin, search] of sources.entries()) {
for (const [targetIndex, target] of search.targets.entries()) {
const path = paths[origin].get(target.id);
const adjacent = !ranking.flow && targetIndex > 0
&& anchors.slice(origin, origin + targetIndex + 1).every((_, offset) => paths[origin + offset].has(anchors[origin + offset + 1].id));
const commonParent = forest
&& sources.slice(0, origin).some((_, earlier) => paths[earlier].has(search.source.id) && paths[earlier].has(target.id));
const fanOut = forest && anchors.some((anchor) => anchor.id !== search.source.id
&& anchor.id !== target.id
&& direct(index.graph, anchor.id, search.source.id)
&& direct(index.graph, anchor.id, target.id));
const targetSource = sources.findIndex(({ source }) => source.id === target.id);
const fanIn = forest && targetSource >= 0
&& anchors.some((anchor) => anchor.id !== search.source.id
&& anchor.id !== target.id
&& visited[origin].has(anchor.id)
&& visited[targetSource].has(anchor.id));
if (adjacent)
continue;
if (!path && targetIndex === 0
&& !commonParent && !fanOut && !fanIn) {
facts.push({
kind: 'disconnected',
subject: `${search.source.id} -> ${target.id}`,
detail: `${verify(index.graph, search.source.id)} -> ${verify(index.graph, target.id)}`,
});
}
for (const edge of path ?? []) {
include(edge.from);
include(edge.to);
if (edgeIds.has(edge.id))
continue;
edgeIds.add(edge.id);
edges.push(edge);
}
}
}
for (const from of nodeIds) {
for (const to of index.graph.successors(from)) {
if (!nodes.has(to))
continue;
for (const graphEdge of index.graph.edgesBetween(from, to)) {
const edge = pathEdge(graphEdge);
if (!allowed(index.graph, edge) || edgeIds.has(edge.id))
continue;
edgeIds.add(edge.id);
edges.push(edge);
}
}
}
return { nodeIds, edges, boundaries: unique(facts), closurePasses: 1 };
}
# Why Madar
Large repositories make coding agents spend early turns rediscovering routes, services, jobs, persistence, and tests. Madar gives the agent a smaller authenticated starting path.
## What it does
```text
madar generate .
madar <agent> install
retrieve(question, budget?)
```
For one repository question, Madar ranks graph anchors, follows one bounded directed closure, verifies source bytes against the canonical graph, and returns exact excerpts plus relationships.
The same call can return explicit missing, disconnected, unsupported, stale, unavailable, corrupt, or truncated boundaries. That is more useful than hiding an incomplete path behind a confidence label.
## What it does not do
Madar does not:
- run the application
- observe production state
- review a pull request by itself
- scan for vulnerabilities
- support load-bearing non-JavaScript/TypeScript code
- guarantee a complete answer for every repository
## Evidence
Historical benchmark receipts show what earlier recorded workflows achieved, including controlled experiments that used task-specific assistance. They remain valid receipts for those versions but are not current universal performance claims.
Core Reset uses pinned held-out repositories, exact-source grading, deterministic performance gates, and package/deletion budgets before making new claims. See [`docs/core-reset/scorecard.md`](../docs/core-reset/scorecard.md).

Sorry, the diff of this file is too big to display