Skip to content
This repository was archived by the owner on Oct 9, 2026. It is now read-only.

Latest commit

 

History

History
511 lines (420 loc) · 23 KB

code-mode-api.md

File metadata and controls

511 lines (420 loc) · 23 KB

Diffr code-mode API proposal

The implementation is available through diffr/api, backed by the shared Rust engine and a Node-API addon. Code mode is ordinary Node.js or Bun with top-level await. This document records the API contract and the agreed type changes.

Shared Rust types

Reuse the existing Source, Region, and Node types. Add one optional serialized field, search_highlights, to Node::Leaf; no separate search tree types or generic payload parameter are needed. Source and Region keep their existing fields.

The only permitted change to the Node declaration is this field addition in src/protocol/mod.rs. Existing fields, variants, attributes, and comments remain unchanged; Source and Region declarations do not change.

diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs
--- a/src/protocol/mod.rs
+++ b/src/protocol/mod.rs
@@ -236,6 +236,8 @@ pub enum Node {
         alignment_id: u32,
         #[serde(default, skip_serializing_if = "Vec::is_empty")]
         changed: Vec<Span>,
+        #[serde(default, skip_serializing_if = "Vec::is_empty")]
+        search_highlights: Vec<Span>,
     },
     /// A foldable region. Its range is the hull of its children.
     Fold { children: Vec<Region> },

Ordinary diffs leave search_highlights empty, so their serialized output remains unchanged. Deserializing older data defaults the field to an empty vector. Rust leaf constructors and exhaustive patterns must be updated for the new field. Full text and syntax live once per source; regions address that text with the existing zero-based UTF-8 byte coordinates and Span type.

An unchanged search match is valid without splitting its containing leaf:

Node::Leaf {
    alignment_id: 7,
    changed: vec![],
    search_highlights: vec![Span {
        line: 79,
        start_column: 4,
        end_column: 14,
    }],
}

changed means structural-diff markings; search_highlights means query-match coverage. The two sets are independent. Highlights may occur in an unchanged region or a wholly unchanged file. Search indexing reads unchanged blobs from Git and builds syntax and fold structure in the search flow. The ordinary diff engine's identical-file fast path remains unchanged.

Hydration retains the complete indexed comparison. It attaches selected highlights and initially collapses regions without evidence. Repeated hits are deduplicated; source trees remain ordered and non-overlapping. Postprocessing unions highlights from compatible selections and applies plugins to these complete trees.

Proposed JS methods on bound search regions derive their answers from the leaves:

region.hasChanges(): boolean
region.hasHighlights(): boolean
region.hasChangedHighlights(): boolean // Highlight/change span intersection.

No stored region_changed or highlights_changed flags. Callers can rank/filter whole results. To deselect a region, clear its descendant search highlights; do not remove or reorder the complete comparison's regions.

Client inputs and signatures

interface Scope {
  repo: string;
  baseWorktree: { commitId: string; path: string };
  headWorktree: { commitId: string; path: string };
}

interface Hit {
  file: string; // Absolute path in either worktree.
  lines: {
    line: number; // 1-based, like rg -n and nl.
    text: string; // Source line without its line terminator.
  }[];
}

export function hydrate(scope: Scope, hits: Hit[]): Promise<SearchResult[]>;

export function postprocess(
  scope: Scope,
  selected: SearchResult[],
  options?: { plugins?: PluginConfig },
): Promise<SearchResult[]>;

SearchResult is the bound client envelope around a complete comparison. Each Source holds one side of one file; its regions can contain multiple hits and structural changes. SearchResult groups the base/head sources for a file correspondence (or just one side), adds file identity and scope, and provides printing and serialization methods. It is not an individual hit or region. Its fields are shown explicitly here:

export type Comparison<F, S> =
  | { file: { lhs: F; rhs: F }; sources: { same: S; lhs?: never; rhs?: never } }
  | { file: { lhs: F; rhs: F }; sources: { lhs: S; rhs: S; same?: never } }
  | { file: { lhs: F; rhs?: never }; sources: { lhs: S; rhs?: never; same?: never } }
  | { file: { lhs?: never; rhs: F }; sources: { lhs?: never; rhs: S; same?: never } };
