fix(session): reject sparse appends and isolate surface options

This commit is contained in:
liukx0205 authored and Kexin Liu committed 2026-11-07 17:02:19 +08:00
1 parent b3d570cdf8
commit 5e77db2d5c
2 files changed
+28 -3

No files matched your search

+8 -2
View File
@@ -862,8 +862,14 @@ export class Session {
this.publicationEntry()
if (inputs.length === 0) return []
const startSeq = this.log.length
const events = inputs.map(([type, data, surfaceOpts], index) => {
const snapshot = snapshotJsonValue({ type, data, ...surfaceOpts })
const events = Array.from(inputs, (input, index) => {
if (input === undefined) throw new Error(`session batch has an empty member at index ${index}`)
const [type, data, surfaceOpts] = input
const snapshot = snapshotJsonValue({
type, data,
...surfaceOpts?.surfaceOp === undefined ? {} : { surfaceOp: surfaceOpts.surfaceOp },
...surfaceOpts?.sourceEventSeqs === undefined ? {} : { sourceEventSeqs: surfaceOpts.sourceEventSeqs },
})
if (snapshot === undefined) throw new Error('session batch carries non-JSON-serializable data')
const event = {
...snapshot, seq: SessionSeq(startSeq + index), time: Date.now(),
+20 -1
View File
@@ -1,13 +1,32 @@
import { describe, expect, it, onTestFinished } from 'vitest'
import { Context } from '@deepseek-ai/cordis'
import { createUserMessage } from '@deepseek-ai/dsh-llm'
import SessionStore, { Session, SessionId, SessionSeq } from '@deepseek-ai/dsh-session'
import SessionStore, { Session, SessionId, SessionSeq, type SessionEventInput } from '@deepseek-ai/dsh-session'
function message(text: string) {
return createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'user' } })
}
describe('multiple Session appends', () => {
it('rejects sparse arrays before changing the log or surface', () => {
const session = Session.create(SessionId('sparse-append'))
const inputs = new Array<SessionEventInput>(3)
inputs[0] = ['turn/start', { turn: 1 }]
inputs[2] = ['user/message', message('last'), { surfaceOp: 'append' }]
expect(() => session.append(inputs)).toThrow(/empty member at index 1/)
expect(session.seq).toBe(0)
expect(session.surface.nodes).toEqual([])
})
it('reads only surface metadata from event options', () => {
const session = Session.create(SessionId('options-append'))
const old = session.append('user/message', message('old'), { surfaceOp: 'append' })
const next = message('new')
const events = session.append([['user/message', next, old]])
expect(events[0]?.data).toEqual(next)
expect(session.deriveMessages()).toEqual([old.data, next])
})
it('leaves the log unchanged for empty input and accepts a single array member', () => {
const session = Session.create(SessionId('empty-append'))
expect(session.append([])).toEqual([])