SLOPSHOPPER

smartcompact

A Claude Code mod that compacts the conversation at a good moment, asked for by the session after a nudge or chosen by a small judge model, and then tells…

commandstatuspromptmodeltimer
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · smartcompact
› fix the failing auth test and add an audit log call ⏺ Read(src/auth.ts) ⎿ Read 6 lines ⏺ Update(src/auth.ts) ⎿ Added 2 lines, removed 1 line ⏺ Bash(bun test) ⎿ 3 pass, 1 fail ● Done. refresh now rejects expired claims and logs an audit event. ✻ Worked for 42s · done 4:20 PM › /smartcompact-prompt ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts ⚠ smartcompact: judge in 60k
README

smartcompact

A Claude Code mod that compacts the conversation at a good moment and then tells Claude to keep working. The repo README covers install and requirements.

claude plugin marketplace add ambervdberg/smartcompact
claude plugin install smartcompact@smartcompact

What it does

During a turn above the token floor, the plugin adds a hidden note after a tool result of the main conversation. The note asks Claude to compact at the next good moment: start no new subagents, let the running ones finish, then end the answer with the compact tag. See The nudge.

After each turn of the main conversation:

  1. When the answer ends with the compact tag, the session asked for a compaction itself. The judge is not asked. See When the session asks.
  2. It checks the context size. Below the token floor it does nothing.
  3. It waits while subagents or teammates still run.
  4. It asks a small judge model whether this is a good moment to compact. A good moment is a finished piece of work, for example when Claude offers to push or to start the next task. The turn never waits for the judge.
  5. On a yes it checks that no new turn and no new subagent started meanwhile. Then it waits until the prompt box is empty and no dialog is open, and runs a compaction with the instructions Keep the current goal, the next step and open decisions.
  6. After its own compaction it sends the continue prompt, so Claude goes on with the work. It never sends one after a compaction it did not start, such as /compact typed by hand or Claude Code's own auto-compact.

If you start a new turn before the compaction runs, the plugin drops it. The next finished turn is judged again. A dialog that stays open for two minutes also drops it.

Token floor

The floor (minTokens, 60k by default) counts only the tokens added since a starting point, not the whole context:

  • The starting point is the first turn of the session, the first turn after a /clear, or the first turn after a compaction.
  • That first turn itself is never judged.
  • The count starts from the context size when that first turn began: the input of its first model request, so the context plus the prompt. A first turn with only one request counts from its size at the end.
  • So a new session does not count its startup context, and a compaction cannot follow right after another one.
  • Claude Code has no token count before the first answer after a new session or a compaction. A turn that ends without a count is not judged, sends no nudge, follows no tag and is not taken as the first turn. The log gets a usage-missing line.
  • A resumed or respawned session keeps its saved count. A resumed session without a saved count starts over as a new session: its first turn is not judged and sets the starting point.

A line under the prompt box shows what it is doing, for example smartcompact: judge in 48k. It sits next to Claude Code's own notices. A custom statusLine script does not show it.

The status line during one compaction: judge in 15k, nudged, compacting, continued

StatusMeaning
judge in 48kBelow the token floor. The judge is asked once the conversation has grown 48k more tokens. A new session and a /clear show the full floor. So does the first turn after them or after a compaction, because that turn sets the starting point.
judge after next turnThe floor is reached. The judge is asked when the next turn ends.
waiting for subagentsSubagents still run, so the judge was not asked.
nudged 14:02The session was asked to compact at the next good moment.
judging...The judge model is asked.
keep going 14:02The judge said this is no good moment to compact.
will compact 14:02The judge said yes or the session asked. The compaction follows when the session is free.
yes cancelled 14:02The judge said yes, but a new turn or subagent started meanwhile.
request ignored 14:02The session asked, but subagents still ran, the loop guard held or the turn had no token count yet. The log says why.
waiting for empty promptA compaction waits until you empty the prompt box.
waiting for idleA compaction waits for a dialog to close or for the session to be free.
compacting..., compacted 14:03The compaction runs or has finished.
compact skipped 14:03Claude Code skipped the compaction, for example because a hook blocked it.
continued 14:03The continue prompt was sent.
dropped 14:03A new turn started, the session stayed busy, you cancelled the compaction or this mode cannot compact (claude -p), so the compaction was dropped.
error 14:02The judge or a session's request failed. The log says why. A judge reply that is no verdict counts as no and keeps the countdown. The log has a judge-unreadable line.

When the session asks

The plugin adds a short rule to the context of each conversation, as a block named smartcompact. Claude Code renders it with the first message and again after each compaction or /clear. It tells Claude to end its answer with <smartcompact>the prompt for the next piece</smartcompact> when a piece of work is done, the next piece of the same task can start right away and no longer needs the details so far, and then to stop. It leaves the tag out when it waits for other sessions or agents, or when all work is done. It also leaves it out when it waits for the user, unless smartcompact asked it to compact. It never writes an empty tag. Subagents get the block too, so it ends with Only the main session does this, never a subagent.

Only a tag at the very end of a main answer counts. Whitespace may follow it. A tag earlier in the text and a tag from a subagent do nothing. An empty or blank tag is ignored too, and the turn goes to the judge. When the main answer ends with the tag, the judge is not asked and the first match below runs:

  1. Subagents still run: the request is ignored and logged. A finishing subagent wakes the session, so it never hangs.
  2. Loop guard: the request is ignored and logged as loop guard when this turn came right after a continue prompt the plugin sent without a compaction. Without it a session that tags again at once loops. The guard holds in two cases:
  3. The previous continue prompt followed a compaction Claude Code skipped. This holds at any token count.
  4. The previous continue prompt was sent below the floor, and this turn is below the floor too.
  5. Below the token floor: nothing is compacted and the continue prompt is sent at once. The first turn of a new session and the first turn after a compaction always count as below the floor.
  6. Else the compaction waits for an empty prompt box and a closed dialog, as after a judge yes. The instructions are Keep the current goal, open decisions and what this next step needs: <text>. When Claude Code skips the compaction, the continue prompt is still sent.

