From 2ef639eba91cf839db05b6ddea21bf6518c9a6cd Mon Sep 17 00:00:00 2001 From: Mikhail Date: Sat, 29 Aug 2026 22:59:38 +0300 Subject: [PATCH] bound cognitive personal-memory projection --- modules/interaction/domain/personal_memory.go | 10 ++++++++-- modules/interaction/domain/personal_memory_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/interaction/domain/personal_memory.go b/modules/interaction/domain/personal_memory.go index 7ecd3a0..8976ecc 100644 --- a/modules/interaction/domain/personal_memory.go +++ b/modules/interaction/domain/personal_memory.go @@ -26,6 +26,12 @@ type PersonalMemoryFact struct { const personalMemoryCurrentLocation = "CURRENT_LOCATION" +// personalMemoryMaxValueRunes keeps the compatibility projection bounded. +// Canonical utterances remain complete in storage; only the rebuildable +// cognitive context is capped so old long-form instructions cannot become a +// provider prompt or crowd out a current household fact. +const personalMemoryMaxValueRunes = 512 + // CompilePersonalMemory turns direct, first-person human statements into a // small typed context. It deliberately preserves preferences as statements // rather than pretending that an arbitrary preference is a verified fact. @@ -119,7 +125,7 @@ func personalPreference(message string) string { } value := strings.TrimSpace(lower[index+len(cue):]) value = strings.Trim(value, " .,!?:;—-") - if len([]rune(value)) >= 2 { + if len([]rune(value)) >= 2 && len([]rune(value)) <= personalMemoryMaxValueRunes { return value } } @@ -144,7 +150,7 @@ func personalPreference(message string) string { separatorWidth = len("—") } value := strings.Trim(strings.TrimSpace(statement[separator+separatorWidth:]), " .,!?:;—-") - if key != "" && len([]rune(value)) >= 2 { + if key != "" && len([]rune(value)) >= 2 && len([]rune(value)) <= personalMemoryMaxValueRunes { return key + " — " + value } } diff --git a/modules/interaction/domain/personal_memory_test.go b/modules/interaction/domain/personal_memory_test.go index 986f405..0d1b92c 100644 --- a/modules/interaction/domain/personal_memory_test.go +++ b/modules/interaction/domain/personal_memory_test.go @@ -28,3 +28,14 @@ func TestCompilePersonalMemoryDoesNotTreatThirdPartyStatementAsPersonalFact(t *t t.Fatalf("ordinary factual question became personal memory: %+v", memory) } } + +func TestCompilePersonalMemoryBoundsLongRememberedValues(t *testing.T) { + longValue := strings.Repeat("x", 513) + memory := CompilePersonalMemory([]interaction.HumanUtterance{{ + Content: "запомни: мой тестовый цвет — " + longValue, + ReceivedAtUnixMillis: 100, + }}) + if memory.ContextText() != "" { + t.Fatalf("long remembered value leaked into cognitive context: %q", memory.ContextText()) + } +}