Skip to content

armgit / Ix

Public Imported from GitHub
0

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

Add intent alignment as 6th confidence factor and switch recency to wall-clock time

riley022796b4a2bd6896
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 7831f514..7225c291 100644
--- a/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala
+++ b/memory-layer/src/main/scala/ix/memory/context/ConfidenceScorer.scala
@@ -1,5 +1,7 @@
package ix.memory.context
+import java.time.Instant
+
import ix.memory.model._
trait ConfidenceScorer {
@@ -10,7 +12,9 @@ case class ScoringContext(
latestRev: Rev,
sourceChanged: Boolean,
corroboratingCount: Int,
- conflictState: ConflictState
+ conflictState: ConflictState,
+ intentAlignment: IntentAlignment,
+ observedAt: Instant
)
sealed trait ConflictState
@@ -22,6 +26,14 @@ object 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(
@@ -31,25 +43,26 @@ class ConfidenceScorerImpl extends ConfidenceScorer {
val verif = Factor(1.0, "no verification data available")
- val recency = computeRecency(claim, ctx)
+ val recency = computeRecency(ctx)
val corr = computeCorroboration(ctx.corroboratingCount)
val conflict = computeConflictPenalty(ctx.conflictState)
+ val intent = computeIntentAlignment(ctx.intentAlignment)
- ScoredClaim(claim, ConfidenceBreakdown(base, verif, recency, corr, conflict))
+ ScoredClaim(claim, ConfidenceBreakdown(base, verif, recency, corr, conflict, intent))
}
- 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
+ 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(recencyValue, s"rev distance: $revDistance, source changed: ${ctx.sourceChanged}")
+ Factor(value, reason)
}
private def computeCorroboration(count: Int): Factor = {
@@ -69,4 +82,11 @@ class ConfidenceScorerImpl extends ConfidenceScorer {
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/ContextService.scala b/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
index ffb2675f..3a8e5399 100644
--- a/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
+++ b/memory-layer/src/main/scala/ix/memory/context/ContextService.scala
@@ -35,9 +35,11 @@ class ContextService(
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
+ sourceChanged = false, // Default for Phase 1
+ corroboratingCount = 1, // Default for Phase 1
+ conflictState = ConflictState.NoConflict, // Default for Phase 1
+ intentAlignment = IntentAlignment.NoConnection,
+ observedAt = c.provenance.observedAt
))
}
diff --git a/memory-layer/src/main/scala/ix/memory/model/ConfidenceBreakdown.scala b/memory-layer/src/main/scala/ix/memory/model/ConfidenceBreakdown.scala
index 789cdfe2..d8ade7e1 100644
--- a/memory-layer/src/main/scala/ix/memory/model/ConfidenceBreakdown.scala
+++ b/memory-layer/src/main/scala/ix/memory/model/ConfidenceBreakdown.scala
@@ -15,7 +15,8 @@ final case class ConfidenceBreakdown(
verification: Factor,
recency: Factor,
corroboration: Factor,
- conflictPenalty: Factor
+ conflictPenalty: Factor,
+ intentAlignment: Factor
) {
/** Confidence score = product of all factor values, clamped to [0, 1]. */
@@ -24,7 +25,8 @@ final case class ConfidenceBreakdown(
verification.value *
recency.value *
corroboration.value *
- conflictPenalty.value
+ conflictPenalty.value *
+ intentAlignment.value
math.max(0.0, math.min(1.0, raw))
}
}
diff --git a/memory-layer/src/test/scala/ix/memory/conflict/ConflictDetectorSpec.scala b/memory-layer/src/test/scala/ix/memory/conflict/ConflictDetectorSpec.scala
index e4e4260f..8ab9b5e8 100644
--- a/memory-layer/src/test/scala/ix/memory/conflict/ConflictDetectorSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/conflict/ConflictDetectorSpec.scala
@@ -26,7 +26,8 @@ class ConflictDetectorSpec extends AnyFlatSpec with Matchers {
verification = Factor(1.0, "test"),
recency = Factor(1.0, "test"),
corroboration = Factor(1.0, "test"),
- conflictPenalty = Factor(1.0, "test")
+ conflictPenalty = Factor(1.0, "test"),
+ intentAlignment = Factor(1.0, "test")
)
ScoredClaim(claim, breakdown)
}
diff --git a/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala b/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala
index 3ffd06ae..8adbf67e 100644
--- a/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/context/ConfidenceScorerSpec.scala
@@ -23,7 +23,9 @@ class ConfidenceScorerSpec extends AnyFlatSpec with Matchers {
latestRev = Rev(5L),
sourceChanged = false,
corroboratingCount = 1,
- conflictState = ConflictState.NoConflict
+ conflictState = ConflictState.NoConflict,
+ intentAlignment = IntentAlignment.NoConnection,
+ observedAt = Instant.now()
)
"ConfidenceScorer" should "score code-sourced claims higher than doc-sourced" in {
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 c9bcf2fc..791f20db 100644
--- a/memory-layer/src/test/scala/ix/memory/context/ContextServiceSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/context/ContextServiceSpec.scala
@@ -218,10 +218,10 @@ class ContextServiceSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {
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(0.5, "low"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok")
))
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(0.9, "high"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok"), Factor(1.0, "ok")
))
val ranked = ContextRanker.rank(Vector(scored1, scored2))
diff --git a/memory-layer/src/test/scala/ix/memory/model/ModelSpec.scala b/memory-layer/src/test/scala/ix/memory/model/ModelSpec.scala
index fa411c8c..b86961b6 100644
--- a/memory-layer/src/test/scala/ix/memory/model/ModelSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/model/ModelSpec.scala
@@ -194,16 +194,17 @@ class ModelSpec extends AnyFlatSpec with Matchers {
// ── ConfidenceBreakdown.score ───────────────────────────────────────
- "ConfidenceBreakdown" should "compute score correctly: 0.90 * 1.1 * 1.0 * 1.1 * 0.85 = ~0.926" in {
+ "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")
+ conflictPenalty = Factor(0.85, "minor conflict"),
+ intentAlignment = Factor(1.0, "no connection to intent")
)
- // 0.90 * 1.1 * 1.0 * 1.1 * 0.85 = 0.92565
+ // 0.90 * 1.1 * 1.0 * 1.1 * 0.85 * 1.0 = 0.92565
breakdown.score shouldBe 0.926 +- 0.001
}
@@ -213,7 +214,8 @@ class ModelSpec extends AnyFlatSpec with Matchers {
verification = Factor(1.5, "highly verified"),
recency = Factor(1.0, "fresh"),
corroboration = Factor(1.5, "strong agreement"),
- conflictPenalty = Factor(1.0, "no conflict")
+ conflictPenalty = Factor(1.0, "no conflict"),
+ intentAlignment = Factor(1.0, "no connection to intent")
)
breakdown.score shouldBe 1.0
@@ -225,7 +227,8 @@ class ModelSpec extends AnyFlatSpec with Matchers {
verification = Factor(1.0, "none"),
recency = Factor(1.0, "none"),
corroboration = Factor(1.0, "none"),
- conflictPenalty = Factor(1.0, "none")
+ conflictPenalty = Factor(1.0, "none"),
+ intentAlignment = Factor(1.0, "none")
)
breakdown.score shouldBe 0.0
@@ -237,7 +240,8 @@ class ModelSpec extends AnyFlatSpec with Matchers {
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")
+ conflictPenalty = Factor(0.85, "minor conflict"),
+ intentAlignment = Factor(1.0, "no connection to intent")
)
val json = breakdown.asJson