Skip to content

Restart a second mate when its conversation passes the context limit - #49

Merged
uayyagari merged 2 commits into
mainfrom
fm/qwr-supervisor-context
Oct 27, 2026
Merged

uayyagari merged 2 commits into
mainfrom
fm/qwr-supervisor-context

Conversation

@uayyagari

@uayyagari uayyagari commented Oct 26, 2026 •

Copy link
Copy Markdown

Problem

  • A second mate that keeps taking wakes without finishing a piece of work never asks for a fresh session (PR 46 only restarts after done/ready/failed), so every call re-reads an ever longer conversation.
  • Measured 2026-10-26: 517 calls at about 218k tokens each for one mate; second-mate homes average 144k context per call.

Fix

  • bin/fm-secondmate-fresh-session.sh sweep now also reads each local Claude mate's newest transcript. Past FM_SECONDMATE_CONTEXT_LIMIT (default 150000, 0 = off) it files the same request and handover note as a finished piece of work (--source context --verb handover).
  • The existing apply guards decide when the restart happens: no open request, no unread steering message, no unacknowledged wake, agent idle. Child workers keep running, and the note points at the session-start digest, which lists open decisions and unread status.
  • A transcript last written before the mate's previous restart is ignored, so the old session cannot trigger a second restart.
  • New bin/fm-context-replay.py replays transcripts with a limit for before/after numbers.
  • Not covered: the main firstmate session has no relaunch path, so the captain restarts it; its session-start digest is the handover.

Worked example

Mate sm1 is at 250105 tokens (250000 cache read + 100 cache creation + 5 input) and has child1 in flight.

before: sm1 keeps its conversation until a done report -> each call re-reads ~250k
after:  sweep sees 250105 >= 150000 -> files request + note ("context was 250105 tokens (limit 150000)")
        unread steering message -> "deferred sm1: a steering message to it is unread" (nothing lost)
        message handled, agent idle -> "restarted sm1"; fresh session reads the note, runs session start
        old transcript is older than the restart -> no second request

Proof

  • bash tests/fm-secondmate-fresh-session.test.sh (new T9: small context and limit 0 do nothing, a large context defers on an unread message then restarts, no second restart) and bash tests/fm-context-replay.test.sh pass; bin/fm-lint.sh clean.
  • Replay of real transcripts, python3 -I bin/fm-context-replay.py 2026-10-24,2026-10-25,2026-10-26 --limit <N> with the measured fresh-session start of 78000 tokens (median first-call context of real second-mate sessions; an earlier 20k start was wrong and made 100k look better than it is):
limit   category            calls  ctx/call before  after   weighted M before  after  saved  handovers
100k    second-mate homes    1646       130816      75202        41.3          39.2    5.2%      72
100k    main supervisor      1777       155460      78698        38.0          30.8   19.0%      66
150k    second-mate homes    1646       130816      93641        41.3          37.1   10.1%      20   <- shipped default
150k    main supervisor      1777       155460      95660        38.0          29.2   23.1%      19   (main not shipped here)
  • 26 Oct alone, second-mate homes: 100k = 8.2% MORE tokens, 120k = 1.2% saved, 150k = 5.5% saved, 200k = 4.7% saved. So the default is 150k. The replay is a best case: it ignores waiting for an idle moment.

  • Local results after rebasing onto origin/main (PRs 52-55 merged), no final no-mistakes run, no GitHub checks (captain instruction): tests/fm-context-replay.test.sh and the full tests/fm-secondmate-fresh-session.test.sh (T1-T9) pass; bin/fm-lint.sh clean; bin/fm-fork-footprint.sh --base origin/main --upstream upstream/main: 3 upstream files touched (+167/-3), all Fork-only block or on the fork ledger, Result: ok.

Fork footprint vs upstream main

  • Upstream files touched by this change: 0 (+0/-0 lines vs upstream)
  • Result: ok

@uayyagari
uayyagari marked this pull request as draft October 26, 2026 18:00
@uayyagari
uayyagari marked this pull request as ready for review October 27, 2026 10:56
A mate that keeps taking wakes without finishing a piece of work never asked
for a fresh session, so every call re-read an ever longer conversation. The
watcher sweep now reads each local Claude mate's transcript and files the same
handover request once it passes FM_SECONDMATE_CONTEXT_LIMIT (default 100000).
The existing guards still decide when the restart happens.
A fresh second-mate session starts near 78k tokens of context, not 20k. With
that start a 100k limit costs 8.2% more on 26 Oct transcripts; 150k saves 6.0%
for second mates and 25.4% for the main supervisor. The restart prompt now
says the context limit caused it, and the replay takes --homes and warns when
it skips old transcripts.
@uayyagari
uayyagari force-pushed the fm/qwr-supervisor-context branch from 17cbad2 to 59fe78f Compare October 27, 2026 10:59
@uayyagari
uayyagari merged commit cfc7f28 into main Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant