Skip to content

Commit 47c2da0

Browse files
authored
🤖 fix: improve compaction prompt for better model compliance (#912)
## Summary Improves the compaction prompt to help models better respect the word count target and produce more useful summaries. ## Changes The new prompt provides structured guidance: **What to include:** - User's overall goal and current task - Key decisions and rationale - Current state of work (what's done, what's in progress) - Important technical details (file paths, function names, configurations) - Errors encountered and resolutions - Unresolved issues/blockers **What NOT to include:** - Suggestions for next steps - Conversational filler or pleasantries - Redundant information **Style:** Factual, dense writing where every sentence conveys essential context. ## Implementation - Extracted `buildCompactionPrompt()` into `src/common/constants/ui.ts` as a shared function - Updated desktop, mobile, and mock scenarios to use the shared function - Removed tautological tests _Generated with `mux`_
1 parent 367fa4a commit 47c2da0

File tree

6 files changed

+42
-23
lines changed

6 files changed

+42
-23
lines changed

mobile/src/utils/slashCommandHelpers.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe("buildMobileCompactionPayload", () => {
5454
const payload = buildMobileCompactionPayload(parsed, baseOptions);
5555

5656
expect(payload.messageText).toContain("approximately 615 words");
57+
5758
expect(payload.messageText).toContain(parsed.continueMessage);
5859
expect(payload.metadata.type).toBe("compaction-request");
5960
expect(payload.metadata.rawCommand).toContain("/compact -t 800 -m anthropic:claude-opus-4-1");

mobile/src/utils/slashCommandHelpers.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import type { MuxFrontendMetadata } from "@/common/types/message";
22
import type { ParsedCommand, SlashSuggestion } from "@/browser/utils/slashCommands/types";
33
import type { InferClientInputs } from "@orpc/client";
44
import type { ORPCClient } from "../orpc/client";
5+
import {
6+
DEFAULT_COMPACTION_WORD_TARGET,
7+
WORDS_TO_TOKENS_RATIO,
8+
buildCompactionPrompt,
9+
} from "@/common/constants/ui";
510

611
type SendMessageOptions = NonNullable<
712
InferClientInputs<ORPCClient>["workspace"]["sendMessage"]["options"]
813
>;
914

1015
export const MOBILE_HIDDEN_COMMANDS = new Set(["telemetry", "vim"]);
11-
const WORDS_PER_TOKEN = 1.3;
12-
const DEFAULT_WORD_TARGET = 2000;
1316

1417
export function extractRootCommand(replacement: string): string | null {
1518
if (typeof replacement !== "string") {
@@ -44,12 +47,10 @@ export function buildMobileCompactionPayload(
4447
baseOptions: SendMessageOptions
4548
): MobileCompactionPayload {
4649
const targetWords = parsed.maxOutputTokens
47-
? Math.round(parsed.maxOutputTokens / WORDS_PER_TOKEN)
48-
: DEFAULT_WORD_TARGET;
50+
? Math.round(parsed.maxOutputTokens / WORDS_TO_TOKENS_RATIO)
51+
: DEFAULT_COMPACTION_WORD_TARGET;
4952

50-
let messageText =
51-
`Summarize this conversation into a compact form for a new Assistant to continue helping the user. ` +
52-
`Use approximately ${targetWords} words.`;
53+
let messageText = buildCompactionPrompt(targetWords);
5354

5455
if (parsed.continueMessage) {
5556
messageText += `\n\nThe user wants to continue with: ${parsed.continueMessage}`;

src/browser/utils/chatCommands.test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,6 @@ describe("prepareCompactionMessage", () => {
128128
expect(metadata.parsed.continueMessage?.model).toBe(sendMessageOptions.model);
129129
});
130130

131-
test("generates correct prompt text with strict summary instructions", () => {
132-
const sendMessageOptions = createBaseOptions();
133-
const { messageText } = prepareCompactionMessage({
134-
workspaceId: "ws-1",
135-
maxOutputTokens: 4096,
136-
sendMessageOptions,
137-
});
138-
139-
expect(messageText).toContain("Focus entirely on the summary");
140-
expect(messageText).toContain("Do not suggest next steps or future actions");
141-
});
142-
143131
test("does not create continueMessage when no text or images provided", () => {
144132
const sendMessageOptions = createBaseOptions();
145133
const { metadata } = prepareCompactionMessage({

src/browser/utils/chatCommands.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import { resolveCompactionModel } from "@/browser/utils/messages/compactionModel
2525
import type { ImageAttachment } from "../components/ImageAttachments";
2626
import { dispatchWorkspaceSwitch } from "./workspaceEvents";
2727
import { getRuntimeKey, copyWorkspaceStorage } from "@/common/constants/storage";
28-
import { DEFAULT_COMPACTION_WORD_TARGET, WORDS_TO_TOKENS_RATIO } from "@/common/constants/ui";
28+
import {
29+
DEFAULT_COMPACTION_WORD_TARGET,
30+
WORDS_TO_TOKENS_RATIO,
31+
buildCompactionPrompt,
32+
} from "@/common/constants/ui";
2933

3034
// ============================================================================
3135
// Workspace Creation
@@ -593,7 +597,7 @@ export function prepareCompactionMessage(options: CompactionOptions): {
593597
: DEFAULT_COMPACTION_WORD_TARGET;
594598

595599
// Build compaction message with optional continue context
596-
let messageText = `Summarize this conversation into a compact form for a new Assistant to continue helping the user. Focus entirely on the summary of what has happened. Do not suggest next steps or future actions. Use approximately ${targetWords} words.`;
600+
let messageText = buildCompactionPrompt(targetWords);
597601

598602
if (options.continueMessage) {
599603
messageText += `\n\nThe user wants to continue with: ${options.continueMessage.text}`;

src/common/constants/ui.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@ export const DEFAULT_COMPACTION_WORD_TARGET = 2000;
4040
*/
4141
export const WORDS_TO_TOKENS_RATIO = 1.3;
4242

43+
/**
44+
* Build the compaction prompt for a given word target.
45+
* Shared across desktop and mobile clients.
46+
*/
47+
export function buildCompactionPrompt(targetWords: number): string {
48+
return `Summarize this conversation for a new Assistant to continue helping the user.
49+
50+
Your summary must be approximately ${targetWords} words.
51+
52+
Include:
53+
- The user's overall goal and current task
54+
- Key decisions made and their rationale
55+
- Current state of the work (what's done, what's in progress)
56+
- Important technical details (file paths, function names, configurations)
57+
- Any errors encountered and how they were resolved
58+
- Unresolved issues or blockers
59+
60+
Do not include:
61+
- Suggestions for next steps
62+
- Conversational filler or pleasantries
63+
- Redundant information
64+
65+
Write in a factual, dense style. Every sentence should convey essential context.`;
66+
}
67+
4368
/**
4469
* Force-compact this many percentage points after threshold.
4570
* Gives user a buffer zone between warning and force-compaction.

src/node/services/mock/scenarios/slashCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { ScenarioTurn } from "@/node/services/mock/scenarioTypes";
22
import { KNOWN_MODELS } from "@/common/constants/knownModels";
33
import { STREAM_BASE_DELAY } from "@/node/services/mock/scenarioTypes";
4+
import { buildCompactionPrompt } from "@/common/constants/ui";
45

56
export const SLASH_COMMAND_PROMPTS = {
67
MODEL_STATUS: "Please confirm which model is currently active for this conversation.",
78
} as const;
89

9-
export const COMPACTION_MESSAGE =
10-
"Summarize this conversation into a compact form for a new Assistant to continue helping the user. Focus entirely on the summary of what has happened. Do not suggest next steps or future actions. Use approximately 385 words.";
10+
export const COMPACTION_MESSAGE = buildCompactionPrompt(385);
1111

1212
export const COMPACT_SUMMARY_TEXT =
1313
"Compact summary: The assistant read project files, listed directory contents, created and inspected test.txt, then confirmed the contents remained 'hello'. Technical details preserved.";

0 commit comments

Comments
 (0)