Long Run|INEMA.CLUB-PRO

Lesson 3 · 18 min

Context in bands: 50, 70 and 85% of the compaction point

The biggest enemy of a long session is repeated compaction: the goal survives, but what is done and what remains get diluted. The hook-longrun measures the context and acts before the limit, in three bands. In this lesson you turn on the hook and trigger all three bands in a rehearsal, without spending a token.

Start the lesson
Industrial pressure gauge with the needle in the middle band on a dark panel.

In this lesson you will

  1. 1Where the bands sit: ~484, ~677 and ~822 thousand tokens
  2. 2Compaction × handoff: what each one keeps
  3. 3Turn on the hook for the five Claude Code events
  4. 4Rehearse the hook with a fake transcript

Goal: turn on the hook-longrun and trigger the three bands in a rehearsal, knowing what each one asks of the agent and when to do /clear + /prime.

01The bands over the compaction point

The percentage is of the point at which Claude Code compacts by size, not of the whole window.

In the measurements from 10/07 to 10/15/2026, no session compacted by size: all 93 compactions were due to idle time (lesson 4).

Hook-longrun 1.9.6 (10/18/2026). The bands change with LONGRUN_FAIXAS="50,70,85". The warning arrives one tool call later, because the transcript is written with a delay.

02Compact, write a handoff, or open a new session

What each path keeps, measured on 10/15/2026 (17 compactions with a handoff from the same session in the 3 h before).

1. Compaction

The summary cites 20 to 54 files and keeps recent steps and commands. Within the same session, it holds more concrete detail than the handoff.

But it doesn't separate what's confirmed from what's a guess, it mixes the decisions into the narrative, and it doesn't survive /clear.

03Turn on the hook and rehearse the bands

The rehearsal writes a fake transcript with whatever number of tokens you want and calls the hook the way Claude Code would.

~/.claude/settings.json · "hooks" excerpt (merge with yours)
{
  "cleanupPeriodDays": 365,
  "hooks": {
    "PreCompact":       [{ "hooks": [{ "type": "command", "command": "bash ~/projetos/execucao-longa/tools/hook-longrun.sh 2>/dev/null || true", "timeout": 10 }] }],
    "SessionStart":     [{ "matcher": "compact|resume|clear", "hooks": [{ "type": "command", "command": "bash ~/projetos/execucao-longa/tools/hook-longrun.sh 2>/dev/null || true", "timeout": 10 }] }],
    "PostToolUse":      [{ "hooks": [{ "type": "command", "command": "bash ~/projetos/execucao-longa/tools/hook-longrun.sh 2>/dev/null || true", "timeout": 10 }] }],
    "UserPromptSubmit": [{ "hooks": [{ "type": "command", "command": "bash ~/projetos/execucao-longa/tools/hook-longrun.sh 2>/dev/null || true", "timeout": 10 }] }],
    "Stop":             [{ "hooks": [{ "type": "command", "command": "bash ~/projetos/execucao-longa/tools/hook-longrun.sh 2>/dev/null || true", "timeout": 10 }] }]
  }
}
ensaio-hook.sh · save to a file and run with bash
# ensaio-hook.sh — dispara o hook-longrun com uma transcrição FALSA (nenhum token gasto)
# uso: bash ensaio-hook.sh <tokens|-> [minutos-parado] [evento]     ("-" reaproveita a transcrição anterior)
E=/tmp/ensaio-hook; K=~/projetos/execucao-longa
mkdir -p $E/proj/longrun/2026-10-19-teste $E/proj/handoffs/history
[ -f $E/proj/longrun/2026-10-19-teste/state.md ] || echo "# Estado" > $E/proj/longrun/2026-10-19-teste/state.md
[ "${1:-500000}" = "-" ] || python3 - "${1:-500000}" "${2:-0}" > $E/t.jsonl <<'PY'
import json, sys, time, datetime
n, parado = int(sys.argv[1]), float(sys.argv[2])
ts = datetime.datetime.fromtimestamp(time.time() - parado * 60, datetime.timezone.utc).isoformat().replace("+00:00", "Z")
print(json.dumps({"type": "assistant", "timestamp": ts, "message": {"model": "claude-opus-5-5",
      "content": [{"type": "text", "text": "ok"}],
      "usage": {"input_tokens": 10, "cache_read_input_tokens": n - 10, "cache_creation_input_tokens": 0}}}))