The text in the tag fills {next} in the continue prompt.

The nudge

Above the token floor, a tool result of the main conversation can be followed by a hidden note. The note reads:

smartcompact: this conversation grew 64k tokens since smartcompact started counting. Plan a compaction at the next good moment. Start no new subagents and let the running ones finish. Then end your answer with <smartcompact>the prompt to start the next piece with</smartcompact>. Do this also when you would wait for the user, and put your question in the tag. Leave the tag out when the user must confirm an action that is hard to undo, such as a push, a delete or a publish.

After a nudge, a question for you goes into the tag. After the compaction the shipped continue prompt asks Claude to ask it again. To let Claude decide and go on without you, put a line like this in your own continue prompt:

- If you were waiting on a choice from me, make the best decision and go on.

Your own continue prompt can also ask Claude to write that choice to the choices file. A yes or no on an action that is hard to undo stays a normal stop for you to answer.

The number is the tokens added since the floor started counting, in thousands. The first note comes with the first tool result above the floor. The next one comes each time the conversation grows another half floor, at least 10k. The count starts over after a compaction, a /clear or a new session. There is no note for subagents and none while the floor waits for its first turn. The status line shows nudged 14:02 and the log has a nudge-sent line. A refused note is logged as nudge-error.

Options

Set them with /plugin configure smartcompact@smartcompact in a session, or in settings.json:

{ "pluginConfigs": { "smartcompact@smartcompact": { "options": { "minTokens": 80000 } } } }

A plugin loaded with --plugin-dir uses the key smartcompact instead of smartcompact@smartcompact.

OptionDefaultWhat it does
minTokens60000The token floor: tokens added before the judge is asked or a session's request compacts.
claudeModelhaikuModel alias or id for the judge. It runs on your own Claude login and needs no setup.

Continue prompt

The plugin sends a continue prompt after its own compaction and after a tag below the token floor. It reads the text from a file each time it sends it, so an edit works at once:

  • ~/.claude/smartcompact/continue-prompt.md when it exists. This is your own copy. An empty file turns the continue prompt off.
  • Else continue-prompt.md in the plugin folder, the shipped default.

Run /smartcompact-prompt to copy the default to your own file. It prints the path and says when it just made the file. A file that exists stays as it is.

When nothing was compacted, a first line Context was compacted automatically. is left out. Any other first line stays.

The plugin fills these placeholders when it sends the prompt, each time they occur.

PlaceholderReplaced with
{next}The text in the session's tag. After a judge yes: Continue where you left off. If the next step is clear, do it. Keep it in your own file, else the session's prompt for the next piece is lost.
{choicesFile}The absolute path of the choices file, in the platform's own spelling.
{choicesHeading}One line `## <ISO time> \<cwd> \<session id>`, with the time of sending.

Choices file

Your own continue prompt can ask Claude to write down the choices it made for you in one central file, ~/.claude/smartcompact/choices.md. It sits in the same folder as the log, so SMARTCOMPACT_LOG moves it too. For example:

Append your choices to {choicesFile}. Create the file if it is missing and never overwrite earlier text. Start with this line:
{choicesHeading}
Then write one bullet per choice you made for me, with what you chose and why.

A filled heading looks like ## 2026-10-04T10:00:00.000Z | C:/work | session-1. Each continue adds one block that starts with this line, followed by one bullet per choice. A dashboard can split the file on lines that start with ## .

Log

Each decision is written as one JSON line to ~/.claude/smartcompact/log.jsonl (SMARTCOMPACT_LOG picks another file). It stays on your machine and keeps only its newest 2 MB. The log keeps no text from the conversation and no text the judge wrote. A line holds the event, the time and details such as token counts, the length of the judge's reason (reasonChars) or an error message. The rows of a compaction carry source: judge or request (the session's tag). A compact-started row shows that the compact call began.

What it reads, sends and writes

The mod keeps no text from your conversation and sends nothing to the author or to any server of its own. The only call that leaves the session is the judge, on your own Claude login. It needs no account, key or setup.

Sends to the judge. After a turn above the token floor, the judge model gets:

  • the last prompt of the person, its first 2000 characters
  • the end of the last answer, its last 4000 characters
  • a one-line summary of the last 40 tool calls since that prompt, 150 characters each

The judge runs through the engine's own model call on your Claude login (claudeModel, Haiku by default). Nothing else leaves the session.

Submits as a prompt. Only the continue prompt, after its own compaction or after a tag below the token floor. It is the text of your own continue-prompt.md or the shipped default, with the placeholders filled: the text of the session's tag, the path of the choices file and a heading line with the time, folder and session id.

Changes in the session.

  • prompt.context adds one block named smartcompact with the rule from When the session asks. It changes no other block.
  • session.append adds the nudge as a hidden row to the main conversation. A subagent gets none.
  • command.run handles only its own command, /smartcompact-prompt.
  • It runs /compact with the instructions shown in What it does.

Writes two files, both in its own folder:

  • ~/.claude/smartcompact/log.jsonl, or the file SMARTCOMPACT_LOG names. One line per decision, with the token count and for a judge call the length of the judge's reason. It keeps no text from the conversation and no text the judge wrote. See Log.
  • ~/.claude/smartcompact/continue-prompt.md, only when you run /smartcompact-prompt and the file does not exist. It is a copy of the shipped default, for you to edit.

It edits no settings, instructions, hooks, build or start-up file. It reads only its own files, the session's messages and token count, the prompt box and the list of running subagents.

Development

claude plugin validate .
claude plugin test .
npx -p typescript tsc -p .

Claude Code writes the API types to .claude-plugin/types/ when it loads the plugin from a folder. tsc needs them there. The tests cannot read the disk, so tests/fake-engine.ts holds a copy of continue-prompt.md. Keep the two the same.

