fix(gateway): enforce per-scope memory quota and escape-aware byte cap
Greptile review on #887 (slice 3, merged): - BuiltinEngine.Query bounds Limit across the whole result, not per scope it is given. renderMemory passed one Limit for all of subject.Scopes() combined, so an early scope (agent-user, queried first) could exhaust the whole budget and starve later, wider scopes. Now queries each scope separately, each capped at memoryRecordsPerScope. - memoryBlockByteCap was checked against each line's raw size, but BuildSystemPrompt's {{xml .Memory}} escapes &/</> afterward, expanding a byte up to fivefold. The cap is now measured against the escaped size (escapeXML, extracted from prompt.go's template func) while the block itself still accumulates raw lines, since the template escapes once on render -- escaping here too would double-escape it.
This commit is contained in:
1 parent
c8f88c84b2
commit
1208458aba
3 files changed
+108
-14
No files matched your search
@@ -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),
|
||||
)
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user