2026-06-26 13:51:01 +08:00
|
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
|
import { Context } from 'cordis'
|
2026-07-15 13:22:44 +08:00
|
|
|
import { toolPairingBalancedAfter, toolPairingBalancedBefore } from '@deepseek-ai/dsh-compact'
|
2026-06-26 13:51:01 +08:00
|
|
|
import type { ContentBlock, GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
|
|
|
|
|
import { CallId, LlmAdapter } from '@deepseek-ai/dsh-llm'
|
2026-07-16 17:39:53 +08:00
|
|
|
import { defineTool } from '@deepseek-ai/dsh-tools'
|
2026-07-18 12:33:49 +08:00
|
|
|
import type { Agent } from '@deepseek-ai/dsh-agent'
|
2026-07-14 02:32:35 +08:00
|
|
|
import AgentLoop from '@deepseek-ai/dsh-agent-loop'
|
2026-07-16 17:39:53 +08:00
|
|
|
import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
|
2026-06-26 13:51:01 +08:00
|
|
|
import * as Invariants from '@deepseek-ai/dsh-invariants'
|
|
|
|
|
import { BasicCompactService } from '@deepseek-ai/dsh-compact-basic'
|
2026-07-15 14:47:29 +08:00
|
|
|
import TokenMeterService from '@deepseek-ai/dsh-token-meter'
|
2026-07-18 12:21:15 +08:00
|
|
|
import { SessionId, type SurfaceEvent } from '@deepseek-ai/dsh-session'
|
2026-06-26 13:51:01 +08:00
|
|
|
|
|
|
|
|
/**
|
2026-07-13 23:27:00 +08:00
|
|
|
* CBR-001 regression through the real loop. A replacement checkpoint has a high
|
|
|
|
|
* log seq at the surface head and carries no tool pair, so both adjacent cuts
|
|
|
|
|
* must be safe and re-compacting that checkpoint alone must succeed. This pins
|
|
|
|
|
* surface-position semantics rather than raw-log scanning.
|
2026-06-26 13:51:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class ReproCompactService extends BasicCompactService {
|
2026-07-14 21:57:52 +08:00
|
|
|
override async summarize(): Promise<{ summary: ContentBlock[]; provider: string; model: string }> {
|
2026-07-17 22:38:46 +08:00
|
|
|
return {
|
|
|
|
|
summary: [{ type: 'text', text: 'CHECKPOINT SUMMARY' }],
|
|
|
|
|
provider: 'mock',
|
|
|
|
|
model: 'stub',
|
|
|
|
|
}
|
2026-06-26 13:51:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Each call emits one tool-call until exhausted, then a final text answer. */
|
|
|
|
|
class StepwiseToolAdapter extends LlmAdapter {
|
|
|
|
|
calls = 0
|
|
|
|
|
constructor(private toolSteps: number) {
|
|
|
|
|
super()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async * stream(_options: GenerateOptions): AsyncIterable<StreamChunk> {
|
|
|
|
|
const n = this.calls
|
|
|
|
|
this.calls += 1
|
|
|
|
|
if (n < this.toolSteps) {
|
|
|
|
|
const id = CallId(`c${n}`)
|
|
|
|
|
const args = `{"i":${n}}`
|
|
|
|
|
yield { type: 'block-start', index: 0, blockType: 'text' }
|
|
|
|
|
yield { type: 'block-end', index: 0, block: { type: 'text', text: `step ${n}` } }
|
|
|
|
|
yield { type: 'block-start', index: 1, blockType: 'tool-call' }
|
|
|
|
|
yield { type: 'block-end', index: 1, block: { type: 'tool-call', id, name: 'work', arguments: args } }
|
|
|
|
|
yield { type: 'finish', reason: { kind: 'tool-calls' } }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
yield { type: 'block-start', index: 0, blockType: 'text' }
|
|
|
|
|
yield { type: 'block-end', index: 0, block: { type: 'text', text: 'all done' } }
|
|
|
|
|
yield { type: 'finish', reason: { kind: 'stop' } }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function harness(toolSteps: number): Promise<{ ctx: Context; compact: ReproCompactService }> {
|
|
|
|
|
const ctx = new Context()
|
2026-07-16 17:39:53 +08:00
|
|
|
await mountAgentLoopTestDependencies(ctx)
|
2026-07-12 03:51:55 +08:00
|
|
|
await ctx.plugin(Invariants)
|
2026-06-26 13:51:01 +08:00
|
|
|
await ctx.plugin(AgentLoop, { agents: [] })
|
2026-07-16 12:58:07 +08:00
|
|
|
await ctx.plugin(TokenMeterService, { contextWindow: 400 })
|
2026-06-26 13:51:01 +08:00
|
|
|
ctx.llm.registerAdapter(['mock'], new StepwiseToolAdapter(toolSteps))
|
|
|
|
|
ctx.tools.register(defineTool({
|
|
|
|
|
name: 'work',
|
|
|
|
|
description: 'does work',
|
|
|
|
|
parameters: { i: { type: 'number' } },
|
|
|
|
|
async execute() {
|
|
|
|
|
return [{ type: 'text', text: 'work result' }]
|
|
|
|
|
},
|
|
|
|
|
}))
|
2026-07-16 12:58:07 +08:00
|
|
|
// Small window so several tool steps cross the threshold and compaction
|
|
|
|
|
// fires within the runaway turn after enough history can shrink.
|
2026-06-26 13:51:01 +08:00
|
|
|
const compact = new ReproCompactService(ctx, {
|
|
|
|
|
auto: true,
|
|
|
|
|
thresholdRatio: 0.5,
|
2026-07-16 12:58:07 +08:00
|
|
|
retainTokens: 50,
|
2026-07-01 22:10:49 +08:00
|
|
|
summarizationModel: '',
|
|
|
|
|
maxTokens: 8192,
|
|
|
|
|
compactionRetries: 1,
|
2026-06-26 13:51:01 +08:00
|
|
|
})
|
|
|
|
|
return { ctx, compact }
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 02:32:35 +08:00
|
|
|
function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
|
2026-06-26 13:51:01 +08:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const dispose = ctx.on('agent/status', (subject, status) => {
|
|
|
|
|
if (subject === agent && status === 'idle') {
|
|
|
|
|
dispose()
|
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('CBR-001: a real-loop checkpoint is a valid boundary on both sides', () => {
|
|
|
|
|
it('the head checkpoint the loop lands is a balanced cut on both sides', async () => {
|
|
|
|
|
const { ctx } = await harness(8)
|
|
|
|
|
try {
|
2026-07-18 12:21:15 +08:00
|
|
|
const agent = ctx.agentLoop.create(SessionId('repro'), { provider: 'mock', model: 'mock' })
|
2026-06-26 13:51:01 +08:00
|
|
|
agent.send([{ type: 'text', text: 'do a long multi-step task' }])
|
|
|
|
|
await waitForIdle(ctx, agent)
|
|
|
|
|
|
|
|
|
|
const events = [...agent.session.events]
|
|
|
|
|
// A compaction ran: at least one checkpoint landed on the surface.
|
|
|
|
|
const checkpoints = events.filter(
|
|
|
|
|
(e): e is SurfaceEvent =>
|
|
|
|
|
e.type === 'user/message'
|
|
|
|
|
&& typeof (e as SurfaceEvent).surfaceOp === 'object',
|
|
|
|
|
)
|
|
|
|
|
expect(checkpoints.length).toBeGreaterThan(0)
|
|
|
|
|
|
2026-07-13 23:27:00 +08:00
|
|
|
// High log position does not make a text-only checkpoint mid-step; both
|
|
|
|
|
// its start and end cuts are balanced in surface order.
|
2026-06-26 13:51:01 +08:00
|
|
|
const nodes = agent.session.surface.nodes
|
|
|
|
|
for (const cp of checkpoints) {
|
2026-07-13 23:56:10 +08:00
|
|
|
const index = nodes.indexOf(cp.seq)
|
|
|
|
|
if (index === -1) continue // shadowed by a later checkpoint — no longer an edge.
|
2026-07-17 22:20:06 +08:00
|
|
|
expect(toolPairingBalancedBefore(agent.session, cp.seq),
|
2026-07-13 23:56:10 +08:00
|
|
|
`checkpoint seq ${cp.seq} must be a balanced region START`).toBe(true)
|
2026-07-17 22:20:06 +08:00
|
|
|
expect(toolPairingBalancedAfter(agent.session, cp.seq),
|
2026-07-13 23:56:10 +08:00
|
|
|
`checkpoint seq ${cp.seq} must be a balanced region END`).toBe(true)
|
2026-06-26 13:51:01 +08:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
await ctx.fiber.dispose()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|