zelong77 found that Claude Code Auto's permission check left several
keepalive tasks running for one check. keepaliveKey hashed the first user
message, and the check sends the whole history in that message, so every
check got a new key. stopKeepalive(newKey) never stopped the old task,
and each reply started another one.
- The key is now the session, the Claude Code agent id and a hash of the
instructions and the tools' names. The first user message is left out
when the agent names a session. The lead, its permission check and a
subagent still get keys of their own, because their instructions,
tools or agent id differ.
- When a request names no session, the first message still tells two
conversations apart. A first message that grew from one seen before
(blocks appended, or text added to the last block) maps to the earlier
key (firstOf). firstOf keeps only hashes, of 256 conversations at most.
- README: what a conversation is.
Run on mote (Bun 1.4.2, HOME=$(mktemp -d)) at this tree, on main 2595d48:
- packages/devin `bun test`: 186 pass, 0 fail.
- The new keepalive tests on main's index.mjs: 4 fail (18 pass, 4 fail).
These are zelong77's repro, with and without session headers, plus a
grown last text and the stay-apart case.
- Whole repo `bun test`: 4741 pass, 12 skip, 0 fail.
`bun scripts/check.mjs`: exit 0.
Not tried: a real Devin account, or a real Claude Code Auto session. The
tests use zelong77's offline requests with fake timers and no network.
A request with no session whose first message starts with another
conversation's whole first message is taken as that conversation.
zelong77 asked for two things once 0.1.30's keep-alive worked: the pings
were invisible in magpie, and the interval and ping count could only be
edited in plugins.json.
- Each ping that wasn't cut short is reported through magpie.usage (new
in magpie, plugins#592) with kind cache_keepalive, the conversation's
model (chat.model) and account (magpie.account at request time), its
input, output, cache read and write tokens, status, time, and why it
failed. The ping still goes straight to Devin, never back through the
gateway: the gateway re-shapes a request, and the cache would no
longer match. An older magpie or OpenCode has no magpie.usage and is
told nothing; the pings go on the same.
- pingOnce now also reads output tokens (usage field 3) and keeps the
failure's message.
- package.json magpie.options gives the two settings' defaults
(285 s, 6), which magpie's Plugins › Options editor now starts from
for a provider plugin. The switch's description says where they are
and that pings show in Usage, in all five languages, each under the
host's 300-character cap.
Run (on mote, sandbox HOME): bun test in packages/devin, 174 pass;
bun scripts/check.mjs devin ok.
Broken by hand: reporting aborted pings too fails "a ping cut short by
the conversation's next request isn't told"; dropping the report call
fails "each ping is told to magpie".
Not tried: a real Devin account's ping reported into a real magpie.
Devin's prompt cache lasts 5 minutes and has no longer TTL to ask for.
A switch, "Keep the cache warm" (cache_keepalive, off by default, with
zh, zh-TW, ja and de labels), keeps it as the CLI's cache_keepalive.rs
does: after a reply that read or wrote the cache, whatever it stopped
for, the same GetChatMessage goes again every 285 s with one more user
prompt, "continue", and max_tokens 1. The request's bytes are encoded
once, and the ping is them with that prompt and field 8's max_tokens.
- A conversation is session + Claude Code agent id (when the headers
reach the plugin) + a hash of the instructions, tool names and first
user message; its next real request stops its pings, so a subagent
never stops its lead's.
- Stops on 401, 402, 429, two pings in a row reading under half their
prompt from the cache, the most pings (6), the switch going off, and
process exit. Timers are unref'd. At most 32 conversations are kept.
- Options cacheKeepaliveInterval (30-299 s) and cacheKeepaliveMaxPings
(1-50); OpenCode, which has no switch, takes cacheKeepalive: true.
- README says the cost (each ping is a cache read of the whole context,
spent whether the user comes back or not) and why it is off.
Run (mote, HOME=$(mktemp -d)):
- bun test packages/devin: 170 pass, 0 fail (14 new in keepalive.test.mjs).
- Whole repo bun test: 4520 pass, 9 skip, 1 fail: gcp-ai's "a lock held
by another process is waited for, then taken" (ENOENT in abandoned()'s
stat, a race in lib/lock.mjs; passes 3 of 3 alone), filed as #594.
- bun scripts/check.mjs: ok.
- The new tests against the old index.mjs: 14 of 14 fail.
Broken by hand, each red in the test named for it: no cancel on the next
request, no agent id in the key, no prefix hash, no stop on 401/402/429,
no miss count, no cap, no unref, max_tokens kept, pings after a reply
that touched no cache.
Real account (the owner's Devin sign-in read, nothing written, sandbox
HOME, this Mac): claude-sonnet-5-5-low, an 11k-token prompt wrote 10,984
to the cache; one ping 287 s later read 10,984 from it (cacheWrite 3,
output 1) and, at max 1, stopped.
Not tried: more than one ping or a long Claude Code session; the OpenCode
2 (v2) path, which has no keepalive. magpie doesn't pass
x-claude-code-session-id / x-claude-code-agent-id to a plugin's fetch, so
under magpie the key rests on the prefix hash (a lead and a subagent
differ there; two sessions with the same prefix share one keepalive).