A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
stop recaps from disappearing while a new turn regenerates them (#239)
kt309342579878
diff --git a/packages/coding-agent/src/modes/agent-connection/snapshot.ts b/packages/coding-agent/src/modes/agent-connection/snapshot.tsindex bd2daf4a18..79e7e247b0 100644--- a/packages/coding-agent/src/modes/agent-connection/snapshot.ts+++ b/packages/coding-agent/src/modes/agent-connection/snapshot.ts@@ -13,12 +13,10 @@ import type {AgentConnectionState,} from "./types.js";-function persistedRecap(- sessionManager: { getLatestAgentStatus?: () => { summary: string; basedOnMessageCount: number } | undefined },- messageCount: number,-): string | undefined {- const status = sessionManager.getLatestAgentStatus?.();- return status && status.basedOnMessageCount === messageCount ? status.summary : undefined;+function persistedRecap(sessionManager: {+ getLatestAgentStatus?: () => { summary: string } | undefined;+}): string | undefined {+ return sessionManager.getLatestAgentStatus?.()?.summary;}export function createAgentConnectionState(@@ -55,10 +53,8 @@ export function createAgentConnectionState(})),activeToolNames: session.getActiveToolNames(),contextUsage: session.getContextUsage(),- // Baseline recap; the daemon overlays the live summary on attach. Only use it- // while it matches the current turn so attach never seeds a stale recap.- // Optional-call so a minimal session manager (tests) still works.- recap: persistedRecap(sessionManager, session.messages.length),+ // Baseline recap; the daemon overlays the live summary on attach.+ recap: persistedRecap(sessionManager),};}diff --git a/packages/coding-agent/src/modes/daemon/daemon-mode.ts b/packages/coding-agent/src/modes/daemon/daemon-mode.tsindex 699afb4688..927935848f 100644--- a/packages/coding-agent/src/modes/daemon/daemon-mode.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-mode.ts@@ -65,12 +65,7 @@ import {isDaemonDialogExtensionUiRequest,success,} from "./daemon-protocol.js";-import {- buildRlmChildSnapshots,- buildSessionList,- isSummaryCurrent,- summaryForActiveSession,-} from "./daemon-session-list.js";+import { buildRlmChildSnapshots, buildSessionList, summaryForActiveSession } from "./daemon-session-list.js";import { DaemonSessionSummarizer } from "./daemon-session-summarizer.js";import {cleanupDaemonSocketPath,@@ -1467,9 +1462,8 @@ export class AgentDaemon {: undefined;const children = buildRlmChildSnapshots(state.activeSessionId, [...this.sessions.values()]);const connectionState = createAgentConnectionState(state.runtime, state.activeSessionId);- // Prefer the live in-memory recap over the persisted baseline, but only- // while it matches the current turn so we don't seed a stale recap.- if (state.summaryState?.summary && isSummaryCurrent(state)) {+ // Prefer the live in-memory recap over the persisted baseline.+ if (state.summaryState?.summary) {connectionState.recap = state.summaryState.summary;}return {diff --git a/packages/coding-agent/src/modes/daemon/daemon-session-list.ts b/packages/coding-agent/src/modes/daemon/daemon-session-list.tsindex f3cd83cb16..c8c36ff7e7 100644--- a/packages/coding-agent/src/modes/daemon/daemon-session-list.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-session-list.ts@@ -153,11 +153,11 @@ export function summaryForActiveSession(activeSession: ActiveSessionState, savedspawnCode: metadata.spawnCode ? metadata.spawnCode.slice(0, SPAWN_CODE_MAX_CHARS) : undefined,modelFallbackMessage: activeSession.runtime.modelFallbackMessage,diagnostics: [...activeSession.runtime.diagnostics],- // Drop both summary and verdict once new messages arrive; a stale recap- // would describe a prior turn. The summarizer refreshes within seconds.- ...(isSummaryCurrent(activeSession)- ? { summary: activeSession.summaryState?.summary, taskState: activeSession.summaryState?.taskState }- : {}),+ // Keep the last recap visible across turns so the view never blanks, but+ // gate the verdict on currency: a stale "completed" must not show on a turn+ // that is active again.+ summary: activeSession.summaryState?.summary,+ ...(isSummaryCurrent(activeSession) ? { taskState: activeSession.summaryState?.taskState } : {}),};}diff --git a/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.ts b/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.tsindex 683556f3c7..6e483e4412 100644--- a/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.ts@@ -20,8 +20,8 @@ const SUMMARY_MAX_TOKENS = 400;export const AGENT_STATUS_SYSTEM_PROMPT = `You generate a status line for an AI coding agent dashboard. You are given the recent conversation between a user and the agent, plus whether the agent is currently working or idle.-Output ONLY these two lines, nothing before or after. Do not think out loud, explain, or add any other text.-SUMMARY: a present-tense clause, at most 12 words, saying what the agent is doing or just did, no trailing period+Output ONLY these two lines, nothing before or after. Do not think out loud, explain, or count words. Put the summary inside <recap></recap> tags and write nothing after the closing tag.+SUMMARY: <recap>a present-tense clause, at most 12 words, saying what the agent is doing or just did, no trailing period</recap>STATUS: one of WORKING, NEEDS_INPUT, COMPLETEDSTATUS meaning:@@ -31,7 +31,7 @@ STATUS meaning:When the agent is idle and you are unsure between COMPLETED and NEEDS_INPUT, choose NEEDS_INPUT.Example:-SUMMARY: Refactoring the auth middleware and updating its tests+SUMMARY: <recap>Refactoring the auth middleware and updating its tests</recap>STATUS: WORKING`;export interface AgentStatusResult {@@ -100,27 +100,57 @@ export function buildStatusContext(messages: readonly AgentMessage[], isWorking:return `<agent-state>${state}</agent-state>\n<conversation>\n${lines.join("\n")}\n</conversation>`;}+// Cuts the word-counting chain-of-thought the model appends on the recap line,+// e.g. `Sending X. That's 5 words? Count: X(1)... = 6 words.`. Restricted to+// structural counting markers so plain words ("Waiting for CI") survive.+const REASONING_TRAILER = /\s*(?:["”]\s*)?(?:\bthat['’]?s\s+\d+\s*words?\b|\bcount\s*:|\(\d+\)|=\s*\d+\s*words?\b).*/i;+const COUNTING_ARTIFACT = /\(\d+\)|=\s*\d+\s*words?\b/i;+const MAX_RECAP_WORDS = 16;++/** Strip reasoning trailer and quotes, then reject anything still polluted. */+function cleanRecap(raw: string): string | undefined {+ const value = raw+ .trim()+ .replace(REASONING_TRAILER, "")+ .replace(/^["“']+|["”']+$/g, "")+ .replace(/[.\s]+$/, "")+ .trim();+ if (!value || value.startsWith("<") || /present-tense|12 words/i.test(value)) {+ return undefined;+ }+ if (COUNTING_ARTIFACT.test(value) || value.split(/\s+/).length > MAX_RECAP_WORDS) {+ return undefined;+ }+ return value;+}+/**- * Parse the two-line reply. Requires an explicit `SUMMARY:` line (last one wins,- * after any reasoning) and never falls back to free text, so a model's- * chain-of-thought can't leak into the recap. Idle verdicts default to- * needs_input on anything unrecognized.+ * Parse the two-line reply. The recap is delimited by `<recap></recap>` so+ * trailing chain-of-thought falls outside it; without the close tag we fall back+ * to the `SUMMARY:`/`RECAP:` line and cut inline word-counting. The last clean+ * recap wins. A still-polluted candidate is rejected; idle verdicts default to+ * needs_input.*/export function parseAgentStatusResponse(text: string, isWorking: boolean): AgentStatusResult | undefined {const reasoningTag = /<\/?(?:think|thinking|reasoning|redacted_thinking)>/gi;const cleaned = text.replace(/<(think|thinking|reasoning|redacted_thinking)>[\s\S]*?<\/\1>/gi, " ").replace(reasoningTag, " ");- let summary: string | undefined;++ // Prefer the tag (last match wins so a corrected draft resolves). Fall back to+ // the SUMMARY/RECAP lines only when the tag is missing or its body is rejected;+ // among lines the last clean one wins.+ const tagMatch = [...cleaned.matchAll(/<recap>([\s\S]*?)<\/recap>/gi)].at(-1);+ const tagSummary = tagMatch ? cleanRecap(tagMatch[1]!) : undefined;+ let summary = tagSummary;+let status: string | undefined;for (const rawLine of cleaned.split("\n")) {const line = rawLine.trim();- const summaryMatch = /^summary\s*:\s*(.+)$/i.exec(line);+ const summaryMatch = /^(?:summary|recap)\s*:\s*(.+)$/i.exec(line);if (summaryMatch) {- const candidate = summaryMatch[1]!.trim().replace(/[.\s]+$/, "");- // Skip an echoed prompt template (e.g. "<one present-tense clause…>").- if (candidate && !candidate.startsWith("<") && !/present-tense|12 words/i.test(candidate)) {- summary = candidate;+ if (!tagSummary) {+ summary = cleanRecap(summaryMatch[1]!.replace(/<\/?recap>/gi, "")) ?? summary;}continue;}diff --git a/packages/coding-agent/test/daemon-session-list.test.ts b/packages/coding-agent/test/daemon-session-list.test.tsindex 0d244117b3..aad7156527 100644--- a/packages/coding-agent/test/daemon-session-list.test.ts+++ b/packages/coding-agent/test/daemon-session-list.test.ts@@ -8,6 +8,7 @@ import {buildSessionList,resolveAttachModelFallbackMessage,type SessionSummary,+ summaryForActiveSession,} from "../src/modes/daemon/daemon-session-list.js";describe("buildSessionList", () => {@@ -95,6 +96,46 @@ describe("buildSessionList", () => {});});+describe("summaryForActiveSession recap currency", () => {+ const twoMessages = [+ { role: "user", content: "hi" },+ { role: "assistant", content: "ok" },+ ] as AgentMessage[];++ it("surfaces both recap and verdict while the summary matches the turn", () => {+ const summary = summaryForActiveSession(+ makeState({+ activeSessionId: "s1",+ messages: twoMessages,+ summaryState: { summary: "Editing the router", taskState: "completed", basedOnMessageCount: 2 },+ }),+ );+ expect(summary.summary).toBe("Editing the router");+ expect(summary.taskState).toBe("completed");+ });++ it("keeps showing the prior recap once a new turn outpaces the summary", () => {+ // New messages arrived (count 3) but the summary is still based on 2; the+ // recap text must survive so the agents view does not flicker to blank.+ const summary = summaryForActiveSession(+ makeState({+ activeSessionId: "s1",+ messages: [...twoMessages, { role: "user", content: "next" } as AgentMessage],+ summaryState: { summary: "Editing the router", taskState: "completed", basedOnMessageCount: 2 },+ }),+ );+ expect(summary.summary).toBe("Editing the router");+ // ...but a stale "completed" verdict must not show on a turn that is active again.+ expect(summary.taskState).toBeUndefined();+ });++ it("omits the recap entirely when there is no summary yet", () => {+ const summary = summaryForActiveSession(makeState({ activeSessionId: "s1", messages: twoMessages }));+ expect(summary.summary).toBeUndefined();+ expect(summary.taskState).toBeUndefined();+ });+});+describe("buildRlmChildSnapshots", () => {it("collects children and grandchildren with event-compatible parent ids", () => {const parent = makeState({ activeSessionId: "parent", sessionFile: "/tmp/parent.jsonl" });@@ -230,6 +271,7 @@ interface StateOptions {pendingToolCalls?: string[];clients?: number;messages?: AgentMessage[];+ summaryState?: ActiveSessionState["summaryState"];childRunStatuses?: Record<string, "queued" | "running" | "done" | "error" | "cancelled">;metadata?: {kind: "top-level" | "subagent";@@ -254,6 +296,7 @@ function makeState(options: StateOptions): ActiveSessionState {activeSessionId: options.activeSessionId,clients,lastEventSequence: 0,+ summaryState: options.summaryState,runtime: {metadata: options.metadata ?? { kind: "top-level", createdAt: 1 },diagnostics: [],diff --git a/packages/coding-agent/test/daemon-session-summarizer.test.ts b/packages/coding-agent/test/daemon-session-summarizer.test.tsindex 5d643628af..660fbd6cff 100644--- a/packages/coding-agent/test/daemon-session-summarizer.test.ts+++ b/packages/coding-agent/test/daemon-session-summarizer.test.ts@@ -79,6 +79,81 @@ describe("daemon session summarizer", () => {expect(parseAgentStatusResponse("", false)).toBeUndefined();expect(parseAgentStatusResponse("STATUS: COMPLETED", false)).toBeUndefined();});++ test("extracts the recap from <recap> tags", () => {+ const text = "SUMMARY: <recap>Sending SSH auth retry to tcg-autoresearch-rl</recap>\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({+ summary: "Sending SSH auth retry to tcg-autoresearch-rl",+ });+ });++ test("drops chain-of-thought after the closing recap tag", () => {+ const text =+ "SUMMARY: <recap>Sending SSH auth retry to tcg-autoresearch-rl</recap> That's 5 words? Count: Sending(1) SSH(2) auth(3) retry(4) to(5) tcg-autoresearch-rl(6) = 6 words. Under..\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({+ summary: "Sending SSH auth retry to tcg-autoresearch-rl",+ });+ });++ test("cuts inline reasoning when the model omits the closing tag", () => {+ const text =+ "SUMMARY: Sending SSH auth retry to tcg-autoresearch-rl. That's 5 words? Count: Sending(1) SSH(2) = 6 words. Under\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({+ summary: "Sending SSH auth retry to tcg-autoresearch-rl",+ });+ });++ test("salvages the clean prefix before counting artifacts begin", () => {+ const text = "SUMMARY: Counting words(1) two(2) three(3) = 3 words\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({ summary: "Counting words" });+ });++ test("rejects a candidate whose counting starts at the very first word", () => {+ // No clean prefix to salvage — the recap is nothing but the artifact.+ const text = "SUMMARY: (1) word(2) count(3) = 3 words\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toBeUndefined();+ });++ test("rejects a rambling candidate that blows past the word ceiling", () => {+ const text =+ "SUMMARY: this is a very long rambling sentence that just keeps going and going well past any reasonable recap length\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toBeUndefined();+ });++ test("strips wrapping quotes the model adds around the recap", () => {+ const text = 'SUMMARY: <recap>"Wiring the recap line"</recap>\nSTATUS: COMPLETED';+ expect(parseAgentStatusResponse(text, false)).toEqual({+ summary: "Wiring the recap line",+ taskState: "completed",+ });+ });++ test("accepts RECAP: as a synonym for SUMMARY:", () => {+ const text = "RECAP: Restarting the daemon\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({ summary: "Restarting the daemon" });+ });++ test("ignores an open recap tag with no close", () => {+ const text = "SUMMARY: <recap>Editing the parser\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({ summary: "Editing the parser" });+ });++ test("keeps recaps that start with words also used in reasoning", () => {+ for (const recap of ["Waiting for CI to finish", "Let me know once tests pass", "Under review by the team"]) {+ expect(parseAgentStatusResponse(`SUMMARY: ${recap}\nSTATUS: WORKING`, true)).toEqual({ summary: recap });+ }+ });++ test("takes the last SUMMARY line when a draft is corrected", () => {+ const text = "SUMMARY: Draft recap\nSUMMARY: Final corrected recap\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({ summary: "Final corrected recap" });+ });++ test("falls back to a SUMMARY line when the tagged body is rejected", () => {+ // The tag body is pure counting (rejected); a later valid line must win.+ const text = "<recap>(1) two(2) = 2 words</recap>\nSUMMARY: Editing the parser\nSTATUS: WORKING";+ expect(parseAgentStatusResponse(text, true)).toEqual({ summary: "Editing the parser" });+ });});describe("buildStatusContext", () => {