feat: evolve adaptive Gen2 retrieval

This commit is contained in:
m
2026-08-30 20:25:23 +03:00
parent a6273d0134
commit 9312441d00
20 changed files with 411 additions and 42 deletions
+16 -1
View File
@@ -76,6 +76,7 @@ type assertionCandidate struct {
fragment ContextFragment
subject string
observed int64
corrects interaction.HumanUtteranceID
}
func explicitAssertionCandidates(history []interaction.HumanUtterance, projectRefs map[interaction.HumanUtteranceID][]interaction.ProjectReference, focusProjectID, query string) []assertionCandidate {
@@ -111,6 +112,7 @@ func explicitAssertionCandidates(history []interaction.HumanUtterance, projectRe
fragment: ContextFragment{UtteranceID: utterance.ID, Text: subject + " = " + value, ScopeRef: scopeRef, Relevance: relevance},
subject: normalizedContextSubject(subject),
observed: utterance.ReceivedAtUnixMillis,
corrects: utterance.CorrectsUtteranceID,
})
}
return result
@@ -120,7 +122,7 @@ func selectCurrentAssertions(candidates []assertionCandidate) ([]ContextFragment
latest := make(map[string]assertionCandidate)
for _, candidate := range candidates {
current, exists := latest[candidate.subject]
if !exists || candidate.observed > current.observed || candidate.observed == current.observed && candidate.fragment.UtteranceID > current.fragment.UtteranceID {
if !exists || supersedesAssertion(candidate, current) {
latest[candidate.subject] = candidate
}
}
@@ -144,6 +146,19 @@ func selectCurrentAssertions(candidates []assertionCandidate) ([]ContextFragment
return current, historical
}
// supersedesAssertion first respects an explicit correction relation, then
// uses observation time only as a deterministic tie-breaker. Applicability is
// already established by principal and typed Scope filtering before this step.
func supersedesAssertion(candidate, current assertionCandidate) bool {
if candidate.corrects == current.fragment.UtteranceID {
return true
}
if current.corrects == candidate.fragment.UtteranceID {
return false
}
return candidate.observed > current.observed || candidate.observed == current.observed && candidate.fragment.UtteranceID > current.fragment.UtteranceID
}
func contextProjectionFromAssertions(current, historical []ContextFragment) ContextProjection {
selected := make([]ContextFragment, 0, len(current))
text := make([]string, 0, len(current))
@@ -60,6 +60,18 @@ func TestGen2ContextKeepsHistoricalAssertionOutOfCurrentProjection(t *testing.T)
}
}
func TestGen2ContextCorrectionSupersedesBeforeTimestampTieBreaker(t *testing.T) {
history := []interaction.HumanUtterance{
{ID: "observed-later", PrincipalID: "principal-1", Content: "контрольный код = LATER", ReceivedAtUnixMillis: 9},
{ID: "original", PrincipalID: "principal-1", Content: "контрольный код = ORIGINAL", ReceivedAtUnixMillis: 1},
{ID: "correction", PrincipalID: "principal-1", Content: "контрольный код = CORRECTED", ReceivedAtUnixMillis: 2, CorrectsUtteranceID: "observed-later"},
}
got := domain.BuildScopedGen2ContextForQuery(history, nil, "principal-1", "", "Какой контрольный код?")
if !strings.Contains(got.Text, "CORRECTED") || strings.Contains(got.Text, "LATER") {
t.Fatalf("explicit correction did not supersede: %#v", got)
}
}
func TestGen2ContextHardFiltersProjectReferencesBeforeRanking(t *testing.T) {
history := []interaction.HumanUtterance{
{ID: "a", PrincipalID: "principal-1", Content: "контрольный код = ALPHA", ReceivedAtUnixMillis: 1},