diff --git a/internal/gateway/prompt.go b/internal/gateway/prompt.go index 44acf0f2e..aabcd1033 100644 --- a/internal/gateway/prompt.go +++ b/internal/gateway/prompt.go @@ -81,13 +81,19 @@ type promptData struct { Date string } +// escapeXML replaces the three bytes that would otherwise be read as markup +// inside a trust="data" block. Exported within the package so callers that +// must bound a block's *rendered* size (turn_memory.go's byte cap) measure +// the same expansion the template applies, rather than the pre-escape size. +var escapeXML = strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", +).Replace + var archiePromptTemplate = template.Must( template.New("archie"). - Funcs(template.FuncMap{"xml": strings.NewReplacer( - "&", "&", - "<", "<", - ">", ">", - ).Replace}). + Funcs(template.FuncMap{"xml": escapeXML}). Parse(archiePromptTpl), ) diff --git a/internal/gateway/turn_memory.go b/internal/gateway/turn_memory.go index 79b54cfdb..7f680f590 100644 --- a/internal/gateway/turn_memory.go +++ b/internal/gateway/turn_memory.go @@ -54,15 +54,25 @@ func renderMemory(ctx context.Context, store MemoryStore, subject domainmemory.S } }() - records, err := store.Query(ctx, domainmemory.Query{ - Scopes: subject.Scopes(), - Limit: memoryRecordsPerScope * len(subject.Scopes()), - }) - if err != nil { - if log != nil { - log.Warn("chat memory read failed; continuing without a memory block", "err", err) + // One Query per scope, each capped at memoryRecordsPerScope: BuiltinEngine. + // Query bounds q.Limit across the whole result, not per scope it is given, + // so a single call across all of subject.Scopes() would let an early scope + // (agent-user is queried first) exhaust the entire budget and starve the + // later, wider scopes. Scopes() is already ordered most-specific-first, so + // appending each scope's results in order preserves that ordering. + var records []domainmemory.Record + for _, scope := range subject.Scopes() { + heads, err := store.Query(ctx, domainmemory.Query{ + Scopes: []domainmemory.Scope{scope}, + Limit: memoryRecordsPerScope, + }) + if err != nil { + if log != nil { + log.Warn("chat memory read failed; continuing without a memory block", "err", err) + } + return "" } - return "" + records = append(records, heads...) } return renderMemoryRecords(records, log) } @@ -73,15 +83,25 @@ func renderMemory(ctx context.Context, store MemoryStore, subject domainmemory.S // truth for addressing. A record whose line would push the block past // memoryBlockByteCap is dropped and logged rather than truncated mid-line, // so a partial block is never mistaken for a complete or a corrupted one. +// +// The cap is measured against each line's *escaped* size -- what +// BuildSystemPrompt's `{{xml .Memory}}` will expand it to -- not its raw +// size, because escapeXML can grow a byte fivefold ("&" -> "&") and an +// unescaped-size cap would let the rendered prompt exceed it by that factor. +// The block itself still accumulates the raw (unescaped) lines: the template +// escapes once on render, so escaping here too would double-escape it. func renderMemoryRecords(records []domainmemory.Record, log *slog.Logger) string { var b strings.Builder + escapedLen := 0 dropped := 0 for _, record := range records { line := fmt.Sprintf("- [%s] (%s, %s) %s\n", record.ID, record.Scope.Kind, record.Kind, record.Content) - if b.Len()+len(line) > memoryBlockByteCap { + next := escapedLen + len(escapeXML(line)) + if next > memoryBlockByteCap { dropped++ continue } + escapedLen = next b.WriteString(line) } if dropped > 0 && log != nil { diff --git a/internal/gateway/turn_memory_test.go b/internal/gateway/turn_memory_test.go index 2571ef869..2a5dce552 100644 --- a/internal/gateway/turn_memory_test.go +++ b/internal/gateway/turn_memory_test.go @@ -324,6 +324,74 @@ func TestRenderMemoryRecordsCapsLoudly(t *testing.T) { } } +// TestRenderMemoryEnforcesPerScopeQuota guards against BuiltinEngine.Query's +// Limit being a total across the scopes it is given, not per scope: a query +// issued once across all four scopes with Limit = memoryRecordsPerScope * +// len(scopes) would let an early scope (agent-user, queried first) consume +// the whole budget and starve every later, wider scope. renderMemory must +// query each scope separately so a scope with many records cannot crowd out +// another scope's records. +func TestRenderMemoryEnforcesPerScopeQuota(t *testing.T) { + engine := newMemoryTestEngine(t) + ctx := context.Background() + + // Flood agent-user scope with far more than memoryRecordsPerScope + // records -- enough that, under a shared total limit, they alone would + // exhaust it. + for i := range memoryRecordsPerScope * 4 { + if _, err := engine.Create(ctx, domainmemory.NewRecord{ + Scope: domainmemory.Scope{Kind: domainmemory.ScopeAgentUser, Agent: "archie", User: "alice"}, + Kind: "note", + Content: fmt.Sprintf("agent-user filler %d", i), + Author: "test", + }); err != nil { + t.Fatalf("Create(agent-user filler %d) error = %v", i, err) + } + } + if _, err := engine.Create(ctx, domainmemory.NewRecord{ + Scope: domainmemory.Scope{Kind: domainmemory.ScopeGlobal}, Kind: "note", + Content: "global fact that must survive the flood", Author: "test", + }); err != nil { + t.Fatalf("Create(global) error = %v", err) + } + + runner, prepared := newMemoryTestRunner(t, engine, telegramIdentity, "archie") + if _, err := runner.Run(ctx, chatMsg("s1", "chat-alice", "alice", "hi"), nil); err != nil { + t.Fatalf("Run() error = %v", err) + } + + prompt := lastSystemPrompt(prepared) + if !strings.Contains(prompt, "global fact that must survive the flood") { + t.Fatalf("global scope was starved by agent-user scope's record volume, prompt = %q", prompt) + } +} + +// TestRenderMemoryRecordsCapBoundsEscapedSize is the escaping half of +// acceptance criterion 8: BuildSystemPrompt's `{{xml .Memory}}` expands "&", +// "<" and ">" up to fivefold after renderMemoryRecords applies its cap, so +// the cap must bound the *escaped* size the template will actually produce, +// not the pre-escape size renderMemoryRecords returns. +func TestRenderMemoryRecordsCapBoundsEscapedSize(t *testing.T) { + var records []domainmemory.Record + for i := range 500 { + records = append(records, domainmemory.Record{ + ID: domainmemory.RecordID(fmt.Sprintf("rec-%d", i)), + Scope: domainmemory.Scope{Kind: domainmemory.ScopeGlobal}, + Kind: "note", + // Every byte is one that escapeXML expands, so a cap measured on + // the raw (unescaped) size would let the escaped size through at + // up to 4x the intended bound. + Content: strings.Repeat("&", 100), + }) + } + + got := renderMemoryRecords(records, slog.New(slog.DiscardHandler)) + escaped := escapeXML(got) + if len(escaped) > memoryBlockByteCap { + t.Fatalf("escaped block = %d bytes, want <= %d (the size BuildSystemPrompt actually renders)", len(escaped), memoryBlockByteCap) + } +} + // TestRenderMemoryNilStoreIsANoOp guards the nil-engine composition case // (setupMemoryEngine failed or was never reached): a nil MemoryEngine must // not panic prepareTurn.