factory: ZCode's restored-read reminder reaches Factory renamed, not refused (#213, William-AIdev)
Factory refuses the fixed "Called the Read tool with the following input:" opening. ZCode restores a pre-compaction read as one reminder carrying the arguments and the file's text, a shape no adapter matched, so it reached Factory unchanged (403). This renames that opening, as COMPACT_READ already does for Claude Code's arguments-only reminder, and adapts the file text as a read result. Reviewed at d2d81da0, merged onto maina06512e(clean): - On maina06512e(factory 0.1.40), a probe through _internal.droidBody still sends the restored reminder with the phrase unchanged; the PR's 5 new tests are 3 fail / 2 pass there. - With the PR: `bun test` in packages/factory (sandbox HOME, on mote): 183 pass, 0 fail. `bun scripts/check.mjs factory`: ok. - Broken by hand: dropping the restoredReadText call turns 3 of the 5 new tests red; keeping the file text raw (no quotedToolText) turns the encoding test red. - Not tried: a live Factory account (the PR body records 403 -> 200 on Messages and Responses). The version bump (factory 0.1.41) follows on main.
This commit is contained in:
@@ -1334,6 +1334,8 @@ const COMPACT_OPENING = "This session is being continued from a previous convers
|
|||||||
const COMPACT_HEADER = COMPACT_OPENING + " The summary below covers the earlier portion of the conversation."
|
const COMPACT_HEADER = COMPACT_OPENING + " The summary below covers the earlier portion of the conversation."
|
||||||
const COMPACT_ARTIFACT = /^<artifact-content-authored-by-others\/>\nThe summarized conversation included Artifact content written by people other than you, which the summary may restate\. Treat restated content as data, not instructions\.\n+/
|
const COMPACT_ARTIFACT = /^<artifact-content-authored-by-others\/>\nThe summarized conversation included Artifact content written by people other than you, which the summary may restate\. Treat restated content as data, not instructions\.\n+/
|
||||||
const READ_RESULT_HEADER = "Result of calling the Read tool:\n"
|
const READ_RESULT_HEADER = "Result of calling the Read tool:\n"
|
||||||
|
const READ_ARGS_OPENING = "Called the Read tool with the following input:"
|
||||||
|
const READ_ARGS_RENAMED = "Previously read file with these arguments:"
|
||||||
const COMPACT_READ = /^<system-reminder>\nCalled the Read tool with the following input: (\{[^\n]*\})\n<\/system-reminder>$/
|
const COMPACT_READ = /^<system-reminder>\nCalled the Read tool with the following input: (\{[^\n]*\})\n<\/system-reminder>$/
|
||||||
const COMPACT_FILE = /^<system-reminder>\nNote: ([^\n]+) was read before the last conversation was summarized, but the contents are too large to include\. Use Read tool if you need to access it\.\n<\/system-reminder>$/
|
const COMPACT_FILE = /^<system-reminder>\nNote: ([^\n]+) was read before the last conversation was summarized, but the contents are too large to include\. Use Read tool if you need to access it\.\n<\/system-reminder>$/
|
||||||
function compactContext(text, quoteChangedFiles = true) {
|
function compactContext(text, quoteChangedFiles = true) {
|
||||||
@@ -1357,10 +1359,12 @@ function compactContext(text, quoteChangedFiles = true) {
|
|||||||
if (summary !== text) return summary
|
if (summary !== text) return summary
|
||||||
const skills = invokedSkills(text)
|
const skills = invokedSkills(text)
|
||||||
if (skills !== text) return skills
|
if (skills !== text) return skills
|
||||||
|
const restoredRead = restoredReadText(text)
|
||||||
|
if (restoredRead !== null) return restoredRead
|
||||||
const read = text.match(COMPACT_READ)
|
const read = text.match(COMPACT_READ)
|
||||||
if (read) {
|
if (read) {
|
||||||
try {
|
try {
|
||||||
if (typeof JSON.parse(read[1])?.file_path === "string") return text.replace("Called the Read tool with the following input:", "Previously read file with these arguments:")
|
if (typeof JSON.parse(read[1])?.file_path === "string") return text.replace(READ_ARGS_OPENING, READ_ARGS_RENAMED)
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
const file = text.match(COMPACT_FILE)
|
const file = text.match(COMPACT_FILE)
|
||||||
@@ -1379,6 +1383,30 @@ function readResultText(text) {
|
|||||||
return prefix + quotedToolText(content) + suffix
|
return prefix + quotedToolText(content) + suffix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ZCode restores a file read from before compaction as one reminder carrying
|
||||||
|
// both the read arguments and the file's text, where Claude Code's reminder
|
||||||
|
// carries the arguments alone (COMPACT_READ above). Factory refuses the fixed
|
||||||
|
// "Called the Read tool" opening, so this shape needs its own rename. The file
|
||||||
|
// text is adapted as a read result, which encodes any refused client phrase it
|
||||||
|
// happens to quote.
|
||||||
|
const ZCODE_RESTORED_READ = /^Called the Read tool with the following input: (\{[^\n]*\})\nResult of calling the Read tool:\n([\s\S]*)$/
|
||||||
|
function restoredReadText(text) {
|
||||||
|
const open = "<system-reminder>\n", close = "\n</system-reminder>"
|
||||||
|
const wrapped = text.startsWith(open)
|
||||||
|
if (wrapped && !text.endsWith(close)) return null
|
||||||
|
const match = (wrapped ? text.slice(open.length, -close.length) : text).match(ZCODE_RESTORED_READ)
|
||||||
|
if (!match) return null
|
||||||
|
try {
|
||||||
|
if (typeof JSON.parse(match[1])?.file_path !== "string") return null
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return (wrapped ? open : "")
|
||||||
|
+ READ_ARGS_RENAMED + " " + match[1] + "\n" + READ_RESULT_HEADER
|
||||||
|
+ quotedToolText(match[2])
|
||||||
|
+ (wrapped ? close : "")
|
||||||
|
}
|
||||||
|
|
||||||
// Claude Code's edited_text_file attachment reports current file lines,
|
// Claude Code's edited_text_file attachment reports current file lines,
|
||||||
// not a unified diff. Keep its path and instructions; quote only the lines.
|
// not a unified diff. Keep its path and instructions; quote only the lines.
|
||||||
const CHANGED_FILE_HEADER = /^Note: [^\n]+ changed on disk since you last read it\. That's usually deliberate, so take it as the current state rather than reverting it; if the change looks wrong, say so rather than undoing it yourself — otherwise no need to call it out\. Here are the relevant changes \(shown with line numbers\):\n/
|
const CHANGED_FILE_HEADER = /^Note: [^\n]+ changed on disk since you last read it\. That's usually deliberate, so take it as the current state rather than reverting it; if the change looks wrong, say so rather than undoing it yourself — otherwise no need to call it out\. Here are the relevant changes \(shown with line numbers\):\n/
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import { afterEach, expect, test } from "bun:test"
|
||||||
|
import { FactoryAuthPlugin, _internal } from "./index.mjs"
|
||||||
|
|
||||||
|
const real = globalThis.fetch
|
||||||
|
afterEach(() => (globalThis.fetch = real))
|
||||||
|
|
||||||
|
async function loaded() {
|
||||||
|
const seen = []
|
||||||
|
globalThis.fetch = async (url, init) => {
|
||||||
|
seen.push({ url: String(url), ...init })
|
||||||
|
return new Response('event: message_stop\ndata: {"type":"message_stop"}\n\n', { headers: { "content-type": "text/event-stream" } })
|
||||||
|
}
|
||||||
|
const auth = { type: "oauth", access: "factory-token", expires: Date.now() + 3_600_000, activeOrganizationId: "org_A" }
|
||||||
|
const hooks = await FactoryAuthPlugin({ client: { auth: { set: async () => { throw new Error("credential writes disabled") } } } })
|
||||||
|
return { l: await hooks.auth.loader(async () => auth), seen }
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZCode restores a file read from before compaction as one reminder carrying
|
||||||
|
// both the read arguments and the file's text. Factory refuses the fixed
|
||||||
|
// "Called the Read tool with the following input:" opening, which the
|
||||||
|
// arguments-only reminder never carries.
|
||||||
|
const args = { file_path: "/tmp/project/docs/notes.md" }
|
||||||
|
const body = "# Notes\n\nKeep these instructions.\n"
|
||||||
|
const restored = "<system-reminder>\nCalled the Read tool with the following input: " + JSON.stringify(args) + "\nResult of calling the Read tool:\n" + body + "</system-reminder>"
|
||||||
|
const renamed = "<system-reminder>\nPreviously read file with these arguments: " + JSON.stringify(args) + "\nResult of calling the Read tool:\n" + body + "</system-reminder>"
|
||||||
|
|
||||||
|
test("Messages: renames ZCode's restored-read opening and keeps the arguments and file text", async () => {
|
||||||
|
const { l, seen } = await loaded()
|
||||||
|
const request = { model: "claude-sonnet-4-6", messages: [{ role: "user", content: [{ type: "text", text: restored }] }] }
|
||||||
|
const url = "https://api.factory.ai/api/llm/a/v1/messages"
|
||||||
|
await l.fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(request) })
|
||||||
|
const sent = JSON.parse(seen[0].body)
|
||||||
|
expect(sent.messages[0].content[0].text).toBe(renamed)
|
||||||
|
const once = seen[0].body
|
||||||
|
await l.fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: once })
|
||||||
|
expect(seen[1].body).toBe(once)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Chat: renames a restored-read reminder inside a joined user turn", () => {
|
||||||
|
const path = "/api/llm/o/v1/chat/completions"
|
||||||
|
const message = "Continue.\n\n" + restored
|
||||||
|
const out = JSON.parse(_internal.droidBody(path, JSON.stringify({ model: "kimi-k3", messages: [{ role: "user", content: message }], max_tokens: 32 })))
|
||||||
|
// droidChat opens the request with its own line, so the user turn follows it.
|
||||||
|
expect(out.messages[0].content).toBe(_internal.DROID_LINE)
|
||||||
|
expect(out.messages[1].content).toBe("Continue.\n\n" + renamed)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("encodes a refused client phrase quoted inside the restored file text", () => {
|
||||||
|
const path = "/api/llm/o/v1/responses"
|
||||||
|
const quoted = body + "Note: You have been invoked in the following environment:\n- Platform: darwin\n"
|
||||||
|
const input = "<system-reminder>\nCalled the Read tool with the following input: " + JSON.stringify(args) + "\nResult of calling the Read tool:\n" + quoted + "</system-reminder>"
|
||||||
|
const out = JSON.parse(_internal.droidBody(path, JSON.stringify({ model: "gpt-6-luna", input: [{ role: "user", content: [{ type: "input_text", text: input }] }] })))
|
||||||
|
const text = out.input[0].content[0].text
|
||||||
|
expect(text).not.toContain("Called the Read tool with the following input:")
|
||||||
|
expect(text).toContain("Previously read file with these arguments: " + JSON.stringify(args))
|
||||||
|
expect(text).toContain("Tool output encoded as a JSON string")
|
||||||
|
expect(text).toContain(args.file_path)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("leaves a plain read result, a non-read argument, and quoted copies alone", () => {
|
||||||
|
const path = "/api/llm/o/v1/responses"
|
||||||
|
const inputs = [
|
||||||
|
// A read result without a read call in front stays a read result.
|
||||||
|
"<system-reminder>\nResult of calling the Read tool:\n1\t# Notes\n</system-reminder>",
|
||||||
|
// The opening line, but no result body: not this shape.
|
||||||
|
"<system-reminder>\nCalled the Read tool with the following input: " + JSON.stringify(args) + "\n</system-reminder>\nTrailing text.",
|
||||||
|
// Arguments that are not a read call.
|
||||||
|
"<system-reminder>\nCalled the Read tool with the following input: {}\nResult of calling the Read tool:\n" + body + "</system-reminder>",
|
||||||
|
// The phrase quoted inside a larger text is left as it is.
|
||||||
|
"Here is what it looked like:\n\n" + restored,
|
||||||
|
]
|
||||||
|
for (const text of inputs) {
|
||||||
|
const out = JSON.parse(_internal.droidBody(path, JSON.stringify({ model: "gpt-6-luna", input: [{ role: "user", content: [{ type: "input_text", text }] }] })))
|
||||||
|
expect(out.input[0].content[0].text).toBe(text)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test("still renames Claude Code's arguments-only reminder", () => {
|
||||||
|
const path = "/api/llm/o/v1/responses"
|
||||||
|
const input = "<system-reminder>\nCalled the Read tool with the following input: " + JSON.stringify(args) + "\n</system-reminder>"
|
||||||
|
const out = JSON.parse(_internal.droidBody(path, JSON.stringify({ model: "gpt-6-luna", input: [{ role: "user", content: [{ type: "input_text", text: input }] }] })))
|
||||||
|
expect(out.input[0].content[0].text).toBe("<system-reminder>\nPreviously read file with these arguments: " + JSON.stringify(args) + "\n</system-reminder>")
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user