-
Notifications
You must be signed in to change notification settings - Fork 6.4k
core: add DiffOverlay type + persistence + blast-radius computation #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeroaltitude
wants to merge
1
commit into
Egonex-AI:main
Choose a base branch
from
zeroaltitude:feat/diff-overlay-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
understand-anything-plugin/packages/core/src/__tests__/diff-overlay-persistence.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
| import { mkdirSync, rmSync, existsSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { tmpdir } from "node:os"; | ||
| import { saveDiffOverlay, loadDiffOverlay } from "../persistence/index.js"; | ||
| import type { DiffOverlay } from "../types.js"; | ||
|
|
||
| const testRoot = join(tmpdir(), "ua-diff-overlay-persist-test"); | ||
|
|
||
| const overlay: DiffOverlay = { | ||
| version: "1.0.0", | ||
| baseBranch: "main", | ||
| generatedAt: "2026-04-01T00:00:00.000Z", | ||
| changedFiles: ["src/auth.ts"], | ||
| changedNodeIds: ["file:src/auth.ts"], | ||
| affectedNodeIds: ["file:src/session.ts"], | ||
| }; | ||
|
|
||
| describe("diff overlay persistence", () => { | ||
| beforeEach(() => { | ||
| if (existsSync(testRoot)) rmSync(testRoot, { recursive: true }); | ||
| mkdirSync(testRoot, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (existsSync(testRoot)) rmSync(testRoot, { recursive: true }); | ||
| }); | ||
|
|
||
| it("saves and loads a diff overlay", () => { | ||
| saveDiffOverlay(testRoot, overlay); | ||
| const loaded = loadDiffOverlay(testRoot); | ||
| expect(loaded).not.toBeNull(); | ||
| expect(loaded!.changedNodeIds).toEqual(["file:src/auth.ts"]); | ||
| expect(loaded!.affectedNodeIds).toEqual(["file:src/session.ts"]); | ||
| expect(loaded!.baseBranch).toBe("main"); | ||
| }); | ||
|
|
||
| it("returns null when no diff overlay exists", () => { | ||
| expect(loadDiffOverlay(testRoot)).toBeNull(); | ||
| }); | ||
|
|
||
| it("saves to diff-overlay.json, alongside but distinct from knowledge-graph.json", () => { | ||
| saveDiffOverlay(testRoot, overlay); | ||
| expect(existsSync(join(testRoot, ".ua", "diff-overlay.json"))).toBe(true); | ||
| expect(existsSync(join(testRoot, ".ua", "knowledge-graph.json"))).toBe(false); | ||
| }); | ||
|
|
||
| it("sanitizes absolute changedFiles paths to stay relative to projectRoot", () => { | ||
| const absoluteOverlay: DiffOverlay = { | ||
| ...overlay, | ||
| changedFiles: [join(testRoot, "src", "auth.ts")], | ||
| }; | ||
| saveDiffOverlay(testRoot, absoluteOverlay); | ||
| const loaded = loadDiffOverlay(testRoot); | ||
| expect(loaded!.changedFiles).toEqual(["src/auth.ts"]); | ||
| }); | ||
|
|
||
| it("returns null (not throw) on a corrupt diff-overlay.json", () => { | ||
| mkdirSync(join(testRoot, ".ua"), { recursive: true }); | ||
| writeFileSync(join(testRoot, ".ua", "diff-overlay.json"), "not json", "utf-8"); | ||
| expect(loadDiffOverlay(testRoot)).toBeNull(); | ||
| }); | ||
| }); |
61 changes: 61 additions & 0 deletions
61
understand-anything-plugin/packages/core/src/__tests__/diff-overlay.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { computeDiffOverlay } from "../diff-overlay.js"; | ||
| import type { KnowledgeGraph } from "../types.js"; | ||
|
|
||
| function makeGraph(): KnowledgeGraph { | ||
| return { | ||
| version: "1.0.0", | ||
| project: { name: "p", languages: ["typescript"], frameworks: [], description: "d", analyzedAt: "2026-01-01T00:00:00.000Z", gitCommitHash: "x" }, | ||
| nodes: [ | ||
| { id: "file:auth.ts", type: "file", name: "auth.ts", filePath: "auth.ts", summary: "Auth.", tags: [], complexity: "moderate" }, | ||
| { id: "function:auth.ts:login", type: "function", name: "login", filePath: "auth.ts", summary: "Login fn.", tags: [], complexity: "moderate" }, | ||
| { id: "file:session.ts", type: "file", name: "session.ts", filePath: "session.ts", summary: "Sessions, imports auth.", tags: [], complexity: "simple" }, | ||
| { id: "file:unrelated.ts", type: "file", name: "unrelated.ts", filePath: "unrelated.ts", summary: "Nothing to do with auth.", tags: [], complexity: "simple" }, | ||
| ], | ||
| edges: [ | ||
| // session.ts imports auth.ts -> session.ts should be "affected" when auth.ts changes | ||
| { source: "file:session.ts", target: "file:auth.ts", type: "imports", direction: "forward", weight: 0.7 }, | ||
| ], | ||
| layers: [], | ||
| tour: [], | ||
| }; | ||
| } | ||
|
|
||
| describe("computeDiffOverlay", () => { | ||
| it("maps changed files to both file and function/class nodes defined in them", () => { | ||
| const overlay = computeDiffOverlay(makeGraph(), ["auth.ts"]); | ||
| expect(overlay.changedNodeIds.sort()).toEqual(["file:auth.ts", "function:auth.ts:login"]); | ||
| }); | ||
|
|
||
| it("finds affected nodes via 1-hop edges in either direction, excluding changed nodes themselves", () => { | ||
| const overlay = computeDiffOverlay(makeGraph(), ["auth.ts"]); | ||
| expect(overlay.affectedNodeIds).toEqual(["file:session.ts"]); | ||
| expect(overlay.affectedNodeIds).not.toContain("file:auth.ts"); | ||
| }); | ||
|
|
||
| it("does not flag unrelated files as affected", () => { | ||
| const overlay = computeDiffOverlay(makeGraph(), ["auth.ts"]); | ||
| expect(overlay.affectedNodeIds).not.toContain("file:unrelated.ts"); | ||
| }); | ||
|
|
||
| it("returns empty changed/affected sets for a file not present in the graph", () => { | ||
| const overlay = computeDiffOverlay(makeGraph(), ["does-not-exist.ts"]); | ||
| expect(overlay.changedNodeIds).toEqual([]); | ||
| expect(overlay.affectedNodeIds).toEqual([]); | ||
| }); | ||
|
|
||
| it("includes baseBranch when provided, omits it when not", () => { | ||
| const withBranch = computeDiffOverlay(makeGraph(), ["auth.ts"], "main"); | ||
| expect(withBranch.baseBranch).toBe("main"); | ||
|
|
||
| const withoutBranch = computeDiffOverlay(makeGraph(), ["auth.ts"]); | ||
| expect(withoutBranch.baseBranch).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("preserves the changedFiles list verbatim and stamps a generatedAt timestamp", () => { | ||
| const overlay = computeDiffOverlay(makeGraph(), ["auth.ts", "does-not-exist.ts"]); | ||
| expect(overlay.changedFiles).toEqual(["auth.ts", "does-not-exist.ts"]); | ||
| expect(typeof overlay.generatedAt).toBe("string"); | ||
| expect(new Date(overlay.generatedAt).toString()).not.toBe("Invalid Date"); | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
understand-anything-plugin/packages/core/src/diff-overlay.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { DiffOverlay, KnowledgeGraph } from "./types.js"; | ||
|
|
||
| /** | ||
| * Maps a set of changed file paths onto an already-analyzed knowledge graph: | ||
| * finds the nodes defined in those files (file nodes plus any function/class | ||
| * nodes whose `filePath` matches, since graph-builder stamps the same | ||
| * filePath on both), then walks one hop of edges in both directions to find | ||
| * the blast radius — what imports/calls the changed nodes, and what they in | ||
| * turn import/call. Pure and deterministic; no LLM involved. | ||
| * | ||
| * `changedFiles` should be paths relative to the project root, matching how | ||
| * node.filePath is stored (see persistence's sanitiseFilePath). | ||
| */ | ||
| export function computeDiffOverlay( | ||
| graph: KnowledgeGraph, | ||
| changedFiles: string[], | ||
| baseBranch?: string, | ||
| ): DiffOverlay { | ||
| const changedFileSet = new Set(changedFiles); | ||
|
|
||
| const changedNodeIds = graph.nodes | ||
| .filter((n) => typeof n.filePath === "string" && changedFileSet.has(n.filePath)) | ||
| .map((n) => n.id); | ||
| const changedNodeIdSet = new Set(changedNodeIds); | ||
|
|
||
| const affectedNodeIdSet = new Set<string>(); | ||
| for (const edge of graph.edges) { | ||
| if (changedNodeIdSet.has(edge.source) && !changedNodeIdSet.has(edge.target)) { | ||
| affectedNodeIdSet.add(edge.target); | ||
| } | ||
| if (changedNodeIdSet.has(edge.target) && !changedNodeIdSet.has(edge.source)) { | ||
| affectedNodeIdSet.add(edge.source); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| version: "1.0.0", | ||
| ...(baseBranch ? { baseBranch } : {}), | ||
| generatedAt: new Date().toISOString(), | ||
| changedFiles, | ||
| changedNodeIds, | ||
| affectedNodeIds: [...affectedNodeIdSet], | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
saveDiffOverlayreceives an absolutechangedFilesentry that is outsideprojectRootbut merely shares its string prefix (for exampleprojectRoot=/tmp/appand/tmp/app-old/src/a.ts), the sanitizer treats it as in-project and writes a../app-old/src/a.tspath into.ua/diff-overlay.jsoninstead of falling back to the basename. That violates the outside-path privacy contract and can expose sibling directory layout; the relativization should only run after a real path-boundary check.Useful? React with 👍 / 👎.