ai-compaction: checkpoint never reused when the store returns message keys in another order
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
TanStack AI version
@tanstack/ai-compaction 1.0.0, @tanstack/ai 1.0.1, @tanstack/ai-persistence 1.0.1
Framework/Library version
TanStack Start 1.169.4, React 19.3.1, Node 26. Messages and checkpoints stored in PostgreSQL jsonb.
Describe the bug and the steps to reproduce it
A compaction checkpoint is never reused when the message store gives messages back with their keys in a different order. Every request past the limit then runs the summarizer again over the same history.
hashMessages (packages/ai-compaction/src/usage-count.ts) hashes JSON.stringify(messages), and that output depends on key order. The checkpoint check compares stored.sourceHash with hashMessages(messages.slice(0, stored.sourceMessageCount)), where messages are the ones the store just loaded (dist/esm/index.js:562 in 1.0.0). The same applies to prefixHash and to lastCoveredHash in the usage count.
Key order isn't preserved by common stores:
- PostgreSQL
jsonbreorders an object's keys: shorter keys first, then by bytes. An assistant message built as{ role, content, metadata, toolCalls, id, createdAt }comes back as{ id, role, content, metadata, createdAt, toolCalls }. - Validating a loaded row with a schema library reorders keys too. Zod's
looseObjectreturns the declared keys first.
The messages are equal by value, which is what the persistence conformance tests check with toEqual. But they hash differently, so the checkpoint looks stale.
Steps:
- Use
withCompactionwith a checkpoint store and asummarizeOldeststrategy, and a message store whoseloadreturns each message with its keys in another order. For example, round-trip each message throughObject.fromEntries(Object.entries(m).sort()). - Send messages until the thread passes the limit. One summary runs and a checkpoint is saved.
- Send two more messages.
Expected: the two later requests reuse the checkpoint, so no new summary runs.
Actual: each request runs the summarizer again over the same history. In our app, the summary request was the same ~287 KB request on every message after the limit. Counted per message after the limit, the summary runs were [1, 1, 1].
The smallest form of the cause:
import { createHash } from 'node:crypto'
const hash = (v) => createHash('sha256').update(JSON.stringify(v)).digest('hex')
const saved = { role: 'user', content: 'hi', id: 'm1' }
const loaded = { id: 'm1', role: 'user', content: 'hi' } // same message, as jsonb returns it
console.log(hash([saved]) === hash([loaded])) // false
A fix that worked for us: hash a canonical form, with object keys sorted at every depth.
function sortedKeys(_key: string, value: unknown): unknown {
return typeof value === 'object' && value !== null && !Array.isArray(value)
? Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
: value
}
// in hashMessages:
const bytes = new TextEncoder().encode(JSON.stringify(messages, sortedKeys))
With that change, the runs per message after the limit were [1, 0, 0]. Checkpoints saved before the change miss once, and the next summary overwrites them.
Your Minimal, Reproducible Example
The snippet above shows the cause. The steps reproduce it with any store that reorders keys. We're running the fix above as a pnpm patch of 1.0.0.
- Ngôn ngữ chính
- TypeScript
- Star
- 3.3k
- Fork
- 378
- Merge trung bình
- 1 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 254
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/ai
-
openai-base: a Responses max_output_tokens cutoff ends as RUN_ERROR without usage instead of finishReason 'length'Có thể đã có người làm @tombeckenham đã nhận 8 ngày trước. Đang mởhas-pr waiting-on: maintainer
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
TanStack/ai#1916 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
AG-UI TOOL_CALL_RESULT metadata is dropped in the live stream reducerCó thể đã có người làm @c446984928 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 1 ngày
-
ai-sandbox: encrypt snapshot blobs at rest with an encrypting blob store wrapperCó thể đã có người làm @AlemTuzlak đã nhận 1 ngày trước. Đang mởwaiting-on: maintainer
TanStack/ai#2062 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Sandbox takeover: the saved transcript loses everything the first host delivered before the restartCó thể đã có người làm @tombeckenham đã nhận 1 ngày trước. Đang mởwaiting-on: maintainer
TanStack/ai#2061 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
withSandbox: each run's save drops the harness tool calls of earlier runsCó thể đã có người làm @tombeckenham đã nhận 1 ngày trước. Đang mởhas-pr waiting-on: maintainer
TanStack/ai#2059 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
The sample email preview's ResizeObserver is never disconnected and keeps raising errors after leaving SettingsCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area:ui bug effort:S priority:P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Typo in WSL project description on Projects pageCó thể đã có người làm @akhil9tiet đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
microsoft/opensource.microsoft.com#1066 · 1 bình luận ·
-
ArcGIS Maps SDK for JavaScript c-carousel calcite-design-tokens enhancement impact - p3 - not time sensitive needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Esri/calcite-design-system#15565 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
linkwarden/linkwarden#1891 ·
Maintainer thường phản hồi trong vòng 1 ngày