{"modulePath":"github.com/Agent-Field/codeaf","version":"v0.8.4","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package delegate\n\nPackage delegate is what codeaf needs of the programs it carries and can hand a whole task to — senior-dev first (docs/design/delegate/PROTOCOL.md): what one IS (a Go value in the build's list, internal/delegate/builtin), the command line every one of them answers (\\`codeaf \\\u003cname\u003e …\\`), the records a running one writes on its stdout and the one reader over them, the model API that is its only road to a model, the log of the conversation it holds over that road, and the launch of it as a child process that streams, stops on SIGTERM and ends with one terminal record.\n\n\"DELEGATE\" IS A WORKING TITLE. Everything a person reads names the program itself — \\`/senior-dev\\`, \\`codeaf senior-dev\\`, its own manual page — and only code says delegate, where a later rename is one package move.\n\nA PROGRAM CODEAF CARRIES IS STILL A PROGRAM APART. It is compiled into this binary, but it runs as a child process of it (\\`codeaf \\\u003cname\u003e run --json …\\`), so a crash in its engine cannot take the chat down, and it reaches a model only through the API codeaf serves it for that one run, so it never holds a key. What runs one AS A WORKER of a run — the live step, the trajectory, the spend bank — is internal/run's; nothing here knows what a task is.\n\nTHIS PACKAGE IS A LEAF ON PURPOSE. The session door lists the programs and checks a name; the run engine seats one; the command line runs one; none of them may import the others, so what they share lives here and imports none of them.\n\n## Constants\n\n```go\nconst (\n\tActionStage = \"stage\"\n\tActionStep  = \"step\"\n\tActionEnd   = \"end\"\n)\n```\nThe kinds of line the log holds: a stage record, a step record, and the terminal record's status and message.\n\n```go\nconst (\n\t// LandsTree is a program that edits files in the folder it is given and\n\t// leaves its changes there: in a git repository on a branch codeaf cut for\n\t// the run and left checked out, with what it left uncommitted committed\n\t// onto that branch when it ends (internal/session's programfolder.go).\n\tLandsTree = \"tree\"\n\t// LandsText is a program that changes nothing in the folder and puts its\n\t// answer in the terminal record's deliverable: codeaf folds the text into\n\t// the conversation the way a quick task's answer arrives.\n\tLandsText = \"text\"\n)\n```\nThe two things a program can leave behind.\n\n```go\nconst (\n\tEnvModelAPI   = \"CODEAF_MODEL_API\"\n\tEnvModelToken = \"CODEAF_MODEL_TOKEN\"\n)\n```\nThe model API's two names in a program's environment: the OpenAI-style base URL codeaf serves this one run, and the token that opens it and nothing else. They are the ONLY road to a model a program has.\n\n```go\nconst (\n\t// An autonomous senior-dev run with no person watching must stop on its own.\n\tDefaultSeniorDevCostUSD = 10.0\n\t// An autonomous senior-dev run with no person watching must stop on its own.\n\tDefaultSeniorDevHours = 3.0\n)\n```\nsenior-dev's own unattended ceilings ([Delegate.Unattended](#Delegate.Unattended)). They are named here rather than in its package because the manual's truth gate and the session's tests read them without linking its engine.\n\n```go\nconst (\n\tFromPerson       = \"person\"\n\tFromConversation = \"conversation\"\n\tFromWorker       = \"worker\"\n)\n```\nWho a message is from, as a program is told it.\n\n```go\nconst (\n\t// RecordHello is the first line a program writes: the protocol it speaks,\n\t// its name, and the stages it will move through, in order.\n\tRecordHello    = \"hello\"\n\tRecordStage    = \"stage\"\n\tRecordStep     = \"step\"\n\tRecordTerminal = \"terminal\"\n\t// RecordHeard names the messages a listening program has put before its\n\t// model (inbox.go), and RecordInbox says it has stopped reading them.\n\tRecordHeard = \"heard\"\n\tRecordInbox = \"inbox\"\n)\n```\nThe record types.\n\n```go\nconst (\n\tStatusPass    = \"pass\"\n\tStatusFail    = \"fail\"\n\tStatusBudget  = \"budget-exhausted\"\n\tStatusCrashed = \"crashed\"\n)\n```\nThe terminal statuses. The set is closed and it is senior-dev's, because senior-dev's projection of an ending onto four words was already the right one: the work stands, it does not, a ceiling stopped it, or the program itself broke.\n\n```go\nconst AcceptMessages = \"messages\"\n```\nAcceptMessages is the word a listening program's hello carries.\n\n```go\nconst ActionsFile = \"delegate-actions.jsonl\"\n```\nActionsFile is the log's name inside a task's record folder.\n\n```go\nconst ConversationFile = \"delegate-conversation.jsonl\"\n```\nConversationFile is the log's name inside a task's record folder.\n\n```go\nconst DefaultGrace = 15 * time.Second\n```\nDefaultGrace is how long a SIGTERM has to work before SIGKILL follows. It is the job registry's own two seconds plus what a program that has to write a terminal record and close a database needs: senior-dev ships its frozen tree on the way out, and a grace that cut that short would lose the one record the whole protocol exists for.\n\n```go\nconst EnvInbox = \"CODEAF_INBOX\"\n```\nEnvInbox names the inbox file in a listening program's environment.\n\n```go\nconst EnvRecords = \"CODEAF_RECORDS\"\n```\nEnvRecords names the run's own record folder in a program's environment: the task's folder for a run the chat started, the run's folder under \\`~/.codeaf/v3/carried/\\\u003cname\u003e/\\` for a shell run. It is where a program that lands text leaves the files its answer points at, because the folder it works in is the person's and it promised to change nothing there.\n\n```go\nconst GuideMax = 400\n```\nGuideMax is the most bytes a program's [Delegate.Guide](#Delegate.Guide) may take. It is a paragraph a model reads on every turn of every conversation that carries the program, so it is held to what a model needs to choose the program and brief it, and the program's manual page carries the rest.\n\n```go\nconst HoldEnv = \"CODEAF_PROGRAM_HOLD_FD\"\n```\nHoldEnv names, in a program's environment, the descriptor its host's hold on the program's folder was handed to it on ([Launch.Hold](#Launch.Hold)).\n\nTHE HOLD OUTLIVES A HOST THAT DIES. The hold is a flock, and a flock belongs to the open file, not to the process: shared with the program, it is let go only when the program has gone too. A host killed outright once let the next codeaf take its run's copy, commit what was in it and remove it, while the program — still inside the grace it is given to stop (\\[watchHost]) — was restoring its candidate and committing its last edits there.\n\n```go\nconst InboxName = \"delegate-inbox.jsonl\"\n```\nInboxName is the inbox file's name in a task's record folder.\n\n```go\nconst MainThread = \"main\"\n```\nMainThread is the thread a call belongs to when the program gave it no other: its one long conversation.\n\n```go\nconst ProgramFile = \"delegate-program.json\"\n```\nProgramFile names, inside a task's record folder, which program the run handed its task to and the stages it said it would move through (its \\`hello\\`). The worker writes it when the hello arrives and again, whole, when the program's process is gone — then whether or not a hello ever came, so a program that died early still has its clock; the task page reads it to say whose conversation it is drawing, after the run as well as during.\n\n```go\nconst ProtocolVersion = 2\n```\nProtocolVersion is the version \\`hello\\` carries. Both ends are this package, so it moves only when a record changes meaning, and a mismatch means the two processes are two builds.\n\nAN OPTIONAL FIELD ADDED TO A RECORD IS NOT A NEW MEANING. A stage's \\`data\\` and a step's \\`tool\\`, \\`step\\` and \\`exit\\` arrived inside version 2: a reader that predates them ignores them as it ignores every field it does not know, and a program that does not send them is read exactly as before.\n\n```go\nconst StageDataCap = 1024\n```\nStageDataCap is the most bytes a stage record's data may take, in JSON. It is a curated copy of what the program already knows about the phase — an attempt number, a count, a verdict of its own checks — for a page to say in words, and never the program's whole account of itself, which stays on its stderr. A reader drops data past it rather than cut it, because half an object is not an object; the program is expected to have curated to it, and senior-dev does (internal/seniordev/app's stage\\_data.go).\n\n## Variables\n\n```go\nvar ErrHelp = flag.ErrHelp\n```\nErrHelp is Parse's answer when the line asked for help and got it.\n\n```go\nvar ErrInboxMessage = errors.New(\"the inbox cannot carry this message\")\n```\nErrInboxMessage distinguishes words the inbox cannot carry from an I/O failure, so one bad message does not block every later message.\n\n```go\nvar ErrNoTerminal = errors.New(\"the program exited without a terminal record\")\n```\nErrNoTerminal is the error a launch answers when the program exited without a terminal record and was not stopped by the caller: the run did not finish in the protocol's terms, whatever the exit code said.\n\n```go\nvar SeniorDevCeilings = Ceilings{CostUSD: DefaultSeniorDevCostUSD, Hours: DefaultSeniorDevHours}\n```\nSeniorDevCeilings is senior-dev's [Delegate.Unattended](#Delegate.Unattended).\n\n## Functions\n\n```go\nfunc AnsweredModels(dir string) ([]string, error)\n```\nAnsweredModels is the distinct model set that actually answered this run, in first-answer order. An unfinished, refused or failed call credits nobody.\n\n```go\nfunc AppendAction(dir string, action Action) error\n```\nAppendAction writes one line to the log in dir, capped, in one write, making the folder when it is not there.\n\n```go\nfunc AppendInbox(path string, message Message) error\n```\nAppendInbox adds one message to the inbox at path, whole, on one line.\n\n```go\nfunc AppendTurn(dir string, turn Turn) error\n```\nAppendTurn writes one turn to the log in dir, capped, in one write, making the folder when it is not there.\n\n```go\nfunc ChildArgs(program Delegate, workspace, brief string, ceilings Ceilings, facts RunFacts) []string\n```\nChildArgs is the line a host starts a program's process with, after codeaf's own executable: the name, the default command, --json, the folder, the ceilings that are set, and the brief after \\`--\\`, so no word of it can be read as a flag. [Parse](#Parse) reads it back to the same invocation.\n\nAN UNSET CEILING IS NOT ON THE LINE. A program handed \\`--max-cost 0\\` might read it as a ceiling of nothing; one handed no flag reads no ceiling.\n\nThe facts codeaf read about the run put the program's own flags on the line after codeaf's: [Delegate.PlainFolder](#Delegate.PlainFolder) for a folder with no git history, and [Delegate.CrewFlags](#Delegate.CrewFlags) for the conversation's crew.\n\n```go\nfunc ChildEnv(api ModelAPI) []string\n```\nChildEnv is the environment a program's process starts in: this process's, with every provider key and model redirection codeaf knows of taken out, and the model API's two names set.\n\nNO PROVIDER KEY IS INHERITED BY A PROGRAM. The child itself receives only the loopback model token; the model's shell strips that token as well. A redirection left here would let a program reach a model outside the API, the one road codeaf can meter and show a person.\n\n```go\nfunc ExitWord(exit *int) string\n```\nExitWord is how a command came out, in the words every program's page uses: \\`passes\\` for an exit of 0, \\`fails · exit N\\` for any other, and nothing for an action that ran no command or learned no exit.\n\n```go\nfunc Help(program Delegate, out io.Writer)\n```\nHelp writes a program's help: what it is, its commands, and the flags every command takes.\n\nEVERY LINE FITS EIGHTY CELLS, the width codeaf's own help pages are held to (cmd/codeaf's helpwidth law); the build's list holds every carried program's pages to it (internal/delegate/builtin).\n\n```go\nfunc KnownStatus(status string) bool\n```\nKnownStatus answers whether a terminal status is one of the four.\n\n```go\nfunc RecordsEnv(folder string) []string\n```\nRecordsEnv is the environment entry that names a run's record folder to its program, nil for none.\n\n```go\nfunc RunChild(ctx context.Context, inv *Invocation, stdout io.Writer) string\n```\nRunChild runs a parsed invocation as the child of a host: its records go to stdout as JSON lines and its models come from the environment. It answers the status of the ending it wrote, and the caller turns that into the exit code.\n\nEXACTLY ONE TERMINAL, ON EVERY PATH. A body that returns without writing one gets one written for it here — the context's end, the error it returned, or the plain fact that it said nothing — because a host reads a missing terminal as work that did not finish and says only that, and the reason the body knew would be lost.\n\nAND IT ENDS WHEN ITS HOST DOES, however the host went (\\[watchHost]).\n\n```go\nfunc WriteProgram(dir string, record ProgramRecord) error\n```\nWriteProgram writes the record, whole, making the folder when it is not there.\n\n## Types\n\n```go\ntype Action struct {\n\t// At is when codeaf received the record.\n\tAt   time.Time `json:\"at\"`\n\tKind string    `json:\"kind\"`\n\t// Stage, Status and Data are a stage's; Status is the ending's too.\n\tStage  string          `json:\"stage,omitempty\"`\n\tStatus string          `json:\"status,omitempty\"`\n\tData   json.RawMessage `json:\"data,omitempty\"`\n\t// Tool, Step, Command, Observation and Exit are a step's.\n\tTool        string `json:\"tool,omitempty\"`\n\tStep        string `json:\"step,omitempty\"`\n\tCommand     string `json:\"command,omitempty\"`\n\tObservation string `json:\"observation,omitempty\"`\n\tExit        *int   `json:\"exit,omitempty\"`\n\t// Added and Removed are a step's lines added and removed, when the program\n\t// counted them.\n\tAdded   *int `json:\"added,omitempty\"`\n\tRemoved *int `json:\"removed,omitempty\"`\n\t// Message is the ending's one sentence.\n\tMessage string `json:\"message,omitempty\"`\n}\n```\nAction is one line of the log.\n\n```go\nfunc EndAction(at time.Time, t Terminal) Action\n```\nEndAction is the terminal record as the log's last line: its status and its sentence. The rest of the record is the result's, which the run keeps whole.\n\n```go\nfunc ReadActions(dir string, n int) ([]Action, error)\n```\nReadActions reads the log in dir in the order it was written, and the last n lines of it (n \\\u003c= 0 for all). A log that is not there is no actions and no error — a run from before the log existed, or one whose program has said nothing yet — and a line that does not parse is skipped, because a log cut mid-write is still a log.\n\n```go\nfunc StageAction(at time.Time, record StageRecord) Action\n```\nStageAction is a stage record as a line of the log, received at at.\n\n```go\nfunc StepAction(at time.Time, record StepRecord) Action\n```\nStepAction is a step record as a line of the log, received at at.\n\n```go\ntype ActionReader func(Action) (Shown, bool)\n```\nActionReader reads a program's action log for its page: each line, in the order the log holds them, as the words a person reads ([Shown](#Shown)), and false for a line the page leaves out. A reader may remember the lines before — a program can only tell its model's second nudge from a retry of the first by what came earlier — so one reader reads one log, from its first line.\n\n```go\ntype Body func(ctx context.Context, host Host, args []string) error\n```\nBody is a command's work. It runs to its ending and reports through the host — the ending included, as one [Host.Terminal](#Host.Terminal) — and answers an error only for a failure it could not put into that record itself. args is what the flags left on the line: the brief's words.\n\n```go\ntype Ceilings struct {\n\tCostUSD float64\n\tHours   float64\n}\n```\nCeilings are a run's limits. Zero is none.\n\n```go\nfunc (c Ceilings) CappedBy(unattended Ceilings) Ceilings\n```\nCappedBy is a conversation's remaining limits held to a program's unattended ceilings: an absent or unreadable limit becomes the program's, and a larger one is cut to it. A field the program leaves at zero is passed through untouched, so a program with no ceiling of its own runs on what is left.\n\n```go\nfunc (c Ceilings) Elapsed() time.Duration\n```\nElapsed is the hours as a duration, zero for none.\n\n```go\nfunc (c Ceilings) FilledFrom(unattended Ceilings) Ceilings\n```\nFilledFrom is a shell line's ceilings with the program's unattended ones put where the line set none. A ceiling the person typed is kept as typed, larger or not: at a shell they are the one watching.\n\n```go\nfunc (c Ceilings) IsZero() bool\n```\nIsZero says neither ceiling is set.\n\n```go\nfunc (c Ceilings) SeniorDev() Ceilings\n```\nSeniorDev is [Ceilings.CappedBy](#Ceilings.CappedBy) senior-dev's ceilings.\n\n```go\nfunc (c Ceilings) SeniorDevDefaults() Ceilings\n```\nSeniorDevDefaults is [Ceilings.FilledFrom](#Ceilings.FilledFrom) senior-dev's ceilings.\n\n```go\nfunc (c Ceilings) Summary() string\n```\nSummary says the two ceilings as the person sees them at either start door, and says nothing of a ceiling that is not set: none at all is \"\", never \\`up to $0.00 and 0h\\` (the emptiness law).\n\n```go\nfunc (c Ceilings) TimeWord() string\n```\nTimeWord spells the wall ceiling without padded zero units.\n\n```go\ntype Command struct {\n\tName string\n\t// Usage is the shape of the line after the command's name, for its help:\n\t// `[flags] -- \u003cbrief\u003e`.\n\tUsage   string\n\tSummary string\n\t// Bind declares the command's own flags on fs and answers its body, which\n\t// reads them once the line has been parsed. It is called once per\n\t// invocation, so the values live in the closure and never in package\n\t// state. codeaf's shared flags (--dir, --max-cost, --max-hours, --json) are\n\t// already on fs; a command may not declare them again.\n\tBind func(fs *flag.FlagSet) Body\n}\n```\nCommand is one verb a program answers to: \\`codeaf \\\u003cname\u003e \\\u003ccommand\u003e \\[flags] -- \\\u003cbrief\u003e\\`.\n\n```go\ntype Crew struct {\n\t// Brain is the planning seat: the model the crew thinks hardest with.\n\tBrain string\n\t// Hands is the working seat: the model the crew does the work with.\n\tHands string\n\t// Light is the cheap seat: summaries, and whatever needs no depth.\n\tLight string\n\t// Asked is the models the person asked this run to work with, in their\n\t// words' order, already resolved to ids. When it is set it is the working\n\t// seat in place of Hands, and a program may not swap any of it for another:\n\t// one it cannot use is a refusal, said before anything is spent.\n\tAsked []string\n\t// Effort is how hard the working seat is asked to think, a rung of\n\t// internal/effort: the one the conversation chose for this hand-off, or\n\t// the one written on the person's working seat. Empty leaves it to the\n\t// program's own default.\n\tEffort string\n}\n```\nCrew is the models a conversation's crew seats, by what each is for, as ids on the service codeaf's model API speaks for (\\`vendor/model\\`), with no effort suffix. An empty field is a seat the crew leaves unset.\n\n```go\nfunc (c Crew) IsZero() bool\n```\nIsZero says the crew names no model and no effort, so no flag is owed for it.\n\n```go\ntype Delegate struct {\n\t// Name is one lowercase word with single hyphens: the chat command\n\t// (`/\u003cname\u003e \u003cbrief\u003e`), the command line's verb (`codeaf \u003cname\u003e`) and the\n\t// word every row says out loud.\n\tName string\n\t// Summary is one sentence saying what it does, in a person's words: the\n\t// command row's tail and its line in `codeaf --help`.\n\tSummary string\n\t// Guide is the program describing itself to the model that hands it work:\n\t// what it is for, what its brief must hold, and what it needs of its\n\t// folder. The conversation prints it under the program's name, where the\n\t// model reads which programs it can name in `via`, and says nothing about\n\t// the program of its own.\n\t//\n\t// THE PROGRAM OWNS WHAT IS TRUE OF IT, AND CODEAF OWNS WHAT IS TRUE OF\n\t// EVERY PROGRAM. The folder a program works in, where its work is left and\n\t// the fact that nobody can be asked anything are codeaf's mechanics, stated\n\t// once beside the list; a guide that restated them would be one more copy\n\t// to drift. A second program brings its own guide, and the conversation's\n\t// page never has to learn its name.\n\t//\n\t// IT RIDES EVERY REQUEST OF EVERY TURN, because the paragraph is part of\n\t// the conversation's fixed prefix (internal/session's prefixbudget_test.go\n\t// weighs it), so it is one paragraph of at most [GuideMax] bytes.\n\tGuide string\n\t// Lands is LandsTree or LandsText. Empty reads as LandsTree, because a\n\t// program that edits a tree is the one this was built for.\n\tLands string\n\t// PlainFolder is the flags the default command takes to work in a folder\n\t// with no git history, which codeaf puts on the line itself when the folder\n\t// it hands a tree program is one ([ChildArgs]). Empty is a program that\n\t// needs no flag for it, or cannot work there and says so in its ending.\n\t//\n\t// CODEAF DECIDES, BECAUSE CODEAF KNOWS. The folder is the one the task was\n\t// proposed on, and whether the program works there on a branch of its own\n\t// is read by codeaf before the program starts: a folder with no history, or\n\t// in a repository at the home folder, is worked in without git, and the\n\t// program is told so on its line. The program says only how it is told, so\n\t// codeaf never has to learn its flag's name.\n\tPlainFolder []string\n\t// Notes is the folder, relative to the folder it works in, where the\n\t// program keeps its own records while it works: its copy of the brief, its\n\t// checklist, its session's database and its whole conversation with its\n\t// model. Empty is a program that keeps nothing there.\n\t//\n\t// THE NOTES ARE MOVED OUT OF THE FOLDER. A program works in the person's\n\t// folder itself, so its records were left there when it ended — 46 files\n\t// for one senior-dev run, a database and the full conversation among them —\n\t// where a `git add -A` would commit them and the next run would read them\n\t// as its own. codeaf moves the folder this names into the run's own record\n\t// folder when the run ends, unless it was there before the run began.\n\tNotes string\n\t// Listens says the program reads the messages codeaf sends it while it\n\t// works (inbox.go): the person's words from its task's page and the\n\t// conversation's `say`. A program that does not listen is refused a message\n\t// in so many words, never handed one nothing reads.\n\tListens bool\n\t// Unattended is the ceilings a run of this program starts under when\n\t// nobody set smaller ones: a conversation with no limit, or a shell line\n\t// with no --max-cost or --max-hours. A zero field is no ceiling of the\n\t// program's own, which leaves that run on whatever the conversation has\n\t// left.\n\t//\n\t// A PROGRAM NOBODY WATCHES MUST STOP ON ITS OWN, AND HOW LONG IT MAY GO IS\n\t// THE PROGRAM'S TO KNOW. senior-dev's change and sec's whole-repository\n\t// audit are different shapes of work: an audit spends its hours long\n\t// before its dollars, which a change does not ([SeniorDevCeilings] and\n\t// secaf.Unattended say each one's figures and why). So the figure lives\n\t// beside the program rather than in a branch of codeaf that names it\n\t// ([Ceilings.CappedBy], [Ceilings.FilledFrom]).\n\tUnattended Ceilings\n\t// FollowUp is one sentence the conversation is handed when a run of a\n\t// program that lands text finishes: what to offer the person next, in the\n\t// program's own terms. Empty offers nothing. It is read only on that turn,\n\t// so it costs the conversation's fixed prefix nothing.\n\t//\n\t// THE OFFER IS THE PROGRAM'S, AND THE DECISION THE PERSON'S. A report that\n\t// found something to fix suggests its own next step better than codeaf can,\n\t// and the wake turn starts nothing ([LandsText]).\n\tFollowUp string\n\t// DefaultBrief is the brief a bare `/\u003cname\u003e` or `codeaf \u003cname\u003e` runs.\n\t// Empty is a program that needs one, and is refused without it.\n\t//\n\t// A BRIEF IS THE WORK, AND SOME WORK IS ALREADY SAID BY THE NAME. \"Fix\n\t// it\" is no task for senior-dev, but an audit of the repository it is\n\t// started in is exactly what a bare `/sec` means.\n\tDefaultBrief string\n\t// Words says the program's brief is a few words it reads itself, such as\n\t// sec's `changes since main thorough`, and not a document for a model.\n\t// Such a program is handed exactly the words it was asked with, from the\n\t// chat's proposal as from a typed `/\u003cname\u003e`; false hands it the composed\n\t// brief every task gets.\n\t//\n\t// A COMPOSED BRIEF OPENS ON ITS OWN HEADINGS, NOT ON THE WORK. A\n\t// proposal's brief is the person's message, the work, the deliverable and\n\t// lines from the conversation under capitals, and a program that read its\n\t// scope off the first line read `WHAT THE PERSON ASKED FOR` there: a\n\t// proposed `whole repository thorough` ran at standard depth, and a\n\t// proposed `changes` audited the whole repository.\n\tWords bool\n\t// Title is the title a typed `/\u003cname\u003e \u003cbrief\u003e` gives its task, from the\n\t// brief as typed; nil takes the brief's own first words, as a task's\n\t// title always has. A brief that is only a program's own scope words\n\t// (`quick`, `changes`) says nothing as a title, so such a program names\n\t// its run itself.\n\tTitle func(brief string) string\n\t// Args is how the command row shows what follows the name, such as\n\t// `[changes] [quick | thorough]`. Empty is `\u003cbrief\u003e`.\n\tArgs string\n\t// Asked says a person's message asks for the program's work, for a program\n\t// whose name is a word people use for much else (review). Nil hears the\n\t// name itself as a word of the message, the way the chat hears senior-dev\n\t// and sec. Set, the bare name is no longer heard: the command (`/\u003cname\u003e`)\n\t// still is, and so is a message Asked holds, such as \"review PR 123\".\n\t//\n\t// A NAME THAT IS ALSO A VERB WOULD CLAIM EVERY MESSAGE THAT USES IT.\n\t// \"review this function\" asks for no program; \"take a look at PR 123\"\n\t// asks for review's work without its name.\n\tAsked func(message string) bool\n\t// ModelFlag is the flag of the default command that names the models it\n\t// works on (senior-dev's `high`, sec's `model`), without its\n\t// dashes. A shell run resolves what the person typed there through the\n\t// profile's own services before the program starts, and puts the\n\t// profile's work seat on the line when they typed nothing. Empty is a\n\t// program that takes no model.\n\tModelFlag string\n\t// AuxiliaryModelFlags names other model flags the shell resolves through\n\t// the same profile. Only an explicitly written value is replaced; these\n\t// flags do not choose the run's default work seat.\n\tAuxiliaryModelFlags []string\n\t// CrewFlags is the flags the default command takes to use the models of\n\t// the conversation's crew ([Crew]), which codeaf puts on the line of every\n\t// run it starts from a conversation. Nil is a program that picks its own\n\t// models whatever the crew says.\n\t//\n\t// THE PERSON'S CREW IS THE DEFAULT, AND THE PROGRAM SAYS HOW IT HEARS IT.\n\t// A person who set which models do the thinking and the typing expects a\n\t// program they hand work to to use them too, rather than a list of its\n\t// own they never chose; codeaf knows the crew and nothing of the program's\n\t// flags, so the program turns the one into the other.\n\tCrewFlags func(Crew) []string\n\t// StageWords is the word a person reads for each stage the program reports\n\t// (its `stage` record), keyed by the stage's own name. The task's row and\n\t// the line over its conversation show the word, never the name: a program's\n\t// stages are its machinery — senior-dev's say `agent-runtime` and\n\t// `router-cancellation` — and this house draws no machinery vocabulary.\n\t// A stage with no word leaves the word shown before it standing, so a\n\t// program's inner phases need not each be named. Nil shows every stage by\n\t// its own name, for a program that has not said.\n\tStageWords map[string]string\n\t// Present is the program's own vocabulary for its task's page: it makes a\n\t// reader ([ActionReader]) that turns each line of its action log\n\t// ([Action]) — a stage, a step or its ending — into the words a person\n\t// reads, under the word for the step of its process it served ([Shown]),\n\t// and answers false for a line the page leaves out. Nil reads every line\n\t// plainly ([Delegate.Reader]).\n\t//\n\t// THE PROGRAM KNOWS WHAT ITS RECORDS MEAN, AND CODEAF KNOWS HOW A PAGE IS\n\t// DRAWN. A stage named `submit` with `patch_files` in its data is\n\t// senior-dev's machinery; that it reads `handed in its work · 4 files` is\n\t// senior-dev's to say, once, beside the words it gives its stages. The page\n\t// draws whatever a program says here, and the same step word leads the\n\t// task's row while the program is in that step.\n\tPresent func() ActionReader\n\t// Default is the command a bare brief runs: `/\u003cname\u003e \u003cbrief\u003e` in the chat\n\t// and `codeaf \u003cname\u003e \u003cbrief\u003e` in a shell. It names one of Commands.\n\tDefault string\n\t// Commands is the program's own verbs, each with its own flags. codeaf owns\n\t// the dispatch and the flags every program shares; the program owns these.\n\tCommands []Command\n\t// Page is the name of its page in the chat's manual (internal/manual/chat):\n\t// what it does, how to ask it, what a run costs, where the work lands. It is\n\t// compiled in with the rest of the manual, so the manual law's own gates\n\t// hold it to that.\n\tPage string\n}\n```\nDelegate is one program this build carries. It is a value in the build's list (internal/delegate/builtin), never a file on the machine: there is nothing to install, and no version of it that differs from the codeaf it ships in.\n\n```go\nfunc (d Delegate) Command(name string) (Command, bool)\n```\nCommand finds one of the program's commands by name.\n\n```go\nfunc (d Delegate) LandsTree() bool\n```\nLandsTree answers whether this program's work is a tree to land, which is the reading of an empty Lands too.\n\n```go\nfunc (d Delegate) Reader() ActionReader\n```\nReader is a fresh reader of this program's action log: through the program's own vocabulary when it has one ([Delegate.Present](#Delegate.Present)), and plainly otherwise — a stage as its name and status, a step as its command with its step id as the step's word and its exit as the outcome, the ending as its sentence. A line with no words is left out, and every line shown keeps the moment codeaf received it.\n\n```go\nfunc (d Delegate) Validate() error\n```\nValidate names the first thing wrong with a program's definition in a sentence the person who wrote it can act on. The build's own test runs it on every program the list carries (internal/delegate/builtin), so a definition that could not run never reaches a person.\n\n```go\ntype Emitter struct {\n\t// contains filtered or unexported fields\n}\n```\nEmitter writes a program's records on its stdout: one JSON object per line, each written whole under one lock, so two goroutines of the program can never interleave half a line of each.\n\nTHE TERMINAL IS WRITTEN AT MOST ONCE. A second is dropped here rather than sent for the reader to drop, so a program's own \"and one more for luck\" on its way out cannot become the record a person reads.\n\n```go\nfunc NewEmitter(w io.Writer) *Emitter\n```\nNewEmitter writes to w, which for a running program is its stdout.\n\n```go\nfunc (e *Emitter) Ended() bool\n```\nEnded answers whether the terminal has been written.\n\n```go\nfunc (e *Emitter) Err() error\n```\nErr is the first write that failed, if any. A program whose stdout is gone has nobody left to tell; the error is kept so its ending can say so.\n\n```go\nfunc (e *Emitter) Heard(ids []string) error\n```\nHeard writes the receipt for messages the program put before its model.\n\n```go\nfunc (e *Emitter) Hello(name string, stages []string) error\n```\nHello writes the first record.\n\n```go\nfunc (e *Emitter) HelloAccepting(name string, stages, accepts []string) error\n```\nHelloAccepting writes the first record naming what the program takes from codeaf while it runs ([Hello.Accepts](#Hello.Accepts)).\n\n```go\nfunc (e *Emitter) InboxClosed(reason string) error\n```\nInboxClosed writes that the program reads no more messages, and why.\n\n```go\nfunc (e *Emitter) Stage(stage StageRecord) error\n```\nStage writes a phase change, with its data when it is an object the reader will keep ([StageDataCap](#StageDataCap)) and without it otherwise, so the record a program writes is the record that arrives.\n\n```go\nfunc (e *Emitter) Step(step StepRecord) error\n```\nStep writes one finished action, capped the way the reader caps it, so what the program meant to say is what arrives. The optional fields are written only when they say something.\n\n```go\nfunc (e *Emitter) Terminal(end Ending) error\n```\nTerminal writes the result, once.\n\n```go\ntype Ending struct {\n\t// Status is StatusPass, StatusFail, StatusBudget or StatusCrashed.\n\tStatus string\n\t// Message is one sentence saying why.\n\tMessage string\n\t// CostUSD is the program's own reading of what it spent, zero for none.\n\t// codeaf's model API meters every call itself; this figure is kept for the\n\t// record and never trusted over that one.\n\tCostUSD float64\n\t// Reason is the longer reason, when there is one.\n\tReason string\n\t// Claim is what the program's model said it did, and Observed is what the\n\t// program itself verified. They are two witnesses and stay two fields.\n\tClaim    string\n\tObserved string\n\t// Deliverable is the answer text of a program that lands text.\n\tDeliverable string\n\t// Extra is any other data the program wants on the record. It never\n\t// overrides a field above.\n\tExtra map[string]any\n}\n```\nEnding is a program's result as it writes it: the terminal record, in fields rather than a map, so a program cannot misspell the one record the whole protocol exists for.\n\n```go\ntype Hello struct {\n\tProtocol int      `json:\"protocol\"`\n\tDelegate string   `json:\"delegate\"`\n\tStages   []string `json:\"stages,omitempty\"`\n\t// Accepts names what the program takes from codeaf while it runs:\n\t// [AcceptMessages] for a program reading its inbox (inbox.go). OPTIONAL AND\n\t// ADDITIVE: a hello without it is a program that takes nothing, which is\n\t// every program before it.\n\tAccepts []string `json:\"accepts,omitempty\"`\n}\n```\nHello is the first record: who is speaking, in which protocol, and the stages it will move through, which is what lets a page draw the whole track before the program has reached the end of it.\n\n```go\nfunc (h Hello) Listening() bool\n```\nListening reports whether the hello says the program reads its inbox.\n\n```go\ntype Host interface {\n\t// Workspace is the folder the program works in, absolute.\n\tWorkspace() string\n\t// Ceilings are the limits codeaf set for this run. The program keeps them\n\t// itself so it can end cleanly, and codeaf enforces them whatever it does.\n\tCeilings() Ceilings\n\t// Hello, Stage, Step and Terminal are the records (protocol.go). Hello\n\t// comes first and Terminal last, once.\n\tHello(stages []string)\n\tStage(stage StageRecord)\n\tStep(step StepRecord)\n\tTerminal(end Ending)\n\t// Models is this run's model API.\n\tModels() ModelAPI\n}\n```\nHost is what a running program asks codeaf for. Its body is handed one and reports through it: the records go to codeaf, and the models come from it.\n\n```go\ntype Invocation struct {\n\tProgram Delegate\n\tCommand Command\n\t// Workspace is --dir, absolute; the current folder when it was not given.\n\tWorkspace string\n\t// Ceilings are --max-cost and --max-hours.\n\tCeilings Ceilings\n\t// JSON is --json: the records on stdout instead of readable lines. A child\n\t// of a host always writes records, so for it the flag only says so aloud.\n\tJSON bool\n\t// Args is what the flags left: the brief's words.\n\tArgs []string\n\t// Line is the arguments as given after the name, with the flags put ahead\n\t// of the brief the way [Parse] reads them, so a host can hand its child a\n\t// line that reads back to the same invocation. THE BRIEF IS ITS TAIL: a\n\t// shell run swaps the person's --dir for its copy and puts the copy's note\n\t// ahead of the brief by counting [Invocation.Args] back from the end, and a\n\t// line kept in the person's own order put that note between a trailing\n\t// --max-cost and its value.\n\tLine []string\n\t// ExplicitFlags records values the caller actually wrote, so a host can\n\t// distinguish a model pin from a program's default after parsing.\n\tExplicitFlags map[string]string\n\t// contains filtered or unexported fields\n}\n```\nInvocation is one \\`codeaf \\\u003cname\u003e …\\` line, parsed.\n\n```go\nfunc Parse(program Delegate, line []string, out io.Writer) (*Invocation, error)\n```\nParse reads the arguments after \\`codeaf \\\u003cname\u003e\\`. The first word picks a command when it names one; otherwise the program's default command runs on the whole line, so \\`codeaf senior-dev fix the flaky test\\` is its \\`run\\`. Help (\\`-h\\`, \\`--help\\`, or \\`help\\` as the first word) is written to out and answered as ErrHelp.\n\n```go\nfunc (inv *Invocation) Brief() string\n```\nBrief is the brief's words, joined.\n\n```go\ntype Launch struct {\n\t// Name is the program's name, for the errors this launch writes.\n\tName string\n\t// Bin and Args are the process: codeaf's own executable and the program's\n\t// line ([ChildArgs]).\n\tBin  string\n\tArgs []string\n\t// Env is the child's whole environment ([ChildEnv]). Nil inherits this\n\t// process's, which only a test wants: it would hand a program every key.\n\tEnv []string\n\t// Dir is the folder the process starts in.\n\tDir string\n\t// StderrPath is the file the program's stderr is appended to. Empty\n\t// discards it, which no real caller wants: stderr is where a program says\n\t// why it could not start.\n\tStderrPath string\n\t// Grace overrides DefaultGrace, for a test that must not wait fifteen\n\t// seconds for a process that ignores SIGTERM.\n\tGrace time.Duration\n\t// Hold is the file the run's hold on its folder is taken on\n\t// (session.ProgramFolder.Hold), which the program's process is handed as\n\t// well ([HoldEnv]); nil hands it nothing.\n\tHold *os.File\n}\n```\nLaunch is one run of one program.\n\n```go\ntype Listener interface {\n\t// Messages answers the messages not yet read, oldest first, and never\n\t// blocks: none is an empty answer.\n\tMessages() []Message\n\t// Heard tells codeaf the program put these messages before its model.\n\tHeard(ids []string)\n\t// CloseInbox tells codeaf the program reads no more messages, and why.\n\tCloseInbox(reason string)\n}\n```\nListener is what a Host that can hand its program messages also is. A program asks for it with a type assertion on its Host, and a host that is not one — a test's, or a run started with no inbox — is a program nobody talks to.\n\n```go\ntype Message struct {\n\tID   string `json:\"id\"`\n\tFrom string `json:\"from\"`\n\tText string `json:\"text\"`\n}\n```\nMessage is one line of the inbox.\n\n```go\ntype MessageSink interface {\n\t// Heard names the messages the program has put before its model.\n\tHeard(ids []string)\n\t// InboxClosed says the program reads no more messages, and why.\n\tInboxClosed(reason string)\n}\n```\nMessageSink is a Sink that sends a listening program messages (inbox.go) and wants to know what became of them. It is optional: [Read](#Read) tells a sink that is one, and passes the records by every other.\n\n```go\ntype ModelAPI struct {\n\tBaseURL string\n\tToken   string\n}\n```\nModelAPI is the model API codeaf serves one run: an OpenAI-style base URL and the bearer token that opens it. A program in codeaf's own tree builds its route through internal/provider, the one package codeaf's funnel law lets spell a model route; a program outside it appends the route the way every OpenAI client does.\n\n```go\nfunc ModelAPIFromEnv() (ModelAPI, bool)\n```\nModelAPIFromEnv reads the API from this process's environment; ok is false outside a run, which is how \\`codeaf \\\u003cname\u003e\\` tells a child of a host from a person at a shell.\n\n```go\nfunc (m ModelAPI) Authorize(req *http.Request)\n```\nAuthorize puts the token on a request the program sends to the API.\n\n```go\nfunc (m ModelAPI) Ready() bool\n```\nReady answers whether there is an API to call.\n\n```go\ntype ProgramRecord struct {\n\tName   string   `json:\"name\"`\n\tStages []string `json:\"stages,omitempty\"`\n\t// CeilingUSD is the dollar ceiling the run handed the program, zero for\n\t// none. It is written here because the run works it out when it starts and\n\t// keeps it nowhere a page could read it afterwards, and a page that shows\n\t// the spend without the ceiling beside it leaves out half the reading.\n\tCeilingUSD float64 `json:\"ceiling_usd,omitempty\"`\n\t// StartedAt and EndedAt are the program's own clock: the instant codeaf\n\t// started its process and the instant that process was gone, written by\n\t// whoever ran it (the run's worker, or the shell verb). They are the ONE\n\t// record of how long the program itself ran, because every other pair of\n\t// times near a run brackets something else — the store is seeded before\n\t// the copy is cut, and the row settles after the landing. EndedAt is zero\n\t// while the program runs, and both are zero in a record written before\n\t// they existed, which a reader draws as no time rather than a wrong one.\n\tStartedAt time.Time `json:\"started_at,omitzero\"`\n\tEndedAt   time.Time `json:\"ended_at,omitzero\"`\n\t// Models and Effort are the models the program said it works on and how\n\t// hard it asks them to think ([StageRecord.Models]), so the task's page can\n\t// name what a run was launched on instead of leaving a person to remember\n\t// the flags. Both are empty until the program says, and stay empty for a\n\t// program that never does.\n\tModels []string `json:\"models,omitempty\"`\n\tEffort string   `json:\"effort,omitempty\"`\n\t// Listening says the program reads the messages codeaf sends it (inbox.go):\n\t// its hello said so. InboxClosed is why it stopped reading them — senior-dev\n\t// once it has handed in — and empty while it still reads. A page and the\n\t// conversation read both before offering, or refusing, to send it words.\n\tListening   bool   `json:\"listening,omitempty\"`\n\tInboxClosed string `json:\"inbox_closed,omitempty\"`\n}\n```\nProgramRecord is ProgramFile's content.\n\n```go\nfunc ReadProgram(dir string) (ProgramRecord, bool)\n```\nReadProgram reads the record; ok is false for a run that handed its task to no program, or whose program has neither said hello nor ended yet.\n\n```go\nfunc (r *ProgramRecord) Heard(stage StageRecord) bool\n```\nHeard keeps the models a stage record names ([StageRecord.Models](#StageRecord.Models)) and reports whether it changed the record, so a sink writes the record again only when there is something new in it.\n\n```go\ntype Reading struct {\n\tHello      *Hello\n\tLastStage  string\n\tLastStatus string\n\tSteps      int\n\tTerminal   *Terminal\n\tIgnored    int\n}\n```\nReading is what a reader saw, for the record the launch keeps: the last stage, how many steps, whether a terminal arrived, and how many lines were not the protocol's (dropped, not failed). What the run spent is not here: the model API metered it call by call, and a reading of the program's stdout is not where money is learned.\n\n```go\nfunc Read(r io.Reader, sink Sink) (Reading, error)\n```\nRead consumes r to its end, telling sink each record, and answers what it saw. It returns when the stream closes, which for a pipe is when the program exits or closes stdout; an error is only a read failure on the stream itself.\n\n```go\ntype Recorder interface {\n\t// Records is the run's record folder, absolute, or \"\" when codeaf named\n\t// none.\n\tRecords() string\n}\n```\nRecorder is a host that names the run's record folder ([EnvRecords](#EnvRecords)). A program asks for it by type, as it asks for a [Listener](#Listener), so a test's host that names none is still a Host.\n\n```go\ntype Result struct {\n\tReading Reading\n\t// ExitCode is the process's own, -1 when it was ended by a signal or never\n\t// ran. The verdict is NOT read from it (§3): a program that failed its task\n\t// exits zero with a terminal saying `fail`.\n\tExitCode int\n\t// Stopped is true when the caller's context ended the program: SIGTERM,\n\t// and SIGKILL when the grace passed. The reading may still hold a terminal\n\t// the program wrote inside the grace.\n\tStopped bool\n\t// Killed is true when SIGKILL was needed.\n\tKilled bool\n\t// Elapsed is the process's wall time.\n\tElapsed time.Duration\n}\n```\nResult is what one launch came to.\n\n```go\nfunc Run(ctx context.Context, launch Launch, sink Sink) (Result, error)\n```\nRun starts the program and reads it to its end. It returns when the process has exited and stdout is drained, so nothing of the child outlives the call.\n\nA CONTEXT THAT ENDS ENDS THE PROGRAM, in the order the protocol promises: SIGTERM to the group, the grace, SIGKILL. The stdout reader keeps reading through the grace, so a terminal written on the way out is the reading's terminal. The error answered is the context's own, so a run supervisor that reads \\`context.Canceled\\` off a worker knows its own ending cut the task.\n\n```go\nfunc (r Result) ExitedAt(started, returned time.Time) time.Time\n```\nExitedAt is the instant the program's process was gone: the launch's own measure of the process's life laid on the instant the caller started it, and never later than returned, the instant the launch gave its answer back.\n\nTHE PROGRAM'S WALL TIME IS ITS PROCESS'S, NOT THE DRAIN'S. A launch returns only once stdout is drained, and a helper the program left holding stdout can keep that drain open for the whole grace after the program itself exited. A conversation's run and a shell run both end the program's clock here, so the same program reads the same time on every surface.\n\n```go\ntype RunFacts struct {\n\t// Plain says the folder has no git history.\n\tPlain bool\n\t// Crew is the conversation's crew; zero for a run no conversation started.\n\tCrew Crew\n}\n```\nRunFacts is what codeaf read about a run before it started the program, each of which puts the program's own flags for it on the line ([ChildArgs](#ChildArgs)).\n\n```go\ntype Said struct {\n\t// Role is \"system\", \"user\" or \"tool\", as the program sent it.\n\tRole string `json:\"role\"`\n\tTool string `json:\"tool,omitempty\"`\n\tText string `json:\"text\"`\n}\n```\nSaid is one message a program sent: whose it is and its words. A tool's result carries the tool it answers.\n\n```go\ntype Shown struct {\n\tAt time.Time `json:\"at\"`\n\t// Step is the word for the part of the program's process the action\n\t// served, printed once at the head of each run of actions in it. Empty is\n\t// an action inside whatever step is under way.\n\tStep string `json:\"step,omitempty\"`\n\t// Text is the action in words: `read internal/auth/middleware.go`.\n\tText string `json:\"text\"`\n\t// Outcome is how it came out, in a word or two: `passes`, `fails · exit 2`,\n\t// `4 files`. Empty when there is nothing to say.\n\tOutcome string `json:\"outcome,omitempty\"`\n\t// Detail is the whole of the step as the log kept it — the command or\n\t// argument, and what came back — which the page opens under the action's\n\t// one line when it is clicked. Empty for a line with nothing more to show.\n\tDetail string `json:\"detail,omitempty\"`\n\t// Lines says the action changed a file and counted how: Added and Removed\n\t// are its lines added and removed, drawn as `+N,-M` in the diff's own\n\t// colours beside the action. False for every other action.\n\tLines   bool `json:\"lines,omitempty\"`\n\tAdded   int  `json:\"added,omitempty\"`\n\tRemoved int  `json:\"removed,omitempty\"`\n\t// Steer marks the program steering its own model — a nudge, a last turn, a\n\t// retry after a dropped call, a correction — rather than working through it.\n\tSteer bool `json:\"steer,omitempty\"`\n\t// Memory marks the action that says the program compacted its memory, and\n\t// Model names the model an action says it moved to, with Reason why. The\n\t// page reads both beside the conversation log, which says the same two\n\t// things from the model's side, so one compaction or one switch is drawn\n\t// once.\n\tMemory bool   `json:\"memory,omitempty\"`\n\tModel  string `json:\"model,omitempty\"`\n\tReason string `json:\"reason,omitempty\"`\n}\n```\nShown is one action as a person reads it on its program's page: the step it belongs under, the words, and how it came out. A program's own vocabulary makes it ([Delegate.Present](#Delegate.Present)); a program with none is read plainly ([Delegate.Reader](#Delegate.Reader)).\n\n```go\ntype Sink interface {\n\t// Hello is the program's first record, told once.\n\tHello(h Hello)\n\t// Stage is a phase change: the live step, and one line of the program's\n\t// action log. Its data is already held to [StageDataCap].\n\tStage(record StageRecord)\n\t// Step is one finished action: command and the observation head, both\n\t// already capped, and the tool, step and exit the program said.\n\tStep(record StepRecord)\n\t// Terminal is the result. It is told at most once; a second terminal on\n\t// the stream is ignored, because the contract says exactly one and the\n\t// first is the one the program wrote on purpose.\n\tTerminal(t Terminal)\n}\n```\nSink is what a reader tells as the stream arrives. Every method is called on the reader's goroutine, in stream order, and none may block on the program: a sink that waits on the child is a deadlock with a pipe in the middle.\n\n```go\ntype StageRecord struct {\n\tStage  string `json:\"stage\"`\n\tStatus string `json:\"status\"`\n\t// Data is a JSON object of at most [StageDataCap] bytes, or nothing. It is\n\t// OPTIONAL AND ADDITIVE: a reader of version 2 that predates it reads the\n\t// record without it.\n\tData json.RawMessage `json:\"data,omitempty\"`\n}\n```\nStageRecord is one \\`stage\\` record: the phase the program moved to, how it stands in it, and the small copy of what it knows about it.\n\n```go\nfunc (s StageRecord) Models() ([]string, string, bool)\n```\nModels is the models a stage record's data says the program works on — its \\`models\\`, a list of model ids, and its \\`effort\\`, the rung it asks them to think at — and false for a record that names none.\n\nTHE PROGRAM SAYS WHICH MODELS IT RUNS ON, because only the program knows. The flags codeaf puts on its line are a request: the program folds in its own defaults, a person's own flags on a shell line, and drops a model it cannot use, and a page that named the request would name models the run never touched. Any stage may carry the pair; senior-dev's \\`bootstrap\\` does.\n\n```go\ntype StepRecord struct {\n\t// Command is the action on one line, `\u003ctool\u003e: \u003cwhat it was about\u003e`.\n\tCommand string `json:\"command\"`\n\t// Observation is the head of what came back.\n\tObservation string `json:\"observation,omitempty\"`\n\t// Tool is the tool's own name.\n\tTool string `json:\"tool,omitempty\"`\n\t// Step is the program's own id for the part of its process the action\n\t// served (senior-dev's are app.Steps). It is the program's word, drawn\n\t// through the program's own vocabulary ([Delegate.Present]).\n\tStep string `json:\"step,omitempty\"`\n\t// Exit is a command's exit code, present only for an action that ran a\n\t// command and learned how it exited — which is why it is a pointer: a\n\t// command that exited 0 and an action that ran none are two facts.\n\tExit *int `json:\"exit,omitempty\"`\n\t// Added and Removed are the lines an action that changed a file added and\n\t// removed, present only when the program counted them.\n\tAdded   *int `json:\"added,omitempty\"`\n\tRemoved *int `json:\"removed,omitempty\"`\n}\n```\nStepRecord is one \\`step\\` record: one finished action, what was run and the head of what came back, and — each optional, each absent from a program that does not say it — the tool that ran it, the step of the program's own process it served, and a command's exit code.\n\n```go\ntype Terminal struct {\n\tStatus  string                     `json:\"status\"`\n\tMessage string                     `json:\"message\"`\n\tData    map[string]json.RawMessage `json:\"data\"`\n}\n```\nTerminal is the one record that is the result. Data is kept whole so the landing note can read the optional keys, in the protocol's spelling and in senior-dev's own, through the accessors below rather than by every caller knowing both.\n\n```go\nfunc (t Terminal) Claim() string\n```\nClaim is what the program's model said it did: \\`claim\\` in the protocol, \\`submission\\_reason\\` in senior-dev's record.\n\n```go\nfunc (t Terminal) CostUSD() (float64, bool)\n```\nCostUSD is the final total, and false when the record did not carry one.\n\n```go\nfunc (t Terminal) Deliverable() string\n```\nDeliverable is the answer text of a delegate that lands text.\n\n```go\nfunc (t Terminal) HandedIn() bool\n```\nHandedIn says the program handed in a change of its own — senior-dev's \\`submitted\\` — whatever its own check of that change then said, and false when the record does not say.\n\nA CHANGE HANDED IN IS FINISHED WORK, AND A PROGRAM'S OWN CHECK OF IT IS A LEAD, NOT A VERDICT. senior-dev ends \\`fail\\` when its guess at the project's build and tests exits non-zero, and in a fortnight of real runs every such ending came from the guess — a CI line cut in half, \\`python3 -m pytest\\` on a machine with no pytest — and none from a change that broke the project. Read as unfinished work, each one woke the conversation to \"fix\" work that was never broken; the host reads a handed-in change as finished instead (internal/run's DelegateWorker), with what the check said kept beside it.\n\n```go\nfunc (t Terminal) Observed() string\n```\nObserved is what the program itself verified: \\`observed\\` in the protocol. senior-dev spells its observation as its own inner status and a count of failing verification commands, which read here as one sentence so the landing note can keep the claim and the observation apart.\n\n```go\nfunc (t Terminal) Reason() string\n```\nReason is the longer reason when there is one.\n\n```go\nfunc (t Terminal) Verdict() string\n```\nVerdict is the program's own word for how its work stood when it ended — senior-dev's inner status (\\`pass\\`, \\`pass-unverified\\`, \\`fail\\`) — beside the protocol's status word, and \"\" when the record carried none.\n\n```go\ntype ToolUse struct {\n\tName string `json:\"name\"`\n\tArgs string `json:\"args,omitempty\"`\n}\n```\nToolUse is one tool a model asked the program to run, with its arguments on one line.\n\n```go\ntype Turn struct {\n\tSeq int `json:\"seq\"`\n\t// Thread tells conversations apart when a program holds more than one at\n\t// once (a summary of its own history, a helper agent): the call's cache key\n\t// or its own id, MainThread when it gave none.\n\tThread  string    `json:\"thread,omitempty\"`\n\tStarted time.Time `json:\"started\"`\n\tEnded   time.Time `json:\"ended,omitempty\"`\n\t// Model is the model the program asked for; Served is the one that\n\t// answered, when codeaf's router answered with another.\n\tModel  string `json:\"model\"`\n\tServed string `json:\"served,omitempty\"`\n\t// Sent is what the program sent that the thread's previous call did not:\n\t// its brief first, then its tools' results and its own words. Restarted is\n\t// true when the program rewrote its history instead of adding to it (a\n\t// compaction), so Sent is then everything it sent.\n\tSent      []Said `json:\"sent,omitempty\"`\n\tRestarted bool   `json:\"restarted,omitempty\"`\n\t// Reply is the model's text, and Calls the tools it asked the program to run.\n\tReply string    `json:\"reply,omitempty\"`\n\tCalls []ToolUse `json:\"calls,omitempty\"`\n\t// The call's size and price, as the funnel metered them.\n\tTokensIn  int     `json:\"tokens_in,omitempty\"`\n\tTokensOut int     `json:\"tokens_out,omitempty\"`\n\tCached    int     `json:\"cached,omitempty\"`\n\tCostUSD   float64 `json:\"cost_usd,omitempty\"`\n\t// Refused is codeaf's own refusal — the ceiling, a run that has ended — set\n\t// when the call never reached a model. Failed is the model's side failing.\n\tRefused string `json:\"refused,omitempty\"`\n\tFailed  string `json:\"failed,omitempty\"`\n}\n```\nTurn is one model call a program made through its model API. A call is written twice under one Seq — when it starts, with no Ended, and when it ends — and a reader keeps the later, which is how the page shows a call in flight without a second file.\n\n```go\nfunc ReadTurns(dir string, n int) ([]Turn, error)\n```\nReadTurns reads the log in dir: every call once, in the order they started, each as its latest record says, and the last n of them (n \\\u003c= 0 for all). A log that is not there is no turns and no error, because a run that has not called a model yet has said nothing; a line that does not parse is skipped, because a log cut mid-write is still a log.\n\n```go\nfunc (t Turn) InFlight() bool\n```\nInFlight answers whether the call has not come back yet.\n\n","path":"github.com/Agent-Field/codeaf/internal/delegate","name":"delegate","synopsis":"Package delegate is what codeaf needs of the programs it carries and can hand a whole task to — senior-dev first (docs/design/delegate/PROTOCOL.md): what one IS (a Go value in the build's list, internal/delegate/builtin), the command line every one of them answers (`codeaf \u003cname\u003e …`), the records a running one writes on its stdout and the one reader over them, the model API that is its only road to a model, the log of the conversation it holds over that road, and the launch of it as a child process that streams, stops on SIGTERM and ends with one terminal record.","isRedistributable":true}
