Skip to content

armgit / Ix

Public Imported from GitHub
0

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

Extract Scala backend to private ix-memory-layer repo (#120)

Tanner Kocherbc62830d855d
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 8d83cccd..df5ff771 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -30,36 +30,3 @@ jobs:
- name: Test
working-directory: ix-cli
run: npm test
-
- backend:
- name: Backend (Scala)
- runs-on: ubuntu-latest
-
- services:
- arangodb:
- image: arangodb:3.12
- ports:
- - 8529:8529
- env:
- ARANGO_NO_AUTH: "1"
- options: >-
- --health-cmd "arangosh --server.authentication false --server.endpoint tcp://127.0.0.1:8529 --javascript.execute-string 'db._version()'"
- --health-interval 10s
- --health-timeout 10s
- --health-retries 10
- --health-start-period 30s
-
- steps:
- - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- - uses: actions/setup-java@c1e323688fd81a25caa38c78aa6df2d33d3e20d9 # v4.8.0
- with:
- java-version: "17"
- distribution: "temurin"
- cache: "sbt"
- - uses: sbt/setup-sbt@dd1ef7d7798fab5ce802a6adab3b782817b8c2d0 # v1.1.20
- - name: Compile
- run: sbt compile
- - name: Test
- run: sbt test
- - name: Assembly
- run: sbt "memoryLayer/assembly"
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 61d04dd7..a182204a 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -4,12 +4,14 @@ name: Release
# Triggered by pushing a version tag: git tag v0.3.0 && git push origin v0.3.0
#
# What it does (in order):
-# 1. Builds the Scala Memory Layer JAR (sbt assembly)
-# 2. Builds + pushes Docker image to ghcr.io
-# 3. Builds the TypeScript CLI
-# 4. Packages CLI tarballs for linux, macOS (amd64+arm64), Windows
-# 5. Creates a GitHub Release with all assets + install instructions
-# 6. Updates the Homebrew formula with the new SHA and commits it
+# 1. Builds the TypeScript CLI
+# 2. Packages CLI tarballs for linux, macOS (amd64+arm64), Windows
+# 3. Creates a GitHub Release with all assets + install instructions
+# 4. Updates the Homebrew formula with the new SHA and commits it
+#
+# Note: The Scala backend (memory-layer) is built and released separately
+# from the private ix-memory-layer repo. Docker images are pushed to
+# ghcr.io/ix-infrastructure/ix-memory-layer from that repo.
# ─────────────────────────────────────────────────────────────────────────────
on:
@@ -24,11 +26,6 @@ on:
permissions:
contents: write
- packages: write
-
-env:
- REGISTRY: ghcr.io
- IMAGE_NAME: ${{ github.repository_owner }}/ix-memory-layer
jobs:
build-and-release:
@@ -38,55 +35,16 @@ jobs:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
- # Need full history + write access for the Homebrew formula commit
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- # ── 1. Build Memory Layer JAR ───────────────────────────────────────
- - name: Set up JDK 17
- uses: actions/setup-java@c1e323688fd81a25caa38c78aa6df2d33d3e20d9 # v4.8.0
- with:
- java-version: "17"
- distribution: "temurin"
- cache: "sbt"
-
- - name: Set up sbt
- uses: sbt/setup-sbt@dd1ef7d7798fab5ce802a6adab3b782817b8c2d0 # v1.1.20
-
- - name: Build JAR
- run: sbt "memoryLayer/assembly"
-
- # ── 2. Build + Push Docker Image ────────────────────────────────────
- - name: Log in to GitHub Container Registry
- uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
- with:
- registry: ${{ env.REGISTRY }}
- username: ${{ github.actor }}
- password: ${{ secrets.GITHUB_TOKEN }}
-
- name: Extract version from tag
id: version
run: |
TAG="${{ github.event.inputs.tag || github.ref_name }}"
echo "version=${TAG#v}" >> "$GITHUB_OUTPUT"
- - name: Set up QEMU (for multi-platform builds)
- uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0
-
- - name: Set up Docker Buildx
- uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
-
- - name: Build and push Docker image
- uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
- with:
- context: ./memory-layer
- push: true
- platforms: linux/amd64,linux/arm64
- tags: |
- ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
- ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
-
- # ── 3. Build CLI ────────────────────────────────────────────────────
+ # ── 1. Build CLI ────────────────────────────────────────────────────
- name: Set up Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
@@ -105,7 +63,7 @@ jobs:
npm run build
npm version "${{ steps.version.outputs.version }}" --no-git-tag-version --allow-same-version
- # ── 3b. Build System Compass (optional — needs COMPASS_TOKEN secret) ─
+ # ── 1b. Build System Compass (optional — needs COMPASS_TOKEN secret) ─
- name: Build System Compass
continue-on-error: true
env:
@@ -125,7 +83,7 @@ jobs:
exit 1
fi
- # ── 4. Package CLI tarballs ─────────────────────────────────────────
+ # ── 2. Package CLI tarballs ─────────────────────────────────────────
- name: Package CLI (linux-amd64)
run: |
STAGING="ix-${{ steps.version.outputs.version }}-linux-amd64"
@@ -204,7 +162,7 @@ jobs:
zip -r "$STAGING.zip" "$STAGING"
shasum -a 256 "$STAGING.zip" > "$STAGING.zip.sha256"
- # ── 5. Create GitHub Release ────────────────────────────────────────
+ # ── 3. Create GitHub Release ────────────────────────────────────────
- name: Create Release
uses: softprops/action-gh-release@da05d552573ad5aba039eaac05058a918a7bf631 # v2.2.2
with:
@@ -229,7 +187,7 @@ jobs:
### Docker image
```bash
- docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
+ docker pull ghcr.io/ix-infrastructure/ix-memory-layer:latest
```
files: |
ix-${{ steps.version.outputs.version }}-linux-amd64.tar.gz
@@ -243,7 +201,7 @@ jobs:
ix-${{ steps.version.outputs.version }}-windows-amd64.zip
ix-${{ steps.version.outputs.version }}-windows-amd64.zip.sha256
- # ── 6. Update Homebrew formula via PR ──────────────────────────────
+ # ── 4. Update Homebrew formula via PR ──────────────────────────────
- name: Update Homebrew formula
continue-on-error: true
env:
diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml
index 96f4c62f..59008cbd 100644
--- a/.github/workflows/security.yml
+++ b/.github/workflows/security.yml
@@ -25,26 +25,4 @@ jobs:
exit-code: 1
scanners: vuln,misconfig
- trivy-image:
- name: Trivy (Docker image)
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- - uses: actions/setup-java@c1e323688fd81a25caa38c78aa6df2d33d3e20d9 # v4.8.0
- with:
- java-version: "17"
- distribution: "temurin"
- cache: "sbt"
- - uses: sbt/setup-sbt@dd1ef7d7798fab5ce802a6adab3b782817b8c2d0 # v1.1.20
- - name: Build JAR
- run: sbt "memoryLayer/assembly"
- - name: Build Docker image
- run: docker build -t ix-memory-layer:ci ./memory-layer
- - name: Trivy image scan
- uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1 # v0.35.0
- with:
- scan-type: image
- image-ref: ix-memory-layer:ci
- severity: CRITICAL,HIGH
- exit-code: 1
- scanners: vuln
+ # Docker image scanning is handled in the private ix-memory-layer repo
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index b1bc125f..88296185 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -5,7 +5,6 @@
### Prerequisites
- Node.js 20+
-- Java 17+ and sbt
- Docker and Docker Compose
### Local Setup
@@ -13,11 +12,10 @@
```bash
git clone https://github.com/ix-infrastructure/Ix.git
cd Ix
-./scripts/dev/setup.sh
+./scripts/backend.sh up # Start ArangoDB + Memory Layer (Docker)
+cd ix-cli && npm ci && npm run build
```
-This installs dependencies, builds the CLI and backend, and starts Docker services.
-
### Building from Source
```bash
@@ -30,10 +28,6 @@ npm run build
cd ../ix-cli
npm ci
npm run build
-
-# memory-layer (Scala backend)
-cd ..
-sbt memoryLayer/compile
```
### Verify Your Setup
@@ -41,12 +35,6 @@ sbt memoryLayer/compile
```bash
# CLI tests
cd ix-cli && npm test
-
-# Backend tests (requires ArangoDB on localhost:8529)
-cd .. && sbt memoryLayer/test
-
-# Smoke test (full stack)
-./scripts/dev/ix-smoke-test.sh
```
## Development Workflow
@@ -55,7 +43,7 @@ cd .. && sbt memoryLayer/test
2. Make your changes
3. Run tests locally
4. Open a PR using the pull request template
-5. Ensure CI passes — both CLI and backend jobs must be green before merge
+5. Ensure CI passes before merge
## Branch Naming
@@ -90,12 +78,8 @@ Breaking changes: use `feat!:` or `fix!:` prefix.
| What changed | Run |
|---|---|
| CLI code | `cd ix-cli && npm test` |
-| Backend code | `sbt memoryLayer/test` |
-| CLI behavior | `./scripts/dev/ix-smoke-test.sh` |
| Any change | Full test suite before opening PR |
-Run smoke tests for any CLI-facing change. Run backend tests for any Scala change.
-
## CLI Standards
### Output quality
@@ -121,6 +105,10 @@ PRs and pushes to `main` run automated security checks:
All checks fail on CRITICAL or HIGH severity findings. If a check fails on your PR, inspect the output and either fix the vulnerability or document why it's a false positive.
+## Backend Development
+
+The Scala backend (memory-layer) lives in a [separate private repo](https://github.com/ix-infrastructure/ix-memory-layer). For backend changes, clone that repo directly.
+
## OSS vs Pro Boundary
This repository contains open-source Ix functionality. Some features are available only in Ix Pro.
diff --git a/build.sbt b/build.sbt
deleted file mode 100644
index 1b129698..00000000
--- a/build.sbt
+++ /dev/null
@@ -1,57 +0,0 @@
-ThisBuild / scalaVersion := "2.13.16"
-ThisBuild / organization := "ix"
-ThisBuild / version := "0.1.0-SNAPSHOT"
-
-lazy val root = (project in file("."))
- .aggregate(memoryLayer)
- .settings(
- name := "ix-memory"
- )
-
-lazy val memoryLayer = (project in file("memory-layer"))
- .settings(
- name := "memory-layer",
-
- libraryDependencies ++= Seq(
- // Cats Effect
- "org.typelevel" %% "cats-effect" % "3.5.4",
-
- // Http4s
- "org.http4s" %% "http4s-ember-server" % "0.23.30",
- "org.http4s" %% "http4s-dsl" % "0.23.30",
- "org.http4s" %% "http4s-circe" % "0.23.30",
-
- // Circe
- "io.circe" %% "circe-core" % "0.14.10",
- "io.circe" %% "circe-generic" % "0.14.10",
- "io.circe" %% "circe-parser" % "0.14.10",
-
- // ArangoDB
- "com.arangodb" % "arangodb-java-driver" % "7.12.0",
-
- // Logging
- "org.typelevel" %% "log4cats-slf4j" % "2.7.0",
- "ch.qos.logback" % "logback-classic" % "1.5.6",
-
- // Test
- "org.scalatest" %% "scalatest" % "3.2.18" % Test,
- "org.typelevel" %% "cats-effect-testing-scalatest" % "1.5.0" % Test
- ),
-
- // Run tests sequentially to avoid ArangoDB write-write conflicts on shared DB
- Test / parallelExecution := false,
-
- // Assembly settings
- assembly / mainClass := Some("ix.memory.Main"),
- assembly / assemblyJarName := "ix-memory-layer.jar",
- assembly / assemblyMergeStrategy := {
- case PathList("META-INF", "services", _*) => MergeStrategy.concat
- case PathList("META-INF", "versions", _*) => MergeStrategy.first
- case PathList("META-INF", "vertx", _*) => MergeStrategy.first
- case PathList("META-INF", "native-image", _*) => MergeStrategy.first
- case PathList("META-INF", "MANIFEST.MF") => MergeStrategy.discard
- case PathList("META-INF", _*) => MergeStrategy.discard
- case "module-info.class" => MergeStrategy.discard
- case _ => MergeStrategy.first
- }
- )
diff --git a/docker-compose.yml b/docker-compose.yml
deleted file mode 100644
index f851361c..00000000
--- a/docker-compose.yml
+++ /dev/null
@@ -1,53 +0,0 @@
-services:
- arangodb:
- image: arangodb:3.12
- networks:
- - backend
- ports:
- - "127.0.0.1:8529:8529"
- environment:
- ARANGO_NO_AUTH: "1"
- volumes:
- - arangodb-data:/var/lib/arangodb3
- healthcheck:
- test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8529/_api/version || exit 1"]
- interval: 5s
- timeout: 5s
- start_period: 60s
- retries: 15
- restart: unless-stopped
-
- memory-layer:
- build: ./memory-layer
- networks:
- - backend
- ports:
- - "127.0.0.1:8090:8090"
- volumes:
- - type: bind
- source: ${IX_HOST_MOUNT_ROOT:-$HOME}
- target: ${IX_CONTAINER_MOUNT_ROOT:-$HOME}
- read_only: true
- environment:
- ARANGO_HOST: arangodb
- ARANGO_PORT: "8529"
- ARANGO_DATABASE: ix_memory
- ARANGO_USER: root
- ARANGO_PASSWORD: ""
- PORT: "8090"
- depends_on:
- arangodb:
- condition: service_healthy
- restart: unless-stopped
- healthcheck:
- test: ["CMD", "curl", "-sf", "http://localhost:8090/v1/health"]
- interval: 10s
- timeout: 3s
- start_period: 15s
- retries: 5
-
-volumes:
- arangodb-data:
-
-networks:
- backend:
diff --git a/homebrew/ix.rb b/homebrew/ix.rb
index 2347a6ca..dd96f5fc 100644
--- a/homebrew/ix.rb
+++ b/homebrew/ix.rb
@@ -15,6 +15,10 @@ class Ix < Formula
system "npm", "run", "build"
end
+ # Install core-ingestion runtime (parser + tree-sitter grammars)
+ # The CLI loads these at runtime via a relative path from dist/cli/commands/
+ (prefix/"core-ingestion").install Dir["core-ingestion/dist", "core-ingestion/node_modules", "core-ingestion/package.json"]
+
cd "ix-cli" do
system "npm", "install", "--silent"
system "npm", "run", "build"
diff --git a/memory-layer/src/test/resources/fixtures/api.ts b/ix-cli/test/fixtures/api.ts
similarity index 100%
rename from memory-layer/src/test/resources/fixtures/api.ts
rename to ix-cli/test/fixtures/api.ts
diff --git a/memory-layer/src/test/resources/fixtures/sample_project/billing_service.py b/ix-cli/test/fixtures/sample_project/billing_service.py
similarity index 100%
rename from memory-layer/src/test/resources/fixtures/sample_project/billing_service.py
rename to ix-cli/test/fixtures/sample_project/billing_service.py
diff --git a/ix-cli/test/parser.smoke.mjs b/ix-cli/test/parser.smoke.mjs
index 40fc09a1..680c8d43 100644
--- a/ix-cli/test/parser.smoke.mjs
+++ b/ix-cli/test/parser.smoke.mjs
@@ -19,7 +19,7 @@ function parseFixture(relativePath) {
}
const pythonResult = parseFixture(
- '../memory-layer/src/test/resources/fixtures/sample_project/billing_service.py'
+ 'test/fixtures/sample_project/billing_service.py'
);
assert(pythonResult, 'Expected Python fixture to parse');
assert(
@@ -36,7 +36,7 @@ assert(
);
const typeScriptResult = parseFixture(
- '../memory-layer/src/test/resources/fixtures/api.ts'
+ 'test/fixtures/api.ts'
);
assert(typeScriptResult, 'Expected TypeScript fixture to parse');
assert(
diff --git a/memory-layer/Dockerfile b/memory-layer/Dockerfile
deleted file mode 100644
index 8414f687..00000000
--- a/memory-layer/Dockerfile
+++ /dev/null
@@ -1,10 +0,0 @@
-FROM eclipse-temurin:17-jre-jammy
-RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*
-WORKDIR /app
-COPY target/scala-2.13/ix-memory-layer.jar app.jar
-EXPOSE 8090
-HEALTHCHECK --interval=10s --timeout=3s --start-period=15s --retries=3 \
- CMD curl -sf http://localhost:8090/v1/health || exit 1
-RUN addgroup --gid 1000 ix && adduser --uid 1000 --gid 1000 --disabled-password --gecos "" ix && chown ix:ix app.jar
-USER ix
-CMD ["java", "-jar", "app.jar"]
diff --git a/memory-layer/src/main/resources/logback.xml b/memory-layer/src/main/resources/logback.xml
deleted file mode 100644
index 845c3616..00000000
--- a/memory-layer/src/main/resources/logback.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-<configuration>
- <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
- <encoder>
- <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
- </encoder>
- </appender>
-
- <root level="INFO">
- <appender-ref ref="CONSOLE" />
- </root>
-</configuration>
diff --git a/memory-layer/src/main/scala/ix/memory/Main.scala b/memory-layer/src/main/scala/ix/memory/Main.scala
deleted file mode 100644
index 266a336b..00000000
--- a/memory-layer/src/main/scala/ix/memory/Main.scala
+++ /dev/null
@@ -1,70 +0,0 @@
-package ix.memory
-
-import cats.effect.{IO, IOApp, Resource}
-import com.comcast.ip4s._
-import org.http4s.ember.server.EmberServerBuilder
-import org.http4s.server.Router
-
-import ix.memory.api.Routes
-import ix.memory.conflict.ConflictService
-import ix.memory.context._
-import ix.memory.db._
-import ix.memory.map.MapService
-import ix.memory.savings.{NaiveCostEstimator, SavingsAccumulator, SavingsMiddleware}
-import ix.memory.smell.SmellService
-import ix.memory.subsystem.SubsystemScoringService
-
-object Main extends IOApp.Simple {
-
- override def run: IO[Unit] = {
- val serverResource = for {
- // 1. ArangoDB client
- client <- ArangoClient.resource(
- host = sys.env.getOrElse("ARANGO_HOST", "localhost"),
- port = sys.env.getOrElse("ARANGO_PORT", "8529").toInt,
- database = sys.env.getOrElse("ARANGO_DATABASE", "ix_memory"),
- user = sys.env.getOrElse("ARANGO_USER", "root"),
- password = sys.env.getOrElse("ARANGO_PASSWORD", "")
- )
- _ <- Resource.eval(client.ensureSchema())
-
- // 2. Core APIs
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
-
- // 3. Services
- bulkWriteApi = new BulkWriteApi(client)
- seeder = new GraphSeeder(queryApi)
- expander = new GraphExpander(queryApi)
- claimCollector = new ClaimCollector(queryApi)
- confidenceScorer = new ConfidenceScorerImpl()
- conflictDetector = new ConflictDetectorImpl()
- contextService = new ContextService(queryApi, seeder, expander,
- claimCollector, confidenceScorer, conflictDetector)
- conflictService = new ConflictService(client, queryApi, writeApi)
- mapService = new MapService(client, queryApi, writeApi)
- smellService = new SmellService(client, writeApi)
- subsystemService = new SubsystemScoringService(client, writeApi, mapService)
- naiveCostEstimator = new NaiveCostEstimator(queryApi)
- savingsAccumulator <- Resource.eval(SavingsAccumulator.create(client))
-
- // 4. HTTP routes
- rawRoutes = Routes.all(contextService, queryApi, writeApi, conflictService, client, mapService, bulkWriteApi, smellService, subsystemService, savingsAccumulator)
- routes = SavingsMiddleware(rawRoutes, savingsAccumulator, naiveCostEstimator)
-
- // 5. Server
- server <- EmberServerBuilder
- .default[IO]
- .withHost(host"0.0.0.0")
- .withPort(
- Port.fromInt(sys.env.getOrElse("PORT", "8090").toInt)
- .getOrElse(port"8090"))
- .withHttpApp(Router("/" -> routes).orNotFound)
- .build
- } yield server
-
- serverResource.use { server =>
- IO.println(s"Ix Memory Layer running at ${server.address}") *> IO.never
- }
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/BulkPatchCommitRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/BulkPatchCommitRoutes.scala
deleted file mode 100644
index b20a1981..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/BulkPatchCommitRoutes.scala
+++ /dev/null
@@ -1,52 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.{Decoder, Json}
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.{BulkWriteApi, FileBatch, GraphQueryApi}
-import ix.memory.model.GraphPatch
-
-class BulkPatchCommitRoutes(bulkWriteApi: BulkWriteApi, queryApi: GraphQueryApi) {
-
- private case class BulkPatchRequest(patches: List[GraphPatch])
- private implicit val reqDecoder: Decoder[BulkPatchRequest] = deriveDecoder
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "patches" / "bulk" =>
- (for {
- body <- req.as[BulkPatchRequest]
- _ <- if (body.patches.isEmpty)
- IO.raiseError(new IllegalArgumentException("patches array must not be empty"))
- else IO.unit
- fileBatches = body.patches.map { patch =>
- FileBatch(
- filePath = patch.source.uri,
- sourceHash = patch.source.sourceHash,
- patch = patch,
- provenance = buildProvenanceMap(patch)
- )
- }.toVector
- latestRev <- queryApi.getLatestRev
- result <- bulkWriteApi.commitBatchChunked(fileBatches, latestRev.value)
- resp <- Ok(Json.obj(
- "status" -> result.status.toString.asJson,
- "rev" -> result.newRev.value.asJson
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-
- private def buildProvenanceMap(patch: GraphPatch): java.util.Map[String, AnyRef] = {
- val result = new java.util.HashMap[String, AnyRef](5)
- result.put("source_uri", patch.source.uri)
- result.put("source_hash", patch.source.sourceHash.orNull)
- result.put("extractor", patch.source.extractor)
- result.put("source_type", patch.source.sourceType.asJson.asString.getOrElse("code"))
- result.put("observed_at", patch.timestamp.toString)
- result
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/ConflictRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/ConflictRoutes.scala
deleted file mode 100644
index 76899595..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/ConflictRoutes.scala
+++ /dev/null
@@ -1,51 +0,0 @@
-package ix.memory.api
-
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.{Decoder, Encoder}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.conflict.ConflictService
-import ix.memory.model.{ClaimId, ConflictId, ConflictStatus}
-
-case class ResolveRequest(winnerClaimId: String)
-
-object ResolveRequest {
- implicit val decoder: Decoder[ResolveRequest] = deriveDecoder[ResolveRequest]
- implicit val encoder: Encoder[ResolveRequest] = deriveEncoder[ResolveRequest]
-}
-
-class ConflictRoutes(conflictService: ConflictService) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
-
- // GET /v1/conflicts?status=<optional>
- case req @ GET -> Root / "v1" / "conflicts" =>
- val status = req.params.get("status").flatMap(parseStatus)
- (for {
- conflicts <- conflictService.listConflicts(status)
- resp <- Ok(conflicts)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- // POST /v1/conflicts/:id/resolve
- case req @ POST -> Root / "v1" / "conflicts" / UUIDVar(id) / "resolve" =>
- (for {
- body <- req.as[ResolveRequest]
- winnerClaimId <- IO.fromTry(scala.util.Try(UUID.fromString(body.winnerClaimId))).map(ClaimId(_))
- conflictId = ConflictId(id)
- _ <- conflictService.resolve(conflictId, winnerClaimId)
- resp <- Ok(io.circe.Json.obj("status" -> io.circe.Json.fromString("resolved")))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-
- private def parseStatus(s: String): Option[ConflictStatus] = s.toLowerCase match {
- case "open" => Some(ConflictStatus.Open)
- case "resolved" => Some(ConflictStatus.Resolved)
- case "dismissed" => Some(ConflictStatus.Dismissed)
- case _ => None
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/ContextRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/ContextRoutes.scala
deleted file mode 100644
index 870a6f09..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/ContextRoutes.scala
+++ /dev/null
@@ -1,31 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.{Decoder, Encoder}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.context.ContextService
-import ix.memory.model.Rev
-
-case class ContextRequest(query: String, asOfRev: Option[Long], depth: Option[String])
-
-object ContextRequest {
- implicit val decoder: Decoder[ContextRequest] = deriveDecoder[ContextRequest]
- implicit val encoder: Encoder[ContextRequest] = deriveEncoder[ContextRequest]
-}
-
-class ContextRoutes(contextService: ContextService) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "context" =>
- (for {
- body <- req.as[ContextRequest]
- asOfRev = body.asOfRev.map(Rev(_))
- result <- contextService.query(body.query, asOfRev, depth = body.depth)
- resp <- Ok(result)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/DecideRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/DecideRoutes.scala
deleted file mode 100644
index f12a01db..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/DecideRoutes.scala
+++ /dev/null
@@ -1,62 +0,0 @@
-package ix.memory.api
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.{Decoder, Json}
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.GraphWriteApi
-import ix.memory.model._
-
-case class DecideRequest(title: String, rationale: String, intentId: Option[String] = None)
-
-object DecideRequest {
- implicit val decoder: Decoder[DecideRequest] = deriveDecoder[DecideRequest]
-}
-
-class DecideRoutes(writeApi: GraphWriteApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "decide" =>
- (for {
- body <- req.as[DecideRequest]
- nodeId = NodeId(UUID.randomUUID())
- patch = GraphPatch(
- patchId = PatchId(UUID.randomUUID()),
- actor = "ix/api",
- timestamp = Instant.now(),
- source = PatchSource("api:///v1/decide", None, "user-decision", SourceType.Human),
- baseRev = Rev(0L),
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Decision, body.title, Map(
- "title" -> Json.fromString(body.title),
- "rationale" -> Json.fromString(body.rationale),
- "intent_id" -> body.intentId.fold(Json.Null)(Json.fromString)
- ))
- ) ++ body.intentId.map { truthId =>
- PatchOp.UpsertEdge(
- EdgeId(UUID.randomUUID()),
- nodeId,
- NodeId(UUID.fromString(truthId)),
- EdgePredicate("DECISION_CONSTRAINED_BY_TRUTH"),
- Map.empty
- )
- }.toVector,
- replaces = Vector.empty,
- intent = Some(s"Decision: ${body.title}")
- )
- result <- writeApi.commitPatch(patch)
- resp <- Ok(Json.obj(
- "status" -> result.status.toString.asJson,
- "nodeId" -> nodeId.value.toString.asJson,
- "rev" -> result.newRev.value.asJson
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/DecisionRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/DecisionRoutes.scala
deleted file mode 100644
index 01cb9ee4..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/DecisionRoutes.scala
+++ /dev/null
@@ -1,29 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Decoder
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.GraphQueryApi
-
-case class DecisionListRequest(limit: Option[Int] = None, topic: Option[String] = None)
-
-object DecisionListRequest {
- implicit val decoder: Decoder[DecisionListRequest] = deriveDecoder[DecisionListRequest]
-}
-
-class DecisionRoutes(queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "decisions" =>
- (for {
- body <- req.as[DecisionListRequest]
- nodes <- queryApi.listDecisions(body.limit.getOrElse(50), body.topic)
- resp <- Ok(nodes.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
deleted file mode 100644
index c491ef52..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
+++ /dev/null
@@ -1,150 +0,0 @@
-package ix.memory.api
-
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.{Decoder, Encoder}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.GraphQueryApi
-import ix.memory.model._
-
-case class DiffRequest(
- fromRev: Long,
- toRev: Long,
- entityId: Option[String] = None,
- summary: Option[Boolean] = None,
- limit: Option[Int] = None
-)
-
-object DiffRequest {
- implicit val decoder: Decoder[DiffRequest] = deriveDecoder[DiffRequest]
- implicit val encoder: Encoder[DiffRequest] = deriveEncoder[DiffRequest]
-}
-
-case class DiffEntry(
- entityId: NodeId,
- changeType: String, // "added", "removed", "modified"
- atFromRev: Option[GraphNode],
- atToRev: Option[GraphNode],
- summary: Option[String]
-)
-
-object DiffEntry {
- implicit val encoder: Encoder[DiffEntry] = deriveEncoder[DiffEntry]
-}
-
-case class DiffResponse(
- fromRev: Long,
- toRev: Long,
- changes: Vector[DiffEntry],
- truncated: Boolean,
- totalChanges: Int
-)
-
-object DiffResponse {
- implicit val encoder: Encoder[DiffResponse] = deriveEncoder[DiffResponse]
-}
-
-case class DiffSummaryResponse(
- fromRev: Long,
- toRev: Long,
- summary: Map[String, Int],
- total: Int
-)
-
-object DiffSummaryResponse {
- implicit val encoder: Encoder[DiffSummaryResponse] = deriveEncoder[DiffSummaryResponse]
-}
-
-class DiffRoutes(queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "diff" =>
- (for {
- body <- req.as[DiffRequest]
- _ <- IO.raiseWhen(body.fromRev >= body.toRev)(
- new IllegalArgumentException("fromRev must be less than toRev")
- )
- resp <- if (body.summary.getOrElse(false)) {
- for {
- counts <- queryApi.getDiffSummary(Rev(body.fromRev), Rev(body.toRev))
- r <- Ok(DiffSummaryResponse(body.fromRev, body.toRev, counts, counts.values.sum))
- } yield r
- } else {
- val effectiveLimit = body.limit.getOrElse(100)
- val changesIO = body.entityId match {
- case Some(eidStr) =>
- for {
- eid <- IO.fromTry(scala.util.Try(UUID.fromString(eidStr))).map(NodeId(_))
- result <- diffEntity(eid, Rev(body.fromRev), Rev(body.toRev))
- } yield result.toVector
- case None =>
- 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 attrSummary = atFromOpt.map { atFrom =>
- diffAttrs(atFrom.attrs, atTo.attrs)
- }
- DiffEntry(
- entityId = atTo.id,
- changeType = changeType,
- atFromRev = atFromOpt,
- atToRev = Some(atTo),
- summary = attrSummary.filter(_.nonEmpty)
- )
- }
- }
- }
- for {
- allChanges <- changesIO
- totalCount = allChanges.size
- truncated = totalCount > effectiveLimit
- limited = if (truncated) allChanges.take(effectiveLimit) else allChanges
- r <- Ok(DiffResponse(body.fromRev, body.toRev, limited, truncated, totalCount))
- } yield r
- }
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-
- private def diffEntity(
- nodeId: NodeId, fromRev: Rev, toRev: Rev
- ): IO[Option[DiffEntry]] =
- for {
- atFrom <- queryApi.getNode(nodeId, asOfRev = Some(fromRev))
- atTo <- queryApi.getNode(nodeId, asOfRev = Some(toRev))
- } yield {
- (atFrom, atTo) match {
- case (None, None) => 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(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/api/EntityRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/EntityRoutes.scala
deleted file mode 100644
index e73e892e..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/EntityRoutes.scala
+++ /dev/null
@@ -1,104 +0,0 @@
-package ix.memory.api
-
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.{Encoder, Json}
-import io.circe.generic.semiauto.deriveEncoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.{Direction, GraphQueryApi}
-import ix.memory.model._
-
-case class EntityResponse(node: GraphNode, claims: Vector[Claim], edges: Vector[GraphEdge])
-
-object EntityResponse {
- implicit val encoder: Encoder[EntityResponse] = deriveEncoder[EntityResponse]
-}
-
-case class ProvenanceEntry(patchId: PatchId, rev: Rev, source: PatchSource, intent: Option[String])
-
-object ProvenanceEntry {
- implicit val encoder: Encoder[ProvenanceEntry] = deriveEncoder[ProvenanceEntry]
-}
-
-case class ProvenanceResponse(entityId: NodeId, chain: Vector[ProvenanceEntry])
-
-object ProvenanceResponse {
- implicit val encoder: Encoder[ProvenanceResponse] = deriveEncoder[ProvenanceResponse]
-}
-
-class EntityRoutes(queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
-
- // GET /v1/resolve-prefix/:prefix
- case GET -> Root / "v1" / "resolve-prefix" / prefix =>
- (for {
- ids <- queryApi.resolvePrefix(prefix)
- resp <- ids.size match {
- case 0 => NotFound(Json.obj("error" -> s"No entity matches prefix: $prefix".asJson))
- case 1 => Ok(Json.obj("id" -> ids.head.value.toString.asJson))
- case _ => Ok(Json.obj(
- "error" -> "ambiguous".asJson,
- "matches" -> ids.map(_.value.toString).asJson
- ))
- }
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- // GET /v1/entity/:id
- case GET -> Root / "v1" / "entity" / UUIDVar(id) =>
- val nodeId = NodeId(id)
- (for {
- nodeOpt <- queryApi.getNode(nodeId)
- node <- IO.fromOption(nodeOpt)(
- new NoSuchElementException(s"Entity not found: $id")
- )
- claims <- queryApi.getClaims(nodeId)
- expanded <- queryApi.expand(nodeId, Direction.Both)
- resp <- Ok(EntityResponse(node, claims, expanded.edges))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- // POST /v1/provenance/:id
- case POST -> Root / "v1" / "provenance" / UUIDVar(id) =>
- val nodeId = NodeId(id)
- (for {
- // Verify entity exists
- nodeOpt <- queryApi.getNode(nodeId)
- _ <- IO.fromOption(nodeOpt)(
- new NoSuchElementException(s"Entity not found: $id")
- )
- // Query patches that touched this entity
- chain <- queryProvenanceChain(nodeId)
- resp <- Ok(ProvenanceResponse(nodeId, chain))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-
- private def queryProvenanceChain(nodeId: NodeId): IO[Vector[ProvenanceEntry]] =
- queryApi.getPatchesForEntity(nodeId).map { jsons =>
- jsons.flatMap { json =>
- val c = json.hcursor
- val dataC = c.downField("data")
- for {
- patchIdStr <- dataC.get[String]("patchId").toOption
- .orElse(c.get[String]("patch_id").toOption)
- patchId <- scala.util.Try(UUID.fromString(patchIdStr)).toOption.map(PatchId(_))
- rev <- c.get[Long]("rev").toOption.map(Rev(_))
- sourceUri <- dataC.downField("source").get[String]("uri").toOption
- sourceHash = dataC.downField("source").get[String]("sourceHash").toOption
- extractor <- dataC.downField("source").get[String]("extractor").toOption
- stStr <- dataC.downField("source").get[String]("sourceType").toOption
- sourceType <- SourceType.decoder.decodeJson(Json.fromString(stStr)).toOption
- intent = dataC.get[String]("intent").toOption
- } yield ProvenanceEntry(
- patchId = patchId,
- rev = rev,
- source = PatchSource(sourceUri, sourceHash, extractor, sourceType),
- intent = intent
- )
- }.toVector
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/ErrorHandler.scala b/memory-layer/src/main/scala/ix/memory/api/ErrorHandler.scala
deleted file mode 100644
index 7510b9ee..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/ErrorHandler.scala
+++ /dev/null
@@ -1,49 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Json
-import io.circe.syntax._
-import org.http4s.{Response, Status}
-import org.http4s.circe._
-import org.http4s.dsl.io._
-
-import ix.memory.map.MapCapacityException
-
-/**
- * Maps domain exceptions to appropriate HTTP responses with JSON error bodies.
- */
-object ErrorHandler {
-
- def handle(err: Throwable): IO[Response[IO]] = err match {
- case e: MapCapacityException =>
- UnprocessableEntity(mapCapacityJson(e))
-
- case e: IllegalArgumentException =>
- BadRequest(errorJson("bad_request", e.getMessage))
-
- case e: NoSuchElementException =>
- NotFound(errorJson("not_found", e.getMessage))
-
- case e: IllegalStateException =>
- Conflict(errorJson("conflict", e.getMessage))
-
- case e: UnsupportedOperationException =>
- UnprocessableEntity(errorJson("unprocessable", e.getMessage))
-
- case e =>
- InternalServerError(errorJson("internal_error", Option(e.getMessage).getOrElse("Unknown error")))
- }
-
- private def errorJson(code: String, message: String): Json =
- Json.obj(
- "error" -> code.asJson,
- "message" -> message.asJson
- )
-
- private def mapCapacityJson(e: MapCapacityException): Json =
- Json.obj(
- "error" -> e.outcome.label.asJson,
- "message" -> e.userMessage.asJson,
- "next" -> e.next.asJson
- )
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/ExpandRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/ExpandRoutes.scala
deleted file mode 100644
index e2034aca..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/ExpandRoutes.scala
+++ /dev/null
@@ -1,75 +0,0 @@
-package ix.memory.api
-
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.{Decoder, Encoder}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.{Direction, ExpandResult, GraphQueryApi}
-import ix.memory.model._
-
-case class ExpandRequest(
- nodeId: String,
- direction: Option[String] = None,
- predicates: Option[List[String]] = None,
- hops: Option[Int] = None,
- asOfRev: Option[Long] = None
-)
-
-object ExpandRequest {
- implicit val decoder: Decoder[ExpandRequest] = deriveDecoder[ExpandRequest]
-}
-
-case class ExpandByNameRequest(
- name: String,
- direction: Option[String] = None,
- predicates: Option[List[String]] = None,
- kinds: Option[List[String]] = None
-)
-
-object ExpandByNameRequest {
- implicit val decoder: Decoder[ExpandByNameRequest] = deriveDecoder[ExpandByNameRequest]
-}
-
-class ExpandRoutes(queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "expand" =>
- (for {
- body <- req.as[ExpandRequest]
- nodeId <- IO.fromOption(
- scala.util.Try(UUID.fromString(body.nodeId)).toOption.map(NodeId(_))
- )(new IllegalArgumentException(s"Invalid node ID: ${body.nodeId}"))
- dir = body.direction match {
- case Some("in") => Direction.In
- case Some("out") => Direction.Out
- case _ => Direction.Both
- }
- preds = body.predicates.map(_.toSet)
- result <- queryApi.expand(nodeId, dir, preds, body.hops.getOrElse(1), body.asOfRev.map(Rev(_)))
- projection <- queryApi.projectExpand(nodeId, dir, preds, body.hops.getOrElse(1), body.asOfRev.map(Rev(_)))
- resp <- Ok(result.copy(projection = projection).asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- case req @ POST -> Root / "v1" / "expand-by-name" =>
- (for {
- body <- req.as[ExpandByNameRequest]
- dir = body.direction match {
- case Some("in") => Direction.In
- case Some("out") => Direction.Out
- case _ => Direction.Both
- }
- preds = body.predicates.map(_.toSet)
- kinds = body.kinds.map(_.flatMap(k =>
- NodeKind.decoder.decodeJson(io.circe.Json.fromString(k)).toOption
- ).toSet)
- result <- queryApi.expandByName(body.name, dir, preds, kinds)
- resp <- Ok(result.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/GoalRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/GoalRoutes.scala
deleted file mode 100644
index 4224b840..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/GoalRoutes.scala
+++ /dev/null
@@ -1,60 +0,0 @@
-package ix.memory.api
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.{Decoder, Json}
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.{GraphQueryApi, GraphWriteApi}
-import ix.memory.model._
-
-case class GoalRequest(statement: String, parentGoal: Option[String] = None)
-
-object GoalRequest {
- implicit val decoder: Decoder[GoalRequest] = deriveDecoder[GoalRequest]
-}
-
-class GoalRoutes(writeApi: GraphWriteApi, queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case GET -> Root / "v1" / "goal" =>
- (for {
- goals <- queryApi.findNodesByKind(NodeKind.Goal)
- resp <- Ok(goals.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- case req @ POST -> Root / "v1" / "goal" =>
- (for {
- body <- req.as[GoalRequest]
- nodeId = NodeId(UUID.randomUUID())
- patch = GraphPatch(
- patchId = PatchId(UUID.randomUUID()),
- actor = "ix/api",
- timestamp = Instant.now(),
- source = PatchSource("api:///v1/goal", None, "user-goal", SourceType.Human),
- baseRev = Rev(0L),
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Goal, body.statement, Map(
- "statement" -> Json.fromString(body.statement),
- "status" -> Json.fromString("active"),
- "parent_goal" -> body.parentGoal.fold(Json.Null)(Json.fromString)
- ))
- ),
- replaces = Vector.empty,
- intent = Some(s"Goal: ${body.statement}")
- )
- result <- writeApi.commitPatch(patch)
- resp <- Ok(Json.obj(
- "status" -> result.status.toString.asJson,
- "nodeId" -> nodeId.value.toString.asJson,
- "rev" -> result.newRev.value.asJson
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/ListRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/ListRoutes.scala
deleted file mode 100644
index 016f4318..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/ListRoutes.scala
+++ /dev/null
@@ -1,37 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Decoder
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.GraphQueryApi
-import ix.memory.model._
-
-case class ListRequest(
- kind: String,
- limit: Option[Int] = None
-)
-
-object ListRequest {
- implicit val decoder: Decoder[ListRequest] = deriveDecoder[ListRequest]
-}
-
-class ListRoutes(queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "list" =>
- (for {
- body <- req.as[ListRequest]
- kind <- IO.fromEither(
- NodeKind.decoder.decodeJson(io.circe.Json.fromString(body.kind))
- .left.map(e => new IllegalArgumentException(s"Unknown kind: ${body.kind}"))
- )
- nodes <- queryApi.findNodesByKind(kind, body.limit.getOrElse(200))
- resp <- Ok(nodes.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/MapEncoder.scala b/memory-layer/src/main/scala/ix/memory/api/MapEncoder.scala
deleted file mode 100644
index 18bdb1cb..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/MapEncoder.scala
+++ /dev/null
@@ -1,186 +0,0 @@
-package ix.memory.api
-
-import io.circe.Json
-import io.circe.syntax._
-
-import ix.memory.map.{ArchitectureEdge, ArchitectureMap, MapPreflightResult, PersistenceEstimate, Region, RegionMatch, ScopedSubsystemView}
-import ix.memory.model.NodeId
-
-/** Shared JSON encoding for ArchitectureMap, used by MapRoutes and SubsystemRoutes. */
-object MapEncoder {
-
- def encodeMap(m: ArchitectureMap): Json = {
- val regionsByLevel = m.regions.groupBy(_.level).toVector.sortBy(_._1)
- val base = Json.obj(
- "file_count" -> m.fileCount.asJson,
- "region_count" -> m.regions.size.asJson,
- "levels" -> regionsByLevel.size.asJson,
- "map_rev" -> m.mapRev.asJson,
- "outcome" -> m.outcome.label.asJson,
- "regions" -> m.regions.sortBy(r => (r.level, r.label)).map(encodeRegion).asJson,
- "edges" -> m.edges.sortBy(e => (e.level, e.src.value.toString, e.dst.value.toString)).map(encodeEdge).asJson,
- "hierarchy" -> buildHierarchyTree(m).asJson
- )
-
- val withPreflight =
- m.preflight match {
- case Some(value) => base.deepMerge(Json.obj("preflight" -> encodePreflight(value)))
- case None => base
- }
-
- m.persistenceEstimate match {
- case Some(value) => withPreflight.deepMerge(Json.obj("persistence" -> encodePersistence(value)))
- case None => withPreflight
- }
- }
-
- def encodeRegion(r: Region): Json =
- Json.obj(
- "id" -> r.id.value.toString.asJson,
- "label" -> r.label.asJson,
- "label_kind" -> r.labelKind.asJson,
- "level" -> r.level.asJson,
- "file_count" -> r.memberFiles.size.asJson,
- "child_region_count" -> r.childRegionIds.size.asJson,
- "parent_id" -> r.parentId.map(_.value.toString).asJson,
- "cohesion" -> r.cohesion.asJson,
- "external_coupling" -> r.externalCoupling.asJson,
- "boundary_ratio" -> r.boundaryRatio.asJson,
- "confidence" -> r.confidence.asJson,
- "crosscut_score" -> r.crosscutScore.asJson,
- "dominant_signals" -> r.dominantSignals.asJson,
- "interface_node_count" -> r.interfaceNodeCount.asJson
- )
-
- def encodeEdge(edge: ArchitectureEdge): Json =
- Json.obj(
- "id" -> edge.id.asJson,
- "src" -> edge.src.value.toString.asJson,
- "dst" -> edge.dst.value.toString.asJson,
- "level" -> edge.level.asJson,
- "weight" -> edge.weight.asJson,
- "dominant_signal" -> edge.dominantSignal.asJson,
- "predicate_counts" -> edge.predicateCounts.asJson
- )
-
- def encodeScopedView(view: ScopedSubsystemView, isCrossCutting: Region => Boolean): Json =
- Json.obj(
- "target" -> encodeScopedRegion(view.target, isCrossCutting).asJson,
- "parent" -> view.parent.map(parent =>
- Json.obj(
- "id" -> parent.id.value.toString.asJson,
- "label" -> parent.label.asJson,
- "level" -> parent.level.asJson,
- "label_kind" -> parent.labelKind.asJson
- )
- ).asJson,
- "summary" -> Json.obj(
- "well_defined" -> view.summary.wellDefined.asJson,
- "moderate" -> view.summary.moderate.asJson,
- "fuzzy" -> view.summary.fuzzy.asJson,
- "cross_cutting" -> view.summary.crossCutting.asJson
- ),
- "children" -> view.children.map(child => encodeScopedRegion(child, isCrossCutting)).asJson,
- "hierarchy" -> buildScopedTree(view, isCrossCutting).asJson
- )
-
- def encodeAmbiguousTarget(target: String, candidates: Vector[RegionMatch], parentOf: Region => Option[Region]): Json =
- Json.obj(
- "error" -> "ambiguous_target".asJson,
- "target_query" -> target.asJson,
- "candidates" -> candidates.zipWithIndex.map { case (candidate, idx) =>
- val region = candidate.region
- Json.obj(
- "pick" -> (idx + 1).asJson,
- "id" -> region.id.value.toString.asJson,
- "label" -> region.label.asJson,
- "level" -> region.level.asJson,
- "label_kind" -> region.labelKind.asJson,
- "file_count" -> region.memberFiles.size.asJson,
- "parent" -> parentOf(region).map(_.label).asJson
- )
- }.asJson
- )
-
- def buildHierarchyTree(m: ArchitectureMap): Json = {
- val regionById: Map[NodeId, Region] = m.regions.map(r => r.id -> r).toMap
-
- // Guard against cycles (can arise when Louvain levels have identical partitions)
- def renderNode(r: Region, visited: Set[NodeId]): Json = {
- val children =
- if (visited.contains(r.id)) Vector.empty
- else r.childRegionIds.toVector
- .flatMap(cid => regionById.get(cid))
- .filter(c => c.level < r.level)
- .sortBy(_.label)
- .map(c => renderNode(c, visited + r.id))
-
- encodeRegion(r).deepMerge(Json.obj("children" -> children.asJson))
- }
-
- val roots = m.regions.filter(_.parentId.isEmpty).sortBy(-_.level)
- roots.map(r => renderNode(r, Set.empty)).asJson
- }
-
- private def encodeScopedRegion(region: Region, isCrossCutting: Region => Boolean): Json =
- Json.obj(
- "id" -> region.id.value.toString.asJson,
- "label" -> region.label.asJson,
- "level" -> region.level.asJson,
- "label_kind" -> region.labelKind.asJson,
- "parent_id" -> region.parentId.map(_.value.toString).asJson,
- "file_count" -> region.memberFiles.size.asJson,
- "confidence" -> region.confidence.asJson,
- "is_cross_cutting" -> isCrossCutting(region).asJson,
- "dominant_signals" -> region.dominantSignals.asJson
- )
-
- private def buildScopedTree(view: ScopedSubsystemView, isCrossCutting: Region => Boolean): Json = {
- val regionsById = view.subtree.map(region => region.id -> region).toMap
-
- def renderNode(region: Region, visited: Set[NodeId]): Json = {
- val children =
- if (visited.contains(region.id)) Vector.empty
- else region.childRegionIds.toVector
- .flatMap(regionsById.get)
- .filter(_.level < region.level)
- .sortBy(child => (-child.memberFiles.size, -child.confidence, child.label.toLowerCase))
- .map(child => renderNode(child, visited + region.id))
-
- encodeScopedRegion(region, isCrossCutting).deepMerge(Json.obj("children" -> children.asJson))
- }
-
- renderNode(view.target, Set.empty)
- }
-
- private def encodePreflight(preflight: MapPreflightResult): Json =
- Json.obj(
- "cost" -> Json.obj(
- "file_count" -> preflight.cost.fileCount.asJson,
- "directory_count" -> preflight.cost.directoryCount.asJson,
- "directory_quadratic" -> preflight.cost.directoryQuadratic.asJson,
- "symbol_estimate" -> preflight.cost.symbolEstimate.asJson,
- "edge_estimate" -> preflight.cost.edgeEstimate.asJson
- ),
- "capacity" -> Json.obj(
- "cpu_cores" -> preflight.capacity.cpuCores.asJson,
- "heap_max_bytes" -> preflight.capacity.heapMaxBytes.asJson,
- "heap_free_bytes" -> preflight.capacity.heapFreeBytes.asJson,
- "container_memory" -> preflight.capacity.containerMemory.asJson,
- "disk_free_bytes" -> preflight.capacity.diskFreeBytes.asJson
- ),
- "risk" -> preflight.risk.label.asJson,
- "mode" -> preflight.mode.label.asJson,
- "warnings" -> preflight.warnings.asJson,
- "duration_ms" -> preflight.durationMs.asJson
- )
-
- private def encodePersistence(persistence: PersistenceEstimate): Json =
- Json.obj(
- "region_nodes" -> persistence.regionNodes.asJson,
- "file_edges" -> persistence.fileEdges.asJson,
- "region_edges" -> persistence.regionEdges.asJson,
- "delete_ops" -> persistence.deleteOps.asJson,
- "total_ops" -> persistence.totalOps.asJson
- )
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/MapRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/MapRoutes.scala
deleted file mode 100644
index 1acadb4e..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/MapRoutes.scala
+++ /dev/null
@@ -1,24 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Json
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.map.MapService
-
-class MapRoutes(mapService: MapService) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
-
- /** POST /v1/map — Run the full map pipeline and return the hierarchy. */
- case req @ POST -> Root / "v1" / "map" =>
- (for {
- body <- req.as[Json].handleError(_ => Json.obj())
- forceFull = body.hcursor.get[Boolean]("full").toOption.getOrElse(false)
- archMap <- mapService.buildMap(forceRecompute = forceFull, forceFull = forceFull)
- resp <- Ok(MapEncoder.encodeMap(archMap))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/PatchCommitRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/PatchCommitRoutes.scala
deleted file mode 100644
index def177bb..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/PatchCommitRoutes.scala
+++ /dev/null
@@ -1,61 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import cats.syntax.traverse._
-import io.circe.syntax._
-import io.circe.Json
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.{BulkWriteApi, CommitResult, CommitStatus, FileBatch, GraphQueryApi, GraphWriteApi}
-import ix.memory.model.{GraphPatch, Rev}
-
-class PatchCommitRoutes(writeApi: GraphWriteApi, bulkWriteApi: BulkWriteApi, queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "patch" =>
- (for {
- patch <- req.as[GraphPatch]
- result <- writeApi.commitPatch(patch)
- resp <- Ok(Json.obj(
- "status" -> result.status.toString.asJson,
- "rev" -> result.newRev.value.asJson
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- // Batch endpoint: use BulkWriteApi (bulk document inserts, no per-patch transaction)
- // instead of traverse(commitPatch) which was N serial ArangoDB transactions.
- case req @ POST -> Root / "v1" / "patches" / "batch" =>
- (for {
- patches <- req.as[Vector[GraphPatch]]
- latestRev <- queryApi.getLatestRev
- result <- if (patches.isEmpty)
- IO.pure(CommitResult(latestRev, CommitStatus.Ok))
- else {
- val fileBatches = patches.map(p => FileBatch(
- filePath = p.source.uri,
- sourceHash = p.source.sourceHash,
- patch = p,
- provenance = buildProvenanceMap(p)
- ))
- bulkWriteApi.commitBatchChunked(fileBatches, latestRev.value)
- }
- // Return one result entry per patch (same rev for all — one bulk revision)
- resp <- Ok(patches.map(_ => Json.obj(
- "status" -> result.status.toString.asJson,
- "rev" -> result.newRev.value.asJson
- )).asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-
- private def buildProvenanceMap(patch: GraphPatch): java.util.Map[String, AnyRef] = {
- val result = new java.util.HashMap[String, AnyRef](5)
- result.put("source_uri", patch.source.uri)
- result.put("source_hash", patch.source.sourceHash.orNull)
- result.put("extractor", patch.source.extractor)
- result.put("source_type", patch.source.sourceType.asJson.asString.getOrElse("code"))
- result.put("observed_at", patch.timestamp.toString)
- result
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/PatchRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/PatchRoutes.scala
deleted file mode 100644
index 2a33a4e4..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/PatchRoutes.scala
+++ /dev/null
@@ -1,65 +0,0 @@
-package ix.memory.api
-
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.syntax._
-import io.circe.{Decoder, Encoder}
-import io.circe.generic.semiauto._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-import org.http4s.dsl.impl.OptionalQueryParamDecoderMatcher
-
-import ix.memory.db.{ArangoClient, GraphQueryApi}
-
-object LimitParam extends OptionalQueryParamDecoderMatcher[Int]("limit")
-
-case class SourceHashesRequest(uris: List[String])
-object SourceHashesRequest {
- implicit val decoder: Decoder[SourceHashesRequest] = deriveDecoder
- implicit val encoder: Encoder[SourceHashesRequest] = deriveEncoder
-}
-
-class PatchRoutes(client: ArangoClient, queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case GET -> Root / "v1" / "patches" :? LimitParam(limit) =>
- val maxResults = limit.getOrElse(50)
- (for {
- patches <- client.query(
- """FOR p IN patches
- | SORT p.rev DESC
- | LIMIT @limit
- | RETURN { patch_id: p.patch_id, rev: p.rev, intent: p.data.intent,
- | source_uri: p.data.source.uri, timestamp: p.data.timestamp }""".stripMargin,
- Map("limit" -> Int.box(maxResults).asInstanceOf[AnyRef])
- )
- resp <- Ok(patches.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- case GET -> Root / "v1" / "patches" / patchIdStr =>
- (for {
- _ <- IO.fromOption(
- scala.util.Try(UUID.fromString(patchIdStr)).toOption
- )(new IllegalArgumentException(s"Invalid patch ID: $patchIdStr"))
- result <- client.queryOne(
- """FOR p IN patches
- | FILTER p.patch_id == @patchId
- | RETURN p""".stripMargin,
- Map("patchId" -> patchIdStr.asInstanceOf[AnyRef])
- )
- patch <- IO.fromOption(result)(
- new NoSuchElementException(s"Patch not found: $patchIdStr")
- )
- resp <- Ok(patch)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- case req @ POST -> Root / "v1" / "source-hashes" =>
- (for {
- body <- req.as[SourceHashesRequest]
- hashes <- queryApi.getSourceHashes(body.uris)
- resp <- Ok(hashes.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/ResetRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/ResetRoutes.scala
deleted file mode 100644
index 898d445b..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/ResetRoutes.scala
+++ /dev/null
@@ -1,30 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Json
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.ArangoClient
-
-class ResetRoutes(client: ArangoClient) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
-
- /** POST /v1/reset — Truncate all graph data (nodes + edges). Dev use only. */
- case POST -> Root / "v1" / "reset" =>
- (for {
- _ <- client.truncateGraph()
- r <- Ok(Json.obj("ok" -> true.asJson, "message" -> "Graph reset. All nodes and edges deleted.".asJson))
- } yield r).handleErrorWith(ErrorHandler.handle(_))
-
- /** POST /v1/reset/code — Delete only code-derived nodes/edges; preserve planning artifacts. */
- case POST -> Root / "v1" / "reset" / "code" =>
- (for {
- _ <- client.truncateCodeGraph()
- r <- Ok(Json.obj("ok" -> true.asJson, "message" -> "Code graph reset. Planning artifacts (goals, plans, tasks, bugs, decisions) preserved.".asJson))
- } yield r).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/Routes.scala b/memory-layer/src/main/scala/ix/memory/api/Routes.scala
deleted file mode 100644
index ffe119f0..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/Routes.scala
+++ /dev/null
@@ -1,68 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import cats.syntax.semigroupk._
-import io.circe.Json
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.conflict.ConflictService
-import ix.memory.context.ContextService
-import ix.memory.db.{ArangoClient, BulkWriteApi, GraphQueryApi, GraphWriteApi}
-import ix.memory.map.MapService
-import ix.memory.savings.SavingsAccumulator
-import ix.memory.smell.SmellService
-import ix.memory.subsystem.SubsystemScoringService
-
-object Routes {
-
- def all(
- contextService: ContextService,
- queryApi: GraphQueryApi,
- writeApi: GraphWriteApi,
- conflictService: ConflictService,
- client: ArangoClient,
- mapService: MapService,
- bulkWriteApi: BulkWriteApi,
- smellService: SmellService,
- subsystemService: SubsystemScoringService,
- savingsAccumulator: SavingsAccumulator
- ): HttpRoutes[IO] = {
-
- val health = HttpRoutes.of[IO] {
- case GET -> Root / "v1" / "health" =>
- Ok(Json.obj("status" -> "ok".asJson))
- }
-
- val contextRoutes = new ContextRoutes(contextService).routes
- val entityRoutes = new EntityRoutes(queryApi).routes
- val diffRoutes = new DiffRoutes(queryApi).routes
- val conflictRoutes = new ConflictRoutes(conflictService).routes
- val decideRoutes = new DecideRoutes(writeApi).routes
- val searchRoutes = new SearchRoutes(queryApi).routes
- val truthRoutes = new TruthRoutes(writeApi, queryApi).routes
- val goalRoutes = new GoalRoutes(writeApi, queryApi).routes
- val patchRoutes = new PatchRoutes(client, queryApi).routes
- val decisionListRoutes = new DecisionRoutes(queryApi).routes
- val expandRoutes = new ExpandRoutes(queryApi).routes
- val statsRoutes = new StatsRoutes(client).routes
- val patchCommitRoutes = new PatchCommitRoutes(writeApi, bulkWriteApi, queryApi).routes
- val listRoutes = new ListRoutes(queryApi).routes
- val mapRoutes = new MapRoutes(mapService).routes
- val resetRoutes = new ResetRoutes(client).routes
- val sourceHashRoutes = new SourceHashRoutes(queryApi).routes
- val bulkPatchCommitRoutes = new BulkPatchCommitRoutes(bulkWriteApi, queryApi).routes
- val smellRoutes = new SmellRoutes(smellService).routes
- val subsystemRoutes = new SubsystemRoutes(subsystemService, mapService).routes
- val savingsRoutes = new SavingsRoutes(savingsAccumulator).routes
-
- health <+> contextRoutes <+> entityRoutes <+>
- diffRoutes <+> conflictRoutes <+> decideRoutes <+> searchRoutes <+>
- truthRoutes <+> patchRoutes <+> decisionListRoutes <+> expandRoutes <+>
- statsRoutes <+> patchCommitRoutes <+> listRoutes <+> goalRoutes <+>
- mapRoutes <+> resetRoutes <+> sourceHashRoutes <+> bulkPatchCommitRoutes <+>
- smellRoutes <+> subsystemRoutes <+> savingsRoutes
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/SavingsRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/SavingsRoutes.scala
deleted file mode 100644
index ab30a645..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/SavingsRoutes.scala
+++ /dev/null
@@ -1,29 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-import org.http4s.dsl.impl.OptionalQueryParamDecoderMatcher
-
-import ix.memory.savings.SavingsAccumulator
-
-object DetailParam extends OptionalQueryParamDecoderMatcher[Boolean]("detail")
-
-class SavingsRoutes(accumulator: SavingsAccumulator) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case GET -> Root / "v1" / "savings" :? DetailParam(detail) =>
- (for {
- data <- accumulator.getSavings(detail.getOrElse(false))
- resp <- Ok(data.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- case DELETE -> Root / "v1" / "savings" =>
- (for {
- _ <- accumulator.reset
- resp <- Ok(io.circe.Json.obj("status" -> "reset".asJson))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/SearchRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/SearchRoutes.scala
deleted file mode 100644
index 10bec70d..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/SearchRoutes.scala
+++ /dev/null
@@ -1,37 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Decoder
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.GraphQueryApi
-import ix.memory.model._
-
-case class SearchRequest(
- term: String,
- limit: Option[Int] = None,
- kind: Option[String] = None,
- language: Option[String] = None,
- asOfRev: Option[Long] = None,
- nameOnly: Option[Boolean] = None
-)
-
-object SearchRequest {
- implicit val decoder: Decoder[SearchRequest] = deriveDecoder[SearchRequest]
-}
-
-class SearchRoutes(queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "search" =>
- (for {
- body <- req.as[SearchRequest]
- nodes <- queryApi.searchNodes(body.term, body.limit.getOrElse(20), body.kind, body.language, body.asOfRev.map(Rev(_)), body.nameOnly.getOrElse(false))
- resp <- Ok(nodes.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/SmellRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/SmellRoutes.scala
deleted file mode 100644
index 2ed44f05..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/SmellRoutes.scala
+++ /dev/null
@@ -1,61 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Json
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-import org.http4s.dsl.impl.OptionalQueryParamDecoderMatcher
-
-import ix.memory.smell.{SmellConfig, SmellService}
-
-object OrphanThresholdParam extends OptionalQueryParamDecoderMatcher[Int]("orphan-max-connections")
-object GodChunkParam extends OptionalQueryParamDecoderMatcher[Int]("god-module-chunks")
-object GodFanParam extends OptionalQueryParamDecoderMatcher[Int]("god-module-fan")
-object WeakNeighborParam extends OptionalQueryParamDecoderMatcher[Int]("weak-max-neighbors")
-
-class SmellRoutes(smellService: SmellService) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
-
- /** POST /v1/smells — run smell detection and persist claims. */
- case req @ POST -> Root / "v1" / "smells"
- :? OrphanThresholdParam(orphanMax)
- +& GodChunkParam(godChunks)
- +& GodFanParam(godFan)
- +& WeakNeighborParam(weakNeighbors) =>
-
- val config = SmellConfig(
- orphanMaxConnections = orphanMax.getOrElse(0),
- godModuleChunkThreshold = godChunks.getOrElse(20),
- godModuleFanThreshold = godFan.getOrElse(15),
- weakComponentMaxNeighbors = weakNeighbors.getOrElse(1)
- )
- (for {
- report <- smellService.run(config)
- resp <- Ok(Json.obj(
- "rev" -> report.rev.asJson,
- "run_at" -> report.runAt.asJson,
- "count" -> report.candidates.size.asJson,
- "candidates" -> report.candidates.map { c =>
- Json.obj(
- "file_id" -> c.fileId.asJson,
- "file" -> c.fileName.asJson,
- "smell" -> c.smellKind.field.asJson,
- "confidence" -> c.confidence.asJson,
- "signals" -> c.signals.asJson,
- "inference_version" -> "smell_v1".asJson
- )
- }.asJson
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- /** GET /v1/smells — retrieve existing smell claims without rerunning. */
- case GET -> Root / "v1" / "smells" =>
- (for {
- smells <- smellService.listSmells()
- resp <- Ok(Json.obj("smells" -> smells.asJson))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/SourceHashRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/SourceHashRoutes.scala
deleted file mode 100644
index dda453c0..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/SourceHashRoutes.scala
+++ /dev/null
@@ -1,32 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.{Decoder, Json}
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.GraphQueryApi
-
-class SourceHashRoutes(queryApi: GraphQueryApi) {
-
- private case class SourceHashRequest(uris: List[String])
- private implicit val reqDecoder: Decoder[SourceHashRequest] = deriveDecoder
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case req @ POST -> Root / "v1" / "source-hashes" =>
- (for {
- body <- req.as[SourceHashRequest]
- hashes <- queryApi.getSourceHashes(body.uris)
- resp <- Ok(Json.fromFields(hashes.map { case (k, v) => k -> v.asJson }))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- case GET -> Root / "v1" / "source-hashes" / "exists" =>
- (for {
- exists <- queryApi.hasIngestBaseline()
- resp <- Ok(Json.obj("exists" -> exists.asJson))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/StatsRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/StatsRoutes.scala
deleted file mode 100644
index 1bfb86a0..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/StatsRoutes.scala
+++ /dev/null
@@ -1,53 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Json
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.ArangoClient
-
-class StatsRoutes(client: ArangoClient) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case GET -> Root / "v1" / "stats" =>
- (for {
- nodeStats <- client.query(
- """FOR n IN nodes
- | FILTER n.deleted_rev == null
- | COLLECT kind = n.kind WITH COUNT INTO cnt
- | SORT cnt DESC
- | RETURN { kind: kind, count: cnt }""".stripMargin,
- Map.empty[String, AnyRef]
- )
- edgeStats <- client.query(
- """FOR e IN edges
- | FILTER e.deleted_rev == null
- | COLLECT predicate = e.predicate WITH COUNT INTO cnt
- | SORT cnt DESC
- | RETURN { predicate: predicate, count: cnt }""".stripMargin,
- Map.empty[String, AnyRef]
- )
- totalNodes <- client.query(
- """RETURN LENGTH(FOR n IN nodes FILTER n.deleted_rev == null RETURN 1)""",
- Map.empty[String, AnyRef]
- )
- totalEdges <- client.query(
- """RETURN LENGTH(FOR e IN edges FILTER e.deleted_rev == null RETURN 1)""",
- Map.empty[String, AnyRef]
- )
- resp <- Ok(Json.obj(
- "nodes" -> Json.obj(
- "total" -> totalNodes.headOption.flatMap(_.asNumber).flatMap(_.toInt).getOrElse(0).asJson,
- "byKind" -> nodeStats.asJson
- ),
- "edges" -> Json.obj(
- "total" -> totalEdges.headOption.flatMap(_.asNumber).flatMap(_.toInt).getOrElse(0).asJson,
- "byPredicate" -> edgeStats.asJson
- )
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/SubsystemRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/SubsystemRoutes.scala
deleted file mode 100644
index 91d95d59..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/SubsystemRoutes.scala
+++ /dev/null
@@ -1,118 +0,0 @@
-package ix.memory.api
-
-import cats.effect.IO
-import io.circe.Json
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.map.MapService
-import ix.memory.subsystem.SubsystemScoringService
-
-class SubsystemRoutes(
- scoringService: SubsystemScoringService,
- mapService: MapService
-) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
-
- /** GET /v1/subsystems/map — retrieve the persisted architecture map or a scoped region view. */
- case req @ GET -> Root / "v1" / "subsystems" / "map" =>
- val params = req.uri.query.params
- val target = params.get("target").map(_.trim).filter(_.nonEmpty)
- val pickRaw = params.get("pick").map(_.trim).filter(_.nonEmpty)
- (for {
- resp <- parsePick(pickRaw) match {
- case Left(message) =>
- BadRequest(Json.obj("error" -> message.asJson))
- case Right(pick) =>
- for {
- archMap <- mapService.loadCachedMap().flatMap {
- case Some(cached) => IO.pure(cached)
- case None => mapService.getOrBuildMap()
- }
- response <- target match {
- case None =>
- if (pick.isDefined) {
- BadRequest(Json.obj("error" -> "--pick requires a target.".asJson))
- } else {
- Ok(MapEncoder.encodeMap(archMap))
- }
- case Some(query) =>
- val matches = mapService.resolveRegion(archMap, query)
- if (matches.isEmpty) {
- NotFound(Json.obj(
- "error" -> "unknown_target".asJson,
- "target_query" -> query.asJson,
- "message" -> s"""No architecture region matched "$query".""".asJson,
- "suggestions" -> Vector("ix subsystems", "ix map").asJson
- ))
- } else {
- selectMatch(matches, pick) match {
- case Left(message) =>
- BadRequest(Json.obj("error" -> message.asJson))
- case Right(None) =>
- BadRequest(MapEncoder.encodeAmbiguousTarget(query, matches, region => mapService.parentOf(archMap, region)))
- case Right(Some(selected)) =>
- Ok(MapEncoder.encodeScopedView(mapService.scopedView(archMap, selected.region), mapService.isCrossCutting))
- }
- }
- }
- } yield response
- }
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- /** POST /v1/subsystems/score — run health scoring and persist claims. */
- case POST -> Root / "v1" / "subsystems" / "score" =>
- (for {
- scores <- scoringService.score()
- resp <- Ok(Json.obj(
- "count" -> scores.size.asJson,
- "scores" -> scores.map { s =>
- Json.obj(
- "region_id" -> s.regionId.asJson,
- "name" -> s.regionName.asJson,
- "level" -> s.level.asJson,
- "label_kind" -> s.labelKind.asJson,
- "file_count" -> s.fileCount.asJson,
- "health_score" -> s.healthScore.asJson,
- "chunk_density"-> s.chunkDensity.asJson,
- "smell_rate" -> s.smellRate.asJson,
- "smell_files" -> s.smellFiles.asJson,
- "total_chunks" -> s.totalChunks.asJson,
- "confidence" -> s.confidence.asJson,
- "inference_version" -> "subsystem_v1".asJson
- )
- }.asJson
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- /** GET /v1/subsystems — retrieve stored health scores. */
- case GET -> Root / "v1" / "subsystems" =>
- (for {
- scores <- scoringService.listScores()
- resp <- Ok(Json.obj("scores" -> scores.asJson))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-
- private def parsePick(pick: Option[String]): Either[String, Option[Int]] =
- pick match {
- case None => Right(None)
- case Some(value) =>
- scala.util.Try(value.toInt).toOption match {
- case Some(parsed) if parsed > 0 => Right(Some(parsed))
- case _ => Left("Invalid --pick value.")
- }
- }
-
- private def selectMatch(matches: Vector[ix.memory.map.RegionMatch], pick: Option[Int]): Either[String, Option[ix.memory.map.RegionMatch]] =
- pick match {
- case None if matches.size == 1 => Right(matches.headOption)
- case None => Right(None)
- case Some(index) if index <= matches.size =>
- Right(matches.lift(index - 1))
- case Some(index) =>
- Left(s"Invalid --pick value: $index\nThere are only ${matches.size} matches for the requested target.")
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/api/TruthRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/TruthRoutes.scala
deleted file mode 100644
index 57883656..00000000
--- a/memory-layer/src/main/scala/ix/memory/api/TruthRoutes.scala
+++ /dev/null
@@ -1,60 +0,0 @@
-package ix.memory.api
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.{Decoder, Json}
-import io.circe.generic.semiauto.deriveDecoder
-import io.circe.syntax._
-import org.http4s.HttpRoutes
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.dsl.io._
-
-import ix.memory.db.{GraphQueryApi, GraphWriteApi}
-import ix.memory.model._
-
-case class IntentRequest(statement: String, parentIntent: Option[String] = None)
-
-object IntentRequest {
- implicit val decoder: Decoder[IntentRequest] = deriveDecoder[IntentRequest]
-}
-
-class TruthRoutes(writeApi: GraphWriteApi, queryApi: GraphQueryApi) {
-
- val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
- case GET -> Root / "v1" / "truth" =>
- (for {
- intents <- queryApi.findNodesByKind(NodeKind.Intent)
- resp <- Ok(intents.asJson)
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
-
- case req @ POST -> Root / "v1" / "truth" =>
- (for {
- body <- req.as[IntentRequest]
- nodeId = NodeId(UUID.randomUUID())
- patch = GraphPatch(
- patchId = PatchId(UUID.randomUUID()),
- actor = "ix/api",
- timestamp = Instant.now(),
- source = PatchSource("api:///v1/truth", None, "user-intent", SourceType.Human),
- baseRev = Rev(0L),
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Intent, body.statement, Map(
- "statement" -> Json.fromString(body.statement),
- "status" -> Json.fromString("active"),
- "parent_intent" -> body.parentIntent.fold(Json.Null)(Json.fromString)
- ))
- ),
- replaces = Vector.empty,
- intent = Some(s"Intent: ${body.statement}")
- )
- result <- writeApi.commitPatch(patch)
- resp <- Ok(Json.obj(
- "status" -> result.status.toString.asJson,
- "nodeId" -> nodeId.value.toString.asJson,
- "rev" -> result.newRev.value.asJson
- ))
- } yield resp).handleErrorWith(ErrorHandler.handle(_))
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/conflict/ConflictService.scala b/memory-layer/src/main/scala/ix/memory/conflict/ConflictService.scala
deleted file mode 100644
index bce05638..00000000
--- a/memory-layer/src/main/scala/ix/memory/conflict/ConflictService.scala
+++ /dev/null
@@ -1,94 +0,0 @@
-package ix.memory.conflict
-
-import cats.effect.IO
-import cats.syntax.traverse._
-import ix.memory.db.{ArangoClient, GraphQueryApi, GraphWriteApi}
-import ix.memory.model._
-import ix.memory.context.ConflictDetector
-
-class ConflictService(client: ArangoClient, queryApi: GraphQueryApi, writeApi: GraphWriteApi) {
-
- def detectAndStore(claims: Vector[ScoredClaim],
- detector: ConflictDetector): IO[Vector[ConflictReport]] = {
- val reports = detector.detect(claims)
- // Store conflict sets in DB for each report
- reports.traverse { report =>
- storeConflictSet(report).map(_ => report)
- }
- }
-
- private def storeConflictSet(report: ConflictReport): IO[Unit] = {
- client.execute(
- """INSERT {
- | _key: @key,
- | id: @id,
- | reason: @reason,
- | status: "open",
- | candidate_claims: @candidates,
- | winner_claim_id: null,
- | created_rev: 0
- |} INTO conflict_sets OPTIONS { overwriteMode: "ignore" }""".stripMargin,
- Map(
- "key" -> report.id.value.toString.asInstanceOf[AnyRef],
- "id" -> report.id.value.toString.asInstanceOf[AnyRef],
- "reason" -> report.reason.asInstanceOf[AnyRef],
- "candidates" -> java.util.Arrays.asList(
- report.claimA.value.toString, report.claimB.value.toString
- ).asInstanceOf[AnyRef]
- )
- )
- }
-
- def listConflicts(status: Option[ConflictStatus] = None): IO[Vector[ConflictSet]] = {
- val statusFilter = status.map(_ => " FILTER cs.status == @status").getOrElse("")
- val statusVar: Map[String, AnyRef] = status.map { s =>
- val str = s match {
- case ConflictStatus.Open => "open"
- case ConflictStatus.Resolved => "resolved"
- case ConflictStatus.Dismissed => "dismissed"
- }
- Map("status" -> str.asInstanceOf[AnyRef])
- }.getOrElse(Map.empty)
-
- client.query(
- s"""FOR cs IN conflict_sets
- | $statusFilter
- | RETURN cs""".stripMargin,
- statusVar
- ).map(_.flatMap(parseConflictSet).toVector)
- }
-
- def resolve(conflictId: ConflictId, winnerClaimId: ClaimId): IO[Unit] = {
- client.execute(
- """FOR cs IN conflict_sets
- | FILTER cs._key == @key
- | UPDATE cs WITH { status: "resolved", winner_claim_id: @winner } IN conflict_sets""".stripMargin,
- Map(
- "key" -> conflictId.value.toString.asInstanceOf[AnyRef],
- "winner" -> winnerClaimId.value.toString.asInstanceOf[AnyRef]
- )
- )
- }
-
- private def parseConflictSet(json: io.circe.Json): Option[ConflictSet] = {
- val c = json.hcursor
- for {
- idStr <- c.get[String]("id").toOption
- id <- scala.util.Try(java.util.UUID.fromString(idStr)).toOption.map(ConflictId(_))
- reason <- c.get[String]("reason").toOption
- statusStr <- c.get[String]("status").toOption
- status = statusStr match {
- case "resolved" => ConflictStatus.Resolved
- case "dismissed" => ConflictStatus.Dismissed
- case _ => ConflictStatus.Open
- }
- candidates <- c.get[Vector[String]]("candidate_claims").toOption
- candidateIds = candidates.flatMap(s =>
- scala.util.Try(java.util.UUID.fromString(s)).toOption.map(ClaimId(_)))
- winnerStr = c.get[String]("winner_claim_id").toOption
- winnerId = winnerStr.flatMap(s =>
- scala.util.Try(java.util.UUID.fromString(s)).toOption.map(ClaimId(_)))
- createdRev <- c.get[Long]("created_rev").toOption.map(Rev(_))
- } yield ConflictSet(id, reason, status, candidateIds, winnerId, createdRev)
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/ClaimCollector.scala b/memory-layer/src/main/scala/ix/memory/context/ClaimCollector.scala
deleted file mode 100644
index 9d5c0a87..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/ClaimCollector.scala
+++ /dev/null
@@ -1,12 +0,0 @@
-package ix.memory.context
-
-import cats.effect.IO
-
-import ix.memory.db.GraphQueryApi
-import ix.memory.model._
-
-class ClaimCollector(queryApi: GraphQueryApi) {
-
- def collect(nodeIds: Vector[NodeId]): IO[Vector[Claim]] =
- queryApi.getClaimsBatch(nodeIds).map(_.distinctBy(_.id))
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala b/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala
deleted file mode 100644
index 62b4b66b..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala
+++ /dev/null
@@ -1,93 +0,0 @@
-package ix.memory.context
-
-import java.time.Instant
-
-import ix.memory.model._
-
-trait ConfidenceScorer {
- def score(claim: Claim, ctx: ScoringContext): ScoredClaim
-}
-
-case class ScoringContext(
- latestRev: Rev,
- sourceChanged: Boolean,
- corroboratingCount: Int,
- conflictState: ConflictState,
- intentAlignment: IntentAlignment,
- observedAt: Instant
-)
-
-sealed trait ConflictState
-object ConflictState {
- case object NoConflict extends ConflictState
- case object ExistsHigherAuthority extends ConflictState
- case object ExistsLowerAuthority extends ConflictState
- case object ResolvedFor extends ConflictState
- case object ResolvedAgainst extends ConflictState
-}
-
-sealed trait IntentAlignment
-object IntentAlignment {
- case object DirectlyLinked extends IntentAlignment // x 1.1
- case object WithinTwoHops extends IntentAlignment // x 1.05
- case object NoConnection extends IntentAlignment // x 1.0
- case object ContradictsIntent extends IntentAlignment // x 0.85
-}
-
-class ConfidenceScorerImpl extends ConfidenceScorer {
- def score(claim: Claim, ctx: ScoringContext): ScoredClaim = {
- val base = Factor(
- SourceType.baseAuthority(claim.provenance.sourceType),
- s"${claim.provenance.sourceType}, ${claim.provenance.extractor}"
- )
-
- val verif = Factor(1.0, "no verification data available")
-
- val recency = computeRecency(ctx)
- val corr = computeCorroboration(ctx.corroboratingCount)
- val conflict = computeConflictPenalty(ctx.conflictState)
- val intent = computeIntentAlignment(ctx.intentAlignment)
-
- val breakdown = ConfidenceBreakdown(base, verif, recency, corr, conflict, intent)
- ScoredClaim(claim, breakdown, relevance = 1.0, finalScore = breakdown.score)
- }
-
- private def computeRecency(ctx: ScoringContext): Factor = {
- if (!ctx.sourceChanged) return Factor(1.0, "Source unchanged since observation")
- val age = java.time.Duration.between(ctx.observedAt, Instant.now())
- val days = age.toDays
- val (value, reason) = days match {
- case d if d < 1 => (1.0, "< 1 day ago")
- case d if d <= 7 => (0.95, "1-7 days ago")
- case d if d <= 28 => (0.85, "1-4 weeks ago")
- case d if d <= 180 => (0.70, "1-6 months ago")
- case _ => (0.50, "6+ months ago")
- }
- Factor(value, reason)
- }
-
- private def computeCorroboration(count: Int): Factor = {
- val value = count match {
- case 0 => 1.0 // No data (neutral)
- case 1 => 1.0 // Single source (neutral)
- case 2 => 1.05 // Two sources agree
- case n if n >= 3 => 1.1 // Strong corroboration
- }
- Factor(value, s"$count corroborating sources")
- }
-
- private def computeConflictPenalty(state: ConflictState): Factor = state match {
- case ConflictState.NoConflict => Factor(1.0, "no conflicts")
- case ConflictState.ExistsLowerAuthority => Factor(0.95, "conflict with lower authority source")
- case ConflictState.ExistsHigherAuthority => Factor(0.70, "conflict with higher authority source")
- case ConflictState.ResolvedFor => Factor(1.05, "conflict resolved in favor")
- case ConflictState.ResolvedAgainst => Factor(0.30, "conflict resolved against")
- }
-
- private def computeIntentAlignment(alignment: IntentAlignment): Factor = alignment match {
- case IntentAlignment.DirectlyLinked => Factor(1.1, "Directly linked to active intent")
- case IntentAlignment.WithinTwoHops => Factor(1.05, "Connected to intent within 2 hops")
- case IntentAlignment.NoConnection => Factor(1.0, "No connection to intent")
- case IntentAlignment.ContradictsIntent => Factor(0.85, "Contradicts active intent")
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/ConflictDetector.scala b/memory-layer/src/main/scala/ix/memory/context/ConflictDetector.scala
deleted file mode 100644
index 4e87f14c..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/ConflictDetector.scala
+++ /dev/null
@@ -1,172 +0,0 @@
-package ix.memory.context
-
-import java.util.UUID
-
-import cats.effect.IO
-import cats.implicits._
-import org.slf4j.LoggerFactory
-import ix.memory.model._
-
-trait ConflictDetector {
- def detect(claims: Vector[ScoredClaim]): Vector[ConflictReport]
-}
-
-class ConflictDetectorImpl(llmJudge: Option[LlmJudge] = None) extends ConflictDetector {
-
- private val log = LoggerFactory.getLogger(classOf[ConflictDetectorImpl])
-
- /**
- * Conflict detection with relevance filtering.
- *
- * Only flags conflicts between claims that share a field prefix
- * (e.g. both start with "calls:") or have keyword-stem overlap.
- * Caps at 20 active claims per entity and 10 conflicts per entity
- * to prevent combinatorial explosion.
- */
- def detect(claims: Vector[ScoredClaim]): Vector[ConflictReport] = {
- val byEntity = claims.groupBy(_.claim.entityId)
-
- byEntity.values.flatMap { entityClaims =>
- val activeClaims = entityClaims.filter(_.claim.status == ClaimStatus.Active).take(20)
-
- if (activeClaims.size < 2) Vector.empty
- else {
- // PASS 1: Hard conflicts (same statement/field, different values)
- val hardConflicts: Vector[ConflictReport] = {
- val byStatement = activeClaims.groupBy(_.claim.statement)
-
- byStatement.values.toVector.flatMap { sameFieldClaims =>
- // Group by value (stringified) to find distinct values for this field
- val byValue = sameFieldClaims.groupBy(sc => sc.claim.value.noSpaces)
- if (byValue.size <= 1) Vector.empty
- else {
- // Pick one representative claim per distinct value (highest confidence wins)
- val reps = byValue.values.toVector.map { claimsForValue =>
- claimsForValue.maxBy(_.confidence.score)
- }
- val winner = reps.maxBy(_.confidence.score)
-
- // Emit one conflict per losing value against the winner
- reps.filterNot(_.claim.id == winner.claim.id).map { loser =>
- makeConflict(winner, loser, "Conflicting values for same field")
- }
- }
- }
- }
-
- // PASS 2: Soft conflicts (heuristics) only if we found no hard conflicts.
- // These are useful for surfacing potential inconsistencies, but can be noisy.
- val softConflicts: Vector[ConflictReport] =
- if (hardConflicts.nonEmpty) Vector.empty
- else {
- val candidates = activeClaims.filter(sc => sc.relevance >= 0.25)
- val pairs = candidates.combinations(2).toVector
- pairs.flatMap {
- case Vector(a, b) if a.claim.statement == b.claim.statement =>
- None // Same statement — not a conflict
- case Vector(a, b) =>
- if (shareFieldPrefix(a.claim.statement, b.claim.statement)) {
- Some(makeConflict(a, b, "Potential inconsistency (same field prefix)"))
- }
- else if (keywordsOverlap(a.claim.statement, b.claim.statement)) {
- Some(makeConflict(a, b, "Potential inconsistency (keyword overlap)"))
- }
- else None
- case _ => None
- }
- }
-
- val conflicts = (hardConflicts ++ softConflicts).take(10)
-
- if (conflicts.nonEmpty) {
- log.warn("Detected {} conflict(s) for entity {}", conflicts.size, entityClaims.head.claim.entityId.value)
- }
- conflicts
- }
- }.toVector
- }
-
- /**
- * Pass 3: LLM-assisted conflict detection.
- *
- * Takes the conflicts found by Pass 1+2 and asks the LLM to evaluate
- * whether each pair truly conflicts semantically. This can:
- * - Upgrade the reason with LLM explanation
- * - Dismiss false positives (remove conflict)
- * - Provide a preferred claim recommendation
- *
- * Only runs if an LlmJudge is provided.
- */
- def refineWithLlm(
- conflicts: Vector[ConflictReport],
- claims: Vector[ScoredClaim]
- ): IO[Vector[ConflictReport]] = {
- llmJudge match {
- case None => IO.pure(conflicts) // No LLM, return as-is
- case Some(judge) =>
- conflicts.traverse { conflict =>
- val claimA = claims.find(_.claim.id == conflict.claimA).map(_.claim)
- val claimB = claims.find(_.claim.id == conflict.claimB).map(_.claim)
-
- (claimA, claimB) match {
- case (Some(a), Some(b)) =>
- judge.judge(a, b).map {
- case Some(judgment) if judgment.isConflict =>
- Some(conflict.copy(
- reason = s"${conflict.reason} [LLM: ${judgment.explanation}]",
- recommendation = judgment.preferredClaim
- .flatMap(id => claims.find(_.claim.id == id))
- .map(sc => s"LLM recommends '${sc.claim.statement}' (LLM confidence: ${judgment.confidence})")
- .getOrElse(conflict.recommendation)
- ))
- case Some(_) =>
- // LLM says not a conflict — dismiss
- None
- case None =>
- // LLM didn't respond — keep original
- Some(conflict)
- }
- case _ =>
- // Claims not found — keep original conflict
- IO.pure(Some(conflict))
- }
- }.map(_.flatten)
- }
- }
-
- /** Check whether two statements share a field prefix (e.g. both start with "calls:"). */
- private def shareFieldPrefix(a: String, b: String): Boolean = {
- val idxA = a.indexOf(':')
- val idxB = b.indexOf(':')
- idxA > 0 && idxB > 0 && a.substring(0, idxA) == b.substring(0, idxB)
- }
-
- /** Check whether two statement strings share at least one 4-char stem. */
- private def keywordsOverlap(a: String, b: String): Boolean = {
- val stemsA = tokenize(a).map(_.take(4).toLowerCase).toSet
- val stemsB = tokenize(b).map(_.take(4).toLowerCase).toSet
- (stemsA intersect stemsB).nonEmpty
- }
-
- /** Split on non-alphanumeric runs, keep tokens of length >= 3. */
- private def tokenize(s: String): Vector[String] =
- s.split("[^a-zA-Z0-9]+").filter(_.length >= 3).toVector
-
- private def makeConflict(a: ScoredClaim, b: ScoredClaim, reason: String): ConflictReport = {
- val (winner, _) =
- if (a.confidence.score >= b.confidence.score) (a, b) else (b, a)
- ConflictReport(
- id = ConflictId(UUID.randomUUID()),
- claimA = a.claim.id,
- claimB = b.claim.id,
- reason = reason,
- recommendation = s"Prefer '${winner.claim.statement}' (confidence: ${winner.confidence.score})"
- )
- }
-}
-
-/** Stub kept for backward compatibility -- prefer ConflictDetectorImpl. */
-class DefaultConflictDetector extends ConflictDetector {
- def detect(claims: Vector[ScoredClaim]): Vector[ConflictReport] =
- Vector.empty
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/ContextRanker.scala b/memory-layer/src/main/scala/ix/memory/context/ContextRanker.scala
deleted file mode 100644
index c570b9e6..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/ContextRanker.scala
+++ /dev/null
@@ -1,9 +0,0 @@
-package ix.memory.context
-
-import ix.memory.model.ScoredClaim
-
-object ContextRanker {
-
- def rank(claims: Vector[ScoredClaim]): Vector[ScoredClaim] =
- claims.sortBy(sc => -sc.finalScore)
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/ContextService.scala b/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
deleted file mode 100644
index aea33644..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
+++ /dev/null
@@ -1,171 +0,0 @@
-package ix.memory.context
-
-import cats.effect.IO
-import cats.syntax.parallel._
-
-import ix.memory.db.GraphQueryApi
-import ix.memory.model._
-
-class ContextService(
- queryApi: GraphQueryApi,
- seeder: GraphSeeder,
- expander: GraphExpander,
- claimCollector: ClaimCollector,
- confidenceScorer: ConfidenceScorer,
- conflictDetector: ConflictDetector
-) {
-
- def query(question: String,
- asOfRev: Option[Rev] = None,
- depth: Option[String] = None): IO[StructuredContext] =
- for {
- // 0. Resolve revision + depth (used for scoring + output shaping)
- rev <- asOfRev.fold(queryApi.getLatestRev)(IO.pure)
- // Normalize depth values coming from different clients:
- // - MCP server may send shallow|deep
- // - CLI may send compact|standard|full
- effectiveDepthRaw = depth.getOrElse("standard").toLowerCase
- effectiveDepth = effectiveDepthRaw match {
- case "shallow" => "compact"
- case "deep" => "full"
- case other => other
- }
-
- // Strict limits to prevent huge MCP payloads
- (claimLimit, nodeLimit, edgeLimit, conflictLimit) = effectiveDepth match {
- case "compact" => (8, 8, 10, 3)
- case "full" => (250, 200, 800, 100)
- case _ => (30, 40, 120, 15) // standard
- }
-
- // 1. Extract entity keywords from the natural language query
- terms <- IO.pure(EntityExtractor.extract(question))
-
- // 2. Seed: search the graph for nodes matching extracted terms
- seeds <- seeder.seed(terms, asOfRev)
-
- // 3. Expand: traverse the graph 1 hop from each seed (with edge priority)
- expanded <- expander.expand(seeds.map(_.id), hops = 1, asOfRev = asOfRev)
-
- // 4. Collect claims for all discovered nodes
- allNodeIds = (seeds.map(_.id) ++ expanded.nodes.map(_.id)).distinct
- claims <- claimCollector.collect(allNodeIds)
-
- // 4.5. Detect stale sources + count corroboration in parallel
- stalenessAndCorroboration <- (
- StalenessDetector.detect(claims),
- IO.blocking(CorroborationCounter.count(claims))
- ).parTupled
- (stalenessMap, corroborationMap) = stalenessAndCorroboration
-
- // 5. Score each claim for confidence
- scored = claims.map { c =>
- confidenceScorer.score(c, ScoringContext(
- latestRev = rev,
- sourceChanged = stalenessMap.getOrElse(c.id, false),
- corroboratingCount = corroborationMap.getOrElse(c.id, 0),
- conflictState = ConflictState.NoConflict,
- intentAlignment = IntentAlignment.NoConnection,
- observedAt = c.provenance.observedAt
- ))
- }
-
- // 7. Relevance score: weight claims by hop distance from seeds, boost exact matches, prefer symbols
- nodeKindMap: Map[NodeId, NodeKind] = (seeds ++ expanded.nodes).map(n => n.id -> n.kind).toMap
- relevant = RelevanceScorer.scoreWithTerms(scored, seeds.map(_.id).toSet, expanded.edges, terms, nodeKindMap)
-
- // 8. Rank claims by finalScore (relevance x confidence) descending
- ranked = ContextRanker.rank(relevant)
-
- // 8.5 Trim claims to strict limit (prevents massive MCP responses)
- rankedTrimmed = ranked.take(claimLimit)
-
- // 9. Detect conflicts only within the trimmed claim set
- pass12Conflicts = conflictDetector.detect(rankedTrimmed.toVector)
-
- // 9.5 Refine conflicts with LLM (Pass 3, if available) using the same trimmed claims
- conflictsAll <- conflictDetector match {
- case impl: ConflictDetectorImpl => impl.refineWithLlm(pass12Conflicts, rankedTrimmed.toVector)
- case _ => IO.pure(pass12Conflicts)
- }
-
- // Only surface hard conflicts in query responses.
- // Heuristic "Potential inconsistency" reports are too noisy for MCP/LLM output.
- hardConflictsOnly: Vector[ConflictReport] = conflictsAll.filterNot(_.reason.startsWith("Potential inconsistency"))
- conflictsTrimmed: Vector[ConflictReport] = hardConflictsOnly.take(conflictLimit)
-
- // Collect decisions and intents from expanded nodes (apply strict node/edge limits)
- allNodesAll: Vector[GraphNode] = (seeds ++ expanded.nodes).distinctBy(_.id)
- allNodes: Vector[GraphNode] = allNodesAll.take(nodeLimit)
- edgesTrimmed: Vector[GraphEdge] = expanded.edges.take(edgeLimit)
-
- // Lightweight summaries for LLM navigation (lets the model drill down with ix_entity/ix_expand)
- nodeSummaries: Vector[NodeSummary] = allNodes.map { n =>
- val display = n.attrs.hcursor.get[String]("name").toOption
- .orElse(n.attrs.hcursor.get[String]("title").toOption)
- .orElse(n.attrs.hcursor.get[String]("heading").toOption)
- .getOrElse {
- val uri = n.provenance.sourceUri
- val basename = uri.split("/").lastOption.getOrElse(uri)
- if (basename.nonEmpty) basename else n.id.value.toString.take(8)
- }
- NodeSummary(n.id, n.kind, display, rev)
- }
- edgeSummaries: Vector[EdgeSummary] = edgesTrimmed.map(e => EdgeSummary(e.id, e.src, e.dst, e.predicate, rev))
-
- // In compact/standard, omit full nodes/edges to keep MCP payload small.
- nodesOut: Vector[GraphNode] = if (effectiveDepth == "full") allNodes else Vector.empty
- edgesOut: Vector[GraphEdge] = if (effectiveDepth == "full") edgesTrimmed else Vector.empty
-
- decisions: Vector[DecisionReport] = allNodes.filter(_.kind == NodeKind.Decision).map(toDecisionReport(_, rev))
- intents: Vector[IntentReport] = allNodes.filter(_.kind == NodeKind.Intent).map(toIntentReport)
-
- // Shape claims and compactClaims based on depth
- claimsOut: List[ScoredClaim] = effectiveDepth match {
- case "compact" => Nil
- case _ => rankedTrimmed.toList
- }
- compactClaimsOut: List[CompactScoredClaim] = effectiveDepth match {
- case "full" => Nil
- case _ => rankedTrimmed.map(_.toCompact).toList
- }
-
- } yield StructuredContext(
- claims = claimsOut,
- compactClaims = compactClaimsOut,
- conflicts = conflictsTrimmed.toList,
- decisions = decisions.toList,
- intents = intents.toList,
- nodes = nodesOut.toList,
- edges = edgesOut.toList,
- nodeSummaries = nodeSummaries.toList,
- edgeSummaries = edgeSummaries.toList,
- metadata = ContextMetadata(
- query = question,
- seedEntities = seeds.map(_.id).take(25).toList,
- hopsExpanded = 1,
- asOfRev = rev,
- depth = Some(effectiveDepthRaw)
- )
- )
-
- private def toDecisionReport(node: GraphNode, rev: Rev): DecisionReport =
- DecisionReport(
- title = node.attrs.hcursor.get[String]("title").getOrElse(node.id.value.toString),
- rationale = node.attrs.hcursor.get[String]("rationale").getOrElse(""),
- entityId = Some(node.id),
- intentId = node.attrs.hcursor.get[String]("intent_id").toOption.flatMap(s =>
- scala.util.Try(java.util.UUID.fromString(s)).toOption.map(NodeId(_))
- ),
- rev = rev
- )
-
- private def toIntentReport(node: GraphNode): IntentReport =
- IntentReport(
- id = node.id,
- statement = node.attrs.hcursor.get[String]("statement").getOrElse(""),
- status = node.attrs.hcursor.get[String]("status").getOrElse("active"),
- confidence = node.attrs.hcursor.get[Double]("confidence").getOrElse(1.0),
- parentIntent = node.attrs.hcursor.get[String]("parent_intent").toOption
- )
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/CorroborationCounter.scala b/memory-layer/src/main/scala/ix/memory/context/CorroborationCounter.scala
deleted file mode 100644
index d1be2b07..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/CorroborationCounter.scala
+++ /dev/null
@@ -1,42 +0,0 @@
-package ix.memory.context
-
-import ix.memory.model._
-
-/**
- * Counts how many independent sources corroborate each claim.
- *
- * Two claims "corroborate" each other if:
- * 1. They share the same entityId
- * 2. Their statements share at least one keyword stem (same 4-char prefix)
- * 3. They come from different source URIs (independent sources)
- *
- * Returns a Map from ClaimId to corroboration count.
- */
-object CorroborationCounter {
-
- def count(claims: Vector[Claim]): Map[ClaimId, Int] = {
- // Group claims by entityId
- val byEntity = claims.groupBy(_.entityId)
-
- byEntity.values.flatMap { entityClaims =>
- // For each claim, count how many other claims from different sources
- // have overlapping keyword stems
- entityClaims.map { claim =>
- val stems = tokenize(claim.statement).map(_.take(4).toLowerCase).toSet
- val corroborating = entityClaims.count { other =>
- other.id != claim.id &&
- other.provenance.sourceUri != claim.provenance.sourceUri &&
- {
- val otherStems = tokenize(other.statement).map(_.take(4).toLowerCase).toSet
- (stems intersect otherStems).nonEmpty
- }
- }
- claim.id -> corroborating
- }
- }.toMap
- }
-
- /** Split on non-alphanumeric runs, keep tokens of length >= 3. */
- private def tokenize(s: String): Vector[String] =
- s.split("[^a-zA-Z0-9]+").filter(_.length >= 3).toVector
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/EntityExtractor.scala b/memory-layer/src/main/scala/ix/memory/context/EntityExtractor.scala
deleted file mode 100644
index dca489be..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/EntityExtractor.scala
+++ /dev/null
@@ -1,18 +0,0 @@
-package ix.memory.context
-
-object EntityExtractor {
-
- private val stopwords = Set(
- "how", "does", "the", "is", "a", "an", "what", "when", "where", "why",
- "do", "it", "in", "of", "to", "for", "and", "or", "not", "with", "by",
- "from", "at", "on", "this", "that", "are", "was", "be", "has", "have", "can"
- )
-
- def extract(query: String): Vector[String] =
- query.toLowerCase
- .split("[\\s,;.!?]+")
- .map(_.trim)
- .filter(w => w.nonEmpty && w.length > 1 && !stopwords.contains(w))
- .toVector
- .distinct
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/GraphExpander.scala b/memory-layer/src/main/scala/ix/memory/context/GraphExpander.scala
deleted file mode 100644
index 346d8b39..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/GraphExpander.scala
+++ /dev/null
@@ -1,32 +0,0 @@
-package ix.memory.context
-
-import cats.effect.IO
-import cats.syntax.parallel._
-
-import ix.memory.db.{GraphQueryApi, Direction, ExpandResult}
-import ix.memory.model._
-
-class GraphExpander(queryApi: GraphQueryApi) {
-
- /** Low-priority edge predicates — skipped at hop 2+ to focus expansion. */
- private val lowPriorityPredicates: Set[String] =
- Set("mentioned_in", "changed_with", "same_as")
-
- def expand(seeds: Vector[NodeId], hops: Int = 1,
- predicates: Option[Set[String]] = None,
- asOfRev: Option[Rev] = None): IO[ExpandResult] =
- seeds
- .parTraverse { nodeId =>
- queryApi.expand(nodeId, Direction.Both, predicates, hops, asOfRev)
- }
- .map { results =>
- val allNodes = results.flatMap(_.nodes).distinctBy(_.id)
- val allEdges = results.flatMap(_.edges).distinctBy(_.id)
- // Filter out low-priority edges when expanding beyond 1 hop
- val filteredEdges = if (hops > 1)
- allEdges.filterNot(e => lowPriorityPredicates.contains(e.predicate.value.toLowerCase))
- else
- allEdges
- ExpandResult(allNodes, filteredEdges)
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/GraphSeeder.scala b/memory-layer/src/main/scala/ix/memory/context/GraphSeeder.scala
deleted file mode 100644
index 13c1307c..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/GraphSeeder.scala
+++ /dev/null
@@ -1,16 +0,0 @@
-package ix.memory.context
-
-import cats.effect.IO
-import cats.syntax.parallel._
-
-import ix.memory.db.GraphQueryApi
-import ix.memory.model._
-
-class GraphSeeder(queryApi: GraphQueryApi) {
-
- def seed(terms: Vector[String],
- asOfRev: Option[Rev] = None): IO[Vector[GraphNode]] =
- terms
- .parTraverse(term => queryApi.searchNodes(term))
- .map(_.flatten.distinctBy(_.id))
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/LlmJudge.scala b/memory-layer/src/main/scala/ix/memory/context/LlmJudge.scala
deleted file mode 100644
index 9c6fede6..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/LlmJudge.scala
+++ /dev/null
@@ -1,36 +0,0 @@
-package ix.memory.context
-
-import cats.effect.IO
-import ix.memory.model._
-
-/**
- * Trait for LLM-based conflict judgment.
- * Implementations should send the two claim statements to an LLM
- * and return a judgment on whether they truly conflict.
- */
-trait LlmJudge {
- /**
- * Ask the LLM whether two claims conflict.
- * @return Some(judgment) if the LLM detects a semantic conflict, None if no conflict
- */
- def judge(claimA: Claim, claimB: Claim): IO[Option[LlmJudgment]]
-}
-
-/**
- * The LLM's judgment about a pair of claims.
- */
-case class LlmJudgment(
- isConflict: Boolean,
- confidence: Double, // 0.0 to 1.0 — how confident the LLM is in its judgment
- explanation: String, // Why the LLM thinks they do or don't conflict
- preferredClaim: Option[ClaimId] // Which claim the LLM prefers, if any
-)
-
-/**
- * Stub implementation — always returns None (no LLM available).
- * Replace with real implementation when LLM integration is ready.
- */
-class StubLlmJudge extends LlmJudge {
- def judge(claimA: Claim, claimB: Claim): IO[Option[LlmJudgment]] =
- IO.pure(None)
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/RelevanceScorer.scala b/memory-layer/src/main/scala/ix/memory/context/RelevanceScorer.scala
deleted file mode 100644
index e938abd9..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/RelevanceScorer.scala
+++ /dev/null
@@ -1,50 +0,0 @@
-package ix.memory.context
-
-import ix.memory.model._
-
-object RelevanceScorer {
- def score(
- claims: Vector[ScoredClaim],
- seedNodeIds: Set[NodeId],
- expandedEdges: Vector[GraphEdge]
- ): Vector[ScoredClaim] =
- scoreWithTerms(claims, seedNodeIds, expandedEdges, Vector.empty, Map.empty)
-
- def scoreWithTerms(
- claims: Vector[ScoredClaim],
- seedNodeIds: Set[NodeId],
- expandedEdges: Vector[GraphEdge],
- queryTerms: Vector[String],
- nodeKindMap: Map[NodeId, NodeKind] = Map.empty
- ): Vector[ScoredClaim] = {
- val oneHopIds = expandedEdges
- .flatMap(e => Vector(e.src, e.dst))
- .toSet -- seedNodeIds
- val termsLower = queryTerms.map(_.toLowerCase)
-
- claims.map { sc =>
- val hopRelevance =
- if (seedNodeIds.contains(sc.claim.entityId)) 1.0
- else if (oneHopIds.contains(sc.claim.entityId)) 0.7
- else 0.4
-
- val fieldLower = sc.claim.statement.toLowerCase
- val exactBoost = if (termsLower.exists(t => fieldLower.contains(t))) 1.2 else 1.0
-
- val pathLower = sc.claim.provenance.sourceUri.toLowerCase
- val pathBoost = if (termsLower.exists(t => pathLower.contains(t))) 1.1 else 1.0
-
- val kindBoost = nodeKindMap.get(sc.claim.entityId) match {
- case Some(NodeKind.Method) | Some(NodeKind.Function) => 1.3
- case Some(NodeKind.Class) | Some(NodeKind.Trait)
- | Some(NodeKind.Object) | Some(NodeKind.Interface) => 1.3
- case Some(NodeKind.Module) => 1.2
- case Some(NodeKind.File) => 0.6
- case _ => 1.0
- }
-
- val relevance = math.min(1.0, hopRelevance * exactBoost * pathBoost * kindBoost)
- sc.copy(relevance = relevance, finalScore = relevance * sc.confidence.score)
- }
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/context/StalenessDetector.scala b/memory-layer/src/main/scala/ix/memory/context/StalenessDetector.scala
deleted file mode 100644
index 83b56a4e..00000000
--- a/memory-layer/src/main/scala/ix/memory/context/StalenessDetector.scala
+++ /dev/null
@@ -1,57 +0,0 @@
-package ix.memory.context
-
-import java.nio.file.{Files, Paths}
-import java.security.MessageDigest
-
-import cats.effect.IO
-import ix.memory.model._
-
-/**
- * Detects whether claims are stale by comparing the current source file hash
- * to the hash recorded in the claim's provenance.
- *
- * A claim is "stale" if:
- * - Its source file's current hash differs from provenance.sourceHash
- * - Its source file no longer exists
- * - Its provenance has no sourceHash (unknown state, treated as potentially stale)
- */
-object StalenessDetector {
-
- /**
- * For a collection of claims, determine which have stale sources.
- * Returns a Map from ClaimId to whether the source has changed.
- *
- * Uses IO since file system access is a side effect.
- */
- def detect(claims: Vector[Claim]): IO[Map[ClaimId, Boolean]] = IO.blocking {
- // Group by sourceUri to avoid hashing the same file multiple times
- val bySource = claims.groupBy(_.provenance.sourceUri)
-
- bySource.flatMap { case (sourceUri, sourceClaims) =>
- val currentHash = computeHash(sourceUri)
- sourceClaims.map { claim =>
- val changed = claim.provenance.sourceHash match {
- case Some(recordedHash) => currentHash.fold(true)(_ != recordedHash) // file gone or hash differs
- case None => false // no recorded hash, can't determine staleness -- treat as fresh
- }
- claim.id -> changed
- }
- }
- }
-
- /** Compute SHA-256 hash of a file, returning None if the file doesn't exist or can't be read. */
- private def computeHash(sourceUri: String): Option[String] = {
- try {
- val path = Paths.get(sourceUri)
- if (Files.exists(path) && Files.isRegularFile(path)) {
- val bytes = Files.readAllBytes(path)
- val digest = MessageDigest.getInstance("SHA-256")
- Some(digest.digest(bytes).map("%02x".format(_)).mkString)
- } else {
- None // File doesn't exist
- }
- } catch {
- case _: Exception => None // Can't read file
- }
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoClient.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoClient.scala
deleted file mode 100644
index 0f12b9e1..00000000
--- a/memory-layer/src/main/scala/ix/memory/db/ArangoClient.scala
+++ /dev/null
@@ -1,264 +0,0 @@
-package ix.memory.db
-
-import scala.concurrent.duration._
-
-import cats.effect.{IO, Resource}
-import cats.syntax.traverse._
-import com.arangodb.{ArangoDB, ArangoDatabase}
-import com.arangodb.entity.MultiDocumentEntity
-import com.arangodb.model.{AqlQueryOptions, DocumentCreateOptions, OverwriteMode, StreamTransactionOptions}
-import com.fasterxml.jackson.databind.ObjectMapper
-import io.circe.Json
-import io.circe.parser.{parse => parseJson}
-
-import scala.jdk.CollectionConverters._
-
-class ArangoClient private (db: ArangoDatabase) {
- private val mapper = new ObjectMapper()
-
- private def toJson(value: AnyRef): IO[Json] =
- IO.fromEither(
- parseJson(mapper.writeValueAsString(value))
- .left.map(e => new RuntimeException(s"Failed to parse ArangoDB result: ${e.message}"))
- )
-
- private def asJava(bindVars: Map[String, AnyRef]): java.util.Map[String, AnyRef] = {
- val m = new java.util.HashMap[String, AnyRef]()
- bindVars.foreach { case (k, v) => m.put(k, v) }
- m
- }
-
- private def queryOptions(txId: Option[String]): AqlQueryOptions =
- txId.fold(new AqlQueryOptions())(id => new AqlQueryOptions().streamTransactionId(id))
-
- def query(
- aql: String,
- bindVars: Map[String, AnyRef] = Map.empty,
- txId: Option[String] = None
- ): IO[List[Json]] =
- IO.blocking {
- val cursor = db.query(aql, classOf[AnyRef], asJava(bindVars), queryOptions(txId))
- cursor.asListRemaining().asScala.toList
- }.flatMap(_.traverse(toJson))
-
- def queryOne(
- aql: String,
- bindVars: Map[String, AnyRef] = Map.empty,
- txId: Option[String] = None
- ): IO[Option[Json]] =
- query(aql, bindVars, txId).map(_.headOption)
-
- def execute(
- aql: String,
- bindVars: Map[String, AnyRef] = Map.empty,
- txId: Option[String] = None
- ): IO[Unit] =
- IO.blocking {
- db.query(aql, classOf[Void], asJava(bindVars), queryOptions(txId))
- ()
- }
-
- def queryLong(
- aql: String,
- bindVars: Map[String, AnyRef] = Map.empty,
- txId: Option[String] = None
- ): IO[Long] =
- queryOne(aql, bindVars, txId).map {
- _.flatMap(_.asNumber.flatMap(_.toLong)).getOrElse(0L)
- }
-
- /** Bulk insert/replace documents into a collection. Uses Document API (not AQL). */
- def bulkInsert(
- collection: String,
- documents: Seq[java.util.Map[String, AnyRef]],
- overwriteMode: String = "replace"
- ): IO[Int] =
- if (documents.isEmpty) IO.pure(0)
- else IO.blocking {
- val opts = new DocumentCreateOptions()
- .overwriteMode(OverwriteMode.valueOf(overwriteMode))
- .waitForSync(false)
- .silent(true)
- val docs = new java.util.ArrayList[java.util.Map[String, AnyRef]](documents.size)
- documents.foreach(docs.add)
- val result = db.collection(collection).insertDocuments(docs, opts)
- documents.size - result.getErrors.size
- }
-
- /** Bulk insert edge documents. Uses 'update' overwrite mode by default —
- * edge documents are keyed by a deterministic ID so update-merge is safe
- * and faster than full replace (avoids read-delete-write per document). */
- def bulkInsertEdges(
- collection: String,
- documents: Seq[java.util.Map[String, AnyRef]],
- overwriteMode: String = "update"
- ): IO[Int] = bulkInsert(collection, documents, overwriteMode)
-
- // ── Stream Transaction lifecycle ──────────────────────────────────
-
- def beginTransaction(
- readCollections: Seq[String],
- writeCollections: Seq[String]
- ): IO[String] =
- IO.blocking {
- val opts = new StreamTransactionOptions()
- .readCollections(readCollections: _*)
- .writeCollections(writeCollections: _*)
- db.beginStreamTransaction(opts).getId
- }
-
- def commitTransaction(txId: String): IO[Unit] =
- IO.blocking {
- db.commitStreamTransaction(txId)
- ()
- }
-
- def abortTransaction(txId: String): IO[Unit] =
- IO.blocking {
- db.abortStreamTransaction(txId)
- ()
- }
-
- def ensureSchema(): IO[Unit] = ArangoSchema.ensure(db)
-
- /** Abort all active stream transactions so truncation can acquire locks. */
- private def abortAllTransactions(): IO[Unit] =
- IO.blocking {
- val txns = db.getStreamTransactions().asScala.toList
- txns.filter(_.getState.toString == "running").foreach { tx =>
- try db.abortStreamTransaction(tx.getId)
- catch { case _: Exception => () }
- }
- }
-
- /** Truncate all graph state. Destructive — dev use only. */
- def truncateGraph(): IO[Unit] =
- abortAllTransactions() *> IO.blocking {
- db.collection("nodes").truncate()
- db.collection("edges").truncate()
- db.collection("patches").truncate()
- db.collection("claims").truncate()
- db.collection("idempotency_keys").truncate()
- db.collection("revisions").truncate()
- db.collection("conflict_sets").truncate()
- ()
- }
-
- /**
- * Delete only code-derived nodes and edges, preserving planning artifacts
- * (goals/intents, plans, tasks, decisions, bugs and their linking edges).
- *
- * Uses larger batched REMOVE passes and avoids materializing `RETURN 1`
- * result sets for every deleted document. We first count the matching docs,
- * then execute enough bounded delete passes to cover that cardinality, with
- * a final recount only if needed. Edges are removed before nodes.
- */
- def truncateCodeGraph(): IO[Unit] = abortAllTransactions() *> {
- val batchSize = 5000
- val codeKinds = Array(
- "file", "function", "class", "method", "interface",
- "module", "chunk", "region"
- )
- val codePredicates = Array(
- "CALLS", "IMPORTS", "EXTENDS", "IMPLEMENTS",
- "CONTAINS", "DEFINES", "CONTAINS_CHUNK", "REFERENCES", "NEXT", "IN_REGION"
- )
-
- def removeAll(
- countAql: String,
- removeAql: String,
- bindVars: Map[String, AnyRef]
- ): IO[Unit] = {
- val varsWithBatch = bindVars + ("batchSize" -> Int.box(batchSize))
-
- def runPasses(remaining: Long): IO[Unit] = {
- val passes = math.ceil(remaining.toDouble / batchSize.toDouble).toInt
- (0 until passes).foldLeft(IO.unit) { (acc, _) =>
- acc *> execute(removeAql, varsWithBatch)
- }
- }
-
- def loop: IO[Unit] =
- queryLong(countAql, bindVars).flatMap { remaining =>
- if (remaining <= 0) IO.unit
- else runPasses(remaining) *> queryLong(countAql, bindVars).flatMap { tail =>
- if (tail <= 0) IO.unit else loop
- }
- }
-
- loop
- }
-
- for {
- // Edges first — avoids locking nodes that are about to be removed
- _ <- removeAll(
- "RETURN LENGTH(FOR e IN edges FILTER e.predicate IN @predicates RETURN 1)",
- "FOR e IN edges FILTER e.predicate IN @predicates LIMIT @batchSize REMOVE e IN edges",
- Map("predicates" -> codePredicates.asInstanceOf[AnyRef])
- )
- // Claims next — code ingests append versioned/retracted claims forever, so
- // reset-code must clear them before removing the corresponding nodes.
- _ <- removeAll(
- """RETURN LENGTH(
- | FOR c IN claims
- | FILTER LENGTH(
- | FOR n IN nodes
- | FILTER n.logical_id == c.entity_id
- | AND n.kind IN @kinds
- | LIMIT 1
- | RETURN 1
- | ) > 0
- | RETURN 1
- |)""".stripMargin,
- """FOR c IN claims
- | FILTER LENGTH(
- | FOR n IN nodes
- | FILTER n.logical_id == c.entity_id
- | AND n.kind IN @kinds
- | LIMIT 1
- | RETURN 1
- | ) > 0
- | LIMIT @batchSize
- | REMOVE c IN claims""".stripMargin,
- Map("kinds" -> codeKinds.asInstanceOf[AnyRef])
- )
- _ <- removeAll(
- "RETURN LENGTH(FOR n IN nodes FILTER n.kind IN @kinds RETURN 1)",
- "FOR n IN nodes FILTER n.kind IN @kinds LIMIT @batchSize REMOVE n IN nodes",
- Map("kinds" -> codeKinds.asInstanceOf[AnyRef])
- )
- _ <- removeAll(
- "RETURN LENGTH(FOR p IN patches FILTER STARTS_WITH(p.data.source.extractor, 'tree-sitter') RETURN 1)",
- "FOR p IN patches FILTER STARTS_WITH(p.data.source.extractor, 'tree-sitter') LIMIT @batchSize REMOVE p IN patches",
- Map.empty
- )
- } yield ()
- }
-}
-
-object ArangoClient {
- def resource(
- host: String,
- port: Int,
- database: String,
- user: String,
- password: String
- ): Resource[IO, ArangoClient] =
- Resource.make(
- IO.blocking {
- val arango = new ArangoDB.Builder()
- .host(host, port)
- .user(user)
- .password(if (password.isEmpty) null else password)
- .maxConnections(64)
- .build()
- val existingDbs = arango.getDatabases.asScala.toSet
- if (!existingDbs.contains(database)) {
- arango.createDatabase(database)
- }
- (arango, new ArangoClient(arango.db(database)))
- }
- ) { case (arango, _) =>
- IO.blocking(arango.shutdown())
- }.map(_._2)
-}
diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
deleted file mode 100644
index 9d2b9b84..00000000
--- a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
+++ /dev/null
@@ -1,727 +0,0 @@
-package ix.memory.db
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.syntax.all._
-import io.circe.Json
-
-import org.slf4j.LoggerFactory
-
-import ix.memory.model._
-
-class ArangoGraphQueryApi(client: ArangoClient) extends GraphQueryApi {
-
- private val log = LoggerFactory.getLogger(classOf[ArangoGraphQueryApi])
- private val SymbolProjectionKinds = Vector(
- NodeKind.Function,
- NodeKind.Method,
- NodeKind.Class,
- NodeKind.Interface,
- NodeKind.Trait,
- NodeKind.Object,
- NodeKind.Variable,
- )
- private val SymbolProjectionPredicates = Set(
- "CONTAINS",
- "CALLS",
- "IMPORTS",
- "REFERENCES",
- "EXTENDS",
- "IMPLEMENTS",
- )
-
- // ── Public API ──────────────────────────────────────────────────────
-
- override def getNode(id: NodeId, asOfRev: Option[Rev] = None): IO[Option[GraphNode]] =
- for {
- rev <- asOfRev.fold(getLatestRev)(IO.pure)
- result <- client.queryOne(
- """FOR n IN nodes
- | FILTER n.logical_id == @id
- | AND n.created_rev <= @rev
- | AND (n.deleted_rev == null OR @rev < n.deleted_rev)
- | RETURN n""".stripMargin,
- Map(
- "id" -> id.value.toString.asInstanceOf[AnyRef],
- "rev" -> Long.box(rev.value).asInstanceOf[AnyRef]
- )
- )
- } yield result.flatMap(parseNode)
-
- override def findNodesByKind(kind: NodeKind, limit: Int = 100): IO[Vector[GraphNode]] =
- client.query(
- """FOR n IN nodes
- | FILTER n.kind == @kind
- | AND n.deleted_rev == null
- | LIMIT @limit
- | RETURN n""".stripMargin,
- Map(
- "kind" -> nodeKindToString(kind).asInstanceOf[AnyRef],
- "limit" -> Int.box(limit).asInstanceOf[AnyRef]
- )
- ).map(_.flatMap(parseNode).toVector)
-
- override def listDecisions(limit: Int = 50, topic: Option[String] = None): IO[Vector[GraphNode]] = {
- val topicFilter = topic match {
- case Some(_) =>
- """AND (
- | CONTAINS(LOWER(TO_STRING(n.attrs.title)), LOWER(@topic))
- | OR CONTAINS(LOWER(TO_STRING(n.attrs.rationale)), LOWER(@topic))
- |)""".stripMargin
- case None => ""
- }
- val aql =
- s"""FOR n IN nodes
- | FILTER n.kind == "decision"
- | AND n.deleted_rev == null
- | $topicFilter
- | SORT n.created_at DESC
- | LIMIT @limit
- | RETURN n""".stripMargin
- val binds = scala.collection.mutable.Map[String, AnyRef](
- "limit" -> Int.box(limit).asInstanceOf[AnyRef]
- )
- topic.foreach(t => binds += ("topic" -> t.asInstanceOf[AnyRef]))
- client.query(aql, binds.toMap).map(_.flatMap(parseNode).toVector)
- }
-
- override def searchNodes(
- text: String,
- limit: Int = 20,
- kind: Option[String] = None,
- language: Option[String] = None,
- asOfRev: Option[Rev] = None,
- nameOnly: Boolean = false
- ): IO[Vector[GraphNode]] = {
- val liveFilter = asOfRev match {
- case Some(r) => s"AND n.created_rev <= ${r.value} AND (n.deleted_rev == null OR ${r.value} < n.deleted_rev)"
- case None => "AND n.deleted_rev == null"
- }
- val claimLiveFilter = asOfRev match {
- case Some(r) => s"AND c.created_rev <= ${r.value} AND (c.deleted_rev == null OR ${r.value} < c.deleted_rev)"
- case None => "AND c.deleted_rev == null"
- }
- val kindFilter = kind.map(_ => "FILTER n2.kind == @kind").getOrElse("")
- val langFilter = language.map(_ => "FILTER CONTAINS(LOWER(TO_STRING(n2.provenance.source_uri)), LOWER(@language))").getOrElse("")
-
- val nameOnlyAql =
- s"""LET name_matches = (
- | FOR n IN nodes
- | FILTER CONTAINS(LOWER(n.name), LOWER(@text))
- | $liveFilter
- | LET w = (LOWER(n.name) == LOWER(@text) ? 100 : 60)
- | RETURN DISTINCT { id: n.logical_id, weight: w }
- |)
- |
- |FOR entry IN name_matches
- | FOR n2 IN nodes
- | FILTER n2.logical_id == entry.id AND n2.deleted_rev == null
- | $kindFilter
- | $langFilter
- | LET symbol_priority = n2.kind IN ["function", "method", "class", "trait", "object", "interface"] ? 0 : 1
- | SORT entry.weight DESC, symbol_priority ASC, n2.name ASC
- | LIMIT @limit
- | RETURN MERGE(n2, { attrs: MERGE(n2.attrs, { _search_weight: entry.weight }) })""".stripMargin
-
- // Gate attr_matches on term length to avoid noise from short/common terms
- val attrMatchAql = if (text.length >= 6) {
- s"""LET attr_matches = (
- | FOR n IN nodes
- | FILTER CONTAINS(LOWER(TO_STRING(n.attrs)), LOWER(@text))
- | $liveFilter
- | RETURN DISTINCT { id: n.logical_id, weight: 10 }
- |)""".stripMargin
- } else {
- "LET attr_matches = []"
- }
-
- val fullAql =
- s"""LET name_matches = (
- | FOR n IN nodes
- | FILTER CONTAINS(LOWER(n.name), LOWER(@text))
- | $liveFilter
- | LET w = (LOWER(n.name) == LOWER(@text) ? 100 : 60)
- | RETURN DISTINCT { id: n.logical_id, weight: w }
- |)
- |
- |LET provenance_matches = (
- | FOR n IN nodes
- | FILTER CONTAINS(LOWER(n.provenance.source_uri), LOWER(@text))
- | $liveFilter
- | RETURN DISTINCT { id: n.logical_id, weight: 40 }
- |)
- |
- |LET claim_matches = (
- | FOR c IN claims
- | FILTER (
- | CONTAINS(LOWER(c.field), LOWER(@text))
- | OR CONTAINS(LOWER(TO_STRING(c.value)), LOWER(@text))
- | )
- | $claimLiveFilter
- | RETURN DISTINCT { id: c.entity_id, weight: 20 }
- |)
- |
- |LET decision_matches = (
- | FOR n IN nodes
- | FILTER n.kind == "decision"
- | $liveFilter
- | AND (
- | CONTAINS(LOWER(TO_STRING(n.attrs.title)), LOWER(@text))
- | OR CONTAINS(LOWER(TO_STRING(n.attrs.rationale)), LOWER(@text))
- | )
- | RETURN DISTINCT { id: n.logical_id, weight: 20 }
- |)
- |
- |$attrMatchAql
- |
- |LET all_scored = UNION(name_matches, provenance_matches, claim_matches, decision_matches, attr_matches)
- |
- |LET best_per_id = (
- | FOR s IN all_scored
- | COLLECT id = s.id AGGREGATE maxWeight = MAX(s.weight)
- | RETURN { id, weight: maxWeight }
- |)
- |
- |FOR entry IN best_per_id
- | FOR n2 IN nodes
- | FILTER n2.logical_id == entry.id AND n2.deleted_rev == null
- | $kindFilter
- | $langFilter
- | LET symbol_priority = n2.kind IN ["function", "method", "class", "trait", "object", "interface"] ? 0 : 1
- | SORT entry.weight DESC, symbol_priority ASC, n2.name ASC
- | LIMIT @limit
- | RETURN MERGE(n2, { attrs: MERGE(n2.attrs, { _search_weight: entry.weight }) })""".stripMargin
-
- val aql = if (nameOnly) nameOnlyAql else fullAql
-
- val binds = scala.collection.mutable.Map[String, AnyRef](
- "text" -> text.asInstanceOf[AnyRef],
- "limit" -> Int.box(limit).asInstanceOf[AnyRef]
- )
- kind.foreach(k => binds += ("kind" -> k.asInstanceOf[AnyRef]))
- language.foreach(l => binds += ("language" -> l.asInstanceOf[AnyRef]))
-
- client.query(aql, binds.toMap).map(_.flatMap(parseNode).toVector)
- }
-
- override def expand(
- nodeId: NodeId,
- direction: Direction,
- predicates: Option[Set[String]] = None,
- hops: Int = 1,
- asOfRev: Option[Rev] = None
- ): IO[ExpandResult] = {
- val predicateFilter = predicates.filter(_.nonEmpty) match {
- case Some(_) => " AND e.predicate IN @predicates"
- case None => ""
- }
-
- // I4: always use MVCC filter with resolved rev (never bare deleted_rev == null)
- val deletedFilter = " AND e.created_rev <= @rev AND (e.deleted_rev == null OR @rev < e.deleted_rev)"
-
- def buildEdgeQuery(srcOrDst: String): String =
- s"""FOR e IN edges
- | FILTER e.$srcOrDst == @nodeId
- | $deletedFilter
- | $predicateFilter
- | RETURN e""".stripMargin
-
- def buildBindVars(rev: Rev): Map[String, AnyRef] = {
- val base = Map(
- "nodeId" -> nodeId.value.toString.asInstanceOf[AnyRef],
- "rev" -> Long.box(rev.value).asInstanceOf[AnyRef]
- )
- predicates.filter(_.nonEmpty) match {
- case Some(ps) =>
- val javaList = new java.util.ArrayList[String]()
- ps.foreach(javaList.add)
- base + ("predicates" -> javaList.asInstanceOf[AnyRef])
- case None => base
- }
- }
-
- for {
- // I4: resolve revision once at the start so edges and nodes see the same snapshot
- resolvedRev <- asOfRev.fold(getLatestRev)(IO.pure)
- bindVars = buildBindVars(resolvedRev)
-
- outEdges <- direction match {
- case Direction.Out | Direction.Both =>
- client.query(buildEdgeQuery("src"), bindVars).map(_.flatMap(parseEdge).toVector)
- case Direction.In =>
- IO.pure(Vector.empty[GraphEdge])
- }
-
- inEdges <- direction match {
- case Direction.In | Direction.Both =>
- client.query(buildEdgeQuery("dst"), bindVars).map(_.flatMap(parseEdge).toVector)
- case Direction.Out =>
- IO.pure(Vector.empty[GraphEdge])
- }
-
- allEdges = (outEdges ++ inEdges).distinctBy(_.id)
-
- // Collect the "other side" node IDs
- neighborIds = allEdges.flatMap { e =>
- if (e.src == nodeId) Vector(e.dst)
- else if (e.dst == nodeId) Vector(e.src)
- else Vector(e.src, e.dst)
- }.distinct
-
- // I1: batch-fetch all neighbor nodes in a single AQL query instead of N+1
- nodes <- if (neighborIds.isEmpty) IO.pure(Vector.empty[GraphNode])
- else {
- val idList = new java.util.ArrayList[String]()
- neighborIds.foreach(nid => idList.add(nid.value.toString))
- val aql = """FOR n IN nodes
- | FILTER n.logical_id IN @ids
- | AND n.created_rev <= @rev AND (n.deleted_rev == null OR @rev < n.deleted_rev)
- | RETURN n""".stripMargin
- client.query(aql, Map(
- "ids" -> idList.asInstanceOf[AnyRef],
- "rev" -> Long.box(resolvedRev.value).asInstanceOf[AnyRef]
- )).map(_.flatMap(parseNode).toVector)
- }
-
- } yield ExpandResult(nodes, allEdges)
- }
-
- override def projectExpand(
- nodeId: NodeId,
- direction: Direction,
- predicates: Option[Set[String]] = None,
- hops: Int = 1,
- asOfRev: Option[Rev] = None
- ): IO[Option[ExpandProjection]] =
- for {
- resolvedRev <- asOfRev.fold(getLatestRev)(IO.pure)
- originOpt <- getNode(nodeId, Some(resolvedRev))
- projection <- originOpt match {
- case Some(origin) if origin.kind == NodeKind.File =>
- buildFileSymbolProjection(origin, resolvedRev, predicates)
- case _ =>
- IO.pure(None)
- }
- } yield projection
-
- override def getClaims(entityId: NodeId): IO[Vector[Claim]] =
- client.query(
- """FOR c IN claims
- | FILTER c.entity_id == @entityId
- | AND c.deleted_rev == null
- | RETURN c""".stripMargin,
- Map(
- "entityId" -> entityId.value.toString.asInstanceOf[AnyRef]
- )
- ).map(_.flatMap(parseClaim).toVector)
-
- override def getClaimsBatch(entityIds: Seq[NodeId]): IO[Vector[Claim]] =
- if (entityIds.isEmpty) IO.pure(Vector.empty)
- else {
- val idList = new java.util.ArrayList[String](entityIds.size)
- entityIds.foreach(id => idList.add(id.value.toString))
- client.query(
- """FOR c IN claims
- | FILTER c.entity_id IN @ids
- | AND c.deleted_rev == null
- | RETURN c""".stripMargin,
- Map("ids" -> idList.asInstanceOf[AnyRef])
- ).map(_.flatMap(parseClaim).toVector)
- }
-
- override def getLatestRev: IO[Rev] =
- client.queryOne(
- """FOR r IN revisions
- | FILTER r._key == @key
- | RETURN r.rev""".stripMargin,
- Map("key" -> "current".asInstanceOf[AnyRef])
- ).map(_.flatMap(_.as[Long].toOption).map(Rev(_)).getOrElse(Rev(0L)))
-
- override def getPatchesForEntity(entityId: NodeId): IO[List[Json]] = {
- val nodeIdStr = entityId.value.toString
- client.query(
- """FOR p IN patches
- | FILTER @nodeId IN (IS_ARRAY(p.data.entityIds) ? p.data.entityIds : [])
- | OR LENGTH(
- | FOR op IN (IS_ARRAY(p.data.ops) ? p.data.ops : [])
- | FILTER op.id == @nodeId OR op.entityId == @nodeId
- | RETURN 1
- | ) > 0
- | SORT p.rev ASC
- | RETURN p""".stripMargin,
- Map(
- "nodeId" -> nodeIdStr.asInstanceOf[AnyRef]
- )
- )
- }
-
- override def getPatchesBySource(sourceUri: String, extractor: String): IO[Vector[Json]] =
- client.query(
- """FOR p IN patches
- | FILTER p.data.source.uri == @uri AND p.data.source.extractor == @extractor
- | SORT p.rev DESC
- | RETURN p""".stripMargin,
- Map(
- "uri" -> sourceUri.asInstanceOf[AnyRef],
- "extractor" -> extractor.asInstanceOf[AnyRef]
- )
- ).map(_.toVector)
-
- override def getChangedEntities(fromRev: Rev, toRev: Rev): IO[Vector[(GraphNode, Option[GraphNode])]] = {
- val aql = """
- FOR n IN nodes
- FILTER n.created_rev > @fromRev AND n.created_rev <= @toRev
- COLLECT logicalId = n.logical_id
- LET atFrom = FIRST(
- FOR x IN nodes
- FILTER x.logical_id == logicalId
- AND x.created_rev <= @fromRev
- AND (x.deleted_rev == null OR @fromRev < x.deleted_rev)
- SORT x.created_rev DESC
- LIMIT 1
- RETURN x
- )
- LET atTo = FIRST(
- FOR x IN nodes
- FILTER x.logical_id == logicalId
- AND x.created_rev <= @toRev
- AND (x.deleted_rev == null OR @toRev < x.deleted_rev)
- SORT x.created_rev DESC
- LIMIT 1
- RETURN x
- )
- FILTER atTo != null
- RETURN { atFrom: atFrom, atTo: atTo }
- """
- client.query(aql, Map(
- "fromRev" -> Long.box(fromRev.value).asInstanceOf[AnyRef],
- "toRev" -> Long.box(toRev.value).asInstanceOf[AnyRef]
- )).map { results =>
- results.flatMap { json =>
- val c = json.hcursor
- for {
- atTo <- c.downField("atTo").focus.flatMap(parseNode)
- } yield {
- val atFrom = c.downField("atFrom").focus.flatMap(parseNode)
- (atTo, atFrom)
- }
- }.toVector
- }
- }
-
- override def getDiffSummary(fromRev: Rev, toRev: Rev): IO[Map[String, Int]] = {
- val aql = """
- FOR n IN nodes
- FILTER n.created_rev > @fromRev AND n.created_rev <= @toRev
- COLLECT logicalId = n.logical_id
- LET atFrom = FIRST(
- FOR x IN nodes
- FILTER x.logical_id == logicalId
- AND x.created_rev <= @fromRev
- AND (x.deleted_rev == null OR @fromRev < x.deleted_rev)
- RETURN x
- )
- LET atTo = FIRST(
- FOR x IN nodes
- FILTER x.logical_id == logicalId
- AND x.created_rev <= @toRev
- AND (x.deleted_rev == null OR @toRev < x.deleted_rev)
- RETURN x
- )
- LET changeType = (atFrom == null ? "added" : (atTo == null ? "removed" : "modified"))
- COLLECT type = changeType WITH COUNT INTO cnt
- RETURN { type: type, cnt: cnt }
- """
- client.query(aql, Map(
- "fromRev" -> Long.box(fromRev.value).asInstanceOf[AnyRef],
- "toRev" -> Long.box(toRev.value).asInstanceOf[AnyRef]
- )).map { results =>
- results.flatMap { json =>
- for {
- t <- json.hcursor.downField("type").as[String].toOption
- c <- json.hcursor.downField("cnt").as[Int].toOption
- } yield t -> c
- }.toMap
- }
- }
-
- override def resolvePrefix(prefix: String): IO[Vector[NodeId]] =
- client.query(
- """FOR n IN nodes
- | FILTER n.deleted_rev == null
- | AND STARTS_WITH(n.logical_id, @prefix)
- | COLLECT lid = n.logical_id
- | LIMIT 10
- | RETURN lid""".stripMargin,
- Map("prefix" -> prefix.asInstanceOf[AnyRef])
- ).map(_.flatMap(_.asString).flatMap(s =>
- scala.util.Try(java.util.UUID.fromString(s)).toOption.map(NodeId(_))
- ).toVector)
-
- override def getSourceHashes(sourceUris: Seq[String]): IO[Map[String, String]] = {
- if (sourceUris.isEmpty) IO.pure(Map.empty)
- else {
- val uriList = new java.util.ArrayList[String](sourceUris.size)
- sourceUris.foreach(uriList.add)
- client.query(
- """FOR p IN patches
- | FILTER p.data.source.uri IN @uris
- | SORT p.rev DESC
- | COLLECT uri = p.data.source.uri INTO groups
- | LET latest = FIRST(groups)
- | RETURN { uri: uri, hash: latest.p.data.source.sourceHash }""".stripMargin,
- Map("uris" -> uriList.asInstanceOf[AnyRef])
- ).map { results =>
- results.flatMap { json =>
- for {
- uri <- json.hcursor.get[String]("uri").toOption
- hash <- json.hcursor.get[String]("hash").toOption
- } yield uri -> hash
- }.toMap
- }
- }
- }
-
- override def hasIngestBaseline(): IO[Boolean] =
- client.query(
- """FOR p IN patches
- | FILTER p.data.source.sourceHash != null
- | LIMIT 1
- | RETURN 1""".stripMargin,
- Map.empty
- ).map(_.nonEmpty)
-
- override def expandByName(
- name: String,
- direction: Direction,
- predicates: Option[Set[String]] = None,
- kinds: Option[Set[NodeKind]] = None
- ): IO[ExpandResult] = {
- val kindFilter = kinds.filter(_.nonEmpty) match {
- case Some(_) => " AND n.kind IN @kinds"
- case None => ""
- }
- val predicateFilter = predicates.filter(_.nonEmpty) match {
- case Some(_) => " AND e.predicate IN @predicates"
- case None => ""
- }
- val directionFilter = direction match {
- case Direction.Out => "FILTER e.src IN target_ids"
- case Direction.In => "FILTER e.dst IN target_ids"
- case Direction.Both => "FILTER e.src IN target_ids OR e.dst IN target_ids"
- }
-
- // Phase 1: fetch edges only (no correlated node subquery per row)
- val edgeAql =
- s"""LET target_ids = (
- | FOR n IN nodes
- | FILTER n.name == @name AND n.deleted_rev == null
- | $kindFilter
- | RETURN DISTINCT n.logical_id
- |)
- |FOR e IN edges
- | FILTER e.deleted_rev == null
- | $directionFilter
- | $predicateFilter
- | RETURN MERGE(e, { _target_ids: target_ids })""".stripMargin
-
- val binds = scala.collection.mutable.Map[String, AnyRef](
- "name" -> name.asInstanceOf[AnyRef]
- )
- kinds.filter(_.nonEmpty).foreach { ks =>
- val kindList = new java.util.ArrayList[String]()
- ks.foreach(k => kindList.add(nodeKindToString(k)))
- binds += ("kinds" -> kindList.asInstanceOf[AnyRef])
- }
- predicates.filter(_.nonEmpty).foreach { ps =>
- val predList = new java.util.ArrayList[String]()
- ps.foreach(predList.add)
- binds += ("predicates" -> predList.asInstanceOf[AnyRef])
- }
-
- for {
- edgeRows <- client.query(edgeAql, binds.toMap)
- allEdges = edgeRows.flatMap(parseEdge).toVector
-
- // Determine which side is the "other" node by checking if src was a target
- targetIdSet: Set[String] = edgeRows.headOption
- .flatMap(_.hcursor.downField("_target_ids").as[List[String]].toOption)
- .getOrElse(Nil).toSet
-
- neighborIds = allEdges.flatMap { e =>
- if (targetIdSet.contains(e.src.value.toString)) Vector(e.dst)
- else if (targetIdSet.contains(e.dst.value.toString)) Vector(e.src)
- else Vector(e.src, e.dst)
- }.distinct
-
- // Phase 2: batch-fetch all neighbor nodes in one query
- nodes <- if (neighborIds.isEmpty) IO.pure(Vector.empty[GraphNode])
- else {
- val idList = new java.util.ArrayList[String](neighborIds.size)
- neighborIds.foreach(nid => idList.add(nid.value.toString))
- client.query(
- """FOR n IN nodes
- | FILTER n.logical_id IN @ids AND n.deleted_rev == null
- | SORT n.logical_id, n.created_rev DESC
- | COLLECT lid = n.logical_id INTO grp
- | RETURN FIRST(grp).n""".stripMargin,
- Map("ids" -> idList.asInstanceOf[AnyRef])
- ).map(_.flatMap(parseNode).toVector)
- }
- } yield ExpandResult(nodes, allEdges)
- }
-
- private def buildFileSymbolProjection(
- fileNode: GraphNode,
- rev: Rev,
- predicates: Option[Set[String]]
- ): IO[Option[ExpandProjection]] = {
- val activePredicates = predicates
- .map(_.intersect(SymbolProjectionPredicates))
- .getOrElse(SymbolProjectionPredicates)
-
- val kindList = new java.util.ArrayList[String](SymbolProjectionKinds.size)
- SymbolProjectionKinds.foreach(kind => kindList.add(nodeKindToString(kind)))
-
- val symbolNodeQuery =
- """FOR n IN nodes
- | FILTER n.provenance.source_uri == @sourceUri
- | AND n.kind IN @symbolKinds
- | AND n.created_rev <= @rev
- | AND (n.deleted_rev == null OR @rev < n.deleted_rev)
- | SORT TO_NUMBER(n.attrs.line_start), TO_NUMBER(n.attrs.line_end), n.name ASC
- | RETURN n""".stripMargin
-
- for {
- symbolRows <- client.query(
- symbolNodeQuery,
- Map(
- "sourceUri" -> fileNode.provenance.sourceUri.asInstanceOf[AnyRef],
- "symbolKinds" -> kindList.asInstanceOf[AnyRef],
- "rev" -> Long.box(rev.value).asInstanceOf[AnyRef],
- ),
- )
- symbolNodes = symbolRows.flatMap(parseNode).toVector
- edgeRows <- if (symbolNodes.isEmpty || activePredicates.isEmpty) IO.pure(Vector.empty[Json])
- else {
- val idList = new java.util.ArrayList[String](symbolNodes.size)
- symbolNodes.foreach(node => idList.add(node.id.value.toString))
- val predicateList = new java.util.ArrayList[String](activePredicates.size)
- activePredicates.toVector.sorted.foreach(predicateList.add)
- client.query(
- """FOR e IN edges
- | FILTER e.src IN @ids
- | AND e.dst IN @ids
- | AND e.predicate IN @predicates
- | AND e.created_rev <= @rev
- | AND (e.deleted_rev == null OR @rev < e.deleted_rev)
- | RETURN e""".stripMargin,
- Map(
- "ids" -> idList.asInstanceOf[AnyRef],
- "predicates" -> predicateList.asInstanceOf[AnyRef],
- "rev" -> Long.box(rev.value).asInstanceOf[AnyRef],
- ),
- )
- }
- symbolEdges = edgeRows.flatMap(parseEdge).toVector
- } yield Some(ExpandProjection(
- kind = "symbol_graph",
- nodes = symbolNodes,
- edges = symbolEdges,
- ))
- }
-
- // ── JSON Parsers (snake_case → camelCase) ───────────────────────────
-
- private def parseNode(json: Json): Option[GraphNode] = {
- val c = json.hcursor
- val result = for {
- id <- c.get[String]("id").toOption
- .orElse(c.get[String]("logical_id").toOption)
- .flatMap(s => scala.util.Try(UUID.fromString(s)).toOption)
- .map(NodeId(_))
- kindStr <- c.get[String]("kind").toOption
- kind <- NodeKind.decoder.decodeJson(Json.fromString(kindStr)).toOption
- name = c.get[String]("name").getOrElse("")
- attrs <- c.get[Json]("attrs").toOption
- provenance <- parseProvenance(c.downField("provenance").focus)
- createdRev <- c.get[Long]("created_rev").toOption.map(Rev(_))
- deletedRev = c.get[Long]("deleted_rev").toOption.map(Rev(_))
- createdAt <- c.get[String]("created_at").toOption
- .flatMap(s => scala.util.Try(Instant.parse(s)).toOption)
- updatedAt <- c.get[String]("updated_at").toOption
- .flatMap(s => scala.util.Try(Instant.parse(s)).toOption)
- } yield GraphNode(id, kind, name, attrs, provenance, createdRev, deletedRev, createdAt, updatedAt)
- if (result.isEmpty) log.warn("Failed to parse node from JSON: {}", json.noSpaces.take(200))
- result
- }
-
- private def parseEdge(json: Json): Option[GraphEdge] = {
- val c = json.hcursor
- val result = for {
- id <- c.get[String]("id").toOption
- .flatMap(s => scala.util.Try(UUID.fromString(s)).toOption)
- .map(EdgeId(_))
- src <- c.get[String]("src").toOption
- .flatMap(s => scala.util.Try(UUID.fromString(s)).toOption)
- .map(NodeId(_))
- dst <- c.get[String]("dst").toOption
- .flatMap(s => scala.util.Try(UUID.fromString(s)).toOption)
- .map(NodeId(_))
- predicate <- c.get[String]("predicate").toOption.map(EdgePredicate(_))
- attrs <- c.get[Json]("attrs").toOption
- provenance <- parseProvenance(c.downField("provenance").focus)
- createdRev <- c.get[Long]("created_rev").toOption.map(Rev(_))
- deletedRev = c.get[Long]("deleted_rev").toOption.map(Rev(_))
- } yield GraphEdge(id, src, dst, predicate, attrs, provenance, createdRev, deletedRev)
- if (result.isEmpty) log.warn("Failed to parse edge from JSON: {}", json.noSpaces.take(200))
- result
- }
-
- private def parseClaim(json: Json): Option[Claim] = {
- val c = json.hcursor
- val result = for {
- key <- c.get[String]("_key").toOption
- .flatMap(s => scala.util.Try(UUID.fromString(s)).toOption)
- .map(ClaimId(_))
- entityId <- c.get[String]("entity_id").toOption
- .flatMap(s => scala.util.Try(UUID.fromString(s)).toOption)
- .map(NodeId(_))
- field <- c.get[String]("field").toOption
- value = c.downField("value").focus.getOrElse(Json.Null)
- confidence = c.get[Double]("confidence").toOption
- statusStr <- c.get[String]("status").toOption
- status <- statusStr.toLowerCase match {
- case "active" => Some(ClaimStatus.Active)
- case "stale" => Some(ClaimStatus.Stale)
- case "retracted" => Some(ClaimStatus.Retracted)
- case _ => None
- }
- provenance <- parseProvenance(c.downField("provenance").focus)
- createdRev <- c.get[Long]("created_rev").toOption.map(Rev(_))
- deletedRev = c.get[Long]("deleted_rev").toOption.map(Rev(_))
- } yield Claim(key, entityId, field, value, confidence, status, provenance, createdRev, deletedRev)
- if (result.isEmpty) log.warn("Failed to parse claim from JSON: {}", json.noSpaces.take(200))
- result
- }
-
- private def parseProvenance(jsonOpt: Option[Json]): Option[Provenance] = {
- jsonOpt.flatMap { json =>
- val c = json.hcursor
- for {
- sourceUri <- c.get[String]("source_uri").toOption
- sourceHash = c.get[String]("source_hash").toOption
- extractor <- c.get[String]("extractor").toOption
- stStr <- c.get[String]("source_type").toOption
- sourceType <- SourceType.decoder.decodeJson(Json.fromString(stStr)).toOption
- observedAt <- c.get[String]("observed_at").toOption
- .flatMap(s => scala.util.Try(Instant.parse(s)).toOption)
- } yield Provenance(sourceUri, sourceHash, extractor, sourceType, observedAt)
- }
- }
-
- private def nodeKindToString(nk: NodeKind): String =
- NodeKind.encoder.apply(nk).asString.getOrElse(nk.toString)
-}
diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphWriteApi.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoGraphWriteApi.scala
deleted file mode 100644
index 715e214c..00000000
--- a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphWriteApi.scala
+++ /dev/null
@@ -1,541 +0,0 @@
-package ix.memory.db
-
-import java.time.Instant
-import java.util.UUID
-
-import scala.collection.mutable
-
-import cats.effect.IO
-import cats.syntax.traverse._
-import cats.syntax.foldable._
-import cats.instances.vector._
-import io.circe.syntax._
-import io.circe.Json
-
-import ix.memory.model._
-
-class ArangoGraphWriteApi(client: ArangoClient) extends GraphWriteApi {
-
- // I1 fix: single ObjectMapper instance shared across all calls
- private val mapper = new com.fasterxml.jackson.databind.ObjectMapper()
- private val CurrentRevisionKey = "current"
- private val SourceGraphRevisionKey = "source-graph"
- private val SourceGraphIgnoredKinds: Set[NodeKind] = Set(
- NodeKind.Module,
- NodeKind.Config,
- NodeKind.ConfigEntry,
- NodeKind.Doc,
- NodeKind.Decision,
- NodeKind.Intent,
- NodeKind.Bug,
- NodeKind.Plan,
- NodeKind.Task,
- NodeKind.Goal,
- NodeKind.Region
- )
- private val SourceGraphPredicates = Set("CALLS", "IMPORTS", "EXTENDS", "IMPLEMENTS")
-
- // ── Collections involved in a commit transaction ──────────────────
-
- private val ReadCollections = Seq("revisions", "idempotency_keys")
- private val WriteCollections = Seq("nodes", "edges", "claims", "patches", "revisions", "idempotency_keys")
-
- // ── Public entry point ────────────────────────────────────────────
-
- override def commitPatch(patch: GraphPatch): IO[CommitResult] =
- client.beginTransaction(ReadCollections, WriteCollections).flatMap { txId =>
- val commit = for {
- // Step 1: Check idempotency inside the transaction
- idempotent <- checkIdempotency(patch.patchId, txId)
- result <- idempotent match {
- case Some(storedRev) =>
- // Already applied — commit the (read-only) transaction and return
- client.commitTransaction(txId) *>
- IO.pure(CommitResult(storedRev, CommitStatus.Idempotent))
- case None =>
- doCommit(patch, txId)
- }
- } yield result
-
- commit.handleErrorWith { err =>
- client.abortTransaction(txId) *> IO.raiseError(err)
- }
- }
-
- // ── Private helpers ───────────────────────────────────────────────
-
- private def checkIdempotency(patchId: PatchId, txId: String): IO[Option[Rev]] =
- client.queryOne(
- """FOR ik IN idempotency_keys
- | FILTER ik.key == @key
- | RETURN ik.rev""".stripMargin,
- Map("key" -> idempotencyKey(patchId).asInstanceOf[AnyRef]),
- txId = Some(txId)
- ).map(_.flatMap(_.as[Long].toOption).map(Rev(_)))
-
- private def idempotencyKey(patchId: PatchId): String =
- patchId.value.toString
-
- private def doCommit(patch: GraphPatch, txId: String): IO[CommitResult] = {
- val entityIdsForClaims = patch.ops.collect {
- case PatchOp.AssertClaim(eid, _, _, _, _) => eid.value.toString
- }.toSet
-
- for {
- // Step 2: Load latest_rev (inside transaction)
- latestRev <- loadLatestRev(txId)
-
- // Step 3: Check baseRev (if > 0, must match latest)
- result <- {
- if (patch.baseRev.value > 0L && patch.baseRev.value != latestRev) {
- // Mismatch — commit the read-only transaction cleanly, then return
- client.commitTransaction(txId) *>
- IO.pure(CommitResult(Rev(latestRev), CommitStatus.BaseRevMismatch))
- } else {
- val newRev = latestRev + 1L
- val advanceSourceGraphRev = shouldAdvanceSourceGraphRev(patch)
- for {
- // Step 4b: Pre-load active claims for all entities in this patch (one query)
- // Eliminates per-claim hasIdenticalActiveClaim DB round-trips
- claimCache <- loadActiveClaimsCache(entityIdsForClaims, txId)
- // Step 5: Execute each op
- _ <- patch.ops.traverse_(op => executeOp(op, patch, newRev, txId, claimCache))
- // Step 5b: Retire absent claims — single batch query instead of one per entity
- _ <- retireAbsentClaimsBatch(patch, newRev, txId)
- // Step 6: Store patch
- _ <- storePatch(patch, newRev, txId)
- // Step 7: Store idempotency key
- _ <- storeIdempotencyKey(patch.patchId, newRev, txId)
- // Step 8: Update revision
- _ <- updateRevision(newRev, txId, advanceSourceGraphRev)
- // Step 9: Commit the transaction
- _ <- client.commitTransaction(txId)
- } yield CommitResult(Rev(newRev), CommitStatus.Ok)
- }
- }
- } yield result
- }
-
- private def loadLatestRev(txId: String): IO[Long] =
- client.queryOne(
- """FOR r IN revisions
- | FILTER r._key == @key
- | RETURN r.rev""".stripMargin,
- Map("key" -> "current".asInstanceOf[AnyRef]),
- txId = Some(txId)
- ).map(_.flatMap(_.as[Long].toOption).getOrElse(0L))
-
- private def executeOp(op: PatchOp, patch: GraphPatch, newRev: Long, txId: String, claimCache: mutable.Set[(String, String, String)]): IO[Unit] = op match {
- case PatchOp.UpsertNode(id, kind, name, attrs) =>
- val logicalId = id.value.toString
- val now = Instant.now().toString
- val provenanceJson = buildProvenance(patch)
- val attrsJson = Json.obj(attrs.toSeq.map { case (k, v) => k -> v }: _*).noSpaces
-
- // Check if a live (non-tombstoned) row exists for this logical_id
- val checkAql = """
- FOR n IN nodes
- FILTER n.logical_id == @logicalId
- AND n.deleted_rev == null
- RETURN n
- """
- for {
- existing <- client.queryOne(checkAql,
- Map("logicalId" -> logicalId.asInstanceOf[AnyRef]),
- txId = Some(txId))
- _ <- existing match {
- case Some(existingJson) =>
- // Node exists — check if attrs actually changed
- val existingAttrsStr = existingJson.hcursor.downField("attrs").focus
- .map(_.noSpaces).getOrElse("{}")
- if (existingAttrsStr == attrsJson) {
- // Same attrs — idempotent no-op
- IO.unit
- } else {
- // Different attrs — tombstone old row + create new versioned row
- val existingKey = existingJson.hcursor.get[String]("_key")
- .getOrElse(logicalId)
- val tombstoneAql = """
- UPDATE { _key: @key } WITH { deleted_rev: @rev, updated_at: @now } IN nodes
- OPTIONS { waitForSync: false }
- """
- val newKey = s"${logicalId}_${newRev}"
- val upsertAql = """
- UPSERT { _key: @key }
- INSERT {
- _key: @key, logical_id: @logicalId, id: @id, kind: @kind, name: @name,
- attrs: @attrs, provenance: @provenance,
- created_rev: @rev, deleted_rev: null,
- created_at: @now, updated_at: @now
- }
- UPDATE {
- logical_id: @logicalId, id: @id, kind: @kind, name: @name,
- attrs: @attrs, provenance: @provenance,
- deleted_rev: null, updated_at: @now
- }
- IN nodes OPTIONS { waitForSync: false }
- """
- for {
- _ <- client.execute(tombstoneAql, Map(
- "key" -> existingKey.asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "now" -> now.asInstanceOf[AnyRef]
- ), txId = Some(txId))
- _ <- client.execute(upsertAql, Map(
- "key" -> newKey.asInstanceOf[AnyRef],
- "logicalId" -> logicalId.asInstanceOf[AnyRef],
- "id" -> logicalId.asInstanceOf[AnyRef],
- "kind" -> nodeKindToString(kind).asInstanceOf[AnyRef],
- "name" -> name.asInstanceOf[AnyRef],
- "attrs" -> parseToJavaMap(attrsJson).asInstanceOf[AnyRef],
- "provenance" -> parseToJavaMap(provenanceJson.noSpaces).asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "now" -> now.asInstanceOf[AnyRef]
- ), txId = Some(txId))
- } yield ()
- }
-
- case None =>
- // New node — create first row with versioned key
- val newKey = s"${logicalId}_${newRev}"
- val upsertAql = """
- UPSERT { _key: @key }
- INSERT {
- _key: @key, logical_id: @logicalId, id: @id, kind: @kind, name: @name,
- attrs: @attrs, provenance: @provenance,
- created_rev: @rev, deleted_rev: null,
- created_at: @now, updated_at: @now
- }
- UPDATE {
- logical_id: @logicalId, id: @id, kind: @kind, name: @name,
- attrs: @attrs, provenance: @provenance,
- deleted_rev: null, updated_at: @now
- }
- IN nodes OPTIONS { waitForSync: false }
- """
- client.execute(upsertAql, Map(
- "key" -> newKey.asInstanceOf[AnyRef],
- "logicalId" -> logicalId.asInstanceOf[AnyRef],
- "id" -> logicalId.asInstanceOf[AnyRef],
- "kind" -> nodeKindToString(kind).asInstanceOf[AnyRef],
- "name" -> name.asInstanceOf[AnyRef],
- "attrs" -> parseToJavaMap(attrsJson).asInstanceOf[AnyRef],
- "provenance" -> parseToJavaMap(provenanceJson.noSpaces).asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "now" -> now.asInstanceOf[AnyRef]
- ), txId = Some(txId))
- }
- } yield ()
-
- case PatchOp.UpsertEdge(id, src, dst, predicate, attrs) =>
- val now = Instant.now().toString
- val provenanceJson = buildProvenance(patch)
- val attrsJson = Json.obj(attrs.toSeq.map { case (k, v) => k -> v }: _*).noSpaces
- client.execute(
- """UPSERT { _key: @key }
- | INSERT {
- | _key: @key,
- | _from: @from,
- | _to: @to,
- | id: @id,
- | src: @src,
- | dst: @dst,
- | predicate: @predicate,
- | attrs: @attrs,
- | created_rev: @created_rev,
- | deleted_rev: null,
- | provenance: @provenance,
- | created_at: @now,
- | updated_at: @now
- | }
- | UPDATE {
- | _from: @from,
- | _to: @to,
- | src: @src,
- | dst: @dst,
- | predicate: @predicate,
- | attrs: @attrs,
- | deleted_rev: null,
- | provenance: @provenance,
- | updated_at: @now
- | }
- | IN edges""".stripMargin,
- Map(
- "key" -> id.value.toString.asInstanceOf[AnyRef],
- "id" -> id.value.toString.asInstanceOf[AnyRef],
- "from" -> s"nodes/${src.value}".asInstanceOf[AnyRef],
- "to" -> s"nodes/${dst.value}".asInstanceOf[AnyRef],
- "src" -> src.value.toString.asInstanceOf[AnyRef],
- "dst" -> dst.value.toString.asInstanceOf[AnyRef],
- "predicate" -> predicate.value.asInstanceOf[AnyRef],
- "attrs" -> parseToJavaMap(attrsJson).asInstanceOf[AnyRef],
- "created_rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "provenance" -> parseToJavaMap(provenanceJson.noSpaces).asInstanceOf[AnyRef],
- "now" -> now.asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
-
- case PatchOp.DeleteNode(id) =>
- client.execute(
- """FOR n IN nodes
- | FILTER n.logical_id == @logicalId AND n.deleted_rev == null
- | UPDATE n WITH { deleted_rev: @deleted_rev } IN nodes""".stripMargin,
- Map(
- "logicalId" -> id.value.toString.asInstanceOf[AnyRef],
- "deleted_rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
-
- case PatchOp.DeleteEdge(id) =>
- client.execute(
- """FOR e IN edges
- | FILTER e._key == @key
- | UPDATE e WITH { deleted_rev: @deleted_rev } IN edges""".stripMargin,
- Map(
- "key" -> id.value.toString.asInstanceOf[AnyRef],
- "deleted_rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
-
- case PatchOp.AssertClaim(entityId, field, value, confidence, inferenceVersion) =>
- val provenanceJson = buildProvenance(patch)
- val eid = entityId.value.toString
- val valueStr = value.noSpaces
- for {
- // Step 1: Retire active claims for same (entity_id, field) with different value
- _ <- retireClaims(eid, field, value, newRev, txId)
- // Mirror retirement in the cache: remove entries for (eid, field) with different value
- _ = claimCache.filterInPlace { case (e, f, v) => !(e == eid && f == field && v != valueStr) }
- // Step 2: Check in-memory cache — avoids a DB round-trip per claim
- _ <- if (claimCache.contains((eid, field, valueStr))) IO.unit
- else {
- val claimKey = UUID.randomUUID().toString
- for {
- _ <- client.execute(
- """INSERT {
- | _key: @key,
- | entity_id: @entity_id,
- | field: @field,
- | value: @value,
- | confidence: @confidence,
- | status: @status,
- | created_rev: @created_rev,
- | deleted_rev: null,
- | provenance: @provenance,
- | inference_version: @inference_version
- |} INTO claims""".stripMargin,
- Map(
- "key" -> claimKey.asInstanceOf[AnyRef],
- "entity_id" -> eid.asInstanceOf[AnyRef],
- "field" -> field.asInstanceOf[AnyRef],
- "value" -> jsonToJava(value).asInstanceOf[AnyRef],
- "confidence" -> confidence.map(Double.box).orNull.asInstanceOf[AnyRef],
- "status" -> "active".asInstanceOf[AnyRef],
- "created_rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "provenance" -> parseToJavaMap(provenanceJson.noSpaces).asInstanceOf[AnyRef],
- "inference_version" -> inferenceVersion.orNull.asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
- _ = claimCache.add((eid, field, valueStr))
- } yield ()
- }
- } yield ()
-
- case PatchOp.RetractClaim(claimId) =>
- client.execute(
- """FOR c IN claims
- | FILTER c._key == @key
- | UPDATE c WITH { status: "retracted", deleted_rev: @deleted_rev } IN claims""".stripMargin,
- Map(
- "key" -> claimId.value.toString.asInstanceOf[AnyRef],
- "deleted_rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
- }
-
- private def storePatch(patch: GraphPatch, newRev: Long, txId: String): IO[Unit] = {
- val patchJson = patch.asJson.noSpaces
- client.execute(
- """UPSERT { _key: @key }
- | INSERT { _key: @key, patch_id: @patch_id, rev: @rev, data: @data }
- | UPDATE {}
- | IN patches""".stripMargin,
- Map(
- "key" -> patch.patchId.value.toString.asInstanceOf[AnyRef],
- "patch_id" -> patch.patchId.value.toString.asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "data" -> parseToJavaMap(patchJson).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
- }
-
- private def storeIdempotencyKey(patchId: PatchId, newRev: Long, txId: String): IO[Unit] =
- client.execute(
- """INSERT {
- | key: @key,
- | rev: @rev,
- | created_at: DATE_NOW() / 1000
- |} INTO idempotency_keys""".stripMargin,
- Map(
- "key" -> idempotencyKey(patchId).asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
-
- private def shouldAdvanceSourceGraphRev(patch: GraphPatch): Boolean =
- patch.ops.exists {
- case PatchOp.UpsertNode(_, kind, _, _) =>
- !SourceGraphIgnoredKinds.contains(kind)
- case PatchOp.UpsertEdge(_, _, _, predicate, _) =>
- SourceGraphPredicates.contains(predicate.value)
- case PatchOp.DeleteNode(_) | PatchOp.DeleteEdge(_) =>
- patch.source.sourceType == SourceType.Code || patch.source.sourceType == SourceType.Test
- case _ =>
- false
- }
-
- private def updateRevision(newRev: Long, txId: String, advanceSourceGraphRev: Boolean): IO[Unit] =
- client.execute(
- """FOR key IN @keys
- | UPSERT { _key: key }
- | INSERT { _key: key, rev: @rev }
- | UPDATE { rev: @rev }
- | IN revisions""".stripMargin,
- Map(
- "keys" -> {
- val keys =
- if (advanceSourceGraphRev) Array(CurrentRevisionKey, SourceGraphRevisionKey)
- else Array(CurrentRevisionKey)
- keys.asInstanceOf[AnyRef]
- },
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
-
- /** Retire active claims for the same (entity_id, field) that have a different value. */
- private def retireClaims(entityId: String, field: String, value: Json, newRev: Long, txId: String): IO[Unit] =
- client.execute(
- """FOR c IN claims
- | FILTER c.entity_id == @entity_id
- | AND c.field == @field
- | AND c.deleted_rev == null
- | AND c.value != @value
- | UPDATE c WITH { status: "retracted", deleted_rev: @deleted_rev } IN claims""".stripMargin,
- Map(
- "entity_id" -> entityId.asInstanceOf[AnyRef],
- "field" -> field.asInstanceOf[AnyRef],
- "value" -> jsonToJava(value).asInstanceOf[AnyRef],
- "deleted_rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
-
- /**
- * Pre-load all active claims for the given entity IDs in one DB query.
- * Returns a mutable set of (entityId, field, valueNoSpaces) triples used
- * as an in-memory duplicate-check cache throughout a single patch commit.
- */
- private def loadActiveClaimsCache(entityIds: Set[String], txId: String): IO[mutable.Set[(String, String, String)]] =
- if (entityIds.isEmpty) IO.pure(mutable.Set.empty)
- else {
- val eids = new java.util.ArrayList[String]()
- entityIds.foreach(eids.add)
- client.query(
- """FOR c IN claims
- | FILTER c.entity_id IN @entityIds AND c.deleted_rev == null
- | RETURN { entity_id: c.entity_id, field: c.field, value: c.value }""".stripMargin,
- Map("entityIds" -> eids.asInstanceOf[AnyRef]),
- txId = Some(txId)
- ).map { results =>
- val cache = mutable.Set.empty[(String, String, String)]
- results.foreach { json =>
- val parsed = for {
- eid <- json.hcursor.get[String]("entity_id").toOption
- field <- json.hcursor.get[String]("field").toOption
- value <- json.hcursor.downField("value").focus
- } yield (eid, field, value.noSpaces)
- parsed.foreach(cache.add)
- }
- cache
- }
- }
-
- /**
- * After all ops execute, retire claims from the same extractor/entity that are
- * no longer emitted (the "absent claims" policy). Uses a single batch AQL query
- * across all entities instead of one query per entity.
- */
- private def retireAbsentClaimsBatch(patch: GraphPatch, newRev: Long, txId: String): IO[Unit] = {
- val assertedClaims = patch.ops.collect {
- case PatchOp.AssertClaim(entityId, field, _, _, _) => (entityId.value.toString, field)
- }
-
- if (assertedClaims.isEmpty) IO.unit
- else {
- val fieldsByEntity: Map[String, Set[String]] = assertedClaims
- .groupBy(_._1)
- .map { case (eid, pairs) => eid -> pairs.map(_._2).toSet }
-
- // Build Java list of {entity_id, fields} for AQL — one query instead of N
- val entityFieldMap = new java.util.ArrayList[java.util.Map[String, AnyRef]]()
- fieldsByEntity.foreach { case (eid, fields) =>
- val m = new java.util.HashMap[String, AnyRef]()
- val fl = new java.util.ArrayList[String]()
- fields.foreach(fl.add)
- m.put("entity_id", eid)
- m.put("fields", fl)
- entityFieldMap.add(m)
- }
-
- client.execute(
- """LET fieldMap = MERGE(
- | FOR ef IN @entityFieldMap RETURN { [ef.entity_id]: ef.fields }
- |)
- |FOR c IN claims
- | FILTER c.entity_id IN ATTRIBUTES(fieldMap)
- | AND c.field NOT IN fieldMap[c.entity_id]
- | AND c.deleted_rev == null
- | AND c.provenance.extractor == @extractor
- | UPDATE c WITH { status: "retracted", deleted_rev: @deleted_rev } IN claims""".stripMargin,
- Map(
- "entityFieldMap" -> entityFieldMap.asInstanceOf[AnyRef],
- "extractor" -> patch.source.extractor.asInstanceOf[AnyRef],
- "deleted_rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- ),
- txId = Some(txId)
- )
- }
- }
-
- private def buildProvenance(patch: GraphPatch): Json =
- Json.obj(
- "source_uri" -> Json.fromString(patch.source.uri),
- "source_hash" -> patch.source.sourceHash.fold(Json.Null)(Json.fromString),
- "extractor" -> Json.fromString(patch.source.extractor),
- "source_type" -> Json.fromString(sourceTypeToString(patch.source.sourceType)),
- "observed_at" -> Json.fromString(patch.timestamp.toString)
- )
-
- // I3 fix: reuse Circe encoders instead of duplicating string conversion logic
- private def sourceTypeToString(st: SourceType): String =
- st.asJson.asString.getOrElse(st.toString)
-
- private def nodeKindToString(nk: NodeKind): String =
- nk.asJson.asString.getOrElse(nk.toString)
-
- /** Convert a JSON string into a java.util.Map for use as an ArangoDB bind variable. */
- private def parseToJavaMap(jsonStr: String): java.util.Map[String, AnyRef] =
- mapper.readValue(jsonStr, classOf[java.util.Map[String, AnyRef]])
-
- /** Convert a circe Json value to a Java object suitable for ArangoDB bind variables. */
- private def jsonToJava(json: Json): AnyRef =
- mapper.readValue(json.noSpaces, classOf[AnyRef])
-}
diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoSchema.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoSchema.scala
deleted file mode 100644
index 066098f2..00000000
--- a/memory-layer/src/main/scala/ix/memory/db/ArangoSchema.scala
+++ /dev/null
@@ -1,125 +0,0 @@
-package ix.memory.db
-
-import cats.effect.IO
-import com.arangodb.ArangoDatabase
-import com.arangodb.entity.CollectionType
-import com.arangodb.model.{CollectionCreateOptions, PersistentIndexOptions, TtlIndexOptions}
-
-object ArangoSchema {
-
- def ensure(db: ArangoDatabase): IO[Unit] = IO.blocking {
- // Vertex collections
- ensureCollection(db, "nodes")
- ensureCollection(db, "claims")
- ensureCollection(db, "conflict_sets")
- ensureCollection(db, "patches")
- ensureCollection(db, "revisions")
- ensureCollection(db, "idempotency_keys")
- ensureCollection(db, "savings")
-
- // Edge collection
- ensureCollection(db, "edges", edge = true)
-
- // Indexes — nodes
- val nodes = db.collection("nodes")
- nodes.ensurePersistentIndex(
- java.util.Arrays.asList("kind"),
- new PersistentIndexOptions()
- )
- nodes.ensurePersistentIndex(
- java.util.Arrays.asList("name"),
- new PersistentIndexOptions().sparse(true)
- )
- nodes.ensurePersistentIndex(
- java.util.Arrays.asList("logical_id"),
- new PersistentIndexOptions()
- )
- // Compound index for tombstone query: FILTER logical_id IN @ids AND deleted_rev == null
- // Without this, the single-field logical_id index returns all historical versions of each
- // node and applies deleted_rev as a post-filter — gets slower as the collection grows.
- nodes.ensurePersistentIndex(
- java.util.Arrays.asList("logical_id", "deleted_rev"),
- new PersistentIndexOptions()
- )
-
- // Indexes — edges
- val edges = db.collection("edges")
- edges.ensurePersistentIndex(
- java.util.Arrays.asList("src"),
- new PersistentIndexOptions()
- )
- edges.ensurePersistentIndex(
- java.util.Arrays.asList("dst"),
- new PersistentIndexOptions()
- )
- edges.ensurePersistentIndex(
- java.util.Arrays.asList("predicate"),
- new PersistentIndexOptions()
- )
-
- // Indexes — nodes (provenance)
- nodes.ensurePersistentIndex(
- java.util.Arrays.asList("provenance.source_uri"),
- new PersistentIndexOptions().sparse(true)
- )
-
- // Indexes — claims
- val claims = db.collection("claims")
- claims.ensurePersistentIndex(
- java.util.Arrays.asList("entity_id"),
- new PersistentIndexOptions()
- )
- // Compound index for retire query: FILTER entity_id IN @ids AND deleted_rev == null
- // Mirrors the nodes compound index — avoids post-filtering all historical claim versions.
- claims.ensurePersistentIndex(
- java.util.Arrays.asList("entity_id", "deleted_rev"),
- new PersistentIndexOptions()
- )
- claims.ensurePersistentIndex(
- java.util.Arrays.asList("status"),
- new PersistentIndexOptions()
- )
- claims.ensurePersistentIndex(
- java.util.Arrays.asList("entity_id", "field", "deleted_rev"),
- new PersistentIndexOptions()
- )
- claims.ensurePersistentIndex(
- java.util.Arrays.asList("field"),
- new PersistentIndexOptions().sparse(true)
- )
-
- // Indexes — patches
- val patches = db.collection("patches")
- patches.ensurePersistentIndex(
- java.util.Arrays.asList("patch_id"),
- new PersistentIndexOptions().unique(true)
- )
- patches.ensurePersistentIndex(
- java.util.Arrays.asList("data.source.uri", "data.source.extractor"),
- new PersistentIndexOptions()
- )
-
- // Indexes — idempotency_keys
- val idem = db.collection("idempotency_keys")
- idem.ensurePersistentIndex(
- java.util.Arrays.asList("key"),
- new PersistentIndexOptions().unique(true)
- )
- idem.ensureTtlIndex(
- java.util.Arrays.asList("created_at"),
- new TtlIndexOptions().expireAfter(86400)
- )
-
- ()
- }
-
- private def ensureCollection(db: ArangoDatabase, name: String, edge: Boolean = false): Unit = {
- if (!db.collection(name).exists()) {
- if (edge) {
- db.createCollection(name, new CollectionCreateOptions().`type`(CollectionType.EDGES))
- } else {
- db.createCollection(name)
- }
- }
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/db/BulkWriteApi.scala b/memory-layer/src/main/scala/ix/memory/db/BulkWriteApi.scala
deleted file mode 100644
index 353c08df..00000000
--- a/memory-layer/src/main/scala/ix/memory/db/BulkWriteApi.scala
+++ /dev/null
@@ -1,496 +0,0 @@
-package ix.memory.db
-
-import java.nio.charset.StandardCharsets
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.syntax.parallel._
-import cats.syntax.traverse._
-import io.circe.Json
-import io.circe.syntax._
-import org.typelevel.log4cats.Logger
-import org.typelevel.log4cats.slf4j.Slf4jLogger
-
-import scala.concurrent.duration._
-
-import ix.memory.model._
-
-/**
- * High-performance batch writer that uses ArangoDB Document API
- * instead of individual AQL queries.
- */
-class BulkWriteApi(client: ArangoClient) {
- private type BulkDoc = java.util.Map[String, AnyRef]
- private val CurrentRevisionKey = "current"
- private val SourceGraphRevisionKey = "source-graph"
- private val DefaultChunkSize = 500
- private val DefaultChunkPayloadBytes = 16 * 1024 * 1024
- private val MaxNodeDocs = 10000
- private val MaxEdgeDocs = 50000
- private val MaxClaimDocs = 10000
- private val MaxPatchDocs = 500
- private val SlowChunkMs = 2000L
- private val debugBulkWrites = sys.env.get("IX_DEBUG").exists(v => v.nonEmpty && v != "0" && !v.equalsIgnoreCase("false"))
- private val logger: Logger[IO] = Slf4jLogger.getLoggerFromName[IO]("ix.bulk-write")
-
- private def withRetry[A](action: IO[A], maxAttempts: Int = 3, delay: FiniteDuration = 500.millis): IO[A] = {
- action.handleErrorWith { err =>
- if (maxAttempts <= 1) IO.raiseError(err)
- else IO.sleep(delay) *> withRetry(action, maxAttempts - 1, delay * 2)
- }
- }
-
- private def chunkDocs[T](docs: Seq[T], maxDocs: Int): Seq[Seq[T]] =
- docs.grouped(maxDocs).toVector
-
- private def insertCollectionChunked(
- collection: String,
- docs: Seq[BulkDoc],
- maxDocs: Int
- )(insert: Seq[BulkDoc] => IO[Int]): IO[Long] =
- if (docs.isEmpty) IO.pure(0L)
- else {
- val batches = chunkDocs(docs, maxDocs).toVector
- batches.parTraverse { batch =>
- for {
- ms <- timedUnit(withRetry(insert(batch)))
- _ <- if (debugBulkWrites)
- logger.info(s"[bulk-write] collection=$collection docs=${batch.size} insertMs=$ms")
- else IO.unit
- } yield ms
- }.map(_.sum)
- }
-
- /**
- * Build documents for a single chunk and insert them into ArangoDB.
- * Document building runs on the compute pool (IO.delay) in parallel
- * across FileBatches, then inserts run concurrently across collections.
- */
- def commitBatch(
- fileBatches: Vector[FileBatch],
- baseRev: Long,
- skipTombstone: Boolean = false
- ): IO[CommitResult] = {
- val newRev = baseRev + 1L
-
- for {
- // Build documents in parallel across FileBatches on the compute pool.
- // Each FileBatch produces its own (nodes, edges, claims, patch) tuple independently.
- docsAndBuildMs <- timed {
- fileBatches.parTraverse { fb =>
- IO.delay {
- (fb.nodeDocuments(newRev), fb.edgeDocuments(newRev), fb.claimDocuments(newRev), fb.patchDocument(newRev))
- }
- }.map { results =>
- val nodesBuf = Vector.newBuilder[java.util.Map[String, AnyRef]]
- val edgesBuf = Vector.newBuilder[java.util.Map[String, AnyRef]]
- val claimsBuf = Vector.newBuilder[java.util.Map[String, AnyRef]]
- val patchesBuf = Vector.newBuilder[java.util.Map[String, AnyRef]]
- results.foreach { case (nodes, edges, claims, patch) =>
- nodesBuf ++= nodes
- edgesBuf ++= edges
- claimsBuf ++= claims
- patchesBuf += patch
- }
- (nodesBuf.result(), edgesBuf.result(), claimsBuf.result(), patchesBuf.result())
- }
- }
- (docs, buildDocsMs) = docsAndBuildMs
- (allNodes, allEdges, allClaims, allPatches) = docs
- logicalIds = allNodes.flatMap(doc => Option(doc.get("logical_id")).map(_.toString))
- chunkSummary = s"files=${fileBatches.size} nodes=${allNodes.size} edges=${allEdges.size} claims=${allClaims.size} patches=${allPatches.size}"
- // Skip tombstone/retire on first ingest — DB is empty, no existing docs to update.
- tombstoneAndRetireMs <-
- if (skipTombstone || baseRev == 0) IO.pure((0L, 0L))
- else {
- val sourceUris = fileBatches.map(_.filePath)
- timedUnit(tombstoneExistingNodes(logicalIds, newRev))
- .both(timedUnit(tombstoneExistingEdges(sourceUris, newRev)))
- .both(timedUnit(retireOldClaims(fileBatches, newRev)))
- .map { case ((nodesMs, edgesMs), claimsMs) => (nodesMs + edgesMs, claimsMs) }
- }
- (tombstoneMs: Long, retireClaimsMs: Long) = tombstoneAndRetireMs
- // Run all four collection inserts concurrently — they write to independent collections
- // with no referential-integrity enforcement between them. Wall time drops from the
- // sum of all four to the max of any one (typically nodes or edges).
- allInsertMs <- insertCollectionChunked("nodes", allNodes, MaxNodeDocs)(docs => client.bulkInsert("nodes", docs))
- .both(insertCollectionChunked("edges", allEdges, MaxEdgeDocs)(docs => client.bulkInsertEdges("edges", docs)))
- .both(insertCollectionChunked("claims", allClaims, MaxClaimDocs)(docs => client.bulkInsert("claims", docs)))
- .both(insertCollectionChunked("patches", allPatches, MaxPatchDocs)(docs => client.bulkInsert("patches", docs)))
- .map { case (((n, e), c), p) => (n, e, c, p) }
- (insertNodesMs, insertEdgesMs, insertClaimsMs, insertPatchesMs) = allInsertMs
- totalMs = buildDocsMs + tombstoneMs + retireClaimsMs + insertNodesMs + insertEdgesMs + insertClaimsMs + insertPatchesMs
- _ <- logChunkTiming(
- s"rev=$newRev $chunkSummary",
- totalMs,
- List(
- "buildDocs" -> buildDocsMs,
- "tombstoneNodes" -> tombstoneMs,
- "retireClaims" -> retireClaimsMs,
- "insertNodes" -> insertNodesMs,
- "insertEdges" -> insertEdgesMs,
- "insertClaims" -> insertClaimsMs,
- "insertPatches" -> insertPatchesMs,
- )
- )
- } yield CommitResult(Rev(newRev), CommitStatus.Ok)
- }
-
- private def tombstoneExistingNodes(logicalIds: Vector[String], newRev: Long): IO[Unit] = {
- if (logicalIds.isEmpty) IO.unit
- else {
- val idList = new java.util.ArrayList[String](logicalIds.size)
- logicalIds.distinct.foreach(idList.add)
- client.execute(
- """FOR n IN nodes
- | FILTER n.logical_id IN @ids
- | AND n.deleted_rev == null
- | UPDATE n WITH { deleted_rev: @rev, updated_at: @now } IN nodes""".stripMargin,
- Map(
- "ids" -> idList.asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "now" -> Instant.now().toString.asInstanceOf[AnyRef]
- )
- )
- }
- }
-
- // Tombstone all active edges whose provenance.source_uri matches a re-ingested file.
- // Without this, edges removed from the parse output (e.g. a decorator CALLS edge that
- // was fixed) persist in the graph indefinitely, since UpsertEdge only updates edges
- // that still exist in the new parse — it never deletes edges that were dropped.
- // The subsequent bulkInsertEdges (overwriteMode=replace) will un-tombstone any edges
- // that still exist in the new parse, so only genuinely removed edges remain tombstoned.
- private def tombstoneExistingEdges(sourceUris: Vector[String], newRev: Long): IO[Unit] = {
- if (sourceUris.isEmpty) IO.unit
- else {
- val uriList = new java.util.ArrayList[String](sourceUris.size)
- sourceUris.distinct.foreach(uriList.add)
- client.execute(
- """FOR e IN edges
- | FILTER e.provenance.source_uri IN @uris
- | AND e.deleted_rev == null
- | UPDATE e WITH { deleted_rev: @rev, updated_at: @now } IN edges""".stripMargin,
- Map(
- "uris" -> uriList.asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "now" -> Instant.now().toString.asInstanceOf[AnyRef]
- )
- )
- }
- }
-
- private def retireOldClaimsByEntityIds(entityIds: Seq[String], newRev: Long): IO[Unit] =
- if (entityIds.isEmpty) IO.unit
- else {
- val idList = new java.util.ArrayList[String](entityIds.size)
- entityIds.foreach(idList.add)
- client.execute(
- """FOR c IN claims
- | FILTER c.entity_id IN @ids
- | AND c.deleted_rev == null
- | UPDATE c WITH { status: "retracted", deleted_rev: @rev } IN claims""".stripMargin,
- Map(
- "ids" -> idList.asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef]
- )
- )
- }
-
- private def retireOldClaims(fileBatches: Vector[FileBatch], newRev: Long): IO[Unit] =
- retireOldClaimsByEntityIds(
- fileBatches.flatMap(_.patch.ops.collect {
- case PatchOp.UpsertNode(id, _, _, _) => id.value.toString
- }).distinct,
- newRev
- )
-
- /**
- * Commit file batches in chunks with pre-assigned revisions.
- *
- * Each chunk gets a unique revision assigned upfront (baseRev+1, baseRev+2, ...),
- * allowing all chunks to build documents and insert concurrently rather than
- * waiting for the previous chunk's revision to be committed. The revision counter
- * is updated once at the end to the final value.
- *
- * When multiple chunks exist and baseRev > 0 (incremental update), tombstone+retire
- * runs once upfront for all files in parallel rather than once per chunk sequentially.
- */
- def commitBatchChunked(
- fileBatches: Vector[FileBatch],
- baseRev: Long,
- chunkSize: Int = DefaultChunkSize,
- maxPayloadBytes: Int = DefaultChunkPayloadBytes
- ): IO[CommitResult] = {
- if (fileBatches.isEmpty)
- return IO.pure(CommitResult(Rev(baseRev), CommitStatus.Ok))
-
- val chunks = chunkForCommit(fileBatches, chunkSize, maxPayloadBytes)
- val multiChunk = chunks.size > 1 && baseRev > 0
- val finalRev = baseRev + chunks.size
-
- // Pre-tombstone: collect all entity IDs upfront and retire/tombstone in one parallel pass.
- val preTombstoneIO: IO[Unit] =
- if (!multiChunk) IO.unit
- else {
- val allIds = fileBatches.flatMap(_.patch.ops.collect {
- case PatchOp.UpsertNode(id, _, _, _) => id.value.toString
- }).distinct
- val sourceUris = fileBatches.map(_.filePath)
- val preTombRev = baseRev + 1L
- timedUnit(tombstoneExistingNodes(allIds.toVector, preTombRev))
- .both(timedUnit(tombstoneExistingEdges(sourceUris, preTombRev)))
- .both(timedUnit(retireOldClaimsByEntityIds(allIds, preTombRev)))
- .flatMap { case ((tombMs, edgeTombMs), retireMs) =>
- if (debugBulkWrites)
- logger.info(s"[bulk-write] pre-tombstone files=${fileBatches.size} tombstoneMs=$tombMs edgeTombMs=$edgeTombMs retireMs=$retireMs")
- else IO.unit
- }
- }
-
- for {
- _ <- preTombstoneIO
- // Pre-assign revisions and run all chunks concurrently.
- // Each chunk writes to unique keys (logicalId_rev) so no conflicts.
- _ <- chunks.zipWithIndex.toVector.parTraverse { case (chunk, idx) =>
- commitBatch(chunk, baseRev + idx, skipTombstone = multiChunk)
- }
- // Update revision counter once at the end.
- _ <- updateRevision(finalRev)
- } yield CommitResult(Rev(finalRev), CommitStatus.Ok)
- }
-
- private def updateRevision(newRev: Long): IO[Unit] = {
- // Use Document API (replace mode) instead of AQL UPSERT.
- // AQL UPSERT produces write-write conflicts when multiple concurrent HTTP
- // requests all try to update the same "current" revision key simultaneously.
- // The Document API replace is atomic at the document level — no conflict possible.
- val doc1 = new java.util.HashMap[String, AnyRef]()
- doc1.put("_key", CurrentRevisionKey)
- doc1.put("rev", Long.box(newRev))
- val doc2 = new java.util.HashMap[String, AnyRef]()
- doc2.put("_key", SourceGraphRevisionKey)
- doc2.put("rev", Long.box(newRev))
- client.bulkInsert("revisions", Seq(doc1, doc2)).void
- }
-
- private def chunkForCommit(
- fileBatches: Vector[FileBatch],
- maxFiles: Int,
- maxPayloadBytes: Int
- ): Vector[Vector[FileBatch]] = {
- val chunks = Vector.newBuilder[Vector[FileBatch]]
- var current = Vector.empty[FileBatch]
- var currentBytes = 0
-
- fileBatches.foreach { batch =>
- val batchBytes = batch.estimatedPayloadBytes
- val exceedsPayload = current.nonEmpty && currentBytes + batchBytes > maxPayloadBytes
- val exceedsCount = current.size >= maxFiles
- if (exceedsPayload || exceedsCount) {
- chunks += current
- current = Vector.empty
- currentBytes = 0
- }
- current = current :+ batch
- currentBytes += batchBytes
- }
-
- if (current.nonEmpty) {
- chunks += current
- }
-
- chunks.result()
- }
-
- private def timed[A](ioa: IO[A]): IO[(A, Long)] =
- for {
- start <- IO.monotonic
- result <- ioa
- end <- IO.monotonic
- } yield (result, (end - start).toMillis)
-
- private def timedUnit[A](ioa: IO[A]): IO[Long] =
- timed(ioa).map(_._2)
-
- private def logChunkTiming(summary: String, totalMs: Long, stages: List[(String, Long)]): IO[Unit] = {
- val shouldLog = debugBulkWrites || totalMs >= SlowChunkMs
- if (!shouldLog) IO.unit
- else {
- val stageSummary = stages.map { case (name, ms) => s"$name=${ms}ms" }.mkString(" ")
- logger.info(s"$summary total=${totalMs}ms $stageSummary")
- }
- }
-}
-
-/**
- * Pre-computed batch of documents for a single file,
- * ready to be merged with other files and bulk-inserted.
- */
-case class FileBatch(
- filePath: String,
- sourceHash: Option[String],
- patch: GraphPatch,
- provenance: java.util.Map[String, AnyRef]
-) {
- // Direct Circe-to-Java conversion — no string serialization round-trip.
- private def jsonToJava(json: Json): AnyRef = json.fold(
- jsonNull = null.asInstanceOf[AnyRef],
- jsonBoolean = b => java.lang.Boolean.valueOf(b),
- jsonNumber = n => java.lang.Double.valueOf(n.toDouble),
- jsonString = s => s,
- jsonArray = arr => {
- val list = new java.util.ArrayList[AnyRef](arr.size)
- arr.foreach(j => list.add(jsonToJava(j)))
- list
- },
- jsonObject = obj => {
- val map = new java.util.LinkedHashMap[String, AnyRef](obj.size)
- obj.toMap.foreach { case (k, v) => map.put(k, jsonToJava(v)) }
- map
- }
- )
-
- private def attrsToJavaMap(attrs: Map[String, Json]): java.util.Map[String, AnyRef] = {
- val result = new java.util.HashMap[String, AnyRef](attrs.size)
- attrs.foreach { case (k, v) => result.put(k, jsonToJava(v)) }
- result
- }
-
- /** Heuristic payload estimate — avoids full Circe serialization round-trip.
- * ~200 bytes per node op, ~180 per edge op, ~150 per claim op, +1KB overhead. */
- lazy val estimatedPayloadBytes: Int = {
- var nodes = 0; var edges = 0; var claims = 0
- patch.ops.foreach {
- case _: PatchOp.UpsertNode => nodes += 1
- case _: PatchOp.UpsertEdge => edges += 1
- case _: PatchOp.AssertClaim => claims += 1
- case _ => ()
- }
- nodes * 200 + edges * 180 + claims * 150 + 1024
- }
-
- def nodeDocuments(rev: Long): Vector[java.util.Map[String, AnyRef]] = {
- val now = Instant.now().toString
- patch.ops.collect { case PatchOp.UpsertNode(id, kind, name, attrs) =>
- val logicalId = id.value.toString
- val doc = new java.util.HashMap[String, AnyRef]()
- doc.put("_key", s"${logicalId}_${rev}")
- doc.put("logical_id", logicalId)
- doc.put("id", logicalId)
- doc.put("kind", NodeKind.toWireString(kind))
- doc.put("name", name)
- doc.put("attrs", attrsToJavaMap(attrs))
- doc.put("provenance", provenance)
- doc.put("created_rev", Long.box(rev))
- doc.put("deleted_rev", null)
- doc.put("created_at", now)
- doc.put("updated_at", now)
- doc: java.util.Map[String, AnyRef]
- }
- }
-
- def edgeDocuments(rev: Long): Vector[java.util.Map[String, AnyRef]] = {
- val now = Instant.now().toString
- patch.ops.collect { case PatchOp.UpsertEdge(id, src, dst, predicate, attrs) =>
- val doc = new java.util.HashMap[String, AnyRef]()
- doc.put("_key", id.value.toString)
- doc.put("_from", s"nodes/${src.value}")
- doc.put("_to", s"nodes/${dst.value}")
- doc.put("id", id.value.toString)
- doc.put("src", src.value.toString)
- doc.put("dst", dst.value.toString)
- doc.put("predicate", predicate.value)
- doc.put("attrs", attrsToJavaMap(attrs))
- doc.put("created_rev", Long.box(rev))
- doc.put("deleted_rev", null)
- doc.put("provenance", provenance)
- doc.put("created_at", now)
- doc.put("updated_at", now)
- doc: java.util.Map[String, AnyRef]
- }
- }
-
- def claimDocuments(rev: Long): Vector[java.util.Map[String, AnyRef]] =
- patch.ops.collect { case PatchOp.AssertClaim(entityId, field, value, confidence, inferenceVersion) =>
- val doc = new java.util.HashMap[String, AnyRef]()
- doc.put("_key", UUID.randomUUID().toString)
- doc.put("entity_id", entityId.value.toString)
- doc.put("field", field)
- doc.put("value", jsonToJava(value))
- doc.put("confidence", confidence.map(java.lang.Double.valueOf).orNull)
- doc.put("status", "active")
- doc.put("created_rev", Long.box(rev))
- doc.put("deleted_rev", null)
- doc.put("provenance", provenance)
- doc.put("inference_version", inferenceVersion.orNull)
- doc: java.util.Map[String, AnyRef]
- }
-
- def patchDocument(rev: Long): java.util.Map[String, AnyRef] = {
- val source = new java.util.HashMap[String, AnyRef]()
- source.put("uri", patch.source.uri)
- source.put("sourceHash", patch.source.sourceHash.orNull)
- source.put("extractor", patch.source.extractor)
- source.put("sourceType", patch.source.sourceType.asJson.asString.getOrElse(patch.source.sourceType.toString))
-
- val replaces = new java.util.ArrayList[String](patch.replaces.size)
- patch.replaces.foreach(id => replaces.add(id.value.toString))
-
- val entityIds = new java.util.LinkedHashSet[String]()
- var nodeOpCount = 0
- var edgeOpCount = 0
- var claimOpCount = 0
- patch.ops.foreach {
- case PatchOp.UpsertNode(id, _, _, _) =>
- nodeOpCount += 1
- entityIds.add(id.value.toString)
- case PatchOp.DeleteNode(id) =>
- nodeOpCount += 1
- entityIds.add(id.value.toString)
- case PatchOp.UpsertEdge(id, _, _, _, _) =>
- edgeOpCount += 1
- entityIds.add(id.value.toString)
- case PatchOp.DeleteEdge(id) =>
- edgeOpCount += 1
- entityIds.add(id.value.toString)
- case PatchOp.AssertClaim(entityId, _, _, _, _) =>
- claimOpCount += 1
- entityIds.add(entityId.value.toString)
- case PatchOp.RetractClaim(claimId) =>
- claimOpCount += 1
- entityIds.add(claimId.value.toString)
- }
-
- val entityIdList = new java.util.ArrayList[String](entityIds.size)
- val entityIdIter = entityIds.iterator()
- while (entityIdIter.hasNext) {
- entityIdList.add(entityIdIter.next())
- }
-
- val data = new java.util.HashMap[String, AnyRef]()
- data.put("patchId", patch.patchId.value.toString)
- data.put("actor", patch.actor)
- data.put("timestamp", patch.timestamp.toString)
- data.put("source", source)
- data.put("baseRev", Long.box(patch.baseRev.value))
- data.put("replaces", replaces)
- data.put("intent", patch.intent.orNull)
- data.put("entityIds", entityIdList)
- data.put("opCount", Int.box(patch.ops.size))
- data.put("nodeOpCount", Int.box(nodeOpCount))
- data.put("edgeOpCount", Int.box(edgeOpCount))
- data.put("claimOpCount", Int.box(claimOpCount))
-
- val doc = new java.util.HashMap[String, AnyRef]()
- doc.put("_key", patch.patchId.value.toString)
- doc.put("patch_id", patch.patchId.value.toString)
- doc.put("rev", Long.box(rev))
- // Bulk ingest only needs patch metadata plus touched-entity IDs for provenance
- // and source-hash lookups. Storing full ops here duplicates the graph payload
- // and significantly inflates Kubernetes save latency.
- doc.put("data", data)
- doc: java.util.Map[String, AnyRef]
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala b/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
deleted file mode 100644
index ee6246d1..00000000
--- a/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
+++ /dev/null
@@ -1,67 +0,0 @@
-package ix.memory.db
-
-import cats.effect.IO
-import io.circe.Json
-import ix.memory.model._
-
-trait GraphQueryApi {
- def getNode(id: NodeId, asOfRev: Option[Rev] = None): IO[Option[GraphNode]]
- def findNodesByKind(kind: NodeKind, limit: Int = 100): IO[Vector[GraphNode]]
- def listDecisions(limit: Int = 50, topic: Option[String] = None): IO[Vector[GraphNode]]
- def searchNodes(text: String, limit: Int = 20,
- kind: Option[String] = None, language: Option[String] = None,
- asOfRev: Option[Rev] = None,
- nameOnly: Boolean = false): IO[Vector[GraphNode]]
- def expand(nodeId: NodeId, direction: Direction,
- predicates: Option[Set[String]] = None, hops: Int = 1,
- asOfRev: Option[Rev] = None): IO[ExpandResult]
- def getClaims(entityId: NodeId): IO[Vector[Claim]]
- def getClaimsBatch(entityIds: Seq[NodeId]): IO[Vector[Claim]]
- 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, Option[GraphNode])]]
- def getDiffSummary(fromRev: Rev, toRev: Rev): IO[Map[String, Int]]
- def resolvePrefix(prefix: String): IO[Vector[NodeId]]
- def getSourceHashes(sourceUris: Seq[String]): IO[Map[String, String]]
- def hasIngestBaseline(): IO[Boolean]
- def projectExpand(
- nodeId: NodeId,
- direction: Direction,
- predicates: Option[Set[String]] = None,
- hops: Int = 1,
- asOfRev: Option[Rev] = None
- ): IO[Option[ExpandProjection]] = IO.pure(None)
- def expandByName(
- name: String,
- direction: Direction,
- predicates: Option[Set[String]] = None,
- kinds: Option[Set[NodeKind]] = None
- ): IO[ExpandResult]
-}
-
-sealed trait Direction
-object Direction {
- case object Out extends Direction
- case object In extends Direction
- case object Both extends Direction
-}
-
-final case class ExpandProjection(
- kind: String,
- nodes: Vector[GraphNode],
- edges: Vector[GraphEdge]
-)
-
-final case class ExpandResult(
- nodes: Vector[GraphNode],
- edges: Vector[GraphEdge],
- projection: Option[ExpandProjection] = None
-)
-
-object ExpandResult {
- import io.circe.Encoder
- import io.circe.generic.semiauto.deriveEncoder
- implicit val projectionEncoder: Encoder[ExpandProjection] = deriveEncoder[ExpandProjection]
- implicit val encoder: Encoder[ExpandResult] = deriveEncoder[ExpandResult]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/db/GraphWriteApi.scala b/memory-layer/src/main/scala/ix/memory/db/GraphWriteApi.scala
deleted file mode 100644
index 2a79e36e..00000000
--- a/memory-layer/src/main/scala/ix/memory/db/GraphWriteApi.scala
+++ /dev/null
@@ -1,17 +0,0 @@
-package ix.memory.db
-
-import cats.effect.IO
-import ix.memory.model._
-
-trait GraphWriteApi {
- def commitPatch(patch: GraphPatch): IO[CommitResult]
-}
-
-final case class CommitResult(newRev: Rev, status: CommitStatus)
-
-sealed trait CommitStatus
-object CommitStatus {
- case object Ok extends CommitStatus
- case object Idempotent extends CommitStatus
- case object BaseRevMismatch extends CommitStatus
-}
diff --git a/memory-layer/src/main/scala/ix/memory/map/LouvainClustering.scala b/memory-layer/src/main/scala/ix/memory/map/LouvainClustering.scala
deleted file mode 100644
index 3ae6331d..00000000
--- a/memory-layer/src/main/scala/ix/memory/map/LouvainClustering.scala
+++ /dev/null
@@ -1,558 +0,0 @@
-package ix.memory.map
-
-import java.util.UUID
-
-import ix.memory.model.NodeId
-
-import scala.util.Random
-
-/**
- * Louvain community detection with recursive graph coarsening.
- *
- * Produces multi-level partitions: each level groups the original file nodes
- * into progressively coarser communities.
- *
- * Algorithm:
- * 1. Initialize each node in its own community.
- * 2. For each node (random order), try moving it to a neighbor's community.
- * Accept if the modularity gain ΔQ > 0.
- * 3. Repeat until no improvement or max passes reached.
- * 4. Collapse communities into supernodes; rebuild weighted graph.
- * 5. Repeat on the coarsened graph.
- * 6. Project all levels back to original file IDs.
- *
- * Modularity gain formula (standard Louvain):
- * ΔQ(i → C) = k_i_in(C) / m − γ · Σ_tot(C) · k_i / (2m²)
- *
- * where:
- * k_i_in(C) = sum of edge weights from node i to nodes in community C
- * Σ_tot(C) = sum of all node degrees in C
- * k_i = degree of node i
- * m = total edge weight (each undirected edge counted once)
- * γ = resolution parameter (1.0 by default)
- */
-object LouvainClustering {
-
- /** A partition of original file NodeIds into communities at one hierarchy level. */
- final case class LevelPartition(
- communities: Vector[Set[NodeId]], // each Set is a community of original file IDs
- assignment: Map[NodeId, Int] // fileId → community index
- )
-
- /**
- * Run multi-level Louvain clustering.
- *
- * Returns a vector of LevelPartitions, finest first (index 0 = most granular).
- * Each partition covers ALL original file nodes.
- *
- * Adaptive re-clustering: when the coarsest level exceeds
- * MaxTopLevelCommunities, the algorithm halves the resolution and
- * retries up to MaxAdaptiveRetries times to produce coarser groupings.
- *
- * Singleton absorption: after each Louvain pass, singleton communities
- * (size 1) that have edges to other communities are merged into the
- * community they have the strongest total edge weight to.
- */
- private val MaxTopLevelCommunities = 60
- private val MaxAdaptiveRetries = 3
- /** When consecutive levels jump by more than this ratio, insert intermediate levels. */
- private val MaxLevelJumpRatio = 15
- private val MaxGapFillAttempts = 3
-
- def cluster(
- graph: WeightedFileGraph,
- maxLevels: Int = 5,
- resolution: Double = -1.0,
- minCommunitySize: Int = 2,
- seed: Long = 42L
- ): Vector[LevelPartition] = {
- if (graph.vertices.isEmpty || graph.totalWeight == 0.0) return Vector.empty
-
- val baseResolution =
- if (resolution > 0.0) resolution else adaptiveResolution(graph.vertices.size)
-
- var effectiveResolution = baseResolution
- var bestLevels: Vector[LevelPartition] = Vector.empty
- var retries = 0
-
- while (retries <= MaxAdaptiveRetries) {
- val numSeeds = if (graph.vertices.size < 30) 1L else 3L
- val runs = (0L until numSeeds).map { offset =>
- val levels = clusterOnce(
- graph = graph,
- maxLevels = maxLevels,
- resolution = effectiveResolution,
- minCommunitySize = minCommunitySize,
- seed = seed + offset
- )
- val score = levels.headOption
- .map(level => modularity(graph, level.assignment, effectiveResolution))
- .getOrElse(Double.NegativeInfinity)
- levels -> score
- }
-
- bestLevels = fillHierarchyGaps(
- runs.maxBy(_._2)._1,
- graph, minCommunitySize, seed
- )
-
- val coarsestCount = bestLevels.lastOption
- .map(_.communities.count(_.size >= minCommunitySize))
- .getOrElse(0)
-
- if (coarsestCount > MaxTopLevelCommunities && retries < MaxAdaptiveRetries) {
- effectiveResolution *= 0.5
- retries += 1
- } else {
- retries = MaxAdaptiveRetries + 1 // exit loop
- }
- }
-
- bestLevels
- }
-
- private def clusterOnce(
- graph: WeightedFileGraph,
- maxLevels: Int,
- resolution: Double,
- minCommunitySize: Int,
- seed: Long
- ): Vector[LevelPartition] = {
- val rng = new Random(seed)
-
- var nodeExpansion: Map[NodeId, Set[NodeId]] =
- graph.vertices.map(v => v.id -> Set(v.id)).toMap
-
- var currentGraph = graph
- val levels = scala.collection.mutable.ArrayBuffer.empty[LevelPartition]
-
- var level = 0
- while (level < maxLevels && currentGraph.vertices.size > 1) {
- val rawAssignment = absorbSingletons(louvainPass(currentGraph, resolution, rng), currentGraph)
- val projected = projectPartition(rawAssignment, nodeExpansion)
- val meaningful = projected.communities.count(_.size >= minCommunitySize)
-
- if (meaningful <= 1 && levels.nonEmpty) {
- level = maxLevels
- } else {
- levels += projected
-
- val (coarsenedGraph, newExpansion) =
- coarsen(currentGraph, rawAssignment, nodeExpansion)
-
- nodeExpansion = newExpansion
- currentGraph = coarsenedGraph
- level += 1
-
- if (currentGraph.vertices.size < 3) level = maxLevels
- }
- }
-
- levels.toVector
- }
-
- /**
- * When consecutive levels jump from N communities to M (ratio N/M > MaxLevelJumpRatio),
- * insert intermediate levels by sub-clustering the finer level's communities at
- * progressively lower resolutions to bridge the gap.
- *
- * Example: if level 1 has 847 communities and level 2 has 7, this inserts
- * intermediate levels (e.g., ~60-80 communities) between them.
- */
- private def fillHierarchyGaps(
- levels: Vector[LevelPartition],
- graph: WeightedFileGraph,
- minCommunitySize: Int,
- seed: Long
- ): Vector[LevelPartition] = {
- if (levels.size < 2) return levels
-
- val result = scala.collection.mutable.ArrayBuffer[LevelPartition]()
- result += levels.head
-
- for (i <- 1 until levels.size) {
- val finer = levels(i - 1)
- val coarser = levels(i)
- val finerCount = finer.communities.count(_.size >= minCommunitySize)
- val coarserCount = coarser.communities.count(_.size >= minCommunitySize)
-
- val ratio = if (coarserCount > 0) finerCount.toDouble / coarserCount else 0.0
- if (finerCount > 0 && coarserCount > 0 && ratio > MaxLevelJumpRatio) {
- // Build a weighted graph of the finer-level communities
- val interLevels = bridgeLevels(finer, coarser, graph, minCommunitySize, seed)
- result ++= interLevels
- }
-
- result += coarser
- }
-
- result.toVector
- }
-
- /**
- * Create intermediate partitions between a fine level and a coarse level.
- * Builds a community graph from the fine level and runs Louvain at varying
- * resolutions to produce partitions with intermediate community counts.
- */
- private def bridgeLevels(
- finer: LevelPartition,
- coarser: LevelPartition,
- graph: WeightedFileGraph,
- minCommunitySize: Int,
- seed: Long
- ): Vector[LevelPartition] = {
- val finerCount = finer.communities.count(_.size >= minCommunitySize)
- val coarserCount = coarser.communities.count(_.size >= minCommunitySize)
-
- // Build a supernode graph where each node = one fine-level community
- val commToId: Map[Int, NodeId] = finer.communities.indices.map { ci =>
- val key = "map:bridge:" + ci
- ci -> NodeId(java.util.UUID.nameUUIDFromBytes(key.getBytes("UTF-8")))
- }.toMap
-
- val superVertices = commToId.map { case (ci, nid) =>
- FileVertex(nid, s"comm_$ci")
- }.toVector
-
- // Aggregate inter-community edge weights
- val edgeWeights = scala.collection.mutable.Map[(NodeId, NodeId), Double]()
- for ((src, neighbors) <- graph.adjMatrix) {
- val ci = finer.assignment.getOrElse(src, -1)
- if (ci >= 0) {
- val si = commToId(ci)
- for ((dst, w) <- neighbors) {
- val cj = finer.assignment.getOrElse(dst, -1)
- if (cj >= 0 && ci != cj) {
- val sj = commToId(cj)
- val key = if (si.value.compareTo(sj.value) < 0) (si, sj) else (sj, si)
- edgeWeights(key) = edgeWeights.getOrElse(key, 0.0) + w / 2.0
- }
- }
- }
- }
-
- val newAdj = scala.collection.mutable.Map[NodeId, scala.collection.mutable.Map[NodeId, Double]]()
- for (((si, sj), w) <- edgeWeights) {
- newAdj.getOrElseUpdate(si, scala.collection.mutable.Map())(sj) = w
- newAdj.getOrElseUpdate(sj, scala.collection.mutable.Map())(si) = w
- }
- val adjFinal = newAdj.map { case (k, m) => k -> m.toMap }.toMap
- val degrees = adjFinal.map { case (k, m) => k -> m.values.sum }
- val totalWeight = edgeWeights.values.sum
-
- val bridgeGraph = WeightedFileGraph(superVertices, adjFinal, degrees.toMap, totalWeight, Map.empty)
-
- // Try different resolutions to find intermediate community counts.
- // Higher resolution → more communities (finer); lower → fewer (coarser).
- val rng = new Random(seed + 999)
- val targetMin = coarserCount * 2
- val targetMax = finerCount / 2
- val inserted = scala.collection.mutable.ArrayBuffer[LevelPartition]()
-
- // Sweep resolutions: start very high (fine) and decrease toward coarse.
- // This explores intermediate granularities between the two existing levels.
- val resolutions = Vector(4.0, 2.0, 1.0, 0.5, 0.25, 0.1)
- for (res <- resolutions) {
- val assignment = absorbSingletons(louvainPass(bridgeGraph, res, rng), bridgeGraph)
- val projected = projectBridgePartition(assignment, commToId, finer)
- val count = projected.communities.count(_.size >= minCommunitySize)
-
- if (count > coarserCount && count < finerCount) {
- val isDuplicate = inserted.exists { existing =>
- val ec = existing.communities.count(_.size >= minCommunitySize)
- ec > 0 && (count.toDouble / ec > 0.7 && count.toDouble / ec < 1.4)
- }
- if (!isDuplicate) inserted += projected
- }
- }
-
- // Sort by community count descending (finest first) to maintain level ordering
- inserted.sortBy(p => -p.communities.count(_.size >= minCommunitySize)).toVector
- }
-
- /** Project a bridge-graph partition back to original file IDs. */
- private def projectBridgePartition(
- bridgeAssignment: Map[NodeId, Int],
- commToId: Map[Int, NodeId],
- finer: LevelPartition
- ): LevelPartition = {
- val idToComm = commToId.map(_.swap)
- // For each bridge community, collect all original file IDs from the fine-level communities it contains
- val bridgeComms = scala.collection.mutable.Map[Int, scala.collection.mutable.Set[NodeId]]()
- for ((nid, bridgeIdx) <- bridgeAssignment) {
- idToComm.get(nid).foreach { fineCommIdx =>
- val files = finer.communities(fineCommIdx)
- bridgeComms.getOrElseUpdate(bridgeIdx, scala.collection.mutable.Set()) ++= files
- }
- }
- val commVec = bridgeComms.toVector.sortBy(_._1).map(_._2.toSet)
- val reverseAssign = (for ((members, idx) <- commVec.zipWithIndex; f <- members) yield f -> idx).toMap
- LevelPartition(commVec, reverseAssign)
- }
-
- private[map] def adaptiveResolution(fileCount: Int): Double =
- if (fileCount < 50) 1.2
- else if (fileCount <= 500) 1.0
- else if (fileCount <= 2000) 0.8
- else if (fileCount <= 5000) 0.6
- else 0.4
-
- // ── Single Louvain pass ────────────────────────────────────────────
-
- /**
- * One full Louvain pass on the given graph.
- * Returns assignment: nodeId → community index (indices are 0-based, dense).
- */
- private def louvainPass(
- graph: WeightedFileGraph,
- resolution: Double,
- rng: Random
- ): Map[NodeId, Int] = {
- val nodes = graph.vertices.map(_.id).toArray
- val adj = graph.adjMatrix
- val m = graph.totalWeight.max(1e-12)
-
- // Initialize: node i → community i
- val commOf = scala.collection.mutable.Map(nodes.zipWithIndex: _*)
- val sumTot = scala.collection.mutable.Map(
- nodes.zipWithIndex.map { case (n, i) => i -> graph.degrees.getOrElse(n, 0.0) }: _*
- ).withDefaultValue(0.0)
- // communities: commId → members (mutable set)
- val members = scala.collection.mutable.Map(
- nodes.zipWithIndex.map { case (n, i) => i -> scala.collection.mutable.Set(n) }: _*
- )
-
- var continue = true
- val wToComm = scala.collection.mutable.HashMap.empty[Int, Double]
-
- while (continue) {
- shuffleInPlace(nodes, rng)
- var improved = false
- var passGain = 0.0
-
- var idx = 0
- while (idx < nodes.length) {
- val node = nodes(idx)
- val ci = commOf(node)
- val ki = graph.degrees.getOrElse(node, 0.0)
-
- // Compute weight from this node to each neighboring community
- wToComm.clear()
- for ((neighbor, w) <- adj.getOrElse(node, Map.empty)) {
- val community = commOf(neighbor)
- wToComm.update(community, wToComm.getOrElse(community, 0.0) + w)
- }
-
- // Remove node from current community
- val kiInCi = wToComm.getOrElse(ci, 0.0)
- sumTot(ci) -= ki
- members(ci) -= node
- // Temporarily isolate: treat as its own community (gains = 0 baseline)
- commOf(node) = -1
-
- var bestComm = ci
- var bestGain = 0.0
-
- for ((cj, kiInCj) <- wToComm if cj != -1) {
- val dQ = kiInCj / m - resolution * sumTot.getOrElse(cj, 0.0) * ki / (2.0 * m * m)
- if (dQ > bestGain) {
- bestGain = dQ
- bestComm = cj
- }
- }
-
- // Place node in best community (or back in its old one if no improvement)
- commOf(node) = bestComm
- members.getOrElseUpdate(bestComm, scala.collection.mutable.Set()) += node
- val kiInBest = wToComm.getOrElse(bestComm, 0.0)
- sumTot(bestComm) += ki
-
- if (bestComm != ci) {
- improved = true
- passGain += bestGain
- }
- idx += 1
- }
-
- continue = improved && passGain >= 1e-6
- }
-
- // Re-index communities from 0 to N-1 (dense)
- val nonEmpty = members.filter(_._2.nonEmpty).toMap
- val reindex = nonEmpty.keys.toVector.sorted.zipWithIndex.toMap
- val finalAssign = commOf.map { case (n, c) =>
- n -> reindex.getOrElse(c, 0)
- }.toMap
-
- finalAssign
- }
-
- private def shuffleInPlace(nodes: Array[NodeId], rng: Random): Unit = {
- var i = nodes.length - 1
- while (i > 0) {
- val j = rng.nextInt(i + 1)
- val tmp = nodes(i)
- nodes(i) = nodes(j)
- nodes(j) = tmp
- i -= 1
- }
- }
-
- private def modularity(
- graph: WeightedFileGraph,
- assignment: Map[NodeId, Int],
- resolution: Double
- ): Double = {
- val communities = assignment.groupBy(_._2).map { case (idx, entries) => idx -> entries.keySet }
- val m = graph.totalWeight.max(1e-12)
-
- communities.values.foldLeft(0.0) { (acc, members) =>
- val totalDegree = members.iterator.map(node => graph.degrees.getOrElse(node, 0.0)).sum
- var internalWeight = 0.0
- for {
- src <- members
- (dst, weight) <- graph.adjMatrix.getOrElse(src, Map.empty)
- if members.contains(dst) && src.value.compareTo(dst.value) < 0
- } internalWeight += weight
-
- acc + (internalWeight / m) - resolution * math.pow(totalDegree / (2.0 * m), 2.0)
- }
- }
-
- // ── Singleton absorption ───────────────────────────────────────────
-
- /**
- * After a Louvain pass, merge singleton communities into the neighbor
- * community they have the strongest total edge weight to. Nodes with
- * zero edges to any other community stay as singletons (truly isolated).
- */
- private def absorbSingletons(
- assignment: Map[NodeId, Int],
- graph: WeightedFileGraph
- ): Map[NodeId, Int] = {
- // Identify singleton communities
- val commMembers = assignment.groupBy(_._2).map { case (c, m) => c -> m.keySet }
- val singletonComms = commMembers.filter(_._2.size == 1).keySet
-
- if (singletonComms.isEmpty) return assignment
-
- val result = scala.collection.mutable.Map(assignment.toSeq: _*)
-
- for (comm <- singletonComms) {
- val node = commMembers(comm).head
- val neighbors = graph.adjMatrix.getOrElse(node, Map.empty)
- if (neighbors.nonEmpty) {
- // Sum edge weight to each neighboring community (excluding our own singleton)
- val weightByComm = scala.collection.mutable.Map.empty[Int, Double]
- for ((neighbor, w) <- neighbors) {
- val nc = result(neighbor)
- if (nc != comm) {
- weightByComm(nc) = weightByComm.getOrElse(nc, 0.0) + w
- }
- }
- // Merge into the community with the strongest total weight
- if (weightByComm.nonEmpty) {
- result(node) = weightByComm.maxBy(_._2)._1
- }
- }
- }
-
- // Re-index to keep dense community indices
- val usedComms = result.values.toVector.distinct.sorted
- val reindex = usedComms.zipWithIndex.toMap
- result.map { case (n, c) => n -> reindex(c) }.toMap
- }
-
- // ── Projection ─────────────────────────────────────────────────────
-
- private def projectPartition(
- assignment: Map[NodeId, Int],
- nodeExpansion: Map[NodeId, Set[NodeId]]
- ): LevelPartition = {
- // Map: community index → set of original file IDs
- val comms = scala.collection.mutable.Map[Int, scala.collection.mutable.Set[NodeId]]()
- for ((node, commIdx) <- assignment) {
- val origFiles = nodeExpansion.getOrElse(node, Set(node))
- comms.getOrElseUpdate(commIdx, scala.collection.mutable.Set()) ++= origFiles
- }
-
- val commVec = comms.toVector.sortBy(_._1).map(_._2.toSet)
- // Build reverse assignment: origFileId → commIdx
- val reverseAssign = (for ((members, idx) <- commVec.zipWithIndex; f <- members) yield f -> idx).toMap
-
- LevelPartition(commVec, reverseAssign)
- }
-
- // ── Graph coarsening ────────────────────────────────────────────────
-
- /**
- * Collapse each community in `assignment` into a single supernode.
- * Returns the coarsened graph and an updated nodeExpansion map.
- */
- private def coarsen(
- graph: WeightedFileGraph,
- assignment: Map[NodeId, Int],
- nodeExpansion: Map[NodeId, Set[NodeId]]
- ): (WeightedFileGraph, Map[NodeId, Set[NodeId]]) = {
- // Group current nodes by community
- val commGroups: Map[Int, Set[NodeId]] =
- assignment.groupBy(_._2).map { case (k, v) => k -> v.keySet }
-
- // Assign a deterministic new NodeId to each community (based on sorted member UUIDs)
- val commToSuper: Map[Int, NodeId] = commGroups.map { case (ci, members) =>
- val key = "map:super:" + members.map(_.value.toString).toList.sorted.mkString(",")
- ci -> NodeId(UUID.nameUUIDFromBytes(key.getBytes("UTF-8")))
- }
-
- // Build supernode expansion
- val newExpansion: Map[NodeId, Set[NodeId]] = commGroups.map { case (ci, members) =>
- val origFiles = members.flatMap(n => nodeExpansion.getOrElse(n, Set(n)))
- commToSuper(ci) -> origFiles
- }
-
- // Build supernode vertices (use community index as label for debugging)
- val superVertices = commToSuper.map { case (ci, superId) =>
- FileVertex(superId, s"supernode_$ci")
- }.toVector
-
- // Aggregate inter-community edges
- val edgeWeights =
- scala.collection.mutable.Map[(NodeId, NodeId), Double]()
-
- for ((src, neighbors) <- graph.adjMatrix) {
- val ci = assignment.getOrElse(src, -1)
- if (ci >= 0) {
- val si = commToSuper(ci)
- for ((dst, w) <- neighbors) {
- val cj = assignment.getOrElse(dst, -1)
- if (cj >= 0 && ci != cj) {
- val sj = commToSuper(cj)
- val key = if (si.value.compareTo(sj.value) < 0) (si, sj) else (sj, si)
- edgeWeights(key) = edgeWeights.getOrElse(key, 0.0) + w / 2.0 // undirected: each pair counted twice in adj
- }
- }
- }
- }
-
- // Build new undirected adjacency for the coarsened graph
- val newAdj = scala.collection.mutable.Map[NodeId, scala.collection.mutable.Map[NodeId, Double]]()
- for (((si, sj), w) <- edgeWeights) {
- newAdj.getOrElseUpdate(si, scala.collection.mutable.Map())(sj) = w
- newAdj.getOrElseUpdate(sj, scala.collection.mutable.Map())(si) = w
- }
-
- val newAdjFinal = newAdj.map { case (k, m) => k -> m.toMap }.toMap
- val newDegrees = newAdjFinal.map { case (k, m) => k -> m.values.sum }
- val newTotalWeight = edgeWeights.values.sum
-
- val coarsened = WeightedFileGraph(
- superVertices,
- newAdjFinal,
- newDegrees.toMap,
- newTotalWeight,
- Map.empty
- )
-
- (coarsened, newExpansion)
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/map/MapGraphBuilder.scala b/memory-layer/src/main/scala/ix/memory/map/MapGraphBuilder.scala
deleted file mode 100644
index 55a1d1d1..00000000
--- a/memory-layer/src/main/scala/ix/memory/map/MapGraphBuilder.scala
+++ /dev/null
@@ -1,332 +0,0 @@
-package ix.memory.map
-
-import cats.effect.IO
-import org.typelevel.log4cats.slf4j.Slf4jLogger
-
-import ix.memory.db.ArangoClient
-import ix.memory.model.{NodeId, Rev}
-
-/**
- * Builds a weighted undirected file-level coupling graph from ArangoDB.
- *
- * Key design choices:
- * - Uses provenance.source_uri (absolute path) as the join key between file
- * nodes and coupling edges, avoiding stale CONTAINS traversals.
- * - Skips edges where either endpoint has no live provenance (external libs,
- * tombstoned symbols with no replacement).
- *
- * Signal weights (from spec):
- * α = 1.0 call coupling (highest importance)
- * β = 0.5 import coupling
- * γ = 1.2 extends/implements (type/interface cohesion proxy)
- * ε = 0.2 path proximity (weak structural prior)
- */
-class MapGraphBuilder(client: ArangoClient) {
- private val logger = Slf4jLogger.getLoggerFromName[IO]("ix.map.build")
-
- private val Alpha = 1.0
- private val Beta = 0.5
- private val Gamma = 1.2
- private val Epsilon = 0.2
- private val MaxEdges = 200000
- private val PathProximityMinFiles = 2
- private val PathProximityMaxFiles = 50
- private val SourceGraphRevisionKey = "source-graph"
- private val IgnoredNodeKinds = Vector(
- "module", "config", "config_entry", "doc", "decision", "intent",
- "bug", "plan", "task", "goal", "region",
- "chunk" // chunks are code-text segments, never CALLS/IMPORTS/EXTENDS/IMPLEMENTS endpoints
- )
- private val CouplingPredicates = Vector("CALLS", "IMPORTS", "EXTENDS", "IMPLEMENTS")
-
- def discoverFiles(): IO[Vector[FileVertex]] = fetchFiles()
-
- def buildGraph(files: Vector[FileVertex]): IO[WeightedFileGraph] =
- for {
- t0 <- IO(System.currentTimeMillis())
- rawPairs <- fetchCouplingByUri()
- t1 <- IO(System.currentTimeMillis())
- graph = computeGraph(files, rawPairs)
- t2 <- IO(System.currentTimeMillis())
- _ <- logger.info(
- s"map graph files=${files.size} raw_pairs=${rawPairs.size} fetch=${t1-t0}ms compute=${t2-t1}ms total=${t2-t0}ms"
- )
- } yield graph
-
- def build(): IO[WeightedFileGraph] =
- for {
- files <- discoverFiles()
- graph <- buildGraph(files)
- } yield graph
-
- /**
- * Revision of the graph inputs that materially affect architecture mapping.
- * This intentionally ignores derived artifacts like persisted region nodes or
- * scoring claims so cached maps stay reusable across unchanged source graphs.
- */
- def currentInputRev(): IO[Rev] =
- loadSourceGraphRevision().flatMap {
- case Some(rev) => IO.pure(rev)
- case None => scanCurrentInputRev()
- }
-
- private def loadSourceGraphRevision(): IO[Option[Rev]] =
- client.queryOne(
- """FOR r IN revisions
- | FILTER r._key == @key
- | LIMIT 1
- | RETURN r.rev""".stripMargin,
- Map("key" -> SourceGraphRevisionKey.asInstanceOf[AnyRef])
- ).map(_.flatMap(_.as[Long].toOption).map(Rev(_)))
-
- private def scanCurrentInputRev(): IO[Rev] =
- client.queryOne(
- """LET node_rev = MAX(
- | FOR n IN nodes
- | FILTER n.provenance.source_uri != null
- | AND n.kind NOT IN @ignoredKinds
- | RETURN MAX([
- | TO_NUMBER(n.created_rev),
- | n.deleted_rev != null ? TO_NUMBER(n.deleted_rev) : 0
- | ])
- |)
- |LET edge_rev = MAX(
- | FOR e IN edges
- | FILTER e.predicate IN @predicates
- | RETURN MAX([
- | TO_NUMBER(e.created_rev),
- | e.deleted_rev != null ? TO_NUMBER(e.deleted_rev) : 0
- | ])
- |)
- |RETURN MAX([
- | node_rev != null ? node_rev : 0,
- | edge_rev != null ? edge_rev : 0
- |])""".stripMargin,
- Map(
- "ignoredKinds" -> IgnoredNodeKinds.toArray.asInstanceOf[AnyRef],
- "predicates" -> CouplingPredicates.toArray.asInstanceOf[AnyRef]
- )
- ).map { json =>
- Rev(json.flatMap(_.as[Long].toOption).getOrElse(0L))
- }
-
- def liveFileCount(): IO[Int] =
- client.queryOne(
- """FOR n IN nodes
- | FILTER n.kind == "file"
- | AND n.deleted_rev == null
- | AND n.provenance.source_uri != null
- | LET base = LOWER(n.name)
- | LET ext = CONTAINS(base, ".") ? LOWER(LAST(SPLIT(base, "."))) : null
- | FILTER ext IN @extensions OR base == "dockerfile" OR LIKE(base, "%.dockerfile")
- | COLLECT WITH COUNT INTO cnt
- | RETURN cnt""".stripMargin,
- Map("extensions" -> SourceExtensions.toArray.asInstanceOf[AnyRef])
- ).map(_.flatMap(_.as[Int].toOption).getOrElse(0))
-
- // ── ArangoDB queries ───────────────────────────────────────────────
-
- /**
- * Returns file nodes using provenance.source_uri as the path key.
- * Files without source_uri are skipped (unusual but defensive).
- */
- // Source-like artifacts to include in the architectural map.
- // JSON and common config formats are included so data-heavy repos can still
- // receive file-level singleton regions even when they have little coupling.
- private val SourceExtensions =
- Set("scala","java","kt","ts","tsx","js","jsx","py","go","rs","cs","cpp","c","h","hpp","rb","json","yaml","yml","toml")
-
- private def fetchFiles(): IO[Vector[FileVertex]] =
- client.query(
- """FOR n IN nodes
- | FILTER n.kind == "file"
- | AND n.deleted_rev == null
- | AND n.provenance.source_uri != null
- | LET base = LOWER(n.name)
- | LET ext = CONTAINS(base, ".") ? LOWER(LAST(SPLIT(base, "."))) : null
- | FILTER ext IN @extensions OR base == "dockerfile" OR LIKE(base, "%.dockerfile")
- | RETURN {id: n.logical_id, path: n.provenance.source_uri}""".stripMargin,
- Map("extensions" -> SourceExtensions.toArray.asInstanceOf[AnyRef])
- ).map { rows =>
- rows.flatMap { json =>
- val c = json.hcursor
- for {
- idStr <- c.get[String]("id").toOption
- path <- c.get[String]("path").toOption
- uuid <- try Some(java.util.UUID.fromString(idStr)) catch { case _: Exception => None }
- } yield FileVertex(NodeId(uuid), path)
- }.toVector
- }
-
- /**
- * Returns cross-file coupling pairs keyed by absolute provenance.source_uri.
- *
- * Builds a unified logical_id → source_uri map over all live non-metadata
- * nodes (files AND symbols), then resolves coupling edge endpoints via that
- * map. Stale edges whose endpoints have been tombstoned are naturally
- * excluded because deleted nodes have no entry in the map.
- */
- private def fetchCouplingByUri(): IO[Vector[RawFilePair]] =
- client.query(
- """LET uriMap = (
- | FOR n IN nodes
- | FILTER n.deleted_rev == null
- | AND n.provenance.source_uri != null
- | AND n.kind NOT IN @ignoredKinds
- | RETURN { id: n.logical_id, uri: n.provenance.source_uri }
- |)
- |LET byId = ZIP(uriMap[*].id, uriMap[*].uri)
- |FOR e IN edges
- | FILTER e.predicate IN @predicates
- | AND e.deleted_rev == null
- | LET su = byId[e.src]
- | LET du = byId[e.dst]
- | FILTER su != null AND du != null AND su != du
- | COLLECT srcUri = su, dstUri = du, pred = e.predicate
- | WITH COUNT INTO cnt
- | RETURN {srcUri, dstUri, predicate: pred, count: cnt}""".stripMargin,
- Map(
- "ignoredKinds" -> IgnoredNodeKinds.toArray.asInstanceOf[AnyRef],
- "predicates" -> CouplingPredicates.toArray.asInstanceOf[AnyRef]
- )
- ).map { rows =>
- rows.flatMap { json =>
- val c = json.hcursor
- for {
- srcUri <- c.get[String]("srcUri").toOption
- dstUri <- c.get[String]("dstUri").toOption
- pred <- c.get[String]("predicate").toOption
- cnt <- c.get[Int]("count").toOption
- } yield RawFilePair(srcUri, dstUri, pred, cnt)
- }.toVector
- }
-
- // ── Graph construction ─────────────────────────────────────────────
-
- private def computeGraph(
- files: Vector[FileVertex],
- rawPairs: Vector[RawFilePair]
- ): WeightedFileGraph = {
- if (files.isEmpty) return WeightedFileGraph.empty
-
- // Index files by their absolute source_uri path
- val byUri: Map[String, FileVertex] = files.map(v => v.path -> v).toMap
- val byId: Map[NodeId, FileVertex] = files.map(v => v.id -> v).toMap
- val pathSegmentsById: Map[NodeId, Array[String]] =
- files.map(v => v.id -> directorySegments(v.path)).toMap
-
- // Accumulate coupling counts per canonical file-id pair.
- val pairAcc =
- scala.collection.mutable.Map[(NodeId, NodeId), scala.collection.mutable.Map[String, Int]]()
-
- for (pair <- rawPairs) {
- val sv = byUri.get(pair.srcId) // srcId is source_uri here
- val dv = byUri.get(pair.dstId)
- (sv, dv) match {
- case (Some(s), Some(d)) if s.id != d.id =>
- val key = canonicalPair(s.id, d.id)
- val m = pairAcc.getOrElseUpdate(key, scala.collection.mutable.Map())
- m(pair.predicate) = m.getOrElse(pair.predicate, 0) + pair.count
- case _ =>
- }
- }
-
- // Build adjacency with composite weights
- val adjMut =
- scala.collection.mutable.Map[NodeId, scala.collection.mutable.Map[NodeId, Double]]()
- val predicatePairs =
- scala.collection.mutable.Map[(NodeId, NodeId), Map[String, Int]]()
- var edgeCount = 0
-
- for (((srcId, dstId), predicateCounts) <- pairAcc.iterator) {
- val sv = byId(srcId)
- val dv = byId(dstId)
- val srcSegments = pathSegmentsById(srcId)
- val dstSegments = pathSegmentsById(dstId)
- val sameDir = isSameDirectory(srcSegments, dstSegments)
-
- val callCount = predicateCounts.getOrElse("CALLS", 0)
- val importCount = predicateCounts.getOrElse("IMPORTS", 0)
- val typeCount = predicateCounts.getOrElse("EXTENDS", 0) +
- predicateCounts.getOrElse("IMPLEMENTS", 0)
- val pairSignals =
- if (sameDir) predicateCounts.toMap.updated("PATH", predicateCounts.getOrElse("PATH", 0) + 1)
- else predicateCounts.toMap
-
- val sCall = Alpha * math.log1p(callCount)
- val sImport = Beta * math.log1p(importCount)
- val sType = Gamma * math.log1p(typeCount)
- val sPath = Epsilon * pathProximity(srcSegments, dstSegments, sameDir)
-
- val w = sCall + sImport + sType + sPath
- if (w > 0.0) {
- adjMut.getOrElseUpdate(sv.id, scala.collection.mutable.Map())(dv.id) = w
- adjMut.getOrElseUpdate(dv.id, scala.collection.mutable.Map())(sv.id) = w
- predicatePairs((srcId, dstId)) = pairSignals
- edgeCount += 1
- }
- }
-
- // Path-proximity pass: add structural edges for same-directory file pairs
- // that have no existing coupling edge. This ensures isolated files (no
- // CALLS/IMPORTS/EXTENDS edges) can still be clustered by directory.
- val filesByDir =
- files.groupBy(v => pathSegmentsById(v.id).mkString("/")).toVector.sortBy(_._1)
- for ((_, dirFiles) <- filesByDir if dirFiles.size >= PathProximityMinFiles &&
- dirFiles.size <= PathProximityMaxFiles &&
- edgeCount < MaxEdges) {
- val ordered = dirFiles.sortBy(_.path)
- var i = 0
- while (i < ordered.length - 1 && edgeCount < MaxEdges) {
- var j = i + 1
- while (j < ordered.length && edgeCount < MaxEdges) {
- val sv = ordered(i)
- val dv = ordered(j)
- if (!adjMut.get(sv.id).exists(_.contains(dv.id))) {
- adjMut.getOrElseUpdate(sv.id, scala.collection.mutable.Map())(dv.id) = Epsilon
- adjMut.getOrElseUpdate(dv.id, scala.collection.mutable.Map())(sv.id) = Epsilon
- predicatePairs(canonicalPair(sv.id, dv.id)) = Map("PATH" -> 1)
- edgeCount += 1
- }
- j += 1
- }
- i += 1
- }
- }
-
- val adj = adjMut.map { case (k, m) => k -> m.toMap }.toMap
- val degrees = adj.map { case (k, m) => k -> m.values.sum }
- val totalWeight = degrees.values.sum / 2.0
-
- WeightedFileGraph(files, adj, degrees.toMap, totalWeight, predicatePairs.toMap)
- }
-
- private def canonicalPair(a: NodeId, b: NodeId): (NodeId, NodeId) =
- if (a.value.compareTo(b.value) <= 0) (a, b) else (b, a)
-
- private def directorySegments(path: String): Array[String] =
- path.split("[/\\\\]").dropRight(1)
-
- private def isSameDirectory(partsA: Array[String], partsB: Array[String]): Boolean = {
- if (partsA.length != partsB.length) return false
- var i = 0
- while (i < partsA.length) {
- if (partsA(i) != partsB(i)) return false
- i += 1
- }
- true
- }
-
- /** Path distance heuristic: deeper common prefix → higher proximity. */
- private def pathProximity(partsA: Array[String], partsB: Array[String], sameDir: Boolean): Double = {
- if (sameDir) return 1.0
-
- val maxLen = math.min(partsA.length, partsB.length)
- var common = 0
- while (common < maxLen && partsA(common) == partsB(common)) {
- common += 1
- }
- val dist = (partsA.length - common) + (partsB.length - common)
- 1.0 / (1.0 + dist)
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/map/MapGraphBuilderFast.scala b/memory-layer/src/main/scala/ix/memory/map/MapGraphBuilderFast.scala
deleted file mode 100644
index 8db07619..00000000
--- a/memory-layer/src/main/scala/ix/memory/map/MapGraphBuilderFast.scala
+++ /dev/null
@@ -1,158 +0,0 @@
-package ix.memory.map
-
-import cats.effect.IO
-import org.typelevel.log4cats.slf4j.Slf4jLogger
-
-import ix.memory.db.ArangoClient
-import ix.memory.model.NodeId
-
-/**
- * Fast graph builder for large workloads.
- *
- * Builds a WeightedFileGraph using only cheap file-level signals:
- * 1. File-to-file IMPORTS edges (direct query, no uri_map or symbol traversal)
- * 2. Directory chain structure (O(n) per directory, not O(n²) pairwise)
- *
- * The resulting graph is fully compatible with the downstream pipeline
- * (cross-cut detection, penalty, Louvain clustering, region creation, persistence).
- */
-class MapGraphBuilderFast(client: ArangoClient) {
-
- private val logger = Slf4jLogger.getLoggerFromName[IO]("ix.map.fast")
-
- // Weights aligned with full mode for comparable downstream behavior
- private val ImportWeight = 0.7 // matches full mode β
- private val DirectoryWeight = 0.3 // matches full mode ε
-
- def buildGraph(files: Vector[FileVertex]): IO[WeightedFileGraph] =
- for {
- _ <- logger.info("Using fast map graph construction (reduced coupling model)")
- fileImports <- fetchFileImports()
- graph = computeGraph(files, fileImports)
- _ <- logger.debug(
- s"Fast graph: ${files.size} files, " +
- s"${fileImports.size} file-level import edges, " +
- s"adj entries=${graph.adjMatrix.size}"
- )
- } yield graph
-
- // ── Cheap AQL query ────────────────────────────────────────────────
-
- /**
- * Fetches file-to-file IMPORTS edges directly.
- *
- * This is cheap because:
- * - Filters to IMPORTS only (no CALLS/EXTENDS/IMPLEMENTS)
- * - Joins against file nodes only (indexed logical_id lookup)
- * - Does NOT build a uri_map or scan all symbols
- * - Result set is small (file-to-file edges are a fraction of all edges)
- */
- private def fetchFileImports(): IO[Vector[RawFilePair]] =
- client.query(
- """FOR e IN edges
- | FILTER e.predicate == "IMPORTS"
- | AND e.deleted_rev == null
- | LET srcFile = FIRST(
- | FOR n IN nodes
- | FILTER n.logical_id == e.src
- | AND n.kind == "file"
- | AND n.deleted_rev == null
- | AND n.provenance.source_uri != null
- | RETURN n.provenance.source_uri
- | )
- | LET dstFile = FIRST(
- | FOR n IN nodes
- | FILTER n.logical_id == e.dst
- | AND n.kind == "file"
- | AND n.deleted_rev == null
- | AND n.provenance.source_uri != null
- | RETURN n.provenance.source_uri
- | )
- | FILTER srcFile != null AND dstFile != null AND srcFile != dstFile
- | COLLECT srcUri = srcFile, dstUri = dstFile
- | WITH COUNT INTO cnt
- | RETURN {srcUri, dstUri, predicate: "IMPORTS", count: cnt}""".stripMargin,
- Map.empty
- ).map { rows =>
- rows.flatMap { json =>
- val c = json.hcursor
- for {
- srcUri <- c.get[String]("srcUri").toOption
- dstUri <- c.get[String]("dstUri").toOption
- pred <- c.get[String]("predicate").toOption
- cnt <- c.get[Int]("count").toOption
- } yield RawFilePair(srcUri, dstUri, pred, cnt)
- }.toVector
- }
-
- // ── Graph construction ─────────────────────────────────────────────
-
- private def computeGraph(
- files: Vector[FileVertex],
- fileImports: Vector[RawFilePair]
- ): WeightedFileGraph = {
- if (files.isEmpty) return WeightedFileGraph.empty
-
- val byUri: Map[String, FileVertex] = files.map(v => v.path -> v).toMap
-
- val adjMut =
- scala.collection.mutable.Map[NodeId, scala.collection.mutable.Map[NodeId, Double]]()
-
- def addEdge(a: NodeId, b: NodeId, w: Double): Unit = {
- adjMut.getOrElseUpdate(a, scala.collection.mutable.Map())(b) =
- adjMut.getOrElseUpdate(a, scala.collection.mutable.Map()).getOrElse(b, 0.0) + w
- adjMut.getOrElseUpdate(b, scala.collection.mutable.Map())(a) =
- adjMut.getOrElseUpdate(b, scala.collection.mutable.Map()).getOrElse(a, 0.0) + w
- }
-
- // Signal 1: File-level import edges
- for (pair <- fileImports) {
- val sv = byUri.get(pair.srcId)
- val dv = byUri.get(pair.dstId)
- (sv, dv) match {
- case (Some(s), Some(d)) if s.id != d.id =>
- addEdge(s.id, d.id, ImportWeight * math.log1p(pair.count))
- case _ =>
- }
- }
-
- // Signal 2: Directory chain (O(n) per directory, not O(n²) pairwise)
- // For each directory, sort files by path and connect consecutive pairs.
- // This creates a linear chain that gives Louvain enough signal to group
- // co-located files without quadratic blowup.
- val filesByDir = files.groupBy { v =>
- val parts = v.path.split("[/\\\\]")
- if (parts.length > 1) parts.dropRight(1).mkString("/") else "."
- }
- for ((_, dirFiles) <- filesByDir if dirFiles.size >= 2) {
- val sorted = dirFiles.sortBy(_.path)
- for (i <- 0 until sorted.size - 1) {
- val a = sorted(i)
- val b = sorted(i + 1)
- // Boost weight for files already connected by imports
- val existing = adjMut.get(a.id).flatMap(_.get(b.id)).getOrElse(0.0)
- if (existing > 0.0) {
- // Directory co-location boost on existing edge
- addEdge(a.id, b.id, DirectoryWeight)
- } else {
- // New structural edge for unconnected neighbors
- addEdge(a.id, b.id, DirectoryWeight * pathProximity(a.path, b.path))
- }
- }
- }
-
- val adj = adjMut.map { case (k, m) => k -> m.toMap }.toMap
- val degrees = adj.map { case (k, m) => k -> m.values.sum }
- val totalWeight = degrees.values.sum / 2.0
-
- WeightedFileGraph(files, adj, degrees.toMap, totalWeight, Map.empty)
- }
-
- private def pathProximity(a: String, b: String): Double = {
- val partsA = a.split("[/\\\\]").dropRight(1)
- val partsB = b.split("[/\\\\]").dropRight(1)
- val common = partsA.zip(partsB).takeWhile { case (x, y) => x == y }.length
- val dist = (partsA.length - common) + (partsB.length - common)
- 1.0 / (1.0 + dist)
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/map/MapPreflight.scala b/memory-layer/src/main/scala/ix/memory/map/MapPreflight.scala
deleted file mode 100644
index 4872ffe1..00000000
--- a/memory-layer/src/main/scala/ix/memory/map/MapPreflight.scala
+++ /dev/null
@@ -1,144 +0,0 @@
-package ix.memory.map
-
-import cats.effect.IO
-import org.typelevel.log4cats.slf4j.Slf4jLogger
-
-/**
- * Preflight check between cheap file discovery and the expensive graph stages.
- *
- * Estimates workload cost, probes local capacity, classifies risk, and
- * selects an execution mode — all before any AQL coupling queries run.
- */
-class MapPreflight {
-
- private val logger = Slf4jLogger.getLoggerFromName[IO]("ix.map.preflight")
-
- def evaluate(files: Vector[FileVertex]): IO[MapPreflightResult] =
- for {
- start <- IO(System.currentTimeMillis())
- cost = estimateCost(files)
- capacity <- probeCapacity()
- risk = classifyRisk(cost, capacity)
- mode = selectMode(risk, capacity)
- warnings = buildWarnings(cost, capacity, risk, mode)
- end <- IO(System.currentTimeMillis())
- result = MapPreflightResult(cost, capacity, risk, mode, warnings, end - start)
- _ <- logger.debug(
- s"Preflight: F=${cost.fileCount} D=${cost.directoryCount} " +
- s"D2=${cost.directoryQuadratic} risk=${risk.label} mode=${mode.label} " +
- s"duration=${result.durationMs}ms"
- )
- } yield result
-
- private def estimateCost(files: Vector[FileVertex]): CostEstimate = {
- val fileCount = files.size
- val byDir = files.groupBy { v =>
- val parts = v.path.split("[/\\\\]")
- if (parts.length > 1) parts.dropRight(1).mkString("/") else "."
- }
- val directoryCount = byDir.size
- val directoryQuadratic = byDir.values.map { dirFiles =>
- val n = dirFiles.size.toLong
- n * n
- }.sum
-
- CostEstimate(
- fileCount = fileCount,
- directoryCount = directoryCount,
- directoryQuadratic = directoryQuadratic,
- symbolEstimate = fileCount.toLong * 12,
- edgeEstimate = fileCount.toLong * 8
- )
- }
-
- private def probeCapacity(): IO[LocalCapacity] = IO {
- val rt = Runtime.getRuntime
- val cpuCores = rt.availableProcessors()
- val heapMaxBytes = rt.maxMemory()
- val heapFreeBytes = rt.freeMemory() + (heapMaxBytes - rt.totalMemory())
-
- // Try cgroup v2 first, then v1, for container memory limit
- val containerMemory = readCgroupMemory()
-
- LocalCapacity(
- cpuCores = cpuCores,
- heapMaxBytes = heapMaxBytes,
- heapFreeBytes = heapFreeBytes,
- containerMemory = containerMemory,
- diskFreeBytes = None
- )
- }
-
- private def readCgroupMemory(): Option[Long] = {
- // cgroup v2
- val v2 = tryReadLong("/sys/fs/cgroup/memory.max")
- if (v2.isDefined) return v2
-
- // cgroup v1
- tryReadLong("/sys/fs/cgroup/memory/memory.limit_in_bytes")
- }
-
- private def tryReadLong(path: String): Option[Long] =
- try {
- val content = scala.io.Source.fromFile(path).mkString.trim
- if (content == "max") None else Some(content.toLong)
- } catch {
- case _: Exception => None
- }
-
- private val LowThreshold = 500
- private val MediumThreshold = 2000
- private val HighThreshold = 5000
- private val LowMemoryBytes = 512L * 1024 * 1024 // 512 MB
-
- private def classifyRisk(cost: CostEstimate, capacity: LocalCapacity): RiskTier = {
- val lowMem = capacity.containerMemory.exists(_ < LowMemoryBytes)
- val f = cost.fileCount
-
- // Lower thresholds when running in a constrained container
- val (medT, highT, extremeT) =
- if (lowMem) (300, 1200, 3000)
- else (LowThreshold, MediumThreshold, HighThreshold)
-
- if (f < medT) RiskTier.Low
- else if (f < highT) RiskTier.Medium
- else if (f < extremeT) RiskTier.High
- else RiskTier.Extreme
- }
-
- private def selectMode(risk: RiskTier, capacity: LocalCapacity): MapExecutionMode =
- risk match {
- case RiskTier.Extreme => MapExecutionMode.FastLocal
- case RiskTier.High if capacity.heapFreeBytes < 256L * 1024 * 1024 =>
- MapExecutionMode.FastLocal
- case _ => MapExecutionMode.FullLocal
- }
-
- private def buildWarnings(
- cost: CostEstimate,
- capacity: LocalCapacity,
- risk: RiskTier,
- mode: MapExecutionMode
- ): Vector[String] = {
- val buf = Vector.newBuilder[String]
-
- if (risk == RiskTier.High || risk == RiskTier.Extreme)
- buf += s"Large workload detected: ${cost.fileCount} files, ${cost.directoryCount} directories"
-
- if (cost.directoryQuadratic > 50000)
- buf += s"Path-proximity cost is high (D²=${cost.directoryQuadratic})"
-
- capacity.containerMemory.foreach { mem =>
- if (mem < LowMemoryBytes)
- buf += s"Container memory is low (${mem / (1024 * 1024)}MB)"
- }
-
- if (capacity.heapFreeBytes < 128L * 1024 * 1024)
- buf += s"JVM heap is low (${capacity.heapFreeBytes / (1024 * 1024)}MB free)"
-
- if (mode == MapExecutionMode.FastLocal)
- buf += "Full local map is not recommended for this workload"
-
- buf.result()
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/map/MapService.scala b/memory-layer/src/main/scala/ix/memory/map/MapService.scala
deleted file mode 100644
index 05325b69..00000000
--- a/memory-layer/src/main/scala/ix/memory/map/MapService.scala
+++ /dev/null
@@ -1,1112 +0,0 @@
-package ix.memory.map
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.syntax.parallel._
-import com.fasterxml.jackson.databind.ObjectMapper
-import io.circe.Json
-import io.circe.syntax._
-import org.typelevel.log4cats.slf4j.Slf4jLogger
-
-import ix.memory.db.{ArangoClient, GraphQueryApi, GraphWriteApi}
-import ix.memory.model._
-
-/**
- * Full pipeline for ix map:
- * 1. Build weighted file graph
- * 2. Detect cross-cutting nodes and apply penalty
- * 3. Run multi-level Louvain clustering
- * 4. Compute region metrics (cohesion, coupling, boundary ratio)
- * 5. Infer labels
- * 6. Score confidence
- * 7. Persist Region nodes + IN_REGION edges to ArangoDB (top-down: system → subsystem → module → file)
- * 8. Return ArchitectureMap
- */
-class MapService(
- client: ArangoClient,
- queryApi: GraphQueryApi,
- writeApi: GraphWriteApi
-) {
- private val mapper = new ObjectMapper()
- private val logger = Slf4jLogger.getLoggerFromName[IO]("ix.map")
- private val CrosscutThreshold = 0.10
-
- // Cross-cut penalty base multiplier (reduces edge weights involving crosscut files)
- private val CrosscutLambdaBase = 0.5
- // High-degree threshold: files in the top N% by degree are crosscut candidates
- private val CrosscutDegreeTop = 0.10
- // Path tokens that suggest cross-cutting utilities
- private val CrosscutTokens = Set(
- "util", "utils", "helper", "helpers", "common", "shared", "base",
- "logging", "log", "logger", "config", "conf", "constants", "constant",
- "extensions", "ext"
- )
- private val SignalWeights = Map(
- "CALLS" -> 1.0,
- "IMPORTS" -> 0.5,
- "EXTENDS" -> 1.2,
- "IMPLEMENTS" -> 1.2,
- "PATH" -> 0.2
- )
-
- private val fullBuilder = new MapGraphBuilder(client)
- private val preflight = new MapPreflight()
- private final class RetainedRegion(val level: Int, val communityIndex: Int, val region: Region)
-
- // Label kinds by level — supports arbitrary depth.
- // Top level = "system", bottom level = "module", everything in between = "subsystem".
- private def labelKind(level: Int, maxLevel: Int): String =
- if (maxLevel <= 1) "system"
- else if (level == maxLevel) "system"
- else if (level == 1) "module"
- else "subsystem"
-
- def buildMap(forceRecompute: Boolean = false, forceFull: Boolean = false): IO[ArchitectureMap] =
- getOrBuildMap(forceRecompute, forceFull)
-
- def getOrBuildMap(forceRecompute: Boolean = false, forceFull: Boolean = false): IO[ArchitectureMap] =
- for {
- inputRev <- fullBuilder.currentInputRev()
- cached <- loadCachedMap(inputRev)
- archMap <- if (cached.nonEmpty && !forceRecompute) IO.pure(cached.get)
- else computeAndPersistMap(inputRev, cached, forceFull)
- } yield archMap
-
- def loadPersistedMapForCurrentGraph(): IO[Option[ArchitectureMap]] =
- for {
- inputRev <- fullBuilder.currentInputRev()
- cached <- loadCachedMap(inputRev)
- } yield cached
-
- def loadCachedMap(): IO[Option[ArchitectureMap]] =
- loadPersistedMapForCurrentGraph()
-
- def resolveRegion(map: ArchitectureMap, target: String): Vector[RegionMatch] = {
- val query = target.trim
- val normalizedQuery = normalizeLabelQuery(query)
- if (query.isEmpty || normalizedQuery.isEmpty) Vector.empty
- else {
- map.regions.flatMap { region =>
- matchQuality(region.label, query).map { quality =>
- RegionMatch(region, quality, normalizeLabelQuery(region.label))
- }
- }.sortBy { m =>
- (
- m.matchQuality,
- levelPreference(m.region),
- -m.region.memberFiles.size,
- -m.region.confidence,
- m.normalizedLabel,
- m.region.id.value.toString
- )
- }
- }
- }
-
- def childrenOf(map: ArchitectureMap, region: Region): Vector[Region] = {
- val regionById = map.regions.map(r => r.id -> r).toMap
- val directChildren = region.childRegionIds.toVector
- .flatMap(regionById.get)
- .filter(_.level < region.level)
-
- val children =
- if (directChildren.nonEmpty) directChildren
- else map.regions.filter(_.parentId.contains(region.id))
-
- sortBySignificance(children.distinct)
- }
-
- def parentOf(map: ArchitectureMap, region: Region): Option[Region] = {
- val regionById = map.regions.map(r => r.id -> r).toMap
- region.parentId.flatMap(regionById.get)
- }
-
- def subtreeOf(map: ArchitectureMap, region: Region): Vector[Region] = {
- def loop(current: Region): Vector[Region] = {
- val directChildren = childrenOf(map, current)
- current +: directChildren.flatMap(loop)
- }
- loop(region)
- }
-
- def scopedView(map: ArchitectureMap, region: Region): ScopedSubsystemView = {
- val scopeRegions =
- if (region.labelKind == "module") Vector(region)
- else subtreeOf(map, region)
-
- val summary = ScopedHealthSummary(
- wellDefined = scopeRegions.count(_.confidence >= 0.75),
- moderate = scopeRegions.count(r => r.confidence >= 0.50 && r.confidence < 0.75),
- fuzzy = scopeRegions.count(_.confidence < 0.50),
- crossCutting = scopeRegions.count(isCrossCutting)
- )
-
- ScopedSubsystemView(
- target = region,
- parent = parentOf(map, region),
- children = if (region.labelKind == "module") Vector.empty else childrenOf(map, region),
- subtree = scopeRegions,
- summary = summary
- )
- }
-
- def isCrossCutting(region: Region): Boolean =
- region.crosscutScore > CrosscutThreshold
-
- // ── Cross-cutting detection ─────────────────────────────────────────
-
- private def detectCrosscut(graph: WeightedFileGraph): Map[NodeId, Double] = {
- if (graph.degrees.isEmpty) return Map.empty
- val maxDeg = graph.degrees.values.max.max(1e-10)
- val threshold = highDegreeThreshold(graph)
-
- graph.vertices.map { v =>
- val deg = graph.degrees.getOrElse(v.id, 0.0)
- val nameLower = v.path.toLowerCase
- val pathTokens = nameLower.split("[/\\\\._-]").toSet
-
- val degreeSignal = if (deg >= threshold) (deg - threshold) / (maxDeg - threshold + 1e-10) else 0.0
- val lexicalSignal = if (pathTokens.intersect(CrosscutTokens).nonEmpty) 0.5 else 0.0
-
- val score = (0.7 * degreeSignal + 0.3 * lexicalSignal).min(1.0)
- v.id -> score
- }.toMap
- }
-
- private def highDegreeThreshold(graph: WeightedFileGraph): Double = {
- val sortedDegrees = graph.degrees.values.toVector.sorted
- if (sortedDegrees.isEmpty) 0.0
- else {
- val cutoffIndex = math.ceil(sortedDegrees.size * (1.0 - CrosscutDegreeTop)).toInt - 1
- val clamped = cutoffIndex.max(0).min(sortedDegrees.size - 1)
- sortedDegrees(clamped)
- }
- }
-
- private def adaptiveCrosscutLambda(fileCount: Int): Double =
- if (fileCount <= 500) CrosscutLambdaBase // 0.5
- else if (fileCount <= 2000) CrosscutLambdaBase * 0.6 // 0.3
- else if (fileCount <= 5000) CrosscutLambdaBase * 0.4 // 0.2
- else CrosscutLambdaBase * 0.3 // 0.15
-
- private def applyPenalty(
- graph: WeightedFileGraph,
- scores: Map[NodeId, Double]
- ): WeightedFileGraph = {
- val lambda = adaptiveCrosscutLambda(graph.vertices.size)
- val newAdj = graph.adjMatrix.map { case (src, neighbors) =>
- val sCross = scores.getOrElse(src, 0.0)
- val adjusted = neighbors.map { case (dst, w) =>
- val dCross = scores.getOrElse(dst, 0.0)
- val penalty = (sCross + dCross) / 2.0 * lambda
- dst -> w * (1.0 - penalty)
- }
- src -> adjusted
- }
- val newDeg = newAdj.map { case (k, m) => k -> m.values.sum }
- val newTotal = newDeg.values.sum / 2.0
- graph.copy(adjMatrix = newAdj, degrees = newDeg.toMap, totalWeight = newTotal)
- }
-
- // ── Region construction ─────────────────────────────────────────────
-
- private def buildRegions(
- graph: WeightedFileGraph,
- levels: Vector[LouvainClustering.LevelPartition],
- crosscutScores: Map[NodeId, Double],
- rev: Rev
- ): Vector[Region] = {
- if (levels.isEmpty) return Vector.empty
-
- val fileVertexById = graph.vertices.map(v => v.id -> v).toMap
- val maxLevel = levels.size
-
- // Compute common absolute-path prefix to strip for label inference
- val allPaths = graph.vertices.map(_.path)
- val commonPfxLen = commonPathPrefixLength(allPaths)
- val relativePaths = allPaths.map(path => relativizePath(path, commonPfxLen))
- val globalDirTokenCounts = directoryTokenCounts(relativePaths)
- val signalAdjacency = buildSignalAdjacency(graph)
-
- // Build regions for each level, finest first.
- // Apply minCommunitySize at every level — singleton/doubleton regions are noise.
- val minSize = if (maxLevel >= 3) 3 else 2
- val retainedRegions = levels.zipWithIndex.flatMap { case (partition, levelIdx) =>
- val level = levelIdx + 1
- partition.communities.zipWithIndex.collect {
- case (memberFiles, communityIndex) if memberFiles.size >= minSize =>
- new RetainedRegion(
- level = level,
- communityIndex = communityIndex,
- region = buildOneRegion(
- memberFiles, level, labelKind(level, maxLevel),
- graph, fileVertexById, crosscutScores, signalAdjacency, rev, commonPfxLen, globalDirTokenCounts
- )
- )
- }
- }
- val retainedByLevel = retainedRegions.groupBy(_.level)
- .view
- .mapValues(_.map(retained => retained.communityIndex -> retained.region).toMap)
- .toMap
-
- // Wire parent/child relationships
- val withParents = retainedRegions.map { retained =>
- val parentOpt = levels.lift(retained.level).flatMap { coarseLevel =>
- val candidateCommunities = retained.region.memberFiles.iterator
- .flatMap(fileId => coarseLevel.assignment.get(fileId))
- .toSet
- if (candidateCommunities.size != 1) None
- else {
- retainedByLevel.get(retained.level + 1)
- .flatMap(_.get(candidateCommunities.head))
- .filter(_.memberFiles.size > retained.region.memberFiles.size)
- }
- }
- retained.region.copy(parentId = parentOpt.map(_.id))
- }
-
- // Fill childRegionIds
- val childrenByParent: Map[NodeId, Set[NodeId]] =
- withParents.flatMap(r => r.parentId.map(pid => pid -> r.id))
- .groupBy(_._1)
- .map { case (k, vs) => k -> vs.map(_._2).toSet }
-
- val withChildren = withParents.map { r =>
- r.copy(childRegionIds = childrenByParent.getOrElse(r.id, Set.empty))
- }
- val dedupedHierarchy = collapseDuplicateHierarchyLevels(withChildren)
- val withSingletonFallbacks = appendSingletonRegionsForUncoveredFiles(
- dedupedHierarchy,
- graph,
- fileVertexById,
- crosscutScores,
- signalAdjacency,
- rev,
- commonPfxLen,
- globalDirTokenCounts
- )
-
- // Disambiguate duplicate labels within each level.
- // For each group of same-label regions, find a path segment unique to each.
- val labelGroups = withSingletonFallbacks.groupBy(r => (r.level, r.label))
- val needsDisambig = labelGroups.filter(_._2.size > 1)
-
- if (needsDisambig.isEmpty) withSingletonFallbacks
- else withSingletonFallbacks.map { r =>
- needsDisambig.get((r.level, r.label)) match {
- case Some(group) =>
- val myPaths = r.memberFiles.flatMap(id => fileVertexById.get(id).map(_.path)).toSet
- val otherPaths = group.filterNot(_.id == r.id)
- .flatMap(_.memberFiles.flatMap(id => fileVertexById.get(id).map(_.path))).toSet
-
- // Only use DIRECTORY segments (not filenames) for disambiguation
- def dirSegs(paths: Set[String]): Set[String] =
- paths.flatMap(p => p.split("[/\\\\]").dropRight(1).filterNot(_.isEmpty).map(_.toLowerCase))
-
- val mySegs = dirSegs(myPaths)
- val otherSegs = dirSegs(otherPaths)
- val exclusive = (mySegs -- otherSegs).filterNot(LabelNoiseTokens.contains)
-
- // Prefer shorter tokens (more likely to be a meaningful module name).
- // Fallback: rank by size — largest keeps the clean label, smaller ones get "(2)", "(3)", etc.
- val suffix = exclusive.toVector.sortBy(s => (s.length, s)).headOption
- .map(s => s" / ${s.capitalize}")
- .getOrElse {
- val rank = group.sortBy(-_.memberFiles.size).indexWhere(_.id == r.id) + 1
- if (rank <= 1) "" else s" ($rank)"
- }
- r.copy(label = r.label + suffix)
- case None => r
- }
- }
- }
-
- private[map] def appendSingletonRegionsForUncoveredFiles(
- regions: Vector[Region],
- graph: WeightedFileGraph,
- fileVertexById: Map[NodeId, FileVertex],
- crosscutScores: Map[NodeId, Double],
- signalAdjacency: Map[NodeId, Vector[(NodeId, Map[String, Int])]],
- rev: Rev,
- commonPfxLen: Int = 0,
- globalDirTokenCounts: Map[String, Int] = Map.empty
- ): Vector[Region] = {
- val coveredFileIds = regions.iterator.flatMap(_.memberFiles.iterator).toSet
- val uncoveredFileIds = graph.vertices.iterator.map(_.id).filterNot(coveredFileIds.contains).toVector
- if (uncoveredFileIds.isEmpty) return regions
-
- val singletonRegions = uncoveredFileIds.map { fileId =>
- buildOneRegion(
- memberFiles = Set(fileId),
- level = 1,
- kind = "module",
- graph = graph,
- fileVertexById = fileVertexById,
- crosscutScores = crosscutScores,
- signalAdjacency = signalAdjacency,
- rev = rev,
- commonPfxLen = commonPfxLen,
- globalDirTokenCounts = globalDirTokenCounts
- )
- }
-
- regions ++ singletonRegions
- }
-
- private[map] def collapseDuplicateHierarchyLevels(regions: Vector[Region]): Vector[Region] = {
- val keepById = regions.groupBy(_.id).view.mapValues { duplicates =>
- duplicates.maxBy(region => (region.level, region.confidence, region.label.length))
- }.toMap
-
- regions.filter(region => keepById.get(region.id).contains(region))
- }
-
- private val LabelNoiseTokens = Set(
- "main", "scala", "java", "src", "test", "tests", "__tests__", "kotlin",
- "python", "js", "ts", "tsx", "jsx", "com", "org", "io", "net",
- "ix", "memory", "index", "mod", "lib", "pkg", "spec", "dist",
- "app", "application", "impl", "implementation", "internal", "external",
- "ix-memory", "memory-layer", "ix-cli", "core-ingestion", "ix-plugin",
- "projects", "claude"
- )
- private val WeakLabelTokens = Set(
- "api", "apis", "service", "services", "controller", "controllers",
- "repository", "repositories", "handler", "handlers", "module", "modules",
- "common", "shared", "core", "base", "internal", "external", "model",
- "models", "entity", "entities", "dto", "dtos", "types", "errors",
- "interface", "interfaces"
- )
-
- private def buildOneRegion(
- memberFiles: Set[NodeId],
- level: Int,
- kind: String,
- graph: WeightedFileGraph,
- fileVertexById: Map[NodeId, FileVertex],
- crosscutScores: Map[NodeId, Double],
- signalAdjacency: Map[NodeId, Vector[(NodeId, Map[String, Int])]],
- rev: Rev,
- commonPfxLen: Int = 0,
- globalDirTokenCounts: Map[String, Int] = Map.empty
- ): Region = {
- // Deterministic ID: stable across re-runs with same file set
- val idKey = "map:region:" + memberFiles.map(_.value.toString).toList.sorted.mkString(",")
- val nodeId = NodeId(UUID.nameUUIDFromBytes(idKey.getBytes("UTF-8")))
-
- // Edge weight metrics
- var internalWeight = 0.0
- var externalWeight = 0.0
- var boundaryNodes = Set.empty[NodeId]
-
- for (fileId <- memberFiles) {
- for ((neighbor, w) <- graph.adjMatrix.getOrElse(fileId, Map.empty)) {
- if (memberFiles.contains(neighbor)) {
- internalWeight += w
- } else {
- externalWeight += w
- boundaryNodes += fileId
- }
- }
- }
- // internalWeight is double-counted (symmetric adj), halve it
- internalWeight /= 2.0
-
- val n = memberFiles.size.toDouble.max(1.0)
- val cohesion = if (n > 1) internalWeight / (n * (n - 1) / 2.0) else 0.0
- // Cap at 100 to avoid floating-point overflow for fully-isolated clusters
- val boundaryRatio = if (externalWeight < 1e-6) math.min(internalWeight / 1e-6, 100.0)
- else internalWeight / externalWeight
-
- // Cross-cut score: average of member scores
- val crosscutScore =
- memberFiles.toList.map(f => crosscutScores.getOrElse(f, 0.0)).sum / n
-
- val label = inferLabel(memberFiles, fileVertexById, commonPfxLen, globalDirTokenCounts)
- val confidence = computeConfidence(cohesion, boundaryRatio, memberFiles.size, crosscutScore)
- val dominantSignals = computeDominantSignals(memberFiles, signalAdjacency)
-
- Region(
- id = nodeId,
- label = label,
- labelKind = kind,
- level = level,
- memberFiles = memberFiles,
- childRegionIds = Set.empty,
- parentId = None,
- cohesion = cohesion,
- externalCoupling = externalWeight,
- boundaryRatio = boundaryRatio,
- confidence = confidence,
- crosscutScore = crosscutScore,
- dominantSignals = dominantSignals,
- interfaceNodeCount = boundaryNodes.size,
- mapRev = rev.value
- )
- }
-
- // ── Label inference ─────────────────────────────────────────────────
-
- /**
- * Infer a human-readable label from file paths in the community.
- *
- * Strategy:
- * 1. Extract all path segments and identifier tokens from filenames.
- * 2. Score each token by frequency within cluster (TF) ×
- * specificity (higher if not a common Java/Scala package name).
- * 3. Return top token, Title-cased.
- * Fallback: use the most common non-trivial directory name.
- */
- private def inferLabel(
- memberFiles: Set[NodeId],
- fileVertexById: Map[NodeId, FileVertex],
- commonPfxLen: Int = 0,
- globalDirTokenCounts: Map[String, Int] = Map.empty
- ): String = {
- val rawPaths = memberFiles.flatMap(f => fileVertexById.get(f).map(_.path)).toVector
- if (rawPaths.isEmpty) return "Unknown"
- val paths = rawPaths.map(path => relativizePath(path, commonPfxLen))
-
- val noiseTokens = LabelNoiseTokens
-
- val suffixLabel = longestCommonSuffix(paths.map(path => directorySegments(path).toVector))
- .iterator
- .map(_.toLowerCase)
- .find(token => token.nonEmpty && !noiseTokens.contains(token))
-
- suffixLabel match {
- case Some(token) if !WeakLabelTokens.contains(token) =>
- humanizeLabel(token)
- case _ =>
- bestDiscriminativeDirectoryToken(paths, globalDirTokenCounts)
- .orElse(bestFilenameToken(paths))
- .map(humanizeLabel)
- .getOrElse("Region")
- }
- }
-
- /** Returns the number of leading path segments shared by all paths (the common abs prefix). */
- private def commonPathPrefixLength(paths: Vector[String]): Int = {
- if (paths.size <= 1) return 0
- val split = paths.map(_.split("[/\\\\]").toVector)
- val minLen = split.map(_.length).min
- (0 until minLen).takeWhile(i => split.map(_(i)).distinct.size == 1).length
- }
-
- /** Returns the longest common suffix of all path segment vectors, innermost first. */
- private def longestCommonSuffix(segLists: Vector[Vector[String]]): Vector[String] = {
- if (segLists.isEmpty || segLists.exists(_.isEmpty)) return Vector.empty
- val reversed = segLists.map(_.reverse)
- val maxLen = reversed.map(_.length).min
- (0 until maxLen).toVector
- .takeWhile(i => reversed.map(_(i)).distinct.size == 1)
- .map(i => reversed.head(i))
- }
-
- private def splitCamel(s: String): Array[String] =
- s.split("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|[_\\-.]")
-
- private def relativizePath(path: String, commonPfxLen: Int): String = {
- val segs = path.split("[/\\\\]")
- if (commonPfxLen > 0 && commonPfxLen < segs.length) segs.drop(commonPfxLen).mkString("/")
- else path
- }
-
- private def directorySegments(path: String): Vector[String] =
- path.split("[/\\\\]").dropRight(1).toVector
-
- private def directoryTokenCounts(paths: Vector[String]): Map[String, Int] =
- paths.flatMap(directorySegments)
- .map(_.toLowerCase)
- .filter(token => token.nonEmpty && !LabelNoiseTokens.contains(token))
- .groupBy(identity)
- .map { case (token, entries) => token -> entries.size }
-
- private def bestDiscriminativeDirectoryToken(
- paths: Vector[String],
- globalDirTokenCounts: Map[String, Int]
- ): Option[String] = {
- val clusterCounts = directoryTokenCounts(paths)
- clusterCounts.toVector
- .map { case (token, intraCount) =>
- val globalCount = globalDirTokenCounts.getOrElse(token, intraCount).max(intraCount)
- val score = intraCount.toDouble * (intraCount.toDouble / globalCount.toDouble)
- (token, score, intraCount, globalCount)
- }
- .sortBy { case (token, score, intraCount, globalCount) =>
- (-score, globalCount, -intraCount, token)
- }
- .headOption
- .map(_._1)
- }
-
- private def bestFilenameToken(paths: Vector[String]): Option[String] = {
- val fileTokens = paths.flatMap { p =>
- val name = p.split("[/\\\\]").lastOption.getOrElse("")
- val noExt = name.replaceAll("\\.[^.]+$", "")
- splitCamel(noExt)
- .map(_.toLowerCase)
- .filter(token => token.length > 3 && !LabelNoiseTokens.contains(token))
- }
-
- fileTokens.groupBy(identity)
- .toVector
- .sortBy { case (token, entries) => (-entries.size, token) }
- .headOption
- .map(_._1)
- }
-
- private def humanizeLabel(token: String): String =
- token.split("[-_\\s]+").filter(_.nonEmpty).map(_.capitalize).mkString(" ")
-
- private def normalizeWhitespace(input: String): String =
- input.trim.toLowerCase.replaceAll("\\s+", " ")
-
- private def normalizeLabelQuery(input: String): String =
- normalizeWhitespace(input.replaceAll("[/_-]+", " "))
-
- private def matchQuality(label: String, query: String): Option[Int] = {
- val trimmedLabel = label.trim
- val trimmedQuery = query.trim
- val whitespaceLabel = normalizeWhitespace(trimmedLabel)
- val whitespaceQuery = normalizeWhitespace(trimmedQuery)
- val normalizedLabel = normalizeLabelQuery(trimmedLabel)
- val normalizedQuery = normalizeLabelQuery(trimmedQuery)
-
- if (trimmedLabel.equalsIgnoreCase(trimmedQuery)) Some(0)
- else if (whitespaceLabel == whitespaceQuery) Some(1)
- else if (normalizedLabel == normalizedQuery) Some(2)
- else if (normalizedLabel.startsWith(normalizedQuery)) Some(3)
- else if (normalizedLabel.contains(normalizedQuery)) Some(4)
- else None
- }
-
- private def levelPreference(region: Region): Int =
- region.labelKind match {
- case "subsystem" => 0
- case "module" => 1
- case "system" => 2
- case _ => 3
- }
-
- private def sortBySignificance(regions: Seq[Region]): Vector[Region] =
- regions.toVector.sortBy(r => (-r.memberFiles.size, -r.confidence, r.label.toLowerCase, r.id.value.toString))
-
- private def buildSignalAdjacency(
- graph: WeightedFileGraph
- ): Map[NodeId, Vector[(NodeId, Map[String, Int])]] = {
- val adjacency = scala.collection.mutable.Map.empty[NodeId, Vector[(NodeId, Map[String, Int])]]
- for (((src, dst), predicateCounts) <- graph.predicatePairs) {
- adjacency.update(src, adjacency.getOrElse(src, Vector.empty) :+ (dst -> predicateCounts))
- adjacency.update(dst, adjacency.getOrElse(dst, Vector.empty) :+ (src -> predicateCounts))
- }
- adjacency.toMap
- }
-
- private def computeDominantSignals(
- memberFiles: Set[NodeId],
- signalAdjacency: Map[NodeId, Vector[(NodeId, Map[String, Int])]]
- ): List[String] = {
- val signalScores = scala.collection.mutable.Map(
- "calls" -> 0.0,
- "imports" -> 0.0,
- "type" -> 0.0,
- "path" -> 0.0
- ).withDefaultValue(0.0)
-
- for {
- src <- memberFiles.iterator
- (dst, predicateCounts) <- signalAdjacency.getOrElse(src, Vector.empty)
- if memberFiles.contains(dst)
- if src.value.compareTo(dst.value) < 0
- (predicate, count) <- predicateCounts
- if count > 0
- } {
- predicate match {
- case "CALLS" =>
- signalScores("calls") += SignalWeights("CALLS") * math.log1p(count.toDouble)
- case "IMPORTS" =>
- signalScores("imports") += SignalWeights("IMPORTS") * math.log1p(count.toDouble)
- case "EXTENDS" | "IMPLEMENTS" =>
- signalScores("type") += SignalWeights(predicate) * math.log1p(count.toDouble)
- case "PATH" =>
- signalScores("path") += SignalWeights("PATH") * count.toDouble
- case _ =>
- }
- }
-
- val ranked = signalScores.toVector
- .filter(_._2 > 0.0)
- .sortBy { case (name, score) => (-score, name) }
- .map(_._1)
- .take(3)
- .toList
-
- if (ranked.nonEmpty) ranked else List("path")
- }
-
- private def canonicalRegionPair(a: NodeId, b: NodeId): (NodeId, NodeId) =
- if (a.value.toString <= b.value.toString) (a, b) else (b, a)
-
- private def signalCategory(predicate: String): String =
- predicate match {
- case "CALLS" => "calls"
- case "IMPORTS" => "imports"
- case "EXTENDS" | "IMPLEMENTS" => "type"
- case "PATH" => "path"
- case other => other.toLowerCase
- }
-
- // ── Architecture edge computation ──────────────────────────────────
-
- private def computeArchitectureEdges(
- regions: Vector[Region],
- graph: WeightedFileGraph
- ): Vector[ArchitectureEdge] = {
- if (regions.isEmpty || graph.predicatePairs.isEmpty) return Vector.empty
-
- def buildEdge(
- idSeed: String,
- src: NodeId,
- dst: NodeId,
- level: Int,
- predicateCounts: Map[String, Int]
- ): Option[ArchitectureEdge] = {
- val signalScores = predicateCounts.toVector
- .map { case (predicate, count) =>
- signalCategory(predicate) -> (SignalWeights.getOrElse(predicate, 0.0) * math.log1p(count.toDouble))
- }
- .groupBy(_._1)
- .view
- .mapValues(_.map(_._2).sum)
- .toMap
-
- val weight = signalScores.values.sum
- if (weight <= 0.0) None
- else {
- val dominantSignal = signalScores.toVector
- .sortBy { case (signal, score) => (-score, signal) }
- .headOption
- .map(_._1)
- .getOrElse("path")
-
- Some(ArchitectureEdge(
- id = UUID.nameUUIDFromBytes(idSeed.getBytes("UTF-8")).toString,
- src = src,
- dst = dst,
- level = level,
- weight = weight,
- dominantSignal = dominantSignal,
- predicateCounts = predicateCounts
- ))
- }
- }
-
- // File-level edges (level 0)
- val fileEdges = graph.predicatePairs.toVector.flatMap { case ((src, dst), predicateCounts) =>
- buildEdge(
- idSeed = s"map:file-edge:${src.value}:${dst.value}",
- src = src,
- dst = dst,
- level = 0,
- predicateCounts = predicateCounts.filter(_._2 > 0)
- )
- }
-
- // Region-level edges: aggregate file pairs into region pairs per level
- val fileToRegionByLevel: Map[Int, Map[NodeId, Region]] = regions.groupBy(_.level).view.mapValues { levelRegions =>
- levelRegions.toVector.flatMap(region =>
- region.memberFiles.toVector.map(fileId => fileId -> region)
- ).toMap
- }.toMap
-
- val aggregated = scala.collection.mutable.Map.empty[(Int, NodeId, NodeId), scala.collection.mutable.Map[String, Int]]
-
- for {
- ((srcFile, dstFile), predicateCounts) <- graph.predicatePairs
- (level, byFile) <- fileToRegionByLevel
- srcRegion <- byFile.get(srcFile)
- dstRegion <- byFile.get(dstFile)
- if srcRegion.id != dstRegion.id
- } {
- val (srcRegionId, dstRegionId) = canonicalRegionPair(srcRegion.id, dstRegion.id)
- val bucket = aggregated.getOrElseUpdate(
- (level, srcRegionId, dstRegionId),
- scala.collection.mutable.Map.empty[String, Int]
- )
- predicateCounts.foreach { case (predicate, count) =>
- bucket.update(predicate, bucket.getOrElse(predicate, 0) + count)
- }
- }
-
- val regionEdges = aggregated.toVector.flatMap { case ((level, src, dst), predicateCountsMut) =>
- val predicateCounts = predicateCountsMut.toMap.filter(_._2 > 0)
- buildEdge(
- idSeed = s"map:region-edge:$level:${src.value}:${dst.value}",
- src = src,
- dst = dst,
- level = level,
- predicateCounts = predicateCounts
- )
- }
-
- (regionEdges ++ fileEdges).sortBy(edge => (edge.level, edge.src.value.toString, edge.dst.value.toString))
- }
-
- // ── Confidence scoring ──────────────────────────────────────────────
-
- private def computeConfidence(
- cohesion: Double,
- boundaryRatio: Double,
- size: Int,
- crosscutScore: Double
- ): Double = {
- val brScore = math.tanh(boundaryRatio / 2.5)
- val cohScore = cohesion.min(1.0)
- val sizeBonus = math.tanh(size / 8.0)
- val crossPenalty = crosscutScore * 0.4
-
- ((0.4 * brScore + 0.3 * cohScore + 0.3 * sizeBonus) - crossPenalty).max(0.0).min(1.0)
- }
-
- // ── Persistence ─────────────────────────────────────────────────────
-
- private def computeAndPersistMap(
- inputRev: Rev,
- cached: Option[ArchitectureMap],
- forceFull: Boolean
- ): IO[ArchitectureMap] =
- for {
- files <- fullBuilder.discoverFiles()
- preflightEval <- preflight.evaluate(files)
- t0 <- IO(System.currentTimeMillis())
- rawGraph <- fullBuilder.buildGraph(files)
- t1 <- IO(System.currentTimeMillis())
- crosscutScores <- IO.blocking(detectCrosscut(rawGraph))
- graph <- IO.blocking(applyPenalty(rawGraph, crosscutScores))
- t2 <- IO(System.currentTimeMillis())
- levels <- IO.blocking(LouvainClustering.cluster(graph, maxLevels = 5, minCommunitySize = 3))
- t3 <- IO(System.currentTimeMillis())
- regions <- IO.blocking(buildRegions(graph, levels, crosscutScores, inputRev))
- edges <- IO.blocking(computeArchitectureEdges(regions, rawGraph))
- fresh = ArchitectureMap(
- regions = regions,
- edges = edges,
- fileCount = rawGraph.vertices.size,
- mapRev = inputRev.value,
- preflight = Some(preflightEval),
- outcome = MapOutcome.FullLocalCompleted
- )
- t4 <- IO(System.currentTimeMillis())
- _ <- persistMap(fresh, cached)
- t5 <- IO(System.currentTimeMillis())
- _ <- logger.info(
- s"map mode=${fresh.outcome.label} files=${files.size} build=${t1-t0}ms crosscut=${t2-t1}ms cluster=${t3-t2}ms regions=${t4-t3}ms persist=${t5-t4}ms total=${t5-t0}ms"
- )
- } yield fresh
-
- private def loadCachedMap(inputRev: Rev): IO[Option[ArchitectureMap]] =
- for {
- rows <- client.query(
- """FOR n IN nodes
- | FILTER n.kind == "region"
- | AND n.deleted_rev == null
- | AND n.attrs.map_generated == true
- | AND TO_NUMBER(n.attrs.map_rev) == @mapRev
- | SORT TO_NUMBER(n.attrs.level) DESC, n.name ASC
- | RETURN {
- | id: n.logical_id,
- | name: n.name,
- | attrs: n.attrs
- | }""".stripMargin,
- Map("mapRev" -> Long.box(inputRev.value).asInstanceOf[AnyRef])
- )
- decoded = rows.flatMap(decodePersistedRegion(_, inputRev)).toVector
- regions = rehydrateRelationships(decoded)
- fcAndGraph <- if (regions.isEmpty) IO.pure((0, None))
- else (
- fullBuilder.liveFileCount(),
- fullBuilder.discoverFiles().flatMap(fullBuilder.buildGraph).map(Some(_))
- ).parTupled
- (fileCount, rawGraphOpt) = fcAndGraph
- edges = rawGraphOpt.map(g => computeArchitectureEdges(regions, g)).getOrElse(Vector.empty)
- } yield {
- if (regions.isEmpty) None
- else Some(ArchitectureMap(regions, edges, fileCount, inputRev.value))
- }
-
- private def decodePersistedRegion(json: Json, inputRev: Rev): Option[Region] = {
- val c = json.hcursor
- val attrs = c.downField("attrs")
-
- for {
- idStr <- c.get[String]("id").toOption
- label <- c.get[String]("name").toOption
- id <- parseNodeId(idStr)
- level <- attrs.get[Int]("level").toOption
- labelKind <- attrs.get[String]("label_kind").toOption
- } yield {
- val memberFiles = attrs.get[Vector[String]]("member_files").getOrElse(Vector.empty).flatMap(parseNodeId).toSet
- val childIds = attrs.get[Vector[String]]("child_region_ids").getOrElse(Vector.empty).flatMap(parseNodeId).toSet
- val parentId = attrs.get[Option[String]]("parent_id").getOrElse(None).flatMap(parseNodeId)
-
- Region(
- id = id,
- label = label,
- labelKind = labelKind,
- level = level,
- memberFiles = memberFiles,
- childRegionIds = childIds,
- parentId = parentId,
- cohesion = attrs.get[Double]("cohesion").getOrElse(0.0),
- externalCoupling = attrs.get[Double]("external_coupling").getOrElse(0.0),
- boundaryRatio = attrs.get[Double]("boundary_ratio").getOrElse(0.0),
- confidence = attrs.get[Double]("confidence").getOrElse(0.0),
- crosscutScore = attrs.get[Double]("crosscut_score").getOrElse(0.0),
- dominantSignals = attrs.get[List[String]]("dominant_signals").getOrElse(Nil),
- interfaceNodeCount = attrs.get[Int]("interface_node_count").getOrElse(0),
- mapRev = attrs.get[Long]("map_rev").getOrElse(inputRev.value)
- )
- }
- }
-
- private def parseNodeId(id: String): Option[NodeId] =
- scala.util.Try(UUID.fromString(id)).toOption.map(NodeId(_))
-
- private def rehydrateRelationships(regions: Vector[Region]): Vector[Region] = {
- if (regions.isEmpty) return regions
-
- val parentByChild = regions.iterator
- .flatMap(region => region.childRegionIds.iterator.map(childId => childId -> region.id))
- .toMap
-
- val childrenByParent = regions
- .flatMap(region => region.parentId.map(parentId => parentId -> region.id))
- .groupBy(_._1)
- .view
- .mapValues(_.map(_._2).toSet)
- .toMap
-
- regions.map { region =>
- val parentId = region.parentId.orElse(parentByChild.get(region.id))
- val childIds =
- if (region.childRegionIds.nonEmpty) region.childRegionIds
- else childrenByParent.getOrElse(region.id, Set.empty)
- region.copy(parentId = parentId, childRegionIds = childIds)
- }
- }
-
- private def persistMap(fresh: ArchitectureMap, cached: Option[ArchitectureMap]): IO[Unit] = {
- if (cached.exists(existing => sameRegions(existing.regions, fresh.regions))) IO.unit
- else {
- for {
- oldLogicalIds <- loadOldRegionIds()
- _ <- submitMapPatch(oldLogicalIds, fresh.regions, Rev(fresh.mapRev))
- } yield ()
- }
- }
-
- private def sameRegions(left: Vector[Region], right: Vector[Region]): Boolean =
- left.sortBy(_.id.value.toString) == right.sortBy(_.id.value.toString)
-
- /** Fetch only the logical_ids of current live region nodes — cheaper than full GraphNode. */
- private def loadOldRegionIds(): IO[Vector[String]] =
- client.query(
- """FOR n IN nodes
- | FILTER n.kind == "region"
- | AND n.deleted_rev == null
- | LIMIT 5000
- | RETURN n.logical_id""".stripMargin,
- Map.empty
- ).map(_.flatMap(_.as[String].toOption).toVector)
-
- /**
- * Persist map regions using direct bulk operations instead of the per-op transaction path.
- *
- * This replaces the old commitPatch approach (which made N+1 DB round-trips inside
- * a transaction, causing timeouts on large repos) with five bulk operations:
- * 1. Batch AQL tombstone for old region nodes
- * 2. Batch AQL tombstone for old region edges
- * 3. bulkInsert for new region nodes
- * 4. bulkInsertEdges for IN_REGION edges
- * 5. Revision update
- */
- private def submitMapPatch(
- oldRegionIds: Vector[String],
- regions: Vector[Region],
- rev: Rev
- ): IO[Unit] = {
- if (oldRegionIds.isEmpty && regions.isEmpty) return IO.unit
-
- val now = Instant.now().toString
- val provMap = {
- val m = new java.util.HashMap[String, AnyRef]()
- m.put("source_uri", "ix:map")
- m.put("source_hash", null)
- m.put("extractor", "map-service")
- m.put("source_type", "inferred")
- m.put("observed_at", now)
- m
- }
-
- for {
- latestRev <- queryApi.getLatestRev
- newRev = latestRev.value + 1L
-
- // 1. Tombstone old region nodes
- _ <- if (oldRegionIds.isEmpty) IO.unit
- else {
- val ids = new java.util.ArrayList[String]()
- oldRegionIds.foreach(ids.add)
- client.execute(
- """FOR n IN nodes
- | FILTER n.logical_id IN @ids AND n.deleted_rev == null
- | UPDATE n WITH { deleted_rev: @rev, updated_at: @now } IN nodes""".stripMargin,
- Map(
- "ids" -> ids.asInstanceOf[AnyRef],
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "now" -> now.asInstanceOf[AnyRef]
- )
- )
- }
-
- // 2. Tombstone all active IN_REGION edges in one indexed query.
- // Replaces the old loadActiveRegionEdgeIds approach (which did a full
- // collection scan with a large src/dst IN clause — the main freeze point).
- _ <- client.execute(
- """FOR e IN edges
- | FILTER e.predicate == "IN_REGION"
- | AND e.deleted_rev == null
- | UPDATE e WITH { deleted_rev: @rev, updated_at: @now } IN edges""".stripMargin,
- Map(
- "rev" -> Long.box(newRev).asInstanceOf[AnyRef],
- "now" -> now.asInstanceOf[AnyRef]
- )
- )
-
- // 3. Bulk-insert new region nodes
- nodeDocs = regions.map(r => buildRegionNodeDoc(r, newRev, now, provMap))
- _ <- if (nodeDocs.isEmpty) IO.unit
- else client.bulkInsert("nodes", nodeDocs).void
-
- // 4. Bulk-insert IN_REGION edges
- edgeDocs = buildRegionEdgeDocs(regions, newRev, now, provMap)
- _ <- if (edgeDocs.isEmpty) IO.unit
- else client.bulkInsertEdges("edges", edgeDocs).void
-
- // 5. Update current revision
- _ <- client.execute(
- """UPSERT { _key: "current" }
- | INSERT { _key: "current", rev: @rev }
- | UPDATE { rev: @rev }
- | IN revisions""".stripMargin,
- Map("rev" -> Long.box(newRev).asInstanceOf[AnyRef])
- )
- } yield ()
- }
-
- private def buildRegionNodeDoc(
- r: Region,
- newRev: Long,
- now: String,
- provMap: java.util.Map[String, AnyRef]
- ): java.util.Map[String, AnyRef] = {
- val logicalId = r.id.value.toString
- val attrsJson = Json.obj(
- "label" -> r.label.asJson,
- "map_generated" -> Json.True,
- "level" -> r.level.asJson,
- "label_kind" -> r.labelKind.asJson,
- "file_count" -> r.memberFiles.size.asJson,
- "member_files" -> r.memberFiles.map(_.value.toString).asJson,
- "parent_id" -> r.parentId.map(_.value.toString).asJson,
- "child_region_ids" -> r.childRegionIds.toVector.map(_.value.toString).sorted.asJson,
- "cohesion" -> r.cohesion.asJson,
- "external_coupling" -> r.externalCoupling.asJson,
- "boundary_ratio" -> r.boundaryRatio.asJson,
- "confidence" -> r.confidence.asJson,
- "crosscut_score" -> r.crosscutScore.asJson,
- "dominant_signals" -> r.dominantSignals.asJson,
- "interface_node_count" -> r.interfaceNodeCount.asJson,
- "map_rev" -> r.mapRev.asJson
- ).noSpaces
- val doc = new java.util.HashMap[String, AnyRef]()
- doc.put("_key", s"${logicalId}_${newRev}")
- doc.put("logical_id", logicalId)
- doc.put("id", logicalId)
- doc.put("kind", "region")
- doc.put("name", r.label)
- doc.put("attrs", mapper.readValue(attrsJson, classOf[java.util.Map[String, AnyRef]]))
- doc.put("provenance", provMap)
- doc.put("created_rev", Long.box(newRev))
- doc.put("deleted_rev", null)
- doc.put("created_at", now)
- doc.put("updated_at", now)
- doc
- }
-
- private[map] def buildRegionEdgeDocs(
- regions: Vector[Region],
- newRev: Long,
- now: String,
- provMap: java.util.Map[String, AnyRef]
- ): Vector[java.util.Map[String, AnyRef]] = {
- val fileRegionById = regions
- .flatMap(region => region.memberFiles.toVector.map(_ -> region))
- .groupBy(_._1)
- .view
- .mapValues(_.map(_._2).minBy(region => (region.level, region.labelKind)))
- .toMap
-
- val fileEdges = fileRegionById.toVector.map { case (fileId, region) =>
- val edgeId = UUID.nameUUIDFromBytes(s"in_region:${region.id.value}:${fileId.value}".getBytes("UTF-8"))
- buildRegionEdgeDoc(
- edgeId.toString,
- s"nodes/${region.id.value}",
- s"nodes/${fileId.value}",
- region.id.value.toString,
- fileId.value.toString,
- newRev,
- now,
- provMap
- )
- }
- val regionEdges = regions.flatMap { r =>
- r.parentId.map { pid =>
- val edgeId = UUID.nameUUIDFromBytes(s"in_region:${pid.value}:${r.id.value}".getBytes("UTF-8"))
- buildRegionEdgeDoc(edgeId.toString, s"nodes/${pid.value}", s"nodes/${r.id.value}",
- pid.value.toString, r.id.value.toString, newRev, now, provMap)
- }
- }
- fileEdges ++ regionEdges
- }
-
- private def buildRegionEdgeDoc(
- key: String,
- from: String,
- to: String,
- src: String,
- dst: String,
- newRev: Long,
- now: String,
- provMap: java.util.Map[String, AnyRef]
- ): java.util.Map[String, AnyRef] = {
- val doc = new java.util.HashMap[String, AnyRef]()
- doc.put("_key", key)
- doc.put("_from", from)
- doc.put("_to", to)
- doc.put("id", key)
- doc.put("src", src)
- doc.put("dst", dst)
- doc.put("predicate", "IN_REGION")
- doc.put("attrs", new java.util.HashMap[String, AnyRef]())
- doc.put("created_rev", Long.box(newRev))
- doc.put("deleted_rev", null)
- doc.put("provenance", provMap)
- doc.put("created_at", now)
- doc.put("updated_at", now)
- doc
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/map/MapTypes.scala b/memory-layer/src/main/scala/ix/memory/map/MapTypes.scala
deleted file mode 100644
index 7cb4f287..00000000
--- a/memory-layer/src/main/scala/ix/memory/map/MapTypes.scala
+++ /dev/null
@@ -1,169 +0,0 @@
-package ix.memory.map
-
-import ix.memory.model.NodeId
-
-/** A file node with its UUID and path (used as label source). */
-final case class FileVertex(id: NodeId, path: String)
-
-/** Raw edge from ArangoDB before weight computation. */
-final case class RawFilePair(srcId: String, dstId: String, predicate: String, count: Int)
-
-/** Weighted edge between two file nodes (symmetric — both directions stored). */
-final case class FileCouplingEdge(src: NodeId, dst: NodeId, weight: Double, dominant: String)
-
-/**
- * Weighted, undirected file-level coupling graph.
- *
- * adjMatrix is symmetric: adjMatrix(a)(b) == adjMatrix(b)(a)
- * totalWeight is the sum of all weights counting each edge once.
- */
-final case class WeightedFileGraph(
- vertices: Vector[FileVertex],
- adjMatrix: Map[NodeId, Map[NodeId, Double]],
- degrees: Map[NodeId, Double],
- totalWeight: Double,
- predicatePairs: Map[(NodeId, NodeId), Map[String, Int]]
-)
-
-object WeightedFileGraph {
- val empty: WeightedFileGraph =
- WeightedFileGraph(Vector.empty, Map.empty, Map.empty, 0.0, Map.empty)
-}
-
-/**
- * A single inferred architectural region at one level of the hierarchy.
- *
- * `memberFiles` always refers to the original file NodeIds.
- * `parentId` is set for regions at level >= 2 (their containing region at level+1).
- */
-final case class Region(
- id: NodeId,
- label: String,
- labelKind: String, // "module" | "subsystem" | "system"
- level: Int, // 1 = finest, 2 = mid, 3 = coarse
- memberFiles: Set[NodeId],
- childRegionIds: Set[NodeId], // direct children at level-1 (empty for level 1)
- parentId: Option[NodeId],
- cohesion: Double,
- externalCoupling: Double,
- boundaryRatio: Double,
- confidence: Double,
- crosscutScore: Double,
- dominantSignals: List[String],
- interfaceNodeCount: Int,
- mapRev: Long
-)
-
-final case class ArchitectureEdge(
- id: String,
- src: NodeId,
- dst: NodeId,
- level: Int,
- weight: Double,
- dominantSignal: String,
- predicateCounts: Map[String, Int]
-)
-
-// ── Preflight types ──────────────────────────────────────────────────
-
-sealed trait RiskTier { def label: String }
-object RiskTier {
- case object Low extends RiskTier { val label = "Low" }
- case object Medium extends RiskTier { val label = "Medium" }
- case object High extends RiskTier { val label = "High" }
- case object Extreme extends RiskTier { val label = "Extreme" }
-}
-
-sealed trait MapExecutionMode { def label: String }
-object MapExecutionMode {
- case object FullLocal extends MapExecutionMode { val label = "FullLocal" }
- case object FastLocal extends MapExecutionMode { val label = "FastLocal" }
-}
-
-final case class LocalCapacity(
- cpuCores: Int,
- heapMaxBytes: Long,
- heapFreeBytes: Long,
- containerMemory: Option[Long],
- diskFreeBytes: Option[Long]
-)
-
-final case class CostEstimate(
- fileCount: Int,
- directoryCount: Int,
- directoryQuadratic: Long,
- symbolEstimate: Long,
- edgeEstimate: Long
-)
-
-final case class MapPreflightResult(
- cost: CostEstimate,
- capacity: LocalCapacity,
- risk: RiskTier,
- mode: MapExecutionMode,
- warnings: Vector[String],
- durationMs: Long
-)
-
-// ── Outcome and guardrail types ──────────────────────────────────────
-
-sealed trait MapOutcome { def label: String }
-object MapOutcome {
- case object FullLocalCompleted extends MapOutcome { val label = "full_local_completed" }
- case object FastLocalCompleted extends MapOutcome { val label = "fast_local_completed" }
- case object LocalMapTooLarge extends MapOutcome { val label = "local_map_too_large" }
- case object LocalMapNotRecommended extends MapOutcome { val label = "local_map_not_recommended" }
-}
-
-final case class PersistenceEstimate(
- regionNodes: Int,
- fileEdges: Int,
- regionEdges: Int,
- deleteOps: Int,
- totalOps: Int
-)
-
-/**
- * Structured exception for map capacity failures.
- *
- * Carries the outcome category, a user-facing message, and a next-step
- * suggestion so the error handler can produce clean structured JSON
- * without relying on ad-hoc exception parsing.
- */
-class MapCapacityException(
- val outcome: MapOutcome,
- val userMessage: String,
- val next: String
-) extends RuntimeException(userMessage)
-
-/** The full multi-level architecture map returned by MapService. */
-final case class ArchitectureMap(
- regions: Vector[Region],
- edges: Vector[ArchitectureEdge] = Vector.empty,
- fileCount: Int,
- mapRev: Long,
- preflight: Option[MapPreflightResult] = None,
- outcome: MapOutcome = MapOutcome.FullLocalCompleted,
- persistenceEstimate: Option[PersistenceEstimate] = None
-)
-
-final case class RegionMatch(
- region: Region,
- matchQuality: Int,
- normalizedLabel: String
-)
-
-final case class ScopedHealthSummary(
- wellDefined: Int,
- moderate: Int,
- fuzzy: Int,
- crossCutting: Int
-)
-
-final case class ScopedSubsystemView(
- target: Region,
- parent: Option[Region],
- children: Vector[Region],
- subtree: Vector[Region],
- summary: ScopedHealthSummary
-)
diff --git a/memory-layer/src/main/scala/ix/memory/model/Claim.scala b/memory-layer/src/main/scala/ix/memory/model/Claim.scala
deleted file mode 100644
index c3523dfa..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/Claim.scala
+++ /dev/null
@@ -1,47 +0,0 @@
-package ix.memory.model
-
-import io.circe.{Decoder, Encoder, Json}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-
-sealed trait ClaimStatus
-object ClaimStatus {
- case object Active extends ClaimStatus
- case object Stale extends ClaimStatus
- case object Retracted extends ClaimStatus
-
- private val nameMap: Map[String, ClaimStatus] = Map(
- "Active" -> Active,
- "Stale" -> Stale,
- "Retracted" -> Retracted
- )
-
- implicit val encoder: Encoder[ClaimStatus] = Encoder[String].contramap {
- case Active => "Active"
- case Stale => "Stale"
- case Retracted => "Retracted"
- }
-
- implicit val decoder: Decoder[ClaimStatus] = Decoder[String].emap { s =>
- nameMap.get(s).toRight(s"Unknown ClaimStatus: $s")
- }
-}
-
-final case class Claim(
- id: ClaimId,
- entityId: NodeId,
- statement: String,
- value: Json,
- confidence: Option[Double],
- status: ClaimStatus,
- provenance: Provenance,
- createdRev: Rev,
- deletedRev: Option[Rev],
- /** Identifies the inference pass that produced this claim (e.g. "smell_v1", "subsystem_v1").
- * Absent for observed facts extracted from source code. Missing legacy claims are treated as "legacy". */
- inferenceVersion: Option[String] = None
-)
-
-object Claim {
- implicit val encoder: Encoder[Claim] = deriveEncoder[Claim]
- implicit val decoder: Decoder[Claim] = deriveDecoder[Claim]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/ConfidenceBreakdown.scala b/memory-layer/src/main/scala/ix/memory/model/ConfidenceBreakdown.scala
deleted file mode 100644
index e3f9bf3c..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/ConfidenceBreakdown.scala
+++ /dev/null
@@ -1,88 +0,0 @@
-package ix.memory.model
-
-import io.circe.{Decoder, Encoder, Json}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-import io.circe.syntax._
-
-final case class Factor(value: Double, reason: String)
-
-object Factor {
- implicit val encoder: Encoder[Factor] = deriveEncoder[Factor]
- implicit val decoder: Decoder[Factor] = deriveDecoder[Factor]
-}
-
-final case class CompactConfidence(
- score: Double,
- authority: Option[Double],
- nonTrivialFactors: List[(String, Factor)]
-)
-
-object CompactConfidence {
-
- def isTrivial(f: Factor): Boolean = f.value >= 0.99 && f.value <= 1.01
-
- implicit val encoder: Encoder[CompactConfidence] = Encoder.instance { cc =>
- val base = List("score" -> Json.fromDoubleOrNull(cc.score))
-
- val auth = cc.authority.map(a => "authority" -> Json.fromDoubleOrNull(a)).toList
-
- val factors = if (cc.nonTrivialFactors.nonEmpty) {
- val obj = Json.obj(cc.nonTrivialFactors.map { case (name, factor) =>
- name -> factor.asJson
- }: _*)
- List("factors" -> obj)
- } else Nil
-
- Json.obj((base ++ auth ++ factors): _*)
- }
-
- implicit val decoder: Decoder[CompactConfidence] = Decoder.instance { c =>
- for {
- score <- c.downField("score").as[Double]
- authority <- c.downField("authority").as[Option[Double]]
- } yield CompactConfidence(score, authority, Nil)
- }
-}
-
-final case class ConfidenceBreakdown(
- baseAuthority: Factor,
- verification: Factor,
- recency: Factor,
- corroboration: Factor,
- conflictPenalty: Factor,
- intentAlignment: Factor
-) {
-
- /** Confidence score = product of all factor values, clamped to [0, 1]. */
- def score: Double = {
- val raw = baseAuthority.value *
- verification.value *
- recency.value *
- corroboration.value *
- conflictPenalty.value *
- intentAlignment.value
- math.max(0.0, math.min(1.0, raw))
- }
-
- /** Collapse trivial (≈1.0) multipliers into a compact representation. */
- def toCompact: CompactConfidence = {
- val candidates = List(
- "verification" -> verification,
- "recency" -> recency,
- "corroboration" -> corroboration,
- "conflictPenalty" -> conflictPenalty,
- "intentAlignment" -> intentAlignment
- )
- val nonTrivial = candidates.filterNot { case (_, f) => CompactConfidence.isTrivial(f) }
- CompactConfidence(
- score = score,
- authority = Some(baseAuthority.value),
- nonTrivialFactors = nonTrivial
- )
- }
-}
-
-object ConfidenceBreakdown {
- implicit val encoder: Encoder[ConfidenceBreakdown] = deriveEncoder[ConfidenceBreakdown]
- implicit val decoder: Decoder[ConfidenceBreakdown] = deriveDecoder[ConfidenceBreakdown]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/ConflictSet.scala b/memory-layer/src/main/scala/ix/memory/model/ConflictSet.scala
deleted file mode 100644
index 859fadb6..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/ConflictSet.scala
+++ /dev/null
@@ -1,41 +0,0 @@
-package ix.memory.model
-
-import io.circe.{Decoder, Encoder}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-
-sealed trait ConflictStatus
-object ConflictStatus {
- case object Open extends ConflictStatus
- case object Resolved extends ConflictStatus
- case object Dismissed extends ConflictStatus
-
- private val nameMap: Map[String, ConflictStatus] = Map(
- "Open" -> Open,
- "Resolved" -> Resolved,
- "Dismissed" -> Dismissed
- )
-
- implicit val encoder: Encoder[ConflictStatus] = Encoder[String].contramap {
- case Open => "Open"
- case Resolved => "Resolved"
- case Dismissed => "Dismissed"
- }
-
- implicit val decoder: Decoder[ConflictStatus] = Decoder[String].emap { s =>
- nameMap.get(s).toRight(s"Unknown ConflictStatus: $s")
- }
-}
-
-final case class ConflictSet(
- id: ConflictId,
- reason: String,
- status: ConflictStatus,
- candidateClaims: Vector[ClaimId],
- winnerClaimId: Option[ClaimId],
- createdRev: Rev
-)
-
-object ConflictSet {
- implicit val encoder: Encoder[ConflictSet] = deriveEncoder[ConflictSet]
- implicit val decoder: Decoder[ConflictSet] = deriveDecoder[ConflictSet]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/Edge.scala b/memory-layer/src/main/scala/ix/memory/model/Edge.scala
deleted file mode 100644
index f7bf5cd3..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/Edge.scala
+++ /dev/null
@@ -1,26 +0,0 @@
-package ix.memory.model
-
-import io.circe.{Decoder, Encoder, Json}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-
-final case class EdgePredicate(value: String) extends AnyVal
-object EdgePredicate {
- implicit val encoder: Encoder[EdgePredicate] = Encoder[String].contramap(_.value)
- implicit val decoder: Decoder[EdgePredicate] = Decoder[String].map(EdgePredicate(_))
-}
-
-final case class GraphEdge(
- id: EdgeId,
- src: NodeId,
- dst: NodeId,
- predicate: EdgePredicate,
- attrs: Json,
- provenance: Provenance,
- createdRev: Rev,
- deletedRev: Option[Rev]
-)
-
-object GraphEdge {
- implicit val encoder: Encoder[GraphEdge] = deriveEncoder[GraphEdge]
- implicit val decoder: Decoder[GraphEdge] = deriveDecoder[GraphEdge]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/GraphPatch.scala b/memory-layer/src/main/scala/ix/memory/model/GraphPatch.scala
deleted file mode 100644
index 7c0b7881..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/GraphPatch.scala
+++ /dev/null
@@ -1,98 +0,0 @@
-package ix.memory.model
-
-import java.time.Instant
-
-import io.circe.{Decoder, DecodingFailure, Encoder, HCursor, Json}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-import io.circe.syntax._
-
-sealed trait PatchOp
-object PatchOp {
- final case class UpsertNode(id: NodeId, kind: NodeKind, name: String, attrs: Map[String, Json]) extends PatchOp
- final case class UpsertEdge(id: EdgeId, src: NodeId, dst: NodeId, predicate: EdgePredicate, attrs: Map[String, Json]) extends PatchOp
- final case class DeleteNode(id: NodeId) extends PatchOp
- final case class DeleteEdge(id: EdgeId) extends PatchOp
- final case class AssertClaim(
- entityId: NodeId,
- field: String,
- value: Json,
- confidence: Option[Double],
- /** Inference pass identifier. Absent for observed facts; required for inferred claims. */
- inferenceVersion: Option[String] = None
- ) extends PatchOp
- final case class RetractClaim(claimId: ClaimId) extends PatchOp
-
- implicit val encoder: Encoder[PatchOp] = Encoder.instance {
- case UpsertNode(id, kind, name, attrs) =>
- Json.obj("type" -> "UpsertNode".asJson, "id" -> id.asJson, "kind" -> kind.asJson, "name" -> name.asJson, "attrs" -> attrs.asJson)
- case UpsertEdge(id, src, dst, predicate, attrs) =>
- Json.obj("type" -> "UpsertEdge".asJson, "id" -> id.asJson, "src" -> src.asJson, "dst" -> dst.asJson, "predicate" -> predicate.asJson, "attrs" -> attrs.asJson)
- case DeleteNode(id) => Json.obj("type" -> "DeleteNode".asJson, "id" -> id.asJson)
- case DeleteEdge(id) => Json.obj("type" -> "DeleteEdge".asJson, "id" -> id.asJson)
- case AssertClaim(entityId, field, value, confidence, inferenceVersion) =>
- Json.obj("type" -> "AssertClaim".asJson, "entityId" -> entityId.asJson, "field" -> field.asJson, "value" -> value.asJson, "confidence" -> confidence.asJson, "inferenceVersion" -> inferenceVersion.asJson)
- case RetractClaim(claimId) => Json.obj("type" -> "RetractClaim".asJson, "claimId" -> claimId.asJson)
- }
-
- implicit val decoder: Decoder[PatchOp] = Decoder.instance { (c: HCursor) =>
- c.downField("type").as[String].flatMap {
- case "UpsertNode" =>
- for {
- id <- c.downField("id").as[NodeId]
- kind <- c.downField("kind").as[NodeKind]
- name <- c.downField("name").as[String]
- attrs <- c.downField("attrs").as[Map[String, Json]]
- } yield UpsertNode(id, kind, name, attrs)
- case "UpsertEdge" =>
- for {
- id <- c.downField("id").as[EdgeId]
- src <- c.downField("src").as[NodeId]
- dst <- c.downField("dst").as[NodeId]
- predicate <- c.downField("predicate").as[EdgePredicate]
- attrs <- c.downField("attrs").as[Map[String, Json]]
- } yield UpsertEdge(id, src, dst, predicate, attrs)
- case "DeleteNode" => c.downField("id").as[NodeId].map(DeleteNode)
- case "DeleteEdge" => c.downField("id").as[EdgeId].map(DeleteEdge)
- case "AssertClaim" =>
- for {
- entityId <- c.downField("entityId").as[NodeId]
- field <- c.downField("field").as[String]
- value <- c.downField("value").as[Json]
- confidence <- c.downField("confidence").as[Option[Double]]
- inferenceVersion <- c.downField("inferenceVersion").as[Option[String]]
- } yield AssertClaim(entityId, field, value, confidence, inferenceVersion)
- case "RetractClaim" => c.downField("claimId").as[ClaimId].map(RetractClaim)
- case other => Left(DecodingFailure(s"Unknown PatchOp type: $other", c.history))
- }
- }
-}
-
-final case class PatchSource(
- uri: String,
- sourceHash: Option[String],
- extractor: String,
- sourceType: SourceType
-)
-
-object PatchSource {
- implicit val encoder: Encoder[PatchSource] = deriveEncoder[PatchSource]
- implicit val decoder: Decoder[PatchSource] = deriveDecoder[PatchSource]
-}
-
-final case class GraphPatch(
- patchId: PatchId,
- actor: String,
- timestamp: Instant,
- source: PatchSource,
- baseRev: Rev,
- ops: Vector[PatchOp],
- replaces: Vector[PatchId],
- intent: Option[String]
-)
-
-object GraphPatch {
- import Provenance.{instantEncoder, instantDecoder}
-
- implicit val encoder: Encoder[GraphPatch] = deriveEncoder[GraphPatch]
- implicit val decoder: Decoder[GraphPatch] = deriveDecoder[GraphPatch]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/Identifiers.scala b/memory-layer/src/main/scala/ix/memory/model/Identifiers.scala
deleted file mode 100644
index 591d51c1..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/Identifiers.scala
+++ /dev/null
@@ -1,41 +0,0 @@
-package ix.memory.model
-
-import java.util.UUID
-
-import io.circe.{Decoder, Encoder}
-
-final case class NodeId(value: UUID) extends AnyVal
-object NodeId {
- implicit val encoder: Encoder[NodeId] = Encoder[UUID].contramap(_.value)
- implicit val decoder: Decoder[NodeId] = Decoder[UUID].map(NodeId(_))
-}
-
-final case class EdgeId(value: UUID) extends AnyVal
-object EdgeId {
- implicit val encoder: Encoder[EdgeId] = Encoder[UUID].contramap(_.value)
- implicit val decoder: Decoder[EdgeId] = Decoder[UUID].map(EdgeId(_))
-}
-
-final case class PatchId(value: UUID) extends AnyVal
-object PatchId {
- implicit val encoder: Encoder[PatchId] = Encoder[UUID].contramap(_.value)
- implicit val decoder: Decoder[PatchId] = Decoder[UUID].map(PatchId(_))
-}
-
-final case class ClaimId(value: UUID) extends AnyVal
-object ClaimId {
- implicit val encoder: Encoder[ClaimId] = Encoder[UUID].contramap(_.value)
- implicit val decoder: Decoder[ClaimId] = Decoder[UUID].map(ClaimId(_))
-}
-
-final case class ConflictId(value: UUID) extends AnyVal
-object ConflictId {
- implicit val encoder: Encoder[ConflictId] = Encoder[UUID].contramap(_.value)
- implicit val decoder: Decoder[ConflictId] = Decoder[UUID].map(ConflictId(_))
-}
-
-final case class Rev(value: Long) extends AnyVal
-object Rev {
- implicit val encoder: Encoder[Rev] = Encoder[Long].contramap(_.value)
- implicit val decoder: Decoder[Rev] = Decoder[Long].map(Rev(_))
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/Node.scala b/memory-layer/src/main/scala/ix/memory/model/Node.scala
deleted file mode 100644
index 48fdf031..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/Node.scala
+++ /dev/null
@@ -1,121 +0,0 @@
-package ix.memory.model
-
-import java.time.Instant
-
-import io.circe.{Decoder, Encoder, Json}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-
-sealed trait NodeKind
-object NodeKind {
- case object Module extends NodeKind
- case object File extends NodeKind
- case object Class extends NodeKind
- case object Function extends NodeKind
- case object Variable extends NodeKind
- case object Config extends NodeKind
- case object ConfigEntry extends NodeKind
- case object Service extends NodeKind
- case object Endpoint extends NodeKind
- case object Intent extends NodeKind
- case object Decision extends NodeKind
- case object Doc extends NodeKind
- case object Object extends NodeKind
- case object Trait extends NodeKind
- case object Interface extends NodeKind
- case object Method extends NodeKind
- case object Plan extends NodeKind
- case object Task extends NodeKind
- case object Bug extends NodeKind
- case object Goal extends NodeKind
- case object Region extends NodeKind
- case object Chunk extends NodeKind
- case object Macro extends NodeKind
- case object Table extends NodeKind
- case object View extends NodeKind
-
- private val nameMap: Map[String, NodeKind] = Map(
- "module" -> Module,
- "file" -> File,
- "class" -> Class,
- "function" -> Function,
- "variable" -> Variable,
- "config" -> Config,
- "config_entry" -> ConfigEntry,
- "service" -> Service,
- "endpoint" -> Endpoint,
- "intent" -> Intent,
- "decision" -> Decision,
- "doc" -> Doc,
- "object" -> Object,
- "trait" -> Trait,
- "interface" -> Interface,
- "method" -> Method,
- "plan" -> Plan,
- "task" -> Task,
- "bug" -> Bug,
- "goal" -> Goal,
- "region" -> Region,
- "chunk" -> Chunk,
- "macro" -> Macro,
- "table" -> Table,
- "view" -> View
- )
-
- implicit val encoder: Encoder[NodeKind] = Encoder[String].contramap {
- case Module => "module"
- case File => "file"
- case Class => "class"
- case Function => "function"
- case Variable => "variable"
- case Config => "config"
- case ConfigEntry => "config_entry"
- case Service => "service"
- case Endpoint => "endpoint"
- case Intent => "intent"
- case Decision => "decision"
- case Doc => "doc"
- case Object => "object"
- case Trait => "trait"
- case Interface => "interface"
- case Method => "method"
- case Plan => "plan"
- case Task => "task"
- case Bug => "bug"
- case Goal => "goal"
- case Region => "region"
- case Chunk => "chunk"
- case Macro => "macro"
- case Table => "table"
- case View => "view"
- }
-
- implicit val decoder: Decoder[NodeKind] = Decoder[String].emap { s =>
- nameMap.get(s).toRight(s"Unknown NodeKind: $s")
- }
-
- /** Pre-computed reverse lookup — avoids per-call Circe encoding in hot paths. */
- private val kindToString: Map[NodeKind, String] = nameMap.map(_.swap)
-
- /** Fast wire-format string without Circe encoding overhead. */
- def toWireString(nk: NodeKind): String =
- kindToString.getOrElse(nk, nk.toString)
-}
-
-final case class GraphNode(
- id: NodeId,
- kind: NodeKind,
- name: String,
- attrs: Json,
- provenance: Provenance,
- createdRev: Rev,
- deletedRev: Option[Rev],
- createdAt: Instant,
- updatedAt: Instant
-)
-
-object GraphNode {
- import Provenance.{instantEncoder, instantDecoder}
-
- implicit val encoder: Encoder[GraphNode] = deriveEncoder[GraphNode]
- implicit val decoder: Decoder[GraphNode] = deriveDecoder[GraphNode]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/Provenance.scala b/memory-layer/src/main/scala/ix/memory/model/Provenance.scala
deleted file mode 100644
index 60079cf5..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/Provenance.scala
+++ /dev/null
@@ -1,87 +0,0 @@
-package ix.memory.model
-
-import java.time.Instant
-
-import io.circe.{Decoder, Encoder}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-
-sealed trait SourceType
-object SourceType {
- case object Code extends SourceType
- case object Config extends SourceType
- case object Doc extends SourceType
- case object Test extends SourceType
- case object Schema extends SourceType
- case object Commit extends SourceType
- case object Comment extends SourceType
- case object Inferred extends SourceType
- case object Human extends SourceType
- case object Cli extends SourceType
- case object Decision extends SourceType
-
- /** Base authority weight for confidence scoring. */
- def baseAuthority(st: SourceType): Double = st match {
- case Code => 0.90
- case Config => 0.85
- case Doc => 0.70
- case Test => 0.95
- case Schema => 0.85
- case Commit => 0.80
- case Comment => 0.50
- case Inferred => 0.40
- case Human => 0.80
- case Cli => 0.75
- case Decision => 0.80
- }
-
- private val nameMap: Map[String, SourceType] = Map(
- "code" -> Code,
- "config" -> Config,
- "doc" -> Doc,
- "test" -> Test,
- "schema" -> Schema,
- "commit" -> Commit,
- "comment" -> Comment,
- "inferred" -> Inferred,
- "human" -> Human,
- "cli" -> Cli,
- "decision" -> Decision
- )
-
- implicit val encoder: Encoder[SourceType] = Encoder[String].contramap {
- case Code => "code"
- case Config => "config"
- case Doc => "doc"
- case Test => "test"
- case Schema => "schema"
- case Commit => "commit"
- case Comment => "comment"
- case Inferred => "inferred"
- case Human => "human"
- case Cli => "cli"
- case Decision => "decision"
- }
-
- implicit val decoder: Decoder[SourceType] = Decoder[String].emap { s =>
- nameMap.get(s).toRight(s"Unknown SourceType: $s")
- }
-}
-
-final case class Provenance(
- sourceUri: String,
- sourceHash: Option[String],
- extractor: String,
- sourceType: SourceType,
- observedAt: Instant
-)
-
-object Provenance {
- implicit val instantEncoder: Encoder[Instant] = Encoder[String].contramap(_.toString)
- implicit val instantDecoder: Decoder[Instant] = Decoder[String].emap { s =>
- try Right(Instant.parse(s))
- catch { case e: Exception => Left(s"Invalid instant: $s") }
- }
-
- implicit val encoder: Encoder[Provenance] = deriveEncoder[Provenance]
- implicit val decoder: Decoder[Provenance] = deriveDecoder[Provenance]
-}
diff --git a/memory-layer/src/main/scala/ix/memory/model/StructuredContext.scala b/memory-layer/src/main/scala/ix/memory/model/StructuredContext.scala
deleted file mode 100644
index 967a382f..00000000
--- a/memory-layer/src/main/scala/ix/memory/model/StructuredContext.scala
+++ /dev/null
@@ -1,195 +0,0 @@
-package ix.memory.model
-
-import io.circe.{Decoder, Encoder, Json}
-import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
-import io.circe.syntax._
-
-final case class CompactScoredClaim(
- entityId: NodeId,
- field: String,
- value: Json,
- score: Double,
- confidence: CompactConfidence,
- path: Option[String],
- lineRange: Option[(Int, Int)]
-)
-
-object CompactScoredClaim {
-
- /** Truncate string JSON values longer than 120 characters. */
- def truncateValue(v: Json): Json =
- v.asString match {
- case Some(s) if s.length > 120 => Json.fromString(s.take(120) + "...")
- case _ => v
- }
-
- implicit val encoder: Encoder[CompactScoredClaim] = Encoder.instance { csc =>
- val fields = List(
- "entityId" -> csc.entityId.asJson,
- "field" -> Json.fromString(csc.field),
- "value" -> truncateValue(csc.value),
- "score" -> Json.fromDoubleOrNull(csc.score),
- "confidence" -> csc.confidence.asJson
- ) ++
- csc.path.map(p => "path" -> Json.fromString(p)).toList ++
- csc.lineRange.map { case (s, e) => "lineRange" -> Json.arr(Json.fromInt(s), Json.fromInt(e)) }.toList
-
- Json.obj(fields: _*)
- }
-
- implicit val decoder: Decoder[CompactScoredClaim] = Decoder.instance { c =>
- for {
- entityId <- c.get[NodeId]("entityId")
- field <- c.get[String]("field")
- value <- c.get[Json]("value")
- score <- c.get[Double]("score")
- confidence <- c.get[CompactConfidence]("confidence")
- path <- c.get[Option[String]]("path")
- lineRange <- c.downField("lineRange").as[Option[List[Int]]].map(_.collect {
- case List(s, e) => (s, e)
- })
- } yield CompactScoredClaim(entityId, field, value, score, confidence, path, lineRange)
- }
-}
-
-final case class ScoredClaim(
- claim: Claim,
- confidence: ConfidenceBreakdown,
- relevance: Double = 1.0,
- finalScore: Double = 0.0
-) {
- def toCompact: CompactScoredClaim = CompactScoredClaim(
- entityId = claim.entityId,
- field = claim.statement,
- value = claim.value,
- score = finalScore,
- confidence = confidence.toCompact,
- path = Some(claim.provenance.sourceUri),
- lineRange = None
- )
-}
-
-object ScoredClaim {
- implicit val encoder: Encoder[ScoredClaim] = deriveEncoder[ScoredClaim]
- implicit val decoder: Decoder[ScoredClaim] = deriveDecoder[ScoredClaim]
-}
-
-final case class ConflictReport(
- id: ConflictId,
- claimA: ClaimId,
- claimB: ClaimId,
- reason: String,
- recommendation: String
-)
-
-object ConflictReport {
- implicit val encoder: Encoder[ConflictReport] = deriveEncoder[ConflictReport]
- implicit val decoder: Decoder[ConflictReport] = deriveDecoder[ConflictReport]
-}
-
-final case class DecisionReport(
- title: String,
- rationale: String,
- entityId: Option[NodeId],
- intentId: Option[NodeId],
- rev: Rev
-)
-
-object DecisionReport {
- implicit val encoder: Encoder[DecisionReport] = deriveEncoder[DecisionReport]
- implicit val decoder: Decoder[DecisionReport] = deriveDecoder[DecisionReport]
-}
-
-final case class IntentReport(
- id: NodeId,
- statement: String,
- status: String,
- confidence: Double,
- parentIntent: Option[String]
-)
-
-object IntentReport {
- implicit val encoder: Encoder[IntentReport] = deriveEncoder[IntentReport]
- implicit val decoder: Decoder[IntentReport] = deriveDecoder[IntentReport]
-}
-
-final case class ContextMetadata(
- query: String,
- seedEntities: List[NodeId],
- hopsExpanded: Int,
- asOfRev: Rev,
- depth: Option[String]
-)
-
-object ContextMetadata {
- implicit val encoder: Encoder[ContextMetadata] = deriveEncoder[ContextMetadata]
- implicit val decoder: Decoder[ContextMetadata] = deriveDecoder[ContextMetadata]
-}
-
-final case class NodeSummary(
- id: NodeId,
- kind: NodeKind,
- name: String,
- rev: Rev
-)
-
-object NodeSummary {
- implicit val encoder: Encoder[NodeSummary] = deriveEncoder[NodeSummary]
- implicit val decoder: Decoder[NodeSummary] = deriveDecoder[NodeSummary]
-}
-
-final case class EdgeSummary(
- id: EdgeId,
- src: NodeId,
- dst: NodeId,
- predicate: EdgePredicate,
- rev: Rev
-)
-
-object EdgeSummary {
- implicit val encoder: Encoder[EdgeSummary] = deriveEncoder[EdgeSummary]
- implicit val decoder: Decoder[EdgeSummary] = deriveDecoder[EdgeSummary]
-}
-
-final case class StructuredContext(
- claims: List[ScoredClaim],
- compactClaims: List[CompactScoredClaim] = Nil,
- conflicts: List[ConflictReport],
- decisions: List[DecisionReport],
- intents: List[IntentReport],
- nodes: List[GraphNode],
- edges: List[GraphEdge],
- nodeSummaries: List[NodeSummary] = Nil,
- edgeSummaries: List[EdgeSummary] = Nil,
- metadata: ContextMetadata
-)
-
-object StructuredContext {
- implicit val encoder: Encoder[StructuredContext] = deriveEncoder[StructuredContext]
-
- implicit val decoder: Decoder[StructuredContext] = Decoder.instance { c =>
- for {
- claims <- c.getOrElse[List[ScoredClaim]]("claims")(Nil)
- compactClaims <- c.getOrElse[List[CompactScoredClaim]]("compactClaims")(Nil)
- conflicts <- c.getOrElse[List[ConflictReport]]("conflicts")(Nil)
- decisions <- c.getOrElse[List[DecisionReport]]("decisions")(Nil)
- intents <- c.getOrElse[List[IntentReport]]("intents")(Nil)
- nodes <- c.getOrElse[List[GraphNode]]("nodes")(Nil)
- edges <- c.getOrElse[List[GraphEdge]]("edges")(Nil)
- nodeSummaries <- c.getOrElse[List[NodeSummary]]("nodeSummaries")(Nil)
- edgeSummaries <- c.getOrElse[List[EdgeSummary]]("edgeSummaries")(Nil)
- metadata <- c.get[ContextMetadata]("metadata")
- } yield StructuredContext(
- claims = claims,
- compactClaims = compactClaims,
- conflicts = conflicts,
- decisions = decisions,
- intents = intents,
- nodes = nodes,
- edges = edges,
- nodeSummaries = nodeSummaries,
- edgeSummaries = edgeSummaries,
- metadata = metadata
- )
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/savings/NaiveCostEstimator.scala b/memory-layer/src/main/scala/ix/memory/savings/NaiveCostEstimator.scala
deleted file mode 100644
index 8ae59f30..00000000
--- a/memory-layer/src/main/scala/ix/memory/savings/NaiveCostEstimator.scala
+++ /dev/null
@@ -1,92 +0,0 @@
-package ix.memory.savings
-
-import cats.effect.IO
-import io.circe.Json
-
-import ix.memory.db.{GraphQueryApi, ArangoClient}
-import ix.memory.model._
-
-/**
- * Estimates the "naive" token cost of getting equivalent information without Ix.
- *
- * Uses graph-derived file sizes for entity commands (overview, impact, callers, etc.)
- * and fixed multipliers for search/analysis commands.
- */
-class NaiveCostEstimator(queryApi: GraphQueryApi) {
-
- /** Fixed multipliers per command type, used when graph derivation isn't practical. */
- private val multipliers: Map[String, Int] = Map(
- "search" -> 6,
- "text" -> 6,
- "locate" -> 6,
- "inventory" -> 8,
- "rank" -> 8,
- "diff" -> 5,
- "history" -> 5,
- "smells" -> 10,
- "subsystems" -> 10,
- )
-
- private val defaultMultiplier = 4
-
- /**
- * Estimate naive token cost for a command.
- *
- * @param commandType the ix command name (e.g., "overview", "search")
- * @param responseChars the character count of the actual response
- * @param entityIds optional entity IDs resolved during the command
- * @return estimated naive token count
- */
- def estimate(
- commandType: String,
- responseChars: Long,
- entityIds: Seq[NodeId] = Seq.empty
- ): IO[Long] = {
- val responseTokens = math.max(1L, responseChars / 4)
-
- commandType match {
- case "overview" | "explain" =>
- estimateFromGraph(entityIds, depthFactor = 3).map(_.getOrElse(responseTokens * 5))
-
- case "impact" =>
- estimateFromGraph(entityIds, depthFactor = 4).map(_.getOrElse(responseTokens * 8))
-
- case "callers" | "callees" | "imported-by" | "imports" =>
- estimateFromGraph(entityIds, depthFactor = 2).map(_.getOrElse(responseTokens * 4))
-
- case "contains" =>
- estimateFromGraph(entityIds, depthFactor = 1).map(_.getOrElse(responseTokens * 3))
-
- case "depends" =>
- estimateFromGraph(entityIds, depthFactor = 3).map(_.getOrElse(responseTokens * 6))
-
- case cmd =>
- val mult = multipliers.getOrElse(cmd, defaultMultiplier)
- IO.pure(responseTokens * mult)
- }
- }
-
- /**
- * Estimate by summing actual file sizes of involved entities from the graph.
- * Returns None if no entities are available.
- */
- private def estimateFromGraph(entityIds: Seq[NodeId], depthFactor: Int): IO[Option[Long]] = {
- if (entityIds.isEmpty) return IO.pure(None)
-
- entityIds.toList.foldLeft(IO.pure(0L)) { (accIO, nodeId) =>
- for {
- acc <- accIO
- nodeOpt <- queryApi.getNode(nodeId)
- size: Long = nodeOpt.map { node =>
- val attrsC = node.attrs.hcursor
- attrsC.get[Long]("line_end").toOption.map(_ * 80L) // estimate chars from line count
- .orElse(attrsC.get[String]("source_uri").toOption.map(_ => 2000L)) // fallback: 2KB per file
- .getOrElse(1000L) // default estimate
- }.getOrElse(1000L)
- } yield acc + size
- }.map { totalChars =>
- val tokens = totalChars / 4 * depthFactor
- Some(math.max(tokens, 100L))
- }
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/savings/SavingsAccumulator.scala b/memory-layer/src/main/scala/ix/memory/savings/SavingsAccumulator.scala
deleted file mode 100644
index 93d7872c..00000000
--- a/memory-layer/src/main/scala/ix/memory/savings/SavingsAccumulator.scala
+++ /dev/null
@@ -1,169 +0,0 @@
-package ix.memory.savings
-
-import cats.effect.{IO, Ref}
-import io.circe.{Encoder, Json}
-import io.circe.syntax._
-
-import ix.memory.db.ArangoClient
-
-/** Tracks token savings per session (in-memory) and lifetime (ArangoDB). */
-class SavingsAccumulator(client: ArangoClient, session: Ref[IO, SavingsData]) {
-
- /** Record savings for a single command invocation. Fire-and-forget safe. */
- def record(commandType: String, actualTokens: Long, naiveEstimate: Long): IO[Unit] = {
- val saved = math.max(0L, naiveEstimate - actualTokens)
- val entry = CommandSavings(count = 1, saved = saved)
- for {
- _ <- session.update(_.add(commandType, actualTokens, naiveEstimate, saved))
- _ <- persistLifetime(commandType, actualTokens, naiveEstimate, saved)
- } yield ()
- }
-
- /** Get current savings data. Lifetime is floored to session values since
- * some AQL writes may fail silently under concurrent load. */
- def getSavings(detail: Boolean): IO[SavingsResponse] =
- for {
- sess <- session.get
- dbLife <- loadLifetime
- lifetime = dbLife.atLeast(sess)
- } yield SavingsResponse(
- session = if (detail) sess else sess.withoutBreakdown,
- lifetime = if (detail) lifetime else lifetime.withoutBreakdown
- )
-
- /** Reset lifetime totals. */
- def reset: IO[Unit] =
- for {
- _ <- session.set(SavingsData.empty)
- _ <- client.execute(
- """UPSERT { _key: "lifetime" }
- |INSERT { _key: "lifetime", totalSaved: 0, totalNaive: 0, totalActual: 0,
- | commandCount: 0, byCommandType: {}, lastUpdated: DATE_ISO8601(DATE_NOW()) }
- |UPDATE { totalSaved: 0, totalNaive: 0, totalActual: 0,
- | commandCount: 0, byCommandType: {}, lastUpdated: DATE_ISO8601(DATE_NOW()) }
- |IN savings""".stripMargin
- )
- } yield ()
-
- private def persistLifetime(
- commandType: String, actual: Long, naive: Long, saved: Long
- ): IO[Unit] =
- client.execute(
- """UPSERT { _key: "lifetime" }
- |INSERT { _key: "lifetime", totalSaved: @saved, totalNaive: @naive, totalActual: @actual,
- | commandCount: 1, byCommandType: { [@cmd]: { count: 1, saved: @saved } },
- | lastUpdated: DATE_ISO8601(DATE_NOW()) }
- |UPDATE {
- | totalSaved: OLD.totalSaved + @saved,
- | totalNaive: OLD.totalNaive + @naive,
- | totalActual: OLD.totalActual + @actual,
- | commandCount: OLD.commandCount + 1,
- | byCommandType: MERGE(OLD.byCommandType, {
- | [@cmd]: {
- | count: (HAS(OLD.byCommandType, @cmd) ? OLD.byCommandType[@cmd].count : 0) + 1,
- | saved: (HAS(OLD.byCommandType, @cmd) ? OLD.byCommandType[@cmd].saved : 0) + @saved
- | }
- | }),
- | lastUpdated: DATE_ISO8601(DATE_NOW())
- |} IN savings""".stripMargin,
- Map(
- "saved" -> Long.box(saved).asInstanceOf[AnyRef],
- "naive" -> Long.box(naive).asInstanceOf[AnyRef],
- "actual" -> Long.box(actual).asInstanceOf[AnyRef],
- "cmd" -> commandType.asInstanceOf[AnyRef]
- )
- )
-
- private def loadLifetime: IO[SavingsData] =
- client.queryOne(
- """FOR doc IN savings FILTER doc._key == "lifetime" RETURN doc""",
- Map.empty[String, AnyRef]
- ).map {
- case Some(json) =>
- val c = json.hcursor
- val byCmd = c.downField("byCommandType").focus
- .flatMap(_.asObject)
- .map { obj =>
- obj.toMap.map { case (k, v) =>
- val vc = v.hcursor
- k -> CommandSavings(
- count = vc.get[Long]("count").getOrElse(0L),
- saved = vc.get[Long]("saved").getOrElse(0L)
- )
- }
- }.getOrElse(Map.empty)
- SavingsData(
- totalSaved = c.get[Long]("totalSaved").getOrElse(0L),
- totalNaive = c.get[Long]("totalNaive").getOrElse(0L),
- totalActual = c.get[Long]("totalActual").getOrElse(0L),
- commandCount = c.get[Long]("commandCount").getOrElse(0L),
- byCommandType = byCmd
- )
- case None => SavingsData.empty
- }
-}
-
-object SavingsAccumulator {
- def create(client: ArangoClient): IO[SavingsAccumulator] =
- Ref.of[IO, SavingsData](SavingsData.empty).map(new SavingsAccumulator(client, _))
-}
-
-case class CommandSavings(count: Long, saved: Long)
-
-object CommandSavings {
- implicit val encoder: Encoder[CommandSavings] = Encoder.forProduct2("count", "saved")(cs =>
- (cs.count, cs.saved))
-}
-
-case class SavingsData(
- totalSaved: Long,
- totalNaive: Long,
- totalActual: Long,
- commandCount: Long,
- byCommandType: Map[String, CommandSavings]
-) {
- def add(cmd: String, actual: Long, naive: Long, saved: Long): SavingsData = {
- val existing = byCommandType.getOrElse(cmd, CommandSavings(0, 0))
- copy(
- totalSaved = totalSaved + saved,
- totalNaive = totalNaive + naive,
- totalActual = totalActual + actual,
- commandCount = commandCount + 1,
- byCommandType = byCommandType.updated(cmd,
- CommandSavings(existing.count + 1, existing.saved + saved))
- )
- }
-
- def withoutBreakdown: SavingsData = copy(byCommandType = Map.empty)
-
- /** Ensure this data is at least as large as `other` in all metrics. */
- def atLeast(other: SavingsData): SavingsData = copy(
- totalSaved = math.max(totalSaved, other.totalSaved),
- totalNaive = math.max(totalNaive, other.totalNaive),
- totalActual = math.max(totalActual, other.totalActual),
- commandCount = math.max(commandCount, other.commandCount),
- byCommandType = {
- val allKeys = byCommandType.keySet ++ other.byCommandType.keySet
- allKeys.map { k =>
- val a = byCommandType.getOrElse(k, CommandSavings(0, 0))
- val b = other.byCommandType.getOrElse(k, CommandSavings(0, 0))
- k -> CommandSavings(math.max(a.count, b.count), math.max(a.saved, b.saved))
- }.toMap
- }
- )
-}
-
-object SavingsData {
- val empty: SavingsData = SavingsData(0, 0, 0, 0, Map.empty)
-
- implicit val encoder: Encoder[SavingsData] = Encoder.forProduct5(
- "commandCount", "tokensSaved", "naiveTokens", "actualTokens", "byCommandType"
- )(d => (d.commandCount, d.totalSaved, d.totalNaive, d.totalActual, d.byCommandType))
-}
-
-case class SavingsResponse(session: SavingsData, lifetime: SavingsData)
-
-object SavingsResponse {
- implicit val encoder: Encoder[SavingsResponse] = Encoder.forProduct2("session", "lifetime")(r =>
- (r.session, r.lifetime))
-}
diff --git a/memory-layer/src/main/scala/ix/memory/savings/SavingsMiddleware.scala b/memory-layer/src/main/scala/ix/memory/savings/SavingsMiddleware.scala
deleted file mode 100644
index 32922d8d..00000000
--- a/memory-layer/src/main/scala/ix/memory/savings/SavingsMiddleware.scala
+++ /dev/null
@@ -1,73 +0,0 @@
-package ix.memory.savings
-
-import cats.data.Kleisli
-import cats.effect.IO
-import org.http4s.{HttpRoutes, Request, Response}
-
-// Middleware that intercepts responses from /v1/* routes and records
-// token savings. Runs as a fire-and-forget side effect.
-object SavingsMiddleware {
-
- // Routes that should not be tracked (meta/health endpoints).
- private val excludedPaths = Set("health", "savings", "stats")
-
- def apply(
- routes: HttpRoutes[IO],
- accumulator: SavingsAccumulator,
- estimator: NaiveCostEstimator
- ): HttpRoutes[IO] = Kleisli { (req: Request[IO]) =>
- routes(req).semiflatMap { response =>
- extractCommandType(req) match {
- case Some(cmd) if !excludedPaths.contains(cmd) =>
- // Read the response body to measure size, then reconstruct it
- response.body.compile.toVector.flatMap { bodyBytes =>
- val responseChars = bodyBytes.length.toLong
- val recordIO = for {
- naiveEstimate <- estimator.estimate(cmd, responseChars)
- actualTokens = math.max(1L, responseChars / 4)
- _ <- accumulator.record(cmd, actualTokens, naiveEstimate)
- } yield ()
-
- // Fire-and-forget: don't let savings tracking failure affect the response
- recordIO.attempt.void.as(
- response.withBodyStream(fs2.Stream.emits(bodyBytes))
- )
- }
-
- case _ => IO.pure(response)
- }
- }
- }
-
- // Extract the command type from the request path.
- private def extractCommandType(req: Request[IO]): Option[String] = {
- val segments = req.pathInfo.renderString.stripPrefix("/").split("/").toList
- segments match {
- case "v1" :: cmd :: _ => Some(mapPathToCommand(cmd))
- case _ => None
- }
- }
-
- // Map URL path segments to logical command names.
- private def mapPathToCommand(segment: String): String = segment match {
- case "context" => "query"
- case "search" => "search"
- case "entity" => "entity"
- case "expand" => "callers"
- case "expand-by-name"=> "callers"
- case "diff" => "diff"
- case "conflicts" => "conflicts"
- case "decide" => "decide"
- case "truth" => "truth"
- case "patches" => "history"
- case "map" => "map"
- case "list" => "inventory"
- case "decisions" => "decisions"
- case "smells" => "smells"
- case "subsystems" => "subsystems"
- case "provenance" => "history"
- case "resolve-prefix"=> "locate"
- case "ingest" => "map"
- case other => other
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/smell/SmellService.scala b/memory-layer/src/main/scala/ix/memory/smell/SmellService.scala
deleted file mode 100644
index 47e37fb8..00000000
--- a/memory-layer/src/main/scala/ix/memory/smell/SmellService.scala
+++ /dev/null
@@ -1,238 +0,0 @@
-package ix.memory.smell
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.syntax.parallel._
-import cats.syntax.traverse._
-import io.circe.Json
-import io.circe.syntax._
-
-import ix.memory.db.{ArangoClient, GraphWriteApi}
-import ix.memory.model._
-
-// ---------------------------------------------------------------------------
-// Data types
-// ---------------------------------------------------------------------------
-
-sealed trait SmellKind { def field: String }
-object SmellKind {
- case object OrphanFile extends SmellKind { val field = "has_smell.orphan_file" }
- case object GodModule extends SmellKind { val field = "has_smell.god_module" }
- case object WeakComponent extends SmellKind { val field = "has_smell.weak_component_member" }
-}
-
-final case class SmellCandidate(
- fileId: String, // logical_id UUID
- fileName: String,
- smellKind: SmellKind,
- confidence: Double,
- signals: Map[String, Json] // evidence key → value
-)
-
-final case class SmellConfig(
- /** Max total edge connectivity for a file to be an orphan candidate. */
- orphanMaxConnections: Int = 0,
- /** Min chunk count to flag a god module. */
- godModuleChunkThreshold: Int = 20,
- /** Min fan-in or fan-out (IMPORTS edges) to flag a god module. */
- godModuleFanThreshold: Int = 15,
- /** Max unique file neighbors for a weak-component candidate. */
- weakComponentMaxNeighbors: Int = 1
-)
-
-final case class SmellReport(
- candidates: Vector[SmellCandidate],
- rev: Long,
- runAt: String
-)
-
-// ---------------------------------------------------------------------------
-// Service
-// ---------------------------------------------------------------------------
-
-class SmellService(client: ArangoClient, writeApi: GraphWriteApi) {
-
- private val InferenceVersion = "smell_v1"
- private val Actor = "ix/smell"
-
- def run(config: SmellConfig = SmellConfig()): IO[SmellReport] =
- for {
- results <- (detectOrphans(config), detectGodModules(config), detectWeakComponents(config)).parTupled
- (orphans, godMods, weakComps) = results
- candidates = orphans ++ godMods ++ weakComps
- rev <- commitSmells(candidates)
- } yield SmellReport(candidates, rev, Instant.now().toString)
-
- /** Retrieve existing smell claims from the graph without rerunning detection. */
- def listSmells(): IO[Vector[Json]] =
- client.query(
- """FOR c IN claims
- | FILTER STARTS_WITH(c.field, "has_smell.")
- | AND c.status == "active"
- | AND c.deleted_rev == null
- | SORT c.entity_id, c.field
- | RETURN {
- | entity_id: c.entity_id,
- | smell: c.field,
- | value: c.value,
- | inference_version: c.inference_version
- | }""".stripMargin,
- Map.empty
- ).map(_.toVector)
-
- // ── Detectors (3 parallel, no MERGE maps — memory-safe) ──────────────────
-
- private def detectOrphans(cfg: SmellConfig): IO[Vector[SmellCandidate]] =
- client.query(
- // Per-file edge counts via subquery against indexed src/dst fields.
- // No global aggregation — each file only touches its own edges.
- """FOR f IN nodes
- | FILTER f.kind == "file" AND f.deleted_rev == null
- | LET imports_out = LENGTH(FOR e IN edges FILTER e.src == f.logical_id AND e.predicate == "IMPORTS" AND e.deleted_rev == null RETURN 1)
- | LET imports_in = LENGTH(FOR e IN edges FILTER e.dst == f.logical_id AND e.predicate == "IMPORTS" AND e.deleted_rev == null RETURN 1)
- | LET calls_out = LENGTH(FOR e IN edges FILTER e.src == f.logical_id AND e.predicate == "CALLS" AND e.deleted_rev == null RETURN 1)
- | LET calls_in = LENGTH(FOR e IN edges FILTER e.dst == f.logical_id AND e.predicate == "CALLS" AND e.deleted_rev == null RETURN 1)
- | LET connectivity = imports_out + imports_in + calls_out + calls_in
- | FILTER connectivity <= @threshold
- | RETURN {
- | logical_id: f.logical_id, name: f.name,
- | imports_out, imports_in, calls_out, calls_in, connectivity
- | }""".stripMargin,
- Map("threshold" -> Int.box(cfg.orphanMaxConnections).asInstanceOf[AnyRef])
- ).map(_.flatMap { json =>
- for {
- id <- json.hcursor.get[String]("logical_id").toOption
- name <- json.hcursor.get[String]("name").toOption
- connectivity <- json.hcursor.get[Int]("connectivity").toOption
- importsOut <- json.hcursor.get[Int]("imports_out").toOption
- importsIn <- json.hcursor.get[Int]("imports_in").toOption
- callsOut <- json.hcursor.get[Int]("calls_out").toOption
- callsIn <- json.hcursor.get[Int]("calls_in").toOption
- } yield SmellCandidate(
- fileId = id,
- fileName = name,
- smellKind = SmellKind.OrphanFile,
- confidence = if (connectivity == 0) 0.85 else 0.6,
- signals = Map(
- "connectivity" -> connectivity.asJson,
- "imports_out" -> importsOut.asJson,
- "imports_in" -> importsIn.asJson,
- "calls_out" -> callsOut.asJson,
- "calls_in" -> callsIn.asJson
- )
- )
- }.toVector)
-
- private def detectGodModules(cfg: SmellConfig): IO[Vector[SmellCandidate]] =
- client.query(
- """FOR f IN nodes
- | FILTER f.kind == "file" AND f.deleted_rev == null
- | LET chunks = LENGTH(FOR e IN edges FILTER e.src == f.logical_id AND e.predicate == "CONTAINS_CHUNK" AND e.deleted_rev == null RETURN 1)
- | LET symbols = LENGTH(FOR e IN edges FILTER e.src == f.logical_id AND e.predicate == "CONTAINS" AND e.deleted_rev == null RETURN 1)
- | LET fan_out = LENGTH(FOR e IN edges FILTER e.src == f.logical_id AND e.predicate == "IMPORTS" AND e.deleted_rev == null RETURN 1)
- | LET fan_in = LENGTH(FOR e IN edges FILTER e.dst == f.logical_id AND e.predicate == "IMPORTS" AND e.deleted_rev == null RETURN 1)
- | FILTER chunks >= @chunkThreshold OR fan_in >= @fanThreshold OR fan_out >= @fanThreshold
- | RETURN { logical_id: f.logical_id, name: f.name, chunks, symbols, fan_in, fan_out }""".stripMargin,
- Map(
- "chunkThreshold" -> Int.box(cfg.godModuleChunkThreshold).asInstanceOf[AnyRef],
- "fanThreshold" -> Int.box(cfg.godModuleFanThreshold).asInstanceOf[AnyRef]
- )
- ).map(_.flatMap { json =>
- for {
- id <- json.hcursor.get[String]("logical_id").toOption
- name <- json.hcursor.get[String]("name").toOption
- chunks <- json.hcursor.get[Int]("chunks").toOption
- symbols <- json.hcursor.get[Int]("symbols").toOption
- fanIn <- json.hcursor.get[Int]("fan_in").toOption
- fanOut <- json.hcursor.get[Int]("fan_out").toOption
- } yield {
- val triggeredSignals = Seq(
- Option.when(chunks >= cfg.godModuleChunkThreshold)(s"chunks=$chunks"),
- Option.when(fanIn >= cfg.godModuleFanThreshold)(s"fan_in=$fanIn"),
- Option.when(fanOut >= cfg.godModuleFanThreshold)(s"fan_out=$fanOut")
- ).flatten
- SmellCandidate(
- fileId = id,
- fileName = name,
- smellKind = SmellKind.GodModule,
- confidence = Math.min(0.5 + triggeredSignals.size * 0.15, 0.9),
- signals = Map(
- "chunks" -> chunks.asJson,
- "symbols" -> symbols.asJson,
- "fan_in" -> fanIn.asJson,
- "fan_out" -> fanOut.asJson,
- "triggers" -> triggeredSignals.asJson
- )
- )
- }
- }.toVector)
-
- private def detectWeakComponents(cfg: SmellConfig): IO[Vector[SmellCandidate]] =
- client.query(
- """FOR f IN nodes
- | FILTER f.kind == "file" AND f.deleted_rev == null
- | LET out_nbrs = (FOR e IN edges FILTER e.src == f.logical_id AND e.predicate IN ["IMPORTS","CALLS"] AND e.deleted_rev == null RETURN DISTINCT e.dst)
- | LET in_nbrs = (FOR e IN edges FILTER e.dst == f.logical_id AND e.predicate IN ["IMPORTS","CALLS"] AND e.deleted_rev == null RETURN DISTINCT e.src)
- | LET neighbor_count = LENGTH(UNION_DISTINCT(out_nbrs, in_nbrs))
- | FILTER neighbor_count > 0 AND neighbor_count <= @maxNeighbors
- | RETURN { logical_id: f.logical_id, name: f.name, neighbor_count }""".stripMargin,
- Map("maxNeighbors" -> Int.box(cfg.weakComponentMaxNeighbors).asInstanceOf[AnyRef])
- ).map(_.flatMap { json =>
- for {
- id <- json.hcursor.get[String]("logical_id").toOption
- name <- json.hcursor.get[String]("name").toOption
- neighborCount <- json.hcursor.get[Int]("neighbor_count").toOption
- } yield SmellCandidate(
- fileId = id,
- fileName = name,
- smellKind = SmellKind.WeakComponent,
- confidence = 0.55,
- signals = Map("neighbor_count" -> neighborCount.asJson)
- )
- }.toVector)
-
- // ── Claim persistence ────────────────────────────────────────────────────
-
- private def commitSmells(candidates: Vector[SmellCandidate]): IO[Long] = {
- if (candidates.isEmpty) return client.query(
- "FOR rev IN latest_rev SORT rev.value DESC LIMIT 1 RETURN rev.value",
- Map.empty
- ).map(_.headOption.flatMap(_.as[Long].toOption).getOrElse(0L))
-
- val ops: Vector[PatchOp] = candidates.map { c =>
- PatchOp.AssertClaim(
- entityId = NodeId(UUID.fromString(c.fileId)),
- field = c.smellKind.field,
- value = Json.obj(
- "confidence" -> c.confidence.asJson,
- "signals" -> c.signals.asJson,
- "file" -> c.fileName.asJson
- ),
- confidence = Some(c.confidence),
- inferenceVersion = Some(InferenceVersion)
- )
- }
-
- val patchId = PatchId(UUID.randomUUID())
- val sourceHash = UUID.nameUUIDFromBytes(s"smell_run:${Instant.now()}".getBytes("UTF-8")).toString
- val patch = GraphPatch(
- patchId = patchId,
- actor = Actor,
- timestamp = Instant.now(),
- source = PatchSource(
- uri = "ix://smell-detector",
- sourceHash = Some(sourceHash),
- extractor = s"smell-detector/$InferenceVersion",
- sourceType = SourceType.Code
- ),
- baseRev = Rev(0L),
- ops = ops,
- replaces = Vector.empty,
- intent = Some(s"Smell detection: ${candidates.size} candidates")
- )
-
- writeApi.commitPatch(patch).map(_.newRev.value)
- }
-}
diff --git a/memory-layer/src/main/scala/ix/memory/smell/SubsystemService.scala b/memory-layer/src/main/scala/ix/memory/smell/SubsystemService.scala
deleted file mode 100644
index 5aff04db..00000000
--- a/memory-layer/src/main/scala/ix/memory/smell/SubsystemService.scala
+++ /dev/null
@@ -1,195 +0,0 @@
-package ix.memory.smell
-
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.Json
-import io.circe.syntax._
-
-import ix.memory.db.{ArangoClient, GraphQueryApi}
-
-class SubsystemService(client: ArangoClient, queryApi: GraphQueryApi) {
-
- private val InferenceVersion = "subsystem_v1"
- private val mapper = new com.fasterxml.jackson.databind.ObjectMapper()
-
- // ── Public API ──────────────────────────────────────────────────────
-
- def scoreSubsystems(): IO[Json] =
- for {
- rows <- queryRawData()
- scores = rows.map(computeScore)
- rev <- queryApi.getLatestRev
- _ <- retireOldScores()
- _ <- storeScores(scores, rev.value)
- } yield Json.obj("scores" -> scores.map(encodeScore).asJson)
-
- def listSubsystems(): IO[Json] =
- client.query(
- """FOR c IN claims
- | FILTER c.field == "subsystem_health" AND c.deleted_rev == null
- | RETURN {
- | region_id: c.entity_id,
- | name: c.value.name,
- | level: c.value.level,
- | label_kind: c.value.label_kind,
- | file_count: c.value.file_count,
- | health_score: c.value.health_score,
- | chunk_density: c.value.chunk_density,
- | smell_rate: c.value.smell_rate,
- | smell_files: c.value.smell_files,
- | total_chunks: c.value.total_chunks,
- | confidence: c.value.confidence,
- | inference_version: c.value.inference_version
- | }""".stripMargin,
- Map.empty
- ).map(scores => Json.obj("scores" -> scores.asJson))
-
- // ── Internal data model ─────────────────────────────────────────────
-
- private case class RawData(
- regionId: String,
- name: String,
- level: Int,
- labelKind: String,
- fileCount: Int,
- totalChunks: Int,
- smellFiles: Int,
- confidence: Double
- )
-
- private case class Score(
- regionId: String,
- name: String,
- level: Int,
- labelKind: String,
- fileCount: Int,
- healthScore: Double,
- chunkDensity: Double,
- smellRate: Double,
- smellFiles: Int,
- totalChunks: Int,
- confidence: Double
- )
-
- // ── Raw data query ──────────────────────────────────────────────────
-
- /**
- * Single AQL query that joins regions with smell claims and CONTAINS_CHUNK
- * edge counts. The MERGE + SUM approach avoids N+1 queries.
- */
- private def queryRawData(): IO[List[RawData]] =
- client.query(
- """LET smell_ids = (
- | FOR c IN claims
- | FILTER STARTS_WITH(c.field, "has_smell.") AND c.deleted_rev == null
- | RETURN DISTINCT c.entity_id
- |)
- |LET chunk_map = MERGE(
- | FOR e IN edges
- | FILTER e.predicate == "CONTAINS" AND e.deleted_rev == null
- | COLLECT src = e.src INTO g
- | RETURN { [src]: LENGTH(g) }
- |)
- |FOR n IN nodes
- | FILTER n.kind == "region" AND n.deleted_rev == null
- | LET mf = n.attrs.member_files != null ? n.attrs.member_files : []
- | FILTER LENGTH(mf) > 0
- | LET file_count = LENGTH(mf)
- | LET total_chunks = SUM(
- | FOR f IN mf RETURN HAS(chunk_map, f) ? chunk_map[f] : 0
- | )
- | LET smell_count = LENGTH(
- | FOR f IN mf FILTER f IN smell_ids RETURN 1
- | )
- | RETURN {
- | region_id: n.logical_id,
- | name: n.name,
- | level: n.attrs.level,
- | label_kind: n.attrs.label_kind,
- | file_count: file_count,
- | total_chunks: total_chunks,
- | smell_files: smell_count,
- | confidence: n.attrs.confidence != null ? n.attrs.confidence : 0.0
- | }""".stripMargin,
- Map.empty
- ).map(_.flatMap { json =>
- val c = json.hcursor
- for {
- regionId <- c.get[String]("region_id").toOption
- name <- c.get[String]("name").toOption
- level <- c.get[Int]("level").toOption
- labelKind <- c.get[String]("label_kind").toOption
- fileCount <- c.get[Int]("file_count").toOption
- totalChunks <- c.get[Int]("total_chunks").toOption
- smellFiles <- c.get[Int]("smell_files").toOption
- confidence <- c.get[Double]("confidence").toOption
- } yield RawData(regionId, name, level, labelKind, fileCount, totalChunks, smellFiles, confidence)
- })
-
- // ── Scoring formula ─────────────────────────────────────────────────
-
- /**
- * Health score formula (from spec):
- * 0.4 × (1 − smell_rate) + 0.3 × tanh(chunk_density / 5) + 0.3 × confidence
- */
- private def computeScore(raw: RawData): Score = {
- val n = raw.fileCount.toDouble.max(1)
- val chunkDensity = raw.totalChunks.toDouble / n
- val smellRate = raw.smellFiles.toDouble / n
- val healthScore = (0.4 * (1 - smellRate) +
- 0.3 * Math.tanh(chunkDensity / 5) +
- 0.3 * raw.confidence).max(0.0).min(1.0)
- Score(raw.regionId, raw.name, raw.level, raw.labelKind,
- raw.fileCount, healthScore, chunkDensity, smellRate,
- raw.smellFiles, raw.totalChunks, raw.confidence)
- }
-
- // ── Persistence ─────────────────────────────────────────────────────
-
- private def retireOldScores(): IO[Unit] =
- client.execute(
- """FOR c IN claims
- | FILTER c.field == "subsystem_health" AND c.deleted_rev == null
- | REMOVE c IN claims""".stripMargin,
- Map.empty
- )
-
- private def storeScores(scores: List[Score], rev: Long): IO[Unit] =
- if (scores.isEmpty) IO.unit
- else {
- val docs = scores.map { s =>
- val key = UUID.nameUUIDFromBytes(s"subsystem:${s.regionId}".getBytes("UTF-8")).toString
- val value = encodeScore(s)
- val doc = new java.util.HashMap[String, AnyRef]()
- doc.put("_key", key)
- doc.put("entity_id", s.regionId)
- doc.put("field", "subsystem_health")
- doc.put("value", mapper.readValue(value.noSpaces, classOf[AnyRef]))
- doc.put("confidence", java.lang.Double.valueOf(s.healthScore))
- doc.put("status", "active")
- doc.put("created_rev", Long.box(rev))
- doc.put("deleted_rev", null)
- doc: java.util.Map[String, AnyRef]
- }
- client.bulkInsert("claims", docs, "replace").void
- }
-
- // ── Encoding ────────────────────────────────────────────────────────
-
- private def encodeScore(s: Score): Json =
- Json.obj(
- "region_id" -> s.regionId.asJson,
- "name" -> s.name.asJson,
- "level" -> s.level.asJson,
- "label_kind" -> s.labelKind.asJson,
- "file_count" -> s.fileCount.asJson,
- "health_score" -> s.healthScore.asJson,
- "chunk_density" -> s.chunkDensity.asJson,
- "smell_rate" -> s.smellRate.asJson,
- "smell_files" -> s.smellFiles.asJson,
- "total_chunks" -> s.totalChunks.asJson,
- "confidence" -> s.confidence.asJson,
- "inference_version" -> InferenceVersion.asJson
- )
-}
diff --git a/memory-layer/src/main/scala/ix/memory/subsystem/SubsystemScoringService.scala b/memory-layer/src/main/scala/ix/memory/subsystem/SubsystemScoringService.scala
deleted file mode 100644
index c6013199..00000000
--- a/memory-layer/src/main/scala/ix/memory/subsystem/SubsystemScoringService.scala
+++ /dev/null
@@ -1,224 +0,0 @@
-package ix.memory.subsystem
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import io.circe.Json
-import io.circe.syntax._
-
-import ix.memory.db.{ArangoClient, GraphWriteApi}
-import ix.memory.map.MapService
-import ix.memory.model._
-
-// ---------------------------------------------------------------------------
-// Data types
-// ---------------------------------------------------------------------------
-
-final case class SubsystemScore(
- regionId: String,
- regionName: String,
- level: Int,
- labelKind: String,
- fileCount: Int,
- totalChunks: Int,
- smellFiles: Int,
- confidence: Double,
- chunkDensity: Double,
- smellRate: Double,
- healthScore: Double
-)
-
-// ---------------------------------------------------------------------------
-// Service
-// ---------------------------------------------------------------------------
-
-/**
- * Computes evidence-based health scores for each architectural region (subsystem).
- *
- * Score formula (range 0–1):
- * health = 0.4*(1 − smellRate) + 0.3*tanh(chunkDensity/5) + 0.3*confidence
- *
- * Signals:
- * chunkDensity — avg CONTAINS_CHUNK edges per member file (more = richer structure)
- * smellRate — fraction of member files that have an active smell claim (lower = healthier)
- * confidence — MapService cohesion/boundary score carried from the region node
- *
- * Results stored as `has_score.subsystem_health` claims on Region nodes,
- * with inferenceVersion = "subsystem_v1".
- */
-class SubsystemScoringService(
- client: ArangoClient,
- writeApi: GraphWriteApi,
- mapService: MapService
-) {
-
- private val InferenceVersion = "subsystem_v1"
- private val Actor = "ix/subsystem-scorer"
- private val ScoreField = "has_score.subsystem_health"
-
- /** Run scoring: query chunk/smell metrics, compute health, persist claims. */
- def score(): IO[Vector[SubsystemScore]] =
- for {
- archMap <- mapService.getOrBuildMap()
- rows <- queryRegionMetrics(archMap.regions.map(_.id.value.toString), archMap.mapRev)
- scores = rows.flatMap(computeScore)
- _ <- persistScores(scores)
- } yield scores
-
- /** Retrieve previously stored health-score claims joined with region metadata. */
- def listScores(): IO[Vector[Json]] =
- client.query(
- """FOR r IN nodes
- | FILTER r.kind == "region" AND r.deleted_rev == null
- | LET score_claim = FIRST(
- | FOR c IN claims
- | FILTER c.entity_id == r.logical_id
- | AND c.field == "has_score.subsystem_health"
- | AND c.status == "active"
- | AND c.deleted_rev == null
- | SORT c.created_rev DESC
- | LIMIT 1
- | RETURN c
- | )
- | FILTER score_claim != null
- | SORT score_claim.value.health_score DESC
- | RETURN {
- | region_id: r.logical_id,
- | name: r.name,
- | level: r.attrs.level,
- | label_kind: r.attrs.label_kind,
- | file_count: r.attrs.file_count,
- | health_score: score_claim.value.health_score,
- | chunk_density: score_claim.value.chunk_density,
- | smell_rate: score_claim.value.smell_rate,
- | smell_files: score_claim.value.smell_files,
- | total_chunks: score_claim.value.total_chunks,
- | confidence: score_claim.value.confidence,
- | inference_version: score_claim.inference_version
- | }""".stripMargin,
- Map.empty
- ).map(_.toVector)
-
- // ── Internals ─────────────────────────────────────────────────────────────
-
- /**
- * Single AQL pass: for each region node, count CONTAINS_CHUNK edges across
- * member files, and count distinct member files that have any smell claim.
- */
- private def queryRegionMetrics(regionIds: Vector[String], mapRev: Long): IO[Vector[Json]] =
- if (regionIds.isEmpty) IO.pure(Vector.empty)
- else
- client.query(
- """FOR r IN nodes
- | FILTER r.kind == "region" AND r.deleted_rev == null
- | AND r.logical_id IN @regionIds
- | AND TO_NUMBER(r.attrs.map_rev) == @mapRev
- | LET chunk_count = LENGTH(
- | FOR e IN edges
- | FILTER e.src IN r.attrs.member_files
- | AND e.predicate == "CONTAINS_CHUNK"
- | AND e.deleted_rev == null
- | RETURN 1
- | )
- | LET smell_files = LENGTH(UNIQUE(
- | FOR c IN claims
- | FILTER c.entity_id IN r.attrs.member_files
- | AND STARTS_WITH(c.field, "has_smell.")
- | AND c.status == "active"
- | AND c.deleted_rev == null
- | RETURN c.entity_id
- | ))
- | RETURN {
- | id: r.logical_id,
- | name: r.name,
- | level: r.attrs.level,
- | label_kind: r.attrs.label_kind,
- | file_count: r.attrs.file_count,
- | confidence: r.attrs.confidence,
- | chunk_count: chunk_count,
- | smell_files: smell_files
- | }""".stripMargin,
- Map(
- "regionIds" -> regionIds.toArray.asInstanceOf[AnyRef],
- "mapRev" -> Long.box(mapRev).asInstanceOf[AnyRef]
- )
- ).map(_.toVector)
-
- private def computeScore(json: Json): Option[SubsystemScore] = {
- val c = json.hcursor
- for {
- regionId <- c.get[String]("id").toOption
- regionName <- c.get[String]("name").toOption
- } yield {
- val level = c.get[Int]("level").getOrElse(1)
- val labelKind = c.get[String]("label_kind").getOrElse("module")
- val fileCount = c.get[Int]("file_count").getOrElse(1).max(1)
- val confidence = c.get[Double]("confidence").getOrElse(0.0)
- val totalChunks = c.get[Int]("chunk_count").getOrElse(0)
- val smellFiles = c.get[Int]("smell_files").getOrElse(0)
-
- val chunkDensity = totalChunks.toDouble / fileCount
- val smellRate = smellFiles.toDouble / fileCount
- val normDensity = Math.tanh(chunkDensity / 5.0)
- val healthScore = (0.4 * (1.0 - smellRate) + 0.3 * normDensity + 0.3 * confidence)
- .max(0.0).min(1.0)
-
- SubsystemScore(
- regionId = regionId,
- regionName = regionName,
- level = level,
- labelKind = labelKind,
- fileCount = fileCount,
- totalChunks = totalChunks,
- smellFiles = smellFiles,
- confidence = confidence,
- chunkDensity = chunkDensity,
- smellRate = smellRate,
- healthScore = healthScore
- )
- }
- }
-
- private def persistScores(scores: Vector[SubsystemScore]): IO[Long] = {
- if (scores.isEmpty) return IO.pure(0L)
-
- val ops: Vector[PatchOp] = scores.flatMap { s =>
- scala.util.Try(UUID.fromString(s.regionId)).toOption.map { uuid =>
- PatchOp.AssertClaim(
- entityId = NodeId(uuid),
- field = ScoreField,
- value = Json.obj(
- "health_score" -> s.healthScore.asJson,
- "chunk_density" -> s.chunkDensity.asJson,
- "smell_rate" -> s.smellRate.asJson,
- "smell_files" -> s.smellFiles.asJson,
- "total_chunks" -> s.totalChunks.asJson,
- "file_count" -> s.fileCount.asJson,
- "confidence" -> s.confidence.asJson
- ),
- confidence = Some(s.healthScore),
- inferenceVersion = Some(InferenceVersion)
- )
- }
- }
-
- val patch = GraphPatch(
- patchId = PatchId(UUID.randomUUID()),
- actor = Actor,
- timestamp = Instant.now(),
- source = PatchSource(
- uri = "ix://subsystem-scorer",
- sourceHash = None,
- extractor = s"subsystem-scorer/$InferenceVersion",
- sourceType = SourceType.Inferred
- ),
- baseRev = Rev(0L),
- ops = ops,
- replaces = Vector.empty,
- intent = Some(s"Subsystem health scoring: ${scores.size} regions")
- )
-
- writeApi.commitPatch(patch).map(_.newRev.value)
- }
-}
diff --git a/memory-layer/src/test/resources/fixtures/billing_service.py b/memory-layer/src/test/resources/fixtures/billing_service.py
deleted file mode 100644
index 1d77f63d..00000000
--- a/memory-layer/src/test/resources/fixtures/billing_service.py
+++ /dev/null
@@ -1,22 +0,0 @@
-import http_client
-from config import RETRY_COUNT
-
-class BillingService:
- def __init__(self, gateway):
- self.gateway = gateway
-
- def process_payment(self, amount, currency):
- result = self.gateway.charge(amount, currency)
- if not result.success:
- self.retry_handler(amount, currency)
- return result
-
- def retry_handler(self, amount, currency, max_retries=3):
- for attempt in range(max_retries):
- result = self.gateway.charge(amount, currency)
- if result.success:
- return result
- raise PaymentError("Max retries exceeded")
-
-def standalone_function():
- return BillingService(None)
diff --git a/memory-layer/src/test/resources/fixtures/command_module.ts b/memory-layer/src/test/resources/fixtures/command_module.ts
deleted file mode 100644
index 382466b7..00000000
--- a/memory-layer/src/test/resources/fixtures/command_module.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import type { Command } from "commander";
-
-export function registerExplainCommand(program: Command): void {
- program
- .command("explain <symbol>")
- .description("Explain an entity")
- .action(async (symbol: string) => {
- const result = await resolveEntity(symbol);
- console.log(result);
- });
-}
-
-async function resolveEntity(symbol: string): Promise<string> {
- return `Resolved: ${symbol}`;
-}
diff --git a/memory-layer/src/test/resources/fixtures/command_registration.ts b/memory-layer/src/test/resources/fixtures/command_registration.ts
deleted file mode 100644
index 324867dd..00000000
--- a/memory-layer/src/test/resources/fixtures/command_registration.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import { Command } from "commander";
-import { registerExplainCommand } from "./commands/command_module.js";
-
-const program = new Command();
-program.name("test-cli").version("0.1.0");
-
-registerExplainCommand(program);
-
-program.parse();
diff --git a/memory-layer/src/test/resources/fixtures/node_kind.scala b/memory-layer/src/test/resources/fixtures/node_kind.scala
deleted file mode 100644
index 2da07612..00000000
--- a/memory-layer/src/test/resources/fixtures/node_kind.scala
+++ /dev/null
@@ -1,11 +0,0 @@
-package ix.memory.model
-
-sealed trait NodeKind
-
-object NodeKind {
- case object File extends NodeKind
- case object Module extends NodeKind
- case object Class extends NodeKind
- case object Function extends NodeKind
- case object Method extends NodeKind
-}
diff --git a/memory-layer/src/test/resources/fixtures/node_kind_user.scala b/memory-layer/src/test/resources/fixtures/node_kind_user.scala
deleted file mode 100644
index 57aba7a4..00000000
--- a/memory-layer/src/test/resources/fixtures/node_kind_user.scala
+++ /dev/null
@@ -1,13 +0,0 @@
-package ix.memory.ingestion
-
-class GraphPatchBuilder {
- def buildNode(kind: NodeKind, name: String): GraphNode = {
- val effectiveKind = NodeKind.File
- val result = createNode(name, effectiveKind)
- result
- }
-
- private def createNode(name: String, kind: NodeKind): GraphNode = {
- GraphNode(name, kind)
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/ClaimLifecycleSpec.scala b/memory-layer/src/test/scala/ix/memory/ClaimLifecycleSpec.scala
deleted file mode 100644
index be46d412..00000000
--- a/memory-layer/src/test/scala/ix/memory/ClaimLifecycleSpec.scala
+++ /dev/null
@@ -1,243 +0,0 @@
-package ix.memory
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import io.circe.Json
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import ix.memory.db._
-import ix.memory.model._
-
-class ClaimLifecycleSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers with TestDbHelper {
-
- val clientResource = ArangoClient.resource(
- host = "localhost", port = 8529,
- database = "ix_test_claim_lifecycle", user = "root", password = ""
- )
-
- private val testEntityId = NodeId(UUID.nameUUIDFromBytes("test:entity1".getBytes("UTF-8")))
-
- private def makePatch(
- baseRev: Rev = Rev(0L),
- ops: Vector[PatchOp] = Vector.empty,
- patchId: PatchId = PatchId(UUID.randomUUID()),
- source: PatchSource = PatchSource("file:///tmp/test.md", Some("hash1"), "markdown-parser/1.0", SourceType.Doc)
- ): GraphPatch =
- GraphPatch(
- patchId = patchId,
- actor = "test-actor",
- timestamp = Instant.parse("2025-06-01T12:00:00Z"),
- source = source,
- baseRev = baseRev,
- ops = ops,
- replaces = Vector.empty,
- intent = Some("test intent")
- )
-
- // ── Test 1: Claim Retirement ─────────────────────────────────────────
-
- "ClaimLifecycle" should "retire old claims when a new value is asserted for the same field" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
-
- // Step 1: Create node + assert initial content claim
- patch1 = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(testEntityId, NodeKind.Doc, "test.md", Map.empty[String, Json]),
- PatchOp.AssertClaim(testEntityId, "content", Json.fromString("hello"), None)
- )
- )
- commit1 <- writeApi.commitPatch(patch1)
-
- // Verify initial claim exists
- claimsBefore <- queryApi.getClaims(testEntityId)
- _ = {
- commit1.status shouldBe CommitStatus.Ok
- claimsBefore.count(_.status == ClaimStatus.Active) should be >= 1
- claimsBefore.exists(_.statement == "content") shouldBe true
- }
-
- // Step 2: Re-ingest with changed content
- patch2 = makePatch(
- baseRev = commit1.newRev,
- source = PatchSource("file:///tmp/test.md", Some("hash2"), "markdown-parser/1.0", SourceType.Doc),
- ops = Vector(
- PatchOp.UpsertNode(testEntityId, NodeKind.Doc, "test.md", Map.empty[String, Json]),
- PatchOp.AssertClaim(testEntityId, "content", Json.fromString("zzzxqv123"), None)
- )
- )
- commit2 <- writeApi.commitPatch(patch2)
-
- // Verify: only one active content claim, old one is retracted
- claimsAfter <- queryApi.getClaims(testEntityId)
- } yield {
- commit2.status shouldBe CommitStatus.Ok
-
- // Only one active content claim
- val activeContentClaims = claimsAfter.filter(c => c.statement == "content" && c.status == ClaimStatus.Active)
- activeContentClaims.size shouldBe 1
-
- // The old claim should be retired (not returned by getClaims which filters deleted_rev == null)
- // But we can check that no stale content claims exist in active state
- claimsAfter.count(c => c.statement == "content" && c.status == ClaimStatus.Active) shouldBe 1
- }
- }
- }
-
- // ── Test 2: Search Coverage ──────────────────────────────────────────
-
- it should "find entities by name, provenance, claim field, and claim value" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
-
- // Ingest a doc node with claims
- patch = makePatch(
- source = PatchSource("file:///projects/readme.md", Some("abc"), "markdown-parser/1.0", SourceType.Doc),
- ops = Vector(
- PatchOp.UpsertNode(testEntityId, NodeKind.Doc, "readme.md", Map.empty[String, Json]),
- PatchOp.AssertClaim(testEntityId, "language", Json.fromString("markdown"), None),
- PatchOp.AssertClaim(testEntityId, "content", Json.fromString("This is a test document about billing"), None)
- )
- )
- commit <- writeApi.commitPatch(patch)
- _ = commit.status shouldBe CommitStatus.Ok
-
- // Search by node name
- byName <- queryApi.searchNodes("readme")
- // Search by provenance (source_uri)
- byProvenance <- queryApi.searchNodes("/projects/readme")
- // Search by claim field
- byField <- queryApi.searchNodes("language")
- // Search by claim value
- byValue <- queryApi.searchNodes("markdown")
- // Search by content
- byContent <- queryApi.searchNodes("billing")
- } yield {
- byName should not be empty
- byName.head.id shouldBe testEntityId
-
- byProvenance should not be empty
- byProvenance.head.id shouldBe testEntityId
-
- byField should not be empty
- byField.head.id shouldBe testEntityId
-
- byValue should not be empty
- byValue.head.id shouldBe testEntityId
-
- byContent should not be empty
- byContent.head.id shouldBe testEntityId
- }
- }
- }
-
- // ── Test 3: Identical Claim Dedup ────────────────────────────────────
-
- it should "not create duplicate active claims when the same claim is asserted twice" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
-
- // Step 1: Assert a claim
- patch1 = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(testEntityId, NodeKind.Doc, "test.md", Map.empty[String, Json]),
- PatchOp.AssertClaim(testEntityId, "language", Json.fromString("markdown"), None)
- )
- )
- commit1 <- writeApi.commitPatch(patch1)
-
- claimsBefore <- queryApi.getClaims(testEntityId)
- langClaimsBefore = claimsBefore.count(c => c.statement == "language" && c.status == ClaimStatus.Active)
-
- // Step 2: Assert the exact same claim again
- patch2 = makePatch(
- baseRev = commit1.newRev,
- ops = Vector(
- PatchOp.UpsertNode(testEntityId, NodeKind.Doc, "test.md", Map.empty[String, Json]),
- PatchOp.AssertClaim(testEntityId, "language", Json.fromString("markdown"), None)
- )
- )
- commit2 <- writeApi.commitPatch(patch2)
-
- claimsAfter <- queryApi.getClaims(testEntityId)
- langClaimsAfter = claimsAfter.count(c => c.statement == "language" && c.status == ClaimStatus.Active)
- } yield {
- commit1.status shouldBe CommitStatus.Ok
- commit2.status shouldBe CommitStatus.Ok
-
- // Should have exactly one active language claim, not two
- langClaimsBefore shouldBe 1
- langClaimsAfter shouldBe 1
- }
- }
- }
-
- // ── Test 4: Absent Claims Retirement ─────────────────────────────────
-
- it should "retire claims from the same extractor that are no longer emitted" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
-
- // Step 1: Assert two claims from the same extractor
- patch1 = makePatch(
- source = PatchSource("file:///tmp/test.md", Some("hash1"), "markdown-parser/1.0", SourceType.Doc),
- ops = Vector(
- PatchOp.UpsertNode(testEntityId, NodeKind.Doc, "test.md", Map.empty[String, Json]),
- PatchOp.AssertClaim(testEntityId, "language", Json.fromString("markdown"), None),
- PatchOp.AssertClaim(testEntityId, "content", Json.fromString("hello world"), None)
- )
- )
- commit1 <- writeApi.commitPatch(patch1)
-
- claimsBefore <- queryApi.getClaims(testEntityId)
- _ = {
- claimsBefore.count(_.status == ClaimStatus.Active) shouldBe 2
- }
-
- // Step 2: Re-ingest from same extractor but only emit "language" (content is absent)
- patch2 = makePatch(
- baseRev = commit1.newRev,
- source = PatchSource("file:///tmp/test.md", Some("hash2"), "markdown-parser/1.0", SourceType.Doc),
- ops = Vector(
- PatchOp.UpsertNode(testEntityId, NodeKind.Doc, "test.md", Map.empty[String, Json]),
- PatchOp.AssertClaim(testEntityId, "language", Json.fromString("markdown"), None)
- )
- )
- commit2 <- writeApi.commitPatch(patch2)
-
- claimsAfter <- queryApi.getClaims(testEntityId)
- } yield {
- commit2.status shouldBe CommitStatus.Ok
-
- // Only "language" should remain active; "content" should be retired
- val activeClaims = claimsAfter.filter(_.status == ClaimStatus.Active)
- activeClaims.size shouldBe 1
- activeClaims.head.statement shouldBe "language"
- }
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/TestDbHelper.scala b/memory-layer/src/test/scala/ix/memory/TestDbHelper.scala
deleted file mode 100644
index 9d618590..00000000
--- a/memory-layer/src/test/scala/ix/memory/TestDbHelper.scala
+++ /dev/null
@@ -1,18 +0,0 @@
-package ix.memory
-
-import cats.effect.IO
-import ix.memory.db.ArangoClient
-
-trait TestDbHelper {
-
- /** Truncate all collections to ensure a clean state for each test. */
- protected def cleanDatabase(client: ArangoClient): IO[Unit] = {
- val collections = List(
- "nodes", "edges", "claims", "patches",
- "revisions", "idempotency_keys", "conflict_sets"
- )
- collections.foldLeft(IO.unit) { (acc, name) =>
- acc >> client.execute(s"FOR doc IN $name REMOVE doc IN $name", Map.empty[String, AnyRef])
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/api/RoutesSpec.scala b/memory-layer/src/test/scala/ix/memory/api/RoutesSpec.scala
deleted file mode 100644
index 080a4074..00000000
--- a/memory-layer/src/test/scala/ix/memory/api/RoutesSpec.scala
+++ /dev/null
@@ -1,365 +0,0 @@
-package ix.memory.api
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import io.circe.Json
-import io.circe.syntax._
-import org.http4s._
-import org.http4s.circe.CirceEntityCodec._
-import org.http4s.implicits._
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import ix.memory.TestDbHelper
-import ix.memory.conflict.ConflictService
-import ix.memory.context._
-import ix.memory.db._
-import ix.memory.map.MapService
-import ix.memory.model._
-import ix.memory.savings.SavingsAccumulator
-import ix.memory.smell.SmellService
-import ix.memory.subsystem.SubsystemScoringService
-
-class RoutesSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers with TestDbHelper {
-
- val clientResource = ArangoClient.resource(
- host = "localhost", port = 8529,
- database = "ix_test_routes", user = "root", password = ""
- )
-
- private def makePatch(
- baseRev: Rev = Rev(0L),
- ops: Vector[PatchOp] = Vector.empty,
- patchId: PatchId = PatchId(UUID.randomUUID())
- ): GraphPatch =
- GraphPatch(
- patchId = patchId,
- actor = "test-actor",
- timestamp = Instant.parse("2025-06-01T12:00:00Z"),
- source = PatchSource(
- uri = "test://source",
- sourceHash = Some("hash123"),
- extractor = "test-extractor",
- sourceType = SourceType.Code
- ),
- baseRev = baseRev,
- ops = ops,
- replaces = Vector.empty,
- intent = Some("test intent")
- )
-
- private def buildRoutes(
- client: ArangoClient,
- writeApi: ArangoGraphWriteApi,
- queryApi: ArangoGraphQueryApi
- ): HttpApp[IO] = {
- val conflictService = new ConflictService(client, queryApi, writeApi)
- val contextService = new ContextService(
- queryApi,
- new GraphSeeder(queryApi),
- new GraphExpander(queryApi),
- new ClaimCollector(queryApi),
- new ConfidenceScorerImpl(),
- new ConflictDetectorImpl()
- )
- val bulkWriteApi = new BulkWriteApi(client)
- val mapService = new MapService(client, queryApi, writeApi)
- val smellService = new SmellService(client, writeApi)
- val subsystemService = new SubsystemScoringService(client, writeApi, mapService)
- val savingsAccumulator = SavingsAccumulator.create(client).unsafeRunSync()(ioRuntime)
-
- Routes.all(
- contextService,
- queryApi,
- writeApi,
- conflictService,
- client,
- mapService,
- bulkWriteApi,
- smellService,
- subsystemService,
- savingsAccumulator
- ).orNotFound
- }
-
- "Routes" should "return 200 OK for health check" in {
- clientResource.use { client =>
- val app = buildRoutes(client, new ArangoGraphWriteApi(client), new ArangoGraphQueryApi(client))
-
- for {
- resp <- app.run(Request[IO](Method.GET, uri"/v1/health"))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.get[String]("status") shouldBe Right("ok")
- }
- }
- }
-
- it should "return entity with claims and edges" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
- val app = buildRoutes(client, writeApi, queryApi)
- val nodeId = NodeId(UUID.randomUUID())
- val patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Function, "test_func", Map("lang" -> Json.fromString("scala"))),
- PatchOp.AssertClaim(nodeId, "returns", Json.fromString("Int"), Some(0.9))
- )
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- resp <- app.run(Request[IO](Method.GET, Uri.unsafeFromString(s"/v1/entity/${nodeId.value}")))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.downField("node").downField("id").as[String] shouldBe Right(nodeId.value.toString)
- body.hcursor.downField("claims").focus.flatMap(_.asArray).map(_.nonEmpty) shouldBe Some(true)
- }
- }
- }
-
- it should "return empty conflicts list" in {
- clientResource.use { client =>
- val app = buildRoutes(client, new ArangoGraphWriteApi(client), new ArangoGraphQueryApi(client))
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- resp <- app.run(Request[IO](Method.GET, uri"/v1/conflicts"))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.asArray.map(_.isEmpty) shouldBe Some(true)
- }
- }
- }
-
- it should "diff between revisions and detect added entity" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
- val app = buildRoutes(client, writeApi, queryApi)
- val nodeId = NodeId(UUID.randomUUID())
- val patch = makePatch(
- ops = Vector(PatchOp.UpsertNode(nodeId, NodeKind.Function, "diff_func", Map.empty[String, Json]))
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- result <- writeApi.commitPatch(patch)
- req = Request[IO](Method.POST, uri"/v1/diff").withEntity(Json.obj(
- "fromRev" -> 0L.asJson,
- "toRev" -> result.newRev.value.asJson,
- "entityId" -> nodeId.value.toString.asJson
- ))
- resp <- app.run(req)
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- val changes = body.hcursor.downField("changes").focus.flatMap(_.asArray).getOrElse(Vector.empty)
- changes.size shouldBe 1
- }
- }
- }
-
- it should "return structured context for a query" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
- val app = buildRoutes(client, writeApi, queryApi)
- val nodeId = NodeId(UUID.randomUUID())
- val patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Function, "billing_calc", Map.empty[String, Json]),
- PatchOp.AssertClaim(nodeId, "calculates billing", Json.fromString("true"), Some(0.9))
- )
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- resp <- app.run(Request[IO](Method.POST, uri"/v1/context").withEntity(Json.obj("query" -> "billing".asJson)))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.downField("metadata").downField("query").as[String] shouldBe Right("billing")
- }
- }
- }
-
- it should "return provenance chain for entity" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
- val app = buildRoutes(client, writeApi, queryApi)
- val nodeId = NodeId(UUID.randomUUID())
- val patch = makePatch(ops = Vector(PatchOp.UpsertNode(nodeId, NodeKind.Function, "prov_func", Map.empty[String, Json])))
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- resp <- app.run(Request[IO](Method.POST, Uri.unsafeFromString(s"/v1/provenance/${nodeId.value}")))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.downField("entityId").as[String] shouldBe Right(nodeId.value.toString)
- }
- }
- }
-
- it should "return provenance chain for entity after bulk commit" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
- val bulkWriteApi = new BulkWriteApi(client)
- val app = buildRoutes(client, writeApi, queryApi)
- val nodeId = NodeId(UUID.randomUUID())
- val patch = makePatch(ops = Vector(PatchOp.UpsertNode(nodeId, NodeKind.Function, "bulk_prov_func", Map.empty[String, Json])))
- val provenance = new java.util.HashMap[String, AnyRef]()
- provenance.put("source_uri", patch.source.uri)
- provenance.put("source_hash", patch.source.sourceHash.orNull)
- provenance.put("extractor", patch.source.extractor)
- provenance.put("source_type", patch.source.sourceType.asJson.asString.getOrElse("code"))
- provenance.put("observed_at", patch.timestamp.toString)
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- bulkWriteApi.commitBatchChunked(
- Vector(FileBatch(patch.source.uri, patch.source.sourceHash, patch, provenance)),
- baseRev = 0L
- )
- resp <- app.run(Request[IO](Method.POST, Uri.unsafeFromString(s"/v1/provenance/${nodeId.value}")))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.downField("entityId").as[String] shouldBe Right(nodeId.value.toString)
- body.hcursor.downField("chain").focus.flatMap(_.asArray).exists(_.nonEmpty) shouldBe true
- }
- }
- }
-
- it should "create a decision node via POST /v1/decide" in {
- clientResource.use { client =>
- val app = buildRoutes(client, new ArangoGraphWriteApi(client), new ArangoGraphQueryApi(client))
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- resp <- app.run(Request[IO](Method.POST, uri"/v1/decide").withEntity(Json.obj(
- "title" -> "Use exponential backoff".asJson,
- "rationale" -> "Better for transient failures".asJson
- )))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.get[String]("status").toOption shouldBe Some("Ok")
- }
- }
- }
-
- it should "search nodes by term via POST /v1/search" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
- val app = buildRoutes(client, writeApi, queryApi)
- val nodeId = NodeId(UUID.randomUUID())
- val patch = makePatch(ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Service, "billing_search_test", Map.empty[String, Json])
- ))
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- resp <- app.run(Request[IO](Method.POST, uri"/v1/search").withEntity(Json.obj("term" -> "billing_search_test".asJson)))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.asArray should not be empty
- }
- }
- }
-
- it should "return intent list via GET /v1/truth" in {
- clientResource.use { client =>
- val app = buildRoutes(client, new ArangoGraphWriteApi(client), new ArangoGraphQueryApi(client))
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- resp <- app.run(Request[IO](Method.GET, uri"/v1/truth"))
- } yield {
- resp.status shouldBe Status.Ok
- }
- }
- }
-
- it should "create an intent via POST /v1/truth" in {
- clientResource.use { client =>
- val app = buildRoutes(client, new ArangoGraphWriteApi(client), new ArangoGraphQueryApi(client))
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- resp <- app.run(Request[IO](Method.POST, uri"/v1/truth").withEntity(Json.obj(
- "statement" -> "Ship retry system by Friday".asJson
- )))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.get[String]("status").toOption shouldBe Some("Ok")
- }
- }
- }
-
- it should "list recent patches via GET /v1/patches" in {
- clientResource.use { client =>
- val app = buildRoutes(client, new ArangoGraphWriteApi(client), new ArangoGraphQueryApi(client))
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- resp <- app.run(Request[IO](Method.GET, uri"/v1/patches"))
- } yield {
- resp.status shouldBe Status.Ok
- }
- }
- }
-
- it should "return a specific patch by ID via GET /v1/patches/:id" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
- val app = buildRoutes(client, writeApi, queryApi)
- val nodeId = NodeId(UUID.randomUUID())
- val patchId = PatchId(UUID.randomUUID())
- val patch = makePatch(
- patchId = patchId,
- ops = Vector(PatchOp.UpsertNode(nodeId, NodeKind.Function, "patch_test_func", Map.empty[String, Json]))
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- resp <- app.run(Request[IO](Method.GET, Uri.unsafeFromString(s"/v1/patches/${patchId.value}")))
- body <- resp.as[Json]
- } yield {
- resp.status shouldBe Status.Ok
- body.hcursor.get[String]("patch_id").toOption shouldBe Some(patchId.value.toString)
- }
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/conflict/ConflictDetectorSpec.scala b/memory-layer/src/test/scala/ix/memory/conflict/ConflictDetectorSpec.scala
deleted file mode 100644
index 3211e087..00000000
--- a/memory-layer/src/test/scala/ix/memory/conflict/ConflictDetectorSpec.scala
+++ /dev/null
@@ -1,120 +0,0 @@
-package ix.memory.conflict
-
-import java.time.Instant
-import java.util.UUID
-import org.scalatest.flatspec.AnyFlatSpec
-import org.scalatest.matchers.should.Matchers
-import io.circe.Json
-import ix.memory.model._
-import ix.memory.context.ConflictDetectorImpl
-
-class ConflictDetectorSpec extends AnyFlatSpec with Matchers {
- val detector = new ConflictDetectorImpl()
-
- private def makeScoredClaim(entityId: NodeId, statement: String,
- score: Double, sourceType: SourceType = SourceType.Code): ScoredClaim = {
- val claim = Claim(
- id = ClaimId(UUID.randomUUID()),
- entityId = entityId,
- statement = statement,
- value = Json.Null,
- confidence = None,
- status = ClaimStatus.Active,
- provenance = Provenance("test.py", Some("abc"), "test", sourceType, Instant.now()),
- createdRev = Rev(1L),
- deletedRev = None
- )
- val breakdown = ConfidenceBreakdown(
- baseAuthority = Factor(score, "test"),
- verification = Factor(1.0, "test"),
- recency = Factor(1.0, "test"),
- corroboration = Factor(1.0, "test"),
- conflictPenalty = Factor(1.0, "test"),
- intentAlignment = Factor(1.0, "test")
- )
- ScoredClaim(claim, breakdown)
- }
-
- "ConflictDetector" should "detect conflicting claims on same entity" in {
- val entityId = NodeId(UUID.randomUUID())
- val claim1 = makeScoredClaim(entityId, "returns String", 0.90)
- val claim2 = makeScoredClaim(entityId, "returns Int", 0.75)
-
- val conflicts = detector.detect(Vector(claim1, claim2))
-
- conflicts.size shouldBe 1
- conflicts.head.reason should include("inconsistency")
- conflicts.head.recommendation should include("returns String")
- }
-
- it should "not flag claims on different entities" in {
- val entity1 = NodeId(UUID.randomUUID())
- val entity2 = NodeId(UUID.randomUUID())
- val claim1 = makeScoredClaim(entity1, "returns String", 0.90)
- val claim2 = makeScoredClaim(entity2, "returns Int", 0.75)
-
- val conflicts = detector.detect(Vector(claim1, claim2))
-
- conflicts shouldBe empty
- }
-
- it should "not flag claims with the same statement" in {
- val entityId = NodeId(UUID.randomUUID())
- val claim1 = makeScoredClaim(entityId, "returns String", 0.90)
- val claim2 = makeScoredClaim(entityId, "returns String", 0.85)
-
- val conflicts = detector.detect(Vector(claim1, claim2))
-
- conflicts shouldBe empty
- }
-
- it should "recommend the higher-confidence claim" in {
- val entityId = NodeId(UUID.randomUUID())
- val highConf = makeScoredClaim(entityId, "returns String", 0.95)
- val lowConf = makeScoredClaim(entityId, "returns Int", 0.40)
-
- val conflicts = detector.detect(Vector(highConf, lowConf))
-
- conflicts.head.recommendation should include("returns String")
- conflicts.head.recommendation should include("0.95")
- }
-
- it should "detect multiple conflicts in a group" in {
- val entityId = NodeId(UUID.randomUUID())
- val claim1 = makeScoredClaim(entityId, "returns String", 0.90)
- val claim2 = makeScoredClaim(entityId, "returns Int", 0.75)
- val claim3 = makeScoredClaim(entityId, "returns Boolean", 0.60)
-
- val conflicts = detector.detect(Vector(claim1, claim2, claim3))
-
- conflicts.size shouldBe 3 // 3 pairs from 3 different statements
- }
-
- it should "ignore retracted claims" in {
- val entityId = NodeId(UUID.randomUUID())
- val activeClaim = makeScoredClaim(entityId, "returns String", 0.90)
- val retractedClaim = {
- val sc = makeScoredClaim(entityId, "returns Int", 0.75)
- sc.copy(claim = sc.claim.copy(status = ClaimStatus.Retracted))
- }
-
- val conflicts = detector.detect(Vector(activeClaim, retractedClaim))
-
- conflicts shouldBe empty
- }
-
- it should "return empty for single claim" in {
- val entityId = NodeId(UUID.randomUUID())
- val claim = makeScoredClaim(entityId, "returns String", 0.90)
-
- val conflicts = detector.detect(Vector(claim))
-
- conflicts shouldBe empty
- }
-
- it should "return empty for empty input" in {
- val conflicts = detector.detect(Vector.empty)
-
- conflicts shouldBe empty
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala b/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala
deleted file mode 100644
index ef7f1e05..00000000
--- a/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala
+++ /dev/null
@@ -1,85 +0,0 @@
-package ix.memory.context
-
-import java.time.Instant
-import java.util.UUID
-import org.scalatest.flatspec.AnyFlatSpec
-import org.scalatest.matchers.should.Matchers
-import io.circe.Json
-import ix.memory.model._
-
-class ConfidenceScorerSpec extends AnyFlatSpec with Matchers {
- val scorer = new ConfidenceScorerImpl()
-
- private def makeClaim(sourceType: SourceType, rev: Long = 1L): Claim = Claim(
- id = ClaimId(UUID.randomUUID()),
- entityId = NodeId(UUID.randomUUID()),
- statement = "test statement",
- value = Json.Null,
- confidence = None,
- status = ClaimStatus.Active,
- provenance = Provenance("test.py", Some("abc123"), "tree-sitter", sourceType, Instant.now()),
- createdRev = Rev(rev),
- deletedRev = None
- )
-
- private val defaultCtx = ScoringContext(
- latestRev = Rev(5L),
- sourceChanged = false,
- corroboratingCount = 1,
- conflictState = ConflictState.NoConflict,
- intentAlignment = IntentAlignment.NoConnection,
- observedAt = Instant.now()
- )
-
- "ConfidenceScorer" should "score code-sourced claims higher than doc-sourced" in {
- val codeClaim = makeClaim(SourceType.Code)
- val docClaim = makeClaim(SourceType.Doc)
-
- val codeResult = scorer.score(codeClaim, defaultCtx)
- val docResult = scorer.score(docClaim, defaultCtx)
-
- codeResult.confidence.score should be > docResult.confidence.score
- }
-
- it should "penalize claims with conflicts" in {
- val claim = makeClaim(SourceType.Code)
- val noConflict = scorer.score(claim, defaultCtx.copy(conflictState = ConflictState.NoConflict))
- val withConflict = scorer.score(claim, defaultCtx.copy(conflictState = ConflictState.ExistsHigherAuthority))
-
- noConflict.confidence.score should be > withConflict.confidence.score
- }
-
- it should "boost claims with corroboration" in {
- val claim = makeClaim(SourceType.Code)
- val single = scorer.score(claim, defaultCtx.copy(corroboratingCount = 1))
- val corroborated = scorer.score(claim, defaultCtx.copy(corroboratingCount = 3))
-
- corroborated.confidence.score should be > single.confidence.score
- }
-
- it should "provide explainable breakdown" in {
- val claim = makeClaim(SourceType.Code)
- val result = scorer.score(claim, defaultCtx)
-
- result.confidence.baseAuthority.reason should include("Code")
- result.confidence.score should be > 0.0
- result.confidence.score should be <= 1.0
- }
-
- it should "reduce score for stale sources" in {
- val claim = makeClaim(SourceType.Code)
- val fresh = scorer.score(claim, defaultCtx.copy(sourceChanged = false))
- // Use an observedAt 60 days in the past so the recency penalty kicks in
- val staleObservedAt = Instant.now().minus(java.time.Duration.ofDays(60))
- val stale = scorer.score(claim, defaultCtx.copy(sourceChanged = true, observedAt = staleObservedAt))
-
- fresh.confidence.score should be > stale.confidence.score
- }
-
- it should "severely penalize conflict resolved against" in {
- val claim = makeClaim(SourceType.Code)
- val result = scorer.score(claim, defaultCtx.copy(conflictState = ConflictState.ResolvedAgainst))
-
- result.confidence.score should be < 0.5
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/context/ContextServiceSpec.scala b/memory-layer/src/test/scala/ix/memory/context/ContextServiceSpec.scala
deleted file mode 100644
index 41a7ddb9..00000000
--- a/memory-layer/src/test/scala/ix/memory/context/ContextServiceSpec.scala
+++ /dev/null
@@ -1,242 +0,0 @@
-package ix.memory.context
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import io.circe.Json
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import ix.memory.TestDbHelper
-import ix.memory.db._
-import ix.memory.model._
-
-class ContextServiceSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers with TestDbHelper {
-
- val clientResource = ArangoClient.resource(
- host = "localhost", port = 8529,
- database = "ix_test_context_service", user = "root", password = ""
- )
-
- private def makePatch(
- baseRev: Rev = Rev(0L),
- ops: Vector[PatchOp] = Vector.empty,
- patchId: PatchId = PatchId(UUID.randomUUID())
- ): GraphPatch =
- GraphPatch(
- patchId = patchId,
- actor = "test-actor",
- timestamp = Instant.parse("2025-06-01T12:00:00Z"),
- source = PatchSource(
- uri = "test://source",
- sourceHash = Some("hash123"),
- extractor = "test-extractor",
- sourceType = SourceType.Code
- ),
- baseRev = baseRev,
- ops = ops,
- replaces = Vector.empty,
- intent = Some("test intent")
- )
-
- private def buildContextService(queryApi: GraphQueryApi): ContextService = {
- val seeder = new GraphSeeder(queryApi)
- val expander = new GraphExpander(queryApi)
- val collector = new ClaimCollector(queryApi)
- val scorer = new ConfidenceScorerImpl()
- val detector = new ConflictDetectorImpl()
- new ContextService(queryApi, seeder, expander, collector, scorer, detector)
- }
-
- // ── Test 1: Full pipeline returns structured context ───────────────
-
- // TODO: fix context query seeding — search doesn't find nodes in fresh CI database
- "ContextService" should "return structured context for a query" ignore {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- val billingId = NodeId(UUID.nameUUIDFromBytes(s"test:billing_service".getBytes))
- val retryId = NodeId(UUID.nameUUIDFromBytes(s"test:retry_handler".getBytes))
- val edgeId = EdgeId(UUID.randomUUID())
-
- val contextService = buildContextService(queryApi)
-
- val patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(billingId, NodeKind.Function, "billing_service", Map.empty[String, Json]),
- PatchOp.UpsertNode(retryId, NodeKind.Function, "retry_handler", Map.empty[String, Json]),
- PatchOp.UpsertEdge(edgeId, billingId, retryId, EdgePredicate("calls"), Map.empty[String, Json]),
- PatchOp.AssertClaim(billingId, "handles_billing", Json.fromString("true"), Some(0.9))
- )
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- result <- contextService.query("How does billing retry?")
- } yield {
- result.nodes should not be empty
- result.metadata.query shouldBe "How does billing retry?"
- // seedEntities is List[NodeId] — verify at least 1 seed was found
- result.metadata.seedEntities should not be empty
- result.metadata.hopsExpanded shouldBe 1
- result.metadata.depth shouldBe Some("standard")
- }
- }
- }
-
- // ── Test 2: Empty context for unknown query ────────────────────────
-
- it should "return empty context for unknown query" in {
- clientResource.use { client =>
- val queryApi = new ArangoGraphQueryApi(client)
- val contextService = buildContextService(queryApi)
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- result <- contextService.query("something totally unknown xyz123")
- } yield {
- result.nodes shouldBe empty
- result.claims shouldBe empty
- }
- }
- }
-
- // ── Test 3: EntityExtractor extracts meaningful terms ──────────────
-
- it should "extract meaningful terms from queries" in {
- val terms = EntityExtractor.extract("How does billing retry?")
- terms should contain("billing")
- terms should contain("retry")
- terms should not contain ("how")
- terms should not contain ("does")
- }
-
- // ── Test 4: Claims are scored and ranked ───────────────────────────
-
- it should "score and rank claims by confidence" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- val nodeId = NodeId(UUID.randomUUID())
-
- val contextService = buildContextService(queryApi)
-
- val patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Function, "billing_func", Map.empty[String, Json]),
- PatchOp.AssertClaim(nodeId, "returns_int", Json.fromString("true"), Some(0.9)),
- PatchOp.AssertClaim(nodeId, "billing_logic", Json.fromString("complex"), Some(0.8))
- )
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- result <- contextService.query("What does billing do?")
- } yield {
- result.claims should not be empty
- // All claims should have a confidence score > 0
- result.claims.foreach { sc =>
- sc.confidence.score should be > 0.0
- }
- // Claims should have finalScore > 0
- result.claims.foreach { sc =>
- sc.finalScore should be > 0.0
- }
- // Claims should be sorted descending by finalScore
- val scores = result.claims.map(_.finalScore)
- scores shouldBe scores.sorted.reverse
- }
- }
- }
-
- // ── Test 5: GraphExpander returns connected nodes ──────────────────
-
- it should "expand seeds to find connected nodes" ignore {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- val billingId = NodeId(UUID.randomUUID())
- val retryId = NodeId(UUID.randomUUID())
- val edgeId = EdgeId(UUID.randomUUID())
-
- val contextService = buildContextService(queryApi)
-
- val patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(billingId, NodeKind.Service, "billing_service", Map.empty[String, Json]),
- PatchOp.UpsertNode(retryId, NodeKind.Function, "retry_logic", Map.empty[String, Json]),
- PatchOp.UpsertEdge(edgeId, billingId, retryId, EdgePredicate("calls"), Map.empty[String, Json])
- )
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- result <- contextService.query("How does billing work?")
- } yield {
- // Should find billing_service as seed, then expand to find retry_logic
- result.nodes.length should be >= 1
- result.edges should not be empty
- }
- }
- }
-
- // ── Test 6: No conflicts when only one claim per entity ────────────
-
- it should "return empty conflicts when no contradictions exist" in {
- clientResource.use { client =>
- val writeApi = new ArangoGraphWriteApi(client)
- val queryApi = new ArangoGraphQueryApi(client)
-
- val nodeId = NodeId(UUID.randomUUID())
- val contextService = buildContextService(queryApi)
-
- val patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Function, "billing_calc", Map.empty[String, Json]),
- PatchOp.AssertClaim(nodeId, "billing_info", Json.fromString("test"), Some(0.9))
- )
- )
-
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- _ <- writeApi.commitPatch(patch)
- result <- contextService.query("billing info")
- } yield {
- result.conflicts shouldBe empty
- }
- }
- }
-
- // ── Test 7: ContextRanker sorts correctly ──────────────────────────
-
- it should "rank claims in descending confidence order" in {
- val prov = Provenance("test://uri", None, "test", SourceType.Code, Instant.now())
- val claim1 = Claim(ClaimId(UUID.randomUUID()), NodeId(UUID.randomUUID()), "field1",
- Json.Null, None, ClaimStatus.Active, prov, Rev(1L), None)
- val claim2 = Claim(ClaimId(UUID.randomUUID()), NodeId(UUID.randomUUID()), "field2",
- Json.Null, None, ClaimStatus.Active, prov, Rev(1L), None)
-
- val scored1 = ScoredClaim(claim1, ConfidenceBreakdown(
- Factor(0.5, "low"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok")
- ), relevance = 1.0, finalScore = 0.5)
- val scored2 = ScoredClaim(claim2, ConfidenceBreakdown(
- Factor(0.9, "high"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok")
- ), relevance = 1.0, finalScore = 0.9)
-
- val ranked = ContextRanker.rank(Vector(scored1, scored2))
- ranked.head.finalScore should be > ranked.last.finalScore
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/context/CorroborationCounterSpec.scala b/memory-layer/src/test/scala/ix/memory/context/CorroborationCounterSpec.scala
deleted file mode 100644
index fae04745..00000000
--- a/memory-layer/src/test/scala/ix/memory/context/CorroborationCounterSpec.scala
+++ /dev/null
@@ -1,90 +0,0 @@
-package ix.memory.context
-
-import java.time.Instant
-import java.util.UUID
-
-import org.scalatest.flatspec.AnyFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import io.circe.Json
-import ix.memory.model._
-
-class CorroborationCounterSpec extends AnyFlatSpec with Matchers {
-
- private def makeClaim(entityId: NodeId, statement: String, sourceUri: String): Claim =
- Claim(
- id = ClaimId(UUID.randomUUID()),
- entityId = entityId,
- statement = statement,
- value = Json.Null,
- confidence = None,
- status = ClaimStatus.Active,
- provenance = Provenance(sourceUri, None, "test", SourceType.Code, Instant.now()),
- createdRev = Rev(1L),
- deletedRev = None
- )
-
- val entityA = NodeId(UUID.randomUUID())
- val entityB = NodeId(UUID.randomUUID())
-
- // Test 1: Single claim — no corroboration
- "CorroborationCounter" should "return 0 for a single claim" in {
- val claim = makeClaim(entityA, "handles billing", "src/billing.py")
- val counts = CorroborationCounter.count(Vector(claim))
- counts(claim.id) shouldBe 0
- }
-
- // Test 2: Two claims, same entity, same topic, different sources — corroborate
- it should "count corroborating claims from independent sources" in {
- val c1 = makeClaim(entityA, "handles billing logic", "src/billing.py")
- val c2 = makeClaim(entityA, "billing handler function", "docs/api.md")
- val counts = CorroborationCounter.count(Vector(c1, c2))
- counts(c1.id) shouldBe 1
- counts(c2.id) shouldBe 1
- }
-
- // Test 3: Two claims, same entity, same source — NOT corroborating
- it should "not count claims from the same source" in {
- val c1 = makeClaim(entityA, "handles billing logic", "src/billing.py")
- val c2 = makeClaim(entityA, "billing handler function", "src/billing.py")
- val counts = CorroborationCounter.count(Vector(c1, c2))
- counts(c1.id) shouldBe 0
- counts(c2.id) shouldBe 0
- }
-
- // Test 4: Two claims, different entities — NOT corroborating
- it should "not count claims on different entities" in {
- val c1 = makeClaim(entityA, "handles billing logic", "src/billing.py")
- val c2 = makeClaim(entityB, "handles billing logic", "docs/api.md")
- val counts = CorroborationCounter.count(Vector(c1, c2))
- counts(c1.id) shouldBe 0
- counts(c2.id) shouldBe 0
- }
-
- // Test 5: Two claims, same entity, no keyword overlap — NOT corroborating
- it should "not count claims with no keyword overlap" in {
- val c1 = makeClaim(entityA, "handles billing logic", "src/billing.py")
- val c2 = makeClaim(entityA, "returns integer value", "docs/api.md")
- val counts = CorroborationCounter.count(Vector(c1, c2))
- counts(c1.id) shouldBe 0
- counts(c2.id) shouldBe 0
- }
-
- // Test 6: Three claims corroborating
- it should "count multiple corroborating sources" in {
- val c1 = makeClaim(entityA, "billing service handler", "src/billing.py")
- val c2 = makeClaim(entityA, "billing api handler", "docs/billing.md")
- val c3 = makeClaim(entityA, "handles billing requests", "test/billing_test.py")
- val counts = CorroborationCounter.count(Vector(c1, c2, c3))
- // c1 should be corroborated by c2 and c3 (all share "bill" stem)
- counts(c1.id) shouldBe 2
- counts(c2.id) shouldBe 2
- counts(c3.id) shouldBe 2
- }
-
- // Test 7: Empty claims vector
- it should "return empty map for no claims" in {
- val counts = CorroborationCounter.count(Vector.empty)
- counts shouldBe empty
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/context/LlmJudgeSpec.scala b/memory-layer/src/test/scala/ix/memory/context/LlmJudgeSpec.scala
deleted file mode 100644
index 35545662..00000000
--- a/memory-layer/src/test/scala/ix/memory/context/LlmJudgeSpec.scala
+++ /dev/null
@@ -1,103 +0,0 @@
-package ix.memory.context
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import io.circe.Json
-import ix.memory.model._
-
-class LlmJudgeSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {
-
- private def makeScoredClaim(statement: String, entityId: NodeId, score: Double = 0.8): ScoredClaim = {
- val claim = Claim(
- ClaimId(UUID.randomUUID()), entityId, statement,
- Json.Null, None, ClaimStatus.Active,
- Provenance("test://src", None, "test", SourceType.Code, Instant.now()),
- Rev(1L), None
- )
- ScoredClaim(claim, ConfidenceBreakdown(
- Factor(score, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"),
- Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok")
- ), relevance = 1.0, finalScore = score)
- }
-
- val entityId: NodeId = NodeId(UUID.randomUUID())
-
- // Test 1: StubLlmJudge returns None
- "StubLlmJudge" should "always return None" in {
- val judge = new StubLlmJudge()
- val c1 = makeScoredClaim("handles billing", entityId).claim
- val c2 = makeScoredClaim("does not handle billing", entityId).claim
- judge.judge(c1, c2).asserting(_ shouldBe None)
- }
-
- // Test 2: ConflictDetectorImpl without LLM — refineWithLlm returns conflicts unchanged
- "ConflictDetectorImpl" should "return conflicts unchanged when no LLM is provided" in {
- val detector = new ConflictDetectorImpl(llmJudge = None)
- val sc1 = makeScoredClaim("handles billing", entityId)
- val sc2 = makeScoredClaim("does not handle billing", entityId)
- val conflicts = detector.detect(Vector(sc1, sc2))
- conflicts should not be empty
-
- detector.refineWithLlm(conflicts, Vector(sc1, sc2)).asserting { refined =>
- refined shouldBe conflicts
- }
- }
-
- // Test 3: ConflictDetectorImpl with LLM that confirms conflict
- it should "upgrade conflict reason with LLM explanation" in {
- val mockJudge = new LlmJudge {
- def judge(a: Claim, b: Claim): IO[Option[LlmJudgment]] =
- IO.pure(Some(LlmJudgment(
- isConflict = true,
- confidence = 0.95,
- explanation = "These claims directly contradict each other",
- preferredClaim = Some(a.id)
- )))
- }
- val detector = new ConflictDetectorImpl(llmJudge = Some(mockJudge))
- val sc1 = makeScoredClaim("handles billing", entityId)
- val sc2 = makeScoredClaim("does not handle billing", entityId)
- val conflicts = detector.detect(Vector(sc1, sc2))
-
- detector.refineWithLlm(conflicts, Vector(sc1, sc2)).asserting { refined =>
- refined should have length conflicts.length.toLong
- refined.head.reason should include("[LLM:")
- refined.head.recommendation should include("LLM recommends")
- }
- }
-
- // Test 4: ConflictDetectorImpl with LLM that dismisses conflict
- it should "dismiss conflicts when LLM says no conflict" in {
- val mockJudge = new LlmJudge {
- def judge(a: Claim, b: Claim): IO[Option[LlmJudgment]] =
- IO.pure(Some(LlmJudgment(
- isConflict = false,
- confidence = 0.9,
- explanation = "These are complementary, not contradictory",
- preferredClaim = None
- )))
- }
- val detector = new ConflictDetectorImpl(llmJudge = Some(mockJudge))
- val sc1 = makeScoredClaim("handles billing", entityId)
- val sc2 = makeScoredClaim("billing service handler", entityId)
- val conflicts = detector.detect(Vector(sc1, sc2))
-
- detector.refineWithLlm(conflicts, Vector(sc1, sc2)).asserting { refined =>
- refined shouldBe empty
- }
- }
-
- // Test 5: Empty conflicts — nothing to refine
- it should "return empty when no conflicts to refine" in {
- val detector = new ConflictDetectorImpl(llmJudge = Some(new StubLlmJudge()))
- detector.refineWithLlm(Vector.empty, Vector.empty).asserting { refined =>
- refined shouldBe empty
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/context/RelevanceScorerSpec.scala b/memory-layer/src/test/scala/ix/memory/context/RelevanceScorerSpec.scala
deleted file mode 100644
index d28a7e81..00000000
--- a/memory-layer/src/test/scala/ix/memory/context/RelevanceScorerSpec.scala
+++ /dev/null
@@ -1,125 +0,0 @@
-package ix.memory.context
-
-import java.time.Instant
-import java.util.UUID
-
-import org.scalatest.flatspec.AnyFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import ix.memory.model._
-
-class RelevanceScorerSpec extends AnyFlatSpec with Matchers {
-
- private val prov = Provenance("test://uri", None, "test", SourceType.Code, Instant.now())
- private val defaultBreakdown = ConfidenceBreakdown(
- Factor(0.8, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"),
- Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok")
- )
-
- private def makeClaim(entityId: NodeId): ScoredClaim = {
- val claim = Claim(
- ClaimId(UUID.randomUUID()), entityId, "test_field",
- io.circe.Json.Null, None, ClaimStatus.Active, prov, Rev(1L), None
- )
- ScoredClaim(claim, defaultBreakdown, relevance = 1.0, finalScore = defaultBreakdown.score)
- }
-
- "RelevanceScorer" should "assign relevance 1.0 to seed node claims" in {
- val seedId = NodeId(UUID.randomUUID())
- val claims = Vector(makeClaim(seedId))
- val result = RelevanceScorer.score(claims, Set(seedId), Vector.empty)
- result.head.relevance shouldBe 1.0
- result.head.finalScore shouldBe (1.0 * defaultBreakdown.score)
- }
-
- it should "assign relevance 0.7 to one-hop node claims" in {
- val seedId = NodeId(UUID.randomUUID())
- val oneHopId = NodeId(UUID.randomUUID())
- val edge = GraphEdge(
- id = EdgeId(UUID.randomUUID()),
- src = seedId, dst = oneHopId,
- predicate = EdgePredicate("calls"),
- attrs = io.circe.Json.obj(),
- provenance = prov,
- createdRev = Rev(1L), deletedRev = None
- )
- val claims = Vector(makeClaim(oneHopId))
- val result = RelevanceScorer.score(claims, Set(seedId), Vector(edge))
- result.head.relevance shouldBe 0.7
- result.head.finalScore shouldBe (0.7 * defaultBreakdown.score +- 0.001)
- }
-
- it should "assign relevance 0.4 to two-hop-plus node claims" in {
- val seedId = NodeId(UUID.randomUUID())
- val farId = NodeId(UUID.randomUUID())
- val claims = Vector(makeClaim(farId))
- val result = RelevanceScorer.score(claims, Set(seedId), Vector.empty)
- result.head.relevance shouldBe 0.4
- result.head.finalScore shouldBe (0.4 * defaultBreakdown.score +- 0.001)
- }
-
- it should "handle mixed claims correctly" in {
- val seedId = NodeId(UUID.randomUUID())
- val oneHopId = NodeId(UUID.randomUUID())
- val farId = NodeId(UUID.randomUUID())
- val edge = GraphEdge(
- id = EdgeId(UUID.randomUUID()),
- src = seedId, dst = oneHopId,
- predicate = EdgePredicate("calls"),
- attrs = io.circe.Json.obj(),
- provenance = prov,
- createdRev = Rev(1L), deletedRev = None
- )
- val claims = Vector(makeClaim(seedId), makeClaim(oneHopId), makeClaim(farId))
- val result = RelevanceScorer.score(claims, Set(seedId), Vector(edge))
-
- result(0).relevance shouldBe 1.0
- result(1).relevance shouldBe 0.7
- result(2).relevance shouldBe 0.4
- }
-
- it should "boost symbol node claims with kindBoost 1.3x" in {
- val seedId = NodeId(UUID.randomUUID())
- val kindMap = Map(seedId -> NodeKind.Function)
- val claims = Vector(makeClaim(seedId))
- val result = RelevanceScorer.scoreWithTerms(claims, Set(seedId), Vector.empty, Vector.empty, kindMap)
- // hopRelevance=1.0 * kindBoost=1.3 → clamped to 1.0
- result.head.relevance shouldBe 1.0
- }
-
- it should "penalize File node claims with kindBoost 0.6x" in {
- val seedId = NodeId(UUID.randomUUID())
- val kindMap = Map(seedId -> NodeKind.File)
- val claims = Vector(makeClaim(seedId))
- val result = RelevanceScorer.scoreWithTerms(claims, Set(seedId), Vector.empty, Vector.empty, kindMap)
- // hopRelevance=1.0 * kindBoost=0.6 = 0.6
- result.head.relevance shouldBe 0.6
- result.head.finalScore shouldBe (0.6 * defaultBreakdown.score +- 0.001)
- }
-
- it should "rank Method claims above File claims for same seed" in {
- val methodId = NodeId(UUID.randomUUID())
- val fileId = NodeId(UUID.randomUUID())
- val kindMap = Map(methodId -> NodeKind.Method, fileId -> NodeKind.File)
- val claims = Vector(makeClaim(fileId), makeClaim(methodId))
- val result = RelevanceScorer.scoreWithTerms(
- claims, Set(methodId, fileId), Vector.empty, Vector.empty, kindMap
- ).sortBy(-_.finalScore)
- result.head.claim.entityId shouldBe methodId
- }
-
- it should "apply neutral 1.0x kindBoost for Config nodes" in {
- val seedId = NodeId(UUID.randomUUID())
- val kindMap = Map(seedId -> NodeKind.Config)
- val claims = Vector(makeClaim(seedId))
- val result = RelevanceScorer.scoreWithTerms(claims, Set(seedId), Vector.empty, Vector.empty, kindMap)
- result.head.relevance shouldBe 1.0
- }
-
- it should "use default kindBoost 1.0 when nodeKindMap is empty" in {
- val seedId = NodeId(UUID.randomUUID())
- val claims = Vector(makeClaim(seedId))
- val result = RelevanceScorer.scoreWithTerms(claims, Set(seedId), Vector.empty, Vector.empty, Map.empty)
- result.head.relevance shouldBe 1.0
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/context/StalenessDetectorSpec.scala b/memory-layer/src/test/scala/ix/memory/context/StalenessDetectorSpec.scala
deleted file mode 100644
index 1db40fec..00000000
--- a/memory-layer/src/test/scala/ix/memory/context/StalenessDetectorSpec.scala
+++ /dev/null
@@ -1,123 +0,0 @@
-package ix.memory.context
-
-import java.nio.file.{Files, Path}
-import java.security.MessageDigest
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import io.circe.Json
-import ix.memory.model._
-
-class StalenessDetectorSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {
-
- private def hashOf(content: String): String =
- MessageDigest.getInstance("SHA-256")
- .digest(content.getBytes("UTF-8"))
- .map("%02x".format(_))
- .mkString
-
- private def makeClaim(sourceUri: String, sourceHash: Option[String]): Claim =
- Claim(
- id = ClaimId(UUID.randomUUID()),
- entityId = NodeId(UUID.randomUUID()),
- statement = "test claim",
- value = Json.Null,
- confidence = None,
- status = ClaimStatus.Active,
- provenance = Provenance(sourceUri, sourceHash, "test", SourceType.Code, Instant.now()),
- createdRev = Rev(1L),
- deletedRev = None
- )
-
- // Test 1: File unchanged -- not stale
- "StalenessDetector" should "return false when file hash matches" in {
- val tmpFile = Files.createTempFile("staleness-test-", ".txt")
- Files.writeString(tmpFile, "hello world")
- val hash = hashOf("hello world")
- val claim = makeClaim(tmpFile.toString, Some(hash))
-
- StalenessDetector.detect(Vector(claim)).map { result =>
- result(claim.id) shouldBe false
- Files.deleteIfExists(tmpFile)
- succeed
- }
- }
-
- // Test 2: File changed -- stale
- it should "return true when file hash differs" in {
- val tmpFile = Files.createTempFile("staleness-test-", ".txt")
- Files.writeString(tmpFile, "updated content")
- val oldHash = hashOf("original content")
- val claim = makeClaim(tmpFile.toString, Some(oldHash))
-
- StalenessDetector.detect(Vector(claim)).map { result =>
- result(claim.id) shouldBe true
- Files.deleteIfExists(tmpFile)
- succeed
- }
- }
-
- // Test 3: File doesn't exist -- stale
- it should "return true when file no longer exists" in {
- val claim = makeClaim("/nonexistent/path/to/file.py", Some("abc123"))
-
- StalenessDetector.detect(Vector(claim)).map { result =>
- result(claim.id) shouldBe true
- }
- }
-
- // Test 4: No source hash recorded -- treat as fresh
- it should "return false when no source hash is recorded" in {
- val claim = makeClaim("/some/path.py", None)
-
- StalenessDetector.detect(Vector(claim)).map { result =>
- result(claim.id) shouldBe false
- }
- }
-
- // Test 5: Multiple claims from same file -- efficient (single hash)
- it should "handle multiple claims from the same source efficiently" in {
- val tmpFile = Files.createTempFile("staleness-test-", ".txt")
- Files.writeString(tmpFile, "content")
- val hash = hashOf("content")
- val c1 = makeClaim(tmpFile.toString, Some(hash))
- val c2 = makeClaim(tmpFile.toString, Some(hash))
-
- StalenessDetector.detect(Vector(c1, c2)).map { result =>
- result(c1.id) shouldBe false
- result(c2.id) shouldBe false
- Files.deleteIfExists(tmpFile)
- succeed
- }
- }
-
- // Test 6: Empty claims
- it should "return empty map for no claims" in {
- StalenessDetector.detect(Vector.empty).map { result =>
- result shouldBe empty
- }
- }
-
- // Test 7: Mix of stale and fresh
- it should "handle mix of stale and fresh claims" in {
- val tmpFile = Files.createTempFile("staleness-test-", ".txt")
- Files.writeString(tmpFile, "current content")
- val correctHash = hashOf("current content")
- val wrongHash = hashOf("old content")
-
- val fresh = makeClaim(tmpFile.toString, Some(correctHash))
- val stale = makeClaim(tmpFile.toString, Some(wrongHash))
-
- StalenessDetector.detect(Vector(fresh, stale)).map { result =>
- result(fresh.id) shouldBe false
- result(stale.id) shouldBe true
- Files.deleteIfExists(tmpFile)
- succeed
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/db/ArangoClientSpec.scala b/memory-layer/src/test/scala/ix/memory/db/ArangoClientSpec.scala
deleted file mode 100644
index d693d7c3..00000000
--- a/memory-layer/src/test/scala/ix/memory/db/ArangoClientSpec.scala
+++ /dev/null
@@ -1,55 +0,0 @@
-package ix.memory.db
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-import io.circe.Json
-
-class ArangoClientSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {
-
- // Requires ArangoDB running on localhost:8529
- val clientResource = ArangoClient.resource(
- host = "localhost", port = 8529,
- database = "ix_test_arango_client", user = "root", password = ""
- )
-
- "ArangoClient" should "connect and ensure schema" in {
- clientResource.use { client =>
- client.ensureSchema().map(_ => succeed)
- }
- }
-
- it should "execute a simple query" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- results <- client.query("RETURN 1", Map.empty)
- } yield results.size shouldBe 1
- }
- }
-
- it should "insert and retrieve a document" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- // Clean up any leftover document from previous runs
- _ <- client.execute(
- "FOR n IN nodes FILTER n._key == @key REMOVE n IN nodes",
- Map("key" -> "test1")
- )
- _ <- client.execute(
- "INSERT { _key: @key, name: @name } INTO nodes",
- Map("key" -> "test1", "name" -> "testNode")
- )
- result <- client.queryOne(
- "FOR n IN nodes FILTER n._key == @key RETURN n",
- Map("key" -> "test1")
- )
- } yield {
- result shouldBe defined
- result.get.hcursor.get[String]("name") shouldBe Right("testNode")
- }
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/db/GraphQueryApiSpec.scala b/memory-layer/src/test/scala/ix/memory/db/GraphQueryApiSpec.scala
deleted file mode 100644
index 8038c276..00000000
--- a/memory-layer/src/test/scala/ix/memory/db/GraphQueryApiSpec.scala
+++ /dev/null
@@ -1,276 +0,0 @@
-package ix.memory.db
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import io.circe.Json
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import ix.memory.TestDbHelper
-import ix.memory.model._
-
-class GraphQueryApiSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers with TestDbHelper {
-
- val clientResource = ArangoClient.resource(
- host = "localhost", port = 8529,
- database = "ix_test_graph_query", user = "root", password = ""
- )
-
- private def makePatch(
- baseRev: Rev = Rev(0L),
- ops: Vector[PatchOp] = Vector.empty,
- patchId: PatchId = PatchId(UUID.randomUUID())
- ): GraphPatch =
- GraphPatch(
- patchId = patchId,
- actor = "test-actor",
- timestamp = Instant.parse("2025-06-01T12:00:00Z"),
- source = PatchSource(
- uri = "test://source",
- sourceHash = Some("hash123"),
- extractor = "test-extractor",
- sourceType = SourceType.Code
- ),
- baseRev = baseRev,
- ops = ops,
- replaces = Vector.empty,
- intent = Some("test intent")
- )
-
- // ── Test 1: getNode should return a committed node ──────────────────
-
- "GraphQueryApi" should "getNode should return a committed node" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
- nodeId = NodeId(UUID.randomUUID())
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId,
- kind = NodeKind.Function,
- name = "billing_calc",
- attrs = Map("lang" -> Json.fromString("scala"))
- )
- )
- )
- _ <- writeApi.commitPatch(patch)
- result <- queryApi.getNode(nodeId)
- } yield {
- result shouldBe defined
- val node = result.get
- node.id shouldBe nodeId
- node.kind shouldBe NodeKind.Function
- node.attrs.hcursor.get[String]("lang") shouldBe Right("scala")
- node.provenance.extractor shouldBe "test-extractor"
- node.provenance.sourceType shouldBe SourceType.Code
- node.deletedRev shouldBe None
- }
- }
- }
-
- // ── Test 2: getNode should respect MVCC visibility ──────────────────
-
- it should "getNode should respect MVCC visibility" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
- nodeId = NodeId(UUID.randomUUID())
- // Commit patch1: create the node
- patch1 = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId,
- kind = NodeKind.Class,
- name = "MyClass",
- attrs = Map.empty[String, Json]
- )
- )
- )
- res1 <- writeApi.commitPatch(patch1)
- rev1 = res1.newRev
- // Commit patch2: delete the node
- patch2 = makePatch(
- baseRev = rev1,
- ops = Vector(PatchOp.DeleteNode(nodeId))
- )
- res2 <- writeApi.commitPatch(patch2)
- rev2 = res2.newRev
- // Query as of rev1 → should see the node
- atRev1 <- queryApi.getNode(nodeId, asOfRev = Some(rev1))
- // Query as of rev2 → should not see the node (deleted)
- atRev2 <- queryApi.getNode(nodeId, asOfRev = Some(rev2))
- } yield {
- atRev1 shouldBe defined
- atRev1.get.id shouldBe nodeId
- atRev2 shouldBe None
- }
- }
- }
-
- // ── Test 3: findNodesByKind should return nodes of given kind ────────
-
- it should "findNodesByKind should return nodes of given kind" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
- funcId1 = NodeId(UUID.randomUUID())
- funcId2 = NodeId(UUID.randomUUID())
- classId = NodeId(UUID.randomUUID())
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(funcId1, NodeKind.Function, "func1", Map.empty[String, Json]),
- PatchOp.UpsertNode(funcId2, NodeKind.Function, "func2", Map.empty[String, Json]),
- PatchOp.UpsertNode(classId, NodeKind.Class, "cls1", Map.empty[String, Json])
- )
- )
- _ <- writeApi.commitPatch(patch)
- funcs <- queryApi.findNodesByKind(NodeKind.Function)
- classes <- queryApi.findNodesByKind(NodeKind.Class)
- } yield {
- funcs.length shouldBe 2
- funcs.map(_.kind).toSet shouldBe Set(NodeKind.Function)
- classes.length shouldBe 1
- classes.head.kind shouldBe NodeKind.Class
- }
- }
- }
-
- // ── Test 4: expand should return connected nodes and edges ──────────
-
- it should "expand should return connected nodes and edges" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
- nodeA = NodeId(UUID.randomUUID())
- nodeB = NodeId(UUID.randomUUID())
- edgeId = EdgeId(UUID.randomUUID())
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(nodeA, NodeKind.Function, "funcA", Map.empty[String, Json]),
- PatchOp.UpsertNode(nodeB, NodeKind.Function, "funcB", Map.empty[String, Json]),
- PatchOp.UpsertEdge(edgeId, nodeA, nodeB, EdgePredicate("calls"), Map.empty[String, Json])
- )
- )
- _ <- writeApi.commitPatch(patch)
- outRes <- queryApi.expand(nodeA, Direction.Out)
- inRes <- queryApi.expand(nodeB, Direction.In)
- bothRes <- queryApi.expand(nodeA, Direction.Both)
- } yield {
- // Out from nodeA should find nodeB via "calls" edge
- outRes.edges.length shouldBe 1
- outRes.edges.head.predicate shouldBe EdgePredicate("calls")
- outRes.edges.head.src shouldBe nodeA
- outRes.edges.head.dst shouldBe nodeB
- outRes.nodes.length shouldBe 1
- outRes.nodes.head.id shouldBe nodeB
-
- // In to nodeB should find nodeA
- inRes.edges.length shouldBe 1
- inRes.nodes.length shouldBe 1
- inRes.nodes.head.id shouldBe nodeA
-
- // Both from nodeA should also find the edge/node
- bothRes.edges.length shouldBe 1
- bothRes.nodes.length shouldBe 1
- }
- }
- }
-
- // ── Test 5: searchNodes should find nodes by name ───────────────────
-
- it should "searchNodes should find nodes by name" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
- node1 = NodeId(UUID.randomUUID())
- node2 = NodeId(UUID.randomUUID())
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(node1, NodeKind.Service, "billing_service", Map.empty[String, Json]),
- PatchOp.UpsertNode(node2, NodeKind.Service, "payment_gateway", Map.empty[String, Json])
- )
- )
- _ <- writeApi.commitPatch(patch)
- results <- queryApi.searchNodes("billing")
- } yield {
- results.length shouldBe 1
- results.head.id shouldBe node1
- }
- }
- }
-
- // ── Test 6: getClaims should return active claims for entity ────────
-
- it should "getClaims should return active claims for entity" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
- nodeId = NodeId(UUID.randomUUID())
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Function, "myFunc", Map.empty[String, Json]),
- PatchOp.AssertClaim(nodeId, "returns", Json.fromString("Int"), Some(0.9))
- )
- )
- _ <- writeApi.commitPatch(patch)
- claims <- queryApi.getClaims(nodeId)
- } yield {
- claims.length shouldBe 1
- val claim = claims.head
- claim.entityId shouldBe nodeId
- claim.statement shouldBe "returns"
- claim.status shouldBe ClaimStatus.Active
- claim.provenance.extractor shouldBe "test-extractor"
- }
- }
- }
-
- // ── Test 7: getLatestRev should return current revision ─────────────
-
- it should "getLatestRev should return current revision" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- writeApi = new ArangoGraphWriteApi(client)
- queryApi = new ArangoGraphQueryApi(client)
- nodeId = NodeId(UUID.randomUUID())
- // Before any commits, rev should be 0
- rev0 <- queryApi.getLatestRev
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(nodeId, NodeKind.Module, "mod1", Map.empty[String, Json])
- )
- )
- res <- writeApi.commitPatch(patch)
- rev1 <- queryApi.getLatestRev
- } yield {
- rev0 shouldBe Rev(0L)
- rev1 shouldBe res.newRev
- rev1.value should be > 0L
- }
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/db/GraphWriteApiSpec.scala b/memory-layer/src/test/scala/ix/memory/db/GraphWriteApiSpec.scala
deleted file mode 100644
index c4a32e53..00000000
--- a/memory-layer/src/test/scala/ix/memory/db/GraphWriteApiSpec.scala
+++ /dev/null
@@ -1,222 +0,0 @@
-package ix.memory.db
-
-import java.time.Instant
-import java.util.UUID
-
-import cats.effect.IO
-import cats.effect.testing.scalatest.AsyncIOSpec
-import io.circe.Json
-import org.scalatest.flatspec.AsyncFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import ix.memory.TestDbHelper
-import ix.memory.model._
-
-class GraphWriteApiSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers with TestDbHelper {
-
- val clientResource = ArangoClient.resource(
- host = "localhost", port = 8529,
- database = "ix_test_graph_write", user = "root", password = ""
- )
-
- private def makePatch(
- baseRev: Rev = Rev(0L),
- ops: Vector[PatchOp] = Vector.empty,
- patchId: PatchId = PatchId(UUID.randomUUID())
- ): GraphPatch =
- GraphPatch(
- patchId = patchId,
- actor = "test-actor",
- timestamp = Instant.parse("2025-06-01T12:00:00Z"),
- source = PatchSource(
- uri = "test://source",
- sourceHash = Some("hash123"),
- extractor = "test-extractor",
- sourceType = SourceType.Code
- ),
- baseRev = baseRev,
- ops = ops,
- replaces = Vector.empty,
- intent = Some("test intent")
- )
-
- // ── Test 1: commit a patch and increment revision ──────────────────
-
- "GraphWriteApi" should "commit a patch and increment revision" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- api = new ArangoGraphWriteApi(client)
- nodeId = NodeId(UUID.randomUUID())
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId,
- kind = NodeKind.Function,
- name = "testFunc",
- attrs = Map("lang" -> Json.fromString("scala"))
- )
- )
- )
- result <- api.commitPatch(patch)
- } yield {
- result.status shouldBe CommitStatus.Ok
- result.newRev.value should be > 0L
- }
- }
- }
-
- // ── Test 2: be idempotent on duplicate patch_id ────────────────────
-
- it should "be idempotent on duplicate patch_id" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- api = new ArangoGraphWriteApi(client)
- nodeId = NodeId(UUID.randomUUID())
- patchId = PatchId(UUID.randomUUID())
- patch = makePatch(
- patchId = patchId,
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId,
- kind = NodeKind.Module,
- name = "testModule",
- attrs = Map.empty[String, Json]
- )
- )
- )
- result1 <- api.commitPatch(patch)
- result2 <- api.commitPatch(patch)
- } yield {
- result1.status shouldBe CommitStatus.Ok
- result2.status shouldBe CommitStatus.Idempotent
- result2.newRev shouldBe result1.newRev
- }
- }
- }
-
- // ── Test 3: reject on base_rev mismatch ────────────────────────────
-
- it should "reject on base_rev mismatch" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- api = new ArangoGraphWriteApi(client)
- nodeId1 = NodeId(UUID.randomUUID())
- // First patch to establish rev 1
- patch1 = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId1,
- kind = NodeKind.Service,
- name = "svc1",
- attrs = Map.empty[String, Json]
- )
- )
- )
- _ <- api.commitPatch(patch1)
- // Second patch with wrong baseRev
- nodeId2 = NodeId(UUID.randomUUID())
- patch2 = makePatch(
- baseRev = Rev(999L),
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId2,
- kind = NodeKind.Service,
- name = "svc2",
- attrs = Map.empty[String, Json]
- )
- )
- )
- result <- api.commitPatch(patch2)
- } yield {
- result.status shouldBe CommitStatus.BaseRevMismatch
- }
- }
- }
-
- // ── Test 4: persist node visible via query ─────────────────────────
-
- it should "persist node visible via query" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- api = new ArangoGraphWriteApi(client)
- nodeId = NodeId(UUID.randomUUID())
- patch = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId,
- kind = NodeKind.File,
- name = "Main.scala",
- attrs = Map("path" -> Json.fromString("/src/Main.scala"))
- )
- )
- )
- _ <- api.commitPatch(patch)
- result <- client.queryOne(
- "FOR n IN nodes FILTER n.logical_id == @id AND n.deleted_rev == null RETURN n",
- Map("id" -> nodeId.value.toString.asInstanceOf[AnyRef])
- )
- } yield {
- result shouldBe defined
- val doc = result.get
- doc.hcursor.get[String]("kind") shouldBe Right("file")
- doc.hcursor.get[String]("name") shouldBe Right("Main.scala")
- // attrs should contain the path attribute
- doc.hcursor.downField("attrs").downField("path").as[String] shouldBe Right("/src/Main.scala")
- // deleted_rev should be null (not soft-deleted)
- doc.hcursor.get[Option[Long]]("deleted_rev") shouldBe Right(None)
- }
- }
- }
-
- // ── Test 5: soft delete node via MVCC ──────────────────────────────
-
- it should "soft delete node via MVCC" in {
- clientResource.use { client =>
- for {
- _ <- client.ensureSchema()
- _ <- cleanDatabase(client)
- api = new ArangoGraphWriteApi(client)
- nodeId = NodeId(UUID.randomUUID())
- // First patch: create the node
- patch1 = makePatch(
- ops = Vector(
- PatchOp.UpsertNode(
- id = nodeId,
- kind = NodeKind.Class,
- name = "MyClass",
- attrs = Map.empty[String, Json]
- )
- )
- )
- res1 <- api.commitPatch(patch1)
- // Second patch: soft delete the node
- patch2 = makePatch(
- baseRev = res1.newRev,
- ops = Vector(PatchOp.DeleteNode(nodeId))
- )
- res2 <- api.commitPatch(patch2)
- // Query the node to verify soft delete (find by logical_id, expect deleted_rev set)
- result <- client.queryOne(
- "FOR n IN nodes FILTER n.logical_id == @id AND n.deleted_rev != null RETURN n",
- Map("id" -> nodeId.value.toString.asInstanceOf[AnyRef])
- )
- } yield {
- res1.status shouldBe CommitStatus.Ok
- res2.status shouldBe CommitStatus.Ok
- res2.newRev.value shouldBe res1.newRev.value + 1
-
- result shouldBe defined
- val doc = result.get
- doc.hcursor.get[Long]("deleted_rev") shouldBe Right(res2.newRev.value)
- }
- }
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/db/SearchAqlSpec.scala b/memory-layer/src/test/scala/ix/memory/db/SearchAqlSpec.scala
deleted file mode 100644
index 6fb3fdc3..00000000
--- a/memory-layer/src/test/scala/ix/memory/db/SearchAqlSpec.scala
+++ /dev/null
@@ -1,51 +0,0 @@
-package ix.memory.db
-
-import org.scalatest.flatspec.AnyFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-/**
- * Verify that search AQL queries use Arango-compatible functions.
- * COALESCE() is SQL — ArangoDB uses NOT_NULL() or direct field access.
- */
-class SearchAqlSpec extends AnyFlatSpec with Matchers {
-
- // Read the source file to inspect AQL strings
- private val sourceFile = scala.io.Source.fromFile(
- "memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala"
- ).mkString
-
- "ArangoGraphQueryApi AQL" should "not use COALESCE (not valid AQL)" in {
- sourceFile should not include "COALESCE"
- }
-
- it should "embed _search_weight into attrs via MERGE" in {
- sourceFile should include ("_search_weight: entry.weight")
- sourceFile should include ("MERGE(n2.attrs, { _search_weight")
- }
-
- it should "use weighted scoring in nameOnly mode" in {
- // nameOnly AQL should still return weight
- sourceFile should include ("LOWER(n.name) == LOWER(@text) ? 100 : 60")
- }
-
- it should "use weighted scoring in full mode with multiple match tiers" in {
- // Full AQL should have name (100/60), provenance (40), claim (20), decision (20), attrs (10)
- sourceFile should include ("weight: 40")
- sourceFile should include ("weight: 20")
- sourceFile should include ("weight: 10")
- }
-
- it should "gate attr_matches on minimum term length to avoid short-term noise" in {
- // The code should check text.length >= 6 before including attr_matches
- sourceFile should include ("text.length >= 6")
- sourceFile should include ("LET attr_matches = []")
- }
-
- it should "include filePath in edge ID generation for cross-file uniqueness" in {
- // Read the ingestion patch builder source to verify edge IDs include filePath
- val gpbSource = scala.io.Source.fromFile(
- "core-ingestion/src/patch-builder.ts"
- ).mkString
- gpbSource should include ("`${normalizePath(filePath)}:${src}:${dst}:${predicate}`")
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/map/MapServiceScopeSpec.scala b/memory-layer/src/test/scala/ix/memory/map/MapServiceScopeSpec.scala
deleted file mode 100644
index 175ac4e7..00000000
--- a/memory-layer/src/test/scala/ix/memory/map/MapServiceScopeSpec.scala
+++ /dev/null
@@ -1,196 +0,0 @@
-package ix.memory.map
-
-import java.util.UUID
-
-import org.scalatest.flatspec.AnyFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-import ix.memory.model.NodeId
-
-class MapServiceScopeSpec extends AnyFlatSpec with Matchers {
-
- private val service = new MapService(null, null, null)
-
- "resolveRegion" should "order ambiguous matches by match quality, level preference, and significance" in {
- val context = region("Context", "system", 3, files = 120)
- val apiSubsystem = region("Api", "subsystem", 2, files = 35, parent = Some(context))
- val apiModule = region("Api", "module", 1, files = 12, parent = Some(context))
- val apiClient = region("Api / Client", "system", 3, files = 90)
-
- val map = ArchitectureMap(
- regions = Vector(context, apiSubsystem, apiModule, apiClient),
-
- fileCount = 257,
- mapRev = 42L
- )
-
- val matches = service.resolveRegion(map, "api")
-
- matches.map(_.region.label) shouldBe Vector("Api", "Api", "Api / Client")
- matches.map(_.region.labelKind) shouldBe Vector("subsystem", "module", "system")
- }
-
- "scopedView" should "summarize subtree health while returning only direct children" in {
- val system = region("Cli", "system", 3, files = 140, confidence = 0.82)
- val subsystem = region("Cli / Client", "subsystem", 2, files = 105, confidence = 0.72, parent = Some(system))
- val moduleA = region("Cli / Client", "module", 1, files = 53, confidence = 0.91, parent = Some(subsystem))
- val moduleB = region("Queries", "module", 1, files = 10, confidence = 0.42, crosscut = 0.18, parent = Some(subsystem))
- val moduleC = region("Github", "module", 1, files = 22, confidence = 0.63, parent = Some(subsystem))
-
- val map = ArchitectureMap(
- regions = Vector(system, subsystem, moduleA, moduleB, moduleC),
-
- fileCount = 140,
- mapRev = 42L
- )
-
- val view = service.scopedView(map, subsystem)
-
- view.parent.map(_.label) shouldBe Some("Cli")
- view.children.map(_.label) shouldBe Vector("Cli / Client", "Github", "Queries")
- view.summary.wellDefined shouldBe 1
- view.summary.moderate shouldBe 2
- view.summary.fuzzy shouldBe 1
- view.summary.crossCutting shouldBe 1
- }
-
- "collapseDuplicateHierarchyLevels" should "keep only the coarsest region for repeated member sets" in {
- val sharedId = NodeId(UUID.randomUUID())
- val sharedFiles = (1 to 10).map(_ => NodeId(UUID.randomUUID())).toSet
- val child = region("Edges / Resolver", "module", 1, files = 4, parentId = Some(sharedId))
- val module = regionWithMembers("Edges", "module", 1, sharedId, sharedFiles)
- val subsystem = regionWithMembers("Edges", "subsystem", 2, sharedId, sharedFiles)
- val system = regionWithMembers("Edges", "system", 3, sharedId, sharedFiles)
-
- val collapsed = service.collapseDuplicateHierarchyLevels(Vector(child, module, subsystem, system))
-
- collapsed.map(region => region.label -> region.labelKind) shouldBe Vector(
- "Edges / Resolver" -> "module",
- "Edges" -> "system"
- )
- collapsed.map(_.id) should contain (sharedId)
- collapsed.find(_.label == "Edges / Resolver").flatMap(_.parentId) shouldBe Some(sharedId)
- }
-
- "appendSingletonRegionsForUncoveredFiles" should "create module regions for files left out by clustering" in {
- val fileA = fileVertex("C:/repo/src/core/service.ts")
- val fileB = fileVertex("C:/repo/infra/Dockerfile")
- val graph = WeightedFileGraph(
- vertices = Vector(fileA, fileB),
- adjMatrix = Map(fileA.id -> Map.empty, fileB.id -> Map.empty),
- degrees = Map(fileA.id -> 0.0, fileB.id -> 0.0),
- totalWeight = 0.0,
- predicatePairs = Map.empty
- )
- val existing = regionWithMembers("Core", "module", 1, NodeId(UUID.randomUUID()), Set(fileA.id))
-
- val completed = service.appendSingletonRegionsForUncoveredFiles(
- regions = Vector(existing),
- graph = graph,
- fileVertexById = graph.vertices.map(v => v.id -> v).toMap,
- crosscutScores = Map.empty,
- signalAdjacency = Map.empty,
- rev = ix.memory.model.Rev(42L)
- )
-
- completed.map(_.memberFiles) should contain (Set(fileB.id))
- completed.find(_.memberFiles == Set(fileB.id)).map(_.labelKind) shouldBe Some("module")
- }
-
- it should "emit exactly one edge when a file belongs to multiple level-1 regions" in {
- val file = NodeId(UUID.randomUUID())
- val moduleId = NodeId(UUID.randomUUID())
- val subsystemId = NodeId(UUID.randomUUID())
-
- // Both regions are level 1 — old code emitted two IN_REGION edges; new code emits one.
- val moduleRegion = regionWithMembers("Lib", "module", 1, moduleId, Set(file))
- val subsystemRegion = regionWithMembers("Lib", "subsystem", 1, subsystemId, Set(file))
-
- val edgeDocs = service.buildRegionEdgeDocs(
- regions = Vector(moduleRegion, subsystemRegion),
- newRev = 42L,
- now = "2026-04-01T00:00:00Z",
- provMap = new java.util.HashMap[String, AnyRef]()
- )
-
- val fileEdges = edgeDocs.filter(_.get("dst").toString == file.value.toString)
-
- // Exactly one edge — no duplicates from the two level-1 regions.
- fileEdges should have length 1
- // "module" sorts before "subsystem" so the module region wins.
- fileEdges.head.get("src").toString shouldBe moduleId.value.toString
- }
-
- "buildRegionEdgeDocs" should "attach files to their most specific surviving region" in {
- val fileA = NodeId(UUID.randomUUID())
- val fileB = NodeId(UUID.randomUUID())
- val sharedRegionId = NodeId(UUID.randomUUID())
- val childRegionId = NodeId(UUID.randomUUID())
- val systemRegionId = NodeId(UUID.randomUUID())
-
- val module = regionWithMembers("Dist", "module", 1, childRegionId, Set(fileA))
- val system = regionWithMembers("Countries", "system", 2, systemRegionId, Set(fileA, fileB))
- val collapsedSystem = regionWithMembers("Countries", "system", 2, sharedRegionId, Set(fileA))
-
- val edgeDocs = service.buildRegionEdgeDocs(
- regions = Vector(system, collapsedSystem, module),
- newRev = 42L,
- now = "2026-04-01T00:00:00Z",
- provMap = new java.util.HashMap[String, AnyRef]()
- )
-
- val fileEdges = edgeDocs.filter { doc =>
- val dst = doc.get("dst").toString
- dst == fileA.value.toString || dst == fileB.value.toString
- }
- val fileEdgeByDst = fileEdges.map(doc => doc.get("dst").toString -> doc.get("src").toString).toMap
-
- fileEdgeByDst(fileA.value.toString) shouldBe childRegionId.value.toString
- fileEdgeByDst(fileB.value.toString) shouldBe systemRegionId.value.toString
- }
-
- private def fileVertex(path: String): FileVertex =
- FileVertex(NodeId(UUID.randomUUID()), path)
-
- private def region(
- label: String,
- labelKind: String,
- level: Int,
- files: Int,
- confidence: Double = 0.80,
- crosscut: Double = 0.0,
- parent: Option[Region] = None,
- parentId: Option[NodeId] = None,
- ): Region = {
- regionWithMembers(label, labelKind, level, NodeId(UUID.randomUUID()), (1 to files).map(_ => NodeId(UUID.randomUUID())).toSet, confidence, crosscut, parent.map(_.id).orElse(parentId))
- }
-
- private def regionWithMembers(
- label: String,
- labelKind: String,
- level: Int,
- id: NodeId,
- memberFiles: Set[NodeId],
- confidence: Double = 0.80,
- crosscut: Double = 0.0,
- parentId: Option[NodeId] = None,
- ): Region = {
- Region(
- id = id,
- label = label,
- labelKind = labelKind,
- level = level,
- memberFiles = memberFiles,
- childRegionIds = Set.empty,
- parentId = parentId,
- cohesion = 0.5,
- externalCoupling = 0.2,
- boundaryRatio = 1.5,
- confidence = confidence,
- crosscutScore = crosscut,
- dominantSignals = List("calls", "imports"),
- interfaceNodeCount = 1,
- mapRev = 42L
- )
- }
-}
diff --git a/memory-layer/src/test/scala/ix/memory/model/ModelSpec.scala b/memory-layer/src/test/scala/ix/memory/model/ModelSpec.scala
deleted file mode 100644
index 5a9ff034..00000000
--- a/memory-layer/src/test/scala/ix/memory/model/ModelSpec.scala
+++ /dev/null
@@ -1,317 +0,0 @@
-package ix.memory.model
-
-import java.time.Instant
-import java.util.UUID
-
-import io.circe._
-import io.circe.parser._
-import io.circe.syntax._
-import org.scalatest.flatspec.AnyFlatSpec
-import org.scalatest.matchers.should.Matchers
-
-class ModelSpec extends AnyFlatSpec with Matchers {
-
- // ── Identifiers round-trip ──────────────────────────────────────────
-
- "Identifiers" should "round-trip NodeId through JSON" in {
- val id = NodeId(UUID.randomUUID())
- decode[NodeId](id.asJson.noSpaces) shouldBe Right(id)
- }
-
- it should "round-trip EdgeId through JSON" in {
- val id = EdgeId(UUID.randomUUID())
- decode[EdgeId](id.asJson.noSpaces) shouldBe Right(id)
- }
-
- it should "round-trip PatchId through JSON" in {
- val id = PatchId(UUID.randomUUID())
- decode[PatchId](id.asJson.noSpaces) shouldBe Right(id)
- }
-
- it should "round-trip ClaimId through JSON" in {
- val id = ClaimId(UUID.randomUUID())
- decode[ClaimId](id.asJson.noSpaces) shouldBe Right(id)
- }
-
- it should "round-trip ConflictId through JSON" in {
- val id = ConflictId(UUID.randomUUID())
- decode[ConflictId](id.asJson.noSpaces) shouldBe Right(id)
- }
-
- it should "round-trip Rev through JSON" in {
- val rev = Rev(42L)
- decode[Rev](rev.asJson.noSpaces) shouldBe Right(rev)
- }
-
- // ── SourceType ──────────────────────────────────────────────────────
-
- "SourceType" should "encode and decode all variants correctly" in {
- val variants: List[SourceType] = List(
- SourceType.Code,
- SourceType.Config,
- SourceType.Doc,
- SourceType.Test,
- SourceType.Schema,
- SourceType.Commit,
- SourceType.Comment,
- SourceType.Inferred,
- SourceType.Human
- )
-
- variants.foreach { st =>
- val json = st.asJson
- json.isString shouldBe true
- decode[SourceType](json.noSpaces) shouldBe Right(st)
- }
- }
-
- // ── NodeKind ────────────────────────────────────────────────────────
-
- "NodeKind" should "encode and decode all variants correctly" in {
- val variants: List[NodeKind] = List(
- NodeKind.Module,
- NodeKind.File,
- NodeKind.Class,
- NodeKind.Function,
- NodeKind.Variable,
- NodeKind.Config,
- NodeKind.ConfigEntry,
- NodeKind.Service,
- NodeKind.Endpoint,
- NodeKind.Intent,
- NodeKind.Decision,
- NodeKind.Doc,
- NodeKind.Object,
- NodeKind.Trait,
- NodeKind.Interface,
- NodeKind.Method
- )
-
- variants.foreach { nk =>
- val json = nk.asJson
- json.isString shouldBe true
- decode[NodeKind](json.noSpaces) shouldBe Right(nk)
- }
- }
-
- it should "include Intent and Decision" in {
- NodeKind.decoder.decodeJson(Json.fromString("intent")) shouldBe Right(NodeKind.Intent)
- NodeKind.decoder.decodeJson(Json.fromString("decision")) shouldBe Right(NodeKind.Decision)
- }
-
- // ── ClaimStatus ─────────────────────────────────────────────────────
-
- "ClaimStatus" should "encode and decode all variants" in {
- val variants: List[ClaimStatus] = List(
- ClaimStatus.Active,
- ClaimStatus.Stale,
- ClaimStatus.Retracted
- )
-
- variants.foreach { cs =>
- val json = cs.asJson
- json.isString shouldBe true
- decode[ClaimStatus](json.noSpaces) shouldBe Right(cs)
- }
- }
-
- // ── Provenance round-trip ───────────────────────────────────────────
-
- "Provenance" should "round-trip through JSON" in {
- val prov = Provenance(
- sourceUri = "git://repo/file.scala",
- sourceHash = Some("abc123"),
- extractor = "tree-sitter",
- sourceType = SourceType.Code,
- observedAt = Instant.parse("2025-01-01T00:00:00Z")
- )
-
- val json = prov.asJson
- val decoded = decode[Provenance](json.noSpaces)
- decoded shouldBe Right(prov)
- }
-
- // ── GraphPatch serialization ────────────────────────────────────────
-
- "GraphPatch" should "serialize with all fields" in {
- val patchId = PatchId(UUID.randomUUID())
- val nodeId = NodeId(UUID.randomUUID())
- val edgeId = EdgeId(UUID.randomUUID())
- val claimId = ClaimId(UUID.randomUUID())
-
- val dstId = NodeId(UUID.randomUUID())
-
- val ops: Vector[PatchOp] = Vector(
- PatchOp.UpsertNode(
- id = nodeId,
- kind = NodeKind.Function,
- name = "foo",
- attrs = Map("lang" -> Json.fromString("scala"))
- ),
- PatchOp.UpsertEdge(
- id = edgeId,
- src = nodeId,
- dst = dstId,
- predicate = EdgePredicate("CALLS"),
- attrs = Map.empty[String, Json]
- ),
- PatchOp.DeleteNode(nodeId),
- PatchOp.DeleteEdge(edgeId),
- PatchOp.AssertClaim(
- entityId = nodeId,
- field = "purity",
- value = Json.fromString("pure"),
- confidence = Some(0.9)
- ),
- PatchOp.RetractClaim(claimId)
- )
-
- val patch = GraphPatch(
- patchId = patchId,
- actor = "extractor:tree-sitter",
- timestamp = Instant.parse("2025-06-01T12:00:00Z"),
- source = PatchSource(
- uri = "git://repo",
- sourceHash = Some("abc123"),
- extractor = "tree-sitter",
- sourceType = SourceType.Code
- ),
- baseRev = Rev(0L),
- ops = ops,
- replaces = Vector.empty,
- intent = Some("initial extraction")
- )
-
- val json = patch.asJson
- val decoded = decode[GraphPatch](json.noSpaces)
- decoded shouldBe Right(patch)
-
- // Verify all top-level fields are present
- val cursor = json.hcursor
- cursor.downField("patchId").succeeded shouldBe true
- cursor.downField("actor").succeeded shouldBe true
- cursor.downField("timestamp").succeeded shouldBe true
- cursor.downField("source").succeeded shouldBe true
- cursor.downField("baseRev").succeeded shouldBe true
- cursor.downField("ops").succeeded shouldBe true
- cursor.downField("intent").succeeded shouldBe true
- }
-
- // ── ConfidenceBreakdown.score ───────────────────────────────────────
-
- "ConfidenceBreakdown" should "compute score correctly: 0.90 * 1.1 * 1.0 * 1.1 * 0.85 * 1.0 = ~0.926" in {
- val breakdown = ConfidenceBreakdown(
- baseAuthority = Factor(0.90, "code source"),
- verification = Factor(1.1, "test-verified"),
- recency = Factor(1.0, "recent commit"),
- corroboration = Factor(1.1, "multiple extractors agree"),
- conflictPenalty = Factor(0.85, "minor conflict"),
- intentAlignment = Factor(1.0, "no connection to intent")
- )
-
- // 0.90 * 1.1 * 1.0 * 1.1 * 0.85 * 1.0 = 0.92565
- breakdown.score shouldBe 0.926 +- 0.001
- }
-
- it should "clamp score to 1.0 when product exceeds 1.0" in {
- val breakdown = ConfidenceBreakdown(
- baseAuthority = Factor(1.0, "max"),
- verification = Factor(1.5, "highly verified"),
- recency = Factor(1.0, "fresh"),
- corroboration = Factor(1.5, "strong agreement"),
- conflictPenalty = Factor(1.0, "no conflict"),
- intentAlignment = Factor(1.0, "no connection to intent")
- )
-
- breakdown.score shouldBe 1.0
- }
-
- it should "clamp score to 0.0 when product is negative" in {
- val breakdown = ConfidenceBreakdown(
- baseAuthority = Factor(0.0, "unknown"),
- verification = Factor(1.0, "none"),
- recency = Factor(1.0, "none"),
- corroboration = Factor(1.0, "none"),
- conflictPenalty = Factor(1.0, "none"),
- intentAlignment = Factor(1.0, "none")
- )
-
- breakdown.score shouldBe 0.0
- }
-
- it should "round-trip through JSON" in {
- val breakdown = ConfidenceBreakdown(
- baseAuthority = Factor(0.90, "code source"),
- verification = Factor(1.1, "test-verified"),
- recency = Factor(1.0, "recent commit"),
- corroboration = Factor(1.1, "multiple extractors agree"),
- conflictPenalty = Factor(0.85, "minor conflict"),
- intentAlignment = Factor(1.0, "no connection to intent")
- )
-
- val json = breakdown.asJson
- val decoded = decode[ConfidenceBreakdown](json.noSpaces)
- decoded shouldBe Right(breakdown)
- }
-
- it should "produce compact confidence when all multipliers are trivial" in {
- val trivial = ConfidenceBreakdown(
- baseAuthority = Factor(0.9, "Code, tree-sitter-python/1.0"),
- verification = Factor(1.0, "no verification data available"),
- recency = Factor(1.0, "Source unchanged since observation"),
- corroboration = Factor(1.0, "0 corroborating sources"),
- conflictPenalty = Factor(1.0, "no conflicts"),
- intentAlignment = Factor(1.0, "No connection to intent")
- )
- val compact = trivial.toCompact
- compact.score shouldBe trivial.score
- compact.authority shouldBe Some(0.9)
- compact.nonTrivialFactors shouldBe empty
- }
-
- it should "include non-trivial factors in compact confidence" in {
- val interesting = ConfidenceBreakdown(
- baseAuthority = Factor(0.9, "Code, tree-sitter-python/1.0"),
- verification = Factor(1.0, "no verification data available"),
- recency = Factor(0.7, "1-6 months since observation"),
- corroboration = Factor(1.1, "3 corroborating sources"),
- conflictPenalty = Factor(1.0, "no conflicts"),
- intentAlignment = Factor(1.0, "No connection to intent")
- )
- val compact = interesting.toCompact
- compact.score shouldBe interesting.score
- compact.nonTrivialFactors should have size 2
- compact.nonTrivialFactors.map(_._1) should contain allOf ("recency", "corroboration")
- }
-
- // ── CompactScoredClaim ────────────────────────────────────────────
-
- "ScoredClaim" should "produce compact scored claim with summary fields" in {
- import java.time.Instant
- val claim = Claim(
- id = ClaimId(UUID.randomUUID()),
- entityId = NodeId(UUID.randomUUID()),
- statement = "content",
- value = Json.fromString("a very long content blob that should not appear in compact mode but this is just test data"),
- confidence = Some(0.9),
- status = ClaimStatus.Active,
- provenance = Provenance("src/main/Foo.scala", Some("abc123"), "tree-sitter/1.0", SourceType.Code, Instant.now()),
- createdRev = Rev(1L),
- deletedRev = None
- )
- val breakdown = ConfidenceBreakdown(
- baseAuthority = Factor(0.9, "Code"),
- verification = Factor(1.0, "n/a"),
- recency = Factor(1.0, "fresh"),
- corroboration = Factor(1.0, "single"),
- conflictPenalty = Factor(1.0, "none"),
- intentAlignment = Factor(1.0, "none")
- )
- val scored = ScoredClaim(claim, breakdown, 1.0, 0.9)
- val compact = scored.toCompact
- compact.entityId shouldBe claim.entityId
- compact.field shouldBe "content"
- compact.score shouldBe 0.9
- compact.path shouldBe Some("src/main/Foo.scala")
- }
-}
diff --git a/project/build.properties b/project/build.properties
deleted file mode 100644
index 73df629a..00000000
--- a/project/build.properties
+++ /dev/null
@@ -1 +0,0 @@
-sbt.version=1.10.7
diff --git a/project/plugins.sbt b/project/plugins.sbt
deleted file mode 100644
index 11fa359e..00000000
--- a/project/plugins.sbt
+++ /dev/null
@@ -1 +0,0 @@
-addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.0")
diff --git a/scripts/backend.sh b/scripts/backend.sh
index 60006d87..56873d34 100755
--- a/scripts/backend.sh
+++ b/scripts/backend.sh
@@ -4,8 +4,8 @@ set -euo pipefail
# ─────────────────────────────────────────────────────────────────────────────
# IX-Memory — Backend Setup
#
-# Builds the Memory Layer JAR (if needed) and starts ArangoDB + Memory Layer
-# via Docker Compose.
+# Starts ArangoDB + Memory Layer via Docker Compose using the published
+# Docker image from ghcr.io.
#
# Usage:
# ./scripts/backend.sh # Start backend (default)
@@ -14,7 +14,7 @@ set -euo pipefail
# ./scripts/backend.sh status # Show service status
# ./scripts/backend.sh logs # Tail service logs
# ./scripts/backend.sh clean # Stop + remove data volumes
-# ./scripts/backend.sh rebuild # Force rebuild JAR + restart
+# ./scripts/backend.sh restart # Restart services
# ./scripts/backend.sh check # Just check if backend is healthy
# ─────────────────────────────────────────────────────────────────────────────
@@ -33,15 +33,15 @@ if [[ "$(uname -s)" =~ MINGW|MSYS|CYGWIN ]]; then
export IX_CONTAINER_MOUNT_ROOT="${HOME}"
dc() {
MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*' \
- docker compose -f "$(cygpath -m "$IX_DIR/docker-compose.yml")" "$@"
+ docker compose -f "$(cygpath -m "$IX_DIR/$COMPOSE_FILE")" "$@"
}
else
export IX_HOST_MOUNT_ROOT="${HOME}"
export IX_CONTAINER_MOUNT_ROOT="${HOME}"
- dc() { docker compose "$@"; }
+ dc() { docker compose -f "$COMPOSE_FILE" "$@"; }
fi
-JAR_PATH="memory-layer/target/scala-2.13/ix-memory-layer.jar"
+COMPOSE_FILE="docker-compose.standalone.yml"
HEALTH_URL="http://localhost:8090/v1/health"
ARANGO_URL="http://localhost:8529/_api/version"
@@ -63,7 +63,6 @@ ensure_docker() {
echo "Docker is not running. Attempting to start..."
if [ "$(uname)" = "Darwin" ]; then
- # macOS: try Docker Desktop
if [ -d "/Applications/Docker.app" ]; then
open -a Docker
echo " Launched Docker Desktop, waiting for it to start..."
@@ -76,7 +75,6 @@ ensure_docker() {
exit 1
fi
elif [ "$(uname)" = "Linux" ]; then
- # Linux: try systemctl
if command -v systemctl &> /dev/null; then
echo " Running: sudo systemctl start docker"
sudo systemctl start docker
@@ -91,7 +89,6 @@ ensure_docker() {
exit 1
fi
- # Wait for Docker to be ready
for i in $(seq 1 30); do
if docker info &> /dev/null 2>&1; then
echo "[ok] Docker is now running"
@@ -107,22 +104,6 @@ ensure_docker() {
exit 1
}
-build_jar() {
- local force="${1:-false}"
- if [ "$force" = "true" ] || [ ! -f "$JAR_PATH" ]; then
- if ! command -v sbt &> /dev/null; then
- echo "Error: sbt is not installed."
- echo " Install: https://www.scala-sbt.org/download.html"
- exit 1
- fi
- echo "Building Memory Layer JAR..."
- sbt "memoryLayer/assembly" 2>&1 | tail -5
- echo "[ok] JAR built: $JAR_PATH"
- else
- echo "[ok] JAR already exists (use 'rebuild' to force)"
- fi
-}
-
wait_for_health() {
echo "Waiting for services to become healthy..."
for i in $(seq 1 30); do
@@ -157,7 +138,6 @@ containers_running() {
case "${1:-up}" in
up)
- # If backend is already healthy, skip everything
if is_healthy; then
echo "[ok] Backend is already running and healthy"
echo " Memory Layer: http://localhost:8090"
@@ -166,7 +146,6 @@ case "${1:-up}" in
fi
ensure_docker
- build_jar
# If containers are running but not healthy, restart them
if containers_running; then
@@ -174,7 +153,7 @@ case "${1:-up}" in
dc restart
else
echo "Starting backend services..."
- dc up -d --build
+ dc up -d --pull always
fi
wait_for_health
@@ -199,12 +178,9 @@ case "${1:-up}" in
dc down -v
echo "[ok] Backend stopped and data volumes removed."
;;
- rebuild)
- ensure_docker
- build_jar true
- echo "Rebuilding and starting backend..."
- dc up -d --build
- wait_for_health
+ restart)
+ dc restart
+ echo "[ok] Backend restarted."
;;
check)
if is_healthy; then
@@ -219,12 +195,12 @@ case "${1:-up}" in
echo "Usage: ./scripts/backend.sh [COMMAND]"
echo ""
echo "Commands:"
- echo " up Build JAR (if needed) and start services (default)"
+ echo " up Start services using published Docker image (default)"
echo " down Stop all services"
echo " status Show service status and health"
echo " logs Tail service logs"
echo " clean Stop services and remove data volumes"
- echo " rebuild Force rebuild JAR and restart services"
+ echo " restart Restart services"
echo " check Check if backend is healthy (exit 0/1)"
exit 1
;;
diff --git a/scripts/dev/dev.sh b/scripts/dev/dev.sh
deleted file mode 100755
index 57047dd2..00000000
--- a/scripts/dev/dev.sh
+++ /dev/null
@@ -1,51 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-cd "$SCRIPT_DIR"
-
-case "${1:-up}" in
- up)
- if ! command -v docker &> /dev/null; then
- echo "Error: Docker is not installed."
- exit 1
- fi
- if ! docker info &> /dev/null 2>&1; then
- echo "Error: Docker is not running."
- exit 1
- fi
-
- echo "Starting ArangoDB..."
- docker compose up -d arangodb
-
- echo "Waiting for ArangoDB..."
- for i in $(seq 1 20); do
- if curl -sf http://localhost:8529/_api/version > /dev/null 2>&1; then
- echo "ArangoDB is ready at http://localhost:8529"
- break
- fi
- sleep 1
- done
-
- echo ""
- echo "Starting Memory Layer via sbt..."
- ARANGO_HOST=localhost \
- ARANGO_PORT=8529 \
- ARANGO_DATABASE=ix_memory \
- ARANGO_USER=root \
- ARANGO_PASSWORD="" \
- PORT=8090 \
- sbt "memoryLayer/run"
- ;;
- stop)
- docker compose stop arangodb
- echo "ArangoDB stopped."
- ;;
- *)
- echo "Usage: ./dev.sh [up|stop]"
- echo ""
- echo " up Start ArangoDB in Docker + Memory Layer via sbt (default)"
- echo " stop Stop ArangoDB container"
- exit 1
- ;;
-esac
diff --git a/scripts/dev/ix-smoke-test.sh b/scripts/dev/ix-smoke-test.sh
deleted file mode 100755
index 33977648..00000000
--- a/scripts/dev/ix-smoke-test.sh
+++ /dev/null
@@ -1,330 +0,0 @@
-#!/usr/bin/env bash
-# ix-smoke-test.sh
-#
-# Smoke test for Ix-Memory fixes:
-# - Claim lifecycle: old claims retired (deletedRev set) when new ones added
-# - Search indexes: name + provenance + claims (statement/value)
-# - Diff works on the correct entity and shows content change
-#
-# Assumptions:
-# - Run from repo root (Ix)
-# - docker + docker compose available
-# - sbt available
-# - node + npm available
-# - backend listens on http://localhost:8090, arango on http://localhost:8529
-#
-# Notes:
-# - This script starts memory-layer in the background and cleans up on exit.
-# - It uses python3 for JSON assertions (no jq needed).
-
-
-set -euo pipefail
-
-# Enable verbose tracing by setting IX_TRACE=1
-if [[ "${IX_TRACE:-0}" == "1" ]]; then
- set -x
-fi
-
-on_err() {
- local exit_code=$?
- echo "" >&2
- echo "ERROR: script failed (exit=${exit_code}) at line ${BASH_LINENO[0]}: ${BASH_COMMAND}" >&2
- # Print last 120 lines of the most relevant logs if they exist
- [[ -f "${NPM_LOG:-}" ]] && { echo "--- tail ${NPM_LOG} ---" >&2; tail -n 120 "${NPM_LOG}" >&2 || true; }
- [[ -f "${ML_LOG:-}" ]] && { echo "--- tail ${ML_LOG} ---" >&2; tail -n 120 "${ML_LOG}" >&2 || true; }
- exit "${exit_code}"
-}
-trap on_err ERR
-
-REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-cd "$REPO_ROOT"
-
-# ---------- helpers ----------
-log() { printf "\n\033[1m[%s]\033[0m %s\n" "$(date +%H:%M:%S)" "$*"; }
-die() { echo "ERROR: $*" >&2; exit 1; }
-
-require() {
- command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
-}
-
-wait_http_ok() {
- local url="$1"
- local tries="${2:-60}"
- local sleep_s="${3:-1}"
- for _ in $(seq 1 "$tries"); do
- if curl -sf "$url" >/dev/null 2>&1; then
- return 0
- fi
- sleep "$sleep_s"
- done
- return 1
-}
-
-# Wait for a TCP port to open (no curl dependency)
-wait_tcp_open() {
- local host="$1"
- local port="$2"
- local tries="${3:-60}"
- local sleep_s="${4:-1}"
- for _ in $(seq 1 "$tries"); do
- # bash built-in TCP check (no curl dependency)
- if (exec 3<>"/dev/tcp/${host}/${port}") >/dev/null 2>&1; then
- exec 3>&- 3<&- || true
- return 0
- fi
- sleep "$sleep_s"
- done
- return 1
-}
-
-py_assert() {
- # usage: py_assert '<python expr>' '<json file>'
- local expr="$1"
- local file="$2"
- python3 - "$file" "$expr" <<'PY'
-import json, sys
-path = sys.argv[1]
-expr = sys.argv[2]
-with open(path, "r", encoding="utf-8") as f:
- data = json.load(f)
-# Make "data" available to expression
-SAFE_BUILTINS = {
- "isinstance": isinstance,
- "len": len,
- "any": any,
- "all": all,
- "min": min,
- "max": max,
- "sum": sum,
- "sorted": sorted,
- "dict": dict,
- "list": list,
- "str": str,
- "int": int,
- "float": float,
- "bool": bool,
-}
-ok = eval(expr, {"__builtins__": SAFE_BUILTINS}, {"data": data})
-if not ok:
- raise SystemExit(f"Assertion failed: {expr}\nData: {json.dumps(data, indent=2)[:4000]}")
-PY
-}
-
-# ---------- preflight ----------
-log "Preflight checks"
-require docker
-require curl
-require npm
-require node
-require sbt
-require python3
-
-# ---------- cleanup trap ----------
-ML_PID=""
-TMPDIR=""
-STARTED_COMPOSE=0
-cleanup() {
- set +e
- if [[ -n "${ML_PID}" ]]; then
- log "Stopping memory-layer (pid=${ML_PID})"
- kill "${ML_PID}" >/dev/null 2>&1 || true
- wait "${ML_PID}" >/dev/null 2>&1 || true
- fi
- # Only stop arango if we started it
- if [[ "${STARTED_COMPOSE}" == "1" ]]; then
- if [[ -x "./dev.sh" ]]; then
- log "Stopping ArangoDB via dev.sh"
- ./dev.sh down >/dev/null 2>&1 || true
- else
- log "Stopping docker compose stack"
- docker compose down >/dev/null 2>&1 || true
- fi
- fi
- if [[ -n "${TMPDIR}" && -d "${TMPDIR}" ]]; then
- log "Removing temp dir ${TMPDIR}"
- rm -rf "${TMPDIR}" || true
- fi
-}
-trap cleanup EXIT
-
-# ---------- build ix CLI (local entrypoint; no global npm link) ----------
-log "Building ix CLI"
-[[ -d "ix-cli" ]] || die "Expected ./ix-cli directory at repo root"
-NPM_LOG="/tmp/ix-smoke-npm.$(date +%s).log"
-log "npm logs: ${NPM_LOG}"
-pushd ix-cli >/dev/null
-# Don't hide npm errors; write full output to log
-npm install >"${NPM_LOG}" 2>&1
-npm run build >>"${NPM_LOG}" 2>&1
-# Show a short tail so failures are easier to spot in CI logs
-{ echo "--- npm tail ---"; tail -n 40 "${NPM_LOG}"; } >/dev/null || true
-
-# Run the CLI from the locally built entrypoint to avoid relying on global npm commands
-IX_ENTRY="${REPO_ROOT}/ix-cli/dist/cli/main.js"
-[[ -f "${IX_ENTRY}" ]] || { echo "--- npm tail ---" >&2; tail -n 120 "${NPM_LOG}" >&2 || true; die "Expected CLI entrypoint not found: ${IX_ENTRY}. Did TypeScript build output change?"; }
-
-# Use a bash array so we can call the command safely
-IX_CMD=(node "${IX_ENTRY}")
-
-log "ix CLI ready (local): node ${IX_ENTRY}"
-"${IX_CMD[@]}" --help >/dev/null || die "ix --help failed"
-popd >/dev/null
-
-# ---------- start arango ----------
-log "Starting ArangoDB"
-# Always start ArangoDB in detached mode so this script never blocks.
-# dev.sh may run docker compose in the foreground, so avoid calling it here.
-docker compose up -d arangodb >/dev/null
-STARTED_COMPOSE=1
-
-log "Waiting for ArangoDB on http://localhost:8529/_api/version"
-wait_http_ok "http://localhost:8529/_api/version" 60 1 || die "ArangoDB did not become healthy"
-
-# ---------- start memory-layer ----------
-log "Starting memory-layer (sbt memoryLayer/run) in background"
-ML_LOG="/tmp/ix-memory-layer.$(date +%s).log"
-# Run from repo root to ensure multi-project build works
-# Use nohup-style background with PID capture
-( sbt -no-colors 'memoryLayer/run' ) >"${ML_LOG}" 2>&1 &
-ML_PID=$!
-
-log "memory-layer pid=${ML_PID} log=${ML_LOG}"
-log "Waiting for memory-layer to open TCP port 8090"
-wait_tcp_open "localhost" 8090 90 1 || {
- tail -n 120 "${ML_LOG}" >&2 || true
- die "memory-layer did not open port 8090"
-}
-
-# ---------- create test project ----------
-TMPDIR="$(mktemp -d /tmp/ix-small-test.XXXXXX)"
-log "Creating test project at ${TMPDIR}"
-
-cat >"${TMPDIR}/a.md" <<'EOF'
-# hello
-EOF
-cat >"${TMPDIR}/b.ts" <<'EOF'
-console.log("x")
-EOF
-cat >"${TMPDIR}/c.json" <<'EOF'
-{"k": 1}
-EOF
-
-# ---------- init ----------
-log "Running ix init inside temp project"
-pushd "${TMPDIR}" >/dev/null
-"${IX_CMD[@]}" init >/dev/null || die "ix init failed"
-
-# ---------- ingest #1 ----------
-log "Ingest #1 (temp dir: ${TMPDIR})"
-ING1_JSON="${TMPDIR}/ingest1.json"
-"${IX_CMD[@]}" ingest "${TMPDIR}" --recursive --format json > "${ING1_JSON}" || {
- tail -n 120 "${ML_LOG}" >&2 || true
- die "ix ingest #1 failed"
-}
-
-# Basic sanity: processed 3 files
-py_assert 'isinstance(data, dict) and data.get("filesProcessed", 0) >= 3' "${ING1_JSON}"
-
-# Search "hello" -> should return doc entity
-S_HELLO_1="${TMPDIR}/search_hello_1.json"
-"${IX_CMD[@]}" search "hello" --format json > "${S_HELLO_1}"
-py_assert 'isinstance(data, list) and len(data) >= 1' "${S_HELLO_1}"
-py_assert 'any(x.get("kind")=="doc" and x.get("id") for x in data)' "${S_HELLO_1}"
-
-DOC_ID="$(python3 - <<PY
-import json
-d=json.load(open("${S_HELLO_1}","r"))
-for x in d:
- if x.get("kind")=="doc":
- print(x["id"])
- break
-PY
-)"
-[[ -n "${DOC_ID}" ]] || die "Could not find doc id from search hello"
-
-log "Doc entity id: ${DOC_ID}"
-
-# Search by full path should work after the SEARCH fix is implemented
-S_PATH_1="${TMPDIR}/search_path_1.json"
-"${IX_CMD[@]}" search "${TMPDIR}/a.md" --format json > "${S_PATH_1}" || true
-
-# Soft assertion: if patched search is expected, enforce it. If you want this script
-# to pass on older builds, set REQUIRE_PATH_SEARCH=0.
-REQUIRE_PATH_SEARCH="${REQUIRE_PATH_SEARCH:-1}"
-if [[ "${REQUIRE_PATH_SEARCH}" == "1" ]]; then
- py_assert 'isinstance(data, list) and len(data) >= 1' "${S_PATH_1}"
-fi
-
-# Claims search should work after SEARCH fix: statement ("language") and/or value ("markdown")
-REQUIRE_CLAIM_SEARCH="${REQUIRE_CLAIM_SEARCH:-1}"
-S_LANG_1="${TMPDIR}/search_language_1.json"
-"${IX_CMD[@]}" search "language" --format json > "${S_LANG_1}" || true
-if [[ "${REQUIRE_CLAIM_SEARCH}" == "1" ]]; then
- py_assert 'isinstance(data, list) and len(data) >= 1' "${S_LANG_1}"
-fi
-
-# ---------- modify content + ingest #2 ----------
-log "Modifying a.md and ingest #2"
-echo "zzzxqv123" >> "${TMPDIR}/a.md"
-
-ING2_JSON="${TMPDIR}/ingest2.json"
-"${IX_CMD[@]}" ingest "${TMPDIR}" --recursive --format json > "${ING2_JSON}" || {
- tail -n 120 "${ML_LOG}" >&2 || true
- die "ix ingest #2 failed"
-}
-py_assert 'isinstance(data, dict) and data.get("filesProcessed", 0) >= 1' "${ING2_JSON}"
-
-# Search for new content should work after SEARCH fix (claim/value/attrs indexing)
-REQUIRE_CONTENT_SEARCH="${REQUIRE_CONTENT_SEARCH:-1}"
-S_ZZZ="${TMPDIR}/search_zzz.json"
-"${IX_CMD[@]}" search "zzzxqv123" --format json > "${S_ZZZ}" || true
-if [[ "${REQUIRE_CONTENT_SEARCH}" == "1" ]]; then
- py_assert 'isinstance(data, list) and any(x.get("id")== "'"${DOC_ID}"'" for x in data)' "${S_ZZZ}"
-fi
-
-# ---------- diff ----------
-log "Diff doc entity rev3 -> rev4 should show attrs.content changed"
-DIFF_JSON="${TMPDIR}/diff_3_4.json"
-"${IX_CMD[@]}" diff 3 4 --entity "${DOC_ID}" --format json > "${DIFF_JSON}" || die "ix diff failed"
-
-py_assert 'isinstance(data, dict) and data.get("fromRev")==3 and data.get("toRev")==4' "${DIFF_JSON}"
-py_assert 'isinstance(data.get("changes"), list) and len(data["changes"])>=1' "${DIFF_JSON}"
-py_assert 'any(ch.get("entityId")== "'"${DOC_ID}"'" and ch.get("changeType")=="modified" for ch in data["changes"])' "${DIFF_JSON}"
-py_assert 'any(ch.get("atFromRev",{}).get("attrs",{}).get("content","") == "" and ch.get("atToRev",{}).get("attrs",{}).get("content","") == "zzzxqv123" for ch in data.get("changes",[]))' "${DIFF_JSON}"
-
-# ---------- entity + claim lifecycle check (best-effort) ----------
-# If ix entity includes claims, verify old content claim is retired (deletedRev set) and only one active content claim remains.
-log "Checking claim lifecycle via ix entity (best-effort; depends on entity response including claims)"
-ENTITY_JSON="${TMPDIR}/entity_doc.json"
-"${IX_CMD[@]}" entity "${DOC_ID}" --format json > "${ENTITY_JSON}" || die "ix entity failed"
-
-# If entity response includes a "claims" list, enforce lifecycle.
-python3 - <<PY
-import json, sys
-d=json.load(open("${ENTITY_JSON}","r"))
-claims=d.get("claims")
-if not isinstance(claims, list):
- print("NOTE: entity response has no 'claims' array; skipping claim lifecycle assertion.")
- sys.exit(0)
-
-content_claims=[c for c in claims if c.get("statement")=="content"]
-active=[c for c in content_claims if c.get("deletedRev") in (None, "null") and c.get("status","").lower()=="active"]
-if len(active)!=1:
- raise SystemExit(f"Expected exactly 1 active 'content' claim; got {len(active)}\n{content_claims}")
-
-if str(active[0].get("value","")).strip('"') != "zzzxqv123":
- raise SystemExit(f"Active content claim value not zzzxqv123: {active[0]}")
-
-older=[c for c in content_claims if c.get("createdRev")==3]
-if older:
- if older[0].get("deletedRev") not in (4, "4"):
- raise SystemExit(f"Expected rev3 content claim deletedRev=4; got {older[0]}")
-print("Claim lifecycle assertion passed via ix entity.")
-PY
-
-popd >/dev/null
-
-log "✅ Smoke test PASSED"
-log "Temp project: ${TMPDIR}"
-log "memory-layer log: ${ML_LOG}"
-log "Tip: set REQUIRE_PATH_SEARCH=0 / REQUIRE_CLAIM_SEARCH=0 / REQUIRE_CONTENT_SEARCH=0 to relax expectations."
\ No newline at end of file
diff --git a/scripts/dev/rebuild.sh b/scripts/dev/rebuild.sh
deleted file mode 100755
index 00715c16..00000000
--- a/scripts/dev/rebuild.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-cd "$SCRIPT_DIR"
-
-echo "==> Shutting down..."
-./shutdown.sh
-
-echo "==> Building backend (sbt assembly)..."
-sbt assembly
-
-echo "==> Building CLI (npm run build)..."
-cd ix-cli
-npm run build
-cd ..
-
-echo "==> Running setup..."
-./setup.sh
-
-echo "==> Done."
diff --git a/scripts/dev/shutdown.sh b/scripts/dev/shutdown.sh
index c56aa476..5954fc29 100755
--- a/scripts/dev/shutdown.sh
+++ b/scripts/dev/shutdown.sh
@@ -18,17 +18,19 @@ set -euo pipefail
IX_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$IX_DIR"
+COMPOSE_FILE="docker-compose.standalone.yml"
+
if [[ "$(uname -s)" =~ MINGW|MSYS|CYGWIN ]]; then
export IX_HOST_MOUNT_ROOT="$(cygpath -m "$HOME")"
export IX_CONTAINER_MOUNT_ROOT="${HOME}"
dc() {
MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*' \
- docker compose -f "$(cygpath -m "$IX_DIR/docker-compose.yml")" "$@"
+ docker compose -f "$(cygpath -m "$IX_DIR/$COMPOSE_FILE")" "$@"
}
else
export IX_HOST_MOUNT_ROOT="${HOME}"
export IX_CONTAINER_MOUNT_ROOT="${HOME}"
- dc() { docker compose -f "$IX_DIR/docker-compose.yml" "$@"; }
+ dc() { docker compose -f "$IX_DIR/$COMPOSE_FILE" "$@"; }
fi
CLEAN=false
diff --git a/scripts/dev/stack.sh b/scripts/dev/stack.sh
deleted file mode 100755
index 09c1b3ce..00000000
--- a/scripts/dev/stack.sh
+++ /dev/null
@@ -1,88 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-cd "$SCRIPT_DIR"
-
-JAR_PATH="memory-layer/target/scala-2.13/ix-memory-layer.jar"
-
-check_docker() {
- if ! command -v docker &> /dev/null; then
- echo "Error: Docker is not installed."
- exit 1
- fi
- if ! docker info &> /dev/null 2>&1; then
- echo "Error: Docker is not running."
- exit 1
- fi
-}
-
-build_jar() {
- local force="${1:-false}"
- if [ "$force" = "true" ] || [ ! -f "$JAR_PATH" ]; then
- echo "Building Memory Layer JAR..."
- sbt "memoryLayer/assembly" 2>&1 | tail -5
- else
- echo "JAR already exists, skipping build. Use 'rebuild' to force."
- fi
-}
-
-wait_for_health() {
- echo "Waiting for services to become healthy..."
- for i in $(seq 1 30); do
- if curl -sf http://localhost:8090/v1/health > /dev/null 2>&1; then
- echo ""
- echo "Ix backend is ready!"
- echo " Memory Layer: http://localhost:8090"
- echo " ArangoDB: http://localhost:8529"
- return 0
- fi
- printf "."
- sleep 2
- done
- echo ""
- echo "Warning: Health check timed out. Check 'docker compose logs' for details."
- return 1
-}
-
-case "${1:-up}" in
- up)
- check_docker
- build_jar
- echo "Starting Ix backend..."
- docker compose up -d --build
- wait_for_health
- ;;
- down)
- docker compose down
- echo "Ix backend stopped."
- ;;
- logs)
- docker compose logs -f
- ;;
- clean)
- docker compose down -v
- echo "Ix backend stopped and data volumes removed."
- ;;
- rebuild)
- check_docker
- build_jar true
- echo "Rebuilding and starting Ix backend..."
- docker compose up -d --build
- wait_for_health
- ;;
- status)
- docker compose ps
- ;;
- *)
- echo "Usage: ./stack.sh [up|down|logs|clean|rebuild|status]"
- echo ""
- echo " up Build JAR (if needed) and start all services (default)"
- echo " down Stop all services"
- echo " logs Tail service logs"
- echo " clean Stop all services and remove data volumes"
- echo " rebuild Force rebuild JAR and restart services"
- echo " status Show service status"
- exit 1
- ;;
-esac