type SearchResultData = {
  display: "both" | "lhs" | "rhs";
  scope: Scope;
} & Comparison<FileRef, SourceData>;

type SearchResult = {
  display: "both" | "lhs" | "rhs";
  scope: Scope;
  setCollapsed(foldStateId: number, collapsed: boolean): void;
  toString(): string;
  [inspect.custom](): string;
  toJSON(): SearchResultData;
} & Comparison<FileRef, Source>;

FileRef contains path, oid, and mode. sources.same holds shared unchanged content once, while file keeps both revision references. display controls printing and Review presentation; it never changes plugin input. both means all available sides; requesting an absent side rejects. inspect is Node's util symbol.

setCollapsed sets visibility.collapsed on every region with the given fold_state_id across both included sides of this result, preserving labels and other visibility fields. It traverses nested regions and throws if the ID does not exist. Descendants with other IDs retain their collapse state; opening a nested region does not automatically open its ancestors. The helper mutates only this result's visibility, without rerunning plugins or changing highlights.

  • Scope is plain caller-supplied data. Both worktrees are required and pinned.
  • Rust lazily indexes pinned Git blobs and caches structural analysis and query-derived regions before attaching hit-dependent visibility. There is no separate scope-resolution call. Postprocess overrides configure mutation behavior; they do not rebuild the structural queries used by hydration.
  • Hydration maps hits to the indexed source and attaches highlights. The current line-only Hit input produces whole-line highlights; token highlights would require richer search input later. Distinct source counterparts are highlighted independently; Same has one shared tree.
  • Hydration rejects with an error if a hit path is outside the scoped worktrees, does not resolve to an indexed source, or contains an out-of-bounds line number. Each hit line's text must exactly match the corresponding line in Source.text, excluding its line terminator; a mismatch also rejects hydration with an error.
  • The old SourceSelection / DiffSelection / matches payload proposal is replaced by the search tree. Highlight spans cover matched lines; existing region visibility describes what to show. No parallel Coverage or visible-range schema.
  • Combined versus side-only results use indexed correspondence, including renames, not filename equality. Unchanged content uses sources.same.
  • Hydration initially exposes selected evidence without surrounding context.
  • postprocess coalesces compatible selected comparisons, unions their highlights, and applies configured plugins for context and folding. Preserve change spans and deliberate combined/side-only views. Splitting leaves for presentation is allowed but not necessary merely to represent a hit.
  • Hydrated and postprocessed sources retain the full indexed comparison. Unselected unchanged content outside plugin-expanded context is collapsed, not discarded, including before and after the excerpt. Callers can expand it by editing visibility and printing again; this does not add query highlights or require another read or hydration call.
  • Both operations return SearchResult[]. Ordinary JS or a relevance scorer such as Jev can rank/filter results before or after postprocessing; no type conversion is required. Query-specific scores belong to the caller's ranking logic.
  • Initial results can reference shared source content in memory. Do not independently mutate shared visibility trees when constructing distinct views.
  • Plugins run on the complete original comparison, independently of display. Both ordinary diffs and search use the same plugin pipeline.

Plugin contract and behavior

Carry search_highlights: Vec<Span> through the plugin SDK's leaf type and add search-highlights: list<span> to the WIT leaf record. Conversions and plugin moves must preserve both span sets, partitioning them when leaves split and retaining them when regions are grouped. The WIT record change requires updating and rebuilding affected plugin components; JSON omission does not make the plugin ABI unchanged.

Add Unchanged / unchanged to the Rust and plugin file-status enums so wholly unchanged files can run the same classify and mutate hooks with accurate metadata.

Update existing plugins to honor both changed regions and search-highlighted regions. Context expansion uses the union of changed and highlighted lines as anchors for neighboring lines and query-derived scope boundaries. No separate search mode is required: an ordinary diff's search highlights are empty. Changes away from search matches can therefore remain visible too.

