Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: semantic diff with added/modified/removed classification and summaries

riley0227d67d1d3addbc
diff --git a/ix-cli/src/cli/commands/diff.ts b/ix-cli/src/cli/commands/diff.ts
index 3f4472b7..9a0101a8 100644
--- a/ix-cli/src/cli/commands/diff.ts
+++ b/ix-cli/src/cli/commands/diff.ts
@@ -9,10 +9,28 @@ export function registerDiffCommand(program: Command): void {
.command("diff <fromRev> <toRev>")
.description("Show diff between two revisions")
.option("--entity <id>", "Filter by entity ID")
+ .option("--summary", "Show compact summary only")
.option("--format <fmt>", "Output format (text|json)", "text")
- .action(async (fromRev: string, toRev: string, opts: { entity?: string; format: string }) => {
+ .action(async (fromRev: string, toRev: string, opts: { entity?: string; summary?: boolean; format: string }) => {
const client = new IxClient(getEndpoint());
- const result = await client.diff(parseInt(fromRev, 10), parseInt(toRev, 10), opts.entity);
+ const result: any = await client.diff(parseInt(fromRev, 10), parseInt(toRev, 10), opts.entity);
+
+ if (opts.summary && opts.format !== "json") {
+ const changes = result.changes || [];
+ const added = changes.filter((c: any) => c.changeType === "added").length;
+ const modified = changes.filter((c: any) => c.changeType === "modified").length;
+ const removed = changes.filter((c: any) => c.changeType === "removed").length;
+ const legacy = changes.filter((c: any) => c.changeType === "added_or_modified").length;
+ console.log(chalk.cyan.bold(`\nDiff: rev ${result.fromRev} → ${result.toRev}`));
+ if (added) console.log(` ${chalk.green("Added:")} ${added}`);
+ if (modified) console.log(` ${chalk.yellow("Modified:")} ${modified}`);
+ if (removed) console.log(` ${chalk.red("Removed:")} ${removed}`);
+ if (legacy) console.log(` ${chalk.yellow("Changed:")} ${legacy}`);
+ if (!added && !modified && !removed && !legacy) console.log(chalk.dim(" No changes."));
+ console.log();
+ return;
+ }
+
if (opts.format === "json") {
console.log(JSON.stringify(result, null, 2));
} else {
diff --git a/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
index fce9a99e..e81dcdb7 100644
--- a/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
+++ b/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
@@ -20,10 +20,11 @@ object DiffRequest {
}
case class DiffEntry(
- entityId: NodeId,
+ entityId: NodeId,
changeType: String, // "added", "removed", "modified"
atFromRev: Option[GraphNode],
- atToRev: Option[GraphNode]
+ atToRev: Option[GraphNode],
+ summary: Option[String]
)
object DiffEntry {
@@ -53,13 +54,20 @@ class DiffRoutes(queryApi: GraphQueryApi) {
result <- diffEntity(eid, Rev(body.fromRev), Rev(body.toRev))
} yield result.toVector
case None =>
- queryApi.getChangedEntities(Rev(body.fromRev), Rev(body.toRev)).map { nodes =>
- nodes.map { n =>
+ queryApi.getChangedEntities(Rev(body.fromRev), Rev(body.toRev)).map { pairs =>
+ pairs.map { case (atTo, atFromOpt) =>
+ val changeType = if (atFromOpt.isEmpty) "added"
+ else if (atTo.deletedRev.isDefined) "removed"
+ else "modified"
+ val summary = atFromOpt.map { atFrom =>
+ diffAttrs(atFrom.attrs, atTo.attrs)
+ }
DiffEntry(
- entityId = n.id,
- changeType = if (n.deletedRev.isDefined) "removed" else "added_or_modified",
- atFromRev = None,
- atToRev = Some(n)
+ entityId = atTo.id,
+ changeType = changeType,
+ atFromRev = atFromOpt,
+ atToRev = Some(atTo),
+ summary = summary.filter(_.nonEmpty)
)
}
}
@@ -77,14 +85,30 @@ class DiffRoutes(queryApi: GraphQueryApi) {
} yield {
(atFrom, atTo) match {
case (None, None) => None
- case (None, Some(_)) => Some(DiffEntry(nodeId, "added", None, atTo))
- case (Some(_), None) => Some(DiffEntry(nodeId, "removed", atFrom, None))
+ case (None, Some(_)) => Some(DiffEntry(nodeId, "added", None, atTo, None))
+ case (Some(_), None) => Some(DiffEntry(nodeId, "removed", atFrom, None, None))
case (Some(a), Some(b)) =>
if (a.updatedAt != b.updatedAt || a.attrs != b.attrs)
- Some(DiffEntry(nodeId, "modified", Some(a), Some(b)))
+ Some(DiffEntry(nodeId, "modified", Some(a), Some(b), Some(diffAttrs(a.attrs, b.attrs)).filter(_.nonEmpty)))
else
None
}
}
+ private def diffAttrs(from: io.circe.Json, to: io.circe.Json): String = {
+ val fromFields = from.asObject.map(_.keys.toSet).getOrElse(Set.empty)
+ val toFields = to.asObject.map(_.keys.toSet).getOrElse(Set.empty)
+ val added = (toFields -- fromFields).size
+ val removed = (fromFields -- toFields).size
+ val changed = (fromFields intersect toFields).count { k =>
+ from.hcursor.downField(k).focus != to.hcursor.downField(k).focus
+ }
+ val parts = List(
+ if (added > 0) Some(s"+$added attrs") else None,
+ if (removed > 0) Some(s"-$removed attrs") else None,
+ if (changed > 0) Some(s"~$changed attrs") else None
+ ).flatten
+ parts.mkString(", ")
+ }
+
}
diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
index bd5fa4cd..a3ee6cba 100644
--- a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
+++ b/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
@@ -4,6 +4,7 @@ import java.time.Instant
import java.util.UUID
import cats.effect.IO
+import cats.syntax.all._
import io.circe.Json
import org.slf4j.LoggerFactory
@@ -282,18 +283,27 @@ class ArangoGraphQueryApi(client: ArangoClient) extends GraphQueryApi {
)
).map(_.toVector)
- override def getChangedEntities(fromRev: Rev, toRev: Rev): IO[Vector[GraphNode]] =
- client.query(
- """FOR n IN nodes
- | FILTER n.created_rev > @fromRev AND n.created_rev <= @toRev
- | COLLECT logicalId = n.logical_id INTO group
- | LET latest = LAST(group[*].n)
- | RETURN latest""".stripMargin,
- Map(
+ override def getChangedEntities(fromRev: Rev, toRev: Rev): IO[Vector[(GraphNode, Option[GraphNode])]] = {
+ val changedIdsAql = """
+ FOR n IN nodes
+ FILTER n.created_rev > @fromRev AND n.created_rev <= @toRev
+ COLLECT logicalId = n.logical_id
+ RETURN logicalId
+ """
+ for {
+ logicalIds <- client.query(changedIdsAql, Map(
"fromRev" -> Long.box(fromRev.value).asInstanceOf[AnyRef],
"toRev" -> Long.box(toRev.value).asInstanceOf[AnyRef]
- )
- ).map(_.flatMap(parseNode).toVector)
+ )).map(_.flatMap(_.asString).toVector)
+ results <- logicalIds.traverse { lid =>
+ val nodeId = NodeId(java.util.UUID.fromString(lid))
+ for {
+ atFrom <- getNode(nodeId, asOfRev = Some(fromRev))
+ atTo <- getNode(nodeId, asOfRev = Some(toRev))
+ } yield atTo.map(to => (to, atFrom))
+ }
+ } yield results.flatten
+ }
override def resolvePrefix(prefix: String): IO[Vector[NodeId]] =
client.query(
diff --git a/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala b/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
index 9c8447d4..d1500e45 100644
--- a/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
+++ b/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
@@ -18,7 +18,7 @@ trait GraphQueryApi {
def getLatestRev: IO[Rev]
def getPatchesForEntity(entityId: NodeId): IO[List[Json]]
def getPatchesBySource(sourceUri: String, extractor: String): IO[Vector[Json]]
- def getChangedEntities(fromRev: Rev, toRev: Rev): IO[Vector[GraphNode]]
+ def getChangedEntities(fromRev: Rev, toRev: Rev): IO[Vector[(GraphNode, Option[GraphNode])]]
def resolvePrefix(prefix: String): IO[Vector[NodeId]]
def getSourceHashes(sourceUris: Seq[String]): IO[Map[String, String]]
}