Files
deepseek-harness/packages/plan/plan-mode/tests/plan-mode.spec.ts
T
涂轶翔 101f26de4e fix(system-prompt): 保留 section API 的增量系统指令 (#6941)
* feat(system-prompt): admit section updates incrementally

* docs(system-prompt): accept retirement restart gap

* test(system-prompt): cover retirement and reentry before admission

* test(system-prompt): align rebase fixtures and persistence history

* fix: align section prompt consumers and CI fixtures

* test: pin credentials for section snapshot fixtures

* test: align remaining snapshots with section admission

* test: preserve section metadata in Web and PowerShell replays

* refactor(system-prompt): retire sections through Cordis cleanup

* chore: refresh section admission history after rebase

* fix(system-prompt): advance admission head after compaction

* fix(system-prompt): trim sections before admission hashing

* docs: move system upgrade guides to current version

* docs: add system message contribution cookbook

* perf(system-prompt): avoid indexing history for admission

* refactor(system-prompt): derive admission from logged sections

* test(system-prompt): update lifecycle and compaction consumers

* test(system-prompt): refresh CLI section event expectations

* test(system-prompt): refresh question composer section metadata

* feat(system-prompt): resolve same-name sections by scope

* refactor(persona): use ordinary section registrations

* refactor(system-prompt): clarify assembly and related control flow

* test(system-prompt): reconcile rebased section snapshots

* test(python): use current system text helper after rebase

* test(system-prompt): refresh CLI expected section blocks

* test(system-prompt): reconcile latest master integration

* test(agent-loop): seed tools before deferred addition assertion

* refactor(compaction): replace system heads when compacting prefixes

* refactor(system-prompt): separate source text and notice attribution

* fix(system-prompt): preserve legacy instructions in compaction snapshots

* refactor(compaction): build system snapshots while reading the summary prefix

* refactor(compaction): rebuild current system instructions

* refactor(system-prompt): defer compaction rebuild to admission

* refactor(system-prompt): remove redundant fixtures and setup

* fix(plan-mode): announce committed user transitions

* docs(system-prompt): reconcile section lifecycle and compaction design

* refactor(system-prompt): consolidate projection version increments

* fix(system-prompt): reconcile master integration and replay fixtures

* docs(system-prompt): align upgrade guide with current release

* refactor(system-prompt): make admission operations explicit

* fix(system-prompt): reconcile master API and release changes

* docs(system-prompt): regenerate catalogs after master refresh

* refactor(llm-pi-ai): delegate text rendering to pi-ai

* fix(llm-pi-ai): preserve blank lines between system text parts

* refactor(system-prompt): remove redundant retirement metadata

* refactor(system-prompt): own admission and use canonical surface targets

* refactor(system-prompt): compare captured sections at commitment

Assembly fixes section text and lifecycle; PromptAssembly.systemUpdates() compares them with the committed projection when the loop commits. This replaces SystemPrompt.admit(), needsRebuild() and isCurrent(), the SystemAdmission export, the loop's reassembly after compaction, and SessionSurface.systemHeadResetSeq. The projection records a pending rebuild as a boolean that an attributed reset clears.

After a compaction inside a step, the rebuilt head uses the sections captured for that step; later source changes arrive as ordinary updates at the next step.

* test: await the session read fixture and type a matcher for lint

* test(tool-executor-ptc): describe same-name section override on executor replacement

Duplicate section names no longer reject a scoped executor; the earlier registration is overridden and returns as an ordinary update when the executor leaves.

* docs(upgrade-guide): move system-prompt integrations to v0.2.2-rc.3

The root version on master is 0.2.2-rc.3; verify-upgrade-guides rejects a guide added under an earlier version directory.

* refactor(system-prompt): rebuild over system messages without sections

A system message without source.sections now marks rebuilding pending, like a compaction summary. The next model attempt replaces the head with current sections and clears later system messages. This removes the separate legacy flag and its appended whole-prompt replacement and notice.

Sessions written by earlier releases, migrated logs, and imported DeepSeek conversations take the same path, so the importer and its tests return to their master versions.

* test(web): refresh the imported-conversation recording and drop a stray bytecode file

The Web recording for a resumed DeepSeek import was recorded while the prompt was appended after the imported history; the head is replaced again, as on master. The .pyc under the macOS signing skill was staged by accident in an earlier merge.

* refactor(session): scan system nodes on read

surface.systemNodes is read only when a system rebuild is written. Scanning the current surface nodes on each read restores the surface fold's plan types and state to their master definitions, which no longer carry the system message.

* refactor(session): rename SystemSurfaceNode.hasContent to isEmpty

The flag reports that the logged content array has no blocks. isEmpty matches the documented "empty head" wording and covers a head that was written empty without ever being cleared.

* refactor(llm): join system text through extractTextContent

extractTextContent gains a trim option that trims each text block and omits blocks left empty. The DeepSeek serializer, the pi-ai converter, and the prompt inspector use it with a blank-line separator in place of three hand-written joins.

The pi-ai converter now trims system and developer text like the other two, so whitespace-only blocks are omitted there as well.

* test(snapshots): refresh the PowerShell and cordis-inspect recordings

The two PowerShell scenarios still held the initial system message as one unattributed block; replay now writes one block per section with its attribution. The cordis-inspect-jsdoc recording embeds the API catalog, which names SystemSurfaceNode.isEmpty since the rename.

* test(plan-mode,harness-dev-context): cover a rejected pre-step and a repeated legacy prompt

A rejected pre-step must leave a queued plan-mode switch pending and unannounced until the next accepted step. A second unattributed system message with the same legacy paragraphs must keep the admitted paragraphs.

* test(web): refresh Web recordings for section-attributed system messages

Twenty-seven Web recordings still held the initial system message as one unattributed block. Replay now writes one text block per section with its attribution; twenty-six recordings differ only in that line.

plan-review also records the plan-mode switch notice before the first prompt and appends a plan:status update on exit in place of a head replacement and a series header, so its expanded golden gains the notice row.

The Creator persona test joins the system text blocks with blank lines, as the adapters do, before matching the persona line.

* refactor(system-prompt): require the retry argument of systemUpdates

The agent loop passes it on every attempt. A custom loop that omitted it on a retry received a full comparison in place of the pending rebuild, with no diagnostic; the type now makes each caller state which attempt it is.

* docs(upgrade-guide): move the system prompt integrations guide to v0.2.2-rc.5

The root version is 0.2.2-rc.5 after the master merge, and verify-upgrade-guides rejects a guide added under an earlier version directory.

* test(web): refresh the mention path presence recording for section-attributed system messages

* test: align two specs that master left failing

The DeepSeek settings spec waited for a saved notice carrying the provider label from before the rename to DeepSeek API. The Windows packaging workflow spec seeded one of the two helpers that the Stage workflow helpers step copies.

* chore: remove a Python cache file committed by mistake
2026-11-19 21:11:38 +08:00

1267 lines
60 KiB
TypeScript

import { executeToolFixture, executeModelToolFixture } from '@deepseek-ai/dsh-tool-testkit'
import * as NativeToolExecutor from '@deepseek-ai/dsh-tool-executor-native'
import * as PtcToolExecutor from '@deepseek-ai/dsh-tool-executor-ptc'
import { describe, expect, it, vi } from 'vitest'
import { Context } from '@deepseek-ai/cordis'
import { provideWorkingDirectoryFixture } from '@deepseek-ai/dsh-agent-loop-testkit'
import { brandString } from '@deepseek-ai/dsh-brand'
import type { ToolCallId } from '@deepseek-ai/dsh-llm/types'
import { createUserMessage } from '@deepseek-ai/dsh-llm'
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
import ToolRuntime, { RUN_CODE_NAME } from '@deepseek-ai/dsh-tools'
import type { ToolExecutionToken, ToolRunContext } from '@deepseek-ai/dsh-tools'
import { defineContentToolFixture } from '@deepseek-ai/dsh-tool-testkit'
import { Session, type SessionId, SESSION_FORMAT_VERSION, type SessionEvent, type UserMessage } from '@deepseek-ai/dsh-session'
import AgentRegistry, { agentEvents, type Agent } from '@deepseek-ai/dsh-agent'
import { createScope } from '@deepseek-ai/dsh-scope'
import UserQuestionService, {
UserQuestionError, type AskUserQuestionAnswer, type AskUserQuestionRequest,
} from '@deepseek-ai/dsh-user-questions'
import CommandRuntime from '@deepseek-ai/dsh-commands'
import { PtcInspectableValue, PtcRuntime, type PtcRunRequest, type PtcRunResult } from '@deepseek-ai/dsh-ptc-runtime'
import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
import PlanModeController, { EXIT_PLAN_MODE, planProjectionDefinition, resolveConfig } from '../src/index.ts'
import type { PlanModeConfig } from '../src/index.ts'
import type { PlanUnitState } from '../src/types.ts'
const testToolSignal = new AbortController().signal
const TEST_MODEL_SELECTION = { provider: 'test', model: 'test' }
const TEST_PLAN_SECTION = 'Test plan mode instructions.'
const PLAN_CONFIG = { section: TEST_PLAN_SECTION } satisfies PlanModeConfig
interface QuestionAnswerer {
ask(request: AskUserQuestionRequest): Promise<AskUserQuestionAnswer>
}
function registerQuestionAnswerer(ctx: Context, answerer: QuestionAnswerer): () => void {
return ctx.on('user-questions/request', request => answerer.ask(request))
}
/**
* Drives the REAL plugin: mounts `dsh-plan-mode` beside real `SystemPrompt` and
* `ToolRuntime` services, with fake Agents carrying real `Session`s and a
* real scoped `agent.ctx` minted through `createScope`.
* Request boundaries are simulated by dispatching the real pre-step waterfall
* and the following `step/start` session event used by the loop.
*/
async function agentWithSession(
ctx: Context,
id = 'agent-1',
{ active, owner }: { active?: boolean; owner?: Agent } = {},
): Promise<Agent & { session: Session }> {
// A live store session when a store is mounted (the command executor logs
// lifecycle events through it); bare otherwise (fold/tool-only benches).
const session = Session.create(brandString<SessionId>(id), undefined, owner === undefined ? undefined : {
version: SESSION_FORMAT_VERSION, id: brandString<SessionId>(id), createdAt: 0, isSeeded: false, origin: 'subagent', parentSession: owner.id,
})
const agent = {
id: brandString<SessionId>(id),
session,
options: {},
inject(message: UserMessage) {
session.append('user/message', message, { surfaceOp: 'append' })
},
} as Agent & { session: Session }
let scoped!: Context
await ctx.plugin(Object.assign((inner: Context) => { scoped = createScope(inner, agent).ctx }, {
inject: ['tools'],
}))
;(agent as { ctx?: Context }).ctx = scoped
// Seeded plan state lands before the creation announcement, matching resume.
if (active !== undefined) session.append('plan/mode', { active })
// The loop publishes through the live registry when it is composed; narrow
// fold-only benches retain the direct lifecycle event used before it exists.
const agents = ctx.get('agents')
if (agents === undefined) {
await ctx.serial('agent/created', { agent, source: 'startup' })
} else {
agents.enter(agent)
await agents.announce(agent, 'startup')
}
return agent
}
async function assembleFor(ctx: Context, agent: Agent) {
const toolSet = await ctx.tools.resolve({ agent, modelSelection: TEST_MODEL_SELECTION, signal: testToolSignal })
return ctx.systemPrompt.assemble({ agent, scope: agent, toolSet })
}
function foldPlanMode(events: readonly SessionEvent[], end = events.length): boolean {
let state: PlanUnitState = planProjectionDefinition.init()
let index = 0
for (const event of events) {
if (index >= end) break
index++
state = planProjectionDefinition.apply(state, event)
}
return state.active
}
async function mountProjectionSeam(ctx: Context): Promise<void> {
await ctx.plugin(SessionProjectionRegistry)
await ctx.plugin(AgentRegistry)
}
async function setup(config: PlanModeConfig = PLAN_CONFIG): Promise<Context> {
const ctx = new Context()
await mountProjectionSeam(ctx)
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(NativeToolExecutor)
await ctx.plugin(PlanModeController, config)
return ctx
}
/**
* Dispatch pre-step processing and optionally its following step-start commit.
*/
async function boundary(ctx: Context, agent: Agent & { session: Session }, type: 'pre-step' | 'step-start'): Promise<void> {
const events = agentEvents(ctx, agent)
const message = createUserMessage({
content: [{ type: 'text', text: 'boundary probe' }],
source: { kind: 'user' },
})
const signal = new AbortController().signal
const decision = await events.waterfall(
'agent/pre-step',
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [message], turn: 1, step: 1, signal },
() => Promise.resolve({ kind: 'enter' as const, messages: [message] }),
)
if (decision.kind === 'enter') {
for (const message of decision.messages.slice(1)) {
agent.session.append('user/message', message, { surfaceOp: 'append' })
}
}
if (type === 'step-start') {
const event = agent.session.append('step/start', { turn: 1, step: 1 })
ctx.emit('session/event', agent.session, event)
}
}
/** Open a turn so a selection queues for the boundary flush (the mid-turn shape). */
function openTurn(session: Session, turn = 0): void {
session.append('turn/start', { turn })
}
/** Close the open turn (the between-turns shape: selections commit immediately). */
function closeTurn(session: Session, turn = 0): void {
session.append('turn/end', { turn, reason: { kind: 'completed' } })
}
/** Append a minimal `request/header` snapshot so the log has a "what the model was told" anchor. */
function header(session: Session): void {
session.append('request/header', { header: { config: { provider: 'test', model: 'test-model' } }, reason: 'initial' })
}
function noticeTexts(session: Session): string[] {
return session.snapshotEvents()
.filter(event => event.type === 'user/message' && event.data.source.kind !== 'user')
.map(event => (event.data as { content: { type: string; text?: string }[] }).content.map(block => block.text ?? '').join(''))
}
function registerNamedTools(ctx: Context, names: string[]): void {
for (const name of names) {
ctx.tools.register(defineContentToolFixture({
name,
description: `test tool ${name}`,
parameters: {},
execute: () => Promise.resolve([{ type: 'text', text: `ran ${name}` }]),
}))
}
}
/** Assert the mapped PTC mode SDK includes the active plan exit binding and test tools. */
function expectPlanPtcSdkBindings(sdk: string): void {
expect(sdk).toContain('interface ToolArgsMap {')
expect(sdk).toContain('read: Record<string, JsonValue>;')
expect(sdk).toContain('write: Record<string, JsonValue>;')
expect(sdk).toContain('interface ToolOutputMap {')
expect(sdk).toContain('exit_plan_mode: {\n approved: boolean;\n };')
expect(sdk).toContain('[K in ToolName]: (args: ToolArgsMap[K]) => Promise<ToolOutputMap[K]>;')
}
let callCounter = 0
function execute(ctx: Context, name: string, agent?: Agent) {
return executeToolFixture(ctx, {
callId: brandString<ToolCallId>(`call-${++callCounter}`),
name,
arguments: {},
signal: new AbortController().signal,
...agent ? { agent } : {},
})
}
describe('resolveConfig', () => {
it('requires string, non-empty plan instructions', () => {
expect(() => resolveConfig({} as PlanModeConfig))
.toThrow('needs a string `section`')
expect(() => resolveConfig({ section: 5 } as unknown as PlanModeConfig))
.toThrow('needs a string `section`')
expect(() => resolveConfig({ section: ' ' }))
.toThrow('needs a non-empty `section`')
})
it('returns a detached plan config', () => {
const config = { section: TEST_PLAN_SECTION }
const resolved = resolveConfig(config)
expect(resolved).toEqual(config)
expect(resolved).not.toBe(config)
})
it('rejects fields outside the plan policy config', () => {
expect(() => resolveConfig({ section: TEST_PLAN_SECTION, tools: ['read'] } as unknown as PlanModeConfig))
.toThrow('unknown key(s) tools — config is { section }')
})
})
describe('foldPlanMode', () => {
it('folds an empty log to inactive and takes the last plan/mode otherwise', () => {
const session = Session.create(brandString<SessionId>('fold'))
expect(foldPlanMode(session.snapshotEvents())).toBe(false)
session.append('plan/mode', { active: true })
session.append('plan/mode', { active: false })
session.append('plan/mode', { active: true })
expect(foldPlanMode(session.snapshotEvents())).toBe(true)
})
it('folds a prefix when `end` is given', () => {
const session = Session.create(brandString<SessionId>('fold-prefix'))
session.append('plan/mode', { active: true })
session.append('plan/mode', { active: false })
expect(foldPlanMode(session.snapshotEvents(), 1)).toBe(true)
expect(foldPlanMode(session.snapshotEvents(), 0)).toBe(false)
})
})
describe('ctx.planMode: get/set', () => {
it('does not activate without the required projection registry', async () => {
const ctx = new Context()
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(NativeToolExecutor)
await ctx.plugin(PlanModeController, PLAN_CONFIG)
expect(ctx.get('planMode')).toBeUndefined()
})
it('reads the folded state', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
expect(ctx.planMode.get(agent)).toEqual({ active: false })
agent.session.append('plan/mode', { active: true })
expect(ctx.planMode.get(agent)).toEqual({ active: true })
})
it('selects inactive as the plan exit target during an open turn', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
agent.session.append('plan/mode', { active: true })
openTurn(agent.session)
expect(ctx.planMode.set(agent, false)).toBe('queued')
expect(ctx.planMode.get(agent)).toEqual({ active: true, pending: false })
})
it('drops a no-op set (target equals pending, else the current fold)', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
openTurn(agent.session)
expect(ctx.planMode.set(agent, false)).toBe('noop')
expect(ctx.planMode.get(agent)).toEqual({ active: false })
expect(ctx.planMode.set(agent, true)).toBe('queued')
expect(ctx.planMode.set(agent, true)).toBe('noop')
expect(ctx.planMode.get(agent)).toEqual({ active: false, pending: true })
})
it('a between-turns selection commits plan/mode immediately (no boundary would come)', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx, 'agent-idle')
expect(ctx.planMode.set(agent, true)).toBe('committed')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
expect(ctx.planMode.get(agent)).toEqual({ active: true })
// Immediately reversible, still without a boundary.
expect(ctx.planMode.set(agent, false)).toBe('committed')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(false)
// A later boundary finds nothing pending — no double append.
await boundary(ctx, agent, 'step-start')
expect(agent.session.snapshotEvents().filter(event => event.type === 'plan/mode')).toHaveLength(2)
})
it('a between-turns reversal of a mid-turn pending intent cancels without logging', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
openTurn(agent.session)
expect(ctx.planMode.set(agent, true)).toBe('queued')
closeTurn(agent.session)
// Back to the logged state: the pending intent clears, nothing lands.
expect(ctx.planMode.set(agent, false)).toBe('cancelled')
expect(agent.session.snapshotEvents().some(event => event.type === 'plan/mode')).toBe(false)
expect(ctx.planMode.get(agent)).toEqual({ active: false })
})
it('narrates every between-turns transition without request headers', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx, 'agent-idle-narrate')
ctx.planMode.set(agent, true)
ctx.planMode.set(agent, false)
ctx.planMode.set(agent, true)
expect(ctx.planMode.set(agent, true)).toBe('noop')
expect(noticeTexts(agent.session)).toEqual([
'The user switched this session to plan mode.',
'The user switched this session back to the default mode.',
'The user switched this session to plan mode.',
])
})
})
describe('the boundary flush', () => {
it('is inert when no selection is pending', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
const service = ctx.planMode as unknown as { onBoundary(session: Session): void }
expect(() => { service.onBoundary(agent.session) }).not.toThrow()
expect(agent.session.snapshotEvents().some(event => event.type === 'plan/mode')).toBe(false)
})
it('flushes from pre-step before the following step/start', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
await boundary(ctx, agent, 'pre-step')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
expect(ctx.planMode.get(agent)).toEqual({ active: true })
})
it('removes the pre-step flush when the plugin fiber is disposed', async () => {
const ctx = new Context()
await mountProjectionSeam(ctx)
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(NativeToolExecutor)
const fiber = await ctx.plugin(PlanModeController, PLAN_CONFIG)
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
await fiber.dispose()
await boundary(ctx, agent, 'pre-step')
expect(agent.session.snapshotEvents().some(event => event.type === 'plan/mode')).toBe(false)
})
it('flushes at the between-step seam too', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
ctx.planMode.set(agent, true)
await boundary(ctx, agent, 'step-start')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('nets out a flip sequence that returns to the folded mode (no append, no notice)', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
ctx.planMode.set(agent, false)
await boundary(ctx, agent, 'pre-step')
expect(agent.session.snapshotEvents().some(event => event.type === 'plan/mode')).toBe(false)
expect(noticeTexts(agent.session)).toEqual([])
})
it('narrates a committed user selection before the first request header', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
await boundary(ctx, agent, 'pre-step')
expect(noticeTexts(agent.session)).toEqual(['The user switched this session to plan mode.'])
})
it('narrates a pending transition only once', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
await boundary(ctx, agent, 'step-start')
expect(noticeTexts(agent.session)).toEqual(['The user switched this session to plan mode.'])
await boundary(ctx, agent, 'step-start')
expect(noticeTexts(agent.session)).toEqual(['The user switched this session to plan mode.'])
})
it('narrates a switch back to the default mode with the default wording', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
agent.session.append('plan/mode', { active: true })
header(agent.session)
ctx.planMode.set(agent, false)
await boundary(ctx, agent, 'step-start')
expect(noticeTexts(agent.session)).toEqual(['The user switched this session back to the default mode.'])
})
it('narrates re-entry even when the last request header was in plan mode', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
agent.session.append('plan/mode', { active: true })
header(agent.session)
openTurn(agent.session)
ctx.planMode.set(agent, false)
await boundary(ctx, agent, 'step-start')
ctx.planMode.set(agent, true)
await boundary(ctx, agent, 'step-start')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
expect(noticeTexts(agent.session)).toEqual([
'The user switched this session back to the default mode.',
'The user switched this session to plan mode.',
])
})
it('contains an append failure instead of blocking the prompt or the turn', async () => {
const ctx = await setup()
const warn = vi.fn()
ctx.logger.warn = warn as never
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
const original = agent.session.append.bind(agent.session)
// Only the flush's own plan/mode append fails; the boundary event itself
// lands (the loop appended it before the between-step hook fires).
agent.session.append = (((type: string, ...rest: unknown[]) => {
if (type === 'plan/mode') throw new Error('backend gone')
return (original as (...args: unknown[]) => unknown)(type, ...rest)
}) as unknown) as typeof agent.session.append
await boundary(ctx, agent, 'step-start')
expect(warn).toHaveBeenCalledOnce()
// The failed flush re-parks the intent (cleared only after a landed
// append), so the next healthy boundary converges the log with the
// picker's optimistic state instead of dropping the switch forever.
expect(ctx.planMode.get(agent)).toEqual({ active: false, pending: true })
agent.session.append = original
await boundary(ctx, agent, 'step-start')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
expect(ctx.planMode.get(agent).pending).toBeUndefined()
})
it('contains a pre-step append failure and keeps the intent pending', async () => {
const ctx = await setup()
const warn = vi.fn()
ctx.logger.warn = warn as never
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
const original = agent.session.append.bind(agent.session)
agent.session.append = (((type: string, ...rest: unknown[]) => {
if (type === 'plan/mode') throw new Error('backend gone')
return (original as (...args: unknown[]) => unknown)(type, ...rest)
}) as unknown) as typeof agent.session.append
await boundary(ctx, agent, 'pre-step')
expect(warn).toHaveBeenCalledOnce()
expect(ctx.planMode.get(agent)).toEqual({ active: false, pending: true })
})
})
describe('the soft layer', () => {
it('adds the exit schema with the first plan guidance and keeps it after leaving plan mode', async () => {
const ctx = await setup()
registerNamedTools(ctx, ['read', 'write'])
const agent = await agentWithSession(ctx)
const defaultAssembly = await assembleFor(ctx, agent)
expect(defaultAssembly.tools.map(tool => tool.name)).toEqual(['read', 'write'])
expect(defaultAssembly.sections.find(section => section.name === 'plan:policy')?.text).toBe('')
agent.session.append('plan/mode', { active: true })
const planAssembly = await assembleFor(ctx, agent)
expect(planAssembly.tools.map(tool => tool.name)).toEqual([EXIT_PLAN_MODE, 'read', 'write'])
expect(planAssembly.sections.find(section => section.name === 'plan:policy')?.text).toBe(`The following instructions apply only while plan mode is active.\n\n${TEST_PLAN_SECTION}`)
agent.session.append('plan/mode', { active: false })
const leftAssembly = await assembleFor(ctx, agent)
expect(leftAssembly.tools).toEqual(planAssembly.tools)
expect(leftAssembly.sections.find(section => section.name === 'plan:policy')?.text).toBe(planAssembly.sections.find(section => section.name === 'plan:policy')?.text)
})
it('leaves an agent-less assembly untouched', async () => {
const ctx = await setup()
registerNamedTools(ctx, ['read'])
const assembly = await ctx.systemPrompt.assemble()
expect(assembly.tools.map(tool => tool.name)).toEqual([EXIT_PLAN_MODE, 'read'])
expect(assembly.sections.find(section => section.name === 'plan:policy')?.text).toBe('')
expect(assembly.sections.find(section => section.name === 'plan:status')?.text).toBe('')
})
it('keeps the full toolset in plan mode and renders the configured mode section', async () => {
const ctx = await setup()
registerNamedTools(ctx, ['read', 'write', 'todo_write'])
const agent = await agentWithSession(ctx, 'agent-1', { active: true })
const assembly = await assembleFor(ctx, agent)
expect(assembly.tools.map(tool => tool.name).sort()).toEqual([EXIT_PLAN_MODE, 'read', 'todo_write', 'write'])
expect(assembly.sections.find(section => section.name === 'plan:policy')?.text).toBe(`The following instructions apply only while plan mode is active.\n\n${TEST_PLAN_SECTION}`)
})
it('keeps independently registered tools available across plan changes', async () => {
const ctx = await setup()
registerNamedTools(ctx, ['read', 'added-later'])
const planning = await agentWithSession(ctx, 'planning', { active: true })
expect((await assembleFor(ctx, planning)).tools.map(tool => tool.name))
.toEqual(['added-later', 'exit_plan_mode', 'read'])
const defaulted = await agentWithSession(ctx, 'defaulted')
expect((await assembleFor(ctx, defaulted)).tools.map(tool => tool.name))
.toEqual(['added-later', 'read'])
})
it('keeps run_code the only wire tool in plan mode under the registry PTC mode; the SDK gains the exit binding', async () => {
// Minimal scriptable runtime: the SDK section resolves ctx.ptcRuntime at
// assembly time (the ptc.spec fake's shape).
class FakeRuntime extends PtcRuntime {
resolve(request: import('@deepseek-ai/dsh-ptc-runtime').PtcRunRequest): import('@deepseek-ai/dsh-ptc-runtime').PtcRunSpec { return { ...request, cwd: request.cwd ?? process.cwd(), timeoutMs: request.timeoutMs ?? 120_000 } }
readonly language = 'typescript'
readonly isolation = 'fake'
run(_request: PtcRunRequest): Promise<PtcRunResult> { return Promise.resolve({ output: [] }) }
}
const ctx = new Context()
await mountProjectionSeam(ctx)
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(PtcToolExecutor)
await ctx.plugin(FakeRuntime)
await ctx.plugin(PlanModeController, PLAN_CONFIG)
registerNamedTools(ctx, ['read', 'write'])
const agent = await agentWithSession(ctx, 'agent-1', { active: true })
const assembly = await assembleFor(ctx, agent)
expect(assembly.tools.map(tool => tool.name)).toEqual(['run_code'])
// The SDK documents the full binding set plus the exit; plan mode never
// prunes capabilities and restrains through guidance alone.
const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text ?? ''
expectPlanPtcSdkBindings(sdk)
})
it('includes the PTC exit binding from the first plan-mode entry', async () => {
class FakeRuntime extends PtcRuntime {
resolve(request: import('@deepseek-ai/dsh-ptc-runtime').PtcRunRequest): import('@deepseek-ai/dsh-ptc-runtime').PtcRunSpec { return { ...request, cwd: request.cwd ?? process.cwd(), timeoutMs: request.timeoutMs ?? 120_000 } }
readonly language = 'typescript'
readonly isolation = 'fake'
run(_request: PtcRunRequest): Promise<PtcRunResult> { return Promise.resolve({ output: [] }) }
}
const withPlanMode = new Context()
await mountProjectionSeam(withPlanMode)
await withPlanMode.plugin(SystemPrompt)
await withPlanMode.plugin(ToolRuntime)
await withPlanMode.plugin(PtcToolExecutor)
await withPlanMode.plugin(FakeRuntime)
await withPlanMode.plugin(PlanModeController, PLAN_CONFIG)
registerNamedTools(withPlanMode, ['read', 'write'])
const agent = await agentWithSession(withPlanMode)
const defaultSdk = (await assembleFor(withPlanMode, agent)).sections.find(section => section.name === 'tools:sdk')?.text ?? ''
expect(defaultSdk).not.toContain('exit_plan_mode:')
agent.session.append('plan/mode', { active: true })
const planSdk = (await assembleFor(withPlanMode, agent)).sections.find(section => section.name === 'tools:sdk')?.text ?? ''
expectPlanPtcSdkBindings(planSdk)
agent.session.append('plan/mode', { active: false })
const leftSdk = (await assembleFor(withPlanMode, agent)).sections.find(section => section.name === 'tools:sdk')?.text ?? ''
expect(leftSdk).toBe(planSdk)
const bare = new Context()
await bare.plugin(SessionProjectionRegistry)
await bare.plugin(SystemPrompt)
await bare.plugin(ToolRuntime)
await bare.plugin(PtcToolExecutor)
await bare.plugin(FakeRuntime)
registerNamedTools(bare, ['read', 'write'])
const bareSdk = (await bare.systemPrompt.assemble({ agent })).sections.find(section => section.name === 'tools:sdk')?.text ?? ''
expect(bareSdk).not.toContain('exit_plan_mode:')
expect(defaultSdk).toBe(bareSdk)
})
})
describe('no execution gating beyond the exit tool', () => {
it('passes agent-less and default-mode executions through', async () => {
const ctx = await setup()
registerNamedTools(ctx, ['write'])
const agentless = await execute(ctx, 'write')
expect(agentless.isError).toBe(false)
const agent = await agentWithSession(ctx)
const defaulted = await execute(ctx, 'write', agent)
expect(defaulted.isError).toBe(false)
})
it('runs every call in plan mode untouched — guidance and enforcement are separate axes', async () => {
const ctx = await setup()
registerNamedTools(ctx, ['read', 'write', 'bash'])
const agent = await agentWithSession(ctx, 'agent-1', { active: true })
for (const name of ['read', 'write', 'bash']) {
const result = await execute(ctx, name, agent)
expect(result.isError).toBe(false)
}
})
})
describe('/plan', () => {
it('registers only when a commands service is composed and optionally submits the next-step message', async () => {
const bare = await setup()
expect(bare.get('commands')).toBeUndefined()
const ctx = await setup()
await ctx.plugin(CommandRuntime)
// The `ctx.inject` child mounts asynchronously once `commands` resolves.
await new Promise(resolve => setImmediate(resolve))
const plainAgent = await agentWithSession(ctx, 'plain-plan-command')
openTurn(plainAgent.session)
const plainSteer = vi.fn()
;(plainAgent as unknown as { steer: typeof plainSteer }).steer = plainSteer
expect(ctx.commands.list(plainAgent)).toEqual([
{ definitionId: '@deepseek-ai/dsh-plan-mode', name: 'plan', description: 'Enter or leave plan mode', input: { hint: '[off|message]', attachments: true } },
])
const signal = new AbortController().signal
expect(await ctx.commands.execute(plainAgent, '/mode', [], signal)).toBeUndefined()
expect(await ctx.commands.execute(plainAgent, '/review', [], signal)).toBeUndefined()
const plain = await ctx.commands.execute(plainAgent, '/plan', [], signal)
expect(plain?.result).toEqual({
kind: 'success',
text: 'Entering plan mode (applies from the next step). Use /plan off to leave.',
})
expect(ctx.planMode.get(plainAgent)).toEqual({ active: false, pending: true })
expect(plainSteer).not.toHaveBeenCalled()
const messageAgent = await agentWithSession(ctx, 'message-plan-command')
openTurn(messageAgent.session)
const messageSteer = vi.fn()
;(messageAgent as unknown as { steer: typeof messageSteer }).steer = messageSteer
const plan = await ctx.commands.execute(messageAgent, '/plan draft the migration ', [], signal)
expect(plan?.result).toEqual({
kind: 'success',
text: 'Entering plan mode (applies from the next step). Use /plan off to leave.',
})
expect(ctx.planMode.get(messageAgent)).toEqual({ active: false, pending: true })
expect(messageSteer).toHaveBeenCalledExactlyOnceWith({
id: expect.any(String) as unknown,
role: 'user',
content: [{ type: 'text', text: 'draft the migration' }],
source: { kind: 'user' },
})
})
it('leaves active plan mode, cancels a pending entry, and treats inactive exit as idempotent', async () => {
const ctx = await setup()
await ctx.plugin(CommandRuntime)
await new Promise(resolve => setImmediate(resolve))
const signal = new AbortController().signal
const inactive = await agentWithSession(ctx, 'inactive-plan-command')
expect((await ctx.commands.execute(inactive, '/plan off', [], signal))?.result)
.toEqual({ kind: 'success', text: 'Plan mode is already inactive.' })
expect(ctx.planMode.get(inactive)).toEqual({ active: false })
const entering = await agentWithSession(ctx, 'entering-plan-command')
openTurn(entering.session)
const enteringSteer = vi.fn()
;(entering as unknown as { steer: typeof enteringSteer }).steer = enteringSteer
await ctx.commands.execute(entering, '/plan', [], signal)
expect((await ctx.commands.execute(entering, '/plan off', [], signal))?.result)
.toEqual({ kind: 'success', text: 'Plan mode entry cancelled.' })
expect(ctx.planMode.get(entering)).toEqual({ active: false, pending: false })
expect(enteringSteer).not.toHaveBeenCalled()
await boundary(ctx, entering, 'step-start')
expect(ctx.planMode.get(entering)).toEqual({ active: false })
expect(entering.session.snapshotEvents().some(event => event.type === 'plan/mode')).toBe(false)
const active = await agentWithSession(ctx, 'active-plan-command', { active: true })
openTurn(active.session)
const activeSteer = vi.fn()
;(active as unknown as { steer: typeof activeSteer }).steer = activeSteer
expect((await ctx.commands.execute(active, '/plan off', [], signal))?.result)
.toEqual({ kind: 'success', text: 'Leaving plan mode (applies from the next step).' })
expect(ctx.planMode.get(active)).toEqual({ active: true, pending: false })
expect((await ctx.commands.execute(active, '/plan off', [], signal))?.result)
.toEqual({ kind: 'success', text: 'Leaving plan mode (applies from the next step).' })
expect(activeSteer).not.toHaveBeenCalled()
await boundary(ctx, active, 'step-start')
expect(ctx.planMode.get(active)).toEqual({ active: false })
})
it('idle sessions get the immediate-commit copy on both /plan and /plan off', async () => {
const ctx = await setup()
await ctx.plugin(CommandRuntime)
await new Promise(resolve => setImmediate(resolve))
const signal = new AbortController().signal
const agent = await agentWithSession(ctx, 'idle-plan-command')
expect((await ctx.commands.execute(agent, '/plan', [], signal))?.result)
.toEqual({ kind: 'success', text: 'Plan mode on. Use /plan off to leave.' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
expect((await ctx.commands.execute(agent, '/plan off', [], signal))?.result)
.toEqual({ kind: 'success', text: 'Plan mode off.' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(false)
})
it('steers mixed attachments with or without text and refuses them on /plan off', async () => {
const ctx = await setup()
await ctx.plugin(CommandRuntime)
await new Promise(resolve => setImmediate(resolve))
let saved = 0
const saveImage = (input: { mediaType: string }) => {
saved += 1
return Promise.resolve({
attachmentId: `att-${saved}`, mediaType: input.mediaType, bytes: 3, width: 1, height: 1,
})
}
ctx.provide('attachments', {
imageLimits: {
maxImageBytes: 1024, maxImagesPerMessage: 4, maxMessageImageBytes: 1024,
maxImagePixels: 1_000_000, mediaTypes: ['image/png'],
},
validateImage: () => Promise.resolve(),
saveImage,
async saveImages(inputs: readonly { mediaType: string }[]) {
const refs = []
for (const input of inputs) refs.push(await saveImage(input))
return refs
},
saveFile(input: { data: Uint8Array; name?: string }) {
saved += 1
return Promise.resolve({
attachmentId: `att-${saved}`, bytes: input.data.byteLength, name: input.name ?? 'attachment',
})
},
})
ctx.commands.registerFileReceiptResolver((agent, receiptId) => receiptId === 'receipt-notes'
? { attachmentId: `file-${agent.id}` as never, bytes: 5, name: 'notes.txt' }
: undefined)
const signal = new AbortController().signal
const attachments = [
{ type: 'image' as const, mediaType: 'image/png' as const, data: 'AAAA', name: 'diagram.png' },
{ type: 'file' as const, receiptId: 'receipt-notes' },
]
const agent = await agentWithSession(ctx, 'imaged-plan-command')
openTurn(agent.session)
const steer = vi.fn()
;(agent as unknown as { steer: typeof steer }).steer = steer
const withMessage = await ctx.commands.execute(agent, '/plan sketch the layout', attachments, signal)
expect(withMessage?.result.kind).toBe('success')
expect(steer).toHaveBeenCalledExactlyOnceWith({
id: expect.any(String) as unknown,
role: 'user',
content: [
{ type: 'image', attachment: expect.objectContaining({ attachmentId: 'att-1' }) as unknown },
{ type: 'file', attachment: expect.objectContaining({ attachmentId: 'file-imaged-plan-command', name: 'notes.txt' }) as unknown },
{ type: 'text', text: 'sketch the layout' },
],
source: { kind: 'user' },
})
const bareAgent = await agentWithSession(ctx, 'imaged-bare-plan-command')
openTurn(bareAgent.session)
const bareSteer = vi.fn()
;(bareAgent as unknown as { steer: typeof bareSteer }).steer = bareSteer
expect((await ctx.commands.execute(bareAgent, '/plan', attachments, signal))?.result)
.toEqual({ kind: 'success', text: 'Entering plan mode (applies from the next step). Use /plan off to leave.' })
expect(bareSteer).toHaveBeenCalledExactlyOnceWith({
id: expect.any(String) as unknown,
role: 'user',
content: [
{ type: 'image', attachment: expect.objectContaining({ attachmentId: 'att-2' }) as unknown },
{ type: 'file', attachment: expect.objectContaining({ attachmentId: 'file-imaged-bare-plan-command', name: 'notes.txt' }) as unknown },
],
source: { kind: 'user' },
})
expect(ctx.planMode.get(bareAgent)).toEqual({ active: false, pending: true })
const activeAgent = await agentWithSession(ctx, 'imaged-off-plan-command', { active: true })
const offSteer = vi.fn()
;(activeAgent as unknown as { steer: typeof offSteer }).steer = offSteer
expect((await ctx.commands.execute(activeAgent, '/plan off', attachments, signal))?.result)
.toEqual({ kind: 'error', text: 'Attachments cannot accompany /plan off.' })
expect(offSteer).not.toHaveBeenCalled()
expect(ctx.planMode.get(activeAgent)).toEqual({ active: true })
})
it('removes the contributed command when the plan-mode plugin is disposed', async () => {
const ctx = new Context()
await mountProjectionSeam(ctx)
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(NativeToolExecutor)
await ctx.plugin(CommandRuntime)
const fiber = await ctx.plugin(PlanModeController, PLAN_CONFIG)
await new Promise(resolve => setImmediate(resolve))
const agent = await agentWithSession(ctx)
expect(ctx.commands.list(agent).map(command => command.name)).toEqual(['plan'])
await fiber.dispose()
expect(ctx.commands.list(agent)).toEqual([])
})
})
describe('exit_plan_mode', () => {
async function setupWithReview(answer?: { selected: string[]; custom?: string }) {
const ctx = await setup()
await ctx.plugin(UserQuestionService)
const asked: AskUserQuestionRequest[] = []
if (answer !== undefined) {
registerQuestionAnswerer(ctx, {
ask: (request) => {
asked.push(request)
return Promise.resolve({ answers: [{ id: 'plan-review', ...answer }] })
},
})
}
const agent = await agentWithSession(ctx, 'agent-1', { active: true })
return { ctx, agent, asked }
}
async function callExit(ctx: Context, agent: Agent | undefined, plan = '# The plan\n\ndo things') {
const signal = new AbortController().signal
const toolSet = agent === undefined ? undefined : await ctx.tools.resolve({ agent, modelSelection: TEST_MODEL_SELECTION, signal })
return executeToolFixture(ctx, {
...toolSet === undefined ? {} : { toolSet },
callId: brandString<ToolCallId>(`call-exit-${++callCounter}`),
name: EXIT_PLAN_MODE,
arguments: { plan },
signal: new AbortController().signal,
...agent ? { agent } : {},
})
}
it('registers the tool with one required plan argument', async () => {
const ctx = await setup()
const schema = ctx.tools.schemas().find(entry => entry.name === EXIT_PLAN_MODE)
const parameters = schema?.parameters as { required?: string[]; properties?: Record<string, unknown> }
expect(schema?.description).toMatch(/^Use only in plan mode\./)
expect(Object.keys(parameters.properties ?? {})).toEqual(['plan'])
expect(parameters.required).toEqual(['plan'])
})
it('rejects an agent-less call', async () => {
const ctx = await setup()
const result = await callExit(ctx, undefined)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: unknown tool "exit_plan_mode"' }])
})
it('omits and rejects the exit tool before the Session enters plan mode', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
expect((await assembleFor(ctx, agent)).tools.map(tool => tool.name)).not.toContain(EXIT_PLAN_MODE)
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: unknown tool "exit_plan_mode"' }])
})
it('rejects a direct execution without a calling Agent', async () => {
const ctx = await setup()
const exit = ctx.tools.get(EXIT_PLAN_MODE)
expect(exit).toBeDefined()
const exec: ToolRunContext = {
callId: brandString<ToolCallId>('direct-exit'), rootCallId: brandString<ToolCallId>('direct-exit'), name: EXIT_PLAN_MODE,
arguments: { plan: '# P' }, token: Symbol('direct-exit') as ToolExecutionToken, signal: testToolSignal,
deferContext() {}, concludeTurn() {},
}
await expect(exit?.execute({ plan: '# P' }, exec)).rejects.toMatchObject({
name: 'ToolContextUnavailableError',
code: 'TOOL_CONTEXT_UNAVAILABLE',
message: `${EXIT_PLAN_MODE} requires a calling agent`,
})
})
it('keeps the exit tool after leaving plan mode and rejects a call outside it', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx, 'left-plan', { active: true })
agent.session.append('plan/mode', { active: false })
expect((await assembleFor(ctx, agent)).tools.map(tool => tool.name)).toContain(EXIT_PLAN_MODE)
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: exit_plan_mode is only available in plan mode' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_NOT_ACTIVE' })
})
it('admits the exit tool for a selected entry but rejects a call before the entry is logged', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx)
openTurn(agent.session)
ctx.planMode.set(agent, true)
expect((await assembleFor(ctx, agent)).tools.map(tool => tool.name)).toContain(EXIT_PLAN_MODE)
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: exit_plan_mode is only available in plan mode' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_NOT_ACTIVE' })
})
it('rejects an empty or heading-less plan before asking the reviewer', async () => {
const { ctx, agent, asked } = await setupWithReview({ selected: ['Approve'] })
for (const plan of ['', 'do things']) {
const result = await callExit(ctx, agent, plan)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: invalid arguments: exit_plan_mode requires a non-empty markdown plan starting with a # heading' }])
expect(result.error?.info).toEqual({ name: 'ToolArgsError', code: 'INVALID_ARGS' })
}
expect(asked).toHaveLength(0)
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('degrades to the manual exit when no user-questions seam is composed', async () => {
const ctx = await setup()
const agent = await agentWithSession(ctx, 'agent-1', { active: true })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: no user-questions channel is available to review the plan; ask the user to switch the session mode instead' }])
expect(result.error?.info).toEqual({ name: 'ToolContextUnavailableError', code: 'TOOL_CONTEXT_UNAVAILABLE' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('degrades the same way when the seam has no provider (NO_PROVIDER)', async () => {
const { ctx, agent } = await setupWithReview()
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: no user-questions answerer accepted the request' }])
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('rejects review from a subagent with consumer-neutral guidance', async () => {
const ctx = await setup()
await ctx.plugin(UserQuestionService)
const ask = vi.fn(async () => ({ answers: [{ id: 'plan-review', selected: ['Approve'] }] }))
registerQuestionAnswerer(ctx, { ask })
const root = await agentWithSession(ctx, 'review-root')
const child = await agentWithSession(ctx, 'review-child', { active: true, owner: root })
const result = await callExit(ctx, child)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{
type: 'text',
text: "Error: human interaction is unavailable while the calling agent is owned by another live agent; include the unresolved question or decision in the child agent's final result",
}])
expect(ask).not.toHaveBeenCalled()
expect(foldPlanMode(child.session.snapshotEvents())).toBe(true)
})
it('approve: records the boundary-applied switch and confirms (the fold flips at the flush)', async () => {
const { ctx, agent, asked } = await setupWithReview({ selected: ['Approve'] })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(false)
if (result.isError) throw new Error('expected approved plan result')
expect(result.value).toEqual({ approved: true })
expect(result.content).toEqual([{ type: 'text', text: 'Plan approved — plan mode exited; carry out the plan starting with your next step.' }])
// Boundary-applied, not a direct append: the fold stays plan until the
// step's end, so the plan policy covers any remaining call of the SAME batch.
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
expect(ctx.planMode.get(agent)).toEqual({ active: true, pending: false })
await boundary(ctx, agent, 'step-start')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(false)
expect(asked).toHaveLength(1)
expect(asked[0]?.agent).toBe(agent)
expect(asked[0]?.questions[0]?.detail).toBe('# The plan\n\ndo things')
expect(asked[0]?.questions[0]?.options?.map(option => option.label)).toEqual(['Approve', 'Keep planning'])
})
it('carries the exact plan through a PTC mode review and logs the nested dispatch', async () => {
const plan = '# PTC mode plan\n\nUse the existing seam.'
class ExitRuntime extends PtcRuntime {
resolve(request: import('@deepseek-ai/dsh-ptc-runtime').PtcRunRequest): import('@deepseek-ai/dsh-ptc-runtime').PtcRunSpec { return { ...request, cwd: request.cwd ?? process.cwd(), timeoutMs: request.timeoutMs ?? 120_000 } }
readonly language = 'typescript'
readonly isolation = 'fake'
async run(request: PtcRunRequest): Promise<PtcRunResult> {
const exit = request.bindings[0]?.functions[EXIT_PLAN_MODE]
if (exit === undefined) throw new Error('missing exit_plan_mode binding')
const resolved = await exit({ plan })
return { output: [], value: resolved instanceof PtcInspectableValue ? resolved.value : resolved }
}
}
const ctx = new Context()
await mountProjectionSeam(ctx)
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(PtcToolExecutor)
await ctx.plugin(ExitRuntime)
provideWorkingDirectoryFixture(ctx)
await ctx.plugin(PlanModeController, PLAN_CONFIG)
await ctx.plugin(UserQuestionService)
const asked: AskUserQuestionRequest[] = []
registerQuestionAnswerer(ctx, {
ask: (request) => {
asked.push(request)
return Promise.resolve({ answers: [{ id: 'plan-review', selected: ['Approve'] }] })
},
})
const agent = await agentWithSession(ctx, 'ptc-exit', { active: true })
const result = await executeModelToolFixture(ctx, {
toolSet: await ctx.tools.resolve({ agent, modelSelection: TEST_MODEL_SELECTION, signal: testToolSignal }),
callId: brandString<ToolCallId>(`call-exit-${++callCounter}`),
name: RUN_CODE_NAME,
arguments: { code: `return await tools.${EXIT_PLAN_MODE}({ plan: ${JSON.stringify(plan)} })`, description: 'Submit the plan for review' },
signal: new AbortController().signal,
agent,
})
expect(result.isError).toBe(false)
expect(asked).toHaveLength(1)
expect(asked[0]?.questions[0]).toMatchObject({
header: 'Plan review',
question: 'Approve this plan and leave plan mode?',
detail: plan,
})
expect(agent.session.snapshotEvents().find(event => event.type === 'tool/ptc-dispatch')?.data).toMatchObject({
name: EXIT_PLAN_MODE,
arguments: { plan },
isError: false,
})
expect(ctx.planMode.get(agent)).toEqual({ active: true, pending: false })
})
it('an approved exit removes the guidance from the next proposed assembly and keeps the tool', async () => {
const { ctx, agent } = await setupWithReview({ selected: ['Approve'] })
const approved = await callExit(ctx, agent)
expect(approved.isError).toBe(false)
// Calls of the SAME assistant response were requested under the existing
// plan-shaped header. Pending state shapes the proposed next assembly;
// the accepted boundary then commits the matching durable fold. The tool
// stays declared because the Session has entered plan mode.
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
const assembly = await assembleFor(ctx, agent)
expect(assembly.tools.some(tool => tool.name === EXIT_PLAN_MODE)).toBe(true)
expect(assembly.sections.find(section => section.name === 'plan:policy')?.text).toContain('Test plan mode instructions.')
expect(assembly.sections.find(section => section.name === 'plan:status')?.text).toBe('Plan mode is inactive. The plan-mode instructions no longer apply.')
await boundary(ctx, agent, 'step-start')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(false)
const afterExit = await assembleFor(ctx, agent)
expect(afterExit.tools).toEqual(assembly.tools)
expect(afterExit.sections.find(section => section.name === 'plan:policy')?.text).toContain(TEST_PLAN_SECTION)
})
it('the exit flush narrates nothing — the tool result is the narration', async () => {
const { ctx, agent } = await setupWithReview({ selected: ['Approve'] })
header(agent.session)
await callExit(ctx, agent)
await boundary(ctx, agent, 'step-start')
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(false)
expect(noticeTexts(agent.session)).toEqual([])
})
it('keep planning returns the corrective error carrying the feedback verbatim', async () => {
const { ctx, agent } = await setupWithReview({ selected: ['Keep planning'], custom: 'consider the resume path' })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: The user chose to keep planning; their feedback: consider the resume path' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVISION_REQUESTED' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('keep planning without feedback returns the generic corrective error', async () => {
const { ctx, agent } = await setupWithReview({ selected: ['Keep planning'] })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: The user chose to keep planning; revise the plan and present it again.' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVISION_REQUESTED' })
})
it('a custom-text-only answer is feedback, never consent', async () => {
const { ctx, agent } = await setupWithReview({ selected: [], custom: 'add tests first' })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: The user chose to keep planning; their feedback: add tests first' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVISION_REQUESTED' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('requires exactly the single Approve selection', async () => {
const { ctx, agent } = await setupWithReview({ selected: ['Approve', 'Keep planning'] })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: The user chose to keep planning; revise the plan and present it again.' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVISION_REQUESTED' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('treats custom text alongside Approve as feedback, not consent', async () => {
const { ctx, agent } = await setupWithReview({ selected: ['Approve'], custom: 'change the tests' })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: The user chose to keep planning; their feedback: change the tests' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVISION_REQUESTED' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('treats duplicate review answer items as non-consent', async () => {
const { ctx, agent } = await setupWithReview()
registerQuestionAnswerer(ctx, {
ask: () => Promise.resolve({ answers: [
{ id: 'plan-review', selected: ['Approve'] },
{ id: 'plan-review', selected: ['Keep planning'] },
] }),
})
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: The user chose to keep planning; revise the plan and present it again.' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVISION_REQUESTED' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('a missing answer item reads as keep-planning', async () => {
const { ctx, agent } = await setupWithReview()
registerQuestionAnswerer(ctx, { ask: () => Promise.resolve({ answers: [] }) })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: The user chose to keep planning; revise the plan and present it again.' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVISION_REQUESTED' })
})
it('declares the plan-review presentation intent naming its approve option', async () => {
const { ctx, agent, asked } = await setupWithReview({ selected: ['Approve'] })
await callExit(ctx, agent)
const question = asked[0]?.questions[0]
expect(question?.intent).toEqual({ kind: 'plan-review', approve: 'Approve', callId: `call-exit-${callCounter}` })
// The named label is one this same question offers, so a UI honouring the
// intent answers a choice this tool accepts.
expect(question?.options?.map(option => option.label)).toContain(question?.intent?.approve)
})
it('reads a dismissed review as the user taking the turn back: a turn-concluding success, not a failure', async () => {
const { ctx, agent } = await setupWithReview()
registerQuestionAnswerer(ctx, {
ask: () => Promise.reject(Object.assign(
new Error('the user cancelled ask_user_question'),
{ name: 'UserQuestionError', code: 'ASK_CANCELLED' },
)),
})
const result = await callExit(ctx, agent)
expect(result.isError).toBe(false)
expect(result.concludesTurn).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'The user dismissed the plan review to reply in their own words; plan mode remains active.' }])
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('leaves every other review failure its own message', async () => {
const { ctx, agent } = await setupWithReview()
registerQuestionAnswerer(ctx, {
ask: () => Promise.reject(new UserQuestionError(
'ask_user_question was aborted before the user answered', 'ASK_ABORTED')),
})
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: ask_user_question was aborted before the user answered' }])
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('forwards the execution abort signal to the review question', async () => {
const { ctx, agent, asked } = await setupWithReview({ selected: ['Approve'] })
const controller = new AbortController()
const result = await executeToolFixture(ctx, {
toolSet: await ctx.tools.resolve({ agent, modelSelection: TEST_MODEL_SELECTION, signal: testToolSignal }),
callId: brandString<ToolCallId>(`call-exit-${++callCounter}`),
name: EXIT_PLAN_MODE,
arguments: { plan: '# P' },
agent,
signal: controller.signal,
})
expect(result.isError).toBe(false)
expect(asked[0]?.signal).toBe(controller.signal)
})
it('fails the call when the plugin is disposed while the review awaits (no phantom exit)', async () => {
const ctx = new Context()
await mountProjectionSeam(ctx)
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(NativeToolExecutor)
const fiber = await ctx.plugin(PlanModeController, PLAN_CONFIG)
await ctx.plugin(UserQuestionService)
let answer!: (value: { answers: { id: string; selected: string[] }[] }) => void
registerQuestionAnswerer(ctx, {
ask: () => new Promise((resolve) => { answer = resolve }),
})
const agent = await agentWithSession(ctx, 'agent-1', { active: true })
const pending = callExit(ctx, agent)
// Let execute reach the review await, then unload the plugin (HMR) and
// only afterwards approve. The boundary listeners are gone, so a success
// would claim an exit that can never flush — the call must fail instead.
await new Promise(resolve => setImmediate(resolve))
await fiber.dispose()
answer({ answers: [{ id: 'plan-review', selected: ['Approve'] }] })
const result = await pending
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: the plan-mode service was reloaded while the plan was under review; present the plan again' }])
expect(result.error?.info).toEqual({ name: 'PlanModeError', code: 'PLAN_REVIEW_INTERRUPTED' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('a throwing provider surfaces as the corrective isError and the mode stays plan', async () => {
const { ctx, agent } = await setupWithReview()
registerQuestionAnswerer(ctx, { ask: () => { throw new Error('review aborted') } })
const result = await callExit(ctx, agent)
expect(result.isError).toBe(true)
expect(result.content).toEqual([{ type: 'text', text: 'Error: review aborted' }])
expect(result.error?.info).toEqual({ name: 'UserQuestionError', code: 'PROVIDER_ERROR' })
expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true)
})
it('presents the call as a generic card titled by the plan first heading', async () => {
const ctx = await setup()
const def = ctx.tools.get(EXIT_PLAN_MODE)!
expect(def.presentCall?.({ plan: '## Fix the flake\n\nsteps' })).toEqual({
card: 'generic',
title: 'Fix the flake',
kind: 'other',
content: [{ type: 'text', text: '## Fix the flake\n\nsteps' }],
})
expect(def.presentCall?.({ plan: 'no heading here' })).toEqual({
card: 'generic',
title: 'Plan',
kind: 'other',
content: [{ type: 'text', text: 'no heading here' }],
})
})
it('presents the result as a generic review card', async () => {
const ctx = await setup()
const def = ctx.tools.get(EXIT_PLAN_MODE)!
const content = [{ type: 'text' as const, text: 'ok' }]
expect(def.presentResult?.({ plan: '# P' }, { content, isError: false })).toEqual({
card: 'generic',
title: 'Plan review',
content,
})
})
})
describe('HMR disposal', () => {
it('unregisters the service, listeners, prompt section, and exit tool with the plugin fiber', async () => {
const ctx = new Context()
await mountProjectionSeam(ctx)
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRuntime)
await ctx.plugin(NativeToolExecutor)
const fiber = await ctx.plugin(PlanModeController, PLAN_CONFIG)
const agent = await agentWithSession(ctx, 'disposed-recovery')
openTurn(agent.session)
ctx.planMode.set(agent, true)
expect(ctx.get('planMode')).toBeInstanceOf(PlanModeController)
expect(ctx.tools.get(EXIT_PLAN_MODE)).toBeDefined()
const admitted = await ctx.systemPrompt.assemble({ agent, session: agent.session })
expect(admitted.sections.map(section => section.name)).toContain('plan:policy')
for (const message of admittedMessages(admitted)) {
agent.session.append('system/message', { turn: 1, step: 1, message }, { surfaceOp: 'append' })
}
await fiber.dispose()
expect(ctx.get('planMode')).toBeUndefined()
expect(ctx.tools.get(EXIT_PLAN_MODE)).toBeUndefined()
expect((await ctx.systemPrompt.assemble()).sections.find(section => section.name === 'plan:policy')).toBeUndefined()
const retirement = await ctx.systemPrompt.assemble({ agent, session: agent.session })
expect(retirement.sections.find(section => section.name === 'plan:policy')).toMatchObject({ retire: true, text: 'Plan mode is no longer available. Its instructions no longer apply.' })
expect(admittedMessages(retirement).flatMap(message => message.source.sections ?? []))
.toContainEqual(expect.objectContaining({ name: 'plan:policy', state: 'retired' }))
await boundary(ctx, agent, 'step-start')
expect(agent.session.snapshotEvents().some(event => event.type === 'plan/mode')).toBe(false)
})
})
function admittedMessages(assembly: import('@deepseek-ai/dsh-system-prompt').PromptAssembly) {
return assembly.systemUpdates(false).map(update => update.message)
}