Find code by asking what it does. A CLI for coding agents that uses Jev to discover relevant files and source context.
TypeScript ai-sdkclaude-codeclicode-searchcodexcoding-agentscontext-retrievaldeveloper-toolsjevsemantic-searchtypescriptvercel-ai-gateway
Restore frozen source selection orchestration and exact evidence bytes
David Zhang81d63b37a77e
diff --git a/packages/core/src/selection.ts b/packages/core/src/selection.tsindex 34ded87..4d100ba 100644--- a/packages/core/src/selection.ts+++ b/packages/core/src/selection.ts@@ -1,6 +1,13 @@import { EvaluationFailure } from "./gateway";import { evidenceRequest, type Evidence } from "./requests";-import { inspect, pythonNeighborhood, sourceForUnit, type Range, type SourceUnit } from "./source";+import {+ inspect,+ pythonNeighborhood,+ sourceForUnit,+ splitSource,+ type Range,+ type SourceUnit,+} from "./source";import type { Evaluator, FileEvidence, ReadingLead, EvidenceRange } from "./types";import type { Snapshot } from "./filesystem";@@ -23,7 +30,7 @@ function mergeSpans(spans: Span[]): Span[] {return merged;}-/** Classify declarations once per pass; rendering always expands selected evidence, never prior rendering. */+/** The bounded follow-up expands prior context once more; positive selections retain separate provenance. */export async function selectFile(snapshot: Snapshot,query: string,@@ -79,21 +86,59 @@ export async function selectFile(unit.sourceByteEnd !== offsets[unit.range.endLine]);}- const syntax = await inspect(snapshot, { maxUnitBytes: sourceUnitBytes });+ const giantLine = lines.some((line) => Buffer.byteLength(line) > sourceUnitBytes);+ const syntax = await inspect(snapshot, {+ maxUnitBytes: giantLine ? sourceUnitBytes : Math.max(sourceUnitBytes, bytes.length),+ });+ // Giant lines retain byte coordinates; ordinary fallback uses complete-source line fragments.+ let units = syntax.units;+ if (!giantLine && (syntax.mode === "text" || units.every((unit) => unit.partial))) {+ units = splitSource(snapshot, 3000).map((unit) => ({+ ...unit,+ range: {+ ...unit.range,+ endLine: lineAt(Math.max(unit.sourceByteStart, unit.sourceByteEnd - 1)),+ },+ }));+ }+ if (!giantLine)+ units = units.flatMap((unit) => {+ if (+ Buffer.byteLength(lines.slice(unit.range.startLine - 1, unit.range.endLine).join("\n")) <=+ sourceUnitBytes+ )+ return [unit];+ const blocks: SourceUnit[] = [];+ for (let start = unit.range.startLine; start <= unit.range.endLine; start += 16) {+ const end = Math.min(unit.range.endLine, start + 15);+ blocks.push({+ id: `${unit.name}:${start}:${end}`,+ name: unit.name,+ range: { startLine: start, endLine: end },+ sourceByteStart: offsets[start - 1]!,+ sourceByteEnd: offsets[end]!,+ partial: true,+ });+ }+ return blocks;+ });const selected: Span[] = (previous?.selected ?? []).map(spanForRange);+ const contextSpans: Span[] = (previous?.rendered ?? []).map(spanForRange);const leads = new Map<string, ReadingLead>();function addLead(lead: ReadingLead) {const key = JSON.stringify([lead.name, lead.range]);- const old = leads.get(key);- if (!old || lead.score > old.score) leads.set(key, lead);+ leads.set(key, lead);}for (const lead of previous?.leads ?? []) addLead({ ...lead, range: { ...lead.range } });const groups: SourceUnit[][] = [];let pending: SourceUnit[] = [];- for (const unit of syntax.units) {+ for (const unit of units) {if (pending.length &&- (pending.length >= 8 || unit.sourceByteEnd - pending[0]!.sourceByteStart > 14000)+ (pending.length >= 8 ||+ Buffer.byteLength(+ lines.slice(pending[0]!.range.startLine - 1, unit.range.endLine).join("\n"),+ ) > 14000)) {groups.push(pending);pending = [];@@ -108,6 +153,7 @@ export async function selectFile(if (prepared === null) {invalidated = true;selected.length = 0;+ contextSpans.length = 0;leads.clear();break;}@@ -143,7 +189,11 @@ export async function selectFile(return { unit, value };});for (const { unit, value } of values) {- if (value > 0.5) selected.push({ start: unit.sourceByteStart, end: unit.sourceByteEnd });+ if (value > 0.5) {+ const span = { start: unit.sourceByteStart, end: unit.sourceByteEnd };+ selected.push(span);+ contextSpans.push(span);+ }if (value > 0.25 && !unit.name.endsWith(".context"))addLead({name: unit.name,@@ -160,7 +210,7 @@ export async function selectFile(const chosen = mergeSpans(selected),wholeRanges: Range[] = [],rendered: Span[] = [];- for (const span of chosen) {+ for (const span of mergeSpans(contextSpans)) {const range = rangeForSpan(span);if (range.sourceByteStart !== undefined) rendered.push(span);else wholeRanges.push(range);@@ -212,25 +262,33 @@ export async function selectFile(rendered.push({ start: segmentStart, end: offsets[window.endLine]! });}const output = mergeSpans(rendered);+ function renderedRange(span: Span): EvidenceRange {+ const range = rangeForSpan(span);+ // A trailing empty line has no byte interval, but remains part of a line-based window.+ if (+ range.sourceByteStart === undefined &&+ span.end === bytes.length &&+ windows.some((window) => window.endLine === lines.length)+ )+ range.endLine = lines.length;+ return range;+ }const file: FileEvidence = {path: snapshot.path,contentHash: snapshot.contentHash,score,roles: [...(previous?.roles ?? [])],- leads: [...leads.values()].sort(- (a, b) =>- a.range.startLine - b.range.startLine ||- a.range.endLine - b.range.endLine ||- a.name.localeCompare(b.name),- ),+ leads: [...leads.values()],selected: chosen.map(rangeForSpan),- rendered: output.map(rangeForSpan),+ rendered: output.map(renderedRange),excerpts: output.map((span) => {- const range = rangeForSpan(span),+ const range = renderedRange(span),partial = range.sourceByteStart !== undefined;return {range,- source: bytes.subarray(span.start, span.end).toString("utf8"),+ source: partial+ ? bytes.subarray(span.start, span.end).toString("utf8")+ : lines.slice(range.startLine - 1, range.endLine).join("\n"),...(partial ? { sourceByteStart: span.start, sourceByteEnd: span.end, partial: true } : {}),};}),diff --git a/packages/core/src/source.ts b/packages/core/src/source.tsindex 2265030..c7188a8 100644--- a/packages/core/src/source.ts+++ b/packages/core/src/source.ts@@ -219,10 +219,18 @@ export async function inspect();const { source, path } = snapshot;const text = sourceText(source);+ // Context windows use conservative whole-line Python comments, including inside multiline strings.+ const pythonComments = /\.pyi?$/.test(path)+ ? source+ .split("\n")+ .flatMap((line, index) =>+ /^\s*#/.test(line) ? [{ startLine: index + 1, endLine: index + 1 }] : [],+ )+ : [];const fallback = (reason: Inspection["fallback"]): Inspection => ({mode: "text",fallback: reason,- comments: [],+ comments: pythonComments,units: source? textUnits(text, { startLine: 1, endLine: text.lineCount }, "source", maxUnitBytes, true): [],@@ -237,12 +245,7 @@ export async function inspect(if (!tree) return fallback("syntax");try {units = pythonUnits(tree.rootNode, source);- const stack = [tree.rootNode];- while (stack.length) {- const node = stack.pop()!;- if (node.type === "comment") comments.push(nodeRange(node));- else stack.push(...children(node));- }+ comments = pythonComments;mode = "python";} finally {tree.delete();diff --git a/packages/core/test/selection.test.ts b/packages/core/test/selection.test.tsindex 6c67e05..0d1a055 100644--- a/packages/core/test/selection.test.ts+++ b/packages/core/test/selection.test.ts@@ -36,7 +36,7 @@ test("selected source and optional leads retain distinct meanings and exact orig{ name: "selected", score: 0.8 },{ name: "lead", score: 0.4 },]);- expect(result.file.excerpts[0]!.source).toBe(source.split("\n").slice(0, 7).join("\n") + "\n");+ expect(result.file.excerpts[0]!.source).toBe(source.split("\n").slice(0, 7).join("\n"));expect(result.file.excerpts.some((excerpt) => excerpt.source.includes("function lead"))).toBe(false,);@@ -63,7 +63,7 @@ test("strict thresholds keep .context out of leads without conflating it with soexpect(result.file.leads.map((lead) => lead.name)).toEqual(["Example.alpha", "Example.gamma"]);});-test("Python neighbors are rendering context and follow-up passes do not expand that context again", async () => {+test("follow-up context expands without promoting its neighbors to positive evidence", async () => {const method = (name: string) =>` def ${name}(self):\n a = 1\n b = 2\n c = 3\n d = 4\n e = 5\n return a + b + c + d + e\n\n`;const source =@@ -100,7 +100,8 @@ test("Python neighbors are rendering context and follow-up passes do not expandfirst.file,);expect(calls).toBeGreaterThan(0);- expect(second.file.excerpts).toEqual(first.file.excerpts);+ expect(second.file.rendered).toEqual([{ startLine: 1, endLine: source.split("\n").length }]);+ expect(second.file.excerpts[0]!.source).toBe(source);expect(second.file.selected).toEqual(first.file.selected);expect(second.file.roles).toEqual(["implementation"]);});@@ -195,7 +196,7 @@ test("malformed group answers stay unknown without discarding previous successes);expect(second.issues).toEqual([{ kind: "provider", count: 1 }]);expect(second.file.selected).toEqual(first.file.selected);- expect(second.file.excerpts).toEqual(first.file.excerpts);+ expect(second.file.excerpts[0]!.source).toBe(source.split("\n").slice(0, 7).join("\n"));});test("ordinary large-source requests retain the frozen opening and eight-line window", async () => {diff --git a/test/reference/selection-replay.ts b/test/reference/selection-replay.tsnew file mode 100644index 0000000..ce678b7--- /dev/null+++ b/test/reference/selection-replay.ts@@ -0,0 +1,85 @@+import { spawnSync } from "node:child_process";+import { extname } from "node:path";+import frozen from "../../evals/implementation/swebench/hierarchy-unit-locators-spike.ts" with { type: "text" };+import neighborhoodParser from "../../evals/implementation/swebench/source-neighborhood-spike.py" with { type: "text" };+import {+ inspectSource,+ contextWindows,+} from "../../evals/implementation/swebench/source-method-windows-spike";+import { completeSourceFragments } from "../../evals/implementation/swebench/complete-source-v64-spike";+import type { EvaluationRequest } from "../../packages/core/src/gateway";+import type { Declaration } from "../../packages/core/src/requests";+import type { Range } from "../../packages/core/src/source";++/** Execute the frozen selection block itself; only its filesystem and provider boundaries are replaced. */+export async function replaySelection(+ path: string,+ source: string,+ query: string,+ score: (declaration: Declaration, pass: number) => number,+) {+ const requests: EvaluationRequest[] = [];+ const block = frozen.slice(+ frozen.indexOf("const sourceLines="),+ frozen.indexOf("// Role labels"),+ );+ if (!block.includes("for(let evidencePass=0;evidencePass<2;evidencePass++)"))+ throw new Error("Frozen selection block was not found");+ const transpiler = new Bun.Transpiler({ loader: "ts" });+ const run = new Function(+ "dependencies",+ transpiler.transformSync(`+ const {open,contained,inspectSource,contextWindows,completeSourceFragments,spawnSync,extname,+ neighborhoodParser,evaluate,path,source,query}=dependencies;+ return (async()=>{+ const candidates=[{path}], values={query};+ let requests=0, partial=false;+ const requestBudget=50000, waitForRateLimit=async()=>{};+ const gateway={evaluationModel:()=>null};+ ${block}+ return {ranges:locations.get(path)||[],leads:[...(readingLeads.get(path)?.values()||[])],partial};+ })();+ `),+ );+ const bytes = Buffer.from(source);+ const result = (await run({+ path,+ source,+ query,+ inspectSource,+ contextWindows,+ completeSourceFragments,+ spawnSync,+ extname,+ neighborhoodParser,+ contained: async (name: string) => name,+ open: async () => ({+ stat: async () => ({ isFile: () => true, size: bytes.length, mtimeMs: 0 }),+ readFile: async () => bytes,+ close: async () => {},+ }),+ evaluate: async ({ state, questions }: EvaluationRequest) => {+ requests.push({ state, questions });+ const input = state as { declarations: Declaration[]; selectedEvidence?: unknown };+ return {+ answers: Object.fromEntries(+ input.declarations.map((declaration, index) => [+ `q${index}`,+ { type: "boolean", probability: score(declaration, input.selectedEvidence ? 1 : 0) },+ ]),+ ),+ };+ },+ })) as { ranges: Range[]; leads: Declaration[]; partial: boolean };+ return {+ ...result,+ requests,+ excerpts: result.ranges.map((range) => ({+ range,+ source: source+ .split("\n")+ .slice(range.startLine - 1, range.endLine)+ .join("\n"),+ })),+ };+}diff --git a/test/reference/selection.test.ts b/test/reference/selection.test.tsnew file mode 100644index 0000000..708f257--- /dev/null+++ b/test/reference/selection.test.ts@@ -0,0 +1,90 @@+import { expect, test } from "bun:test";+import { selectFile } from "../../packages/core/src/selection";+import type { EvaluationRequest } from "../../packages/core/src/gateway";+import type { Declaration } from "../../packages/core/src/requests";+import { replaySelection } from "./selection-replay";++async function expectParity(+ path: string,+ source: string,+ score: (d: Declaration, pass: number) => number,+) {+ const snapshot = { path, source, contentHash: "same source" },+ query = "target behavior";+ const expected = await replaySelection(path, source, query, score);+ const requests: EvaluationRequest[] = [];+ const evaluator = {+ requests: 0,+ async evaluate(request: EvaluationRequest) {+ requests.push(request);+ const state = request.state as { declarations: Declaration[]; selectedEvidence?: unknown };+ return Object.fromEntries(+ state.declarations.map((d, i) => [`q${i}`, score(d, state.selectedEvidence ? 1 : 0)]),+ );+ },+ };+ const first = await selectFile(snapshot, query, 0.9, evaluator);+ const evidence = first.file.excerpts.map((excerpt) => ({+ path,+ ...excerpt.range,+ source: excerpt.source,+ }));+ const second = await selectFile(+ snapshot,+ query,+ 0.9,+ evaluator,+ async () => ({ evidence }),+ first.file,+ );+ expect(expected.partial).toBe(false);+ expect(second.file.rendered).toEqual(expected.ranges);+ expect(second.file.excerpts.map(({ range, source }) => ({ range, source }))).toEqual(+ expected.excerpts,+ );+ expect(requests).toEqual(expected.requests);+ expect(second.file.leads.map((lead) => ({ name: lead.name, ...lead.range }))).toEqual(+ expected.leads,+ );+ return { first, second };+}++test("two-pass Python selection matches the frozen requests, expansion and source bytes", async () => {+ const method = (name: string) =>+ ` def ${name}(self):\n a = 1\n b = 2\n c = 3\n d = 4\n e = 5\n return a + b + c + d + e\n\n`;+ const source =+ "class Example:\n" + ["before", "target", "after", "unrelated", "later"].map(method).join("");+ const { second } = await expectParity("example.py", source, (d, pass) =>+ pass === 0 && d.name === "Example.target" ? 0.9 : 0,+ );+ expect(second.file.selected).toEqual([{ startLine: 10, endLine: 16 }]);+});++test("text fallback preserves the frozen source fragment questions and follow-up evidence", async () => {+ const source = Array.from(+ { length: 180 },+ (_, i) => `document line ${i}: ${"x".repeat(70)}\n`,+ ).join("");+ await expectParity("guide.txt", source, () => 0.9);+});++test("oversized declarations preserve sixteen-line questions and native context groups", async () => {+ const source =+ "def target():\n" ++ Array.from({ length: 80 }, (_, i) => ` value${i} = '${"x".repeat(400)}'\n`).join("");+ await expectParity("large.py", source, (_d, pass) => (pass === 0 ? 0.9 : 0));+});++test("Python context keeps the frozen conservative comment windows", async () => {+ const source =+ "def target():\n return 1\n\ntext = '''\nnot a Python comment:\n# retained by the source window policy\n# the next line belongs to that same window\n'''\n";+ await expectParity("comments.py", source, (_d, pass) => (pass === 0 ? 0.9 : 0));+});++test("follow-up reading leads retain first-seen order across passes", async () => {+ const source =+ "def earlier():\n return 1\n\n" + "\n".repeat(10) + "def target():\n return earlier()\n";+ await expectParity("leads.py", source, (d, pass) =>+ d.name === "target" ? 0.9 : pass === 1 ? 0.4 : 0,+ );+});