From 581ca13db64ca9671810413a783b9b044c15ed5e Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 2 Nov 2026 14:44:55 +0800 Subject: [PATCH] devin 0.1.33: a permission check whose history grew replaces its keepalive pings (#620) 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. --- packages/devin/README.md | 8 +++- packages/devin/index.mjs | 58 +++++++++++++++++++---- packages/devin/keepalive.test.mjs | 76 +++++++++++++++++++++++++++++++ packages/devin/package.json | 2 +- 4 files changed, 133 insertions(+), 11 deletions(-) diff --git a/packages/devin/README.md b/packages/devin/README.md index b86f43f..a6cd366 100644 --- a/packages/devin/README.md +++ b/packages/devin/README.md @@ -104,8 +104,12 @@ the reply, not only while a tool or subagent works. - The conversation's next real request stops its pings; its own reply starts them again. A conversation is the session the agent names, the Claude Code subagent (`x-claude-code-agent-id`) and a hash of the - instructions, the tools' names and the first user message, so a - subagent's requests never stop its lead's pings. + instructions and the tools' names, so a subagent's or a permission + check's requests never stop its lead's pings. The first user message is + left out, as Claude Code Auto's permission check grows it with the + history on every check (#620). A request that names no session is also + told apart by its first message, where one that grew from a message + seen before is still that conversation. - They stop on a 401, 402 or 429, after two pings in a row that read less than half their prompt from the cache, at the most pings (6), when the switch goes off, and with the plugin host's process. The timers never diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index 93d14bc..4452df9 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -2188,10 +2188,19 @@ const textsOf = (c) => (typeof c === "string" ? [c] : Array.isArray(c) ? c.map(( // keepaliveKey is the conversation a request is of: the session the agent // names, the Claude Code subagent it is of (x-claude-code-agent-id, never -// sent for the lead's own turns), and a hash of the prefix every turn -// shares: the instructions, the tools' names and the first user message. -// A subagent's prefix is its own, so its requests never stop its lead's -// pings, nor the lead's its. +// sent for the lead's own turns), and a hash of what every turn of it +// shares: the instructions and the tools' names. A subagent's, or Claude +// Code's permission check's, instructions and tools are their own, so their +// requests never stop the lead's pings, nor the lead's theirs. +// +// The first user message is not part of it: Claude Code Auto's permission +// check sends the whole history in its first message, which grows on every +// check, and each check's key was then new, so a check left its older +// pings running beside its new ones (plugins#620, zelong77). Only a +// request that names no session is told apart by its first message too, +// as two conversations could share the rest; there, a first message that +// grew from one seen before (blocks added, or the last one's text added +// to) is still that conversation (firstOf). function keepaliveKey(req, messages, headers) { let h try { @@ -2201,16 +2210,49 @@ function keepaliveKey(req, messages, headers) { } const session = SESSION_HEADERS.map((k) => h.get(k)?.trim()).find(Boolean) ?? "" const agent = h.get("x-claude-code-agent-id")?.trim() ?? "" - let prefix + let prefix, first if (messages) { const system = textsOf(req.system).map((t) => t.replace(BILLING_LINE, "")) - prefix = [system, (req.tools ?? []).map((t) => t?.name ?? ""), textsOf(req.messages?.[0]?.content)] + prefix = [system, (req.tools ?? []).map((t) => t?.name ?? "")] + first = textsOf(req.messages?.[0]?.content) } else { const ms = req.messages ?? [] const system = ms.filter((m) => m?.role === "system" || m?.role === "developer").map((m) => textsOf(m.content)) - prefix = [system, (req.tools ?? []).map((t) => t?.function?.name ?? ""), textsOf(ms.find((m) => m?.role === "user")?.content)] + prefix = [system, (req.tools ?? []).map((t) => t?.function?.name ?? "")] + first = textsOf(ms.find((m) => m?.role === "user")?.content) } - return [session, agent, createHash("sha256").update(JSON.stringify(prefix)).digest("hex").slice(0, 32)].join("\n") + const group = [session, agent, sha(JSON.stringify(prefix))].join("\n") + if (session) return group + return firstOf(group, first) +} + +const sha = (s) => createHash("sha256").update(s).digest("hex").slice(0, 32) + +// firsts are the first messages of the conversations seen without a +// session, by key: each block's hash but the last's, and the last's length +// and hash, so a grown message is told without holding its text +const firsts = new Map() // key → {group, n, blocks: [hash], last: {len, hash}} +const FIRSTS_KEPT = 256 + +// firstOf is the key of the conversation, in group, whose first message +// first is, or grew from (the longest such one), else a new key. +function firstOf(group, first) { + const hashes = first.map(sha) + let best, most + for (const [id, f] of firsts) { + if (f.group !== group || f.n > first.length || (f.n === 0) !== (first.length === 0)) continue + if (f.n > 0) { + const t = first[f.n - 1] + if (f.blocks.some((x, j) => x !== hashes[j]) || t.length < f.last.len || sha(t.slice(0, f.last.len)) !== f.last.hash) continue + } + if (!best || f.n > most.n || (f.n === most.n && f.last.len > most.last.len)) (best = id), (most = f) + } + const id = best ?? [group, sha(JSON.stringify(first))].join("\n") + const t = first.at(-1) ?? "" + firsts.delete(id) + firsts.set(id, { group, n: first.length, blocks: hashes.slice(0, -1), last: { len: t.length, hash: sha(t) } }) + while (firsts.size > FIRSTS_KEPT) firsts.delete(firsts.keys().next().value) + return id } // stopKeepalive stops a conversation's pings, one in flight too. diff --git a/packages/devin/keepalive.test.mjs b/packages/devin/keepalive.test.mjs index 506c347..429fd6e 100644 --- a/packages/devin/keepalive.test.mjs +++ b/packages/devin/keepalive.test.mjs @@ -354,3 +354,79 @@ test("package.json gives magpie's Options editor the two settings' defaults", as const { interval, max } = _internal.keepaliveSettings({}) expect(pkg.magpie.options).toEqual({ cacheKeepaliveInterval: interval / 1000, cacheKeepaliveMaxPings: max }) }) + +// plugins#620 (zelong77): Claude Code Auto's permission check sends the +// history in its first user message, which grows on every check. Each +// check's key was new, so its older pings were never stopped and piled up. +// zelong77's offline repro, with its literal requests. +const check = { + model: "claude-sonnet-5-5", + max_tokens: 16, + system: [{ type: "text", text: "Permission classifier" }], + messages: [ + { role: "user", content: [{ type: "text", text: "History A", cache_control: { type: "ephemeral" } }] }, + { role: "user", content: [{ type: "text", text: "Evaluate operation" }] }, + ], +} +const grown = (req, text) => { + const b = structuredClone(req) + b.messages[0].content.push({ type: "text", text, cache_control: { type: "ephemeral" } }) + return b +} +const arrive = (req, headers) => { + const id = keepaliveKey(req, true, headers) + _internal.stopKeepalive(id) + const msg = buildMessages(req, "claude-sonnet-5-5", "fake-key") + _internal.scheduleKeepalive({ id, interval: 285000, max: 6 }, "fake-key", "https://example.invalid", msg) + return id +} +const session = { "x-claude-code-session-id": "same-session", "x-claude-code-agent-id": "same-agent" } + +for (const [name, headers] of [["with no session named", {}], ["in one session and agent", session]]) { + test(`a permission check whose history grew replaces its pings, ${name} (plugins#620)`, () => { + globalThis.fetch = () => { + throw new Error("Network forbidden") + } + const oldKey = arrive(check, headers) + arrive(check, headers) + expect(_internal.keeping().length).toBe(1) + const b = grown(check, "History B") + expect(arrive(b, headers)).toBe(oldKey) + expect(_internal.keeping()).toEqual([oldKey]) + expect(clock.live().length).toBe(1) + // and again, from the grown one + expect(arrive(grown(b, "History C"), headers)).toBe(oldKey) + expect(clock.live().length).toBe(1) + }) +} + +test("a first message whose last text grew is the same conversation (plugins#620)", () => { + const a = { system: "S", messages: [{ role: "user", content: "History A" }] } + const b = { system: "S", messages: [{ role: "user", content: "History A, then B" }] } + const before = keepaliveKey(a, true, {}) + expect(keepaliveKey(b, true, {})).toBe(before) +}) + +test("conversations that really differ keep their own pings (plugins#620)", () => { + // no session named: another first message is another conversation + const a = { system: "S", messages: [{ role: "user", content: [{ type: "text", text: "History A" }] }] } + const other = { system: "S", messages: [{ role: "user", content: [{ type: "text", text: "Something else" }] }] } + const aKey = keepaliveKey(a, true, {}) + expect(keepaliveKey(other, true, {})).not.toBe(aKey) + // one that only shares its first block's start isn't grown from it + const shorter = { system: "S", messages: [{ role: "user", content: [{ type: "text", text: "History" }, { type: "text", text: "x" }] }] } + expect(keepaliveKey(shorter, true, {})).not.toBe(aKey) + // in one session: the lead, its permission check and a subagent + const s = { "x-claude-code-session-id": "s1" } + const leadReq = { system: "You are Claude Code.", tools: [{ name: "Bash" }], messages: [{ role: "user", content: "fix the build" }] } + const keys = new Set([keepaliveKey(leadReq, true, s), keepaliveKey(check, true, s), keepaliveKey(leadReq, true, { ...s, "x-claude-code-agent-id": "a1" })]) + expect(keys.size).toBe(3) + // and a grown check stops only the check's pings + globalThis.fetch = () => { + throw new Error("Network forbidden") + } + arrive(leadReq, s) + arrive(check, s) + arrive(grown(check, "History B"), s) + expect(_internal.keeping().length).toBe(2) +}) diff --git a/packages/devin/package.json b/packages/devin/package.json index f5bfdba..12fe8bb 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.32", + "version": "0.1.33", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs",