Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([["9916"],{38483(e,n,t){t.r(n),t.d(n,{metadata:()=>r,default:()=>h,frontMatter:()=>o,contentTitle:()=>d,toc:()=>l,assets:()=>c});var r=JSON.parse('{"id":"reference/completion-architecture","title":"The Completion Architecture","description":"How Mercury Code guarantees honest, verified, stall-free task completions \u2014 the completion contract, the escalation harness, and the recovery map.","source":"@site/docs/reference/completion-architecture.md","sourceDirName":"reference","slug":"/reference/completion-architecture","permalink":"/docs/reference/completion-architecture","draft":false,"unlisted":false,"editUrl":"https://github.com/cosmicstack-labs/mercury-agent/tree/main/website/docs/reference/completion-architecture.md","tags":[],"version":"current","frontMatter":{"title":"The Completion Architecture","description":"How Mercury Code guarantees honest, verified, stall-free task completions \u2014 the completion contract, the escalation harness, and the recovery map.","keywords":["mercury","mercury-code","completion contract","escalation harness","stall watchdog","recovery"]},"sidebar":"docsSidebar","previous":{"title":"Built-in Tools","permalink":"/docs/reference/built-in-tools"},"next":{"title":"Configuration","permalink":"/docs/reference/configuration"}}'),s=t(74848),i=t(28453);let o={title:"The Completion Architecture",description:"How Mercury Code guarantees honest, verified, stall-free task completions \u2014 the completion contract, the escalation harness, and the recovery map.",keywords:["mercury","mercury-code","completion contract","escalation harness","stall watchdog","recovery"]},d="The Completion Architecture",c={},l=[{value:"The problem it solves",id:"the-problem-it-solves",level:2},{value:"The verdict system",id:"the-verdict-system",level:2},{value:"The narration guard \u2014 the escalation harness",id:"the-narration-guard--the-escalation-harness",level:2},{value:"Compact-on-pressure",id:"compact-on-pressure",level:2},{value:"Honest endings",id:"honest-endings",level:2},{value:"Stream integrity",id:"stream-integrity",level:2},{value:"The watchdog",id:"the-watchdog",level:2},{value:"Output limits \u2014 none of Mercury's own",id:"output-limits--none-of-mercurys-own",level:2},{value:"AUTO mode",id:"auto-mode",level:2},{value:"Testing invariants",id:"testing-invariants",level:2}];function a(e){let n={a:"a",blockquote:"blockquote",code:"code",em:"em",h1:"h1",h2:"h2",header:"header",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.header,{children:(0,s.jsx)(n.h1,{id:"the-completion-architecture",children:"The Completion Architecture"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsxs)(n.p,{children:["How Mercury Code guarantees ",(0,s.jsx)(n.strong,{children:"honest, verified, stall-free"})," task endings \u2014 every failure mode routed to a named recovery path instead of a death, and no task claiming success without evidence."]}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["Introduced in ",(0,s.jsx)(n.strong,{children:"v1.2.3 \u2014 Unstoppable Mercury"}),". This page documents the architecture of the completion pipeline: the problem it solves, the components, and where each lives in the code."]}),"\n",(0,s.jsx)(n.h2,{id:"the-problem-it-solves",children:"The problem it solves"}),"\n",(0,s.jsxs)(n.p,{children:["Before the contract, the agent loop was a single optimistic pass: the model generated, tools ran, and ",(0,s.jsx)(n.strong,{children:"any"}),' ending was celebrated as "Task complete" \u2014 including step-budget exhaustion over half-done work, writes severed mid-argument by an output cap, and provider drops that silently cut responses. A narration-prone model could describe work forever without producing a file, and long builds died at memory pressure.']}),"\n",(0,s.jsxs)(n.p,{children:["The architecture now routes ",(0,s.jsx)(n.strong,{children:"every"})," failure mode to recovery:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-text",children:" USER MESSAGE\n \u2502\n LLM GENERATION\n (step-aware streaming \xb7 no imposed output cap\n \xb7 per-step memory governor checkpoints)\n \u2502\n TOOL EXECUTION\n (forced-action capable: toolChoice 'required'\n + mutating-tools-only on guard steps)\n \u2502\n \u250C\u2500\u2500\u2500\u2500\u2500\u2500 TURN-END VERDICT \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n \u2502 classifyTurnEnd: WHY did it end? \u2502\n \u2514\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n text-stop\u2502 steps-\u2502exhausted\u2502 interrupted/truncated aborted\n \u25BC \u25BC \u25BC\n VERIFICATION AUTO-CONTINUE SECTIONED-WRITE\n GATE (build/ (fresh budget guidance + FULL-\n test required) \xd76 automatic) budget resume\n \u2502 \u2502 \u2502\n \u25BC \u25BC \u25BC\n \u250C\u2500\u2500\u2500\u2500 NARRATION GUARD (if no work landed) \u2500\u2500\u2500\u2500\u2510\n \u2502 1. GROUNDING \u2014 agent runs readdir itself \u2502\n \u2502 2. FORCED STEP \u2014 mutating tools only + \u2502\n \u2502 toolChoice 'required' \u2502\n \u2502 3. PROVIDER ROTATION \u2014 next model per round \u2502\n \u2502 4. WAKE-UP CALL \u2014 doubled bound, blunt \u2502\n \u2502 directive (10 mechanical rounds total) \u2502\n \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n \u25BC\n \u250C\u2500\u2500\u2500\u2500 RECOVERY LAYER \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n \u2502 memory pressure \u2192 COMPACT in place, \u2502\n \u2502 CONTINUE \u2502\n \u2502 doom loops \u2192 abort attempt \u2192 next provider \u2502\n \u2502 stall 3 min \u2192 pulse \xb7 8 min \u2192 resume \u2502\n \u2502 provider errors \u2192 fallback + named ledger \u2502\n \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n \u25BC\n FINAL VERDICT\n \u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n verified completion honest pause \"no changes\" honesty\n + change summary (names blocker, banner (git-verified\n + file previews resumable, persisted) only)\n"})}),"\n",(0,s.jsx)(n.h2,{id:"the-verdict-system",children:"The verdict system"}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"src/core/completion-verdict.ts"})," answers one question at every turn boundary: ",(0,s.jsx)(n.strong,{children:"why did the loop end, and is that a legitimate completion?"})]}),"\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{children:"Verdict"}),(0,s.jsx)(n.th,{children:"Meaning"}),(0,s.jsx)(n.th,{children:"What happens"})]})}),(0,s.jsxs)(n.tbody,{children:[(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"text-stop"})}),(0,s.jsx)(n.td,{children:"The model chose to stop with a final answer"}),(0,s.jsx)(n.td,{children:"Verification gate (execute/AUTO) \u2192 evidence-gated completion"})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"steps-exhausted"})}),(0,s.jsx)(n.td,{children:"Step budget ran out with tool calls pending"}),(0,s.jsxs)(n.td,{children:[(0,s.jsx)(n.strong,{children:"Pause"})," \u2014 bounded auto-continuation (6 fresh budgets), then an honest, resumable pause"]})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"interrupted"})}),(0,s.jsx)(n.td,{children:"Provider dropped mid-generation (no finish signal)"}),(0,s.jsx)(n.td,{children:"Retry/fallback machinery"})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"truncated"})}),(0,s.jsx)(n.td,{children:"Output hit the limit mid-response"}),(0,s.jsx)(n.td,{children:"Sectioned-write guidance + full-budget resume rounds"})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"aborted"})}),(0,s.jsx)(n.td,{children:"User halt / safety control"}),(0,s.jsx)(n.td,{children:"Explicit failed state, artifacts preserved"})]})]})]}),"\n",(0,s.jsx)(n.h2,{id:"the-narration-guard--the-escalation-harness",children:"The narration guard \u2014 the escalation harness"}),"\n",(0,s.jsx)(n.p,{children:'When a model narrates ("Building X\u2026") without doing work, the guard escalates mechanically. None of it depends on the model\'s goodwill:'}),"\n",(0,s.jsxs)(n.ol,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Grounding"})," \u2014 the agent executes a deterministic directory listing itself (no LLM) and injects it as verified state. The model cannot claim it lacks context."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Forced action"})," \u2014 via ",(0,s.jsx)(n.code,{children:"prepareStep"}),", the first step of a guard round runs with ",(0,s.jsx)(n.code,{children:"toolChoice: 'required'"})," and ",(0,s.jsx)(n.strong,{children:"mutating tools only"})," (",(0,s.jsx)(n.code,{children:"create_file"}),", ",(0,s.jsx)(n.code,{children:"write_file"}),", ",(0,s.jsx)(n.code,{children:"edit_file"}),", ",(0,s.jsx)(n.code,{children:"run_command"}),", \u2026). Narration is mechanically impossible on that step."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Provider rotation"})," \u2014 guard rounds walk the fallback chain. A narration-locked model is not the only worker the agent has."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Wake-up call"})," \u2014 after a full failed cycle, the bound doubles (10 mechanical rounds total) with a blunt directive: ",(0,s.jsx)(n.em,{children:'"Your next response MUST begin with a mutating tool call. ZERO prose."'})," Only after both cycles \u2192 an honest pause naming the blocker."]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"compact-on-pressure",children:"Compact-on-pressure"}),"\n",(0,s.jsxs)(n.p,{children:["Adopted from OpenCode's session pipeline: memory pressure ",(0,s.jsx)(n.strong,{children:"compacts the conversation in place and continues"})," instead of aborting. Messages beyond the newest 8 have oversized tool results and long text replaced with head+tail summaries (",(0,s.jsx)(n.code,{children:"compactConversation"})," in ",(0,s.jsx)(n.code,{children:"src/core/memory-governor.ts"}),"). A second consecutive pressure verdict aborts. Long builds stop dying at memory limits."]}),"\n",(0,s.jsx)(n.h2,{id:"honest-endings",children:"Honest endings"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Evidence-gated completion"}),' \u2014 implementation tasks must run a build/test/typecheck command before the "Task complete" banner is allowed (',(0,s.jsx)(n.code,{children:"src/core/execute-guard.ts"}),")."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Named blockers"})," \u2014 every pause carries the last failed mutating-tool result (e.g. ",(0,s.jsx)(n.code,{children:"write_file: permission denied"}),"), so the fix is visible in the chat."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Resumable pauses"})," \u2014 the work ledger records ",(0,s.jsx)(n.code,{children:"paused"}),' status: recovered on restart, resumable by sending "continue" (',(0,s.jsx)(n.code,{children:"src/core/work-ledger.ts"}),")."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"Honest banners"})," \u2014 ",(0,s.jsx)(n.code,{children:"Response delivered \xb7 no file changes"})," (git-verified), first-person pause messages, and a change summary with per-file +/\u2212 stats and verification evidence (",(0,s.jsx)(n.code,{children:"\u2713 Verified: npm test \u2713"}),")."]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"stream-integrity",children:"Stream integrity"}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"src/core/stream-completion.ts"})," classifies provider stream endings: a missing or ",(0,s.jsx)(n.code,{children:"other"})," finish reason is an ",(0,s.jsx)(n.strong,{children:"interruption"})," (never treated as success), ",(0,s.jsx)(n.code,{children:"length"})," is a ",(0,s.jsx)(n.strong,{children:"truncation"})," \u2014 and if the truncation severed a file write, the resume nudge demands sectioned writes: ",(0,s.jsx)(n.code,{children:"create_file"})," the first ~80 lines, then ",(0,s.jsx)(n.code,{children:"edit_file"})," appends, with full-budget resume rounds."]}),"\n",(0,s.jsx)(n.h2,{id:"the-watchdog",children:"The watchdog"}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"src/core/stall-watchdog.ts"})," covers TIME the way the memory governor covers heap:"]}),"\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{children:"Threshold"}),(0,s.jsx)(n.th,{children:"Default"}),(0,s.jsx)(n.th,{children:"Action"})]})}),(0,s.jsxs)(n.tbody,{children:[(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:"Soft"}),(0,s.jsx)(n.td,{children:"3 min"}),(0,s.jsx)(n.td,{children:'Visible "still working \xb7 Ns silent" pulse'})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:"Hard"}),(0,s.jsx)(n.td,{children:"8 min"}),(0,s.jsx)(n.td,{children:"Abort the attempt into the resume machinery"})]})]})]}),"\n",(0,s.jsxs)(n.p,{children:["Tunable via ",(0,s.jsx)(n.code,{children:"MERCURY_STALL_SOFT_MS"})," / ",(0,s.jsx)(n.code,{children:"MERCURY_STALL_HARD_MS"}),". Never silently kills a task \u2014 escalation surfaces in the UI."]}),"\n",(0,s.jsx)(n.h2,{id:"output-limits--none-of-mercurys-own",children:"Output limits \u2014 none of Mercury's own"}),"\n",(0,s.jsxs)(n.p,{children:["Mercury imposes ",(0,s.jsx)(n.strong,{children:"no output size limit"}),": the ceiling is 32,768 tokens, deliberately above every mainstream model's native limit, so the model's own limit governs. Providers that reject a high ",(0,s.jsx)(n.code,{children:"max_tokens"})," get an adaptive halving and the chain continues. Sectioned-write guidance remains for models with genuinely small native limits."]}),"\n",(0,s.jsx)(n.h2,{id:"auto-mode",children:"AUTO mode"}),"\n",(0,s.jsxs)(n.p,{children:["Mercury Code's default flow: read first, plan silently, implement immediately. Small/medium changes build without asking; large or consequential changes present a concise plan with a single ",(0,s.jsx)(n.code,{children:"ask_user"})," confirmation (recommended option default-selected), then build without re-asking. See the ",(0,s.jsx)(n.a,{href:"/docs/releases/1.2.3",children:"v1.2.3 release notes"})," for the AUTO-mode flow."]}),"\n",(0,s.jsx)(n.h2,{id:"testing-invariants",children:"Testing invariants"}),"\n",(0,s.jsxs)(n.p,{children:["The pipeline is pinned by regression tests (",(0,s.jsx)(n.code,{children:"completion-contract.test.ts"}),", ",(0,s.jsx)(n.code,{children:"completion-verdict.test.ts"}),", ",(0,s.jsx)(n.code,{children:"stall-watchdog.test.ts"}),", and friends):"]}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:"Pause paths precede completion delivery in the code \u2014 budget exhaustion can never produce a completion banner."}),"\n",(0,s.jsxs)(n.li,{children:["Sub-agents report ",(0,s.jsx)(n.code,{children:"paused"}),", not ",(0,s.jsx)(n.code,{children:"completed"}),", on budget exhaustion; the supervisor auto-resumes."]}),"\n",(0,s.jsx)(n.li,{children:"Every honest banner exists and is guarded in source."}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["For the full before/after walkthrough, see the ",(0,s.jsx)(n.a,{href:"/docs/releases/1.2.3",children:"v1.2.3 release notes"}),"."]})]})}function h(e={}){let{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(a,{...e})}):a(e)}},28453(e,n,t){t.d(n,{R:()=>o,x:()=>d});var r=t(96540);let s={},i=r.createContext(s);function o(e){let n=r.useContext(i);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function d(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:o(e.components),r.createElement(i.Provider,{value:n},e.children)}}}]);