Existing policies may still fold changed code. Search highlights override those policies: the SDK move applier prevents file, ancestor, and linked-state collapse from hiding a match. Grouping treats highlighted regions as boundaries, removed runs skip highlighted leaves, and summarization excludes affected bodies and linked docstrings before requesting a summary. Diff statistics and change coloring continue to use only changed; search ranking and match coloring use search_highlights. Shared SDK helpers can provide the union for visibility policies without conflating the two meanings.

Highlight fields at the plugin boundaries

The protocol Node::Leaf addition is shown above. These are the corresponding SDK and WIT additions; no new span type is required:

diff --git a/crates/diffr-plugin-sdk/src/tree.rs b/crates/diffr-plugin-sdk/src/tree.rs
--- a/crates/diffr-plugin-sdk/src/tree.rs
+++ b/crates/diffr-plugin-sdk/src/tree.rs
@@ -73,6 +73,7 @@
     Leaf {
         alignment_id: u32,
         changed: Vec<Span>,
+        search_highlights: Vec<Span>,
     },
     Fold {
         children: Vec<Region>,
diff --git a/wit/plugin.wit b/wit/plugin.wit
--- a/wit/plugin.wit
+++ b/wit/plugin.wit
@@ -104,6 +104,7 @@
         /// lines up with this one.
         alignment-id: u32,
         changed: list<span>,
+        search-highlights: list<span>,
     }
 
     /// A leaf tiles the file; a fold's range is the hull of its children.

The WIT generates the Rust types::Leaf records; do not hand-edit generated bindings. Each conversion must copy search_highlights alongside changed:

Boundary Required update
src/plugin/mod.rs: to_tree Destructure protocol search_highlights; map every span's line, start_column, and end_column into SDK spans.
src/plugin/mod.rs: from_tree Destructure SDK search_highlights; map the same three coordinates back into protocol spans.
crates/diffr-plugin-sdk/src/tree.rs: Source::from_record Add search_highlights: leaf.search_highlights.clone() to the reconstructed leaf.
crates/diffr-plugin-sdk/src/tree.rs: Source::to_record Destructure the field and add search_highlights: search_highlights.clone() to the flattened types::Leaf.
src/plugin/wasm.rs: source Map leaf.search_highlights into the component's generated span records, just as leaf.changed is mapped.
Native and guest SDK adapters Continue using the shared record/tree conversions above; no separate highlight payload.

Mapping a span copies coordinates exactly: no line renumbering, merging with changed, or copying highlights from one side to the other. Leaf constructors for ordinary diff output initialize an empty vector. Rebuild generated bindings and plugin components against the updated WIT before using this field.

What happens when a plugin splits a leaf

A leaf covers source lines. Cut divides it into two leaves at a line boundary; it does not edit the source text. Each highlight belongs to the resulting leaf that contains its source line.

For example, using zero-based, half-open source ranges:

Before: leaf [10, 16)
  search_highlights = [(line 11, columns 2..7), (line 14, columns 0..5)]

Cut at offset 3: the boundary is source line 13.

After:  leaf [10, 13)
  search_highlights = [(line 11, columns 2..7)]
        leaf [13, 16)
  search_highlights = [(line 14, columns 0..5)]

The spans keep their original source coordinates. A highlight on boundary line 13 belongs to the second leaf. Because a Span covers only one line and Cut only cuts between lines, a span itself never needs to be cut. The combined highlight coverage before and after the operation must be identical.

The applier already does this for changed. Extend split in crates/diffr-plugin-sdk/src/apply.rs to read both vectors and construct each piece with the same line-membership filter for each vector:

let Node::Leaf { changed, search_highlights, .. } = &leaf.node else {
    unreachable!("only leaves are cut");
};

// Inside the existing piece constructor; `lines` is this piece's source range.
Node::Leaf {
    alignment_id,
    changed: changed.iter().copied()
        .filter(|span| lines.contains(&span.line)).collect(),
    search_highlights: search_highlights.iter().copied()
        .filter(|span| lines.contains(&span.line)).collect(),
}

When a leaf has an aligned counterpart, Cut also splits that counterpart at the same relative line offset. Each side distributes only its own spans, using its own source line numbers. An unmatched counterpart stays unhighlighted.

