diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs
index d1e10bd..987668f 100644
--- a/packages/factory/index.mjs
+++ b/packages/factory/index.mjs
@@ -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 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 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\n", "Earlier conversation context is summarized below:\n\n\n"],
+ ["The following is a summary of a branch that this conversation came back from:\n\n\n", "Summary of an earlier conversation branch:\n\n\n"],
+]
+function piSummary(text) {
+ if (typeof text !== "string" || !text.endsWith("")) 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 {
diff --git a/packages/factory/package.json b/packages/factory/package.json
index e8392e5..b432867 100644
--- a/packages/factory/package.json
+++ b/packages/factory/package.json
@@ -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",
diff --git a/packages/factory/pi.test.mjs b/packages/factory/pi.test.mjs
index b047de2..f15a359 100644
--- a/packages/factory/pi.test.mjs
+++ b/packages/factory/pi.test.mjs
@@ -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 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\nsrc/auth.ts\n"
+const PI_COMPACTION = COMPACTED + "\n\n\n" + SUMMARY + "\n"
+const ADAPTED_COMPACTION = "Earlier conversation context is summarized below:\n\n\n" + SUMMARY + "\n"
+const BRANCHED = "The following is a summary of a branch that this conversation came back from:"
+const PI_BRANCH = BRANCHED + "\n\n\n" + SUMMARY + ""
+const ADAPTED_BRANCH = "Summary of an earlier conversation branch:\n\n\n" + 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\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)
+})