Skip to content

https://arxiv.org/abs/2606.28344. The end of web parsing. The beginning of scalable pixel-native search. link: https://pixelrag.ai/

fix(agent): give the turn cap headroom, and keep partial work when it fires (#150)

Zhifei Lib9a514651c14
diff --git a/web/agent-server.mjs b/web/agent-server.mjs
index 2835871..13eef01 100644
--- a/web/agent-server.mjs
+++ b/web/agent-server.mjs
@@ -38,6 +38,11 @@ const PORT = parseInt(process.env.AGENT_PORT || "30010", 10)
const SEARCH_URL = process.env.PIXELRAG_SEARCH_URL || "https://api.pixelrag.ai"
const MAX_BUDGET = parseFloat(process.env.CHAT_MAX_BUDGET_USD || "2.00")
const THINKING_TOKENS = parseInt(process.env.CHAT_THINKING_TOKENS || "2000", 10)
+// The system prompt's own worst case — two searches (image-then-text), four
+// tiles, then an answer — lands on eight turns exactly, so a cap of 8 cut off
+// the strategy it asks for. Cost is bounded by CHAT_MAX_BUDGET_USD, which had
+// never once been the binding limit; the turn cap always fired first.
+const MAX_TURNS = parseInt(process.env.CHAT_MAX_TURNS || "12", 10)
const ALLOWED_ORIGIN = process.env.ALLOWED_ORIGIN || "*"
// Rate limiting — protects the subscription on a public endpoint.
@@ -307,15 +312,15 @@ const server = http.createServer(async (req, res) => {
const mcpServer = createSdkMcpServer({ name: "pixelrag", version: "1.0.0", tools })
inFlight++
+ let sentText = false
try {
- let sentText = false
for await (const message of query({
prompt,
options: {
systemPrompt: SYSTEM_PROMPT,
mcpServers: { pixelrag: mcpServer },
allowedTools: ["mcp__pixelrag__pixelrag_search", "mcp__pixelrag__pixelrag_tile"],
- maxTurns: 8,
+ maxTurns: MAX_TURNS,
maxBudgetUsd: MAX_BUDGET,
maxThinkingTokens: THINKING_TOKENS,
includePartialMessages: true,
@@ -349,8 +354,23 @@ const server = http.createServer(async (req, res) => {
log(`chat done in ${elapsed}s`)
}
} catch (err) {
- log("chat error:", String(err))
- send("error", { message: String(err) })
+ const detail = String(err)
+ const elapsed = ((Date.now() - t0) / 1000).toFixed(1)
+ // Hitting the turn cap is not a failed turn — the model has usually read
+ // real tiles by then. Discarding that and emitting a raw SDK error string
+ // throws away work the user already waited for.
+ if (/Reached maximum number of turns/i.test(detail)) {
+ if (!sentText) {
+ send("text", {
+ text: "I ran out of steps before I could finish reading the Wikipedia tiles for this one. A narrower or more specific question usually gets there.",
+ })
+ }
+ send("done", { truncated: true })
+ log(`chat TURN-CAPPED in ${elapsed}s (partial answer: ${sentText})`)
+ } else {
+ log("chat error:", detail)
+ send("error", { message: detail })
+ }
} finally {
inFlight--
res.end()
diff --git a/web/app/api/chat/route.ts b/web/app/api/chat/route.ts
index bd04b86..9d99dd8 100644
--- a/web/app/api/chat/route.ts
+++ b/web/app/api/chat/route.ts
@@ -352,8 +352,8 @@ export async function POST(req: Request) {
tools,
})
+ let sentText = false
try {
- let sentText = false
for await (const message of query({
prompt,
options: {
@@ -363,7 +363,7 @@ export async function POST(req: Request) {
"mcp__pixelrag__pixelrag_search",
"mcp__pixelrag__pixelrag_tile",
],
- maxTurns: 8,
+ maxTurns: parseInt(process.env.CHAT_MAX_TURNS || "12", 10),
maxBudgetUsd: parseFloat(
process.env.CHAT_MAX_BUDGET_USD || "0.50"
),
@@ -405,7 +405,20 @@ export async function POST(req: Request) {
send("done", {})
}
} catch (err) {
- send("error", { message: String(err) })
+ const detail = String(err)
+ // Hitting the turn cap is not a failed turn — the model has usually
+ // read real tiles by then, and discarding that to emit a raw SDK error
+ // string throws away work the user already waited for.
+ if (/Reached maximum number of turns/i.test(detail)) {
+ if (!sentText) {
+ send("text", {
+ text: "I ran out of steps before I could finish reading the Wikipedia tiles for this one. A narrower or more specific question usually gets there.",
+ })
+ }
+ send("done", { truncated: true })
+ } else {
+ send("error", { message: detail })
+ }
} finally {
controller.close()
}
diff --git a/web/app/chat/page.tsx b/web/app/chat/page.tsx
index b0792cb..5c93a69 100644
--- a/web/app/chat/page.tsx
+++ b/web/app/chat/page.tsx
@@ -60,6 +60,9 @@ interface ChatMessage {
// grounded in Wikipedia tiles. Marked so the answer cannot be mistaken for
// an ordinary one.
degraded?: boolean
+ // The turn cap stopped the agent mid-investigation. Unlike `degraded`, what
+ // it did say is grounded — it is just incomplete.
+ truncated?: boolean
}
const EXAMPLES = [
@@ -179,7 +182,7 @@ function ChatPageInner() {
case "search_results": return { ...m, searching: undefined, searches: [...(m.searches || []), { query: data.query as string, hits: data.hits as SearchResult["hits"] }] }
case "viewing_tile": return { ...m, viewingTile: true, tiles: [...(m.tiles || []), { article_id: data.article_id as number, tile_index: data.tile_index as number, chunk_index: data.chunk_index as number }] }
case "search_unavailable": return { ...m, searching: undefined, degraded: true }
- case "done": return { ...m, searching: undefined, viewingTile: false, degraded: m.degraded || Boolean(data.degraded) }
+ case "done": return { ...m, searching: undefined, viewingTile: false, degraded: m.degraded || Boolean(data.degraded), truncated: Boolean(data.truncated) }
case "error": return { ...m, content: m.content || `Error: ${data.message}`, searching: undefined }
default: return m
}
@@ -462,6 +465,15 @@ function AssistantMessage({ message, isStreaming, onTileClick }: { message: Chat
</div>
)}
+ {message.truncated && !message.degraded && (
+ <div className="mb-4 flex items-start gap-2.5 rounded-lg border border-[var(--chat-border)] bg-[var(--chat-card)] px-3.5 py-2.5">
+ <Clock className="mt-0.5 h-3.5 w-3.5 shrink-0 text-[var(--chat-muted)]" />
+ <span className="text-[12px] leading-[1.6] text-[var(--chat-secondary)]">
+ Stopped early — this answer is based on the tiles read so far, not a finished search.
+ </span>
+ </div>
+ )}
+
{message.content && (
<div className="chat-prose prose prose-sm max-w-none text-[14px] leading-[1.8] text-[var(--chat-secondary)] prose-headings:font-display prose-headings:font-normal prose-headings:tracking-tight prose-headings:text-[var(--chat-fg)] prose-h2:mt-6 prose-h2:text-lg prose-h3:mt-4 prose-h3:text-[15px] prose-p:text-[var(--chat-secondary)] prose-a:text-[var(--chat-accent)] prose-a:no-underline hover:prose-a:underline prose-strong:font-semibold prose-strong:text-[var(--chat-fg)] prose-code:rounded-md prose-code:bg-[var(--chat-card)] prose-code:px-1.5 prose-code:py-0.5 prose-code:text-[12px] prose-code:text-[var(--chat-secondary)] prose-code:before:content-none prose-code:after:content-none prose-pre:border prose-pre:border-[var(--chat-border)] prose-pre:bg-[var(--chat-card)] prose-pre:rounded-xl prose-li:marker:text-[var(--chat-muted)] prose-hr:border-[var(--chat-border)] prose-blockquote:border-l-[var(--chat-accent-dim)] prose-blockquote:text-[var(--chat-muted)] prose-th:font-mono prose-th:text-[11px] prose-th:font-medium prose-th:uppercase prose-th:tracking-wider prose-th:text-[var(--chat-muted)] prose-td:text-[var(--chat-secondary)] prose-table:text-[13px]">
<Markdown remarkPlugins={[remarkGfm]} components={{ a: ({ href, children }) => (<a href={href} target="_blank" rel="noopener noreferrer">{href?.includes("wikipedia.org") ? decodeURIComponent(href.split("/wiki/").pop() || String(children)).replace(/_/g, " ") : children}</a>) }}>