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 confidence scorer with explainable breakdowns

riley0227c4a3c234f789
diff --git a/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala b/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala
index e645ef96..7831f514 100644
--- a/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala
+++ b/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala
@@ -3,21 +3,70 @@ package ix.memory.context
import ix.memory.model._
trait ConfidenceScorer {
- def score(claim: Claim, latestRev: Rev): ScoredClaim
+ def score(claim: Claim, ctx: ScoringContext): ScoredClaim
}
-/** Stub implementation for Phase 1 -- Task 8 will replace this. */
-class DefaultConfidenceScorer extends ConfidenceScorer {
-
- def score(claim: Claim, latestRev: Rev): ScoredClaim = {
- val baseAuth = SourceType.baseAuthority(claim.provenance.sourceType)
- val breakdown = ConfidenceBreakdown(
- baseAuthority = Factor(baseAuth, s"${claim.provenance.sourceType}"),
- verification = Factor(1.0, "no verification data"),
- recency = Factor(1.0, "default recency"),
- corroboration = Factor(1.0, "default corroboration"),
- conflictPenalty = Factor(1.0, "no conflicts detected")
+case class ScoringContext(
+ latestRev: Rev,
+ sourceChanged: Boolean,
+ corroboratingCount: Int,
+ conflictState: ConflictState
+)
+
+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
+}
+
+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}"
)
- ScoredClaim(claim, breakdown)
+
+ val verif = Factor(1.0, "no verification data available")
+
+ val recency = computeRecency(claim, ctx)
+ val corr = computeCorroboration(ctx.corroboratingCount)
+ val conflict = computeConflictPenalty(ctx.conflictState)
+
+ ScoredClaim(claim, ConfidenceBreakdown(base, verif, recency, corr, conflict))
+ }
+
+ private def computeRecency(claim: Claim, ctx: ScoringContext): Factor = {
+ val revDistance = ctx.latestRev.value - claim.createdRev.value
+ val recencyValue = if (ctx.sourceChanged) {
+ 0.80 // Source has been modified since this claim was observed
+ } else if (revDistance <= 5) {
+ 1.0 // Very recent
+ } else if (revDistance <= 50) {
+ 0.95 // Moderately recent
+ } else {
+ 0.85 // Old but source unchanged
+ }
+ Factor(recencyValue, s"rev distance: $revDistance, source changed: ${ctx.sourceChanged}")
+ }
+
+ 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")
}
}
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 05273886..2ec66722 100644
--- a/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
+++ b/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
@@ -32,7 +32,14 @@ class ContextService(
// 5. Score each claim
rev <- asOfRev.fold(queryApi.getLatestRev(tenant))(IO.pure)
- scored = claims.map(c => confidenceScorer.score(c, rev))
+ scored = claims.map { c =>
+ confidenceScorer.score(c, ScoringContext(
+ latestRev = rev,
+ sourceChanged = false, // Default for Phase 1
+ corroboratingCount = 1, // Default for Phase 1
+ conflictState = ConflictState.NoConflict // Default for Phase 1
+ ))
+ }
// 6. Detect conflicts among scored claims
conflicts = conflictDetector.detect(scored)
diff --git a/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala b/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala
new file mode 100644
index 00000000..3ffd06ae
--- /dev/null
+++ b/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala
@@ -0,0 +1,78 @@
+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 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",
+ 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
+ )
+
+ "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))
+ val stale = scorer.score(claim, defaultCtx.copy(sourceChanged = true))
+
+ 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
index 0677a958..f3066f7f 100644
--- a/memory-layer/src/test/scala/ix/memory/context/ContextServiceSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/context/ContextServiceSpec.scala
@@ -46,7 +46,7 @@ class ContextServiceSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {
val seeder = new GraphSeeder(queryApi)
val expander = new GraphExpander(queryApi)
val collector = new ClaimCollector(queryApi)
- val scorer = new DefaultConfidenceScorer()
+ val scorer = new ConfidenceScorerImpl()
val detector = new DefaultConflictDetector()
new ContextService(queryApi, seeder, expander, collector, scorer, detector)
}