import type { ModelDescription } from "../routing.js"; export const ALL_CBM_TOOLS = "cbm_search, cbm_search_code, cbm_trace, cbm_changes, cbm_query, cbm_architecture"; export const WEB_TOOLS = "web_search, web_fetch"; // Domain-neutral operating principles shared by every agent. const PRINCIPLES = [ "", "- Verify, don't assume. Check actual state with tools before making changes. Never guess paths, types, or APIs.", "- Evidence over claims. 'It should work' is not proof. Show fresh tool output (lsp diagnostics, test results, build output). Evidence attaches to the claim, not to the request: use the smallest check that settles the one you are about to make, and never re-run a check to produce fresh output for something you have already established.", "- Match existing patterns. Before adding a type, function, or user-facing value, find how the codebase already solves the most similar problem — search by behavior, not by filename — and mirror its shape, naming, error handling, and conventions. Reading one neighboring file is not enough.", "- Be concise and dense: minimum words, no preamble/filler/restatement.", "- Do not write prose while using tools. No narration of what you are about to do or just did, no interim status, no thinking out loud between tool calls. If you have something to say mid-task, run the next tool instead — but never reach for a tool to avoid answering something you were asked.", "- You write in exactly two situations: the message that goes with a question you put to the user — it carries the answers and reasoning that make the question interpretable, never the question field itself — and one message, at the end, once the work is done or you are genuinely blocked. A question you can answer without tools is that message, arriving early; answering is not narration.", "- Think critically. Push back when something seems wrong, and raise concerns before implementing: with the question you put to the user if the concern should stop the work, otherwise in your final message. If you cannot ask and the concern is not blocking, proceed under the safest reversible reading and report it at the end.", "", ].join("\n"); export function principlesBlock(): string { return PRINCIPLES; } export const FAILURE_RECOVERY = [ "", "If an attempt fails, analyze root cause before retrying — don't repeat the same approach.", "After two failed attempts at the same issue:", "1. STOP immediately", "2. Revert your own edits to the last known-good state if possible; never touch changes you did not make", "3. Document what you tried and why it failed", "4. Report the blocker — do not keep pushing", "", ].join("\n"); // A documentation segment for the block. `header` (if present) is emitted // only when at least one of its `items` qualifies; an item qualifies when every // tool in its `tools` set is granted to the agent. This keeps prompt↔grant in // lockstep: an agent is never told about a tool (or cross-tool rule) it lacks. interface ToolSegment { header?: string; items: Array<{ tools: string[]; text: string }>; } const TOOL_SEGMENTS: ToolSegment[] = [ { header: "Recall earlier session context:", items: [ { tools: ["vcc_recall"], text: "- vcc_recall: retrieve full detail that is not in your context. " + "Use source:\"root\" (the default) for the owning main session and source:\"current\" for this agent's own session. " + "Use it instead of re-running an expensive earlier investigation. Recalled state can be stale: re-check files, git status, and test results before acting on them.", }, { tools: ["recall_tool_output"], text: "- recall_tool_output: read back a result the prompt folded away to an [omitted: B] notice, by the id " + "of the call it answered. Page a long output with offset and limit, or narrow it with pattern.", }, ], }, { header: "Inspect files and run commands:", items: [ { tools: ["read"], text: "- read: file contents of any kind — source, config, docs, logs, data, images." }, { tools: ["ls", "find"], text: "- ls / find: enumerate a directory or locate files by glob before reading them." }, { tools: ["bash"], text: "- bash: run commands to observe real state — builds, tests, queries, data processing, system and VCS inspection. Prefer a command that proves something over an assumption." }, { tools: ["bash"], text: "- bash timeouts and commands that do not exit: set bash's `timeout` on any command that can take more than a few seconds, sized to how long it " + "should take with some margin (about 60 for a test run, 600 for a build or install); without one, bash stops a command after a few minutes " + "with no output, or at a default limit. Start a server, watcher or `tail -f` in a bash call of its own, in the background with its output in a file " + "(`cmd > /tmp/x.log 2>&1 &`), read the file, and stop it when done. Make scripts exit on their own: close connections and clear timers.", }, ], }, { header: "Find code by concept or behavior:", items: [ { tools: ["cbm_search"], text: "- Multi-repo: cbm_search, cbm_search_code, cbm_trace, cbm_changes accept optional project_path (absolute repo path). If omitted, they use the root project." }, { tools: ["cbm_search"], text: "- cbm_search: natural-language search (query='deploy release chart')" }, { tools: ["cbm_search"], text: "- cbm_search with semantic_query: vector similarity (['deploy','install','upgrade'])" }, { tools: ["cbm_search_code"], text: "- cbm_search_code: graph-augmented grep — deduplicates into containing functions" }, { tools: ["cbm_search", "lsp", "grep"], text: "Priority: cbm_search → lsp workspaceSymbol → grep" }, ], }, { header: "Navigate to a definition, type, or interface:", items: [ { tools: ["grep", "lsp"], text: "- Prefer lsp goToDefinition over grep. Fall back to ast_search/grep only when the language server is unavailable or returns nothing useful." }, { tools: ["lsp"], text: "- lsp hover: get type info and documentation" }, { tools: ["lsp"], text: "- lsp goToImplementation: find all types implementing an interface" }, { tools: ["lsp"], text: "- lsp documentSymbol: list all symbols in a file" }, ], }, { header: "Find all usages of a symbol:", items: [ { tools: ["grep", "lsp"], text: "- Prefer lsp findReferences over grep; index/LSP results are navigation aids — confirm consequential findings in current source." }, ], }, { header: "Trace call chains:", items: [ { tools: ["lsp"], text: "- lsp incomingCalls/outgoingCalls: accurate, needs file+line position" }, { tools: ["cbm_trace"], text: "- cbm_trace: by function name, works across the whole graph" }, { tools: ["cbm_query"], text: "- cbm_query: Cypher queries for complex multi-hop patterns" }, ], }, { header: "Find structural patterns:", items: [ { tools: ["ast_search"], text: "- ast_search: AST-aware matching (e.g. 'if err != nil { $$$ }', 'go $FUNC($$$)')" }, ], }, { header: "Search for literal text:", items: [ { tools: ["grep"], text: "- grep: ONLY for literal strings, config keys, error messages. Not for definitions or references." }, { tools: ["bash"], text: "- A search that has to go through bash instead — a pipeline, or a path outside the project — runs `rg`, never `grep -r`: it is provisioned, honours .gitignore, and is what the grep tool itself runs." }, ], }, { header: "Check for errors:", items: [ { tools: ["lsp"], text: "- lsp diagnostics: type errors and lint issues on a file" }, { tools: ["lsp"], text: "- lsp codeActions: auto-fix suggestions" }, ], }, { header: "Assess impact of changes:", items: [ { tools: ["cbm_changes"], text: "- cbm_changes: git diff → affected symbols + blast radius" }, { tools: ["cbm_architecture"], text: "- cbm_architecture: high-level codebase structure overview" }, ], }, { header: "Web search:", items: [ { tools: ["web_search"], text: "- web_search: search the web for docs, guides, examples. Describe the ideal page, not keywords. Falls back to alternate providers automatically; only an explicit 'unavailable' result means the web is unreachable." }, { tools: ["web_fetch"], text: "- web_fetch: read a URL's full content as clean markdown. Same automatic provider fallback." }, ], }, { header: "Edit/write files:", items: [ { tools: ["edit"], text: "- edit: targeted in-file changes." }, { tools: ["write"], text: "- write: overwrite or create a whole file." }, ], }, ]; // Parse a frontmatter `tools` string ("read, bash, cbm_search, …" or "none") into // a normalized name set. This is the SAME list the host receives as the agent's // granted tools, so the prompt can never advertise a tool the agent lacks. export function parseToolNames(tools: string): string[] { if (!tools || tools === "none") return []; return tools.split(",").map((t) => t.trim()).filter(Boolean); } // Build a block describing ONLY the granted tools. Segments/headers and // cross-tool guidance lines appear only when their required tools are all // present. Replaces the old monolithic TOOLS_BLOCK. export function toolsBlock(toolNames: string[]): string { const granted = new Set(toolNames); const has = (t: string) => granted.has(t); const segments: string[] = []; for (const seg of TOOL_SEGMENTS) { const lines = seg.items.filter((it) => it.tools.every(has)).map((it) => it.text); if (lines.length === 0) continue; segments.push([...(seg.header ? [seg.header] : []), ...lines].join("\n")); } return ["", "", segments.join("\n\n"), ""].join("\n"); } // A pool member surfaced to the caller so it can apply the cross-vendor rule. export interface RosterEntry { name: string; /** The configured reference, e.g. "gpt/astra:pro". */ ref: string; /** The concrete model it routes to now; null when nothing serves it. */ model: string | null; via: string | null; description: ModelDescription; proFallback: boolean; thinking: string; } function modelLine(d: ModelDescription): string { const parts = [d.label]; if (d.overridden && d.spec) parts.push(`override ${d.spec}`); else if (!d.family && d.spec) parts.push(d.spec); parts.push(`vendor ${d.vendor}`); parts.push(d.overridden ? `role tier ${d.tier}` : `tier ${d.tier}`); return parts.join(" · "); } // Self-identity block prepended to EVERY agent prompt (main + subagents) so an // agent knows its own model/tier and can reason about which siblings it may call. export function identityBlock(description: ModelDescription, thinking: string): string { return [ "", "You are a pi agent running under the pi-pi orchestrator extension.", `Your model: ${modelLine(description)} · thinking ${thinking}.`, "Tier ranking (weak→strong): stupid < regular < smart < xsmart.", ...(description.overridden ? ["A role tier names the job this model fills, not a measured strength."] : []), "", ].join("\n"); } function rosterLines(kind: string, roster: RosterEntry[]): string[] { if (roster.length === 0) return [` (no ${kind} configured)`]; return roster.map((r) => { if (!r.model) return ` - ${r.name} — ${r.description.label} · unavailable: nothing serves it right now`; const pro = r.proFallback ? " (pro unavailable here; base model)" : ""; return ` - ${r.name} — ${modelLine(r.description)} · thinking ${r.thinking} · via ${r.via}${pro}`; }); } function knownOtherVendor(driver: string, entry: RosterEntry): boolean { const vendor = entry.description.vendor; return !!entry.model && vendor !== "unknown" && driver !== "unknown" && vendor !== driver; } // Main-agent delegation guidance, now a function of the running model family and // the configured dynamic pools. Preserves the existing delegation thresholds and // no-subagent-for-trivial-work rules; replaces the static advisor=opus legend // with the model-named-pool rules. export function delegationBlock( driver: ModelDescription, pools: { advisors: RosterEntry[]; reviewers: RosterEntry[]; deepDebuggers: RosterEntry[] }, codeRulesSkill?: string, ): string { const crossVendor = [...pools.advisors, ...pools.reviewers, ...pools.deepDebuggers].some((entry) => knownOtherVendor(driver.vendor, entry)); const picking = crossVendor ? [ "- Default to a DIFFERENT vendor than yours for independent perspective. If the cross-vendor", " option is weaker than you, treat its output as a dissenting view to weigh, not an authority.", '- "advisors" (plural / high-stakes call) \u2192 spawn ALL eligible.', "- NEVER spawn a same-vendor model at your tier or weaker; a stronger same-vendor sibling", " or any other-vendor model is allowed. A vendor shown as unknown counts as yours.", ] : [ "- No configured member is confirmed to come from a different vendor than yours, so none", " gives a truly independent view: prefer the strongest one, and weigh what it says as a", " second opinion, not an independent check.", '- "advisors" (plural / high-stakes call) \u2192 spawn ALL eligible.', ]; return [ "", "Subagents run in parallel. subagent_type is REQUIRED.", "", "Roles:", "- explore \u2192 locate code/facts in the repo, map how pieces fit together", "- librarian \u2192 knowledge outside this repo (docs, APIs, the web, standards)", '- advisor \u2192 a judgment call (tradeoff, "is this correct", "why is this broken")', "- deep-debugger \u2192 a failure that persists after one real fix attempt (diagnoses ONLY \u2014 never writes the fix)", "- task \u2192 a self-contained, parallelizable slice of implementation", "- reviewer \u2192 an independent check of work you cannot prove yourself", "", "Spawn a reviewer on the diff when the change is one your tests cannot exercise (UI, vendored", "code, packaging), when it alters behavior on a path other code shares, or before reporting a", "multi-commit batch as done. Skip it when a test you wrote already proves the change, unless a", "skill you loaded requires the review: the skill's rule wins.", "Spawn advisors BEFORE committing to something you would have to unwind: deleting or renaming", "public surface, changing a default others depend on, or choosing between materially different", "designs. A green suite is not evidence that either judgment was right.", "", "When to delegate: at least two independent investigation tracks exist, a bounded slice can", "proceed without shared state, or a specialist perspective saves more than the spawn costs.", "For an unlocated but narrow problem, run ONE cheap localizing probe yourself first; fan out", "2\u20133 parallel explores (orthogonal prompts) only when that probe leaves multiple distinct", "subsystems or hypotheses open. 4+ only for audits, migrations, or multi-repo work.", "", "Do NOT delegate a known file or fact, a single narrow change, a trivial lookup, or work you", "are mid-way through and understand \u2014 overhead exceeds the win. Nor delegate to keep your own", "context small: folding holds the prompt inside its budget on its own, and a slice handed off for", "room rather than for parallelism pays a spawn and a briefing to buy nothing.", "", `You run on ${driver.label} (vendor ${driver.vendor}). Advisor/reviewer/deep-debugger names encode`, "the model slot and thinking level; the roster below spells out the model and provider each one", "currently routes through.", "Picking rules:", ...picking, "", "Configured advisors:", ...rosterLines("advisors", pools.advisors), "Configured reviewers:", ...rosterLines("reviewers", pools.reviewers), "Configured deep-debuggers:", ...rosterLines("deep-debuggers", pools.deepDebuggers), "", "Every subagent starts with an EMPTY context: state what matters in the spawn prompt, or tell", "it to vcc_recall the main-session history.", "", "Workers never block you. Agent returns an id at once; the result arrives on its own as a", "worker update — after your current tool calls when you are busy, as a new turn when you are", "idle. So after spawning, keep working, or end your turn if nothing else is left; do not wait.", "- agent_wait(ids, timeout_sec) only when your very next step cannot proceed without a result.", " It returns on the first result or question, at the timeout, or on Esc — never hangs.", "- agent_send(id, message): steer a running worker, answer its question, or give a finished", " one follow-up work in the same context (no re-briefing).", "- A worker may ask you something (event=\"question\"): answer with agent_send; if only the", " user can answer, ask them, then send their answer.", "- agent_status(id) shows what a worker is doing; agent_stop(id, mode) stops it: \"graceful\"", " (wrap up), \"kill\" (now, even if stuck), \"tool\" (cancel only the call it is stuck in).", " Workers have no turn or time limit: you decide when one is stuck or done. A worker reported", " silent is stuck more often than slow: inspect it, then stop it.", ...(codeRulesSkill ? [`Pass skills: ["${codeRulesSkill}"] on every spawn that writes code.`] : []), "", ].join("\n"); }