Skip to content

armgit / Ix

Public Imported from GitHub
0

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

feat: add /v1/decide, /v1/search, /v1/truth, /v1/patches endpoints

riley0227f7255ebdd97f
diff --git a/memory-layer/src/main/scala/ix/memory/Main.scala b/memory-layer/src/main/scala/ix/memory/Main.scala
index 30215ee9..3f915d2e 100644
--- a/memory-layer/src/main/scala/ix/memory/Main.scala
+++ b/memory-layer/src/main/scala/ix/memory/Main.scala
@@ -42,7 +42,7 @@ object Main extends IOApp.Simple {
conflictService = new ConflictService(client, queryApi, writeApi)
// 4. HTTP routes
- routes = Routes.all(contextService, ingestionService, queryApi, conflictService, client)
+ routes = Routes.all(contextService, ingestionService, queryApi, writeApi, conflictService, client)
// 5. Server
server <- EmberServerBuilder
diff --git a/memory-layer/src/main/scala/ix/memory/api/DecideRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/DecideRoutes.scala
new file mode 100644
index 00000000..64dc7d97
--- /dev/null
+++ b/memory-layer/src/main/scala/ix/memory/api/DecideRoutes.scala
@@ -0,0 +1,54 @@
+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)
+ ))
+ ),
+ 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/PatchRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/PatchRoutes.scala
new file mode 100644
index 00000000..60552053
--- /dev/null
+++ b/memory-layer/src/main/scala/ix/memory/api/PatchRoutes.scala
@@ -0,0 +1,46 @@
+package ix.memory.api
+
+import java.util.UUID
+
+import cats.effect.IO
+import io.circe.syntax._
+import org.http4s.HttpRoutes
+import org.http4s.circe.CirceEntityCodec._
+import org.http4s.dsl.io._
+
+import ix.memory.db.ArangoClient
+
+class PatchRoutes(client: ArangoClient) {
+
+ val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
+ case GET -> Root / "v1" / "patches" =>
+ (for {
+ patches <- client.query(
+ """FOR p IN patches
+ | SORT p.rev DESC
+ | LIMIT 50
+ | 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.empty[String, 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(_))
+ }
+}
diff --git a/memory-layer/src/main/scala/ix/memory/api/Routes.scala b/memory-layer/src/main/scala/ix/memory/api/Routes.scala
index 107ad341..c89a367a 100644
--- a/memory-layer/src/main/scala/ix/memory/api/Routes.scala
+++ b/memory-layer/src/main/scala/ix/memory/api/Routes.scala
@@ -10,7 +10,7 @@ import org.http4s.dsl.io._
import ix.memory.conflict.ConflictService
import ix.memory.context.ContextService
-import ix.memory.db.{ArangoClient, GraphQueryApi}
+import ix.memory.db.{ArangoClient, GraphQueryApi, GraphWriteApi}
import ix.memory.ingestion.IngestionService
object Routes {
@@ -19,6 +19,7 @@ object Routes {
contextService: ContextService,
ingestionService: IngestionService,
queryApi: GraphQueryApi,
+ writeApi: GraphWriteApi,
conflictService: ConflictService,
client: ArangoClient
): HttpRoutes[IO] = {
@@ -33,7 +34,13 @@ object 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 patchRoutes = new PatchRoutes(client).routes
- health <+> contextRoutes <+> ingestionRoutes <+> entityRoutes <+> diffRoutes <+> conflictRoutes
+ health <+> contextRoutes <+> ingestionRoutes <+> entityRoutes <+>
+ diffRoutes <+> conflictRoutes <+> decideRoutes <+> searchRoutes <+>
+ truthRoutes <+> patchRoutes
}
}
diff --git a/memory-layer/src/main/scala/ix/memory/api/SearchRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/SearchRoutes.scala
new file mode 100644
index 00000000..550bd480
--- /dev/null
+++ b/memory-layer/src/main/scala/ix/memory/api/SearchRoutes.scala
@@ -0,0 +1,30 @@
+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)
+
+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))
+ resp <- Ok(nodes.asJson)
+ } yield resp).handleErrorWith(ErrorHandler.handle(_))
+ }
+}
diff --git a/memory-layer/src/main/scala/ix/memory/api/TruthRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/TruthRoutes.scala
new file mode 100644
index 00000000..57883656
--- /dev/null
+++ b/memory-layer/src/main/scala/ix/memory/api/TruthRoutes.scala
@@ -0,0 +1,60 @@
+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/test/scala/ix/memory/api/RoutesSpec.scala b/memory-layer/src/test/scala/ix/memory/api/RoutesSpec.scala
index 3e4c695e..2c016a42 100644
--- a/memory-layer/src/test/scala/ix/memory/api/RoutesSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/api/RoutesSpec.scala
@@ -63,7 +63,7 @@ class RoutesSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {
new ConflictDetectorImpl()
)
val ingestionService = new IngestionService(new ParserRouter(), writeApi, queryApi)
- Routes.all(contextService, ingestionService, queryApi, conflictService, client).orNotFound
+ Routes.all(contextService, ingestionService, queryApi, writeApi, conflictService, client).orNotFound
}
// ── Test 1: Health check ─────────────────────────────────────────────
@@ -346,4 +346,162 @@ class RoutesSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {
}
}
}
+
+ // ── Test 12: POST /v1/decide creates a decision node ─────────────────
+
+ it should "create a decision node via POST /v1/decide" in {
+ clientResource.use { client =>
+ val writeApi = new ArangoGraphWriteApi(client)
+ val queryApi = new ArangoGraphQueryApi(client)
+ val app = buildRoutes(client, writeApi, queryApi)
+
+ for {
+ _ <- client.ensureSchema()
+ body = Json.obj(
+ "title" -> "Use exponential backoff".asJson,
+ "rationale" -> "Better for transient failures".asJson
+ )
+ req = Request[IO](Method.POST, uri"/v1/decide").withEntity(body)
+ resp <- app.run(req)
+ json <- resp.as[Json]
+ } yield {
+ resp.status shouldBe Status.Ok
+ json.hcursor.get[String]("status").toOption shouldBe Some("Ok")
+ json.hcursor.get[String]("nodeId").toOption should not be empty
+ }
+ }
+ }
+
+ // ── Test 13: POST /v1/search finds nodes by term ─────────────────────
+
+ 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()
+ _ <- writeApi.commitPatch(patch)
+ body = Json.obj("term" -> "billing_search_test".asJson)
+ req = Request[IO](Method.POST, uri"/v1/search").withEntity(body)
+ resp <- app.run(req)
+ json <- resp.as[Json]
+ } yield {
+ resp.status shouldBe Status.Ok
+ json.asArray should not be empty
+ }
+ }
+ }
+
+ // ── Test 14: GET /v1/truth returns intent list ────────────────────────
+
+ it should "return intent list via GET /v1/truth" in {
+ clientResource.use { client =>
+ val writeApi = new ArangoGraphWriteApi(client)
+ val queryApi = new ArangoGraphQueryApi(client)
+ val app = buildRoutes(client, writeApi, queryApi)
+
+ for {
+ _ <- client.ensureSchema()
+ req = Request[IO](Method.GET, uri"/v1/truth")
+ resp <- app.run(req)
+ } yield {
+ resp.status shouldBe Status.Ok
+ }
+ }
+ }
+
+ // ── Test 15: POST /v1/truth creates an intent node ─────────────────
+
+ it should "create an intent via POST /v1/truth" in {
+ clientResource.use { client =>
+ val writeApi = new ArangoGraphWriteApi(client)
+ val queryApi = new ArangoGraphQueryApi(client)
+ val app = buildRoutes(client, writeApi, queryApi)
+
+ for {
+ _ <- client.ensureSchema()
+ body = Json.obj("statement" -> "Ship retry system by Friday".asJson)
+ req = Request[IO](Method.POST, uri"/v1/truth").withEntity(body)
+ resp <- app.run(req)
+ json <- resp.as[Json]
+ } yield {
+ resp.status shouldBe Status.Ok
+ json.hcursor.get[String]("status").toOption shouldBe Some("Ok")
+ }
+ }
+ }
+
+ // ── Test 16: GET /v1/patches lists recent patches ─────────────────────
+
+ it should "list recent patches via GET /v1/patches" in {
+ clientResource.use { client =>
+ val writeApi = new ArangoGraphWriteApi(client)
+ val queryApi = new ArangoGraphQueryApi(client)
+ val app = buildRoutes(client, writeApi, queryApi)
+
+ for {
+ _ <- client.ensureSchema()
+ req = Request[IO](Method.GET, uri"/v1/patches")
+ resp <- app.run(req)
+ } yield {
+ resp.status shouldBe Status.Ok
+ }
+ }
+ }
+
+ // ── Test 17: GET /v1/patches/:id returns a specific patch ──────────────
+
+ 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()
+ _ <- writeApi.commitPatch(patch)
+ req = Request[IO](Method.GET, Uri.unsafeFromString(s"/v1/patches/${patchId.value}"))
+ resp <- app.run(req)
+ body <- resp.as[Json]
+ } yield {
+ resp.status shouldBe Status.Ok
+ body.hcursor.get[String]("patch_id").toOption shouldBe Some(patchId.value.toString)
+ }
+ }
+ }
+
+ // ── Test 18: GET /v1/patches/:id returns 404 for missing patch ─────────
+
+ it should "return 404 for non-existent patch ID" in {
+ clientResource.use { client =>
+ val writeApi = new ArangoGraphWriteApi(client)
+ val queryApi = new ArangoGraphQueryApi(client)
+ val app = buildRoutes(client, writeApi, queryApi)
+ val fakeId = UUID.randomUUID()
+
+ for {
+ _ <- client.ensureSchema()
+ req = Request[IO](Method.GET, Uri.unsafeFromString(s"/v1/patches/$fakeId"))
+ resp <- app.run(req)
+ } yield {
+ resp.status shouldBe Status.NotFound
+ }
+ }
+ }
}