factory 0.1.21: pi's compaction summary reaches Factory without its fixed opening sentence (yetone/magpie#1316, Eriice)

After pi compacted a session, every turn on factory/claude-opus-5-5 was 403
"Factory: Forbidden". The reporter's replays through magpie found the
refused part: the sentence pi opens its summary message with,
"The conversation history before this point was compacted into the
following summary:" (pi-coding-agent dist/core/messages.js
COMPACTION_SUMMARY_PREFIX, the same in 1.0.4 and 1.1.0), whole; with it
removed, reworded or halved the request was answered.

A user text that is pi's whole summary message (the sentence, a blank line,
<summary>, and </summary> last) now opens "Earlier conversation context is
summarized below:", the summary itself unchanged. pi's other fixed summary
opening, BRANCH_SUMMARY_PREFIX ("The following is a summary of a branch
that this conversation came back from:"), is reworded the same way; that
Factory refuses it is not confirmed. On Messages (string content and text
blocks), chat completions and Responses user messages alike. The sentence
quoted by a user, without pi's summary around it, or in an assistant turn
goes on as it is.

pi.test.mjs: 3 new tests; the two adaptation tests fail on 0.1.20
(Expected/Received differ on the summary text), the third checks what is
left alone. bun test (114 pass) and bun scripts/check.mjs factory pass.
Not tried against Factory itself (no Factory account here).
This commit is contained in:
yetone
2026-10-08 21:03:04 +08:00
parent 5e80f39d3f
commit f4c0d96c9c
3 changed files with 85 additions and 6 deletions
+30 -5
View File
@@ -286,20 +286,43 @@ function clientOpening(text) {
return typeof text === "string" ? text.replace(PI_OPENING, (_, at) => at + PI_COMPAT) : text
}
// pi turns a compaction, and a branch it came back from, into a user
// message of one text: a fixed sentence, then the summary in <summary> tags
// (pi-coding-agent 1.0.4 and 1.1.0, dist/core/messages.js:
// COMPACTION_SUMMARY_PREFIX and BRANCH_SUMMARY_PREFIX). Factory refuses the
// compaction sentence whole (yetone/magpie#1316): after a compaction every
// turn was 403, and the reporter's replays passed with the sentence removed,
// reworded or only half of it. The branch sentence is pi's other fixed
// summary opening and is reworded the same way. Only a text that is pi's
// whole summary, sentence first and </summary> last, changes; the summary
// itself goes on as it is.
const PI_SUMMARIES = [
["The conversation history before this point was compacted into the following summary:\n\n<summary>\n", "Earlier conversation context is summarized below:\n\n<summary>\n"],
["The following is a summary of a branch that this conversation came back from:\n\n<summary>\n", "Summary of an earlier conversation branch:\n\n<summary>\n"],
]
function piSummary(text) {
if (typeof text !== "string" || !text.endsWith("</summary>")) return text
for (const [from, to] of PI_SUMMARIES) if (text.startsWith(from)) return to + text.slice(from.length)
return text
}
// promptOpenings adapts clientOpening in the system and developer messages
// of msgs (chat completions' messages, Responses' input), their string
// content or text parts: false when nothing changed.
// of msgs (chat completions' messages, Responses' input), and piSummary in
// their user messages, their string content or text parts: false when
// nothing changed.
function promptOpenings(msgs) {
let changed = false
for (const m of Array.isArray(msgs) ? msgs : []) {
if (!m || typeof m !== "object" || (m.role !== "system" && m.role !== "developer")) continue
if (!m || typeof m !== "object") continue
const adapt = m.role === "system" || m.role === "developer" ? clientOpening : m.role === "user" ? piSummary : null
if (!adapt) continue
if (typeof m.content === "string") {
const adapted = clientOpening(m.content)
const adapted = adapt(m.content)
if (adapted !== m.content) (m.content = adapted), (changed = true)
} else if (Array.isArray(m.content)) {
for (const p of m.content) {
if (!p || typeof p !== "object" || typeof p.text !== "string") continue
const adapted = clientOpening(p.text)
const adapted = adapt(p.text)
if (adapted !== p.text) (p.text = adapted), (changed = true)
}
}
@@ -789,6 +812,8 @@ function compactContext(text, quoteChangedFiles = true) {
const quoted = quotedToolText(context, "Conversation context encoded as a JSON string. Decode the JSON string to recover the exact original context before continuing:\n")
return header + (quoted === context ? context : "\n\n" + quoted)
}
const summary = piSummary(text)
if (summary !== text) return summary
const read = text.match(COMPACT_READ)
if (read) {
try {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-factory-auth",
"version": "0.1.20",
"version": "0.1.21",
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
"type": "module",
"main": "./index.mjs",
+54
View File
@@ -85,3 +85,57 @@ test("the sentence quoted by the user, or only part of it, goes on as it is", as
b = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, system: [{ type: "text", text: "Quote: " + OPENING }], messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }] })
expect(b.system[1].text).toBe("Quote: " + OPENING)
})
// yetone/magpie#1316: after pi compacted a session, every turn on
// factory/claude-opus-5-5 was 403 "Factory: Forbidden". The reporter's
// replays found the refused part: the fixed sentence pi opens the summary
// message with (pi-coding-agent dist/core/messages.js
// COMPACTION_SUMMARY_PREFIX), whole; with it removed or reworded the same
// request was answered. pi sends the summary as a user message of one text
// part (convertToLlm), the sentence, the summary in <summary> tags, and
// BRANCH_SUMMARY_PREFIX's sentence for a branch it came back from.
const COMPACTED = "The conversation history before this point was compacted into the following summary:"
const SUMMARY = "## Goal\nFix the login redirect.\n\n## Progress\n### Done\n- [x] Read src/auth.ts\n\n## Next Steps\n1. Patch the callback\n\n<read-files>\nsrc/auth.ts\n</read-files>"
const PI_COMPACTION = COMPACTED + "\n\n<summary>\n" + SUMMARY + "\n</summary>"
const ADAPTED_COMPACTION = "Earlier conversation context is summarized below:\n\n<summary>\n" + SUMMARY + "\n</summary>"
const BRANCHED = "The following is a summary of a branch that this conversation came back from:"
const PI_BRANCH = BRANCHED + "\n\n<summary>\n" + SUMMARY + "</summary>"
const ADAPTED_BRANCH = "Summary of an earlier conversation branch:\n\n<summary>\n" + SUMMARY + "</summary>"
test("pi's compaction summary opens without its fixed sentence on Messages, the summary kept", async () => {
const send = await loaded()
const after = [{ role: "assistant", content: [{ type: "text", text: "Patched." }] }, { role: "user", content: [{ type: "text", text: "go on" }] }]
const b = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, system: [{ type: "text", text: PI, cache_control: { type: "ephemeral" } }], messages: [{ role: "user", content: [{ type: "text", text: PI_COMPACTION }] }, ...after] })
expect(b.messages).toEqual([{ role: "user", content: [{ type: "text", text: ADAPTED_COMPACTION }] }, ...after])
expect(JSON.stringify(b)).not.toContain(COMPACTED)
// a string content, and a branch summary
const c = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, messages: [{ role: "user", content: PI_COMPACTION }, { role: "user", content: [{ type: "text", text: PI_BRANCH }] }] })
expect(c.messages).toEqual([{ role: "user", content: ADAPTED_COMPACTION }, { role: "user", content: [{ type: "text", text: ADAPTED_BRANCH }] }])
})
test("pi's compaction summary opens without its fixed sentence on chat completions and Responses", async () => {
const send = await loaded()
let b = await send(chat, { model: "deepseek-v4.1-flash", messages: [{ role: "developer", content: PI }, { role: "user", content: [{ type: "text", text: PI_COMPACTION }] }, { role: "user", content: "go on" }] })
expect(b.messages.slice(1)).toEqual([{ role: "developer", content: ADAPTED }, { role: "user", content: [{ type: "text", text: ADAPTED_COMPACTION }] }, { role: "user", content: "go on" }])
b = await send(chat, { model: "kimi-k3", messages: [{ role: "user", content: PI_BRANCH }] })
expect(b.messages[1]).toEqual({ role: "user", content: ADAPTED_BRANCH })
b = await send(responses, { model: "gpt-6.1", instructions: PI, input: [{ type: "message", role: "user", content: [{ type: "input_text", text: PI_COMPACTION }] }, { role: "user", content: "go on" }] })
expect(b.input).toEqual([{ type: "message", role: "user", content: [{ type: "input_text", text: ADAPTED_COMPACTION }] }, { role: "user", content: "go on" }])
})
test("the compaction sentence the user writes, or outside pi's whole summary, goes on as it is", async () => {
const send = await loaded()
for (const text of [
"Why does pi write: " + COMPACTED, // quoted mid-text
COMPACTED + " What does that mean?", // the sentence alone, no summary
COMPACTED + "\n\n<summary>\n" + SUMMARY, // no closing tag: not pi's whole message
]) {
const b = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, messages: [{ role: "user", content: [{ type: "text", text }] }] })
expect(b.messages[0].content[0].text).toBe(text)
const c = await send(chat, { model: "kimi-k3", messages: [{ role: "user", content: text }] })
expect(c.messages[1]).toEqual({ role: "user", content: text })
}
// an assistant repeating the summary is not pi's message
const b = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, messages: [{ role: "user", content: "hi" }, { role: "assistant", content: [{ type: "text", text: PI_COMPACTION }] }, { role: "user", content: "ok" }] })
expect(b.messages[1].content[0].text).toBe(PI_COMPACTION)
})