|
| 1 | +/** |
| 2 | + * End-to-end over processStream: a lane that puts a thinking model's chain of |
| 3 | + * thought in `content` instead of `reasoning_content` must not reach a surface |
| 4 | + * as prose, and must never reach one carrying a literal `</think>`. |
| 5 | + */ |
| 6 | +import { TEST_AGENT_RUNTIME_IMPL } from '@codebuff/common/testing/impl/agent-runtime' |
| 7 | +import { getInitialSessionState } from '@codebuff/common/types/session-state' |
| 8 | +import { beforeEach, describe, expect, it } from 'bun:test' |
| 9 | + |
| 10 | +import { mockFileContext } from './test-utils' |
| 11 | +import { processStream } from '../tools/stream-parser' |
| 12 | + |
| 13 | +import type { AgentTemplate } from '../templates/types' |
| 14 | +import type { |
| 15 | + AgentRuntimeDeps, |
| 16 | + AgentRuntimeScopedDeps, |
| 17 | +} from '@codebuff/common/types/contracts/agent-runtime' |
| 18 | +import type { StreamChunk } from '@codebuff/common/types/contracts/llm' |
| 19 | +import type { Message } from '@codebuff/common/types/messages/codebuff-message' |
| 20 | +import type { PrintModeEvent } from '@codebuff/common/types/print-mode' |
| 21 | +import type { PromptResult } from '@codebuff/common/util/error' |
| 22 | + |
| 23 | +const testAgentTemplate: AgentTemplate = { |
| 24 | + id: 'test-agent', |
| 25 | + displayName: 'Test Agent', |
| 26 | + spawnerPrompt: 'Test agent', |
| 27 | + model: 'deepseek/deepseek-v4-flash', |
| 28 | + inputSchema: {}, |
| 29 | + outputMode: 'structured_output', |
| 30 | + includeMessageHistory: true, |
| 31 | + inheritParentSystemPrompt: false, |
| 32 | + mcpServers: {}, |
| 33 | + toolNames: ['read_files', 'end_turn'], |
| 34 | + spawnableAgents: [], |
| 35 | + systemPrompt: 'Test system prompt', |
| 36 | + instructionsPrompt: 'Test instructions', |
| 37 | + stepPrompt: 'Test step prompt', |
| 38 | +} |
| 39 | + |
| 40 | +/** What a surface would render: streamed text, and the thinking box. */ |
| 41 | +interface Rendered { |
| 42 | + text: string |
| 43 | + reasoning: string |
| 44 | + fullResponse: string |
| 45 | +} |
| 46 | + |
| 47 | +describe('processStream — leaked think tags', () => { |
| 48 | + let agentRuntimeImpl: AgentRuntimeDeps & AgentRuntimeScopedDeps |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + agentRuntimeImpl = { ...TEST_AGENT_RUNTIME_IMPL, sendAction: () => {} } |
| 52 | + }) |
| 53 | + |
| 54 | + async function render( |
| 55 | + chunks: StreamChunk[], |
| 56 | + priorHistory: Message[] = [], |
| 57 | + ): Promise<Rendered> { |
| 58 | + async function* stream(): AsyncGenerator< |
| 59 | + StreamChunk, |
| 60 | + PromptResult<string | null> |
| 61 | + > { |
| 62 | + for (const chunk of chunks) yield chunk |
| 63 | + return { aborted: false, value: 'msg-id' } |
| 64 | + } |
| 65 | + |
| 66 | + const sessionState = getInitialSessionState(mockFileContext) |
| 67 | + const agentState = sessionState.mainAgentState |
| 68 | + agentState.messageHistory = [...priorHistory] |
| 69 | + |
| 70 | + let text = '' |
| 71 | + let reasoning = '' |
| 72 | + const result = await processStream({ |
| 73 | + ...agentRuntimeImpl, |
| 74 | + agentContext: {}, |
| 75 | + agentState, |
| 76 | + agentStepId: 'test-step-id', |
| 77 | + agentTemplate: testAgentTemplate, |
| 78 | + ancestorRunIds: [], |
| 79 | + clientSessionId: 'test-session', |
| 80 | + fileContext: mockFileContext, |
| 81 | + fingerprintId: 'test-fingerprint', |
| 82 | + fullResponse: '', |
| 83 | + localAgentTemplates: { 'test-agent': testAgentTemplate }, |
| 84 | + messages: agentState.messageHistory, |
| 85 | + prompt: 'test prompt', |
| 86 | + repoId: undefined, |
| 87 | + repoUrl: undefined, |
| 88 | + runId: 'test-run-id', |
| 89 | + signal: new AbortController().signal, |
| 90 | + stream: stream(), |
| 91 | + system: 'test system', |
| 92 | + tools: {}, |
| 93 | + userId: 'test-user', |
| 94 | + userInputId: 'test-input-id', |
| 95 | + onCostCalculated: async () => {}, |
| 96 | + onResponseChunk: (chunk: string | PrintModeEvent) => { |
| 97 | + if (typeof chunk === 'string') { |
| 98 | + text += chunk |
| 99 | + } else if (chunk.type === 'reasoning_delta') { |
| 100 | + reasoning += chunk.text |
| 101 | + } |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + return { text, reasoning, fullResponse: result.fullResponse } |
| 106 | + } |
| 107 | + |
| 108 | + const textChunks = (...texts: string[]): StreamChunk[] => |
| 109 | + texts.map((t) => ({ type: 'text' as const, text: t })) |
| 110 | + |
| 111 | + const leakedAssistantTurn: Message[] = [ |
| 112 | + { |
| 113 | + role: 'assistant', |
| 114 | + content: [{ type: 'text', text: 'earlier thought</think>earlier answer' }], |
| 115 | + } as Message, |
| 116 | + ] |
| 117 | + |
| 118 | + it('routes a paired block to the thinking box', async () => { |
| 119 | + const { text, reasoning } = await render( |
| 120 | + textChunks('<think>weigh the options</think>', 'The answer.'), |
| 121 | + ) |
| 122 | + expect(reasoning).toBe('weigh the options') |
| 123 | + expect(text).toBe('The answer.') |
| 124 | + }) |
| 125 | + |
| 126 | + it('never streams a bare close marker, even unarmed', async () => { |
| 127 | + const { text, reasoning } = await render( |
| 128 | + textChunks('Saw the anchor.', '</think>', 'Now the fix.'), |
| 129 | + ) |
| 130 | + expect(text).toBe('Saw the anchor.Now the fix.') |
| 131 | + expect(text).not.toContain('</think>') |
| 132 | + expect(reasoning).toBe('') |
| 133 | + }) |
| 134 | + |
| 135 | + it('reclassifies the head once a prior turn proved the lane leaks', async () => { |
| 136 | + const { text, reasoning } = await render( |
| 137 | + textChunks('Ключевая зацепка: ', 'the bundle knows.', '</think>Real answer.'), |
| 138 | + leakedAssistantTurn, |
| 139 | + ) |
| 140 | + expect(reasoning).toBe('Ключевая зацепка: the bundle knows.') |
| 141 | + expect(text).toBe('Real answer.') |
| 142 | + }) |
| 143 | + |
| 144 | + it('releases the head as text when the marker never comes', async () => { |
| 145 | + // The lossless guarantee: a wrong guess delays an answer, never hides it. |
| 146 | + const { text, reasoning } = await render( |
| 147 | + textChunks('A clean answer this time.'), |
| 148 | + leakedAssistantTurn, |
| 149 | + ) |
| 150 | + expect(text).toBe('A clean answer this time.') |
| 151 | + expect(reasoning).toBe('') |
| 152 | + }) |
| 153 | + |
| 154 | + it('stands down when the lane does populate native reasoning', async () => { |
| 155 | + const { text, reasoning } = await render( |
| 156 | + [ |
| 157 | + { type: 'reasoning', text: 'native thought' }, |
| 158 | + ...textChunks('The answer.'), |
| 159 | + ], |
| 160 | + leakedAssistantTurn, |
| 161 | + ) |
| 162 | + expect(reasoning).toBe('native thought') |
| 163 | + expect(text).toBe('The answer.') |
| 164 | + }) |
| 165 | + |
| 166 | + it('leaves fullResponse raw so the turn-end and arming signals survive', async () => { |
| 167 | + // isThinkOnlyResponse reads fullResponse to keep a thinking-only step from |
| 168 | + // ending the turn; historyLeaksThinkTags reads the history to arm the next. |
| 169 | + const { fullResponse } = await render(textChunks('thought', '</think>done')) |
| 170 | + expect(fullResponse).toBe('thought</think>done') |
| 171 | + }) |
| 172 | +}) |
0 commit comments