Skip to main content

onetaskgraph_github_projects/
lib.rs

1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
149//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
150//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `reconcile_status` | **Supported,** and proven by `onetaskgraph-github-projects-e2e`'s `status_reconcile.rs`: the board's own `ProjectV2.items(query:)` filter, `is:closed`, read page by page and never the unfiltered items, each candidate confirmed from its own item node — a closed task whose card is not at the option the mapping gives its closure's category. |
161//! | `complete_by_issue` | **Supported,** and proven by `status_complete.rs` there and in the engine's suite: each issue a reference names read in one aliased request per fifty — its repository, its number, its first page of board memberships with each card's `Status` read by name — and nothing else of the board. |
162//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
163//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
164//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
165//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
166//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
167//!
168//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
169//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
170//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
171//! and the raw bytes as the request body. The numeric repository id is read at most once
172//! per repository per source instance. A loopback API endpoint also selects the loopback
173//! uploads host; production needs no assets repository, commits or extra configuration.
174//! Every returned JSON `url` is read with the same token before a body is written: only
175//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
176//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
177//! asset and HTTP status and says the token type may not be accepted by the upload
178//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
179//! fine-grained PATs or OAuth tokens.
180//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
181//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
182//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
183//! and verified afresh, in the repository the issue already lives in on an update.
184//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
185//! Uploads take the same mutation spacing as content writes. Both uploads and verification
186//! reads wait out classified rate limits within the configured per-call retry budget,
187//! on the supplied clock; verifying reads take no mutation slot.
188//!
189//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
190//! source has documents and that its tasks have comments, both of which hold — and the three
191//! facts behind the uniform `Native` on the
192//! predicates beside it are recorded below rather than re-derived, because a reader who
193//! takes `Native` to mean *the remote service filters* will read that uniformity as a
194//! lie.
195//!
196//! First, the plugin contract defines `Support::Native` as *the source applies this
197//! predicate itself*, and says nothing about where it applies it. What the declaration
198//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
199//! — so that the engine may push it down and apply nothing of its own.
200//!
201//! Second, this source can keep that promise for every predicate at no additional API
202//! cost, because whichever of the reads below answers a query has already read every
203//! candidate that query will return before it filters anything. Filtering those items is
204//! in-process work over data already in hand.
205//!
206//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
207//! applied in process over what that question returned. A project filter has a relationship — a
208//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
209//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
210//! a search for metadata values, is the board-scoped issue search carrying the text and each
211//! value as quoted phrases; an origin is the board's own field filter over its origin field
212//! beside the same search for the id. **The text search narrows, and that is this source's
213//! declared semantics:** GitHub matches whole words where the substring rule this source and
214//! the local Markdown source confirm with would match inside one, so an item holding the text
215//! only inside a longer word is never a candidate. Every item returned does contain the text.
216//! A project query's text, and a document query's scoped to no project, is that same search
217//! and narrows on the same terms, its candidates confirmed by their kind as well.
218//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
219//! so those three are applied in process over the candidates, and a query carrying none of
220//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
221//! engine compensate for work this source has already done, and declaring `projects` native
222//! while ignoring the filter (which this source once did) silently returns another project's
223//! tasks, because the engine trusts the declaration and applies nothing locally.
224//!
225//! # The three ways this source reaches an item, and what each costs
226//!
227//! A board read is charged for what its *nested* connections could return rather than for
228//! what was asked, so one whole-board read costs the same whether the question was about
229//! one project or about all of them. That is why a question about one project is never
230//! answered by reading the board:
231//!
232//! | The question | What is sent | What it costs |
233//! | --- | --- | --- |
234//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
235//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
236//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
237//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
238//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
239//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
240//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
241//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
242//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
243//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
244//! | every task, every document, every label, when nothing above narrows the question | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
245//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
246//!
247//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
248//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
249//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
250//! equal to declared points equal to the row. They include the origin lookup and the
251//! field/repository discovery a create needs. A bound re-copy changes status, priority,
252//! content and metadata; comment recount means a subsequent detail read. Each request here
253//! costs one declared point. A membership beyond the embedded page can additionally require
254//! the one-point membership recovery described above. A bound re-copy of a task filed under a
255//! project adds one read, the engine confirming that project's link by its own id once per
256//! command; and the same-source far ends a write newly names — those that do not already block
257//! the item, whose own read answered for them — are read together by their own ids,
258//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
259//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
260//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
261//!
262//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
263//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
264//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
265//! holds both halves.
266//!
267//! **An existing item is written body last.** A bound re-copy and a `task update` send its
268//! board fields first — the `Status` option and the `Priority` together, in one request — then
269//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
270//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
271//! undoing an earlier field when a later one fails, so that order is what makes a write
272//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
273//! the one piece of metadata written before the body, an origin a copy re-points, is put back
274//! when a later write is refused — and when putting it back is refused too, the write's own
275//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
276//! of those writes in turn, whole and as one aliased field failing after the one before it.
277//!
278//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
279//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
280//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
281//!
282//! - **A board is accepted at creation but its item is not answered, so a create still files
283//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
284//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
285//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
286//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
287//!   against a real board on 2026-10-01 with a create sending the board there and reading the
288//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
289//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
290//!   refused "Content already exists in this project" — GitHub had filed the issue after
291//!   answering, and refuses a second filing rather than answering with the item it holds. A
292//!   create therefore sends no `projectV2Ids` and files the issue with
293//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
294//!   real is the read before it: the board's fields and the repository's id together, in
295//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
296//! - **A comment still reads its target first, so a comment is 2 requests.**
297//!   `AddCommentInput.subjectId: ID!` is declared there with
298//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
299//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
300//!   project's issue, a document's issue, an issue on no board of this source and a pull
301//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
302//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
303//!   refuses those by name.
304//!
305//! | Verb | Requests / points | Documents |
306//! | --- | --- | --- |
307//! | new copy | 4 | ORIGIN_LOOKUPS (the origin, the board's fields and the repository's node and numeric ids together, a page of one carrier for each origin), CREATE_ISSUE filing the issue on the board and under its project, CREATED_BOARD_ITEMS once GitHub has added it, WRITE_FIELD_VALUES |
308//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
309//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS when a board field differs, then UPDATE_ISSUE last carrying only the title, body or state that differs — none, when nothing does |
310//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
311//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
312//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
313//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
314//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
315//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
316//! | recount | 1 | ISSUE_DETAIL |
317//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
318//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
319//! | content | 2 | ISSUE, UPDATE_ISSUE |
320//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
321//! | document metadata, `--expect` a version | 2 | ISSUE, then UPDATE_ISSUE only when the issue is still at that version — the precondition GitHub's own update cannot take — primed with what the read answered |
322//! | update | 3 | `task update` naming any of title, body, metadata, status and priority — all five included: ISSUE, UPDATE_FIELDS (the status option and the priority together), UPDATE_ISSUE (title, body with its metadata slot, and state) last |
323//! | record only | 1 | ISSUE |
324//!
325//! <!-- github-search-paging:start -->
326//! Board-scoped text, metadata, project-name and comment-activity searches send every
327//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
328//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
329//! needs rows. A page is never resized to the rows still needed: GitHub orders one
330//! search differently at different page sizes, so one fixed size makes a paged walk
331//! send exactly the requests one whole read sends, and the answer's order is the order
332//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
333//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
334//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
335//! returned and fetched pages: a limit is sliced from the pages it needs, and local
336//! confirmation can require more candidates than matching rows. Walking all pages
337//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
338//! cursor and how far into that page the last answer stopped, and resumes in the same
339//! process or a new one, without duplicates or gaps. It carries no rows: one process
340//! sends each page's search once, and a new process re-reads only the page it resumes
341//! in, then sends a further page once, never as a re-read, only when its limit still
342//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
343//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
344//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
345//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
346//! not required to include the original process's writes still omitted by the index.
347//! <!-- github-search-paging:end -->
348//!
349//! The board half of an issue — its board item's id, its `Status` option and this
350//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
351//! an item reached any of those ways resolves through the same
352//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
353//! same status, the same labels and the same qualified id. That connection comes back a
354//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
355//! on the page in hand and — only if that page reports more of the connection — in the
356//! last row's read of that one issue's memberships, resumed from the page's own cursor and
357//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
358//! report, which is what keeps an id naming another repository's issue from being answered
359//! as an item of this board; and because the page is where the search starts rather than
360//! where it ends, that answer is one about a connection read to exhaustion and never about
361//! an unread page. Nothing costs the extra read but an issue on more boards than a page
362//! holds: an issue this board really does not hold reports no next page, so its
363//! memberships are already exhausted where they arrived.
364//!
365//! **No document here selects the board's own `Labels` field, and nothing is lost by
366//! that.** An item's labels are read from its content alone, wherever that content is
367//! reached: the three documents above select `Issue.labels` on the fragment, and
368//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
369//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
370//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
371//! create one, and `ProjectV2FieldValue` — the whole of what
372//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
373//! it from the content, for every content type it exists on, and there is nothing it can
374//! hold that the content does not already say: for an `Issue` it *is* that issue's own
375//! labels, so selecting it beside them unions a set with itself.
376//!
377//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
378//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
379//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
380//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
381//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
382//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
383//! label set, and that set is the fixture issue's own, by
384//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
385//! item whose content is a draft reports an empty set, by
386//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
387//! selection is held over [`graphql::DOCUMENTS`] by
388//! `no_document_selects_the_boards_own_labels_field`.
389//!
390//! The whole-board row is still the board's own item connection, and deliberately: a
391//! **draft** board item is not an issue, so no search can list one, and the reads that have
392//! to answer for the whole board are the ones whose cost is the board's size anyway.
393//!
394//! **A question about one item this source already names by id never lists the board.**
395//! Whether that item is on this board, and what its board fields are, is answered by reading
396//! that item — its own `Issue.projectItems`, walked to exhaustion by
397//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
398//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
399//! holds. That covers a write's destination, the project a new item is filed under, a
400//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
401//! and the delete that takes back an item a copy made. What such a write needs of the board
402//! and the item does not carry — the board's id, the `Status` and origin field definitions —
403//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
404//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
405//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
406//! minutes. Scanning this host's 842-item board has refused a document copy and an update
407//! even though the items' own reads named that board. A scan there gives the wrong answer
408//! as well as paying for every page. So a `board.items` lookup does not belong on any of
409//! those paths.
410//!
411//! **What a read may return is capped too, and that cap is on the document rather than on
412//! the board.** GitHub limits the number of nodes **one query may return** to
413//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
414//! an error naming the connection the count crossed at, not a slow or a partial result.
415//! Every board this source reads is refused the same way, so no board is too big for these
416//! documents and none is small enough to save one that is over.
417//!
418//! The count is arithmetic over the document's own text: each connection contributes the
419//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
420//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
421//! restate them — `github-graphql-node-count` implements them, and
422//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
423//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
424//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
425//! same text on every run and fails naming any that reaches the limit — so a connection
426//! added to a shared fragment is caught there rather than by GitHub.
427//!
428//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
429//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
430//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
431//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
432//! effectively squared there, which is why it is the one the limit is most sensitive to.
433//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
434//! page of memberships misses is recovered by one further read rather than refused, so it
435//! buys a bound every read pays for at the price of a request only a multi-board issue
436//! pays.
437//!
438//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
439//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
440//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
441//! `cost` is rate-limit points, metered per hour across everything one credential does; it
442//! is what the two limiters [`Limiter`] tells apart meter, and a document under
443//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
444//! that second number, and `tests/point_cost.rs` pins every document in
445//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
446//! one under, the pin itself is the check. The credentialed lane reconciles both figures
447//! against GitHub's own, off a probe it already sends.
448//!
449//! **What is pinned that way is a per-document price and never a session's.** The record in
450//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
451//! **requests** and **worst-case nodes** — and neither is points. What one whole session
452//! consumes of the hourly point allowance is observable only from a credentialed run's own
453//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
454//! and what `tests/live.rs` prints at the end of every run.
455//!
456//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
457//!
458//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
459//! enumerations of a board can supply one alone.** Resolving a node id is strongly
460//! consistent, so a read by id and a project's own sub-issues are already current. The
461//! other two are not, and they are behind by different amounts and in different directions:
462//!
463//! - GitHub's **issue search** is an index and answers a write made moments ago with the
464//!   value from before it — usually for a second or two.
465//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
466//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
467//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
468//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
469//!
470//! That second one is a measurement rather than a caution. This repository's own
471//! credentialed journey writes a project and waits for the board to report it, then writes a
472//! task and waits for the same thing seconds later on the same board: the project wait is
473//! answered through the search and converged in two or three attempts in each of three runs,
474//! and the task wait is answered through `ProjectV2.items` and converged in none of them
475//! inside thirty. Separately, an item added to a second and larger board was read back by
476//! `Issue.projectItems` on that board's own id while every one of that connection's nine
477//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
478//! through the lagging one alone is what had a board read deny an issue that had certainly
479//! landed on it.
480//!
481//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
482//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
483//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
484//! search reports what the projection is behind on. What closes the last
485//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
486//! source answers is completed with what this process itself wrote, so an item created
487//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
488//! nothing is written down, and the record dies with the process. **A wait that has to
489//! observe GitHub's own data cannot be answered from that record** — which is why the
490//! credentialed journey asks through a source built afresh, and why the union above rather
491//! than a longer wait is what makes such a wait converge.
492//!
493//! **A narrowed read is the same bargain, stated for each of the three predicates it
494//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
495//! than walking the board, and every such answer is completed with what this process wrote —
496//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
497//! each filtered by the same predicates as the rest — so an item this command wrote a moment
498//! ago is returned by a query that matches it whether or not the index has caught up. An item
499//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
500//! consistent. What is left is stated rather than papered over:
501//!
502//! | Read | Finds | Behind by |
503//! | --- | --- | --- |
504//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
505//! | origin, first read | the board's field filter over the origin field — every carrier, whichever release wrote it | what `ProjectV2.items` is behind on, which the measurements above put in minutes |
506//! | origin, second read | the issue search for the id in the body, where a write of this release mirrors it | a second or two, as any search |
507//! | origin, third read | this process's own writes | nothing |
508//!
509//! So an origin carrier another process added within the last second or two, before either
510//! index has it, can be missing from an origin query, and one written by the release before
511//! this one — its origin in the field alone — can be missing for as long as the board's own
512//! item connection is behind on it. A copy that must not duplicate its own earlier write
513//! relies on the link it records, not on either index. **A board draft is not an issue**, so
514//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
515//! lists one, the origin lookup drops any the board's own field filter names, and one this
516//! process wrote is not added back either.
517//!
518//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
519//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
520//! body's metadata slot, so the issue search can find it in seconds. The field is
521//! authoritative: this source reads an item's origin from the field alone, so a slot that
522//! disagrees with it, or holds one where the field holds none, is never read as a second
523//! origin — and the release before this one reads the slot, drops that key's copy for the
524//! field's, and sees the same one origin.
525//!
526//! Filtering happens before paging, so a page of a filtered result is a page of the
527//! survivors rather than the survivors of a page. Label matching and the substring rule a
528//! text candidate is confirmed by answer the same question the same way the local Markdown
529//! source's do; which candidates a text search has to confirm is GitHub's word match, which
530//! is the one place the two sources can answer the same text differently.
531//!
532//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
533//! has one source, `capabilities`, and the note above is the reasoning behind it rather
534//! than a second copy of it: without the three facts recorded here a reader takes the
535//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
536//! crate's own capabilities test, which pins every field of it against a fully spelled-out
537//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
538//! fails to compile there rather than going unasserted. -->
539//! The fixture-server tests above run wherever this crate is selected; the credentialed
540//! lane runs in the same required check, beside them, and can fail it — it verifies the
541//! current schema, then drives every field of the table above against the real board. It builds its own fixture there — two projects, one task filed under each,
542//! one filed under neither, a label on one of the three and a closed status on another —
543//! because that shape is what tells an honoured predicate from an ignored one: a board
544//! holding a single project answers a project filter the same way whether or not this
545//! source applies it, which is exactly how the defect above went unseen.
546//!
547//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
548//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
549//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
550//! session or request, independently of the live demand. Fix that variable in the machine's
551//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
552//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
553//! policy crate; absent credentials or nominations otherwise skip.
554//!
555//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
556//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
557//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
558//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
559//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
560//! after its run reads back as `completed`, immediately before the listed-artifact batch.
561//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
562//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
563//! and the board's `onetaskgraph.origin` field untouched.
564//!
565//! # What a session of requests costs, and where the report is
566//!
567//! This source records **every** request it sends into [`accounting::Accounting`], at
568//! `send_once` — the one place a request leaves this crate, which is why a read path added
569//! later is counted without anybody remembering to count it. That is the whole of what this
570//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
571//! session's spend is arrived at, and what it deliberately does not know are set out.
572//!
573//! What one whole session of the live journey costs, counted that way against this crate's
574//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
575//! reduction it came out of, and with what it does and does not say about rate-limit points.
576//!
577//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
578//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
579//! code path — no environment variable, no feature, no build configuration — because an
580//! instrument nobody switches on measures nothing, and
581//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
582//! source's counts the whole session rather than this source's share. The credentialed lane
583//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
584//! passed or failed.
585//!
586//! **A live session refuses to start unless the account can afford it.** Before it does any
587//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
588//! GitHub documents as not counting against the REST rate limit and which answers both of
589//! its budgets at once — and starts only if, for each of them, what remains minus this
590//! session's estimated cost is still at least
591//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
592//! allowance. A session that cannot **declines**: it did not run, so it is
593//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
594//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
595//! waiting for the budget to come back. The estimate is derived offline from
596//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
597//! which is also where the published rule that model rests on is cited; the accounting
598//! above records the gate's own read like any other request, and
599//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
600//!
601//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
602//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
603//! text, which is what lets it run on every platform and on a pull request from a fork with
604//! no credential — and that is what actually stops a regression merging. But an offline
605//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
606//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
607//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
608//! number of nodes this query may return"* and whose `cost` is what that document would
609//! spend, both for a document **without executing it**, and the lane asks it for every query
610//! document this source sends, under the largest bindings this source sends, and fails when
611//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
612//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
613//! one; the offline pins still cover it. It records what those calls reported about the
614//! account's own allowance, because whether asking is free is a thing to observe rather than
615//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
616//! and `cost` is metered against an hourly allowance the accounting above reads off a
617//! credentialed run's own response headers.
618//!
619//! **GitHub has two rate limiters and this source is refused by both, so nothing here
620//! treats them as one thing.** The primary budget is the hourly allowance `gh api
621//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
622//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
623//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
624//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
625//! one part of.
626#![deny(missing_docs)]
627
628use std::collections::BTreeMap;
629use std::sync::{Arc, Mutex};
630use std::time::Duration;
631
632use chrono::{DateTime, Utc};
633use onetaskgraph_plugin_api::{
634    Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
635    ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
636    DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
637    Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
638    MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
639    ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
640    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
641    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
642    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
643    WaitCause, WriteJournal, WriteSupport, system_clock,
644};
645use reqwest::{Client, StatusCode, Url};
646use schemars::{Schema, schema_for};
647use secrecy::{ExposeSecret, SecretString};
648use serde::{Deserialize, Serialize};
649use serde_json::{Value, json};
650
651pub mod accounting;
652mod assets;
653pub mod change_token;
654mod expected;
655pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
656mod pacing;
657mod visibility;
658
659use accounting::Accounting;
660
661/// The registry name for this plugin.
662pub const KIND: &str = "github-projects";
663/// GitHub's maximum connection page size.
664pub const MAX_PAGE_SIZE: u32 = 100;
665/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
666/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
667/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
668pub const SEARCH_PAGE_SIZE: u32 = 20;
669/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
670/// its comments: the largest batch the node-count model prices at one point.
671///
672/// Each aliased item is resolved once, and what GitHub charges for it is the connections
673/// under it — its labels, its page of board memberships, the field values of each of those
674/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
675/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
676/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
677/// one point and fails if one item more would still be priced at one.
678pub const DETAIL_BATCH: usize = 24;
679/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
680///
681/// Each reference asks GitHub for one connection — a page of its board memberships, each with
682/// its `Status` value read by name rather than through a connection of field values — so fifty
683/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
684/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
685/// issues per source. `tests/point_cost.rs` prices the document at one point.
686pub const REFERENCE_BATCH: usize = 50;
687/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
688/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
689/// point.
690///
691/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
692/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
693/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
694/// if one item more would still be priced at one.
695pub const COPY_DETAIL_BATCH: usize = 21;
696/// How many mutations one aliased request of a copy's creates, closes and updates carries:
697/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
698///
699/// Eleven is what GitHub was seen to accept in one request — the timing spike
700/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
701/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
702/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
703pub const MUTATION_BATCH: usize = 11;
704/// How many aliased writes one request of a copy's board field values and blocked-by edges
705/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
706/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
707///
708/// Nineteen is the most aliases the feasibility spike
709/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
710/// saw GitHub price at the one point every mutation request costs; no larger request was
711/// established, so a copy holding more is split.
712pub const WRITE_BATCH: usize = 19;
713/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
714/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
715/// of five succeeded (the feasibility spike's undo-batching run).
716pub const DELETE_BATCH: usize = 5;
717/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
718/// plan and the project's document, and no more, since each carries a page of the board's field
719/// filter and one of the issue search.
720pub const ORIGIN_BATCH: usize = 10;
721
722/// The most nodes any one document this source sends may be asked to return.
723///
724/// GitHub's own published per-query ceiling, taken from
725/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
726/// workspace cannot hold a stale copy of somebody else's number. A query above it is
727/// **refused before it is executed**, whoever is asking and whatever board they are
728/// asking about — so this is a bound on the documents rather than a budget that runs out.
729///
730/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
731/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
732/// everything the credential does — two numbers against two limits, and this constant
733/// bounds only the first. The second is computed offline too, per document:
734/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
735/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
736/// lane. There is no constant like this one to hold a price under, because points are an
737/// hourly allowance rather than a per-call bound.
738///
739/// Neither is a session's price. What `session-cost.md` records of a whole session is its
740/// **requests** and its **worst-case nodes**; what a whole session spends in points is
741/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
742/// [`accounting`]. The module section on the three ways this source reaches an item says how
743/// the count is arrived at, and which of the page sizes below decide it.
744pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
745
746/// Nested connection size for the connections that hang off one item.
747///
748/// It multiplies through every document that reaches an item under a page — the count
749/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
750/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
751/// every document under these constants and fails naming any that reaches the limit, so
752/// raising this is caught there rather than by GitHub.
753const NESTED_PAGE_SIZE: u32 = 50;
754/// How many of one issue's board memberships are read when an issue is reached directly.
755///
756/// An issue reached through a search or through its own node id carries its board half in
757/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
758/// under a page of issues, so every point of it multiplies through the whole document and
759/// is paid for whether or not any issue is on a second board — which is why it is
760/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
761///
762/// **Three, because what a page misses is now recovered rather than refused**, and the
763/// recovery is what the value is chosen against. An issue whose entry for this board sits
764/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
765/// that page's own cursor — so the value trades a bound every read pays for a request only
766/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
767/// boards would pay that request *per issue*, which is order N against the one page per
768/// hundred issues a read costs today. At three it is only reached by an issue on four or
769/// more boards at once, which keeps the recovery path exceptional rather than routine for
770/// a plausible deployment.
771const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
772/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
773/// its two connections for.
774///
775/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
776/// second is a duplicate a copy already takes the first of. Both connections are walked to
777/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
778/// never what the answer is. It is small because every point of it is paid on every lookup,
779/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
780/// worst-case nodes than the one whole-board read they replaced.
781const ORIGIN_PAGE_SIZE: u32 = 3;
782/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
783/// connections for: one, the one carrier an origin names.
784///
785/// A second carrier still shows: the page then says there is more, and that origin is left to
786/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
787/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
788/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
789/// origins at two points with a first page of three and at one with a first page of one.
790const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
791
792/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
793/// one point buys, since every row carries six connections GitHub charges for once per row.
794/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
795/// fails if one row more would still be priced at one.
796pub const MEMBER_PAGE_SIZE: u32 = 23;
797
798pub use github_graphql_node_count::{NodeCountError, Variables};
799
800/// The largest value this source can bind to each page-size variable its documents name.
801///
802/// Every `first:` in [`graphql`] reads one of these six, and each is capped at the
803/// constant above it wherever a caller's own limit could reach it — `$first` at
804/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
805/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
806/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
807/// not one configuration of it, which is what makes a bound computed under it a bound on
808/// every read.
809pub fn largest_page_sizes() -> Variables {
810    Variables::from([
811        ("first".to_owned(), MAX_PAGE_SIZE),
812        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
813        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
814        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
815        ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
816        ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
817    ])
818}
819
820/// The most nodes `document` could be asked to return, by GitHub's published rules.
821///
822/// Computed offline from the document's own text under [`largest_page_sizes`] — no
823/// network, no credential and no schema — by
824/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
825/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
826/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
827///
828/// # Errors
829///
830/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
831/// no single operation, or binds a page size this source does not name — each of which is
832/// a defect in the document rather than a number.
833pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
834    node_count(document, &largest_page_sizes())
835}
836
837/// The most rate-limit points one call of `document` could spend, by GitHub's published
838/// rules.
839///
840/// Computed offline from the document's own text under [`largest_page_sizes`] — no
841/// network, no credential and no schema — by
842/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
843/// This is `cost`, metered **per hour** against the allowance one credential shares across
844/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
845/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
846/// under, so what `tests/point_cost.rs` does with it is pin every document in
847/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
848/// figures against GitHub's own reported `cost`.
849///
850/// # Errors
851///
852/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
853/// no single operation, or binds a page size this source does not name — each of which is
854/// a defect in the document rather than a number.
855pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
856    github_graphql_node_count::point_cost(document, &largest_page_sizes())
857}
858
859/// The most nodes `document` could be asked to return under `variables`.
860///
861/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
862/// [`accounting`] is this under the bindings one request really sent — one spelling of the
863/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
864/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
865///
866/// # Errors
867///
868/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
869/// no single operation, or binds a page size `variables` does not name.
870pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
871    github_graphql_node_count::node_count(document, variables)
872}
873
874/// The issue-title prefix that makes a board issue a document.
875///
876/// A GitHub Projects board has no document type — it holds issues — so the discriminator
877/// is the title, and this is the whole of it: an issue whose title begins with these bytes
878/// is a document and every other issue is the task or project the sub-issue rule makes it.
879///
880/// It is spelled **once**, here, and read rather than restated everywhere else — including
881/// by the shared journeys, which take it from this constant so a board fixture cannot
882/// drift from what this source reads. `docs/metadata.md` records the two consequences that
883/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
884/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
885/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
886pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
887
888/// Exact GraphQL query documents issued by this plugin.
889///
890/// Keeping the production documents here lets the pinned-schema test validate the same
891/// bytes that are sent to GitHub, rather than a test-only copy which could drift
892/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
893/// field, and its guarded caller always supplies the complete existing option set with ids.
894pub mod graphql {
895    /// The board half of one item: the field values every document here reads it from.
896    ///
897    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
898    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
899    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
900    /// *the same value*, because
901    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
902    /// one path. Three spellings of it is what would drift, so there is one.
903    ///
904    /// The `Status` option and this source's own origin text field are the whole of it. It
905    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
906    /// content, so it holds nothing the content's own `labels` do not already say, and it
907    /// would sit a label connection two page sizes deep.
908    macro_rules! board_item_values {
909        () => {
910            r#"fieldValues(first:$nestedFirst){nodes{
911          ... on ProjectV2ItemFieldSingleSelectValue{name field{
912            ... on ProjectV2SingleSelectField{id name options{id name}}
913          }}
914          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
915        }pageInfo{hasNextPage}}"#
916        };
917    }
918
919    /// The variable every document reading an issue declares for the relationship events its
920    /// change token counts, with its value as the default, so no caller binds it.
921    ///
922    /// A variable rather than a literal list because every argument a production document
923    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
924    /// its default to the pinned enum. Its default is the six event types GitHub records a
925    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
926    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
927    /// project membership on the task, as its parent, which GitHub records there whichever
928    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
929    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
930    /// decide. [`super::change_token`] says why the count is in the token.
931    macro_rules! relationship_events_variable {
932        () => {
933            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
934        };
935    }
936
937    /// Everything this source reads about one issue, wherever it reaches that issue.
938    ///
939    /// A macro rather than a constant so the three documents below can `concat!` it: one
940    /// spelling of these fields is what makes an issue read through the board-scoped
941    /// search, through its own node id, and through its project's sub-issue relationship
942    /// resolve to *the same* item, which is the whole of what
943    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
944    ///
945    /// `projectItems` is what carries the board half of an issue: the board item's own id
946    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
947    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
948    /// issue rather than on the board, which is what makes the cost of a read proportional
949    /// to what was asked for instead of to the board's size.
950    ///
951    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
952    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
953    /// not on that page: a page here is where the search for the entry starts rather than
954    /// where it ends.
955    ///
956    /// It does **not** select the board's `Labels` field value, and that is the whole of
957    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
958    /// a label connection there sits under `fieldValues` under `projectItems` under a page
959    /// of issues, spending `$nestedFirst` twice down one path, and took
960    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
961    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
962    /// above, and that connection is where every label this source reports comes from. No
963    /// document in this module selects the board field any longer, [`BOARD`] included; the
964    /// module documentation records why nothing it could have held is lost.
965    macro_rules! board_issue {
966        () => {
967            concat!(
968                r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
969      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
970      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
971        "#,
972                board_item_values!(),
973                r#"}pageInfo{hasNextPage endCursor}}}"#
974            )
975        };
976    }
977
978    /// Every issue of one board, found by a search scoped to that board.
979    ///
980    /// This is how the projects a board holds are listed, and it selects no `items`
981    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
982    /// container walked page by page, so nothing nested inside a board item is paid for.
983    /// Which of the issues it returns is a project is then read off `parent` — GitHub
984    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
985    /// discriminator has to be applied to the field, which is a scalar on the issue and
986    /// costs nothing.
987    pub const SEARCH_ISSUES: &str = concat!(
988        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
989        relationship_events_variable!(),
990        r#"){
991      search(query:$search,type:$type,first:$first,after:$after){
992        pageInfo{hasNextPage endCursor}
993        nodes{__typename ...BoardIssue}
994      }
995    }"#,
996        board_issue!()
997    );
998
999    /// What a dependency read selects of each far end: enough to say which kind of item it
1000    /// is, its body included for the kind marker.
1001    macro_rules! related_issue {
1002        () => {
1003            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1004        };
1005    }
1006
1007    /// One issue by its own node id, which is what a qualified id names here — with what a
1008    /// write of it needs and the issue does not carry in `board_issue!`: the field
1009    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1010    ///
1011    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1012    /// answers a write made moments ago with the value from before it, and resolving a node
1013    /// id does not.
1014    ///
1015    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1016    /// reads it by its own id, and with them that one read answers everything the write
1017    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1018    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1019    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1020    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1021    /// they sit under one item, and this read is still one point.
1022    pub const ISSUE: &str = concat!(
1023        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1024        relationship_events_variable!(),
1025        r#"){
1026      node(id:$id){__typename ...BoardIssue ... on Issue{
1027        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1028          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1029          ... on ProjectV2Field{__typename id name dataType}
1030        }pageInfo{hasNextPage}}}}}
1031        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1032      }}
1033    }"#,
1034        board_issue!(),
1035        related_issue!()
1036    );
1037
1038    /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1039    /// under and that project's members beside it: the project issue whole, and a page of its
1040    /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1041    ///
1042    /// What a guarded write of a document's metadata reads, and what reading a document by its
1043    /// id is: a write to a board the boundary screens is held to who may read the document,
1044    /// which is the project's and every member's classification as much as its own, and here
1045    /// the one request that holds the version the write is compared against answers all of it.
1046    /// Its members carry no edges, which nothing classifying them reads.
1047    pub const DOCUMENT_IN_PROJECT: &str = concat!(
1048        r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1049        relationship_events_variable!(),
1050        r#"){
1051      node(id:$id){__typename ...BoardIssue ... on Issue{
1052        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1053          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1054          ... on ProjectV2Field{__typename id name dataType}
1055        }pageInfo{hasNextPage}}}}}
1056        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1057        project:parent{__typename ...BoardIssue
1058          subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1059        }
1060      }}
1061    }"#,
1062        board_issue!(),
1063        related_issue!()
1064    );
1065
1066    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1067    /// what blocks it.
1068    ///
1069    /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1070    /// one point buys — rather than at `$first`, because every row carries six connections and
1071    /// a hundred-row page cost six points to list a project of ten.
1072    ///
1073    /// The work this costs is the project's own size. Nothing about it grows as the board
1074    /// gains projects, or as those projects gain tasks.
1075    ///
1076    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1077    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1078    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1079    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1080    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1081    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1082    /// It adds a connection under each issue of the page — one rate-limit point per page,
1083    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1084    /// answer questions that never read an edge.
1085    ///
1086    /// **The project issue itself rides here too**, with the page of what blocks it, because
1087    /// the reader of a project's tasks reads the project next: a copy writing the project reads
1088    /// it and its edges before it writes them. Carried on the request that already names the
1089    /// project, the first page answers that read and the dependency read after it, at the
1090    /// state GitHub answered in this command, rather than spending one more read on an item
1091    /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1092    /// copy's read of a task this listing carried is answered by it rather than sent again.
1093    /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1094    pub const SUB_ISSUES: &str = concat!(
1095        r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1096        relationship_events_variable!(),
1097        r#"){
1098      node(id:$id){__typename ...BoardIssue
1099        ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1100          subIssues(first:$memberFirst,after:$after){
1101          pageInfo{hasNextPage endCursor}
1102          nodes{__typename ...BoardIssue ... on Issue{
1103            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1104          }}
1105        }}}
1106    }"#,
1107        board_issue!(),
1108        related_issue!()
1109    );
1110
1111    /// What a read of the board's own `items` selects of each item's content.
1112    ///
1113    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1114    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1115    /// content by one spelling.
1116    macro_rules! board_item_content {
1117        () => {
1118            r#" content{
1119        ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
1120        ... on PullRequest{__typename id}
1121        ... on DraftIssue{__typename id title body createdAt updatedAt}
1122      }"#
1123        };
1124    }
1125
1126    /// Reads the board's fields and one page of its items.
1127    pub const BOARD: &str = concat!(
1128        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1129        relationship_events_variable!(),
1130        r#"){
1131      owner:repositoryOwner(login:$owner){
1132        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1133      }
1134    } fragment Board on ProjectV2 { id title
1135      fields(first:$nestedFirst){nodes{
1136        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1137        ... on ProjectV2Field{__typename id name dataType}
1138      }pageInfo{hasNextPage}}
1139      items(first:$first,after:$after){nodes{id updatedAt "#,
1140        board_item_values!(),
1141        board_item_content!(),
1142        r#"} pageInfo{hasNextPage endCursor}}
1143    }"#
1144    );
1145
1146    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1147    /// the board.
1148    ///
1149    /// **`originItems`** is the board's own items narrowed by its own field filter —
1150    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1151    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1152    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1153    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1154    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1155    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1156    /// fresh `addProjectV2ItemById` the way that connection does.
1157    ///
1158    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1159    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1160    /// indexes that comment, and the index catches up with a write in a second or two rather
1161    /// than in minutes, so it finds a carrier another process wrote that the first read is
1162    /// still behind on.
1163    ///
1164    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1165    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1166    /// from its last cursor, which answers an empty page. Every candidate either read returns
1167    /// is confirmed against its own origin field before it is reported, so a token match of
1168    /// the search or anything else the filter admits never is.
1169    ///
1170    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1171    /// own whole reads counts this one among them.
1172    pub const ORIGIN_LOOKUP: &str = concat!(
1173        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1174        relationship_events_variable!(),
1175        r#"){
1176      originItems:repositoryOwner(login:$owner){
1177        ... on ProjectV2Owner{projectV2(number:$number){
1178          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1179        board_item_values!(),
1180        board_item_content!(),
1181        r#"} pageInfo{hasNextPage endCursor}}
1182        }}
1183      }
1184      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1185        pageInfo{hasNextPage endCursor}
1186        nodes{__typename ...BoardIssue}
1187      }
1188    }"#,
1189        board_issue!()
1190    );
1191
1192    /// Every closed item of the board, by the board's own field filter — what
1193    /// `task status reconcile` reads, and nothing else of the board.
1194    ///
1195    /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1196    /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1197    /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1198    /// closed, with a reason and a card that contradict each other, from the item node it
1199    /// already carries, so a filter that also let open items through would cost reads and
1200    /// write nothing wrong.
1201    ///
1202    /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1203    /// option writes that follow need no read of the board. The root is aliased `closedItems`
1204    /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1205    /// among them.
1206    pub const CLOSED_ITEMS: &str = concat!(
1207        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1208        relationship_events_variable!(),
1209        r#"){
1210      closedItems:repositoryOwner(login:$owner){
1211        ... on ProjectV2Owner{projectV2(number:$number){id
1212          fields(first:$nestedFirst){nodes{
1213            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1214            ... on ProjectV2Field{__typename id name dataType}
1215          }pageInfo{hasNextPage}}
1216          items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1217        board_item_values!(),
1218        board_item_content!(),
1219        r#"} pageInfo{hasNextPage endCursor}}
1220        }}
1221      }
1222    }"#
1223    );
1224
1225    /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1226    macro_rules! reference_alias {
1227        ($n:literal) => {
1228            concat!(
1229                "\n      r",
1230                stringify!($n),
1231                ":repository(owner:$owner",
1232                stringify!($n),
1233                ",name:$name",
1234                stringify!($n),
1235                "){issue(number:$number",
1236                stringify!($n),
1237                "){...HeldIssue}}"
1238            )
1239        };
1240    }
1241
1242    /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1243    /// number — the one read `task status complete` makes of a source, and nothing else of
1244    /// the board: no `items`, no search and no project read.
1245    ///
1246    /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1247    /// references one point.** It selects no labels, and of each board membership only the
1248    /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1249    /// connection, its page of memberships, where the whole fragment asks for five. What a
1250    /// completion writes is a card and, at most, the issue's state, so the labels and the other
1251    /// field values it leaves out are never what it needs; an item read this way is held for
1252    /// that write alone and never answers another read. An issue whose entry for this board is
1253    /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1254    /// every other read of an issue recovers one.
1255    ///
1256    /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1257    /// fewer references binds the slots it has none for to its last one.
1258    pub const ISSUES_BY_REFERENCE: &str = concat!(
1259        r#"query($owner0:String!,$name0:String!,$number0:Int!,$owner1:String!,$name1:String!,$number1:Int!,$owner2:String!,$name2:String!,$number2:Int!,$owner3:String!,$name3:String!,$number3:Int!,$owner4:String!,$name4:String!,$number4:Int!,$owner5:String!,$name5:String!,$number5:Int!,$owner6:String!,$name6:String!,$number6:Int!,$owner7:String!,$name7:String!,$number7:Int!,$owner8:String!,$name8:String!,$number8:Int!,$owner9:String!,$name9:String!,$number9:Int!,$owner10:String!,$name10:String!,$number10:Int!,$owner11:String!,$name11:String!,$number11:Int!,$owner12:String!,$name12:String!,$number12:Int!,$owner13:String!,$name13:String!,$number13:Int!,$owner14:String!,$name14:String!,$number14:Int!,$owner15:String!,$name15:String!,$number15:Int!,$owner16:String!,$name16:String!,$number16:Int!,$owner17:String!,$name17:String!,$number17:Int!,$owner18:String!,$name18:String!,$number18:Int!,$owner19:String!,$name19:String!,$number19:Int!,$owner20:String!,$name20:String!,$number20:Int!,$owner21:String!,$name21:String!,$number21:Int!,$owner22:String!,$name22:String!,$number22:Int!,$owner23:String!,$name23:String!,$number23:Int!,$owner24:String!,$name24:String!,$number24:Int!,$owner25:String!,$name25:String!,$number25:Int!,$owner26:String!,$name26:String!,$number26:Int!,$owner27:String!,$name27:String!,$number27:Int!,$owner28:String!,$name28:String!,$number28:Int!,$owner29:String!,$name29:String!,$number29:Int!,$owner30:String!,$name30:String!,$number30:Int!,$owner31:String!,$name31:String!,$number31:Int!,$owner32:String!,$name32:String!,$number32:Int!,$owner33:String!,$name33:String!,$number33:Int!,$owner34:String!,$name34:String!,$number34:Int!,$owner35:String!,$name35:String!,$number35:Int!,$owner36:String!,$name36:String!,$number36:Int!,$owner37:String!,$name37:String!,$number37:Int!,$owner38:String!,$name38:String!,$number38:Int!,$owner39:String!,$name39:String!,$number39:Int!,$owner40:String!,$name40:String!,$number40:Int!,$owner41:String!,$name41:String!,$number41:Int!,$owner42:String!,$name42:String!,$number42:Int!,$owner43:String!,$name43:String!,$number43:Int!,$owner44:String!,$name44:String!,$number44:Int!,$owner45:String!,$name45:String!,$number45:Int!,$owner46:String!,$name46:String!,$number46:Int!,$owner47:String!,$name47:String!,$number47:Int!,$owner48:String!,$name48:String!,$number48:Int!,$owner49:String!,$name49:String!,$number49:Int!,$boardItems:Int!,$statusField:String!,$duplicates:Boolean!"#,
1260        relationship_events_variable!(),
1261        r#"){"#,
1262        reference_alias!(0),
1263        reference_alias!(1),
1264        reference_alias!(2),
1265        reference_alias!(3),
1266        reference_alias!(4),
1267        reference_alias!(5),
1268        reference_alias!(6),
1269        reference_alias!(7),
1270        reference_alias!(8),
1271        reference_alias!(9),
1272        reference_alias!(10),
1273        reference_alias!(11),
1274        reference_alias!(12),
1275        reference_alias!(13),
1276        reference_alias!(14),
1277        reference_alias!(15),
1278        reference_alias!(16),
1279        reference_alias!(17),
1280        reference_alias!(18),
1281        reference_alias!(19),
1282        reference_alias!(20),
1283        reference_alias!(21),
1284        reference_alias!(22),
1285        reference_alias!(23),
1286        reference_alias!(24),
1287        reference_alias!(25),
1288        reference_alias!(26),
1289        reference_alias!(27),
1290        reference_alias!(28),
1291        reference_alias!(29),
1292        reference_alias!(30),
1293        reference_alias!(31),
1294        reference_alias!(32),
1295        reference_alias!(33),
1296        reference_alias!(34),
1297        reference_alias!(35),
1298        reference_alias!(36),
1299        reference_alias!(37),
1300        reference_alias!(38),
1301        reference_alias!(39),
1302        reference_alias!(40),
1303        reference_alias!(41),
1304        reference_alias!(42),
1305        reference_alias!(43),
1306        reference_alias!(44),
1307        reference_alias!(45),
1308        reference_alias!(46),
1309        reference_alias!(47),
1310        reference_alias!(48),
1311        reference_alias!(49),
1312        "\n    }",
1313        r#" fragment HeldIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
1314      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1315        status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1316          ... on ProjectV2SingleSelectField{id name options{id name}}
1317        }}}
1318      }pageInfo{hasNextPage endCursor}}}"#
1319    );
1320
1321    /// The board's own id and field definitions, and not one of its items.
1322    ///
1323    /// What a write needs of the board when the item it writes does not say: the id a field
1324    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1325    /// origin fields. It selects no `items`, so what it costs is the board's field list
1326    /// however many items the board holds — and it decides nothing about which items those
1327    /// are, which is the question a read of one item by its own id answers instead.
1328    ///
1329    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1330    /// board's item reads by their root counts this one among them.
1331    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1332      boardFields:repositoryOwner(login:$owner){
1333        ... on ProjectV2Owner{projectV2(number:$number){id
1334          fields(first:$nestedFirst){nodes{
1335            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1336            ... on ProjectV2Field{__typename id name dataType}
1337          }pageInfo{hasNextPage}}
1338        }}
1339      }
1340    }"#;
1341
1342    /// One board draft by its own node id, with the board item it sits in.
1343    ///
1344    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1345    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1346    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1347    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1348    /// board listing hands it to, and nothing has to list the board to find one.
1349    pub const DRAFT: &str = concat!(
1350        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1351      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1352        projectV2Items(first:$boardItems){nodes{id project{id number}
1353        "#,
1354        board_item_values!(),
1355        r#"}pageInfo{hasNextPage endCursor}}}}
1356    }"#
1357    );
1358
1359    /// One issue's board memberships alone, walked past the page a read of it carried.
1360    ///
1361    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1362    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1363    /// boards than that page holds may have this board's entry past its end. This asks that
1364    /// one issue for its memberships and nothing else — the caller already holds the issue —
1365    /// so an answer of "this board does not hold it" is only ever given about a connection
1366    /// read to exhaustion.
1367    ///
1368    /// It selects the board item's id, its project number and the same
1369    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1370    /// very same resolver: an issue recovered this way reports the same title, the same
1371    /// status, the same labels and the same qualified id as one whose entry was on the
1372    /// page.
1373    ///
1374    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1375    /// multiplies through it and the membership connection can be walked at
1376    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1377    /// further request for any issue a person really keeps.
1378    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1379        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1380      node(id:$id){
1381        ... on Issue{projectItems(first:$first,after:$after){
1382          nodes{id updatedAt project{id number}
1383        "#,
1384        board_item_values!(),
1385        r#"}
1386          pageInfo{hasNextPage endCursor}}}
1387      }
1388    }"#
1389    );
1390    /// Whether the board's Project is public — half of what decides whether a write here can
1391    /// be read by anybody. Needs the `read:project` scope.
1392    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1393    /// Resolves the configured repository's node id, which creating an issue requires.
1394    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1395    /// What creating an issue needs and has not read yet: the board's own id and field
1396    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1397    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1398    ///
1399    /// Sent at the point a create knows which repository it is for, when neither half is
1400    /// already known to this process; a create needing only one of them sends that one's own
1401    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1402    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1403    /// status.
1404    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1405      boardFields:repositoryOwner(login:$owner){
1406        ... on ProjectV2Owner{projectV2(number:$number){id
1407          fields(first:$nestedFirst){nodes{
1408            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1409            ... on ProjectV2Field{__typename id name dataType}
1410          }pageInfo{hasNextPage}}
1411        }}
1412      }
1413      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1414    }"#;
1415    /// Reads both dependency directions for one issue, with each far end's own kind — and
1416    /// the issue's own body, which is where an edge to another source is recorded, so that
1417    /// half of a dependency read needs no second read of the issue or of the board.
1418    pub const ISSUE_DEPENDENCIES: &str = concat!(
1419        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1420      ... on Issue{body
1421        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1422        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1423      }}}"#,
1424        related_issue!()
1425    );
1426    /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1427    /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1428    /// along because its metadata slot is where an edge to another source is recorded — and a
1429    /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1430    /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1431    /// and only when that item is this board's: no draft read and no board read follow it.
1432    ///
1433    /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1434    /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1435    /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1436    /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1437    /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1438    /// one point; the credentialed lane's reconciliation is what holds the two to that.
1439    pub const ISSUE_DEPENDS_ON: &str = concat!(
1440        r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1441      node(id:$id){__typename
1442      ... on Issue{body
1443        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1444      }
1445      ... on DraftIssue{id body}}
1446      draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1447      board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1448        related_issue!()
1449    );
1450    /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1451    /// the reverse of a recorded edge is derived from the far end, never written down here.
1452    pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1453        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1454      ... on Issue{
1455        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1456      }}}"#,
1457        related_issue!()
1458    );
1459    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1460    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1461    /// `parentIssueId`, so this one request files it under both — see
1462    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1463    pub const CREATE_ISSUE: &str =
1464        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1465    /// Puts an existing issue on the configured board.
1466    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1467    /// Updates an issue's visible fields and its open or closed state in one call.
1468    pub const UPDATE_ISSUE: &str =
1469        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1470    /// Updates an existing draft's user-visible fields.
1471    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1472    /// Updates a text or single-select value on one project item.
1473    pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1474    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1475    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1476    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1477    pub const UPDATE_FIELDS: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$second:UpdateProjectV2ItemFieldValueInput!,$third:UpdateProjectV2ItemFieldValueInput!,$fourth:UpdateProjectV2ItemFieldValueInput!,$fifth:UpdateProjectV2ItemFieldValueInput!,$sixth:UpdateProjectV2ItemFieldValueInput!,$clear:ClearProjectV2ItemFieldValueInput!,$clearSecond:ClearProjectV2ItemFieldValueInput!,$clearThird:ClearProjectV2ItemFieldValueInput!,$writeFirst:Boolean!,$writeSecond:Boolean!,$writeThird:Boolean!,$writeFourth:Boolean!,$writeFifth:Boolean!,$writeSixth:Boolean!,$writeClear:Boolean!,$writeClearSecond:Boolean!,$writeClearThird:Boolean!){updateProjectV2ItemFieldValue(input:$input) @include(if:$writeFirst){projectV2Item{id}} second:updateProjectV2ItemFieldValue(input:$second) @include(if:$writeSecond){projectV2Item{id}} third:updateProjectV2ItemFieldValue(input:$third) @include(if:$writeThird){projectV2Item{id}} fourth:updateProjectV2ItemFieldValue(input:$fourth) @include(if:$writeFourth){projectV2Item{id}} fifth:updateProjectV2ItemFieldValue(input:$fifth) @include(if:$writeFifth){projectV2Item{id}} sixth:updateProjectV2ItemFieldValue(input:$sixth) @include(if:$writeSixth){projectV2Item{id}} cleared:clearProjectV2ItemFieldValue(input:$clear) @include(if:$writeClear){projectV2Item{id}} clearedSecond:clearProjectV2ItemFieldValue(input:$clearSecond) @include(if:$writeClearSecond){projectV2Item{id}} clearedThird:clearProjectV2ItemFieldValue(input:$clearThird) @include(if:$writeClearThird){projectV2Item{id}}}"#;
1478    /// Clears one project item's value of one field, which is what a `none` priority is.
1479    pub const CLEAR_FIELD: &str = r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1480    /// Creates one field: a single-select one with its options, or a text one for a projected
1481    /// metadata value. Only the guarded field setup may use this document, and only for a
1482    /// field the board lacks.
1483    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1484    /// Replaces a single-select field's options. Only the guarded field setup — the
1485    /// `status-options` and `fields` operations — may use this document, because GitHub
1486    /// treats the input as the complete option list.
1487    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1488    /// A fresh snapshot of the Status field and every board item's assignment.
1489    pub const STATUS_OPTIONS_SNAPSHOT: &str = r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!){owner:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id fields(first:$nestedFirst){nodes{... on ProjectV2SingleSelectField{id name options{id name color description}}}pageInfo{hasNextPage}} items(first:$first,after:$after){nodes{id fieldValues(first:$nestedFirst){nodes{... on ProjectV2ItemFieldSingleSelectValue{name optionId field{... on ProjectV2SingleSelectField{id name}}}}pageInfo{hasNextPage}}}pageInfo{hasNextPage endCursor}}}}}}"#;
1490    /// Files one issue under another as a sub-issue, which is what project membership is.
1491    pub const ADD_SUB_ISSUE: &str =
1492        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1493    /// Takes one issue back out of its parent.
1494    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1495    /// Adds GitHub's native issue blocked-by relationship.
1496    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1497    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1498    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1499    macro_rules! origin_lookup_alias {
1500        ($n:literal) => {
1501            concat!(
1502                "\n      o",
1503                stringify!($n),
1504                ":repositoryOwner(login:$owner) @include(if:$ask",
1505                stringify!($n),
1506                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1507                stringify!($n),
1508                "){nodes{id updatedAt ",
1509                board_item_values!(),
1510                board_item_content!(),
1511                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1512                stringify!($n),
1513                ":search(query:$search",
1514                stringify!($n),
1515                ",type:$type,first:$lookupFirst) @include(if:$ask",
1516                stringify!($n),
1517                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1518            )
1519        };
1520    }
1521
1522    /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1523    /// selected beside them: one spelling of the batched lookup for the documents that send it.
1524    macro_rules! origin_lookups {
1525        ($extra_variables:literal, $extra_fields:literal) => {
1526            concat!(
1527                r#"query($owner:String!,$number:Int!,$type:SearchType!,$lookupFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!,$repositoryOwner:String!,$repositoryName:String!,$filter0:String!,$filter1:String!,$filter2:String!,$filter3:String!,$filter4:String!,$filter5:String!,$filter6:String!,$filter7:String!,$filter8:String!,$filter9:String!,$search0:String!,$search1:String!,$search2:String!,$search3:String!,$search4:String!,$search5:String!,$search6:String!,$search7:String!,$search8:String!,$search9:String!,$ask0:Boolean!,$ask1:Boolean!,$ask2:Boolean!,$ask3:Boolean!,$ask4:Boolean!,$ask5:Boolean!,$ask6:Boolean!,$ask7:Boolean!,$ask8:Boolean!,$ask9:Boolean!"#,
1528                relationship_events_variable!(),
1529                $extra_variables,
1530                r#"){
1531      boardFields:repositoryOwner(login:$owner){
1532        ... on ProjectV2Owner{projectV2(number:$number){id
1533          fields(first:$nestedFirst){nodes{
1534            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1535            ... on ProjectV2Field{__typename id name dataType}
1536          }pageInfo{hasNextPage}}
1537        }}
1538      }
1539      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1540                $extra_fields,
1541                origin_lookup_alias!(0),
1542                origin_lookup_alias!(1),
1543                origin_lookup_alias!(2),
1544                origin_lookup_alias!(3),
1545                origin_lookup_alias!(4),
1546                origin_lookup_alias!(5),
1547                origin_lookup_alias!(6),
1548                origin_lookup_alias!(7),
1549                origin_lookup_alias!(8),
1550                origin_lookup_alias!(9),
1551                "\n    }",
1552                board_issue!()
1553            )
1554        };
1555    }
1556
1557    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1558    /// origins a copy is about to look for, and everything creating an item then reads —
1559    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1560    /// id and the numeric id of the repository an issue is created in — in one request.
1561    ///
1562    /// What a copy reads before it writes anything, so that finding none of its records on the
1563    /// board and creating every one costs this read and the writes. An origin whose page says
1564    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1565    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1566    pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1567
1568    /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1569    /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1570    /// and no connection under it.
1571    ///
1572    /// What a document copy sends where the public boundary screens its write: the document is
1573    /// held to who may read its project, which is its members' classification as much as the
1574    /// project's own, and this request carries both the project — among the origins it asks —
1575    /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1576    /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1577    pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1578        ",$project:ID!,$memberFirst:Int!",
1579        "\n      members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1580    );
1581
1582    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1583    /// whether it is sent.
1584    macro_rules! write_slot {
1585        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1586            concat!(
1587                " ",
1588                $prefix,
1589                stringify!($n),
1590                ":",
1591                $field,
1592                "(input:$",
1593                $prefix,
1594                stringify!($n),
1595                ") @include(if:$send",
1596                stringify!($n),
1597                "){",
1598                $selection,
1599                "}"
1600            )
1601        };
1602    }
1603
1604    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1605    /// and eleven `$send<n>`.
1606    macro_rules! write_variables {
1607        ($prefix:literal, $type:literal) => {
1608            concat!(
1609                "mutation(",
1610                "$",
1611                $prefix,
1612                "0:",
1613                $type,
1614                "!,$",
1615                $prefix,
1616                "1:",
1617                $type,
1618                "!,$",
1619                $prefix,
1620                "2:",
1621                $type,
1622                "!,$",
1623                $prefix,
1624                "3:",
1625                $type,
1626                "!,$",
1627                $prefix,
1628                "4:",
1629                $type,
1630                "!,$",
1631                $prefix,
1632                "5:",
1633                $type,
1634                "!,$",
1635                $prefix,
1636                "6:",
1637                $type,
1638                "!,$",
1639                $prefix,
1640                "7:",
1641                $type,
1642                "!,$",
1643                $prefix,
1644                "8:",
1645                $type,
1646                "!,$",
1647                $prefix,
1648                "9:",
1649                $type,
1650                "!,$",
1651                $prefix,
1652                "10:",
1653                $type,
1654                "!,",
1655                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1656                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1657                "$send10:Boolean!){"
1658            )
1659        };
1660    }
1661
1662    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1663    /// `$field`, each sent only while its `$send<n>` holds.
1664    macro_rules! write_batch {
1665        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1666            concat!(
1667                write_variables!($prefix, $type),
1668                write_slot!($prefix, $field, $selection, 0),
1669                write_slot!($prefix, $field, $selection, 1),
1670                write_slot!($prefix, $field, $selection, 2),
1671                write_slot!($prefix, $field, $selection, 3),
1672                write_slot!($prefix, $field, $selection, 4),
1673                write_slot!($prefix, $field, $selection, 5),
1674                write_slot!($prefix, $field, $selection, 6),
1675                write_slot!($prefix, $field, $selection, 7),
1676                write_slot!($prefix, $field, $selection, 8),
1677                write_slot!($prefix, $field, $selection, 9),
1678                write_slot!($prefix, $field, $selection, 10),
1679                "}"
1680            )
1681        };
1682    }
1683
1684    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1685    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1686    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1687    pub const CREATE_ISSUES: &str = write_batch!(
1688        "create",
1689        "CreateIssueInput",
1690        "createIssue",
1691        "issue{id number url}"
1692    );
1693
1694    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1695    /// the `Status` value of each, read only while `$read<n>` holds.
1696    macro_rules! created_item_alias {
1697        ($n:literal) => {
1698            concat!(
1699                "\n      i",
1700                stringify!($n),
1701                ":node(id:$id",
1702                stringify!($n),
1703                ") @include(if:$read",
1704                stringify!($n),
1705                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1706            )
1707        };
1708    }
1709
1710    /// The board item of each issue a copy created, up to
1711    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1712    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1713    /// answering the create, so its id comes from a read made once every create has been sent.
1714    pub const CREATED_BOARD_ITEMS: &str = concat!(
1715        r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$read0:Boolean!,$read1:Boolean!,$read2:Boolean!,$read3:Boolean!,$read4:Boolean!,$read5:Boolean!,$read6:Boolean!,$read7:Boolean!,$read8:Boolean!,$read9:Boolean!,$read10:Boolean!,$read11:Boolean!,$read12:Boolean!,$read13:Boolean!,$read14:Boolean!,$read15:Boolean!,$read16:Boolean!,$read17:Boolean!,$read18:Boolean!,$read19:Boolean!,$read20:Boolean!,$boardItems:Int!,$statusName:String!){"#,
1716        created_item_alias!(0),
1717        created_item_alias!(1),
1718        created_item_alias!(2),
1719        created_item_alias!(3),
1720        created_item_alias!(4),
1721        created_item_alias!(5),
1722        created_item_alias!(6),
1723        created_item_alias!(7),
1724        created_item_alias!(8),
1725        created_item_alias!(9),
1726        created_item_alias!(10),
1727        created_item_alias!(11),
1728        created_item_alias!(12),
1729        created_item_alias!(13),
1730        created_item_alias!(14),
1731        created_item_alias!(15),
1732        created_item_alias!(16),
1733        created_item_alias!(17),
1734        created_item_alias!(18),
1735        created_item_alias!(19),
1736        created_item_alias!(20),
1737        "\n    }"
1738    );
1739
1740    /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1741    /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1742    macro_rules! wide_variables {
1743        ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1744            concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1745        };
1746    }
1747    macro_rules! wide_slots {
1748        ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1749            concat!($(
1750                " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1751                ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1752            )+)
1753        };
1754    }
1755
1756    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1757    /// created, each on its own item, in one ordered request.
1758    pub const WRITE_FIELD_VALUES: &str = concat!(
1759        "mutation(",
1760        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1761        "){",
1762        wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1763        "}"
1764    );
1765
1766    /// A copy's board field values and its blocked-by edges together, up to
1767    /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1768    /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1769    /// What a copy sends when one request of its writes holds both kinds; one holding a single
1770    /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1771    pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1772        "mutation(",
1773        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1774        wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1775        "){",
1776        wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1777        wide_slots!("block", "addBlockedBy", "issue{id} blockingIssue{id}", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1778        "}"
1779    );
1780
1781    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1782    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1783    pub const CLOSE_ISSUES: &str =
1784        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1785
1786    /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1787    /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1788    pub const UPDATE_ISSUES: &str =
1789        write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1790
1791    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1792    /// issues it created, in one request.
1793    pub const ADD_BLOCKED_BYS: &str = concat!(
1794        "mutation(",
1795        wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1796        "){",
1797        wide_slots!("block", "addBlockedBy", "issue{id} blockingIssue{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1798        "}"
1799    );
1800
1801    /// Removes one native issue blocked-by relationship.
1802    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1803    /// Deletes one issue, which takes its board item with it.
1804    ///
1805    /// The engine sends this in one situation only: undoing a copy that could not finish,
1806    /// over the items that same copy created. Deleting the issue removes the board item
1807    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1808    pub const DELETE_ISSUE: &str =
1809        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1810    /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1811    /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1812    /// part way sends to take them back.
1813    pub const DELETE_ISSUES: &str = concat!(
1814        "mutation(",
1815        wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1816        "){",
1817        wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1818        "}"
1819    );
1820
1821    /// Everything this source reads about one issue comment, wherever it reaches one.
1822    ///
1823    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1824    /// and a comment just edited are handed to one mapper, so they are selected by one
1825    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1826    /// longer exists, and `login` is the one member every kind of actor carries.
1827    macro_rules! issue_comment {
1828        () => {
1829            "id author{login} createdAt updatedAt body url"
1830        };
1831    }
1832
1833    /// One task's comments: a page of its issue's own `comments` connection.
1834    ///
1835    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1836    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1837    /// list every time somebody edited it; left unordered the connection answers in the order
1838    /// the comments were written, which is the order GitHub documents for the same collection
1839    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1840    /// node count and the caller's own page size is pushed straight down.
1841    pub const ISSUE_COMMENTS: &str = concat!(
1842        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1843        issue_comment!(),
1844        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1845    );
1846    /// One issue by its own node id, with a page of its comments: what `task show` and a
1847    /// comment listing read, in one request.
1848    ///
1849    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1850    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1851    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1852    /// connection there would multiply through both of those documents' price, and neither
1853    /// needs one.
1854    pub const ISSUE_DETAIL: &str = concat!(
1855        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1856        relationship_events_variable!(),
1857        r#"){
1858      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1859        issue_comment!(),
1860        r#"}pageInfo{hasNextPage endCursor}}}}
1861    }"#,
1862        board_issue!()
1863    );
1864
1865    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1866    /// page of its comments when `$comments` asks for them.
1867    macro_rules! issue_details_alias {
1868        ($n:literal) => {
1869            concat!(
1870                "\n      i",
1871                stringify!($n),
1872                ":node(id:$id",
1873                stringify!($n),
1874                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1875                issue_comment!(),
1876                "}pageInfo{hasNextPage endCursor}}}}"
1877            )
1878        };
1879    }
1880
1881    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1882    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1883    ///
1884    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1885    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1886    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1887    /// neither — so every connection under it would be priced at nothing and the pin in
1888    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1889    /// one-item read the model already prices, so the batch costs what its aliases cost.
1890    ///
1891    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1892    /// slots it has no item for to the last item it does, and reads that item again; the
1893    /// price is the document's, whatever its variables, so a short batch costs what a full
1894    /// one does and nothing more.
1895    pub const ISSUE_DETAILS: &str = concat!(
1896        r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$id21:ID!,$id22:ID!,$id23:ID!,$first:Int!,$comments:Boolean!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1897        relationship_events_variable!(),
1898        r#"){"#,
1899        issue_details_alias!(0),
1900        issue_details_alias!(1),
1901        issue_details_alias!(2),
1902        issue_details_alias!(3),
1903        issue_details_alias!(4),
1904        issue_details_alias!(5),
1905        issue_details_alias!(6),
1906        issue_details_alias!(7),
1907        issue_details_alias!(8),
1908        issue_details_alias!(9),
1909        issue_details_alias!(10),
1910        issue_details_alias!(11),
1911        issue_details_alias!(12),
1912        issue_details_alias!(13),
1913        issue_details_alias!(14),
1914        issue_details_alias!(15),
1915        issue_details_alias!(16),
1916        issue_details_alias!(17),
1917        issue_details_alias!(18),
1918        issue_details_alias!(19),
1919        issue_details_alias!(20),
1920        issue_details_alias!(21),
1921        issue_details_alias!(22),
1922        issue_details_alias!(23),
1923        "\n    }",
1924        board_issue!()
1925    );
1926
1927    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1928    /// page of its comments and the issues it is blocked by.
1929    macro_rules! issue_copy_details_alias {
1930        ($n:literal) => {
1931            concat!(
1932                "\n      i",
1933                stringify!($n),
1934                ":node(id:$id",
1935                stringify!($n),
1936                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1937                issue_comment!(),
1938                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1939            )
1940        };
1941    }
1942
1943    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1944    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1945    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1946    /// at a time, so a copy of a changed task costs this read and nothing else.
1947    ///
1948    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1949    /// what `task show-many` is priced by and which reads no edge.
1950    ///
1951    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1952    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1953    /// project copy, which reads its project and every task at once, knows from this same read
1954    /// which tasks the project holds at the destination, and its orphan check is answered by it
1955    /// rather than by a listing of the project.
1956    pub const ISSUE_COPY_DETAILS: &str = concat!(
1957        r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$project:ID!,$children:Boolean!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1958        relationship_events_variable!(),
1959        r#"){"#,
1960        issue_copy_details_alias!(0),
1961        issue_copy_details_alias!(1),
1962        issue_copy_details_alias!(2),
1963        issue_copy_details_alias!(3),
1964        issue_copy_details_alias!(4),
1965        issue_copy_details_alias!(5),
1966        issue_copy_details_alias!(6),
1967        issue_copy_details_alias!(7),
1968        issue_copy_details_alias!(8),
1969        issue_copy_details_alias!(9),
1970        issue_copy_details_alias!(10),
1971        issue_copy_details_alias!(11),
1972        issue_copy_details_alias!(12),
1973        issue_copy_details_alias!(13),
1974        issue_copy_details_alias!(14),
1975        issue_copy_details_alias!(15),
1976        issue_copy_details_alias!(16),
1977        issue_copy_details_alias!(17),
1978        issue_copy_details_alias!(18),
1979        issue_copy_details_alias!(19),
1980        issue_copy_details_alias!(20),
1981        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1982        "\n    }",
1983        board_issue!(),
1984        related_issue!()
1985    );
1986
1987    /// Which issue one comment is on, read before that comment is edited or removed.
1988    ///
1989    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1990    /// comment id given against the wrong task would change a comment on another issue.
1991    pub const COMMENT_ISSUE: &str =
1992        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1993    /// Adds one comment to an issue, signed as the account the token belongs to.
1994    pub const ADD_COMMENT: &str = concat!(
1995        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1996        issue_comment!(),
1997        r#"}}}}"#
1998    );
1999    /// Replaces the body of one issue comment.
2000    pub const UPDATE_COMMENT: &str = concat!(
2001        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2002        issue_comment!(),
2003        r#"}}}"#
2004    );
2005    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2006    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2007
2008    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2009    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2010    /// change token's every part and the first page of its comments.
2011    ///
2012    /// Sent **only** while that write runs, in place of the document named beside each
2013    /// below, so every other write's documents, prices and request counts are what they
2014    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2015    /// mutation's response is stamped seconds after the change it made, so against GitHub
2016    /// itself a response rarely determines a version; the cache is then left unserveable
2017    /// for the task, and its next read refreshes it.
2018    macro_rules! whole_issue {
2019        () => {
2020            concat!(
2021                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2022                issue_comment!(),
2023                r#"}pageInfo{hasNextPage endCursor}}}"#
2024            )
2025        };
2026    }
2027
2028    /// The variables every document of `whole_issue!` declares beside its own.
2029    macro_rules! whole_variables {
2030        () => {
2031            concat!(
2032                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2033                relationship_events_variable!()
2034            )
2035        };
2036    }
2037
2038    /// [`ADD_COMMENT`], selecting the whole issue commented on.
2039    pub const ADD_COMMENT_WHOLE: &str = concat!(
2040        r#"mutation($input:AddCommentInput!"#,
2041        whole_variables!(),
2042        r#"){addComment(input:$input){subject{id "#,
2043        whole_issue!(),
2044        r#"} commentEdge{node{"#,
2045        issue_comment!(),
2046        r#"}}}}"#,
2047        board_issue!()
2048    );
2049    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2050    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2051        r#"mutation($input:UpdateIssueCommentInput!"#,
2052        whole_variables!(),
2053        r#"){updateIssueComment(input:$input){issueComment{"#,
2054        issue_comment!(),
2055        r#" issue{"#,
2056        whole_issue!(),
2057        r#"}}}}"#,
2058        board_issue!()
2059    );
2060    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2061    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2062        r#"mutation($input:UpdateIssueInput!"#,
2063        whole_variables!(),
2064        r#"){updateIssue(input:$input){issue{id "#,
2065        whole_issue!(),
2066        r#"}}}"#,
2067        board_issue!()
2068    );
2069    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2070    pub const UPDATE_FIELD_WHOLE: &str = concat!(
2071        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2072        whole_variables!(),
2073        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2074        whole_issue!(),
2075        r#"}}}}"#,
2076        board_issue!()
2077    );
2078    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2079    pub const CLEAR_FIELD_WHOLE: &str = concat!(
2080        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2081        whole_variables!(),
2082        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2083        whole_issue!(),
2084        r#"}}}}"#,
2085        board_issue!()
2086    );
2087
2088    /// The document sent in `document`'s place while a write the store's cache expects runs,
2089    /// when it has one.
2090    #[must_use]
2091    pub fn whole_twin(document: &str) -> Option<&'static str> {
2092        [
2093            (ADD_COMMENT, ADD_COMMENT_WHOLE),
2094            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2095            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2096            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2097            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2098        ]
2099        .into_iter()
2100        .find(|(plain, _)| *plain == document)
2101        .map(|(_, whole)| whole)
2102    }
2103
2104    /// Every document above, with what this source is doing when it sends one.
2105    ///
2106    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2107    /// name the call that was refused, and a `match` with a catch-all arm would answer a
2108    /// document added later with "talking to GitHub" and never say so.
2109    ///
2110    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2111    /// const` here that this list omits, so the two cannot part — which is the same guard
2112    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2113    pub const DOCUMENTS: [(&str, &str); 55] = [
2114        (SEARCH_ISSUES, "searching this board's issues"),
2115        (ISSUE, "reading one issue"),
2116        (
2117            DOCUMENT_IN_PROJECT,
2118            "reading one document with its project and the project's members",
2119        ),
2120        (
2121            ISSUE_BOARD_ITEMS,
2122            "reading one issue's board memberships past the page it came with",
2123        ),
2124        (SUB_ISSUES, "reading a project's tasks"),
2125        (BOARD, "reading the board"),
2126        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2127        (CLOSED_ITEMS, "reading the board's closed items"),
2128        (
2129            ISSUES_BY_REFERENCE,
2130            "reading a batch of issues by their references",
2131        ),
2132        (BOARD_FIELDS, "reading the board's fields"),
2133        (DRAFT, "reading one draft"),
2134        (REPOSITORY, "reading the destination repository"),
2135        (
2136            PROJECT_VISIBILITY,
2137            "reading whether the board's project is public",
2138        ),
2139        (
2140            CREATION_CONTEXT,
2141            "reading the board's fields and the destination repository",
2142        ),
2143        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2144        (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2145        (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2146        (CREATE_ISSUE, "creating an issue"),
2147        (
2148            ORIGIN_LOOKUPS,
2149            "looking up the items copied from several origins, and what creating one reads",
2150        ),
2151        (
2152            ORIGIN_LOOKUPS_WITH_MEMBERS,
2153            "looking up the items copied from several origins, with a project's members",
2154        ),
2155        (CREATE_ISSUES, "creating several issues together"),
2156        (
2157            CREATED_BOARD_ITEMS,
2158            "reading the board items of the issues a copy created",
2159        ),
2160        (
2161            WRITE_FIELD_VALUES,
2162            "writing board fields of the items a copy created",
2163        ),
2164        (
2165            WRITE_FIELDS_AND_EDGES,
2166            "writing board fields and dependencies of the items a copy created",
2167        ),
2168        (CLOSE_ISSUES, "closing the issues a copy created"),
2169        (UPDATE_ISSUES, "updating several issues together"),
2170        (ADD_BLOCKED_BYS, "recording several dependencies together"),
2171        (ADD_TO_BOARD, "adding an issue to the board"),
2172        (UPDATE_ISSUE, "updating an issue"),
2173        (UPDATE_DRAFT, "updating a draft item"),
2174        (UPDATE_FIELD, "writing a board field"),
2175        (UPDATE_FIELDS, "writing board fields together"),
2176        (CLEAR_FIELD, "clearing a board field"),
2177        (
2178            CREATE_FIELD,
2179            "creating a board single-select field with its options",
2180        ),
2181        (
2182            STATUS_OPTIONS_SNAPSHOT,
2183            "snapshotting board Status options and assignments",
2184        ),
2185        (
2186            STATUS_OPTIONS_UPDATE,
2187            "safely replacing the board Status option list",
2188        ),
2189        (ADD_SUB_ISSUE, "filing an issue under its project"),
2190        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2191        (ADD_BLOCKED_BY, "recording a dependency"),
2192        (REMOVE_BLOCKED_BY, "removing a dependency"),
2193        (DELETE_ISSUE, "deleting an issue"),
2194        (DELETE_ISSUES, "deleting several issues together"),
2195        (ISSUE_COMMENTS, "reading a task's comments"),
2196        (ISSUE_DETAIL, "reading one issue with its comments"),
2197        (
2198            ISSUE_DETAILS,
2199            "reading a batch of issues with their comments",
2200        ),
2201        (
2202            ISSUE_COPY_DETAILS,
2203            "reading a batch of issues with their comments and dependencies for a copy",
2204        ),
2205        (COMMENT_ISSUE, "reading which issue a comment is on"),
2206        (ADD_COMMENT, "adding a comment"),
2207        (UPDATE_COMMENT, "editing a comment"),
2208        (DELETE_COMMENT, "deleting a comment"),
2209        (
2210            ADD_COMMENT_WHOLE,
2211            "adding a comment, answered with the whole issue",
2212        ),
2213        (
2214            UPDATE_COMMENT_WHOLE,
2215            "editing a comment, answered with the whole issue",
2216        ),
2217        (
2218            UPDATE_ISSUE_WHOLE,
2219            "updating an issue, answered with it whole",
2220        ),
2221        (
2222            UPDATE_FIELD_WHOLE,
2223            "writing a board field, answered with the whole issue",
2224        ),
2225        (
2226            CLEAR_FIELD_WHOLE,
2227            "clearing a board field, answered with the whole issue",
2228        ),
2229    ];
2230}
2231
2232/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2233/// under, the variable carrying its input, and the boolean variable that includes it.
2234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2235pub struct FieldSlot {
2236    /// The key of the answer's `data` this field's payload is under.
2237    pub alias: &'static str,
2238    /// The variable carrying this field's input.
2239    pub variable: &'static str,
2240    /// The boolean variable whose `true` sends this field.
2241    pub include: &'static str,
2242}
2243
2244/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2245pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2246    FieldSlot {
2247        alias: "updateProjectV2ItemFieldValue",
2248        variable: "input",
2249        include: "writeFirst",
2250    },
2251    FieldSlot {
2252        alias: "second",
2253        variable: "second",
2254        include: "writeSecond",
2255    },
2256    FieldSlot {
2257        alias: "third",
2258        variable: "third",
2259        include: "writeThird",
2260    },
2261    FieldSlot {
2262        alias: "fourth",
2263        variable: "fourth",
2264        include: "writeFourth",
2265    },
2266    FieldSlot {
2267        alias: "fifth",
2268        variable: "fifth",
2269        include: "writeFifth",
2270    },
2271    FieldSlot {
2272        alias: "sixth",
2273        variable: "sixth",
2274        include: "writeSixth",
2275    },
2276];
2277
2278/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2279pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2280    FieldSlot {
2281        alias: "cleared",
2282        variable: "clear",
2283        include: "writeClear",
2284    },
2285    FieldSlot {
2286        alias: "clearedSecond",
2287        variable: "clearSecond",
2288        include: "writeClearSecond",
2289    },
2290    FieldSlot {
2291        alias: "clearedThird",
2292        variable: "clearThird",
2293        include: "writeClearThird",
2294    },
2295];
2296
2297/// Which of GitHub's two rate limiters refused a request.
2298///
2299/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2300/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2301/// the whole reason this is carried rather than collapsed into "rate limited".
2302#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2303enum Limiter {
2304    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2305    Primary,
2306    /// The burst limiter over content-generating requests, which nothing reports.
2307    Secondary,
2308}
2309
2310/// The wordings GitHub answers a secondary rate limit with.
2311///
2312/// It sends them under a forbidden status, under a too-many-requests status, and inside
2313/// the `errors` of a *successful* response, which is why the text is what this matches on
2314/// rather than the status. `abuse detection` is the wording GitHub used before the
2315/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2316/// what a burst of content creation is refused with.
2317///
2318/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2319/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2320/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2321/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2322pub const SECONDARY_WORDINGS: [&str; 5] = [
2323    "secondary rate limit",
2324    "temporarily blocked from content creation",
2325    "abuse detection",
2326    "submitted too quickly",
2327    "exceeded a secondary",
2328];
2329
2330/// The wordings GitHub answers an exhausted primary budget with.
2331///
2332/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2333/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2334/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2335/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2336/// two phrases is a substring of it, so without it that answer read as a refusal that will
2337/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2338/// one reason.
2339pub const PRIMARY_WORDINGS: [&str; 4] = [
2340    "api rate limit exceeded",
2341    "api rate limit already exceeded",
2342    "rate limit exceeded",
2343    "rate_limited",
2344];
2345
2346/// What a response *says about itself*, which is the only place a refusal can be read.
2347///
2348/// Deliberately not the whole response body. A board is a place people write about their
2349/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2350/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2351/// reported. So the item data is never read: what is read is GitHub's own REST-style
2352/// `message` envelope, which is what a forbidden status carries, and the `message` and
2353/// `type` of each GraphQL error, which is where a *successful* response says it.
2354///
2355/// A body that is not JSON at all has nothing structured to read, so only a failing
2356/// response's own text is taken — a successful response that is not JSON is malformed
2357/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2358fn refusal_wording(status: StatusCode, body: &str) -> String {
2359    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2360        return if status.is_success() {
2361            String::new()
2362        } else {
2363            body.to_owned()
2364        };
2365    };
2366    let mut said: Vec<&str> = parsed
2367        .get("message")
2368        .and_then(Value::as_str)
2369        .into_iter()
2370        .collect();
2371    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2372        for error in errors {
2373            said.extend(
2374                ["message", "type"]
2375                    .into_iter()
2376                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
2377            );
2378        }
2379    }
2380    said.join("; ")
2381}
2382
2383impl Limiter {
2384    /// Which limiter refused this response, or `None` when none of them did.
2385    ///
2386    /// The wording is read first and the status only decides what carries none of it,
2387    /// because GitHub answers a secondary limit with a forbidden status far more often
2388    /// than with too-many-requests — while a forbidden status saying nothing about a limit
2389    /// really is a credential this token lacks.
2390    ///
2391    /// A response is a refusal because of its status or its own wording. A spent budget
2392    /// only ever explains one; it never turns an answer into a refusal.
2393    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2394        let normalized = refusal_wording(status, body).to_ascii_lowercase();
2395        if SECONDARY_WORDINGS
2396            .iter()
2397            .any(|wording| normalized.contains(wording))
2398        {
2399            return Some(Self::Secondary);
2400        }
2401        if status == StatusCode::TOO_MANY_REQUESTS {
2402            return Some(Self::Primary);
2403        }
2404        // An exhausted budget *explains* a response that failed; it does not make one that
2405        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2406        // request the budget allowed as well as on the ones it then refuses, so reading
2407        // the header alone threw away a good answer — and, once refusals were retried,
2408        // replayed a request that had already taken effect.
2409        if !status.is_success() && budget_exhausted {
2410            return Some(Self::Primary);
2411        }
2412        // A successful response saying it: GitHub reports a GraphQL rate limit in the
2413        // `errors` of an HTTP 200, where nothing about the status says so at all.
2414        if status.is_success()
2415            && PRIMARY_WORDINGS
2416                .iter()
2417                .any(|wording| normalized.contains(wording))
2418        {
2419            return Some(Self::Primary);
2420        }
2421        None
2422    }
2423
2424    /// What this limiter is called where an operator can look it up.
2425    const fn name(self) -> &'static str {
2426        match self {
2427            Self::Primary => "GitHub's primary API rate limit",
2428            Self::Secondary => "GitHub's secondary rate limit",
2429        }
2430    }
2431
2432    /// What the endpoint an operator would go and check says about this limiter.
2433    const fn where_to_look(self) -> &'static str {
2434        match self {
2435            Self::Primary => {
2436                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2437                 comes back."
2438            }
2439            Self::Secondary => {
2440                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2441                 primary budget and does not report this one, so budget showing there says \
2442                 nothing about this refusal, and every further attempt extends it."
2443            }
2444        }
2445    }
2446
2447    /// The next step this limiter actually calls for.
2448    const fn what_to_do(self) -> &'static str {
2449        match self {
2450            Self::Primary => {
2451                "wait for the reset `gh api rate_limit` reports, then run the command again."
2452            }
2453            Self::Secondary => {
2454                "leave this board alone for a few minutes, then run the command again — or \
2455                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2456            }
2457        }
2458    }
2459}
2460
2461/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2462#[derive(Debug, Clone, Copy)]
2463struct Limited {
2464    limiter: Limiter,
2465    hint: Option<u64>,
2466    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2467    /// `x-ratelimit-reset`.
2468    hinted_by: Hint,
2469}
2470
2471/// Where a refusal's hint came from, which is what its wait is named after.
2472#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2473enum Hint {
2474    /// `retry-after`, or no hint at all.
2475    RetryAfter,
2476    /// An exhausted primary budget's `x-ratelimit-reset`.
2477    PrimaryReset,
2478}
2479
2480impl Limited {
2481    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2482    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2483    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2484    /// extends a secondary limit — or the schedule alone when there is none.
2485    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2486        match (self.hint, self.hinted_by) {
2487            (Some(hint), Hint::RetryAfter) => (
2488                Duration::from_secs(hint).max(backoff),
2489                pacing::cause::RETRY_AFTER,
2490            ),
2491            (Some(hint), Hint::PrimaryReset) => (
2492                Duration::from_secs(hint).max(backoff),
2493                pacing::cause::PRIMARY_RESET,
2494            ),
2495            (None, _) => (backoff, pacing::cause::BACKOFF),
2496        }
2497    }
2498
2499    /// What the caller is told once this source has waited as long as it may.
2500    ///
2501    /// Both limiters report as [`SourceError::RateLimited`], because that is what
2502    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2503    /// about *which* limiter it was makes it a different kind of failure. What differs is
2504    /// the operator's next step, and that is what the message carries — a secondary
2505    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2506    /// budget looks fine, and then back to retry the very burst that was refused.
2507    fn exhausted(
2508        self,
2509        doing: &str,
2510        waits: u32,
2511        waited: Duration,
2512        needed: Duration,
2513        budget: Duration,
2514    ) -> SourceError {
2515        SourceError::RateLimited {
2516            retry_after_seconds: self.hint,
2517            message: Some(format!(
2518                "{} refused this source while {doing}; it waited {} out over {} and was refused \
2519                 again, and the next wait of {} would take it past the {} one call may spend \
2520                 waiting. {} next: {}",
2521                self.limiter.name(),
2522                plural(waits, "refusal"),
2523                seconds(waited),
2524                seconds(needed),
2525                seconds(budget),
2526                self.limiter.where_to_look(),
2527                self.limiter.what_to_do(),
2528            )),
2529        }
2530    }
2531}
2532
2533/// One HTTP attempt's result, with what its response said about the rate limit.
2534///
2535/// The two travel together so the record and the outcome are written from the same place:
2536/// what a response said about the budget is only readable while that response is in hand,
2537/// and what the attempt *meant* is only decidable once its body has been read.
2538struct Attempted {
2539    result: Result<Value, Attempt>,
2540    limits: accounting::RateLimit,
2541    /// GitHub's own reported cost for this call, for a document that asked for it.
2542    reported_cost: Option<u64>,
2543    response_outcome: accounting::ResponseOutcome,
2544}
2545
2546/// One attempt's outcome: an error to report, or a rate limit to wait out.
2547enum Attempt {
2548    Failed(SourceError),
2549    Limited(Limited),
2550}
2551
2552fn plural(count: u32, thing: &str) -> String {
2553    if count == 1 {
2554        format!("{count} {thing}")
2555    } else {
2556        format!("{count} {thing}s")
2557    }
2558}
2559
2560fn seconds(duration: Duration) -> String {
2561    format!("{:.1}s", duration.as_secs_f64())
2562}
2563
2564/// The member [`stamp_served`] writes onto every node of a response that says when it last
2565/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2566const SERVED_AT: &str = "onetaskgraphServedAt";
2567
2568/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2569/// node's time later knows the server's clock on the response that read it — which is what
2570/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2571fn stamp_served(data: &mut Value, served: &str) {
2572    match data {
2573        Value::Object(members) => {
2574            if members.contains_key("updatedAt") {
2575                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2576            }
2577            for member in members.values_mut() {
2578                stamp_served(member, served);
2579            }
2580        }
2581        Value::Array(items) => {
2582            for item in items {
2583                stamp_served(item, served);
2584            }
2585        }
2586        _ => {}
2587    }
2588}
2589
2590/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2591///
2592/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2593/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2594/// header, and neither is what makes a response a refusal — so the whole cost of one this
2595/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2596/// instead. Refusing the response over the header would turn a readable refusal into an
2597/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2598fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2599    value
2600        .and_then(|value| value.to_str().ok())
2601        .and_then(|value| value.trim().parse::<u64>().ok())
2602}
2603
2604/// A transport failure with every cause beneath it, outermost first.
2605///
2606/// The client's own text for a request it could not complete is `error sending request for
2607/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2608/// port range, a connection closed before its answer — is only in the chain of sources below
2609/// it. Those differ in what to do next, so all of them are kept.
2610pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2611    let mut text = error.to_string();
2612    let mut cause = error.source();
2613    while let Some(next) = cause {
2614        let said = next.to_string();
2615        if !text.ends_with(&said) {
2616            text = format!("{text}: {said}");
2617        }
2618        cause = next.source();
2619    }
2620    text
2621}
2622
2623/// Every mutation this source sends creates content — an issue, a board item, a field of
2624/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2625/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2626/// and what the keyword says are the same set. That is what makes the keyword a sound test
2627/// rather than a convenient one: counting an edit or a removal the limiter might not have
2628/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2629pub(crate) fn is_mutation(query: &str) -> bool {
2630    query.trim_start().starts_with("mutation")
2631}
2632
2633/// What this source was doing, for a diagnostic that has to say so.
2634///
2635/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2636/// a document added without a description is caught by that list's own gate instead of
2637/// falling through to the vague arm below.
2638fn operation_description(query: &str) -> &'static str {
2639    graphql::DOCUMENTS
2640        .iter()
2641        .find(|(document, _)| *document == query)
2642        .map_or("talking to GitHub", |(_, doing)| *doing)
2643}
2644
2645/// GitHub's published ceiling on content-generating requests in any one minute.
2646///
2647/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2648/// GitHub's number rather than this source's. This source paces at it over its window: a
2649/// content-creating request goes out at once while the minute's creations leave room for it,
2650/// and waits until they do otherwise — see the `pacing` module.
2651pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2652/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2653/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2654pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2655/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2656/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2657/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2658pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2659/// The secondary points one GraphQL request carrying a mutation is charged.
2660pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2661/// The secondary points one GraphQL request carrying no mutation is charged.
2662pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2663/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2664/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2665///
2666/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2667/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2668/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2669/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2670/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2671pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2672/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2673///
2674/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2675/// own advice for a secondary limit — wait, and wait longer each time — without spending
2676/// the first minute of a transient refusal doing nothing.
2677pub const RETRY_BACKOFF_MS: u64 = 1_000;
2678/// Total time one call may spend waiting out rate limits before it reports a failure.
2679///
2680/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2681/// short enough that a command an operator is watching returns. The bound is what makes
2682/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2683/// the limiter, not in a process nobody can tell from a wedged one.
2684pub const RETRY_BUDGET_MS: u64 = 120_000;
2685
2686fn default_token_env() -> String {
2687    "GH_PROJECTS_TOKEN".to_owned()
2688}
2689fn default_endpoint() -> String {
2690    "https://api.github.com/graphql".to_owned()
2691}
2692
2693/// The name of a `Status` single-select option on the board.
2694///
2695/// Validated on the way in rather than checked later, so a blank option name — which
2696/// nothing on a board can be — is a state this type cannot hold.
2697#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2698#[serde(try_from = "String")]
2699#[schemars(extend("minLength" = 1))]
2700pub struct ColumnName(String);
2701
2702impl ColumnName {
2703    /// The option name, as the board spells it.
2704    fn as_str(&self) -> &str {
2705        &self.0
2706    }
2707}
2708
2709impl TryFrom<String> for ColumnName {
2710    type Error = String;
2711
2712    fn try_from(name: String) -> Result<Self, Self::Error> {
2713        if name.trim().is_empty() {
2714            return Err("a status_mapping option name cannot be blank".to_owned());
2715        }
2716        Ok(Self(name))
2717    }
2718}
2719
2720/// The two closed states this product can mean.
2721///
2722/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2723/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2724/// product never decides on a person's behalf that two issues are the same work.
2725#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2726#[serde(rename_all = "kebab-case")]
2727pub enum ClosedState {
2728    /// `COMPLETED` — precisely done.
2729    Completed,
2730    /// `NOT_PLANNED` — precisely cancelled.
2731    NotPlanned,
2732}
2733
2734impl ClosedState {
2735    const fn reason(self) -> &'static str {
2736        match self {
2737            Self::Completed => "COMPLETED",
2738            Self::NotPlanned => "NOT_PLANNED",
2739        }
2740    }
2741}
2742
2743/// Configuration for one GitHub Projects v2 board.
2744///
2745/// It serializes to a document this same type reads back as itself, so a caller writing a
2746/// configuration — a consumer filing onto a board it configures — can build one here rather
2747/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2748#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2749#[serde(default, deny_unknown_fields)]
2750pub struct GitHubProjectsConfig {
2751    /// Login of the user or organization which owns the board.
2752    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2753    /// The project number shown in the board's GitHub URL.
2754    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2755    // llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This doc is the field's schema description, which is what a person configuring the source reads, so it has to say when the field decides an issue's repository and when the item's own field does; the rule's one executable source is `GitHubProjectsSource::creation_target`, and `tests/plugin.rs` drives each case named here against the loopback board.
2756    /// `owner/name` of the repository this source creates an issue in when the item's own
2757    /// `repositories` field does not decide it.
2758    ///
2759    /// An item naming exactly one repository is created there — on a source declared
2760    /// `visibility: private`, only when that repository reads private, and otherwise as an
2761    /// item naming none is; a task or a document naming none or several is created in its
2762    /// parent project's repository; and a project, or a task or document with no parent,
2763    /// naming none or several is created here. A board
2764    /// has no repository of its own and `createIssue` requires one, so a write without
2765    /// this is refused naming the field. Reads never need it.
2766    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2767    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2768    /// Environment variable containing a fine-grained token with Projects and Issues
2769    /// read/write plus Pull requests read-only access for every repository represented on
2770    /// the board.
2771    #[serde(default = "default_token_env")]
2772    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2773    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2774    #[serde(default = "default_endpoint")]
2775    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2776    /// Per-instance mapping from a status category to the option of the board's one
2777    /// `Status` field it lands on, for a task and for a project.
2778    ///
2779    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2780    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2781    /// where a kind left out leaves the category unmapped for that kind. A category this
2782    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2783    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2784    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2785    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2786    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2787    /// either kind. No two categories may name one option for the same kind, ignoring case.
2788    /// `unknown` may name one existing option; every unknown word then lands on it and
2789    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2790    /// each unknown word because it never creates board options.
2791    #[serde(default)]
2792    pub status_mapping: StatusMapping,
2793    /// Per-instance mapping from a task's priority to an option of this board's
2794    /// single-select field named `Priority`.
2795    ///
2796    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2797    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2798    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2799    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2800    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2801    /// no two levels may name one option. Reads and writes never create the field or an
2802    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2803    /// the board lacks is refused pointing there.
2804    #[serde(default)]
2805    pub priority_mapping: Option<PriorityMappingConfig>,
2806    /// Caller metadata values this source also writes to board text fields, so the board can
2807    /// filter on them.
2808    ///
2809    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2810    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2811    /// value. On every item write — create, update and copy, of a task, a project or a
2812    /// document — a string found there is written to the field, and nothing is sent when the
2813    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2814    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2815    /// issue body stays the one home of the value, and every read reports metadata from it.
2816    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2817    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2818    /// non-text field of that name, is refused pointing there.
2819    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2820    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2821    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2822    #[schemars(!skip_serializing_if)]
2823    pub metadata_fields: Vec<MetadataFieldConfig>,
2824    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2825    ///
2826    /// Every field keeps its shipped default when it is absent, and the defaults are
2827    /// GitHub's own published limits rather than taste. See [`Pacing`].
2828    #[serde(default)]
2829    pub pacing: PacingConfig,
2830}
2831
2832/// Which option of the board's `Priority` field each priority lands on.
2833///
2834/// One member per level rather than a map, so a key that is not a level is refused where
2835/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2836/// value in the field, not an option of it.
2837#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2838#[serde(default, deny_unknown_fields)]
2839pub struct PriorityMappingConfig {
2840    /// The option `urgent` lands on; `Urgent` when absent.
2841    pub urgent: Option<PriorityOptionName>,
2842    /// The option `high` lands on; `High` when absent.
2843    pub high: Option<PriorityOptionName>,
2844    /// The option `medium` lands on; `Medium` when absent.
2845    pub medium: Option<PriorityOptionName>,
2846    /// The option `low` lands on; `Low` when absent.
2847    pub low: Option<PriorityOptionName>,
2848}
2849
2850/// The name of an option of the board's `Priority` single-select field.
2851///
2852/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2853/// blank name.
2854#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2855#[serde(try_from = "String")]
2856#[schemars(extend("minLength" = 1))]
2857pub struct PriorityOptionName(String);
2858
2859impl PriorityOptionName {
2860    /// The option name, as the board spells it.
2861    fn as_str(&self) -> &str {
2862        &self.0
2863    }
2864}
2865
2866impl TryFrom<String> for PriorityOptionName {
2867    type Error = String;
2868
2869    fn try_from(name: String) -> Result<Self, Self::Error> {
2870        if name.trim().is_empty() {
2871            return Err("a priority_mapping option name cannot be blank".to_owned());
2872        }
2873        Ok(Self(name))
2874    }
2875}
2876
2877/// The name of the board field a priority is held in.
2878pub const PRIORITY_FIELD: &str = "Priority";
2879
2880/// The four priorities a board option can hold, in the order a new `Priority` field lists
2881/// them. `none` is not among them: it is the field holding no value.
2882///
2883/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2884/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2885/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2886/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2887pub const PRIORITY_LEVELS: [Priority; 4] = [
2888    Priority::Urgent,
2889    Priority::High,
2890    Priority::Medium,
2891    Priority::Low,
2892];
2893
2894/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2895/// see that list for what this pins.
2896#[must_use]
2897pub const fn level_position(priority: Priority) -> Option<usize> {
2898    match priority {
2899        Priority::None => None,
2900        Priority::Urgent => Some(0),
2901        Priority::High => Some(1),
2902        Priority::Medium => Some(2),
2903        Priority::Low => Some(3),
2904    }
2905}
2906
2907/// This instance's complete priority-to-option mapping, read in both directions.
2908///
2909/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2910/// two levels name one option.
2911#[derive(Debug, Clone)]
2912struct PriorityMapping {
2913    options: [PriorityOptionName; 4],
2914}
2915
2916impl PriorityMapping {
2917    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2918        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2919        let mapping = Self {
2920            options: [
2921                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2922                config.high.unwrap_or_else(|| shipped("High")),
2923                config.medium.unwrap_or_else(|| shipped("Medium")),
2924                config.low.unwrap_or_else(|| shipped("Low")),
2925            ],
2926        };
2927        for (index, option) in mapping.options.iter().enumerate() {
2928            if let Some(earlier) = mapping.options[..index]
2929                .iter()
2930                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2931            {
2932                return Err(SourceError::Config {
2933                    message: format!(
2934                        "priority_mapping of source {instance} sends both {} and {} to the board \
2935                         option {:?}; one option cannot read back as two priorities",
2936                        PRIORITY_LEVELS[earlier],
2937                        PRIORITY_LEVELS[index],
2938                        option.as_str()
2939                    ),
2940                });
2941            }
2942        }
2943        Ok(mapping)
2944    }
2945
2946    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2947    fn option(&self, priority: Priority) -> Option<&str> {
2948        level_position(priority).map(|index| self.options[index].as_str())
2949    }
2950
2951    /// The priority a board option name reports, or `None` when nothing maps to it.
2952    fn priority_of(&self, option: &str) -> Option<Priority> {
2953        self.options
2954            .iter()
2955            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2956            .map(|index| PRIORITY_LEVELS[index])
2957    }
2958
2959    /// Every mapped option name, in the order a new `Priority` field lists them.
2960    fn names(&self) -> impl Iterator<Item = &str> {
2961        self.options.iter().map(PriorityOptionName::as_str)
2962    }
2963}
2964
2965/// One metadata value this source projects onto a board text field.
2966#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2967#[serde(deny_unknown_fields)]
2968pub struct MetadataFieldConfig {
2969    /// The name of the board text field the value is written to.
2970    pub field: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `MetadataField::resolve` refuses a blank, reserved, GitHub-owned or repeated name before the private validated entry is built.
2971    /// The top-level metadata key the value is read from. A key under the reserved
2972    /// `onetaskgraph.` prefix is refused.
2973    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `MetadataField::resolve` refuses a blank or reserved key before the private validated entry is built.
2974    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2975    /// key's own value is the one projected. Left out of a serialized entry when empty.
2976    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2977    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2978    #[schemars(!skip_serializing_if)]
2979    pub path: Vec<String>,
2980}
2981
2982/// The board fields no metadata value may be projected onto, compared ignoring case: the
2983/// three this source writes itself, and those GitHub owns on every board.
2984// llmlint: ignore[contracts_have_one_source_or_a_drift_gate] This list is the configuration contract `metadata_fields`' consumers agreed, refused at read so a mistaken entry is named early — not a mirror GitHub's field set has to stay in step with. What keeps a write off a field GitHub owns is the write's own check, `projection_field`, which refuses any field whose `dataType` is not `TEXT`: every built-in board field reports its own type (TITLE, ASSIGNEES, LABELS, …), and the live contract introspection holds GitHub to `ProjectV2Field.dataType`. So a built-in field GitHub adds later and this list lacks is still never written.
2985const UNPROJECTABLE_FIELDS: [&str; 13] = [
2986    STATUS_FIELD,
2987    PRIORITY_FIELD,
2988    ORIGIN_FIELD,
2989    "Title",
2990    "Assignees",
2991    "Labels",
2992    "Linked pull requests",
2993    "Milestone",
2994    "Repository",
2995    "Reviewers",
2996    "Parent issue",
2997    "Sub-issues progress",
2998    "Type",
2999];
3000
3001/// One validated [`MetadataFieldConfig`] entry.
3002#[derive(Debug, Clone)]
3003struct MetadataField {
3004    field: ProjectedField,
3005    key: ProjectedKey,
3006    path: Vec<String>,
3007}
3008
3009/// The name of a board text field a metadata value is projected onto: never blank, never one
3010/// this source or GitHub writes, and never one another entry of the same source names.
3011#[derive(Debug, Clone, PartialEq, Eq)]
3012struct ProjectedField(String);
3013
3014/// The top-level metadata key a projected value is read from: never blank, and never under
3015/// the namespace this product reserves.
3016#[derive(Debug, Clone, PartialEq, Eq)]
3017struct ProjectedKey(String);
3018
3019impl std::ops::Deref for ProjectedField {
3020    type Target = str;
3021    fn deref(&self) -> &str {
3022        &self.0
3023    }
3024}
3025
3026impl std::ops::Deref for ProjectedKey {
3027    type Target = str;
3028    fn deref(&self) -> &str {
3029        &self.0
3030    }
3031}
3032
3033/// What one item's metadata says one projected field should hold.
3034#[derive(Debug, Clone, PartialEq, Eq)]
3035enum Projected {
3036    /// A string to hold.
3037    Text(String),
3038    /// Nothing: the key or a step of the path is absent, or the value is `null`.
3039    Nothing,
3040}
3041
3042impl MetadataField {
3043    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3044    /// cannot stand, naming the source and the entry.
3045    fn resolve(
3046        configured: Vec<MetadataFieldConfig>,
3047        instance: &SourceName,
3048    ) -> Result<Vec<Self>, SourceError> {
3049        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3050        for (index, entry) in configured.into_iter().enumerate() {
3051            let refuse = |why: String| SourceError::Config {
3052                message: format!(
3053                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3054                    entry.field, entry.key
3055                ),
3056            };
3057            if entry.field.trim().is_empty() {
3058                return Err(refuse("names a blank field".to_owned()));
3059            }
3060            if entry.key.trim().is_empty() {
3061                return Err(refuse("names a blank key".to_owned()));
3062            }
3063            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3064                return Err(refuse(format!(
3065                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
3066                     for its own keys; next: project a caller key instead",
3067                    MetadataKey::RESERVED_NAMESPACE
3068                )));
3069            }
3070            if let Some(owned) = UNPROJECTABLE_FIELDS
3071                .iter()
3072                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3073            {
3074                return Err(refuse(format!(
3075                    "names the board field {owned:?}, which this source or GitHub already \
3076                     writes; next: name a text field of your own"
3077                )));
3078            }
3079            if let Some(earlier) = resolved
3080                .iter()
3081                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3082            {
3083                return Err(refuse(format!(
3084                    "names the same board field as metadata_fields[{earlier}]; one field \
3085                     cannot hold two values"
3086                )));
3087            }
3088            resolved.push(Self {
3089                field: ProjectedField(entry.field),
3090                key: ProjectedKey(entry.key),
3091                path: entry.path,
3092            });
3093        }
3094        Ok(resolved)
3095    }
3096
3097    /// The path as a refusal spells it: `[]` for the key's own value.
3098    fn spelled_path(&self) -> String {
3099        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3100    }
3101
3102    /// What `metadata` says this field should hold, or the refusal for a value no text field
3103    /// can hold.
3104    fn projected(
3105        &self,
3106        metadata: &BTreeMap<String, Value>,
3107        instance: &SourceName,
3108    ) -> Result<Projected, SourceError> {
3109        let mut value = metadata.get(&*self.key);
3110        for step in &self.path {
3111            value = value.and_then(|held| held.get(step.as_str()));
3112        }
3113        let found = match value {
3114            None | Some(Value::Null) => return Ok(Projected::Nothing),
3115            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3116            Some(Value::Bool(_)) => "a boolean",
3117            Some(Value::Number(_)) => "a number",
3118            Some(Value::Array(_)) => "an array",
3119            Some(Value::Object(_)) => "an object",
3120        };
3121        Err(SourceError::Refused {
3122            message: format!(
3123                "source {instance} projects metadata key {:?} at path {} onto its board text \
3124                 field {:?}, and this item holds {found} there; only a string, null or nothing \
3125                 can be written to a text field; next: store a string there, or remove the \
3126                 value",
3127                &*self.key,
3128                self.spelled_path(),
3129                &*self.field
3130            ),
3131        })
3132    }
3133}
3134
3135/// What one item's `Priority` field says, read through this instance's mapping.
3136#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3137enum HeldPriority {
3138    /// A priority this source reports: an option the mapping names, or no value (`none`).
3139    Read(Priority),
3140    /// An option the mapping does not name, which is never read as a level or as `none`.
3141    Unmapped(String),
3142}
3143
3144/// How fast this source writes, and how long it waits out a rate-limit refusal.
3145///
3146/// Configurable because a GitHub Enterprise installation sets its own limits and an
3147/// operator who has already been refused may want to go slower still — not because the
3148/// defaults are guesses.
3149#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3150#[serde(default, deny_unknown_fields)]
3151pub struct PacingConfig {
3152    /// Shortest interval between two content-creating mutations, in milliseconds.
3153    ///
3154    /// Zero sends them as fast as they are asked for, which is what a fixture server on
3155    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3156    pub min_mutation_interval_ms: Option<u64>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `Pacing::resolve` bounds it to `MAX_PACING_MS` before the private validated `Pacing` is built.
3157    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3158    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3159    /// zero while there is a budget to spend, because a schedule of zero-length waits
3160    /// consumes none of it and so never ends.
3161    pub retry_backoff_ms: Option<u64>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `Pacing::resolve` refuses a non-progressing zero and bounds the rest before the private validated `Pacing` is built.
3162    /// Total time one call may spend waiting out rate limits, in milliseconds.
3163    ///
3164    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3165    /// the bound is what makes this a wait rather than a hang.
3166    pub retry_budget_ms: Option<u64>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `Pacing::resolve` bounds it to `MAX_PACING_MS` before the private validated `Pacing` is built.
3167}
3168
3169/// The largest any pacing setting may be, in milliseconds.
3170///
3171/// One hour. GitHub's own harshest published bound on content-generating requests works
3172/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3173/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3174/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3175/// and an interval beyond it is a command that never sends its second mutation. It also
3176/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3177/// what a `Duration` can hold on every platform.
3178pub const MAX_PACING_MS: u64 = 3_600_000;
3179
3180/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3181/// source holds.
3182///
3183/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3184/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3185/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3186/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3187/// is asked for, windows included.
3188#[derive(Debug, Clone, Copy)]
3189struct Pacing {
3190    /// Whether requests are held for GitHub's secondary windows: always, unless
3191    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3192    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3193    windows: bool,
3194    min_mutation_interval: Duration,
3195    retry_backoff: Duration,
3196    retry_budget: Duration,
3197}
3198
3199impl Pacing {
3200    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3201    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3202        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3203            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3204                message: format!(
3205                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3206                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3207                     GitHub's own harshest published limit"
3208                ),
3209            }),
3210            Some(value) => Ok(Duration::from_millis(value)),
3211            None => Ok(Duration::from_millis(default)),
3212        };
3213        let retry_backoff = bounded(
3214            config.retry_backoff_ms,
3215            RETRY_BACKOFF_MS,
3216            "retry_backoff_ms",
3217        )?;
3218        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3219        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3220            return Err(SourceError::Config {
3221                message: format!(
3222                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3223                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3224                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3225                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3226                     waiting at all",
3227                    retry_budget.as_millis()
3228                ),
3229            });
3230        }
3231        Ok(Self {
3232            windows: config.min_mutation_interval_ms != Some(0),
3233            min_mutation_interval: bounded(
3234                config.min_mutation_interval_ms,
3235                0,
3236                "min_mutation_interval_ms",
3237            )?,
3238            retry_backoff,
3239            retry_budget,
3240        })
3241    }
3242}
3243
3244/// Factory for [`GitHubProjectsSource`].
3245#[derive(Debug, Clone, Copy, Default)]
3246pub struct Plugin;
3247
3248impl SourcePlugin for Plugin {
3249    fn kind(&self) -> &'static str {
3250        KIND
3251    }
3252    /// The endpoint, the owner and the board's number: what names one board.
3253    fn remote_identity(&self, config: &Value) -> Option<String> {
3254        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3255        Some(format!(
3256            "{}\n{}\n{}",
3257            config.endpoint, config.owner, config.project_number
3258        ))
3259    }
3260    fn config_schema(&self) -> Schema {
3261        schema_for!(GitHubProjectsConfig)
3262    }
3263    fn build(
3264        &self,
3265        name: &SourceName,
3266        config: &Value,
3267        secrets: &dyn SecretResolver,
3268    ) -> Result<Box<dyn TaskSource>, SourceError> {
3269        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3270    }
3271
3272    fn build_with_clock(
3273        &self,
3274        name: &SourceName,
3275        config: &Value,
3276        secrets: &dyn SecretResolver,
3277        clock: SharedClock,
3278    ) -> Result<Box<dyn TaskSource>, SourceError> {
3279        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3280    }
3281}
3282
3283/// The client `cell` holds, building it with `build` the first time it is asked for.
3284///
3285/// A failed build is the request's failure, with the message the source's construction gave it
3286/// before clients were deferred.
3287// llmlint: ignore[changed_behavior_has_e2e] A client build fails only when the TLS backend cannot initialise, which nothing outside the process can induce: a certificate bundle of garbage still builds one. That the build is deferred is proven end to end by `store_startup::a_command_builds_no_client_for_a_source_it_sends_nothing`.
3288fn built(
3289    cell: &std::sync::OnceLock<Client>,
3290    build: impl FnOnce() -> Result<Client, SourceError>,
3291) -> Result<&Client, SourceError> {
3292    if let Some(client) = cell.get() {
3293        return Ok(client);
3294    }
3295    let client = build()?;
3296    Ok(cell.get_or_init(|| client))
3297}
3298
3299impl Plugin {
3300    /// Build a source recording every request it sends into an accounting the caller holds.
3301    ///
3302    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3303    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3304    /// session total rather than two — see [`accounting`] and
3305    /// [`GitHubProjectsSource::recording_into`].
3306    ///
3307    /// # Errors
3308    ///
3309    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3310    /// [`SourceError::Config`] for configuration this plugin cannot use and
3311    /// [`SourceError::Auth`] for a credential it cannot find.
3312    pub fn build_recording_into(
3313        &self,
3314        name: &SourceName,
3315        config: &Value,
3316        secrets: &dyn SecretResolver,
3317        ledger: Arc<Accounting>,
3318    ) -> Result<Box<dyn TaskSource>, SourceError> {
3319        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3320    }
3321
3322    fn build_recording_with_clock(
3323        &self,
3324        name: &SourceName,
3325        config: &Value,
3326        secrets: &dyn SecretResolver,
3327        ledger: Arc<Accounting>,
3328        clock: SharedClock,
3329    ) -> Result<Box<dyn TaskSource>, SourceError> {
3330        let config: GitHubProjectsConfig =
3331            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3332                message: format!("source {name}: {e}"),
3333            })?;
3334        let prefix = format!("source {name}: ");
3335        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3336            .map_err(|error| match error {
3337                // The shared `StatusMapping::distinct` names the source itself.
3338                SourceError::Config { message } if message.starts_with(&prefix) => {
3339                    SourceError::Config { message }
3340                }
3341                SourceError::Config { message } => SourceError::Config {
3342                    message: format!("{prefix}{message}"),
3343                },
3344                SourceError::Auth { message } => SourceError::Auth {
3345                    message: format!("source {name}: {message}"),
3346                },
3347                other => other,
3348            })?;
3349        source.clock = clock;
3350        Ok(Box::new(source))
3351    }
3352}
3353
3354/// Where a status category lands on this board, once configuration is resolved.
3355#[derive(Debug, Clone, PartialEq, Eq)]
3356enum StatusTarget {
3357    /// Not usable against this instance for this kind, and why.
3358    Disabled(UnmappedStatus),
3359    /// The board's `Status` option of this name.
3360    Column(ColumnName),
3361    /// A closed issue, with both its board option and the reason that says which closed it means.
3362    // llmlint: ignore[invalid_states_unrepresentable] The reason is fixed by the category — `done` closes as completed, `cancelled` as not planned — and this private enum is built in one place, `BoardStatuses::resolve`, which pairs each from the category's own slot. Carrying the reason on the target is what lets every write site that holds only a target derive its `stateInput` from that one resolved model rather than re-deriving it from a category and risking a disagreement with the mapping.
3363    Terminal(ColumnName, ClosedState),
3364}
3365
3366/// Every status category, in the order the vocabulary declares them.
3367///
3368/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3369/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3370/// added to the shared vocabulary fails to compile until it is named there, and this
3371/// crate's suite reconciles this list against that enum's own derived schema, which is
3372/// generated from the variants rather than written beside them. The schema is what
3373/// catches a list left one short — a list checking only the positions it already holds
3374/// would pass while every mapping indexed by the new position panicked.
3375pub const CATEGORIES: [StatusCategory; 8] = [
3376    StatusCategory::Draft,
3377    StatusCategory::Backlog,
3378    StatusCategory::Todo,
3379    StatusCategory::Queued,
3380    StatusCategory::InProgress,
3381    StatusCategory::Done,
3382    StatusCategory::Cancelled,
3383    StatusCategory::Unknown,
3384];
3385
3386/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3387#[must_use]
3388pub const fn category_position(category: StatusCategory) -> usize {
3389    match category {
3390        StatusCategory::Draft => 0,
3391        StatusCategory::Backlog => 1,
3392        StatusCategory::Todo => 2,
3393        StatusCategory::Queued => 3,
3394        StatusCategory::InProgress => 4,
3395        StatusCategory::Done => 5,
3396        StatusCategory::Cancelled => 6,
3397        StatusCategory::Unknown => 7,
3398    }
3399}
3400
3401/// The spelling a status category is configured and reported under.
3402fn category_name(category: StatusCategory) -> &'static str {
3403    match category {
3404        StatusCategory::Draft => "draft",
3405        StatusCategory::Backlog => "backlog",
3406        StatusCategory::Todo => "todo",
3407        StatusCategory::Queued => "queued",
3408        StatusCategory::InProgress => "in-progress",
3409        StatusCategory::Done => "done",
3410        StatusCategory::Cancelled => "cancelled",
3411        StatusCategory::Unknown => "unknown",
3412    }
3413}
3414
3415/// A shipped default's option name.
3416///
3417/// The literals below are this file's own and non-blank, and they are validated by the
3418/// one constructor a configured name goes through rather than beside it.
3419fn shipped_column(name: &'static str) -> ColumnName {
3420    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3421}
3422
3423/// The shipped default for one category this instance's `status_mapping` does not mention,
3424/// for either kind.
3425fn shipped_default(category: StatusCategory) -> StatusTarget {
3426    match category {
3427        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3428        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3429        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3430        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3431        StatusCategory::Done => {
3432            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3433        }
3434        StatusCategory::Cancelled => {
3435            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3436        }
3437        StatusCategory::Draft | StatusCategory::Unknown => {
3438            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3439        }
3440    }
3441}
3442
3443/// The two kinds a status is written and read for, each with its own half of the mapping.
3444const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3445
3446/// This instance's complete category-to-target mapping for each kind, read in both
3447/// directions.
3448///
3449/// One target per category per kind, held at that category's own [`category_position`], so
3450/// a category missing from the mapping, named twice in it, or filed out of order is a state
3451/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3452/// targets are options of the board's one `Status` field.
3453#[derive(Debug, Clone)]
3454struct BoardStatuses {
3455    tasks: [StatusTarget; CATEGORIES.len()],
3456    projects: [StatusTarget; CATEGORIES.len()],
3457}
3458
3459impl BoardStatuses {
3460    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3461    /// would read back from one option.
3462    ///
3463    /// A category the mapping does not mention keeps its shipped default for both kinds; one
3464    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3465    /// omits unmapped rather than defaulted.
3466    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3467        let resolve_kind =
3468            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3469                // `CATEGORIES[position] == category` for every category — the crate's suite
3470                // asserts it — so mapping the list in order fills each category's own slot.
3471                let mut targets = CATEGORIES.map(shipped_default);
3472                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3473                    if !configured.mentions(category) {
3474                        continue;
3475                    }
3476                    *slot = match configured.name_for(category, kind) {
3477                        Err(why) => StatusTarget::Disabled(why),
3478                        Ok(name) => {
3479                            let option = ColumnName::try_from(name.as_str().to_owned())
3480                                .map_err(|message| SourceError::Config { message })?;
3481                            match category {
3482                                StatusCategory::Done => {
3483                                    StatusTarget::Terminal(option, ClosedState::Completed)
3484                                }
3485                                StatusCategory::Cancelled => {
3486                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
3487                                }
3488                                _ => StatusTarget::Column(option),
3489                            }
3490                        }
3491                    };
3492                }
3493                StatusMapping::distinct(
3494                    instance,
3495                    kind,
3496                    CATEGORIES
3497                        .iter()
3498                        .zip(&targets)
3499                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3500                )?;
3501                Ok(targets)
3502            };
3503        Ok(Self {
3504            tasks: resolve_kind(ItemKind::Task)?,
3505            projects: resolve_kind(ItemKind::Project)?,
3506        })
3507    }
3508
3509    /// Every category's target for `kind`, in category order.
3510    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3511        match kind {
3512            ItemKind::Task => &self.tasks,
3513            ItemKind::Project => &self.projects,
3514        }
3515    }
3516
3517    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3518        &self.targets(kind)[category_position(category)]
3519    }
3520
3521    /// The category a board option name reports for `kind`, or `None` when nothing of that
3522    /// kind maps to it.
3523    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3524        CATEGORIES.into_iter().find(|category| {
3525            self.target(kind, *category)
3526                .option()
3527                .is_some_and(|name| name.eq_ignore_ascii_case(option))
3528        })
3529    }
3530
3531    /// Every option name either kind maps a category to, each once ignoring case, in
3532    /// category order with a task's name before a project's — what the guarded setup asks
3533    /// the `Status` field to hold.
3534    fn wanted(&self) -> Vec<String> {
3535        let mut wanted: Vec<String> = Vec::new();
3536        for category in CATEGORIES {
3537            for kind in STATUS_KINDS {
3538                if let Some(name) = self.target(kind, category).option()
3539                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3540                {
3541                    wanted.push(name.to_owned());
3542                }
3543            }
3544        }
3545        wanted
3546    }
3547
3548    /// The status an item of `kind` reports, from the three things a read of it says: its
3549    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3550    ///
3551    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3552    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3553    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3554    /// produce, so it is read permissively rather than refused — reads are faithful, and
3555    /// refusals belong on writes.
3556    ///
3557    /// **Its name follows the closure too, unless the option agrees with it.** The option's
3558    /// own name is kept when this kind's mapping reads that option as the same category, or
3559    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3560    /// named `Shipped`. An option the mapping reads as another category contradicts the
3561    /// closure, and so does no option at all: the name is then the option the mapping gives
3562    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3563    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3564    /// it. An `unknown` closure keeps the option, or `Closed` without one.
3565    ///
3566    /// An open item's option reads through its own kind's mapping, and an option that mapping
3567    /// does not name reads as `Unknown` under its own name.
3568    ///
3569    /// One function of those three rather than of a response, so a narrow status write can
3570    /// answer what a re-read would report by applying it to the state it has just written.
3571    fn status(
3572        &self,
3573        kind: ItemKind,
3574        option: Option<&str>,
3575        closed: bool,
3576        reason: Option<&str>,
3577    ) -> Status {
3578        if closed {
3579            let category = closed_category(reason);
3580            let agrees = option.filter(|option| {
3581                category == StatusCategory::Unknown
3582                    || self
3583                        .category_of(kind, option)
3584                        .is_none_or(|read| read == category)
3585            });
3586            let name = agrees.or_else(|| self.target(kind, category).option());
3587            let fallback = match category {
3588                StatusCategory::Done => "Done",
3589                StatusCategory::Cancelled => "Cancelled",
3590                _ => "Closed",
3591            };
3592            return Status {
3593                category,
3594                name: name.or(option).unwrap_or(fallback).to_owned(),
3595            };
3596        }
3597        let name = option.unwrap_or("Open").to_owned();
3598        Status {
3599            category: self
3600                .category_of(kind, &name)
3601                .unwrap_or(StatusCategory::Unknown),
3602            name,
3603        }
3604    }
3605
3606    /// The option a closed item's `Status` has to hold for its card to agree with its
3607    /// closure, and the category that closure reads as — or `None` when the item is open,
3608    /// its closure reads `unknown`, or its option already agrees.
3609    ///
3610    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3611    /// in line, each to the one option the mapping gives its category — an item with no
3612    /// option, or at one the mapping does not name, included.
3613    fn contradicted(
3614        &self,
3615        kind: ItemKind,
3616        option: Option<&str>,
3617        closed: bool,
3618        reason: Option<&str>,
3619    ) -> Option<(ClosedAs, &str)> {
3620        if !closed {
3621            return None;
3622        }
3623        let closed_as = match closed_category(reason) {
3624            StatusCategory::Done => ClosedAs::Done,
3625            StatusCategory::Cancelled => ClosedAs::Cancelled,
3626            _ => return None,
3627        };
3628        let wanted = self.target(kind, closed_as.category()).option()?;
3629        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3630            return None;
3631        }
3632        Some((closed_as, wanted))
3633    }
3634}
3635
3636/// The category a closed issue reads as, from the reason it was closed with.
3637///
3638/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3639/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3640/// is one this source cannot read a category from.
3641fn closed_category(reason: Option<&str>) -> StatusCategory {
3642    match reason {
3643        None | Some("COMPLETED") => StatusCategory::Done,
3644        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3645        Some(_) => StatusCategory::Unknown,
3646    }
3647}
3648
3649impl BoardStatuses {
3650    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3651    /// case; a kind lacking none is left out.
3652    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3653        STATUS_KINDS
3654            .into_iter()
3655            .filter_map(|kind| {
3656                let missing: Vec<String> = self
3657                    .targets(kind)
3658                    .iter()
3659                    .filter_map(StatusTarget::option)
3660                    .filter(|wanted| {
3661                        !existing
3662                            .iter()
3663                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3664                    })
3665                    .map(str::to_owned)
3666                    .collect();
3667                (!missing.is_empty()).then_some(KindMissing { kind, missing })
3668            })
3669            .collect()
3670    }
3671}
3672
3673impl StatusTarget {
3674    /// The board option this target selects, or `None` for an unmapped one.
3675    fn option(&self) -> Option<&str> {
3676        match self {
3677            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3678            Self::Disabled(_) => None,
3679        }
3680    }
3681}
3682
3683// llmlint: ignore-block[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate] Every `createIssue` names one of these, and which one is the rule — a reader who reaches the type from `create_and_file_issue` gets the rule in one sentence here without the method's refusals, which stay on `creation_target`, the rule's one executable source; `tests/plugin.rs` drives every arm of it against the loopback board.
3684/// One repository this source can create an issue in, as `owner/name`.
3685///
3686/// Every `createIssue` this source sends names one of these: the item's own single
3687/// `repositories` entry — on a source declared private, only when it reads private — else
3688/// its parent project issue's repository, else the configured
3689/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3690/// that choice and says what it refuses before `createIssue`.
3691// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3692#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3693struct RepositoryTarget {
3694    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3695    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3696}
3697
3698impl RepositoryTarget {
3699    fn parse(value: &str) -> Result<Self, SourceError> {
3700        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3701            message: format!(
3702                "repository must be spelled owner/name; {value:?} names no repository"
3703            ),
3704        })?;
3705        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3706            return Err(SourceError::Config {
3707                message: format!(
3708                    "repository must be spelled owner/name with a GitHub login and one \
3709                     repository name; {value:?} is not"
3710                ),
3711            });
3712        }
3713        Ok(Self {
3714            owner: owner.to_owned(),
3715            name: name.to_owned(),
3716        })
3717    }
3718
3719    /// The one host whose repositories this source creates issues in, spelled once: it is
3720    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3721    const HOST: &str = "github.com";
3722
3723    fn origin(&self) -> String {
3724        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3725    }
3726
3727    /// The repository a normalized origin names, or why it is none this source can create
3728    /// an issue in: another host, or more or fewer than `owner/name` under this one.
3729    fn from_origin(origin: &Repository) -> Result<Self, String> {
3730        let not_here = || {
3731            format!(
3732                "{} is not a {}/owner/name repository",
3733                origin.as_str(),
3734                Self::HOST
3735            )
3736        };
3737        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3738        if host != Self::HOST {
3739            return Err(not_here());
3740        }
3741        Self::parse(rest).map_err(|_| not_here())
3742    }
3743
3744    fn slug(&self) -> String {
3745        format!("{}/{}", self.owner, self.name)
3746    }
3747}
3748
3749/// A source which reads GitHub afresh for every operation.
3750pub struct GitHubProjectsSource {
3751    /// This source's configured name, used both to tell a far end naming this source
3752    /// from one naming a system it knows nothing about, and to name the instance a
3753    /// status refusal is about.
3754    name: SourceName,
3755    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3756    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3757    repository: Option<RepositoryTarget>,
3758    endpoint: Url,
3759    token: SecretString,
3760    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3761    statuses: BoardStatuses,
3762    /// Where each priority lands on this board, or `None` when this instance holds none.
3763    priorities: Option<PriorityMapping>,
3764    /// The metadata values this instance projects onto board text fields, in configured order.
3765    metadata_fields: Vec<MetadataField>,
3766    /// Built on the first request rather than with the source, as [`Self::client`] says.
3767    client: std::sync::OnceLock<Client>,
3768    /// Built on the first attachment request, as [`Self::asset_client`] says.
3769    asset_client: std::sync::OnceLock<Client>,
3770    /// Every item this source has created in this command, in the order it created them —
3771    /// dropped by [`TaskSource::end_command`].
3772    ///
3773    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3774    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3775    /// a copy resolving a dependency on an item it had just created refused it as not
3776    /// found. A board read is completed from this — an item remembered here and absent from
3777    /// the read is added back, because the board really does hold it and only the read is
3778    /// behind.
3779    ///
3780    /// It is not a cache of a user's work: nothing is remembered that this process did not
3781    /// itself just write, it lives and dies with the process, and it is never consulted for
3782    /// an item this source did not create.
3783    created: Mutex<Vec<Resolved>>,
3784    /// Every item that already existed and that this source has written in this command, as
3785    /// it wrote it — dropped by [`TaskSource::end_command`].
3786    ///
3787    /// The other half of [`Self::created`], held on the same terms and for the reason a
3788    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3789    /// filter is an index behind a write this process made moments ago, so a query matching
3790    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3791    /// is remembered that this process did not itself just write.
3792    updated: Mutex<Vec<Resolved>>,
3793    /// Every issue this source has added a comment to or edited a comment of in this command
3794    /// — dropped by [`TaskSource::end_command`].
3795    ///
3796    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3797    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3798    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3799    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3800    /// from before — until the index caught up. Each id here is a candidate of every such
3801    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3802    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3803    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3804    /// process wrote is still found only once the index has it.
3805    commented: Mutex<Vec<NativeId>>,
3806    /// How fast this source writes, and how long it waits out a refusal.
3807    pacing: Pacing,
3808    /// When the last content-creating mutation finished, or the moment the furthest-out
3809    /// reserved slot releases the next one, whichever is later — so the one after it can be
3810    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3811    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3812    /// measured from.
3813    last_mutation: Mutex<Option<Duration>>,
3814    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3815    windows: Mutex<pacing::Windows>,
3816    /// The tasks a project holds, as a copy's batched read of the project and every one of them
3817    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3818    /// a listing of that project's tasks is answered without a read of its own.
3819    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3820    /// The web address of each issue this source created in the current command, as its
3821    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3822    /// request.
3823    written: Mutex<BTreeMap<NativeId, String>>,
3824    /// What a copy has held back of the items it created, between
3825    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3826    /// nothing is being held back, when every write lands whole as it is made.
3827    deferred: Mutex<Option<Deferred>>,
3828    /// Each existing issue's update held back while writes are, in the order it was made: sent
3829    /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3830    /// this source sends or reads ever sees the board without them.
3831    held_updates: Mutex<Vec<HeldUpdate>>,
3832    clock: SharedClock,
3833    /// Whether the configuration declares this source private, as
3834    /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
3835    /// named repository reads public is created — see [`Self::creation_rule`].
3836    declared_private: std::sync::atomic::AtomicBool,
3837    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3838    /// The board as this process last read it, for the length of one command — dropped by
3839    /// [`TaskSource::end_command`].
3840    ///
3841    /// A copy of a project used to re-read the whole board, paged, before writing each of
3842    /// its items, which is by far the largest part of a copy's request count and none of
3843    /// its work. Nothing else changes this board while a command runs — this source's own
3844    /// writes are the only writer — so one read answers them all.
3845    ///
3846    /// It is not a store of a user's work and it is not the cache the no-persistence
3847    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3848    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3849    /// an item this command created and then depends on resolves whether or not GitHub's
3850    /// own eventually-consistent read has caught up. A write to an item already on the
3851    /// board updates the entry here too, so what this holds is the last read plus this
3852    /// process's own writes rather than a snapshot taken before them.
3853    board_cache: Mutex<Option<Board>>,
3854    /// Every issue this board's own search reported, for the length of one command — dropped
3855    /// by [`TaskSource::end_command`].
3856    ///
3857    /// The second half of a board read, and cached for the same reason and on the same
3858    /// terms as the first: it lives and dies with the process, nothing is written down, and
3859    /// a write this process makes updates the entry here exactly as it updates the one in
3860    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3861    /// that lists this board's projects and its tasks pays for one search rather than two.
3862    search_cache: Mutex<Option<Vec<Resolved>>>,
3863    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3864    /// the length of one command — dropped by [`TaskSource::end_command`].
3865    ///
3866    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3867    /// and dies with the process, nothing is written down, a write this process makes
3868    /// updates the entry here as it updates the other two, and every answer is completed
3869    /// with this process's own writes each time it is given. A command that asks the same
3870    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3871    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3872    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3873    search_next: Mutex<BTreeMap<String, Option<String>>>,
3874    /// Records already resolved in this command, reused by writes and for comment identity.
3875    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3876    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3877    /// its item as a person has since left it.
3878    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3879    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3880    /// answered as holding nothing — each taken by the next read of that item by id and by
3881    /// nothing else, dropped by any mutation naming it, and cleared by
3882    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3883    /// read-before-write, made in the same request as the others' rather than on its own.
3884    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3885    /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3886    /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3887    /// beside the members of the one project it names, when it names one — or for the next
3888    /// request this source makes, whichever comes first.
3889    pending_origins: Mutex<Vec<String>>,
3890    /// Each project's members as a lookup carrying them read them in this command, trimmed to
3891    /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3892    /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3893    member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3894    /// Every item a listing of one project's members carried whole in this command — the
3895    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3896    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3897    /// it again. No other read is answered from it, so a read by id outside a copy's
3898    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3899    /// item, and cleared by [`TaskSource::end_command`].
3900    carried: Mutex<BTreeMap<NativeId, Resolved>>,
3901    /// Items an issue read by reference reached, held for the one status write each was read
3902    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3903    /// what such a write needs. Dropped by [`TaskSource::end_command`].
3904    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3905    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3906    /// asked for under, with the project that selector named — for the length of one command,
3907    /// dropped by [`TaskSource::end_command`].
3908    ///
3909    /// A caller pages through a project's tasks one engine page at a time, and every page
3910    /// is cut from the whole list of them, so without this each page walked every
3911    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3912    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3913    /// dies with the process, nothing is written down, a write this process makes updates or
3914    /// removes the entry here as it does there, and every answer is completed with this
3915    /// process's own writes each time it is given.
3916    children_cache: Mutex<ProjectChildren>,
3917    /// The board's own id and field definitions as this process last read them on their
3918    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3919    ///
3920    /// What a write needs of the board and its item does not say, read once per command
3921    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3922    /// lives and dies with the process and nothing is written down. It holds no item and so
3923    /// can answer no question about one — see [`Self::board_fields`].
3924    fields_cache: Mutex<Option<BoardFields>>,
3925    /// Each destination repository's node id, resolved once per repository
3926    /// rather than per issue created.
3927    ///
3928    /// A repository's node id does not change, and re-reading it for every issue of a copy
3929    /// spent one request per item on an answer this source already had. It is a map rather
3930    /// than one entry because a copy files each item in the repository its own
3931    /// `repositories` field names, so a plan across five repositories asks GitHub five
3932    /// times and not once per item.
3933    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3934    /// What every request this source sends is recorded into.
3935    ///
3936    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3937    /// a request leaves this crate, so nothing has to be switched on for a session to be
3938    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3939    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3940    /// source's reads and writes — adds up one accounting instead of two. See
3941    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3942    ledger: Arc<Accounting>,
3943    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3944    /// open, the issues primed from the cache, and the comments known on each.
3945    expected: expected::Held,
3946}
3947
3948/// GitHub's closed single-select color vocabulary.
3949#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3950#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3951pub enum StatusOptionColor {
3952    /// Gray.
3953    Gray,
3954    /// Blue.
3955    Blue,
3956    /// Green.
3957    Green,
3958    /// Yellow.
3959    Yellow,
3960    /// Purple.
3961    Purple,
3962    /// Red.
3963    Red,
3964    /// Orange.
3965    Orange,
3966    /// Pink.
3967    Pink,
3968}
3969
3970/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3971/// applies its additions.
3972#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3973pub enum SetupMode {
3974    /// Read without mutation.
3975    Plan,
3976    /// Apply and verify.
3977    Apply,
3978}
3979
3980/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3981/// against it goes on compiling.
3982pub type StatusOptionsMode = SetupMode;
3983
3984/// The explicit result of the requested operation.
3985#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3986#[serde(rename_all = "kebab-case")]
3987pub enum StatusOptionsOutcome {
3988    /// A read-only plan.
3989    Planned,
3990    /// Apply found nothing missing.
3991    Unchanged,
3992    /// Additions were applied and verified.
3993    Applied,
3994}
3995
3996/// A GitHub single-select option's opaque GraphQL node identifier.
3997#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3998#[serde(transparent)]
3999pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4000
4001impl TryFrom<String> for StatusOptionId {
4002    type Error = String;
4003
4004    fn try_from(id: String) -> Result<Self, Self::Error> {
4005        if id.trim().is_empty() {
4006            return Err("a GitHub Status option id cannot be blank".to_owned());
4007        }
4008        Ok(Self(id))
4009    }
4010}
4011
4012/// One existing or proposed option in a guarded Status-field update.
4013#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4014pub struct StatusOption {
4015    /// GitHub's stable id.
4016    pub id: StatusOptionId,
4017    /// The visible option name.
4018    pub name: ColumnName,
4019    /// GitHub's single-select color token.
4020    pub color: StatusOptionColor,
4021    /// The option description, including an empty one.
4022    pub description: String,
4023}
4024
4025/// One board item's Status assignment, retained as recovery data.
4026#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4027pub struct StatusAssignment {
4028    /// The project item id whose assignment this is.
4029    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4030    // carried verbatim as operator recovery data; introducing a semantic type would claim
4031    // validation rules GitHub does not publish and no operation here interprets.
4032    pub item_id: String,
4033    /// The selected option, absent when the item has no status.
4034    #[serde(skip_serializing_if = "Option::is_none")]
4035    pub option: Option<AssignedStatusOption>,
4036}
4037
4038/// The inseparable id and name of an assigned option.
4039#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4040pub struct AssignedStatusOption {
4041    /// GitHub's stable id.
4042    pub id: StatusOptionId,
4043    /// The visible name.
4044    pub name: ColumnName,
4045}
4046
4047/// The plan and verified outcome of reconciling configured Status options.
4048#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4049pub struct StatusOptionsReport {
4050    /// The configured source name.
4051    pub source: SourceName,
4052    /// Configured option names absent before the operation.
4053    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4054    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4055    // serialized string here preserves the report's intentionally simple public contract.
4056    pub missing: Vec<String>,
4057    /// What the requested operation did.
4058    pub outcome: StatusOptionsOutcome,
4059    /// The complete option list observed before any mutation.
4060    pub existing: Vec<StatusOption>,
4061}
4062
4063#[derive(Debug, Clone, PartialEq, Eq)]
4064struct StatusSnapshot {
4065    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4066    // passed back as the mutation's project identity; a newtype could enforce no stronger
4067    // invariant because GitHub publishes no grammar for it.
4068    board_id: String,
4069    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4070    // passed back as the mutation's field identity; a newtype could enforce no stronger
4071    // invariant because GitHub publishes no grammar for it.
4072    field_id: String,
4073    options: Vec<StatusOption>,
4074    assignments: Vec<StatusAssignment>,
4075}
4076
4077/// The name of the board field a status is held in.
4078const STATUS_FIELD: &str = "Status";
4079
4080/// Every item's value of each field `report` names, as it stood before the setup wrote
4081/// anything — what a person puts back when the setup is refused part way.
4082fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4083    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4084        .fields
4085        .iter()
4086        .map(|field| (field.field.name(), before.assignments(field.field)))
4087        .collect();
4088    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4089        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4090    })
4091}
4092
4093/// One board field the guarded setup reads and writes — every one it reads, and the only
4094/// ones it writes.
4095#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4096pub enum BoardField {
4097    /// The single-select `Status` field every instance's `status_mapping` resolves into.
4098    Status,
4099    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4100    Priority,
4101}
4102
4103impl BoardField {
4104    /// The field's name on the board.
4105    #[must_use]
4106    pub const fn name(self) -> &'static str {
4107        match self {
4108            Self::Status => STATUS_FIELD,
4109            Self::Priority => PRIORITY_FIELD,
4110        }
4111    }
4112
4113    /// The field a board calls `name`, or `None` for one this setup does not own.
4114    fn named(name: &str) -> Option<Self> {
4115        [Self::Status, Self::Priority]
4116            .into_iter()
4117            .find(|field| field.name() == name)
4118    }
4119}
4120
4121/// What the guarded setup did to one field.
4122#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4123#[serde(rename_all = "kebab-case")]
4124pub enum FieldOutcome {
4125    /// A read-only plan.
4126    Planned,
4127    /// Apply found the field there with every configured option.
4128    Unchanged,
4129    /// Missing options were added to the field that was there, and verified.
4130    Applied,
4131    /// The field was not there; it was created holding the configured options, and verified.
4132    Created,
4133}
4134
4135/// One field's plan, or its verified outcome.
4136#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4137pub struct FieldReport {
4138    /// Which field.
4139    pub field: BoardField,
4140    /// Whether the board had the field before the operation.
4141    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4142    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4143    // "outcome", "existing"}` — so folding one into the other would change a published JSON
4144    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4145    // one constructor, and it derives `outcome` from `exists` in one match.
4146    pub exists: bool,
4147    /// Configured option names the field lacked before the operation — every one of them,
4148    /// in the order a new field lists them, when the field was not there at all.
4149    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4150    // mapping name and has therefore already passed its nonblank validation; the serialized
4151    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4152    pub missing: Vec<String>,
4153    /// For the `Status` field, which item kind each missing name is configured for: one
4154    /// entry per kind `status_mapping` names a missing option for, task before project, each
4155    /// listing that kind's missing names in category order. A name both kinds use is in
4156    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4157    /// `Priority`, which only a task holds.
4158    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4159    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4160    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4161    #[schemars(!skip_serializing_if)]
4162    pub kinds: Vec<KindMissing>,
4163    /// What the requested operation did.
4164    pub outcome: FieldOutcome,
4165    /// The field's complete option list observed before any mutation; empty when the field
4166    /// was not there.
4167    pub existing: Vec<StatusOption>,
4168}
4169
4170/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4171#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4172pub struct KindMissing {
4173    /// The kind these names are configured for.
4174    pub kind: ItemKind,
4175    /// The names that kind maps a category to and the field lacked, in category order.
4176    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4177    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4178    // intentionally simple public contract.
4179    pub missing: Vec<String>,
4180}
4181
4182/// The plan and verified outcome of setting up every field a source's configuration names.
4183#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4184pub struct FieldsReport {
4185    /// The configured source name.
4186    pub source: SourceName,
4187    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4188    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4189    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4190    // per field would change a published JSON shape. The states the list could hold and the
4191    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4192    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4193    pub fields: Vec<FieldReport>,
4194    /// Each board text field the source's `metadata_fields` projects a value onto, in
4195    /// configured order. Empty — and left out of the JSON — when it projects none.
4196    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4197    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4198    // both SDKs model an absent list as an empty one rather than as `null`.
4199    #[schemars(!skip_serializing_if)]
4200    pub metadata_fields: Vec<MetadataFieldReport>,
4201}
4202
4203/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4204/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4205///
4206/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4207/// no conflict.
4208// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4209// refuses exactly what `try_from` below does.
4210#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4211#[serde(transparent)]
4212pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4213
4214/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4215/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4216/// neither SDK's validator can be relied on to support.
4217const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4218    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4219
4220impl TryFrom<String> for NonTextFieldType {
4221    type Error = String;
4222
4223    fn try_from(kind: String) -> Result<Self, Self::Error> {
4224        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4225            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4226        }
4227        if kind == "TEXT" {
4228            return Err("a TEXT field is no conflict for a projected text field".to_owned());
4229        }
4230        Ok(Self(kind))
4231    }
4232}
4233
4234impl std::fmt::Display for NonTextFieldType {
4235    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4236        formatter.write_str(&self.0)
4237    }
4238}
4239
4240/// One projected metadata text field's plan, or its verified outcome.
4241///
4242/// The setup creates a missing one as a text field and never alters a field that is there:
4243/// a same-named field of another type is reported in `conflict` and left as it is.
4244#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4245pub struct MetadataFieldReport {
4246    /// The board field's name.
4247    pub field: String, // llmlint: ignore[invalid_states_unrepresentable] Copied from a configuration entry `MetadataField::resolve` already validated; the serialized string is the report's intentionally simple public contract, as `FieldReport::missing`'s names are.
4248    /// The metadata key the value is read from.
4249    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4250    /// The object keys walked inside that key's value; empty for the key's own value.
4251    pub path: Vec<String>,
4252    /// Whether the board had a text field of that name before the operation.
4253    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` mirrors `FieldReport`'s published shape; `GitHubProjectsSource::fields` is the one constructor and derives `outcome` from `exists` and `conflict` in one match.
4254    pub exists: bool,
4255    /// The type of a field of that name that is not a text field, which the setup leaves as
4256    /// it is; absent — and left out of the JSON — when there is none.
4257    #[serde(default, skip_serializing_if = "Option::is_none")]
4258    pub conflict: Option<NonTextFieldType>,
4259    /// What the requested operation did: `planned`; `created` for a field that was not
4260    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4261    pub outcome: FieldOutcome,
4262}
4263
4264/// Which options one field is configured with, in the order a new field would list them.
4265struct FieldPlan {
4266    field: BoardField,
4267    wanted: Vec<String>,
4268}
4269
4270/// One single-select field as the guarded setup snapshots it.
4271#[derive(Debug, Clone, PartialEq, Eq)]
4272struct SnapshotField {
4273    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4274    // passed back as the mutation's field identity; a newtype could enforce no stronger
4275    // invariant because GitHub publishes no grammar for it.
4276    field_id: String,
4277    options: Vec<StatusOption>,
4278}
4279
4280/// Every single-select field of a board and every item's value of each.
4281#[derive(Debug, Clone, PartialEq, Eq)]
4282struct BoardSnapshot {
4283    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4284    // passed back as the mutation's project identity; a newtype could enforce no stronger
4285    // invariant because GitHub publishes no grammar for it.
4286    board_id: String,
4287    fields: BTreeMap<BoardField, SnapshotField>,
4288    /// Each board item's id, and its value of each field this setup owns that it holds one of.
4289    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4290}
4291
4292impl BoardSnapshot {
4293    /// Every item's value of `field`, in board order — the recovery data a drift refusal
4294    /// carries.
4295    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4296        self.items
4297            .iter()
4298            .map(|(item_id, values)| StatusAssignment {
4299                item_id: item_id.clone(),
4300                option: values.get(&field).cloned(),
4301            })
4302            .collect()
4303    }
4304}
4305
4306impl GitHubProjectsSource {
4307    /// Report missing configured Status options and, when `apply` is true, add them with
4308    /// a whole-list mutation that preserves every existing id and verifies the result.
4309    ///
4310    /// # Errors
4311    ///
4312    /// Refuses a board without a single-select `Status` field. A post-write difference in
4313    /// any pre-existing option id or item assignment is refused with the complete pre-write
4314    /// assignment snapshot in the diagnostic for recovery.
4315    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4316    // successful mutation, both drift refusals, source selection, missing Status, casing,
4317    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4318    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4319    // responses from entering the defensive malformed-response branches below.
4320    pub async fn status_options(
4321        &self,
4322        mode: StatusOptionsMode,
4323    ) -> Result<StatusOptionsReport, SourceError> {
4324        let before = self.status_snapshot().await?;
4325        // A terminal category's option is as configured as an open one's: a terminal
4326        // write validates it before closing and refuses when the board lacks it. Both
4327        // kinds' names are options of the one field, so both are asked for.
4328        let missing = self
4329            .statuses
4330            .wanted()
4331            .into_iter()
4332            .filter(|wanted| {
4333                !before
4334                    .options
4335                    .iter()
4336                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4337            })
4338            .collect::<Vec<_>>();
4339        let report = StatusOptionsReport {
4340            source: self.name.clone(),
4341            missing: missing.clone(),
4342            outcome: match (mode, missing.is_empty()) {
4343                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4344                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4345                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4346            },
4347            existing: before.options.clone(),
4348        };
4349        if mode == StatusOptionsMode::Plan || missing.is_empty() {
4350            return Ok(report);
4351        }
4352        let mut options = before
4353            .options
4354            .iter()
4355            .map(|option| {
4356                json!({
4357                    "id": option.id, "name": option.name, "color": option.color,
4358                    "description": option.description,
4359                })
4360            })
4361            .collect::<Vec<_>>();
4362        options.extend(missing.iter().map(|name| {
4363            json!({
4364                "name": name, "color": "GRAY", "description": ""
4365            })
4366        }));
4367        self.graphql(
4368            graphql::STATUS_OPTIONS_UPDATE,
4369            json!({"input": {
4370                "projectId": before.board_id, "fieldId": before.field_id,
4371                "singleSelectOptions": options,
4372            }}),
4373        )
4374        .await?;
4375        let after = self.status_snapshot().await?;
4376        let options_preserved = before
4377            .options
4378            .iter()
4379            .all(|old| after.options.iter().any(|new| new == old));
4380        let additions_present = missing.iter().all(|wanted| {
4381            after
4382                .options
4383                .iter()
4384                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4385        });
4386        if !options_preserved || !additions_present || after.assignments != before.assignments {
4387            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4388                SourceError::Malformed {
4389                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4390                }
4391            })?;
4392            return Err(SourceError::Refused {
4393                message: format!(
4394                    "GitHub changed a pre-existing Status option id, name, color or description, or an item assignment after the guarded update; the pre-write item assignment snapshot is:\n{recovery}"
4395                ),
4396            });
4397        }
4398        Ok(report)
4399    }
4400
4401    /// A fresh snapshot of the Status field and every board item's assignment of it.
4402    ///
4403    /// # Errors
4404    ///
4405    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4406    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4407        // Status alone, as this operation has always read it: a `Priority` field is another
4408        // operation's, so nothing about it can refuse this one.
4409        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4410        let field = board
4411            .fields
4412            .remove(&BoardField::Status)
4413            .ok_or_else(|| self.no_status_field())?;
4414        Ok(StatusSnapshot {
4415            assignments: board.assignments(BoardField::Status),
4416            board_id: board.board_id,
4417            field_id: field.field_id,
4418            options: field.options,
4419        })
4420    }
4421
4422    /// The refusal a board with no `Status` field is answered with by the guarded setup.
4423    fn no_status_field(&self) -> SourceError {
4424        SourceError::Refused {
4425            message: format!("source {} board has no Status field", self.name),
4426        }
4427    }
4428
4429    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4430    // the real CLI loopback journey, including pagination. The individual malformed guards
4431    // are defensive validation of a schema-pinned third-party response, not separate user
4432    // journeys; drift and missing-field failures cover the operation's recovery behavior.
4433    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4434    /// every board item's value of each, walked to the end of the board's items. A field not
4435    /// in `owned` is read past whatever it holds.
4436    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4437        let mut after: Option<String> = None;
4438        let mut snapshot: Option<BoardSnapshot> = None;
4439        loop {
4440            let data = self
4441                .graphql(
4442                    graphql::STATUS_OPTIONS_SNAPSHOT,
4443                    json!({
4444                        "owner": self.owner, "number": self.project_number,
4445                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4446                    }),
4447                )
4448                .await?;
4449            let board = data
4450                .pointer("/owner/projectV2")
4451                .filter(|board| board.is_object())
4452                .ok_or_else(|| SourceError::Refused {
4453                    message: format!(
4454                        "source {} has no accessible GitHub Projects board",
4455                        self.name
4456                    ),
4457                })?;
4458            if board
4459                .pointer("/fields/pageInfo/hasNextPage")
4460                .and_then(Value::as_bool)
4461                != Some(false)
4462            {
4463                return Err(SourceError::Malformed {
4464                    message:
4465                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4466                            .into(),
4467                });
4468            }
4469            let mut fields = BTreeMap::new();
4470            // Only the fields this setup owns, by name: a node the single-select fragment did not
4471            // match carries no name, and a person's own single-select field — a `Size`, a
4472            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4473            // `Status` or `Priority` field without its options is malformed, not absent.
4474            // llmlint: ignore[boundary_inputs_validated] The field page this loop reads is validated as complete immediately above: any `fields.pageInfo.hasNextPage` other than `false` is refused as malformed before a node is read, so an incomplete page is never taken for the board's whole field set.
4475            for (owned, field) in board
4476                .pointer("/fields/nodes")
4477                .and_then(Value::as_array)
4478                .ok_or_else(|| SourceError::Malformed {
4479                    message: "GitHub project fields.nodes is not an array".into(),
4480                })?
4481                .iter()
4482                .filter_map(|field| {
4483                    let named = BoardField::named(field.get("name")?.as_str()?)?;
4484                    owned.contains(&named).then_some((named, field))
4485                })
4486            {
4487                let options = field
4488                    .get("options")
4489                    .and_then(Value::as_array)
4490                    .ok_or_else(|| SourceError::Malformed {
4491                        message: "GitHub single-select field options is not an array".into(),
4492                    })?
4493                    .iter()
4494                    .map(|option| {
4495                        Ok(StatusOption {
4496                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4497                                .map_err(|message| SourceError::Malformed { message })?,
4498                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4499                                .map_err(|message| SourceError::Malformed {
4500                                    message: format!(
4501                                        "GitHub single-select option name is invalid: {message}"
4502                                    ),
4503                                })?,
4504                            color: serde_json::from_value(
4505                                option.get("color").cloned().unwrap_or(Value::Null),
4506                            )
4507                            .map_err(|error| {
4508                                SourceError::Malformed {
4509                                    message: format!(
4510                                        "GitHub single-select option color is invalid: {error}"
4511                                    ),
4512                                }
4513                            })?,
4514                            description: optional_str(option, "description")?
4515                                .unwrap_or_default()
4516                                .to_owned(),
4517                        })
4518                    })
4519                    .collect::<Result<Vec<_>, SourceError>>()?;
4520                let snapshot = SnapshotField {
4521                    field_id: required_nonblank_str(field, "id")?.to_owned(),
4522                    options,
4523                };
4524                // A board's field names are unique, so a second one is an answer that cannot
4525                // say which field the setup would act on — refused rather than one chosen.
4526                if fields.insert(owned, snapshot).is_some() {
4527                    return Err(SourceError::Malformed {
4528                        message: format!(
4529                            "GitHub answered two {} fields for this board",
4530                            owned.name()
4531                        ),
4532                    });
4533                }
4534            }
4535            let board_id = required_nonblank_str(board, "id")?.to_owned();
4536            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4537                board_id,
4538                fields,
4539                items: Vec::new(),
4540            });
4541            let items = board
4542                .pointer("/items/nodes")
4543                .and_then(Value::as_array)
4544                .ok_or_else(|| SourceError::Malformed {
4545                    message: "GitHub project items.nodes is not an array".into(),
4546                })?;
4547            for item in items {
4548                let field_values =
4549                    item.get("fieldValues")
4550                        .ok_or_else(|| SourceError::Malformed {
4551                            message: "GitHub project item is missing fieldValues".into(),
4552                        })?;
4553                if field_values
4554                    .pointer("/pageInfo/hasNextPage")
4555                    .and_then(Value::as_bool)
4556                    != Some(false)
4557                {
4558                    return Err(SourceError::Malformed {
4559                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4560                    });
4561                }
4562                let values = item
4563                    .pointer("/fieldValues/nodes")
4564                    .and_then(Value::as_array)
4565                    .ok_or_else(|| SourceError::Malformed {
4566                        message: "GitHub project item fieldValues.nodes is not an array".into(),
4567                    })?;
4568                let item_id = required_nonblank_str(item, "id")?;
4569                let mut assigned = BTreeMap::new();
4570                for value in values {
4571                    let Some(field) = value
4572                        .pointer("/field/name")
4573                        .and_then(Value::as_str)
4574                        .and_then(BoardField::named)
4575                        .filter(|field| owned.contains(field))
4576                    else {
4577                        continue;
4578                    };
4579                    let held = assigned.insert(
4580                        field,
4581                        AssignedStatusOption {
4582                            id: StatusOptionId::try_from(
4583                                required_str(value, "optionId")?.to_owned(),
4584                            )
4585                            .map_err(|message| SourceError::Malformed { message })?,
4586                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4587                                .map_err(|message| SourceError::Malformed {
4588                                    message: format!(
4589                                        "GitHub assigned {} name is invalid: {message}",
4590                                        field.name()
4591                                    ),
4592                                })?,
4593                        },
4594                    );
4595                    // An item holds one value of a field, so a second one leaves no way to
4596                    // tell which it holds — and a verification or recovery built on either
4597                    // could restore the wrong one.
4598                    if held.is_some() {
4599                        return Err(SourceError::Malformed {
4600                            message: format!(
4601                                "GitHub answered two {} values for board item {item_id}",
4602                                field.name()
4603                            ),
4604                        });
4605                    }
4606                }
4607                current.items.push((item_id.to_owned(), assigned));
4608            }
4609            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4610                message: "GitHub project is missing items".into(),
4611            })?;
4612            let has_next = page
4613                .pointer("/pageInfo/hasNextPage")
4614                .and_then(Value::as_bool)
4615                .ok_or_else(|| SourceError::Malformed {
4616                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4617                })?;
4618            if !has_next {
4619                break;
4620            }
4621            let next =
4622                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4623            validate_cursor_progress(after.as_deref(), next)?;
4624            after = Some(next.to_owned());
4625        }
4626        snapshot.ok_or_else(|| SourceError::Malformed {
4627            message: "GitHub returned no board field snapshot".into(),
4628        })
4629    }
4630    // llmlint: ignore-end[changed_behavior_has_e2e]
4631
4632    /// Report every board field this source's configuration names and, with
4633    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4634    /// the `Priority` field when the board has none.
4635    ///
4636    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4637    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4638    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4639    /// color and description: the whole option list goes back with every existing id, because
4640    /// a re-minted id clears every item's value.
4641    ///
4642    /// # Errors
4643    ///
4644    /// Refuses a board without a single-select `Status` field. After an apply the board is
4645    /// read again, and a pre-existing option or any item's value of either field that moved is
4646    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4647    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4648    // unchanged apply, a created field, an added option to each field, drift refusal, a board
4649    // with no Status field and a non-github-projects source through the compiled CLI against
4650    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4651    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4652        let owned: Vec<BoardField> = if self.priorities.is_some() {
4653            vec![BoardField::Status, BoardField::Priority]
4654        } else {
4655            vec![BoardField::Status]
4656        };
4657        let before = self.board_snapshot(&owned).await?;
4658        let mut plans = vec![FieldPlan {
4659            field: BoardField::Status,
4660            wanted: self.statuses.wanted(),
4661        }];
4662        if !before.fields.contains_key(&BoardField::Status) {
4663            return Err(self.no_status_field());
4664        }
4665        if let Some(mapping) = &self.priorities {
4666            plans.push(FieldPlan {
4667                field: BoardField::Priority,
4668                wanted: mapping.names().map(str::to_owned).collect(),
4669            });
4670        }
4671        // The snapshot reads single-select fields alone, so a field it did not find may still
4672        // be on the board under the name, of another type: creating one beside it would fail
4673        // part way, or leave two fields of one name. Asked of the board's own field list, and
4674        // only when a field is missing.
4675        if plans
4676            .iter()
4677            .any(|plan| !before.fields.contains_key(&plan.field))
4678        {
4679            let board = self.board_fields().await?;
4680            for plan in plans
4681                .iter()
4682                .filter(|plan| !before.fields.contains_key(&plan.field))
4683            {
4684                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4685                    return Err(SourceError::Refused {
4686                        message: format!(
4687                            "source {}'s board has a {} field that is not a single-select field \
4688                             (it is a {}), so it cannot hold this source's options; next: rename \
4689                             or remove that field, then run this again",
4690                            self.name,
4691                            plan.field.name(),
4692                            optional_str(field, "__typename")?.unwrap_or("field of another type")
4693                        ),
4694                    });
4695                }
4696            }
4697        }
4698        let mut reports = Vec::new();
4699        for plan in &plans {
4700            let held = before.fields.get(&plan.field);
4701            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4702            let mut missing: Vec<String> = Vec::new();
4703            for wanted in &plan.wanted {
4704                let present = existing
4705                    .iter()
4706                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4707                    || missing
4708                        .iter()
4709                        .any(|named| named.eq_ignore_ascii_case(wanted));
4710                if !present {
4711                    missing.push(wanted.clone());
4712                }
4713            }
4714            let kinds = match plan.field {
4715                BoardField::Status => self.statuses.missing_by_kind(&existing),
4716                BoardField::Priority => Vec::new(),
4717            };
4718            reports.push(FieldReport {
4719                field: plan.field,
4720                exists: held.is_some(),
4721                kinds,
4722                outcome: match (mode, held.is_some(), missing.is_empty()) {
4723                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4724                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4725                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4726                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
4727                },
4728                missing,
4729                existing,
4730            });
4731        }
4732        // A projected field is a text field, which the single-select snapshot cannot see, so
4733        // it is asked of the board's own field list — and only when one is configured.
4734        let mut metadata_fields = Vec::new();
4735        if !self.metadata_fields.is_empty() {
4736            let board = self.board_fields().await?;
4737            for projection in &self.metadata_fields {
4738                let conflict = match Board::field(&board.fields, &projection.field)? {
4739                    None => None,
4740                    Some(field) => {
4741                        let typename = optional_str(field, "__typename")?;
4742                        let data_type = optional_str(field, "dataType")?;
4743                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4744                            None
4745                        } else {
4746                            let named = data_type
4747                                .filter(|kind| *kind != "TEXT")
4748                                .or(typename)
4749                                .unwrap_or("field of another type");
4750                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4751                                |message| SourceError::Malformed {
4752                                    message: format!(
4753                                        "GitHub answered the board's {} field with a type this \
4754                                         setup cannot read: {message}",
4755                                        &*projection.field
4756                                    ),
4757                                },
4758                            )?)
4759                        }
4760                    }
4761                };
4762                let exists =
4763                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4764                metadata_fields.push(MetadataFieldReport {
4765                    field: projection.field.to_string(),
4766                    key: projection.key.to_string(),
4767                    path: projection.path.clone(),
4768                    exists,
4769                    outcome: match (mode, exists || conflict.is_some()) {
4770                        (SetupMode::Plan, _) => FieldOutcome::Planned,
4771                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4772                        (SetupMode::Apply, false) => FieldOutcome::Created,
4773                    },
4774                    conflict,
4775                });
4776            }
4777        }
4778        let report = FieldsReport {
4779            source: self.name.clone(),
4780            fields: reports,
4781            metadata_fields,
4782        };
4783        let writes: Vec<&FieldReport> = report
4784            .fields
4785            .iter()
4786            .filter(|field| !field.missing.is_empty() || !field.exists)
4787            .collect();
4788        let creates: Vec<&MetadataFieldReport> = report
4789            .metadata_fields
4790            .iter()
4791            .filter(|field| field.outcome == FieldOutcome::Created)
4792            .collect();
4793        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4794            return Ok(report);
4795        }
4796        let mut landed: Vec<&str> = Vec::new();
4797        for field in &writes {
4798            let added = field
4799                .missing
4800                .iter()
4801                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4802            let sent = match before.fields.get(&field.field) {
4803                Some(held) => {
4804                    let mut options = held
4805                        .options
4806                        .iter()
4807                        .map(|option| {
4808                            json!({
4809                                "id": option.id, "name": option.name, "color": option.color,
4810                                "description": option.description,
4811                            })
4812                        })
4813                        .collect::<Vec<_>>();
4814                    options.extend(added);
4815                    self.graphql(
4816                        graphql::STATUS_OPTIONS_UPDATE,
4817                        json!({"input": {
4818                            "projectId": before.board_id, "fieldId": held.field_id,
4819                            "singleSelectOptions": options,
4820                        }}),
4821                    )
4822                    .await
4823                }
4824                None => {
4825                    self.graphql(
4826                        graphql::CREATE_FIELD,
4827                        json!({"input": {
4828                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4829                            "name": field.field.name(),
4830                            "singleSelectOptions": added.collect::<Vec<_>>(),
4831                        }}),
4832                    )
4833                    .await
4834                }
4835            };
4836            // A mutation that failed does not establish that GitHub left its field as it was,
4837            // so every failure from here on carries the recovery data a drift refusal does.
4838            match sent {
4839                Ok(_) => landed.push(field.field.name()),
4840                Err(error) => {
4841                    let changed = if landed.is_empty() {
4842                        String::new()
4843                    } else {
4844                        format!("changed the {} field and then ", landed.join(" and "))
4845                    };
4846                    return Err(SourceError::Refused {
4847                        message: format!(
4848                            "the guarded field setup {changed}failed on the {} field, which it may \
4849                             have changed part way: {error}; the pre-write item assignments \
4850                             are:\n{}",
4851                            field.field.name(),
4852                            recovery(&report, &before)?
4853                        ),
4854                    });
4855                }
4856            }
4857        }
4858        for field in &creates {
4859            let sent = self
4860                .graphql(
4861                    graphql::CREATE_FIELD,
4862                    json!({"input": {
4863                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4864                    }}),
4865                )
4866                .await;
4867            if let Err(error) = sent {
4868                let changed = if landed.is_empty() {
4869                    String::new()
4870                } else {
4871                    format!("changed the {} field and then ", landed.join(" and "))
4872                };
4873                return Err(SourceError::Refused {
4874                    message: format!(
4875                        "the guarded field setup {changed}failed creating the {} text field: \
4876                         {error}; run it again to create what is still missing",
4877                        field.field
4878                    ),
4879                });
4880            }
4881            landed.push(&field.field);
4882        }
4883        // The board has been written, so a verification read that fails leaves it unverified
4884        // rather than unchanged, and says what to put back.
4885        let after = match self.board_snapshot(&owned).await {
4886            Ok(after) => after,
4887            Err(error) => {
4888                return Err(SourceError::Refused {
4889                    message: format!(
4890                        "the guarded field setup changed the {} field and then could not read the \
4891                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4892                        landed.join(" and "),
4893                        recovery(&report, &before)?
4894                    ),
4895                });
4896            }
4897        };
4898        let mut moved = Vec::new();
4899        for field in &report.fields {
4900            let name = field.field.name();
4901            let now = after
4902                .fields
4903                .get(&field.field)
4904                .map(|held| held.options.as_slice())
4905                .unwrap_or_default();
4906            if !field.existing.iter().all(|old| now.contains(old)) {
4907                moved.push(format!(
4908                    "a pre-existing {name} option id, name, color or description"
4909                ));
4910            }
4911            if !field.missing.iter().all(|wanted| {
4912                now.iter()
4913                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4914            }) {
4915                moved.push(format!("an added {name} option"));
4916            }
4917            if after.assignments(field.field) != before.assignments(field.field) {
4918                moved.push(format!("an item's {name} value"));
4919            }
4920        }
4921        if !creates.is_empty() {
4922            // Read afresh: the view held for this command is the one from before the creates.
4923            *self.fields_cache()? = None;
4924            let board = match self.board_fields().await {
4925                Ok(board) => board,
4926                Err(error) => {
4927                    return Err(SourceError::Refused {
4928                        message: format!(
4929                            "the guarded field setup changed the {} field and then could not \
4930                             read the board back to verify it: {error}; run it again to verify \
4931                             it, which creates nothing a second time",
4932                            landed.join(" and ")
4933                        ),
4934                    });
4935                }
4936            };
4937            for field in &creates {
4938                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4939                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4940                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4941                });
4942                if !text {
4943                    moved.push(format!("the created {} text field", field.field));
4944                }
4945            }
4946        }
4947        if !moved.is_empty() {
4948            return Err(SourceError::Refused {
4949                message: format!(
4950                    "GitHub changed {} after the guarded field setup; the pre-write item \
4951                     assignments are:\n{}",
4952                    moved.join(", "),
4953                    recovery(&report, &before)?
4954                ),
4955            });
4956        }
4957        Ok(report)
4958    }
4959
4960    /// Validate configuration and capture the named credential without exposing it.
4961    ///
4962    /// # Errors
4963    ///
4964    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4965    /// [`SourceError::Auth`] when the named credential is missing or empty.
4966    pub fn new(
4967        name: &SourceName,
4968        config: GitHubProjectsConfig,
4969        secrets: &dyn SecretResolver,
4970    ) -> Result<Self, SourceError> {
4971        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4972    }
4973
4974    /// The same, recording every request it sends into an accounting the caller holds too.
4975    ///
4976    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4977    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4978    /// up — passes the one it records those into, so the session total accounts for the
4979    /// whole session rather than for this source's share of it.
4980    ///
4981    /// # Errors
4982    ///
4983    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4984    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4985    pub fn recording_into(
4986        name: &SourceName,
4987        config: GitHubProjectsConfig,
4988        secrets: &dyn SecretResolver,
4989        ledger: Arc<Accounting>,
4990    ) -> Result<Self, SourceError> {
4991        if !valid_github_owner(&config.owner) {
4992            return Err(SourceError::Config {
4993                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4994            });
4995        }
4996        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4997            return Err(SourceError::Config {
4998                message: format!("project_number must be between 1 and {}", i32::MAX),
4999            });
5000        }
5001        if !valid_environment_name(&config.token_env) {
5002            return Err(SourceError::Config {
5003                message: "token_env must be a valid environment-variable name".into(),
5004            });
5005        }
5006        let repository = config
5007            .repository
5008            .as_deref()
5009            .map(RepositoryTarget::parse)
5010            .transpose()?;
5011        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5012            message: format!("endpoint is not a valid URL: {e}"),
5013        })?;
5014        if endpoint.scheme() != "https"
5015            && !(endpoint.scheme() == "http"
5016                && endpoint
5017                    .host_str()
5018                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5019        {
5020            return Err(SourceError::Config {
5021                message:
5022                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5023                        .into(),
5024            });
5025        }
5026        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5027            message: format!("environment variable {} is missing or empty; set it to a fine-grained GitHub token granting Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board", config.token_env),
5028        })?;
5029        Ok(Self {
5030            name: name.clone(),
5031            owner: config.owner,
5032            project_number: config.project_number,
5033            repository,
5034            asset_client: std::sync::OnceLock::new(),
5035            endpoint,
5036            token,
5037            credential_name: config.token_env,
5038            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5039            priorities: config
5040                .priority_mapping
5041                .map(|mapping| PriorityMapping::resolve(mapping, name))
5042                .transpose()?,
5043            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5044            client: std::sync::OnceLock::new(),
5045            created: Mutex::new(Vec::new()),
5046            updated: Mutex::new(Vec::new()),
5047            commented: Mutex::new(Vec::new()),
5048            pacing: Pacing::resolve(config.pacing, name)?,
5049            last_mutation: Mutex::new(None),
5050            windows: Mutex::new(pacing::Windows::default()),
5051            task_children: Mutex::new(BTreeMap::new()),
5052            written: Mutex::new(BTreeMap::new()),
5053            deferred: Mutex::new(None),
5054            held_updates: Mutex::new(Vec::new()),
5055            clock: system_clock(),
5056            declared_private: std::sync::atomic::AtomicBool::new(false),
5057            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5058            board_cache: Mutex::new(None),
5059            search_cache: Mutex::new(None),
5060            narrowed_cache: Mutex::new(BTreeMap::new()),
5061            resolved_cache: Mutex::new(BTreeMap::new()),
5062            read_ahead: Mutex::new(BTreeMap::new()),
5063            pending_origins: Mutex::new(Vec::new()),
5064            member_classes: Mutex::new(BTreeMap::new()),
5065            carried: Mutex::new(BTreeMap::new()),
5066            held_cache: Mutex::new(BTreeMap::new()),
5067            children_cache: Mutex::new(BTreeMap::new()),
5068            search_next: Mutex::new(BTreeMap::new()),
5069            fields_cache: Mutex::new(None),
5070            repository_cache: Mutex::new(BTreeMap::new()),
5071            ledger,
5072            expected: expected::Held::default(),
5073        })
5074    }
5075
5076    /// The client every API request goes through, built on the first one.
5077    ///
5078    /// Not with the source: building a client loads the system's certificate store, which
5079    /// costs tens of milliseconds, and a command that configures this board but never sends to
5080    /// it — a read of another source — should not pay that in its process.
5081    fn client(&self) -> Result<&Client, SourceError> {
5082        built(&self.client, || {
5083            Client::builder()
5084                .user_agent("onetaskgraph")
5085                .build()
5086                .map_err(|e| SourceError::Config {
5087                    message: format!("cannot build HTTP client: {e}"),
5088                })
5089        })
5090    }
5091
5092    /// The client attachment requests go through, with its own redirect policy, built on the
5093    /// first one for the reason [`Self::client`] gives.
5094    fn asset_client(&self) -> Result<&Client, SourceError> {
5095        built(&self.asset_client, || assets::client(&self.endpoint))
5096    }
5097
5098    /// A snapshot of every request this source has sent, and what each cost.
5099    ///
5100    /// A value to hold and compare rather than a borrow of the accounting itself, so two
5101    /// of them can sit side by side. When this source was built with
5102    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5103    /// point of building it that way.
5104    #[must_use]
5105    pub fn accounting(&self) -> accounting::Session {
5106        self.ledger.snapshot()
5107    }
5108
5109    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5110    /// rate limit rather than handing it straight back as an error.
5111    ///
5112    /// Retrying is safe for every document here, including the mutations, and the reason
5113    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5114    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5115    /// this replays has already taken effect. An outcome this source cannot know — the
5116    /// send failed, or the body could not be read, so the mutation may well have landed —
5117    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5118    /// attempt. A duplicate write would come from replaying one of those, and none is
5119    /// replayed.
5120    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5121        // Updates held back go out before any other request, so whatever it reads or writes
5122        // sees the board as though each had been sent when it was made.
5123        Box::pin(self.send_held_updates()).await?;
5124        // Origins held back for a read ahead that never came are sent before anything else.
5125        if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5126            Box::pin(self.send_pending_origins(None)).await?;
5127        }
5128        if !self.expected.journaling() {
5129            return self.graphql_sent(query, variables).await;
5130        }
5131        // While a write the store's cache expects runs, its mutations select the whole issue
5132        // they leave behind, so the response vouches for what the cache keeps — and every
5133        // request is recorded, so the write's parts are reported from their own responses.
5134        let (query, variables) = match graphql::whole_twin(query) {
5135            Some(twin) => (twin, expected::whole_variables(variables)),
5136            None => (query, variables),
5137        };
5138        let sent = self.graphql_sent(query, variables.clone()).await;
5139        self.journal(query, &variables, &sent).await;
5140        sent
5141    }
5142
5143    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5144    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5145        if is_mutation(query)
5146            && ![
5147                graphql::ADD_COMMENT,
5148                graphql::UPDATE_COMMENT,
5149                graphql::DELETE_COMMENT,
5150                graphql::ADD_COMMENT_WHOLE,
5151                graphql::UPDATE_COMMENT_WHOLE,
5152            ]
5153            .contains(&query)
5154        {
5155            let mut cache = self.resolved_cache()?;
5156            let mut ahead = self.read_ahead()?;
5157            let mut carried = self.carried()?;
5158            for argument in ["input", "second", "third", "clear"] {
5159                if let Some(input) = variables.get(argument) {
5160                    let names = |id: &NativeId, item_id: Option<&str>| {
5161                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5162                            input
5163                                .get(key)
5164                                .and_then(Value::as_str)
5165                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
5166                        })
5167                    };
5168                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
5169                    ahead.retain(|id, item| {
5170                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5171                    });
5172                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
5173                }
5174            }
5175        }
5176        let doing = operation_description(query);
5177        let mutation = is_mutation(query);
5178        let charge = pacing::Charge::graphql(query, &variables);
5179        let mut waited = Duration::ZERO;
5180        let mut waits = 0_u32;
5181        let mut backoff = self.pacing.retry_backoff;
5182        loop {
5183            let ticket = self.pace(charge, mutation).await;
5184            let attempt = self.send_once(query, &variables, waits + 1).await;
5185            self.paced(ticket, mutation);
5186            let limited = match attempt {
5187                Ok(data) => return Ok(data),
5188                Err(Attempt::Failed(error)) => return Err(error),
5189                Err(Attempt::Limited(limited)) => limited,
5190            };
5191            let (wait, cause) = limited.wait(backoff);
5192            let remaining = self.pacing.retry_budget.saturating_sub(waited);
5193            // A wait of nothing spends none of the budget, so it is exhaustion rather
5194            // than a retry. `Pacing::resolve` rules out every way of configuring one
5195            // except a budget of zero, where reporting the first refusal is the ask.
5196            if wait.is_zero() || wait > remaining {
5197                return Err(limited.exhausted(
5198                    doing,
5199                    waits,
5200                    waited,
5201                    wait,
5202                    self.pacing.retry_budget,
5203                ));
5204            }
5205            self.clock.sleep_because(wait, cause).await;
5206            waited += wait;
5207            waits += 1;
5208            backoff = backoff.saturating_mul(2);
5209        }
5210    }
5211
5212    /// Hold one request charging `charge` until every secondary window it charges has room for
5213    /// the whole of it — and, for a mutation of an instance configured with
5214    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5215    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5216    ///
5217    /// The moment is decided and counted under the lock and waited out outside it, so two
5218    /// callers take two places rather than the same one, and no lock is held across an await.
5219    /// A request with room goes out at once: nothing here waits that a window does not ask for.
5220    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5221        let (wait, cause, ticket) = {
5222            // A poisoned lock here costs pacing, not correctness, and refusing the write over
5223            // it would turn an earlier failure into a second one for no gain.
5224            let mut windows = self
5225                .windows
5226                .lock()
5227                .unwrap_or_else(std::sync::PoisonError::into_inner);
5228            let now = self.clock.now();
5229            let (mut at, mut cause) = if self.pacing.windows {
5230                windows.room(now, charge)
5231            } else {
5232                (now, None)
5233            };
5234            if mutation && !self.pacing.min_mutation_interval.is_zero() {
5235                let mut last = self
5236                    .last_mutation
5237                    .lock()
5238                    .unwrap_or_else(std::sync::PoisonError::into_inner);
5239                // `checked_add` rather than `+`: adding durations can panic on overflow, and
5240                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5241                if let Some(earliest) = last
5242                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5243                    && earliest > at
5244                {
5245                    at = earliest;
5246                    cause = Some(pacing::cause::MIN_INTERVAL);
5247                }
5248                *last = Some(at);
5249            }
5250            let ticket = windows.charge(at, charge);
5251            (at.saturating_sub(now), cause, ticket)
5252        };
5253        if let (false, Some(cause)) = (wait.is_zero(), cause) {
5254            self.clock.sleep_because(wait, cause).await;
5255        }
5256        ticket
5257    }
5258
5259    /// Record that the request `ticket` was charged for has finished, so each window — and
5260    /// the configured spacing — counts it from here rather than from the moment it left.
5261    ///
5262    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5263    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5264    /// departure would hand the limiter a window that looked roomier than it is, so a source
5265    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5266    /// paced well inside a board's threshold was once refused by it on a slower machine while
5267    /// passing on a quick one. The previous request had arrived before its response came back,
5268    /// so counting from here is never earlier than the limiter does, whatever transit costs
5269    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5270    /// reads for the next fifty minutes.
5271    ///
5272    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5273    /// one that never left costs only room nobody needed.
5274    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5275        let now = self.clock.now();
5276        self.windows
5277            .lock()
5278            .unwrap_or_else(std::sync::PoisonError::into_inner)
5279            .finish(ticket, now);
5280        if mutation && !self.pacing.min_mutation_interval.is_zero() {
5281            let mut last = self
5282                .last_mutation
5283                .lock()
5284                .unwrap_or_else(std::sync::PoisonError::into_inner);
5285            // `max` rather than an assignment: a concurrent caller may already have reserved a
5286            // slot further out, and completing this request must never pull that slot back in.
5287            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5288        }
5289    }
5290
5291    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5292    /// failure that waiting cannot help — and recorded, whichever of the three it was.
5293    ///
5294    /// This is the one place a request leaves this crate, which is why the accounting is
5295    /// here rather than at each of the callers: a read path added later is counted without
5296    /// anybody remembering to count it, and
5297    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5298    /// when one is not.
5299    async fn send_once(
5300        &self,
5301        query: &str,
5302        variables: &Value,
5303        number: u32,
5304    ) -> Result<Value, Attempt> {
5305        let executed = self.ledger.priced_document(query);
5306        let Attempted {
5307            result,
5308            limits,
5309            reported_cost,
5310            response_outcome,
5311        } = self.attempt(executed.as_ref(), variables).await;
5312        // No `otherwise` name: every document this source sends is one of its own, and the
5313        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5314        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5315        let outcome = match &result {
5316            Ok(_) => accounting::Outcome::Answered,
5317            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5318            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5319        };
5320        self.ledger
5321            .record(sending.finished(outcome, limits).with_execution(
5322                Some(executed.into_owned()),
5323                response_outcome,
5324                number,
5325            ));
5326        result
5327    }
5328
5329    /// The attempt itself, with what its response said about the rate limit alongside.
5330    ///
5331    /// The two are returned together rather than recorded here because every one of the
5332    /// early exits below is a different outcome, and a record written at each of them is a
5333    /// record one of them can be added without.
5334    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5335        let mut limits = accounting::RateLimit::default();
5336        let mut reported_cost = None;
5337        let mut response_outcome = accounting::ResponseOutcome::TransportError;
5338        let result = self
5339            .attempted(
5340                query,
5341                variables,
5342                &mut limits,
5343                &mut reported_cost,
5344                &mut response_outcome,
5345            )
5346            .await;
5347        Attempted {
5348            result,
5349            limits,
5350            reported_cost,
5351            response_outcome,
5352        }
5353    }
5354
5355    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5356    async fn attempted(
5357        &self,
5358        query: &str,
5359        variables: &Value,
5360        limits: &mut accounting::RateLimit,
5361        reported_cost: &mut Option<u64>,
5362        response_outcome: &mut accounting::ResponseOutcome,
5363    ) -> Result<Value, Attempt> {
5364        let response = self
5365            .client()
5366            .map_err(Attempt::Failed)?
5367            .post(self.endpoint.clone())
5368            .bearer_auth(self.token.expose_secret())
5369            .json(&json!({"query": query, "variables": variables}))
5370            .send()
5371            .await
5372            .map_err(|e| {
5373                Attempt::Failed(SourceError::Unavailable {
5374                    message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5375                })
5376            })?;
5377        let status = response.status();
5378        if !status.is_success() {
5379            *response_outcome = accounting::ResponseOutcome::HttpError;
5380        }
5381        let header = |name: &str| whole_seconds(response.headers().get(name));
5382        let served = response
5383            .headers()
5384            .get(reqwest::header::DATE)
5385            .and_then(|value| value.to_str().ok())
5386            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5387            .map(|at| at.with_timezone(&Utc));
5388        *limits = accounting::RateLimit::read(|name| {
5389            response
5390                .headers()
5391                .get(name)
5392                .and_then(|value| value.to_str().ok())
5393                .map(str::to_owned)
5394        });
5395        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5396        // that are not text at all — is "not known to be exhausted". This never makes a
5397        // response a refusal on its own: it says which limiter a refusal is attributed to
5398        // and where its hint comes from, so a value this cannot read costs a hint rather
5399        // than an answer.
5400        let exhausted = response
5401            .headers()
5402            .get("x-ratelimit-remaining")
5403            .and_then(|value| value.to_str().ok())
5404            == Some("0");
5405        // `retry-after` is what GitHub asks for when it asks; when it does not and the
5406        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5407        // which is the same question answered as an absolute time. Nothing else here is a
5408        // hint, and a schedule is what answers a refusal that carries none.
5409        let retry_after = header("retry-after");
5410        let hinted_by = if retry_after.is_some() || !exhausted {
5411            Hint::RetryAfter
5412        } else {
5413            Hint::PrimaryReset
5414        };
5415        let hint = retry_after.or_else(|| {
5416            exhausted
5417                .then(|| header("x-ratelimit-reset"))
5418                .flatten()
5419                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5420        });
5421        // Read before it is parsed, because the evidence which tells a secondary rate
5422        // limit from a rejected credential is in the body of a response whose status says
5423        // only "forbidden" — and a non-success response was never parsed at all.
5424        let body = response.text().await.map_err(|e| {
5425            Attempt::Failed(SourceError::Unavailable {
5426                message: format!(
5427                    "GitHub GraphQL response could not be read: {}",
5428                    transport_error(&e)
5429                ),
5430            })
5431        })?;
5432        *reported_cost = serde_json::from_str::<Value>(&body)
5433            .ok()
5434            .as_ref()
5435            .and_then(|body| body.pointer("/data/rateLimit/cost"))
5436            .and_then(Value::as_u64);
5437        let parsed = serde_json::from_str::<Value>(&body).ok();
5438        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5439        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5440            return Err(Attempt::Limited(Limited {
5441                limiter,
5442                hint,
5443                hinted_by,
5444            }));
5445        }
5446        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5447            return Err(Attempt::Failed(SourceError::Auth {
5448                message: format!(
5449                    "GitHub rejected the configured credential with HTTP {status}; grant it Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board"
5450                ),
5451            }));
5452        }
5453        if !status.is_success() {
5454            return Err(Attempt::Failed(SourceError::Unavailable {
5455                message: format!("GitHub GraphQL returned HTTP {status}"),
5456            }));
5457        }
5458        let answered = if query == graphql::ISSUES_BY_REFERENCE {
5459            self.answer_unresolved(&body)
5460        } else if query == graphql::CREATE_ISSUES {
5461            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5462        } else {
5463            self.answer(&body)
5464        };
5465        let mut data = answered.map_err(Attempt::Failed)?;
5466        if let Some(served) = served {
5467            stamp_served(&mut data, &served.to_rfc3339());
5468        }
5469        Ok(data)
5470    }
5471
5472    /// What one successful HTTP response says, once its GraphQL errors are read.
5473    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5474    /// for this credential — a repository it cannot see, an issue number it does not hold —
5475    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5476    /// that reference rather than a failure of the others. Any other error refuses the read,
5477    /// as there.
5478    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5479        let parsed: Option<Value> = serde_json::from_str(body).ok();
5480        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5481        // `issue`, and the answer holds `null` at exactly that path: anything else — another
5482        // kind of error, one naming no slot, or one beside a value it says is not there — is a
5483        // refusal of the whole read.
5484        let unresolved = |data: &Value, error: &Value| {
5485            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5486                && error
5487                    .get("path")
5488                    .and_then(Value::as_array)
5489                    .is_some_and(|path| {
5490                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5491                            alias
5492                                .strip_prefix('r')
5493                                .and_then(|slot| slot.parse::<usize>().ok())
5494                                .filter(|slot| *slot < REFERENCE_BATCH)
5495                                .map(|_| alias)
5496                        });
5497                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5498                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5499                            (Some(alias), Some("issue"), 2) => data
5500                                .get(alias)
5501                                .and_then(|repository| repository.get("issue"))
5502                                .is_some_and(Value::is_null),
5503                            _ => false,
5504                        }
5505                    })
5506        };
5507        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5508            parsed.get("data").is_some_and(|data| {
5509                data.is_object()
5510                    && parsed
5511                        .get("errors")
5512                        .and_then(Value::as_array)
5513                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5514            })
5515        });
5516        match parsed {
5517            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5518            _ => self.answer(body),
5519        }
5520    }
5521
5522    fn answer(&self, body: &str) -> Result<Value, SourceError> {
5523        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5524            message: format!("GitHub returned invalid JSON: {e}"),
5525        })?;
5526        let errors = body
5527            .get("errors")
5528            .map(|value| {
5529                value.as_array().ok_or_else(|| SourceError::Malformed {
5530                    message: "GitHub response errors is not an array".into(),
5531                })
5532            })
5533            .transpose()?;
5534        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5535            let messages = errors
5536                .iter()
5537                .filter_map(|e| e.get("message").and_then(Value::as_str))
5538                .collect::<Vec<_>>()
5539                .join("; ");
5540            let message = if messages.is_empty() {
5541                "GitHub returned GraphQL errors".into()
5542            } else {
5543                messages
5544            };
5545            let normalized = message.to_ascii_lowercase();
5546            if normalized.contains("resource not accessible") || normalized.contains("scope") {
5547                return Err(SourceError::Auth {
5548                    message: format!(
5549                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5550                        self.credential_name
5551                    ),
5552                });
5553            }
5554            return Err(SourceError::Refused { message });
5555        }
5556        body.get("data")
5557            .filter(|data| data.is_object())
5558            .cloned()
5559            .ok_or_else(|| SourceError::Malformed {
5560                message: "GitHub response has no data object".into(),
5561            })
5562    }
5563
5564    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5565    // GraphQL cannot independently page them inside the outer item page. This source page is
5566    // deliberately bounded at that published maximum; the live drift journey exercises it.
5567    async fn board_page(
5568        &self,
5569        items_after: Option<&str>,
5570        items_first: u32,
5571    ) -> Result<Value, SourceError> {
5572        let data = self
5573            .graphql(
5574                graphql::BOARD,
5575                json!({"owner":self.owner,"number":self.project_number,
5576                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5577                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5578            )
5579            .await?;
5580        data.pointer("/owner/projectV2")
5581            .filter(|v| !v.is_null())
5582            .cloned()
5583            .ok_or_else(|| SourceError::Refused {
5584                message: format!(
5585                    "GitHub project {}/{} was not found or is not visible to the token",
5586                    self.owner, self.project_number
5587                ),
5588            })
5589    }
5590
5591    /// The search that finds the issues of this board, narrowed by `also` when it is
5592    /// given.
5593    ///
5594    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5595    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5596    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5597    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5598    /// from a task by the `parent` field each issue carries rather than by the search.
5599    fn board_search(&self, also: Option<&str>) -> String {
5600        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5601        match also {
5602            Some(also) => format!("{scope} {also}"),
5603            None => scope,
5604        }
5605    }
5606
5607    /// One issue this source reached directly, as the board item a read of the board would
5608    /// have produced — or `None` when this board does not hold it.
5609    ///
5610    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5611    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5612    /// item's own id, that item's field values, and the issue as its content. One resolver
5613    /// for both routes is what makes an issue read through a search, through its own node
5614    /// id, or through its project's sub-issues report the same title, the same status, the
5615    /// same labels and the same qualified id.
5616    ///
5617    /// An issue with no entry for *this* board is not this source's to report, which is
5618    /// what keeps an id naming some other repository's issue from being answered as an item
5619    /// of this board. That answer is given about an **exhausted** connection and never
5620    /// about an unread page: the entry is looked for on the page in hand, and only if that
5621    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5622    /// rest of it.
5623    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5624        self.resolve_issue_as(issue, true).await
5625    }
5626
5627    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5628    /// and writes only when `remember` says to.
5629    async fn resolve_issue_as(
5630        &self,
5631        issue: &Value,
5632        remember: bool,
5633    ) -> Result<Option<Resolved>, SourceError> {
5634        if optional_str(issue, "__typename")? != Some("Issue") {
5635            return Ok(None);
5636        }
5637        let memberships = issue
5638            .get("projectItems")
5639            .ok_or_else(|| SourceError::Malformed {
5640                message: "GitHub issue is missing projectItems".into(),
5641            })?;
5642        let nodes = memberships
5643            .get("nodes")
5644            .and_then(Value::as_array)
5645            .ok_or_else(|| SourceError::Malformed {
5646                message: "GitHub issue projectItems.nodes is not an array".into(),
5647            })?;
5648        let held = match self.board_entry(nodes) {
5649            Some(held) => held.clone(),
5650            None => {
5651                let info = memberships
5652                    .get("pageInfo")
5653                    .ok_or_else(|| SourceError::Malformed {
5654                        message: "GitHub issue projectItems has no pageInfo".into(),
5655                    })?;
5656                // The page held no entry for this board. Whether that means the issue is
5657                // not on it is a question about the rest of the connection, and only a
5658                // connection with no rest answers it here.
5659                if !required_bool(info, "hasNextPage")? {
5660                    return Ok(None);
5661                }
5662                let cursor = required_str(info, "endCursor")?;
5663                validate_cursor_progress(None, cursor)?;
5664                let issue_id = required_str(issue, "id")?;
5665                match self.board_membership(issue_id, cursor).await? {
5666                    Some(held) => held,
5667                    None => return Ok(None),
5668                }
5669            }
5670        };
5671        // The membership's own `updatedAt` rides along: it is the board half of the item's change
5672        // token, which a field change moves where the issue's own time does not.
5673        let item = json!({
5674            "id": required_str(&held, "id")?,
5675            "updatedAt": held.get("updatedAt"),
5676            "project": held.get("project"),
5677            "fieldValues": held.get("fieldValues"),
5678            "content": issue,
5679        });
5680        if remember {
5681            self.resolve(&item)
5682        } else {
5683            self.resolve_unremembered(&item)
5684        }
5685    }
5686
5687    /// This board's own entry among one page of an issue's `Issue.projectItems`.
5688    ///
5689    /// One spelling of *which membership is this board's*, so the page a read carries and
5690    /// the pages [`Self::board_membership`] walks are searched by the same rule.
5691    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5692        nodes.iter().find(|node| {
5693            node.pointer("/project/number").and_then(Value::as_u64)
5694                == Some(u64::from(self.project_number))
5695        })
5696    }
5697
5698    /// The rest of one issue's board memberships, from `after`, for this board's entry.
5699    ///
5700    /// The recovery read: a page of memberships that holds no entry for this board says
5701    /// nothing about the memberships past it, so the connection is walked to exhaustion
5702    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5703    /// positive answer — the whole connection was read and no entry named this board —
5704    /// rather than a failure, and the walk is held to
5705    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5706    /// with a cursor that does not advance is refused instead of spun on.
5707    async fn board_membership(
5708        &self,
5709        issue: &str,
5710        after: &str,
5711    ) -> Result<Option<Value>, SourceError> {
5712        let mut after = after.to_owned();
5713        loop {
5714            let data = self
5715                .graphql(
5716                    graphql::ISSUE_BOARD_ITEMS,
5717                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5718                           "nestedFirst":NESTED_PAGE_SIZE}),
5719                )
5720                .await?;
5721            let Some(connection) = data
5722                .pointer("/node/projectItems")
5723                .filter(|value| !value.is_null())
5724            else {
5725                // The id resolved to nothing, or to something with no memberships to walk —
5726                // which is the same answer as a connection holding no entry for this board.
5727                return Ok(None);
5728            };
5729            let nodes = connection
5730                .get("nodes")
5731                .and_then(Value::as_array)
5732                .ok_or_else(|| SourceError::Malformed {
5733                    message: "GitHub issue projectItems.nodes is not an array".into(),
5734                })?;
5735            if let Some(held) = self.board_entry(nodes) {
5736                return Ok(Some(held.clone()));
5737            }
5738            let info = connection
5739                .get("pageInfo")
5740                .ok_or_else(|| SourceError::Malformed {
5741                    message: "GitHub issue projectItems has no pageInfo".into(),
5742                })?;
5743            let next = required_bool(info, "hasNextPage")?
5744                .then(|| required_str(info, "endCursor"))
5745                .transpose()?;
5746            match next {
5747                Some(next) => {
5748                    validate_cursor_progress(Some(&after), next)?;
5749                    after = next.to_owned();
5750                }
5751                None => return Ok(None),
5752            }
5753        }
5754    }
5755
5756    /// One page of a board-scoped issue search, and where the next page resumes.
5757    async fn search_page(
5758        &self,
5759        search: &str,
5760        first: u32,
5761        after: Option<&str>,
5762    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5763        let data = self
5764            .graphql(
5765                graphql::SEARCH_ISSUES,
5766                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5767                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5768                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5769            )
5770            .await?;
5771        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5772            message: "GitHub search response has no search connection".into(),
5773        })?;
5774        let mut found = Vec::new();
5775        for node in connection
5776            .get("nodes")
5777            .and_then(Value::as_array)
5778            .ok_or_else(|| SourceError::Malformed {
5779                message: "GitHub search nodes is not an array".into(),
5780            })?
5781        {
5782            if let Some(resolved) = self.resolve_issue(node).await? {
5783                found.push(resolved);
5784            }
5785        }
5786        let info = connection
5787            .get("pageInfo")
5788            .ok_or_else(|| SourceError::Malformed {
5789                message: "GitHub search connection has no pageInfo".into(),
5790            })?;
5791        let next = required_bool(info, "hasNextPage")?
5792            .then(|| required_str(info, "endCursor"))
5793            .transpose()?
5794            .map(str::to_owned);
5795        if let Some(next) = &next {
5796            validate_cursor_progress(after, next)?;
5797        }
5798        Ok((found, next))
5799    }
5800
5801    /// Every issue this board holds, completed with what this run wrote.
5802    ///
5803    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5804    /// is an index and is eventually consistent, so an issue this run created seconds ago
5805    /// can be absent from it, and a project listed straight after being written would
5806    /// otherwise be missing from its own board. What is added back is only what this
5807    /// process itself wrote, out of [`Self::created`], which lives and dies with the
5808    /// process.
5809    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5810        let found = self.searched_issues().await?;
5811        self.completed_with_written(found, |_| true)
5812    }
5813
5814    /// Every issue this board's own search reports, walked to exhaustion, read once per
5815    /// source.
5816    ///
5817    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5818    /// needs it too and the two would otherwise walk the same search twice in one command.
5819    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5820    /// is.
5821    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5822        let cached = self.search_cache()?.clone();
5823        if let Some(held) = cached {
5824            return Ok(held);
5825        }
5826        let mut after: Option<String> = None;
5827        let mut found = Vec::new();
5828        let search = self.board_search(None);
5829        loop {
5830            let (page, next) = self
5831                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5832                .await?;
5833            found.extend(page);
5834            match next {
5835                Some(next) => after = Some(next),
5836                None => break,
5837            }
5838        }
5839        *self.search_cache()? = Some(found.clone());
5840        Ok(found)
5841    }
5842
5843    /// Every issue this board's own search reports with `also` beside its scope, walked to
5844    /// exhaustion — held for no later read, which may not be narrowed alike.
5845    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5846        let mut after: Option<String> = None;
5847        let mut found = Vec::new();
5848        let search = self.board_search(Some(also));
5849        loop {
5850            let (page, next) = self
5851                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5852                .await?;
5853            found.extend(page);
5854            match next {
5855                Some(next) => after = Some(next),
5856                None => return Ok(found),
5857            }
5858        }
5859    }
5860
5861    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5862    fn search_cache(
5863        &self,
5864    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5865        self.search_cache
5866            .lock()
5867            .map_err(|_| SourceError::Unavailable {
5868                message: "this source's view of the board's issues was left inconsistent by an \
5869                      earlier failure; next: run the command again"
5870                    .into(),
5871            })
5872    }
5873
5874    /// `found`, with everything this run wrote that `keep` accepts and the read did not
5875    /// report.
5876    ///
5877    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5878    /// at all: the search index is behind, and a node read of an item filed moments ago can
5879    /// be too.
5880    fn completed_with_written(
5881        &self,
5882        mut found: Vec<Resolved>,
5883        keep: impl Fn(&Resolved) -> bool,
5884    ) -> Result<Vec<Resolved>, SourceError> {
5885        for own in self.created()?.iter().filter(|own| keep(own)) {
5886            if !found.iter().any(|item| item.id == own.id) {
5887                found.push(own.clone());
5888            }
5889        }
5890        Ok(found)
5891    }
5892
5893    /// What resolving one node id reached.
5894    ///
5895    /// Three answers rather than an `Option`, because a board *draft* is none of the other
5896    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5897    /// is completed by a read of the draft itself rather than reported as nothing.
5898    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5899        let asked = self
5900            .graphql(
5901                graphql::ISSUE,
5902                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5903                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5904            )
5905            .await;
5906        let data = match asked {
5907            Ok(data) => data,
5908            // A string that is not a node id at all is not a failure to report: it is an id
5909            // this board does not hold, which is what every read of one already answers.
5910            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5911            Err(error) => return Err(error),
5912        };
5913        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5914            return Ok(Reached::Nothing);
5915        };
5916        if optional_str(node, "__typename")? == Some("DraftIssue") {
5917            return Ok(Reached::Draft);
5918        }
5919        Ok(match self.resolve_issue(node).await? {
5920            Some(item) => Reached::Held(Box::new(item)),
5921            None => Reached::Nothing,
5922        })
5923    }
5924
5925    /// One item of this board by its own id, whatever kind it is.
5926    ///
5927    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5928    /// run wrote is read first, because a node read of an item created moments ago can
5929    /// still be behind the board field values written onto it — see [`Self::created`].
5930    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5931        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5932            return Ok(Some(own.clone()));
5933        }
5934        if let Some(held) = self.read_ahead()?.remove(id) {
5935            return Ok(held);
5936        }
5937        match self.reach(id).await? {
5938            Reached::Held(item) => Ok(Some(*item)),
5939            Reached::Nothing => Ok(None),
5940            Reached::Draft => self.draft_by_id(id).await,
5941        }
5942    }
5943
5944    /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
5945    /// answers the project it is filed under and that project's members beside it.
5946    ///
5947    /// Each is held for this command as a read of it would have been: the project for the
5948    /// next read of it by its id, and — when its page of members is the whole of them — the
5949    /// members for the next listing of that project. So a write held to who may read the
5950    /// document, which reads all three, is answered by this one request.
5951    async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5952        if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
5953            return self.item_by_id(id).await;
5954        }
5955        let asked = self
5956            .graphql(
5957                graphql::DOCUMENT_IN_PROJECT,
5958                json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
5959                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5960                       "duplicates":true}),
5961            )
5962            .await;
5963        let data = match asked {
5964            Ok(data) => data,
5965            Err(error) if unresolvable_node(&error) => return Ok(None),
5966            Err(error) => return Err(error),
5967        };
5968        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5969            return Ok(None);
5970        };
5971        if optional_str(node, "__typename")? == Some("DraftIssue") {
5972            return self.draft_by_id(id).await;
5973        }
5974        let Some(item) = self.resolve_issue(node).await? else {
5975            return Ok(None);
5976        };
5977        // An answer for another issue than the one asked about says nothing of that one.
5978        if item.id != *id {
5979            return Err(SourceError::Malformed {
5980                message: format!(
5981                    "GitHub answered issue {} when asked for document {}",
5982                    item.id.0, id.0
5983                ),
5984            });
5985        }
5986        if let Some(project) = node.get("project").filter(|value| !value.is_null())
5987            && let Some(parent) = self
5988                .resolve_issue(project)
5989                .await?
5990                .filter(|parent| Some(&parent.id) == item.parent.as_ref())
5991        {
5992            let members = project.get("subIssues").filter(|value| !value.is_null());
5993            let whole = members
5994                .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
5995                .and_then(Value::as_bool)
5996                == Some(false);
5997            if whole {
5998                let nodes = members
5999                    .and_then(|members| members.get("nodes"))
6000                    .and_then(Value::as_array)
6001                    .ok_or_else(|| SourceError::Malformed {
6002                        message: "GitHub subIssues.nodes is not an array".into(),
6003                    })?;
6004                let mut children = Vec::new();
6005                for member in nodes {
6006                    if let Some(resolved) = self.resolve_issue(member).await? {
6007                        children.push(resolved);
6008                    }
6009                }
6010                // Held as the project's members only when every one resolved and says it is
6011                // filed under it: a page with any other answers nothing of all the members.
6012                if children.len() == nodes.len()
6013                    && children
6014                        .iter()
6015                        .all(|child| child.parent.as_ref() == Some(&parent.id))
6016                {
6017                    self.children_cache()?
6018                        .insert(parent.id.clone(), (parent.id.clone(), children));
6019                }
6020            }
6021            self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6022        }
6023        Ok(Some(item))
6024    }
6025
6026    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6027    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6028    /// than one request per id.
6029    ///
6030    /// What this run wrote answers first, as it does there, and only the rest is read. One id
6031    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6032    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6033    /// id at a time, so that id is answered as not held and the others as themselves; a draft
6034    /// is completed by a read of the draft, exactly as there.
6035    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6036        let mut found: Vec<Option<Option<Resolved>>> = {
6037            let created = self.created()?;
6038            ids.iter()
6039                .map(|id| {
6040                    created
6041                        .iter()
6042                        .find(|own| own.id == *id)
6043                        .map(|own| Some(own.clone()))
6044                })
6045                .collect()
6046        };
6047        let unread: Vec<NativeId> = ids
6048            .iter()
6049            .zip(&found)
6050            .filter(|(_, found)| found.is_none())
6051            .map(|(id, _)| id.clone())
6052            .collect();
6053        let mut read = Vec::with_capacity(unread.len());
6054        if let [one] = unread.as_slice() {
6055            read.push(self.item_by_id(one).await?);
6056        } else {
6057            for batch in unread.chunks(DETAIL_BATCH) {
6058                let data = match self
6059                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6060                    .await
6061                {
6062                    Ok(data) => data,
6063                    Err(error) if unresolvable_node(&error) => {
6064                        for id in batch {
6065                            read.push(self.item_by_id(id).await?);
6066                        }
6067                        continue;
6068                    }
6069                    Err(error) => return Err(error),
6070                };
6071                for (slot, id) in batch.iter().enumerate() {
6072                    let node =
6073                        data.get(format!("i{slot}"))
6074                            .ok_or_else(|| SourceError::Malformed {
6075                                message: format!(
6076                                    "GitHub answered a batch read with no item for {}",
6077                                    id.0
6078                                ),
6079                            })?;
6080                    read.push(if node.is_null() {
6081                        None
6082                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6083                        self.draft_by_id(id).await?
6084                    } else {
6085                        if optional_str(node, "__typename")? == Some("Issue")
6086                            && required_str(node, "id")? != id.0
6087                        {
6088                            return Err(SourceError::Malformed {
6089                                message: format!(
6090                                    "GitHub answered the read of {} with issue {}",
6091                                    id.0,
6092                                    required_str(node, "id")?
6093                                ),
6094                            });
6095                        }
6096                        self.resolve_issue(node).await?
6097                    });
6098                }
6099            }
6100        }
6101        let mut read = read.into_iter();
6102        Ok(found
6103            .iter_mut()
6104            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6105            .collect())
6106    }
6107
6108    fn read_ahead(
6109        &self,
6110    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6111        self.read_ahead
6112            .lock()
6113            .map_err(|_| SourceError::Unavailable {
6114                message:
6115                    "items read ahead for a copy were left inconsistent; run the command again"
6116                        .into(),
6117            })
6118    }
6119
6120    fn resolved_cache(
6121        &self,
6122    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6123        self.resolved_cache
6124            .lock()
6125            .map_err(|_| SourceError::Unavailable {
6126                message: "resolved item records were left inconsistent; run the command again"
6127                    .into(),
6128            })
6129    }
6130
6131    /// Reuse a record this invocation already resolved. The mutation sender invalidates
6132    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6133    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6134        let cached = self.resolved_cache()?.get(id).cloned();
6135        if let Some(item) = cached {
6136            return Ok(Some(item));
6137        }
6138        self.item_by_id(id).await
6139    }
6140
6141    /// Take out the item an issue read by reference reached for `id`, when one is held: the
6142    /// status write it was read for is the one caller, so no other read or write is ever
6143    /// answered from a read that selected only what that write needs.
6144    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6145        Ok(self
6146            .held_cache
6147            .lock()
6148            .map_err(|_| SourceError::Unavailable {
6149                message: "items read by issue reference were left inconsistent; run the \
6150                          command again"
6151                    .into(),
6152            })?
6153            .remove(id))
6154    }
6155
6156    /// One board draft by its own id, with the board item it sits in — or `None` when no
6157    /// item of this board is that draft's.
6158    ///
6159    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6160    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6161    /// links a draft to one board item, so the page this read carries is the whole of that
6162    /// connection, and a page that reports more than it holds is refused rather than read
6163    /// as an answer about memberships nobody read.
6164    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6165        let data = self
6166            .graphql(
6167                graphql::DRAFT,
6168                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6169                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6170            )
6171            .await?;
6172        // Gone between the two reads is an answer — the draft is no longer there. Anything
6173        // else than the draft [`Self::reach`] was just told this id is, is not one.
6174        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6175            return Ok(None);
6176        };
6177        if optional_str(draft, "__typename")? != Some("DraftIssue") {
6178            return Err(SourceError::Malformed {
6179                message: format!(
6180                    "GitHub answered {} as a draft and then as something else",
6181                    id.0
6182                ),
6183            });
6184        }
6185        if required_str(draft, "id")? != id.0 {
6186            return Err(SourceError::Malformed {
6187                message: format!("GitHub answered a different draft for {}", id.0),
6188            });
6189        }
6190        let memberships = draft
6191            .get("projectV2Items")
6192            .ok_or_else(|| SourceError::Malformed {
6193                message: format!("GitHub draft {} is missing projectV2Items", id.0),
6194            })?;
6195        let nodes = memberships
6196            .get("nodes")
6197            .and_then(Value::as_array)
6198            .ok_or_else(|| SourceError::Malformed {
6199                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6200            })?;
6201        let info = memberships
6202            .get("pageInfo")
6203            .ok_or_else(|| SourceError::Malformed {
6204                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6205            })?;
6206        // Read whether or not this board's entry is on the page: a page claiming more than
6207        // the one item GitHub links a draft to is a malformed answer either way.
6208        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6209            return Err(SourceError::Malformed {
6210                message: format!(
6211                    "GitHub draft {} reports more board items than the one GitHub links a draft \
6212                     to",
6213                    id.0
6214                ),
6215            });
6216        }
6217        if let Some(node) = nodes.first()
6218            && node
6219                .pointer("/project/number")
6220                .and_then(Value::as_u64)
6221                .is_none()
6222        {
6223            return Err(SourceError::Malformed {
6224                message: format!(
6225                    "GitHub draft {} board item has no numeric project number",
6226                    id.0
6227                ),
6228            });
6229        }
6230        let Some(held) = self.board_entry(nodes) else {
6231            return Ok(None);
6232        };
6233        if required_str(
6234            held.get("project").ok_or_else(|| SourceError::Malformed {
6235                message: format!("GitHub draft {} board item has no project", id.0),
6236            })?,
6237            "id",
6238        )? != self.board_fields().await?.id.as_str()
6239        {
6240            return Ok(None);
6241        }
6242        let item = json!({
6243            "id": required_str(held, "id")?,
6244            "project": held.get("project"),
6245            "fieldValues": held.get("fieldValues"),
6246            "content": draft,
6247        });
6248        self.resolve(&item)
6249    }
6250
6251    /// The board's own id and field definitions, for a write whose item does not carry
6252    /// them — never its items.
6253    ///
6254    /// A board this command has already listed supplies them, since it read them beside its
6255    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6256    /// is consulted about which items the board holds: see the module documentation for
6257    /// why a question about one known item is answered by reading that item.
6258    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6259        if let Some(board) = self.board_cache()?.as_ref() {
6260            return Ok(BoardFields {
6261                id: BoardId::parse(&board.id)?,
6262                fields: board.fields.clone(),
6263            });
6264        }
6265        if let Some(held) = self.fields_cache()?.clone() {
6266            return Ok(held);
6267        }
6268        let data = self
6269            .graphql(
6270                graphql::BOARD_FIELDS,
6271                json!({"owner":self.owner,"number":self.project_number,
6272                       "nestedFirst":NESTED_PAGE_SIZE}),
6273            )
6274            .await?;
6275        self.fields_read(&data)
6276    }
6277
6278    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6279    /// the rest of this command.
6280    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6281        let board = data
6282            .pointer("/boardFields/projectV2")
6283            .filter(|value| !value.is_null())
6284            .ok_or_else(|| SourceError::Refused {
6285                message: format!(
6286                    "GitHub project {}/{} was not found or is not visible to the token",
6287                    self.owner, self.project_number
6288                ),
6289            })?;
6290        let read = BoardFields {
6291            id: BoardId::parse(required_str(board, "id")?)?,
6292            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6293        };
6294        *self.fields_cache()? = Some(read.clone());
6295        Ok(read)
6296    }
6297
6298    /// Read what creating an issue in `repository` needs and this command has not read yet —
6299    /// the board's fields and the repository's node id — in one request when it needs both.
6300    ///
6301    /// When either is already known this sends nothing, and the other is read by its own
6302    /// document where it is asked for, so no create reads anything twice.
6303    async fn creation_context(
6304        &self,
6305        repository: &RepositoryTarget,
6306        incoming: &Incoming<'_>,
6307    ) -> Result<(), SourceError> {
6308        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6309        if fields_known || self.repository_cache()?.contains_key(repository) {
6310            return Ok(());
6311        }
6312        let data = self
6313            .graphql(
6314                graphql::CREATION_CONTEXT,
6315                json!({"owner":self.owner,"number":self.project_number,
6316                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6317                       "repositoryName":repository.name}),
6318            )
6319            .await?;
6320        self.fields_read(&data)?;
6321        self.repository_read(&data, repository, incoming)?;
6322        Ok(())
6323    }
6324
6325    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6326    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6327        self.fields_cache
6328            .lock()
6329            .map_err(|_| SourceError::Unavailable {
6330                message: "this source's view of the board's fields was left inconsistent by an \
6331                      earlier failure; next: run the command again"
6332                    .into(),
6333            })
6334    }
6335
6336    /// What a write to `item` needs of the board, read off that item when it says enough and
6337    /// off [`Self::board_fields`] when it does not.
6338    ///
6339    /// A node read of an item names its board and carries the definition of every field it
6340    /// holds a value of — so an item naming its board, holding a value of the origin field
6341    /// when the write carries an origin, and, when the write carries a status, holding a
6342    /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6343    /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6344    /// as absent would refuse a write the board can take or skip a field write the board
6345    /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6346    /// from their own read instead. A write carrying no origin to an item holding none — every
6347    /// write out of a source declared private — writes no origin whatever the board defines,
6348    /// so it needs no definition of that field.
6349    async fn fields_for(
6350        &self,
6351        item: Option<&Resolved>,
6352        writes_origin: bool,
6353        writes_status: bool,
6354        selects_priority: bool,
6355    ) -> Result<BoardFields, SourceError> {
6356        if let Some(board) = item.and_then(Resolved::carried_board) {
6357            return Ok(board);
6358        }
6359        if let Some(item) = item
6360            && let Some(board_id) = item.named_board()
6361            && (!writes_origin || item.defines(ORIGIN_FIELD))
6362            && (!writes_status || item.defines("Status"))
6363            && (!selects_priority || item.defines(PRIORITY_FIELD))
6364            && self
6365                .metadata_fields
6366                .iter()
6367                .all(|projection| item.defines(&projection.field))
6368        {
6369            return Ok(BoardFields {
6370                id: board_id,
6371                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6372            });
6373        }
6374        self.board_fields().await
6375    }
6376
6377    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6378    /// when that id names nothing here with a sub-issue relationship to walk.
6379    ///
6380    /// `None` and an empty answer are different: `None` is *this is not an issue of this
6381    /// GitHub*, which is what sends a project selector on to be read as a name, and an
6382    /// empty vector is a project that holds nothing.
6383    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6384        let mut after: Option<String> = None;
6385        let mut children = Vec::new();
6386        // The project issue as the first page carried it, held once the walk has ended.
6387        let mut parent: Option<Resolved> = None;
6388        loop {
6389            let asked = self
6390                .graphql(
6391                    graphql::SUB_ISSUES,
6392                    json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6393                           "nestedFirst":NESTED_PAGE_SIZE,
6394                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6395                )
6396                .await;
6397            let data = match asked {
6398                Ok(data) => data,
6399                // A string that is not a node id at all is not a failure to report: it is
6400                // the ordinary answer to a selector naming a project by its name.
6401                Err(error) if unresolvable_node(&error) => return Ok(None),
6402                Err(error) => return Err(error),
6403            };
6404            let Some(connection) = data
6405                .pointer("/node/subIssues")
6406                .filter(|value| !value.is_null())
6407            else {
6408                // No such node, or one with no sub-issue relationship — a board draft is
6409                // the one this board can really hold.
6410                return Ok(None);
6411            };
6412            if after.is_none()
6413                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6414            {
6415                parent = self
6416                    .resolve_issue(node)
6417                    .await?
6418                    .filter(|resolved| resolved.id == *id);
6419            }
6420            for node in connection
6421                .get("nodes")
6422                .and_then(Value::as_array)
6423                .ok_or_else(|| SourceError::Malformed {
6424                    message: "GitHub subIssues.nodes is not an array".into(),
6425                })?
6426            {
6427                if let Some(resolved) = self.resolve_issue(node).await? {
6428                    children.push(resolved);
6429                }
6430            }
6431            let info = connection
6432                .get("pageInfo")
6433                .ok_or_else(|| SourceError::Malformed {
6434                    message: "GitHub subIssues connection has no pageInfo".into(),
6435                })?;
6436            let next = required_bool(info, "hasNextPage")?
6437                .then(|| required_str(info, "endCursor"))
6438                .transpose()?;
6439            match next {
6440                Some(next) => {
6441                    validate_cursor_progress(after.as_deref(), next)?;
6442                    after = Some(next.to_owned());
6443                }
6444                None => {
6445                    // Held only once the walk has ended, so one failing part way holds
6446                    // nothing and every read it would have answered is made.
6447                    // The project issue the first page carried answers the next read of the
6448                    // project by its id in this command, as a read of it made now would: what
6449                    // classifying a project for a write reads straight after its members.
6450                    if let Some(parent) = &parent {
6451                        self.read_ahead()?
6452                            .entry(parent.id.clone())
6453                            .or_insert_with(|| Some(parent.clone()));
6454                    }
6455                    let mut carried = self.carried()?;
6456                    for item in parent.into_iter().chain(children.iter().cloned()) {
6457                        carried.insert(item.id.clone(), item);
6458                    }
6459                    return Ok(Some(children));
6460                }
6461            }
6462        }
6463    }
6464
6465    fn carried(
6466        &self,
6467    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6468        self.carried.lock().map_err(|_| SourceError::Unavailable {
6469            message: "items a project's listing carried were left inconsistent; run the \
6470                      command again"
6471                .into(),
6472        })
6473    }
6474
6475    /// Which issue of this board a project *name* is, or `None` when none is.
6476    ///
6477    /// One bounded query which filters on that name at the server, rather than a walk of
6478    /// every issue the board holds. The name is compared again here: the qualifier narrows
6479    /// what GitHub sends, and this source decides what it names.
6480    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6481        let search = self.board_search(Some(&title_qualifier(name)));
6482        let mut after = None;
6483        loop {
6484            let (candidates, next) = self
6485                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6486                .await?;
6487            if let Some(item) = candidates.into_iter().find(|item| {
6488                item.kind == BoardKind::Work(ItemKind::Project)
6489                    && item.title.eq_ignore_ascii_case(name)
6490            }) {
6491                return Ok(Some(item.id));
6492            }
6493            match next {
6494                Some(next) => after = Some(next),
6495                None => return Ok(None),
6496            }
6497        }
6498    }
6499
6500    /// Everything filed under one project of this board: the sub-issues of the issue that
6501    /// project is.
6502    ///
6503    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6504    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6505    /// gains projects, or as another project gains tasks.
6506    ///
6507    /// A qualified id names the issue and is asked for its sub-issues directly: one
6508    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6509    /// read as a project *name*, which costs the one bounded search
6510    /// [`Self::project_by_name`] makes.
6511    ///
6512    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6513    /// — and each answer is still cut to the items whose parent is this project, so one this
6514    /// process has since filed elsewhere is not reported here, and completed with what this
6515    /// process filed under it.
6516    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6517        let held = self.children_cache()?.get(selector).cloned();
6518        let (project, children) = match held {
6519            Some(held) => held,
6520            None => {
6521                let answered = match self.sub_issues(selector).await? {
6522                    Some(children) => (selector.clone(), children),
6523                    None => match self.project_by_name(&selector.0).await? {
6524                        Some(project) => {
6525                            let children = self.sub_issues(&project).await?.unwrap_or_default();
6526                            (project, children)
6527                        }
6528                        None => return Ok(Vec::new()),
6529                    },
6530                };
6531                self.children_cache()?
6532                    .insert(selector.clone(), answered.clone());
6533                answered
6534            }
6535        };
6536        let mut children: Vec<Resolved> = children
6537            .into_iter()
6538            .filter(|child| child.parent.as_ref() == Some(&project))
6539            .collect();
6540        for own in self.updated()?.iter() {
6541            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6542                children.push(own.clone());
6543            }
6544        }
6545        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6546    }
6547
6548    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6549    /// completed with what this run wrote — the candidates a comment-activity read confirms.
6550    ///
6551    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6552    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6553    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6554    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6555    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6556    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6557    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6558    /// rather than silently narrowing a caller's answer.
6559    ///
6560    /// The instant is written to the second, rounded down, which can only widen what the
6561    /// search returns; confirmation against each candidate's own comments is what makes the
6562    /// answer exact. The search is an index that lags a write by a second or two — the module
6563    /// documentation records it — so a caller that asks again from its last instant should
6564    /// overlap the two by more than that.
6565    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6566        let found = self.searched(&updated_qualifier(since)).await?;
6567        self.completed_with_written(found, |_| true)
6568    }
6569
6570    /// Every issue of this board GitHub's issue search reports for the board-scoped search
6571    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6572    ///
6573    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6574    /// own record is the fresher of the two.
6575    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6576        let search = self.board_search(Some(also));
6577        let mut after: Option<String> = None;
6578        let mut found = Vec::new();
6579        loop {
6580            let (page, next) = self
6581                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6582                .await?;
6583            found.extend(page);
6584            match next {
6585                Some(next) => after = Some(next),
6586                None => return Ok(found),
6587            }
6588        }
6589    }
6590
6591    /// A bounded task answer; the versioned cursor carries the connection position, how
6592    /// many rows of the page starting there were already handed out, and the own-write ids
6593    /// already observed, including across a new source instance.
6594    ///
6595    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6596    /// sliced from the pages it needs; why is the module documentation's paging contract.
6597    async fn search_tasks(
6598        &self,
6599        query: &TaskQuery,
6600        page: &PageRequest,
6601        also: &str,
6602    ) -> Result<Page<Task>, SourceError> {
6603        let mut position = match &page.cursor {
6604            None => SearchPosition::default(),
6605            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6606                .ok()
6607                .filter(|position| {
6608                    position.version == SEARCH_CURSOR_VERSION
6609                        && position.connection.valid_resume(position.offset)
6610                })
6611                .ok_or_else(|| SourceError::Config {
6612                    message: "page cursor is invalid".into(),
6613                })?,
6614        };
6615        let search = self.board_search(Some(also));
6616        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6617        let own = self.with_own_writes(Vec::new())?;
6618        // An issue this process commented on is a candidate of a comment-activity read
6619        // whether or not the search has caught up with the comment; see `Self::commented`.
6620        let commented = match query.commented_since {
6621            Some(_) => self.commented()?.clone(),
6622            None => Vec::new(),
6623        };
6624        for id in own.iter().map(|item| &item.id).chain(&commented) {
6625            if !position.own.contains(id) {
6626                position.own.push(id.clone());
6627            }
6628        }
6629        let mut tasks = Vec::new();
6630        while !position.connection.exhausted() && tasks.len() < limit {
6631            let first = SEARCH_PAGE_SIZE;
6632            // Page size is part of the key: a short cached answer cannot answer a wider ask.
6633            let key =
6634                serde_json::to_string(&("page", &search, &position.connection.after(), first))
6635                    .expect("search page key is serializable");
6636            let cached = if query.commented_since.is_none() {
6637                self.narrowed_cache()?.get(&key).cloned()
6638            } else {
6639                None
6640            };
6641            let (found, next) = match cached {
6642                Some(found) => {
6643                    let next = self
6644                        .search_next
6645                        .lock()
6646                        .map_err(|_| SourceError::Unavailable {
6647                            message:
6648                                "search pagination was left inconsistent; run the command again"
6649                                    .into(),
6650                        })?
6651                        .get(&key)
6652                        .cloned()
6653                        .flatten();
6654                    (found, next)
6655                }
6656                None => {
6657                    let (found, next) = self
6658                        .search_page(&search, first, position.connection.after())
6659                        .await?;
6660                    if query.commented_since.is_none() {
6661                        self.search_next
6662                            .lock()
6663                            .map_err(|_| SourceError::Unavailable {
6664                                message:
6665                                    "search pagination was left inconsistent; run the command again"
6666                                        .into(),
6667                            })?
6668                            .insert(key.clone(), next.clone());
6669                        self.narrowed_cache()?.insert(key, found.clone());
6670                    }
6671                    (found, next)
6672                }
6673            };
6674            let rows = found.len();
6675            for mut item in found.into_iter().skip(position.offset) {
6676                if tasks.len() == limit {
6677                    break;
6678                }
6679                position.offset += 1;
6680                if position.own.contains(&item.id) {
6681                    if position.seen.contains(&item.id) {
6682                        continue;
6683                    }
6684                    position.seen.push(item.id.clone());
6685                    // The search's own copy of an issue this process only commented on is as
6686                    // good as a node read of it, since its comments are read either way.
6687                    let only_commented = commented.contains(&item.id)
6688                        && !own.iter().any(|written| written.id == item.id);
6689                    if !only_commented {
6690                        let updated_at = item.updated_at;
6691                        let Some(written) = self.search_written(&own, &item.id).await? else {
6692                            continue;
6693                        };
6694                        item = written;
6695                        item.updated_at = item.updated_at.max(updated_at);
6696                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6697                    }
6698                }
6699                if item.kind == BoardKind::Work(ItemKind::Task) {
6700                    let task = item.task()?;
6701                    if task_matches(&task, query, &query.project)
6702                        && self.commented_since(&item, query.commented_since).await?
6703                    {
6704                        tasks.push(task);
6705                    }
6706                }
6707            }
6708            if position.offset < rows {
6709                continue;
6710            }
6711            position.offset = 0;
6712            position.connection = match next {
6713                Some(after) => SearchConnection::Continuing {
6714                    after: Cursor(after),
6715                },
6716                None => SearchConnection::Exhausted {},
6717            };
6718        }
6719        if position.connection.exhausted() {
6720            for id in position.own.clone() {
6721                if position.seen.contains(&id) {
6722                    continue;
6723                }
6724                if tasks.len() == limit {
6725                    break;
6726                }
6727                position.seen.push(id.clone());
6728                let Some(item) = self.search_written(&own, &id).await? else {
6729                    continue;
6730                };
6731                if item.kind == BoardKind::Work(ItemKind::Task) {
6732                    let task = item.task()?;
6733                    if task_matches(&task, query, &query.project)
6734                        && self.commented_since(&item, query.commented_since).await?
6735                    {
6736                        tasks.push(task);
6737                    }
6738                }
6739            }
6740        }
6741        let more = !position.connection.exhausted()
6742            || position.own.iter().any(|id| !position.seen.contains(id));
6743        Ok(Page {
6744            items: tasks,
6745            next: more.then(|| {
6746                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6747            }),
6748        })
6749    }
6750
6751    /// A resumed process has the ids but no write records; resolve only a record the
6752    /// current page needs, by its uncached node read rather than the lagging search index.
6753    async fn search_written(
6754        &self,
6755        own: &[Resolved],
6756        id: &NativeId,
6757    ) -> Result<Option<Resolved>, SourceError> {
6758        match own.iter().find(|item| item.id == *id) {
6759            Some(item) => Ok(Some(item.clone())),
6760            None => self.item_by_id(id).await,
6761        }
6762    }
6763
6764    /// The candidates for a task query carrying a text, metadata or origin predicate, read
6765    /// without enumerating the board — or `None` for a query carrying none of the three, which
6766    /// keeps the reads it always had.
6767    ///
6768    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6769    /// because it names at most a handful of items. Text and metadata are answered by one
6770    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6771    /// further by `updated:>=` when the query also asks for comment activity, since both
6772    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6773    /// process afterwards by the same predicates [`task_matches`] applies to every read.
6774    ///
6775    /// Completed with what this process wrote, its own record winning over the index's copy
6776    /// of the same item: see [`Self::with_own_writes`].
6777    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6778        self.send_pending_origins(None).await?;
6779        let asked = match (&query.origin, narrowing_qualifiers(query)) {
6780            (Some(origin), _) => Narrowing::Origin(origin.clone()),
6781            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6782                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6783                None => qualifiers,
6784            }),
6785            (None, None) => return Ok(None),
6786        };
6787        // A question about comment activity is asked afresh every time, as it always was: it
6788        // is the one a caller polls from one source while waiting for the index, and an
6789        // answer held from the first poll would be the answer to every later one.
6790        let key = query.commented_since.is_none().then(|| asked.key());
6791        let cached = match &key {
6792            Some(key) => self.narrowed_cache()?.get(key).cloned(),
6793            None => None,
6794        };
6795        let found = match cached {
6796            Some(found) => found,
6797            None => {
6798                let found = match &asked {
6799                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6800                    Narrowing::Search(also) => self.searched(also).await?,
6801                };
6802                if let Some(key) = key {
6803                    self.narrowed_cache()?.insert(key, found.clone());
6804                }
6805                found
6806            }
6807        };
6808        self.with_own_writes(found).map(Some)
6809    }
6810
6811    /// The candidates for a project or unscoped document query carrying a searchable text,
6812    /// read without enumerating the board — or `None` for a query with no text or a blank one,
6813    /// which keeps the read it always had.
6814    ///
6815    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6816    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6817    /// costs is the issues that match and never the board. Its answer is held for the command
6818    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6819    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6820    /// substring rule, exactly as an item of the wider read was, and is completed with what this
6821    /// process wrote: see [`Self::with_own_writes`].
6822    async fn text_searched(
6823        &self,
6824        text: Option<&TextQuery>,
6825    ) -> Result<Option<Vec<Resolved>>, SourceError> {
6826        let Some(also) = text_qualifiers(text) else {
6827            return Ok(None);
6828        };
6829        let key = Narrowing::Search(also.clone()).key();
6830        let cached = self.narrowed_cache()?.get(&key).cloned();
6831        let found = match cached {
6832            Some(found) => found,
6833            None => {
6834                let found = self.searched(&also).await?;
6835                self.narrowed_cache()?.insert(key, found.clone());
6836                found
6837            }
6838        };
6839        self.with_own_writes(found).map(Some)
6840    }
6841
6842    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6843    /// [`Self::narrowed`] reads for a task query carrying that origin — one
6844    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6845    /// what this process wrote — each confirmed against its own origin entry.
6846    ///
6847    /// What answers a project or document listing by origin, so finding every copy of one plan
6848    /// costs the items that carry its origin and never a page of the board.
6849    async fn copied_from(
6850        &self,
6851        origin: &str,
6852        kind: BoardKind,
6853    ) -> Result<Vec<Resolved>, SourceError> {
6854        let wanted = TaskQuery {
6855            origin: Some(origin.to_owned()),
6856            ..TaskQuery::default()
6857        };
6858        Ok(self
6859            .narrowed(&wanted)
6860            .await?
6861            .unwrap_or_default()
6862            .into_iter()
6863            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6864            .collect())
6865    }
6866
6867    /// Every item of this board that may carry `origin` — a superset of those that do — found
6868    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6869    ///
6870    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6871    /// which reads the field every carrier holds, whichever release wrote it — and the
6872    /// board-scoped issue search for the same id as a phrase in the body, where this source
6873    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6874    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6875    /// query's, exactly.
6876    ///
6877    /// Both connections are walked to exhaustion, each from its own cursor. One that has
6878    /// already ended is sent its last cursor again, which answers an empty page, so the one
6879    /// document serves every page of either. What the two leave is stated in the module
6880    /// documentation: a carrier another process added within the last second or two, before
6881    /// either index has it.
6882    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6883        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6884        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6885        let mut items_after: Option<String> = None;
6886        let mut search_after: Option<String> = None;
6887        let mut found: Vec<Resolved> = Vec::new();
6888        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6889            if !found.iter().any(|held| held.id == resolved.id) {
6890                found.push(resolved);
6891            }
6892        };
6893        loop {
6894            let data = self
6895                .graphql(
6896                    graphql::ORIGIN_LOOKUP,
6897                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6898                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6899                           "itemsAfter":items_after,"searchAfter":search_after,
6900                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6901                           "duplicates":true}),
6902                )
6903                .await?;
6904            let items = data
6905                .pointer("/originItems/projectV2/items")
6906                .filter(|value| !value.is_null())
6907                .ok_or_else(|| SourceError::Refused {
6908                    message: format!(
6909                        "GitHub project {}/{} was not found or is not visible to the token",
6910                        self.owner, self.project_number
6911                    ),
6912                })?;
6913            for item in optional_nodes(Some(items), "project items")?
6914                .into_iter()
6915                .flatten()
6916            {
6917                // The board's own items list its drafts too, and a draft is not an issue: no
6918                // narrowed read answers with one, whatever its origin field holds.
6919                if let Some(resolved) = self.resolve(item)?
6920                    && resolved.content_kind == ContentKind::Issue
6921                {
6922                    keep(resolved, &mut found);
6923                }
6924            }
6925            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6926                message: "GitHub search response has no search connection".into(),
6927            })?;
6928            for node in optional_nodes(Some(searched), "search")?
6929                .into_iter()
6930                .flatten()
6931            {
6932                if let Some(resolved) = self.resolve_issue(node).await? {
6933                    keep(resolved, &mut found);
6934                }
6935            }
6936            let items_next = resumed(items, items_after.as_deref())?;
6937            let search_next = resumed(searched, search_after.as_deref())?;
6938            if !items_next.has_more() && !search_next.has_more() {
6939                return Ok(found);
6940            }
6941            items_after = items_next.cursor();
6942            search_after = search_next.cursor();
6943        }
6944    }
6945
6946    /// `found`, with every item this process created or wrote in its place, and every one of
6947    /// them the read did not report added.
6948    ///
6949    /// This process's own record wins over the read's copy of the same item, because a read
6950    /// of an item written moments ago can still be behind what was written onto it — the
6951    /// origin field included, which is the one a narrowed read is confirmed against — and a
6952    /// read that still names an item under a predicate this process's write moved it out of
6953    /// must not return it. The one thing the read knows that the record cannot is when GitHub
6954    /// last saw the item change, which is what a comment-activity read rules a candidate out
6955    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6956    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6957    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6958        // A board draft is not an issue, so no narrowed read returns one, and this process
6959        // having written one does not make it an answer either.
6960        let own: Vec<Resolved> = self
6961            .created()?
6962            .iter()
6963            .chain(self.updated()?.iter())
6964            .filter(|own| own.content_kind == ContentKind::Issue)
6965            .cloned()
6966            .collect();
6967        for mut own in own {
6968            self.resolved_cache()?.insert(own.id.clone(), own.clone());
6969            match found.iter_mut().find(|read| read.id == own.id) {
6970                Some(read) => {
6971                    own.updated_at = own.updated_at.max(read.updated_at);
6972                    *read = own;
6973                }
6974                None => found.push(own),
6975            }
6976        }
6977        Ok(found)
6978    }
6979
6980    /// Whether `item` has a comment created or last edited at or after `since` — always, when
6981    /// there is no instant to hold it to.
6982    ///
6983    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6984    /// or after the instant moved it there: an issue not updated since holds no such comment,
6985    /// and its comments are never asked for — unless this process commented on it in this
6986    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6987    /// only as far as the first that matches. A board draft is not an issue and has no
6988    /// comments, so it never matches.
6989    async fn commented_since(
6990        &self,
6991        item: &Resolved,
6992        since: Option<DateTime<Utc>>,
6993    ) -> Result<bool, SourceError> {
6994        let Some(since) = since else {
6995            return Ok(true);
6996        };
6997        if item.content_kind == ContentKind::DraftIssue {
6998            return Ok(false);
6999        }
7000        // An `updatedAt` this process's own record or a lagging index holds can predate a
7001        // comment this process wrote since, so only an issue it did not comment on is ruled
7002        // out by one.
7003        if item.updated_at.is_some_and(|updated| updated < since)
7004            && !self.commented()?.contains(&item.id)
7005        {
7006            return Ok(false);
7007        }
7008        let query = TaskQuery {
7009            commented_since: Some(since),
7010            ..TaskQuery::default()
7011        };
7012        let mut after: Option<String> = None;
7013        loop {
7014            let data = self
7015                .graphql(
7016                    graphql::ISSUE_COMMENTS,
7017                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7018                )
7019                .await?;
7020            let Some(connection) = data
7021                .get("node")
7022                .filter(|value| !value.is_null())
7023                .and_then(|node| node.get("comments"))
7024                .filter(|value| !value.is_null())
7025            else {
7026                // Removed since the search reported it: no longer an issue with comments.
7027                return Ok(false);
7028            };
7029            let comments = optional_nodes(Some(connection), "issue comments")?
7030                .into_iter()
7031                .flatten()
7032                .map(comment_from)
7033                .collect::<Result<Vec<_>, _>>()?;
7034            if query.comments_match(&comments) {
7035                return Ok(true);
7036            }
7037            match next_cursor(connection)? {
7038                Some(next) => {
7039                    validate_cursor_progress(after.as_deref(), &next.0)?;
7040                    after = Some(next.0);
7041                }
7042                None => return Ok(false),
7043            }
7044        }
7045    }
7046
7047    /// Every item on the board: the union of both enumerations GitHub offers of one.
7048    ///
7049    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7050    /// board **draft** and reads the board's own fields beside its items, and only the search
7051    /// reports an item that connection is behind on. The module documentation is where the lag and the
7052    /// measurements behind it are written down.
7053    ///
7054    /// A search result is admitted on the same terms as any other issue this source reaches
7055    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7056    /// names *this* board — so an issue the index still believes is here after it was taken
7057    /// off is refused rather than reported.
7058    ///
7059    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7060    /// which is what the cache could otherwise have broken.
7061    async fn board(&self) -> Result<Board, SourceError> {
7062        self.board_completed_by(None).await
7063    }
7064
7065    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7066    /// that half could add is then answered only if it holds.
7067    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7068        let cached = self.board_cache()?.clone();
7069        let mut board = match cached {
7070            Some(board) => board,
7071            None => {
7072                let read = self.read_board().await?;
7073                *self.board_cache()? = Some(read.clone());
7074                read
7075            }
7076        };
7077        let searched = match also {
7078            None => self.searched_issues().await?,
7079            Some(also) => self.searched_narrowly(also).await?,
7080        };
7081        for held in searched {
7082            if !board.items.iter().any(|item| item.id == held.id) {
7083                board.items.push(held);
7084            }
7085        }
7086        for own in self.created()?.iter() {
7087            if !board.items.iter().any(|item| item.id == own.id) {
7088                board.items.push(own.clone());
7089            }
7090        }
7091        Ok(board)
7092    }
7093
7094    /// This process's own view of the board, or the refusal a poisoned lock is.
7095    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7096        self.board_cache
7097            .lock()
7098            .map_err(|_| SourceError::Unavailable {
7099                message: "this source's view of the board was left inconsistent by an earlier \
7100                      failure; next: run the command again"
7101                    .into(),
7102            })
7103    }
7104
7105    /// Bring this process's own view of the board up to an item it has just written.
7106    ///
7107    /// A created item goes to `created`, which is what completes a board read GitHub's own
7108    /// eventual consistency has left behind. An item that was already there is replaced
7109    /// where it sits, so a second write of it in the same command reads its real parent
7110    /// rather than the one it had before the first write.
7111    ///
7112    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7113    /// that wins: an item this same run created is held in `created` and not in the cached
7114    /// board, and `board` completes the cached board *from* `created`, so replacing only
7115    /// the cached copy of such an item replaces nothing and the read still reports the
7116    /// title it was created with. The search is the third, and it is the one an item the
7117    /// board's own projection is behind on sits in *alone* — which is exactly the item this
7118    /// source is least able to re-read, so leaving it out would put the stale title back on
7119    /// the only items the completion in [`Self::board`] exists for.
7120    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7121        self.resolved_cache()?.insert(item.id.clone(), item.clone());
7122        if created {
7123            self.created()?.push(item);
7124            return Ok(());
7125        }
7126        {
7127            let mut own = self.created()?;
7128            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7129                *held = item;
7130                return Ok(());
7131            }
7132        }
7133        {
7134            let mut own = self.updated()?;
7135            match own.iter_mut().find(|held| held.id == item.id) {
7136                Some(held) => *held = item.clone(),
7137                None => own.push(item.clone()),
7138            }
7139        }
7140        if let Some(board) = self.board_cache()?.as_mut()
7141            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7142        {
7143            *held = item.clone();
7144        }
7145        if let Some(found) = self.search_cache()?.as_mut()
7146            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7147        {
7148            *held = item.clone();
7149        }
7150        for found in self.narrowed_cache()?.values_mut() {
7151            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7152                *held = item.clone();
7153            }
7154        }
7155        for (_, found) in self.children_cache()?.values_mut() {
7156            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7157                *held = item.clone();
7158            }
7159        }
7160        Ok(())
7161    }
7162
7163    /// Forget one item this process has just deleted, from every half of its own view.
7164    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7165        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7166            created.retain(|held| held.content != *id);
7167        }
7168        self.resolved_cache()?.remove(id);
7169        self.read_ahead()?.remove(id);
7170        self.carried()?.remove(id);
7171        self.created()?.retain(|own| own.id != *id);
7172        self.updated()?.retain(|own| own.id != *id);
7173        self.commented()?.retain(|own| own != id);
7174        if let Some(board) = self.board_cache()?.as_mut() {
7175            board.items.retain(|item| item.id != *id);
7176        }
7177        if let Some(found) = self.search_cache()?.as_mut() {
7178            found.retain(|item| item.id != *id);
7179        }
7180        for found in self.narrowed_cache()?.values_mut() {
7181            found.retain(|item| item.id != *id);
7182        }
7183        for (_, found) in self.children_cache()?.values_mut() {
7184            found.retain(|item| item.id != *id);
7185        }
7186        Ok(())
7187    }
7188
7189    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7190    fn narrowed_cache(
7191        &self,
7192    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7193        self.narrowed_cache
7194            .lock()
7195            .map_err(|_| SourceError::Unavailable {
7196                message: "this source's view of a narrowed read was left inconsistent by an \
7197                      earlier failure; next: run the command again"
7198                    .into(),
7199            })
7200    }
7201
7202    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7203    /// is.
7204    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7205    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7206        Ok(self.task_children()?.get(project).cloned())
7207    }
7208
7209    fn task_children(
7210        &self,
7211    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7212        self.task_children
7213            .lock()
7214            .map_err(|_| SourceError::Unavailable {
7215                message: "this source's record of a project's tasks was left inconsistent by an \
7216                          earlier failure; next: run the command again"
7217                    .into(),
7218            })
7219    }
7220
7221    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7222        self.children_cache
7223            .lock()
7224            .map_err(|_| SourceError::Unavailable {
7225                message: "this source's view of a project's tasks was left inconsistent by an \
7226                      earlier failure; next: run the command again"
7227                    .into(),
7228            })
7229    }
7230
7231    /// Every page of the board, read from GitHub.
7232    async fn read_board(&self) -> Result<Board, SourceError> {
7233        let mut after: Option<String> = None;
7234        let mut items = Vec::new();
7235        let mut board;
7236        loop {
7237            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7238            for item in page
7239                .pointer("/items/nodes")
7240                .and_then(Value::as_array)
7241                .ok_or_else(|| SourceError::Malformed {
7242                    message: "GitHub project items.nodes is not an array".into(),
7243                })?
7244            {
7245                if let Some(resolved) = self.resolve(item)? {
7246                    items.push(resolved);
7247                }
7248            }
7249            let info = page
7250                .pointer("/items/pageInfo")
7251                .ok_or_else(|| SourceError::Malformed {
7252                    message: "GitHub project items have no pageInfo".into(),
7253                })?;
7254            let has_next = required_bool(info, "hasNextPage")?;
7255            let next = has_next
7256                .then(|| required_str(info, "endCursor"))
7257                .transpose()?;
7258            board = page.clone();
7259            match next {
7260                Some(next) => {
7261                    validate_cursor_progress(after.as_deref(), next)?;
7262                    after = Some(next.to_owned());
7263                }
7264                None => break,
7265            }
7266        }
7267        Ok(Board {
7268            id: required_str(&board, "id")?.to_owned(),
7269            fields: board.get("fields").cloned().unwrap_or(Value::Null),
7270            items,
7271        })
7272    }
7273
7274    /// The existing items this source has written, for completing a narrowed read that is
7275    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7276    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7277        self.updated.lock().map_err(|_| SourceError::Unavailable {
7278            message: "this source's record of what it wrote in this run was left inconsistent \
7279                      by an earlier failure; next: run the command again"
7280                .into(),
7281        })
7282    }
7283
7284    /// The issues this source has commented on in this command; see
7285    /// [`Self::commented`](GitHubProjectsSource::commented).
7286    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7287        self.commented.lock().map_err(|_| SourceError::Unavailable {
7288            message: "this source's record of what it commented on in this run was left \
7289                      inconsistent by an earlier failure; next: run the command again"
7290                .into(),
7291        })
7292    }
7293
7294    /// Called only once GitHub has answered the comment write, so an issue whose comment
7295    /// failed is never made a candidate a later read would pay a node read for.
7296    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7297        let mut commented = self.commented()?;
7298        if !commented.contains(issue) {
7299            commented.push(issue.clone());
7300        }
7301        Ok(())
7302    }
7303
7304    /// The items this source has created, for completing a board read that is behind.
7305    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7306        self.created.lock().map_err(|_| SourceError::Unavailable {
7307            message: "this source's record of what it created in this run was left \
7308                      inconsistent by an earlier failure; next: run the command again"
7309                .into(),
7310        })
7311    }
7312
7313    /// One board item as this source reports it, or `None` for content it ignores.
7314    ///
7315    /// A pull request is neither a project nor a task — it is somebody's change, not a
7316    /// unit of plan — and an item whose content the token cannot see has nothing to
7317    /// report at all.
7318    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7319        let resolved = self.resolve_unremembered(item)?;
7320        if let Some(resolved) = &resolved {
7321            self.resolved_cache()?
7322                .insert(resolved.id.clone(), resolved.clone());
7323        }
7324        Ok(resolved)
7325    }
7326
7327    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7328    /// write's own response needs, which says nothing this process should answer a later read
7329    /// with.
7330    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7331        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7332            message: "GitHub project item is missing content".into(),
7333        })?;
7334        if content.is_null() {
7335            return Ok(None);
7336        }
7337        let content_kind = match required_str(content, "__typename")? {
7338            "Issue" => ContentKind::Issue,
7339            "DraftIssue" => ContentKind::DraftIssue,
7340            _ => return Ok(None),
7341        };
7342        let field_values = item
7343            .get("fieldValues")
7344            .ok_or_else(|| SourceError::Malformed {
7345                message: "GitHub project item is missing fieldValues".into(),
7346            })?;
7347        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7348        let nodes = field_values
7349            .get("nodes")
7350            .and_then(Value::as_array)
7351            .ok_or_else(|| SourceError::Malformed {
7352                message: "GitHub project item fieldValues.nodes is not an array".into(),
7353            })?;
7354        if let Some(labels) = content.get("labels") {
7355            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7356        }
7357        let raw_body = optional_str(content, "body")?.map(str::to_owned);
7358        let (body, slot) = metadata_body(raw_body.clone())?;
7359        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7360            .map(|id| NativeId(id.to_owned()));
7361        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7362        // to read one from; it is a task, and never a project.
7363        let sub_issues = match content_kind {
7364            ContentKind::Issue => sub_issue_total(content)?,
7365            ContentKind::DraftIssue => 0,
7366        };
7367        let content_id = required_str(content, "id")?;
7368        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7369            message: format!("GitHub issue {content_id}: {message}"),
7370        })?;
7371        let raw_title = required_str(content, "title")?;
7372        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7373        // The title a person wrote, which for a document is the one without the prefix —
7374        // the same way `content` above is the body without this source's metadata slot.
7375        let title = match kind {
7376            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7377            BoardKind::Work(_) => raw_title.to_owned(),
7378        };
7379        let own_repository = content
7380            .pointer("/repository/nameWithOwner")
7381            .and_then(Value::as_str)
7382            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7383            .transpose()
7384            .map_err(|message| SourceError::Malformed { message })?;
7385        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7386            Repository::from_metadata(&slot)
7387                .map_err(|message| SourceError::Malformed { message })?
7388        } else {
7389            own_repository.clone().into_iter().collect()
7390        };
7391        let classification = Classification::from_metadata(&slot)
7392            .map_err(|message| SourceError::Malformed { message })?;
7393        let id = NativeId(content_id.to_owned());
7394        // Read only for a task, because only a task has either list: a project or a
7395        // document holding one of these keys holds nothing this source reports, and the
7396        // keys are left out of its caller-visible metadata all the same.
7397        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7398            let listed = |key: &str| {
7399                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7400                    .map_err(|message| SourceError::Malformed { message })
7401            };
7402            (
7403                listed(TaskRef::DELIVERS_KEY)?,
7404                listed(TaskRef::DELIVERED_BY_KEY)?,
7405            )
7406        } else {
7407            (Vec::new(), Vec::new())
7408        };
7409        let (option, closed, reason) = Self::status_parts(nodes, content)?;
7410        let priority = self.held_priority(nodes)?;
7411        let item_labels = labels(content)?;
7412        // Present when the item was reached through its own issue, whose board entry
7413        // names the board; a read of the board's own items has the board already. An
7414        // empty id names nothing a field write could address, so it is read as absent and
7415        // the write goes back to reading the board.
7416        let board_id = item
7417            .pointer("/project/id")
7418            .and_then(Value::as_str)
7419            .filter(|id| !id.is_empty());
7420        let resolved = Resolved {
7421            item_id: required_str(item, "id")?.to_owned(),
7422            id,
7423            content_kind,
7424            kind,
7425            title,
7426            body: body.filter(|value| !value.is_empty()),
7427            raw_body,
7428            status: self
7429                .statuses
7430                .status(kind.status_kind(), option, closed, reason),
7431            option: option.map(str::to_owned),
7432            priority,
7433            closed,
7434            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7435            delivers,
7436            delivered_by,
7437            labels: item_labels.clone(),
7438            parent,
7439            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7440            projected: self.held_projections(nodes)?,
7441            number: match content_kind {
7442                ContentKind::Issue => Some(issue_number(content)?),
7443                // A draft is filed in no repository, so nothing ever numbered it:
7444                // `DraftIssue` declares no `number` at all, exactly as it declares no
7445                // `subIssuesSummary` the branch above reads.
7446                ContentKind::DraftIssue => None,
7447            },
7448            url: optional_str(content, "url")?.map(str::to_owned),
7449            created_at: optional_time(content, "createdAt")?,
7450            updated_at: optional_time(content, "updatedAt")?,
7451            item_updated_at: optional_time(item, "updatedAt")?,
7452            comment_count: content
7453                .pointer("/commentCount/totalCount")
7454                .and_then(Value::as_u64),
7455            relationship_events: content
7456                .pointer("/relationshipEvents/totalCount")
7457                .and_then(Value::as_u64),
7458            // The issue node is stamped wherever it was read; a board item a read of the issue
7459            // put together from its memberships is not.
7460            served_at: match optional_time(content, SERVED_AT)? {
7461                Some(served) => Some(served),
7462                None => optional_time(item, SERVED_AT)?,
7463            },
7464            // Both connections were read to their end above — one reporting a further page is
7465            // refused — so these are every field value and every label the item holds. A
7466            // response that did not select the labels at all says nothing of them, and gives
7467            // no facets.
7468            facets: content.get("labels").is_some().then(|| {
7469                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7470            }),
7471            own_repository,
7472            repositories,
7473            classification,
7474            slot,
7475            board_id: board_id.map(str::to_owned),
7476            fields: field_definitions(nodes),
7477            board_fields: Self::carried_board_fields(content, board_id)?,
7478            blocked_by: carried_blocked_by(content)?,
7479        };
7480        Ok(Some(resolved))
7481    }
7482
7483    /// The field definitions of the board `board_id` names — the project this issue's own
7484    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7485    /// `None` when the read carried none, carried no entry for that board, or the board item
7486    /// named no board, which a write then answers by reading the board's fields itself.
7487    ///
7488    /// Matched by the board's node id and never by its number alone: a project number is
7489    /// unique only within its owner, so another owner's board numbered alike can sit on the
7490    /// same page, and its field and option ids address nothing on this one.
7491    fn carried_board_fields(
7492        content: &Value,
7493        board_id: Option<&str>,
7494    ) -> Result<Option<Value>, SourceError> {
7495        let (Some(nodes), Some(board_id)) = (
7496            content.pointer("/boards/nodes").and_then(Value::as_array),
7497            board_id,
7498        ) else {
7499            return Ok(None);
7500        };
7501        let Some(board) = nodes.iter().find_map(|node| {
7502            let project = node.get("project")?;
7503            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7504        }) else {
7505            return Ok(None);
7506        };
7507        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7508            return Ok(None);
7509        };
7510        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7511        Ok(Some(fields.clone()))
7512    }
7513
7514    /// What one board item's `Priority` field says, through this instance's mapping.
7515    ///
7516    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7517    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7518    /// option the mapping does not name is kept as itself — never read as a level or as
7519    /// `none` — for a read of the task to report by name.
7520    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7521        let Some(mapping) = &self.priorities else {
7522            return Ok(HeldPriority::Read(Priority::None));
7523        };
7524        // A value of the field that names no option — a text field someone called `Priority` —
7525        // is malformed rather than `none`: reading it as no priority would let the next copy
7526        // clear one a person set.
7527        let Some(option) = field_values
7528            .iter()
7529            .find(|value| {
7530                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7531            })
7532            .map(|value| required_str(value, "name"))
7533            .transpose()?
7534        else {
7535            return Ok(HeldPriority::Read(Priority::None));
7536        };
7537        Ok(mapping.priority_of(option).map_or_else(
7538            || HeldPriority::Unmapped(option.to_owned()),
7539            HeldPriority::Read,
7540        ))
7541    }
7542
7543    /// What each projected metadata field holds for one board item, by field name — every text
7544    /// value GitHub answers, an empty one included.
7545    fn held_projections(
7546        &self,
7547        field_values: &[Value],
7548    ) -> Result<BTreeMap<String, String>, SourceError> {
7549        let mut held = BTreeMap::new();
7550        for projection in &self.metadata_fields {
7551            if let Some(text) = text_field(field_values, &projection.field)? {
7552                held.insert(projection.field.to_string(), text);
7553            }
7554        }
7555        Ok(held)
7556    }
7557
7558    /// What one board item's status is read from: its `Status` option, whether its issue
7559    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7560    /// three into the status it reports.
7561    fn status_parts<'a>(
7562        field_values: &'a [Value],
7563        content: &'a Value,
7564    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7565        let option = field_values
7566            .iter()
7567            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7568            .map(|value| required_str(value, "name"))
7569            .transpose()?;
7570        let closed = optional_str(content, "state")? == Some("CLOSED");
7571        Ok((option, closed, optional_str(content, "stateReason")?))
7572    }
7573
7574    /// The board Status option this write selects, or the refusal that says why not.
7575    ///
7576    /// The mapped option is required for both open and terminal targets. A terminal write
7577    /// validates it before changing either representation, so it can never fall back to
7578    /// closing an issue whose board cannot display the matching status.
7579    ///
7580    /// Answers the field's id, the option's id, and the option's name as the board spells
7581    /// it — which is the name a read of the item reports once it sits there.
7582    fn column_for(
7583        &self,
7584        fields: &Value,
7585        kind: ItemKind,
7586        category: StatusCategory,
7587        target: &StatusTarget,
7588    ) -> Result<Option<(String, String, String)>, SourceError> {
7589        let Some(wanted) = target.option() else {
7590            return Ok(None);
7591        };
7592        let missing = |detail: &str| SourceError::Refused {
7593            message: format!(
7594                "{} status {} of source {} needs the board Status option {wanted:?}, and \
7595                 {detail}; next: add that option to the board, which `onetaskgraph sources \
7596                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7597                 it has",
7598                kind.marker(),
7599                category_name(category),
7600                self.name,
7601                self.name,
7602                category_name(category),
7603                kind.marker()
7604            ),
7605        };
7606        let Some(field) = Board::field(fields, "Status")? else {
7607            return Err(missing("this board has no Status field"));
7608        };
7609        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7610            return Err(missing(
7611                "this board's Status field is not a single-select field",
7612            ));
7613        }
7614        let option = field
7615            .get("options")
7616            .and_then(Value::as_array)
7617            .and_then(|options| {
7618                options.iter().find(|option| {
7619                    option
7620                        .get("name")
7621                        .and_then(Value::as_str)
7622                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7623                })
7624            });
7625        match option {
7626            None => Err(missing("this board does not have it")),
7627            Some(option) => Ok(Some((
7628                required_str(field, "id")?.to_owned(),
7629                required_str(option, "id")?.to_owned(),
7630                required_str(option, "name")?.to_owned(),
7631            ))),
7632        }
7633    }
7634
7635    /// The refusal a status that closes an issue is answered with over a board draft.
7636    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7637        SourceError::Refused {
7638            message: format!(
7639                "status {} of source {} closes the item's issue, and GitHub draft items have \
7640                 no open or closed state",
7641                category_name(category),
7642                self.name
7643            ),
7644        }
7645    }
7646
7647    /// What a status write to one item needs of the board: the board's id and the
7648    /// definition of its `Status` field, read off the item when the item says both.
7649    ///
7650    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7651    /// and its `Status` value carries that field's definition, options and all. An item that
7652    /// does not say — no board id, or no `Status` value to read the field off — takes them
7653    /// from [`Self::board_fields`], which reads no item.
7654    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7655        if let Some(board) = item.carried_board() {
7656            return Ok(board);
7657        }
7658        if item.defines("Status")
7659            && let Some(board_id) = item.named_board()
7660        {
7661            return Ok(BoardFields {
7662                id: board_id,
7663                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7664            });
7665        }
7666        self.board_fields().await
7667    }
7668
7669    /// Set one task's or one project's status and nothing else; see
7670    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7671    async fn set_status(
7672        &self,
7673        id: &NativeId,
7674        kind: ItemKind,
7675        category: StatusCategory,
7676    ) -> Result<Option<Status>, SourceError> {
7677        // Refused before anything is read, in the words a write of the same status is.
7678        let target = self.resolved_target(kind, category)?;
7679        // An item read by issue reference carries only what this write needs, so what it
7680        // becomes is never remembered for a later read to be answered from.
7681        let held = self.take_held(id)?;
7682        let by_reference = held.is_some();
7683        let bound = match held {
7684            Some(item) => Some(item),
7685            None => self.bound_item(id).await?,
7686        };
7687        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7688            return Ok(None);
7689        };
7690        let board = self.status_board(&item).await?;
7691        let (field, option, name) = self
7692            .column_for(&board.fields, kind, category, &target)?
7693            .ok_or_else(|| SourceError::Malformed {
7694                message: format!(
7695                    "status {} of source {} names no board Status option",
7696                    category_name(category),
7697                    self.name
7698                ),
7699            })?;
7700        if item.status.category == category && item.option.as_deref() == Some(&name) {
7701            return Ok(Some(item.status));
7702        }
7703        match &target {
7704            StatusTarget::Terminal(_, reason) => {
7705                if item.content_kind == ContentKind::DraftIssue {
7706                    return Err(self.closes_a_draft(category));
7707                }
7708                // The option first, so a refused selection leaves an open issue open rather
7709                // than closed behind a card that still reads open.
7710                self.set_item_field(
7711                    board.id.as_str(),
7712                    &item.item_id,
7713                    &field,
7714                    json!({"singleSelectOptionId": option}),
7715                )
7716                .await?;
7717                // An issue already closed in a way that reads as this category keeps its state
7718                // and its reason: only its card was out of line, and closing it again would
7719                // overwrite a reason — a duplicate's — that says more than this write does.
7720                let already = item.closed && item.status.category == category;
7721                if !already {
7722                    self.update_content(
7723                        ContentKind::Issue,
7724                        &item.id,
7725                        json!({"stateInput": state_input(Some(&target))}),
7726                    )
7727                    .await?;
7728                }
7729                let reason = if already {
7730                    item.reason.clone()
7731                } else {
7732                    Some(reason.reason().to_owned())
7733                };
7734                item.closed = true;
7735                item.status = self
7736                    .statuses
7737                    .status(kind, Some(&name), true, reason.as_deref());
7738                item.option = Some(name);
7739                item.reason = reason;
7740            }
7741            StatusTarget::Column(_) => {
7742                // An option is what an open item's status is, so a closed issue is reopened
7743                // first — sitting closed in the column, it would read back as closed. A draft has
7744                // no state to reopen.
7745                if item.content_kind == ContentKind::Issue && item.closed {
7746                    self.update_content(
7747                        ContentKind::Issue,
7748                        &item.id,
7749                        json!({"stateInput": state_input(Some(&target))}),
7750                    )
7751                    .await?;
7752                    item.closed = false;
7753                }
7754                self.set_item_field(
7755                    board.id.as_str(),
7756                    &item.item_id,
7757                    &field,
7758                    json!({"singleSelectOptionId": option}),
7759                )
7760                .await?;
7761                item.status = self.statuses.status(kind, Some(&name), false, None);
7762                item.option = Some(name);
7763            }
7764            StatusTarget::Disabled(_) => {
7765                unreachable!("resolved_target refused a disabled status")
7766            }
7767        }
7768        let status = item.status.clone();
7769        if !by_reference {
7770            self.remember_written(item, false)?;
7771        }
7772        Ok(Some(status))
7773    }
7774
7775    /// Replace one task's `delivered_by` and nothing else; see
7776    /// [`TaskSource::set_delivered_by`].
7777    ///
7778    /// One update of the body, which differs from the body GitHub holds only inside the
7779    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7780    async fn replace_delivered_by(
7781        &self,
7782        id: &NativeId,
7783        delivered_by: &[TaskRef],
7784    ) -> Result<Option<()>, SourceError> {
7785        let entries = TaskRef::listed(
7786            TaskRef::DELIVERED_BY_KEY,
7787            id,
7788            Some(&self.name),
7789            delivered_by.to_vec(),
7790        )
7791        .map_err(|message| SourceError::Refused { message })?;
7792        let Some(mut item) = self
7793            .bound_item(id)
7794            .await?
7795            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7796        else {
7797            return Ok(None);
7798        };
7799        let mut slot = item.slot.clone();
7800        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7801        self.write_slot(&mut item, &slot).await?;
7802        item.delivered_by = entries;
7803        self.remember_written(item, false)?;
7804        Ok(Some(()))
7805    }
7806
7807    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7808    /// see [`TaskSource::set_task_metadata`].
7809    ///
7810    /// `None` when this board holds no item by that id, or holds one of another kind. The
7811    /// answer is the item as this source now reads it, so what a caller is told the key
7812    /// holds is what the slot holds.
7813    ///
7814    /// A key already holding the value is answered without a write, compared as JSON rather
7815    /// than as the body's bytes: a slot a person spelled with other whitespace would
7816    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7817    async fn set_slot_key(
7818        &self,
7819        id: &NativeId,
7820        kind: BoardKind,
7821        key: &MetadataKey,
7822        value: &Value,
7823    ) -> Result<Option<Resolved>, SourceError> {
7824        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7825            return Ok(None);
7826        };
7827        if item.slot.get(key.as_str()) == Some(value) {
7828            return Ok(Some(item));
7829        }
7830        let mut slot = item.slot.clone();
7831        slot.insert(key.as_str().to_owned(), value.clone());
7832        // A key a board text field projects moves that field first, so the body — the value's
7833        // one authoritative home — is written last, as every write of an existing item is.
7834        if self
7835            .metadata_fields
7836            .iter()
7837            .any(|projection| &*projection.key == key.as_str())
7838        {
7839            let board = self.projection_board(&item).await?;
7840            let (writes, projected) = self.projection_writes(
7841                &board.fields,
7842                Some(&item.projected),
7843                &slot,
7844                Some(&[key.as_str()]),
7845            )?;
7846            let (mut values, mut clears) = (Vec::new(), Vec::new());
7847            for write in &writes {
7848                write.join(&mut values, &mut clears);
7849            }
7850            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7851                .await?;
7852            item.projected = projected;
7853        }
7854        self.write_slot(&mut item, &slot).await?;
7855        self.remember_written(item.clone(), false)?;
7856        Ok(Some(item))
7857    }
7858
7859    /// What a projection write to `item` needs of the board, read off the item when it says
7860    /// enough, as [`Self::fields_for`] reads it.
7861    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7862        if let Some(board) = item.carried_board() {
7863            return Ok(board);
7864        }
7865        if let Some(board_id) = item.named_board()
7866            && self
7867                .metadata_fields
7868                .iter()
7869                .all(|projection| item.defines(&projection.field))
7870        {
7871            return Ok(BoardFields {
7872                id: board_id,
7873                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7874            });
7875        }
7876        self.board_fields().await
7877    }
7878
7879    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7880    /// `item` up to what that write left.
7881    ///
7882    /// The body sent differs from the body GitHub holds only inside the slot — see
7883    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7884    /// the mutation the item's content takes, so a board draft's body is written with
7885    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7886    async fn write_slot(
7887        &self,
7888        item: &mut Resolved,
7889        slot: &BTreeMap<String, Value>,
7890    ) -> Result<(), SourceError> {
7891        let held = item.raw_body.clone().unwrap_or_default();
7892        let body = with_slot(&held, slot)?;
7893        if body != held {
7894            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7895                .await?;
7896        }
7897        let (visible, slot) = metadata_body(Some(body.clone()))?;
7898        item.body = visible.filter(|value| !value.is_empty());
7899        item.raw_body = Some(body);
7900        item.slot = slot;
7901        Ok(())
7902    }
7903
7904    /// This instance's target for a category written to an item of `kind`, refusing one
7905    /// that kind has no option for — before anything is read or written.
7906    ///
7907    /// Nothing here mutates the board's option set to make room for a status. GitHub
7908    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7909    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7910    /// field and every item's status.
7911    fn resolved_target(
7912        &self,
7913        kind: ItemKind,
7914        category: StatusCategory,
7915    ) -> Result<StatusTarget, SourceError> {
7916        let target = self.statuses.target(kind, category).clone();
7917        let StatusTarget::Disabled(why) = target else {
7918            return Ok(target);
7919        };
7920        let refusal = why.refusal(&self.name, category, kind);
7921        // Why there is no shipped default, which is the question a person meeting this
7922        // refusal on a source that never mentioned the category asks.
7923        let shipped_none = match category {
7924            StatusCategory::Draft => Some(
7925                "draft has no shipped default because GitHub draft issues cannot have \
7926                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7927            ),
7928            StatusCategory::Unknown => Some(
7929                "unknown has no shipped default because this board keeps no open-ended status \
7930                 word: every word classified unknown is written to the one board Status option \
7931                 status_mapping.unknown names",
7932            ),
7933            _ => None,
7934        };
7935        Err(match (refusal, shipped_none, why) {
7936            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7937                SourceError::Refused {
7938                    message: format!("{message}; {note}"),
7939                }
7940            }
7941            (refusal, _, _) => refusal,
7942        })
7943    }
7944
7945    /// What writing `priority` does to one item's `Priority` field on this board, or the
7946    /// refusal naming what the board lacks.
7947    ///
7948    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7949    /// none already, or of an item not created yet. Every other priority selects the option
7950    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7951    /// without that option, is refused rather than given one: reads and writes never create
7952    /// a field or an option.
7953    fn priority_write(
7954        &self,
7955        fields: &Value,
7956        existing: Option<&Resolved>,
7957        priority: Priority,
7958    ) -> Result<Option<PriorityWrite>, SourceError> {
7959        let Some(mapping) = &self.priorities else {
7960            return Err(self.holds_no_priority());
7961        };
7962        let Some(wanted) = mapping.option(priority) else {
7963            if !existing.is_some_and(Resolved::holds_priority) {
7964                return Ok(None);
7965            }
7966            let field =
7967                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7968                    message: format!(
7969                        "an item holding a {PRIORITY_FIELD} value was read without that field"
7970                    ),
7971                })?;
7972            return Ok(Some(PriorityWrite::Clear {
7973                field: required_str(field, "id")?.to_owned(),
7974            }));
7975        };
7976        let missing = |detail: &str| SourceError::Refused {
7977            message: format!(
7978                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7979                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7980                 it, or point priority_mapping.{priority} of this source at an option the board \
7981                 has",
7982                self.name, self.name
7983            ),
7984        };
7985        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7986            return Err(missing(&format!(
7987                "this board has no {PRIORITY_FIELD} field"
7988            )));
7989        };
7990        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7991            return Err(missing(&format!(
7992                "this board's {PRIORITY_FIELD} field is not a single-select field"
7993            )));
7994        }
7995        // An options list that is absent or not a list is an answer this source cannot read,
7996        // not a board lacking the option: `sources fields --apply` is no remedy for it.
7997        let option = field
7998            .get("options")
7999            .and_then(Value::as_array)
8000            .ok_or_else(|| SourceError::Malformed {
8001                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
8002            })?
8003            .iter()
8004            .find(|option| {
8005                option
8006                    .get("name")
8007                    .and_then(Value::as_str)
8008                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8009            })
8010            .ok_or_else(|| missing("this board does not have it"))?;
8011        Ok(Some(PriorityWrite::Select {
8012            field: required_str(field, "id")?.to_owned(),
8013            option: required_str(option, "id")?.to_owned(),
8014        }))
8015    }
8016
8017    /// What writing `metadata` does to each projected text field of one item, and what those
8018    /// fields hold after it — or the refusal, before anything is sent.
8019    ///
8020    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8021    /// narrows the work to the entries projecting one of those metadata keys, for a write that
8022    /// touches only them; `None` is every entry. A string the field already holds, and nothing
8023    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8024    /// and so is a board without the field, or with a field of that name that is not a text
8025    /// field: reads and writes never create a field.
8026    fn projection_writes(
8027        &self,
8028        fields: &Value,
8029        held: Option<&BTreeMap<String, String>>,
8030        metadata: &BTreeMap<String, Value>,
8031        keys: Option<&[&str]>,
8032    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8033        let mut projected = held.cloned().unwrap_or_default();
8034        let mut writes = Vec::new();
8035        for projection in self
8036            .metadata_fields
8037            .iter()
8038            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8039        {
8040            let wanted = projection.projected(metadata, &self.name)?;
8041            let field = self.projection_field(fields, projection)?;
8042            let holding = projected.get(&*projection.field);
8043            match wanted {
8044                Projected::Text(text) if holding != Some(&text) => {
8045                    writes.push(ProjectionWrite::Set {
8046                        field,
8047                        text: text.clone(),
8048                    });
8049                    projected.insert(projection.field.to_string(), text);
8050                }
8051                Projected::Nothing if holding.is_some() => {
8052                    writes.push(ProjectionWrite::Clear { field });
8053                    projected.remove(&*projection.field);
8054                }
8055                Projected::Text(_) | Projected::Nothing => {}
8056            }
8057        }
8058        Ok((writes, projected))
8059    }
8060
8061    /// The id of the board text field one projection writes, or the refusal naming what the
8062    /// board has instead.
8063    fn projection_field(
8064        &self,
8065        fields: &Value,
8066        projection: &MetadataField,
8067    ) -> Result<String, SourceError> {
8068        let refuse = |detail: String| SourceError::Refused {
8069            message: format!(
8070                "source {} projects metadata key {:?} onto the board text field {:?}, and \
8071                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8072                 text field, or name another field in metadata_fields",
8073                self.name, &*projection.key, &*projection.field, self.name
8074            ),
8075        };
8076        let Some(field) = Board::field(fields, &projection.field)? else {
8077            return Err(refuse("this board has no field of that name".to_owned()));
8078        };
8079        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8080        let data_type = optional_str(field, "dataType")?;
8081        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8082            return Err(refuse(format!(
8083                "this board's field of that name is not a text field (it is a {}), so it is \
8084                 left as it is; rename or remove that field first",
8085                data_type.unwrap_or(typename)
8086            )));
8087        }
8088        Ok(required_str(field, "id")?.to_owned())
8089    }
8090
8091    /// Apply one priority write to one board item.
8092    async fn write_priority(
8093        &self,
8094        board_id: &str,
8095        item_id: &str,
8096        write: &PriorityWrite,
8097    ) -> Result<(), SourceError> {
8098        match write {
8099            PriorityWrite::Select { field, option } => {
8100                self.set_item_field(
8101                    board_id,
8102                    item_id,
8103                    field,
8104                    json!({"singleSelectOptionId": option}),
8105                )
8106                .await
8107            }
8108            PriorityWrite::Clear { field } => {
8109                let data = self
8110                    .graphql(
8111                        graphql::CLEAR_FIELD,
8112                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8113                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
8114                    )
8115                    .await?;
8116                let returned = data
8117                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8118                    .ok_or_else(|| SourceError::Malformed {
8119                        message: "GitHub field clear returned no project item".into(),
8120                    })?;
8121                if required_str(returned, "id")? != item_id {
8122                    return Err(SourceError::Malformed {
8123                        message: "GitHub field clear returned the wrong project item".into(),
8124                    });
8125                }
8126                Ok(())
8127            }
8128        }
8129    }
8130
8131    /// The refusal a priority is answered with by an instance configured with no
8132    /// `priority_mapping`, which holds none.
8133    fn holds_no_priority(&self) -> SourceError {
8134        SourceError::Refused {
8135            message: format!(
8136                "source {} holds no task priority: its configuration sets no priority_mapping; \
8137                 next: set priority_mapping on this source, then run `onetaskgraph sources \
8138                 fields {} --apply` to set its board up",
8139                self.name, self.name
8140            ),
8141        }
8142    }
8143
8144    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8145    ///
8146    /// One field write — a select, or a clear for `none` — and no title, body, label, state
8147    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8148    async fn set_priority(
8149        &self,
8150        id: &NativeId,
8151        priority: Priority,
8152    ) -> Result<Option<Priority>, SourceError> {
8153        if self.priorities.is_none() {
8154            return Err(self.holds_no_priority());
8155        }
8156        let Some(mut item) = self
8157            .bound_item(id)
8158            .await?
8159            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8160        else {
8161            return Ok(None);
8162        };
8163        if priority == Priority::None && !item.holds_priority() {
8164            return Ok(Some(priority));
8165        }
8166        // The item's own read carries the field's definition whenever it holds a value of
8167        // it, which a clear always does; a select onto an item holding none reads the board.
8168        let board = match (item.carried_board(), item.named_board()) {
8169            (Some(board), _) => board,
8170            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8171                id,
8172                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8173            },
8174            _ => self.board_fields().await?,
8175        };
8176        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8177            return Ok(Some(priority));
8178        };
8179        let (document, root, input) = match write {
8180            PriorityWrite::Select { field, option } => (
8181                graphql::UPDATE_FIELD,
8182                "updateProjectV2ItemFieldValue",
8183                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8184            ),
8185            PriorityWrite::Clear { field } => (
8186                graphql::CLEAR_FIELD,
8187                "clearProjectV2ItemFieldValue",
8188                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8189            ),
8190        };
8191        let data = self
8192            .graphql(
8193                document,
8194                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8195            )
8196            .await?;
8197        let returned = data
8198            .get(root)
8199            .and_then(|value| value.get("projectV2Item"))
8200            .ok_or_else(|| SourceError::Malformed {
8201                message: "GitHub priority write returned no project item".into(),
8202            })?;
8203        if required_str(returned, "id")? != item.item_id {
8204            return Err(SourceError::Malformed {
8205                message: "GitHub priority write returned the wrong project item".into(),
8206            });
8207        }
8208        let value = returned
8209            .get("fieldValueByName")
8210            .ok_or_else(|| SourceError::Malformed {
8211                message: "GitHub priority write returned no priority read-back".into(),
8212            })?;
8213        if !value.is_null()
8214            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8215        {
8216            return Err(SourceError::Malformed {
8217                message: "GitHub priority read-back is not a Priority field value".into(),
8218            });
8219        }
8220        let values = if value.is_null() {
8221            Vec::new()
8222        } else {
8223            vec![value.clone()]
8224        };
8225        item.priority = self.held_priority(&values)?;
8226        let answer = item.task()?.priority;
8227        self.remember_written(item, false)?;
8228        Ok(Some(answer))
8229    }
8230
8231    /// Replace one task's visible body and nothing else; see
8232    /// [`TaskSource::set_task_content`].
8233    ///
8234    /// One update of the body, which differs from the body GitHub holds only outside the
8235    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8236    /// this source keeps there reads back as it was. A body that would not change is not
8237    /// sent at all.
8238    async fn replace_content(
8239        &self,
8240        id: &NativeId,
8241        content: &str,
8242    ) -> Result<Option<()>, SourceError> {
8243        let Some(mut item) = self
8244            .bound_item(id)
8245            .await?
8246            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8247        else {
8248            return Ok(None);
8249        };
8250        let held = item.raw_body.clone().unwrap_or_default();
8251        let body = with_content(&held, content)?;
8252        // Checked before anything is sent: content ending in what this source reads as its own
8253        // metadata slot would read back as metadata rather than as the content it was.
8254        let (visible, slot) = metadata_body(Some(body.clone()))?;
8255        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8256            return Err(SourceError::Refused {
8257                message: format!(
8258                    "this content ends in what source {} reads as its own metadata slot \
8259                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8260                     as content; next: remove that trailing block from the content",
8261                    self.name
8262                ),
8263            });
8264        }
8265        if body != held {
8266            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8267                .await?;
8268        }
8269        item.body = visible.filter(|value| !value.is_empty());
8270        item.raw_body = Some(body);
8271        item.slot = slot;
8272        self.remember_written(item, false)?;
8273        Ok(Some(()))
8274    }
8275
8276    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8277    ///
8278    /// One read of the item — which carries the board's field definitions and the issue's
8279    /// `blockedBy`, so neither is read again — and then only what differs from it: the
8280    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8281    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8282    /// the title, the body — visible content and metadata slot together — and a state change.
8283    /// So an update naming any of title, body, metadata, status and priority is one read and
8284    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8285    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8286    /// as a whole write does; an open one selects its option and then reopens. The origin
8287    /// field is never written: an update is of an item that already exists, whose origin is
8288    /// what it is.
8289    ///
8290    /// The task answered is the item as those writes left it, built from the read and what was
8291    /// sent rather than read again — the same record a later read in this run answers from.
8292    async fn targeted_update(
8293        &self,
8294        id: &NativeId,
8295        update: &TaskUpdate,
8296    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8297        // Everything this source can refuse without reading the item is refused first, in the
8298        // words a whole write of the same fields is refused with.
8299        update.consistent()?;
8300        if update
8301            .title
8302            .as_deref()
8303            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8304        {
8305            return Err(SourceError::Refused {
8306                message: format!(
8307                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8308                     spells a document, so it would read back as one rather than as a task; \
8309                     retitle it",
8310                    self.name
8311                ),
8312            });
8313        }
8314        if let Some(delivers) = &update.delivers {
8315            TaskRef::listed(
8316                TaskRef::DELIVERS_KEY,
8317                id,
8318                Some(&self.name),
8319                delivers.clone(),
8320            )
8321            .map_err(|message| SourceError::Refused { message })?;
8322        }
8323        if self.priorities.is_none()
8324            && update
8325                .priority
8326                .is_some_and(|priority| priority != Priority::None)
8327        {
8328            return Err(self.holds_no_priority());
8329        }
8330        let target = update
8331            .status
8332            .as_ref()
8333            .map(|status| self.resolved_target(ItemKind::Task, status.category))
8334            .transpose()?;
8335        let Some(mut item) = self
8336            .bound_item(id)
8337            .await?
8338            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8339        else {
8340            return Ok(None);
8341        };
8342        let before = item.task()?;
8343
8344        let mut status_move = None;
8345        if let (Some(status), Some(target)) = (&update.status, target) {
8346            let board = self.status_board(&item).await?;
8347            let (field, option, name) = self
8348                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8349                .ok_or_else(|| SourceError::Malformed {
8350                    message: format!(
8351                        "status {} of source {} names no board Status option",
8352                        category_name(status.category),
8353                        self.name
8354                    ),
8355                })?;
8356            let terminal = matches!(target, StatusTarget::Terminal(_, _));
8357            if terminal && item.content_kind == ContentKind::DraftIssue {
8358                return Err(self.closes_a_draft(status.category));
8359            }
8360            let landed = match &target {
8361                StatusTarget::Terminal(_, reason) => {
8362                    self.statuses
8363                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8364                }
8365                _ => self
8366                    .statuses
8367                    .status(ItemKind::Task, Some(&name), false, None),
8368            };
8369            let option_moves = item
8370                .option
8371                .as_deref()
8372                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8373            let state_moves = item.content_kind == ContentKind::Issue
8374                && (item.closed != terminal || (terminal && item.status != landed));
8375            if let Some(moves) = Moves::of(option_moves, state_moves) {
8376                status_move = Some(StatusMove {
8377                    board: board.id,
8378                    field,
8379                    option,
8380                    name,
8381                    target,
8382                    landed,
8383                    moves,
8384                });
8385            }
8386        }
8387
8388        let mut priority_move = None;
8389        if let Some(priority) = update.priority
8390            && self.priorities.is_some()
8391            && item.priority != HeldPriority::Read(priority)
8392        {
8393            let board = match (item.carried_board(), item.named_board()) {
8394                (Some(board), _) => board,
8395                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8396                    id: board,
8397                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8398                },
8399                _ => self.board_fields().await?,
8400            };
8401            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8402                priority_move = Some((board.id, write, priority));
8403            }
8404        }
8405
8406        // The metadata keys this update names that a board text field projects, and what
8407        // that does to those fields — refused, as a whole write refuses it, before anything
8408        // is sent.
8409        let touched: Vec<&str> = update
8410            .metadata_set
8411            .keys()
8412            .chain(update.metadata_remove.iter())
8413            .map(MetadataKey::as_str)
8414            .collect();
8415        let mut projection_move = None;
8416        if self
8417            .metadata_fields
8418            .iter()
8419            .any(|projection| touched.contains(&&*projection.key))
8420        {
8421            let mut metadata = item.slot.clone();
8422            for (key, value) in &update.metadata_set {
8423                metadata.insert(key.as_str().to_owned(), value.clone());
8424            }
8425            for key in &update.metadata_remove {
8426                metadata.remove(key.as_str());
8427            }
8428            let board = self.projection_board(&item).await?;
8429            let (writes, projected) = self.projection_writes(
8430                &board.fields,
8431                Some(&item.projected),
8432                &metadata,
8433                Some(&touched),
8434            )?;
8435            projection_move = Some((board.id, writes, projected));
8436        }
8437
8438        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8439        // recorded in the slot, and the slot travels in the one body update below.
8440        let edges = match &update.depends_on {
8441            Some(edges) => Some(
8442                self.partition_edges(
8443                    BoardKind::Work(ItemKind::Task),
8444                    item.content_kind,
8445                    item.blocked_by.as_deref(),
8446                    edges,
8447                )
8448                .await?,
8449            ),
8450            None => None,
8451        };
8452
8453        let mut slot = item.slot.clone();
8454        for (key, value) in &update.metadata_set {
8455            slot.insert(key.as_str().to_owned(), value.clone());
8456        }
8457        for key in &update.metadata_remove {
8458            slot.remove(key.as_str());
8459        }
8460        if let Some(delivers) = &update.delivers {
8461            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8462        }
8463        if let Some((_, recorded)) = &edges {
8464            record_edges(&mut slot, recorded);
8465        }
8466        let held = item.raw_body.clone().unwrap_or_default();
8467        let content = match &update.content {
8468            Some(content) => with_content(&held, content)?,
8469            None => held.clone(),
8470        };
8471        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8472        // the body's bytes, as a metadata write compares it: a slot a person spelled with
8473        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8474        let body = if slot == item.slot {
8475            content
8476        } else {
8477            with_slot(&content, &slot)?
8478        };
8479        // Checked before anything is sent, as a content write checks it: content ending in
8480        // what this source reads as its own slot would read back as metadata.
8481        let (visible, read) = metadata_body(Some(body.clone()))?;
8482        let wanted = update.content.as_deref().or(item.body.as_deref());
8483        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8484            return Err(SourceError::Refused {
8485                message: format!(
8486                    "this content ends in what source {} reads as its own metadata slot \
8487                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8488                     as content; next: remove that trailing block from the content",
8489                    self.name
8490                ),
8491            });
8492        }
8493        let recorded_moves =
8494            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8495
8496        // One `updateIssue` carries all three, because every mutation spends the secondary
8497        // limiter and the title, body and state are one mutation's inputs.
8498        let mut fields = serde_json::Map::new();
8499        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8500            fields.insert("title".to_owned(), json!(title));
8501        }
8502        if body != held {
8503            fields.insert("body".to_owned(), json!(body));
8504        }
8505        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8506            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8507        }
8508        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8509        // GitHub runs no two requests as one, and runs one document's mutation fields in order
8510        // without undoing an earlier field when a later one fails — so a body written before a
8511        // board field the board then refused would be left changed. Written after every other
8512        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8513        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8514        // first, together in one request — a terminal option selected before the issue
8515        // closes, as a whole write does — then the `blockedBy` difference, then the body.
8516        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8517        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8518        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
8519            board_writes.push((
8520                &moving.board,
8521                (
8522                    moving.field.clone(),
8523                    json!({"singleSelectOptionId": moving.option}),
8524                ),
8525            ));
8526        }
8527        match &priority_move {
8528            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8529                board,
8530                (field.clone(), json!({"singleSelectOptionId": option})),
8531            )),
8532            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8533            None => {}
8534        }
8535        if let Some((board, writes, _)) = &projection_move {
8536            for write in writes {
8537                let (mut values, mut cleared) = (Vec::new(), Vec::new());
8538                write.join(&mut values, &mut cleared);
8539                board_writes.extend(values.into_iter().map(|value| (board, value)));
8540                clears.extend(cleared.into_iter().map(|field| (board, field)));
8541            }
8542        }
8543        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8544        boards.extend(clears.iter().map(|(board, _)| *board));
8545        boards.dedup_by(|one, other| one.as_str() == other.as_str());
8546        for board in boards {
8547            let writes = board_writes
8548                .iter()
8549                .filter(|(on, _)| on.as_str() == board.as_str())
8550                .map(|(_, write)| write.clone())
8551                .collect::<Vec<_>>();
8552            let cleared = clears
8553                .iter()
8554                .filter(|(on, _)| on.as_str() == board.as_str())
8555                .map(|(_, field)| *field)
8556                .collect::<Vec<_>>();
8557            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8558                .await?;
8559        }
8560        let mut blocked_by_moved = false;
8561        if let Some((native, _)) = &edges
8562            && item.content_kind == ContentKind::Issue
8563        {
8564            blocked_by_moved = self
8565                .reconcile_blocked_by(
8566                    &item.id,
8567                    native,
8568                    Issue::Existing(item.blocked_by.as_deref()),
8569                )
8570                .await?;
8571        }
8572        if !fields.is_empty() {
8573            self.update_content(item.content_kind, &item.id, Value::Object(fields))
8574                .await?;
8575        }
8576
8577        // The `blockedBy` this item was read with is what the write just changed, so it is no
8578        // answer any more: a later write in this command would reconcile against it — re-adding
8579        // what it kept and never removing what it dropped — and a forward-edge read would
8580        // answer with it. Left unknown, each asks GitHub instead.
8581        if blocked_by_moved {
8582            item.blocked_by = None;
8583        }
8584        if let Some(title) = &update.title {
8585            item.title.clone_from(title);
8586        }
8587        item.body = visible.filter(|value| !value.is_empty());
8588        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8589        item.slot = slot;
8590        if let Some(delivers) = &update.delivers {
8591            item.delivers.clone_from(delivers);
8592        }
8593        if let Some(moving) = status_move {
8594            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8595                && item.content_kind == ContentKind::Issue;
8596            item.status = moving.landed;
8597            item.option = Some(moving.name);
8598        }
8599        if let Some((_, _, priority)) = priority_move {
8600            item.priority = HeldPriority::Read(priority);
8601        }
8602        if let Some((_, _, projected)) = projection_move {
8603            item.projected = projected;
8604        }
8605        let task = item.task()?;
8606        let mut written = update.changed(&before, &task);
8607        if blocked_by_moved || recorded_moves {
8608            written.insert(UpdatedField::DependsOn);
8609        }
8610        self.remember_written(item, false)?;
8611        Ok(Some(TaskUpdateOutcome {
8612            task,
8613            written,
8614            delivers_before: before.delivers,
8615        }))
8616    }
8617
8618    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8619    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8620    ///
8621    /// One update of the body: the content outside the slot, and inside it that one entry,
8622    /// every other entry kept as it was. This source keeps no template answers — an issue has
8623    /// no room beside itself that is not its body, and answers written there would duplicate
8624    /// what the content already says and count against GitHub's body limit — so `answers`
8625    /// reaches nothing here. A body that would not change is not sent at all.
8626    async fn replace_rendering(
8627        &self,
8628        id: &NativeId,
8629        kind: BoardKind,
8630        content: &str,
8631        provenance: &Value,
8632        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8633    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8634        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8635            return Ok(None);
8636        };
8637        let held = item.raw_body.clone().unwrap_or_default();
8638        let mut slot = item.slot.clone();
8639        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8640        let rewritten;
8641        let content = if let Some(assets) = assets {
8642            let uploads = self
8643                .upload_assets(item.own_repository.as_ref(), assets)
8644                .await?;
8645            rewritten =
8646                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8647            rewritten.as_str()
8648        } else {
8649            content
8650        };
8651        let body = with_slot(&with_content(&held, content)?, &slot)?;
8652        // Checked before anything is sent, as a content write checks it.
8653        let (visible, read) = metadata_body(Some(body.clone()))?;
8654        if visible.as_deref().unwrap_or_default() != content || read != slot {
8655            return Err(SourceError::Refused {
8656                message: format!(
8657                    "this content ends in what source {} reads as its own metadata slot \
8658                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8659                     as content; next: remove that trailing block from the template",
8660                    self.name
8661                ),
8662            });
8663        }
8664        if body != held {
8665            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8666                .await?;
8667        }
8668        item.body = visible.filter(|value| !value.is_empty());
8669        item.raw_body = Some(body);
8670        item.slot = read;
8671        self.remember_written(item, false)?;
8672        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8673            id: id.clone(),
8674            content: Some(content.to_owned()),
8675        }))
8676    }
8677
8678    async fn set_item_field(
8679        &self,
8680        board_id: &str,
8681        item_id: &str,
8682        field_id: &str,
8683        value: Value,
8684    ) -> Result<(), SourceError> {
8685        let data = self
8686            .graphql(
8687                graphql::UPDATE_FIELD,
8688                json!({"input":{
8689                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8690                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8691            )
8692            .await?;
8693        let returned = data
8694            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
8695            .ok_or_else(|| SourceError::Malformed {
8696                message: "GitHub field update returned no project item".into(),
8697            })?;
8698        if required_str(returned, "id")? != item_id {
8699            return Err(SourceError::Malformed {
8700                message: "GitHub field update returned the wrong project item".into(),
8701            });
8702        }
8703        Ok(())
8704    }
8705
8706    /// GitHub accepts one value per field mutation; aliases combine those mutations in
8707    /// one request — writes in the order given, then clears. A lone write is
8708    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8709    /// request's slots hold go in further requests of the same document, in order. Every
8710    /// returned item id is checked, including optional aliases.
8711    async fn set_item_fields(
8712        &self,
8713        board: &str,
8714        item: &str,
8715        fields: &[(String, Value)],
8716        clears: &[&str],
8717    ) -> Result<(), SourceError> {
8718        match (fields, clears) {
8719            ([], []) => return Ok(()),
8720            ([(field, value)], []) => {
8721                return self.set_item_field(board, item, field, value.clone()).await;
8722            }
8723            ([], [field]) => {
8724                return self
8725                    .write_priority(
8726                        board,
8727                        item,
8728                        &PriorityWrite::Clear {
8729                            field: (*field).to_owned(),
8730                        },
8731                    )
8732                    .await;
8733            }
8734            _ => {}
8735        }
8736        // Any field of the request stands in for an input a slot leaves out, which GitHub
8737        // still requires to be well formed although it never runs it.
8738        let filler = fields
8739            .first()
8740            .map_or_else(|| clears[0], |(field, _)| field.as_str());
8741        let mut writes = fields.iter();
8742        let mut cleared = clears.iter();
8743        loop {
8744            let batch: Vec<&(String, Value)> =
8745                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8746            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8747            if batch.is_empty() && batch_clears.is_empty() {
8748                return Ok(());
8749            }
8750            let mut variables = serde_json::Map::new();
8751            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8752                let input = match batch.get(index) {
8753                    Some((field, value)) => {
8754                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8755                    }
8756                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8757                                   "value":{"text":""}}),
8758                };
8759                variables.insert(slot.variable.to_owned(), input);
8760                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8761            }
8762            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8763                let field = batch_clears.get(index).map_or(filler, |field| **field);
8764                variables.insert(
8765                    slot.variable.to_owned(),
8766                    json!({"projectId":board,"itemId":item,"fieldId":field}),
8767                );
8768                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8769            }
8770            let data = self
8771                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8772                .await?;
8773            let sent = FIELD_WRITE_SLOTS[..batch.len()]
8774                .iter()
8775                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8776            for slot in sent {
8777                let alias = slot.alias;
8778                let returned = data
8779                    .get(alias)
8780                    .and_then(|value| value.get("projectV2Item"))
8781                    .ok_or_else(|| SourceError::Malformed {
8782                        message: format!("GitHub field update {alias} returned no project item"),
8783                    })?;
8784                if required_str(returned, "id")? != item {
8785                    return Err(SourceError::Malformed {
8786                        message: format!(
8787                            "GitHub field update {alias} returned the wrong project item"
8788                        ),
8789                    });
8790                }
8791            }
8792        }
8793    }
8794
8795    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8796        let mut after: Option<String> = None;
8797        let mut ids = Vec::new();
8798        loop {
8799            let data = self
8800                .graphql(
8801                    graphql::ISSUE_DEPENDENCIES,
8802                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8803                )
8804                .await?;
8805            let connection =
8806                data.pointer("/node/blockedBy")
8807                    .ok_or_else(|| SourceError::Malformed {
8808                        message: "GitHub dependency response has no blockedBy connection".into(),
8809                    })?;
8810            ids.extend(
8811                connection
8812                    .get("nodes")
8813                    .and_then(Value::as_array)
8814                    .ok_or_else(|| SourceError::Malformed {
8815                        message: "GitHub dependency response nodes is not an array".into(),
8816                    })?
8817                    .iter()
8818                    .map(|value| required_str(value, "id").map(str::to_owned))
8819                    .collect::<Result<Vec<_>, _>>()?,
8820            );
8821            let next = next_cursor(connection)?;
8822            if let Some(next) = &next {
8823                validate_cursor_progress(after.as_deref(), &next.0)?;
8824            }
8825            after = next.map(|cursor| cursor.0);
8826            if after.is_none() {
8827                return Ok(ids);
8828            }
8829        }
8830    }
8831
8832    async fn dependencies(
8833        &self,
8834        id: &NativeId,
8835        near_kind: ItemKind,
8836        direction: Direction,
8837        page: &PageRequest,
8838    ) -> Result<Page<DependencyEdge>, SourceError> {
8839        validate_page(page)?;
8840        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8841        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8842        let recorded = recorded_offset(cursor, direction)?;
8843        // What this issue is blocked by, when a read of it by its own id in this command
8844        // already carried the whole connection — a copy reads the item it writes before it
8845        // reads its edges — and the page asked for is the whole of it, or the recorded tail
8846        // after it. Answered from that read, in the shape the dependency read answers in;
8847        // anything else is asked of GitHub.
8848        let carried = match direction {
8849            Direction::DependsOn => self
8850                .resolved_cache()?
8851                .get(id)
8852                .filter(|item| item.content_kind == ContentKind::Issue)
8853                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8854            Direction::DependedOnBy => None,
8855        }
8856        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8857        // Asked for even in the recorded phase, whose page reads nothing from the
8858        // connection: `__typename` is what says whether this item has a native
8859        // relationship at all, and that is what decides which far ends the reserved key is
8860        // allowed to hold.
8861        let data = match carried {
8862            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8863                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8864            None => {
8865                let after = if recorded.is_some() { None } else { cursor };
8866                let first = page.limit.min(MAX_PAGE_SIZE);
8867                match direction {
8868                    Direction::DependsOn => {
8869                        self.graphql(
8870                            graphql::ISSUE_DEPENDS_ON,
8871                            json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
8872                                   "number":self.project_number,
8873                                   "boardItems":BOARD_ITEMS_PAGE_SIZE}),
8874                        )
8875                        .await?
8876                    }
8877                    Direction::DependedOnBy => {
8878                        self.graphql(
8879                            graphql::ISSUE_DEPENDED_ON_BY,
8880                            json!({"id":id.0,"first":first,"after":after}),
8881                        )
8882                        .await?
8883                    }
8884                }
8885            }
8886        };
8887        let draft_slot = draft_slot(&data, id, self.project_number)?;
8888        let node =
8889            data.get("node")
8890                .filter(|v| !v.is_null())
8891                .ok_or_else(|| SourceError::Refused {
8892                    message: format!(
8893                        "GitHub item {} was not found or does not support dependencies",
8894                        id.0
8895                    ),
8896                })?;
8897        let connection_name = match direction {
8898            Direction::DependsOn => "blockedBy",
8899            Direction::DependedOnBy => "blocking",
8900        };
8901        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8902        // named natively and the reserved key may hold any far end. An issue's connections
8903        // hold issues, and this source reads them at the near item's own level.
8904        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8905        if let Some(offset) = recorded {
8906            return Ok(recorded_page(
8907                self.recorded_edges(
8908                    id,
8909                    near_kind,
8910                    direction,
8911                    natively_names,
8912                    node,
8913                    draft_slot.as_ref(),
8914                )
8915                .await?,
8916                offset,
8917                limit,
8918            ));
8919        }
8920        if natively_names.is_none() {
8921            return Ok(recorded_page(
8922                self.recorded_edges(
8923                    id,
8924                    near_kind,
8925                    direction,
8926                    natively_names,
8927                    node,
8928                    draft_slot.as_ref(),
8929                )
8930                .await?,
8931                0,
8932                limit,
8933            ));
8934        }
8935        let connection = node
8936            .get(connection_name)
8937            .ok_or_else(|| SourceError::Malformed {
8938                message: "GitHub dependency response is missing its connection".into(),
8939            })?;
8940        let nodes = connection
8941            .get("nodes")
8942            .and_then(Value::as_array)
8943            .ok_or_else(|| SourceError::Malformed {
8944                message: "GitHub dependency response nodes is not an array".into(),
8945            })?;
8946        // `from` depends on `to`, always. GitHub spells the same relationship from either
8947        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8948        // it — so the near item is `from` in one direction and `to` in the other.
8949        let items = nodes
8950            .iter()
8951            .map(|value| {
8952                let related = NativeId(required_str(value, "id")?.into());
8953                let related_kind = related_kind(value)?;
8954                let (from, to) = match direction {
8955                    Direction::DependsOn => (
8956                        DependencyEndpoint::from_native(id.clone(), near_kind),
8957                        DependencyEndpoint::from_native(related, related_kind),
8958                    ),
8959                    Direction::DependedOnBy => (
8960                        DependencyEndpoint::from_native(related, related_kind),
8961                        DependencyEndpoint::from_native(id.clone(), near_kind),
8962                    ),
8963                };
8964                Ok(DependencyEdge {
8965                    from,
8966                    to,
8967                    kind: DependencyKind::Blocks,
8968                })
8969            })
8970            .collect::<Result<Vec<_>, SourceError>>()?;
8971        let mut next = next_cursor(connection)?;
8972        if let Some(next) = &next {
8973            validate_cursor_progress(cursor, &next.0)?;
8974        }
8975        // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
8976        // body they are recorded in, as far as the page has room — so they cost no request of
8977        // their own — and only what does not fit is resumed by a cursor.
8978        let mut items = items;
8979        if next.is_none() {
8980            let recorded = self
8981                .recorded_edges(
8982                    id,
8983                    near_kind,
8984                    direction,
8985                    natively_names,
8986                    node,
8987                    draft_slot.as_ref(),
8988                )
8989                .await?;
8990            let room = limit.saturating_sub(items.len());
8991            let total = recorded.len();
8992            items.extend(recorded.into_iter().take(room));
8993            if total > room {
8994                next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
8995            }
8996        }
8997        Ok(Page { items, next })
8998    }
8999
9000    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
9001    /// a far end in another source has to live: no GitHub issue relationship can name one.
9002    ///
9003    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
9004    /// source never writes one down.
9005    ///
9006    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
9007    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9008    /// request beyond the read already made, and reading the board for them would be a
9009    /// walk of every item for one field of one. A draft has no body in that answer, because
9010    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9011    /// listing of the board, which can be behind on the very item asked about.
9012    async fn recorded_edges(
9013        &self,
9014        id: &NativeId,
9015        near_kind: ItemKind,
9016        direction: Direction,
9017        natively_names: Option<ItemKind>,
9018        node: &Value,
9019        draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9020    ) -> Result<Vec<DependencyEdge>, SourceError> {
9021        if direction != Direction::DependsOn {
9022            return Ok(Vec::new());
9023        }
9024        let slot = match (node.get("body"), draft_slot) {
9025            (Some(body), _) if natively_names.is_some() => {
9026                metadata_body(body.as_str().map(str::to_owned))?.1
9027            }
9028            // A draft the dependency read itself answered: this board's, or none of its.
9029            (_, Some(Some(slot))) => slot.clone(),
9030            (_, Some(None)) => return Ok(Vec::new()),
9031            _ => {
9032                let Some(item) = self.bound_item(id).await? else {
9033                    return Ok(Vec::new());
9034                };
9035                item.slot
9036            }
9037        };
9038        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9039            .map_err(|message| SourceError::Malformed { message })
9040    }
9041
9042    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9043        self.repository
9044            .as_ref()
9045            .ok_or_else(|| SourceError::Refused {
9046                message: format!(
9047                    "source {} has no repository configured, and a GitHub Projects board has no \
9048                 repository of its own to create an issue in; set repository: owner/name on \
9049                 this source",
9050                    self.name
9051                ),
9052            })
9053    }
9054
9055    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9056    /// states.
9057    ///
9058    /// The fallback is demanded first, whichever arm answers: a write without a configured
9059    /// repository is refused naming the field exactly as it was before the rule existed,
9060    /// so a source that could not write before cannot write now, rather than writing for
9061    /// the one item whose own field happens to decide it.
9062    ///
9063    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9064    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9065    /// entry owned by someone other than the owner of the parent issue's repository —
9066    /// GitHub accepts a sub-issue from another repository of the same owner and from no
9067    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9068    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9069    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9070    /// and is visible to the token is checked where its node id is resolved, still before
9071    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9072    /// looked up in a listing of the board, which can be minutes behind an issue its own
9073    /// `projectItems` already places on it — and that read answers first from this process's
9074    /// own record, so a project created moments ago in this command answers though GitHub
9075    /// has not caught up.
9076    async fn creation_target(
9077        &self,
9078        incoming: &Incoming<'_>,
9079    ) -> Result<RepositoryTarget, SourceError> {
9080        let what = format!(
9081            "{} {:?}",
9082            incoming.written.kind().describes(),
9083            incoming.title
9084        );
9085        self.creation_rule(incoming.repositories, incoming.parent, &what)
9086            .await
9087    }
9088
9089    /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9090    /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9091    /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
9092    /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
9093    /// differently.
9094    ///
9095    /// On a source declared private, an item's one named repository is read for its
9096    /// visibility first — once per engine hold, see the `visibility` module — and one that
9097    /// does not read private decides nothing: the issue is created in the parent's
9098    /// repository, else the configured one. A read that cannot be answered refuses.
9099    async fn creation_rule(
9100        &self,
9101        repositories: &[Repository],
9102        parent: Option<&NativeId>,
9103        what: &str,
9104    ) -> Result<RepositoryTarget, SourceError> {
9105        let parent = match parent {
9106            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9107                SourceError::Refused {
9108                    message: format!(
9109                        "GitHub project issue {} was not found on the board of source {}, so {} \
9110                         cannot be filed under it",
9111                        parent.0, self.name, what
9112                    ),
9113                }
9114            })?),
9115            None => None,
9116        };
9117        let parents_repository = parent
9118            .as_ref()
9119            .map(|parent| {
9120                // A draft is on the board and so is found, but it has no repository to
9121                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9122                // would refuse the task only once `createIssue` had made it.
9123                if parent.content_kind == ContentKind::DraftIssue {
9124                    return Err(SourceError::Refused {
9125                        message: format!(
9126                            "GitHub project item {} on the board of source {} is a draft, \
9127                             which cannot have sub-issues, so {} cannot be filed under it",
9128                            parent.id.0, self.name, what
9129                        ),
9130                    });
9131                }
9132                // An issue's repository is where a sub-issue is placed and whose owner it
9133                // is compared against, so a parent whose repository this source cannot
9134                // spell as `owner/name` — GitHub's login grammar is wider than this
9135                // source's floor — is one nothing can be filed under.
9136                parent
9137                    .own_repository
9138                    .as_ref()
9139                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9140                    .ok_or_else(|| SourceError::Malformed {
9141                        message: format!(
9142                            "GitHub project issue {} on the board of source {} is in {}, which \
9143                             is not a {}/owner/name repository this source can place {} in",
9144                            parent.id.0,
9145                            self.name,
9146                            parent
9147                                .own_repository
9148                                .as_ref()
9149                                .map_or("no repository", Repository::as_str),
9150                            RepositoryTarget::HOST,
9151                            what
9152                        ),
9153                    })
9154            })
9155            .transpose()?;
9156        self.creation_rule_in_repository(repositories, parents_repository, what)
9157            .await
9158    }
9159
9160    async fn creation_rule_in_repository(
9161        &self,
9162        repositories: &[Repository],
9163        parents_repository: Option<RepositoryTarget>,
9164        what: &str,
9165    ) -> Result<RepositoryTarget, SourceError> {
9166        let fallback = self.configured_repository()?;
9167        match repositories {
9168            [named] => {
9169                let target =
9170                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9171                        message: format!(
9172                            "{} names repository {}, which is not a {}/owner/name repository \
9173                             source {} can create an issue in; name one that is, or name none",
9174                            what,
9175                            named.as_str(),
9176                            RepositoryTarget::HOST,
9177                            self.name
9178                        ),
9179                    })?;
9180                // On a source declared private, an issue whose one named repository does not
9181                // read private is created where an item naming none is: the item still names
9182                // that repository in its metadata slot, and its issue stays private.
9183                if self.is_declared_private()
9184                    && self.repository_visibility(&target).await?
9185                        != onetaskgraph_plugin_api::Visibility::Private
9186                {
9187                    return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
9188                }
9189                if let Some(parents) = &parents_repository
9190                    && parents.owner != target.owner
9191                {
9192                    return Err(SourceError::Refused {
9193                        message: format!(
9194                            "{} names repository {}, owned by {}, but its project's issue is in \
9195                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
9196                             of the same owner as its parent issue; name a repository of {}, or \
9197                             name none",
9198                            what,
9199                            target.slug(),
9200                            target.owner,
9201                            parents.slug(),
9202                            parents.owner,
9203                            parents.owner
9204                        ),
9205                    });
9206                }
9207                Ok(target)
9208            }
9209            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9210        }
9211    }
9212
9213    /// The node id of the repository `incoming` is being created in, or the refusal naming
9214    /// the item and the repository the token cannot see.
9215    ///
9216    /// Resolved once per command per repository; see [`Self::repository_cache`].
9217    async fn repository_id(
9218        &self,
9219        repository: &RepositoryTarget,
9220        incoming: &Incoming<'_>,
9221    ) -> Result<String, SourceError> {
9222        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9223            return Ok(id);
9224        }
9225        let data = self
9226            .graphql(
9227                graphql::REPOSITORY,
9228                json!({"owner":repository.owner,"name":repository.name}),
9229            )
9230            .await?;
9231        self.repository_read(&data, repository, incoming)
9232    }
9233
9234    /// The repository's node id out of an answer carrying the `repository` root, held for
9235    /// the rest of this command, or the refusal naming the item that cannot be created in it.
9236    fn repository_read(
9237        &self,
9238        data: &Value,
9239        repository: &RepositoryTarget,
9240        incoming: &Incoming<'_>,
9241    ) -> Result<String, SourceError> {
9242        let node = data
9243            .get("repository")
9244            .filter(|value| !value.is_null())
9245            .ok_or_else(|| SourceError::Refused {
9246                message: format!(
9247                    "GitHub repository {} was not found or is not visible to the token, so {} \
9248                     {:?} cannot be created in it",
9249                    repository.slug(),
9250                    incoming.written.kind().describes(),
9251                    incoming.title
9252                ),
9253            })?;
9254        let id = required_str(node, "id")?.to_owned();
9255        self.repository_cache()?
9256            .insert(repository.clone(), id.clone());
9257        Ok(id)
9258    }
9259
9260    fn repository_cache(
9261        &self,
9262    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9263        self.repository_cache
9264            .lock()
9265            .map_err(|_| SourceError::Unavailable {
9266                message: "this source's record of the destination repository was left \
9267                          inconsistent by an earlier failure; next: run the command again"
9268                    .into(),
9269            })
9270    }
9271
9272    /// Create or update one board item, whichever kind it is.
9273    async fn write_item(
9274        &self,
9275        incoming: &Incoming<'_>,
9276        target: Option<&NativeId>,
9277        depends_on: &[DependencyEdge],
9278    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9279        let prepared = self.prepare_write(incoming, target, depends_on).await?;
9280        let landed = match &prepared.destination {
9281            // Its content is written last, by `complete_write`, once everything else has landed.
9282            WriteDestination::Existing(item) => Landed {
9283                content_id: item.id.clone(),
9284                item_id: Some(item.item_id.clone()),
9285                url: item.url.clone(),
9286                number: item.number,
9287            },
9288            WriteDestination::New(target) => {
9289                if self.deferring()? {
9290                    self.create_filed_issue(
9291                        prepared.board.id.as_str(),
9292                        target,
9293                        incoming,
9294                        &prepared.body,
9295                    )
9296                    .await?
9297                } else {
9298                    self.create_and_file_issue(
9299                        prepared.board.id.as_str(),
9300                        target,
9301                        incoming,
9302                        &prepared.body,
9303                    )
9304                    .await?
9305                }
9306            }
9307        };
9308        self.complete_write(incoming, prepared, landed).await
9309    }
9310
9311    /// Everything one write of `incoming` reads and decides before it sends anything: what is
9312    /// there now, where a new item is created, the board's fields, and every value refused while
9313    /// nothing has been written — the item's body included.
9314    async fn prepare_write(
9315        &self,
9316        incoming: &Incoming<'_>,
9317        target: Option<&NativeId>,
9318        depends_on: &[DependencyEdge],
9319    ) -> Result<Prepared, SourceError> {
9320        // Refused before anything is read or written: a task or a project titled the way
9321        // this board spells a document would land as an issue this same source reads back
9322        // as a document, so the field this destination cannot carry is named rather than
9323        // written and silently reclassified.
9324        if let Written::Work(kind, _) = incoming.written
9325            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9326        {
9327            return Err(SourceError::Refused {
9328                message: format!(
9329                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9330                     spells a document, so it would read back as one rather than as a {}; \
9331                     retitle it, or copy it as a document",
9332                    kind.marker(),
9333                    self.name,
9334                    kind.marker()
9335                ),
9336            });
9337        }
9338        // The destination is read by its own id, and whether this board holds it is decided
9339        // by that read — its own `projectItems` — rather than by whether a listing of the
9340        // board happens to include it yet. See the module documentation.
9341        // An existing issue is never moved; a new one is created where the rule says — and
9342        // knowing where is what lets the board's fields and that repository's id be read
9343        // together, before anything below needs either.
9344        let destination = match target {
9345            Some(target) => {
9346                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9347                    || SourceError::Refused {
9348                        message: format!("GitHub destination item {} was not found", target.0),
9349                    },
9350                )?))
9351            }
9352            None => {
9353                let target = self.creation_target(incoming).await?;
9354                self.creation_context(&target, incoming).await?;
9355                WriteDestination::New(target)
9356            }
9357        };
9358        let existing = destination.existing();
9359        let board = self
9360            .fields_for(
9361                existing,
9362                incoming.metadata.contains_key(ORIGIN_KEY),
9363                incoming.written.status().is_some(),
9364                incoming
9365                    .priority
9366                    .is_some_and(|priority| priority != Priority::None),
9367            )
9368            .await?;
9369        let status_target = incoming
9370            .written
9371            .work_status()
9372            .map(|(kind, status)| self.resolved_target(kind, status.category))
9373            .transpose()?;
9374        let column = match (incoming.written.work_status(), status_target.as_ref()) {
9375            (Some((kind, status)), Some(target)) => {
9376                self.column_for(&board.fields, kind, status.category, target)?
9377            }
9378            _ => None,
9379        };
9380        // Resolved before anything is created, for the reason the column above is: a
9381        // priority this board has no option for is refused while nothing has been written.
9382        let priority_write = match incoming.priority {
9383            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9384            None => None,
9385        };
9386        // And for the reason the priority is: a value no projected field can hold, or a board
9387        // that cannot hold the field, is refused while nothing has been written.
9388        let (projections, projected) = self.projection_writes(
9389            &board.fields,
9390            existing.map(|item| &item.projected),
9391            incoming.metadata,
9392            None,
9393        )?;
9394        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9395        if content_kind == ContentKind::DraftIssue {
9396            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9397                (status_target.as_ref(), incoming.written.status())
9398            {
9399                return Err(self.closes_a_draft(status.category));
9400            }
9401            if incoming.parent.is_some() {
9402                return Err(SourceError::Refused {
9403                    message: "GitHub draft items cannot be a project's sub-issue".into(),
9404                });
9405            }
9406        }
9407        match existing {
9408            Some(item) if content_kind == ContentKind::Issue => {
9409                if item.labels != incoming.labels {
9410                    return Err(SourceError::Refused {
9411                        message: "GitHub issue labels differ from the labels being written".into(),
9412                    });
9413                }
9414            }
9415            _ => {
9416                if !incoming.labels.is_empty() {
9417                    return Err(SourceError::Refused {
9418                        message: "GitHub items created by this destination carry no labels".into(),
9419                    });
9420                }
9421            }
9422        }
9423
9424        // The repository the issue really lives in is what the slot below is written against,
9425        // so a single entry that is where the issue is created travels as no key at all, and
9426        // the read side derives it back from the issue.
9427        let own_repository = match &destination {
9428            WriteDestination::Existing(item) => item.own_repository.clone(),
9429            WriteDestination::New(target) => Some(
9430                Repository::try_from(target.origin())
9431                    .map_err(|message| SourceError::Config { message })?,
9432            ),
9433        };
9434        let (native, fallback) = self
9435            .partition_edges(
9436                incoming.written.kind(),
9437                content_kind,
9438                existing.and_then(|item| item.blocked_by.as_deref()),
9439                depends_on,
9440            )
9441            .await?;
9442        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9443        let content = match incoming.assets {
9444            Some(assets) => {
9445                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9446                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9447                    incoming.content.unwrap_or_default(),
9448                    &mut slot,
9449                    &uploads,
9450                );
9451                incoming.content.map(|_| rewritten)
9452            }
9453            None => incoming.content.map(str::to_owned),
9454        };
9455        let body = compose_body(content.as_deref(), &slot)?;
9456        // Read before anything is created, for the reason the field below is: a value
9457        // this destination cannot store has to refuse, and refusing after `createIssue`
9458        // would leave an issue behind that nothing asked for. The engine writes a
9459        // qualified id here; a caller handing this key anything else is told so rather
9460        // than having it silently stored as no origin at all.
9461        // llmlint: ignore[boundary_inputs_validated, changed_behavior_has_e2e] The qualified id's syntax is the engine's and not this plugin's to police: `GlobalId` is deliberately absent from the contract crate because a plugin never sees a qualified id (AGENTS.md), no plugin crate may depend on the engine to parse one, and `docs/metadata.md` says the contents of this key are what no plugin constructs or interprets. What this boundary owns is whether the value is a string its text field can hold, and that is what it checks.
9462        let origin = match incoming.metadata.get(ORIGIN_KEY) {
9463            None => "",
9464            Some(Value::String(origin)) => origin.as_str(),
9465            Some(other) => {
9466                return Err(SourceError::Refused {
9467                    message: format!(
9468                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9469                         is {other}"
9470                    ),
9471                });
9472            }
9473        };
9474        // Resolved before anything is created: a board that cannot carry the copy origin
9475        // has to refuse the write, and refusing it after `createIssue` would leave an
9476        // issue behind that nothing asked for.
9477        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9478            Some(field) => {
9479                if required_str(field, "__typename")? != "ProjectV2Field" {
9480                    return Err(SourceError::Refused {
9481                        message: format!(
9482                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9483                        ),
9484                    });
9485                }
9486                Some(required_str(field, "id")?.to_owned())
9487            }
9488            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9489                return Err(SourceError::Refused {
9490                    message: format!(
9491                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9492                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9493                         the board"
9494                    ),
9495                });
9496            }
9497            None => None,
9498        };
9499
9500        Ok(Prepared {
9501            origin: origin.to_owned(),
9502            destination,
9503            board,
9504            status_target,
9505            column,
9506            priority_write,
9507            projections,
9508            projected,
9509            content_kind,
9510            own_repository,
9511            native,
9512            slot,
9513            content,
9514            body,
9515            origin_field,
9516        })
9517    }
9518
9519    /// Everything one write does once the item it writes exists: its board fields, its
9520    /// relationships and — for an item that was already there — its content last, then what this
9521    /// process remembers of it.
9522    ///
9523    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9524    /// item this command created has its fields, its closing and its edges held for
9525    /// [`TaskSource::finish_writes`] rather than sent here.
9526    async fn complete_write(
9527        &self,
9528        incoming: &Incoming<'_>,
9529        prepared: Prepared,
9530        landed: Landed,
9531    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9532        let Prepared {
9533            destination,
9534            board,
9535            status_target,
9536            column,
9537            priority_write,
9538            projections,
9539            projected,
9540            content_kind,
9541            own_repository,
9542            native,
9543            slot,
9544            content,
9545            body,
9546            origin,
9547            origin_field,
9548        } = prepared;
9549        let existing = destination.existing();
9550        let origin = origin.as_str();
9551        let Landed {
9552            content_id,
9553            item_id,
9554            url,
9555            number,
9556        } = landed;
9557
9558        // Only what differs from the item as this write read it is sent: a priority the item
9559        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9560        // it since that read is left alone.
9561        let priority_write = priority_write.filter(|_| {
9562            !existing.is_some_and(|item| {
9563                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9564            })
9565        });
9566        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9567        let column = column
9568            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9569            .map(|(field, option, _)| (field, option));
9570        // Creating an item here is several calls — `createIssue`, which files it on the
9571        // board, then its board fields, the parent and the dependencies — and GitHub can fail
9572        // at any of them. Everything this source can refuse *before* the first of those is
9573        // already checked above, so what is left is GitHub itself failing part way. When it
9574        // does over an item this call created, the issue is taken back: a write that
9575        // refused must not leave an item behind that nobody asked for, and one that does
9576        // makes the retry create a second.
9577        // Whether the board-field write carrying a moved origin was answered as landing whole.
9578        // When it was refused, GitHub does not say which of its fields ran before the one that
9579        // failed, so the origin may or may not have moved.
9580        let mut origin_landed = false;
9581        // An item this command created while writes are held back has every part that can land
9582        // later held for `finish_writes` — its own create filed it on the board and under its
9583        // project already — and so does a later write of it in the same command.
9584        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9585        let landed = if holding {
9586            self.hold_back(
9587                &board.id,
9588                &content_id,
9589                existing,
9590                origin_field.as_deref(),
9591                origin,
9592                column,
9593                status_target.as_ref(),
9594                priority_write.as_ref(),
9595                &projections,
9596                &native,
9597            )
9598        } else if let Some(item_id) = item_id.as_deref() {
9599            self.finish_write(
9600                board.id.as_str(),
9601                incoming,
9602                &content_id,
9603                item_id,
9604                content_kind,
9605                existing,
9606                origin_field.as_deref(),
9607                origin,
9608                column,
9609                status_target.as_ref(),
9610                priority_write.as_ref(),
9611                &projections,
9612                &native,
9613                &mut origin_landed,
9614            )
9615            .await
9616        } else {
9617            // An issue a create filed on the board itself has no board item this process
9618            // knows until `finish_writes` reads it, so nothing but a held-back write may land
9619            // on it before then.
9620            Err(SourceError::Malformed {
9621                message: format!(
9622                    "issue {} was written before the board item GitHub filed it as was read",
9623                    content_id.0
9624                ),
9625            })
9626        };
9627        // An existing item's title, body and state go last, in one `updateIssue`, once its board
9628        // fields and its relationships have landed: a refusal of any of those then leaves its
9629        // body — and the metadata slot inside it — exactly as it stood.
9630        let landed = match (landed, existing) {
9631            (Ok(()), Some(item)) => {
9632                self.update_existing(item, incoming, &body, status_target.as_ref())
9633                    .await
9634            }
9635            (landed, _) => landed,
9636        };
9637        if let Err(error) = landed {
9638            match existing {
9639                // Best effort, and the write's own failure is what the caller is told: a
9640                // refusal naming the tidy-up would hide why the write failed at all.
9641                None => {
9642                    let _ = self.delete_issue(&content_id).await;
9643                }
9644                // The origin field is the one piece of an existing item's metadata written
9645                // before its body, so a write refused after it puts it back as it was. When
9646                // that is refused too, the write's own failure is still what the caller is
9647                // told — with what it left behind added, because the item's metadata is then
9648                // not as it stood and a caller retrying has to know which key moved.
9649                Some(item) => {
9650                    let before = item.origin.as_deref().unwrap_or("");
9651                    if let Some(field) = origin_field.as_deref()
9652                        && before != origin
9653                        && let Err(restore) = self
9654                            .set_item_field(
9655                                board.id.as_str(),
9656                                &item.item_id,
9657                                field,
9658                                json!({"text": before}),
9659                            )
9660                            .await
9661                    {
9662                        let left = if origin_landed {
9663                            format!(
9664                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9665                                 not be put back to {before:?} ({restore}), so item {} still \
9666                                 holds {origin:?} there",
9667                                item.id.0
9668                            )
9669                        } else {
9670                            format!(
9671                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9672                                 {origin:?}, GitHub does not say whether that part of it ran, \
9673                                 and putting it back to {before:?} was refused ({restore}), so \
9674                                 item {} holds {origin:?} or {before:?} there",
9675                                item.id.0
9676                            )
9677                        };
9678                        return Err(noting(
9679                            error,
9680                            &format!(
9681                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9682                                 run the write again"
9683                            ),
9684                        ));
9685                    }
9686                }
9687            }
9688            return Err(error);
9689        }
9690
9691        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9692            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9693                self.statuses
9694                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9695            }
9696            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9697                self.statuses
9698                    .status(kind, written_option.as_deref(), false, None)
9699            }
9700            (Some((_, status)), _) => status.clone(),
9701            (None, _) => Status {
9702                category: StatusCategory::Unknown,
9703                name: "Open".to_owned(),
9704            },
9705        };
9706
9707        // So the rest of this command reads what it just did rather than what the board
9708        // said before it. See `remember_written` for which half takes it.
9709        let remembered = Resolved {
9710            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9711            // and `learn_item` puts it here.
9712            // llmlint: ignore[invalid_states_unrepresentable] `Resolved` is serialised into the cache's stored write context, so its field type is a stored shape this change may not move; the empty id lives only until `finish_writes` reads the board item in this same command, and nothing addresses it before then — `complete_write` refuses any write naming no board item.
9713            item_id: item_id.unwrap_or_default(),
9714            id: content_id.clone(),
9715            content_kind,
9716            kind: incoming.written.kind(),
9717            title: incoming.title.to_owned(),
9718            // The visible half of the body this write composed, split back off it the
9719            // way a read splits it — so what this record reports is what a read of the
9720            // same issue reports, rather than the person's text with the metadata slot
9721            // still on the end of it.
9722            body: metadata_body(body.clone())?.0,
9723            raw_body: body.clone(),
9724            // A document has no status of its own; what it reads back as is whatever
9725            // the issue's own state says, which is what a re-read reports.
9726            status: written_status,
9727            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9728            priority: match incoming.priority {
9729                Some(priority) => HeldPriority::Read(priority),
9730                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9731                    item.priority.clone()
9732                }),
9733            },
9734            // What `state_input` asked for: closed for a terminal target, open for any other
9735            // status, and the issue's own state left as it was by a document write.
9736            closed: content_kind == ContentKind::Issue
9737                && match status_target.as_ref() {
9738                    Some(StatusTarget::Terminal(_, _)) => true,
9739                    Some(_) => false,
9740                    None => existing.is_some_and(|item| item.closed),
9741                },
9742            reason: match status_target.as_ref() {
9743                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9744                    Some(reason.reason().to_owned())
9745                }
9746                Some(_) => None,
9747                None => existing.and_then(|item| item.reason.clone()),
9748            },
9749            delivers: incoming.delivers.to_vec(),
9750            delivered_by: incoming.delivered_by.to_vec(),
9751            labels: incoming.labels.to_vec(),
9752            parent: incoming.parent.cloned(),
9753            origin: (!origin.is_empty()).then(|| origin.to_owned()),
9754            projected,
9755            number,
9756            // In the update path this is the item's own url, read off `existing` where the
9757            // record above was bound, so one expression serves both halves.
9758            url,
9759            created_at: existing.and_then(|item| item.created_at),
9760            updated_at: existing.and_then(|item| item.updated_at),
9761            // What this write moved is GitHub's to say, so a record of it gives no token until
9762            // it is read again.
9763            item_updated_at: None,
9764            comment_count: None,
9765            relationship_events: None,
9766            served_at: None,
9767            facets: None,
9768            own_repository,
9769            repositories: incoming.repositories.to_vec(),
9770            classification: incoming.classification,
9771            slot,
9772            board_id: Some(board.id.as_str().to_owned()),
9773            fields: board
9774                .fields
9775                .get("nodes")
9776                .and_then(Value::as_array)
9777                .cloned()
9778                .unwrap_or_default(),
9779            board_fields: Some(board.fields.clone()),
9780            // What this write left the relationship holding is known by id alone, and a
9781            // later read of its edges needs each far end's kind, so it reads them again.
9782            blocked_by: None,
9783        };
9784        self.remember_written(remembered, existing.is_none())?;
9785        Ok(onetaskgraph_plugin_api::AssetsWritten {
9786            id: content_id,
9787            content,
9788        })
9789    }
9790
9791    /// Everything a write does after the item exists: its board fields, its parent, and
9792    /// its dependencies.
9793    ///
9794    /// Split out of `write_item` so there is one place a failure past the point of no
9795    /// return is caught, rather than a tidy-up repeated at each `?` above.
9796    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9797    // so there is one place a failure past the point of no return is caught, and its
9798    // arguments are exactly the values that tail already had in scope. Bundling them into a
9799    // struct would describe no concept — it would be "the arguments of this function" — and
9800    // would put the whole of `write_item`'s locals behind one more indirection.
9801    #[allow(clippy::too_many_arguments)]
9802    async fn finish_write(
9803        &self,
9804        board_id: &str,
9805        incoming: &Incoming<'_>,
9806        content_id: &NativeId,
9807        item_id: &str,
9808        content_kind: ContentKind,
9809        existing: Option<&Resolved>,
9810        origin_field: Option<&str>,
9811        origin: &str,
9812        column: Option<(String, String)>,
9813        status_target: Option<&StatusTarget>,
9814        priority: Option<&PriorityWrite>,
9815        projections: &[ProjectionWrite],
9816        native: &[String],
9817        origin_landed: &mut bool,
9818    ) -> Result<(), SourceError> {
9819        let (fields, clears) = field_writes(
9820            existing,
9821            origin_field,
9822            origin,
9823            column,
9824            priority,
9825            projections,
9826        );
9827        self.set_item_fields(board_id, item_id, &fields, &clears)
9828            .await?;
9829        *origin_landed = true;
9830
9831        // An existing issue closes in the `updateIssue` its write ends with; one created just
9832        // now closes here, once its option is selected.
9833        if existing.is_none()
9834            && content_kind == ContentKind::Issue
9835            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9836        {
9837            self.update_content(
9838                ContentKind::Issue,
9839                content_id,
9840                json!({"stateInput":state_input(status_target)}),
9841            )
9842            .await?;
9843        }
9844
9845        if content_kind == ContentKind::Issue {
9846            self.reparent(
9847                existing.and_then(|item| item.parent.clone()),
9848                content_id,
9849                incoming.parent,
9850            )
9851            .await?;
9852            // A document takes part in no dependency graph, so writing one neither reads
9853            // nor changes the issue's own `blockedBy` relationships. Reconciling them
9854            // against the empty list a document write carries would *delete* whatever
9855            // relationships a person had made on that issue, which is a write nobody
9856            // asked for.
9857            if incoming.written.kind() != BoardKind::Document {
9858                let issue = match existing {
9859                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9860                    None => Issue::Created,
9861                };
9862                self.reconcile_blocked_by(content_id, native, issue).await?;
9863            }
9864        }
9865        Ok(())
9866    }
9867
9868    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9869    fn deferring(&self) -> Result<bool, SourceError> {
9870        Ok(self.deferred()?.is_some())
9871    }
9872
9873    /// Whether `content` is an item this command created whose parts are held back.
9874    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9875        Ok(self
9876            .deferred()?
9877            .as_ref()
9878            .is_some_and(|deferred| deferred.holds(content)))
9879    }
9880
9881    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9882        self.deferred.lock().map_err(|_| SourceError::Unavailable {
9883            message: "this source's record of the writes it is holding back was left \
9884                      inconsistent by an earlier failure; next: run the command again"
9885                .into(),
9886        })
9887    }
9888
9889    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9890    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9891    /// creates with: one request where adding it to the board and filing it under its project
9892    /// were two more. GitHub adds the board item after answering, so its id is not known here;
9893    /// [`TaskSource::finish_writes`] reads it.
9894    async fn create_filed_issue(
9895        &self,
9896        board_id: &str,
9897        repository: &RepositoryTarget,
9898        incoming: &Incoming<'_>,
9899        body: &Option<String>,
9900    ) -> Result<Landed, SourceError> {
9901        let repository_id = self.repository_id(repository, incoming).await?;
9902        let data = self
9903            .graphql(
9904                graphql::CREATE_ISSUE,
9905                json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9906            )
9907            .await?;
9908        let created = data
9909            .pointer("/createIssue/issue")
9910            .filter(|value| !value.is_null())
9911            .ok_or_else(|| SourceError::Malformed {
9912                message: "GitHub issue creation returned no issue".into(),
9913            })?;
9914        self.created_landed(created).await
9915    }
9916
9917    /// Keep the address `createIssue` answered for the issue `content`, for
9918    /// [`TaskSource::written_location`].
9919    fn note_written(&self, content: &NativeId, url: Option<&str>) {
9920        if let Some(url) = url {
9921            self.written
9922                .lock()
9923                .unwrap_or_else(std::sync::PoisonError::into_inner)
9924                .insert(content.clone(), url.to_owned());
9925        }
9926    }
9927
9928    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
9929    /// read, the refusal, after taking the issue back.
9930    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
9931        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
9932        let url = optional_str(created, "url")?.map(str::to_owned);
9933        let number = match created_issue_number(created) {
9934            Ok(number) => number,
9935            Err(error) => {
9936                let _ = self.delete_issue(&content_id).await;
9937                return Err(error);
9938            }
9939        };
9940        self.note_written(&content_id, url.as_deref());
9941        Ok(Landed {
9942            content_id,
9943            item_id: None,
9944            url,
9945            number,
9946        })
9947    }
9948
9949    /// Hold back everything a write of the created item `content` would send after creating it,
9950    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
9951    /// `finish_write` sends, the parent excepted, which its create already named. A later write
9952    /// of the same item replaces what it changes.
9953    // llmlint: ignore[suppressions_justified] The arguments are `finish_write`'s, which this holds back instead of sending, and they are exactly the values `complete_write` has in scope; bundling them would describe no concept.
9954    #[allow(clippy::too_many_arguments)]
9955    fn hold_back(
9956        &self,
9957        board_id: &BoardId,
9958        content: &NativeId,
9959        existing: Option<&Resolved>,
9960        origin_field: Option<&str>,
9961        origin: &str,
9962        column: Option<(String, String)>,
9963        status_target: Option<&StatusTarget>,
9964        priority: Option<&PriorityWrite>,
9965        projections: &[ProjectionWrite],
9966        native: &[String],
9967    ) -> Result<(), SourceError> {
9968        let status = column.map(|(field, option)| {
9969            let name = self
9970                .fields_cache()
9971                .ok()
9972                .and_then(|fields| {
9973                    fields.as_ref().and_then(|board| {
9974                        option_name(&board.fields, &field, &option).map(str::to_owned)
9975                    })
9976                })
9977                .unwrap_or_default();
9978            (field, option, name)
9979        });
9980        let (fields, clears) =
9981            field_writes(existing, origin_field, origin, None, priority, projections);
9982        let close = match status_target {
9983            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
9984            _ => None,
9985        };
9986        let mut deferred = self.deferred()?;
9987        let deferred = deferred.get_or_insert_with(Deferred::default);
9988        if let Deferred::Nothing = deferred {
9989            *deferred = Deferred::Held {
9990                board: board_id.clone(),
9991                created: Vec::new(),
9992            };
9993        }
9994        let Deferred::Held { created, .. } = deferred else {
9995            unreachable!("held back on the line above");
9996        };
9997        let held = match created.iter().position(|held| held.content == *content) {
9998            Some(at) => &mut created[at],
9999            None => {
10000                created.push(HeldBack {
10001                    content: content.clone(),
10002                    fields: Vec::new(),
10003                    status: None,
10004                    clears: Vec::new(),
10005                    close: None,
10006                    edges: Vec::new(),
10007                });
10008                created.last_mut().expect("just held")
10009            }
10010        };
10011        for (field, value) in fields {
10012            held.fields.retain(|(held, _)| *held != field);
10013            held.clears.retain(|held| *held != field);
10014            held.fields.push((field, value));
10015        }
10016        for field in clears {
10017            held.fields.retain(|(held, _)| held != field);
10018            if !held.clears.iter().any(|held| held == field) {
10019                held.clears.push(field.to_owned());
10020            }
10021        }
10022        if status.is_some() {
10023            held.status = status;
10024        }
10025        if close.is_some() {
10026            held.close = close;
10027        }
10028        // Nothing of an item created in this command has been sent but its create, so the edges
10029        // it is to have are exactly the ones its latest write names.
10030        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
10031        Ok(())
10032    }
10033
10034    /// Create every task of `writes`, together, filed on the board and under each one's
10035    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10036    ///
10037    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10038    /// before the first request — then created, then completed with its parts held back. A
10039    /// request refused part way leaves the issues it did create, which are taken back, with the
10040    /// rest of this call's, before the refusal is answered.
10041    async fn create_together(
10042        &self,
10043        writes: &[ItemWrite<Task>],
10044    ) -> Result<Vec<NativeId>, SourceError> {
10045        let incomings: Vec<Incoming<'_>> = writes
10046            .iter()
10047            .map(|write| self.task_incoming(write, None))
10048            .collect::<Result<_, _>>()?;
10049        let mut prepared = Vec::with_capacity(writes.len());
10050        for (incoming, write) in incomings.iter().zip(writes) {
10051            if write.target.is_some() {
10052                return Err(SourceError::Malformed {
10053                    message: "a write naming its target was handed to a batch of creates".into(),
10054                });
10055            }
10056            prepared.push(
10057                self.prepare_write(incoming, None, &write.depends_on)
10058                    .await?,
10059            );
10060        }
10061        let mut inputs = Vec::with_capacity(prepared.len());
10062        for (incoming, prepared) in incomings.iter().zip(&prepared) {
10063            let WriteDestination::New(target) = &prepared.destination else {
10064                return Err(SourceError::Malformed {
10065                    message: "an update was decided for a write handed to a batch of creates"
10066                        .into(),
10067                });
10068            };
10069            let repository_id = self.repository_id(target, incoming).await?;
10070            inputs.push(filed_input(
10071                &repository_id,
10072                prepared.board.id.as_str(),
10073                incoming,
10074                &prepared.body,
10075            ));
10076        }
10077        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10078        let refused = 'create: {
10079            for chunk in inputs.chunks(MUTATION_BATCH) {
10080                let mut variables = serde_json::Map::new();
10081                for slot in 0..MUTATION_BATCH {
10082                    variables.insert(
10083                        format!("create{slot}"),
10084                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10085                    );
10086                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10087                }
10088                let data = match self
10089                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10090                    .await
10091                {
10092                    Ok(data) => data,
10093                    Err(error) => break 'create Some(error),
10094                };
10095                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10096                    let said: Vec<&str> = errors
10097                        .as_array()
10098                        .into_iter()
10099                        .flatten()
10100                        .filter_map(|error| error.get("message").and_then(Value::as_str))
10101                        .collect();
10102                    SourceError::Refused {
10103                        message: if said.is_empty() {
10104                            "GitHub returned GraphQL errors".to_owned()
10105                        } else {
10106                            said.join("; ")
10107                        },
10108                    }
10109                });
10110                for slot in 0..chunk.len() {
10111                    match data
10112                        .pointer(&format!("/create{slot}/issue"))
10113                        .filter(|value| !value.is_null())
10114                    {
10115                        Some(created) => match self.created_landed(created).await {
10116                            Ok(created) => landed.push(created),
10117                            Err(error) => {
10118                                refusal.get_or_insert(error);
10119                            }
10120                        },
10121                        // An alias GitHub refused answers nothing, and the refusal says why.
10122                        None => {
10123                            refusal.get_or_insert(SourceError::Malformed {
10124                                message: "GitHub issue creation returned no issue".into(),
10125                            });
10126                        }
10127                    }
10128                }
10129                if let Some(error) = refusal {
10130                    break 'create Some(error);
10131                }
10132            }
10133            None
10134        };
10135        if let Some(error) = refused {
10136            // None of these ids reaches the caller, so an issue GitHub will not take back is
10137            // named here or by nobody.
10138            let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10139            let left = self.delete_issues(&ids).await;
10140            let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10141            return Err(left_behind(error, &left));
10142        }
10143        let created: Vec<NativeId> = landed
10144            .iter()
10145            .map(|landed| landed.content_id.clone())
10146            .collect();
10147        let mut ids = Vec::with_capacity(landed.len());
10148        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10149            match self.complete_write(incoming, prepared, landed).await {
10150                Ok(written) => ids.push(written.id),
10151                // llmlint: ignore[changed_behavior_has_e2e] While writes are held back, which is the only time a batch is created, completing a created item only holds its parts back, and that fails on nothing but a poisoned lock, which no journey can cause; the issues are taken back regardless, because the caller is never handed their ids.
10152                Err(error) => {
10153                    let ids: Vec<&NativeId> = created.iter().collect();
10154                    let left = self.delete_issues(&ids).await;
10155                    let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10156                    return Err(left_behind(error, &left));
10157                }
10158            }
10159        }
10160        Ok(ids)
10161    }
10162
10163    /// Hold back the update of the existing issue `issue` — `members`, what its own
10164    /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10165    /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10166    /// as the mutation itself would drop it. Answers whether it was held: never while a write
10167    /// the store's cache expects runs, which records each mutation's own answer, and never for
10168    /// an issue an update is already held for, which that one is sent ahead of.
10169    fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10170        if self.expected.journaling() || self.deferred()?.is_none() {
10171            return Ok(false);
10172        }
10173        let mut held = self
10174            .held_updates
10175            .lock()
10176            .map_err(|_| SourceError::Unavailable {
10177                message: "issue updates held back were left inconsistent; run the command again"
10178                    .into(),
10179            })?;
10180        if held.iter().any(|earlier| earlier.issue == *issue) {
10181            return Ok(false);
10182        }
10183        let named = |value: &str| issue.0 == value;
10184        self.resolved_cache()?
10185            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10186        self.read_ahead()?.retain(|id, item| {
10187            !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10188        });
10189        self.carried()?
10190            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10191        held.push(HeldUpdate::new(issue, members));
10192        Ok(true)
10193    }
10194
10195    /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10196    /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10197    /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10198    async fn send_held_updates(&self) -> Result<(), SourceError> {
10199        let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10200            SourceError::Unavailable {
10201                message: "issue updates held back were left inconsistent; run the command again"
10202                    .into(),
10203            }
10204        })?);
10205        if let [one] = held.as_slice() {
10206            return self
10207                .send_update(
10208                    graphql::UPDATE_ISSUE,
10209                    "/updateIssue/issue",
10210                    &one.issue,
10211                    one.input.clone(),
10212                )
10213                .await;
10214        }
10215        for chunk in held.chunks(MUTATION_BATCH) {
10216            let mut variables = serde_json::Map::new();
10217            for slot in 0..MUTATION_BATCH {
10218                variables.insert(
10219                    format!("update{slot}"),
10220                    chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10221                );
10222                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10223            }
10224            let data = self
10225                .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10226                .await?;
10227            let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10228            check_aliased_answers(
10229                graphql::UPDATE_ISSUES,
10230                &data,
10231                "update",
10232                &inputs,
10233                &[("issue", "id")],
10234            )?;
10235        }
10236        Ok(())
10237    }
10238
10239    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10240    /// created items' board items, then their field values and edges together in requests of
10241    /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10242    /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10243    async fn send_held_back(&self) -> Result<(), SourceError> {
10244        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10245            return Ok(());
10246        };
10247        if created.is_empty() {
10248            return Ok(());
10249        }
10250        let sent = self.send_deferred(board.as_str(), &created).await;
10251        if let Err(error) = sent {
10252            // Best effort, and the failure is what the caller is told, as a write of one item
10253            // that fails part way takes back what it created: the copy that held these back
10254            // journalled every one, so one this cannot take back its undo tries again, and says
10255            // so when it cannot either.
10256            let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10257            let _ = self.delete_issues(&ids).await;
10258            return Err(error);
10259        }
10260        Ok(())
10261    }
10262
10263    /// The requests [`Self::send_held_back`] makes.
10264    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10265        let items = self.created_board_items(board, created).await?;
10266        let mut fields: Vec<Value> = Vec::new();
10267        let mut closes: Vec<Value> = Vec::new();
10268        let mut edges: Vec<Value> = Vec::new();
10269        for held in created {
10270            let (item, status) = &items[&held.content];
10271            let mut values = held.fields.clone();
10272            // A status the board already shows — set by a workflow a person turned on to file
10273            // new items at it — is not written again.
10274            if let Some((field, option, name)) = &held.status
10275                && status.as_deref() != Some(name.as_str())
10276            {
10277                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10278            }
10279            for (field, value) in values {
10280                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10281            }
10282            if let Some(reason) = held.close {
10283                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10284            }
10285            for far in &held.edges {
10286                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10287            }
10288            if !held.clears.is_empty() {
10289                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10290                self.set_item_fields(board, item, &[], &clears).await?;
10291            }
10292        }
10293        // Each alias answers with what it wrote, which is what its input named: the board item a
10294        // field value is set on, the issue closed, the issue blocked and the one blocking it.
10295        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10296        let close_answer: &[(&str, &str)] = &[("issue", "id")];
10297        let block_answer: &[(&str, &str)] =
10298            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10299        // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10300        // request: a request holding both kinds is the one document carrying both, so a plan's
10301        // values and edges cost one request where they fit in one.
10302        let writes: Vec<HeldWrite<'_>> = fields
10303            .iter()
10304            .map(HeldWrite::Field)
10305            .chain(edges.iter().map(HeldWrite::Edge))
10306            .collect();
10307        for chunk in writes.chunks(WRITE_BATCH) {
10308            let values: Vec<&Value> = chunk
10309                .iter()
10310                .filter_map(|write| match write {
10311                    HeldWrite::Field(input) => Some(*input),
10312                    HeldWrite::Edge(_) => None,
10313                })
10314                .collect();
10315            let blocks: Vec<&Value> = chunk
10316                .iter()
10317                .filter_map(|write| match write {
10318                    HeldWrite::Edge(input) => Some(*input),
10319                    HeldWrite::Field(_) => None,
10320                })
10321                .collect();
10322            let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10323                (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10324                (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10325                _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10326            };
10327            let mut variables = serde_json::Map::new();
10328            let kinds = [
10329                ("field", &values, sends[0], field_answer),
10330                ("block", &blocks, sends[1], block_answer),
10331            ];
10332            for (prefix, inputs, send, _) in &kinds {
10333                if inputs.is_empty() {
10334                    continue;
10335                }
10336                for slot in 0..WRITE_BATCH {
10337                    variables.insert(
10338                        format!("{prefix}{slot}"),
10339                        (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10340                    );
10341                    variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10342                }
10343            }
10344            let data = self.graphql(document, Value::Object(variables)).await?;
10345            for (prefix, inputs, _, answers) in kinds {
10346                check_aliased_answers(document, &data, prefix, inputs, answers)?;
10347            }
10348        }
10349        for chunk in closes.chunks(MUTATION_BATCH) {
10350            let mut variables = serde_json::Map::new();
10351            for slot in 0..MUTATION_BATCH {
10352                variables.insert(
10353                    format!("close{slot}"),
10354                    chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10355                );
10356                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10357            }
10358            let data = self
10359                .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10360                .await?;
10361            let inputs: Vec<&Value> = chunk.iter().collect();
10362            check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10363        }
10364        for held in created {
10365            self.learn_item(&held.content, &items[&held.content].0)?;
10366        }
10367        Ok(())
10368    }
10369
10370    /// The board item each of `created` is on this board, with its `Status` as the board set it:
10371    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10372    /// eight and sixteen — for only those GitHub has not filed yet.
10373    ///
10374    /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10375    /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10376    /// create answer carrying the item — so only a read says it is there. One still absent once
10377    /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10378    /// filing it meanwhile refuses that "Content already exists in this project", which says it
10379    /// is placed, and one more read finds the item rather than a second one being made.
10380    async fn created_board_items(
10381        &self,
10382        board: &str,
10383        created: &[HeldBack],
10384    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10385        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10386        let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10387            created
10388                .iter()
10389                .map(|held| held.content.clone())
10390                .filter(|id| !found.contains_key(id))
10391                .collect()
10392        };
10393        let mut wait = Duration::from_secs(1);
10394        for attempt in 0..6 {
10395            let absent = unfiled(&found);
10396            if absent.is_empty() {
10397                return Ok(found);
10398            }
10399            if attempt > 0 {
10400                self.clock
10401                    .sleep_because(wait, pacing::cause::BOARD_FILING)
10402                    .await;
10403                wait = wait.saturating_mul(2);
10404            }
10405            self.read_created_items(board, &absent, &mut found).await?;
10406        }
10407        let mut placed_meanwhile = Vec::new();
10408        for id in unfiled(&found) {
10409            match self
10410                .graphql(
10411                    graphql::ADD_TO_BOARD,
10412                    json!({"input":{"projectId":board,"contentId":id.0}}),
10413                )
10414                .await
10415            {
10416                Ok(added) => {
10417                    let item = added
10418                        .pointer("/addProjectV2ItemById/item")
10419                        .filter(|value| !value.is_null())
10420                        .ok_or_else(|| SourceError::Malformed {
10421                            message: "GitHub board addition returned no project item".into(),
10422                        })?;
10423                    found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10424                }
10425                Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10426                Err(error) => return Err(error),
10427            }
10428        }
10429        if !placed_meanwhile.is_empty() {
10430            self.read_created_items(board, &placed_meanwhile, &mut found)
10431                .await?;
10432        }
10433        let absent = unfiled(&found);
10434        if absent.is_empty() {
10435            return Ok(found);
10436        }
10437        let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10438        Err(SourceError::Unavailable {
10439            message: format!(
10440                "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10441                 to it, still answers {} on no item of it: {}; next: run the copy again once \
10442                 GitHub has caught up",
10443                if absent.len() == 1 {
10444                    "an issue"
10445                } else {
10446                    "issues"
10447                },
10448                if absent.len() == 1 { "it" } else { "them" },
10449                if absent.len() == 1 { "it" } else { "them" },
10450                absent.join(", ")
10451            ),
10452        })
10453    }
10454
10455    /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10456    /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10457    async fn read_created_items(
10458        &self,
10459        board: &str,
10460        ids: &[NativeId],
10461        found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10462    ) -> Result<(), SourceError> {
10463        for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10464            let mut variables = serde_json::Map::new();
10465            for slot in 0..COPY_DETAIL_BATCH {
10466                variables.insert(
10467                    format!("id{slot}"),
10468                    json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10469                );
10470                variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10471            }
10472            variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10473            variables.insert("statusName".into(), json!(STATUS_FIELD));
10474            let data = self
10475                .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10476                .await?;
10477            for (slot, id) in chunk.iter().enumerate() {
10478                // An answer for another issue than the one asked about says nothing of
10479                // where that one is filed, and its board item is not this one's.
10480                let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10481                let named = node.map(|node| node.get("id").and_then(Value::as_str));
10482                if let Some(named) = named
10483                    && named != Some(id.0.as_str())
10484                {
10485                    return Err(SourceError::Malformed {
10486                        message: format!(
10487                            "GitHub answered the board items of {} when asked for those of {}",
10488                            named.unwrap_or("an issue it did not name"),
10489                            id.0
10490                        ),
10491                    });
10492                }
10493                // An issue GitHub does not answer yet is not filed yet; one it answers is
10494                // read whole, its connection, each board item's board and its status, and
10495                // any of them unreadable is refused rather than read as not filed.
10496                let Some(node) = node else { continue };
10497                let malformed = |what: &str| SourceError::Malformed {
10498                    message: format!("GitHub answered the board items of {} with {what}", id.0),
10499                };
10500                let nodes = node
10501                    .pointer("/projectItems/nodes")
10502                    .and_then(Value::as_array)
10503                    .ok_or_else(|| malformed("no list of board items"))?;
10504                let mut item = None;
10505                for node in nodes {
10506                    let on = node
10507                        .pointer("/project/id")
10508                        .and_then(Value::as_str)
10509                        .ok_or_else(|| malformed("a board item naming no board"))?;
10510                    if on == board {
10511                        item = Some(node);
10512                    }
10513                }
10514                if let Some(item) = item {
10515                    let status = match item.get("status") {
10516                        None | Some(Value::Null) => None,
10517                        Some(status) => Some(
10518                            status
10519                                .get("name")
10520                                .and_then(Value::as_str)
10521                                .ok_or_else(|| malformed("a status that names no option"))?
10522                                .to_owned(),
10523                        ),
10524                    };
10525                    found.insert(
10526                        id.clone(),
10527                        (required_nonblank_str(item, "id")?.to_owned(), status),
10528                    );
10529                }
10530            }
10531        }
10532        Ok(())
10533    }
10534
10535    /// The board item id of `content`, which this process created and remembered without one,
10536    /// in every half of its own view.
10537    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10538        if let Some(held) = self.resolved_cache()?.get_mut(content) {
10539            held.item_id = item.to_owned();
10540        }
10541        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10542            held.item_id = item.to_owned();
10543        }
10544        Ok(())
10545    }
10546
10547    /// Ask together which items each of `origins` names, and what creating an item reads; see
10548    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10549    /// page is left to its own lookup.
10550    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10551        if self.repository.is_none() {
10552            return Ok(());
10553        }
10554        let unasked: Vec<&String> = {
10555            let held = self.narrowed_cache()?;
10556            let mut unasked: Vec<&String> = Vec::new();
10557            for origin in origins {
10558                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10559                    && !unasked.contains(&origin)
10560                {
10561                    unasked.push(origin);
10562                }
10563            }
10564            unasked
10565        };
10566        if unasked.is_empty() {
10567            return Ok(());
10568        }
10569        // One batch is held back for the read ahead that follows, which may carry a project's
10570        // members beside it in the same request.
10571        if unasked.len() <= ORIGIN_BATCH {
10572            let mut pending =
10573                self.pending_origins
10574                    .lock()
10575                    .map_err(|_| SourceError::Unavailable {
10576                        message:
10577                            "origins held for a read ahead were left inconsistent; run the command \
10578                          again"
10579                                .into(),
10580                    })?;
10581            for origin in unasked {
10582                if !pending.contains(origin) {
10583                    pending.push(origin.clone());
10584                }
10585            }
10586            return Ok(());
10587        }
10588        let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10589        self.send_origin_lookups(&unasked, None).await
10590    }
10591
10592    /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10593    /// classification and repositories as the members page `data` carries them — when that page
10594    /// is every member, each one names `project` as its parent, and every record it holds is
10595    /// readable. Any other page holds nothing, so the members are read as they always were.
10596    fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10597        let Some(page) = data
10598            .pointer("/members/subIssues")
10599            .filter(|page| !page.is_null())
10600        else {
10601            return Ok(());
10602        };
10603        if page
10604            .pointer("/pageInfo/hasNextPage")
10605            .and_then(Value::as_bool)
10606            != Some(false)
10607        {
10608            return Ok(());
10609        }
10610        let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10611            return Ok(());
10612        };
10613        let mut members = Vec::with_capacity(nodes.len());
10614        for node in nodes {
10615            if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10616                return Ok(());
10617            }
10618            let Some(body) = node.get("body").and_then(Value::as_str) else {
10619                return Ok(());
10620            };
10621            // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10622            // tasks whatever its marker says, so each is a member the full read would classify.
10623            let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10624                return Ok(());
10625            };
10626            let Ok(classification) = Classification::from_metadata(&slot) else {
10627                return Ok(());
10628            };
10629            let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10630                let Ok(repositories) = Repository::from_metadata(&slot) else {
10631                    return Ok(());
10632                };
10633                repositories
10634            } else {
10635                let Some(origin) = node
10636                    .pointer("/repository/nameWithOwner")
10637                    .and_then(Value::as_str)
10638                else {
10639                    return Ok(());
10640                };
10641                let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10642                else {
10643                    return Ok(());
10644                };
10645                vec![own]
10646            };
10647            members.push((classification, repositories));
10648        }
10649        self.member_classes
10650            .lock()
10651            .map_err(|_| SourceError::Unavailable {
10652                message: "a project's members held for its classification were left \
10653                          inconsistent; run the command again"
10654                    .into(),
10655            })?
10656            .insert(project.clone(), members);
10657        Ok(())
10658    }
10659
10660    /// Send every origin held back for the read ahead, beside the members of `members` when it
10661    /// names a project — or nothing, when none is held.
10662    async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10663        let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10664            SourceError::Unavailable {
10665                message: "origins held for a read ahead were left inconsistent; run the command \
10666                          again"
10667                    .into(),
10668            }
10669        })?);
10670        if pending.is_empty() {
10671            return Ok(());
10672        }
10673        self.send_origin_lookups(&pending, members).await
10674    }
10675
10676    /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10677    /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10678    /// a project — holding each origin's answer and, from a members page that is the whole of
10679    /// them, each member's classification.
10680    async fn send_origin_lookups(
10681        &self,
10682        unasked: &[String],
10683        members: Option<&NativeId>,
10684    ) -> Result<(), SourceError> {
10685        let Some(repository) = self.repository.clone() else {
10686            return Ok(());
10687        };
10688        let mut members = members;
10689        for chunk in unasked.chunks(ORIGIN_BATCH) {
10690            let mut variables = json!({"owner":self.owner,"number":self.project_number,
10691                "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10692                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10693                "repositoryOwner":repository.owner,"repositoryName":repository.name});
10694            for slot in 0..ORIGIN_BATCH {
10695                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10696                variables[format!("filter{slot}")] =
10697                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10698                variables[format!("search{slot}")] = json!(
10699                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10700                );
10701                variables[format!("ask{slot}")] = json!(slot < chunk.len());
10702            }
10703            let project = members.take();
10704            let document = match project {
10705                Some(project) => {
10706                    variables["project"] = json!(project.0);
10707                    variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10708                    graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10709                }
10710                None => graphql::ORIGIN_LOOKUPS,
10711            };
10712            let data = self.graphql(document, variables).await?;
10713            self.fields_read(&data)?;
10714            if let Some(project) = project {
10715                self.hold_member_classes(project, &data)?;
10716            }
10717            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10718                let id = required_nonblank_str(node, "id")?.to_owned();
10719                self.repository_cache()?.insert(repository.clone(), id);
10720                if let Some(numeric) = node
10721                    .get("databaseId")
10722                    .and_then(Value::as_u64)
10723                    .and_then(std::num::NonZeroU64::new)
10724                {
10725                    self.numeric_repositories
10726                        .lock()
10727                        .await
10728                        .insert(repository.clone(), numeric);
10729                }
10730            }
10731            for (slot, origin) in chunk.iter().enumerate() {
10732                let items = data
10733                    .pointer(&format!("/o{slot}/projectV2/items"))
10734                    .filter(|value| !value.is_null())
10735                    .ok_or_else(|| SourceError::Refused {
10736                        message: format!(
10737                            "GitHub project {}/{} was not found or is not visible to the token",
10738                            self.owner, self.project_number
10739                        ),
10740                    })?;
10741                let searched =
10742                    data.get(format!("s{slot}"))
10743                        .ok_or_else(|| SourceError::Malformed {
10744                            message: "GitHub search response has no search connection".into(),
10745                        })?;
10746                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10747                    continue;
10748                }
10749                let mut found: Vec<Resolved> = Vec::new();
10750                for item in optional_nodes(Some(items), "project items")?
10751                    .into_iter()
10752                    .flatten()
10753                {
10754                    if let Some(resolved) = self.resolve(item)?
10755                        && resolved.content_kind == ContentKind::Issue
10756                        && !found.iter().any(|held| held.id == resolved.id)
10757                    {
10758                        found.push(resolved);
10759                    }
10760                }
10761                for node in optional_nodes(Some(searched), "search")?
10762                    .into_iter()
10763                    .flatten()
10764                {
10765                    if let Some(resolved) = self.resolve_issue(node).await?
10766                        && !found.iter().any(|held| held.id == resolved.id)
10767                    {
10768                        found.push(resolved);
10769                    }
10770                }
10771                self.narrowed_cache()?
10772                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
10773            }
10774        }
10775        Ok(())
10776    }
10777
10778    /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
10779    /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
10780    /// confirm, every one of a request refused whole included.
10781    async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
10782        let mut left = Vec::new();
10783        for chunk in ids.chunks(DELETE_BATCH) {
10784            let mut variables = serde_json::Map::new();
10785            for slot in 0..DELETE_BATCH {
10786                let id = chunk.get(slot).unwrap_or(&chunk[0]);
10787                variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
10788                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10789            }
10790            let data = self
10791                .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
10792                .await
10793                .unwrap_or(Value::Null);
10794            for (slot, id) in chunk.iter().enumerate() {
10795                // Confirmed only by the repository the deleted issue was in, named by a
10796                // non-blank id: anything else says nothing of whether the issue is gone.
10797                let confirmed = data
10798                    .pointer(&format!("/delete{slot}/repository/id"))
10799                    .and_then(Value::as_str)
10800                    .is_some_and(|id| !id.trim().is_empty());
10801                if !confirmed {
10802                    left.push((*id).clone());
10803                }
10804            }
10805        }
10806        left
10807    }
10808
10809    /// Delete one issue, which takes its board item with it.
10810    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10811        let data = self
10812            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10813            .await?;
10814        data.pointer("/deleteIssue/repository")
10815            .filter(|value| !value.is_null())
10816            .ok_or_else(|| SourceError::Malformed {
10817                message: "GitHub issue deletion returned no repository".into(),
10818            })?;
10819        self.forget(id)?;
10820        Ok(())
10821    }
10822
10823    /// Remove one item this copy created, so a copy that could not finish leaves the board
10824    /// as it found it.
10825    ///
10826    /// Deleting the issue takes its board item with it, so there is no second mutation to
10827    /// keep in step. An id the board does not hold is not an error: the item is already
10828    /// gone, which is the state this asks for. Which that is, is decided by reading the item
10829    /// by its own id — a listing of the board can still be missing an item it holds, and
10830    /// reading that as *already gone* would leave behind the very item this was asked to
10831    /// take back.
10832    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10833        let Some(item) = self.bound_item(id).await? else {
10834            return Ok(());
10835        };
10836        if item.content_kind == ContentKind::DraftIssue {
10837            return Err(SourceError::Refused {
10838                message: format!(
10839                    "GitHub item {} is a draft, and this source removes an item by deleting \
10840                     its issue; next: remove it from the board by hand",
10841                    id.0
10842                ),
10843            });
10844        }
10845        let data = self
10846            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10847            .await?;
10848        data.pointer("/deleteIssue/repository")
10849            .filter(|value| !value.is_null())
10850            .ok_or_else(|| SourceError::Malformed {
10851                message: "GitHub issue deletion returned no repository".into(),
10852            })?;
10853        self.forget(id)?;
10854        Ok(())
10855    }
10856
10857    /// The issue a comment call on `task` is about, or `None` when this board holds no such
10858    /// task.
10859    ///
10860    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10861    /// read of the task cannot disagree about which ids name one: a project or a document of
10862    /// this board is not a task here either.
10863    ///
10864    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10865    /// issues and a draft is not one. It is refused rather than answered with an empty page,
10866    /// which would read as a task nobody has commented on yet.
10867    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10868        let cached = self.resolved_cache()?.get(task).cloned();
10869        let Some(item) = (match cached {
10870            Some(item) => Some(item),
10871            None => self.item_by_id(task).await?,
10872        })
10873        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10874            return Ok(None);
10875        };
10876        if item.content_kind == ContentKind::DraftIssue {
10877            return Err(self.draft_has_no_comments(task));
10878        }
10879        Ok(Some(item.id))
10880    }
10881
10882    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10883    /// issues, and a draft is not one.
10884    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10885        SourceError::Refused {
10886            message: format!(
10887                "task {} of source {} is a draft item on the board, and GitHub keeps \
10888                 comments on issues alone, so a draft has none to read or write; next: \
10889                 convert the draft to an issue on the board, then comment on the issue it \
10890                 becomes",
10891                task.0, self.name
10892            ),
10893        }
10894    }
10895
10896    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10897    /// request — or `None` when this board holds no task by that id.
10898    ///
10899    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10900    /// is answered with the draft and the refusal, at the price of the draft's own read.
10901    async fn issue_detail(
10902        &self,
10903        id: &NativeId,
10904        page: &PageRequest,
10905    ) -> Result<Option<TaskDetailRead>, SourceError> {
10906        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10907        let asked = self
10908            .graphql(
10909                graphql::ISSUE_DETAIL,
10910                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10911                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10912                       "duplicates":true}),
10913            )
10914            .await;
10915        let data = match asked {
10916            Ok(data) => data,
10917            Err(error) if unresolvable_node(&error) => return Ok(None),
10918            Err(error) => return Err(error),
10919        };
10920        // `node` is null for an id that names nothing, and absent only from an answer this
10921        // source cannot read — never the same thing.
10922        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
10923            message: format!("GitHub answered the read of {} with no node", id.0),
10924        })?;
10925        self.detail_of(id, node, true, after).await
10926    }
10927
10928    /// Several tasks, each with the first page of its comments when `comments` is set, read
10929    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
10930    /// order.
10931    ///
10932    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
10933    /// one item at a time, so that id is answered as missing and the others as themselves; any
10934    /// other refusal is every id of that batch's answer.
10935    async fn issue_details(
10936        &self,
10937        ids: &[NativeId],
10938        comments: Option<&PageRequest>,
10939    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10940        let mut read = Vec::with_capacity(ids.len());
10941        for batch in ids.chunks(DETAIL_BATCH) {
10942            match self
10943                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
10944                .await
10945            {
10946                Ok(data) => {
10947                    for (slot, id) in batch.iter().enumerate() {
10948                        // Every alias asked for is answered, null for an id naming nothing;
10949                        // one missing is an answer this source cannot read.
10950                        let read_one = match data.get(format!("i{slot}")) {
10951                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
10952                            None => Err(SourceError::Malformed {
10953                                message: format!(
10954                                    "GitHub answered a batch read with no item for {}",
10955                                    id.0
10956                                ),
10957                            }),
10958                        };
10959                        read.push(read_one);
10960                    }
10961                }
10962                Err(error) if unresolvable_node(&error) => {
10963                    for id in batch {
10964                        read.push(match comments {
10965                            Some(page) => self.issue_detail(id, page).await,
10966                            None => self.task_read(id).await,
10967                        });
10968                    }
10969                }
10970                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10971            }
10972        }
10973        read
10974    }
10975
10976    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
10977    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
10978        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
10979            task,
10980            comments: None,
10981        }))
10982    }
10983
10984    /// What one node a detail read reached says: the task this board holds by `id`, with the
10985    /// page of comments the node carries when `commented` — or `None` for a node that is no
10986    /// task of this board.
10987    ///
10988    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
10989    /// and an item this process created answers from this process's own record, which a node
10990    /// read taken moments after the write can still be behind.
10991    async fn detail_of(
10992        &self,
10993        id: &NativeId,
10994        node: &Value,
10995        commented: bool,
10996        after: Option<&str>,
10997    ) -> Result<Option<TaskDetailRead>, SourceError> {
10998        if node.is_null() {
10999            return Ok(None);
11000        }
11001        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
11002        // An issue answered under one id is that id's, or the answer is not one this source
11003        // can report: reporting another issue's task and comments under the qualified id asked
11004        // for would be the one wrong answer here. A draft's own read checks the same.
11005        if !draft
11006            && optional_str(node, "__typename")? == Some("Issue")
11007            && required_str(node, "id")? != id.0
11008        {
11009            return Err(SourceError::Malformed {
11010                message: format!(
11011                    "GitHub answered the read of {} with issue {}",
11012                    id.0,
11013                    required_str(node, "id")?
11014                ),
11015            });
11016        }
11017        let item = if draft {
11018            self.draft_by_id(id).await?
11019        } else {
11020            self.resolve_issue(node).await?
11021        };
11022        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
11023        // for this command, so the dependency read the copy makes next is answered from it.
11024        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
11025            self.resolved_cache()?.insert(read.id.clone(), read.clone());
11026        }
11027        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11028            return Ok(None);
11029        };
11030        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
11031        let task = own.unwrap_or(item).task()?;
11032        let comments = match (commented, draft) {
11033            (false, _) => None,
11034            (true, true) => Some(Err(self.draft_has_no_comments(id))),
11035            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11036        };
11037        if commented && !draft && after.is_none() {
11038            self.expected.learn_comments(id, node);
11039        }
11040        Ok(Some(TaskDetailRead { task, comments }))
11041    }
11042
11043    /// Whether the comment `comment` is one of `issue`'s own.
11044    ///
11045    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11046    /// comment's id and nothing else: a comment id given against the wrong task would
11047    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11048    /// names something that is not an issue comment, is a comment this task does not have —
11049    /// which is what GitHub refusing to resolve it means too.
11050    async fn comment_is_on(
11051        &self,
11052        issue: &NativeId,
11053        comment: &NativeId,
11054    ) -> Result<bool, SourceError> {
11055        Ok(self.comment_on(issue, comment).await?.is_some())
11056    }
11057
11058    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11059    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11060    async fn comment_on(
11061        &self,
11062        issue: &NativeId,
11063        comment: &NativeId,
11064    ) -> Result<Option<String>, SourceError> {
11065        // A write the store's cache expects knows which comments its issue held, and their
11066        // bodies, at the version it expects: what this read would answer.
11067        if let Some(primed) = self.expected.primed_comment(issue, comment) {
11068            return Ok(primed);
11069        }
11070        let asked = self
11071            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11072            .await;
11073        let data = match asked {
11074            Ok(data) => data,
11075            Err(error) if unresolvable_node(&error) => return Ok(None),
11076            Err(error) => return Err(error),
11077        };
11078        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11079            return Ok(None);
11080        };
11081        if optional_str(node, "__typename")? != Some("IssueComment") {
11082            return Ok(None);
11083        }
11084        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11085            message: format!("GitHub issue comment {} names no issue", comment.0),
11086        })?;
11087        if required_str(on, "id")? != issue.0 {
11088            return Ok(None);
11089        }
11090        Ok(Some(required_str(node, "body")?.to_owned()))
11091    }
11092
11093    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11094    async fn partition_edges(
11095        &self,
11096        near_kind: BoardKind,
11097        near_content: ContentKind,
11098        carried: Option<&[Value]>,
11099        depends_on: &[DependencyEdge],
11100    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11101        let mut native = Vec::new();
11102        let mut fallback = Vec::new();
11103        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11104            .iter()
11105            .map(|edge| {
11106                // An opaque far end names another tracker, so it is never this board's own.
11107                let same_source = edge.to.designator().is_none()
11108                    && edge
11109                        .to
11110                        .source()
11111                        .is_none_or(|source| source == self.name.as_str());
11112                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11113                // `DependencyEndpoint::source` both read it that way — and a native id may hold
11114                // colons of its own, so the far end is everything after that one separator.
11115                // Splitting at the last would truncate `work:urn:task:7` to `7`.
11116                let far_id = if edge.to.is_qualified() {
11117                    edge.to
11118                        .id()
11119                        .split_once(':')
11120                        .map_or(edge.to.id(), |(_, native)| native)
11121                } else {
11122                    edge.to.id()
11123                };
11124                // One that already blocks the near issue was answered by that issue's own
11125                // read, which carried each of its blockers' kinds — an issue every one — so it
11126                // is not read again.
11127                let blocking = carried.and_then(|nodes| {
11128                    nodes
11129                        .iter()
11130                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11131                });
11132                (edge, far_id, same_source, blocking)
11133            })
11134            .collect();
11135        // Every other same-source far end is read by its own id, exactly as the item it is a
11136        // far end of is: whether this board holds it is that read's answer, never a listing's.
11137        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11138        let mut unread: Vec<NativeId> = Vec::new();
11139        for (_, far_id, same_source, blocking) in &far_ends {
11140            let id = NativeId((*far_id).to_owned());
11141            if *same_source && blocking.is_none() && !unread.contains(&id) {
11142                unread.push(id);
11143            }
11144        }
11145        let read: BTreeMap<NativeId, Option<Resolved>> = unread
11146            .iter()
11147            .cloned()
11148            .zip(self.items_by_ids(&unread).await?)
11149            .collect();
11150        for (edge, far_id, same_source, blocking) in far_ends {
11151            let far = match (same_source, blocking) {
11152                (false, _) => None,
11153                (true, Some(node)) => Some(FarEnd {
11154                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11155                        BoardKind::Document
11156                    } else {
11157                        BoardKind::Work(related_kind(node)?)
11158                    },
11159                    content_kind: ContentKind::Issue,
11160                }),
11161                (true, None) => {
11162                    let read = read
11163                        .get(&NativeId(far_id.to_owned()))
11164                        .cloned()
11165                        .flatten()
11166                        .ok_or_else(|| SourceError::Refused {
11167                            message: format!("GitHub dependency item {far_id} was not found"),
11168                        })?;
11169                    Some(FarEnd {
11170                        kind: read.kind,
11171                        content_kind: read.content_kind,
11172                    })
11173                }
11174            };
11175            let far = far.as_ref();
11176            // The caller says which kind the far end is, and this board holds the far end
11177            // itself, so a disagreement is settled here rather than stored: recorded, the
11178            // wrong kind would read back as a cross-level edge that never existed; written
11179            // natively, it would name a relationship of a different level than the caller
11180            // asked for.
11181            //
11182            // A far end this board holds as a *document* fails the same comparison and is
11183            // refused by the same sentence: `ItemKind` has no document variant because
11184            // nothing may point at one, so no caller can name it correctly and the refusal
11185            // is the only honest answer.
11186            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11187                return Err(SourceError::Refused {
11188                    message: format!(
11189                        "GitHub dependency item {far_id} is a {} of this board, and this item \
11190                         names it as a {}; record the kind it is",
11191                        disagreeing.kind.describes(),
11192                        edge.to.kind.marker()
11193                    ),
11194                });
11195            }
11196            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11197            // however the far end is spelled — and one classified native here would be
11198            // written nowhere at all, because a draft's native reconciliation never runs.
11199            let native_here = near_content == ContentKind::Issue
11200                && far.is_some_and(|far| {
11201                    far.content_kind == ContentKind::Issue
11202                        && BoardKind::Work(edge.to.kind) == near_kind
11203                });
11204            if native_here {
11205                native.push(far_id.to_owned());
11206            } else {
11207                fallback.push(edge.clone());
11208            }
11209        }
11210        Ok((native, fallback))
11211    }
11212
11213    async fn update_existing(
11214        &self,
11215        item: &Resolved,
11216        incoming: &Incoming<'_>,
11217        body: &Option<String>,
11218        status_target: Option<&StatusTarget>,
11219    ) -> Result<(), SourceError> {
11220        let title = incoming.written_title();
11221        // A terminal status closes the issue here, in the same mutation as its body: its board
11222        // option was selected before this, so a close never lands on an item whose board cannot
11223        // show it.
11224        let mut fields = match item.content_kind {
11225            ContentKind::DraftIssue => json!({"title":title,"body":body}),
11226            ContentKind::Issue => json!({"title":title,"body":body,
11227                                         "stateInput":state_input(status_target)}),
11228        };
11229        // Only the members whose value differs from the item as this write read it are sent,
11230        // and nothing at all when none does — whether or not the store's cache expects a
11231        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11232        // of it since that read is kept rather than overwritten with what it already held,
11233        // and a re-copy of an unchanged item costs no mutation.
11234        if let Some(members) = fields.as_object_mut() {
11235            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11236                members.remove("title");
11237            }
11238            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11239                (Some(Value::String(sent)), Some(held)) => sent == held,
11240                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11241                _ => false,
11242            };
11243            if body_unchanged {
11244                members.remove("body");
11245            }
11246            // An issue already in the state the target asks for keeps it: an open one stays
11247            // open, and a closed one whose closure reads as the target's category keeps its
11248            // reason — a duplicate's says more than this write would.
11249            let unchanged_state = match status_target {
11250                Some(StatusTarget::Terminal(_, reason)) => {
11251                    item.closed
11252                        && closed_category(item.reason.as_deref())
11253                            == closed_category(Some(reason.reason()))
11254                }
11255                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11256                None => true,
11257            };
11258            if unchanged_state {
11259                members.remove("stateInput");
11260            }
11261            if members.is_empty() {
11262                return Ok(());
11263            }
11264        }
11265        self.update_content(item.content_kind, &item.id, fields)
11266            .await
11267    }
11268
11269    /// Update one board item's content with exactly `fields` beside its id, through the
11270    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11271    /// a draft.
11272    ///
11273    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11274    /// is what lets a narrow write carry the one thing it changes and nothing else.
11275    async fn update_content(
11276        &self,
11277        kind: ContentKind,
11278        id: &NativeId,
11279        fields: Value,
11280    ) -> Result<(), SourceError> {
11281        let (operation, id_key, pointer) = match kind {
11282            ContentKind::DraftIssue => (
11283                graphql::UPDATE_DRAFT,
11284                "draftIssueId",
11285                "/updateProjectV2DraftIssue/draftIssue",
11286            ),
11287            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11288        };
11289        if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11290            return Ok(());
11291        }
11292        let mut input = fields;
11293        input[id_key] = json!(id.0);
11294        self.send_update(operation, pointer, id, input).await
11295    }
11296
11297    /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11298    /// to naming that item, `id`.
11299    async fn send_update(
11300        &self,
11301        operation: &str,
11302        pointer: &str,
11303        id: &NativeId,
11304        input: Value,
11305    ) -> Result<(), SourceError> {
11306        let data = self.graphql(operation, json!({"input":input})).await?;
11307        let returned = data
11308            .pointer(pointer)
11309            .ok_or_else(|| SourceError::Malformed {
11310                message: "GitHub item update returned no item".into(),
11311            })?;
11312        if required_str(returned, "id")? != id.0 {
11313            return Err(SourceError::Malformed {
11314                message: "GitHub item update returned the wrong item".into(),
11315            });
11316        }
11317        Ok(())
11318    }
11319
11320    /// Creates one issue, files it on the board, and reports what a read of it would say:
11321    /// its content id, its board item id, and the web address GitHub gave it.
11322    ///
11323    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11324    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11325    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11326    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11327    /// "Content already exists in this project". A terminal status is not written here:
11328    /// `finish_write` selects its option first and closes the issue after, so a close never
11329    /// lands on an item whose board cannot show it.
11330    ///
11331    /// The address and the number come back here because this is the only place either is
11332    /// known before GitHub's own board read catches up — an item this run created answers
11333    /// the reads that follow it out of the record below, and one remembered without them
11334    /// would report no location and no key for the rest of the run.
11335    async fn create_and_file_issue(
11336        &self,
11337        board_id: &str,
11338        repository: &RepositoryTarget,
11339        incoming: &Incoming<'_>,
11340        body: &Option<String>,
11341    ) -> Result<Landed, SourceError> {
11342        let repository_id = self.repository_id(repository, incoming).await?;
11343        let data = self
11344            .graphql(
11345                graphql::CREATE_ISSUE,
11346                json!({"input":{
11347                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11348                }}),
11349            )
11350            .await?;
11351        let created = data
11352            .pointer("/createIssue/issue")
11353            .filter(|value| !value.is_null())
11354            .ok_or_else(|| SourceError::Malformed {
11355                message: "GitHub issue creation returned no issue".into(),
11356            })?;
11357        let content_id = NativeId(required_str(created, "id")?.to_owned());
11358        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11359        // a response without it is not worth failing a landed write over — the item simply
11360        // reports no location until the board read catches up, which is what it did before.
11361        let url = optional_str(created, "url")?.map(str::to_owned);
11362        // The issue exists from here on, so an unreadable number and a refused board
11363        // filing below each try, best effort, to take it back: an issue in the repository
11364        // that is on no board is an item nobody asked for and nothing here would find again.
11365        //
11366        // Its number is optional on the same terms its address is — a landed write is not
11367        // worth failing over a member that came back missing, and such an item reports no
11368        // handle until a board read catches up. A number that is *present* and is not an
11369        // unsigned integer is still a response this source cannot read.
11370        let number = match created_issue_number(created) {
11371            Ok(number) => number,
11372            Err(error) => {
11373                let _ = self.delete_issue(&content_id).await;
11374                return Err(error);
11375            }
11376        };
11377        let added = match self
11378            .graphql(
11379                graphql::ADD_TO_BOARD,
11380                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11381            )
11382            .await
11383        {
11384            Ok(added) => added,
11385            Err(error) => {
11386                let _ = self.delete_issue(&content_id).await;
11387                return Err(error);
11388            }
11389        };
11390        let item = added
11391            .pointer("/addProjectV2ItemById/item")
11392            .filter(|value| !value.is_null())
11393            .ok_or_else(|| SourceError::Malformed {
11394                message: "GitHub board addition returned no project item".into(),
11395            })?;
11396        self.note_written(&content_id, url.as_deref());
11397        Ok(Landed {
11398            content_id,
11399            item_id: Some(required_str(item, "id")?.to_owned()),
11400            url,
11401            number,
11402        })
11403    }
11404
11405    /// Move one issue under the project it now belongs to, or out of the one it left.
11406    async fn reparent(
11407        &self,
11408        held: Option<NativeId>,
11409        child: &NativeId,
11410        wanted: Option<&NativeId>,
11411    ) -> Result<(), SourceError> {
11412        if held.as_ref() == wanted {
11413            return Ok(());
11414        }
11415        if let Some(held) = &held {
11416            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11417                .await?;
11418        }
11419        if let Some(wanted) = wanted {
11420            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11421                .await?;
11422        }
11423        Ok(())
11424    }
11425
11426    async fn sub_issue(
11427        &self,
11428        operation: &str,
11429        parent: &NativeId,
11430        child: &NativeId,
11431        root: &str,
11432    ) -> Result<(), SourceError> {
11433        let data = self
11434            .graphql(
11435                operation,
11436                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11437            )
11438            .await?;
11439        let issue =
11440            data.pointer(&format!("/{root}/issue"))
11441                .ok_or_else(|| SourceError::Malformed {
11442                    message: "GitHub sub-issue update returned no issue".into(),
11443                })?;
11444        let sub =
11445            data.pointer(&format!("/{root}/subIssue"))
11446                .ok_or_else(|| SourceError::Malformed {
11447                    message: "GitHub sub-issue update returned no sub-issue".into(),
11448                })?;
11449        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11450            return Err(SourceError::Malformed {
11451                message: "GitHub sub-issue update returned the wrong issues".into(),
11452            });
11453        }
11454        Ok(())
11455    }
11456
11457    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11458    /// whether there was one.
11459    ///
11460    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11461    /// relationships are not read: there is nothing a read of them could find.
11462    async fn reconcile_blocked_by(
11463        &self,
11464        content_id: &NativeId,
11465        native: &[String],
11466        issue: Issue<'_>,
11467    ) -> Result<bool, SourceError> {
11468        let current = match issue {
11469            Issue::Created => Vec::new(),
11470            Issue::Existing(Some(held)) => held
11471                .iter()
11472                .map(|far| required_str(far, "id").map(str::to_owned))
11473                .collect::<Result<Vec<_>, _>>()?,
11474            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11475        };
11476        let mut changed = false;
11477        for (operation, far_id) in current
11478            .iter()
11479            .filter(|id| !native.contains(id))
11480            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11481            .chain(
11482                native
11483                    .iter()
11484                    .filter(|id| !current.contains(id))
11485                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11486            )
11487        {
11488            let data = self
11489                .graphql(
11490                    operation,
11491                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11492                )
11493                .await?;
11494            let root = if operation == graphql::ADD_BLOCKED_BY {
11495                "addBlockedBy"
11496            } else {
11497                "removeBlockedBy"
11498            };
11499            let issue =
11500                data.pointer(&format!("/{root}/issue"))
11501                    .ok_or_else(|| SourceError::Malformed {
11502                        message: "GitHub dependency update returned no issue".into(),
11503                    })?;
11504            let blocker = data
11505                .pointer(&format!("/{root}/blockingIssue"))
11506                .ok_or_else(|| SourceError::Malformed {
11507                    message: "GitHub dependency update returned no blocking issue".into(),
11508                })?;
11509            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11510            {
11511                return Err(SourceError::Malformed {
11512                    message: "GitHub dependency update returned the wrong issues".into(),
11513                });
11514            }
11515            changed = true;
11516        }
11517        Ok(changed)
11518    }
11519}
11520
11521/// The board field values one write sends, in the order GitHub runs them, and the fields it
11522/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11523/// priority, then each projected field.
11524fn field_writes<'a>(
11525    existing: Option<&Resolved>,
11526    origin_field: Option<&str>,
11527    origin: &str,
11528    column: Option<(String, String)>,
11529    priority: Option<&'a PriorityWrite>,
11530    projections: &'a [ProjectionWrite],
11531) -> (Vec<(String, Value)>, Vec<&'a str>) {
11532    let mut fields = Vec::new();
11533    if let Some(field_id) = origin_field
11534        && existing.map_or(!origin.is_empty(), |item| {
11535            item.origin.as_deref().unwrap_or("") != origin
11536        })
11537    {
11538        fields.push((field_id.to_owned(), json!({"text":origin})));
11539    }
11540    if let Some((field_id, option_id)) = column {
11541        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11542    }
11543    let mut clears = Vec::new();
11544    match priority {
11545        Some(PriorityWrite::Select { field, option }) => {
11546            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11547        }
11548        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11549        None => {}
11550    }
11551    for projection in projections {
11552        projection.join(&mut fields, &mut clears);
11553    }
11554    (fields, clears)
11555}
11556
11557/// Everything one write decided before sending anything: see
11558/// [`GitHubProjectsSource::prepare_write`].
11559struct Prepared {
11560    /// What the write lands on.
11561    destination: WriteDestination,
11562    board: BoardFields,
11563    status_target: Option<StatusTarget>,
11564    /// The `Status` field, the option and the option's name the status lands on.
11565    // llmlint: ignore[invalid_states_unrepresentable] Exactly what `column_for` returns, which predates this change and four callers share; this record only carries it from `prepare_write` to `complete_write`, which a retyping of that return would reach and this change does not need.
11566    column: Option<(String, String, String)>,
11567    priority_write: Option<PriorityWrite>,
11568    projections: Vec<ProjectionWrite>,
11569    projected: BTreeMap<String, String>,
11570    content_kind: ContentKind,
11571    own_repository: Option<Repository>,
11572    /// The far ends of the item's native blocked-by relationships.
11573    // llmlint: ignore[invalid_states_unrepresentable] What `partition_edges` answers and `finish_write` takes as `&[String]`, both of which predate this change; this record only carries the value between them.
11574    native: Vec<String>,
11575    slot: BTreeMap<String, Value>,
11576    content: Option<String>,
11577    body: Option<String>,
11578    /// The copy origin the item records, empty for none.
11579    // llmlint: ignore[invalid_states_unrepresentable] `finish_write` and `field_writes` take the origin as `&str`, empty for none, as they did before this change; this record only carries it to them.
11580    origin: String,
11581    origin_field: Option<String>,
11582}
11583
11584/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11585/// edge, each its input.
11586enum HeldWrite<'a> {
11587    Field(&'a Value),
11588    Edge(&'a Value),
11589}
11590
11591/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11592/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11593/// it, rather than answering with the item it holds.
11594fn already_on_the_board(error: &SourceError) -> bool {
11595    error
11596        .to_string()
11597        .contains("Content already exists in this project")
11598}
11599
11600/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11601/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11602/// item.
11603fn check_aliased_answers(
11604    document: &str,
11605    data: &Value,
11606    prefix: &str,
11607    inputs: &[&Value],
11608    answers: &[(&str, &str)],
11609) -> Result<(), SourceError> {
11610    for (slot, input) in inputs.iter().enumerate() {
11611        for (root, named) in answers {
11612            let answered = data
11613                .pointer(&format!("/{prefix}{slot}/{root}/id"))
11614                .and_then(Value::as_str)
11615                .ok_or_else(|| SourceError::Malformed {
11616                    message: format!(
11617                        "GitHub answered {} with nothing at {prefix}{slot}",
11618                        operation_description(document)
11619                    ),
11620                })?;
11621            let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11622            if answered != asked {
11623                return Err(SourceError::Malformed {
11624                    message: format!(
11625                        "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11626                         {asked}, the item it was asked to write",
11627                        operation_description(document)
11628                    ),
11629                });
11630            }
11631        }
11632    }
11633    Ok(())
11634}
11635
11636/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11637/// back — each issue of `left` it created that is still in the repository, which its caller,
11638/// never handed their ids, could not name.
11639fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11640    if left.is_empty() {
11641        return error;
11642    }
11643    SourceError::Refused {
11644        message: format!(
11645            "{error}; GitHub then refused to take back {}, which this write created and which \
11646             are still in the repository; next: delete {} before writing again",
11647            left.join(", "),
11648            if left.len() == 1 { "it" } else { "them" }
11649        ),
11650    }
11651}
11652
11653/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11654enum WriteDestination {
11655    /// The item as it stood, for an update.
11656    Existing(Box<Resolved>),
11657    /// Where a new item is created.
11658    New(RepositoryTarget),
11659}
11660
11661impl WriteDestination {
11662    /// The item an update writes over, as it stood; `None` for a create.
11663    fn existing(&self) -> Option<&Resolved> {
11664        match self {
11665            Self::Existing(item) => Some(item),
11666            Self::New(_) => None,
11667        }
11668    }
11669}
11670
11671/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11672/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11673/// it writes beside that issue's id, which only [`Self::new`] sets.
11674struct HeldUpdate {
11675    issue: NativeId,
11676    // llmlint: ignore[invalid_states_unrepresentable] The members are exactly what `update_content` would have sent at once, which every one of its callers builds as GraphQL variables; holding them back changes when they are sent, not what they may be, and only `new` builds one, fixing the issue's id. Typing `UpdateIssueInput` would retype every caller of `update_content`, a change of its own.
11677    input: Value,
11678}
11679
11680impl HeldUpdate {
11681    /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11682    fn new(issue: &NativeId, members: &Value) -> Self {
11683        let mut input = members.clone();
11684        input["id"] = json!(issue.0);
11685        Self {
11686            issue: issue.clone(),
11687            input,
11688        }
11689    }
11690}
11691
11692/// What a copy has held back of the items it created; see
11693/// [`GitHubProjectsSource::hold_back`].
11694#[derive(Debug, Default)]
11695enum Deferred {
11696    /// Writes are being held back, and nothing has been held yet.
11697    #[default]
11698    Nothing,
11699    /// The items held back, every one of them on `board`.
11700    Held {
11701        /// The board every one of them is on.
11702        board: BoardId,
11703        /// Each created item, in the order it was created.
11704        created: Vec<HeldBack>,
11705    },
11706}
11707
11708impl Deferred {
11709    /// Whether `content` is one of the items held back.
11710    fn holds(&self, content: &NativeId) -> bool {
11711        match self {
11712            Self::Nothing => false,
11713            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11714        }
11715    }
11716}
11717
11718/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11719#[derive(Debug)]
11720struct HeldBack {
11721    /// The issue.
11722    content: NativeId,
11723    /// Its board field values other than its `Status`, in the order they are written.
11724    fields: Vec<(String, Value)>,
11725    /// The `Status` field, and the option and its name that the item's status lands on.
11726    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11727    status: Option<(String, String, String)>,
11728    /// The fields it clears after those are written.
11729    clears: Vec<String>,
11730    /// The reason it is closed for, once its option is selected; `None` leaves it open.
11731    close: Option<ClosedState>,
11732    /// The issues it is blocked by.
11733    edges: Vec<NativeId>,
11734}
11735
11736/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11737/// no GitHub type has a field of this name.
11738const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11739
11740/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11741/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11742///
11743/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11744/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11745/// created named in the data beside it — the ids a copy needs to take them back.
11746fn answer_with_errors(body: &str) -> Option<Value> {
11747    let parsed: Value = serde_json::from_str(body).ok()?;
11748    let errors = parsed
11749        .get("errors")?
11750        .as_array()
11751        .filter(|errors| !errors.is_empty())?;
11752    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11753    data[ANSWER_ERRORS] = Value::Array(errors.clone());
11754    Some(data)
11755}
11756
11757/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11758/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11759fn filed_input(
11760    repository: &str,
11761    board: &str,
11762    incoming: &Incoming<'_>,
11763    body: &Option<String>,
11764) -> Value {
11765    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11766                           "body":body,"projectV2Ids":[board]});
11767    if let Some(parent) = incoming.parent {
11768        input["parentIssueId"] = json!(parent.0);
11769    }
11770    input
11771}
11772
11773/// The name of option `option` of field `field` among a board's `fields`.
11774fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11775    fields
11776        .get("nodes")?
11777        .as_array()?
11778        .iter()
11779        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11780        .get("options")?
11781        .as_array()?
11782        .iter()
11783        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11784        .get("name")?
11785        .as_str()
11786}
11787
11788/// What a write needs to know of one far end it names: which kind of item it is, and whether
11789/// it is an issue a native relationship can name.
11790struct FarEnd {
11791    kind: BoardKind,
11792    content_kind: ContentKind,
11793}
11794
11795/// Whether the issue one write reconciles was created by that write or was already there.
11796#[derive(Clone, Copy, PartialEq, Eq)]
11797enum Issue<'a> {
11798    /// Created by this write, so it holds no relationships yet.
11799    Created,
11800    /// On the board before this write, holding whatever relationships it holds — the far
11801    /// ends of its whole `blockedBy`, when the read that reached it carried them.
11802    Existing(Option<&'a [Value]>),
11803}
11804
11805/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
11806enum Reached {
11807    /// An issue this board holds, resolved into everything this source reports about it.
11808    Held(Box<Resolved>),
11809    /// Nothing this board holds: no such node, or a node on some other board.
11810    Nothing,
11811    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11812    /// again by [`GitHubProjectsSource::draft_by_id`].
11813    Draft,
11814}
11815
11816/// What GitHub says when a string is not a node id it can resolve.
11817///
11818/// Matched because it is the ordinary answer to a project selector naming a project by its
11819/// *name*, and reporting that as a failure would make naming one impossible. It is read
11820/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11821/// does not define the syntax of a GitHub node id and would be wrong about it.
11822const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11823
11824/// `error` with `note` added to the end of what it says, its kind and every other member
11825/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11826/// what that failure left behind.
11827fn noting(error: SourceError, note: &str) -> SourceError {
11828    match error {
11829        SourceError::Config { message } => SourceError::Config {
11830            message: message + note,
11831        },
11832        SourceError::Auth { message } => SourceError::Auth {
11833            message: message + note,
11834        },
11835        SourceError::Refused { message } => SourceError::Refused {
11836            message: message + note,
11837        },
11838        SourceError::RateLimited {
11839            retry_after_seconds,
11840            message,
11841        } => SourceError::RateLimited {
11842            retry_after_seconds,
11843            message: Some(message.unwrap_or_default() + note),
11844        },
11845        SourceError::Unavailable { message } => SourceError::Unavailable {
11846            message: message + note,
11847        },
11848        SourceError::Malformed { message } => SourceError::Malformed {
11849            message: message + note,
11850        },
11851    }
11852}
11853
11854/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11855/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11856fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11857    let mut variables = serde_json::Map::new();
11858    for slot in 0..COPY_DETAIL_BATCH {
11859        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11860        variables.insert(format!("id{slot}"), json!(id));
11861    }
11862    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11863    variables.insert("children".to_owned(), json!(children));
11864    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11865    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11866    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11867    variables.insert("duplicates".to_owned(), json!(true));
11868    Value::Object(variables)
11869}
11870
11871/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11872/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11873/// for them.
11874///
11875/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11876/// is read again at no added price.
11877fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11878    let mut variables = serde_json::Map::new();
11879    for slot in 0..DETAIL_BATCH {
11880        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11881        variables.insert(format!("id{slot}"), json!(id));
11882    }
11883    variables.insert(
11884        "first".to_owned(),
11885        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11886    );
11887    variables.insert("comments".to_owned(), json!(comments.is_some()));
11888    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11889    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11890    variables.insert("duplicates".to_owned(), json!(true));
11891    Value::Object(variables)
11892}
11893
11894/// Whether this refusal is GitHub saying the id names no node at all.
11895fn unresolvable_node(error: &SourceError) -> bool {
11896    matches!(error, SourceError::Refused { message }
11897        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11898}
11899
11900/// One project name, as a search qualifier which filters on it at the server.
11901///
11902/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11903/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11904/// the way it documents. A title matched here is still compared for equality afterwards:
11905/// the qualifier narrows what the server sends, and this source decides what it names.
11906fn title_qualifier(name: &str) -> String {
11907    format!("in:title {}", quoted(name))
11908}
11909
11910/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11911/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11912/// it documents — so a value holding a qualifier's spelling is searched for rather than
11913/// obeyed.
11914fn quoted(value: &str) -> String {
11915    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11916    format!("\"{escaped}\"")
11917}
11918
11919/// The search qualifier for the issues updated at or after `since`.
11920///
11921/// Written to the second, rounded down, which can only widen what the search returns.
11922fn updated_qualifier(since: DateTime<Utc>) -> String {
11923    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
11924}
11925
11926/// The search terms that narrow a board-scoped issue search to a task query's text and
11927/// metadata predicates, or `None` when it carries neither.
11928///
11929/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
11930/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
11931/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
11932/// matches each in any field the `in:` qualifier names, so a query naming a title search and
11933/// a metadata value searches both fields for both — wider than asked, never narrower, and
11934/// every candidate is confirmed in process afterwards.
11935///
11936/// **This narrows a text search, and that is this source's declared semantics.** GitHub
11937/// matches whole tokens where a substring rule would match inside a word, so an item holding
11938/// the text only inside a longer word is not returned. A text of nothing but whitespace
11939/// matches every item, so it narrows nothing and is not sent.
11940fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
11941    let text = query
11942        .text
11943        .as_ref()
11944        .filter(|text| !text.terms.trim().is_empty());
11945    if text.is_none() && query.metadata.is_empty() {
11946        return None;
11947    }
11948    let (title, body) = match text.map(|text| text.fields) {
11949        None => (false, true),
11950        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
11951        Some(TextFields::Content) => (false, true),
11952        Some(TextFields::TitleOrContent) => (true, true),
11953    };
11954    let fields = match (title, body) {
11955        (true, true) => "in:title,body",
11956        (true, false) => "in:title",
11957        _ => "in:body",
11958    };
11959    let phrases = text
11960        .map(|text| text.terms.clone())
11961        .into_iter()
11962        .chain(
11963            query
11964                .metadata
11965                .iter()
11966                .map(|wanted| as_stored(wanted.value())),
11967        )
11968        .map(|phrase| quoted(&phrase))
11969        .collect::<Vec<_>>();
11970    Some(format!("{fields} {}", phrases.join(" ")))
11971}
11972
11973/// The search terms that narrow a board-scoped issue search to a project or document query's
11974/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
11975/// carrying that text alone is sent as by [`narrowing_qualifiers`].
11976fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
11977    narrowing_qualifiers(&TaskQuery {
11978        text: text.cloned(),
11979        ..TaskQuery::default()
11980    })
11981}
11982
11983/// Refuses a project or document query's text GitHub's issue search cannot find, before
11984/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
11985/// query's.
11986fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
11987    refuse_unsearchable(&TaskQuery {
11988        text: text.cloned(),
11989        ..TaskQuery::default()
11990    })
11991}
11992
11993/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
11994/// before anything is asked of GitHub.
11995///
11996/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
11997/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
11998/// left out, the search is every issue of the board. So this source says it cannot answer
11999/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
12000/// nothing GitHub could search for, and keeps the board read it always had.
12001fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
12002    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
12003                       letter or digit with a bounded query";
12004    if let Some(text) = &query.text
12005        && !text.terms.trim().is_empty()
12006        && !has_words(&text.terms)
12007    {
12008        return Err(SourceError::Refused {
12009            message: format!(
12010                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
12011                text.terms
12012            ),
12013        });
12014    }
12015    if let Some(wanted) = query
12016        .metadata
12017        .iter()
12018        .find(|wanted| !has_words(wanted.value()))
12019    {
12020        return Err(SourceError::Refused {
12021            message: format!(
12022                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
12023                wanted.value(),
12024                std::iter::once(wanted.key())
12025                    .chain(wanted.path().iter().map(String::as_str))
12026                    .collect::<Vec<_>>()
12027                    .join("/"),
12028            ),
12029        });
12030    }
12031    Ok(())
12032}
12033
12034/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12035fn has_words(phrase: &str) -> bool {
12036    phrase.chars().any(char::is_alphanumeric)
12037}
12038
12039/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12040///
12041/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12042/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12043/// which GitHub's word match would read as different words.
12044fn as_stored(value: &str) -> String {
12045    let encoded = Value::String(value.to_owned()).to_string();
12046    encoded[1..encoded.len() - 1].to_owned()
12047}
12048
12049/// The one narrower question a task query carrying a text, metadata or origin predicate is
12050/// sent as.
12051enum Narrowing {
12052    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12053    Origin(String),
12054    /// The board-scoped issue search narrowed by these qualifiers.
12055    Search(String),
12056}
12057
12058impl Narrowing {
12059    /// What this question is remembered under for the length of one command.
12060    fn key(&self) -> String {
12061        match self {
12062            Self::Origin(origin) => format!("origin {origin}"),
12063            Self::Search(also) => format!("search {also}"),
12064        }
12065    }
12066}
12067
12068/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12069enum Resumed {
12070    /// It reported another page, which starts after this cursor.
12071    More(String),
12072    /// It has ended. Sending this cursor again — the page's own end when it had one, and
12073    /// otherwise the cursor it was reached from — answers an empty page, so the one document
12074    /// can go on walking the other connection.
12075    Ended(Option<String>),
12076}
12077
12078impl Resumed {
12079    /// Whether the connection has another page.
12080    const fn has_more(&self) -> bool {
12081        matches!(self, Self::More(_))
12082    }
12083
12084    /// The cursor to send this connection next.
12085    fn cursor(self) -> Option<String> {
12086        match self {
12087            Self::More(next) => Some(next),
12088            Self::Ended(last) => last,
12089        }
12090    }
12091}
12092
12093/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12094/// with no cursor to it, or from a cursor that does not advance.
12095fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12096    let info = connection
12097        .get("pageInfo")
12098        .ok_or_else(|| SourceError::Malformed {
12099            message: "GitHub connection has no pageInfo".into(),
12100        })?;
12101    let end = optional_str(info, "endCursor")?;
12102    if required_bool(info, "hasNextPage")? {
12103        let next = end.ok_or_else(|| SourceError::Malformed {
12104            message: "GitHub connection reports another page and no endCursor".into(),
12105        })?;
12106        validate_cursor_progress(after, next)?;
12107        return Ok(Resumed::More(next.to_owned()));
12108    }
12109    Ok(Resumed::Ended(
12110        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12111    ))
12112}
12113
12114/// The board, and every item on it this source reports.
12115#[derive(Clone)]
12116struct Board {
12117    id: String,
12118    fields: Value,
12119    items: Vec<Resolved>,
12120}
12121
12122/// What a write needs of the board and nothing more: its node id and its field
12123/// definitions, in the shape a read of the board's own `fields` gives them.
12124///
12125/// Deliberately no items. A write decides which item it writes, which parent it files
12126/// under and which far ends it names by reading each of them by its own id; this is the
12127/// half of the board those reads cannot carry, and holding no item is what keeps it from
12128/// ever being asked whether an item is there.
12129#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12130struct BoardFields {
12131    id: BoardId,
12132    fields: Value,
12133}
12134
12135/// A board's node id: what a field write and `addProjectV2ItemById` address.
12136///
12137/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12138/// refused where it is read, and one an item names blank is read as not named at all.
12139#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12140#[serde(try_from = "String")]
12141struct BoardId(String);
12142
12143impl TryFrom<String> for BoardId {
12144    type Error = String;
12145
12146    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12147    fn try_from(id: String) -> Result<Self, Self::Error> {
12148        Self::parse(&id).map_err(|error| error.to_string())
12149    }
12150}
12151
12152/// Where one write left its item, for the record the rest of the command reads it out of.
12153///
12154/// A named record rather than a tuple because the update arm and the create arm each fill
12155/// all four, and two `Option`s of different meaning side by side in a tuple are two
12156/// positions a reader has to count.
12157struct Landed {
12158    /// The issue's own node id, which is the [`NativeId`] this source reports.
12159    content_id: NativeId,
12160    /// The board item's id, which is what a field write addresses — `None` for an issue its
12161    /// create filed on the board itself, whose board item GitHub adds after answering and
12162    /// [`TaskSource::finish_writes`] reads.
12163    // llmlint: ignore[invalid_states_unrepresentable] This field and the one below are `Resolved::item_id` and `Resolved::url` carried out of one call: the update arm assigns them from an existing `Resolved` and the whole record is assigned straight back into one. A newtype introduced here alone would be wrapped at both of those boundaries and unwrapped at every use, and would make this private record disagree with the type the same values have on the struct they come from and return to. Where the board item id gets a newtype is on `Resolved`, which is the contract's own shape and not this change's to move.
12164    item_id: Option<String>,
12165    /// The web address GitHub gave the issue, when it gave one.
12166    // llmlint: ignore[invalid_states_unrepresentable] The answer `Resolved::url` and the contract's `Task::url` already record: a web address this source never parses, resolves or compares — it reads GitHub's string and hands it back, and `Location::Url` is where the contract gives it a shape. Validating it here would have this plugin decide what GitHub may call an address.
12167    url: Option<String>,
12168    /// The issue's number on its repository, when GitHub reported one.
12169    number: Option<u64>,
12170}
12171
12172impl BoardId {
12173    fn parse(id: &str) -> Result<Self, SourceError> {
12174        if id.trim().is_empty() {
12175            return Err(SourceError::Malformed {
12176                message: "GitHub named a board with a blank node id".into(),
12177            });
12178        }
12179        Ok(Self(id.to_owned()))
12180    }
12181
12182    fn as_str(&self) -> &str {
12183        &self.0
12184    }
12185}
12186
12187impl Board {
12188    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12189        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12190        let nodes = fields
12191            .get("nodes")
12192            .and_then(Value::as_array)
12193            .ok_or_else(|| SourceError::Malformed {
12194                message: "GitHub project fields.nodes is not an array".into(),
12195            })?;
12196        Ok(nodes
12197            .iter()
12198            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12199    }
12200}
12201
12202/// One board item, resolved into everything this source reports about it.
12203///
12204/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12205/// [`TaskSource::prime_write`] takes back — see [`expected`].
12206#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12207struct Resolved {
12208    item_id: String,
12209    id: NativeId,
12210    content_kind: ContentKind,
12211    kind: BoardKind,
12212    title: String,
12213    body: Option<String>,
12214    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12215    /// that changes the slot alone has to keep byte for byte outside it.
12216    raw_body: Option<String>,
12217    status: Status,
12218    /// The name of the board `Status` option this item sits in, as the board spells it.
12219    option: Option<String>,
12220    /// What its `Priority` field says, read through this instance's mapping.
12221    priority: HeldPriority,
12222    /// Whether this item's issue is closed. A draft has no such state and is never closed.
12223    closed: bool,
12224    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12225    /// issue, a draft, and an issue closed before GitHub recorded reasons.
12226    #[serde(default)]
12227    reason: Option<String>,
12228    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12229    delivers: Vec<TaskRef>,
12230    /// Every task that delivers this one, read out of its slot. Empty for anything not a
12231    /// task.
12232    delivered_by: Vec<TaskRef>,
12233    labels: Vec<Label>,
12234    parent: Option<NativeId>,
12235    // llmlint: ignore[invalid_states_unrepresentable] The write side's reason, read back: this is the engine's qualified id, taken out of a board text field and handed on untouched. A newtype here would have this plugin define the syntax of an id `docs/metadata.md` says no plugin ever constructs or interprets.
12236    origin: Option<String>,
12237    /// What each board text field this instance projects metadata onto holds for this item,
12238    /// by field name — only the fields holding a value, an empty text included. What a write compares the
12239    /// item's metadata against, so a field already holding its value is not written again;
12240    /// never read back as metadata, which the slot alone reports.
12241    projected: BTreeMap<String, String>,
12242    /// The issue's own number on its repository, as GitHub reports it.
12243    ///
12244    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12245    /// declares none, and a draft is not filed in a repository to be numbered by one — and
12246    /// an issue this run created whose creating mutation answered without one, which is a
12247    /// response GitHub's own schema says cannot happen and which a landed write is not
12248    /// worth failing over. An `Issue` read off the board always has one.
12249    number: Option<u64>,
12250    url: Option<String>,
12251    created_at: Option<DateTime<Utc>>,
12252    updated_at: Option<DateTime<Utc>>,
12253    /// When the board item itself last changed — what a board field write moves — when the
12254    /// read that reached it said.
12255    item_updated_at: Option<DateTime<Utc>>,
12256    /// How many comments the issue holds, when the read that reached it said.
12257    comment_count: Option<u64>,
12258    /// How many relationship events the issue's timeline holds — every dependency, as
12259    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12260    /// removed — when the read that reached it said.
12261    relationship_events: Option<u64>,
12262    /// The server's clock on the response that read it — its `Date` header — when the
12263    /// response carried one.
12264    served_at: Option<DateTime<Utc>>,
12265    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12266    /// its labels — when the read that reached it carried all three whole.
12267    #[serde(default)]
12268    facets: Option<change_token::FacetsDigest>,
12269    own_repository: Option<Repository>,
12270    repositories: Vec<Repository>,
12271    classification: Classification,
12272    slot: BTreeMap<String, Value>,
12273    /// The node id of the board this item sits on, when the read that reached it said.
12274    board_id: Option<String>,
12275    /// The definition of every board field this item holds a value of, in the shape a read
12276    /// of the board's own `fields` gives one.
12277    ///
12278    /// Only the fields this item has a value in: a field it holds nothing of is not here,
12279    /// which says nothing about whether the board has it.
12280    fields: Vec<Value>,
12281    /// Every field the board this item sits on defines, as its own read of the board's
12282    /// `fields` gives them — when the read that reached the item carried them, which a read
12283    /// of it by its own id does. What a write of it needs of the board, then, needs no read
12284    /// of the board.
12285    board_fields: Option<Value>,
12286    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12287    /// selects one — when the read that reached it carried the connection to its end, which a
12288    /// read of it by its own id does for any issue blocked by no more than a page. What a
12289    /// write reconciles that relationship against, and what a read of its forward edges in
12290    /// the same command answers with.
12291    blocked_by: Option<Vec<Value>>,
12292}
12293
12294impl Resolved {
12295    /// The board this item's own read names it on, when that read named one this source can
12296    /// address.
12297    fn named_board(&self) -> Option<BoardId> {
12298        self.board_id
12299            .as_deref()
12300            .and_then(|id| BoardId::parse(id).ok())
12301    }
12302
12303    /// The board's id and every field it defines, when the read that reached this item
12304    /// carried both — which a read of it by its own id does.
12305    fn carried_board(&self) -> Option<BoardFields> {
12306        Some(BoardFields {
12307            id: self.named_board()?,
12308            fields: self.board_fields.clone()?,
12309        })
12310    }
12311
12312    /// Whether this item holds a value of the board field called `name`, and so carries
12313    /// that field's definition. `false` says nothing about whether the board has the field.
12314    fn defines(&self, name: &str) -> bool {
12315        self.fields
12316            .iter()
12317            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12318    }
12319
12320    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12321    /// in a field of its own, and none of the five keys that are only an encoding.
12322    ///
12323    /// The two delivery keys are left out for every kind, not only for a task: they are
12324    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12325    /// document carrying one holds nothing a caller's own metadata could mean by it.
12326    fn metadata(&self) -> BTreeMap<String, Value> {
12327        let mut metadata = self.slot.clone();
12328        metadata.remove(Repository::METADATA_KEY);
12329        metadata.remove(DependencyEdge::RECORDED_KEY);
12330        metadata.remove(ItemKind::METADATA_KEY);
12331        metadata.remove(TaskRef::DELIVERS_KEY);
12332        metadata.remove(TaskRef::DELIVERED_BY_KEY);
12333        metadata.remove(Classification::METADATA_KEY);
12334        // The board field is the origin, and the body's copy of it is only a mirror for the
12335        // issue search to find: an item whose field holds none has none, whatever its body
12336        // says, so no reader ever sees two answers.
12337        metadata.remove(ORIGIN_KEY);
12338        if let Some(origin) = &self.origin {
12339            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12340        }
12341        metadata
12342    }
12343
12344    /// Where this item is, as a link a reader can open.
12345    ///
12346    /// A board is a hosted place and every issue on it has a web address, so that address
12347    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12348    /// of place it is, so a reader knows to open it rather than to read a file out. It
12349    /// does not replace or derive from `url`: the field goes on reporting exactly what it
12350    /// reported before, and this says what that address *is*.
12351    ///
12352    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12353    /// rather than a third variant, which is the contract's "the source did not say". An
12354    /// issue this run created is not one of those: its address comes back from the
12355    /// creating mutation, so it is somewhere a reader can open from the moment it exists
12356    /// rather than from whenever the board read catches up.
12357    fn location(&self) -> Option<Location> {
12358        self.url.clone().map(Location::Url)
12359    }
12360
12361    /// The short handle this board's backend shows people for a task: the issue's number
12362    /// alone, as a decimal string.
12363    ///
12364    /// The number alone rather than `owner/repo#1043`, because that is the contract's
12365    /// value for this backend. A draft has no number and so no handle, which is the
12366    /// contract's *absent* rather than a handle of some other shape — and the native
12367    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12368    /// derives from.
12369    fn key(&self) -> Option<String> {
12370        self.number.map(|number| number.to_string())
12371    }
12372
12373    /// This item's change token — see [`change_token`] — or `None` when the read that
12374    /// reached it did not carry all six of its parts, which a draft never does, did not say
12375    /// when GitHub served it, or found it changed too recently to have settled.
12376    fn version(&self) -> Option<String> {
12377        match self.content_kind {
12378            ContentKind::Issue => {
12379                let (issue, item) = (self.updated_at?, self.item_updated_at?);
12380                let parts = change_token::Parts {
12381                    issue,
12382                    item,
12383                    comments: self.comment_count?,
12384                    relationships: self.relationship_events?,
12385                    facets: self.facets.clone()?,
12386                    repository: self.own_repository.clone()?,
12387                };
12388                change_token::settled(issue, item, self.served_at?)
12389                    .then(|| change_token::spell(&parts))
12390            }
12391            ContentKind::DraftIssue => None,
12392        }
12393    }
12394
12395    /// Whether its `Priority` field holds a value at all, mapped or not.
12396    fn holds_priority(&self) -> bool {
12397        self.priority != HeldPriority::Read(Priority::None)
12398    }
12399
12400    /// The task this item is.
12401    ///
12402    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12403    /// reading that as a level would be a guess, and reading it as `none` would let the next
12404    /// copy clear a priority a person set.
12405    fn task(&self) -> Result<Task, SourceError> {
12406        let priority = match &self.priority {
12407            HeldPriority::Read(priority) => *priority,
12408            HeldPriority::Unmapped(option) => {
12409                return Err(SourceError::Malformed {
12410                    message: format!(
12411                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12412                         this source's priority_mapping does not name, so its priority cannot be \
12413                         read; next: name {option:?} under priority_mapping, or move the item to \
12414                         a mapped option",
12415                        self.id,
12416                        self.number
12417                            .map(|number| format!(" (#{number})"))
12418                            .unwrap_or_default()
12419                    ),
12420                });
12421            }
12422        };
12423        Ok(Task {
12424            version: self.version(),
12425            id: self.id.clone(),
12426            key: self.key(),
12427            title: self.title.clone(),
12428            content: self.body.clone(),
12429            status: self.status.clone(),
12430            priority,
12431            labels: self.labels.clone(),
12432            project: self.parent.clone(),
12433            url: self.url.clone(),
12434            location: self.location(),
12435            created_at: self.created_at,
12436            updated_at: self.updated_at,
12437            metadata: self.metadata(),
12438            repositories: self.repositories.clone(),
12439            delivers: self.delivers.clone(),
12440            delivered_by: self.delivered_by.clone(),
12441            classification: self.classification,
12442        })
12443    }
12444
12445    fn project(&self) -> Project {
12446        Project {
12447            version: self.version(),
12448            id: self.id.clone(),
12449            title: self.title.clone(),
12450            content: self.body.clone(),
12451            status: self.status.clone(),
12452            labels: self.labels.clone(),
12453            url: self.url.clone(),
12454            location: self.location(),
12455            created_at: self.created_at,
12456            updated_at: self.updated_at,
12457            metadata: self.metadata(),
12458            repositories: self.repositories.clone(),
12459            classification: self.classification,
12460        }
12461    }
12462
12463    /// The same issue as a document: the project it is filed under, and no status and no
12464    /// dependencies, because a document is not work.
12465    fn document(&self) -> Document {
12466        Document {
12467            version: self.version(),
12468            id: self.id.clone(),
12469            title: self.title.clone(),
12470            content: self.body.clone(),
12471            project: self.parent.clone(),
12472            labels: self.labels.clone(),
12473            url: self.url.clone(),
12474            location: self.location(),
12475            created_at: self.created_at,
12476            updated_at: self.updated_at,
12477            metadata: self.metadata(),
12478            repositories: self.repositories.clone(),
12479            classification: self.classification,
12480        }
12481    }
12482}
12483
12484/// Where one targeted update moves an item's status, and which of its two halves move.
12485struct StatusMove {
12486    /// The board the item's `Status` field is on.
12487    board: BoardId,
12488    /// The `Status` field's id.
12489    field: String,
12490    /// The option's id.
12491    option: String,
12492    /// The option's name, as the board spells it.
12493    name: String,
12494    /// What the status asks of the issue's state.
12495    target: StatusTarget,
12496    /// The status the item reads as once it is there.
12497    landed: Status,
12498    /// Which of the status's two halves differ from what the item holds.
12499    moves: Moves,
12500}
12501
12502/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12503/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12504/// and is not a value of this type.
12505#[derive(Clone, Copy, PartialEq, Eq)]
12506enum Moves {
12507    /// The option alone.
12508    Option,
12509    /// The issue's state alone: open, closed, or closed with another reason.
12510    State,
12511    /// Both.
12512    Both,
12513}
12514
12515impl Moves {
12516    /// What differs, or `None` when nothing does.
12517    const fn of(option: bool, state: bool) -> Option<Self> {
12518        match (option, state) {
12519            (true, true) => Some(Self::Both),
12520            (true, false) => Some(Self::Option),
12521            (false, true) => Some(Self::State),
12522            (false, false) => None,
12523        }
12524    }
12525
12526    /// Whether the option moves.
12527    const fn option(self) -> bool {
12528        matches!(self, Self::Option | Self::Both)
12529    }
12530
12531    /// Whether the issue's state moves.
12532    const fn state(self) -> bool {
12533        matches!(self, Self::State | Self::Both)
12534    }
12535}
12536
12537/// What one write is, and the status that comes with being it.
12538///
12539/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12540/// status and a task or a project always has one, so "a document carrying a status" and
12541/// "a task carrying none" are states a write cannot be in rather than states every use
12542/// site below has to defend against.
12543enum Written<'a> {
12544    /// A document, which is not work and so has no status at all.
12545    Document,
12546    /// A task or a project, and the status it is being written with.
12547    Work(ItemKind, &'a Status),
12548}
12549
12550impl Written<'_> {
12551    /// Which of the board's three kinds this write is.
12552    const fn kind(&self) -> BoardKind {
12553        match self {
12554            Self::Document => BoardKind::Document,
12555            Self::Work(kind, _) => BoardKind::Work(*kind),
12556        }
12557    }
12558
12559    /// The status this write carries. A document carries none, so a write of one says
12560    /// nothing about the issue's open or closed state and selects no board `Status`
12561    /// option.
12562    const fn status(&self) -> Option<&Status> {
12563        match self {
12564            Self::Document => None,
12565            Self::Work(_, status) => Some(status),
12566        }
12567    }
12568
12569    /// The status this write carries with the kind whose half of `status_mapping` it is
12570    /// written through.
12571    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12572        match self {
12573            Self::Document => None,
12574            Self::Work(kind, status) => Some((*kind, status)),
12575        }
12576    }
12577}
12578
12579/// The item being written, in the one shape all three write methods reach.
12580struct Incoming<'a> {
12581    written: Written<'a>,
12582    /// The title a person wrote. A document's goes onto the issue with
12583    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12584    title: &'a str,
12585    content: Option<&'a str>,
12586    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12587    labels: &'a [Label],
12588    metadata: &'a BTreeMap<String, Value>,
12589    repositories: &'a [Repository],
12590    /// Recorded in the slot while private, and nowhere while public.
12591    classification: Classification,
12592    parent: Option<&'a NativeId>,
12593    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12594    /// what keeps either key out of their slot.
12595    delivers: &'a [TaskRef],
12596    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12597    delivered_by: &'a [TaskRef],
12598    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12599    /// project, a document, and every write to an instance with no `priority_mapping` —
12600    /// which is what keeps such a write's requests exactly what they were before.
12601    priority: Option<Priority>,
12602}
12603
12604/// What one write does to one projected metadata text field, by the field's id.
12605#[derive(Debug, Clone)]
12606enum ProjectionWrite {
12607    /// Write this text.
12608    Set {
12609        field: String, // llmlint: ignore[invalid_states_unrepresentable] A private opaque GraphQL field id passed straight back to GitHub, as `PriorityWrite`'s are; GitHub publishes no grammar for it.
12610        text: String,
12611    },
12612    /// Clear the field's value.
12613    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12614}
12615
12616impl ProjectionWrite {
12617    /// Add this write to a field write's list of values and list of clears.
12618    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12619        match self {
12620            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12621            Self::Clear { field } => clears.push(field),
12622        }
12623    }
12624}
12625
12626/// What one write does to an item's `Priority` field.
12627enum PriorityWrite {
12628    /// Select this option of this field.
12629    Select {
12630        /// The `Priority` field's id.
12631        field: String,
12632        /// The mapped option's id.
12633        option: String,
12634    },
12635    /// Clear the field's value, which is what `none` is.
12636    Clear {
12637        /// The `Priority` field's id.
12638        field: String,
12639    },
12640}
12641
12642impl Incoming<'_> {
12643    /// The title this write puts on the issue.
12644    fn written_title(&self) -> String {
12645        match self.written {
12646            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12647            Written::Work(..) => self.title.to_owned(),
12648        }
12649    }
12650}
12651
12652#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12653enum ContentKind {
12654    DraftIssue,
12655    Issue,
12656}
12657
12658/// What one board issue is: a document, or the work an [`ItemKind`] names.
12659///
12660/// A type of this source's own rather than an `ItemKind` with a third variant, because
12661/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12662/// document — the contract keeps a document out of that enum deliberately. Holding the
12663/// board's three answers in one value is what makes every place that asks "which is this?"
12664/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12665/// two thirds of the board.
12666#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12667enum BoardKind {
12668    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12669    Document,
12670    /// Every other issue, and every draft.
12671    Work(ItemKind),
12672}
12673
12674impl BoardKind {
12675    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12676    /// sub-issues and carrying the kind marker `marked` in its slot.
12677    ///
12678    /// The design prefix is read *first*, before either of the two rules that separate a
12679    /// project from a task. A document is not work whatever sub-issues it has and whatever
12680    /// marker it carries, and reading the prefix later would make a design issue with none of
12681    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12682    /// overrides it: an issue filed under a project is that project's task even when it has
12683    /// sub-issues of its own.
12684    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12685        if title.starts_with(DESIGN_TITLE_PREFIX) {
12686            Self::Document
12687        } else if filed {
12688            Self::Work(ItemKind::Task)
12689        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12690            Self::Work(ItemKind::Project)
12691        } else {
12692            Self::Work(ItemKind::Task)
12693        }
12694    }
12695
12696    /// What the change token's facets call this kind.
12697    const fn facet(self) -> change_token::FacetKind {
12698        match self {
12699            Self::Document => change_token::FacetKind::Document,
12700            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12701            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12702        }
12703    }
12704
12705    /// Whose half of `status_mapping` an item of this kind reads its status through. A
12706    /// document has no status of its own, so the task half stands in for whatever the issue
12707    /// holds; nothing reports it.
12708    const fn status_kind(self) -> ItemKind {
12709        match self {
12710            Self::Document => ItemKind::Task,
12711            Self::Work(kind) => kind,
12712        }
12713    }
12714
12715    /// How a refusal names this kind to the person reading it.
12716    const fn describes(self) -> &'static str {
12717        match self {
12718            Self::Document => "document",
12719            Self::Work(kind) => kind.marker(),
12720        }
12721    }
12722}
12723
12724/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12725///
12726/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12727/// the shared cross-source journeys assert one answer to one question, so two sources
12728/// that disagree about what "carries the label bug" means fail them.
12729fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12730    let holds = |name: &String| {
12731        labels
12732            .iter()
12733            .any(|label| label.name.eq_ignore_ascii_case(name))
12734    };
12735    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12736        && filter.all_of.iter().all(holds)
12737        && !filter.none_of.iter().any(holds)
12738}
12739
12740/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12741/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12742fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12743    statuses.is_empty() || statuses.contains(&category)
12744}
12745
12746/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12747///
12748/// `content` is the item's own prose — the body with this source's trailing metadata
12749/// comment already taken off — so a search never matches an encoding the author of the
12750/// issue never wrote.
12751fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12752    let terms = query.terms.to_lowercase();
12753    let in_title = title.to_lowercase().contains(&terms);
12754    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12755    match query.fields {
12756        TextFields::Title => in_title,
12757        TextFields::Content => in_content,
12758        TextFields::TitleOrContent => in_title || in_content,
12759    }
12760}
12761
12762/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12763///
12764/// The project predicate is passed separately because a read narrowed to one project has
12765/// already answered it by asking *that project* for its own items — and re-applying it
12766/// there would compare the caller's selector, which may be a project's **name**, against
12767/// the id of the project that name resolved to, and keep nothing. Every other read passes
12768/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12769/// source really does apply.
12770fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12771    labels_match(&task.labels, &query.labels)
12772        && status_matches(task.status.category, &query.statuses)
12773        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12774        && match project {
12775            ProjectFilter::Any => true,
12776            ProjectFilter::Orphans => task.project.is_none(),
12777            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12778        }
12779        && query
12780            .text
12781            .as_ref()
12782            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12783        // Against the parsed metadata slot, and against the origin field, which is where
12784        // `Resolved::metadata` reads each of them from.
12785        && query.metadata_matches(&task.metadata)
12786        && query.origin_matches(&task.metadata)
12787}
12788
12789fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12790    labels_match(&project.labels, &query.labels)
12791        && status_matches(project.status.category, &query.statuses)
12792        && query
12793            .text
12794            .as_ref()
12795            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12796}
12797
12798/// The same three predicates a task query carries, minus the status filter.
12799///
12800/// A document is not work, so it has no status for one to compare against and the query
12801/// type carries none. The project predicate is the same one — a design issue filed under a
12802/// project issue is in that project, and one filed under nothing is in none — so it is
12803/// spelled the same way here rather than answered differently.
12804fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
12805    labels_match(&document.labels, &query.labels)
12806        && match project {
12807            ProjectFilter::Any => true,
12808            ProjectFilter::Orphans => document.project.is_none(),
12809            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12810        }
12811        && query
12812            .text
12813            .as_ref()
12814            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12815}
12816
12817/// Why a GitHub item's change token does not vouch for its forward edges.
12818///
12819/// A forward edge names its far end and that far end's kind, and the near item's token moves
12820/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12821/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12822/// nothing the near token reads, so an edge kept at the near version could name a far end that
12823/// is no longer what it said.
12824const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12825     whether it became a document, or its deletion, so its forward edges are always read from \
12826     github";
12827
12828/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12829/// categories an open issue reads as narrows a board-scoped search with.
12830const OPEN_QUALIFIER: &str = "is:open";
12831
12832/// The board filter `task status reconcile` reads the board through: closed items alone.
12833const CLOSED_FILTER: &str = "is:closed";
12834
12835/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12836/// one reference, and a slot the batch has none for names its last one again.
12837fn reference_batch(batch: &[IssueRef]) -> Value {
12838    let mut variables = serde_json::Map::new();
12839    for slot in 0..REFERENCE_BATCH {
12840        let issue = batch.get(slot).or(batch.last());
12841        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12842        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12843        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12844    }
12845    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12846    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12847    variables.insert("duplicates".to_owned(), json!(true));
12848    Value::Object(variables)
12849}
12850
12851/// One issue an issue read by reference answered, in the shape every other read of an issue
12852/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12853/// value that membership carries.
12854///
12855/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12856/// field of that name — and otherwise an option value naming its option; anything else is not
12857/// an answer to this read, and is refused rather than read as a card at no option.
12858fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12859    let malformed = |what: &str| SourceError::Malformed {
12860        message: format!("GitHub answered an issue read by reference with {what}"),
12861    };
12862    let mut issue = issue.clone();
12863    let nodes = issue
12864        .pointer_mut("/projectItems/nodes")
12865        .and_then(Value::as_array_mut)
12866        .ok_or_else(|| malformed("no list of board memberships"))?;
12867    for node in nodes {
12868        let node = node
12869            .as_object_mut()
12870            .ok_or_else(|| malformed("a board membership that is not an object"))?;
12871        let status = match node.get("status") {
12872            None => return Err(malformed("a board membership with no status member")),
12873            Some(Value::Null) => None,
12874            Some(status) if status.get("name").is_some_and(Value::is_string) => {
12875                Some(status.clone())
12876            }
12877            // A single-select value of another field type answers `{}`; only an option value
12878            // carries a name.
12879            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12880            Some(_) => return Err(malformed("a membership status that names no option")),
12881        };
12882        node.insert(
12883            "fieldValues".to_owned(),
12884            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12885                   "pageInfo": {"hasNextPage": false}}),
12886        );
12887    }
12888    Ok(issue)
12889}
12890
12891/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12892/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12893/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12894/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12895/// than a reference nothing holds.
12896fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12897    let alias = format!("r{slot}");
12898    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12899        message: format!("GitHub answered a read by reference with no {alias}"),
12900    })?;
12901    if repository.is_null() {
12902        return Ok(None);
12903    }
12904    let issue = repository
12905        .get("issue")
12906        .ok_or_else(|| SourceError::Malformed {
12907            message: format!("GitHub answered {alias} of a read by reference with no issue"),
12908        })?;
12909    if issue.is_null() {
12910        return Ok(None);
12911    }
12912    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12913    if optional_str(issue, "__typename")? != Some("Issue") {
12914        return Err(SourceError::Malformed {
12915            message: format!(
12916                "GitHub answered {alias} of a read by reference with an issue whose __typename \
12917                 is not Issue"
12918            ),
12919        });
12920    }
12921    Ok(Some(issue))
12922}
12923
12924/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
12925fn spoken_reason(reason: &str) -> String {
12926    reason.to_ascii_lowercase().replace('_', " ")
12927}
12928
12929impl GitHubProjectsSource {
12930    /// Every task and project of this board whose card contradicts its issue's closure, each
12931    /// judged against its own kind's half of `status_mapping`, read through the board's own
12932    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
12933    ///
12934    /// Each item the filter answers is confirmed from its own node: an open one, one whose
12935    /// closure reads `unknown`, and one whose card is already in line are left out whatever
12936    /// the filter let through. The board's fields ride along and are held for the option
12937    /// writes that follow, so those need no read of the board.
12938    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12939        let mut after: Option<String> = None;
12940        let mut found = Vec::new();
12941        loop {
12942            let data = self
12943                .graphql(
12944                    graphql::CLOSED_ITEMS,
12945                    json!({"owner":self.owner,"number":self.project_number,
12946                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
12947                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
12948                )
12949                .await?;
12950            let board = data
12951                .pointer("/closedItems/projectV2")
12952                .filter(|value| !value.is_null())
12953                .ok_or_else(|| SourceError::Refused {
12954                    message: format!(
12955                        "GitHub project {}/{} was not found or is not visible to the token",
12956                        self.owner, self.project_number
12957                    ),
12958                })?;
12959            *self.fields_cache()? = Some(BoardFields {
12960                id: BoardId::parse(required_str(board, "id")?)?,
12961                fields: board.get("fields").cloned().unwrap_or(Value::Null),
12962            });
12963            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
12964                message: "GitHub project closed items are missing".into(),
12965            })?;
12966            for item in items
12967                .get("nodes")
12968                .and_then(Value::as_array)
12969                .ok_or_else(|| SourceError::Malformed {
12970                    message: "GitHub project items.nodes is not an array".into(),
12971                })?
12972            {
12973                let Some(resolved) = self.resolve(item)? else {
12974                    continue;
12975                };
12976                let BoardKind::Work(kind) = resolved.kind else {
12977                    continue;
12978                };
12979                let Some((closed_as, name)) = self.statuses.contradicted(
12980                    kind,
12981                    resolved.option.as_deref(),
12982                    resolved.closed,
12983                    resolved.reason.as_deref(),
12984                ) else {
12985                    continue;
12986                };
12987                found.push(StatusContradiction {
12988                    closed_as,
12989                    to: name.to_owned(),
12990                    option: resolved.option.clone(),
12991                    item: match kind {
12992                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
12993                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
12994                    },
12995                });
12996            }
12997            let info = items
12998                .get("pageInfo")
12999                .ok_or_else(|| SourceError::Malformed {
13000                    message: "GitHub project items have no pageInfo".into(),
13001                })?;
13002            if !required_bool(info, "hasNextPage")? {
13003                return Ok(found);
13004            }
13005            let next = required_str(info, "endCursor")?;
13006            validate_cursor_progress(after.as_deref(), next)?;
13007            after = Some(next.to_owned());
13008        }
13009    }
13010
13011    /// What this board holds for each issue in `issues`, once each in the order given, read
13012    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
13013    ///
13014    /// An issue GitHub cannot find for this source's credential, and one with no item on this
13015    /// board, are both absent. A closure that already says the work will not be done here —
13016    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
13017    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
13018    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
13019    /// tasks as sub-issues — is held under exactly these rules, judged against the project
13020    /// kind's half of `status_mapping`.
13021    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13022        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
13023        for issue in issues {
13024            if !distinct.contains(issue) {
13025                distinct.push(issue.clone());
13026            }
13027        }
13028        let done = |kind: ItemKind| {
13029            self.statuses
13030                .target(kind, StatusCategory::Done)
13031                .option()
13032                .unwrap_or("Done")
13033                .to_owned()
13034        };
13035        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13036        // its range names no issue any repository can hold: it is answered absent and never sent,
13037        // where binding it would have GitHub refuse the whole batch.
13038        let readable: Vec<IssueRef> = distinct
13039            .iter()
13040            .filter(|issue| i32::try_from(issue.number).is_ok())
13041            .cloned()
13042            .collect();
13043        let mut holdings = Vec::with_capacity(readable.len());
13044        for batch in readable.chunks(REFERENCE_BATCH) {
13045            let data = self
13046                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13047                .await?;
13048            for (slot, issue) in batch.iter().enumerate() {
13049                let node = referenced_issue(&data, slot)?;
13050                let resolved = match node {
13051                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13052                    None => None,
13053                };
13054                // A task and a project are both held; a document is not work, so nothing is.
13055                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13056                    BoardKind::Work(kind) => Some((item, kind)),
13057                    BoardKind::Document => None,
13058                }) else {
13059                    holdings.push(IssueHolding {
13060                        issue: issue.clone(),
13061                        completion: Completion::Absent,
13062                    });
13063                    continue;
13064                };
13065                let task = match kind {
13066                    ItemKind::Task => {
13067                        let held = item.task()?;
13068                        HeldTask {
13069                            id: held.id,
13070                            kind,
13071                            option: item.option.clone(),
13072                            classification: held.classification,
13073                            repositories: held.repositories,
13074                            delivers: held.delivers,
13075                        }
13076                    }
13077                    // A project delivers no task, so what its write keeps in step is nothing.
13078                    ItemKind::Project => {
13079                        let held = item.project();
13080                        HeldTask {
13081                            id: held.id,
13082                            kind,
13083                            option: item.option.clone(),
13084                            classification: held.classification,
13085                            repositories: held.repositories,
13086                            delivers: Vec::new(),
13087                        }
13088                    }
13089                };
13090                let done = done(kind);
13091                let completion = match (item.closed, item.reason.as_deref()) {
13092                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13093                        task,
13094                        reason: format!("closed as {}", spoken_reason(reason)),
13095                    },
13096                    (true, None | Some("COMPLETED"))
13097                        if item
13098                            .option
13099                            .as_deref()
13100                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13101                    {
13102                        Completion::Unchanged { task }
13103                    }
13104                    _ => {
13105                        let kept = item.clone();
13106                        self.held_cache
13107                            .lock()
13108                            .map_err(|_| SourceError::Unavailable {
13109                                message: "items read by issue reference were left \
13110                                          inconsistent; run the command again"
13111                                    .into(),
13112                            })?
13113                            .insert(kept.id.clone(), kept);
13114                        Completion::Due {
13115                            task,
13116                            to: done.clone(),
13117                        }
13118                    }
13119                };
13120                holdings.push(IssueHolding {
13121                    issue: issue.clone(),
13122                    completion,
13123                });
13124            }
13125        }
13126        let mut read = holdings.into_iter();
13127        let mut answered = Vec::with_capacity(distinct.len());
13128        for issue in distinct {
13129            if i32::try_from(issue.number).is_ok() {
13130                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13131                    message: format!("no holding was read for {issue}"),
13132                })?);
13133            } else {
13134                answered.push(IssueHolding {
13135                    issue,
13136                    completion: Completion::Absent,
13137                });
13138            }
13139        }
13140        Ok(answered)
13141    }
13142}
13143
13144#[async_trait::async_trait]
13145impl TaskSource for GitHubProjectsSource {
13146    fn kind(&self) -> &'static str {
13147        KIND
13148    }
13149    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13150        change_token::order(a, b)
13151    }
13152    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13153    /// member, the change each part of the token moves for. Not its forward edges — see
13154    /// [`FAR_ENDS_UNCOVERED`].
13155    fn version_coverage(&self) -> VersionCoverage {
13156        VersionCoverage {
13157            forward_edges: Coverage::Uncovered {
13158                reason: FAR_ENDS_UNCOVERED.to_owned(),
13159            },
13160            ..VersionCoverage::whole()
13161        }
13162    }
13163    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13164    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13165    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13166    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13167    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13168    // llmlint: ignore[names_match_behavior] The manager's ruling (c-d7838f2c) keeps 0.6.0's GitHub skip and has this plugin state that bounded claim through the coverage it reports, with the bound documented here, in §4.13d and by the copy_skip journey; a separate bounded representation would be a contract change beyond a defaulted addition, which the ruling reserves for the contract's owner, and a follow-up is drafted for a far-end-aware skip.
13169    fn copy_version_coverage(&self) -> VersionCoverage {
13170        VersionCoverage::whole()
13171    }
13172    /// A project's fields, and not its forward edges, for the same reason a task's are not.
13173    fn project_version_coverage(&self) -> ProjectVersionCoverage {
13174        ProjectVersionCoverage {
13175            fields: Coverage::Covered,
13176            forward_edges: Coverage::Uncovered {
13177                reason: FAR_ENDS_UNCOVERED.to_owned(),
13178            },
13179        }
13180    }
13181    /// A document's fields and the manifest of its image assets, which its metadata slot
13182    /// records: both are the issue's body, whose every edit moves the token's issue time.
13183    fn document_version_coverage(&self) -> DocumentVersionCoverage {
13184        DocumentVersionCoverage::whole()
13185    }
13186    fn write_context(&self, id: &NativeId) -> Option<Value> {
13187        self.context_of(id)
13188    }
13189    fn board_context(&self) -> Option<Value> {
13190        self.board_context_held()
13191    }
13192    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13193        self.prime(id, context, board)
13194    }
13195    fn unprime(&self, id: &NativeId) {
13196        self.unprime_held(id);
13197    }
13198    fn begin_journal(&self) {
13199        self.expected.begin();
13200    }
13201    fn end_journal(&self) -> Option<WriteJournal> {
13202        Some(self.expected.end())
13203    }
13204    fn capabilities(&self) -> Capabilities {
13205        Capabilities {
13206            projects: Support::Native,
13207            documents: Support::Native,
13208            comments: Support::Native,
13209            assets: Support::Native,
13210            priority: if self.priorities.is_some() {
13211                Support::Native
13212            } else {
13213                Support::Unsupported
13214            },
13215            filter_by_priority: Support::Native,
13216            filter_by_comment_activity: Support::Native,
13217            filter_by_metadata: Support::Native,
13218            filter_by_origin: Support::Native,
13219            reconcile_status: Support::Native,
13220            complete_by_issue: Support::Native,
13221            orphan_tasks: Support::Native,
13222            filter_by_label: Support::Native,
13223            filter_by_status: Support::Native,
13224            search_title: Support::Native,
13225            search_content: Support::Native,
13226            task_dependencies: DependencySupport::BothDirections,
13227            project_dependencies: DependencySupport::BothDirections,
13228            max_page_size: MAX_PAGE_SIZE,
13229        }
13230    }
13231    async fn visibility(
13232        &self,
13233        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13234    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13235        self.write_visibility(target).await
13236    }
13237    async fn visibility_with_project(
13238        &self,
13239        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13240        project: &onetaskgraph_plugin_api::WriteTarget<'_>,
13241    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13242        self.write_visibility_with_project(target, project).await
13243    }
13244    fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
13245        self.declared_private.store(
13246            declared == onetaskgraph_plugin_api::Visibility::Private,
13247            std::sync::atomic::Ordering::SeqCst,
13248        );
13249    }
13250    fn scope_visibility<'a>(
13251        &'a self,
13252        operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
13253    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
13254        self.scope_visibility_reads(operation)
13255    }
13256
13257    /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13258    /// the create applies — reading the parent project's issue only when `target` names one.
13259    async fn creation_repository(
13260        &self,
13261        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13262    ) -> Result<Option<Repository>, SourceError> {
13263        let onetaskgraph_plugin_api::WriteTarget::New {
13264            repositories,
13265            project,
13266        } = target
13267        else {
13268            return Ok(None);
13269        };
13270        let target = self
13271            .creation_rule(repositories, *project, "a new item")
13272            .await?;
13273        Repository::try_from(target.origin())
13274            .map(Some)
13275            .map_err(|message| SourceError::Malformed { message })
13276    }
13277    async fn health(&self) -> Result<Health, SourceError> {
13278        let board = self.board_page(None, 1).await?;
13279        Ok(Health {
13280            reachable: true,
13281            detail: Some(format!(
13282                "reading GitHub project {}/{} ({})",
13283                self.owner,
13284                self.project_number,
13285                required_str(&board, "title")?
13286            )),
13287        })
13288    }
13289    /// The board's own node id, digested: the one request reading its fields, or none when
13290    /// this command has read them already.
13291    async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13292        let board = self.board_fields().await?;
13293        Designator::of_backend(KIND, board.id.as_str())
13294            .map(Some)
13295            .map_err(|message| SourceError::Malformed { message })
13296    }
13297    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13298        self.item_by_id(id)
13299            .await?
13300            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13301            .map(|item| item.task())
13302            .transpose()
13303    }
13304    async fn task_assets(
13305        &self,
13306        id: &NativeId,
13307    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13308        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13309    }
13310    async fn task_asset(
13311        &self,
13312        id: &NativeId,
13313        name: &onetaskgraph_plugin_api::AssetName,
13314    ) -> Result<Option<Vec<u8>>, SourceError> {
13315        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13316            .await
13317    }
13318    async fn set_task_rendering_with_assets(
13319        &self,
13320        id: &NativeId,
13321        content: &str,
13322        provenance: &Value,
13323        _answers: &BTreeMap<String, Value>,
13324        assets: &onetaskgraph_plugin_api::AssetWrite,
13325    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13326        self.replace_rendering(
13327            id,
13328            BoardKind::Work(ItemKind::Task),
13329            content,
13330            provenance,
13331            Some(assets),
13332        )
13333        .await
13334    }
13335    async fn document_assets(
13336        &self,
13337        id: &NativeId,
13338    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13339        self.held_assets(id, BoardKind::Document).await
13340    }
13341    async fn document_asset(
13342        &self,
13343        id: &NativeId,
13344        name: &onetaskgraph_plugin_api::AssetName,
13345    ) -> Result<Option<Vec<u8>>, SourceError> {
13346        self.held_asset(id, BoardKind::Document, name).await
13347    }
13348    async fn set_document_rendering_with_assets(
13349        &self,
13350        id: &NativeId,
13351        content: &str,
13352        provenance: &Value,
13353        _answers: &BTreeMap<String, Value>,
13354        assets: &onetaskgraph_plugin_api::AssetWrite,
13355    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13356        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13357            .await
13358    }
13359    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13360        Ok(self
13361            .item_by_id(id)
13362            .await?
13363            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13364            .map(|item| item.project()))
13365    }
13366    async fn query_tasks(
13367        &self,
13368        query: &TaskQuery,
13369        page: &PageRequest,
13370    ) -> Result<Page<Task>, SourceError> {
13371        validate_page(page)?;
13372        refuse_unsearchable(query)?;
13373        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13374        // category — `unknown`; every other category reads only off an open issue or a draft. So
13375        // a status filter naming none of those three asks every issue search it sends for open
13376        // issues alone; each candidate's category is still confirmed from its own item below.
13377        let open_only = !query.statuses.is_empty()
13378            && query.statuses.iter().all(|category| {
13379                !matches!(
13380                    category,
13381                    StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13382                )
13383            });
13384        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13385            let mut qualifiers: Vec<String> = [
13386                query.commented_since.map(updated_qualifier),
13387                narrowing_qualifiers(query),
13388            ]
13389            .into_iter()
13390            .flatten()
13391            .collect();
13392            // A narrowed search never answers a draft, which is no issue, so narrowing it to
13393            // open issues too leaves out nothing such a search could have answered.
13394            if !qualifiers.is_empty() {
13395                if open_only {
13396                    qualifiers.push(OPEN_QUALIFIER.to_owned());
13397                }
13398                return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13399            }
13400        }
13401
13402        // A read narrowed to one project asks that project for its own tasks, so nothing
13403        // about it costs what the rest of the board holds. A read carrying a text, metadata
13404        // or origin predicate asks GitHub the narrower question those predicates are, and a
13405        // read narrowed to comment activity alone asks the board's own issue search for the
13406        // issues updated since, which is every issue a comment could have been written or
13407        // edited on since. Every other task read is a question about the whole board and is
13408        // answered by reading it.
13409        let (held, membership) = match (&query.project, query.commented_since) {
13410            (ProjectFilter::Is(project), _) => (
13411                match self.held_task_children(project)? {
13412                    // Taken as `project_children` takes the project's own sub-issue read: with
13413                    // every item this process wrote under it since, and none it moved away.
13414                    Some(held) => self.completed_with_written(
13415                        held.into_iter()
13416                            .filter(|child| child.parent.as_ref() == Some(project))
13417                            .collect(),
13418                        |own| own.parent.as_ref() == Some(project),
13419                    )?,
13420                    None => self.project_children(project).await?,
13421                },
13422                // Answered by where these items came from; see `task_matches`.
13423                &ProjectFilter::Any,
13424            ),
13425            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13426                match (self.narrowed(query).await?, since) {
13427                    (Some(narrowed), _) => (narrowed, &query.project),
13428                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13429                    // The board's own items list every draft; only the search half, which
13430                    // completes them with issues they lag behind on, is narrowed — a closed
13431                    // issue that half leaves out is at no category the filter keeps.
13432                    (None, None) if open_only => (
13433                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13434                        &query.project,
13435                    ),
13436                    (None, None) => (self.board().await?.items, &query.project),
13437                }
13438            }
13439        };
13440        // Filtered before paged: a page of a filtered result is a page of the survivors,
13441        // never the survivors of a page.
13442        let mut tasks = Vec::new();
13443        for item in held
13444            .iter()
13445            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13446        {
13447            let task = item.task()?;
13448            if task_matches(&task, query, membership)
13449                && self.commented_since(item, query.commented_since).await?
13450            {
13451                tasks.push(task);
13452            }
13453        }
13454        Ok(offset_page(
13455            tasks,
13456            numeric_cursor(page.cursor.as_ref())?,
13457            page.limit.min(MAX_PAGE_SIZE) as usize,
13458        ))
13459    }
13460    async fn query_projects(
13461        &self,
13462        query: &ProjectQuery,
13463        page: &PageRequest,
13464    ) -> Result<Page<Project>, SourceError> {
13465        validate_page(page)?;
13466        refuse_unsearchable_text(query.text.as_ref())?;
13467        // The projects a board holds are found by an issue search scoped to that board,
13468        // never by walking the board's own item connection: what tells a project from a
13469        // task is the `parent` each issue carries, which costs nothing to read. A query
13470        // carrying a text asks that search for the text too, so it reads the issues that
13471        // hold it rather than every issue of the board.
13472        let held = match self.text_searched(query.text.as_ref()).await? {
13473            Some(searched) => searched,
13474            None => self.board_issues().await?,
13475        };
13476        let projects = held
13477            .iter()
13478            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13479            .map(Resolved::project)
13480            .filter(|project| project_matches(project, query))
13481            .collect();
13482        Ok(offset_page(
13483            projects,
13484            numeric_cursor(page.cursor.as_ref())?,
13485            page.limit.min(MAX_PAGE_SIZE) as usize,
13486        ))
13487    }
13488    async fn query_projects_by_origin(
13489        &self,
13490        origin: &str,
13491        page: &PageRequest,
13492    ) -> Result<Page<Project>, SourceError> {
13493        validate_page(page)?;
13494        let projects = self
13495            .copied_from(origin, BoardKind::Work(ItemKind::Project))
13496            .await?
13497            .iter()
13498            .map(Resolved::project)
13499            .collect();
13500        Ok(offset_page(
13501            projects,
13502            numeric_cursor(page.cursor.as_ref())?,
13503            page.limit.min(MAX_PAGE_SIZE) as usize,
13504        ))
13505    }
13506    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13507        let Some(item) = self
13508            .document_by_id(id)
13509            .await?
13510            .filter(|item| item.kind == BoardKind::Document)
13511        else {
13512            return Ok(None);
13513        };
13514        self.expected.read_document(&item);
13515        Ok(Some(item.document()))
13516    }
13517    async fn query_documents_by_origin(
13518        &self,
13519        origin: &str,
13520        page: &PageRequest,
13521    ) -> Result<Page<Document>, SourceError> {
13522        validate_page(page)?;
13523        let documents = self
13524            .copied_from(origin, BoardKind::Document)
13525            .await?
13526            .iter()
13527            .map(Resolved::document)
13528            .collect();
13529        Ok(offset_page(
13530            documents,
13531            numeric_cursor(page.cursor.as_ref())?,
13532            page.limit.min(MAX_PAGE_SIZE) as usize,
13533        ))
13534    }
13535    async fn query_documents(
13536        &self,
13537        query: &DocumentQuery,
13538        page: &PageRequest,
13539    ) -> Result<Page<Document>, SourceError> {
13540        validate_page(page)?;
13541        // Narrowed to one project, this is the same sub-issue read a task list scoped to
13542        // that project makes — a document filed under a project is a sub-issue of it too,
13543        // and which of them come back is the kind this caller asked for. Unscoped, a query
13544        // carrying a text asks the board-scoped issue search for it, as a task query does,
13545        // and only one carrying none reads the board.
13546        let (held, membership) = match &query.project {
13547            ProjectFilter::Is(project) => (
13548                self.project_children(project).await?,
13549                // Answered by where these items came from; see `task_matches`.
13550                &ProjectFilter::Any,
13551            ),
13552            ProjectFilter::Any | ProjectFilter::Orphans => {
13553                refuse_unsearchable_text(query.text.as_ref())?;
13554                match self.text_searched(query.text.as_ref()).await? {
13555                    Some(searched) => (searched, &query.project),
13556                    None => (self.board().await?.items, &query.project),
13557                }
13558            }
13559        };
13560        // Filtered before paged, exactly as a task read is: a page of a filtered result is
13561        // a page of the survivors, never the survivors of a page.
13562        let documents = held
13563            .iter()
13564            .filter(|item| item.kind == BoardKind::Document)
13565            .map(Resolved::document)
13566            .filter(|document| document_matches(document, query, membership))
13567            .collect();
13568        Ok(offset_page(
13569            documents,
13570            numeric_cursor(page.cursor.as_ref())?,
13571            page.limit.min(MAX_PAGE_SIZE) as usize,
13572        ))
13573    }
13574    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13575        validate_page(page)?;
13576        let offset = numeric_cursor(page.cursor.as_ref())?;
13577        let mut labels = self
13578            .board()
13579            .await?
13580            .items
13581            .into_iter()
13582            .flat_map(|item| item.labels)
13583            .fold(Vec::new(), |mut all, label| {
13584                if !all.iter().any(|x: &Label| x.id == label.id) {
13585                    all.push(label);
13586                }
13587                all
13588            });
13589        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13590        Ok(offset_page(
13591            labels,
13592            offset,
13593            page.limit.min(MAX_PAGE_SIZE) as usize,
13594        ))
13595    }
13596    async fn task_dependencies(
13597        &self,
13598        id: &NativeId,
13599        direction: Direction,
13600        page: &PageRequest,
13601    ) -> Result<Page<DependencyEdge>, SourceError> {
13602        self.dependencies(id, ItemKind::Task, direction, page).await
13603    }
13604    async fn project_dependencies(
13605        &self,
13606        id: &NativeId,
13607        direction: Direction,
13608        page: &PageRequest,
13609    ) -> Result<Page<DependencyEdge>, SourceError> {
13610        self.dependencies(id, ItemKind::Project, direction, page)
13611            .await
13612    }
13613
13614    fn writes(&self) -> WriteSupport {
13615        WriteSupport::Supported
13616    }
13617
13618    /// Create or update one task.
13619    ///
13620    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
13621    /// neither may name the task itself or name one task twice — and land in the body's
13622    /// metadata slot under their reserved keys, in place of any caller metadata of those
13623    /// names.
13624    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
13625        self.write_task_assets(write, None)
13626            .await
13627            .map(|written| written.id)
13628    }
13629
13630    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
13631        self.origins_ahead(origins).await
13632    }
13633
13634    async fn member_classifications(
13635        &self,
13636        project: &NativeId,
13637    ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
13638        Ok(self
13639            .member_classes
13640            .lock()
13641            .map_err(|_| SourceError::Unavailable {
13642                message: "a project's members held for its classification were left \
13643                          inconsistent; run the command again"
13644                    .into(),
13645            })?
13646            .remove(project))
13647    }
13648
13649    /// The issue's own web address, as the `createIssue` that made it in this command answered
13650    /// it: a task, a project and a document are each an issue here.
13651    fn written_location(
13652        &self,
13653        _record: onetaskgraph_plugin_api::MetadataRecord,
13654        id: &NativeId,
13655    ) -> Option<Location> {
13656        self.written
13657            .lock()
13658            .unwrap_or_else(std::sync::PoisonError::into_inner)
13659            .get(id)
13660            .cloned()
13661            .map(Location::Url)
13662    }
13663
13664    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
13665    /// in one request, and a copy's tasks are created together only then, since their fields
13666    /// and edges are what is held back.
13667    fn create_batch_limit(&self) -> usize {
13668        if self.deferring().unwrap_or(false) {
13669            MUTATION_BATCH
13670        } else {
13671            1
13672        }
13673    }
13674
13675    // llmlint: ignore[invalid_states_unrepresentable] The signature is the plugin contract's additive `TaskSource::write_tasks`, which the manager approved taking `ItemWrite<Task>` as `write_task` does; a create-only input type would be a new contract type. A write naming an existing target is refused below before anything is sent.
13676    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
13677        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
13678            return Err(SourceError::Refused {
13679                message: format!(
13680                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
13681                     next: update it with write_task",
13682                    write.item.id,
13683                    write.target.as_ref().map_or("", |target| target.0.as_str())
13684                ),
13685            });
13686        }
13687        if !self.deferring()? {
13688            let mut created = Vec::with_capacity(writes.len());
13689            for write in writes {
13690                match self.write_task(write).await {
13691                    Ok(id) => created.push(id),
13692                    Err(error) => {
13693                        let mut left: Vec<&str> = Vec::new();
13694                        for id in &created {
13695                            if self.delete_issue(id).await.is_err() {
13696                                left.push(id.0.as_str());
13697                            }
13698                        }
13699                        return Err(left_behind(error, &left));
13700                    }
13701                }
13702            }
13703            return Ok(created);
13704        }
13705        self.create_together(writes).await
13706    }
13707
13708    fn begin_deferred_writes(&self) {
13709        if let Ok(mut deferred) = self.deferred.lock() {
13710            deferred.get_or_insert_with(Deferred::default);
13711        }
13712    }
13713
13714    async fn finish_writes(&self) -> Result<(), SourceError> {
13715        self.send_held_updates().await?;
13716        self.send_held_back().await
13717    }
13718
13719    async fn write_task_with_assets(
13720        &self,
13721        write: &ItemWrite<Task>,
13722        _answers: Option<&BTreeMap<String, Value>>,
13723        assets: &onetaskgraph_plugin_api::AssetWrite,
13724    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13725        self.write_task_assets(write, Some(assets)).await
13726    }
13727
13728    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
13729        self.write_item(
13730            &Incoming {
13731                written: Written::Work(ItemKind::Project, &write.item.status),
13732                title: &write.item.title,
13733                content: write.item.content.as_deref(),
13734                assets: None,
13735                labels: &write.item.labels,
13736                metadata: &write.item.metadata,
13737                repositories: &write.item.repositories,
13738                classification: write.item.classification,
13739                parent: None,
13740                delivers: &[],
13741                delivered_by: &[],
13742                priority: None,
13743            },
13744            write.target.as_ref(),
13745            &write.depends_on,
13746        )
13747        .await
13748        .map(|written| written.id)
13749    }
13750
13751    /// Create or update one document, which is one issue titled the way this board spells
13752    /// a document.
13753    ///
13754    /// Everything else is exactly a task write: caller metadata goes to the same canonical
13755    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
13756    /// or a field this board cannot carry is refused by name rather than dropped, a target
13757    /// naming an issue this board does not hold is refused rather than created, and an
13758    /// issue this call created is taken back when the rest of the write fails.
13759    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
13760        self.write_document_assets(write, None)
13761            .await
13762            .map(|written| written.id)
13763    }
13764
13765    async fn write_document_with_assets(
13766        &self,
13767        write: &ItemWrite<Document>,
13768        _answers: Option<&BTreeMap<String, Value>>,
13769        assets: &onetaskgraph_plugin_api::AssetWrite,
13770    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13771        self.write_document_assets(write, Some(assets)).await
13772    }
13773
13774    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
13775    /// which reads nothing; then the board's `Status` option. Over an existing item that is
13776    /// read off the item, as the write reads it, and the item is held among this command's
13777    /// resolved records so the write that follows reuses that read rather than repeating it;
13778    /// an item that does not carry the field takes the board's fields, which are held once
13779    /// read. A create is checked against the board's fields only when this command already
13780    /// holds them, because a create reads them together with its repository, in one request,
13781    /// and refuses a missing option before it writes anything.
13782    async fn check_status_write(
13783        &self,
13784        kind: ItemKind,
13785        category: StatusCategory,
13786        target: Option<&NativeId>,
13787    ) -> Result<(), SourceError> {
13788        let status = self.resolved_target(kind, category)?;
13789        if status.option().is_none() {
13790            return Ok(());
13791        }
13792        let fields = match target {
13793            Some(target) => {
13794                // A target this board does not hold is the write's own refusal to make.
13795                let Some(item) = self.bound_item(target).await? else {
13796                    return Ok(());
13797                };
13798                self.resolved_cache()?.insert(target.clone(), item.clone());
13799                // Only the `Status` field decides whether the write can land.
13800                self.fields_for(Some(&item), false, true, false)
13801                    .await?
13802                    .fields
13803            }
13804            None => {
13805                let held = self
13806                    .board_cache()?
13807                    .as_ref()
13808                    .map(|board| board.fields.clone());
13809                match held.or_else(|| {
13810                    self.fields_cache()
13811                        .ok()
13812                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
13813                }) {
13814                    Some(fields) => fields,
13815                    None => return Ok(()),
13816                }
13817            }
13818        };
13819        self.column_for(&fields, kind, category, &status)
13820            .map(|_| ())
13821    }
13822
13823    /// What a write of `status` reads back as: the `Status` option its category maps to for
13824    /// `kind`, and for a closing category the closure that option and its fixed reason read
13825    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
13826    /// this instance has no option for is answered as given, since writing it is refused.
13827    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
13828        match self.statuses.target(kind, status.category) {
13829            StatusTarget::Column(name) => {
13830                self.statuses.status(kind, Some(name.as_str()), false, None)
13831            }
13832            StatusTarget::Terminal(name, reason) => {
13833                self.statuses
13834                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
13835            }
13836            StatusTarget::Disabled(_) => status.clone(),
13837        }
13838    }
13839
13840    /// Set one task's status alone.
13841    ///
13842    /// An open target reopens a closed issue with an `updateIssue` carrying only its
13843    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
13844    /// terminal target selects its mapped option, then closes with its fixed reason. No
13845    /// request carries a title, a body or a label. The status
13846    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
13847    /// what a re-read reports.
13848    async fn set_task_status(
13849        &self,
13850        id: &NativeId,
13851        category: StatusCategory,
13852    ) -> Result<Option<Status>, SourceError> {
13853        self.set_status(id, ItemKind::Task, category).await
13854    }
13855
13856    /// Set one task's status and answer the task with it, reading the item once: the read the
13857    /// task is answered from is handed to the status write, which would otherwise read the
13858    /// same issue again — two requests on a fresh process, the read and the field write,
13859    /// where the default's `get_task` before `set_task_status` sent three.
13860    async fn set_task_status_reading(
13861        &self,
13862        id: &NativeId,
13863        category: StatusCategory,
13864    ) -> Result<Option<Task>, SourceError> {
13865        // Refused before anything is read, in the words the write itself refuses it.
13866        self.resolved_target(ItemKind::Task, category)?;
13867        let Some(item) = self
13868            .bound_item(id)
13869            .await?
13870            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13871        else {
13872            return Ok(None);
13873        };
13874        let mut task = item.task()?;
13875        self.held_cache
13876            .lock()
13877            .map_err(|_| SourceError::Unavailable {
13878                message: "items read by issue reference were left inconsistent; run the \
13879                          command again"
13880                    .into(),
13881            })?
13882            .insert(id.clone(), item);
13883        let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
13884            return Ok(None);
13885        };
13886        task.status = status;
13887        Ok(Some(task))
13888    }
13889
13890    /// Set one project's status alone, by exactly the paired write a task's status takes, for
13891    /// the project kind: a terminal category selects the project kind's mapped option and then
13892    /// closes the issue with the category's fixed reason — an issue already closed with a
13893    /// reason that reads that category is left as it is — and an open category reopens a closed
13894    /// issue and then selects its option. A board draft is refused a terminal category as a
13895    /// task's is.
13896    async fn set_project_status(
13897        &self,
13898        id: &NativeId,
13899        category: StatusCategory,
13900    ) -> Result<Option<Status>, SourceError> {
13901        self.set_status(id, ItemKind::Project, category).await
13902    }
13903
13904    /// Every task and project whose card contradicts its issue's closure, read through the
13905    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
13906    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13907        self.contradictions().await
13908    }
13909
13910    /// What this board holds for each issue reference, one request per fifty of them; see
13911    /// [`TaskSource::issue_holdings`].
13912    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13913        self.holdings(issues).await
13914    }
13915
13916    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
13917    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
13918    /// for `none`. Refused by an instance with no `priority_mapping`.
13919    async fn set_task_priority(
13920        &self,
13921        id: &NativeId,
13922        priority: Priority,
13923    ) -> Result<Option<Priority>, SourceError> {
13924        self.set_priority(id, priority).await
13925    }
13926
13927    /// Replace one task's content with a single body update that keeps the metadata slot
13928    /// byte for byte.
13929    async fn set_task_content(
13930        &self,
13931        id: &NativeId,
13932        content: &str,
13933    ) -> Result<Option<()>, SourceError> {
13934        self.replace_content(id, content).await
13935    }
13936
13937    /// Replace one task issue's content and its provenance slot entry with a single body
13938    /// update. The answers are not kept: see `replace_rendering`.
13939    async fn set_task_rendering(
13940        &self,
13941        id: &NativeId,
13942        content: &str,
13943        provenance: &Value,
13944        _answers: &BTreeMap<String, Value>,
13945    ) -> Result<Option<()>, SourceError> {
13946        self.replace_rendering(
13947            id,
13948            BoardKind::Work(ItemKind::Task),
13949            content,
13950            provenance,
13951            None,
13952        )
13953        .await
13954        .map(|written| written.map(|_| ()))
13955    }
13956
13957    /// Replace one design-document issue's content and its provenance slot entry, on exactly
13958    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13959    async fn set_document_rendering(
13960        &self,
13961        id: &NativeId,
13962        content: &str,
13963        provenance: &Value,
13964        _answers: &BTreeMap<String, Value>,
13965    ) -> Result<Option<()>, SourceError> {
13966        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
13967            .await
13968            .map(|written| written.map(|_| ()))
13969    }
13970
13971    /// Replace one project issue's content and its provenance slot entry, on exactly the
13972    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13973    async fn set_project_rendering(
13974        &self,
13975        id: &NativeId,
13976        content: &str,
13977        provenance: &Value,
13978        _answers: &BTreeMap<String, Value>,
13979    ) -> Result<Option<()>, SourceError> {
13980        self.replace_rendering(
13981            id,
13982            BoardKind::Work(ItemKind::Project),
13983            content,
13984            provenance,
13985            None,
13986        )
13987        .await
13988        .map(|written| written.map(|_| ()))
13989    }
13990
13991    /// Apply a targeted update with one read of the item and a write only for what differs:
13992    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
13993    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
13994    async fn update_task(
13995        &self,
13996        id: &NativeId,
13997        update: &TaskUpdate,
13998    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
13999        self.targeted_update(id, update).await
14000    }
14001
14002    /// Replace one task's `delivered_by` with a single body update that changes the
14003    /// metadata slot and nothing outside it.
14004    async fn set_delivered_by(
14005        &self,
14006        id: &NativeId,
14007        delivered_by: &[TaskRef],
14008    ) -> Result<Option<()>, SourceError> {
14009        self.replace_delivered_by(id, delivered_by).await
14010    }
14011
14012    /// Set one key of one task issue's metadata with a single body update that changes the
14013    /// metadata slot and nothing outside it — no title, label, state or board field request —
14014    /// and sends nothing when the task already holds that value under the key.
14015    async fn set_task_metadata(
14016        &self,
14017        id: &NativeId,
14018        key: &MetadataKey,
14019        value: &Value,
14020    ) -> Result<Option<Task>, SourceError> {
14021        Ok(self
14022            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
14023            .await?
14024            .map(|item| item.task())
14025            .transpose()?)
14026    }
14027
14028    /// Set one key of one project issue's metadata, on exactly the terms of
14029    /// [`set_task_metadata`](TaskSource::set_task_metadata).
14030    async fn set_project_metadata(
14031        &self,
14032        id: &NativeId,
14033        key: &MetadataKey,
14034        value: &Value,
14035    ) -> Result<Option<Project>, SourceError> {
14036        Ok(self
14037            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
14038            .await?
14039            .map(|item| item.project()))
14040    }
14041
14042    /// Set one key of one design-document issue's metadata, on exactly the terms of
14043    /// [`set_task_metadata`](TaskSource::set_task_metadata).
14044    async fn set_document_metadata(
14045        &self,
14046        id: &NativeId,
14047        key: &MetadataKey,
14048        value: &Value,
14049    ) -> Result<Option<Document>, SourceError> {
14050        Ok(self
14051            .set_slot_key(id, BoardKind::Document, key, value)
14052            .await?
14053            .map(|item| item.document()))
14054    }
14055
14056    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14057        self.delete_item(id).await
14058    }
14059
14060    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14061        self.delete_item(id).await
14062    }
14063
14064    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14065        self.delete_item(id).await
14066    }
14067
14068    /// One page of the task issue's own comments, walked by GitHub's own cursor.
14069    ///
14070    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14071    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14072    ///
14073    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14074    /// board is the read of its comments. A draft this process already resolved is refused
14075    /// without one.
14076    async fn task_comments(
14077        &self,
14078        task: &NativeId,
14079        page: &PageRequest,
14080    ) -> Result<Option<Page<Comment>>, SourceError> {
14081        validate_page(page)?;
14082        let cached = self.resolved_cache()?.get(task).cloned();
14083        if let Some(item) = cached {
14084            if item.kind != BoardKind::Work(ItemKind::Task) {
14085                return Ok(None);
14086            }
14087            if item.content_kind == ContentKind::DraftIssue {
14088                return Err(self.draft_has_no_comments(task));
14089            }
14090        }
14091        match self.issue_detail(task, page).await? {
14092            Some(TaskDetailRead {
14093                comments: Some(comments),
14094                ..
14095            }) => comments,
14096            _ => Ok(None),
14097        }
14098    }
14099
14100    /// Every id's task, with the first page of its comments when `comments` names it:
14101    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14102    /// comments in one [`graphql::ISSUE_DETAIL`] request.
14103    async fn get_task_details(
14104        &self,
14105        ids: &[NativeId],
14106        comments: Option<&PageRequest>,
14107    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14108        if let Some(page) = comments
14109            && let Err(error) = validate_page(page)
14110        {
14111            return ids.iter().map(|_| Err(error.clone())).collect();
14112        }
14113        match (ids, comments) {
14114            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14115            ([id], None) => vec![self.task_read(id).await],
14116            _ => self.issue_details(ids, comments).await,
14117        }
14118    }
14119
14120    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14121    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14122    /// request, so the dependency read a copy makes of each next is answered from this one.
14123    async fn get_task_details_for_copy(
14124        &self,
14125        ids: &[NativeId],
14126        comments: &PageRequest,
14127    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14128        // A page past the first is a page of one task's comments, which the detail read of it
14129        // alone answers.
14130        if comments.cursor.is_some() {
14131            return self.get_task_details(ids, Some(comments)).await;
14132        }
14133        if let Err(error) = validate_page(comments) {
14134            return ids.iter().map(|_| Err(error.clone())).collect();
14135        }
14136        let mut read = Vec::with_capacity(ids.len());
14137        for batch in ids.chunks(COPY_DETAIL_BATCH) {
14138            match self
14139                .graphql(
14140                    graphql::ISSUE_COPY_DETAILS,
14141                    copy_detail_batch(batch, comments, false),
14142                )
14143                .await
14144            {
14145                Ok(data) => {
14146                    for (slot, id) in batch.iter().enumerate() {
14147                        let read_one = match data.get(format!("i{slot}")) {
14148                            Some(node) => self.detail_of(id, node, true, None).await,
14149                            None => Err(SourceError::Malformed {
14150                                message: format!(
14151                                    "GitHub answered a batch read with no item for {}",
14152                                    id.0
14153                                ),
14154                            }),
14155                        };
14156                        read.push(read_one);
14157                    }
14158                }
14159                Err(error) if unresolvable_node(&error) => {
14160                    for id in batch {
14161                        read.push(self.issue_detail(id, comments).await);
14162                    }
14163                }
14164                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14165            }
14166        }
14167        read
14168    }
14169
14170    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14171    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14172    /// and held for the next read of each by id; see `read_ahead`.
14173    ///
14174    /// What this run wrote is never read: its own record answers, as it does for a read by id.
14175    /// Nor is an item a listing of its project carried whole earlier in this command — see
14176    /// `carried` — which is held in place of reading it.
14177    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14178    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14179    /// answer naming another issue than the one asked for, are left to their own reads too.
14180    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14181        // The origins asked just before go out now, beside the members of the one project this
14182        // names when it names one; a project those origins answered whole is held for its next
14183        // read by id rather than read again here.
14184        let hinted = match ids {
14185            [one] => Some(one),
14186            _ => None,
14187        };
14188        self.send_pending_origins(hinted).await?;
14189        if let Some(project) = hinted {
14190            let answered = self.resolved_cache()?.get(project).cloned();
14191            if let Some(answered) = answered
14192                && answered.kind == BoardKind::Work(ItemKind::Project)
14193            {
14194                self.read_ahead()?.insert(project.clone(), Some(answered));
14195                return Ok(());
14196            }
14197        }
14198        let mut unread: Vec<NativeId> = Vec::new();
14199        // Read whole by a listing of its project in this command, which a mutation naming it
14200        // would have dropped: held as a batch would have held it, once every batch is answered.
14201        let mut listed: Vec<NativeId> = Vec::new();
14202        {
14203            let created = self.created()?;
14204            let held = self.read_ahead()?;
14205            let carried = self.carried()?;
14206            for id in ids {
14207                if unread.contains(id)
14208                    || listed.contains(id)
14209                    || held.contains_key(id)
14210                    || created.iter().any(|own| own.id == *id)
14211                {
14212                    continue;
14213                }
14214                // An issue a listing carried without the whole of what blocks it is read in a
14215                // batch too, which carries that, rather than having its dependency read sent
14216                // on its own once the copy reconciles its edges.
14217                let whole = carried.get(id).is_some_and(|item| {
14218                    item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14219                });
14220                if whole {
14221                    listed.push(id.clone());
14222                } else {
14223                    unread.push(id.clone());
14224                }
14225            }
14226        }
14227        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14228        // what the item itself costs.
14229        let comments = PageRequest {
14230            cursor: None,
14231            limit: 1,
14232        };
14233        // Held only once every batch has been answered, so a read-ahead that fails part way
14234        // holds nothing — what a listing carried included, which stays carried — and every
14235        // item is read on its own, as the method promises.
14236        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14237        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14238        // answered them: what a project copy's orphan check asks, when that first id is its
14239        // project.
14240        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14241        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14242            let data = match self
14243                .graphql(
14244                    graphql::ISSUE_COPY_DETAILS,
14245                    copy_detail_batch(batch, &comments, index == 0),
14246                )
14247                .await
14248            {
14249                Ok(data) => data,
14250                Err(error) if unresolvable_node(&error) => continue,
14251                Err(error) => return Err(error),
14252            };
14253            for (slot, id) in batch.iter().enumerate() {
14254                let Some(node) = data.get(format!("i{slot}")) else {
14255                    continue;
14256                };
14257                let held = if node.is_null() {
14258                    None
14259                } else {
14260                    match optional_str(node, "__typename")? {
14261                        Some("DraftIssue") => continue,
14262                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
14263                        _ => {}
14264                    }
14265                    // Remembered below with the rest, so a later batch refused leaves no
14266                    // dependency read answered from this one either.
14267                    self.resolve_issue_as(node, false).await?
14268                };
14269                read.push((id.clone(), held));
14270            }
14271            if index == 0
14272                && let Some(node) = data.get("children").filter(|node| !node.is_null())
14273                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14274                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14275            {
14276                // A list GitHub did not answer is not an empty one: it would answer the project's
14277                // task listing with nothing, and the orphan check with no orphans.
14278                let listed = node
14279                    .pointer("/subIssues/nodes")
14280                    .and_then(Value::as_array)
14281                    .ok_or_else(|| SourceError::Malformed {
14282                        message: format!(
14283                            "GitHub answered the sub-issues of project {} with no list of them",
14284                            batch[0].0
14285                        ),
14286                    })?
14287                    .iter()
14288                    .map(|child| {
14289                        Ok((
14290                            required_str(child, "id")?.to_owned(),
14291                            required_str(child, "title")?.to_owned(),
14292                        ))
14293                    })
14294                    .collect::<Result<Vec<_>, SourceError>>()?;
14295                children = Some((batch[0].clone(), listed));
14296            }
14297        }
14298        // The project's tasks are known from this read alone when every child it named is held
14299        // from it, but for a document, which its title says is one and which a task listing
14300        // never answers: then a listing of the project's tasks is answered from them.
14301        if let Some((project, listed)) = children
14302            && read.iter().any(|(id, held)| {
14303                *id == project
14304                    && held
14305                        .as_ref()
14306                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14307            })
14308        {
14309            let mut tasks = Vec::new();
14310            let mut whole = true;
14311            for (child, title) in &listed {
14312                match read.iter().find(|(id, _)| id.0 == *child) {
14313                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14314                        if held.kind == BoardKind::Work(ItemKind::Task) {
14315                            tasks.push(held.clone());
14316                        }
14317                    }
14318                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14319                    _ => whole = false,
14320                }
14321            }
14322            if whole {
14323                self.task_children()?.insert(project, tasks);
14324            }
14325        }
14326        let mut resolved = self.resolved_cache()?;
14327        let mut ahead = self.read_ahead()?;
14328        let mut carried = self.carried()?;
14329        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14330            resolved.insert(item.id.clone(), item.clone());
14331        }
14332        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14333        // and its next read is then its own.
14334        for id in &listed {
14335            let Some(item) = carried.remove(id) else {
14336                continue;
14337            };
14338            // The whole of what it is blocked by came with it, so the dependency read a copy
14339            // makes next is answered from this one, as a copy's detail read is.
14340            if item.blocked_by.is_some() {
14341                resolved.insert(item.id.clone(), item.clone());
14342            }
14343            read.push((id.clone(), Some(item)));
14344        }
14345        for (id, held) in read {
14346            ahead.insert(id, held);
14347        }
14348        Ok(())
14349    }
14350
14351    /// Add one comment to the task's issue, as the account the token belongs to.
14352    ///
14353    /// The author is refused before anything is sent — not even the task is read — because
14354    /// no answer GitHub could give would make posting under another name than the one asked
14355    /// for the right outcome.
14356    async fn add_comment(
14357        &self,
14358        task: &NativeId,
14359        comment: &NewComment,
14360    ) -> Result<Option<Comment>, SourceError> {
14361        if let Some(author) = &comment.author {
14362            return Err(SourceError::Refused {
14363                message: format!(
14364                    "source {} cannot post a comment as {author:?}: GitHub records the account \
14365                     the token signs in as the author of every comment; next: leave --author \
14366                     out, and the comment is posted as that account",
14367                    self.name
14368                ),
14369            });
14370        }
14371        let Some(issue) = self.commented_issue(task).await? else {
14372            return Ok(None);
14373        };
14374        let data = self
14375            .graphql(
14376                graphql::ADD_COMMENT,
14377                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14378            )
14379            .await?;
14380        let subject = data
14381            .pointer("/addComment/subject")
14382            .filter(|value| !value.is_null())
14383            .ok_or_else(|| SourceError::Malformed {
14384                message: "GitHub comment addition returned no subject".into(),
14385            })?;
14386        if required_str(subject, "id")? != issue.0 {
14387            return Err(SourceError::Malformed {
14388                message: "GitHub comment addition answered about another issue".into(),
14389            });
14390        }
14391        let added = data
14392            .pointer("/addComment/commentEdge/node")
14393            .filter(|value| !value.is_null())
14394            .ok_or_else(|| SourceError::Malformed {
14395                message: "GitHub comment addition returned no comment".into(),
14396            })?;
14397        let added = comment_from(added)?;
14398        self.remember_commented(&issue)?;
14399        Ok(Some(added))
14400    }
14401
14402    async fn edit_comment(
14403        &self,
14404        task: &NativeId,
14405        comment: &NativeId,
14406        body: &CommentBody,
14407    ) -> Result<Option<Comment>, SourceError> {
14408        let Some(issue) = self.commented_issue(task).await? else {
14409            return Ok(None);
14410        };
14411        let Some(held) = self.comment_on(&issue, comment).await? else {
14412            return Ok(None);
14413        };
14414        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14415        let body = CommentOrigin::kept(&held, body)?;
14416        let data = self
14417            .graphql(
14418                graphql::UPDATE_COMMENT,
14419                json!({"input":{"id":comment.0,"body":body.as_str()}}),
14420            )
14421            .await?;
14422        let edited = data
14423            .pointer("/updateIssueComment/issueComment")
14424            .filter(|value| !value.is_null())
14425            .ok_or_else(|| SourceError::Malformed {
14426                message: "GitHub comment update returned no comment".into(),
14427            })?;
14428        let edited = comment_from(edited)?;
14429        if edited.id != *comment {
14430            return Err(SourceError::Malformed {
14431                message: "GitHub comment update returned the wrong comment".into(),
14432            });
14433        }
14434        self.remember_commented(&issue)?;
14435        Ok(Some(edited))
14436    }
14437
14438    async fn delete_comment(
14439        &self,
14440        task: &NativeId,
14441        comment: &NativeId,
14442    ) -> Result<Option<NativeId>, SourceError> {
14443        let Some(issue) = self.commented_issue(task).await? else {
14444            return Ok(None);
14445        };
14446        if !self.comment_is_on(&issue, comment).await? {
14447            return Ok(None);
14448        }
14449        let data = self
14450            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14451            .await?;
14452        // The payload says nothing about the comment it removed, so what is checked is that
14453        // GitHub answered the mutation at all rather than leaving it unanswered.
14454        data.get("deleteIssueComment")
14455            .filter(|value| !value.is_null())
14456            .ok_or_else(|| SourceError::Malformed {
14457                message: "GitHub comment deletion returned no payload".into(),
14458            })?;
14459        Ok(Some(comment.clone()))
14460    }
14461
14462    /// Every request this source has recorded, and what each of GitHub's two budgets was
14463    /// attributed — read off the same accounting the session report is rendered from, so
14464    /// the two cannot count one request two ways.
14465    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14466        Ok(Some(self.ledger.snapshot().metering()))
14467    }
14468
14469    /// Drop every item, search answer and board read this source holds, so the next command
14470    /// reads the board as a person has since left it.
14471    ///
14472    /// Every one of those is held on the assumption that nothing but this source writes the
14473    /// board while a command runs, which stops being true the moment the command is over: a
14474    /// body a person edited would be overwritten from the record held here, and a card they
14475    /// moved would be read as still where this source left it. The board's own field
14476    /// definitions go too, because a person can add or delete a `Status` option and a write
14477    /// resolved against the held list would not re-read on a miss. What stays is what stays
14478    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14479    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14480    /// accounting [`metering`](TaskSource::metering) answers from.
14481    ///
14482    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14483    /// refused, because clearing it is what puts it right.
14484    async fn end_command(&self) -> Result<(), SourceError> {
14485        fn clear<T: Default>(held: &Mutex<T>) {
14486            *held
14487                .lock()
14488                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14489            held.clear_poison();
14490        }
14491        clear(&self.created);
14492        clear(&self.updated);
14493        clear(&self.commented);
14494        clear(&self.board_cache);
14495        clear(&self.search_cache);
14496        clear(&self.narrowed_cache);
14497        clear(&self.search_next);
14498        clear(&self.resolved_cache);
14499        clear(&self.read_ahead);
14500        clear(&self.pending_origins);
14501        clear(&self.member_classes);
14502        clear(&self.carried);
14503        clear(&self.held_cache);
14504        clear(&self.children_cache);
14505        clear(&self.task_children);
14506        clear(&self.written);
14507        clear(&self.fields_cache);
14508        self.expected.end_command();
14509        Ok(())
14510    }
14511}
14512
14513/// One member of a project as a classification-only read answers it: its classification and
14514/// the repositories its work changes.
14515type MemberClass = (Classification, Vec<Repository>);
14516
14517/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14518/// for under, beside the project that selector named; see `children_cache`.
14519type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14520
14521/// One issue comment as the contract carries it.
14522///
14523/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14524/// and when it answers an actor with no login, because either way the source did not say who
14525/// wrote it — which is what an absent author means, rather than an author called nothing.
14526///
14527/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14528/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14529/// and is read back with the marker taken out and reported as its origin.
14530fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14531    Ok(Comment {
14532        origin: None,
14533        id: NativeId(required_str(value, "id")?.to_owned()),
14534        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14535            .map(str::to_owned),
14536        created_at: optional_time(value, "createdAt")?,
14537        updated_at: optional_time(value, "updatedAt")?,
14538        body: required_str(value, "body")?.to_owned(),
14539        url: optional_str(value, "url")?.map(str::to_owned),
14540    }
14541    .unmarked())
14542}
14543
14544/// The page of comments one issue node carries, resumed from `after`.
14545fn comment_page(
14546    node: &Value,
14547    issue: &str,
14548    after: Option<&str>,
14549) -> Result<Page<Comment>, SourceError> {
14550    let connection = node
14551        .get("comments")
14552        .filter(|value| !value.is_null())
14553        .ok_or_else(|| SourceError::Malformed {
14554            message: format!("GitHub issue {issue} answered with no comments connection"),
14555        })?;
14556    let items = optional_nodes(Some(connection), "issue comments")?
14557        .into_iter()
14558        .flatten()
14559        .map(comment_from)
14560        .collect::<Result<Vec<_>, _>>()?;
14561    let next = next_cursor(connection)?;
14562    if let Some(next) = &next {
14563        validate_cursor_progress(after, &next.0)?;
14564    }
14565    Ok(Page { items, next })
14566}
14567
14568/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14569/// end — `None` when it carried none, or a page with more past it.
14570fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14571    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14572        return Ok(None);
14573    };
14574    if next_cursor(connection)?.is_some() {
14575        return Ok(None);
14576    }
14577    Ok(Some(
14578        optional_nodes(Some(connection), "blocked-by issues")?
14579            .into_iter()
14580            .flatten()
14581            .cloned()
14582            .collect(),
14583    ))
14584}
14585
14586/// Where the recorded tail of a dependency walk resumes; see
14587/// [`GitHubProjectsSource::recorded_edges`].
14588const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
14589
14590/// The board text field this source keeps a copy's origin in.
14591///
14592/// Named after the key it holds, and held to that name by the guard below rather than by
14593/// a reader noticing.
14594const ORIGIN_FIELD: &str = "onetaskgraph.origin";
14595
14596/// The metadata key that field holds.
14597///
14598/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
14599/// constructs or interprets the qualified id it carries. This source names it only to
14600/// route it — a short, typed value belongs in a typed field rather than in the body slot
14601/// a caller's own prose shares.
14602///
14603/// Restated rather than imported, because no plugin crate may depend on the engine. What
14604/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
14605/// target in `check`: it reads the engine's own literal and fails naming the file and the
14606/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
14607/// that creates a second item every run instead of finding the one it wrote — and that is
14608/// too late to learn it.
14609const ORIGIN_KEY: &str = "onetaskgraph.origin";
14610
14611/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
14612///
14613/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
14614/// is derived from the far end, never written down on the near item — so only a forward
14615/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
14616/// it did not come from, and it is told so rather than answered with an empty page that
14617/// reads as a walk which ended.
14618fn recorded_offset(
14619    cursor: Option<&str>,
14620    direction: Direction,
14621) -> Result<Option<usize>, SourceError> {
14622    cursor
14623        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
14624        .map(|offset| {
14625            if direction != Direction::DependsOn {
14626                return Err(SourceError::Config {
14627                    message: format!(
14628                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
14629                         reverse dependency read never issues; resume it in the direction \
14630                         that reported it"
14631                    ),
14632                });
14633            }
14634            offset.parse().map_err(|_| SourceError::Config {
14635                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
14636            })
14637        })
14638        .transpose()
14639}
14640
14641/// What a dependency read's own answer says of a board draft: `None` when the item is not a
14642/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
14643/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
14644/// draft's metadata, for one it does — its board item naming this board by its number and by
14645/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
14646fn draft_slot(
14647    data: &Value,
14648    id: &NativeId,
14649    project_number: u32,
14650) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
14651    let Some(draft) = data
14652        .get("node")
14653        .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
14654    else {
14655        return Ok(None);
14656    };
14657    let Some(items) = data.pointer("/draft/projectV2Items") else {
14658        return Ok(None);
14659    };
14660    if required_str(draft, "id")? != id.0 {
14661        return Err(SourceError::Malformed {
14662            message: format!("GitHub answered a different draft for {}", id.0),
14663        });
14664    }
14665    let nodes = items
14666        .get("nodes")
14667        .and_then(Value::as_array)
14668        .ok_or_else(|| SourceError::Malformed {
14669            message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
14670        })?;
14671    let info = items
14672        .get("pageInfo")
14673        .ok_or_else(|| SourceError::Malformed {
14674            message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
14675        })?;
14676    if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
14677        return Err(SourceError::Malformed {
14678            message: format!(
14679                "GitHub draft {} reports more board items than the one GitHub links a draft to",
14680                id.0
14681            ),
14682        });
14683    }
14684    let board = data
14685        .pointer("/board/projectV2/id")
14686        .and_then(Value::as_str)
14687        .ok_or_else(|| SourceError::Malformed {
14688            message: format!(
14689                "GitHub answered draft {} without this board's own id beside it",
14690                id.0
14691            ),
14692        })?;
14693    let mut held = false;
14694    for node in nodes {
14695        let project = node.get("project").ok_or_else(|| SourceError::Malformed {
14696            message: format!("GitHub draft {} board item has no project", id.0),
14697        })?;
14698        let number = project
14699            .get("number")
14700            .and_then(Value::as_u64)
14701            .ok_or_else(|| SourceError::Malformed {
14702                message: format!(
14703                    "GitHub draft {} board item has no numeric project number",
14704                    id.0
14705                ),
14706            })?;
14707        let project_id = required_str(project, "id")?;
14708        held |= number == u64::from(project_number) && project_id == board;
14709    }
14710    if !held {
14711        return Ok(Some(None));
14712    }
14713    Ok(Some(Some(
14714        metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
14715    )))
14716}
14717
14718fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
14719    let mut page = offset_page(edges, offset, limit.max(1));
14720    page.next = page
14721        .next
14722        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
14723    page
14724}
14725
14726/// The kind of one issue reached through a dependency connection.
14727///
14728/// The same questions the board scan asks, over the fields the dependency document
14729/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
14730/// then anything with sub-issues or the marker is a project.
14731///
14732/// # Errors
14733///
14734/// A far end this board holds as a document is refused rather than reported. The two
14735/// answers that are not refusals would both be wrong: reporting it as a task names an id
14736/// no task read of this source can find, and reporting it as a project names one no
14737/// project read can. There is no third value to return — `ItemKind` has no document
14738/// variant, because nothing may point at a document — so the relationship itself is what
14739/// the person is told about.
14740fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
14741    let id = required_str(value, "id")?;
14742    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
14743        return Err(SourceError::Refused {
14744            message: format!(
14745                "GitHub issue {id} is a document of this board — its title begins \
14746                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
14747                 on by one; next: remove that issue's blocking relationship on this board"
14748            ),
14749        });
14750    }
14751    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
14752    if parent.is_some() {
14753        return Ok(ItemKind::Task);
14754    }
14755    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
14756    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
14757        message: format!("GitHub issue {id}: {message}"),
14758    })?;
14759    let sub_issues = sub_issue_total(value)?;
14760    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
14761        ItemKind::Project
14762    } else {
14763        ItemKind::Task
14764    })
14765}
14766
14767/// The `stateInput` that closes an issue for `reason`.
14768fn closed_input(reason: ClosedState) -> Value {
14769    json!({"value":"CLOSED","stateReason":reason.reason()})
14770}
14771
14772/// The `IssueStateUpdateInput` one status target asks for.
14773///
14774/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
14775/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
14776/// a currently-closed issue: without that the item would read back `Unknown` and a copy
14777/// would report a change forever. A document has no status at all, and asks for neither.
14778fn state_input(target: Option<&StatusTarget>) -> Value {
14779    match target {
14780        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
14781        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
14782        // A document has no status, so a write of one says nothing about the issue's open
14783        // or closed state rather than forcing it open: `stateInput` is what carries that
14784        // instruction, and an explicit null asks for no change to it.
14785        None => Value::Null,
14786    }
14787}
14788
14789/// The metadata one write stores in the item's body slot.
14790///
14791/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
14792/// rather than carried: the kind marker so an empty project stays readable, the
14793/// repository list only when it is not exactly the issue's own repository, and the far
14794/// ends no relationship here can name.
14795///
14796/// The copy origin is the one typed field that is also mirrored here, and only as a
14797/// mirror: it lands in the board's origin field as well, which stays the one every reader
14798/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
14799/// and catches up with a write in seconds rather than minutes — can find the item by it.
14800/// A reader of the release before this one drops the slot's copy and reads the field, so an
14801/// item written here still reads with exactly one origin there.
14802fn slot_metadata(
14803    incoming: &Incoming<'_>,
14804    own_repository: Option<&Repository>,
14805    fallback: &[DependencyEdge],
14806) -> BTreeMap<String, Value> {
14807    let mut metadata = incoming.metadata.clone();
14808    match metadata.remove(ORIGIN_KEY) {
14809        Some(Value::String(origin)) if !origin.is_empty() => {
14810            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
14811        }
14812        _ => {}
14813    }
14814    match incoming.written.kind() {
14815        BoardKind::Work(kind) => metadata.insert(
14816            ItemKind::METADATA_KEY.to_owned(),
14817            Value::String(kind.marker().to_owned()),
14818        ),
14819        // A document is told by its title, so it carries no kind marker: that key names
14820        // what a dependency endpoint points at, and nothing may point at a document.
14821        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
14822    };
14823    incoming.classification.record(&mut metadata);
14824    let derivable = own_repository
14825        .map(|own| incoming.repositories == [own.clone()])
14826        .unwrap_or(incoming.repositories.is_empty());
14827    if derivable {
14828        metadata.remove(Repository::METADATA_KEY);
14829    } else {
14830        metadata.insert(
14831            Repository::METADATA_KEY.to_owned(),
14832            Value::Array(
14833                incoming
14834                    .repositories
14835                    .iter()
14836                    .map(|repository| Value::String(repository.as_str().to_owned()))
14837                    .collect(),
14838            ),
14839        );
14840    }
14841    // The typed lists are what land, whatever the caller's own metadata held under their
14842    // keys: a key of either name travelling beside the field would otherwise be a second
14843    // answer to the same question, and the field is the one the contract names.
14844    for (key, entries) in [
14845        (TaskRef::DELIVERS_KEY, incoming.delivers),
14846        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
14847    ] {
14848        set_task_list(&mut metadata, key, entries);
14849    }
14850    record_edges(&mut metadata, fallback);
14851    metadata
14852}
14853
14854/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
14855/// one slot's metadata, or no such key when there are none.
14856fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
14857    if fallback.is_empty() {
14858        metadata.remove(DependencyEdge::RECORDED_KEY);
14859    } else {
14860        metadata.insert(
14861            DependencyEdge::RECORDED_KEY.to_owned(),
14862            Value::Array(
14863                fallback
14864                    .iter()
14865                    .map(|edge| {
14866                        serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
14867                    })
14868                    .collect(),
14869            ),
14870        );
14871    }
14872}
14873
14874/// Every label one item carries, from its content's own connection and nowhere else.
14875///
14876/// There is no second place to read one from: no document this source sends selects the
14877/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
14878/// cannot carry one at all. The module documentation records the three schema facts that
14879/// settle it.
14880fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
14881    optional_nodes(content.get("labels"), "content labels")?
14882        .into_iter()
14883        .flatten()
14884        .map(|v| {
14885            Ok(Label {
14886                id: NativeId(required_str(v, "id")?.to_owned()),
14887                name: required_str(v, "name")?.to_owned(),
14888                color: optional_str(v, "color")?.map(str::to_owned),
14889            })
14890        })
14891        .collect()
14892}
14893
14894/// Each board field value one item holds, as the change token's facets name it: the field's
14895/// name, with the single-select option's name or the text. A value of a type no read here
14896/// selects, and one naming no field, says nothing a person can rename and is left out.
14897fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
14898    field_values
14899        .iter()
14900        .filter_map(|value| {
14901            let field = value.pointer("/field/name")?.as_str()?;
14902            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
14903            Some((field.to_owned(), held.to_owned()))
14904        })
14905        .collect()
14906}
14907
14908/// The digest of the facets of the issue `content`, whose board item's values are
14909/// `field_values` — or `None` when the response did not carry, whole, every member they are read
14910/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
14911/// label, and every field value.
14912///
14913/// What a write's own response spells its token from, by exactly the rules a read takes them by.
14914fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
14915    let ended = |connection: &Value| {
14916        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14917    };
14918    let labels = content.get("labels").filter(|labels| ended(labels))?;
14919    if !ended(field_values) {
14920        return None;
14921    }
14922    let parent = content.get("parent")?;
14923    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
14924    let kind = BoardKind::of(
14925        content.get("title")?.as_str()?,
14926        !parent.is_null(),
14927        sub_issue_total(content).ok()?,
14928        ItemKind::from_metadata(&slot).ok()?,
14929    );
14930    let labels = self::labels(&json!({"labels": labels})).ok()?;
14931    let nodes = field_values.get("nodes")?.as_array()?;
14932    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
14933}
14934
14935/// The definition of each board field one item's values are values of, in the shape a read
14936/// of the board's own `fields` gives one.
14937///
14938/// A value names its field through a fragment on that field's own type, so the type is
14939/// known from which kind of value it is: a single-select value's field is a
14940/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
14941/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
14942fn field_definitions(field_values: &[Value]) -> Vec<Value> {
14943    field_values
14944        .iter()
14945        .filter_map(|value| {
14946            let field = value.get("field")?.as_object()?;
14947            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
14948            let typename = if value.get("text").is_some() {
14949                "ProjectV2Field"
14950            } else if value.get("name").is_some() {
14951                "ProjectV2SingleSelectField"
14952            } else {
14953                return None;
14954            };
14955            let mut defined = field.clone();
14956            defined.insert("__typename".to_owned(), json!(typename));
14957            // Only a text field holds a text value, so the value says the field's type too.
14958            if typename == "ProjectV2Field" {
14959                defined.insert("dataType".to_owned(), json!("TEXT"));
14960            }
14961            Some(Value::Object(defined))
14962        })
14963        .collect()
14964}
14965
14966fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
14967    let Some(node) = field_values
14968        .iter()
14969        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
14970    else {
14971        return Ok(None);
14972    };
14973    Ok(optional_str(node, "text")?.map(str::to_owned))
14974}
14975
14976fn valid_github_owner(owner: &str) -> bool {
14977    !owner.is_empty()
14978        && owner.len() <= 39
14979        && !owner.starts_with('-')
14980        && !owner.ends_with('-')
14981        && !owner.contains("--")
14982        && owner
14983            .bytes()
14984            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
14985}
14986
14987/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
14988/// neither of the two names a path segment already means.
14989fn valid_github_repository_name(name: &str) -> bool {
14990    !name.is_empty()
14991        && name.len() <= 100
14992        && name != "."
14993        && name != ".."
14994        && name
14995            .bytes()
14996            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
14997}
14998
14999fn valid_environment_name(name: &str) -> bool {
15000    let mut bytes = name.bytes();
15001    bytes
15002        .next()
15003        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
15004        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
15005}
15006
15007/// How many sub-issues one issue has.
15008///
15009/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
15010/// absent or non-integer one is a response this source cannot read — and reading it as
15011/// zero would classify a project as a task, which is exactly the mistake the marker
15012/// exists to keep from happening quietly.
15013fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
15014    let summary = issue
15015        .get("subIssuesSummary")
15016        .ok_or_else(|| SourceError::Malformed {
15017            message: "GitHub issue is missing subIssuesSummary".into(),
15018        })?;
15019    summary
15020        .get("total")
15021        .and_then(Value::as_u64)
15022        .ok_or_else(|| SourceError::Malformed {
15023            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
15024        })
15025}
15026
15027/// One issue's own `number`.
15028///
15029/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
15030/// an issue in this module asks for it. So a read of one that comes back without it, or
15031/// with something that is not an unsigned integer, is a response this source cannot read —
15032/// absence here is **not** "this issue has no number". A draft is the content that has
15033/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
15034/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
15035fn issue_number(issue: &Value) -> Result<u64, SourceError> {
15036    issue
15037        .get("number")
15038        .and_then(Value::as_u64)
15039        .ok_or_else(|| SourceError::Malformed {
15040            message: "GitHub issue number is missing or is not an unsigned integer".into(),
15041        })
15042}
15043
15044/// The `number` a creating mutation answered with, and `None` when it answered without one;
15045/// why a missing one is tolerated is at the call in `create_and_file_issue`.
15046fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
15047    match created.get("number") {
15048        None | Some(Value::Null) => Ok(None),
15049        Some(value) => value
15050            .as_u64()
15051            .map(Some)
15052            .ok_or_else(|| SourceError::Malformed {
15053                message: "GitHub created issue number is not an unsigned integer".into(),
15054            }),
15055    }
15056}
15057
15058fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15059    value
15060        .get(field)
15061        .and_then(Value::as_str)
15062        .ok_or_else(|| SourceError::Malformed {
15063            message: format!("GitHub response is missing string field {field}"),
15064        })
15065}
15066
15067fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15068    let found = required_str(value, field)?;
15069    if found.trim().is_empty() {
15070        return Err(SourceError::Malformed {
15071            message: format!("GitHub response has blank string field {field}"),
15072        });
15073    }
15074    Ok(found)
15075}
15076
15077/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15078/// needs one — Linear spells them too, in its own description field.
15079///
15080/// Restated rather than shared, because a plugin crate depends on the contract crate and
15081/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15082/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15083/// source round-trips its own writes perfectly well under its own spelling.
15084const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15085const METADATA_CLOSE: &str = "\n-->";
15086
15087/// What the composer puts between a non-empty visible body and the slot, and the one thing
15088/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15089/// other trailing byte of the body comes back as it was written.
15090// llmlint: ignore[contracts_have_one_source_or_a_drift_gate] How a composer lays the slot after prose is this source's own; `docs/metadata.md` and its gate settle only the delimiters, and no other source declares a separator to reconcile against.
15091const METADATA_SEPARATOR: &str = "\n\n";
15092
15093/// The visible body and the metadata slot at the end of it.
15094///
15095/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15096/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15097/// own content and is left alone. The visible body is everything before the slot less the
15098/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15099fn metadata_body(
15100    body: Option<String>,
15101) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15102    let Some(body) = body else {
15103        return Ok((None, BTreeMap::new()));
15104    };
15105    let Some(slot) = slot_span(&body)? else {
15106        return Ok((Some(body), BTreeMap::new()));
15107    };
15108    let metadata =
15109        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15110            SourceError::Malformed {
15111                message: format!(
15112                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15113                ),
15114            }
15115        })?;
15116    let before = &body[..slot.start];
15117    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15118    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15119}
15120
15121/// Where the metadata slot sits in one body, as byte offsets into it.
15122struct SlotSpan {
15123    /// Where [`METADATA_OPEN`] begins.
15124    start: usize,
15125    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15126    encoded_start: usize,
15127    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15128    encoded_end: usize,
15129    /// Just past [`METADATA_CLOSE`].
15130    end: usize,
15131}
15132
15133/// The slot at the very end of `body`, or `None` when it has none.
15134///
15135/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15136/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15137/// slot.
15138fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15139    let Some(start) = body.rfind(METADATA_OPEN) else {
15140        return Ok(None);
15141    };
15142    let encoded_start = start + METADATA_OPEN.len();
15143    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15144        return Err(SourceError::Malformed {
15145            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15146        });
15147    };
15148    let encoded_end = encoded_start + relative_end;
15149    let end = encoded_end + METADATA_CLOSE.len();
15150    if !body[end..].trim().is_empty() {
15151        return Ok(None);
15152    }
15153    Ok(Some(SlotSpan {
15154        start,
15155        encoded_start,
15156        encoded_end,
15157        end,
15158    }))
15159}
15160
15161/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15162/// slot as it was.
15163///
15164/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15165/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15166/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15167/// or alone in an empty body — and a body with no slot that is given no metadata is
15168/// returned as it is.
15169fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15170    let encoded = if metadata.is_empty() {
15171        None
15172    } else {
15173        Some(
15174            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15175                message: error.to_string(),
15176            })?,
15177        )
15178    };
15179    Ok(match (slot_span(body)?, encoded) {
15180        (Some(slot), Some(encoded)) => format!(
15181            "{}{encoded}{}",
15182            &body[..slot.encoded_start],
15183            &body[slot.encoded_end..]
15184        ),
15185        (Some(slot), None) => {
15186            let before = &body[..slot.start];
15187            format!(
15188                "{}{}",
15189                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15190                &body[slot.end..]
15191            )
15192        }
15193        (None, None) => body.to_owned(),
15194        (None, Some(encoded)) if body.is_empty() => {
15195            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15196        }
15197        (None, Some(encoded)) => {
15198            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15199        }
15200    })
15201}
15202
15203/// `body` with everything before its metadata slot replaced by `content`, and the slot
15204/// itself kept byte for byte.
15205///
15206/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15207/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15208/// `content` is empty — so a read of the result reports `content` as the visible body and
15209/// the slot's metadata exactly as it was.
15210fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15211    let Some(slot) = slot_span(body)? else {
15212        return Ok(content.to_owned());
15213    };
15214    let kept = &body[slot.start..];
15215    Ok(if content.is_empty() {
15216        kept.to_owned()
15217    } else {
15218        format!("{content}{METADATA_SEPARATOR}{kept}")
15219    })
15220}
15221
15222/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15223fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15224    if entries.is_empty() {
15225        metadata.remove(key);
15226    } else {
15227        metadata.insert(
15228            key.to_owned(),
15229            Value::Array(
15230                entries
15231                    .iter()
15232                    .map(|entry| Value::String(entry.as_str().to_owned()))
15233                    .collect(),
15234            ),
15235        );
15236    }
15237}
15238
15239fn compose_body(
15240    content: Option<&str>,
15241    metadata: &BTreeMap<String, Value>,
15242) -> Result<Option<String>, SourceError> {
15243    let visible = content.unwrap_or_default();
15244    if metadata.is_empty() {
15245        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15246    }
15247    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15248        message: error.to_string(),
15249    })?;
15250    Ok(Some(if visible.is_empty() {
15251        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15252    } else {
15253        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15254    }))
15255}
15256
15257fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15258    value
15259        .get(field)
15260        .and_then(Value::as_bool)
15261        .ok_or_else(|| SourceError::Malformed {
15262            message: format!("GitHub response is missing boolean field {field}"),
15263        })
15264}
15265fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15266    match value.get(field) {
15267        None | Some(Value::Null) => Ok(None),
15268        Some(value) => value
15269            .as_str()
15270            .map(Some)
15271            .ok_or_else(|| SourceError::Malformed {
15272                message: format!("GitHub response field {field} is not a string or null"),
15273            }),
15274    }
15275}
15276fn optional_nodes<'a>(
15277    connection: Option<&'a Value>,
15278    name: &str,
15279) -> Result<Option<&'a Vec<Value>>, SourceError> {
15280    match connection {
15281        None | Some(Value::Null) => Ok(None),
15282        Some(value) => value
15283            .get("nodes")
15284            .and_then(Value::as_array)
15285            .map(Some)
15286            .ok_or_else(|| SourceError::Malformed {
15287                message: format!("GitHub {name}.nodes is not an array"),
15288            }),
15289    }
15290}
15291fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15292    let page_info = connection
15293        .get("pageInfo")
15294        .ok_or_else(|| SourceError::Malformed {
15295            message: format!("GitHub {name} has no pageInfo"),
15296        })?;
15297    if required_bool(page_info, "hasNextPage")? {
15298        return Err(SourceError::Malformed {
15299            message: format!(
15300                "GitHub {name} exceeds the supported nested connection size of {size}"
15301            ),
15302        });
15303    }
15304    Ok(())
15305}
15306fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15307    optional_str(value, field)?
15308        .map(|timestamp| {
15309            timestamp.parse().map_err(|error| SourceError::Malformed {
15310                message: format!("GitHub response field {field} is not a timestamp: {error}"),
15311            })
15312        })
15313        .transpose()
15314}
15315fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15316    if page.limit == 0 {
15317        Err(SourceError::Config {
15318            message: "page limit must be at least 1".into(),
15319        })
15320    } else {
15321        Ok(())
15322    }
15323}
15324fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15325    let page = connection
15326        .get("pageInfo")
15327        .filter(|value| value.is_object())
15328        .ok_or_else(|| SourceError::Malformed {
15329            message: "GitHub connection is missing pageInfo".into(),
15330        })?;
15331    if required_bool(page, "hasNextPage")? {
15332        let cursor = required_str(page, "endCursor")?;
15333        validate_cursor_progress(None, cursor)?;
15334        Ok(Some(Cursor(cursor.into())))
15335    } else {
15336        Ok(None)
15337    }
15338}
15339fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15340    if next.is_empty() || previous == Some(next) {
15341        Err(SourceError::Malformed {
15342            message: "GitHub pagination cursor is empty or did not advance".into(),
15343        })
15344    } else {
15345        Ok(())
15346    }
15347}
15348/// The version of this plugin's opaque narrowing-search cursor.
15349pub const SEARCH_CURSOR_VERSION: u32 = 4;
15350
15351#[derive(Serialize, Deserialize)]
15352#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15353enum SearchConnection {
15354    Initial {},
15355    Continuing { after: Cursor },
15356    Exhausted {},
15357}
15358impl SearchConnection {
15359    fn after(&self) -> Option<&str> {
15360        match self {
15361            Self::Continuing { after } => Some(&after.0),
15362            _ => None,
15363        }
15364    }
15365    fn exhausted(&self) -> bool {
15366        matches!(self, Self::Exhausted { .. })
15367    }
15368    /// Whether a cursor naming this position, `offset` rows into its page, is one this
15369    /// plugin could have handed out: a page is resumed only part of the way through it — an
15370    /// offset of a whole page or more would skip rows nobody was given — an initial page
15371    /// only once some of it was handed out, and an exhausted connection has no page to be
15372    /// part of the way through.
15373    fn valid_resume(&self, offset: usize) -> bool {
15374        let within = offset < SEARCH_PAGE_SIZE as usize;
15375        match self {
15376            Self::Initial { .. } => offset > 0 && within,
15377            Self::Continuing { after } => !after.0.is_empty() && within,
15378            Self::Exhausted { .. } => offset == 0,
15379        }
15380    }
15381}
15382
15383/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15384#[derive(Serialize, Deserialize)]
15385#[serde(deny_unknown_fields)]
15386struct SearchPosition {
15387    version: u32,
15388    connection: SearchConnection,
15389    /// How many rows of the page `connection` starts were already handed out.
15390    #[serde(default, skip_serializing_if = "is_zero")]
15391    offset: usize,
15392    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15393    seen: Vec<NativeId>,
15394    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15395    own: Vec<NativeId>,
15396}
15397impl Default for SearchPosition {
15398    fn default() -> Self {
15399        Self {
15400            version: SEARCH_CURSOR_VERSION,
15401            connection: SearchConnection::Initial {},
15402            offset: 0,
15403            seen: Vec::new(),
15404            own: Vec::new(),
15405        }
15406    }
15407}
15408
15409fn is_zero(offset: &usize) -> bool {
15410    *offset == 0
15411}
15412
15413fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15414    cursor.map_or(Ok(0), |c| {
15415        c.0.parse().map_err(|_| SourceError::Config {
15416            message: "page cursor is invalid".into(),
15417        })
15418    })
15419}
15420fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15421    if offset > items.len() {
15422        return Page::last(vec![]);
15423    }
15424    let tail = items.split_off(offset);
15425    let mut selected = tail;
15426    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15427    selected.truncate(limit);
15428    Page {
15429        items: selected,
15430        next,
15431    }
15432}
15433
15434impl GitHubProjectsSource {
15435    async fn write_task_assets(
15436        &self,
15437        write: &ItemWrite<Task>,
15438        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15439    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15440        self.write_item(
15441            &self.task_incoming(write, assets)?,
15442            write.target.as_ref(),
15443            &write.depends_on,
15444        )
15445        .await
15446    }
15447
15448    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15449    /// are known to be ones this source can write.
15450    fn task_incoming<'a>(
15451        &self,
15452        write: &'a ItemWrite<Task>,
15453        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15454    ) -> Result<Incoming<'a>, SourceError> {
15455        let near = write.target.as_ref().unwrap_or(&write.item.id);
15456        for (key, entries) in [
15457            (TaskRef::DELIVERS_KEY, &write.item.delivers),
15458            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15459        ] {
15460            TaskRef::listed(key, near, Some(&self.name), entries.clone())
15461                .map_err(|message| SourceError::Refused { message })?;
15462        }
15463        if self.priorities.is_none() && write.item.priority != Priority::None {
15464            return Err(self.holds_no_priority());
15465        }
15466        Ok(Incoming {
15467            written: Written::Work(ItemKind::Task, &write.item.status),
15468            title: &write.item.title,
15469            content: write.item.content.as_deref(),
15470            assets,
15471            labels: &write.item.labels,
15472            metadata: &write.item.metadata,
15473            repositories: &write.item.repositories,
15474            classification: write.item.classification,
15475            parent: write.item.project.as_ref(),
15476            delivers: &write.item.delivers,
15477            delivered_by: &write.item.delivered_by,
15478            priority: self.priorities.as_ref().map(|_| write.item.priority),
15479        })
15480    }
15481    async fn write_document_assets(
15482        &self,
15483        write: &ItemWrite<Document>,
15484        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15485    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15486        // A document takes part in no dependency graph, so there is no far end to write
15487        // natively and none to record: a caller naming one is told so rather than having it
15488        // stored under the reserved key, where a later read would report an edge the
15489        // contract says cannot exist.
15490        if !write.depends_on.is_empty() {
15491            return Err(SourceError::Refused {
15492                message: format!(
15493                    "this write names {} dependencies for a document, and a document takes \
15494                     part in no dependency graph; next: put the dependency on the task or \
15495                     project the document is about",
15496                    write.depends_on.len()
15497                ),
15498            });
15499        }
15500        self.write_item(
15501            &Incoming {
15502                written: Written::Document,
15503                title: &write.item.title,
15504                content: write.item.content.as_deref(),
15505                assets,
15506                labels: &write.item.labels,
15507                metadata: &write.item.metadata,
15508                repositories: &write.item.repositories,
15509                classification: write.item.classification,
15510                parent: write.item.project.as_ref(),
15511                delivers: &[],
15512                delivered_by: &[],
15513                priority: None,
15514            },
15515            write.target.as_ref(),
15516            &[],
15517        )
15518        .await
15519    }
15520}
15521
15522/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15523/// itself, for the two things no journey can observe.
15524///
15525/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15526/// with and without the call, that a settlement, a board listing and a metadata search each
15527/// read afresh after it — the resolved records, the written-item overlay, the board and its
15528/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15529/// because a status write naming an option a person deleted is refused the same whether or
15530/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15531/// while one of its locks is held. So these assert those directly, and every other holder
15532/// beside them so a holder added later without a clear in the call fails here.
15533#[cfg(test)]
15534mod end_command_tests {
15535    use super::*;
15536
15537    struct Token;
15538
15539    impl SecretResolver for Token {
15540        fn get(&self, var: &str) -> Option<SecretString> {
15541            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15542        }
15543    }
15544
15545    fn source() -> GitHubProjectsSource {
15546        let config = serde_json::from_value(json!({
15547            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15548            // Nothing here is sent: the source is only built and its state inspected.
15549            "endpoint": "http://127.0.0.1:9/graphql",
15550        }))
15551        .expect("a usable configuration");
15552        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15553            .expect("the source builds")
15554    }
15555
15556    /// One issue as a board read answers it.
15557    fn resolved(source: &GitHubProjectsSource) -> Resolved {
15558        source
15559            .resolve(&json!({
15560                "id": "ITEM-1",
15561                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15562                            "body": "what a person may since have edited", "state": "OPEN",
15563                            "stateReason": null, "url": null, "number": 1,
15564                            "subIssuesSummary": {"total": 0},
15565                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15566                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15567            }))
15568            .expect("the item reads")
15569            .expect("an issue")
15570    }
15571
15572    /// Hold something in every holder the call clears, and the repository id it keeps.
15573    fn fill(source: &GitHubProjectsSource) {
15574        let item = resolved(source);
15575        source.created.lock().unwrap().push(item.clone());
15576        source.updated.lock().unwrap().push(item.clone());
15577        *source.board_cache.lock().unwrap() = Some(Board {
15578            id: "PVT-board".into(),
15579            fields: json!({"nodes": []}),
15580            items: vec![item.clone()],
15581        });
15582        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
15583        source
15584            .narrowed_cache
15585            .lock()
15586            .unwrap()
15587            .insert("status:todo".into(), vec![item.clone()]);
15588        source.children_cache.lock().unwrap().insert(
15589            NativeId("P-1".into()),
15590            (NativeId("P-1".into()), vec![item.clone()]),
15591        );
15592        source
15593            .search_next
15594            .lock()
15595            .unwrap()
15596            .insert("status:todo".into(), Some("cursor".into()));
15597        source
15598            .resolved_cache
15599            .lock()
15600            .unwrap()
15601            .insert(item.id.clone(), item);
15602        *source.fields_cache.lock().unwrap() = Some(BoardFields {
15603            id: BoardId::parse("PVT-board").unwrap(),
15604            fields: json!({"nodes": []}),
15605        });
15606        source
15607            .repository_cache
15608            .lock()
15609            .unwrap()
15610            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
15611    }
15612
15613    fn assert_dropped(source: &GitHubProjectsSource) {
15614        assert!(source.created().unwrap().is_empty(), "created");
15615        assert!(source.updated().unwrap().is_empty(), "updated");
15616        assert!(source.board_cache().unwrap().is_none(), "board");
15617        assert!(source.search_cache.lock().unwrap().is_none(), "search");
15618        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
15619        assert!(
15620            source.children_cache.lock().unwrap().is_empty(),
15621            "project children"
15622        );
15623        assert!(
15624            source.search_next.lock().unwrap().is_empty(),
15625            "search paging"
15626        );
15627        assert!(
15628            source.resolved_cache().unwrap().is_empty(),
15629            "resolved records"
15630        );
15631        assert!(source.fields_cache().unwrap().is_none(), "board fields");
15632        assert_eq!(
15633            source.repository_cache().unwrap().len(),
15634            1,
15635            "a repository's node id stays valid and is kept"
15636        );
15637    }
15638
15639    fn end(source: &GitHubProjectsSource) {
15640        tokio::runtime::Builder::new_current_thread()
15641            .build()
15642            .unwrap()
15643            .block_on(source.end_command())
15644            .expect("the command ends");
15645    }
15646
15647    #[test]
15648    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
15649        let source = source();
15650        fill(&source);
15651        end(&source);
15652        assert_dropped(&source);
15653    }
15654
15655    #[test]
15656    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
15657        fn poison<T: Send>(held: &Mutex<T>) {
15658            std::thread::scope(|scope| {
15659                let _ = scope
15660                    .spawn(|| {
15661                        let _guard = held.lock().unwrap();
15662                        panic!("a failure while the lock is held");
15663                    })
15664                    .join();
15665            });
15666            assert!(held.is_poisoned());
15667        }
15668        let source = source();
15669        fill(&source);
15670        poison(&source.created);
15671        poison(&source.updated);
15672        poison(&source.board_cache);
15673        poison(&source.search_cache);
15674        poison(&source.narrowed_cache);
15675        poison(&source.search_next);
15676        poison(&source.resolved_cache);
15677        poison(&source.fields_cache);
15678        assert!(
15679            source.resolved_cache().is_err(),
15680            "a poisoned lock is refused before the call"
15681        );
15682        end(&source);
15683        assert_dropped(&source);
15684    }
15685}
15686
15687#[cfg(test)]
15688mod unprojectable_fields_tests {
15689    use super::*;
15690
15691    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
15692    fn documented() -> std::collections::BTreeSet<String> {
15693        let document = std::fs::read_to_string(
15694            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
15695        )
15696        .expect("docs/metadata.md is readable");
15697        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
15698        let lead = "a `field` equal, ignoring case, to ";
15699        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
15700        let sentence = &document[start..];
15701        let sentence = &sentence[..sentence
15702            .find("; and two entries naming one field")
15703            .expect("the refused-field sentence ends at the duplicate rule")];
15704        let (own, github) = sentence
15705            .split_once(" or a field GitHub owns on every board (")
15706            .expect("the sentence names GitHub's own fields");
15707        own.split('`')
15708            .skip(1)
15709            .step_by(2)
15710            .chain(
15711                github
15712                    .strip_suffix(')')
15713                    .expect("GitHub's own fields close the sentence")
15714                    .split(", "),
15715            )
15716            .map(str::to_owned)
15717            .collect()
15718    }
15719
15720    #[test]
15721    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
15722        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
15723            .iter()
15724            .map(|&name| name.to_owned())
15725            .collect();
15726        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
15727        let instance = SourceName::new("work").unwrap();
15728        for name in UNPROJECTABLE_FIELDS {
15729            for spelled in [name.to_owned(), name.to_uppercase()] {
15730                let entry = MetadataFieldConfig {
15731                    field: spelled.clone(),
15732                    key: "team.machine".to_owned(),
15733                    path: Vec::new(),
15734                };
15735                let refused = MetadataField::resolve(vec![entry], &instance)
15736                    .expect_err("an unprojectable field is refused");
15737                assert!(
15738                    refused.to_string().contains(&format!("{name:?}")),
15739                    "{spelled}: {refused}"
15740                );
15741            }
15742        }
15743    }
15744}