JoinFolds retains the child leaves, so their highlight data stays in place. LinkFoldState, SetCollapsed, SetLabel, and SetTags do not reconstruct leaf span data. Linking and collapsing can still hide highlights; preserving coverage and keeping matches visible are separate requirements for plugin behavior.

Storage

Storage is controlled by diffr config, entirely in Rust. Set storage.backend to memory, file, or sqlite and storage.path to the cache directory (default .cache/diffr, relative to the source repository). The JS API does not expose storage options. See computed diff storage for configuration, the Rust trait, keys, and lifecycle.

Jev selection adapter

The optional diffr/jev adapter uses the official @typesafe-ai/sdk in TypeScript. Run it after postprocessing, as a final result ranker/filter:

import * as diffr from "diffr/api";
import { createJevRanker } from "diffr/jev";
import { TypeSafeClient } from "@typesafe-ai/sdk";

const jev = createJevRanker({ client: new TypeSafeClient(), concurrency: 4 });
const hydrated = await diffr.hydrate(scope, hits);
const results = await diffr.postprocess(scope, hydrated);
const ranked = await jev.rank("Find code that describes a retry label", results);
console.table(ranked.map(({ result, score }) => ({
  file: (result.file.rhs ?? result.file.lhs)!.path, score,
})));
const selected = jev.filter(ranked, { minScore: 0.6, limit: 5 });
console.log(selected);

One request judges one complete result. Its state is exactly:

{ query, result: result.toString() }

The result string is the same pretty-printed output the caller sees, including paired base/head lines, function signatures, plugin-provided context, and fold notices. The adapter adds no surrounding source lines and does not expose hidden source. Jev judges visible evidence only. Expand folds before ranking if more evidence is needed. Postprocessing has already coalesced duplicate hydrated candidates.

Each score is a Noul: the probability that the displayed result provides evidence relevant to the query. Results sort highest first with stable ties. The caller can inspect each judgment's model and token usage. The model inherits the TypeSafe client's default unless createJevRanker({ model }) overrides it. rank accepts SDK request options, including an abort signal, as its third argument. Errors reject the operation without silently selecting a fallback.

filter is local and makes no model calls. minScore is inclusive; limit counts complete results, not individual regions or sides. The example's 0.6 is illustrative, not a calibrated guarantee. Selected results are independent copies with the same source pairing, regions, fold state, and printed output. Neither operation mutates the input. There is no second postprocessing call.

Run bun run test:jev for the live fixture example using TYPESAFE_API_KEY (Bun loads .env). It prints all scores and the selected pretty-printed output. An optional path argument saves the exact HTTP request and response bodies: bun run test:jev /tmp/jev-exchanges.json. Headers and credentials are excluded. The regular code-mode suite tests the SDK transport contract deterministically; the live example uses the real service and fails if credentials are missing.

Composition example

The eval host can preload the imports. scope below is a caller-supplied plain object; Rust creates or reuses the comparison index for its pins.

import * as diffr from "diffr/api";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import * as fs from "node:fs/promises";

const exec = promisify(execFile);
const scope = {
  repo: "/repo",
  baseWorktree: { commitId: baseCommitId, path: "/checkouts/base" },
  headWorktree: { commitId: headCommitId, path: "/checkouts/head" },
};

// 1. RETRIEVE: normal rg across both worktrees; no diffr search API.
const { stdout } = await exec("rg", [
  "--json", "--fixed-strings", "--", "apply_fold",
  scope.baseWorktree.path, scope.headWorktree.path,
], { maxBuffer: 16 * 1024 * 1024 }).catch(error => {
  if (error.code === 1) return { stdout: "" }; // No matches.
  throw error;
});

// 2. ADAPT: ordinary JS, here for rg's default single-line text mode.
const hits = stdout.split("\n").filter(Boolean)
  .map(line => JSON.parse(line))
  .filter(event => event.type === "match")
  .map(({ data }) => {
    if (data.path.text === undefined || data.lines.text === undefined) {
      throw new Error("This adapter requires text paths and content");
    }
    return {
      file: data.path.text,
      lines: [{
        line: data.line_number,
        text: data.lines.text.replace(/\r?\n$/, ""),
      }],
    };
  });

// Optional: inspect raw hits using their paired line/text representation.
console.log(hits.flatMap(hit =>
  hit.lines.map(({ line, text }) => `${hit.file}:${line}: ${text}`)
).join("\n"));

// 3. HYDRATE: attach query highlights to the indexed search-region trees.
const hydrated = await diffr.hydrate(scope, hits);

// 4. SELECT: rank whole file comparisons, preferring changed matches.
const selected = hydrated.sort((a, b) =>
  Number(Object.values(b.sources).some(s => s.regions.some(r => r.hasChangedHighlights()))) -
  Number(Object.values(a.sources).some(s => s.regions.some(r => r.hasChangedHighlights())))
).slice(0, 5);

// 5. POSTPROCESS: apply context/folding plugins to the complete comparisons.
const results = await diffr.postprocess(scope, selected);

// 6. READ: each object pretty-prints like numbered source plus a unified diff.
console.log(results[0]);
console.log(results);

// Structured data is still available for normal JS and persistence.
await fs.writeFile("/scratch/results.json", JSON.stringify(results));

// An eval host that displays completion values through Node inspection can
// display these directly. A JSON-serializing host receives toJSON() data instead.
results;

Illustrative custom-inspection output for one result:

src/retry.ts — base → head
 base  head
              … base 1–17 / head 1–17 collapsed [fold_state_id=4] …
   18    18    async function retry(request) {
   19       -   const attempts = 3;
         19 +   const attempts = options.attempts ?? 3;
   20    20      for (let i = 0; i < attempts; i++) {
              … base 21–28 / head 21–28 collapsed [fold_state_id=12] …
   29    29      throw lastError;
   30    30    }
              … base 31–64 / head 31–65 collapsed [fold_state_id=19] …

[More context: call result.setCollapsed(fold_state_id, false) with an indicated
ID, then print the result again. Full source
text and region children are already present; no read call is needed.]

Display line numbers are 1-based. Unchanged evidence can print as a single numbered source excerpt. Gaps remain explicit and source line numbers are never renumbered. Every collapsed area, including leading/trailing content, prints its side-specific line ranges and a file-local fold_state_id. The helper updates matching regions on both included sides, since that ID links their collapse state. Missing sides have no range. These notices describe folded content, not output-budget truncation.

For example, ordinary JS can open the middle gap above:

const result = results[0];

// Open the linked regions on both included sides.
result.setCollapsed(12, false);
console.log(result);

The printer uses current visibility each time; it must not reuse a stale rendered string. Opening an outer fold can reveal further collapsed children with their own hints. No need to rerun postprocessing merely to expand a fold, since plugins could collapse it again. Visibility edits are local to this result, even when source text is shared with other results.

Like Pi's read tool, omission notices give an actionable next step. Here that step edits the in-memory object rather than fetching another page.

Saving JSON preserves data, not methods; a future deserialization helper can restore object printing without recomputing the comparison.

Future Review composition (illustrative, not part of this API):

review.anchor(results[0]);
review.codePeek(results[0]);
// Use the same result as a sequence step's source evidence.

The leaf highlights identify matched lines; the region trees supply expanded context and visibility. A future Review adapter can consume these together without confusing matched lines with surrounding context. The extended leaf field and client envelope are not asserted to be accepted by the current Review API unchanged.

Original comparison at the plugin boundary

The plugin SDK Pairing<Source> and WIT source-sides gain Same { source } / same(source). This represents identical source once, with ordinary regions, empty changed spans, and selected search highlights. It is neither added nor deleted. Both, LeftOnly, and RightOnly describe the original comparison, never display-side selection. File metadata retains the actual original refs, including both refs for Same.

Hydration owns highlight attachment and retains complete trees. Postprocessing validates them against the pinned index and runs plugins directly on the original comparison. Equivalent selections with different display sides share processing. The JS result uses the Comparison union above; Review upgrades older stored evidence on read without inventing missing sides. The computed-diff cache schema is unchanged. The SDK and WIT change requires rebuilding external plugins.