devin 0.1.32: a system notice mid-conversation keeps its place and cache mark (#599)
buildMessages now sends a role:"system" notice partway through a conversation as a user prompt that keeps its cache mark and its place. Before, the notice was lost or merged away. A notice that arrives while tool calls are still waiting for results is held until those results are answered, so it never comes between a call and its result. A notice after an interrupted call goes after the missing-result placeholder. An empty notice is skipped.
By leosocy (#599), reviewed at 647039a.
Run on the Mac mini with Bun 1.4.2 and HOME=$(mktemp -d), on 647039a merged with main 1a05a77:
- `bun test packages/devin`: 182 pass, 0 fail.
- The new tests on main's index.mjs: 4 of cache-marks.test.mjs fail (13 pass, 4 fail), as they should.
- Whole repo `bun test` (with #606 and #596 merged in too): 4592 pass, 11 skip, 0 fail. `bun scripts/check.mjs`: exit 0.
Not tried: a real Devin account.
This commit is contained in:
@@ -67,6 +67,10 @@ The plugin's `fetch` handles this:
|
||||
`prompt_cache_options {type: EPHEMERAL}` (field 8), as the devin CLI
|
||||
marks its own. Blocks joined into one message lose the mark: a grown
|
||||
history then read only ~558 tokens from the cache.
|
||||
- Mid-conversation `role: "system"` notices stay in the history as user
|
||||
prompts, with their block boundaries and cache marks. A notice that
|
||||
arrives before pending tool results follows those results, as Devin
|
||||
requires; it does not replace them or join the stable system prefix.
|
||||
- Claude Code's `x-anthropic-billing-header` line isn't sent: its
|
||||
`cch` changes every turn.
|
||||
- The reply comes back as Anthropic's message or events, with the
|
||||
|
||||
@@ -74,6 +74,110 @@ test("a grown history keeps every prompt the last mark covered as it was sent (y
|
||||
expect([round2[3].text, round2[3].cache]).toEqual([history(50, 5), EPHEMERAL])
|
||||
})
|
||||
|
||||
test("a system notice after a tool result keeps its position and cache mark", () => {
|
||||
const ps = prompts(buildMessages({
|
||||
system: [{ type: "text", text: "Stable instructions", cache_control: mark }],
|
||||
messages: [
|
||||
{ role: "user", content: "Read calc.py." },
|
||||
{ role: "assistant", content: [{ type: "tool_use", id: "c1", name: "read_file", input: { path: "calc.py" } }] },
|
||||
{ role: "user", content: [{ type: "tool_result", tool_use_id: "c1", content: "return 42" }] },
|
||||
{ role: "system", content: [{ type: "text", text: "Run the focused tests next.", cache_control: mark }] },
|
||||
],
|
||||
}, "claude-opus-5-5", "k"))
|
||||
expect(ps.map((p) => [p.role, p.text, p.callID, p.calls, p.cache])).toEqual([
|
||||
[1, "Stable instructions", "", [], EPHEMERAL],
|
||||
[1, "Read calc.py.", "", [], null],
|
||||
[2, "", "", ["read_file"], null],
|
||||
[4, "return 42", "c1", [], null],
|
||||
[1, "Run the focused tests next.", "", [], EPHEMERAL],
|
||||
])
|
||||
})
|
||||
|
||||
test("a growing conversation moves its system notice's cache mark without changing the old prefix", () => {
|
||||
const first = [
|
||||
{ role: "user", content: "Inspect calc.py." },
|
||||
{ role: "system", content: [{ type: "text", text: "Keep changes focused." }, { type: "text", text: "Check the result." }] },
|
||||
]
|
||||
const round1 = prompts(buildMessages({ messages: [first[0], { ...first[1], content: [first[1].content[0], { ...first[1].content[1], cache_control: mark }] }] }, "claude-opus-5-5", "k"))
|
||||
expect(round1.map((p) => [p.role, p.text, p.cache])).toEqual([
|
||||
[1, "Inspect calc.py.", null],
|
||||
[1, "Keep changes focused.", null],
|
||||
[1, "Check the result.", EPHEMERAL],
|
||||
])
|
||||
const round2 = prompts(buildMessages({ messages: [
|
||||
...first,
|
||||
{ role: "assistant", content: [{ type: "text", text: "The result is correct." }] },
|
||||
{ role: "user", content: "Continue." },
|
||||
{ role: "system", content: [{ type: "text", text: "Run tests now.", cache_control: mark }] },
|
||||
] }, "claude-opus-5-5", "k"))
|
||||
expect(round2.map((p) => [p.role, p.text, p.cache])).toEqual([
|
||||
[1, "Inspect calc.py.", null],
|
||||
[1, "Keep changes focused.", null],
|
||||
[1, "Check the result.", null],
|
||||
[2, "The result is correct.", null],
|
||||
[1, "Continue.", null],
|
||||
[1, "Run tests now.", EPHEMERAL],
|
||||
])
|
||||
expect(round2.slice(0, round1.length).map(({ cache, ...p }) => p)).toEqual(round1.map(({ cache, ...p }) => p))
|
||||
})
|
||||
|
||||
test("system notices before tool results follow the real results without interrupting calls", () => {
|
||||
const ps = prompts(buildMessages({ messages: [
|
||||
{ role: "user", content: "Read calc.py." },
|
||||
{ role: "assistant", content: [{ type: "tool_use", id: "c1", name: "read_file", input: {} }, { type: "tool_use", id: "c2", name: "read_file", input: {} }] },
|
||||
{ role: "system", content: "The user asked to stop after this read." },
|
||||
{ role: "system", content: [{ type: "text", text: "Report both results.", cache_control: mark }] },
|
||||
{ role: "user", content: [{ type: "tool_result", tool_use_id: "c1", content: "return 42" }, { type: "tool_result", tool_use_id: "c2", content: "return 43" }] },
|
||||
] }, "claude-opus-5-5", "k"))
|
||||
expect(ps.map((p) => [p.role, p.text, p.callID, p.calls, p.cache])).toEqual([
|
||||
[1, "Read calc.py.", "", [], null],
|
||||
[2, "", "", ["read_file", "read_file"], null],
|
||||
[4, "return 42", "c1", [], null],
|
||||
[4, "return 43", "c2", [], null],
|
||||
[1, "The user asked to stop after this read.", "", [], null],
|
||||
[1, "Report both results.", "", [], EPHEMERAL],
|
||||
])
|
||||
})
|
||||
|
||||
test("empty system notices add no prompts or fallback turn", () => {
|
||||
const ps = prompts(buildMessages({ messages: [
|
||||
{ role: "user", content: "Continue." },
|
||||
{ role: "system", content: "" },
|
||||
{ role: "system", content: [], output_config: { effort: "low" } },
|
||||
] }, "claude-opus-5-5", "k"))
|
||||
expect(ps.map((p) => [p.role, p.text, p.cache])).toEqual([[1, "Continue.", null]])
|
||||
})
|
||||
|
||||
for (const [name, content] of [["empty string", ""], ["empty array", []], ["empty text block", [{ type: "text", text: "" }]]]) {
|
||||
test(`a system notice with ${name} does not split an assistant's tool-call turn`, () => {
|
||||
const ps = prompts(buildMessages({ messages: [
|
||||
{ role: "user", content: "Read both files." },
|
||||
{ role: "assistant", content: [{ type: "tool_use", id: "c1", name: "read_file", input: {} }] },
|
||||
{ role: "system", content },
|
||||
{ role: "assistant", content: [{ type: "tool_use", id: "c2", name: "read_file", input: {} }] },
|
||||
{ role: "user", content: [{ type: "tool_result", tool_use_id: "c1", content: "first" }, { type: "tool_result", tool_use_id: "c2", content: "second" }] },
|
||||
] }, "claude-opus-5-5", "k"))
|
||||
expect(ps.map((p) => [p.role, p.text, p.callID, p.calls])).toEqual([
|
||||
[1, "Read both files.", "", []],
|
||||
[2, "", "", ["read_file", "read_file"]],
|
||||
[4, "first", "c1", []],
|
||||
[4, "second", "c2", []],
|
||||
])
|
||||
})
|
||||
}
|
||||
|
||||
test("a notice after an interrupted tool call follows its missing-result placeholder", () => {
|
||||
const ps = prompts(buildMessages({ messages: [
|
||||
{ role: "user", content: "Read calc.py." },
|
||||
{ role: "assistant", content: [{ type: "tool_use", id: "c1", name: "read_file", input: {} }] },
|
||||
{ role: "system", content: [{ type: "text", text: "The read was interrupted.", cache_control: mark }] },
|
||||
] }, "claude-opus-5-5", "k"))
|
||||
expect(ps.slice(-2).map((p) => [p.role, p.text, p.callID, p.cache])).toEqual([
|
||||
[4, "Tool use was interrupted and did not produce a result.", "c1", null],
|
||||
[1, "The read was interrupted.", "", EPHEMERAL],
|
||||
])
|
||||
})
|
||||
|
||||
test("tool calls and results keep their own prompts and marks", () => {
|
||||
const body = buildMessages(
|
||||
{
|
||||
|
||||
@@ -1913,18 +1913,23 @@ function buildMessages(req, uid, key) {
|
||||
// means one turn
|
||||
const turns = []
|
||||
for (const m of req.messages ?? []) {
|
||||
if (m?.role !== "user" && m?.role !== "assistant") continue
|
||||
if (m?.role !== "user" && m?.role !== "assistant" && m?.role !== "system") continue
|
||||
const blocks = typeof m.content === "string" ? (m.content ? [{ type: "text", text: m.content }] : []) : Array.isArray(m.content) ? m.content : []
|
||||
// Empty effort notices must not split a streamed assistant turn.
|
||||
if (m.role === "system" && !blocks.some((b) => b?.type === "text" && typeof b.text === "string" && b.text)) continue
|
||||
const last = turns[turns.length - 1]
|
||||
if (last?.role === m.role) last.blocks.push(...blocks)
|
||||
else turns.push({ role: m.role, blocks: [...blocks] })
|
||||
}
|
||||
|
||||
const msgs = []
|
||||
const notices = [] // held until the pending tool calls are answered
|
||||
let pending = [] // calls the last reply made, not yet answered
|
||||
const answer = () => {
|
||||
for (const c of pending) msgs.push({ role: TOOL, callID: c.id, text: NO_RESULT })
|
||||
pending = []
|
||||
msgs.push(...notices)
|
||||
notices.length = 0
|
||||
}
|
||||
for (const t of turns) {
|
||||
if (t.role === "assistant") {
|
||||
@@ -1967,6 +1972,7 @@ function buildMessages(req, uid, key) {
|
||||
const said = text || "(no output)"
|
||||
msgs.push({ role: TOOL, callID: pending[i].id, text: b.is_error ? "Error: " + said : said, images, cache: marked(b) || marked(inner[inner.length - 1]) })
|
||||
pending.splice(i, 1)
|
||||
if (!pending.length) answer()
|
||||
continue
|
||||
}
|
||||
if (b?.type === "text" && typeof b.text === "string") {
|
||||
@@ -1974,10 +1980,13 @@ function buildMessages(req, uid, key) {
|
||||
if (marked(b) && words) words.cache = true
|
||||
continue
|
||||
}
|
||||
answer()
|
||||
// Devin requires tool results before the next user prompt. A
|
||||
// system notice can arrive first, so hold it until they follow.
|
||||
if (t.role !== "system") answer()
|
||||
words = { role: USER, text: b.text, images: held, cache: marked(b) }
|
||||
held = []
|
||||
msgs.push(words)
|
||||
if (t.role === "system" && pending.length) notices.push(words)
|
||||
else msgs.push(words)
|
||||
continue
|
||||
}
|
||||
const im = anthropicImage(b)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-devin-auth",
|
||||
"version": "0.1.31",
|
||||
"version": "0.1.32",
|
||||
"description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user