Source 30 files
hooks/register.ts 201 lines
1import type { EngineInterface, Register, TurnCompleteInput } from 'claude-code';
2import { readCompactRequest } from './compact-request.ts';
3import { CompactNudge } from './compact-nudge.ts';
4import { withCompactRule } from './compact-rule.ts';
5import { ContextFloor } from './context-floor.ts';
6import { contextTokens } from './context-tokens.ts';
7import type { Engine } from './engine.ts';
8import { messageOf } from './error-message.ts';
9import { logEvent } from './event-log.ts';
10import { startFloor } from './floor-start.ts';
11import { saveFloorState } from './floor-store.ts';
12import { PendingCompaction } from './pending-compaction.ts';
13import { settingsFrom } from './plugin-settings.ts';
14import { PROMPT_COMMAND, createOverrideIfMissing } from './prompt-command.ts';
15import { RequestFollowing } from './request-following.ts';
16import { showJudgeCountdown } from './status-line.ts';
17import { TurnCounter } from './turn-counter.ts';
18import { TurnJudging } from './turn-judging.ts';
19
20/** Wires the engine events to the judging, the nudge, the pending compaction and the turn count. */
21export const register: Register = (on, options) => {
22  const settings = settingsFrom(options);
23  const turns = new TurnCounter();
24  const floor = new ContextFloor();
25  const compaction = new PendingCompaction(turns, floor);
26  const judging = new TurnJudging(settings, turns, floor, compaction);
27  const requests = new RequestFollowing(settings, turns, floor, compaction);
28  const nudge = new CompactNudge(settings, floor);
29
30  // Without the countdown the line stays empty until the first turn ends.
31  on('session.start', async ($, e, next) => {
32    const engine = engineOf($);
33
34    await startFloor(engine, floor);
35    nudge.startCountOver();
36
37    const tokens = await contextTokens(engine);
38    // A new session has no count before its first response, and nothing is added yet.
39    const added = tokens === undefined ? 0 : floor.addedTokens(tokens);
40
41    showJudgeCountdown(engine, settings.minTokens - added);
42    await registerPromptCommand(engine);
43
44    return next(e);
45  });
46
47  // Every main loop turn counts: typed, queued, from a task notification or from this plugin.
48  on('turn.start', ($, e, next) => {
49    turns.noteTurnStarted();
50    void compaction.drop(engineOf($), 'new turn started');
51
52    return next(e);
53  });
54
55  // The count before a main step after the first is the first request's input: the context at turn start plus the
56  // prompt. The stream passes on unchanged.
57  on('turn.step', async function* ($, e, next) {
58    if (e.agentId === undefined && e.index >= 1 && floor.state().waitingFor !== null) {
59      const tokens = await contextTokens(engineOf($));
60
61      if (tokens !== undefined) {
62        floor.noteTurnStartReading(tokens);
63      }
64    }
65
66    return yield* next(e);
67  });
68
69  // The judge or the tag's request runs after the turn has settled, so the turn never waits on it.
70  on('turn.complete', async ($, e, next) => {
71    const turnsAtEnd = turns.count();
72    const completed = await next(e);
73
74    if (isMainLoopAnswer(e)) {
75      const turn = { answer: e.answer, turnsAtEnd };
76      const request = readCompactRequest(e.answer);
77
78      void (request === null
79        ? judging.judgeTurn(engineOf($), turn)
80        : requests.followRequest(engineOf($), turn, request));
81    }
82
83    return completed;
84  });
85
86  // Awaited, so the nudge row is stored before the engine sends its next request. The nudge itself comes in by
87  // door `note` and never fires this hook again.
88  on('session.append', { door: 'tool-result' }, async ($, e, next) => {
89    const stored = await next(e);
90
91    if (e.agentId === undefined) {
92      await nudge.nudgeIfDue(engineOf($));
93    }
94
95    return stored;
96  });
97
98  on('prompt.edit', async ($, e, next) => {
99    const edited = await next(e);
100
101    compaction.retryAfterEdit(engineOf($), edited.text);
102
103    return edited;
104  });
105
106  // The engine renders the blocks again after a compaction or /clear, so the session always has the rule.
107  on('prompt.context', async (_$, e, next) => {
108    const context = await next(e);
109
110    return { ...context, blocks: withCompactRule(context.blocks) };
111  });
112
113  on('command.run', { command: PROMPT_COMMAND.name }, async ($) => ({
114    text: await createOverrideIfMissing(engineOf($)),
115  }));
116
117  // A /compact typed by hand or Claude's own auto-compact already did the work.
118  on('session.compact', async ($, e, next) => {
119    if (e.agentId === undefined && (e.trigger === 'manual' || e.trigger === 'auto')) {
120      const engine = engineOf($);
121      // A missing count leaves the tokens field out of the row.
122      logEvent(engine, 'compact-other', { trigger: e.trigger, tokens: await contextTokens(engine) });
123      await compaction.drop(engine, `${e.trigger} compact`);
124    }
125
126    const compacted = await next(e);
127
128    if (e.agentId === undefined) {
129      floor.restartAfterCompaction();
130      await saveFloorState(engineOf($), floor);
131    }
132
133    return compacted;
134  });
135
136  // A /clear fires no session.start, so the floor and its countdown start over here.
137  on('session.end', async ($, e, next) => {
138    await compaction.drop(engineOf($), `session ${e.reason}`);
139
140    if (e.reason === 'clear') {
141      floor.startNewSession();
142      nudge.startCountOver();
143      showJudgeCountdown(engineOf($), settings.minTokens);
144    }
145
146    return next(e);
147  });
148};
149
150// A registered command lasts one session, so each start registers it again. A refusal must not stop the start.
151async function registerPromptCommand(engine: Engine): Promise<void> {
152  try {
153    await engine.registerCommand(PROMPT_COMMAND);
154  } catch (error) {
155    logEvent(engine, 'command-error', { message: messageOf(error) });
156  }
157}
158
159// A subagent's turn, an interrupt or an API error is no moment to judge or to follow a tag.
160function isMainLoopAnswer(e: TurnCompleteInput): boolean {
161  return e.agentId === undefined && e.reason === 'answer';
162}
163
164/** Binds every engine call the plugin makes. Each one is spelled `$.noun.event(...)`, as the engine requires. */
165function engineOf($: EngineInterface): Engine {
166  return {
167    pluginRoot: () => $.plugin.root,
168    now: () => $.clock.now(),
169    after: (ms, fn) => $.clock.after(ms, fn),
170    // The engine puts the plugin name in front by itself.
171    status: (text) => $.ui.status(text),
172    sessionId: () => $.session.id(),
173    cwd: () => $.session.cwd(),
174    usage: () => $.session.usage(),
175    messages: () => $.session.messages(),
176    agents: () => $.agent.list(),
177    readPrompt: () => $.prompt.read(),
178    fillPrompt: (input) => $.prompt.fill(input),
179    submitPrompt: (input) => $.prompt.submit(input),
180    appendNote: (text) => $.session.append({ message: { type: 'user', content: [{ type: 'text', text }] } }),
181    compact: (args) => $.session.compact(args),
182    registerCommand: (command) => $.command.register(command),
183    complete: (request) => $.model.complete(request),
184    fs: {
185      exists: (path) => $.fs.exists(path),
186      read: (path) => $.fs.read(path),
187      write: (path, text) => $.fs.write(path, text),
188    },
189    store: {
190      get: (key) => $.store.get(key),
191      set: (key, value) => $.store.set(key, value),
192      delete: (key) => $.store.delete(key),
193    },
194    env: {
195      logFile: () => $.env.get('SMARTCOMPACT_LOG'),
196      userProfile: () => $.env.get('USERPROFILE'),
197      home: () => $.env.get('HOME'),
198    },
199  };
200}
201
hooks/compact-request.ts 37 lines
1const OPEN_TAG = '<smartcompact>';
2const CLOSE_TAG = '</smartcompact>';
3
4/** A session's own request to compact: the prompt it wants for the next piece of work. */
5export type CompactRequest = {
6  next: string;
7};
8
9/**
10 * Reads the compact tag that ends an answer. Whitespace may follow it.
11 * With two tags only the last one counts, and only when it ends the answer.
12 */
13export function readCompactRequest(answer: string): CompactRequest | null {
14  const text = answer.trimEnd();
15
16  // A tag earlier in the text, such as one the session quotes, asks nothing.
17  if (!text.endsWith(CLOSE_TAG)) {
18    return null;
19  }
20
21  const body = text.slice(0, -CLOSE_TAG.length);
22  const start = body.lastIndexOf(OPEN_TAG);
23
24  if (start === -1) {
25    return null;
26  }
27
28  const next = body.slice(start + OPEN_TAG.length).trim();
29
30  // A tag without a prompt has nothing to continue with, so the turn is judged like an untagged one.
31  if (next === '') {
32    return null;
33  }
34
35  return { next };
36}
37
hooks/compact-nudge.ts 126 lines
1import type { ContextFloor } from './context-floor.ts';
2import { contextTokens } from './context-tokens.ts';
3import type { Engine } from './engine.ts';
4import { messageOf } from './error-message.ts';
5import { logEvent } from './event-log.ts';
6import type { Settings } from './plugin-settings.ts';
7import { showTimedStatus } from './status-line.ts';
8
9// A floor of 0 for a live test would else nudge after every tool call.
10const MIN_STEP_TOKENS = 10_000;
11
12/** How every nudge row starts. The judge input skips rows that start this way. */
13export const NUDGE_TEXT_START = 'smartcompact: ';
14
15/** The row that asks the session to pick a moment to compact, with the added tokens in thousands. */
16export function nudgeText(addedTokens: number): string {
17  const added = `${Math.floor(addedTokens / 1000)}k`;
18
19  return [
20    `${NUDGE_TEXT_START}this conversation grew ${added} tokens since smartcompact started counting.`,
21    'Plan a compaction at the next good moment.',
22    'Start no new subagents and let the running ones finish.',
23    'Then end your answer with <smartcompact>the prompt to start the next piece with</smartcompact>.',
24    'Do this also when you would wait for the user, and put your question in the tag.',
25    'Leave the tag out when the user must confirm an action that is hard to undo,',
26    'such as a push, a delete or a publish.',
27  ].join(' ');
28}
29
30/**
31 * Asks the main session to compact once the floor is passed, and again after each further step.
32 * The session picks the moment itself, so a session that always runs subagents still compacts.
33 */
34export class CompactNudge {
35  #settings: Settings;
36  #floor: ContextFloor;
37  /** The added tokens at the last nudge since the count started over, undefined before the first. */
38  #lastNudgeAt: number | undefined;
39  /** The floor baseline at the last nudge. A different baseline means the floor restarted. */
40  #baselineAtNudge: number | undefined;
41  #nudges = 0;
42  #isChecking = false;
43
44  constructor(settings: Settings, floor: ContextFloor) {
45    this.#settings = settings;
46    this.#floor = floor;
47  }
48
49  /** Runs after a main tool result is stored. It never throws: a failure is logged as `nudge-error`. */
50  async nudgeIfDue(engine: Engine): Promise<void> {
51    // Parallel tool calls store several results at once. One check is enough for all of them.
52    if (this.#isChecking) {
53      return;
54    }
55
56    this.#isChecking = true;
57
58    try {
59      await this.#nudgeIfDue(engine);
60    } catch (error) {
61      logEvent(engine, 'nudge-error', { message: messageOf(error) });
62    } finally {
63      this.#isChecking = false;
64    }
65  }
66
67  async #nudgeIfDue(engine: Engine): Promise<void> {
68    const tokens = await contextTokens(engine);
69
70    if (tokens === undefined) {
71      logEvent(engine, 'usage-missing', { hook: 'session.append' });
72
73      return;
74    }
75
76    const added = this.#floor.addedTokens(tokens);
77
78    if (!this.#isDue(added)) {
79      return;
80    }
81
82    const appended = await engine.appendNote(nudgeText(added));
83
84    if (appended.deny !== undefined) {
85      logEvent(engine, 'nudge-error', { message: appended.deny });
86
87      return;
88    }
89
90    this.#lastNudgeAt = added;
91    this.#baselineAtNudge = this.#floor.baseline();
92    this.#nudges += 1;
93    logEvent(engine, 'nudge-sent', { tokens, added, nudge: this.#nudges });
94    await showTimedStatus(engine, 'nudged');
95  }
96
97  #isDue(added: number): boolean {
98    // A compaction or /clear restarts the floor, so the nudges count from 1 again.
99    if (this.#floor.state().waitingFor !== null || this.#hasBaselineMoved()) {
100      this.startCountOver();
101    }
102
103    if (this.#floor.state().waitingFor !== null || added < this.#settings.minTokens) {
104      return false;
105    }
106
107    return this.#lastNudgeAt === undefined || added - this.#lastNudgeAt >= this.#step();
108  }
109
110  // The floor takes a new baseline on its first turn after a compaction, a /clear or a new session.
111  #hasBaselineMoved(): boolean {
112    return this.#baselineAtNudge !== undefined && this.#floor.baseline() !== this.#baselineAtNudge;
113  }
114
115  #step(): number {
116    return Math.max(Math.floor(this.#settings.minTokens / 2), MIN_STEP_TOKENS);
117  }
118
119  /** Forgets the last nudge, so the next one is number 1. */
120  startCountOver(): void {
121    this.#lastNudgeAt = undefined;
122    this.#baselineAtNudge = undefined;
123    this.#nudges = 0;
124  }
125}
126
hooks/compact-rule.ts 20 lines
1import type { PromptContextBlock } from 'claude-code';
2
3const RULE_BLOCK_NAME = 'smartcompact';
4
5// No "fresh context" (it sounds like /clear) and no summary by the plugin: it only starts /compact.
6const RULE_TEXT = [
7  'When you finish a piece of work and can start the next piece of the same task right away, and that piece no longer needs the details so far, end your answer with:',
8  '<smartcompact>the prompt to start the next piece with</smartcompact>',
9  'Then stop. The smartcompact plugin runs `/compact` and sends that prompt afterwards.',
10  'Leave the tag out when you wait for other sessions or agents, or when all work is done. Leave it out when you wait for the user too, unless smartcompact asked you to compact.',
11  // Sessions copied the tag pattern and wrote an empty tag to mean "no compaction".
12  'Never write an empty tag.',
13  'Only the main session does this, never a subagent.',
14].join('\n');
15
16/** The context blocks with the smartcompact rule last. A block of that name from below is replaced. */
17export function withCompactRule(blocks: readonly PromptContextBlock[]): PromptContextBlock[] {
18  return [...blocks.filter((block) => block.name !== RULE_BLOCK_NAME), { name: RULE_BLOCK_NAME, text: RULE_TEXT }];
19}
20
hooks/context-floor.ts 82 lines
1/** Why a first turn is not judged. The text goes into the log as the skip reason. */
2export type FirstTurnReason = 'first turn of session' | 'first turn after compaction';
3
4/** What the floor keeps between restarts. */
5export type FloorState = {
6  baseline: number;
7  waitingFor: FirstTurnReason | null;
8};
9
10/**
11 * Measures the tokens a conversation added since its first turn, or since the first turn after its last compaction.
12 * A resumed session without saved state waits for its first turn, as a new session does.
13 */
14export class ContextFloor {
15  #baseline = 0;
16  #waitingFor: FirstTurnReason | null = null;
17  /** The context size at the start of the awaited first turn, when a step of it was read. */
18  #turnStartReading: number | undefined;
19
20  /**
21   * Returns why the turn is skipped when it is the awaited first turn, and sets the baseline from it.
22   * Every other turn changes nothing and gets undefined.
23   */
24  takeFirstTurn(tokens: number): FirstTurnReason | undefined {
25    if (this.#waitingFor === null) {
26      return undefined;
27    }
28
29    const reason = this.#waitingFor;
30
31    // The size at the end holds the work of the turn. A one-step turn has no start reading and adds little.
32    this.#baseline = this.#turnStartReading ?? tokens;
33    this.#waitingFor = null;
34    this.#turnStartReading = undefined;
35
36    return reason;
37  }
38
39  /** Keeps the first context size read during the awaited first turn. Does nothing at any other time. */
40  noteTurnStartReading(tokens: number): void {
41    if (this.#waitingFor !== null && this.#turnStartReading === undefined) {
42      this.#turnStartReading = tokens;
43    }
44  }
45
46  /** A new session already holds its startup context, so the first turn sets the baseline. */
47  startNewSession(): void {
48    this.#waitingFor = 'first turn of session';
49    this.#turnStartReading = undefined;
50  }
51
52  /** After any compaction the next turn sets the baseline. */
53  restartAfterCompaction(): void {
54    this.#waitingFor = 'first turn after compaction';
55    this.#turnStartReading = undefined;
56  }
57
58  /** Takes over a state saved for the same session. */
59  restore(state: FloorState): void {
60    this.#baseline = state.baseline;
61    this.#waitingFor = state.waitingFor;
62    this.#turnStartReading = undefined;
63  }
64
65  state(): FloorState {
66    return { baseline: this.#baseline, waitingFor: this.#waitingFor };
67  }
68
69  baseline(): number {
70    return this.#baseline;
71  }
72
73  /** Zero while the first turn is awaited. Never below zero: after a compaction the context can drop under the baseline. */
74  addedTokens(tokens: number): number {
75    if (this.#waitingFor !== null) {
76      return 0;
77    }
78
79    return Math.max(0, tokens - this.#baseline);
80  }
81}
82
hooks/context-tokens.ts 10 lines
1import type { Engine } from './engine.ts';
2
3/**
4 * The context size in tokens. Undefined until the first response after a new session or a compaction.
5 * A missing count is no size at all, so it must never stand in as 0.
6 */
7export async function contextTokens(engine: Engine): Promise<number | undefined> {
8  return (await engine.usage()).context.tokens;
9}
10
hooks/engine.ts 58 lines
1import type {
2  AgentInfo,
3  CommandSpec,
4  ModelCompleteRequest,
5  ModelCompleteResult,
6  PromptBox,
7  PromptFillArgs,
8  PromptFilled,
9  PromptSubmitArgs,
10  PromptSubmitResult,
11  SessionAppendResult,
12  SessionCompactArgs,
13  SessionCompactResult,
14  SessionMessage,
15  SessionUsage,
16  TimerCall,
17} from 'claude-code';
18
19/**
20 * The engine calls this plugin makes. The engine refuses a module that passes `$` around,
21 * so register.ts binds each call from `$` and the other files get this.
22 */
23export type Engine = {
24  /** The plugin's folder, where its shipped files sit. */
25  pluginRoot: () => string;
26  now: () => Promise<number>;
27  after: TimerCall;
28  status: (text: string | undefined) => void;
29  sessionId: () => Promise<string>;
30  cwd: () => Promise<string>;
31  usage: () => Promise<SessionUsage>;
32  messages: () => Promise<SessionMessage[]>;
33  agents: () => Promise<AgentInfo[]>;
34  readPrompt: () => Promise<PromptBox>;
35  fillPrompt: (input: PromptFillArgs) => Promise<PromptFilled>;
36  submitPrompt: (input: PromptSubmitArgs) => Promise<PromptSubmitResult>;
37  /** Adds a user row the model reads and the person does not see as typed. */
38  appendNote: (text: string) => Promise<SessionAppendResult>;
39  compact: (args: SessionCompactArgs) => Promise<SessionCompactResult>;
40  registerCommand: (command: CommandSpec) => Promise<{ command: string }>;
41  complete: (request: ModelCompleteRequest) => Promise<ModelCompleteResult>;
42  fs: {
43    exists: (path: string) => Promise<boolean>;
44    read: (path: string) => Promise<string>;
45    write: (path: string, text: string) => Promise<void>;
46  };
47  store: {
48    get: (key: string) => Promise<unknown>;
49    set: (key: string, value: unknown) => Promise<void>;
50    delete: (key: string) => Promise<void>;
51  };
52  env: {
53    logFile: () => Promise<string | undefined>;
54    userProfile: () => Promise<string | undefined>;
55    home: () => Promise<string | undefined>;
56  };
57};
58
hooks/error-message.ts 5 lines
1/** A short message for the log, from anything a promise rejected with. */
2export function messageOf(error: unknown): string {
3  return (error instanceof Error ? error.message : String(error)).slice(0, 200);
4}
5
hooks/event-log.ts 44 lines
1import type { Engine } from './engine.ts';
2import { logFilePath } from './log-paths.ts';
3
4export type EventDetails = Record<string, unknown>;
5
6// `$.fs` has no append and reads at most 4 MiB, so a long log keeps only its newest part.
7const MAX_LOG_CHARS = 2_000_000;
8const KEPT_LOG_CHARS = 1_000_000;
9
10// One write at a time, so two events of one session never overwrite each other.
11let pendingWrite: Promise<void> = Promise.resolve();
12
13/** Adds one JSON line to the log file in the background. It never throws. */
14export function logEvent(engine: Engine, event: string, details: EventDetails = {}): void {
15  pendingWrite = pendingWrite.then(() => appendLine(engine, event, details)).catch(() => undefined);
16}
17
18async function appendLine(engine: Engine, event: string, details: EventDetails): Promise<void> {
19  const path = await logFilePath(engine);
20  const line = JSON.stringify(await lineFor(engine, event, details));
21  const existing = (await engine.fs.exists(path)) ? await engine.fs.read(path) : '';
22
23  await engine.fs.write(path, `${newestPartOf(existing)}${line}\n`);
24}
25
26/** The fixed fields come first and the event details follow, as in the wrapper's log. */
27async function lineFor(engine: Engine, event: string, details: EventDetails): Promise<EventDetails> {
28  return {
29    time: new Date(await engine.now()).toISOString(),
30    event,
31    session: await engine.sessionId(),
32    cwd: await engine.cwd(),
33    ...details,
34  };
35}
36
37function newestPartOf(log: string): string {
38  if (log.length <= MAX_LOG_CHARS) {
39    return log;
40  }
41
42  return log.slice(log.indexOf('\n', log.length - KEPT_LOG_CHARS) + 1);
43}
44
hooks/floor-start.ts 21 lines
1import type { ContextFloor } from './context-floor.ts';
2import type { Engine } from './engine.ts';
3import { loadFloorState } from './floor-store.ts';
4
5/**
6 * Sets the floor for a starting session. Saved state for this session id wins, so a resume keeps its count.
7 * Without it the session counts as new, also a resume, because its startup context is not work done.
8 */
9export async function startFloor(engine: Engine, floor: ContextFloor): Promise<void> {
10  const saved = await loadFloorState(engine, await engine.sessionId());
11
12  if (saved !== undefined) {
13    floor.restore(saved);
14
15    return;
16  }
17
18  // Also replaces the floor of the previous session after a /resume to another session in the same process.
19  floor.startNewSession();
20}
21
hooks/floor-store.ts 63 lines
1import type { ContextFloor, FirstTurnReason, FloorState } from './context-floor.ts';
2import type { Engine } from './engine.ts';
3
4// One store key holds every session's floor state, keyed by session id.
5const STORE_KEY = 'floors';
6const MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
7
8type SavedFloor = FloorState & { savedAt: number };
9
10type SavedFloors = Record<string, SavedFloor>;
11
12/** Reads the state saved for this session id. Returns undefined when none is saved. */
13export async function loadFloorState(engine: Engine, sessionId: string): Promise<FloorState | undefined> {
14  const saved = (await readSavedFloors(engine))[sessionId];
15
16  if (saved === undefined) {
17    return undefined;
18  }
19
20  return { baseline: saved.baseline, waitingFor: saved.waitingFor };
21}
22
23/** Saves the floor under the current session id and drops the entries older than 30 days. */
24export async function saveFloorState(engine: Engine, floor: ContextFloor): Promise<void> {
25  const now = await engine.now();
26  const sessionId = await engine.sessionId();
27  const floors = withoutOldEntries(await readSavedFloors(engine), now);
28
29  floors[sessionId] = { ...floor.state(), savedAt: now };
30
31  await engine.store.set(STORE_KEY, floors);
32}
33
34async function readSavedFloors(engine: Engine): Promise<SavedFloors> {
35  const value = await engine.store.get(STORE_KEY);
36
37  if (typeof value !== 'object' || value === null) {
38    return {};
39  }
40
41  return Object.fromEntries(Object.entries(value).filter(([, entry]) => isSavedFloor(entry)));
42}
43
44function withoutOldEntries(floors: SavedFloors, now: number): SavedFloors {
45  return Object.fromEntries(Object.entries(floors).filter(([, saved]) => now - saved.savedAt <= MAX_AGE_MS));
46}
47
48function isSavedFloor(entry: unknown): entry is SavedFloor {
49  const saved = entry as Partial<SavedFloor> | null;
50
51  return (
52    typeof saved === 'object' &&
53    saved !== null &&
54    typeof saved.baseline === 'number' &&
55    typeof saved.savedAt === 'number' &&
56    isWaitingFor(saved.waitingFor)
57  );
58}
59
60function isWaitingFor(value: unknown): value is FirstTurnReason | null {
61  return value === null || value === 'first turn of session' || value === 'first turn after compaction';
62}
63
hooks/pending-compaction.ts 273 lines
1import type { Timer } from 'claude-code';
2import type { Engine } from './engine.ts';
3import { composerBlocker } from './composer-blocker.ts';
4import type { ContextFloor } from './context-floor.ts';
5import { messageOf } from './error-message.ts';
6import { submitContinuePrompt } from './continue-prompt-submit.ts';
7import { logEvent } from './event-log.ts';
8import { saveFloorState } from './floor-store.ts';
9import { NOT_IN_THIS_MODE, isUnavailableInThisMode } from './mode-unavailable.ts';
10import { showTimedStatus } from './status-line.ts';
11import type { TurnCounter } from './turn-counter.ts';
12
13// No engine event says that a dialog closed, so a busy session is retried on a clock: two minutes at most.
14const RETRY_MS = 3000;
15const MAX_RETRIES = 40;
16// How the engine's error for a compaction the person cancelled ends.
17const COMPACTION_CANCELLED = 'Compaction canceled.';
18
19type WaitReason = 'prompt has text' | 'dialog open' | 'session busy';
20
21/** Who asked for a compaction: the judge after a yes, or the session with its tag. */
22export type CompactionSource = 'judge' | 'request';
23
24/** A compaction this plugin will run: after a judge yes, or because the session asked for it with its tag. */
25export type CompactionRequest = {
26  /** Logged with each row of the compaction, so the log tells the two paths apart. */
27  source: CompactionSource;
28  tokens: number;
29  /** The turn count when the asking turn ended. A higher count when it is held means a new turn took over. */
30  turnsAtEnd: number;
31  /** What the summarizer is told to keep. */
32  instructions: string;
33  /** The text for `{next}` in the continue prompt. Empty gets the fallback text. */
34  next: string;
35  /** True when the session asked. It has stopped and waits, so a skipped compaction still sends the prompt. */
36  continueWhenSkipped: boolean;
37  /** Logged when a retry from the clock or an edit fails, the same event the asking path logs. */
38  errorEvent: 'judge-error' | 'request-error';
39  /** Gets the turn count at the send of a continue prompt after a skipped compaction. It feeds the loop guard. */
40  onContinuedAfterSkip?: (turnsAtSend: number) => void;
41};
42
43/** A request that waits for its compaction. */
44type HeldRequest = CompactionRequest & {
45  heldAt: number;
46  retries: number;
47  loggedWait?: WaitReason;
48  timer?: Timer;
49};
50
51/**
52 * Holds a compaction request until the prompt box is empty and no dialog is open, then compacts and continues.
53 * A new turn drops the request. The next finished turn is judged or read for a tag again.
54 */
55export class PendingCompaction {
56  #turns: TurnCounter;
57  #floor: ContextFloor;
58  #held: HeldRequest | undefined;
59
60  constructor(turns: TurnCounter, floor: ContextFloor) {
61    this.#turns = turns;
62    this.#floor = floor;
63  }
64
65  /** Holds the request and tries to compact right away. */
66  async holdAndTry(engine: Engine, request: CompactionRequest): Promise<void> {
67    if (this.#turns.hasChangedSince(request.turnsAtEnd)) {
68      await this.#dropBeforeHold(engine, request);
69
70      return;
71    }
72
73    this.#take()?.timer?.cancel();
74    this.#held = { ...request, heldAt: await engine.now(), retries: 0 };
75
76    await this.#attempt(engine);
77  }
78
79  /** Tries again when an edit left the prompt box empty. */
80  retryAfterEdit(engine: Engine, draft: string): void {
81    if (this.#held !== undefined && draft.trim() === '') {
82      void this.#attemptAndLogError(engine);
83    }
84  }
85
86  /** Forgets a held request and logs why. Does nothing when no request is held. */
87  async drop(engine: Engine, reason: string): Promise<void> {
88    const held = this.#take();
89
90    if (held === undefined) {
91      return;
92    }
93
94    held.timer?.cancel();
95    logEvent(engine, 'compact-dropped', { source: held.source, reason, waitedMs: await waitedMs(engine, held) });
96    await showTimedStatus(engine, 'dropped');
97  }
98
99  // The new turn's drop ran while the request was still on its way here, so it found nothing to drop.
100  async #dropBeforeHold(engine: Engine, request: CompactionRequest): Promise<void> {
101    logEvent(engine, 'compact-dropped', { source: request.source, reason: 'new turn started', waitedMs: 0 });
102    await showTimedStatus(engine, 'dropped');
103  }
104
105  // A retry from the clock or an edit has no caller that catches, so a failure is logged here.
106  async #attemptAndLogError(engine: Engine): Promise<void> {
107    const held = this.#held;
108
109    if (held === undefined) {
110      return;
111    }
112
113    try {
114      await this.#attempt(engine);
115    } catch (error) {
116      logEvent(engine, held.errorEvent, { message: messageOf(error) });
117      await showTimedStatus(engine, 'error');
118    }
119  }
120
121  async #attempt(engine: Engine): Promise<void> {
122    const held = this.#held;
123
124    if (held === undefined) {
125      return;
126    }
127
128    const blocker = await composerBlocker(engine);
129
130    // Dropped or replaced while the prompt box was read.
131    if (this.#held !== held) {
132      return;
133    }
134
135    if (blocker !== undefined) {
136      await this.#wait(engine, held, blocker);
137
138      return;
139    }
140
141    this.#take();
142    held.timer?.cancel();
143    await this.#compact(engine, held);
144  }
145
146  async #compact(engine: Engine, held: HeldRequest): Promise<void> {
147    const turnsAtCompact = this.#turns.count();
148
149    engine.status('compacting...');
150    // Shows that the compact call began, also when no outcome row follows.
151    logEvent(engine, 'compact-started', { source: held.source, tokens: held.tokens, retries: held.retries });
152
153    try {
154      const result = await engine.compact({ instructions: held.instructions });
155
156      if (result.skip !== undefined) {
157        logEvent(engine, 'compact-failed', { source: held.source, message: result.skip });
158        await showTimedStatus(engine, 'compact skipped');
159
160        if (held.continueWhenSkipped) {
161          await this.#continueAfterSkip(engine, held, turnsAtCompact);
162        }
163
164        return;
165      }
166    } catch (error) {
167      await this.#handleCompactError(engine, held, turnsAtCompact, error);
168
169      return;
170    }
171
172    const waited = await waitedMs(engine, held);
173
174    logEvent(engine, 'compact-typed', { source: held.source, tokens: held.tokens, waitedMs: waited });
175    // The engine does not run this plugin's own session.compact hook for this call, so the floor restarts here.
176    this.#floor.restartAfterCompaction();
177    await saveFloorState(engine, this.#floor);
178    await showTimedStatus(engine, 'compacted');
179    await submitContinuePrompt(engine, this.#turns, turnsAtCompact, {
180      next: held.next,
181      kind: 'after-compaction',
182      source: held.source,
183    });
184  }
185
186  // Nothing was compacted, so the prompt goes out without the compacted line and the send is reported.
187  async #continueAfterSkip(engine: Engine, held: HeldRequest, turnsAtCompact: number): Promise<void> {
188    const sent = await submitContinuePrompt(engine, this.#turns, turnsAtCompact, {
189      next: held.next,
190      kind: 'after-skip',
191      source: held.source,
192    });
193
194    if (sent) {
195      held.onContinuedAfterSkip?.(turnsAtCompact);
196    }
197  }
198
199  // The engine refuses a compaction while a turn runs. A turn that started is a drop. A mode without compaction or a
200  // cancel by the person is a drop too, since a retry cannot work or would undo the cancel. Anything else is a wait.
201  async #handleCompactError(engine: Engine, held: HeldRequest, turnsAtCompact: number, error: unknown): Promise<void> {
202    const { source } = held;
203
204    if (this.#turns.hasChangedSince(turnsAtCompact)) {
205      logEvent(engine, 'compact-dropped', { source, reason: 'new turn started', waitedMs: await waitedMs(engine, held) });
206
207      return;
208    }
209
210    const finalReason = finalReasonOf(error);
211
212    if (finalReason !== undefined) {
213      logEvent(engine, 'compact-failed', { source, message: messageOf(error) });
214      logEvent(engine, 'compact-dropped', { source, reason: finalReason, waitedMs: await waitedMs(engine, held) });
215      await showTimedStatus(engine, 'dropped');
216
217      return;
218    }
219
220    // Logged once per wait. The retries stay quiet.
221    if (held.loggedWait !== 'session busy') {
222      logEvent(engine, 'compact-failed', { source, message: messageOf(error) });
223    }
224
225    this.#held = held;
226    await this.#wait(engine, held, 'session busy');
227  }
228
229  /** Shows and logs why the request waits. A full prompt box waits for an edit, the rest is retried on the clock. */
230  async #wait(engine: Engine, held: HeldRequest, reason: WaitReason): Promise<void> {
231    if (held.loggedWait !== reason) {
232      held.loggedWait = reason;
233      logEvent(engine, 'compact-waiting', { reason });
234    }
235
236    engine.status(reason === 'prompt has text' ? 'waiting for empty prompt' : 'waiting for idle');
237    held.timer?.cancel();
238
239    if (reason === 'prompt has text') {
240      return;
241    }
242
243    if (held.retries >= MAX_RETRIES) {
244      await this.drop(engine, 'not idle in time');
245
246      return;
247    }
248
249    held.retries += 1;
250    held.timer = engine.after(RETRY_MS, () => void this.#attemptAndLogError(engine));
251  }
252
253  #take(): HeldRequest | undefined {
254    const held = this.#held;
255    this.#held = undefined;
256
257    return held;
258  }
259}
260
261// The drop reason for an error no retry can fix, else undefined.
262function finalReasonOf(error: unknown): string | undefined {
263  if (isUnavailableInThisMode(error)) {
264    return NOT_IN_THIS_MODE;
265  }
266
267  return messageOf(error).endsWith(COMPACTION_CANCELLED) ? 'cancelled' : undefined;
268}
269
270async function waitedMs(engine: Engine, held: HeldRequest): Promise<number> {
271  return (await engine.now()) - held.heldAt;
272}
273