Metadata-Version: 2.4
Name: coduck
Version: 0.17.0
Summary: A local review tool that keeps code reviews in git refs
Keywords: code review,git,agents
Author: Evert Lammerts
License-Expression: MIT
License-File: LICENSE
License-File: src/coduck/page/vendor/highlight.js.LICENSE
License-File: src/coduck/page/vendor/markdown-it.LICENSE
License-File: src/coduck/page/vendor/mermaid.LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Version Control :: Git
Requires-Python: >=3.12
Project-URL: Repository, https://github.com/evertlammerts/coduck
Description-Content-Type: text/markdown

# CoDuck

CoDuck is a code review tool for one machine. The author of a branch, usually an agent, opens a review with the
`coduck` command; the reviewer reads it in a web page served on localhost, comments on lines, edits Markdown
files and approves. Every review lives in the repo of its branch as a git ref of its own, `refs/coduck/<number>`,
and changes only by a new commit on that ref. CoDuck never merges, never fetches and never moves a branch of a repo
it reviews. It also keeps products, projects and issues, as Markdown files in a git repository of its own; see
[Products, projects and issues](#products-projects-and-issues).

This file is the user documentation: a quickstart first, then every feature in detail. Every command also
explains itself with `-h`, and `coduck skill` prints the instructions for agents that ship with the installed
version.

## Quickstart

You need git, Python 3.12 or newer, and [uv](https://docs.astral.sh/uv/). CoDuck is developed and run on Linux.
It is not tested on macOS, where the limits on a run's memory and processes do not exist. It does not run on
Windows, which lacks the file locks and process groups it uses.

1. **Install the command** from PyPI:

   ```sh
   uv tool install coduck
   coduck -h
   ```

   Or from a release tag of a clone of CoDuck's repository, the newest tag in place of `v0.17.0`. An install from a
   tag, not from the working files, keeps a running server on code that does not change under it.

   ```sh
   git clone <the URL of CoDuck's repository> coduck
   uv tool install "git+file://$PWD/coduck@v0.17.0"
   ```

2. **Start the server** and leave it running. It serves every registered repo, on 127.0.0.1 only; opening a
   review registers its repo.

   ```sh
   coduck serve
   ```

   It prints `listening on http://127.0.0.1:8090/`. To run it as a service that survives a logout, and to reach
   it from another machine through an ssh port forward, see [deploy/install.md](deploy/install.md).

3. **Open a review**, as the author, in the worktree that has the branch checked out. Commit the work first, write
   the description to a Markdown file whose first line is the title, a few words that name the change in at most 60
   characters, and run:

   ```sh
   coduck -n docs-session open description.md
   ```

   It prints `opened review 1 of feature in myrepo`. The review is against the default branch of the repo; name
   another base with `-b`. `-n` gives the name the command writes under, here `docs-session`; see
   [Names](#names).

4. **Review it**, as the reviewer, at `http://localhost:8090/`. The list shows the review under "Open", marked as
   new for you. Open it, read the description on the Overview tab and the diffs on the Files changed tab, and click a
   line number to comment. In Scratch mode, the default, your comments and edits are drafts until you press Post
   all.

5. **Answer**, as the author, under the same name. `show -u` prints what waits on you and ends with the review's
   version; `wait` returns when the reviewer, or another session under another name, has added something other
   than a note after that version.

   ```sh
   coduck -n docs-session show -u
   coduck -n docs-session reply 0f5f6ffc answer.md
   coduck -n docs-session wait -s <version>
   ```

   When the reviewer edited a file in the page, take the edit into your worktree and commit it, here for a reviewer
   called Ada:

   ```sh
   coduck -n docs-session pull
   git commit -m "[Ada] Shorter troubleshooting paragraph"
   ```

6. **Finish.** When the reviewer is satisfied they approve, which keeps the review open for later changes. The
   author finishes what is still in flight, such as a push for a pull request, and then says so:

   ```sh
   coduck -n docs-session ready
   ```

   Once the branch has landed, by a pull request or an ordinary `git merge` outside CoDuck, or the work is
   dropped, the reviewer presses Done. The review leaves the open sections of the list and its branch is free for a
   new review.

7. **Report and triage issues**, from any folder. Once a product exists, an agent reports a defect it found, which
   lands in the inbox; triage reads the inbox and gives an issue its state:

   ```sh
   coduck products new -t CoDuck
   coduck -n docs-session issues new -p coduck -l defect report.md
   coduck issues inbox
   coduck issues set 1 state todo
   ```

   See [Products, projects and issues](#products-projects-and-issues).

## Concepts

- **Review.** A branch of a repo, offered for review against a base branch. A branch has at most one open review.
  Reviews are numbered from 1 in each repo, and a review is named `<repo>/<number>`, the repo being the folder name
  of its main worktree. Two registered repos cannot share that name: `coduck open` and `coduck register` refuse
  a repo whose name another registered repo has.
- **Base.** The branch the review is against: a local branch or a remote tracking branch. The page shows the
  changes from the merge base of the two to the head of the branch. CoDuck never fetches, so a remote tracking
  base is as the last fetch left it.
- **Thread.** A conversation on lines of a file, on lines of the description, or the general conversation of the
  review, whose id is `conversation`. Other threads have an id of eight hex digits.
- **Outdated.** A thread whose lines changed since it was written. It stays visible with the lines it was written
  on and counts as open until it is resolved. The page shows it where the diff that outdated it put those lines,
  its place, or at the top of its file when it has none; see [Threads](#threads).
- **Draft.** Something the reviewer did in Scratch mode that is not posted yet. Drafts are not in git and the
  author never sees them.
- **Pending edit.** A change the reviewer made to a file of the branch, kept in the review until the author takes
  it in with `coduck pull`.
- **Version.** The commit the review's ref points at. Every write to a review makes a new version, and
  `coduck wait` counts news from one.
- **State.** An open review needs input, is approved, or is ready; see [The state of a
  review](#the-state-of-a-review). A finished review has no such state.
- **New for you.** What happened in a review since the reviewer last wrote in it; see [What is new for
  you](#what-is-new-for-you).
- **Note.** A fact about a review that nobody is asked to answer, such as where its pull request is; see
  [Notes](#notes).
- **Run specification.** A named Python version and list of requirements, kept in a review, that its Python blocks
  can run on; see [Run specifications](#run-specifications).
- **Names and sides.** A command writes under the name its `-n` gives, or else under one shared name, `agent`
  unless set otherwise, and the page writes as the reviewer's name. Every write also records its side: what the
  page writes is the reviewer's, what a command writes an author's, except a message that a command posts for the
  reviewer with `-w`, which is the reviewer's and also records the session that posted it. That side, not the
  name, is how CoDuck tells what is new for the reviewer and who gave the ready signal. See [Names](#names),
  [Posting for the reviewer](#posting-for-the-reviewer) and [Settings](#settings).
- **Products, projects and issues.** A product is a piece of software, a project a unit of work on one with a
  board of issues, and an issue something to do or decide about a product. Each is a Markdown file in the state
  repository, a git repository of CoDuck's own; see [Products, projects and issues](#products-projects-and-issues).

## The page

### The review list

`http://localhost:8090/` lists the reviews of every registered repo in three sections, by their
[state](#the-state-of-a-review):

- **Open**, the reviews that need input or are approved, the latest activity first.
- **Ready**, the approved reviews whose author has said that nothing more is in flight, the latest activity first.
- **Done**, the reviews finished in the last 14 days, the latest first, with a switch for 30 days, 90 days and all.

A review's activity is the latest of its last write, its branch head's commit and the reviewer's newest draft.

A row shows the state as a chip, "Needs input", "Approved" or "Ready", or how the review finished, then the title
and number, the repo, the author (the name the review was opened under), the project, the branch and its base, and
the counts of open threads, drafts and pending edits. A review that is [new for you](#what-is-new-for-you) carries
a mark with its first reason and how long it has been new, such as "New commits, 3h", in Open or Ready; its
tooltip names every reason. A row also shows the first line of the review's newest note, with its links, an idle
mark on an open review with no activity for 7 days, and a dot with "N new" for what arrived since this browser last
showed the review.

Two or more reviews of one project in a section stand together as a group. Its heading shows the project, a link to
the project view, how many reviews the group holds and, when any of them are new for you, how many, such as "2 new
for you". Under it are the group's rows, set in a little, each as above but without the project, which the heading
carries. A group stands where its most recently active review would stand, in Done its most recently finished, and
its rows keep that order. A project with one review in a section, and a review without a project, is a row of its
own; a project's reviews in two sections form a group in each section that holds two or more of them.

Above the sections are a text filter, matched word by word against title, number, author, branch, repo and project,
a repo filter and a project filter. The text filter also finds finished reviews of any age. The filters live in the
address, so a reload or a link keeps them. A click on a project opens the project view: the same list for the
reviews of one project across repos, where the rows keep their project and form no groups. A group holds the rows
that match the filters, and is a group while two or more of them do.

The list follows changes without a reload. The "N new" count, the times, the marks of what is new and a group's
count of them update in place; when the rows themselves would change, as when a review changes its state, its place
in the order or its project, a bar "The list changed. Show" appears at the bottom instead of moving rows under your
pointer. A badge in the top bar of every page, "New for you", counts the reviews that are new for you, and so does
the list's title.

A repo that is no longer at its registered path shows as missing above the sections, with a Remove button.

### The reviews menu

The button of three lines at the left of the top bar of every page, Reviews, opens a panel over the left edge of the
page with the list's Open and Ready sections in short, in the list's order. Each section says how many reviews it
holds, or that it holds none. Two or more reviews of one project in a section stand together under the project's name
in small type, as they do in the list. A row is one line: the mark of what is new for you, an "Approved" chip on an
approved review, the title, and the review's name, such as `myrepo/1`, which a long repo name cuts with an ellipsis.
A click on a row opens that review; the row of the review the page shows is marked and does nothing. All reviews,
under the sections, goes to the list. The panel closes on Esc, on a click outside it, on a second click on the button
and when the page goes to a row's review; a row opened in another tab, with the middle button or with Ctrl or Cmd,
leaves it open. It shows the list the page already follows, and changes with it while it is open, its marks aging
with the minute; until the page has read the list for the first time, it says "Loading the reviews".

### A review

The top bar holds the repo and the title of the review, the base and the branch, the comment mode and Post all. The
title takes the width of 32 characters at most, cut with an ellipsis; its tooltip holds it whole. In a narrow window
the bar gives way in this order: the title down to 12 characters, the word Comments, the repo and the branch names
down to 8 characters each, cut with an ellipsis and whole in their tooltips, the words of the "New for you" badge,
which keeps its count, and then the rest of the title. The mode switch and Post all stay whole, inside any window
900 wide or wider. Below the top bar the header shows the title, the state as a chip ("Needs input", "Approved" or
"Ready" for an open review, and how it finished for one that is not), the project, and the counts of commits,
changed files, open threads and drafts, and the review has three tabs:

- **Overview**: the description, the general conversation and Done, with a side column that shows the notes, the
  mode, the number of drafts that wait with Post all, the open threads, the commits of the branch, oldest first,
  and the changed files.
- **Files changed**: a file tree and one card per changed file, and one per file that holds a comment although the
  review does not change it (see [Threads](#threads)), in one list by path. A card shows the diff as Unified or Side
  by side, with the changes only or the full file. A binary file is named and not shown. In the tree, a balloon
  beside a file holds the number of its open and drafted threads.
- **Tasks**: the epic and task files of the branch, see [Epic and task files](#epic-and-task-files).

The page checks the review every three seconds and shows new replies and commits without a reload, keeping the
text you are reading in place.

In a card that shows the changes only, each change has three unchanged lines around it and the other unchanged
lines are folded. A fold says how many lines it hides and opens twenty lines at a time from either side, or whole.
Its controls name what they show by the lines' new numbers, in this order: "Show lines 551 to 570", the twenty
just below what comes before the fold, "Show all 63 lines", and "Show lines 594 to 613", the twenty just above what
follows it. A fold at the top of the file has nothing over it and so no step under it, one at its end has nothing
under it and so no step over it, and one of twenty lines or fewer opens only whole. The control you click stays
where it was on the screen, so you can click it again for the next twenty. A thread, a drafted thread and a comment
you are writing have their lines and three on each side shown, as a change has, wherever they sit: in a fold, on a
change's own lines around it, or running from one into the other; the rest of a fold stays folded before and after
them, and such lines that meet other shown lines join them. The lines you open are remembered by their numbers in
the base of the review, which a commit on the branch does not move, so they stay the same lines when the agent
adds, moves or takes back a change, and they stay open while the page is open, also when the thread or comment
that sat beside them goes. A comment you cancel folds its lines again unless you had opened them.

The row above each change names its line numbers and what git finds the change is in, by git's own rules for the
file's language: the method in Python or Java, the function in Rust, the nearest heading in Markdown. CoDuck ships
an attributes file that assigns git's built-in rules by file extension and gives it to git for the diff the page
shows, in place of your own attributes file; no other git command that CoDuck runs reads it, so `coduck pull`
keeps your attributes. `.m` files get no rules, being both MATLAB and Objective C. A repo's own attributes come
first: its `.git/info/attributes` and the `.gitattributes` files in the index of its main worktree, so a repo that
names other rules, or none, for a file gets those. The `.gitattributes` files of the branch under review count
only as far as the main worktree has them, and a `.gitattributes` in the folder the server runs in never counts.
Without rules git names the nearest line above the change that starts in the first column, such as the class
around a method. The rules change only that text, never which lines a change holds: every diff CoDuck makes uses
git's default algorithm whatever git's settings say, so for someone whose settings name another algorithm for one
of git's rule sets, threads may sit elsewhere than under an earlier version. The row is the fold nearest above its
change, and stays above the change while lines open under it; once the whole stretch of unchanged lines above the
change is open, the lines run on without it.

The arrow at the left of a card's head folds the file. When you had scrolled into the file, so that its head stood
at the top of the column, the page then shows that head at the top, with the next file under it.

### Comment modes and Post all

The mode switch in the top bar reads Comments: Scratch or Direct. The browser remembers the choice.

- **Scratch** holds comments and edits back. New threads, replies, resolves and reopens, applied suggestions, file
  edits and their withdrawals, and description edits are drafts, marked as such, that you can change or delete. Post all
  opens the dialog "Post your review", which lists the drafts and takes an optional summary and a verdict, Comment
  or Approve, and writes all drafts in one commit, so that a waiting author wakes once. On a review that is approved
  or ready the dialog has a third choice, Comment and mark not ready: it posts as Comment does, with the mark needs
  input that ends the approval (see [The state of a review](#the-state-of-a-review)), and like Comment it needs
  drafts or a summary to post. When you choose Approve
  while threads are not resolved, the dialog says how many, lists each with a link to it, and its button reads
  "Approve anyway". A thread counts as the review will hold it once the drafts are posted: a drafted resolve
  resolves it, and a drafted reopen and a drafted new thread are not resolved. When a write still on its way
  brings a thread the dialog did not name, Post review asks again before it posts. Approving with open threads is
  allowed.
- **Direct** posts each action at once. On a review that is approved or ready, a composer of a comment, a reply or
  a message in the conversation has a second button beside its submit, Comment and mark not ready, which posts the
  message with the mark needs input. Ctrl or Cmd with Enter posts it without the mark.

A change of base, a change of project, a note and Done act at once in both modes. Drafts are kept on the server's
disk, outside git, and survive a reload and a restart.

### Threads

Click a line number in a diff to comment on that line; shift click another line to take the lines between too.
Or press a line number and drag, up or down, over the numbers of the lines you want, and let go: the lines between
are marked as you go, and the comment opens on them, wherever a click on a line number starts a comment: both
layouts of the diff, the full file and the source of the split view, though not rendered text. A drag stays on the
side it started on, the old or the new, and on the old side it starts only on a removed line, as a click does. Once
it is under way only the pointer's height counts: the line taken is the one of that side at that height, wherever the
pointer is from left to right, over the code, over the other side's numbers or beside the file, and where that side
has no line at that height, or the pointer is above or below the file's rows, the nearest line of that side. Near
the top or bottom of the window the page scrolls, or the split view's source does, and the lines taken follow.
Escape gives the drag up, and so do a second press and the window losing focus; none of them opens a comment. A
comment already open on the file moves to the lines you took, with what you typed in it. A drag that starts on the
code itself selects text to copy, as before. In rendered text, the
description included, the + beside a block starts a thread on the source lines of that block. Ctrl or Cmd with
Enter submits what you typed. Comments are Markdown. A thread can be on the new text of the branch, on removed
lines of the old text, or on lines of the description.

A thread follows its lines through later commits, also through a rebase, as long as git's line diff keeps every
one of its lines unchanged and puts no line between them. Any change to a line of the thread, a change of
indentation or a rewrap included, a line inserted between its lines, or the removal or rename of its file, makes
the thread outdated. A thread on removed lines stays current only while the merge base is the commit it was
written against and the branch still removes those lines, so a change of base or a rebase onto a newer base
outdates it. Nothing is matched by likeness.

An outdated thread on the new text or on the description still has a place, read from the same diff that outdated
it: a line the diff keeps sits at its new number, a line it replaces sits on the lines that replaced it, and a line
it deletes sits on the line the deletion follows, or on line 1 when the deletion starts the file. The place runs
from where the thread's first line went to where its last line went, so a thread on one line of a block that was
rewritten sits on the whole new block. The page shows the thread there among the lines, as it shows a current
thread, in every view of a file and in the description: its header names the lines it sits on now, beside the
Outdated label, and below the header are the lines it was written on. A thread on removed lines has no place, and
neither has one whose commit is no longer in the repo, or whose file was removed, renamed or emptied, or is no
plain file, such as a link, at the thread's commit or at the head: such a thread is listed at the top of its file,
under "On lines that have changed since". A place is not a position: a suggestion on an outdated thread cannot be
applied, wherever the thread sits.

A file at the head of the branch that the review does not change, whether it never did or no longer does, has a card
of its own while it holds a thread, open or resolved, a drafted thread, a pending or drafted edit, or a drafted
withdraw of an edit. Its card stands among the others by path, and in the tree too, where it shows a plain file icon
in place of the letter of its status; its head says "Not changed" in place of the counts, and it is not counted
among the changed files, in the header, on the tab, in the Overview's list and in the title of the tree. The card
shows the file in one column of line numbers: its threads sit on their lines, with three lines around each as around
a change, the rest is folded, and the folds open as in a diff. A comment starts there on a line as anywhere else, and
a Markdown file has its Rendered and Split views and its Edit button. A card without changed lines, as of a file
whose mode alone changed, says "No lines changed." above the same rows, with its threads on their lines. A binary
file lists its threads under "Threads on this file". The threads and edits of a file that is not at the head of the
branch, such as one the branch removed or renamed, are listed at the end, under "Threads and edits on files that are
not at the head of the branch", and so are a comment you are writing and a file editor you left open on a file that
has no card. While the review has no changes to show, because its branch or its base is gone or the two share no
history, no file has a card: every thread and edit on a file is listed at the end, under "Threads and edits, while
there are no changes to show", and a thread there that is not outdated shows the lines it is on, numbered where they
are now.

A comment you are still writing sits where its thread would sit. When the branch, its base or the description
changes while it is open, the page asks the server, once for each comment and each new head or description, where a
thread on its lines would sit in what the page is about to show, and draws the comment there: beside the same lines
at their new numbers while they are unchanged, at their place when they changed, and under "On lines that have
changed since" only when a thread there would have no place. While its lines are unchanged the comment moves with
them: it names them at their new numbers on the head shown, as it says above what you type, and that is what
posting records, so a comment written across a commit, or across a rebase that kept its lines, is posted on the head.
A comment whose lines changed keeps the commit and the lines you opened it on. The page draws what it read only once
it has those answers for every comment open then, unless a request for one fails or names a head that was rebased
away before any write recorded it, when that comment waits under the heading until the next read; so a comment does
not jump away from lines that stayed as they were, and what you typed, the caret and the focus stay as they were, in
every view. A shift click on another line widens a comment from where it sits now, while its lines are unchanged;
one whose lines changed opens on the clicked line alone, with what you typed. A drafted thread sits at its place in
the same way.

Every thread has an address, `/r/<repo>/<number>/thread/<id>` on the server, and every comment in it one of its
own, `/r/<repo>/<number>/thread/<id>/<k>` for the k-th, counted from 1 in the order the page shows them, with at
most six digits; in the conversation, whose id is `conversation`, the summary of a Post all counts as a comment.
Copy link in a thread's header copies the thread's whole address, and a click on a comment's time copies the
comment's; the page says "Link copied", and where the browser refuses the clipboard it shows the address in a field
to copy by hand. Opening an address shows the review where the thread is, unfolds it when it is resolved, and marks
the thread or the comment until your next click or key. For a thread the review does not hold, the page stays where
it is and says so; for a comment a thread does not hold, it shows the thread and says so.

In comments, notes and the description, an address of a thread or a comment on this server, under either of its
names, `localhost` or `127.0.0.1`, or written as a path from `/r/`, is a link to it, and a bare one shows short, as
`myrepo/1 thread 0f5f6ffc, comment 2`. A click on one of the review shown moves there without loading the page
again, as a step in the browser's history: the address bar shows it, and Back returns to where you were, the tab and
the place in it, also when Back comes from another page. One of another review opens in a new tab, and so does any
of them in a preview or in the dialog of a file opened read only, so that what it holds stays.

A message that a session posted for you, with `-w` (see [Posting for the reviewer](#posting-for-the-reviewer)), is
yours: it shows under your name and without the Agent label, with "through NAME" in small type beside the name,
NAME being the session that posted it, and it is not marked as new. Your drafts never carry such a name.

Resolve closes a thread and Reopen opens it again. The general conversation cannot be resolved.

### Suggestions

In a comment on new lines of a file, Suggest change inserts a fenced code block marked `suggestion`, the word
after the three backticks that open it, which holds the thread's lines. Change them to what you propose. Apply
appears on a posted message that holds exactly one such block, outside any list or quote, that changes the lines,
while the review is open and the thread is not outdated. It turns the suggestion into a pending edit of those
lines, for the author to take in, and works once per message. In Scratch mode an Apply is a draft like any other
action. A suggestion can be made on any text file. It never runs, also when a block in it is marked `run`: see
[Running a block in the page](#running-a-block-in-the-page).

### Editing files and the description

The Edit button on the card of a Markdown file opens the whole file in an editor with a preview. Saving makes a
pending edit: the branch does not change until the author runs `coduck pull` and commits the result. The file's card
shows each pending edit, which waits for `coduck pull`, with a Withdraw button that takes it back before the author
takes it in, and each drafted edit, held until you post. Once `coduck pull` has taken an edit into the worktree, the
page takes no further change to that file until the result is committed, and says so when you try.

An edit made on top of an earlier pending edit of the same file replaces it. Outside the Tasks tab the page edits
Markdown files only; for other files, make a suggestion.

The description has an Edit button too. A change to it takes effect as soon as it is posted, since the description
is part of the review and not a file of the branch.

### Markdown views

A Markdown file can be read three ways: **Diff**, the source lines; **Rendered**, the rendered text with changed blocks
marked; and **Split**, the source and the rendered text side by side, scrolling together by source line. In the split
view the threads sit in the source, and badges on the rendered side open them. Fenced blocks of code are highlighted and
Mermaid diagrams are drawn: in the description, in a Markdown file's rendered and split views, in a file opened read
only, and in the epic and a task on the Tasks tab, with the comments that stand inside the description or a rendered
file. A comment under Conversation, a note and a preview show a Mermaid block as its source. A click on a drawn diagram,
or on the control that shows in its corner on hover or keyboard focus, opens it large in a dialog, scaled to fit; a
click that ends a selection of its text opens nothing. There the wheel or a pinch zooms around the pointer, from the
whole diagram up to eight times that, a drag moves it, and Fit shows the whole diagram again. Tab stays among the
dialog's controls. Esc, a click that begins and ends outside the diagram's frame, the close button and Back close the
dialog, and the focus returns to the diagram's control. A click on a diagram starts no comment: the plus beside its
block does, on its source lines. Blocks of Python and SQL marked to run, and their outputs, show as
[Runnable blocks and their outputs](#runnable-blocks-and-their-outputs) says, wherever Markdown is rendered, and a
comment on their lines sits after the block and its outputs.

Links in rendered Markdown stay inside the review where they can. A relative link is resolved against the folder
of its document, and against the root of the repo in the description and the conversation. A link to a file that has
a card opens that card on the Files changed tab; a link to any other file of the repo opens it read only at the head
of the branch. Headings get the anchors GitHub gives them. A reference in the form `owner/repo#12`
links to GitHub, and a link that leaves the repo by a relative path is shown as plain text.

### What is new since you last looked

Each browser remembers the version and the branch head it last showed of each review. From that the list shows
"N new" on a row, the new commits and the new actions on the author's side, notes left out, and a review shows a
dot on threads
with new messages from that side, a "New" mark on those messages, and a divider "New since you last looked" in the
commit list. When the branch moves while you have the review open, a notice names the review's
author, says how many commits were pushed and takes you to them. A browser that has not shown a review before marks
every commit and everything the author's side wrote in it as new, notes left out.

### Keys

- `n` and `p`: the next and the previous change, on the Files changed tab.
- `j` and `k`: the next and the previous thread that is not resolved, drafted threads included, on the Overview
  and Files changed tabs.
- `Esc`: close a dialog, a menu or the task panel.
- Ctrl or Cmd with `Enter`: submit the comment being typed.
- `Shift` with `Enter` in the text of a comment being written: run the runnable block the caret is in; elsewhere in
  the text it makes a line break, as it always did. See [Running a block in the page](#running-a-block-in-the-page).

### Base and project

Change base, beside the branches in the top bar, reviews the branch against another base, for example `main` once
the branch that this one was stacked on has landed. A review whose base no longer exists, or shares no history
with the branch, shows its description and conversation and no changes until its base is changed.

A review can carry a project: the name of an effort that can span repos, written as lowercase letters and digits
in words joined by single hyphens, at most 64 characters. The button beside the project in the header, Change or
Set project, picks a project in use, takes a new name, or drops the project.

### The state of a review

An open review is in one of three states, read from what the review holds:

- **Approved**: the reviewer approved the branch with a Post all, the branch's head is still the head that Post all
  saw, and nothing since ended the approval. A Post all that saw no head, because the branch was gone or the review
  was written before writes recorded heads, approves nothing.
- **Ready**: approved, and the author gave the ready signal, `coduck ready`, after that Post all: nothing more is
  in flight.
- **Needs input**: every other open review.

An approval, and the ready signal with it, ends when:

- the branch's head is another than the one the approving Post all saw: every write records the head it saw, so a
  new commit, a reset, a rebase and a deleted branch all count, whatever the commits' times;
- the description or the base is another than the one the approving Post all left, by either side, a description
  drafted in Scratch mode and posted with Post all included; what that Post all wrote itself is part of the
  approval, and a description or a base changed back to what it was ends nothing;
- a message is posted with the mark needs input, by either side: in the page with Comment and mark not ready, a
  second button beside the submit of a comment, a reply or a message in the conversation in Direct mode, or the
  third choice of Post all; with the command, `coduck reply -i` or `coduck comment -i`, also together with `-w`.

A message without the mark ends nothing, whoever writes it: a reply, a new thread, a message in the conversation,
a Post all with the verdict Comment. Nor does any of these: a resolve or a reopen, a note, the author taking in
edits with `coduck pull`, a change of project, an edit or a suggestion applied by the reviewer and its withdrawal,
a migration, an approval, the ready signal, a commit on the base. What is new for the reviewer
does not depend on the mark: every message is new for them as [What is new for you](#what-is-new-for-you) says,
with the mark or without it. A finished review has no state.

The mark is for a message that asks something of the other side or reports something that needs their action, such
as a check that failed upstream; an acknowledgement, or a report that needs nothing, goes without it. It is recorded
as an event `needs-input` at the end of the conversation, in the commit of its message, under the writer's name and
on their side, and the conversation shows it as a line, "NAME marked the review as needing input". The page offers
it only on a review that is approved or ready. A command takes `-i` on any open review, where it changes the state
only of an approved or ready one, and a finished review refuses it. The mark is no one's word in the conversation:
which threads `coduck show -u` lists does not depend on it.

**Approve**, a verdict of Post all, says the reviewer is satisfied with the branch as it stands. The review stays
open, so that later changes, such as a fix for a check that fails upstream, come back in the same review, and the
review needs input again. The page has no control for the ready signal: it is the author's.

### What is new for you

A review is new for you, the reviewer, when something happened in it since your last write on it: any write of
yours except a note and a change of project, a resolve and an approval included. A message that a session posted
for you (see [Posting for the reviewer](#posting-for-the-reviewer)) is your word and no look at the review: it
answers the author's messages before it, and leaves every other reason as it was. The reasons, in this order:

- **Not reviewed yet**: you have not written in it yourself. Until you do, this stands for everything the author
  did, and only your drafts can be new beside it.
- **New messages**: the author's side wrote a message, started a thread or opened one again, whatever the thread's
  state.
- **New commits**: the branch's head is another than the one your last write saw; it is new since the first commit
  at or after that write, or since the write when the head moved without one, as after a reset.
- **New description or base**: the author's side changed the description or the base.
- **Said ready**: the author's side gave the ready signal after your last write, and has written nothing since. Your
  next write takes it away, as it takes every reason, and so does the author's next write, such as the note that
  says where the work went: that is what ends it when your next step, such as opening a pull request, happens
  outside CoDuck. The review stays ready.
- **Your drafts**: you have drafts on the review.

A look that writes nothing leaves the mark; a reply, a resolve or a verdict takes it away. An open thread whose last
word is the author's is no reason by itself: once you have written anywhere in the review, it is not new. This is
read from the review alone, so `coduck list` and other tools see the same; what this browser last showed is the
row's own "N new".

A message with the mark needs input (see [The state of a review](#the-state-of-a-review)) counts here as any
message does: the mark changes none of these reasons.

### Notes

A note is a fact about a review that nobody is asked to answer, such as where its pull request is or which review
it waits for. It is no comment and no log of what happened: what needs someone's attention is a message. A note is
Markdown of any number of lines, at most 10,000 characters. Either side adds one, and either side can change or
remove any note; a review of any state takes notes, a finished one too.

A note changes no state, is not new for you and does not count in "N new", and wakes no waiting author, but it does
move the review's activity, by which the list sorts. In both comment modes it is written at once.

The Overview's side column starts with the notes, the oldest first, each a card with its rendered Markdown and who
added or last changed it and when. A note longer than about seven lines folds, with "Show all". Add, and the pencil
on a card, open a dialog with the Markdown and its preview side by side and the button "Save note"; the bin on a
card removes the note after you confirm. In the Markdown, as in comments, `owner/repo#N` links to that number on
GitHub, and an address links to itself; links to other sites open in a new tab. The list's row shows the first line
of the review's newest note, the one added or changed last, with its links working; a click on such a link does not
open the review. Notes do not show on the Files changed tab.

### Done

**Done**, under the conversation on the Overview tab, ends a review once its work has landed or is dropped. It
takes no note: add a note at any time, also after Done, to say where the work landed. Done is refused while the
reviewer has drafts, and held back while text is typed and not saved, so that nothing written is lost. Pending
edits do not stop it, and the dialog says how many will never be taken in. A done review takes only messages, in
its threads and its conversation, notes, run specifications and a change of project; it cannot be opened again. Its branch is free for
a new review, which gets a new number. It keeps showing the changes it covered after its branch is deleted.

CoDuck does not merge. How a branch lands is ordinary git work outside it.

## The command

Run `coduck` in a worktree of the repo. A command that works on a review takes the open review of the branch
that is checked out, or review N of the repo it runs in with `-r N`. `-r REPO/N`, the form `coduck list` prints,
names review N of the repo registered under the name REPO from any folder, and the command then reads the screen
names from that repo; a REPO that is not registered, or whose folder is gone, is refused. `coduck -C DIR COMMAND`
runs any command as if CoDuck was started in DIR: the repo, the worktree and every FILE that is not absolute are
found from DIR, and a DIR that is no folder is refused. `coduck -n NAME COMMAND` runs any command under NAME, see
[Names](#names). Where a command reads text from FILE, `-` reads standard input. There are no long options: help
is `-h`, on `coduck` and on every command. A refused command prints the reason on standard error and exits 1; a
usage error exits 2. The commands of products, projects and issues, `products`, `projects`, `issues` and
`problems`, need no repo and take no `-r`; see [The commands of the state
repository](#the-commands-of-the-state-repository).

### Names

`-n NAME`, before the command's name like `-C`, gives the name the command writes under: the author of every event
it adds and of the commit on the review's ref, and the review's author when it opens one. With `-w`, `reply` and
`comment` write under the reviewer's name instead and record NAME as the name they were posted through (see
[Posting for the reviewer](#posting-for-the-reviewer)). NAME is also the caller's name, for `wait` and `show -u`,
which count what NAME wrote or posted as the caller's own and what any other name wrote as news. Without `-n` the
name is the setting `coduck.cliuser`. NAME follows the rule of every name (see
[Settings](#settings)) and must differ from the reviewer's name, ignoring case; both refusals exit 1 before
anything is written. Every command that reads names checks NAME, also one that writes nothing, such as `show`;
`skill`, `check`, `version`, `list` and `migrate` leave it unread.

The commands of products, projects and issues read both names, `coduck.cliuser` and the reviewer's, from git's
configuration in the state repository: the machine's, the user's and the state repository's own, never the repo
they run in; before the state repository exists, from the machine's and the user's alone. A change writes under
NAME and compares it with the reviewer's name there. A command that writes nothing, a `list`, `inbox` or `show`, and
`problems`, leaves `-n` unread.

Give each session its own name, the one under which the reviewer and other sessions find it, and use it on every
command of a review:

```sh
coduck -n docs-session show -u
coduck -n docs-session wait -s <version>
```

Whatever the name, a command writes on the author's side and the page on the reviewer's; the one exception is a
message that a command posts for the reviewer with `-w`, which is on the reviewer's side. The review records the
side of every write: what is new for the reviewer, the ready signal and the Agent label on messages read that side.
Two sessions under two names can work on one review, and each one's writes, notes left out, are news for the other.

### Opening and describing

- `coduck open [-b BASE] [-p PROJECT] FILE` opens a review of the current branch with FILE as its description,
  and adds the repo to the server's list. The title is the first line of the description that is not empty,
  without its leading `#` marks; a description without one is refused. Keep the title to a few words that name the
  change, at most 60 characters: a longer one is not refused, but the page cuts it in its top bar. Without `-b` the
  base is the branch that origin's `HEAD` names, else `main`, else `master`. BASE is a local branch
  (`-b feature-x`) or a remote tracking branch (`-b origin/main`). A branch with an open review, a branch with no
  commits, the base itself and a detached `HEAD` are refused.
- `coduck describe [-r [REPO/]N] FILE` replaces the description, and with it the title. The reviewer can edit
  the description too, so read it with `show` first.
- `coduck base [-r [REPO/]N] BASE` changes the base. The change shows in the conversation.
- `coduck project [-r [REPO/]N] PROJECT` sets the project, and `coduck project [-r [REPO/]N] -d` drops it.

### Reading and waiting

- `coduck show [-r [REPO/]N] [-u] [-j]` prints the title, the branches and the state, with `needs-input`,
  `approved` or `ready` after `open` (and `status` in its JSON), the description, the notes
  with their ids, oldest first, every thread and the pending edits, and last the line `version: <id>`. A note's
  first line is `note <id> by NAME at TIME`, with `, changed by NAME at TIME` once it was changed, and its Markdown
  follows, indented. A message that a session posted for the reviewer shows its author as
  `REVIEWER (through NAME)`, NAME being that session. The conversation shows the ready signal as
  `NAME gave the ready signal at TIME` and the mark needs input as `NAME marked the review as needing input at
  TIME`. With `-u` it prints only what waits on the caller, the name
  `-n` gives or else `coduck.cliuser`: the open threads whose last word is another name's, and the pending edits;
  a message the caller posted for the reviewer is the caller's own word. Each thread's first
  line says where its lines are now, as `now lines 23 to 24`, or that it is outdated, with its place when it has
  one, as `outdated, placed at line 18`. With `-j` it prints JSON, in which each thread has `position`, where its
  lines are while it is current, and `place`, where it sits, the two equal for a current thread, an event that a
  session posted for the reviewer has `through`, `notes` holds the notes and `specs` the run specifications.
- `coduck wait [-r [REPO/]N] [-s VERSION] [-t SECONDS]` returns as soon as a name other than the caller's has
  written to the review after VERSION (a message, a verdict, a resolve or reopen, an edit or its withdrawal, a new
  description, a change of base or of project, the ready signal), or anyone has marked it done, the caller
  included, since two sessions can share a name. A message that a session posted for the reviewer was written by
  that session's name: it is news for every other name and none for that session. A note, a run specification
  and a migration are no news. It prints what arrived and the new version and exits 0. Without `-t` it has no time
  limit; with `-t`, after SECONDS it prints `no news on review N since <id>` and exits 1. VERSION is the full id of
  one of the review's versions, as `show` prints it. Without `-s`, wait counts from the caller's own last change,
  or from the opening when the caller made none. It never reads standard input, so it can run in the background.
  When the repo is gone from the path it had when the wait started, as after its folder was moved, the wait says so
  with that path and exits 1: start it again where the repo is now. Any other failure of git ends it with git's own
  message.
- `coduck list` prints the open reviews of every registered repo, one line each with the review as `REPO/N`, its
  state (`needs-input`, `approved` or `ready`), its branch and base, its project and its title, and a line for
  each repo that is missing or holds reviews this version cannot read.

The author's loop is `coduck -n NAME show -u`, act on each item, then `coduck -n NAME wait -s` with the version
that `show` printed, under the same NAME throughout. Counting from that version means nothing that arrived in
between is missed, from the reviewer or from another session under another name.

### Answering

- `coduck reply [-r [REPO/]N] [-w] [-i] THREAD FILE` adds a message to a thread; THREAD is a thread id or
  `conversation`.
- `coduck comment [-r [REPO/]N] [-w] [-i] PATH:N[-M] [old] FILE` starts a thread on lines N to M of a file at the
  head of the branch, or with the word `old` on the lines of the merge base.
- `-i`, on either, posts the message with the mark needs input, also together with `-w`: on an approved or ready
  review it ends the approval and the ready signal; see [The state of a review](#the-state-of-a-review). It is
  taken on any open review and refused, with exit 1 and nothing written, on a finished one.
- `coduck resolve [-r [REPO/]N] THREAD` and `coduck reopen [-r [REPO/]N] THREAD`.

### Posting for the reviewer

With `-w`, `reply` and `comment` post the message for the reviewer, as when the reviewer worked out an answer with a
session and asked it to post the outcome. The message is the reviewer's: its events and its commit are written
under the reviewer's name, the setting `coduck.webuser` or else `user.name` of the review's repo as the command
reads it when it runs, on the reviewer's side, and both also record the name the command runs under, the one `-n`
gives or else `coduck.cliuser`: each event as `through`, and the commit in a trailer `Through`. A new thread
records it on its opening as well as on its message. A `Through` trailer counts only beside `Role: reviewer`.

It is the reviewer's word: with `-i` it carries the mark needs input on the reviewer's side, recorded with the
same `through`, and ends an approval as the reviewer's own mark does; the page shows it under the reviewer's name,
and it answers what the author's side wrote before it, so that those messages are no longer [new
for the reviewer](#what-is-new-for-you). It is no look at the review: commits, a new description or base and the
ready signal that were new for the reviewer stay new, and a review the reviewer has not written in themselves stays
not reviewed yet. For `wait` and `show -u` it is the posting session's own: it is news for every other name and
none for that session, and a thread whose last word it is waits on every other name and not on that session. Two
sessions under one name take each other's writes for their own, so a session that posts for the reviewer runs
under a name of its own, never under the name of a session that waits on the review: that session would not wake.

Only a message can be posted for the reviewer: `-w` exists on `reply` and `comment` and on no other command, so a
session cannot approve, resolve, reopen, describe, change the base or the project, add a note, give the ready
signal or mark a review done for the reviewer. `-w` is refused, and exits 1 before anything is written, in a repo
where git sets neither `coduck.webuser` nor `user.name`, as the server is; and a name from `-n` that is the
reviewer's is refused with `-w` as without it. The page never posts through a name, so a draft carries none.

### Taking in edits

`coduck pull [-r [REPO/]N]` applies the pending edits to the worktree and its index, in the order they were made,
marks them taken, and commits nothing. Commit what it applied at once; only the commit puts the change on the
branch. The convention is a commit message that starts with the editor's name in square brackets.

Pull runs in a worktree of the review's repo that has the review's branch checked out, so with `-r REPO/N` from
another folder, point it at that worktree with `-C`; anywhere else, a folder in no repo and a worktree of another
repo included, it is refused and says where it runs. It needs no uncommitted change to a tracked file, and no merge,
cherry pick, revert or rebase in progress. It prints one line per edit and exits 0 when every edit was taken, 1
otherwise:

- An edit that clashes with the branch leaves git's conflict markers in the file, counts as taken and ends the
  pull. Resolve the markers, commit, and pull again for the edits after it.
- An edit that git cannot apply at all stays pending, and pull says why.
- An edit can add or remove a file, and pull stages it as such.
- An edit that the reviewer replaced with a later edit of the same file is withdrawn, and pull does not apply it.
  The line of the later edit names the edits it replaced, oldest first, also those that they had replaced in turn:
  `took edit C of PATH by NAME into the worktree and its index; it replaces edits A and B`, or `edit A` for one.
  The line of such an edit that clashes ends the same way.
- An edit that the reviewer withdraws while pull applies it stays in the worktree; pull says so and exits 1. Undo
  that change before you commit, if the withdrawal stands.
- A pull that stops halfway leaves its edit staged and still pending. Commit it and pull again to mark it taken,
  or discard it and pull again to apply it again.
- With no pending edits, pull says so and exits 0.

An edit still pending when the review is marked done is never taken in.

### Saying it is ready

`coduck ready [-r [REPO/]N]` gives the ready signal on an approved review: nothing more is in flight. It writes a
`ready` event in the conversation, in one commit named `ready`, and prints `review N is ready`. It is given on a
review that is approved, as [The state of a review](#the-state-of-a-review) has it, and refused, with exit 1, on any
other, so also after a message with the mark needs input until the reviewer approves again; on a review that is
ready already it writes nothing and exits 0. It is news for the reviewer and ends nothing. After it, another head
of the branch, a new description or base, or a message with the mark needs input takes the review back to needs
input; a message without the mark, by either side, leaves it ready.

### Notes

- `coduck note [-r [REPO/]N] FILE` adds a note, the Markdown of FILE, and prints `added note ID to review N`.
- `coduck note [-r [REPO/]N] -e ID FILE` changes note ID to the Markdown of FILE.
- `coduck note [-r [REPO/]N] -d ID` removes note ID.

A note that holds only white space or more than 10,000 characters is refused, and so is an ID the review has no note
under; each exits 1. Each write is one commit named `note`. See [Notes](#notes) for what a note is.

### Finishing

`coduck done [-r [REPO/]N]` ends the review. It does what the Done button does, and takes no note.

### Checking epic and task files

`coduck check [PATH ...]` checks the headers of epic and task files in the worktree; see
[coduck check](#coduck-check).

### Products, projects, issues and problems

`coduck products`, `coduck projects` and `coduck issues`, each with a verb, keep the products, projects and issues
of the state repository, and `coduck problems` lists what does not fit there; see [The commands of the state
repository](#the-commands-of-the-state-repository).

### Server, backup, skill and version

- `coduck serve [-p PORT]` runs the server on 127.0.0.1, port 8090 unless given; `-p 0` picks a free port. The
  first line it prints is `listening on http://127.0.0.1:<port>/`. It runs a code block only when someone asks for
  it in the page ([Running code blocks](#running-code-blocks)), and when it stops, on Ctrl-C or SIGTERM, it stops
  the runs still going first.
- `coduck register` adds the current repo to the server's list without opening a review.
- `coduck bundle FILE` writes every review ref of the current repo to a git bundle.
- `coduck push REPOSITORY` pushes the reviews of the current repo to an archive repository.
- `coduck skill` prints the instructions for agents that ship with this version, for the folder an agent reads
  its skills from: `coduck skill > <folder>/coduck/SKILL.md`.
- `coduck version` prints the version of CoDuck that is installed, as `coduck X.Y.Z`. Like `skill`, it reads
  no repo and no setting. Run from its source without an installed package, it is refused and says so.
- `coduck key` prints the key that every request about runs carries, for you to give the page the first time you
  run a block in a browser. The server makes it when it first starts; before that, and when the key file is a
  link, no plain file, or readable by other users, the command is refused and says why. It reads no repo and no
  setting. It is yours alone: an agent never runs it.
- `coduck migrate` brings every review of every registered repo to the format this version reads; see
  [How a review is stored](#how-a-review-is-stored).

## Runnable blocks and their outputs

A fenced block of Python or SQL can be marked as runnable, and what it printed is kept right after it in an output
block. Both are plain Markdown, which reads as code wherever Markdown is shown, and both can be written by hand: for
code you ran in a shell, the output block is the record of what it printed and what it ran on. CoDuck never marks
an output as current or stale; an author may change the code and its outputs freely.

### A runnable block

A fenced block whose first line goes on, after its backticks, with `python run` or `sql run` is a runnable block; the
first word is read in any case, so `Python run` runs too. A block marked `python` or `sql` alone is only coloured, so
that code that shows something never runs.

A third word is the name of a file the block is a part of: `python run invoice.py`, `sql run prices.sql`. A name holds
letters a to z in either case, digits, dots, hyphens and underscores, at most 64 characters, and names no folder:
`.` and `..` are no names.
Within one text (a comment, the description, a note, a Markdown file, a task) the blocks of one kind that carry one
name are the parts of one script, in the order they stand: a later part sees what an earlier one made. A block
without a name is a script of its own, and SQL and Python never share anything.

Anything else after `run`, as a second name or a name with a slash, and `run` after another word than `python` or
`sql`, as `mermaid run`, makes the block a plain block, coloured or drawn as it would be without `run`, and the page
says why above it.

### An output block

Right after a runnable block, with blank lines only between, comes one output block per configuration the code ran
on. A SQL block that ran on DuckDB 2.0.1:

````markdown
```sql run
SELECT p::DOUBLE AS price, round(price, 2) AS cents FROM (VALUES (2.675), (0.125), (1.005), (0.015)) t(p);
```

```output v1
duckdb: 2.0.1 (8d78114e90)

┌────────┬────────┐
│ price  │ cents  │
│ double │ double │
├────────┼────────┤
│  2.675 │   2.67 │
│  0.125 │   0.13 │
│  1.005 │    1.0 │
│  0.015 │   0.01 │
└────────┴────────┘
```
````

The block opens with `output v1`, `v1` being the version of this form. Then come the header lines, one empty line,
and the text exactly as it was printed, line by line: a carriage return ends a line as a line feed does, and a NUL
character reads as the replacement character, as Markdown reads them. A header line is a name, a colon and a value:

- `duckdb`: the version and, in brackets, the source id, as `PRAGMA version` gives them: `duckdb: 2.0.1 (8d78114e90)`.
- `python`: the interpreter's version, for a Python block: `python: 3.14.0`.
- `packages`: the name and version of each package the code loaded, apart from duckdb and Python's own, separated by
  commas: `packages: pandas 2.2.3, numpy 2.1.0`.
- `extensions`: the DuckDB extensions loaded beyond those built in, separated by commas: `extensions: tpch`.
- `exit`: the program's exit status, when it is not 0: `exit: 1`.
- `stopped`: why the run was stopped, or why later parts did not run: `stopped: time limit 60 s`.

Leave out a line that has nothing to say; a line with an empty value says nothing, as if it were not there, and a
value holds no line break. The header may be empty: the block then starts with the empty line. A
Python block's output looks like this:

````markdown
```output v1
python: 3.14.0
duckdb: 2.0.1 (8d78114e90)
packages: pandas 2.2.3, numpy 2.1.0

(2.675, 2.67)
```
````

`duckdb -version` prints what the `duckdb` line needs, as `v2.0.1 (Cyanoptera) 8d78114e90`, and that line will do as
it is: the page reads the first version number in the value, and ten hexadecimal digits anywhere in it as the source
id. In Python, `sys.version` and `duckdb.sql("PRAGMA version").fetchall()` give the same.

How the page reads an output block, so that one written by hand works:

- The names are read in any case and in any order, and so is the word `output`; `v1` may be left out: `output`
  alone is read as `v1`.
- A line is a header line only when it starts with one of the six names and a colon. A block whose first line is
  neither a header line nor empty has no header and is all text, and the page says that it does not say what it ran
  on. So an output whose first line is `total: 2.81` is not taken for a header.
- Without the empty line, the header ends at the first line that is no header line, and the page says it read the
  header leniently.
- A name given twice is shown twice.
- An output block of another version, as `output v2`, is not read: the page shows it as a plain block and says so,
  and does the same for an output block that follows no runnable block.
- A text that itself holds a line of three backticks needs a longer fence around the output block: four backticks,
  or one more than the longest run of backticks in the text.

### What the page shows

Wherever the page renders Markdown (the description, comments, notes, previews, a Markdown file's rendered and split
views, a file opened read only, the epic and the tasks), a runnable block has a head: its kind; its file name, with
"part 2 of 3" and arrows to the part before and after when the script has several parts; what each of its outputs
ran on, as the DuckDB version, as the Python version (`python 3.14.0`) for an output that names Python and no DuckDB,
or as "not said"; and Run, with the control beside it that chooses what it runs on: see
[Running a block in the page](#running-a-block-in-the-page).

Its outputs follow it. One output shows its header as chips (the DuckDB version and source id, the Python version,
the packages, an exit status, why it stopped) and then its text. Several are compared with the first: a line says
whether they are the same or how many different outputs there are, and they show side by side, one under another,
or as a diff of their lines. Side by side and one under another mark what differs from the first output, except in
an output that ended otherwise than the first, by an exit status or a stop, where marking every line would only be
noise. The view you choose, Table or Text, and an output shown in full stay while the page is open, for the few
hundred blocks you chose on last, and blocks of the same text share them.

Where an output is a table that DuckDB's command line program drew, the page reads it back into cells and shows a
table: the column names, their types, the rows, NULL set apart, numbers to the right, the line of dots where DuckDB
left rows out, the column of dots where it left columns out, and the lines it printed under the table, as
`1000 rows (40 shown)`. Lines of dots are rows left out only when those lines say that fewer rows are shown than there
are; otherwise, as for a value that is a dot, they are rows. Compared outputs mark the cells that differ: rows are
paired by their text first, so that a row that came or went is marked whole and the rows after it are not. The diff
compares a table row by row, cell by cell, so that a column that grew wider changes no line. Text shows the output as it is stored, and the copy button
beside it copies it as stored. Text around a table, such as an error, stays text. A table of another program stays
text too, and so does a table one of whose values holds the line DuckDB draws between cells. A value that DuckDB
wrapped over several lines shows as several rows, one per line.

An output of more than 30 lines is folded, and so is a diff of more than 30 lines, with Show all to open it; a folded
output still scrolls sideways. The page builds only what a fold shows, so the browser's find does not see the folded
part of an output until Show all opens it. Choosing a view, Table or Text, or a fold draws that block's outputs again
and nothing else. A comment on a block's lines, or on an output's, sits after the block and its outputs; the plus
beside the code comments on the code, and the one beside an output on that output, or in the Diff view beside the
diff on the outputs together. In the Split view the badge of a thread stands beside the code or the output it is on.

## Epic and task files

An epic is a Markdown file whose name ends in `.epic.md`, and a task one whose name ends in `.task.md`, in any
folder of any repo. They are plain Markdown that reads well without CoDuck. The Tasks tab of a review shows them
with controls, and every change made there is an edit of the review like any other: a draft in Scratch mode, a
pending edit once posted, taken in with `coduck pull`.

### The files

A task file opens with a header: a line of exactly three hyphens, the fields, and a second line of three hyphens.
Between them, a field is `key: value`, or `key:` followed by lines `  - item`, or `key: [item, item]`:

```
state: in progress
waits-for:
  - read-header.task.md
pull-requests: [example/coduck#12]
```

Every value is text. The header takes no comment, no empty line and no value inside a value, and a file with
Windows line endings has no header.

- `state` is required: `todo`, `in progress`, `paused`, `complete` or `cancelled`.
- `waits-for` lists the task files this task waits for, each as a path from the task's own folder, or from the
  root of the repo with a leading slash.
- `pull-requests` lists pull requests as `owner/repo#N`.
- Any other field is kept and shown as written.

A task's title is its first heading of level one, or without one its file name without the suffix.

An epic lists its tasks in its text. A list item whose first line is the list marker and then a link to a task
file is a task of the epic, in the order of the text, and what the item holds after its first paragraph is the
task's summary. A link to a task file anywhere else is only a mention.

```markdown
# Task files in the page

- [Read the header](tasks/read-header.task.md)

  Both the page and the command read it, from one table of cases.
- [Render the header as a table](tasks/render-header.task.md)
```

An epic's header is optional. Its `project` field names the project that reviews carry, and the Tasks tab lists
the project's other open reviews beside the epic.

In every Markdown file the page shows, a readable header renders as a table of its fields, and a list item that
starts with `[ ]` or `[x]` renders with a checkbox.

### The Tasks tab

The tab shows an epic as its own rendered text, where each task item carries:

- the task's state as a button that changes it;
- a handle and arrows that move the item within its list;
- the tasks it waits for, its pull requests and its ticked checkboxes;
- a pencil that edits the summary;
- a mark for what waits: a draft, an edit waiting for `coduck pull`, or an edit taken in and not committed yet.

Below the last task a field adds a task: a title and a path. The new file starts with `state: todo` and the title,
and the epic gains an item for it. A click on a task opens a panel with its state, the tasks it waits for, its
pull requests, its other fields, its problems, and its text with checkboxes that can be ticked, and with Edit text
and Delete task. Delete removes the file and its items in the epic, and names the tasks that wait for it.

With no epic on the branch the tab offers New epic; with several it lists them. A side column shows the open
reviews of the epic's project, the count of tasks per state, and the task files that are no task of the epic. A
finished review shows the tab without controls.

### coduck check

`coduck check [PATH ...]` reads the epic and task files below the current folder that git tracks or would add,
or the files and folders named, and prints each problem as `path:line: problem`: a header it cannot read, a
missing or unknown state, a path in `waits-for` that names no task file, tasks that wait for each other in a
circle, a pull request in another form, a project that is no project name. It exits 0 with no problem, 1 with
problems and 2 for a path it cannot check. It needs no review and writes nothing. It reads headers, not an epic's
list: a task item whose file does not exist shows on the Tasks tab only.

## Products, projects and issues

Besides reviews, CoDuck keeps products, projects and issues: the software there is, the units of work on it, and the
things to do or decide about it. Each is a Markdown file with a header of fields, in a git repository of CoDuck's
own, the state repository, which any program can read and write by the format below. The commands
`coduck products`, `coduck projects` and `coduck issues` keep them, `coduck problems` lists what does not fit, and
the server answers for them in [the HTTP API](#the-http-api). The page does not show them yet.

- A **product** is a piece of software that projects and issues are about, such as CoDuck. It has a title and a
  text, and it may have an origin, the URL of the repository that holds its code, and labels it offers.
- A **project** is a unit of work, about one product, with a brief and a state, open or closed. It names its issues
  in the order of its board.
- An **issue** is something to do or decide about a product: a defect, a feature, a question. It has a state,
  labels, children (the issues that are a part of it), the issues it is blocked by, and pull requests.

### The state repository

- It is the bare git repository `state.git` in the state folder: `~/.local/state/coduck/state.git`, or
  `$XDG_STATE_HOME/coduck/state.git` when that is set. Its branch `main` holds every product, project and issue, one
  file each.
- The first change that writes something makes it, readable by its user alone, with a first commit,
  `make the state repository`, written as `coduck` on neither side, that holds its `README.md` and the file
  `format`. Before that every read finds nothing and makes nothing.
- CoDuck reads it only from the commit `main` points at, never from a worktree. Every change is one new commit on
  `main`, made on the condition of the branch's old value and made again from the new `main` when another writer
  moved it first, so that no change is lost when the server and several commands write at once.
- To browse it, clone it, and pull in the clone for what came later:

  ```sh
  git clone ~/.local/state/coduck/state.git ~/coduck-state
  ```

  To change a file by hand, change it in the clone, commit and push. The state repository refuses a push that does
  not fast forward and one that deletes `main`: pull and push again. Change a field and leave the others as they
  are, and never rename, move or delete a file.
- A commit that CoDuck makes changes only the files its action names, and in each file only the field, the title
  or the text it names: every other byte stays, a field it does not know among them. CoDuck never deletes, renames
  or moves a file there. Its message is one line that names the action, whose first word is one of `make`,
  `create`, `set`, `add`, `remove` and `move`, as `create issue 1-wait-never-returns in project 2-p` or
  `set state of issue 1-wait-never-returns to todo`, then the trailers `Role`, `agent` for a command and `reviewer`
  for the server, and `Coduck`, the version. Its author and committer are the name of whoever acted, with an empty
  email: for a command the name `-n` gives or else `coduck.cliuser`, for the server `coduck.webuser` or else
  `user.name`, each read in the state repository (see [Names](#names)).
- It has no remote. The backup script bundles its `main` (see [Backup and archive](#backup-and-archive)).
- A state repository that CoDuck cannot read as a whole is refused by every command and by the server, which say
  why: something at its path that is no git repository, a repository with a worktree, one without `main`, one
  without the file `format` or whose `format` holds no number, and one in another format than 1, as a newer CoDuck
  writes it. Move what is in the way, and the next change makes a new state repository.

### The format, version 1

The top of `main` holds `README.md`, which says what the repository is, `format`, one line with the number of the
format, `1`, and the folders `products/`, `projects/` and `issues/`, one per kind, which hold files alone. Anything
else at the top, as `.github/` or notes of your own, is left alone.

**Names.** A file is named by an id and `.md`, or by an id, a hyphen, words and `.md`: `12.md` or
`12-notes-on-a-finished-review.md`. The name without `.md` is what a reference holds and what the command shows.
It never changes, and a file never moves.

- An id is a number without leading zeros, as `12`, with lowercase letters before it for something brought in from
  elsewhere, as `r5`. `R5` and `007` are no ids. A new file gets one more than the highest id of digits alone
  among the names of its kind, or 1: the ids of products, of projects and of issues each count by themselves. An id
  need not be unique, since the name is what identifies a file: `7.md` and `7-other-words.md` are two issues.
- The words are made from the title a thing has when it is made, and never change after: the title in lowercase,
  `ß` as `ss`, accented letters made plain, apostrophes left out, every run of characters other than `a` to `z` and
  digits made one hyphen, no hyphen at either end, and cut after the last whole word that keeps them within 60
  characters. A letter that has no plain form, as `ø`, is no letter of a word and ends it: `smørrebrød` gives
  `sm-rrebr-d`. A title that leaves no words, or whose first word
  is longer than 60 characters, gives a name of the id alone. `A diff of 1e5 lines (or more) hangs the page` gives
  `a-diff-of-1e5-lines-or-more-hangs-the-page`, and `CoDuck's page` gives `coducks-page`.

**The header** is that of a task file (see [The files](#the-files)). A file opens with a line of exactly three
hyphens, the fields, and a second line of three hyphens, and a file that opens with anything else has no header:

- a field is `key: value`, or `key:` followed by one line per item of two spaces, a hyphen, a space and the item,
  or `key: [item, item]`;
- a key is a letter followed by letters, digits and hyphens;
- every value and every item is text, on one line;
- lines end in a line feed alone;
- no comment, no empty line and no value inside a value.

The order of the fields carries no meaning. A field the format does not name is kept and shown as written, and a
field that appears twice is a problem of the file. A list with no items and a field that is not there mean the
same, and CoDuck leaves out a field whose list has become empty. Blanks around a value and around an item of a
bracket list are dropped, and so are blanks at the end of an item line. A value or an item that has the same quote
character at both ends, single or double, loses that pair; any other quote stays, and nothing inside quotes is
read specially. In the bracket form the text between the brackets is split at every comma, also a comma between
quotes, so an item that holds a comma needs the form with one item per line. CoDuck writes a list one item per line,
so that a change to a list shows as one line in a diff, and quotes a value only where reading it back would lose
something: blanks at its ends, the same quote at both ends, or a `[` that opens a value of a field that takes one.

**The header is not YAML.** A header CoDuck writes reads as YAML too, which is why a git host shows it as a table,
but a program must read and write it by the rules above, not with a YAML library as it comes:

- every value is text: `13` is no number, and `2.10` is not 2.1;
- a `#` in a value is text: in `title: fix for #12` the value is `fix for #12`;
- a value can start with any character but `[`;
- quotes have no escapes, and a comma between quotes still splits a bracket list;
- a key with nothing after it is an empty list;
- an item line is exactly two spaces, a hyphen and a space: a YAML library that writes items at the start of the
  line, or wraps a long value onto a second line, writes a header CoDuck cannot read;
- a comment, an empty line and a nested value are problems of the file.

**The title and the text.** The title is the first line of the body, below the header, that is a heading of level
one: up to three spaces, a `#`, and then a blank or nothing, outside a fenced block of three or more backticks or
tildes. It is taken without the blanks around it and without a closing run of `#` that stands after a blank or
alone, so `# Fix # #` is the title `Fix #`, and `# C#` is `C#`. A heading underlined with `=` is not read. Without
such a heading, or with an empty one, the title is the name of the file. Everything after the title line is the
text; a file without a title line is text throughout.

**An issue** needs a header with `product` and `state`, and may hold these fields:

- `product`: the product it is about, one value;
- `state`: one of the seven states, one value;
- `labels`: free text, for filtering;
- `children`: the issues that are a part of it, in the order a board shows them;
- `blocked-by`: the issues that block it;
- `pull-requests`: pull requests as `owner/repo#N`;
- `reviews`: the reviews of it, by name.

The seven states are the same for every product. Five are open: `inbox`, reported and not triaged yet; `proposed`,
written as a part of planned work and not accepted yet; `todo`, accepted, with work not started; `in progress`; and
`paused`, started and stopped for now. Two are closed: `complete`, done, and `cancelled`, not done and not to be
done.

The inbox is the issues in the state `inbox`, and nothing else decides it. Something that is reported, as a defect
an agent finds, is made in the inbox, and triage gives it `todo` or `cancelled`. An issue written while work is
scoped, in a project or as a part of another issue, is made `proposed`, and is accepted by giving it `todo`. An
issue is blocked while an issue its `blocked-by` names is open; a name that leads nowhere, and an issue that cannot
be read, block nothing. What an issue is a part of is not in its file: CoDuck reads it from the projects and the
issues that name it.

**A project** needs a header with `product` and `state`, and may hold these fields:

- `product`: the product it is about, one value;
- `state`: `open` or `closed`, set by hand and never read from its issues;
- `labels`;
- `issues`: the issues of its board, in the board's order;
- `reviews`.

Its text is the brief. Its board shows its issues in their order, each with its children below it in theirs.

**A product** needs no header at all: an empty file is a product whose title is the name of its file. It may hold
`origin`, the URL of the repository that holds its code, one value, and `labels`, the labels it offers for its
issues and projects, which may carry others too. A product whose last field goes loses its header, unless the text
below it opens with a line of three hyphens: then the header stays, empty, so that the text is not read as one. An
empty header is a header with no field.

**References.** `product` names a product, `children`, `blocked-by` and `issues` name issues, and `reviews` names
reviews, each by the name of its file without `.md`, exactly: `19` names `issues/19.md` and never
`issues/19-wait-never-returns.md`. Nothing names a project. In the text below the header, a file names another with
an ordinary Markdown link to its real path, and no relation is read from it. A review is never found by its name in
this version, and shows as not here.

**When and by whom.** When a thing was made, and by whom, is the author date and the author of the newest commit
that added its file, merges left out; when it was last changed, and by whom, is the newest commit that touched it.

**Between files.** Nothing makes one file agree with another: anyone can write a file, and git merges what two
copies did. CoDuck checks when it reads, shows what does not fit with the thing it belongs to, and repairs nothing:
it removes no name that leads nowhere and rewrites no file it did not set out to change. A loop of `children` or of
`blocked-by`, and an issue that is its own child, are shown as written: a board shows each issue once and marks
where a loop closes, and issues that block each other in a loop are all blocked. A closed project may name open
issues, and a complete issue may have open children: no state is read from another file.

### What does not fit

`coduck problems` lists every file that breaks the format and every name that leads nowhere, as
`path:line: problem`, sorted by path, then by line, those with no line first. The server's `/api/problems` and the
thing a problem belongs to show the same. A file CoDuck cannot read stops nothing else.

A thing whose file breaks one of these uses no field. The command shows it with its problems and its file as it is,
and refuses every change of it, and a new issue in it, with its first problem. A project that cannot be read shows
no board, and its issues are still issues; a product that cannot be read shows by the name of its file and keeps
the issues and projects that name it.

- a header CoDuck cannot read: `an empty line inside the header`, `a comment, which the header does not take`,
  `an indented line that is no list item`, `not a field in the form key: value`, `the field state appears twice`,
  `a list that opens with [ and never closes`, a header that is never closed, and Windows line endings;
- a file of an issue or a project without a header;
- no product or no state: `the header has no product`, `the header has no state`;
- a list where one value goes: `state takes one value`;
- an unknown state: `"active" is no state; a project's state is open or closed`;
- a file that is no text, not UTF-8 or with a NUL byte, and a link or a submodule where a file goes, which have no
  line.

A thing with one of these problems is still used, and the problem shows on it:

- a product that names nothing: `product names 999-x, which is no product`; the thing shows under no product;
- a name in `children`, `blocked-by` or `issues` that leads nowhere: `blocked-by names 99-x, which is no issue`,
  followed by `: a reference holds the whole name, as 2-another-valid-issue` when a single issue has the id or the
  words it holds; it shows as not found and blocks nothing;
- a name twice in one list: `children names 13-x twice; it counts once, at its first place`;
- a pull request in another form: `"#136" is no pull request in the form owner/repo#N`.

These are problems of the repository, which have no line, and CoDuck leaves them alone:

- a file whose name follows no rule, as `notes.md`, `007.md` or `99.comments.md`;
- a folder inside one of the three folders, in which CoDuck reads nothing, an empty one too; a new file never takes
  the name of such a folder;
- `products`, `projects` or `issues` that is no folder: that kind reads as empty, and every change that would write
  one is refused.

No problem at all, only what shows on the thing: a review that is not here, a label its product does not offer, a
loop, two files with one id, a product without a header, and a closed holder with open parts.

### The commands of the state repository

They run in any folder, need no repo and take no `-r`. FILE is Markdown, or `-` for standard input. Their names are
read in the state repository (see [Names](#names)).

**Naming.** A thing is named in full (`19-wait-never-returns`), by its id (`19`) or by its words
(`wait-never-returns`): the exact name first, then the id, then the words, and the first of the three that fits
anything decides, so `7` names `7.md` beside `7-other-words.md`. When an id or words fit several things, the command
refuses and names them: `issue 21 is the id of 21-a and 21-b; name the issue in full`. Labels and pull requests are
taken as written. An item that `remove` or `move` takes from a list, and the item that `-b` places before, are first
matched as the list holds them, so that a name that leads nowhere can be taken out. The server takes names in full
alone.

**Products.**

- `coduck products list [-j]` lists every product: its name, its origin or `no origin`, and its title.
- `coduck products show [-j] PRODUCT` shows a product: its origin, the labels it offers, the labels in use, as
  `defect (2 issues, 1 project), crash (1 issue, not offered)`, how many of its issues and projects are open and
  closed, its other fields, who made it and who changed it last, its problems and its text, and last the line
  `version: <id>`, the commit of `main` it read.
- `coduck products new [-o ORIGIN] [-l LABEL] (-t TITLE [FILE] | FILE)` makes a product and prints
  `created product 1-coduck`. `-o` gives its origin, and `-l` a label it offers, once per label. The title is that
  of `-t`, or else the first heading of level one of FILE, wherever it stands; FILE holds the text.
- `coduck products set [-d] PRODUCT FIELD [VALUE ...]` changes the `title` or the `origin` to the words of VALUE
  joined by single spaces, or with `-d` drops the origin, and prints `origin of product 1-coduck: VALUE`, or
  `none`.
- `coduck products add PRODUCT labels LABEL ...` and `coduck products remove PRODUCT labels LABEL ...` offer labels
  and stop offering them, and print the whole list.
- `coduck products text PRODUCT FILE` replaces the text below the title.

**Projects.**

- `coduck projects list [-j] [-p PRODUCT] [-a]` lists the open projects, and with `-a` the closed ones too, each
  state newest first, with those that cannot be read last: the name, the state, the product, how many issues of
  the board are open and closed, and the title. `-p` keeps those of PRODUCT, once per product.
- `coduck projects show [-j] PROJECT` shows a project: its product, its state, its labels, its board, its reviews,
  its other fields, who made it and who changed it last, its problems and its brief, and last `version: <id>`. The
  board shows each issue with its state and its title, its children below it two spaces deeper, `(loop)` where a
  loop closes, `(shown above)` for an issue shown before, `not found` for a name that leads nowhere and
  `cannot be read` for an issue that cannot.
- `coduck projects new -p PRODUCT [-l LABEL] (-t TITLE [FILE] | FILE)` makes a project, open, and prints
  `created project 18-threads, open`.
- `coduck projects set PROJECT FIELD VALUE ...` changes the `title`, the `state`, `open` or `closed`, or the
  `product`. Closing a project changes none of its issues.
- `coduck projects add [-b ISSUE] PROJECT FIELD ITEM ...` names issues on the board, before ISSUE with `-b` and at
  the end without, or labels; `coduck projects remove PROJECT FIELD ITEM ...` takes them out, and
  `coduck projects move [-b ISSUE] PROJECT issues ISSUE` moves an issue before another or to the end. Each prints the
  whole list.
- `coduck projects text PROJECT FILE` replaces the brief.

**Issues.**

- `coduck issues list [-j] [-p PRODUCT] [-s STATE] [-l LABEL] [-a]` lists the open issues, one line each: the name,
  the state, followed by `, blocked` while an issue that blocks it is open, the product, `no product` where it names
  none, and the title; by the order of the seven states, each newest first, then by name, and those that cannot be
  read last, as `unreadable`. `-p` once per product, `-s` once per state and `-l` once per label narrow the list, and
  an issue needs every label. `-a` lists closed issues too, and `-s` lists the states it names, closed or open.
- `coduck issues inbox [-j] [-p PRODUCT]` lists the inbox, across products.
- `coduck issues show [-j] ISSUE` shows an issue: its product, its state, its labels, what it is a part of, its
  children as a board, what blocks it, what it blocks, its pull requests and reviews, its other fields, who made it
  and who changed it last, its problems and its text, and last `version: <id>`.
- `coduck issues new [-p PRODUCT] [-s STATE] [-l LABEL] [-i PROJECT] [-c ISSUE] (-t TITLE [FILE] | FILE)` reports or
  writes an issue and prints `created issue 447-a-diff-of-1e5-lines-or-more-hangs-the-page, inbox`. A reported issue
  goes to the inbox. One made with `-i`, an issue of PROJECT at the end of its board, once per project, or with `-c`, a part
  of ISSUE at the end of its children, once per issue, is proposed, and the one commit that makes it changes those
  files too. `-s` gives another state, any of the seven. Without `-p` the product is that of the first project or
  parent whose product exists.
- `coduck issues set ISSUE FIELD VALUE ...` changes the `title`, the `state` or the `product`.
- `coduck issues add [-b CHILD] ISSUE FIELD ITEM ...` adds `labels`, `children`, `blocked-by` or `pull-requests`,
  with `-b` among `children` alone; `coduck issues remove ISSUE FIELD ITEM ...` takes them out, and
  `coduck issues move [-b CHILD] ISSUE children CHILD` moves a child. Each prints the whole list.
- `coduck issues text ISSUE FILE` replaces the text below the title.

**Every change** prints what the thing holds after it, as `state of issue 448-keep-the-anchor: todo`,
`children of issue 448-keep-the-anchor: 449-a-test, 450-docs` or `issue 19-x has a new text`. A change to what the
thing holds already prints the same line and writes no commit. FILE is read as it is, its line ends kept. It must be
UTF-8 text without a NUL byte, and must not open with a header, also after empty lines: the fields go in the
options. A list that holds an item a header cannot hold as CoDuck writes it, as one that ends in a carriage return,
changes only by taking that item out, and a commit's subject shows a name with a control character as Python writes
it. The FILE of `text` must not open with a heading of level one,
since the title changes with `set`, and neither may the FILE of `new` beside `-t`. Where the file has no title line,
the FILE of `text` holds no heading of level one at all, an empty one too, since it would become the title line. A
title and a
label are one line that holds more than space, an origin one line without white space, a state one of those of its
kind, and a pull request `owner/repo#N`; a name a field takes must be the name of a thing of its kind. A refusal says
why on standard error and exits 1. A usage error exits 2: an unknown verb or field, no title and no FILE, `-b` with a
list that keeps no order, and `-d` on another field than the origin or beside a VALUE.

**`coduck problems [-j]`** prints every problem, one per line, and exits 1 when there is one; with none it prints
`no problems in 2 products, 18 projects and 451 issues` and exits 0. It writes nothing. With `-j` it prints
`{"version", "problems"}`, each problem `{"path", "line", "words", "group", "kind", "name"}`, and exits as without.
`group` says what kind of problem it is, so that a reader can group problems without reading their words; it is one
of these:

- `unreadable`: the file is no text, or no plain file but a link or a submodule;
- `header`: the header cannot be read, or a file of an issue or a project has none;
- `field`: a field is missing (`the header has no state`), holds a list where one value goes, or holds a pull
  request in another form;
- `state`: the state is none of those of its kind;
- `product`: the product names no product;
- `reference`: a name in `children`, `blocked-by`, `issues` or `reviews` counts for nothing: it leads nowhere, or
  it stands in its list a second time;
- `stray`: a file or a folder inside one of the three folders that is no product, project or issue;
- `repository`: `products`, `projects` or `issues` is no folder.

`-j` on `list`, `inbox`, `show` and `problems` prints the JSON the server answers (see [The HTTP
API](#the-http-api)); the JSON of a list holds the rows its filters keep. A board that nests deeper than the
command prints as JSON, some hundreds of levels, is refused with the route to try instead. The server refuses in
turn a board that nests deeper than it writes as JSON, some thousands of levels.

An agent reports a defect, and triage takes it into a project before issue 12:

```sh
coduck -n diff-session issues new -p coduck -l defect report.md
coduck issues inbox
coduck issues set 447 state todo
coduck projects add 2 issues 447 -b 12
```

A project is scoped with issues, one of which is a part of another and one blocked by another:

```sh
coduck projects new -p coducks-page -t "Threads follow their lines"
coduck issues new -i 18 -t "Keep the anchor of each thread"
coduck issues new -c 448 -t "A test that rebases the branch mid review"
coduck issues new -i 18 -t "Document how a thread moves"
coduck issues add 450 blocked-by 448
coduck projects show 18
```

## Running code blocks

A `sql run` or `python run` block runs only when someone presses Run on it in the page: reading a review, in the
page or with a command, runs nothing, and no command runs a block. The server does the running, with the
permissions of the user it runs as.

Every request about runs, to start, read or stop one, carries the key: the header `X-Coduck-Key` with what
`coduck key` prints, 64 hex digits of 32 random bytes that the server makes at its first start in `key` in the
state folder, readable by the user alone. The page asks for it once in each browser, the first time someone runs a
block there, and keeps it in the browser's storage. The guard of every other write, the `Host` header and
`X-Coduck: 1`, stops a page of another site; it stops no program on the machine, and running code is what any
account on a shared machine, or an agent kept from the user's files but free to reach localhost, must not be able
to make the server do for it. A request without the key, or with another, is refused with 403. A program that runs
as the user can read the key as it can read anything of the user's; agents are told never to run `coduck key` or
start a run.

### Running a block in the page

Run is in the head of every runnable block, wherever the page shows one. It runs the block's script: the block
alone, or for a block with a file name every block of its text of the same kind with that name, from the first. It
runs once for each configuration chosen in the block's picker, the control beside Run, and Run names what it runs
on: "Run on 2.0.1", "Run on this branch" for the branch's build or .venv, "Run on NAME" for a run specification,
"Run on 2" for two. The choice holds for every part of the script while the page is open. Before you choose, a SQL
block runs on the DuckDB versions its outputs say they ran on, where they can run here; otherwise, and for Python,
on what you last ran a block of the kind on; else on the newest DuckDB installed, the branch's build or the newest
release for SQL, and on the branch's .venv or the first run specification for Python. The server takes two runs at
once, of all its reviews: the page starts two and queues the rest, which say that they wait for their turn, and a
run that finds both places taken by others waits until one ends and says so.

The picker lists what the server finds, as [What a block runs on](#what-a-block-runs-on) says: for SQL the DuckDB
programs installed on this machine and those built in the branch's worktree, which run at once, the releases, newest
first, which uv fetches the first time, and a field for a version typed by hand; for Python the branch's .venv,
which may build first when its duckdb is an editable install, and the review's run specifications, which uv fetches
the first time. It says the limits a run of the review has, once the page knows the key, and where no scope holds a
run's memory, that DuckDB's own memory setting is its limit. New run specification from this block opens a dialog
with a name, a Python version and the requirements, one per line, filled in from the block: the newest Python uv
has, duckdb pinned to the newest release, and the packages the block imports, Python's own left out. Save keeps the
specification in the review, for everyone in it, and checks it in the picker; Save and run also runs on it.
Change, beside a specification, opens the same dialog, with Remove. Without uv a specification is listed as one
that cannot run, and why. Where nothing can run (no DuckDB installed or built and no uv for SQL, no
.venv and no uv for Python) or where runs are off, the picker says why and what to install, Run says so when you
point at it, and Look again asks the server once more. A Run with nothing to run on starts nothing and says why.

While a run goes, the block shows under its code, for each configuration, its phase in words (waiting for its turn,
fetching through uv, building the branch's duckdb, running, stopping), how long it has taken, the last lines that
part has printed so far, and Cancel. Cancel asks the server to stop the run, and the run shows as stopping until the
server says it ended, which can take ten seconds, five for the script to end and five to learn what it ran on, and
thirty while a build prepares. A run that could not start, or that failed while it prepared, says why, with what it
printed while it prepared, folded when that is long. When the server refuses to say how a run goes, as after a
restart with runs off, the run ends in the page with the server's words; while the server does not answer, the run
says so and the page goes on asking, and a read or a Cancel that has no answer within six seconds counts as not
answered. A start is never sent twice, since the server starts a run before it answers: when its answer takes more
than six seconds, the run says that the server has not answered yet and the page waits for that answer, and a Cancel
pressed meanwhile is sent once the answer names the run. A run that waits for a place ends with the server's words
when the server refuses to list the runs, and one refused as the place freed is tried again. A Cancel refused for
the key is sent again once the key is given. Run on a block whose script runs does nothing. Runs of the review that
this page did not start, as in another tab or before a reload, show under the review's title, with Cancel.

When the runs of one Run have all ended, each part of the script gets one output for each run, in the order of the
picker, written from the header and the text the server gives. Of what a run prints the page keeps text: terminal
escape sequences, as the colours DuckDB's command line program prints in some cases even into a pipe, after `.once`,
in their two byte and their one byte forms and also cut off, and every control character but the tab and the line
ends, are taken out before an output is shown or written; an escape character that begins no sequence goes alone.
The header values a run reports are cleaned the same way. A run in which no part ran, as one cancelled while it
prepared or one that failed to start, gives no output and says why. Where the outputs go depends on where the block
stands:

- In a text being written (a comment, a reply, the summary of Post all, the description or a file in its editor, a
  note in its editor, a task's summary): into the text, after each part of the script, in place of every output that
  stood there, in the line ends the text uses. They go to the block at the place the run started from, and only while
  its code is what ran: when the code changed while it ran, the text stays as it is and the page says so. A part
  that did not run gets an output whose header says why, as the server reports it. Saving, posting or closing the
  text while its block runs, Post all included, stops the run, and the page says that its outputs were not written.
- On a posted comment, a draft, the description, a note, an epic or a task: to you alone, under the block, with
  Discard. An output of yours that ran on what a stored output of the block ran on, the same `duckdb` and `python`,
  says whether it is the same as that one, the same text that ended the same way (`exit` and `stopped`). Otherwise
  it says what differs in how the two ended, if anything, and stands compared with it: in the views the stored
  outputs have where the texts differ, and one under the other where only the end does. Under a comment of a thread
  or of the conversation, Post as a reply writes a reply, a draft in Scratch mode, that holds a line saying whose
  block it ran, every part of the script as a runnable block, and your outputs after each, so that the reply stands
  on its own. Post as a reply and Discard stand once for a run, under its first part. A run whose text leaves the
  page while it goes, as a draft posted with Post all or deleted, or a note removed, here or in another tab, is
  stopped, and the page says so.
- In a rendered Markdown file: to you alone, with Keep as a suggested edit, which makes an edit of the file as the
  file editor would, from the text that editor would open on, with your outputs after the block in place of those
  that stood there, in the file's own line ends. The page never writes a file.

Outputs that are yours alone live in the page's memory and are gone when you reload; the page says so beside them.

In the Write tab of a comment, `Shift` with `Enter` runs the block the caret is in, and its runs show under the text,
since the blocks are drawn only in Preview; elsewhere in the text the keys make a line break. As soon as the text of a
comment holds a runnable block, its foot says that `Shift` with `Enter` runs the block the caret is in, and in the
Write tab the strip under the text has Run for that block, with the picker beside it, named for what it runs on as
Run is everywhere else. It runs the block as the keys do, the same question, key and queue included. With the caret
outside every block the strip says to put the caret in one, and a text without a runnable block shows neither. The
SQL and Python buttons above the text put an empty runnable block at the caret, with the caret inside it. The picker,
the question below and the one for the key open over a dialog that is open, as the note editor or the file editor,
and give it back as it was: its text, its caret and where it was scrolled.

A new comment on lines of the branch's side of a Markdown text, a file in its Diff, Rendered or Split view or the
description, offers Run this code above its text when those lines are a fenced `python` or `sql` block, plain or
marked `run`, or lie inside one; the editor of such a comment drafted offers it too. Removed lines are the merge
base's, so a comment on them offers no copy. Run this code puts a copy of the block's code into the comment, after
what the comment already says and outside a fence it leaves open, as a runnable block of the same kind without a file
name, with the caret in it, so that `Shift` with `Enter` runs it there. When the comment holds such a block with that
code already, it puts the caret in that one instead. The copy is in the comment only: the file or the description
does not change.

A comment of yours that starts a thread on a Markdown file or the description, as you write it, in its draft and once
posted, holds the review author's code in every block whose kind and code are those of a fenced block that the text
holds now on the branch, one whose first word is `python` or `sql` whatever follows it, as every text of the branch is
the author's. Such a block asks before its first run as the block of the text would, wherever the comment's lines are
and whether or not they have changed since; every other block of the comment is yours and asks nothing, a copy whose
code you changed among them, and so are your replies. A copy asks as long as the text still holds a block with that
code. On a Markdown file that did not load, every block of such a comment asks, until the file loads.

A suggestion is a change of lines and never runs, also when a block in it is marked `run`, since the page shows it as
lines that change and not as Markdown; a comment that holds such a block in a suggestion says so, and names Run this
code where the comment offers it.

Before the first run of a block that someone else wrote (an agent's comment or note, a file of the branch, the
description, the epic or a task), the page asks, shows the code, and says that it runs on this machine as you, with
your own permissions. Code in the branch's own texts, its files, the description, the epic and tasks, counts as the
review's author's, whoever committed it, so the branch takes one question in a review. Such a text opened for
editing, in the file editor, the description editor, the note editor or a task's summary, holds that author's code
until you change it: a block whose code differs from every block of the text the editor was opened on is yours,
and a block that is as one of them stays the code of whoever the text was by when the editor opened. Both hold
whatever became of the text in the review since: changed by someone else, by you in another tab, or removed. A
draft of the description, shown under it, holds that author's code where a block is as one of the description as
it is now. The answer holds for that author in that review and is kept in the browser's storage, and it runs the
code the question showed: when the block changed while the question was open, it is asked again for what is there
now. The first run in a browser asks for the key that `coduck key` prints and keeps it in the browser's storage,
in small letters however it was typed; a key the server refuses asks again. Until a run asks for it, nothing of the
page needs the key.

### What a block runs on

The page offers what the server finds for the review. CoDuck installs nothing and keeps no environment of its
own:

- SQL on each DuckDB command line program installed as `~/.duckdb/cli/VERSION/duckdb`.
- SQL on a released version from 1.5.0 on, or on a version typed by hand, which uv fetches as the PyPI package
  `duckdb-cli` the first time it is used and keeps in its cache, when `uv` is on the server's path.
- SQL on each `duckdb` program built in a folder under `build/` of the worktree that has the review's branch
  checked out, such as `build/release/duckdb`.
- Python in that worktree's `.venv`, when it has one. Its duckdb is imported once before the script runs, so that
  an editable install that rebuilds the client on import builds while the run prepares.
- Python on one of the review's run specifications, through uv.

Without `uv` only what is installed or built is offered to run on; the picker still lists the review's run
specifications, as ones that cannot run without it. DuckDB extensions are not managed: a block says `INSTALL` and
`LOAD` itself.

### Run specifications

A run specification is a name, a Python version and a list of requirements as pip reads them, such as
`duckdb==2.0.1` and `pandas>=2`. Anyone in the review makes, changes and removes one in the page; no command writes
one. Its name is how the page lists it where one runs, and it never appears in an output. It is a file of its own
in the review, written like a note, but it moves nothing: it changes no state, is new for no one, wakes no waiting
author and does not move the review's activity. `coduck show -j` carries the review's specifications as `specs`.

- The name is one line of at most 64 characters, and not all white space.
- The Python version is `MAJOR.MINOR` or `MAJOR.MINOR.PATCH`, such as `3.12` or `3.12.3`.
- At most 50 requirements, each a line of at most 200 characters without white space at either end; an option,
  which starts with `-`, and a comment, which starts with `#`, are refused.

A run on a specification has uv make the environment from a script whose header names the Python version and the
requirements (PEP 723). uv keeps it in its cache, and fetches what its cache lacks.

### How a run goes

A run is one script: a block alone, or the blocks of one Markdown text that are of one kind, SQL or Python, and carry
the same file name, in the order they stand. First it prepares, without a limit, since a build needs the machine's
resources: a release or a specification is fetched through uv, and the branch's `.venv` imports duckdb once. Before
the script, CoDuck asks a DuckDB program for its help, to learn whether it knows `-no-agent`; after it, where the
run could not measure them, for its version, or the interpreter for its own. Each such question, like every step of
preparing, is a process group of its own, and a question has 30 seconds. Cancel stops a question before the script.
The question after it is asked also after a Cancel, so that a cancelled run says what it ran on as one a limit
stopped does. After a Cancel it has five seconds, from the Cancel or from its own start when the Cancel came first,
and is killed when they are over; it changes neither the time nor the reason of the stop. Then the script runs:

- In a new folder of its own as its working directory, removed when the run ends. The folders of runs are in the
  state folder, under `runs/`, which the server makes for the user alone, and not in the machine's shared temporary
  folder, where another account could plant a link that the removal would follow into the user's own folders, and
  where on some machines what a query writes would take memory. Each server keeps the folders of its runs in a
  folder of its own beside a lock that it holds while it lives; when a server starts, it removes what no living
  server holds and any link it finds there. When `runs/` cannot be made, or is no folder of the user's that nobody
  else can enter, or the key cannot be had, runs are off: `runtimes` says why, every request about runs is refused
  with that reason, and the review server serves all the same. The server looks at the folder and the key when it
  starts, so a fix, as a `chmod`, takes effect when it starts again, and the reason says so.
- With a small environment: `HOME`, `PATH` and the locale `C.UTF-8`, and nothing else of the server's, so that no
  variable around the server changes what a program prints. DuckDB 2.0's command line program prints for an AI
  agent when it sees an agent's variables; it is also started with `-no-agent`, which a program that knows that
  option gets.
- With the user's permissions and the network as it is: a block can read and write every file the user can, run
  programs and reach the network. The limits guard a shared machine against accidents, not against a block
  written to do harm.
- Within limits that hold for its whole process group: the time limit `coduck.runseconds` (60 seconds), the
  memory limit `coduck.runmemory` (4G), 4 GB of what it writes into its folder, counted as the blocks allocated
  to it, links not followed, and looked at twice a second, since DuckDB puts there what does not fit in its memory,
  at most 256 processes and threads, and 64 KiB of output kept per part; the rest of the output is counted and
  dropped, and the output says how many bytes. Where systemd runs a manager for the user, the script runs in a
  scope of the user's own (`systemd-run`) with that memory, those processes and four CPUs, from which DuckDB takes
  its own memory limit and number of threads. The end of a run kills whatever is left in the scope, also a process
  that left its process group or ignores being asked to end, so a run always ends and gives its place back.
  Elsewhere the time, disk and output limits hold, and DuckDB's own memory setting is the limit. A program that
  crashes writes no core dump.
- A run that a limit ended says which one in the part it stopped: `time limit 60 s`, `memory limit 4 GB`, when the
  scope says the kernel killed it for its memory, or `disk limit 4 GB`; Cancel says `cancelled at 8 s`, the time
  since the script started. Cancel and a limit ask the whole process group, and the scope, to end, and kill them
  after a grace period of five seconds, thirty while a `.venv` builds, that holds until the last process of the
  group has ended, not only the first. A program that closes its outputs is watched all the same until it ends.

SQL runs in one process of the DuckDB program, with the whole script as its input at once, the parts in order, as
if they were typed one after the other. After each part CoDuck adds a line that ends a statement the part left
open, and lines that have the program print a line of CoDuck's own to tell the parts' outputs apart; only a whole
line that is exactly that line counts, and the line break before it is CoDuck's too. CoDuck parses no SQL, and
reads how the parts went from those lines alone. A part ends inside a string or a comment, and so swallows the
lines after it, until the string or comment ends: when a later line of CoDuck's comes, what came until it is shown
under the first of the parts that swallowed theirs, and each part between says that its output could not be told
apart. When the program ends and no later line of CoDuck's came, the parts after the last one it reached say `not
run, the program ended in part N`: so it is after `.quit`, `.exit N` or an error after `.bail on`, and also when a
string or a comment that a part opened swallowed everything to the end of the input. The program goes on after an
error, as it does with piped input, and its exit status is that of the whole input. Measuring first sets the
program's output back to its standard output, so that a part that leaves `.output FILE` or `.once FILE` on never
gets CoDuck's own lines as its output.

Python runs in a driver that ships with CoDuck, in the interpreter of the `.venv` or of the specification's
environment. The script is written into the run's folder under its file name, `block.py` for a block alone, and
runs as the module `__main__` with `__file__` naming it, as `python FILE` would run it: a function defined in it
can be pickled, `multiprocessing` can start processes from it, and tracebacks show its lines. The file is parsed
once as a whole, so a syntax error anywhere keeps it from running; each part then runs the statements that start in
it, compiled with the file's own `from __future__` imports, and line numbers count through the file. An exception
that is not caught ends the file, so the parts after it are not run and say so. Each part's standard output and
error go to a pipe of the part's own, so nothing is guessed: output goes under the part during which it is written,
so a thread that part 1 started and that prints during part 2 prints under part 2, and what the program prints
after its last part, at its exit and from its threads, goes under the last part that ran.

After the last part the run measures what it ran on: for SQL `PRAGMA version` and the extensions loaded beyond
the built in ones, for Python the interpreter's version, duckdb's version and source id when the script loaded it,
and the packages the script loaded. Where the script swallowed the measuring lines or ended before them, the
program's own `-version`, or the interpreter's version, stands in. A failure while measuring is the run's `error`
and leaves the parts' outputs and headers as they were.

At most two runs go at once; a third is refused, and so is a run while the server stops. A run lives in the
server's memory and nowhere else: nothing of it is written to git, and its folder is removed when it ends. The
server keeps the last twenty runs that ended for the page to read, until it stops; a server that restarts forgets
the runs that were going and those it kept. A server that stops, on Ctrl-C or when its service stops, first stops
every run it has.

## Settings

CoDuck has four settings, all git configuration keys, read with git's own layering, so that each can be set for
one repo, for a user or for a machine. A setting made for a single worktree is not read, so that a repo's commands
and its server read the same names. CoDuck reads no environment variables of its own.

- `coduck.webuser`: the name the page writes as. It defaults to git's `user.name`, and the server refuses to
  start when neither is set.
- `coduck.cliuser`: the name a command writes as when its `-n` gives none. It defaults to `agent`.
- `coduck.runseconds`: how long the running phase of a run may take, in whole seconds from 1 to 86400. It
  defaults to 60.
- `coduck.runmemory`: how much memory a run may take: a number of bytes, or a number followed by `K`, `M`, `G` or
  `T` (powers of 1024). A `B` may end either form, and the letters may be small or capital, as in `4G`, `512m`,
  `2048KB` or `1073741824`. The number has one to twelve digits and is not 0. It defaults to `4G`, and holds where a
  scope holds the run.

The server reads the two limits from the configuration of the repo whose review runs a block, at each run, and
refuses a run while either holds something else than it takes.

```sh
git config coduck.webuser Ada
```

A name is one line that git stores as given: not empty, no `<` or `>`, and no white space, control character or
one of `,:;"'\` at either end; a name that `-n` gives too. The two names must differ, ignoring case: the server and
every command but `skill`, `check`, `version`, `key`, `list`, `migrate`, `problems` and the verbs `list`, `inbox`
and `show` of products, projects and issues, which read no names (`migrate` reads them for a review in format 1
alone), refuse to run when they are equal, since one thread could then show the same name on both sides. For the
same reason a command refuses a name from `-n` that is the reviewer's. The commands and the server read the names
for the state repository in its own configuration (see [Names](#names)).

A review records the side of every write, so a change of `coduck.webuser` changes nothing in the reviews that
exist: what the reviewer wrote under the old name stays the reviewer's, and what the page writes from then on
carries the new name.

## How a review is stored

A review is the ref `refs/coduck/<number>` in the repo of its branch, a chain of commits. The tree of each commit
holds `review.json`, the record of the review, `PR.md`, the description, a log per thread under `threads/`, one
event per line, the pending edits under `edits/`, and a file per note under `notes/`: its author and side, when it
was added, its place in the order the notes were added, its Markdown and, once it was changed, who changed it, on
which side and when, and a file per run specification under `specs/`: its name, Python version and requirements,
who made it, when and in which place of their order, and once it was changed, who changed it and when. The
conversation's log also holds the ready signals and the marks needs input. Each commit's message names the action
on its first line, and as trailers below it the branch head and merge base the review compared then (`Head`,
`Merge-Base`), the side that wrote it (`Role`, `reviewer` or `agent`), for a message a command posted for the
reviewer the name it was posted through (`Through`), and the version of CoDuck that wrote it (`Coduck`; `Redline` on
a commit written through format 5, when CoDuck had that name). Each event records its author and its side as
`author` and `role`, and a message, the opening of a thread and the mark needs input that a command posted for the
reviewer the name they were posted through as `through`.

`review.json` holds `format`, the number of the form the review is stored in; a review without it is in format 1,
everything written before version 0.9.0, format 2 what 0.9.0 and 0.10.0 wrote, and format 3 what 0.11.0 and 0.12.0
wrote, which added the ready signal and notes, format 4 what 0.13.0 to 0.14.3 wrote, which added `through`, and
format 5 what the last version named redline wrote, which was never released and added run specifications. Through
format 5 CoDuck was named redline, and a review was the ref `refs/redline/<number>`. Format 6, what 0.15.0 and 0.15.1
wrote, is format 5 under the ref `refs/coduck/<number>`. This version writes and reads format 7, which adds the mark
needs input, an event of the conversation. The package ships a JSON Schema (draft 2020-12) for each format, under
`coduck/schema/<format>/` in the installed package and `src/coduck/schema/` in its source: `review.schema.json` for
`review.json`, `event.schema.json` for one line of a thread's log, `edit.schema.json` for the record of an edit,
from format 3 `note.schema.json` for a note, and from format 5 `spec.schema.json` for a run specification. The
schemas of formats 6 and 7 are strict: they name every field and allow no other, and allow `through` only beside the
role `reviewer`. Format 1's say what earlier versions could write.

A version reads one format. A review in an older one is refused wherever it is read, by every command and by the
server, with `review N of REPO is in format F; run coduck migrate`, and one in a newer format with `written by a
newer CoDuck`. The list shows such a repo as one line that says so, without its reviews, and `coduck register`
says so too. A review still under `refs/redline/` is in an older format and is refused the same way; while a repo
holds one, none of its reviews is listed and none is opened.

`coduck migrate` brings every review of every registered repo that is present to the current format, open and
finished alike, with one commit per step of the format on each review that needs it, named `migrate` and written
as `coduck`, on neither side. It prints per repo how many reviews it migrated, names a repo it cannot migrate
with the reason and goes on with the others, and exits 1 when it refused one. A migration commit is no news for a
waiting author and nothing new in the page. The step to format 2 records the reviewer's name in `reviewers`, as
the setting `coduck.webuser` (or git's `user.name`) gives it when the migration runs, so that every older write
whose author has that name counts as the reviewer's and every other as an author's; it is refused for a repo with
no such name. It also records, for a finished review that lacks it, when it finished, as the time of the commit
that finished it, and for a closed review without the span it covered, the span it has at the migration. The step
to format 3 turns the note that a done review was marked done with into a note, added under the name and on the
side of the commit that marked it done, at the time it finished, and takes the note out of `review.json`; it
changes nothing else. The steps to format 4 and to format 5 change nothing but the format.

The step to format 6 moves the review to the ref it is kept under now. It adds its commit, as every step does, and
then one git transaction, for all the reviews of the repo together, creates each `refs/coduck/<number>` at that
commit and deletes each `refs/redline/<number>`: every review is under one of the two names whatever happens in
between, nothing of its history is rewritten, and a repo that is refused keeps every review where it was. A number
that names a ref under both is refused, with both names; delete the ref that is not the review and migrate again.
Before it reads the list of repos, `coduck migrate` also takes over the state folder of those versions,
`~/.local/state/redline/`, and says so: its registered repos join the list, each file of drafts moves, and the folder
is removed. A repo whose name the new list holds for another path, and a file of drafts whose name the new folder
holds, give way, each named in a line that starts with `left behind`. Its key is not taken over, and neither is
what the browser kept for the page under the old name: paste the key that `coduck key` prints once more, and
every review shows as unread once. The settings are not taken over either: set `coduck.webuser` and
`coduck.cliuser` where `redline.webuser` and `redline.cliuser` were set.

The step to format 7 changes nothing but the format. The state of a review is read by the rule of this version
whatever format its past was written in: an open review whose approval only messages without the mark followed is
approved again after the migration, and ready when the ready signal came after that approval.

On an upgrade, stop the server, install the new version, run `coduck migrate`, and start the server again, so that
no server of the older version writes into a review of the new format.

## Where things are kept

- **In the repo**: each review is the ref `refs/coduck/<number>`. Its commits hold the description, one log of
  events per thread, the pending edits, the notes and the run specifications; each commit carries the name of
  whoever acted as its author, and records the branch head and merge base it covered. Refs outside `refs/heads` are not pushed or fetched unless
  asked for, so reviews stay out of ordinary pushes.
- **In the state repository**, `state.git` in the state folder: products, projects and issues, one Markdown file
  each on `main`; see [Products, projects and issues](#products-projects-and-issues).
- **On the server's disk**, under `~/.local/state/coduck/`, or `$XDG_STATE_HOME/coduck/` when that is set: the
  list of registered repos (`repos`), the reviewer's drafts (`drafts/`), the key that running code needs (`key`),
  and the folders of runs (`runs/`), each server's in a folder of its own with its lock beside it, which hold a
  run's folder while it goes.
- **In the browser**: the comment mode, what the browser last showed of each review, the key, once the page asked
  for it, and for each review the authors whose code you said may run. The outputs of a run that are yours alone
  live in the page's memory and are gone when you reload it.
- **In the server's memory**: the runs of code blocks that go, and the last twenty that ended, until it stops; a
  restart forgets them.

The server keeps no database, log or queue. The page and `coduck wait` read the review refs, and the server reads
the products, projects and issues from `main` of the state repository, so a restart or an upgrade loses nothing but
the runs: those that were going and what it kept of those that ended.

## The server

One server shows every registered repo, and the products, projects and issues of the state repository.
`coduck open` registers its repo; `coduck register` does so without opening a review. The server listens on 127.0.0.1 only, refuses a request whose `Host` header does not name
localhost or 127.0.0.1 with its own port, and refuses a write without the header `X-Coduck: 1`, so that a page
of another site in the same browser cannot reach it. For that reason a port forward from another machine must use
the same port number on both ends. Besides git, it starts programs only to run a code block that someone asked for
in the page, with the key that `coduck key` prints, and to say what a block can run on: it asks uv, with the
network off, which Pythons it has, and systemd once whether it can make a scope of the user's own; neither runs
anything of a review. CoDuck itself reaches the network only through uv, which fetches what a run needs while it
prepares; what a block's script does on the network, as a SQL block's `INSTALL` of an extension, is the script's
own. See [Running code blocks](#running-code-blocks).

[deploy/install.md](deploy/install.md) has the steps for running the server as a user service, upgrading it, and
setting the names.

## The HTTP API

The page reads and writes through a JSON API under `/api/`, behind the guard above: every request names this
server in its `Host` header, and every request but a GET carries `X-Coduck: 1`. A refused request is answered with
`{"error": REASON}` and 400 for a malformed request, 404 for what is not there, 409 for what CoDuck refuses and
413 for a body of more than 64 MiB. These are the routes of run specifications and runs. The four routes about
runs, to start, list, read and stop them, also carry `X-Coduck-Key`, the key that `coduck key` prints, and are
refused with 403 without it or with another, in words that say how to get it, also while runs are off; with the
key, while runs are off, they are refused with 409 and the reason. While the server has no key, as when it could
not make or read its key file, runs are off: a request about runs whose `X-Coduck-Key` is any 64 hex digits is
refused with 409 and that reason, and one without such a value with 403. The others need no key:

- `POST /api/reviews/REPO/N/specs` with `{"name", "python", "requirements"}` makes a run specification and answers
  `{"version", "spec"}`, the review's new version and the specification's id. `PUT /api/reviews/REPO/N/specs/ID`
  with the same three fields changes it, and `DELETE /api/reviews/REPO/N/specs/ID` removes it; both answer
  `{"version"}`, and a change to what the specification holds already writes nothing. The review's data
  (`GET /api/reviews/REPO/N`) has them as `specs`, in the order they were made, each with its `id`, the three
  fields, `author`, `role`, `at`, `seq` and, once changed, `changedBy`, `changedRole` and `changedAt`.
- `GET /api/reviews/REPO/N/runtimes` answers what the review's blocks can run on, found as
  [What a block runs on](#what-a-block-runs-on) says:
  - `off`: null while runs can go, else the reason they are off, such as a folder of runs that is a link or that
    other users can enter, or a key that others can read. Every request about runs is then refused with it.
  - `home`: the server's home folder, under which the page shows a path as `~/...`.
  - `worktree`: the folder of the worktree that has the branch checked out, or null.
  - `uv`: whether `uv` is on the server's path.
  - `sql`: `installed`, each with `version` and `path`; `releases`, the versions uv can fetch from 1.5.0 on; and
    `builds`, each with `build`, the folder's name under `build/`, and `path`.
  - `python`: `venv`, null or `path`, `python` (its version, or null when its `pyvenv.cfg` does not say it),
    `duckdb` (the version of the duckdb it holds, or null) and `editable` (whether that duckdb is an editable
    install, whose import may build); `pythons`, the Python versions uv has, each with `version` and
    `implementation`, empty without uv; and `specs`, as in the review's data.

  It starts no program but uv, which it asks with the network off which Pythons it has.
- `POST /api/reviews/REPO/N/runs` asks for a run with `{"kind", "file", "parts", "on"}`, and answers at once with
  `{"run": ID}`:
  - `kind`: `sql` or `python`.
  - `file`: the file name of the blocks, or null for a block alone.
  - `parts`: the code of each part, in order: 1 to 100 texts without NUL, of at most 1,048,576 characters
    together.
  - `on`: what to run on, for SQL `{"source": "installed", "version": "2.0.1"}`,
    `{"source": "release", "version": "1.5.3"}` or `{"source": "build", "build": "release"}`, for Python
    `{"source": "venv"}` or `{"source": "spec", "spec": ID}`.

  It refuses with 404 a program, build, `.venv` or specification that is not there, and with 409 a release or a
  specification while `uv` is not on the path, a limit that its setting does not give, a third run, a run while the
  server stops, and every run while runs are off.
- `GET /api/reviews/REPO/N/runs` answers the runs that go for the review, so that a page that did not start one,
  as after a reload or in another tab, can show it and stop it: `runs`, the oldest first, each with `id`, `kind`,
  `file`, `on` and `phase`; `going`, how many runs go on the server, for every review; `most`, how many it takes at
  once; and `limits`, as a run's view has them, or null while their settings are wrong.
- `GET /api/runs/ID` answers the run as it is now:
  - `id`, `review` (`{"repo", "number"}`), `kind`, `file` and `on`: the run as it was asked for.
  - `phase`: `preparing`, `running` or `done`. While it prepares, `preparing` says `fetching` (through uv) or
    `building` (the `.venv`'s first import), else it is null; `prepareText` holds the last 64 KiB of what
    preparing printed, also once the run is done.
  - `stopping`: whether a stop was asked and the run has not ended; `seconds`: how long it has run, or ran, from
    the start of its script, null before.
  - `outcome`, once done: `finished`, when the script ended by itself, with any exit status; `stopped`, by Cancel or
    a limit, also while it prepared; `failed`, when it could not prepare, start or be watched, as when something
    wrote to the driver's own pipe, with the reason in `error`. A finished run whose measuring failed says so in
    `error` too.
  - `limits`: `seconds`, `memory` as its setting gives it, and `scope`, whether a scope holds them.
  - `configuration`: what the run measured, as the header fields below.
  - `parts`, one for each part, in order: `text`, what it printed so far; `dropped`, how many bytes the output limit
    dropped; `done`; `ran`, whether it started; `joined`, the number of the part whose text holds its output when
    the two could not be told apart, or null; and `header` once the run is done, else null.
- `DELETE /api/runs/ID` stops a run: its whole process group and its scope are asked to end, and are killed when
  the group has not ended five seconds later, thirty while the `.venv`'s import builds; the question after the
  script, of what the run ran on, then has five seconds. It answers as `GET` does, at once, and changes
  nothing of a run that ended. A run the server no longer keeps, past the last twenty that ended, answers 404.

A part's `header` holds the header fields of its output block, each only where it applies, for the page to write
the block from: `duckdb`, the version and source id, as `2.0.1 (8d78114e90)`; `python`, the interpreter's version;
`packages`, as `pandas 2.2.3, numpy 2.1.0`; `extensions`, as `httpfs v1.5.6, spatial`; `exit`, the exit status
when it is not 0, or `killed by SIGKILL` when a signal ended the program; and `stopped`, as `time limit 60 s`,
`memory limit 4 GB`, `disk limit 4 GB`, `cancelled at 8 s`, or why the part did not run: `not run, part 2 raised
ValueError`, `not run, part 2 does not compile`, `not run, the program ended in part 2`, `not run, the time limit
stopped part 2`, `not run, the memory limit stopped part 2`, `not run, the disk limit stopped part 2`, `not run,
cancelled in part 2` or `not run, cancelled while preparing`. A limit or a Cancel that came after the last part
ended says so in the last part's `stopped`. A program's exit status goes on the part during which it ended,
the last part when it ended after them all. Once the run is done, the `text` of a part that dropped output ends
with a line that says how many bytes, and the `text` of a part whose output could not be told apart is a line that
names the part whose text holds it; both lines are in square brackets.

The routes of products, projects and issues read and write the state repository with the checks of the command, in
its words, without what the command adds about its own options and verbs: where the command refuses with
`FILE opens with a header; give the fields with the options, and the title and text in FILE`, the server answers
`the text opens with a header`. A name in a path or in a body is a name in full; a short form, as an id alone, names
nothing. Every
answer holds `version`, the commit `main` points at after the request, null before the first change, and every
write is made as `coduck.webuser`, or else `user.name`, as git's configuration in the state repository gives it, on
the reviewer's side. A body with a key the route does not take or a value of another type, and a value the command
refuses, as a title, a label, a state, an origin, a pull request, a text, or an issue with no product, answer 400;
a name that leads nowhere 404; and a change of a thing that cannot be read, an item that is not in its list, a
list that holds an item a header cannot hold, a board that nests deeper than the server writes JSON, a state
repository CoDuck cannot read as a whole, also for `/api/state/version`, and a state repository whose configuration
names no web user, 409.

- `GET /api/state/version` answers `{"version"}`, cheap enough to ask again and again.
- `GET /api/state` answers `{"version", "path", "format", "problems"}`: the path of the state repository, its format,
  1, and how many problems it holds.
- `GET /api/problems` answers `{"version", "problems"}`, each problem `{"path", "line", "words", "group", "kind",
  "name"}` in the order of `coduck problems`: `line` is null where there is none, `group` is the kind of problem, as
  under [The commands of the state repository](#the-commands-of-the-state-repository), and `kind` and `name` are
  those of the thing it belongs to, null for a problem of the repository. The views list a thing's problems in the
  same form.
- `GET /api/products`, `/api/projects` and `/api/issues` answer `{"version", "products"}`, `projects` or `issues`:
  the rows of every thing of the kind, open, closed and unreadable, in the order of the command's list.
- `GET /api/products/NAME`, `/api/projects/NAME` and `/api/issues/NAME` answer `{"version", "product"}`, `project`
  or `issue`: the view of one thing.
- `POST /api/products` with `{"title", "text", "origin", "labels"}`, `POST /api/projects` with
  `{"product", "title", "text", "labels"}`, and `POST /api/issues` with
  `{"title", "text", "product", "state", "labels", "projects", "parents"}` make a thing and answer `{"version",
  "name"}`, for an issue with its `state`. The title, and a project's product, are needed; the others may be left
  out. An issue takes the states and the product as `coduck issues new` does.
- `PUT /api/KIND/NAME/FIELD` with `{"value"}` sets the `title` or the `text`, the `origin` of a product, null taking
  it away, and the `state` or the `product` of a project or an issue, and answers `{"version", "value"}`.
- `POST /api/KIND/NAME/FIELD` changes a list, the `labels` of any kind, the `issues` of a project and the
  `children`, `blocked-by` and `pull-requests` of an issue, with `{"add": [ITEM, ...]}`, `{"remove": [ITEM, ...]}`
  or `{"move": ITEM}`; `"before": ITEM` places what `add` or `move` gives before an item of `issues` or `children`,
  and `move` is for those two alone. It answers `{"version", "items"}`, the whole list.

A change to what a thing holds already writes nothing and answers the version that is there. The rows and views:

- **An issue's row** holds `name`, `path`, `title`, `product` (`{"name", "title"}`, or null where it names none),
  `state`, `open`, `blocked`, `labels`, `childCount`, `partOf` (`{"projects", "issues"}`, the names of the holders
  that name it), `created` and `changed` (each `{"at", "by"}`), `unreadable` and `problems`, a count. Of an issue
  that cannot be read, `state` and `product` are null and its lists empty.
- **Its view** adds `fields` (each `{"key", "line"}` with `value` or `items`, every field as written), `text`, `raw`
  (the file), `board` (the rows of its children), `blockedBy` (each `{"name", "title", "state", "open", "missing",
  "unreadable"}`), `blocks` (names), `pullRequests`, `reviews` (each `{"name", "here"}`, `here` false), `partOf`
  with `{"name", "title", "state"}` for each holder, `problems` (the list) and `offered`, the labels its product
  offers.
- **A project's row** holds `name`, `path`, `title`, `product`, `state`, `labels`, `counts` (how many distinct
  issues of its board are in each of the seven states, `unreadable` and `missing`), `issueCount`, `created`,
  `changed`, `active` (the newest change of the project or of an issue on its board), `unreadable` and `problems`.
  **Its view** adds `fields`, `text` (the brief), `raw`, `board` (null for one that cannot be read), `reviews`,
  `problems` and `offered`.
- **A row of a board** holds `name`, `title`, `state`, `open`, `blocked`, `labels`, `product`, `missing`,
  `unreadable`, `loop`, `repeat` and `children`, its own rows; one that is missing, unreadable, a loop or a repeat
  has none.
- **A product's row** holds `name`, `path`, `title`, `origin`, `labels`, `issues` and `projects` (each
  `{"open", "closed"}`), `created`, `changed`, `unreadable` and `problems`. **Its view** adds `fields`, `text`,
  `raw`, `labelsInUse` (each `{"label", "issues", "projects", "offered"}`) and `problems`.

## Backup and archive

- `coduck bundle FILE` writes all review refs of the current repo to a git bundle. A fetch from the bundle into a
  repo with no review refs brings every review back, and `coduck register` then lists the repo again; reviews from
  a bundle made before their format changed need `coduck migrate` after that. A bundle made before format 6 holds
  its reviews under `refs/redline/`: fetch them to the same names, and the migration moves them.
- `coduck push REPOSITORY` pushes every review of the current repo to an archive repository, named by URL or
  path; the name of a remote is refused. A review goes to `refs/coduck/<repo>/<opened>-<number>-<title>`: the
  time it was opened, in UTC as `20261124T092712Z`, its number, and the words of its first title in lowercase
  joined by hyphens, at most 60 characters. The name never changes. The push only fast forwards and never deletes,
  and it sends the reviews alone, never the commits of the reviewed branches.

Both copy review refs as they are, whatever their format and also where a review is still under `refs/redline/`,
so that a backup goes on before a migration; a bundle or
the archive can hold reviews in an older format, which `coduck migrate` brings to the current one once they are
back in a registered repo. A migrated review is a descendant of the one archived before, so its next push is a fast
forward.

Neither runs by itself: a scheduled script calls them. [deploy/install.md](deploy/install.md) describes
`deploy/backup.sh`, a script that does both for every registered repo and that a timer can run, the archive, and
how to restore. The script also writes a git bundle of `main` of the state repository, `state/state.bundle` in the
folder of the bundles, when there is one, since the state repository has no remote of its own.

Never configure a fetch that writes into `refs/coduck/` in a clone that opens reviews: with pruning on, such a
fetch deletes every review the remote lacks.

## What CoDuck does not do

- It does not merge, push or fetch branches, and it never writes to a branch or a worktree, except for
  `coduck pull`, which writes the worktree it runs in, and for `main` of its own state repository.
- It has one reviewer and no accounts: whoever reaches the page writes as `coduck.webuser`.
- It reviews branches of repos on the machine the server runs on.
- It runs a code block only when someone asks for it in the page, never for a command and never on reading; it
  installs nothing for a run and keeps no environment of its own.

## Licence

CoDuck is released under the MIT licence; see `LICENSE`. The page ships three libraries, unchanged, under
`src/coduck/page/vendor/`, each with its licence beside it: Highlight.js with its GitHub theme (BSD 3-Clause),
markdown-it (MIT) and Mermaid (MIT). The Mermaid file holds the code of the projects it is built from, and lists
their licences at its end.
