fix(agent): use ULID for session entry IDs and verify sort order in tests

This commit is contained in:
yiling
2026-05-21 12:37:47 +08:00
parent be07f6b26c
commit 74993c9803
2 changed files with 40 additions and 9 deletions
@@ -245,4 +245,40 @@ describe('DatabaseSessionStorage', () => {
const label = await storage.getLabel('label-1')
expect(label).toBe('Test Label')
})
it('should retrieve entries in order', async () => {
const { agent } = await setupTestData()
const storage = await DatabaseSessionStorage.create({ agentId: agent.id })
// Insert entries out of order (by insertion time) but with ordered IDs
await storage.appendEntry({
type: 'message',
id: '01B',
parentId: null,
timestamp: new Date().toISOString(),
message: { role: 'user', content: [], timestamp: Date.now() },
})
await storage.appendEntry({
type: 'message',
id: '01A',
parentId: null,
timestamp: new Date().toISOString(),
message: { role: 'user', content: [], timestamp: Date.now() },
})
await storage.appendEntry({
type: 'message',
id: '01C',
parentId: null,
timestamp: new Date().toISOString(),
message: { role: 'user', content: [], timestamp: Date.now() },
})
const entries = await storage.getEntries()
expect(entries).toHaveLength(3)
expect(entries[0].id).toBe('01A')
expect(entries[1].id).toBe('01B')
expect(entries[2].id).toBe('01C')
})
})
+4 -9
View File
@@ -1,5 +1,6 @@
import { prisma } from '@/db'
import { s3Service } from '@/services/s3/s3'
import { ulid } from 'ulid'
import {
type AgentMessage,
type SessionMetadata,
@@ -47,15 +48,11 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
}
async createEntryId(): Promise<string> {
// We can use ULID from prisma or crypto.randomUUID() if needed,
// but the harness expects a string.
// The previous implementation used the entry's own ID.
// Let's use crypto.randomUUID for now or a ULID generator if available.
return crypto.randomUUID()
return ulid()
}
async appendEntry(entry: SessionTreeEntry): Promise<void> {
const strippedEntry = JSON.parse(JSON.stringify(entry))
const strippedEntry = structuredClone(entry)
// Strip image data before saving to DB
if (strippedEntry.type === 'message') {
@@ -79,9 +76,7 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
data: {
id: entry.id,
sessionId: this.sessionId,
// Entry is stored as Json in Prisma and bound to PiSessionEntry via prisma-json-types
// eslint-disable-next-line @typescript-eslint/no-explicit-any
entry: strippedEntry as any,
entry: strippedEntry,
},
})