Skip to content

Commit 989a056

Browse files
authored
fix(prompt): stop mutating the message array for related questions (#971)
The related-questions reminder was injected into every agent call through a prepareCall hook. It is never persisted, so the slot it occupies is filled by the assistant reply when the next turn replays history, and the provider's prompt cache stops matching there. Both shipped placements behave the same way: folding it into the current user message, and appending it as a trailing message of its own. Only not injecting anything keeps the prefix stable. Remove the hook and give the model the format example from history instead. stripSpecFromMessages dropped every spec fence on replay, so a thread never showed an answer that carried the block. Keeping the related-questions block and stripping the rest leaves that example in place while still keeping older spec payloads out of the context window. The replay path needs its own strip because its output has to depend only on the message it is given. The clipboard helper normalizes whitespace unconditionally, which cannot return its input unchanged, so it stays as it is and keeps its current callers. Fixes #969
1 parent dbba0f4 commit 989a056

6 files changed

Lines changed: 134 additions & 238 deletions

File tree

‎lib/agents/__tests__/researcher.test.ts‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
22

33
import type { Model } from '@/lib/types/models'
44

5-
import { RELATED_QUESTIONS_REMINDER } from '../prompts/related-questions-reminder'
6-
75
const mocks = vi.hoisted(() => ({
86
agentOptions: undefined as Record<string, any> | undefined
97
}))
@@ -116,29 +114,4 @@ describe('createResearcher', () => {
116114
openai: { reasoningEffort: 'low' }
117115
})
118116
})
119-
120-
it('places the related-questions reminder after conversation history', () => {
121-
createResearcher({
122-
model: 'openai:model-id',
123-
modelConfig: createModel('openai')
124-
})
125-
126-
const prepareCall = mocks.agentOptions?.prepareCall
127-
const result = prepareCall({
128-
messages: [
129-
{ role: 'user', content: 'Initial question' },
130-
{ role: 'assistant', content: 'Initial answer' },
131-
{ role: 'user', content: 'Deep follow-up' }
132-
]
133-
})
134-
135-
expect(result.messages.at(-1)).toEqual({
136-
role: 'user',
137-
content: [{ type: 'text', text: RELATED_QUESTIONS_REMINDER }]
138-
})
139-
expect(result.messages.at(-2)).toEqual({
140-
role: 'user',
141-
content: 'Deep follow-up'
142-
})
143-
})
144117
})

‎lib/agents/prompts/__tests__/related-questions-reminder.test.ts‎

Lines changed: 0 additions & 142 deletions
This file was deleted.

‎lib/agents/prompts/related-questions-reminder.ts‎

Lines changed: 0 additions & 53 deletions
This file was deleted.

‎lib/agents/researcher.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { SearchMode } from '../types/search'
1111
import { getModel } from '../utils/registry'
1212
import { isTracingEnabled } from '../utils/telemetry'
1313

14-
import { appendRelatedQuestionsReminder } from './prompts/related-questions-reminder'
1514
import {
1615
getAdaptiveModePrompt,
1716
QUICK_MODE_PROMPT
@@ -144,15 +143,6 @@ export function createResearcher({
144143
tools,
145144
activeTools: activeToolsList,
146145
stopWhen: stepCountIs(maxSteps),
147-
prepareCall: options => ({
148-
...options,
149-
...(options.messages && {
150-
messages: appendRelatedQuestionsReminder(
151-
options.messages,
152-
modelConfig?.providerId
153-
)
154-
})
155-
}),
156146
...(providerOptions && { providerOptions }),
157147
// Spans join the parent Langfuse trace via OTel context propagation
158148
experimental_telemetry: {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import type { UIMessage } from 'ai'
2+
import { describe, expect, it } from 'vitest'
3+
4+
import { stripSpecFromMessages } from '../strip-spec-from-messages'
5+
6+
const RELATED_BLOCK = [
7+
'```spec',
8+
'{"op":"add","path":"/root","value":"main"}',
9+
'{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{"direction":"vertical","gap":"sm"},"children":["header","questions"]}}',
10+
'{"op":"add","path":"/elements/header","value":{"type":"Heading","props":{"title":"Related","icon":"related"},"children":[]}}',
11+
'{"op":"add","path":"/elements/questions","value":{"type":"Stack","props":{"direction":"vertical","gap":"xs"},"children":["q1","q2","q3"]}}',
12+
'{"op":"add","path":"/elements/q1","value":{"type":"Button","props":{"text":"A follow-up question","variant":"link","icon":"arrow-right"},"on":{"press":{"action":"submitQuery","params":{"query":"A follow-up question"}}},"children":[]}}',
13+
'```'
14+
].join('\n')
15+
16+
const IMAGE_BLOCK = [
17+
'```spec',
18+
'{"op":"add","path":"/root","value":"gallery"}',
19+
'{"op":"add","path":"/elements/gallery","value":{"type":"ImageGrid","props":{"columns":3},"children":[]}}',
20+
'```'
21+
].join('\n')
22+
23+
function assistant(text: string): UIMessage {
24+
return {
25+
id: 'a1',
26+
role: 'assistant',
27+
parts: [{ type: 'text', text }]
28+
} as unknown as UIMessage
29+
}
30+
31+
function textOf(message: UIMessage): string {
32+
const part = message.parts[0]
33+
return part.type === 'text' ? part.text : ''
34+
}
35+
36+
describe('stripSpecFromMessages', () => {
37+
it('keeps the related-questions block', () => {
38+
const input = `Here is the answer.\n\n${RELATED_BLOCK}`
39+
const [result] = stripSpecFromMessages([assistant(input)])
40+
41+
expect(textOf(result)).toBe(input)
42+
})
43+
44+
it('strips a spec block that is not the related-questions block', () => {
45+
const input = `Here is the answer.\n\n${IMAGE_BLOCK}`
46+
const [result] = stripSpecFromMessages([assistant(input)])
47+
48+
expect(textOf(result)).toBe('Here is the answer.')
49+
})
50+
51+
it('keeps only the related block when a message carries both', () => {
52+
const input = [
53+
'Intro paragraph.',
54+
'',
55+
IMAGE_BLOCK,
56+
'',
57+
'Closing paragraph.',
58+
'',
59+
RELATED_BLOCK
60+
].join('\n')
61+
62+
const [result] = stripSpecFromMessages([assistant(input)])
63+
64+
expect(textOf(result)).toBe(
65+
['Intro paragraph.', '', 'Closing paragraph.', '', RELATED_BLOCK].join(
66+
'\n'
67+
)
68+
)
69+
})
70+
71+
it('passes text without a spec block through byte-identically', () => {
72+
const input = '# Title\n\n\nBody with trailing space \n\nand a tail.\n\n'
73+
const [result] = stripSpecFromMessages([assistant(input)])
74+
75+
expect(textOf(result)).toBe(input)
76+
})
77+
78+
it('leaves user and system messages untouched', () => {
79+
const user = {
80+
id: 'u1',
81+
role: 'user',
82+
parts: [{ type: 'text', text: `Question\n\n${IMAGE_BLOCK}` }]
83+
} as unknown as UIMessage
84+
const system = {
85+
id: 's1',
86+
role: 'system',
87+
parts: [{ type: 'text', text: `Instructions\n\n${IMAGE_BLOCK}` }]
88+
} as unknown as UIMessage
89+
90+
const result = stripSpecFromMessages([user, system])
91+
92+
expect(result[0]).toBe(user)
93+
expect(result[1]).toBe(system)
94+
})
95+
})

0 commit comments

Comments
 (0)