Harden paged chat history loading

Close the per-thread history loading gate before launching requests, make live/history part reconciliation linear, and reject duplicate history envelope fields.

Complete task-owned history paging work remains preserved in the preceding recovery commit. Checked packaged templates and packaged authoring skill references: neither needs an update; docs/notes/large-chat-history.md records the stale shared documentation.
This commit is contained in:
2026-10-18 15:21:09 +01:00
parent ff1775e11f
commit 18e7d073a6
4 changed files with 40 additions and 14 deletions
+11 -8
View File
@@ -84,26 +84,29 @@ func readHistoryPage(ctx context.Context, sessionID string, r io.Reader) ([]prot
} else if descending {
p.take(nil, '{')
found := false
fields := make(map[string]struct{})
for err == nil {
key := &historyBuffer{limit: 256}
if err = p.str(key, nil); err != nil {
break
}
var name string
if !key.overflow {
_ = json.Unmarshal(key.Bytes(), &name)
if key.overflow || json.Unmarshal(key.Bytes(), &name) != nil {
err = fmt.Errorf("invalid history envelope field")
break
}
if _, duplicate := fields[name]; duplicate {
err = fmt.Errorf("duplicate history envelope field %q", name)
break
}
fields[name] = struct{}{}
if err = p.take(nil, ':'); err != nil {
break
}
switch name {
case "data":
if found {
err = fmt.Errorf("duplicate history data")
} else {
found = true
err = readMessages()
}
found = true
err = readMessages()
case "cursor":
cursor := &historyBuffer{limit: 64 << 10}
err = p.value(cursor, 1, false)
+2 -1
View File
@@ -138,7 +138,8 @@ func TestHistoryLegacyLargeToolAndNonJSON(t *testing.T) {
func TestHistoryRejectsMalformedIncludingDiscardedDetail(t *testing.T) {
for _, raw := range []string{
`null`, `{"data":null}`, `{"data":{}}`, `{"data":[],"cursor":{"next":42}}`,
`{"data":[]} {}`, `{"data":[}`, `{"data":[],}`, `{"data":[],"x":01}`,
`{"data":[],"data":[]}`, `{"data":[],"cursor":{"next":"one"},"cursor":{"next":"two"}}`,
`{"data":[]} {}`, `{"data":[}`, `{"data":[],}`, `{"data":[],"x":01}`, `{"data":[],"x":1,"x":2}`,
`{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20) + `\q"}}`,
`{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20) + `\u12G4"}}`,
`{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20),