feat(claude): compact old threads before they burn through usage (#8144)

Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 authored and GitHub committed 2026-08-25 01:08:57 -07:00
1 parent 1baf99195a
commit c7222ca4df
25 files changed
+1105 -32

No files matched your search

@@ -0,0 +1,35 @@
import { describe, expect, it } from "vite-plus/test";
import {
formatClaudeResumeCompactionQuestion,
isClaudeResumeCompactionQuestion,
} from "./claudeCompaction.ts";
describe("claude resume compaction copy", () => {
// The matcher must recognize every question the formatter can produce.
// This is the drift guard: rewording one side fails here.
it.each([
{ ageMinutes: 145, estimatedTokens: 275_123 },
{ ageMinutes: 70, estimatedTokens: 100_000 },
{ ageMinutes: 59, estimatedTokens: 1_234_567 },
{ ageMinutes: 0, estimatedTokens: 0 },
])("matches its own formatted question (%o)", (input) => {
const question = formatClaudeResumeCompactionQuestion(input);
expect(isClaudeResumeCompactionQuestion(question)).toBe(true);
});
it("formats ages above and below one hour", () => {
expect(
formatClaudeResumeCompactionQuestion({ ageMinutes: 145, estimatedTokens: 275_123 }),
).toBe("This session is 2h 25m old and uses 275,123 tokens. Compact it before continuing?");
expect(formatClaudeResumeCompactionQuestion({ ageMinutes: 45, estimatedTokens: 1_000 })).toBe(
"This session is 45m old and uses 1,000 tokens. Compact it before continuing?",
);
});
it("does not match unrelated questions", () => {
expect(
isClaudeResumeCompactionQuestion("The build cache is large. Compact it before continuing?"),
).toBe(false);
});
});
+26
View File
@@ -0,0 +1,26 @@
/**
* Copy for Claude's resume compaction dialog, shared by the server adapter
* (which asks the question) and the web client (which recognizes the
* question and its "never" answer in resolved user-input activities to
* mirror the dismissal). Both sides must agree on these strings, so they
* live here: reword the question or the answer label in this file only.
*/
export const CLAUDE_RESUME_COMPACTION_NEVER_ANSWER = "Don't ask again";
export function formatClaudeResumeCompactionQuestion(input: {
readonly ageMinutes: number;
readonly estimatedTokens: number;
}): string {
const ageLabel =
input.ageMinutes >= 60
? `${Math.floor(input.ageMinutes / 60)}h ${input.ageMinutes % 60}m`
: `${input.ageMinutes}m`;
return `This session is ${ageLabel} old and uses ${input.estimatedTokens.toLocaleString("en-US")} tokens. Compact it before continuing?`;
}
const CLAUDE_RESUME_COMPACTION_QUESTION_PATTERN =
/^This session is (?:\d+h \d+m|\d+m) old and uses \d{1,3}(?:,\d{3})* tokens\. Compact it before continuing\?$/u;
export function isClaudeResumeCompactionQuestion(question: string): boolean {
return CLAUDE_RESUME_COMPACTION_QUESTION_PATTERN.test(question);
}
+31
View File
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
import { ProviderDriverKind, ProviderInstanceId, type ModelCapabilities } from "@t3tools/contracts";
import {
applyClaudePromptEffortPrefix,
buildProviderOptionSelectionsFromDescriptors,
createModelCapabilities,
createModelSelection,
@@ -155,3 +156,33 @@ describe("model slug normalization", () => {
expect(normalizeCustomModelSlug(" opus ")).toBe("opus");
});
});
describe("applyClaudePromptEffortPrefix", () => {
it("keeps slash commands intact when ultrathink is selected", () => {
expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact");
expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe(
"/compact keep recent errors",
);
expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe(
"/review src/model.ts",
);
expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe(
"/security-review",
);
expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe(
"/plugin:skill run",
);
expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe(
"/deploy.prod to staging",
);
});
it("still adds the ultrathink prefix to ordinary prompts", () => {
expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe(
"Ultrathink:\nInvestigate this failure",
);
expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe(
"Ultrathink:\n/home/theo/app.ts crashed on load",
);
});
});
+5 -1
View File
@@ -362,7 +362,11 @@ export function applyClaudePromptEffortPrefix(
if (!trimmed) {
return trimmed;
}
if (effort !== "ultrathink") {
// Prefixing a slash command turns it into plain prose, so Claude never
// runs it. Command names come from arbitrary file names ("/deploy.prod",
// "/plugin:skill"), so accept any first token without a second slash;
// absolute paths like "/home/theo/app.ts" keep the prefix.
if (effort !== "ultrathink" || /^\/[^\s/]+(?:\s|$)/u.test(trimmed)) {
return trimmed;
}
if (trimmed.startsWith("Ultrathink:")) {