Skip to content

armgit / Ix

Public Imported from GitHub
0

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

feat: corroboration counting for confidence scoring

riley0227aa26078f4964
diff --git a/memory-layer/src/main/scala/ix/memory/context/ContextService.scala b/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
index 0203b39a..9f92ebc4 100644
--- a/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
+++ b/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
@@ -33,11 +33,12 @@ class ContextService(
// 5. Score each claim for confidence
rev <- asOfRev.fold(queryApi.getLatestRev)(IO.pure)
+ corroborationMap = CorroborationCounter.count(claims)
scored = claims.map { c =>
confidenceScorer.score(c, ScoringContext(
latestRev = rev,
sourceChanged = false,
- corroboratingCount = 1,
+ corroboratingCount = corroborationMap.getOrElse(c.id, 0),
conflictState = ConflictState.NoConflict,
intentAlignment = IntentAlignment.NoConnection,
observedAt = c.provenance.observedAt
diff --git a/memory-layer/src/main/scala/ix/memory/context/CorroborationCounter.scala b/memory-layer/src/main/scala/ix/memory/context/CorroborationCounter.scala
new file mode 100644
index 00000000..d1be2b07
--- /dev/null
+++ b/memory-layer/src/main/scala/ix/memory/context/CorroborationCounter.scala
@@ -0,0 +1,42 @@
+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/test/scala/ix/memory/context/CorroborationCounterSpec.scala b/memory-layer/src/test/scala/ix/memory/context/CorroborationCounterSpec.scala
new file mode 100644
index 00000000..bb353598
--- /dev/null
+++ b/memory-layer/src/test/scala/ix/memory/context/CorroborationCounterSpec.scala
@@ -0,0 +1,87 @@
+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 CorroborationCounterSpec extends AnyFlatSpec with Matchers {
+
+ private def makeClaim(entityId: NodeId, statement: String, sourceUri: String): Claim =
+ Claim(
+ id = ClaimId(UUID.randomUUID()),
+ entityId = entityId,
+ statement = statement,
+ 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
+ }
+}