SLOPSHOPPER

loop-brake

Ends a turn after N forced Stop-hook continuations so a looping goal hook cannot burn your usage

toaststatuspromptprocess
★ 2v0.2.1no licenseupdated 2026-10-19cpeaustriajc/cj-stack/mods/loop-brake
A shopper browsing a rack in a slop shop
README

loop-brake

A mod (a plugin that adds a hook to Claude Code) that ends a turn after a set number of forced continuations. A looping goal hook then cannot burn your usage.

Why

A Stop hook runs when Claude ends its turn. The hook can block the stop and force Claude to continue. A goal hook that never gets satisfied blocks every time, and Claude keeps working.

loop-brake counts these forced continuations. At the cap, it lets the stop through and shows a toast (a short message in the interface).

Install

/plugin marketplace add cpeaustriajc/cj-stack
/plugin install loop-brake@cj-stack

To see that it is on, run claude plugin list and look for loop-brake. The mod is optional: cj-stack does not install it.

Example

  1. A Stop hook blocks the stop with "Not satisfied: keep going". Claude continues.
  2. The hook blocks twice more. That is three forced continuations, the default cap.
  3. The hook blocks a fourth time. The mod lets this stop through, and Claude stops.

You see this toast: "loop-brake: stopped after 3 forced continuations — reply to keep going". Send a message to start a new turn.

What it does

EventDecision
A Stop hook blocks the stopCount it and show the count.
The count is at the cap and the hook blocks againRemove the block, show the toast, reset the count.
A Stop hook does not blockReset the count.
A Stop hook ends the session outrightReset the count and leave the result as it is.
You send a prompt from the composer or from your phoneReset the count.
A machine-injected prompt arrivesKeep the count.
A session startsDelete this mod's state files older than seven days.

When a desktop or phone surface is attached, the status row shows "1 of 3 forced continuations". A terminal-only session shows no status row.

The mod also writes { "v": 1, "streak": 1, "cap": 3 } to ~/.claude/mod-state/<session id>.loop-brake.json for a custom status line to read. If CLAUDE_CONFIG_DIR is set, the file goes to $CLAUDE_CONFIG_DIR/mod-state.

Settings

OptionDefaultMeaning
maxContinuations3Forced continuations allowed per turn, 1 to 20.

A value that is not a whole number from 1 to 20 falls back to 3. Run claude plugin configure loop-brake@cj-stack to show the options and which are unset.

To turn the mod off, run claude plugin disable loop-brake@cj-stack.

Limits

  • It counts only blocks from Stop hooks. It does not limit tool calls or token use.
  • A reset writes a zero count to the state file. It does not delete the file.
  • If the mod cannot find a home directory, it writes and prunes no state file. The brake still counts.
  • If the prune fails, the brake still counts. If the state file write fails, the hook throws "loop-brake: could not publish state for the status line".

More

Source 1 files
hooks/register.ts 113 lines
1import type { Hook, Register } from 'claude-code'
2
3type Api = Parameters<Hook<'classic.Stop'>>[0]
4
5const MOD = 'loop-brake'
6const WEEK = 7 * 86_400_000
7const DEFAULT_CAP = 3
8
9function capFrom(value: unknown): number {
10  return typeof value === 'number' && Number.isInteger(value) && value >= 1 && value <= 20 ? value : DEFAULT_CAP
11}
12
13async function stateDir($: Api): Promise<string | undefined> {
14  const config = await $.env.get('CLAUDE_CONFIG_DIR')
15  if (config) return `${config}/mod-state`
16  const home = (await $.env.get('HOME')) || (await $.env.get('USERPROFILE'))
17  return home ? `${home}/.claude/mod-state` : undefined
18}
19
20async function remove($: Api, paths: string[]): Promise<void> {
21  if (paths.length === 0) return
22  await $.process.run(['rm', '-f', '--', ...paths])
23}
24
25async function publishState($: Api, streak: number, cap: number): Promise<void> {
26  try {
27    const dir = await stateDir($)
28    if (!dir) return
29    const path = `${dir}/${await $.session.id()}.${MOD}.json`
30    await $.fs.write(path, JSON.stringify({ v: 1, streak, cap }))
31  } catch (cause) {
32    throw new Error(`${MOD}: could not publish state for the status line`, { cause })
33  }
34}
35
36async function pruneState($: Api): Promise<void> {
37  try {
38    const dir = await stateDir($)
39    if (!dir || !(await $.fs.exists(dir))) return
40    const now = await $.clock.now()
41    const stale = (await $.fs.list(dir)).filter(
42      f => f.kind === 'file' && f.name.endsWith(`.${MOD}.json`) && now - f.mtimeMs > WEEK,
43    )
44    await remove($, stale.map(f => `${dir}/${f.name}`))
45  } catch (cause) {
46    throw new Error(`${MOD}: could not prune old state files`, { cause })
47  }
48}
49
50async function drawStatus($: Api, streak: number, cap: number): Promise<void> {
51  const surfaces = await $.session.surfaces()
52  const shown = streak > 0 && surfaces.some(s => s !== 'terminal')
53  await $.ui.status(shown ? `${streak} of ${cap} forced continuations` : undefined)
54}
55
56async function show($: Api, streak: number, cap: number): Promise<void> {
57  await drawStatus($, streak, cap)
58  await publishState($, streak, cap)
59}
60
61async function clearStreak($: Api, streak: number, cap: number): Promise<void> {
62  if (streak > 0) await show($, 0, cap)
63}
64
65export const register: Register = (on, options) => {
66  const cap = capFrom(options.maxContinuations)
67  let streak = 0
68
69  on('session.start', async ($, e, next) => {
70    const result = await next(e)
71    await pruneState($)
72    return result
73  })
74
75  on('session.attach', async ($, e, next) => {
76    const result = await next(e)
77    await drawStatus($, streak, cap)
78    return result
79  })
80
81  on('session.detach', async ($, e, next) => {
82    const result = await next(e)
83    await drawStatus($, streak, cap)
84    return result
85  })
86
87  on('prompt.submit', async ($, e, next) => {
88    if (e.origin.kind === 'composer' || e.origin.kind === 'bridge') {
89      await clearStreak($, streak, cap)
90      streak = 0
91    }
92    return next(e)
93  })
94
95  on('classic.Stop', async ($, e, next) => {
96    const result = await next(e)
97    if (result.block === undefined || result.preventContinuation) {
98      await clearStreak($, streak, cap)
99      streak = 0
100      return result
101    }
102    if (streak >= cap) {
103      await clearStreak($, streak, cap)
104      streak = 0
105      $.ui.toast(`loop-brake: stopped after ${cap} forced continuations — reply to keep going`)
106      return { ...result, block: undefined }
107    }
108    streak++
109    await show($, streak, cap)
110    return result
111  })
112}
113