devin 0.1.34: keepalive pings timed from when the request was sent; magpie's session header keys them (#626, #620)
#626 (zelong77): keptAfter scheduled the first ping 285 s after the
reply's stop event, and run() scheduled each next one 285 s after the
ping before had come back. Devin renews its cache when it takes a
request, so a 33-47 s Fable reply or a 10-20 s ping put the next ping
past the 5 minutes. In zelong77's usage.jsonl every ping 315.9 s or more
after its request was sent missed and wrote ~500k tokens again.
- roundtrip stamps sentAt before it sends; scheduleKeepalive waits
max(0, interval - (now - sentAt)), and each ping is timed from when it
was sent. A reply or ping longer than the interval is followed at once.
- The timers object carries the clock (now), so tests hold it.
#620, after 0.1.33 (zelong77, 08:18): through magpie, check A and the
grown check B still kept separate pings. magpie hands a plugin's fetch
none of the agent's own headers, so x-claude-code-session-id never
reached keepaliveKey, and magpie's translation merges Claude Code's two
user messages into one whose history grows in the middle, which firstOf
can't follow.
- keepaliveKey reads x-magpie-session first and x-magpie-subagent beside
x-claude-code-agent-id. magpie sets both on a plugin's fetch in a
release that follows this one; the host strips them from what a plugin
sends on.
- README: the timing, and the headers.
Run on mote (Bun 1.4.2, HOME=$(mktemp -d)) at this tree, on main 63cd0e4:
- packages/devin `bun test`: 189 pass, 0 fail.
- The 3 new keepalive tests on main's index.mjs: 3 fail (22 pass). The
two timing tests got 285000 where 238200 and 0 were due; the merged
check A's timer was never cleared.
- Whole repo `bun test`: 4781 pass, 0 fail. `bun scripts/check.mjs
devin`: ok.
Not tried: a real Devin account or a real Claude Code session; fake
timers and a fake Devin only. Without any session header (not Claude
Code's case once magpie sends x-magpie-session), a merged check whose
history grows in the middle still gets a new key: matching on a shared
leading block would merge different conversations that start with the
same reminder, which costs a full rewrite rather than an extra read.
This commit is contained in:
@@ -101,9 +101,17 @@ more user prompt, `continue`, and `max_tokens` 1. Each one reads the cache
|
||||
and renews it. Unlike the CLI's, it also runs while the user is reading
|
||||
the reply, not only while a tool or subagent works.
|
||||
|
||||
Each ping goes 285 seconds after the request before it was *sent* (the
|
||||
real one, or the ping before), not after its reply ended: Devin renews the
|
||||
cache when it takes a request, so a 30–47 s reply or a slow ping would
|
||||
otherwise put the next ping past the 5 minutes (#626). A reply or ping
|
||||
that took longer than the interval is followed by a ping at once.
|
||||
|
||||
- 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
|
||||
starts them again. A conversation is the session the agent names (in
|
||||
magpie, `x-magpie-session`, as the agent's own headers don't reach the
|
||||
plugin), the Claude Code subagent (`x-claude-code-agent-id`, in magpie
|
||||
`x-magpie-subagent`) and a hash of the
|
||||
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
|
||||
|
||||
+36
-17
@@ -1773,6 +1773,9 @@ async function roundtrip({ key, server, families, keep }, chat, signal, wire = C
|
||||
const uid = variantFor(families, chat.model, wire.effort(chat))
|
||||
const msg = wire.build(chat, uid, key)
|
||||
const body = frame(msg)
|
||||
// when Devin was asked, which its cache's 5 minutes run from: the pings
|
||||
// are timed from it, not from the reply's end (plugins#626)
|
||||
const sentAt = now()
|
||||
let res
|
||||
try {
|
||||
res = await fetch(server + CHAT_RPC, {
|
||||
@@ -1808,7 +1811,7 @@ async function roundtrip({ key, server, families, keep }, chat, signal, wire = C
|
||||
return head.value.error
|
||||
}
|
||||
const it = prepend(head.value, source)
|
||||
return { it: keep ? keptAfter(it, keep, { key, server, msg }) : it }
|
||||
return { it: keep ? keptAfter(it, keep, { key, server, msg, sentAt }) : it }
|
||||
}
|
||||
|
||||
// complete answers a chat completion through GetChatMessage.
|
||||
@@ -2160,10 +2163,12 @@ const KEEPALIVE_MAX = 6
|
||||
const KEEPALIVE_KEPT = 32 // conversations kept warm at once; past it the oldest stops
|
||||
const KEEPALIVE_STOP = new Set([401, 402, 429])
|
||||
|
||||
// timers are what the pings wait on; tests hand in their own. A timer is
|
||||
// unref'd: it never keeps a process or a test running.
|
||||
const realTimers = { set: (fn, ms) => setTimeout(fn, ms), clear: (t) => clearTimeout(t) }
|
||||
// timers are what the pings wait on, and the clock they are timed by;
|
||||
// tests hand in their own. A timer is unref'd: it never keeps a process or
|
||||
// a test running.
|
||||
const realTimers = { set: (fn, ms) => setTimeout(fn, ms), clear: (t) => clearTimeout(t), now: () => Date.now() }
|
||||
let timers = realTimers
|
||||
const now = () => (timers.now ?? Date.now)()
|
||||
|
||||
const keeps = new Map() // conversation → {n, misses, timer, ctl}
|
||||
|
||||
@@ -2179,8 +2184,11 @@ function keepaliveSettings(options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
// the headers an agent names its session in, as magpie's gateway reads them
|
||||
const SESSION_HEADERS = ["x-claude-code-session-id", "x-session-id", "session_id", "session-id", "x-opencode-session", "x-session-affinity", "x-conversation-id"]
|
||||
// the headers an agent names its session in, as magpie's gateway reads them;
|
||||
// first magpie's own, which tells a plugin's fetch the session the agent
|
||||
// named to magpie (the agent's own headers never reach the plugin through
|
||||
// magpie: plugins#620)
|
||||
const SESSION_HEADERS = ["x-magpie-session", "x-claude-code-session-id", "x-session-id", "session_id", "session-id", "x-opencode-session", "x-session-affinity", "x-conversation-id"]
|
||||
|
||||
// textsOf is a message's or system's words, without the cache marks a
|
||||
// client moves forward as the history grows
|
||||
@@ -2209,7 +2217,9 @@ function keepaliveKey(req, messages, headers) {
|
||||
h = new Headers()
|
||||
}
|
||||
const session = SESSION_HEADERS.map((k) => h.get(k)?.trim()).find(Boolean) ?? ""
|
||||
const agent = h.get("x-claude-code-agent-id")?.trim() ?? ""
|
||||
// the Claude Code subagent, as Claude Code names it, or as magpie tells
|
||||
// the plugin it (x-magpie-subagent)
|
||||
const agent = (h.get("x-claude-code-agent-id")?.trim() || h.get("x-magpie-subagent")?.trim()) ?? ""
|
||||
let prefix, first
|
||||
if (messages) {
|
||||
const system = textsOf(req.system).map((t) => t.replace(BILLING_LINE, ""))
|
||||
@@ -2327,8 +2337,17 @@ function reportPing(keep, r, ms) {
|
||||
}
|
||||
|
||||
// scheduleKeepalive starts a conversation's pings for the request msg
|
||||
// just answered, in place of any it had.
|
||||
function scheduleKeepalive(keep, key, server, msg) {
|
||||
// just answered, sent at sentAt, in place of any it had.
|
||||
//
|
||||
// Each ping is timed from when the request before it was sent, the real
|
||||
// one's or the ping's own, not from when it was answered: Devin renews its
|
||||
// cache when it takes a request, and a reply of 30 to 47 s (Fable over half
|
||||
// a million tokens), or a ping of 10 to 20 s, put the next ping past the
|
||||
// cache's 5 minutes when it waited its whole interval from the end. Pings
|
||||
// 315.9 s or more after the request they followed was sent missed, and
|
||||
// each wrote the whole context again (plugins#626, zelong77). One that
|
||||
// came back after its interval had passed is followed at once.
|
||||
function scheduleKeepalive(keep, key, server, msg, sentAt = now()) {
|
||||
const { id, interval, max } = keep
|
||||
const ping = keepalivePing(msg)
|
||||
if (!ping) return
|
||||
@@ -2338,8 +2357,8 @@ function scheduleKeepalive(keep, key, server, msg) {
|
||||
const k = { n: 0, misses: 0, timer: null, ctl: null }
|
||||
keeps.set(id, k)
|
||||
hookExit()
|
||||
const next = () => {
|
||||
k.timer = timers.set(run, interval)
|
||||
const next = (from) => {
|
||||
k.timer = timers.set(run, Math.max(0, interval - Math.max(0, now() - from)))
|
||||
k.timer?.unref?.()
|
||||
}
|
||||
const run = async () => {
|
||||
@@ -2347,28 +2366,28 @@ function scheduleKeepalive(keep, key, server, msg) {
|
||||
if (keeps.get(id) !== k) return
|
||||
k.n++
|
||||
const ctl = (k.ctl = new AbortController())
|
||||
const at = Date.now()
|
||||
const at = now()
|
||||
const r = await pingOnce(server, key, body, ctl.signal)
|
||||
k.ctl = null
|
||||
reportPing(keep, { ...r, aborted: ctl.signal.aborted }, Date.now() - at)
|
||||
reportPing(keep, { ...r, aborted: ctl.signal.aborted }, now() - at)
|
||||
if (keeps.get(id) !== k) return // a real request came, or the switch went off
|
||||
if (KEEPALIVE_STOP.has(r.status)) return stopKeepalive(id)
|
||||
const t = r.tokens
|
||||
const hit = r.status === 200 && t.cacheRead > 0 && t.cacheRead * 2 >= t.input + t.cacheRead + t.cacheWrite
|
||||
k.misses = hit ? 0 : k.misses + 1
|
||||
if (k.misses >= 2 || k.n >= max) return stopKeepalive(id)
|
||||
next()
|
||||
next(at)
|
||||
}
|
||||
next()
|
||||
next(sentAt)
|
||||
}
|
||||
|
||||
// keptAfter passes a reply's events on, and once it has finished (any stop:
|
||||
// a tool call waiting too) schedules the conversation's pings, when the
|
||||
// reply read or wrote the cache: a request with nothing cached has nothing
|
||||
// to keep.
|
||||
async function* keptAfter(it, keep, { key, server, msg }) {
|
||||
async function* keptAfter(it, keep, { key, server, msg, sentAt }) {
|
||||
for await (const e of it) {
|
||||
if (e.stop && (e.tokens?.cacheRead || e.tokens?.cacheWrite)) scheduleKeepalive(keep, key, server, msg)
|
||||
if (e.stop && (e.tokens?.cacheRead || e.tokens?.cacheWrite)) scheduleKeepalive(keep, key, server, msg, sentAt)
|
||||
yield e
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,12 @@ const configs = new Uint8Array(readFileSync(join(import.meta.dir, "testdata", "m
|
||||
|
||||
// fake timers: each set is held until the test fires it
|
||||
let clock
|
||||
// and a clock (now), which a timer's firing moves on to its time
|
||||
const fakeTimers = () => {
|
||||
const q = []
|
||||
return {
|
||||
const c = {
|
||||
t: 1_000_000,
|
||||
now: () => c.t,
|
||||
q,
|
||||
set(fn, ms) {
|
||||
const t = { fn, ms, cleared: false, fired: false, unrefd: false, unref: () => (t.unrefd = true) }
|
||||
@@ -36,9 +39,11 @@ const fakeTimers = () => {
|
||||
const [t] = q.filter((t) => !t.cleared && !t.fired)
|
||||
if (!t) throw new Error("no timer to fire")
|
||||
t.fired = true
|
||||
c.t += t.ms
|
||||
await t.fn()
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
beforeEach(() => {
|
||||
clock = fakeTimers()
|
||||
@@ -430,3 +435,95 @@ test("conversations that really differ keep their own pings (plugins#620)", () =
|
||||
arrive(grown(check, "History B"), s)
|
||||
expect(_internal.keeping().length).toBe(2)
|
||||
})
|
||||
|
||||
// plugins#626 (zelong77): the pings were timed from the reply's end, and
|
||||
// each from the end of the ping before. Devin renews its cache when it
|
||||
// takes a request, so a long reply or a slow ping put the next ping past
|
||||
// the cache's 5 minutes: 285 s after a 33.5 s reply was 318.5 s after the
|
||||
// request, a miss that wrote 503,797 tokens again. Each is timed from when
|
||||
// the request before it was sent.
|
||||
test("the first ping is 285 s after the request was sent, not after its reply (plugins#626)", async () => {
|
||||
devin()
|
||||
const ask = await plugin(true)
|
||||
// zelong77's slowest reply, 46.8 s
|
||||
real = () => ((clock.t += 46_800), answer({ write: 200, read: 9000 }))
|
||||
await ask(lead())
|
||||
expect(clock.live().map((t) => t.ms)).toEqual([285_000 - 46_800])
|
||||
// a ping that took 15 s: the next goes 285 s after it was sent
|
||||
pings.push(() => ((clock.t += 15_000), answer({ input: 3, read: 9200 })))
|
||||
const sentAt = clock.t + clock.live()[0].ms
|
||||
await clock.fire()
|
||||
expect(pingsSent()).toBe(1)
|
||||
expect(clock.live().map((t) => t.ms)).toEqual([285_000 - 15_000])
|
||||
expect(sentAt + 285_000).toBe(clock.t + clock.live()[0].ms)
|
||||
// a quick one waits its whole interval, less what it took
|
||||
pings.push(() => answer({ input: 3, read: 9200 }))
|
||||
await clock.fire()
|
||||
expect(clock.live().map((t) => t.ms)).toEqual([285_000])
|
||||
})
|
||||
|
||||
test("a reply that took longer than the interval is followed by a ping at once (plugins#626)", async () => {
|
||||
devin()
|
||||
const ask = await plugin(true)
|
||||
real = () => ((clock.t += 300_000), answer({ write: 200, read: 9000 }))
|
||||
await ask(lead())
|
||||
expect(clock.live().map((t) => t.ms)).toEqual([0])
|
||||
// and a ping that took longer than the interval too
|
||||
pings.push(() => ((clock.t += 290_000), answer({ input: 3, read: 9200 })))
|
||||
await clock.fire()
|
||||
expect(clock.live().map((t) => t.ms)).toEqual([0])
|
||||
})
|
||||
|
||||
// plugins#620, after 0.1.33 (zelong77, 11-02): magpie hands a plugin's fetch
|
||||
// none of the agent's own headers, so x-claude-code-session-id never
|
||||
// reached the key, and magpie's translation merges Claude Code's two user
|
||||
// messages into one: [fixed instructions, transcript…, current operation].
|
||||
// The transcript grows in the middle of it, which firstOf can't follow, so
|
||||
// checks A and B kept separate pings. magpie now names the session the
|
||||
// agent named (x-magpie-session) and its Claude Code subagent
|
||||
// (x-magpie-subagent) to the plugin.
|
||||
const merged = (history, op) => ({
|
||||
model: "claude-haiku-5-5",
|
||||
max_tokens: 64,
|
||||
system: [{ type: "text", text: "Permission classifier" }],
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Fixed instructions", cache_control: { type: "ephemeral" } },
|
||||
...history.map((text) => ({ type: "text", text })),
|
||||
{ type: "text", text: "Current operation:" },
|
||||
{ type: "text", text: op },
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
const mainTurn = { model: "claude-haiku-5-5", max_tokens: 64, system: [{ type: "text", text: "You are Claude Code." }], messages: [{ role: "user", content: "fix the build" }] }
|
||||
|
||||
test("magpie's session header keys a permission check whose merged history grew in the middle (plugins#620)", async () => {
|
||||
devin()
|
||||
const hooks = await DevinAuthPlugin({ magpie: { option: (p, k) => p === "devin" && k === KEEPALIVE } })
|
||||
const { fetch } = await hooks.auth.loader(async () => auth)
|
||||
const ask = async (req, headers) => {
|
||||
const res = await fetch("https://server.codeium.com/devin/v1/messages", { method: "POST", headers, body: JSON.stringify(req) })
|
||||
expect(res.status).toBe(200)
|
||||
await res.text()
|
||||
}
|
||||
const s = { "x-magpie-session": "cc-session-1" }
|
||||
// check A, the main conversation C, then check B with the history grown
|
||||
await ask(merged(["Bash ls"], "Bash rm a"), s)
|
||||
const [a] = clock.live()
|
||||
await ask(mainTurn, s)
|
||||
const [, c] = clock.live()
|
||||
await ask(merged(["Bash ls", "Bash cat b"], "Bash rm b"), s)
|
||||
expect(a.cleared).toBe(true)
|
||||
expect(c.cleared).toBe(false)
|
||||
expect(clock.live().length).toBe(2)
|
||||
expect(_internal.keeping().length).toBe(2)
|
||||
// a subagent magpie names is a conversation of its own in that session
|
||||
await ask(mainTurn, { ...s, "x-magpie-subagent": "agent-1" })
|
||||
expect(c.cleared).toBe(false)
|
||||
expect(clock.live().length).toBe(3)
|
||||
// the same keys as Claude Code's own headers give
|
||||
expect(keepaliveKey(mainTurn, true, { "x-magpie-session": "x", "x-magpie-subagent": "a" })).toBe(keepaliveKey(mainTurn, true, { "x-claude-code-session-id": "x", "x-claude-code-agent-id": "a" }))
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-devin-auth",
|
||||
"version": "0.1.33",
|
||||
"version": "0.1.34",
|
||||
"description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user