|
|
@@ -1,1682 +1,797 @@
|
|
|
-import { describe, expect, it } from 'vitest'
|
|
|
+import { describe, expect, it, vi } from 'vitest'
|
|
|
import { Context } from 'cordis'
|
|
|
-import { BasicCompactService } from '@deepseek-ai/dsh-compact-basic'
|
|
|
+import BasicCompactService, {
|
|
|
+ resolveConfig,
|
|
|
+ resolveModelConfig,
|
|
|
+} from '@deepseek-ai/dsh-compact-basic'
|
|
|
import type { BasicCompactConfig } from '@deepseek-ai/dsh-compact-basic'
|
|
|
+import { selectCompactableRange } from '@deepseek-ai/dsh-compact-basic/src/region.ts'
|
|
|
+import type { CompactionResult } from '@deepseek-ai/dsh-compact'
|
|
|
+import LlmService, { CallId, LlmAdapter } from '@deepseek-ai/dsh-llm'
|
|
|
import type { ContentBlock, GenerateOptions, Message, StreamChunk } from '@deepseek-ai/dsh-llm'
|
|
|
-import { CallId, LlmAdapter, LlmService } from '@deepseek-ai/dsh-llm'
|
|
|
-import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
|
|
|
-import type { SessionEvent, SurfaceEvent } from '@deepseek-ai/dsh-session'
|
|
|
-import * as Invariants from '@deepseek-ai/dsh-invariants'
|
|
|
+import { Session, SessionId } from '@deepseek-ai/dsh-session'
|
|
|
+import TokenMeterService, {
|
|
|
+ TOKEN_METER_MODEL_UNCONFIGURED,
|
|
|
+ TokenMeterError,
|
|
|
+} from '@deepseek-ai/dsh-token-meter'
|
|
|
import type { Agent } from '@deepseek-ai/dsh-agent'
|
|
|
|
|
|
-/** A never-aborted signal for the required `compactIfNeeded`/listener arg. */
|
|
|
const SIGNAL = new AbortController().signal
|
|
|
+const MODEL = 'test-model'
|
|
|
|
|
|
-/**
|
|
|
- * Baseline config with every required knob set. `BasicCompactConfig` has no
|
|
|
- * defaults for the numeric/model knobs (only `auto` defaults), so each test
|
|
|
- * builds a complete config via `cfg()` and overrides only the knob under test.
|
|
|
- */
|
|
|
-const TEST_CONFIG: BasicCompactConfig = {
|
|
|
- contextWindow: 128000,
|
|
|
- thresholdRatio: 0.8,
|
|
|
- retainTokens: 20480,
|
|
|
- summarizationModel: '',
|
|
|
- maxTokens: 8192,
|
|
|
- compactionRetries: 1,
|
|
|
-}
|
|
|
-
|
|
|
-/** A complete config with `overrides` applied over the baseline. */
|
|
|
-function cfg(overrides: Partial<BasicCompactConfig> = {}): BasicCompactConfig {
|
|
|
- return { ...TEST_CONFIG, ...overrides }
|
|
|
-}
|
|
|
-
|
|
|
-/** Long enough that the real checkpoint preamble is smaller than two fixture messages. */
|
|
|
-const LONG_FIXTURE_TEXT = ' Detailed fixture context that makes framed checkpoint compaction genuinely shrinking.'.repeat(20)
|
|
|
-
|
|
|
-/**
|
|
|
- * A BasicCompactService with summarize() stubbed (no real model call) and a
|
|
|
- * predictable token estimate, for deterministic unit tests of the algorithm.
|
|
|
- */
|
|
|
-class TestCompactService extends BasicCompactService {
|
|
|
- private readonly summaryOutputs = new WeakSet<readonly ContentBlock[]>()
|
|
|
- /** Boundary/unit tests use tiny fixtures; keep framing from dominating them unless a test opts out. */
|
|
|
- estimateFramedSummariesCheaply = true
|
|
|
- /** Track calls to summarize for test assertions. */
|
|
|
- summarizeCalls: { text: string; model: string }[] = []
|
|
|
- /** The fixed summary to return. */
|
|
|
- mockSummary: ContentBlock[] = [{ type: 'text', text: 'Test summary of compacted content.' }]
|
|
|
- /** Per-call summaries; when set, each summarize() call shifts one value. */
|
|
|
- mockSummaryQueue: ContentBlock[][] = []
|
|
|
- /** If set, summarize() throws this error. */
|
|
|
- summarizeError: Error | null = null
|
|
|
-
|
|
|
- override estimateContentTokens(blocks: readonly ContentBlock[]): number {
|
|
|
- if (this.summaryOutputs.has(blocks)) return blocks.length * 2
|
|
|
- if (this.estimateFramedSummariesCheaply && isFramedCheckpoint(blocks)) return blocks.length * 2
|
|
|
- // 10 tokens per block — predictable for retention/threshold math.
|
|
|
- return blocks.length * 10
|
|
|
- }
|
|
|
-
|
|
|
- override async summarize(text: string, agent: Agent): Promise<{ summary: ContentBlock[]; model: string; maxTokens?: number }> {
|
|
|
- const model = this.config.summarizationModel || agent.options.model || ''
|
|
|
- this.summarizeCalls.push({ text, model })
|
|
|
- if (this.summarizeError) throw this.summarizeError
|
|
|
- const summary = this.mockSummaryQueue.shift() ?? this.mockSummary
|
|
|
- this.summaryOutputs.add(summary)
|
|
|
- return { summary, model }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function isFramedCheckpoint(blocks: readonly ContentBlock[]): boolean {
|
|
|
- const first = blocks[0]
|
|
|
- const last = blocks[blocks.length - 1]
|
|
|
- return first?.type === 'text'
|
|
|
- && first.text.includes('<compacted-summary>')
|
|
|
- && last?.type === 'text'
|
|
|
- && last.text === '</compacted-summary>'
|
|
|
+function createContext(
|
|
|
+ models: Record<string, { contextWindow?: number; charsPerToken?: number }> = {
|
|
|
+ [MODEL]: { contextWindow: 100, charsPerToken: 1_000 },
|
|
|
+ },
|
|
|
+): Context {
|
|
|
+ const ctx = new Context()
|
|
|
+ void new TokenMeterService(ctx, { models })
|
|
|
+ return ctx
|
|
|
}
|
|
|
|
|
|
-/** Create a test service with a throwaway context (auto disabled — no model). */
|
|
|
-function createTestService(overrides: Partial<BasicCompactConfig> = {}): TestCompactService {
|
|
|
- return new TestCompactService(new Context(), cfg({ auto: false, ...overrides }))
|
|
|
+function agent(session: Session, model?: string): Agent {
|
|
|
+ return { session, options: model === undefined ? {} : { model } } as Agent
|
|
|
}
|
|
|
|
|
|
-/** Build closed turns plus an open compaction turn unless `leaveOpen` is false. */
|
|
|
-function multiTurnSession(turns: number, messagesPerTurn: number = 2, opts: { leaveOpen?: boolean } = {}): Session {
|
|
|
- const leaveOpen = opts.leaveOpen ?? true
|
|
|
- const s = new Session(SessionId('test'))
|
|
|
- for (let t = 1; t <= turns; t++) {
|
|
|
- s.append('turn/start', { turn: t, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: t, step: 1 })
|
|
|
- for (let m = 0; m < messagesPerTurn; m++) {
|
|
|
- s.append('user/message', {
|
|
|
- content: [{ type: 'text', text: `turn ${t} user message ${m + 1}.${LONG_FIXTURE_TEXT}` }],
|
|
|
- source: { kind: 'user' },
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: t, step: 1,
|
|
|
- content: [{ type: 'text', text: `turn ${t} assistant response ${m + 1}.${LONG_FIXTURE_TEXT}` }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- }
|
|
|
- s.append('step/end', { turn: t, step: 1 })
|
|
|
- s.append('turn/end', { turn: t, reason: { kind: 'completed' } })
|
|
|
- }
|
|
|
- // Open one more turn so compaction's events are turn-enclosed, as they are
|
|
|
- // when the loop runs the auto-compaction listener mid-turn.
|
|
|
- if (leaveOpen) {
|
|
|
- s.append('turn/start', { turn: turns + 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
+/** Closed two-message turns followed by one open turn for durable compaction events. */
|
|
|
+function conversation(turns = 4, text = 'fixture'): Session {
|
|
|
+ const session = new Session(SessionId(`conversation-${turns}`))
|
|
|
+ for (let turn = 1; turn <= turns; turn += 1) {
|
|
|
+ session.append('turn/start', { turn, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
+ session.append('user/message', {
|
|
|
+ content: [{ type: 'text', text: `${text} user ${turn}` }],
|
|
|
+ source: { kind: 'user' },
|
|
|
+ }, { surfaceOp: 'append' })
|
|
|
+ session.append('step/start', { turn, step: 1 })
|
|
|
+ session.append('assistant/message', {
|
|
|
+ turn,
|
|
|
+ step: 1,
|
|
|
+ content: [{ type: 'text', text: `${text} assistant ${turn}` }],
|
|
|
+ }, { surfaceOp: 'append' })
|
|
|
+ session.append('step/end', { turn, step: 1 })
|
|
|
+ session.append('turn/end', { turn, reason: { kind: 'completed' } })
|
|
|
}
|
|
|
- return s
|
|
|
-}
|
|
|
-
|
|
|
-/** Build a session with tool calls for richer extraction tests. */
|
|
|
-function sessionWithTools(): Session {
|
|
|
- const s = new Session(SessionId('tools'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('user/message', {
|
|
|
- content: [{ type: 'text', text: 'read file x' }],
|
|
|
- source: { kind: 'user' },
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [
|
|
|
- { type: 'text', text: 'Let me read that file.' },
|
|
|
- { type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{"command":"cat x"}' },
|
|
|
- ],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('tool/call', { turn: 1, step: 1, callId: CallId('c1'), name: 'bash', arguments: '{"command":"cat x"}' })
|
|
|
- s.append('tool/result', {
|
|
|
- turn: 1, step: 1, callId: CallId('c1'),
|
|
|
- content: [{ type: 'text', text: 'hello world' }],
|
|
|
- isError: false,
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [{ type: 'text', text: 'The file contains: hello world' }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- // Open a trailing turn so compaction's events are turn-enclosed (as they are
|
|
|
- // when the loop runs the auto-compaction listener mid-turn).
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- return s
|
|
|
+ session.append('turn/start', {
|
|
|
+ turn: turns + 1,
|
|
|
+ trigger: { kind: 'message', source: { kind: 'user' } },
|
|
|
+ })
|
|
|
+ return session
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * Build a session of `turns` turns, each a SINGLE step containing an
|
|
|
- * assistant/message that issues a tool-call plus its tool/result — the real
|
|
|
- * multi-node-step shape (a step is two surface nodes: the assistant and the
|
|
|
- * result). Each turn is preceded by a user/message. Used to exercise
|
|
|
- * step-alignment: a region boundary must not fall between the assistant and its
|
|
|
- * result.
|
|
|
- */
|
|
|
-function toolTurnSession(turns: number): Session {
|
|
|
- const s = new Session(SessionId('tools-multi'))
|
|
|
- for (let t = 1; t <= turns; t++) {
|
|
|
- s.append('turn/start', { turn: t, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('user/message', {
|
|
|
- content: [{ type: 'text', text: `turn ${t} request` }],
|
|
|
+function toolConversation(): Session {
|
|
|
+ const session = new Session(SessionId('tools'))
|
|
|
+ for (let turn = 1; turn <= 3; turn += 1) {
|
|
|
+ const callId = CallId(`call-${turn}`)
|
|
|
+ session.append('turn/start', { turn, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
+ session.append('user/message', {
|
|
|
+ content: [{ type: 'text', text: `request ${turn} `.repeat(300) }],
|
|
|
source: { kind: 'user' },
|
|
|
}, { surfaceOp: 'append' })
|
|
|
- s.append('step/start', { turn: t, step: 1 })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: t, step: 1,
|
|
|
+ session.append('step/start', { turn, step: 1 })
|
|
|
+ session.append('assistant/message', {
|
|
|
+ turn,
|
|
|
+ step: 1,
|
|
|
content: [
|
|
|
- { type: 'text', text: `turn ${t} calling tool` },
|
|
|
- { type: 'tool-call', id: CallId(`c${t}`), name: 'bash', arguments: '{"command":"ls"}' },
|
|
|
+ { type: 'text', text: `calling ${turn} `.repeat(300) },
|
|
|
+ { type: 'tool-call', id: callId, name: 'read', arguments: '{}' },
|
|
|
],
|
|
|
}, { surfaceOp: 'append' })
|
|
|
- s.append('tool/call', { turn: t, step: 1, callId: CallId(`c${t}`), name: 'bash', arguments: '{"command":"ls"}' })
|
|
|
- s.append('tool/result', {
|
|
|
- turn: t, step: 1, callId: CallId(`c${t}`),
|
|
|
- content: [{ type: 'text', text: `turn ${t} output` }],
|
|
|
+ session.append('tool/call', { turn, step: 1, callId, name: 'read', arguments: '{}' })
|
|
|
+ session.append('tool/result', {
|
|
|
+ turn,
|
|
|
+ step: 1,
|
|
|
+ callId,
|
|
|
+ content: [{ type: 'text', text: `result ${turn} `.repeat(300) }],
|
|
|
isError: false,
|
|
|
}, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: t, step: 1 })
|
|
|
- s.append('turn/end', { turn: t, reason: { kind: 'completed' } })
|
|
|
+ session.append('step/end', { turn, step: 1 })
|
|
|
+ session.append('turn/end', { turn, reason: { kind: 'completed' } })
|
|
|
}
|
|
|
- // Open a trailing turn so compaction's events are turn-enclosed.
|
|
|
- s.append('turn/start', { turn: turns + 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- return s
|
|
|
+ session.append('turn/start', { turn: 4, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
+ return session
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * Assert the derived transcript has NO orphaned tool-result: every
|
|
|
- * `tool-result` block's `toolCallId` must be matched by a preceding `tool-call`
|
|
|
- * block in an earlier (assistant) message. A dangling tool-result is exactly
|
|
|
- * what splitting a step at compaction produces, and every provider rejects it.
|
|
|
- */
|
|
|
-function expectNoOrphanToolResults(messages: Message[]): void {
|
|
|
- const seenCallIds = new Set<string>()
|
|
|
- for (const msg of messages) {
|
|
|
- for (const block of msg.content) {
|
|
|
- if (block.type === 'tool-call') seenCallIds.add(block.id)
|
|
|
- if (block.type === 'tool-result') {
|
|
|
- expect(seenCallIds.has(block.toolCallId),
|
|
|
- `orphaned tool-result for callId ${block.toolCallId} (no preceding tool-call)`).toBe(true)
|
|
|
- }
|
|
|
- }
|
|
|
+class TestCompactService extends BasicCompactService {
|
|
|
+ summary: ContentBlock[] = [{ type: 'text', text: 'small checkpoint' }]
|
|
|
+ summaryModel = 'summary-model'
|
|
|
+ error: unknown
|
|
|
+ mutateDuringSummary: (() => void) | undefined
|
|
|
+ calls: Array<{ text: string; signal: AbortSignal | undefined }> = []
|
|
|
+
|
|
|
+ override async summarize(
|
|
|
+ text: string,
|
|
|
+ _agent: Agent,
|
|
|
+ signal?: AbortSignal,
|
|
|
+ ): Promise<{ summary: ContentBlock[]; model: string; maxTokens?: number }> {
|
|
|
+ this.calls.push({ text, signal })
|
|
|
+ this.mutateDuringSummary?.()
|
|
|
+ if (this.error !== undefined) throw this.error
|
|
|
+ return { summary: this.summary, model: this.summaryModel, maxTokens: 123 }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-describe('BasicCompactService step-alignment (never split a tool-call/result pair)', () => {
|
|
|
- it('compactIfNeeded rounds the retained boundary head-ward to keep a whole step (no orphaned tool-result)', async () => {
|
|
|
- // Retain the recent tail while the older assistant/result pairs compact as
|
|
|
- // whole units; no boundary may orphan a result.
|
|
|
- const svc = createTestService({ contextWindow: 280, thresholdRatio: 0.5, retainTokens: 55 })
|
|
|
- const session = toolTurnSession(3)
|
|
|
-
|
|
|
- const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
|
|
|
- expect(result).not.toBeNull()
|
|
|
- expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
|
|
|
- // No dangling tool-result: every compacted/retained step stayed whole.
|
|
|
- expectNoOrphanToolResults(session.deriveMessages())
|
|
|
- // The most-recent step's result is retained verbatim (still on the surface).
|
|
|
- const lastResultSeq = session.events.findLast(e => e.type === 'tool/result')!.seq
|
|
|
- expect(result!.shadowedSeqs).not.toContain(lastResultSeq)
|
|
|
- })
|
|
|
-
|
|
|
- it('compactIfNeeded returns null when the only compactable region is an un-splittable single step', async () => {
|
|
|
- // The only candidate cut is inside one assistant/result pair; with no safe
|
|
|
- // compactable prefix, decline rather than split it.
|
|
|
- const s = new Session(SessionId('one-step'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [{ type: 'text', text: 'calling' }, { type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{}' }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('tool/call', { turn: 1, step: 1, callId: CallId('c1'), name: 'bash', arguments: '{}' })
|
|
|
- s.append('tool/result', { turn: 1, step: 1, callId: CallId('c1'), content: [{ type: 'text', text: 'out' }], isError: false }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- // Turn stays open.
|
|
|
-
|
|
|
- const svc = createTestService({ contextWindow: 100, thresholdRatio: 0.1, retainTokens: 5 })
|
|
|
- const result = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
|
|
|
- expect(result).toBeNull()
|
|
|
- expect(s.events.some(e => e.type === 'compact/start')).toBe(false)
|
|
|
- })
|
|
|
-
|
|
|
- it('compactRegion rejects a start that splits a step (unbalanced boundary)', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = toolTurnSession(1)
|
|
|
- const nodes = session.surface.nodes // [user, asst(tool-call), result]
|
|
|
- const userSeq = nodes[0]!.seq
|
|
|
- const resultSeq = nodes[2]!.seq
|
|
|
- // start = the tool/result: its issuing assistant precedes it IN THE SAME STEP,
|
|
|
- // so starting here would orphan that assistant's tool-call. end is fine (user).
|
|
|
- await expect(compactRegion(svc, session, resultSeq, resultSeq, 'm'))
|
|
|
- .rejects.toThrow(/start seq .* is not a balanced boundary/)
|
|
|
- expect(userSeq).toBeLessThan(resultSeq) // sanity: ordering as expected
|
|
|
- })
|
|
|
-
|
|
|
- it('compactRegion rejects an end that splits a step (unbalanced boundary)', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = toolTurnSession(1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
- const userSeq = nodes[0]!.seq
|
|
|
- const asstSeq = nodes[1]!.seq
|
|
|
- // end = the assistant/message: its tool/result follows IN THE SAME STEP, so
|
|
|
- // ending here would strand that result. start is fine (the pre-step user).
|
|
|
- await expect(compactRegion(svc, session, userSeq, asstSeq, 'm'))
|
|
|
- .rejects.toThrow(/end seq .* is not a balanced boundary/)
|
|
|
- })
|
|
|
-
|
|
|
- it('compactRegion rejects an end inside an open tail step', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('open-tail'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [{ type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{}' }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- const nodes = s.surface.nodes // [user, asst]
|
|
|
- const userSeq = nodes[0]!.seq
|
|
|
- const asstSeq = nodes[1]!.seq
|
|
|
- await expect(compactRegion(svc, s, userSeq, asstSeq, 'm'))
|
|
|
- .rejects.toThrow(/end seq .* is not a balanced boundary/)
|
|
|
- })
|
|
|
-
|
|
|
- it('compactRegion accepts step-aligned boundaries (pre-step user → last result of a closed step)', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = toolTurnSession(2)
|
|
|
- const nodes = session.surface.nodes // [user1, asst1, res1, user2, asst2, res2]
|
|
|
- const startSeq = nodes[0]!.seq // pre-step user1 (free boundary)
|
|
|
- const endSeq = nodes[2]!.seq // res1 = last node of turn 1's closed step
|
|
|
- const result = await compactRegion(svc, session, startSeq, endSeq, 'm')
|
|
|
- expect(result.shadowedRange).toEqual({ start: startSeq, end: endSeq })
|
|
|
- expectNoOrphanToolResults(session.deriveMessages())
|
|
|
- })
|
|
|
+function service(
|
|
|
+ config: BasicCompactConfig = { auto: false },
|
|
|
+ ctx = createContext(),
|
|
|
+): TestCompactService {
|
|
|
+ return new TestCompactService(ctx, config)
|
|
|
+}
|
|
|
|
|
|
- it('compactRegion accepts a single inter-step node (start === end on a pre-step user/message)', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = toolTurnSession(1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
- const userSeq = nodes[0]!.seq // pre-step user: free boundary both ways
|
|
|
- const result = await compactRegion(svc, session, userSeq, userSeq, 'm')
|
|
|
- expect(result.shadowedRange).toEqual({ start: userSeq, end: userSeq })
|
|
|
- })
|
|
|
+async function compactIfNeeded(
|
|
|
+ compact: BasicCompactService,
|
|
|
+ session: Session,
|
|
|
+ model: string | undefined = MODEL,
|
|
|
+ system = '',
|
|
|
+ prefix: readonly Message[] = [],
|
|
|
+): Promise<CompactionResult | null> {
|
|
|
+ return compact.compactIfNeeded(agent(session, model), system, prefix, SIGNAL)
|
|
|
+}
|
|
|
|
|
|
- it('compactRegion accepts an injection-turn context node (no step at all)', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('inject'))
|
|
|
- // An idle inject(): turn/start → context/message, NO step. A later turn is
|
|
|
- // open so compaction's events are turn-enclosed.
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'injection', source: { kind: 'user' } } })
|
|
|
- s.append('context/message', { content: [{ type: 'text', text: 'ctx' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- const nodes = s.surface.nodes
|
|
|
- const ctxSeq = nodes[0]!.seq
|
|
|
- const result = await compactRegion(svc, s, ctxSeq, ctxSeq, 'm')
|
|
|
- expect(result.shadowedRange).toEqual({ start: ctxSeq, end: ctxSeq })
|
|
|
- })
|
|
|
-})
|
|
|
+describe('compact configuration and defaults', () => {
|
|
|
+ it('uses low-friction common and per-profile defaults', () => {
|
|
|
+ const ctx = createContext({
|
|
|
+ [MODEL]: { contextWindow: 100, charsPerToken: 1_000 },
|
|
|
+ large: { contextWindow: 1_000, charsPerToken: 4 },
|
|
|
+ })
|
|
|
+ const resolved = resolveConfig({}, ctx.tokenMeter)
|
|
|
|
|
|
-describe('BasicCompactService.estimateEventTokens', () => {
|
|
|
- it('returns 0 for non-message events (boundary, chunk, step/end, tool/call)', () => {
|
|
|
- const svc = createTestService()
|
|
|
- expect(svc.estimateEventTokens({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } })).toBe(0)
|
|
|
- expect(svc.estimateEventTokens({ type: 'step/start', seq: 1, time: 2, data: { turn: 1, step: 1 } })).toBe(0)
|
|
|
- expect(svc.estimateEventTokens({ type: 'assistant/chunk', seq: 2, time: 3, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'h' } } })).toBe(0)
|
|
|
- expect(svc.estimateEventTokens({ type: 'step/end', seq: 3, time: 4, data: { turn: 1, step: 1 } })).toBe(0)
|
|
|
- expect(svc.estimateEventTokens({ type: 'tool/call', seq: 4, time: 5, data: { turn: 1, step: 1, callId: CallId('c1'), name: 'read', arguments: '{}' } })).toBe(0)
|
|
|
+ expect(resolved).toEqual({
|
|
|
+ models: {},
|
|
|
+ summarizationModel: '',
|
|
|
+ maxTokens: 8192,
|
|
|
+ compactionRetries: 1,
|
|
|
+ auto: true,
|
|
|
+ })
|
|
|
+ expect(resolveModelConfig(resolved, ctx.tokenMeter.resolve(MODEL))).toEqual({
|
|
|
+ model: MODEL,
|
|
|
+ contextWindow: 100,
|
|
|
+ thresholdRatio: 0.8,
|
|
|
+ retainTokens: 16,
|
|
|
+ })
|
|
|
+ expect(resolveModelConfig(resolved, ctx.tokenMeter.resolve('large')).retainTokens).toBe(160)
|
|
|
+ expect(Object.isFrozen(resolved)).toBe(true)
|
|
|
})
|
|
|
|
|
|
- it('returns estimate for message-producing events', () => {
|
|
|
- const svc = createTestService()
|
|
|
- const userEvent: SessionEvent = { type: 'user/message', seq: 0, time: 1, data: { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } } }
|
|
|
- expect(svc.estimateEventTokens(userEvent)).toBe(10)
|
|
|
-
|
|
|
- const asstEvent: SessionEvent = { type: 'assistant/message', seq: 1, time: 2, data: { turn: 1, step: 1, content: [{ type: 'text', text: 'a' }, { type: 'text', text: 'b' }] } }
|
|
|
- expect(svc.estimateEventTokens(asstEvent)).toBe(20)
|
|
|
+ it('merges threshold and retention overrides field-wise', () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ const thresholdOnly = resolveConfig({
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.5 } },
|
|
|
+ }, ctx.tokenMeter)
|
|
|
+ expect(resolveModelConfig(thresholdOnly, ctx.tokenMeter.resolve(MODEL))).toMatchObject({
|
|
|
+ thresholdRatio: 0.5,
|
|
|
+ retainTokens: 16,
|
|
|
+ })
|
|
|
|
|
|
- const toolEvent: SessionEvent = { type: 'tool/result', seq: 2, time: 3, data: { turn: 1, step: 1, callId: CallId('c1'), content: [{ type: 'text', text: 'output' }], isError: false } }
|
|
|
- expect(svc.estimateEventTokens(toolEvent)).toBe(10)
|
|
|
+ const retentionOnly = resolveConfig({
|
|
|
+ models: { [MODEL]: { retainTokens: 7 } },
|
|
|
+ }, ctx.tokenMeter)
|
|
|
+ expect(resolveModelConfig(retentionOnly, ctx.tokenMeter.resolve(MODEL))).toMatchObject({
|
|
|
+ thresholdRatio: 0.8,
|
|
|
+ retainTokens: 7,
|
|
|
+ })
|
|
|
})
|
|
|
-})
|
|
|
|
|
|
-describe('BasicCompactService.estimateTokens', () => {
|
|
|
- it('sums token estimates across messages', () => {
|
|
|
- const svc = createTestService()
|
|
|
- const messages: Message[] = [
|
|
|
- { role: 'user', content: [{ type: 'text', text: 'hello' }] },
|
|
|
- { role: 'assistant', content: [{ type: 'text', text: 'hi' }, { type: 'text', text: 'there' }] },
|
|
|
- ]
|
|
|
- // 1 block * 10 + 4 (role) + 2 blocks * 10 + 4 (role) = 10 + 4 + 20 + 4 = 38
|
|
|
- expect(svc.estimateTokens(messages)).toBe(38)
|
|
|
+ it('validates common values and model policy invariants', () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ const bad = [
|
|
|
+ [{ maxTokens: 0 }, /maxTokens/],
|
|
|
+ [{ compactionRetries: -1 }, /compactionRetries/],
|
|
|
+ [{ auto: 'yes' }, /auto must be a boolean/],
|
|
|
+ [{ summarizationModel: 1 }, /summarizationModel must be a string/],
|
|
|
+ [{ models: null }, /models must be an object/],
|
|
|
+ [{ models: { [MODEL]: null } }, /must be an object/],
|
|
|
+ [{ models: { [MODEL]: { thresholdRatio: 0 } } }, /number in \(0, 1\]/],
|
|
|
+ [{ models: { [MODEL]: { thresholdRatio: 1.1 } } }, /number in \(0, 1\]/],
|
|
|
+ [{ models: { [MODEL]: { retainTokens: -1 } } }, /non-negative integer/],
|
|
|
+ [{ models: { [MODEL]: { thresholdRatio: 0.5, retainTokens: 50 } } }, /less than threshold/],
|
|
|
+ ] as Array<[unknown, RegExp]>
|
|
|
+
|
|
|
+ for (const [config, pattern] of bad) {
|
|
|
+ expect(() => resolveConfig(config as BasicCompactConfig, ctx.tokenMeter)).toThrow(pattern)
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
- it('includes system prompt in the estimate', () => {
|
|
|
- const svc = createTestService()
|
|
|
- const messages: Message[] = [
|
|
|
- { role: 'user', content: [{ type: 'text', text: 'hi' }] },
|
|
|
- ]
|
|
|
- const systemPrompt = 'You are a helpful assistant.'
|
|
|
- // 1 block * 10 + 4 (role) + ceil(28/4) = 10 + 4 + 7 = 21
|
|
|
- expect(svc.estimateTokens(messages, systemPrompt)).toBe(21)
|
|
|
+ it('rejects an override for an unknown meter profile with the exact typed error', () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ expect(() => resolveConfig({ models: { missing: { retainTokens: 1 } } }, ctx.tokenMeter))
|
|
|
+ .toThrow(expect.objectContaining({
|
|
|
+ code: TOKEN_METER_MODEL_UNCONFIGURED,
|
|
|
+ model: 'missing',
|
|
|
+ }))
|
|
|
})
|
|
|
})
|
|
|
|
|
|
-describe('BasicCompactService.compactRegion', () => {
|
|
|
- it('shadows surface nodes and inserts a summary via user/message', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(3, 1) // 3 turns, 2 surface nodes each = 6 nodes
|
|
|
+describe('pressure measurement and retention', () => {
|
|
|
+ const compactConfig: BasicCompactConfig = {
|
|
|
+ auto: false,
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.5, retainTokens: 18 } },
|
|
|
+ }
|
|
|
|
|
|
- const nodes = session.surface.nodes
|
|
|
- expect(nodes.length).toBe(6)
|
|
|
-
|
|
|
- const firstSeq = nodes[0]!.seq
|
|
|
- const secondSeq = nodes[1]!.seq
|
|
|
- const result = await compactRegion(svc, session, firstSeq, secondSeq, 'test-model')
|
|
|
-
|
|
|
- expect(result.shadowedSeqs).toEqual([firstSeq, secondSeq])
|
|
|
- expect(result.shadowedRange.start).toBe(firstSeq)
|
|
|
- expect(result.shadowedRange.end).toBe(secondSeq)
|
|
|
- expect(result.summary).toEqual(svc.mockSummary)
|
|
|
-
|
|
|
- const events = session.events
|
|
|
- const startEvent = events.findLast(e => e.type === 'compact/start')
|
|
|
- const summaryEvent = events.findLast(e => e.type === 'compact/summary')
|
|
|
- const endEvent = events.findLast(e => e.type === 'compact/end')
|
|
|
- expect(startEvent).toBeDefined()
|
|
|
- expect(summaryEvent).toBeDefined()
|
|
|
- expect(endEvent).toBeDefined()
|
|
|
- // The provenance record carries the summarize call's envelope, so "which
|
|
|
- // model wrote this summary" is answerable from the log alone.
|
|
|
- expect(summaryEvent?.type === 'compact/summary' && summaryEvent.data.model).toBe('test-model')
|
|
|
-
|
|
|
- // compact/* events are log-only — no surfaceOp (type system enforces this).
|
|
|
- const startRaw = startEvent as unknown as { surfaceOp?: unknown }
|
|
|
- expect(startRaw.surfaceOp).toBeUndefined()
|
|
|
-
|
|
|
- // The user/message carries the replace surfaceOp.
|
|
|
- const userMsg = events.findLast(e => e.type === 'user/message')!
|
|
|
- const surfaceUserMsg = userMsg as SurfaceEvent
|
|
|
- expect(surfaceUserMsg.surfaceOp).toEqual({ op: 'replace', start: firstSeq, end: secondSeq })
|
|
|
- expect(surfaceUserMsg.sourceEventSeqs).toContain(startEvent!.seq)
|
|
|
- expect(surfaceUserMsg.sourceEventSeqs).toContain(summaryEvent!.seq)
|
|
|
- expect(surfaceUserMsg.sourceEventSeqs).toContain(firstSeq)
|
|
|
- expect(surfaceUserMsg.sourceEventSeqs).toContain(secondSeq)
|
|
|
- // compact/end is appended AFTER the replacement (the lock brackets the whole
|
|
|
- // op), so the replacement cannot reference it — sourceEventSeqs may only
|
|
|
- // reference earlier seqs.
|
|
|
- expect(surfaceUserMsg.sourceEventSeqs).not.toContain(endEvent!.seq)
|
|
|
- expect(endEvent!.seq).toBeGreaterThan(userMsg.seq)
|
|
|
-
|
|
|
- // Surface now has: summary user/message + retained 4 nodes = 5 nodes.
|
|
|
- const newNodes = session.surface.nodes
|
|
|
- expect(newNodes.length).toBe(5)
|
|
|
- expect(newNodes[0]!.seq).toBe(userMsg.seq)
|
|
|
-
|
|
|
- // deriveMessages() produces the framed summary as a user-role message:
|
|
|
- // a checkpoint preamble + tag-wrapped summary blocks.
|
|
|
- const derived = session.deriveMessages()
|
|
|
- expect(derived.length).toBe(5)
|
|
|
- expect(derived[0]!.role).toBe('user')
|
|
|
- const framed = derived[0]!.content
|
|
|
- expect(framed[0]).toMatchObject({ type: 'text' })
|
|
|
- expect((framed[0] as { text: string }).text).toContain('<compacted-summary>')
|
|
|
- expect(framed).toContainEqual(svc.mockSummary[0])
|
|
|
- expect((framed[framed.length - 1] as { text: string }).text).toBe('</compacted-summary>')
|
|
|
+ it('skips the provisional check only when no routed or fallback model exists', async () => {
|
|
|
+ const compact = service(compactConfig)
|
|
|
+ const session = conversation()
|
|
|
+ expect(await compact.compactIfNeeded(agent(session), '', [], SIGNAL)).toBeNull()
|
|
|
+ expect(compact.calls).toHaveLength(0)
|
|
|
})
|
|
|
|
|
|
- it('throws when start or end are not surface nodes', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(1, 1)
|
|
|
- await expect(compactRegion(svc, session, 999, 1000, 'm'))
|
|
|
- .rejects.toThrow(/start seq 999 not found in surface/)
|
|
|
+ it('throws for a named unconfigured model instead of swallowing it', async () => {
|
|
|
+ const compact = service(compactConfig)
|
|
|
+ await expect(compactIfNeeded(compact, conversation(), 'missing'))
|
|
|
+ .rejects.toMatchObject({ code: TOKEN_METER_MODEL_UNCONFIGURED, model: 'missing' })
|
|
|
})
|
|
|
|
|
|
- it('throws when start is positioned after end on the surface', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
- await expect(compactRegion(svc, session, nodes[1]!.seq, nodes[0]!.seq, 'm'))
|
|
|
- .rejects.toThrow(/is after end seq .* on the surface/)
|
|
|
- })
|
|
|
+ it('does nothing below threshold and compacts a priced head above threshold', async () => {
|
|
|
+ const compact = service(compactConfig)
|
|
|
+ expect(await compactIfNeeded(compact, conversation(2))).toBeNull()
|
|
|
|
|
|
- it('throws when compaction is already in progress', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
- session.append('compact/start', { turn: 2 })
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
|
|
|
- .rejects.toThrow(/compaction already in progress/)
|
|
|
+ const session = conversation(4)
|
|
|
+ const result = await compactIfNeeded(compact, session)
|
|
|
+ expect(result).not.toBeNull()
|
|
|
+ expect(result?.shadowedSeqs.length).toBeGreaterThan(2)
|
|
|
+ expect(session.surface.nodes.length).toBeLessThan(8)
|
|
|
})
|
|
|
|
|
|
- it('appends compact/end with error on summarize failure', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- svc.summarizeError = new Error('model unavailable')
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
-
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
|
|
|
- .rejects.toThrow('model unavailable')
|
|
|
-
|
|
|
- const endEvent = session.events.findLast(e => e.type === 'compact/end')
|
|
|
- expect(endEvent).toBeDefined()
|
|
|
- // multiTurnSession(2,…) closes turns 1-2 and leaves turn 3 open; compaction
|
|
|
- // stamps the open turn.
|
|
|
- expect(endEvent!.data).toMatchObject({ turn: 3, error: 'model unavailable' })
|
|
|
-
|
|
|
- // No replace-op user/message was appended (summarize failed).
|
|
|
- const userMsgsAfter = session.events.filter(e => e.type === 'user/message')
|
|
|
- const replaceMsgs = userMsgsAfter.filter((e) => {
|
|
|
- const se = e as unknown as { surfaceOp?: unknown }
|
|
|
- return se.surfaceOp !== undefined && typeof se.surfaceOp !== 'string'
|
|
|
+ it('counts the current prompt and request prefix without putting either on the surface', async () => {
|
|
|
+ const compact = service({
|
|
|
+ auto: false,
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.7, retainTokens: 9 } },
|
|
|
})
|
|
|
- expect(replaceMsgs.length).toBe(0)
|
|
|
- })
|
|
|
-
|
|
|
- it('extracts conversation text for summarization', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(1, 2)
|
|
|
- const nodes = session.surface.nodes
|
|
|
-
|
|
|
- await compactRegion(svc, session, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
|
|
|
-
|
|
|
- expect(svc.summarizeCalls.length).toBe(1)
|
|
|
- const { text, model } = svc.summarizeCalls[0]!
|
|
|
- expect(model).toBe('m')
|
|
|
- expect(text).toContain('User: turn 1 user message 1')
|
|
|
- expect(text).toContain('Assistant: turn 1 assistant response 1')
|
|
|
+ const session = conversation(2, 'x'.repeat(2_000))
|
|
|
+ expect(await compactIfNeeded(compact, session)).toBeNull()
|
|
|
+
|
|
|
+ const prefix: Message[] = [{
|
|
|
+ role: 'user',
|
|
|
+ content: [{ type: 'text', text: 'p'.repeat(10_000) }],
|
|
|
+ }]
|
|
|
+ const result = await compactIfNeeded(compact, session, MODEL, 's'.repeat(5_000), prefix)
|
|
|
+ expect(result).not.toBeNull()
|
|
|
+ expect(prefix).toHaveLength(1)
|
|
|
+ expect(session.events.some(event => event.type === 'context/message')).toBe(false)
|
|
|
})
|
|
|
|
|
|
- it('frames the landed summary with a checkpoint preamble and tags, keeping raw provenance', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- svc.mockSummary = [{ type: 'text', text: 'STRUCTURED SUMMARY' }]
|
|
|
- const session = multiTurnSession(3, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
-
|
|
|
- const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')
|
|
|
-
|
|
|
- // Provenance (compact/summary) carries the RAW, unframed summary.
|
|
|
- expect(result.summary).toEqual([{ type: 'text', text: 'STRUCTURED SUMMARY' }])
|
|
|
- const summaryEvent = session.events.findLast(e => e.type === 'compact/summary')!
|
|
|
- expect(summaryEvent.data).toMatchObject({ summary: [{ type: 'text', text: 'STRUCTURED SUMMARY' }] })
|
|
|
+ it('uses the latest logged routed model instead of AgentOptions.model', async () => {
|
|
|
+ const ctx = createContext({
|
|
|
+ actual: { contextWindow: 100, charsPerToken: 1_000 },
|
|
|
+ fallback: { contextWindow: 10_000, charsPerToken: 1_000 },
|
|
|
+ })
|
|
|
+ const compact = service({
|
|
|
+ auto: false,
|
|
|
+ models: { actual: { thresholdRatio: 0.5, retainTokens: 18 } },
|
|
|
+ }, ctx)
|
|
|
+ const session = conversation(4)
|
|
|
+ session.append('request/header', {
|
|
|
+ header: { config: { model: 'actual' } },
|
|
|
+ reason: 'initial',
|
|
|
+ })
|
|
|
|
|
|
- // The landed surface node is framed: preamble + tag-wrapped summary.
|
|
|
- const landed = session.deriveMessages()[0]!.content
|
|
|
- expect((landed[0] as { text: string }).text).toContain('checkpoint')
|
|
|
- expect((landed[0] as { text: string }).text).toContain('<compacted-summary>')
|
|
|
- expect(landed).toContainEqual({ type: 'text', text: 'STRUCTURED SUMMARY' })
|
|
|
- expect((landed[landed.length - 1] as { text: string }).text).toBe('</compacted-summary>')
|
|
|
+ const result = await compactIfNeeded(compact, session, 'fallback')
|
|
|
+ expect(result).not.toBeNull()
|
|
|
})
|
|
|
|
|
|
- it('extracts tool-call and tool-result context', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = sessionWithTools()
|
|
|
- const nodes = session.surface.nodes
|
|
|
-
|
|
|
- const firstSeq = nodes[0]!.seq
|
|
|
- const lastSeq = nodes[nodes.length - 1]!.seq
|
|
|
- await compactRegion(svc, session, firstSeq, lastSeq, 'm')
|
|
|
-
|
|
|
- expect(svc.summarizeCalls.length).toBe(1)
|
|
|
- const { text } = svc.summarizeCalls[0]!
|
|
|
- expect(text).toContain('read file x')
|
|
|
- expect(text).toContain('bash')
|
|
|
- expect(text).toContain('Tool result')
|
|
|
- })
|
|
|
-})
|
|
|
+ it('declines when envelope pressure is high but the surface has no compactable range', async () => {
|
|
|
+ const compact = service(compactConfig)
|
|
|
+ const empty = new Session(SessionId('empty'))
|
|
|
+ expect(await compactIfNeeded(compact, empty, MODEL, 'x'.repeat(100_000))).toBeNull()
|
|
|
|
|
|
-describe('BasicCompactService.compactIfNeeded', () => {
|
|
|
- it('returns null when tokens are under threshold', async () => {
|
|
|
- const svc = createTestService({ contextWindow: 128000, thresholdRatio: 0.8 })
|
|
|
- const session = multiTurnSession(1, 1)
|
|
|
- expect(await compactIfNeeded(svc, session, '', 'm', SIGNAL)).toBeNull()
|
|
|
+ const retained = conversation(1)
|
|
|
+ expect(await compactIfNeeded(compact, retained, MODEL, 'x'.repeat(100_000))).toBeNull()
|
|
|
})
|
|
|
|
|
|
- it('compacts when tokens exceed threshold', async () => {
|
|
|
- const svc = createTestService({ contextWindow: 100, thresholdRatio: 0.5, retainTokens: 10 })
|
|
|
- const session = multiTurnSession(3, 1) // 6 surface nodes, 10 tokens each = 60
|
|
|
-
|
|
|
- const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
|
|
|
- expect(result).not.toBeNull()
|
|
|
- expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
|
|
|
- })
|
|
|
+ it('detects scalar/surface revision disagreement', async () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ const meter = ctx.tokenMeter.resolve(MODEL)
|
|
|
+ const original = meter.measureSurface.bind(meter)
|
|
|
+ vi.spyOn(meter, 'measureSurface').mockImplementation((session) => {
|
|
|
+ const measurement = original(session)
|
|
|
+ return { ...measurement, logRevision: measurement.logRevision - 1 }
|
|
|
+ })
|
|
|
+ const compact = service(compactConfig, ctx)
|
|
|
|
|
|
- it('counts the session prefix toward pressure (every request carries it in front of the history)', async () => {
|
|
|
- const svc = createTestService({ contextWindow: 200, thresholdRatio: 0.5, retainTokens: 10 })
|
|
|
- const session = multiTurnSession(3, 1) // 6 derived messages ≈ 84 estimated tokens — under the 100 threshold alone
|
|
|
- expect(await compactIfNeeded(svc, session, '', 'm', SIGNAL)).toBeNull()
|
|
|
-
|
|
|
- // The loop composes the agent/session-prefix product before the pre-step
|
|
|
- // seam and hands it to the gate; it rides every request, so pressure must
|
|
|
- // include it — the same history now crosses the threshold.
|
|
|
- const sessionPrefix: Message[] = [
|
|
|
- { role: 'user', content: [{ type: 'text', text: `opener one.${LONG_FIXTURE_TEXT}` }] },
|
|
|
- { role: 'user', content: [{ type: 'text', text: `opener two.${LONG_FIXTURE_TEXT}` }] },
|
|
|
- ]
|
|
|
- const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL, sessionPrefix)
|
|
|
- expect(result).not.toBeNull()
|
|
|
- // The prefix itself is NOT history: compaction shadowed surface nodes only.
|
|
|
- expect(sessionPrefix).toHaveLength(2)
|
|
|
+ await expect(compactIfNeeded(compact, conversation(4))).rejects.toThrow(/revision/)
|
|
|
})
|
|
|
|
|
|
- it('returns the first compaction result when a zero-retry pass converges after the loop', async () => {
|
|
|
- // With compactionRetries=0 there is no next-loop threshold check after the
|
|
|
- // first mutation, so the success path is the post-loop `return result`.
|
|
|
- const svc = createTestService({
|
|
|
- contextWindow: 100,
|
|
|
- thresholdRatio: 0.7,
|
|
|
- retainTokens: 10,
|
|
|
+ it('bounds retries when a shrinking checkpoint remains above threshold', async () => {
|
|
|
+ const compact = service({
|
|
|
+ auto: false,
|
|
|
compactionRetries: 0,
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.5, retainTokens: 18 } },
|
|
|
})
|
|
|
- const session = multiTurnSession(3, 1) // 6 derived messages = 84 estimated tokens.
|
|
|
-
|
|
|
- const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
|
|
|
+ compact.summary = Array.from({ length: 7 }, (_, index) => ({
|
|
|
+ type: 'text',
|
|
|
+ text: `summary ${index}`,
|
|
|
+ }))
|
|
|
|
|
|
- expect(result).not.toBeNull()
|
|
|
- expect(session.events.filter(e => e.type === 'compact/summary')).toHaveLength(1)
|
|
|
- expect(svc.estimateTokens(session.deriveMessages(), '')).toBeLessThan(70)
|
|
|
+ await expect(compactIfNeeded(compact, conversation(4)))
|
|
|
+ .rejects.toThrow(/still above threshold after 1 compaction attempts/)
|
|
|
})
|
|
|
|
|
|
- it('walks tail→head and retains nodes within token budget', async () => {
|
|
|
- const svc = createTestService({ contextWindow: 350, thresholdRatio: 0.2, retainTokens: 15 })
|
|
|
- const session = multiTurnSession(5, 1) // 10 surface nodes = ~100 tokens
|
|
|
-
|
|
|
- const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
|
|
|
+ it('rounds a retention cut head-ward to preserve tool-call/result pairing', async () => {
|
|
|
+ const compact = service({
|
|
|
+ auto: false,
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.8, retainTokens: 8 } },
|
|
|
+ })
|
|
|
+ const session = toolConversation()
|
|
|
+ const result = await compactIfNeeded(compact, session)
|
|
|
expect(result).not.toBeNull()
|
|
|
- const nodes = session.surface.nodes
|
|
|
- expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
|
|
|
- expect(result!.shadowedSeqs).not.toContain(nodes[nodes.length - 1]!.seq)
|
|
|
- })
|
|
|
-
|
|
|
- it('returns null when the whole surface fits the retain budget (over threshold by role/system overhead)', async () => {
|
|
|
- // Role overhead pushes the request above its 48-token threshold, but the
|
|
|
- // raw four-node retention walk remains below retainTokens=45, so all fit.
|
|
|
- const svc = createTestService({ contextWindow: 480, thresholdRatio: 0.1, retainTokens: 45 })
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- expect(await compactIfNeeded(svc, session, '', 'm', SIGNAL)).toBeNull()
|
|
|
- })
|
|
|
|
|
|
- it('compacts a runaway turn: its early CLOSED steps summarize while recent steps stay verbatim', async () => {
|
|
|
- // Completed early steps of the open turn remain eligible; protecting the
|
|
|
- // whole turn would make a runaway turn impossible to compact.
|
|
|
- const svc = createTestService({ contextWindow: 800, thresholdRatio: 0.1, retainTokens: 25 })
|
|
|
- const s = new Session(SessionId('runaway'))
|
|
|
- // ONE open turn with 5 closed steps; each step is [asst(tool-call), result].
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: 'do a big multi-step task' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- for (let step = 1; step <= 5; step++) {
|
|
|
- s.append('step/start', { turn: 1, step })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step,
|
|
|
- content: [{ type: 'text', text: `step ${step}` }, { type: 'tool-call', id: CallId(`c${step}`), name: 'bash', arguments: '{}' }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('tool/call', { turn: 1, step, callId: CallId(`c${step}`), name: 'bash', arguments: '{}' })
|
|
|
- s.append('tool/result', { turn: 1, step, callId: CallId(`c${step}`), content: [{ type: 'text', text: `out ${step}` }], isError: false }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step })
|
|
|
+ const messages = session.deriveMessages()
|
|
|
+ const calls = new Set<string>()
|
|
|
+ for (const message of messages) {
|
|
|
+ for (const block of message.content) {
|
|
|
+ if (block.type === 'tool-call') calls.add(block.id)
|
|
|
+ if (block.type === 'tool-result') expect(calls.has(block.toolCallId)).toBe(true)
|
|
|
+ }
|
|
|
}
|
|
|
- // The turn stays OPEN (no turn/end) — the model is mid-turn, about to run
|
|
|
- // step 6. Surface: user + 5×[asst, result] = 11 nodes.
|
|
|
- const nodesBefore = s.surface.nodes.length
|
|
|
- expect(nodesBefore).toBe(11)
|
|
|
-
|
|
|
- const result = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
|
|
|
- expect(result).not.toBeNull()
|
|
|
- // Early steps of the SAME open turn were shadowed (impossible under layer 2).
|
|
|
- expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
|
|
|
- // The most-recent step's tool result is retained verbatim (still on surface).
|
|
|
- const lastResultSeq = s.events.findLast(e => e.type === 'tool/result')!.seq
|
|
|
- expect(result!.shadowedSeqs).not.toContain(lastResultSeq)
|
|
|
- expect(s.surface.nodes.some(n => n.seq === lastResultSeq)).toBe(true)
|
|
|
- // No orphaned tool-result survives (whole-step boundaries respected).
|
|
|
- expectNoOrphanToolResults(s.deriveMessages())
|
|
|
})
|
|
|
|
|
|
- it('returns null for an empty surface', async () => {
|
|
|
- const svc = createTestService({ contextWindow: 100, thresholdRatio: 0.5, retainTokens: 10 })
|
|
|
- const session = new Session(SessionId('empty'))
|
|
|
- expect(await compactIfNeeded(svc, session, '', 'm', SIGNAL)).toBeNull()
|
|
|
+ it('rejects a priced surface that is not the current positional surface', () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ const session = conversation(2)
|
|
|
+ const priced = ctx.tokenMeter.resolve(MODEL).measureSurface(session)
|
|
|
+ expect(() => selectCompactableRange(session, {
|
|
|
+ ...priced,
|
|
|
+ nodes: priced.nodes.slice(1),
|
|
|
+ }, 1)).toThrow(/does not match/)
|
|
|
})
|
|
|
|
|
|
- it('compacts again after a prior summary node heads the surface (the summary stays eligible)', async () => {
|
|
|
- // Head-anchored recompaction must include the previous summary and retained context.
|
|
|
- const svc = createTestService({ contextWindow: 800, thresholdRatio: 0.1, retainTokens: 25 })
|
|
|
- const s = multiTurnSession(4, 1) // turns 1-4 closed, turn 5 open (no surface yet)
|
|
|
-
|
|
|
- const first = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
|
|
|
- expect(first).not.toBeNull()
|
|
|
- // The summary node now heads the surface with a fresh high seq.
|
|
|
- const summaryHeadSeq = s.surface.nodes[0]!.seq
|
|
|
- const turn5StartSeq = s.events.filter(e => e.type === 'turn/start').at(-1)!.seq
|
|
|
- expect(summaryHeadSeq).toBeGreaterThan(turn5StartSeq)
|
|
|
-
|
|
|
- // Append a verbatim node in the open turn (a step's output), still over
|
|
|
- // threshold, then compact again — the older summary + closed turns compact,
|
|
|
- // the fresh nodes are retained.
|
|
|
- s.append('step/start', { turn: 5, step: 1 })
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: 'turn 5 work' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', { turn: 5, step: 1, content: [{ type: 'text', text: 'reply 5' }] }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 5, step: 1 })
|
|
|
-
|
|
|
- const second = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
|
|
|
- expect(second).not.toBeNull()
|
|
|
- expect(second!.shadowedSeqs.length).toBeGreaterThan(0)
|
|
|
- // The fresh open-turn nodes were NOT compacted.
|
|
|
- const turn5UserSeq = s.events.find(e => e.type === 'user/message' && e.data.content.some(b => b.type === 'text' && b.text === 'turn 5 work'))!.seq
|
|
|
- expect(second!.shadowedSeqs).not.toContain(turn5UserSeq)
|
|
|
+ it('declines when rounding a cut would consume the only tool pair', () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ const session = new Session(SessionId('one-tool-pair'))
|
|
|
+ const callId = CallId('only')
|
|
|
+ session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
+ session.append('step/start', { turn: 1, step: 1 })
|
|
|
+ session.append('assistant/message', {
|
|
|
+ turn: 1,
|
|
|
+ step: 1,
|
|
|
+ content: [{ type: 'tool-call', id: callId, name: 'read', arguments: '{}' }],
|
|
|
+ }, { surfaceOp: 'append' })
|
|
|
+ session.append('tool/call', { turn: 1, step: 1, callId, name: 'read', arguments: '{}' })
|
|
|
+ session.append('tool/result', {
|
|
|
+ turn: 1,
|
|
|
+ step: 1,
|
|
|
+ callId,
|
|
|
+ content: [{ type: 'text', text: 'result' }],
|
|
|
+ isError: false,
|
|
|
+ }, { surfaceOp: 'append' })
|
|
|
+ session.append('step/end', { turn: 1, step: 1 })
|
|
|
+
|
|
|
+ const priced = ctx.tokenMeter.resolve(MODEL).measureSurface(session)
|
|
|
+ expect(selectCompactableRange(session, priced, 1)).toBeNull()
|
|
|
})
|
|
|
+})
|
|
|
|
|
|
- it('re-compacts smaller summaries until the post-compaction surface drops below threshold', async () => {
|
|
|
- const svc = createTestService({
|
|
|
- contextWindow: 100,
|
|
|
- thresholdRatio: 0.5,
|
|
|
- retainTokens: 10,
|
|
|
- compactionRetries: 2,
|
|
|
+describe('compaction region transaction', () => {
|
|
|
+ it('lands a framed, replayable checkpoint with exact pricing provenance', async () => {
|
|
|
+ const compact = service()
|
|
|
+ const session = conversation(3)
|
|
|
+ const before = session.surface.nodes
|
|
|
+ const result = await compact.compactRegion(
|
|
|
+ session,
|
|
|
+ before[0]!.seq,
|
|
|
+ before[3]!.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ SIGNAL,
|
|
|
+ )
|
|
|
+
|
|
|
+ expect(result.shadowedSeqs).toEqual(before.slice(0, 4).map(node => node.seq))
|
|
|
+ expect(result.shadowedTokenCount).toBeGreaterThan(0)
|
|
|
+ expect(compact.calls[0]).toMatchObject({ signal: SIGNAL })
|
|
|
+ expect(compact.calls[0]?.text).toContain('fixture user 1')
|
|
|
+ const summary = session.events.findLast(event => event.type === 'compact/summary')
|
|
|
+ expect(summary?.data).toMatchObject({
|
|
|
+ shadowedSeqs: result.shadowedSeqs,
|
|
|
+ shadowedTokenCount: result.shadowedTokenCount,
|
|
|
+ model: 'summary-model',
|
|
|
+ maxTokens: 123,
|
|
|
})
|
|
|
- svc.estimateFramedSummariesCheaply = false
|
|
|
- svc.mockSummaryQueue = [
|
|
|
- Array.from({ length: 4 }, (_, index) => ({ type: 'text', text: `first ${index}` })),
|
|
|
- [{ type: 'text', text: 'second' }],
|
|
|
- ]
|
|
|
- const session = multiTurnSession(4, 1)
|
|
|
-
|
|
|
- const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
|
|
|
+ const head = session.deriveMessages()[0]!
|
|
|
+ expect(head.content[0]?.type).toBe('text')
|
|
|
+ expect(head.content[0]?.type === 'text' ? head.content[0].text : '').toContain('<compacted-summary>')
|
|
|
+ expect(head.content.at(-1)).toEqual({ type: 'text', text: '</compacted-summary>' })
|
|
|
|
|
|
- expect(result).not.toBeNull()
|
|
|
- expect(svc.summarizeCalls).toHaveLength(2)
|
|
|
- expect(session.events.filter(e => e.type === 'compact/summary')).toHaveLength(2)
|
|
|
- expect(svc.estimateTokens(session.deriveMessages(), '')).toBeLessThan(50)
|
|
|
+ const replay = new Session(SessionId('replay'), [...session.events])
|
|
|
+ expect(replay.deriveMessages()).toEqual(session.deriveMessages())
|
|
|
})
|
|
|
|
|
|
- it('throws after the configured re-compaction attempts still leave the surface above threshold', async () => {
|
|
|
- const svc = createTestService({
|
|
|
- contextWindow: 100,
|
|
|
- thresholdRatio: 0.5,
|
|
|
- retainTokens: 10,
|
|
|
- compactionRetries: 1,
|
|
|
+ it.each([
|
|
|
+ ['start missing', 9_001, undefined, /start seq 9001 not found/],
|
|
|
+ ['end missing', undefined, 9_002, /end seq 9002 not found/],
|
|
|
+ ])('rejects %s', async (_label, startOverride, endOverride, pattern) => {
|
|
|
+ const compact = service()
|
|
|
+ const session = conversation(2)
|
|
|
+ const nodes = session.surface.nodes
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ startOverride ?? nodes[0]!.seq,
|
|
|
+ endOverride ?? nodes[1]!.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ )).rejects.toThrow(pattern)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('rejects reversed and tool-unbalanced positional boundaries', async () => {
|
|
|
+ const compact = service()
|
|
|
+ const plain = conversation(2)
|
|
|
+ const nodes = plain.surface.nodes
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ plain,
|
|
|
+ nodes[2]!.seq,
|
|
|
+ nodes[1]!.seq,
|
|
|
+ agent(plain, MODEL),
|
|
|
+ )).rejects.toThrow(/is after end/)
|
|
|
+
|
|
|
+ const tools = toolConversation()
|
|
|
+ const toolNodes = tools.surface.nodes
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ tools,
|
|
|
+ toolNodes[2]!.seq,
|
|
|
+ toolNodes[4]!.seq,
|
|
|
+ agent(tools, MODEL),
|
|
|
+ )).rejects.toThrow(/start seq .* not a balanced boundary/)
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ tools,
|
|
|
+ toolNodes[0]!.seq,
|
|
|
+ toolNodes[1]!.seq,
|
|
|
+ agent(tools, MODEL),
|
|
|
+ )).rejects.toThrow(/end seq .* not a balanced boundary/)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('requires an open turn and an idle compaction bracket', async () => {
|
|
|
+ const compact = service()
|
|
|
+ const closed = conversation(1)
|
|
|
+ closed.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
|
|
|
+ const nodes = closed.surface.nodes
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ closed,
|
|
|
+ nodes[0]!.seq,
|
|
|
+ nodes[1]!.seq,
|
|
|
+ agent(closed, MODEL),
|
|
|
+ )).rejects.toThrow(/no open turn/)
|
|
|
+
|
|
|
+ const locked = conversation(1)
|
|
|
+ locked.append('compact/start', { turn: 2 })
|
|
|
+ const lockedNodes = locked.surface.nodes
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ locked,
|
|
|
+ lockedNodes[0]!.seq,
|
|
|
+ lockedNodes[1]!.seq,
|
|
|
+ agent(locked, MODEL),
|
|
|
+ )).rejects.toThrow(/already in progress/)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('rejects a session with no turn boundary at all', async () => {
|
|
|
+ const compact = service()
|
|
|
+ const session = new Session(SessionId('turnless'))
|
|
|
+ session.append('user/message', {
|
|
|
+ content: [{ type: 'text', text: 'orphan' }],
|
|
|
+ source: { kind: 'user' },
|
|
|
+ }, { surfaceOp: 'append' })
|
|
|
+ const node = session.surface.nodes[0]!
|
|
|
+
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ node.seq,
|
|
|
+ node.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ )).rejects.toThrow(/no open turn/)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('rejects a meter snapshot that changed before summarization began', async () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ const meter = ctx.tokenMeter.resolve(MODEL)
|
|
|
+ const original = meter.measureSurface.bind(meter)
|
|
|
+ vi.spyOn(meter, 'measureSurface').mockImplementationOnce((session) => {
|
|
|
+ const measurement = original(session)
|
|
|
+ return { ...measurement, nodes: measurement.nodes.slice(1) }
|
|
|
})
|
|
|
- svc.estimateFramedSummariesCheaply = false
|
|
|
- svc.mockSummaryQueue = [
|
|
|
- Array.from({ length: 4 }, (_, index) => ({ type: 'text', text: `first ${index}` })),
|
|
|
- Array.from({ length: 3 }, (_, index) => ({ type: 'text', text: `second ${index}` })),
|
|
|
- ]
|
|
|
- const session = multiTurnSession(4, 1)
|
|
|
-
|
|
|
- await expect(compactIfNeeded(svc, session, '', 'm', SIGNAL))
|
|
|
- .rejects.toThrow(/still above threshold after 2 compaction attempts/)
|
|
|
- expect(svc.summarizeCalls).toHaveLength(2)
|
|
|
- })
|
|
|
-})
|
|
|
-
|
|
|
-describe('BasicCompactService replay equivalence', () => {
|
|
|
- it('produces identical deriveMessages() after seeding from compacted log', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(3, 1)
|
|
|
+ const compact = service({ auto: false }, ctx)
|
|
|
+ const session = conversation(2)
|
|
|
const nodes = session.surface.nodes
|
|
|
|
|
|
- await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')
|
|
|
- const derived = session.deriveMessages()
|
|
|
-
|
|
|
- const replayed = new Session(SessionId('replay'), [...session.events])
|
|
|
- expect(replayed.deriveMessages()).toEqual(derived)
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ nodes[0]!.seq,
|
|
|
+ nodes[2]!.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ )).rejects.toThrow(/selected surface changed/)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('records summarizer failures without mutating the surface', async () => {
|
|
|
+ const compact = service()
|
|
|
+ compact.error = new Error('summary unavailable')
|
|
|
+ const session = conversation(2)
|
|
|
+ const before = session.surface.nodes
|
|
|
+
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ before[0]!.seq,
|
|
|
+ before[2]!.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ )).rejects.toThrow('summary unavailable')
|
|
|
+ expect(session.surface.nodes).toEqual(before)
|
|
|
+ expect(session.events.findLast(event => event.type === 'compact/end')?.data)
|
|
|
+ .toMatchObject({ error: 'summary unavailable' })
|
|
|
})
|
|
|
-})
|
|
|
|
|
|
-describe('BasicCompactService blocking (compaction in progress)', () => {
|
|
|
- it('detects in-progress compaction from unmatched compact/start', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(1, 1)
|
|
|
- session.append('compact/start', { turn: 1 })
|
|
|
+ it('stringifies non-Error failures in the durable end bracket', async () => {
|
|
|
+ const compact = service()
|
|
|
+ compact.error = 'plain failure'
|
|
|
+ const session = conversation(2)
|
|
|
const nodes = session.surface.nodes
|
|
|
- // Whole step (user → assistant) is a step-aligned region, so the call reaches
|
|
|
- // the in-progress check rather than being rejected for splitting a step.
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
|
|
|
- .rejects.toThrow(/compaction already in progress/)
|
|
|
- })
|
|
|
-
|
|
|
- it('allows compaction after compact/end is appended', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ nodes[0]!.seq,
|
|
|
+ nodes[2]!.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ )).rejects.toBe('plain failure')
|
|
|
+ expect(session.events.findLast(event => event.type === 'compact/end')?.data)
|
|
|
+ .toMatchObject({ error: 'plain failure' })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('rejects concurrent durable appends before committing the replacement', async () => {
|
|
|
+ const compact = service()
|
|
|
+ const session = conversation(2)
|
|
|
+ compact.mutateDuringSummary = () => {
|
|
|
+ session.append('request/header', {
|
|
|
+ header: { config: { model: MODEL } },
|
|
|
+ reason: 'initial',
|
|
|
+ })
|
|
|
+ }
|
|
|
const nodes = session.surface.nodes
|
|
|
- session.append('compact/start', { turn: 1 })
|
|
|
- session.append('compact/end', { turn: 1 })
|
|
|
- const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')
|
|
|
- expect(result).toBeDefined()
|
|
|
- })
|
|
|
-
|
|
|
- it('is not wedged by an orphaned compact/start from a prior (now-closed) turn', async () => {
|
|
|
- // An orphaned start in a closed repaired turn is stale; only the current
|
|
|
- // turn participates in the in-progress lock.
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('stale-lock'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: 'turn 1' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'reply 1' }] }, { surfaceOp: 'append' })
|
|
|
- s.append('compact/start', { turn: 1 }) // ← orphaned: no matching compact/end
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) // repair closed the turn
|
|
|
- // A new open turn.
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- const nodes = s.surface.nodes
|
|
|
-
|
|
|
- // The stale start is before the turn/end, so it is NOT seen as in-progress.
|
|
|
- const result = await compactRegion(svc, s, nodes[0]!.seq, nodes[1]!.seq, 'm')
|
|
|
- expect(result).toBeDefined()
|
|
|
- })
|
|
|
-})
|
|
|
-
|
|
|
-describe('BasicCompactService token estimation (char/4 heuristic)', () => {
|
|
|
- it('estimates text blocks with char/4 + overhead', () => {
|
|
|
- const svc = new BasicCompactService(new Context(), cfg({ auto: false }))
|
|
|
- // 'this is a somewhat longer text block' = 36 → ceil(36/4)+4 = 13; 'short' = 5 → 2+4 = 6
|
|
|
- const blocks: ContentBlock[] = [
|
|
|
- { type: 'text', text: 'this is a somewhat longer text block' },
|
|
|
- { type: 'text', text: 'short' },
|
|
|
- ]
|
|
|
- expect(svc.estimateContentTokens(blocks)).toBe(19)
|
|
|
- })
|
|
|
-
|
|
|
- it('estimates reasoning blocks same as text', () => {
|
|
|
- const svc = new BasicCompactService(new Context(), cfg({ auto: false }))
|
|
|
- // 'thinking about this...' = 22 → ceil(22/4)+4 = 10
|
|
|
- expect(svc.estimateContentTokens([{ type: 'reasoning', text: 'thinking about this...' }])).toBe(10)
|
|
|
- })
|
|
|
-
|
|
|
- it('estimates tool-call blocks from name + arguments', () => {
|
|
|
- const svc = new BasicCompactService(new Context(), cfg({ auto: false }))
|
|
|
- // 'bash' = 4 → 1; '{"command":"ls"}' = 16 → 4; + 4 overhead = 9
|
|
|
- expect(svc.estimateContentTokens([
|
|
|
- { type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{"command":"ls"}' },
|
|
|
- ])).toBe(9)
|
|
|
- })
|
|
|
-
|
|
|
- it('estimates tool-result blocks recursively', () => {
|
|
|
- const svc = new BasicCompactService(new Context(), cfg({ auto: false }))
|
|
|
- // inner text 5 → 2+4 = 6; outer 6 + 4 overhead = 10
|
|
|
- expect(svc.estimateContentTokens([
|
|
|
- { type: 'tool-result', toolCallId: CallId('c1'), content: [{ type: 'text', text: 'hello' }], isError: false },
|
|
|
- ])).toBe(10)
|
|
|
- })
|
|
|
-
|
|
|
- it('returns 0 for empty content blocks', () => {
|
|
|
- const svc = new BasicCompactService(new Context(), cfg({ auto: false }))
|
|
|
- expect(svc.estimateContentTokens([])).toBe(0)
|
|
|
- })
|
|
|
-
|
|
|
- it('honors a configured charsPerToken (fractional densities included)', () => {
|
|
|
- // 'this is a somewhat longer text block' = 36 chars.
|
|
|
- const blocks: ContentBlock[] = [{ type: 'text', text: 'this is a somewhat longer text block' }]
|
|
|
- // charsPerToken 2: ceil(36/2)+4 = 22 — a CJK-density config doubles the estimate.
|
|
|
- const dense = new BasicCompactService(new Context(), cfg({ auto: false, charsPerToken: 2 }))
|
|
|
- expect(dense.estimateContentTokens(blocks)).toBe(22)
|
|
|
- // Fractional density is legal: ceil(36/1.5)+4 = 28.
|
|
|
- const fractional = new BasicCompactService(new Context(), cfg({ auto: false, charsPerToken: 1.5 }))
|
|
|
- expect(fractional.estimateContentTokens(blocks)).toBe(28)
|
|
|
- // The system-prompt term scales with the same knob: 36-char prompt at density 2 → ceil(36/2) = 18.
|
|
|
- expect(dense.estimateTokens([], 'this is a somewhat longer text block')).toBe(18)
|
|
|
- })
|
|
|
-})
|
|
|
-
|
|
|
-describe('BasicCompactService HMR safety', () => {
|
|
|
- it('registers as ctx.compact', () => {
|
|
|
- const ctx = new Context()
|
|
|
- void new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- expect(ctx.compact).toBeDefined()
|
|
|
- expect(ctx.compact).toBeInstanceOf(BasicCompactService)
|
|
|
- })
|
|
|
-
|
|
|
- it('disposing the plugin fiber unregisters ctx.compact', async () => {
|
|
|
- // Mount through the real plugin fiber (the Loader path), then dispose it and confirm the
|
|
|
- // service registration is torn down.
|
|
|
- const ctx = new Context()
|
|
|
- await ctx.plugin(LlmService)
|
|
|
- const fiber = await ctx.plugin(BasicCompactService, cfg({ auto: false }))
|
|
|
- expect(ctx.get('compact')).toBeInstanceOf(BasicCompactService)
|
|
|
|
|
|
- await fiber.dispose()
|
|
|
- expect(ctx.get('compact')).toBeUndefined()
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ nodes[0]!.seq,
|
|
|
+ nodes[2]!.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ )).rejects.toThrow(/session log changed/)
|
|
|
+ expect(session.events.some(event => event.type === 'compact/summary')).toBe(false)
|
|
|
})
|
|
|
-})
|
|
|
|
|
|
-describe('BasicCompactService config validation', () => {
|
|
|
- it('rejects invalid numeric config values', () => {
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, contextWindow: 0 })))
|
|
|
- .toThrow(/contextWindow .* positive integer/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, thresholdRatio: 0 }))).toThrow(/thresholdRatio .* \(0, 1\]/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, thresholdRatio: 1.1 }))).toThrow(/thresholdRatio .* \(0, 1\]/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, retainTokens: -1 })))
|
|
|
- .toThrow(/retainTokens .* non-negative integer/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, maxTokens: 0 }))).toThrow(/maxTokens .* positive integer/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, compactionRetries: -1 })))
|
|
|
- .toThrow(/compactionRetries .* non-negative integer/)
|
|
|
- expect(() => new BasicCompactService(
|
|
|
- new Context(), cfg({ auto: false, summarizationModel: 1 } as unknown as Partial<BasicCompactConfig>),
|
|
|
- )).toThrow(/summarizationModel must be a string/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: 'no' } as unknown as Partial<BasicCompactConfig>)))
|
|
|
- .toThrow(/auto must be a boolean/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, charsPerToken: 0 })))
|
|
|
- .toThrow(/charsPerToken .* positive finite number/)
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false, charsPerToken: Number.NaN })))
|
|
|
- .toThrow(/charsPerToken .* positive finite number/)
|
|
|
- })
|
|
|
+ it('rejects a non-shrinking framed summary under the conversation meter', async () => {
|
|
|
+ const compact = service()
|
|
|
+ compact.summary = Array.from({ length: 20 }, (_, index) => ({
|
|
|
+ type: 'text',
|
|
|
+ text: `verbose ${index}`,
|
|
|
+ }))
|
|
|
+ const session = conversation(2)
|
|
|
+ const nodes = session.surface.nodes
|
|
|
|
|
|
- it('accepts a large retain budget because convergence is enforced dynamically', () => {
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({
|
|
|
- auto: false,
|
|
|
- contextWindow: 1000,
|
|
|
- thresholdRatio: 0.5,
|
|
|
- retainTokens: 900,
|
|
|
- }))).not.toThrow()
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ nodes[0]!.seq,
|
|
|
+ nodes[2]!.seq,
|
|
|
+ agent(session, MODEL),
|
|
|
+ )).rejects.toThrow(/summary is not smaller/)
|
|
|
+ expect(session.events.some(event => event.type === 'compact/summary')).toBe(false)
|
|
|
})
|
|
|
|
|
|
- it('the default config is valid', () => {
|
|
|
- expect(() => new BasicCompactService(new Context(), cfg({ auto: false }))).not.toThrow()
|
|
|
+ it('requires a conversation model for pricing', async () => {
|
|
|
+ const compact = service()
|
|
|
+ const session = conversation(1)
|
|
|
+ const nodes = session.surface.nodes
|
|
|
+ await expect(compact.compactRegion(
|
|
|
+ session,
|
|
|
+ nodes[0]!.seq,
|
|
|
+ nodes[1]!.seq,
|
|
|
+ agent(session),
|
|
|
+ )).rejects.toThrow(/no routed or configured conversation model/)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
-/** An adapter that emits a fixed summary text, for exercising the real summarize() path. */
|
|
|
class ScriptedAdapter extends LlmAdapter {
|
|
|
- lastOptions: GenerateOptions | null = null
|
|
|
- constructor(private summaryText: string) {
|
|
|
- super()
|
|
|
- }
|
|
|
+ lastOptions: GenerateOptions | undefined
|
|
|
|
|
|
- async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
|
|
|
- this.lastOptions = options
|
|
|
- yield { type: 'block-start', index: 0, blockType: 'text' }
|
|
|
- yield { type: 'text-delta', index: 0, text: this.summaryText }
|
|
|
- yield { type: 'finish', reason: { kind: 'stop' } }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/** An adapter that emits arbitrary content blocks, preserving reasoning/text shape. */
|
|
|
-class BlocksAdapter extends LlmAdapter {
|
|
|
- lastOptions: GenerateOptions | null = null
|
|
|
- constructor(private blocks: readonly ContentBlock[]) {
|
|
|
+ constructor(
|
|
|
+ private readonly blocks: readonly ContentBlock[],
|
|
|
+ private readonly finish: (StreamChunk & { type: 'finish' })['reason'] = { kind: 'stop' },
|
|
|
+ ) {
|
|
|
super()
|
|
|
}
|
|
|
|
|
|
- async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
|
|
|
+ override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
|
|
|
this.lastOptions = options
|
|
|
for (const [index, block] of this.blocks.entries()) {
|
|
|
yield { type: 'block-start', index, blockType: block.type }
|
|
|
- switch (block.type) {
|
|
|
- case 'text':
|
|
|
- yield { type: 'text-delta', index, text: block.text }
|
|
|
- break
|
|
|
- case 'reasoning':
|
|
|
- yield { type: 'reasoning-delta', index, text: block.text }
|
|
|
- break
|
|
|
- default:
|
|
|
- yield { type: 'block-end', index, block }
|
|
|
+ if (block.type === 'text') {
|
|
|
+ yield { type: 'text-delta', index, text: block.text }
|
|
|
+ } else if (block.type === 'reasoning') {
|
|
|
+ yield { type: 'reasoning-delta', index, text: block.text }
|
|
|
+ } else {
|
|
|
+ yield { type: 'block-end', index, block }
|
|
|
}
|
|
|
}
|
|
|
- yield { type: 'finish', reason: { kind: 'stop' } }
|
|
|
+ yield { type: 'finish', reason: this.finish }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/** Wire a real LlmService + arbitrary-block adapter into a context. */
|
|
|
-async function ctxWithBlocks(blocks: readonly ContentBlock[], model = 'test-model'): Promise<{ ctx: Context; adapter: BlocksAdapter }> {
|
|
|
- const ctx = new Context()
|
|
|
- await ctx.plugin(LlmService)
|
|
|
- const adapter = new BlocksAdapter(blocks)
|
|
|
- ctx.llm.registerAdapter([model], adapter)
|
|
|
- return { ctx, adapter }
|
|
|
-}
|
|
|
-
|
|
|
-/** Wire a real LlmService + scripted adapter into a context. */
|
|
|
-async function ctxWithModel(summaryText: string, model = 'test-model'): Promise<{ ctx: Context; adapter: ScriptedAdapter }> {
|
|
|
+async function summarizerHarness(
|
|
|
+ blocks: readonly ContentBlock[],
|
|
|
+ finish?: (StreamChunk & { type: 'finish' })['reason'],
|
|
|
+ model = MODEL,
|
|
|
+ config: BasicCompactConfig = { auto: false },
|
|
|
+): Promise<{ ctx: Context; adapter: ScriptedAdapter; compact: BasicCompactService }> {
|
|
|
const ctx = new Context()
|
|
|
await ctx.plugin(LlmService)
|
|
|
- const adapter = new ScriptedAdapter(summaryText)
|
|
|
+ void new TokenMeterService(ctx, { models: { [model]: { contextWindow: 100 } } })
|
|
|
+ const adapter = new ScriptedAdapter(blocks, finish)
|
|
|
ctx.llm.registerAdapter([model], adapter)
|
|
|
- return { ctx, adapter }
|
|
|
-}
|
|
|
-
|
|
|
-/** An adapter whose stream ends with a finish chunk of the given reason (no content). */
|
|
|
-class FinishOnlyAdapter extends LlmAdapter {
|
|
|
- constructor(private reason: StreamChunk & { type: 'finish' }) {
|
|
|
- super()
|
|
|
- }
|
|
|
-
|
|
|
- async * stream(): AsyncIterable<StreamChunk> {
|
|
|
- yield this.reason
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/** Wire a real LlmService + finish-only adapter into a context. */
|
|
|
-async function ctxWithFinish(reason: (StreamChunk & { type: 'finish' })['reason'], model = 'test-model'): Promise<Context> {
|
|
|
- const ctx = new Context()
|
|
|
- await ctx.plugin(LlmService)
|
|
|
- ctx.llm.registerAdapter([model], new FinishOnlyAdapter({ type: 'finish', reason }))
|
|
|
- return ctx
|
|
|
-}
|
|
|
-
|
|
|
-/** A minimal Agent stub carrying just session + options (enough for the listeners). */
|
|
|
-function stubAgent(session: Session, model?: string): Agent {
|
|
|
- return { session, options: { model } } as unknown as Agent
|
|
|
-}
|
|
|
-
|
|
|
-function compactIfNeeded(
|
|
|
- svc: BasicCompactService,
|
|
|
- session: Session,
|
|
|
- fullSystemPrompt: string,
|
|
|
- model: string,
|
|
|
- signal: AbortSignal,
|
|
|
- sessionPrefix: readonly Message[] = [],
|
|
|
-) {
|
|
|
- return svc.compactIfNeeded(stubAgent(session, model), fullSystemPrompt, sessionPrefix, signal)
|
|
|
+ const compact = new BasicCompactService(ctx, config)
|
|
|
+ return { ctx, adapter, compact }
|
|
|
}
|
|
|
|
|
|
-function compactRegion(
|
|
|
- svc: BasicCompactService,
|
|
|
- session: Session,
|
|
|
- start: number,
|
|
|
- end: number,
|
|
|
- model: string,
|
|
|
- signal?: AbortSignal,
|
|
|
-) {
|
|
|
- return svc.compactRegion(session, start, end, stubAgent(session, model), signal)
|
|
|
-}
|
|
|
-
|
|
|
-function summarize(svc: BasicCompactService, text: string, model: string) {
|
|
|
- return svc.summarize(text, stubAgent(new Session(SessionId('summary')), model))
|
|
|
-}
|
|
|
-
|
|
|
-describe('BasicCompactService.summarize (real ctx.llm.stream)', () => {
|
|
|
- it('summarizes via the registered adapter and returns its content', async () => {
|
|
|
- const { ctx, adapter } = await ctxWithModel('SUMMARY TEXT')
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false, maxTokens: 512 }))
|
|
|
-
|
|
|
- const { summary, model, maxTokens } = await summarize(svc, 'User: hi\n\nAssistant: hello', 'test-model')
|
|
|
- expect(summary).toEqual([{ type: 'text', text: 'SUMMARY TEXT' }])
|
|
|
- // The returned envelope reports what the call actually used — the caller
|
|
|
- // logs it on compact/summary (the reconstructability RFC).
|
|
|
- expect(model).toBe('test-model')
|
|
|
- expect(maxTokens).toBe(512)
|
|
|
- // The fixed system prompt and maxTokens flow through.
|
|
|
- expect(adapter.lastOptions!.system).toContain('compaction engine')
|
|
|
- expect(adapter.lastOptions!.system).toContain('## Next Step')
|
|
|
- expect(adapter.lastOptions!.maxTokens).toBe(512)
|
|
|
- expect(adapter.lastOptions!.sessionId).toBe(SessionId('summary'))
|
|
|
- expect(adapter.lastOptions!.messages[0]!.content[0]).toMatchObject({ type: 'text' })
|
|
|
- })
|
|
|
-
|
|
|
- it('uses maxTokens as the summarization provider cap', async () => {
|
|
|
- const { ctx, adapter } = await ctxWithModel('SUMMARY TEXT')
|
|
|
- const svc = new BasicCompactService(ctx, cfg({
|
|
|
+describe('default one-shot summarizer', () => {
|
|
|
+ it('uses configured model/default cap, forwards cancellation, and keeps only safe text', async () => {
|
|
|
+ const { adapter, compact } = await summarizerHarness([
|
|
|
+ { type: 'reasoning', text: 'private' },
|
|
|
+ { type: 'text', text: 'public summary' },
|
|
|
+ { type: 'tool-call', id: CallId('unexpected'), name: 'x', arguments: '{}' },
|
|
|
+ ], undefined, MODEL, {
|
|
|
auto: false,
|
|
|
- maxTokens: 50,
|
|
|
- }))
|
|
|
-
|
|
|
- await summarize(svc, 'User: hi', 'test-model')
|
|
|
-
|
|
|
- expect(adapter.lastOptions!.maxTokens).toBe(50)
|
|
|
- })
|
|
|
-
|
|
|
- it('keeps only text blocks in the stored summary (drops reasoning and tool-call)', async () => {
|
|
|
- const { ctx } = await ctxWithBlocks([
|
|
|
- { type: 'reasoning', text: 'private chain of thought' },
|
|
|
- { type: 'text', text: 'PUBLIC SUMMARY' },
|
|
|
- // A model reply can carry a tool-call; it must not survive into the
|
|
|
- // synthesized user/message summary as an orphaned call.
|
|
|
- { type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{}' },
|
|
|
- ])
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
-
|
|
|
- const { summary } = await summarize(svc, 'User: hi', 'test-model')
|
|
|
-
|
|
|
- expect(summary).toEqual([{ type: 'text', text: 'PUBLIC SUMMARY' }])
|
|
|
- })
|
|
|
-
|
|
|
- it('throws when no text block remains after filtering', async () => {
|
|
|
- const { ctx } = await ctxWithBlocks([{ type: 'reasoning', text: 'private only' }])
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
-
|
|
|
- await expect(summarize(svc, 'User: hi', 'test-model')).rejects.toThrow(/no text summary content/)
|
|
|
- })
|
|
|
-
|
|
|
- it('throws when no model is provided', async () => {
|
|
|
- const { ctx } = await ctxWithModel('x')
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- await expect(summarize(svc, 'text', '')).rejects.toThrow(/no model available/)
|
|
|
- })
|
|
|
-
|
|
|
- it('rethrows when the stream ends with a finish-error chunk', async () => {
|
|
|
- const ctx = await ctxWithFinish({ kind: 'error', message: 'provider 401', code: 'UNAUTHORIZED' })
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- await expect(summarize(svc, 'text', 'test-model')).rejects.toMatchObject({ message: 'provider 401', code: 'UNAUTHORIZED' })
|
|
|
- })
|
|
|
-
|
|
|
- it('rethrows a finish-error chunk without a code (code stays undefined)', async () => {
|
|
|
- const ctx = await ctxWithFinish({ kind: 'error', message: 'opaque failure' })
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- const error = await summarize(svc, 'text', 'test-model').then(() => null, (e: unknown) => e as Error & { code?: string })
|
|
|
- expect(error?.message).toBe('opaque failure')
|
|
|
- expect(error?.code).toBeUndefined()
|
|
|
- })
|
|
|
-
|
|
|
- it('rethrows when the stream ends with a finish-aborted chunk', async () => {
|
|
|
- const ctx = await ctxWithFinish({ kind: 'aborted' })
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- await expect(summarize(svc, 'text', 'test-model')).rejects.toMatchObject({ message: 'summarization stream aborted', code: 'ABORTED' })
|
|
|
- })
|
|
|
-
|
|
|
- it('fails closed on a max-tokens finish (an incomplete checkpoint must not commit)', async () => {
|
|
|
- const ctx = await ctxWithFinish({ kind: 'max-tokens' })
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- await expect(summarize(svc, 'text', 'test-model')).rejects.toMatchObject({ code: 'MAX_TOKENS' })
|
|
|
- })
|
|
|
-
|
|
|
- it('compactRegion leaves the surface intact when summarization hits max-tokens', async () => {
|
|
|
- const ctx = await ctxWithFinish({ kind: 'max-tokens' })
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const before = [...session.surface.nodes]
|
|
|
- const nodes = session.surface.nodes
|
|
|
-
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model'))
|
|
|
- .rejects.toMatchObject({ code: 'MAX_TOKENS' })
|
|
|
-
|
|
|
- // No replacement landed — the surface is byte-identical, and the lock was
|
|
|
- // released with the error (compact/end carries it).
|
|
|
- expect(session.surface.nodes).toEqual(before)
|
|
|
- const endEvent = session.events.findLast(e => e.type === 'compact/end')!
|
|
|
- const endData = endEvent.data as { error?: string }
|
|
|
- expect(endData.error).toContain('truncated')
|
|
|
- })
|
|
|
-
|
|
|
- it('compactRegion uses the real summarizer end-to-end', async () => {
|
|
|
- const { ctx } = await ctxWithModel('CONDENSED')
|
|
|
- const svc = new BasicCompactService(ctx, cfg({ auto: false }))
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
-
|
|
|
- const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model')
|
|
|
- expect(result.summary).toEqual([{ type: 'text', text: 'CONDENSED' }])
|
|
|
- // The raw summary is wrapped in the checkpoint framing on the surface.
|
|
|
- expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'CONDENSED' })
|
|
|
- })
|
|
|
-
|
|
|
- it('rejects a summary that is not smaller than the shadowed content', async () => {
|
|
|
- const svc = createTestService({ auto: false })
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
- svc.mockSummary = Array.from({ length: 20 }, (_, index) => ({ type: 'text', text: `large ${index}` }))
|
|
|
-
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
|
|
|
- .rejects.toThrow(/summary is not smaller than the shadowed content/)
|
|
|
- expect(session.events.some(e => e.type === 'compact/summary')).toBe(false)
|
|
|
- })
|
|
|
-
|
|
|
- it('rejects when the framed checkpoint is not smaller than the shadowed content', async () => {
|
|
|
- const svc = createTestService({ auto: false })
|
|
|
- svc.estimateFramedSummariesCheaply = false
|
|
|
- const session = new Session(SessionId('framed-nonshrinking'))
|
|
|
- session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- session.append('step/start', { turn: 1, step: 1 })
|
|
|
- session.append('user/message', { content: [{ type: 'text', text: 'tiny user' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- session.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'tiny assistant' }] }, { surfaceOp: 'append' })
|
|
|
- session.append('step/end', { turn: 1, step: 1 })
|
|
|
- session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- session.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- const before = [...session.surface.nodes]
|
|
|
- const nodes = session.surface.nodes
|
|
|
-
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
|
|
|
- .rejects.toThrow(/summary is not smaller than the shadowed content/)
|
|
|
- expect(session.events.some(e => e.type === 'compact/summary')).toBe(false)
|
|
|
- expect(session.surface.nodes).toEqual(before)
|
|
|
- })
|
|
|
-})
|
|
|
-
|
|
|
-describe('BasicCompactService auto-compaction (agent/pre-step listener)', () => {
|
|
|
- /** Fire the agent/pre-step serial checkpoint as the loop does. */
|
|
|
- function firePreStep(ctx: Context, agent: Agent, step: number, fullSystemPrompt: string): Promise<unknown> {
|
|
|
- return ctx.serial('agent/pre-step', agent, 1, step, fullSystemPrompt, [], SIGNAL)
|
|
|
- }
|
|
|
-
|
|
|
- it('compacts (mutating the surface) when over threshold', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
- void new BasicCompactService(ctx, cfg({ contextWindow: 200, thresholdRatio: 0.5, retainTokens: 20 }))
|
|
|
- const session = multiTurnSession(5, 1) // 10 surface nodes
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
- const before = session.surface.nodes.length
|
|
|
-
|
|
|
- await firePreStep(ctx, agent, 1, '')
|
|
|
-
|
|
|
- // The surface shrank in place, and a summary checkpoint landed.
|
|
|
- expect(session.surface.nodes.length).toBeLessThan(before)
|
|
|
- expect(session.events.some(e => e.type === 'compact/summary')).toBe(true)
|
|
|
- // The re-derived head message is the framed summary checkpoint.
|
|
|
- expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'SUMMARY' })
|
|
|
- })
|
|
|
-
|
|
|
- it('logs compaction details when auto-compaction returns a converged result', async () => {
|
|
|
- const ctx = new Context()
|
|
|
- const infos: string[] = []
|
|
|
- ctx.logger.info = ((msg: string) => void infos.push(msg)) as typeof ctx.logger.info
|
|
|
- void new TestCompactService(ctx, cfg({
|
|
|
- contextWindow: 100,
|
|
|
- thresholdRatio: 0.7,
|
|
|
- retainTokens: 10,
|
|
|
- compactionRetries: 0,
|
|
|
- }))
|
|
|
- const session = multiTurnSession(3, 1)
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
-
|
|
|
- await firePreStep(ctx, agent, 1, '')
|
|
|
-
|
|
|
- expect(session.events.filter(e => e.type === 'compact/summary')).toHaveLength(1)
|
|
|
- expect(infos.some(msg => msg.includes('compaction: shadowed'))).toBe(true)
|
|
|
- expect(infos.some(msg => msg.includes('estimated tokens after compaction'))).toBe(true)
|
|
|
- })
|
|
|
-
|
|
|
- it('compacts mid-turn on steps after the first (the surface grows within a turn)', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
- void new BasicCompactService(ctx, cfg({ contextWindow: 100, thresholdRatio: 0.5, retainTokens: 10 }))
|
|
|
- const session = multiTurnSession(3, 1) // over the 0.5 threshold
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
+ summarizationModel: MODEL,
|
|
|
+ maxTokens: 321,
|
|
|
+ })
|
|
|
+ const session = conversation(1)
|
|
|
+ const output = await compact.summarize('transcript', agent(session, 'fallback'), SIGNAL)
|
|
|
|
|
|
- // A step-2 checkpoint (a tool-heavy turn's later step) must still compact —
|
|
|
- // the surface accumulated assistant/message + tool/result nodes since step 1.
|
|
|
- await firePreStep(ctx, agent, 2, '')
|
|
|
- expect(session.events.some(e => e.type === 'compact/start')).toBe(true)
|
|
|
+ expect(output).toEqual({
|
|
|
+ summary: [{ type: 'text', text: 'public summary' }],
|
|
|
+ model: MODEL,
|
|
|
+ maxTokens: 321,
|
|
|
+ })
|
|
|
+ expect(adapter.lastOptions).toMatchObject({
|
|
|
+ model: MODEL,
|
|
|
+ maxTokens: 321,
|
|
|
+ signal: SIGNAL,
|
|
|
+ sessionId: session.id,
|
|
|
+ })
|
|
|
+ expect(adapter.lastOptions?.system).toContain('## Primary Request and Intent')
|
|
|
})
|
|
|
|
|
|
- it('does nothing when under threshold', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
- void new BasicCompactService(ctx, cfg({ contextWindow: 128000, thresholdRatio: 0.8 }))
|
|
|
- const session = multiTurnSession(1, 1)
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
-
|
|
|
- await firePreStep(ctx, agent, 1, '')
|
|
|
- expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
|
|
|
+ it('resolves latest routed model before AgentOptions.model', async () => {
|
|
|
+ const { adapter, compact } = await summarizerHarness([{ type: 'text', text: 'summary' }], undefined, 'routed')
|
|
|
+ const session = conversation(1)
|
|
|
+ session.append('request/header', {
|
|
|
+ header: { config: { model: 'routed' } },
|
|
|
+ reason: 'initial',
|
|
|
+ })
|
|
|
+ const output = await compact.summarize('history', agent(session, 'fallback'))
|
|
|
+ expect(output.model).toBe('routed')
|
|
|
+ expect(adapter.lastOptions?.model).toBe('routed')
|
|
|
})
|
|
|
|
|
|
- it('leaves the surface intact when compaction fails (summarize rejects)', async () => {
|
|
|
- // No adapter registered for this model → summarize() rejects → caught, the
|
|
|
- // surface is untouched (the loop derives the full history).
|
|
|
+ it('fails clearly when no summarization model can be resolved', async () => {
|
|
|
const ctx = new Context()
|
|
|
await ctx.plugin(LlmService)
|
|
|
- void new BasicCompactService(ctx, cfg({ contextWindow: 300, thresholdRatio: 0.1, retainTokens: 10 }))
|
|
|
- const session = multiTurnSession(3, 1)
|
|
|
- const agent = stubAgent(session, 'missing-model')
|
|
|
- const before = session.surface.nodes.length
|
|
|
-
|
|
|
- await firePreStep(ctx, agent, 1, '')
|
|
|
- // No summary landed; the surface is unchanged.
|
|
|
- expect(session.events.some(e => e.type === 'compact/summary')).toBe(false)
|
|
|
- expect(session.surface.nodes.length).toBe(before)
|
|
|
- })
|
|
|
-
|
|
|
- it('does not register the listener when auto is false', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
- void new BasicCompactService(ctx, cfg({ auto: false, contextWindow: 100, thresholdRatio: 0.1, retainTokens: 5 }))
|
|
|
- const session = multiTurnSession(3, 1)
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
-
|
|
|
- await firePreStep(ctx, agent, 1, '')
|
|
|
- expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
|
|
|
- })
|
|
|
-
|
|
|
- it('summarization is interceptable at llm/stream (model routing for direct calls)', async () => {
|
|
|
- const { ctx, adapter } = await ctxWithModel('ROUTED SUMMARY', 'routed-model')
|
|
|
- // One-shot summaries bypass agent/request but remain mutable at llm/stream;
|
|
|
- // adapter selection happens after the waterfall rewrite.
|
|
|
- ctx.on('llm/stream', (options, next) => {
|
|
|
- options.model = 'routed-model'
|
|
|
- return next()
|
|
|
- })
|
|
|
- void new BasicCompactService(ctx, cfg({ contextWindow: 200, thresholdRatio: 0.5, retainTokens: 20 }))
|
|
|
- const session = multiTurnSession(5, 1)
|
|
|
- const agent = stubAgent(session, 'agent-model')
|
|
|
-
|
|
|
- await ctx.serial('agent/pre-step', agent, 1, 1, '', [], SIGNAL)
|
|
|
-
|
|
|
- expect(adapter.lastOptions?.model).toBe('routed-model')
|
|
|
- expect(session.events.some(e => e.type === 'compact/summary')).toBe(true)
|
|
|
- expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'ROUTED SUMMARY' })
|
|
|
- })
|
|
|
-
|
|
|
- it('removes the auto pre-step listener when the plugin fiber is disposed', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
- const fiber = await ctx.plugin(BasicCompactService, cfg({
|
|
|
- contextWindow: 200,
|
|
|
- thresholdRatio: 0.5,
|
|
|
- retainTokens: 20,
|
|
|
- }))
|
|
|
- const session = multiTurnSession(5, 1)
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
-
|
|
|
- await fiber.dispose()
|
|
|
- await firePreStep(ctx, agent, 1, '')
|
|
|
-
|
|
|
- expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
|
|
|
- expect(ctx.get('compact')).toBeUndefined()
|
|
|
- })
|
|
|
-})
|
|
|
-
|
|
|
-describe('BasicCompactService transcript rendering (delegated to dsh-compact)', () => {
|
|
|
- it('renders reasoning, context, and steering messages', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('rich'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('context/message', {
|
|
|
- content: [{ type: 'text', text: 'project context here' }],
|
|
|
- source: { kind: 'user' },
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [{ type: 'reasoning', text: 'thinking hard' }, { type: 'text', text: 'answer' }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('steering/message', {
|
|
|
- turn: 1,
|
|
|
- content: [{ type: 'text', text: 'steer this way' }],
|
|
|
- source: { kind: 'user' },
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
-
|
|
|
- const nodes = s.surface.nodes
|
|
|
- await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
|
|
|
-
|
|
|
- const { text } = svc.summarizeCalls[0]!
|
|
|
- expect(text).toContain('[Context: project context here]')
|
|
|
- expect(text).toContain('[reasoning: thinking hard]')
|
|
|
- expect(text).toContain('[Steering: steer this way]')
|
|
|
- })
|
|
|
-
|
|
|
- it('labels tool errors distinctly from tool results', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('toolerr'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: 'run it' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [{ type: 'tool-call', id: CallId('c9'), name: 'bash', arguments: '{}' }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('tool/call', { turn: 1, step: 1, callId: CallId('c9'), name: 'bash', arguments: '{}' })
|
|
|
- s.append('tool/result', {
|
|
|
- turn: 1, step: 1, callId: CallId('c9'),
|
|
|
- content: [{ type: 'text', text: 'boom failure' }],
|
|
|
- isError: true,
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
+ void new TokenMeterService(ctx)
|
|
|
+ const compact = new BasicCompactService(ctx, { auto: false })
|
|
|
+ await expect(compact.summarize('history', agent(new Session(SessionId('model-less')))))
|
|
|
+ .rejects.toThrow(/no model available for summarization/)
|
|
|
+ })
|
|
|
+
|
|
|
+ it.each([
|
|
|
+ [{ kind: 'error', message: 'provider failed', code: 'PROVIDER' }, 'PROVIDER', /provider failed/],
|
|
|
+ [{ kind: 'error', message: 'opaque' }, undefined, /opaque/],
|
|
|
+ [{ kind: 'aborted' }, 'ABORTED', /aborted/],
|
|
|
+ [{ kind: 'max-tokens' }, 'MAX_TOKENS', /token cap/],
|
|
|
+ ] as Array<[(StreamChunk & { type: 'finish' })['reason'], string | undefined, RegExp]>) (
|
|
|
+ 'rejects terminal finish %#',
|
|
|
+ async (finish, code, pattern) => {
|
|
|
+ const { compact } = await summarizerHarness([], finish)
|
|
|
+ let thrown: unknown
|
|
|
+ try {
|
|
|
+ await compact.summarize('history', agent(conversation(1), MODEL))
|
|
|
+ } catch (error: unknown) {
|
|
|
+ thrown = error
|
|
|
+ }
|
|
|
+ expect(thrown).toBeInstanceOf(Error)
|
|
|
+ expect((thrown as Error).message).toMatch(pattern)
|
|
|
+ expect((thrown as Error & { code?: string }).code).toBe(code)
|
|
|
+ },
|
|
|
+ )
|
|
|
|
|
|
- const nodes = s.surface.nodes
|
|
|
- await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
|
|
|
- expect(svc.summarizeCalls[0]!.text).toContain('Tool error (call c9): boom failure')
|
|
|
+ it('rejects empty or reasoning-only successful output', async () => {
|
|
|
+ const { compact } = await summarizerHarness([{ type: 'reasoning', text: 'private' }])
|
|
|
+ await expect(compact.summarize('history', agent(conversation(1), MODEL)))
|
|
|
+ .rejects.toThrow(/no text summary content/)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
-describe('BasicCompactService edge cases', () => {
|
|
|
- it('renders bare and nested tool-result placeholders and unknown blocks', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('toolresult'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- // assistant/message carrying a nested tool-result block, an unknown block,
|
|
|
- // and the tool-call that the following tool/result answers (so the surface
|
|
|
- // is tool-pairing balanced).
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [
|
|
|
- { type: 'tool-result', toolCallId: CallId('n1'), content: [{ type: 'chart', data: 'x' } as unknown as ContentBlock] },
|
|
|
- { type: 'custom-widget', payload: 'x' } as unknown as ContentBlock,
|
|
|
- { type: 'tool-call', id: CallId('b1'), name: 'bash', arguments: '{}' },
|
|
|
- ],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- // tool/result whose content is itself only non-text → bare '[tool-result]'.
|
|
|
- s.append('tool/call', { turn: 1, step: 1, callId: CallId('b1'), name: 'bash', arguments: '{}' })
|
|
|
- s.append('tool/result', {
|
|
|
- turn: 1, step: 1, callId: CallId('b1'),
|
|
|
- content: [{ type: 'tool-result', toolCallId: CallId('inner'), content: [] }],
|
|
|
- isError: false,
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
-
|
|
|
- const nodes = s.surface.nodes
|
|
|
- await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
|
|
|
- const { text } = svc.summarizeCalls[0]!
|
|
|
- expect(text).toContain('[tool-result: [chart]]') // nested tool-result with content
|
|
|
- expect(text).toContain('[custom-widget]') // unknown block placeholder
|
|
|
- expect(text).toContain('Tool result (call b1): [tool-result]') // empty nested → bare placeholder
|
|
|
- })
|
|
|
-
|
|
|
- it('estimates unknown block types via JSON length (default branch)', () => {
|
|
|
- const svc = new BasicCompactService(new Context(), cfg({ auto: false }))
|
|
|
- // A block whose type is none of the known kinds — exercises the default arm.
|
|
|
- const unknown = { type: 'custom-widget', payload: 'some data' } as unknown as ContentBlock
|
|
|
- expect(svc.estimateContentTokens([unknown])).toBeGreaterThan(0)
|
|
|
- })
|
|
|
-
|
|
|
- it('auto-compaction reports bounded retry exhaustion after committing a smaller summary', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
- const warnings: string[] = []
|
|
|
- ctx.logger.warn = ((msg: string) => void warnings.push(msg)) as typeof ctx.logger.warn
|
|
|
- void new BasicCompactService(ctx, cfg({
|
|
|
- contextWindow: 300,
|
|
|
- thresholdRatio: 0.1,
|
|
|
- retainTokens: 5,
|
|
|
- compactionRetries: 0,
|
|
|
- }))
|
|
|
- const session = multiTurnSession(4, 1)
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
-
|
|
|
- await ctx.serial('agent/pre-step', agent, 1, 1, '', [], SIGNAL)
|
|
|
- expect(session.events.some(e => e.type === 'compact/summary')).toBe(true)
|
|
|
- // The surface was mutated; the head message is the framed summary checkpoint.
|
|
|
- expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'SUMMARY' })
|
|
|
- expect(warnings.some(w => w.includes('still above threshold after 1 compaction attempts'))).toBe(true)
|
|
|
- })
|
|
|
-
|
|
|
- it('rejects compaction when no turn is open (compaction events must be turn-enclosed)', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- // A session whose only turn has CLOSED — scanning back from the tail hits
|
|
|
- // turn/end before any turn/start, so there is no open turn to enclose
|
|
|
- // compaction's compact/* + replacement events, which the log contract forbids.
|
|
|
- const s = new Session(SessionId('noturn'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: 'orphan' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'reply' }] }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- const nodes = s.surface.nodes
|
|
|
-
|
|
|
- await expect(compactRegion(svc, s, nodes[0]!.seq, nodes[1]!.seq, 'm'))
|
|
|
- .rejects.toThrow(/no open turn/)
|
|
|
- // The lock was never acquired — no compact/start landed.
|
|
|
- expect(s.events.some(e => e.type === 'compact/start')).toBe(false)
|
|
|
- })
|
|
|
-
|
|
|
- it('rejects compaction on a session with no turn boundaries at all', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- // No turn events whatsoever — the open-turn scan falls through to the end
|
|
|
- // of the log and finds none, so compaction is rejected (its events have no
|
|
|
- // turn to enclose them).
|
|
|
- const s = new Session(SessionId('turnless'))
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: 'orphan' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- const nodes = s.surface.nodes
|
|
|
-
|
|
|
- await expect(compactRegion(svc, s, nodes[0]!.seq, nodes[0]!.seq, 'm'))
|
|
|
- .rejects.toThrow(/no open turn/)
|
|
|
- expect(s.events.some(e => e.type === 'compact/start')).toBe(false)
|
|
|
- })
|
|
|
-
|
|
|
- it('compactIfNeeded returns null for empty surface even when over threshold', async () => {
|
|
|
- const svc = createTestService({ contextWindow: 1000, thresholdRatio: 0.1, retainTokens: 5 })
|
|
|
- const session = new Session(SessionId('empty-but-pressured'))
|
|
|
- // No surface nodes, but a large system prompt pushes the estimate over threshold.
|
|
|
- const bigPrompt = 'x'.repeat(800) // ceil(800/4) = 200 tokens >> threshold 100
|
|
|
- expect(await compactIfNeeded(svc, session, bigPrompt, 'm', SIGNAL)).toBeNull()
|
|
|
- })
|
|
|
-
|
|
|
- it('compactRegion throws when end is not a surface node (start valid)', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const session = multiTurnSession(1, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, 9999, 'm'))
|
|
|
- .rejects.toThrow(/end seq 9999 not found in surface/)
|
|
|
- })
|
|
|
+describe('automatic listener and loader composition', () => {
|
|
|
+ function preStep(ctx: Context, owner: Agent): Promise<unknown> {
|
|
|
+ return ctx.serial('agent/pre-step', owner, 1, 1, '', [], SIGNAL)
|
|
|
+ }
|
|
|
|
|
|
- it('compactRegion stringifies a non-Error thrown by summarize', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- // Throw a non-Error value to exercise the String(error) branch in the catch.
|
|
|
- svc.summarizeError = 'plain string failure' as unknown as Error
|
|
|
- const session = multiTurnSession(1, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
+ it('compacts above threshold and remains idle below it', async () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ const compact = new TestCompactService(ctx, {
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.5, retainTokens: 18 } },
|
|
|
+ })
|
|
|
+ const pressured = conversation(4)
|
|
|
+ await preStep(ctx, agent(pressured, MODEL))
|
|
|
+ expect(pressured.events.some(event => event.type === 'compact/summary')).toBe(true)
|
|
|
|
|
|
- // Whole step (user → assistant): a step-aligned region that reaches summarize.
|
|
|
- await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')).rejects.toBe('plain string failure')
|
|
|
- const endEvent = session.events.findLast(e => e.type === 'compact/end')!
|
|
|
- expect(endEvent.data).toMatchObject({ error: 'plain string failure' })
|
|
|
+ const small = conversation(1)
|
|
|
+ await preStep(ctx, agent(small, MODEL))
|
|
|
+ expect(small.events.some(event => event.type === 'compact/start')).toBe(false)
|
|
|
+ expect(compact.calls).toHaveLength(1)
|
|
|
})
|
|
|
|
|
|
- it('auto-compaction listener stringifies a non-Error and proceeds', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
+ it('warns and continues after operational failures, including non-Errors', async () => {
|
|
|
+ const ctx = createContext()
|
|
|
const warnings: string[] = []
|
|
|
- ctx.logger.warn = ((msg: string) => void warnings.push(msg)) as typeof ctx.logger.warn
|
|
|
- const svc = new TestCompactService(ctx, cfg({ contextWindow: 300, thresholdRatio: 0.1, retainTokens: 10 }))
|
|
|
- svc.summarizeError = 'boom' as unknown as Error
|
|
|
- const session = multiTurnSession(3, 1)
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
- const before = session.surface.nodes.length
|
|
|
-
|
|
|
- await ctx.serial('agent/pre-step', agent, 1, 1, '', [], SIGNAL)
|
|
|
- // The failure was swallowed; the surface is untouched and a warning logged.
|
|
|
- expect(session.surface.nodes.length).toBe(before)
|
|
|
- expect(session.events.some(e => e.type === 'compact/summary')).toBe(false)
|
|
|
- expect(warnings.some(w => w.includes('compaction failed: boom'))).toBe(true)
|
|
|
- })
|
|
|
-
|
|
|
- it('auto-compaction listener takes the result-null branch (nothing to compact)', async () => {
|
|
|
- const { ctx } = await ctxWithModel('SUMMARY')
|
|
|
- // A large system prompt pushes the listener's estimate over threshold, but
|
|
|
- // retainTokens is huge so compactIfNeeded walks everything and returns null.
|
|
|
- // threshold = floor(2000*0.1) = 200; invariant: 5 + 150 = 155 ≤ 200.
|
|
|
- const svc = new TestCompactService(ctx, cfg({ contextWindow: 2000, thresholdRatio: 0.1, retainTokens: 150 }))
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const agent = stubAgent(session, 'test-model')
|
|
|
- const bigSystem = 'x'.repeat(900) // ceil(900/4)=225 > threshold 200
|
|
|
-
|
|
|
- await ctx.serial('agent/pre-step', agent, 1, 1, bigSystem, [], SIGNAL)
|
|
|
- expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
|
|
|
- expect(svc.summarizeCalls.length).toBe(0)
|
|
|
- })
|
|
|
-
|
|
|
- it('skips messages whose extracted text is empty across all kinds', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('empties'))
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- s.append('user/message', { content: [{ type: 'text', text: '' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'reasoning', text: '' }] }, { surfaceOp: 'append' })
|
|
|
- s.append('context/message', { content: [], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('steering/message', { turn: 1, content: [{ type: 'text', text: '' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- // Keep the log pairing-valid while the empty result covers the final message kind.
|
|
|
- s.append('step/start', { turn: 1, step: 2 })
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 2,
|
|
|
- content: [{ type: 'tool-call', id: CallId('z1'), name: 'bash', arguments: '{}' }],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- s.append('tool/call', { turn: 1, step: 2, callId: CallId('z1'), name: 'bash', arguments: '{}' })
|
|
|
- s.append('tool/result', { turn: 1, step: 2, callId: CallId('z1'), content: [], isError: false }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 2 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
-
|
|
|
- const nodes = s.surface.nodes
|
|
|
- await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
|
|
|
- expect(svc.summarizeCalls[0]!.text).toBe('Assistant: [tool-call: bash({})]')
|
|
|
- })
|
|
|
-
|
|
|
- it('renders non-text blocks as type-tagged placeholders across all message kinds', async () => {
|
|
|
- const svc = createTestService()
|
|
|
- const s = new Session(SessionId('placeholders'))
|
|
|
- // A plugin-added block type (merge-extensible ContentBlockMap) — the
|
|
|
- // placeholder path must cover every message kind, not just assistant.
|
|
|
- const chart = (id: string): ContentBlock => ({ type: 'chart', data: id } as unknown as ContentBlock)
|
|
|
- s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- s.append('step/start', { turn: 1, step: 1 })
|
|
|
- // user/message with only a plugin-added block → '[chart]' placeholder.
|
|
|
- s.append('user/message', { content: [chart('y')], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- // assistant/message with a plugin-added block AND the tool-call its
|
|
|
- // tool/result answers (so the surface is tool-pairing balanced).
|
|
|
- s.append('assistant/message', {
|
|
|
- turn: 1, step: 1,
|
|
|
- content: [
|
|
|
- chart('z'),
|
|
|
- { type: 'tool-call', id: CallId('e1'), name: 'bash', arguments: '{}' },
|
|
|
- ],
|
|
|
- }, { surfaceOp: 'append' })
|
|
|
- // tool/result with a plugin-added block → '[chart]' placeholder.
|
|
|
- s.append('tool/call', { turn: 1, step: 1, callId: CallId('e1'), name: 'bash', arguments: '{}' })
|
|
|
- s.append('tool/result', { turn: 1, step: 1, callId: CallId('e1'), content: [chart('r')], isError: false }, { surfaceOp: 'append' })
|
|
|
- // context/message and steering/message with plugin-added content.
|
|
|
- s.append('context/message', { content: [chart('c')], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('steering/message', { turn: 1, content: [chart('s')], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- s.append('step/end', { turn: 1, step: 1 })
|
|
|
- s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
|
- s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
-
|
|
|
- const nodes = s.surface.nodes
|
|
|
- await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
|
|
|
- const { text } = svc.summarizeCalls[0]!
|
|
|
- // Every non-text block surfaces as a placeholder rather than being dropped.
|
|
|
- expect(text).toContain('User: [chart]')
|
|
|
- expect(text).toContain('Assistant: [chart]')
|
|
|
- expect(text).toContain('Tool result (call e1): [chart]')
|
|
|
- expect(text).toContain('[Context: [chart]]')
|
|
|
- expect(text).toContain('[Steering: [chart]]')
|
|
|
- })
|
|
|
-
|
|
|
-})
|
|
|
+ ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
|
|
|
+ const compact = new TestCompactService(ctx, {
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.5, retainTokens: 18 } },
|
|
|
+ })
|
|
|
+ compact.error = 'temporary failure'
|
|
|
+ const session = conversation(4)
|
|
|
|
|
|
-describe('BasicCompactService positional range (surface seqs are not monotonic after a replace)', () => {
|
|
|
- it('compacts a second region after the first replace lands a high-seq summary at the head position', async () => {
|
|
|
- // Replacement makes surface seqs non-monotonic. The next region is a
|
|
|
- // positional span even when startSeq > endSeq.
|
|
|
- const svc = createTestService({ auto: false })
|
|
|
- const session = multiTurnSession(4, 1)
|
|
|
-
|
|
|
- // A replacement puts its high-seq summary at the surface head.
|
|
|
- const nodes0 = session.surface.nodes
|
|
|
- const first = await compactRegion(svc, session, nodes0[0]!.seq, nodes0[1]!.seq, 'm')
|
|
|
-
|
|
|
- const nodes1 = session.surface.nodes
|
|
|
- expect(nodes1[0]!.seq).toBeGreaterThanOrEqual(first.summarySeq)
|
|
|
- expect(nodes1[0]!.seq).toBeGreaterThan(nodes1[1]!.seq)
|
|
|
-
|
|
|
- const startSeq = nodes1[0]!.seq
|
|
|
- const endSeq = nodes1[2]!.seq
|
|
|
- expect(startSeq).toBeGreaterThan(endSeq)
|
|
|
- const second = await compactRegion(svc, session, startSeq, endSeq, 'm')
|
|
|
-
|
|
|
- // Selection follows surface positions, not sequence-number order.
|
|
|
- expect(second.shadowedSeqs).toEqual([nodes1[0]!.seq, nodes1[1]!.seq, nodes1[2]!.seq])
|
|
|
- const finalNodes = session.surface.nodes
|
|
|
- expect(finalNodes[0]!.seq).toBeGreaterThanOrEqual(second.summarySeq)
|
|
|
- expect(session.deriveMessages().length).toBe(finalNodes.length)
|
|
|
+ await expect(preStep(ctx, agent(session, MODEL))).resolves.toBeUndefined()
|
|
|
+ expect(warnings).toContainEqual(expect.stringContaining('temporary failure'))
|
|
|
+ expect(session.events.some(event => event.type === 'compact/summary')).toBe(false)
|
|
|
})
|
|
|
|
|
|
- it('extracts the second-compaction transcript in surface order, not log-seq order', async () => {
|
|
|
- const svc = createTestService({ auto: false })
|
|
|
- const session = multiTurnSession(3, 1)
|
|
|
-
|
|
|
- // Put a high-seq summary at the head; log order would place retained older nodes first.
|
|
|
- const n0 = session.surface.nodes
|
|
|
- await compactRegion(svc, session, n0[0]!.seq, n0[1]!.seq, 'm')
|
|
|
-
|
|
|
- const n1 = session.surface.nodes
|
|
|
- svc.summarizeCalls = []
|
|
|
- await compactRegion(svc, session, n1[0]!.seq, n1[2]!.seq, 'm')
|
|
|
-
|
|
|
- // Extraction must match surface and `deriveMessages()` order.
|
|
|
- const { text } = svc.summarizeCalls[0]!
|
|
|
- const checkpointIdx = text.indexOf('compacted-summary')
|
|
|
- const olderIdx = text.indexOf('turn 2 user')
|
|
|
- expect(checkpointIdx).toBeGreaterThanOrEqual(0)
|
|
|
- expect(olderIdx).toBeGreaterThan(checkpointIdx)
|
|
|
+ it('propagates a named unknown-model configuration failure', async () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ void new TestCompactService(ctx)
|
|
|
+ await expect(preStep(ctx, agent(conversation(4), 'missing'))).rejects.toMatchObject({
|
|
|
+ code: TOKEN_METER_MODEL_UNCONFIGURED,
|
|
|
+ model: 'missing',
|
|
|
+ })
|
|
|
})
|
|
|
-})
|
|
|
|
|
|
-describe('BasicCompactService llm inject (real plugin-load path)', () => {
|
|
|
- it('declares llm in static inject so a sibling fiber can resolve ctx.llm', () => {
|
|
|
- // summarize() reads ctx.llm; the inject lets the cordis ctx proxy resolve a sibling
|
|
|
- // LlmService when this service is mounted as its own plugin fiber.
|
|
|
- expect(BasicCompactService.inject).toContain('llm')
|
|
|
+ it('auto:false installs no listener', async () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ void new TestCompactService(ctx, {
|
|
|
+ auto: false,
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.5, retainTokens: 18 } },
|
|
|
+ })
|
|
|
+ const session = conversation(4)
|
|
|
+ await preStep(ctx, agent(session, MODEL))
|
|
|
+ expect(session.events.some(event => event.type === 'compact/start')).toBe(false)
|
|
|
})
|
|
|
|
|
|
- it('resolves ctx.llm and summarizes when mounted as a sibling plugin of LlmService', async () => {
|
|
|
+ it('loads and disposes the real zero-config service stack', async () => {
|
|
|
const ctx = new Context()
|
|
|
await ctx.plugin(LlmService)
|
|
|
- ctx.llm.registerAdapter(['test-model'], new ScriptedAdapter('CONDENSED'))
|
|
|
- // Mount the service through its real plugin fiber (NOT new …(rootCtx)), so
|
|
|
- // the sibling-fiber ctx.llm resolution actually exercises the inject.
|
|
|
- const fiber = await ctx.plugin(BasicCompactService, cfg({ auto: false }))
|
|
|
-
|
|
|
- const svc = ctx.compact as BasicCompactService
|
|
|
- const session = multiTurnSession(2, 1)
|
|
|
- const nodes = session.surface.nodes
|
|
|
- const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model')
|
|
|
- expect(result.summary).toEqual([{ type: 'text', text: 'CONDENSED' }])
|
|
|
+ const meterFiber = await ctx.plugin(TokenMeterService)
|
|
|
+ const compactFiber = await ctx.plugin(BasicCompactService, { auto: false })
|
|
|
|
|
|
- // Tear the fiber down so this test owns no leaked registration; the
|
|
|
- // dedicated cleanup assertion lives in the "HMR safety" suite.
|
|
|
- await fiber.dispose()
|
|
|
+ expect(ctx.tokenMeter.resolve('deepseek-v4-flash').contextWindow).toBe(128_000)
|
|
|
+ expect(ctx.get('compact')).toBeInstanceOf(BasicCompactService)
|
|
|
+ await compactFiber.dispose()
|
|
|
expect(ctx.get('compact')).toBeUndefined()
|
|
|
+ await meterFiber.dispose()
|
|
|
+ expect(ctx.get('tokenMeter')).toBeUndefined()
|
|
|
})
|
|
|
-})
|
|
|
|
|
|
-describe('BasicCompactService under the real invariants plugin', () => {
|
|
|
- /**
|
|
|
- * Drive compaction through a session whose `session/event` listeners include
|
|
|
- * the real dev-mode invariants plugin (as a real app loads it via agent-core).
|
|
|
- * The invariants throw on append, so a passing run proves the compaction
|
|
|
- * sequence is contract-valid: every event is turn-enclosed, and the positional
|
|
|
- * replace op is accepted even when the surface is no longer seq-ordered.
|
|
|
- */
|
|
|
- async function setup(): Promise<{ ctx: Context; session: Session; svc: BasicCompactService }> {
|
|
|
+ it('removes its automatic listener with the plugin fiber', async () => {
|
|
|
const ctx = new Context()
|
|
|
- await ctx.plugin(SessionStore)
|
|
|
- await ctx.plugin(Invariants)
|
|
|
await ctx.plugin(LlmService)
|
|
|
- ctx.llm.registerAdapter(['test-model'], new ScriptedAdapter('CONDENSED'))
|
|
|
- await ctx.plugin(BasicCompactService, cfg({ auto: false }))
|
|
|
- const session = ctx.sessions.create()
|
|
|
- return { ctx, session, svc: ctx.compact as BasicCompactService }
|
|
|
- }
|
|
|
-
|
|
|
- /** Append one closed turn of [user, assistant] surface nodes via the store. */
|
|
|
- function closedTurn(session: Session, turn: number): void {
|
|
|
- session.append('turn/start', { turn, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
- session.append('step/start', { turn, step: 1 })
|
|
|
- session.append('user/message', { content: [{ type: 'text', text: `turn ${turn} user.${LONG_FIXTURE_TEXT}` }], source: { kind: 'user' } }, { surfaceOp: 'append' })
|
|
|
- session.append('assistant/message', { turn, step: 1, content: [{ type: 'text', text: `turn ${turn} assistant.${LONG_FIXTURE_TEXT}` }] }, { surfaceOp: 'append' })
|
|
|
- session.append('step/end', { turn, step: 1 })
|
|
|
- session.append('turn/end', { turn, reason: { kind: 'completed' } })
|
|
|
- }
|
|
|
-
|
|
|
- it('runs a turn-enclosed compaction whose positional replace the invariants accept', async () => {
|
|
|
- const { session, svc } = await setup()
|
|
|
- closedTurn(session, 1)
|
|
|
- closedTurn(session, 2)
|
|
|
- // Open turn 3, as the loop has when the auto-compaction listener fires.
|
|
|
- session.append('turn/start', { turn: 3, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
+ await ctx.plugin(TokenMeterService, {
|
|
|
+ models: { [MODEL]: { contextWindow: 100, charsPerToken: 1_000 } },
|
|
|
+ })
|
|
|
+ const fiber = await ctx.plugin(TestCompactService, {
|
|
|
+ models: { [MODEL]: { thresholdRatio: 0.5, retainTokens: 18 } },
|
|
|
+ })
|
|
|
+ await fiber.dispose()
|
|
|
|
|
|
- const nodes = session.surface.nodes
|
|
|
- // No invariant throws here: compact/* + the replacement are all in turn 3.
|
|
|
- const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model')
|
|
|
- expect(result.shadowedSeqs.length).toBe(2)
|
|
|
- expect(session.surface.nodes[0]!.seq).toBeGreaterThan(session.surface.nodes[1]!.seq)
|
|
|
+ const session = conversation(4)
|
|
|
+ await preStep(ctx, agent(session, MODEL))
|
|
|
+ expect(session.events.some(event => event.type === 'compact/start')).toBe(false)
|
|
|
})
|
|
|
+})
|
|
|
|
|
|
- it('accepts a second compaction over the non-monotonic surface left by the first', async () => {
|
|
|
- const { session, svc } = await setup()
|
|
|
- closedTurn(session, 1)
|
|
|
- closedTurn(session, 2)
|
|
|
- closedTurn(session, 3)
|
|
|
- session.append('turn/start', { turn: 4, trigger: { kind: 'message', source: { kind: 'user' } } })
|
|
|
-
|
|
|
- const n0 = session.surface.nodes
|
|
|
- await compactRegion(svc, session, n0[0]!.seq, n0[1]!.seq, 'test-model')
|
|
|
-
|
|
|
- // Surface head now carries a higher seq than the older retained nodes. A
|
|
|
- // second compaction spanning [head … a later closed-step end] must pass the
|
|
|
- // invariants' positional replace check even though startSeq > endSeq.
|
|
|
- const n1 = session.surface.nodes
|
|
|
- expect(n1[0]!.seq).toBeGreaterThan(n1[2]!.seq)
|
|
|
- const second = await compactRegion(svc, session, n1[0]!.seq, n1[2]!.seq, 'test-model')
|
|
|
- expect(second.shadowedSeqs).toEqual([n1[0]!.seq, n1[1]!.seq, n1[2]!.seq])
|
|
|
+describe('typed unknown-model boundary', () => {
|
|
|
+ it('uses TokenMeterError identity rather than message matching', () => {
|
|
|
+ const ctx = createContext()
|
|
|
+ let thrown: unknown
|
|
|
+ try {
|
|
|
+ ctx.tokenMeter.resolve('missing')
|
|
|
+ } catch (error: unknown) {
|
|
|
+ thrown = error
|
|
|
+ }
|
|
|
+ expect(thrown).toBeInstanceOf(TokenMeterError)
|
|
|
+ expect(thrown).toMatchObject({ code: TOKEN_METER_MODEL_UNCONFIGURED })
|
|
|
})
|
|
|
})
|