set() on an idle agent appends plan/mode at once — no request boundary would arrive until the next prompt, so a queued intent used to hang as pending forever (the composer showed a dead pending target). A running agent keeps the boundary-flush path unchanged. set() now reports which branch ran (committed/queued/cancelled/noop); the /plan handler's copy follows the branch (idle: "Plan mode on/off", mid-turn: the next-step wording), and both commit paths share the header-delta narration. The invariant drops turn enclosure: plan/mode is a standalone whole-value event (the synthetic log-only turns removal already established the between-turns append shape). The fixture mirrors the idle commit.
97 lines
3.9 KiB
TypeScript
97 lines
3.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { Context } from 'cordis'
|
|
import SessionStore, { Session, SessionId, type SessionEvent } from '@deepseek-ai/dsh-session'
|
|
import * as PlanModeInvariant from '@deepseek-ai/dsh-plan-mode/invariant'
|
|
import InvariantService from '@deepseek-ai/dsh-invariants'
|
|
|
|
async function setup(): Promise<Context> {
|
|
const ctx = new Context()
|
|
await ctx.plugin(SessionStore)
|
|
await ctx.plugin(InvariantService, { enabled: true })
|
|
await ctx.plugin(PlanModeInvariant)
|
|
return ctx
|
|
}
|
|
|
|
function event(active: unknown): SessionEvent {
|
|
return { type: 'plan/mode', seq: 0, time: 0, data: { active } } as SessionEvent
|
|
}
|
|
|
|
function emitTurnStart(ctx: Context, session: Session): void {
|
|
ctx.emit('session/event', session, {
|
|
type: 'turn/start', seq: 0, time: 0,
|
|
data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } },
|
|
})
|
|
}
|
|
|
|
describe('plan-mode stream invariants', () => {
|
|
it('accepts either boolean state', async () => {
|
|
const ctx = await setup()
|
|
const session = new Session(SessionId('plan-state'))
|
|
emitTurnStart(ctx, session)
|
|
expect(() => { ctx.emit('session/event', session, event(true)) }).not.toThrow()
|
|
expect(() => { ctx.emit('session/event', session, event(false)) }).not.toThrow()
|
|
ctx.emit('session/event', session, {
|
|
type: 'turn/end', seq: 3, time: 3,
|
|
data: { turn: 1, reason: { kind: 'completed' } },
|
|
})
|
|
})
|
|
|
|
it.each([42, 'plan', undefined])('rejects invalid durable plan state %j', async (active) => {
|
|
const ctx = await setup()
|
|
const session = new Session(SessionId(`invalid-${String(active)}`))
|
|
emitTurnStart(ctx, session)
|
|
expect(() => { ctx.emit('session/event', session, event(active)) })
|
|
.toThrow(/expected a boolean/)
|
|
})
|
|
|
|
it('accepts standalone plan state between turns (the idle immediate commit)', async () => {
|
|
const ctx = await setup()
|
|
expect(() => ctx.sessions.create().append('plan/mode', { active: true }))
|
|
.not.toThrow()
|
|
})
|
|
|
|
it('ignores unrelated dispatches and session events', async () => {
|
|
const ctx = await setup()
|
|
const session = new Session(SessionId('unrelated'))
|
|
expect(() => {
|
|
ctx.emit('tools/change')
|
|
ctx.emit('session/event', session, {
|
|
type: 'turn/start', seq: 0, time: 0, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } },
|
|
})
|
|
}).not.toThrow()
|
|
})
|
|
|
|
it('rejects invalid existing state on late registration', async () => {
|
|
const ctx = new Context()
|
|
await ctx.plugin(SessionStore)
|
|
const session = ctx.sessions.create()
|
|
session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
session.append('plan/mode', { active: 'plan' as unknown as boolean })
|
|
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
await ctx.plugin(InvariantService, { enabled: true })
|
|
|
|
await expect(ctx.plugin(PlanModeInvariant).then(() => undefined)).rejects.toThrow(/expected a boolean/)
|
|
})
|
|
|
|
it('replays enclosed existing plan state through its closing boundary', async () => {
|
|
const ctx = new Context()
|
|
await ctx.plugin(SessionStore)
|
|
const session = ctx.sessions.create()
|
|
session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
session.append('plan/mode', { active: true })
|
|
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
await ctx.plugin(InvariantService, { enabled: true })
|
|
|
|
await expect(ctx.plugin(PlanModeInvariant).then(() => undefined)).resolves.toBeUndefined()
|
|
})
|
|
|
|
it('accepts standalone existing plan state on late registration', async () => {
|
|
const ctx = new Context()
|
|
await ctx.plugin(SessionStore)
|
|
ctx.sessions.create().append('plan/mode', { active: true })
|
|
await ctx.plugin(InvariantService, { enabled: true })
|
|
|
|
await expect(ctx.plugin(PlanModeInvariant).then(() => undefined)).resolves.toBeUndefined()
|
|
})
|
|
})
|