Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

feat(agent): a new turn carries the open plan instead of hoping for it

David7ea419b18f45
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index 52b9b081..815838bf 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -71,6 +71,7 @@ import { explainError as explainToolError } from '../api/agents/error-hints'
import { finalStripThinkingTags, splitOrphanCloser, splitUnclosedThink } from '../lib/thinking-stripper'
import { openPlanGap, planReconcileSteer, PLAN_RECONCILE_BUDGET } from '../lib/plan-reconcile'
import { PlanStaleness, planStalenessSteer } from '../lib/plan-staleness'
+import { planResumeAnchor } from '../lib/plan-resume'
import { reasoningOnlyRound, REASONING_CONTINUE_BUDGET, REASONING_CONTINUE_STEER } from '../lib/reasoning-round'
import { findUnbackedLinks, unbackedLinksSteer } from '../lib/unbacked-links'
import { useTodoStore } from '../stores/todoStore'
@@ -612,6 +613,21 @@ export function useAgentChat() {
// skipped. Caps derive from the user's actual request.
const userPromptText =
[...agentMessages].reverse().find((m) => m.role === 'user')?.content || ''
+ // G27b, parity with useCodex: a new turn on a conversation whose plan is
+ // still open carries the plan instead of hoping the model digs it back out
+ // of the history. This loop persists NO hidden tool chain at all, so
+ // without the anchor the next request contains no todo_write whatsoever.
+ // The plan state comes from the todoStore, which is what the PlanBar shows.
+ // Pushed LAST, behind the user's own message: volatile tail, stable head
+ // (plan A5). Deliberately after userPromptText above, so the media-intent
+ // regexes keep reading the user's words instead of this line.
+ {
+ const resume = planResumeAnchor(useTodoStore.getState().getTodos(convId))
+ if (resume) {
+ log.info('agent.plan_resume_anchor', { done: resume.gap.done, total: resume.gap.total })
+ agentMessages.push({ role: 'user', content: resume.text })
+ }
+ }
// Seed the media intent from the router's continuation hint too (David
// 2026-06-20): "ok generiere jetzt" carries no noun, so detecting wants from
// the last message alone left wantsVideo=false and the synth fallback never
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index a9f5a50e..3fde7faf 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -48,6 +48,7 @@ import { budgetFromSettings } from '../api/agents/budget'
import { finalStripThinkingTags, splitOrphanCloser, splitUnclosedThink } from '../lib/thinking-stripper'
import { openPlanGap, planReconcileSteer, PLAN_RECONCILE_BUDGET } from '../lib/plan-reconcile'
import { PlanStaleness, planStalenessSteer } from '../lib/plan-staleness'
+import { planResumeAnchor } from '../lib/plan-resume'
import { useTodoStore } from '../stores/todoStore'
import { httpStatusOf } from '../lib/http-status'
import { CREDITS_EXHAUSTED_MESSAGE } from '../lib/credits-exhausted'
@@ -694,6 +695,21 @@ export function useCodex() {
return msg
}),
]
+ // G27b: a new turn on a conversation whose plan is still open gets the
+ // plan handed to it instead of being asked to find it again (David,
+ // 2026-08-22: an interrupted run left the plan lying and "continue" was
+ // pure hope). Read from the todoStore, which holds the live plan, never
+ // from a history whose newest todo_write may have aged out or fallen off
+ // the 60-message persist cap below. Last in the array, so the stable head
+ // a prefix cache matches stays byte-identical (plan A5), and BEFORE
+ // messagesStartLen so the anchor is never persisted back into the chat.
+ if (convId) {
+ const resume = planResumeAnchor(useTodoStore.getState().getTodos(convId))
+ if (resume) {
+ void diagLog('plan-resume-anchor', { done: resume.gap.done, total: resume.gap.total })
+ messages.push({ role: 'user', content: resume.text })
+ }
+ }
const messagesStartLen = messages.length
// Setup
diff --git a/src/lib/__tests__/plan-resume.test.ts b/src/lib/__tests__/plan-resume.test.ts
new file mode 100644
index 00000000..f92ed15f
--- /dev/null
+++ b/src/lib/__tests__/plan-resume.test.ts
@@ -0,0 +1,191 @@
+/**
+ * Resuming a plan must be a mechanism, not hope.
+ *
+ * David, 2026-08-22 on the running build: an agent run with a plan was
+ * interrupted by a side question, the model answered in prose and left the
+ * plan lying. "continue" then asked the model to rediscover its own plan in a
+ * history it may no longer hold. G27b hands it over instead: one line at the
+ * end of the next request, built from the todoStore.
+ *
+ * Run: npx vitest run src/lib/__tests__/plan-resume.test.ts
+ */
+import { describe, it, expect, beforeEach } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { fileURLToPath } from 'node:url'
+import { dirname, resolve } from 'node:path'
+
+import { planResumeAnchor } from '../plan-resume'
+import { buildRequestMessages, trimWorkingHistory, type DecayMessage } from '../context-decay'
+import { useTodoStore } from '../../stores/todoStore'
+import type { TodoItem } from '../../stores/todoStore'
+
+const here = dirname(fileURLToPath(import.meta.url))
+const src = (...p: string[]) => readFileSync(resolve(here, '../..', ...p), 'utf8')
+
+const t = (content: string, status: TodoItem['status']): TodoItem => ({ content, status })
+
+/** David's shape: a long plan, a few steps in, interrupted mid-flight. */
+const INTERRUPTED: TodoItem[] = [
+ t('Read the existing router', 'completed'),
+ t('Sketch the new endpoint', 'completed'),
+ t('Write src/api/orders.ts', 'in_progress'),
+ t('Wire the route', 'pending'),
+ t('Add a test', 'pending'),
+]
+
+describe('the anchor fires exactly when a plan is open', () => {
+ it('an interrupted plan produces the line, with the numbers and the next step', () => {
+ const resume = planResumeAnchor(INTERRUPTED)
+ expect(resume).not.toBeNull()
+ expect(resume!.gap).toEqual({ done: 2, total: 5, next: 'Write src/api/orders.ts' })
+ expect(resume!.text).toContain('2 of 5')
+ expect(resume!.text).toContain('Write src/api/orders.ts')
+ })
+
+ it('the user keeps the right of way, and the plan can be rewritten', () => {
+ const { text } = planResumeAnchor(INTERRUPTED)!
+ expect(text).toMatch(/unless/i)
+ expect(text).toMatch(/comes first/i)
+ expect(text).toContain('todo_write')
+ })
+
+ it('it is one short line, not a second system prompt', () => {
+ expect(planResumeAnchor(INTERRUPTED)!.text.length).toBeLessThan(400)
+ })
+
+ it('NEGATIVE CONTROL: no plan at all, no anchor', () => {
+ expect(planResumeAnchor([])).toBeNull()
+ })
+
+ it('NEGATIVE CONTROL: a finished plan is finished, no anchor', () => {
+ expect(planResumeAnchor([t('a', 'completed'), t('b', 'completed')])).toBeNull()
+ })
+
+ it('NEGATIVE CONTROL: the last open step closing removes the anchor', () => {
+ const nearlyDone = INTERRUPTED.map((i) => t(i.content, 'completed'))
+ expect(planResumeAnchor(nearlyDone)).toBeNull()
+ })
+})
+
+describe('the state comes from the store, not from the history', () => {
+ beforeEach(() => {
+ useTodoStore.setState({ byConversation: {}, updatedAt: {} })
+ })
+
+ it('reads the live plan of THIS conversation', () => {
+ useTodoStore.getState().setTodos('conv-a', INTERRUPTED)
+ useTodoStore.getState().setTodos('conv-b', [t('other work', 'pending')])
+ const a = planResumeAnchor(useTodoStore.getState().getTodos('conv-a'))
+ const b = planResumeAnchor(useTodoStore.getState().getTodos('conv-b'))
+ expect(a!.text).toContain('2 of 5')
+ expect(b!.text).toContain('0 of 1')
+ })
+
+ it('a conversation without a plan gets nothing', () => {
+ expect(planResumeAnchor(useTodoStore.getState().getTodos('never-planned'))).toBeNull()
+ })
+
+ it('the store wins over a stale todo_write still sitting in the history', () => {
+ // The history says 1 of 5 because that todo_write is three steps old; the
+ // store says 4 of 5 because the model kept working. The anchor must carry
+ // the store's number, which is exactly what the PlanBar shows the user.
+ useTodoStore.getState().setTodos('conv-a', [
+ ...INTERRUPTED.slice(0, 4).map((i) => t(i.content, 'completed')),
+ t('Add a test', 'in_progress'),
+ ])
+ const stale = 'Plan updated (1/5 done, now: Sketch the new endpoint).'
+ const resume = planResumeAnchor(useTodoStore.getState().getTodos('conv-a'))
+ expect(resume!.text).toContain('4 of 5')
+ expect(resume!.text).not.toContain(stale)
+ })
+})
+
+describe('A5: the anchor rides in the volatile tail and survives the decay', () => {
+ /** What the hooks build: system head, history, the user's new message, anchor. */
+ const request = (): DecayMessage[] => {
+ const { text } = planResumeAnchor(INTERRUPTED)!
+ const history: DecayMessage[] = [
+ { role: 'system', content: 'STABLE HEAD' },
+ { role: 'user', content: 'build the orders endpoint' },
+ ]
+ for (let i = 0; i < 12; i++) {
+ history.push({ role: 'assistant', content: '', tool_calls: [{ id: `c${i}`, function: { name: 'file_read', arguments: { path: `f${i}.ts` } } }] })
+ history.push({ role: 'tool', content: `body ${i} ${'x'.repeat(4000)}`, tool_call_id: `c${i}` })
+ }
+ history.push({ role: 'user', content: 'continue' })
+ history.push({ role: 'user', content: text })
+ return history
+ }
+
+ it('the anchor is the LAST message, so the stable head keeps its bytes', () => {
+ const msgs = request()
+ expect(msgs[msgs.length - 1].content).toContain('2 of 5')
+ expect(msgs[0].content).toBe('STABLE HEAD')
+ })
+
+ it('a tight send window drops old results but never the anchor', () => {
+ const msgs = request()
+ const trimmed = trimWorkingHistory(msgs, 2000).messages
+ const built = buildRequestMessages(trimmed, { budgetTokens: 2000 })
+ const last = built.messages[built.messages.length - 1]
+ expect(String(last.content)).toContain('2 of 5')
+ expect(built.messages[0].content).toBe('STABLE HEAD')
+ expect(built.messages.length).toBeLessThan(msgs.length)
+ })
+})
+
+/**
+ * Line comments are stripped before any of the wiring checks below. Without
+ * that, commenting the anchor out leaves every assertion green (measured while
+ * running the negative control for this very file), which would make these
+ * tests a search for a sentence rather than for working code.
+ */
+const codeOnly = (s: string) =>
+ s.split('\n').filter((l) => !l.trim().startsWith('//')).join('\n')
+
+describe('both surfaces are wired, in the request copy only', () => {
+ const codex = codeOnly(src('hooks', 'useCodex.ts'))
+ const agent = codeOnly(src('hooks', 'useAgentChat.ts'))
+
+ it('useCodex appends it to the request array before the persist marker', () => {
+ expect(codex).toContain('planResumeAnchor(useTodoStore.getState().getTodos(convId))')
+ const pushAt = codex.indexOf("messages.push({ role: 'user', content: resume.text })")
+ const builtAt = codex.indexOf('let messages: ChatMessage[] = [')
+ const startLenAt = codex.indexOf('const messagesStartLen = messages.length')
+ expect(pushAt).toBeGreaterThan(builtAt)
+ // Before the marker the hidden-history persist slices from: the anchor is
+ // a request line, it must never be written back into the conversation.
+ expect(pushAt).toBeLessThan(startLenAt)
+ })
+
+ it('useAgentChat appends it to agentMessages, after the media intent is read', () => {
+ expect(agent).toContain('planResumeAnchor(useTodoStore.getState().getTodos(convId))')
+ const pushAt = agent.indexOf("agentMessages.push({ role: 'user', content: resume.text })")
+ const builtAt = agent.indexOf('let agentMessages: ChatMessage[] = [')
+ const promptTextAt = agent.indexOf('const userPromptText =')
+ const loopAt = agent.indexOf('while (runningRef.current && !abort.signal.aborted)')
+ expect(pushAt).toBeGreaterThan(builtAt)
+ expect(pushAt).toBeGreaterThan(promptTextAt)
+ expect(pushAt).toBeLessThan(loopAt)
+ })
+
+ it('neither surface writes the anchor into the visible chat', () => {
+ for (const hook of [codex, agent]) {
+ expect(hook).not.toMatch(/addMessage\([^)]*resume\.text/)
+ expect(hook).not.toMatch(/updateMessageContent\([^)]*resume\.text/)
+ }
+ })
+
+ it('it is pushed once per turn, outside the loop', () => {
+ expect(codex.match(/resume\.text/g)).toHaveLength(1)
+ expect(agent.match(/resume\.text/g)).toHaveLength(1)
+ })
+
+ it('NEGATIVE CONTROL: it does not touch the in-run steer budgets', () => {
+ const block = (s: string) => s.slice(s.indexOf('const resume = planResumeAnchor'), s.indexOf('const resume = planResumeAnchor') + 400)
+ for (const hook of [codex, agent]) {
+ expect(block(hook)).not.toContain('planReconcilesRemaining')
+ expect(block(hook)).not.toContain('planStaleness')
+ }
+ })
+})
diff --git a/src/lib/plan-resume.ts b/src/lib/plan-resume.ts
new file mode 100644
index 00000000..3d1ef69d
--- /dev/null
+++ b/src/lib/plan-resume.ts
@@ -0,0 +1,54 @@
+import type { TodoItem } from '../stores/todoStore'
+import { openPlanGap, type PlanGap } from './plan-reconcile'
+
+/**
+ * The line that makes "continue" deterministic.
+ *
+ * Measured by David on the running build (2026-08-22): an agent run with a
+ * plan was interrupted by a side question the model could not answer, it
+ * replied in prose and left the plan lying there. Sending "continue" then
+ * asked the model to FIND its own plan again somewhere in the history, and
+ * that is hope, not a mechanism. Two things make the search fail outright:
+ * the coding loop persists only the last 60 messages of its hidden tool chain
+ * (useCodex HIDDEN_HISTORY_MAX), so on a long run the newest todo_write is
+ * simply gone from the next turn, and the agent loop persists no hidden tool
+ * chain at all, so no todo_write ever reaches the following request.
+ *
+ * The plan itself never went anywhere: it lives in the todoStore, keyed by
+ * conversation and persisted across restarts, and the PlanBar renders it the
+ * whole time. So the state the model is asked to rediscover is state the app
+ * already holds. This turns it into one short line on the new turn.
+ *
+ * Two properties it must keep:
+ * - the numbers come from the STORE, never from the history that just aged
+ * out, which is the whole reason the anchor exists;
+ * - the user still outranks the plan. A new message that changes direction
+ * is an instruction, not a distraction, and the plan waits or gets
+ * rewritten with todo_write.
+ *
+ * Placement is plan A5: the anchor rides at the END of the request, behind the
+ * user's own message, never in the stable head and never in the visible
+ * history. It is pushed into the request copy exactly once per turn, and it
+ * touches neither the reconcile budget nor the staleness budget: those two
+ * argue with a model DURING a run, this one hands over state BETWEEN runs.
+ */
+
+export interface PlanResume {
+ gap: PlanGap
+ text: string
+}
+
+/** The anchor for a new turn, or null when there is no open plan to resume. */
+export function planResumeAnchor(todos: TodoItem[]): PlanResume | null {
+ const gap = openPlanGap(todos)
+ if (!gap) return null
+ return {
+ gap,
+ text:
+ `A plan from this conversation is still open: ${gap.done} of ${gap.total} steps completed, ` +
+ `next open step: "${gap.next}". Continue with it now, ` +
+ `unless the message above clearly sends you somewhere else. ` +
+ `If it does, that instruction comes first; take the plan up again afterwards, ` +
+ `or rewrite it with todo_write once you know it no longer fits.`,
+ }
+}