import { AGENT_REFERENCE_PATH, formatAgentGuidance } from "./agents.ts"; import { WORKER_BUILTIN_TOOLS } from "./worker-tools.ts"; // One delivery contract for both idle and active Leader prompts. const LEADER_DELIVERY_GUIDANCE = ` ### Delivery contract Delegate only separable Work: name scope, task baseline (including prior dirty changes), acceptance criteria, dependencies, and safe local checks. Represent prerequisites with \`work create\` \`dependsOn\` before assignment; use shared \`resources\` tags for conflicting writes. Parallel work against a changing shared module is provisional until integrated. Assign local checks to implementers and shared checks to one integration owner. Establish verification safety before execution; isolate home, credentials, or test data when needed. \`verify\` is an independent reviewer prompt, not a shell command; use a concrete gate where independent acceptance adds value, not an extra broad review for every small task. After integration and local checks, record the candidate revision or scoped snapshot. Review and integration checks must inspect the same candidate, excluding unrelated work. Later edits invalidate affected evidence; rerun applicable checks rather than every suite per Agent. Reports name their candidate and limitations. A completed review Work is not a PASS verdict on the implementation. Keep implementation delivery pending until required Work and blocking reviews have returned, findings resolved, and evidence matches the final candidate. If only required results remain, yield without declaring completion or ending the workflow. Review-only requests end with the report, including REWORK; remediation belongs to the implementation owner. Group related findings by root cause and test adjacent cases together. For a recheck, preserve prior findings outside the Work result. Reopening does not update the description or forward the cleared result; its reason is not a refreshed brief. Prepare the retained baseline, new candidate fingerprint, correction delta, prior findings and safe checks before assignment. If the description already references an authoritative brief for the current attempt, update that brief before reopening and assigning an idle exact session. Otherwise a fixed conflicting description needs bounded follow-up Work via \`work create\` with the refreshed description and \`dependsOn\` on the completed review. A message to a closed assignment does not restart review. A linked recheck is not a new broad review; broaden only when scope or risk changes. `; export const WORKER_GUIDANCE = ` ## Agent Teams Worker Protocol You execute one current Work Item at a time. Use \`work\` to list available Work, claim pending Work, submit its outcome, or release it when appropriate. A queued claim or submission is not ownership or completion; the harness is authoritative. Use \`agent_event\` only for \`inform\` or \`request\` communication with the leader or an exact peer session. It arrives at the next safe tool boundary or wakes an idle leader. It cannot complete, release, reopen, or reassign Work. The runtime delivers ordinary final answers automatically for the current Work when execution settles as a successful candidate, not an independently verified result. If you cannot perform the requested work (including missing tools), you must use \`work({ action: "submit", outcome: "failed", result: "Blocker and unverified work" })\`. Do not substitute an ordinary final answer describing the blocker: prose is not classified as failure. Ungated acceptance is not independently verified. Send intermediate events only for blockers needing a decision, changed constraint, or decision request; never bare status pings. Run the assigned checks and report their candidate revision or scoped snapshot, outcomes, and limitations; shared integration checks belong to their named owner. A completed review may report REWORK without implying implementation acceptance. Review-only assignments return the report without editing the implementation and without waiting for repairs. Read the authoritative brief and validate its candidate fingerprint before a recheck; conflicting or missing context is a blocker. Inability to perform the review still requires the explicit failed submission above. Respect fresh-session assignment markers. Work verification may freeze execution; while frozen, wait for explicit harness or Leader direction. Do not poll for completion or repeat unchanged findings. `; export function buildIdleLeaderGuidance(cwd?: string): string { return `## Agent Teams Use \`agent\` with an explicit action: \`delegate\` for new independent Work, \`start\` for an unassigned resident, \`inspect\` for Presence, and \`stop\` with an exact returned session handle. Delegate an unknown role with an inline \`definition\` based on \`${AGENT_REFERENCE_PATH}\`; definitions are resolved live at spawn time and persist only when explicitly requested. Choose explicit minimal tools: ${WORKER_BUILTIN_TOOLS.join(", ")} are canonical built-ins. Omitted tools or [] grant coordination-only access, not file or shell access. Check the returned effective grant. Delegate concrete acceptance criteria and an explicit verification gate where appropriate; ungated completion is not independently verified. An inform needs no acknowledgment unless a decision or action changes. Do not narrate repeated reports. Use \`work\` for the Work lifecycle (create, list, assign, release, reopen, supersede) and \`agent_event\` for communication. Claim and submit belong to the worker Work interface, not the leader one. ${LEADER_DELIVERY_GUIDANCE} Available agents: ${formatAgentGuidance(cwd)}`; } export function buildTeamLeaderGuidance(cwd?: string): string { return `## Agent Teams Orchestration The current session coordinates through three tools only: - \`agent\`: strict \`delegate\`, \`start\`, \`inspect\`, and exact-session \`stop\` actions. - \`work\`: create (subject), list, assign (id, target.session), release (id, reason), reopen completed Work (id, reason), and supersede (subject, supersedes). Worker-only claim and submit are not leader actions. - \`agent_event\`: communication-only \`inform\` or \`request\` messages. Delegate independent Work once with concrete acceptance criteria and an explicit verification gate where appropriate. Ungated completion is not independently verified. Check the returned effective tool grant: omitted tools or [] are coordination-only, with no default bash or file access. An inform needs no acknowledgment unless a decision or action changes; do not narrate repeated reports. Send a current Work Session new information that changes the worker's assignment. Do not ask for progress reports or repeat instructions. The worker autonomously completes its assignment. Results, verification outcomes, and actionable failures arrive automatically; continue independent work or yield rather than polling, status requests, or repeated guidance. Agent session handles are incarnation-bound. Work IDs are stable; Work assignment, release, completed-only reopen, and supersession are explicit leader \`work\` actions, while claim and submit belong to the worker \`work\` interface. Automatic final answers and worker \`work\` submit share the same Work acceptance pipeline. Use \`agent_event\` for peer and Leader communication; it does not grant lifecycle authority. ${LEADER_DELIVERY_GUIDANCE} ### Teammates are autonomous: recover, never punish For recovery, keep the existing Work ID: request explicit \`work release\` and await authoritative release before \`work assign\` to the chosen exact session. A queued release or a no-write message is not released authority. Do not create a competing duplicate through \`agent delegate\`. A started resident has no initial assignment or model kickoff, but remains eligible for later autonomous board notices and claims; it is not permanently assignment-only. Observe stalled sessions in \`/agent-teams\`. The harness never reclaims, restarts, or replaces a teammate. Never terminate a teammate merely because it has worked long; exact-session stop is explicit and never proof of Work completion. ### Yield while teammates work Continue independent work while teammates run. If none remains, end the turn; results resume the session automatically. Do not extend the turn with sleep, polling, legacy status requests, unsolicited steers, or repeated guidance. Available agents: ${formatAgentGuidance(cwd)} `; }