claudeRoom bounded how many Claude Code processes run, not how large each one is. A run is kept for its conversation's next turn, turn after turn, and on a service never restarted one grew for days: a node-MainThread in magpie.service reached 8 GiB RSS on a 31 GiB host and the OOM killer took the machine down (#3256). Now each time a run's reply ends, its process tree's resident memory is read (proc.Tree.RSS: /proc VmRSS on Linux, ps on macOS, the working sets of its job's processes on Windows, so an npm claude.cmd's node counts) and logged with its pid. Over the budget (an eighth of memLimit on Linux, of the computer's memory elsewhere, 1 GiB at least, 4 GiB if unknown), a whole turn's run is not kept idle: its session is shelved and the next turn resumes it in a new Claude Code with --resume, told only the new messages. A run parked on tool calls is let go past 1.5x the budget (no session to shelve there; its results start a new run). Each Claude Code also logs pid and model at start, and run time, how it ended and peak RSS (ru_maxrss) at its end, so an OOM pid can be found in magpie's log. Budget on the reporter's 31 GiB host: 3.9 GiB. Run (worktree at99de8e1ad+ this change, macOS arm64): - go test -timeout 45m ./internal/gateway -run 'Claude|Subscription|Room|Park|Idle|Resume' -count=1: ok (60.5s). - go test ./internal/proc -count=1: every test ok except TestNoCommandBypassesProc, red on origin/main since75e78a5a0(internal/tui/keep.go, filed as #3567, not this change). - New tests: TestClaudeOversizedRunIsLetGoAndResumes (injected RSS over budget: the run ends, the next turn runs --resume s<pid> and is not told a1 again), TestClaudeRunWithinBudgetIsKept, TestClaudeOversizedParkedRunIsLetGo, TestClaudeRunBudgetScalesWithMemory (31 GiB MemTotal -> an eighth; 2 GiB -> the 1 GiB floor), TestTreeRSS (a real sleep's RSS read; none once waited for). - Broken by hand: with both checks turned off, the oversized idle and parked tests go red ("the oversized run to end", "a run over its budget was kept waiting on its tool calls"). - Real Claude Code 2.1.310 in a sandbox HOME and CLAUDE_CONFIG_DIR, stream-json like the bridge, against DeepSeek's Anthropic endpoint (deepseek-chat, real key, synthetic 40KB turns): RSS after turns 1/10/20/30 = 192/244/223/233 MiB, peak 245 MiB over 30 turns and ~1.2 MB of conversation. oversized() on that real process with the budget 1 MiB under its RSS said over; with the real budget on this 64 GiB Mac (8192 MiB) it said keep. - gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/proc ./internal/gateway clean. Not tried: a real Claude subscription run through the bridge on Linux until it crosses the budget (the retire path is shown with injected RSS on the fake Claude Code, and the RSS read on a real one); the Windows RSS reader and job listing on the Windows box (vet only); a Linux run of the /proc reader (macOS reads through ps); the full ./internal/gateway package.
117 lines
3.8 KiB
Go
117 lines
3.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"runtime"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// A Claude Code run is kept for its conversation's next turn, turn after
|
|
// turn, for days on a service that is never restarted, and nothing bounded
|
|
// what one process grew to: claudeRoom bounds how many run, not how large
|
|
// each is. On a 31 GiB host one reached 8 GiB resident after four days of
|
|
// a large conversation, and the kernel's OOM killer took the machine's
|
|
// other work with it (#3256). Once a turn is over, a run holding more than
|
|
// claudeRunBudget is let go instead of kept: its session is shelved, and
|
|
// the next turn resumes it in a new Claude Code, which asks Anthropic the
|
|
// same cached prefix.
|
|
//
|
|
// The budget is an eighth of what magpie's processes may use (memLimit on
|
|
// Linux, the computer's memory elsewhere), claudeRunFloor at least; where
|
|
// neither is known, claudeRunUnknown. A Claude Code holds ~100 MiB of its
|
|
// own at the start (claudeRoom).
|
|
const (
|
|
claudeRunFloor = 1 << 30
|
|
claudeRunUnknown = 4 << 30
|
|
)
|
|
|
|
// claudeRunRSS reads a run's resident memory, for tests to give one.
|
|
var claudeRunRSS = func(t *proc.Tree) (int64, bool) { return t.RSS() }
|
|
|
|
var runBudget struct {
|
|
sync.Once
|
|
n int64
|
|
why string
|
|
}
|
|
|
|
// claudeRunBudget is the most a Claude Code run may hold resident and
|
|
// still be kept for its next turn, and what sized it.
|
|
func claudeRunBudget() (int64, string) {
|
|
runBudget.Do(func() {
|
|
limit, why := memLimit(memGOOS, memRoot)
|
|
if limit <= 0 && memGOOS == runtime.GOOS {
|
|
limit, why = proc.MemTotal(), "the computer's memory"
|
|
}
|
|
if limit <= 0 {
|
|
runBudget.n, runBudget.why = claudeRunUnknown, "memory not known"
|
|
return
|
|
}
|
|
runBudget.n = max(limit/8, claudeRunFloor)
|
|
runBudget.why = fmt.Sprintf("an eighth of %d MiB (%s), %d MiB at least", limit>>20, why, claudeRunFloor>>20)
|
|
})
|
|
return runBudget.n, runBudget.why
|
|
}
|
|
|
|
// oversized says the run holds more than claudeRunBudget times share/2
|
|
// (2 a whole turn, 3 a run that waits on tool calls), and logs what it
|
|
// holds either way: a run's growth across turns is in the log, for an OOM
|
|
// to be put down to the process it was. One waiting on its caller's tool
|
|
// results has no session to shelve there: let go, it is started anew with
|
|
// the whole conversation, which Anthropic's cache has not seen, so it is
|
|
// let go only past half as much again.
|
|
func (r *subscriptionRun) oversized(share int64) (string, bool) {
|
|
r.mu.Lock()
|
|
t := r.tree
|
|
r.mu.Unlock()
|
|
if t == nil {
|
|
return "", false
|
|
}
|
|
rss, ok := claudeRunRSS(t)
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
budget, why := claudeRunBudget()
|
|
budget = budget * share / 2
|
|
log.Printf("claude: Claude Code pid %d (%s) holds %d MiB after its turn; %d MiB may be kept (%s)", r.pid(), r.model, rss>>20, budget>>20, why)
|
|
if rss <= budget {
|
|
return "", false
|
|
}
|
|
return fmt.Sprintf("its Claude Code held %d MiB, over the %d MiB a run may hold to be kept (%s)", rss>>20, budget>>20, why), true
|
|
}
|
|
|
|
// pid is the run's Claude Code process id, 0 before it starts.
|
|
func (r *subscriptionRun) pid() int {
|
|
if r.cmd == nil || r.cmd.Process == nil {
|
|
return 0
|
|
}
|
|
return r.cmd.Process.Pid
|
|
}
|
|
|
|
// letGoOversized lets go of a run whose turn was whole and that holds too
|
|
// much to be kept (oversized): its session is shelved at key, as one let go
|
|
// past idleMost is, so the next turn resumes it in a new Claude Code.
|
|
func (b *subscriptionBridge) letGoOversized(r *subscriptionRun, key, conv, why string) {
|
|
b.mu.Lock()
|
|
r.mu.Lock()
|
|
closed := r.closed
|
|
r.mu.Unlock()
|
|
if !closed {
|
|
if r.idleKey != "" && b.idle[r.idleKey] == r {
|
|
delete(b.idle, r.idleKey)
|
|
}
|
|
r.idleKey, r.idleAt = key, time.Now()
|
|
if conv != "" {
|
|
r.convKey = conv
|
|
}
|
|
b.shelve(r)
|
|
r.idleKey = ""
|
|
}
|
|
b.mu.Unlock()
|
|
log.Printf("claude: Claude Code pid %d (%s) is let go: %s; its conversation's next turn resumes its session in a new one", r.pid(), r.model, why)
|
|
r.abortFor(why)
|
|
}
|