Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
// Copyright 2026 Ix Infrastructure Inc.
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import type { Command } from "commander";
import chalk from "chalk";
import { IxClient } from "../../client/api.js";
import { createClient } from "../../client/factory.js";
import { absoluteFromSourceUri, isReadablePath, resolveWorkspaceRoot } from "../config.js";
import { resolveFileOrReport, printResolved, type ResolvedEntity } from "../resolve.js";
import { formatDiff, relativePath, printJson } from "../format.js";
import { llmLine } from "../llm.js";
import { reportFailure } from "../ui.js";
import { parsePickOption } from "../options.js";
import { resolveWorkspaceId } from "../bootstrap.js";
/**
* The most changes the backend returns in one diff (`/v1/diff` clamps `limit`
* to 5000; it does not page). `--full` asks for this many, since sending no
* limit got the server's default of 100.
*/
export const DIFF_MAX_LIMIT = 5000;
/**
* The `workspace_id` to send with a diff: `*` (every workspace) for `--all`,
* else the workspace this directory belongs to, as the other graph reads use.
* Outside any mapped workspace it is undefined, which the backend reads as
* every workspace -- what `ix diff` always did, so no run that worked before
* now fails.
*/
export function diffWorkspaceScope(all: boolean | undefined, cwd = process.cwd()): string | undefined {
if (all) return "*";
return resolveWorkspaceId(cwd);
}
/** The `limit` to send: the backend's maximum for `--full`, else `--limit` or the server default. */
export function diffRequestLimit(opts: { full?: boolean; limit?: string }): number | undefined {
if (opts.full) return DIFF_MAX_LIMIT;
return opts.limit ? parseInt(opts.limit, 10) : undefined;
}
/** The text-mode line over a truncated diff. */
export function diffTruncationNote(shown: number, total: number, full: boolean | undefined): string {
if (full) {
return `Showing ${shown} of ${total} changes: the backend returns at most ${DIFF_MAX_LIMIT}. ` +
"Narrow it with a target or a smaller revision range, or use --summary for counts.";
}
return `Showing ${shown} of ${total} changes. Use --full to see up to ${DIFF_MAX_LIMIT}.`;
}
/**
* Subset of `DiffOptions` consumed by `detectDiffModeConflict`. Kept as a
* structural type so the detector can be unit-tested without driving Commander.
*/
export interface DiffModeOptions {
summary?: boolean;
content?: boolean;
full?: boolean;
limit?: string;
}
/**
* Detect mutually-incompatible output flags on `ix diff` and return a
* human-readable message naming the conflict, or `undefined` if no conflict.
*
* The action handler early-returns on `--summary` before the `--content` branch
* is reached, so passing both flags silently dropped `--content` (and the user
* got a summary when they asked for the full textual diff). Catching the
* combination up front and surfacing it as a hard error mirrors the
* explicit-conflict style in subsystems.ts. `--full --limit <n>` is also
* flagged: `--full` is documented as "no limit" so the silent precedence over
* `--limit` was the same kind of UX gap.
*
* Order is load-bearing, not incidental. `--summary` is the flag that wins at
* runtime, so when it is present its message is the one that names the flag
* actually in charge. Checking `--full --limit` first meant
* `ix diff 3 5 --summary --full --limit 20` reported "--full and --limit cannot
* be combined" -- two flags `--summary` was going to ignore anyway -- and the
* message that would have explained the run was unreachable for that input.
*/
export function detectDiffModeConflict(
opts: DiffModeOptions,
): string | undefined {
if (opts.summary && opts.content) {
return "--summary and --content cannot be combined; --summary renders counts only, --content renders the full textual diff. Pick one.";
}
if (
opts.summary &&
(opts.full || opts.limit !== undefined)
) {
return "--summary ignores --limit and --full; --summary is server-side counts only. Drop --summary to control change volume, or drop --limit/--full.";
}
if (opts.full && opts.limit !== undefined) {
return "--full and --limit cannot be combined; --full is documented to return all changes without a limit. Drop --limit, or drop --full to keep the existing limit.";
}
return undefined;
}
/** Render a `--summary` diff as a single llm record. */
function renderDiffSummaryLlm(result: any): string {
const s = result.summary || {};
return llmLine("diff", [
["from", result.fromRev], ["to", result.toRev],
["added", s.added || undefined], ["modified", s.modified || undefined],
["removed", s.removed || undefined], ["total", result.total],
]);
}
const execFileAsync = promisify(execFile);
// ── JSON compaction ────────────────────────────────────────────────
/** Compact a node snapshot from a diff change — drop timestamps, null deletedRev, member_files UUIDs. */
function compactNodeSnapshot(node: any): any {
if (!node) return node;
const out: any = { name: node.name, kind: node.kind };
if (node.provenance) {
const p = node.provenance;
const uri = relativePath(p.source_uri ?? p.sourceUri);
if (uri) out.path = uri;
if (p.source_hash ?? p.sourceHash) out.hash = (p.source_hash ?? p.sourceHash).slice(0, 12);
}
if (node.attrs) {
const a = { ...node.attrs };
// Drop member_files UUID arrays (bulky, not useful in diff output)
delete a.member_files;
delete a.memberFiles;
// Keep only non-empty attrs
if (Object.keys(a).length > 0) out.attrs = a;
}
return out;
}
/** Compact a diff result — strip bloat from change snapshots. */
function compactDiffResult(result: any): any {
const out: any = {
fromRev: result.fromRev,
toRev: result.toRev,
total: result.total ?? result.totalChanges,
};
if (result.truncated) out.truncated = true;
if (result.changes) {
out.changes = result.changes.map((c: any) => {
const change: any = { changeType: c.changeType };
if (c.entityId) change.entityId = c.entityId;
if (c.atFromRev) change.atFromRev = compactNodeSnapshot(c.atFromRev);
if (c.atToRev) change.atToRev = compactNodeSnapshot(c.atToRev);
// Preserve source content fields added by --content fallback
if (c.sourceContentBefore !== undefined) change.sourceContentBefore = c.sourceContentBefore;
if (c.sourceContentAfter !== undefined) change.sourceContentAfter = c.sourceContentAfter;
if (c.sourceDiff !== undefined) change.sourceDiff = c.sourceDiff;
if (c.sourceContent !== undefined) change.sourceContent = c.sourceContent;
return change;
});
}
if (result.summary) out.summary = result.summary;
return out;
}
// ── File content loading ────────────────────────────────────────────
// A source_uri is workspace-relative. These used to resolve it against the
// current directory and run git there, so `ix diff --content` from anywhere but
// the workspace root found no file, and `git show <hash>:<uri>` -- which reads
// the path from the REPOSITORY top level -- missed even from the root when the
// workspace is a subdirectory of its repo. Everything below resolves the uri
// against the workspace root and runs git there, naming the file `./<uri>` so
// git reads it relative to that directory. (The root, not the file's own
// directory: a deleted file's directory may be gone, and its history is not.)
/** Load full file content from current disk. */
export function loadFileFromDisk(uri: string, root?: string): string | null {
const filePath = absoluteFromSourceUri(uri, root);
// A source_uri comes from the graph, and an absolute one is used as-is: the
// same confinement as `ix read`, so a crafted graph cannot make diff print
// /etc/passwd.
if (!isReadablePath(filePath, root)) return null;
try {
if (!fs.existsSync(filePath)) return null;
return fs.readFileSync(filePath, "utf-8");
} catch {
return null;
}
}
/**
* Load full file content at a specific timestamp via git.
* Returns null if git fails or file doesn't exist at that point.
*/
export async function loadFileAtTimestamp(uri: string, timestamp: string, root?: string): Promise<string | null> {
const cwd = resolveWorkspaceRoot(root);
const rel = path.relative(cwd, absoluteFromSourceUri(uri, cwd)).split(path.sep).join("/");
const name = `./${rel}`;
try {
const { stdout: commitHash } = await execFileAsync("git", [
"log", "-1", "--format=%H", `--before=${timestamp}`, "--", name,
], { cwd, timeout: 10_000 });
const hash = commitHash.trim();
if (!hash) return null;
const { stdout: fileContent } = await execFileAsync("git", [
"show", `${hash}:${name}`,
], { cwd, timeout: 10_000 });
return fileContent;
} catch {
return null;
}
}
/**
* Get the timestamp for a specific graph revision by scanning the patch list.
* Falls back to current time if revision not found.
*/
async function getRevisionTimestamp(client: IxClient, rev: number): Promise<string | null> {
try {
const patches = await client.listPatches({ limit: 200 });
// Find the patch at or closest to this revision
const exact = patches.find((p: any) => p.rev === rev);
if (exact?.timestamp) return exact.timestamp;
// Find nearest rev <= target
const earlier = patches
.filter((p: any) => p.rev <= rev && p.timestamp)
.sort((a: any, b: any) => b.rev - a.rev);
if (earlier.length > 0) return (earlier[0] as any).timestamp;
return null;
} catch {
return null;
}
}
// ── Textual diff via git ────────────────────────────────────────────
/**
* Compute a unified diff between two strings using git diff --no-index.
* Returns parsed hunk lines (starting with @@, +, -, or space).
* Returns empty array if contents are identical.
*/
export async function computeTextualDiff(
before: string,
after: string,
): Promise<string[]> {
// Create a private temp directory atomically (0700, unique name) rather than
// writing predictably-named files into the shared tmpdir, which is a symlink/
// race vector (CodeQL js/insecure-temporary-file).
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "ix-diff-"));
const beforePath = path.join(tmpRoot, "a");
const afterPath = path.join(tmpRoot, "b");
fs.writeFileSync(beforePath, before);
fs.writeFileSync(afterPath, after);
try {
await execFileAsync("git", [
"diff", "--no-index", "--unified=3", "--no-color",
"--", beforePath, afterPath,
], { timeout: 10_000 });
// Exit code 0 = identical
return [];
} catch (err: any) {
// Exit code 1 = files differ (normal for git diff --no-index)
const stdout: string = err.stdout ?? "";
if (!stdout) return [];
return parseDiffOutput(stdout);
} finally {
try { fs.rmSync(tmpRoot, { recursive: true, force: true }); } catch {}
}
}
/** Parse git diff output — keep only hunk headers and content lines. */
function parseDiffOutput(raw: string): string[] {
const lines = raw.split("\n");
const result: string[] = [];
let inHunk = false;
for (const line of lines) {
if (line.startsWith("@@")) {
inHunk = true;
result.push(line);
} else if (inHunk) {
if (line.startsWith("diff ") || line.startsWith("index ") ||
line.startsWith("--- ") || line.startsWith("+++ ") ||
line.startsWith("\\ No newline")) {
// New diff section or metadata — skip
if (line.startsWith("diff ")) inHunk = false;
continue;
}
result.push(line);
}
}
return result;
}
/**
* Slice textual diff hunks to only include lines within a line range.
* Returns only hunks that overlap with [lineStart, lineEnd].
*/
function sliceHunksToSpan(diffLines: string[], lineStart: number, lineEnd: number): string[] {
const result: string[] = [];
let afterLineNo = 0;
let inRange = false;
for (const line of diffLines) {
if (line.startsWith("@@")) {
// Parse hunk header: @@ -a,b +c,d @@
const match = line.match(/@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
if (match) {
afterLineNo = parseInt(match[1], 10);
// Check if this hunk overlaps with our range
inRange = false; // Will be set per-line
continue;
}
}
if (line.startsWith("+")) {
if (afterLineNo >= lineStart && afterLineNo <= lineEnd) {
if (!inRange) {
result.push(`@@ line ${afterLineNo} @@`);
inRange = true;
}
result.push(line);
}
afterLineNo++;
} else if (line.startsWith("-")) {
// Removed lines: if we're in range, include them
if (afterLineNo >= lineStart && afterLineNo <= lineEnd + 1) {
if (!inRange) {
result.push(`@@ line ${afterLineNo} @@`);
inRange = true;
}
result.push(line);
}
} else {
// Context line
if (afterLineNo >= lineStart && afterLineNo <= lineEnd) {
if (inRange) {
result.push(line);
}
} else {
inRange = false;
}
afterLineNo++;
}
}
return result;
}
// ── Source span reading (for graph-driven diff) ─────────────────────
/** Try to read source lines from disk using entity provenance and line spans. */
function readSourceSpan(node: any): string | null {
if (!node) return null;
const uri: string = node.provenance?.source_uri ?? node.provenance?.sourceUri ?? "";
if (!uri) return null;
const attrs = node.attrs ?? {};
const lineStart = attrs.line_start ?? attrs.lineStart;
const lineEnd = attrs.line_end ?? attrs.lineEnd;
if (lineStart == null || lineEnd == null) return null;
const filePath = absoluteFromSourceUri(uri);
if (!isReadablePath(filePath)) return null;
try {
if (!fs.existsSync(filePath)) return null;
const content = fs.readFileSync(filePath, "utf-8");
const lines = content.split("\n");
const start = Math.max(0, Number(lineStart) - 1);
const end = Math.min(lines.length, Number(lineEnd));
if (start >= end) return null;
return lines.slice(start, end).join("\n");
} catch {
return null;
}
}
/**
* Read source span at a historical point in time using git.
* Falls back to readSourceSpan (current disk) if git fails.
*/
export async function readSourceSpanAtTimestamp(node: any): Promise<string | null> {
if (!node) return null;
const uri: string = node.provenance?.source_uri ?? node.provenance?.sourceUri ?? "";
if (!uri) return null;
const attrs = node.attrs ?? {};
const lineStart = attrs.line_start ?? attrs.lineStart;
const lineEnd = attrs.line_end ?? attrs.lineEnd;
if (lineStart == null || lineEnd == null) return null;
const timestamp = node.updatedAt ?? node.createdAt;
if (!timestamp) return readSourceSpan(node);
try {
const fileContent = await loadFileAtTimestamp(uri, timestamp);
if (fileContent === null) return readSourceSpan(node);
const lines = fileContent.split("\n");
const start = Math.max(0, Number(lineStart) - 1);
const end = Math.min(lines.length, Number(lineEnd));
if (start >= end) return readSourceSpan(node);
return lines.slice(start, end).join("\n");
} catch {
return readSourceSpan(node);
}
}
// ── Get file URI from resolved entity or graph changes ──────────────
function getFileUri(resolved: ResolvedEntity | null, changes: any[]): string | null {
// From resolved entity path
if (resolved?.path) return resolved.path;
// From graph changes provenance
for (const c of changes) {
const node = c.atToRev ?? c.atFromRev;
const uri = node?.provenance?.source_uri ?? node?.provenance?.sourceUri;
if (uri) return uri;
}
return null;
}
function getLineSpan(resolved: ResolvedEntity | null, changes: any[]): { start: number; end: number } | null {
// Symbol targets (non-file kinds) have line spans
if (resolved && resolved.kind !== "file") {
for (const c of changes) {
const node = c.atToRev ?? c.atFromRev;
const attrs = node?.attrs ?? {};
const lineStart = attrs.line_start ?? attrs.lineStart;
const lineEnd = attrs.line_end ?? attrs.lineEnd;
if (lineStart != null && lineEnd != null) {
return { start: Number(lineStart), end: Number(lineEnd) };
}
}
}
return null;
}
/**
* Get line spans for both before and after revisions of a symbol.
* Returns null if the target is a file or spans can't be determined.
*/
function getSymbolSpans(resolved: ResolvedEntity | null, changes: any[]): {
before: { start: number; end: number } | null;
after: { start: number; end: number } | null;
} | null {
if (!resolved || resolved.kind === "file") return null;
let before: { start: number; end: number } | null = null;
let after: { start: number; end: number } | null = null;
for (const c of changes) {
if (c.atFromRev) {
const attrs = c.atFromRev.attrs ?? {};
const ls = attrs.line_start ?? attrs.lineStart;
const le = attrs.line_end ?? attrs.lineEnd;
if (ls != null && le != null) before = { start: Number(ls), end: Number(le) };
}
if (c.atToRev) {
const attrs = c.atToRev.attrs ?? {};
const ls = attrs.line_start ?? attrs.lineStart;
const le = attrs.line_end ?? attrs.lineEnd;
if (ls != null && le != null) after = { start: Number(ls), end: Number(le) };
}
}
if (!before && !after) return null;
return { before, after };
}
/**
* Extract a slice of lines from full file content.
* lineStart and lineEnd are 1-based inclusive.
*/
function sliceContent(content: string, lineStart: number, lineEnd: number): string {
const lines = content.split("\n");
const start = Math.max(0, lineStart - 1);
const end = Math.min(lines.length, lineEnd);
return lines.slice(start, end).join("\n");
}
// ── CLI command ─────────────────────────────────────────────────────
export function registerDiffCommand(program: Command): void {
program
.command("diff <fromRev> <toRev> [target]")
.description("Show diff between two revisions, optionally scoped to a file or entity")
.option("--entity <id>", "Filter by entity ID (deprecated, use positional target)")
.option("--summary", "Show compact summary only (server-side, fast)")
.option("--content", "Show detailed attribute changes for each entity")
.option("--limit <n>", "Max changes to return (default 100)")
.option("--full", `Return all changes (up to the backend's maximum, ${DIFF_MAX_LIMIT})`)
.option("--all", "Diff every workspace on the backend, not just this one")
.option("--format <fmt>", "Output format (text|json|llm)", "text")
.option("--kind <kind>", "Filter target entity by kind")
.option("--path <path>", "Restrict to symbols from files matching this path substring")
.option("--pick <n>", "Pick Nth candidate from ambiguous results (1-based)", parsePickOption)
.addHelpText("after", `\nExamples:
ix diff 3 5
ix diff 3 5 ix-cli/src/cli/commands/decide.ts
ix diff 3 5 decide.ts --content
ix diff 3 5 buildDecisionPatch --content --pick 1
ix diff 3 5 --summary`)
.action(async (fromRev: string, toRev: string, target: string | undefined, opts: {
entity?: string; summary?: boolean; content?: boolean; limit?: string; full?: boolean; format: string;
kind?: string; path?: string; pick?: number; all?: boolean;
}) => {
const conflict = detectDiffModeConflict(opts);
if (conflict) {
reportFailure("mode_conflict", conflict, opts.format);
return;
}
const client = createClient();
const workspaceId = diffWorkspaceScope(opts.all);
const from = parseInt(fromRev, 10);
const to = parseInt(toRev, 10);
// Resolve the scope entity from positional target or --entity flag
const resolveOpts = {
kind: opts.kind,
path: opts.path,
pick: opts.pick,
};
let entityId: string | undefined = opts.entity;
let resolved: ResolvedEntity | null = null;
if (target && !entityId) {
resolved = await resolveFileOrReport(client, target, resolveOpts, opts.format);
if (!resolved) return;
if (opts.format === "text") printResolved(resolved);
entityId = resolved.id;
}
if (opts.summary) {
const result: any = await client.diff(from, to, { summary: true, entityId, workspaceId });
if (opts.format === "json") {
printJson(compactDiffResult(result));
} else if (opts.format === "llm") {
console.log(renderDiffSummaryLlm(result));
} else {
const s = result.summary || {};
console.log(chalk.cyan.bold(`\nDiff: rev ${result.fromRev} → ${result.toRev}`));
if (s.added) console.log(` ${chalk.green("Added:")} ${s.added}`);
if (s.modified) console.log(` ${chalk.yellow("Modified:")} ${s.modified}`);
if (s.removed) console.log(` ${chalk.red("Removed:")} ${s.removed}`);
if (!s.added && !s.modified && !s.removed) console.log(chalk.dim(" No changes."));
console.log(` ${chalk.dim("Total:")} ${result.total}`);
console.log();
}
return;
}
const limit = diffRequestLimit(opts);
const result: any = await client.diff(from, to, { entityId, limit, workspaceId });
const changes: any[] = result.changes ?? [];
// ── Content mode: textual diff is primary ───────────────────────
if (opts.content) {
// Try to get file URI for textual diffing
const fileUri = getFileUri(resolved, changes);
if (fileUri) {
// Load full file content at both revisions
const beforeFile = await loadContentAtRev(client, fileUri, from, changes, "from");
const afterFile = await loadContentAtRev(client, fileUri, to, changes, "to");
if (beforeFile !== null && afterFile !== null) {
const symbolSpans = getSymbolSpans(resolved, changes);
let diffLines: string[];
let diffMode: string;
let displaySpan: { start: number; end: number } | null = null;
if (symbolSpans) {
// Symbol target: extract symbol body at each revision and diff those
const beforeBody = symbolSpans.before
? sliceContent(beforeFile, symbolSpans.before.start, symbolSpans.before.end)
: "";
const afterBody = symbolSpans.after
? sliceContent(afterFile, symbolSpans.after.start, symbolSpans.after.end)
: "";
diffLines = await computeTextualDiff(beforeBody, afterBody);
diffMode = "symbol";
displaySpan = symbolSpans.after ?? symbolSpans.before;
} else {
// File target: diff the whole file
diffLines = await computeTextualDiff(beforeFile, afterFile);
diffMode = "file";
}
if (opts.format === "json") {
const jsonResult: any = {
...compactDiffResult(result),
textualDiff: diffLines,
hasTextualChanges: diffLines.length > 0,
diffMode,
};
if (displaySpan) {
jsonResult.lineSpan = displaySpan;
}
printJson(jsonResult);
} else {
console.log(chalk.cyan.bold(`\nDiff: rev ${from} → ${to}`));
if (diffLines.length === 0) {
console.log(chalk.dim(" No textual changes detected."));
} else {
console.log(` ${chalk.dim("file:")} ${fileUri}`);
if (displaySpan) {
console.log(` ${chalk.dim(`scoped to lines ${displaySpan.start}-${displaySpan.end}`)}`);
}
console.log();
for (const line of diffLines) {
if (line.startsWith("@@")) {
console.log(` ${chalk.cyan(line)}`);
} else if (line.startsWith("+")) {
console.log(` ${chalk.green(line)}`);
} else if (line.startsWith("-")) {
console.log(` ${chalk.red(line)}`);
} else {
console.log(` ${line}`);
}
}
}
// Also show graph-level changes if any
if (changes.length > 0) {
console.log(chalk.dim(`\n Graph changes: ${changes.length}`));
for (const c of changes) {
const name = c.atToRev?.name ?? c.atFromRev?.name ?? "?";
const kind = c.atToRev?.kind ?? c.atFromRev?.kind ?? "unknown";
const typeChar = c.changeType === "added" ? "+" : c.changeType === "removed" ? "-" : "~";
console.log(` ${chalk.dim(typeChar)} ${chalk.dim(kind)} ${chalk.dim(name)}`);
}
}
console.log();
}
return;
}
}
// Fallback: graph-driven content diff (no file URI or git failed)
if (opts.format === "json") {
for (const c of changes) {
const beforeSource = await readSourceSpanAtTimestamp(c.atFromRev);
const afterSource = readSourceSpan(c.atToRev);
if (beforeSource !== null) c.sourceContentBefore = beforeSource;
if (afterSource !== null) c.sourceContentAfter = afterSource;
if (beforeSource !== null && afterSource !== null) {
c.sourceDiff = await computeTextualDiff(beforeSource, afterSource);
} else if (beforeSource !== null || afterSource !== null) {
c.sourceContent = afterSource ?? beforeSource;
}
}
printJson(compactDiffResult(result));
} else {
await formatDiffContent(result);
}
return;
}
// ── Non-content mode ────────────────────────────────────────────
// If target is a file/symbol and graph shows no changes,
// check for textual changes as a supplementary signal.
if (resolved && changes.length === 0) {
const fileUri = getFileUri(resolved, changes) ?? resolved.path;
if (fileUri) {
const beforeContent = await loadContentAtRev(client, fileUri, from, changes, "from");
const afterContent = await loadContentAtRev(client, fileUri, to, changes, "to");
if (beforeContent !== null && afterContent !== null && beforeContent !== afterContent) {
const diffLines = await computeTextualDiff(beforeContent, afterContent);
const changeCount = diffLines.filter(l => l.startsWith("+") || l.startsWith("-")).length;
if (opts.format === "json") {
printJson({
...compactDiffResult(result),
textualChanges: {
detected: true,
lineChanges: changeCount,
note: "Textual changes detected but not captured by graph parser (e.g. comments, whitespace).",
},
});
} else {
const name = resolved.name ?? fileUri;
console.log(chalk.yellow(`${name} modified (${changeCount} textual changes — not captured by parser)`));
console.log(chalk.dim(" Use --content to see full diff."));
}
return;
}
}
}
if (opts.format === "json") {
printJson(compactDiffResult(result));
} else if (opts.format === "llm") {
formatDiff(result, "llm");
} else {
if (result.truncated) {
console.log(chalk.yellow(`${diffTruncationNote(result.changes.length, result.totalChanges, opts.full)}\n`));
}
formatDiff(result, "text");
}
});
}
/**
* Resolve the "before" timestamp for textual diffing.
* Tries: graph change snapshots → patch list → null.
*/
async function resolveFromTimestamp(
client: IxClient,
fromRev: number,
changes: any[],
): Promise<string | null> {
// Try to get timestamp from graph change snapshots
for (const c of changes) {
const ts = c.atFromRev?.updatedAt ?? c.atFromRev?.createdAt;
if (ts) return ts;
}
// Fall back to patch list
return getRevisionTimestamp(client, fromRev);
}
/**
* Resolve the "after" timestamp for textual diffing.
* Tries: graph change snapshots → patch list → null.
* Returns null if toRev is the latest revision (caller should use disk).
*/
async function resolveToTimestamp(
client: IxClient,
toRev: number,
changes: any[],
): Promise<string | null> {
// Check if toRev is the latest revision
try {
const patches = await client.listPatches({ limit: 1 });
if (patches.length > 0 && (patches[0] as any).rev === toRev) {
return null; // Latest rev — use disk
}
} catch {}
// Try to get timestamp from graph change snapshots
for (const c of changes) {
const ts = c.atToRev?.updatedAt ?? c.atToRev?.createdAt;
if (ts) return ts;
}
// Fall back to patch list
return getRevisionTimestamp(client, toRev);
}
/**
* Load file content for a specific revision.
* Uses disk for latest revision, git for historical revisions.
*/
async function loadContentAtRev(
client: IxClient,
fileUri: string,
rev: number,
changes: any[],
side: "from" | "to",
): Promise<string | null> {
if (side === "from") {
const ts = await resolveFromTimestamp(client, rev, changes);
return ts ? loadFileAtTimestamp(fileUri, ts) : loadFileFromDisk(fileUri);
} else {
const ts = await resolveToTimestamp(client, rev, changes);
// null timestamp = latest rev = use disk
return ts ? loadFileAtTimestamp(fileUri, ts) : loadFileFromDisk(fileUri);
}
}
// ── Graph-driven content display (fallback) ─────────────────────────
/** Render detailed attribute-level changes with source code for each entity. */
async function formatDiffContent(result: any): Promise<void> {
console.log(chalk.cyan.bold(`\nDiff: rev ${result.fromRev} → ${result.toRev}`));
const changes = result.changes ?? [];
if (changes.length === 0) {
console.log(chalk.dim(" No changes in this range."));
return;
}
for (const c of changes) {
const before = c.atFromRev;
const after = c.atToRev;
const name = after?.name ?? before?.name ?? c.entityId?.substring(0, 8) ?? "?";
const kind = after?.kind ?? before?.kind ?? "unknown";
const typeChar = c.changeType === "added" ? "+" : c.changeType === "removed" ? "-" : "~";
const typeColor = c.changeType === "added" ? chalk.green : c.changeType === "removed" ? chalk.red : chalk.yellow;
console.log(`\n${typeColor(typeChar)} ${chalk.cyan(kind)} ${chalk.bold(name)}`);
const activeNode = after ?? before;
const uri = activeNode?.provenance?.source_uri ?? activeNode?.provenance?.sourceUri ?? "";
if (uri) console.log(` ${chalk.dim("file:")} ${uri}`);
if (c.changeType === "added" && after) {
printAttrs(" ", after.attrs);
} else if (c.changeType === "modified" && before && after) {
diffAttrs(" ", before.attrs ?? {}, after.attrs ?? {});
}
// Textual diff using git for historical before content
const beforeSource = await readSourceSpanAtTimestamp(before);
const afterSource = readSourceSpan(after);
if (beforeSource !== null && afterSource !== null && c.changeType === "modified") {
const diffLines = await computeTextualDiff(beforeSource, afterSource);
if (diffLines.length > 0) {
console.log(` ${chalk.dim("source diff:")}`);
for (const line of diffLines) {
if (line.startsWith("@@")) {
console.log(` ${chalk.dim("│")} ${chalk.cyan(line)}`);
} else if (line.startsWith("+")) {
console.log(` ${chalk.dim("│")} ${chalk.green(line)}`);
} else if (line.startsWith("-")) {
console.log(` ${chalk.dim("│")} ${chalk.red(line)}`);
} else {
console.log(` ${chalk.dim("│")} ${line}`);
}
}
} else {
console.log(` ${chalk.dim("(no textual changes in source span)")}`);
}
} else {
// Single-revision fallback
const source = readSourceSpan(activeNode);
if (source) {
const lineStart = (activeNode.attrs?.line_start ?? activeNode.attrs?.lineStart) || "?";
console.log(` ${chalk.dim(`source (L${lineStart}):`)} `);
for (const line of source.split("\n")) {
console.log(` ${chalk.dim("│")} ${line}`);
}
}
}
}
console.log();
}
function printAttrs(indent: string, attrs: Record<string, unknown> | undefined): void {
if (!attrs) return;
for (const [key, val] of Object.entries(attrs)) {
if (val === null || val === undefined) continue;
const display = typeof val === "string" ? val : JSON.stringify(val);
console.log(`${indent}${chalk.dim(key + ":")} ${display}`);
}
}
function diffAttrs(indent: string, before: Record<string, unknown>, after: Record<string, unknown>): void {
const allKeys = new Set([...Object.keys(before), ...Object.keys(after)]);
for (const key of allKeys) {
const bVal = before[key];
const aVal = after[key];
if (bVal === undefined && aVal !== undefined) {
const display = typeof aVal === "string" ? aVal : JSON.stringify(aVal);
console.log(`${indent}${chalk.green("+")} ${chalk.dim(key + ":")} ${display}`);
} else if (bVal !== undefined && aVal === undefined) {
const display = typeof bVal === "string" ? bVal : JSON.stringify(bVal);
console.log(`${indent}${chalk.red("-")} ${chalk.dim(key + ":")} ${display}`);
} else if (JSON.stringify(bVal) !== JSON.stringify(aVal)) {
const bDisplay = typeof bVal === "string" ? bVal : JSON.stringify(bVal);
const aDisplay = typeof aVal === "string" ? aVal : JSON.stringify(aVal);
console.log(`${indent}${chalk.yellow("~")} ${chalk.dim(key + ":")} ${chalk.red(String(bDisplay))} → ${chalk.green(String(aDisplay))}`);
}
}
}