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.