Skip to content

armgit / Ix

Public Imported from GitHub
0

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

Add multi-pass conflict detection with keyword-overlap (pass 2)

riley0227131e330c3616
diff --git a/memory-layer/src/main/scala/ix/memory/context/ConflictDetector.scala b/memory-layer/src/main/scala/ix/memory/context/ConflictDetector.scala
index 8954af37..c3ac7816 100644
--- a/memory-layer/src/main/scala/ix/memory/context/ConflictDetector.scala
+++ b/memory-layer/src/main/scala/ix/memory/context/ConflictDetector.scala
@@ -8,39 +8,67 @@ trait ConflictDetector {
}
class ConflictDetectorImpl extends ConflictDetector {
+
+ /**
+ * Multi-pass conflict detection.
+ *
+ * Pass 1 – exact contradiction: two active claims on the same entity whose
+ * statements differ and share NO keyword stems.
+ * Pass 2 – keyword overlap: two active claims on the same entity whose
+ * statements differ but share at least one keyword stem (i.e. they
+ * talk about the same topic yet say different things).
+ */
def detect(claims: Vector[ScoredClaim]): Vector[ConflictReport] = {
- // Group claims by entityId
- val grouped = claims.groupBy(_.claim.entityId)
+ val byEntity = claims.groupBy(_.claim.entityId)
- grouped.values.flatMap { entityClaims =>
- // Only consider active claims
- val active = entityClaims.filter(_.claim.status == ClaimStatus.Active)
+ byEntity.values.flatMap { entityClaims =>
+ val activeClaims = entityClaims.filter(_.claim.status == ClaimStatus.Active)
- // Find pairs with different statements (same entity, different facts)
- if (active.size < 2) Vector.empty
+ if (activeClaims.size < 2) Vector.empty
else {
- val pairs = for {
- i <- active.indices
- j <- (i + 1) until active.size
- a = active(i)
- b = active(j)
- if a.claim.statement != b.claim.statement
- } yield {
- // The higher-confidence claim is the recommended winner
- 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 = s"Contradictory statements on entity ${a.claim.entityId.value}: " +
- s"'${a.claim.statement}' vs '${b.claim.statement}'",
- recommendation = s"Prefer '${winner.claim.statement}' (confidence: ${winner.confidence.score})"
- )
+ val pairs = activeClaims.combinations(2).toVector
+ pairs.flatMap {
+ case Vector(a, b) =>
+ if (a.claim.statement == b.claim.statement) {
+ // Identical statements are not in conflict
+ None
+ }
+ // Pass 1: different statements with keyword overlap (same-topic contradiction)
+ else if (keywordsOverlap(a.claim.statement, b.claim.statement)) {
+ Some(makeConflict(a, b, "Contradictory statements (keyword overlap)"))
+ }
+ // Pass 2: different statements with no keyword overlap (unrelated contradiction)
+ else {
+ Some(makeConflict(a, b, s"Contradictory statements on entity ${a.claim.entityId.value}"))
+ }
+ case _ => None // safety for non-pair combinations
}
- pairs.toVector
}
}.toVector
}
+
+ /** 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. */