PY
echo "{\"hook_event_name\":\"${3:-PostToolUse}\",\"cwd\":\"$E/proj\",\"session_id\":\"ensaio\",\"transcript_path\":\"$E/t.jsonl\",\"prompt\":\"oi\"}" \
  | LONGRUN_ESTADO=$E/estado LONGRUN_AGENDAR_ARQ=$E/agenda.jsonl python3 $K/tools/hook-longrun.py
echo
Terminal · the three bands
rm -rf /tmp/ensaio-hook
bash ensaio-hook.sh 500000    # faixa 1
bash ensaio-hook.sh 500000    # mesma faixa: silêncio (uma vez por faixa)
bash ensaio-hook.sh 700000    # faixa 2
bash ensaio-hook.sh 830000    # faixa 3
Example from the repository
$ bash ensaio-hook.sh 500000
{"hookSpecificOutput": {"hookEventName": "PostToolUse", "additionalContext":
 "Contexto em 52% do ponto de compactação (500 mil de 967 mil tokens). FAIXA 1 de 3:
 antes de seguir, acrescente em longrun/2026-10-19-teste/canal.md o que você precisaria
 se o histórico sumisse agora (…) Só acrescentar. Depois continue o trabalho normalmente."}}

$ bash ensaio-hook.sh 500000

$ bash ensaio-hook.sh 700000
{… "Contexto em 72% do ponto de compactação (700 mil de 967 mil tokens). FAIXA 2 de 3:
 atualize state.md, progress.md e (…) canal.md; termine a unidade de trabalho atual e
 grave o handoff agora (…)"},
 "systemMessage": "Execução longa em 72% do ponto de compactação: hora de /compact
 (o agente foi avisado)."}

$ bash ensaio-hook.sh 830000
{… "Contexto em 86% do ponto de compactação (830 mil de 967 mil tokens). FAIXA 3 de 3:
 não abra trabalho novo. (…) /session-handoff, sessão nova e /prime (…) ao fim da
 unidade em curso, sem largá-la no meio. (…)"},
 "systemMessage": "Execução longa em 86% do ponto de compactação: hora de
 /session-handoff + sessão nova + /prime."}

Line breaks and (…) added to fit the screen; the text is from hook-longrun 1.9.6. The additionalContext goes to the agent; the systemMessage appears to you.

04Practice

The first exercise touches your settings.json: make a backup first.

E6 · Hook turned on for all five events

15 min

Copy settings.json to settings.json.bak and merge the hooks excerpt into the ones you already have (don't delete yours). Open a new Claude Code session to load it.

Done when: python3 -c "import json,os;h=json.load(open(os.path.expanduser('~/.claude/settings.json')))['hooks'];print(sorted(e for e,l in h.items() if 'hook-longrun' in json.dumps(l)))" → ['PostToolUse', 'PreCompact', 'SessionStart', 'Stop', 'UserPromptSubmit'].

E7 · The three bands in the rehearsal

10 min

Save ensaio-hook.sh and run the three-band sequence. Then delete /tmp/ensaio-hook and run LONGRUN_TETO=600000 bash ensaio-hook.sh 500000.

Done when: these appear, in this order: FAIXA 1 de 3, nothing, FAIXA 2 de 3, and FAIXA 3 de 3; with LONGRUN_TETO=600000, 500 thousand already lands in FAIXA 2 de 3 (83% of 600 thousand).

05Lesson quiz

Question 1 of 4

On a 1M model, at how many tokens does the hook warn for band 2 by default?skill

06In three sentences

About the right momentThe bands are 50/70/85% of ~967k: canal, then state + handoff + /compact, then nothing new and a new session at the end.
Both togetherThe compaction keeps more detail; the handoff keeps what's proven and the decisions, and survives /clear.
Rehearse firstWith a fake transcript you see every hook warning without spending a single token.

Going further

Previous lessonNext lesson