Skip to main content

onetaskgraph_github_projects/
lib.rs

1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A write held to a version** (`--if-version`) is checked immediately before its first
4//! mutation and not as one step with it: GitHub takes no conditional write, so the engine reads
5//! the task through this source just before writing it, and a change landing between that read
6//! and the mutation — the time one request takes — is GitHub's residual race.
7//!
8//! **A board is a container of projects, not a project.** Its own `title`,
9//! `shortDescription` and `readme` are never read as an item's fields and are never
10//! written: nothing in this source can rename the board a user configured.
11//!
12//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
13//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
14//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
15//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
16//! naming the field — but that repository is the *fallback*, not the home of every item.
17//!
18//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
19//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
20//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
21//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
22//! change to the rule. -->
23//! **Which repository an issue is created in is decided by the item's own `repositories`
24//! field, under one rule.** Exactly one entry names the repository the issue is created in:
25//! a task issue is where a person finds the work from the repository it changes, and one
26//! filed in a board's nominated repository is invisible from every other. Zero entries, or
27//! two or more, name none, so a task's or a document's issue is created in the repository
28//! its parent project's issue lives in — read from the board, or from this process's own
29//! record of a project it created earlier in the same command — and a project's issue, or
30//! a task or document written with no parent, is created in the configured `repository:`.
31//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
32//! existing issue is never moved: the update path leaves the issue where it is and records
33//! the list in the metadata slot when it differs, so the read side's derivation and the
34//! creation rule agree by construction.
35//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
36//!
37//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
38//! board has no document type and nothing but issues to hold one in, so the title is the
39//! discriminator and it is the whole of it. The title this source *reports* is the one a
40//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
41//! the body so `content` is what the person wrote — and writing a document puts the prefix
42//! back, so a round trip returns the title that went in.
43//!
44//! **Telling a document from a project from a task.** The design prefix is read **first**:
45//! a document is never a project and never a task, whatever sub-issues it has or does not
46//! have. Only then does the rest apply — a board issue is a project when *either* it has
47//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
48//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
49//! necessary: it is what makes an *empty* project — the state a project copy passes
50//! through between creating the project and filing its first task — readable as a
51//! project, while the sub-issue arm lets a person author a project on the board by hand
52//! with no knowledge of this product's metadata at all. Reading the prefix later than the
53//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
54//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
55//! task nor a document and are ignored.
56//!
57//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
58//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
59//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
60//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
61//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
62//! every comment as the account the token belongs to, so a comment handed an author of its
63//! own is refused rather than posted under another name. GitHub's comment mutations take the
64//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
65//! is on, and a comment on some other issue is one this task does not have.
66//!
67//! **Where an entity is, is a link.** Every project, task and document this source reports
68//! carries a [`Location::Url`] naming the issue's own web address — the same address the
69//! `url` field already reports, in the shape that says a reader can open it. That is the
70//! contrast the location contract exists for: a reader holding an entity from this source
71//! is handed something to link to and one holding an entity from a folder of Markdown is
72//! handed a path, and neither has to know which plugin answered. It does not replace or
73//! derive from `url`; that field goes on reporting what it always reported.
74//!
75//! **Where metadata lives.** Short typed things go to typed fields and native relations:
76//! status to the board's `Status` single-select and the issue's own state, the copy
77//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
78//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
79//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
80//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
81//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
82//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
83//! a project or a document alike — is one update of the issue body that changes that slot
84//! and not one byte outside it, and it is not sent at all when the key already holds the
85//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
86//! is kept in that same slot, written by that same update.
87//!
88//! **A caller's value can also be projected onto a board text field**, so the board can filter
89//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
90//! metadata key and a path inside its value. The slot stays the value's one home — every read
91//! reports metadata from it alone — and the field follows it on every write of an item, a
92//! create, an update and a copy alike: a string is written, and not sent again while the field
93//! holds it — an empty string included, which is written as itself; nothing there or `null`
94//! clears a field holding a value; any other
95//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
96//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
97//! own moves its field first and the body last, as every write of an existing item does. A
98//! board without the field, or with a non-text field of that name, is refused before any
99//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
100//! a text field and never changes a field that is there.
101//!
102// 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.
103//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
104//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
105//! option of the board's one `Status` field for a task and for a project: a bare name is
106//! the option for both kinds, `null` disables the category for both, and `{task, project}`
107//! names it per kind. A category the mapping does not mention keeps its shipped default for
108//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
109//! gets the shipped default for the kind it leaves out. Two categories one kind would read
110//! back from one option are refused as the configuration is read, ignoring case, while one
111//! option may stand for different categories of the two kinds. Writes go by the kind of the
112//! item written: a status that kind has no option for, or whose option the board lacks, is
113//! refused before any mutation, naming the source, the kind, the category and the key
114//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
115//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
116//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
117//! before selecting its option. Reads give a closed issue's reason precedence over its
118//! option, while an open issue's option decides its category through its own kind's
119//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
120//! The guarded [`GitHubProjectsSource::status_options`] and
121//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
122//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
123//! existing option id and verify the field and item assignments immediately afterwards.
124//! They ask for both kinds' options, counting a terminal category's mapped option as
125//! configured because a terminal write refuses without it. No ordinary source read or
126//! write calls that mutation, whose
127//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
128//! and a mistake destroys every item's status. A status this board cannot represent is a
129//! refusal naming the status and the instance instead.
130//!
131//! `unknown` has no shipped option because this source cannot preserve an open-ended
132//! status word: it writes an existing board option and never
133//! creates an option. An operator may map `unknown` to one existing option, in which case
134//! every unknown word lands on that option and reads back as `unknown` under the option's
135//! name. This differs from `local-md`, which writes and reads the original word itself.
136//!
137//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
138//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
139//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
140//! finished tasks were only moved to a "Done" column would read 0% complete forever.
141// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
142//!
143//! # What this source declares, field by field
144//!
145//! One verdict per field of [`Capabilities`], and what `Native` means when this source
146//! says it. *Proven* means a shared journey drives it against the real
147//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
148//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
149//! [`capabilities`](TaskSource::capabilities) from parting.
150//!
151//! | Field | Verdict |
152//! | --- | --- |
153//! | `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. |
154//! | `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. |
155//! | `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. |
156//! | `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. |
157//! | `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. |
158//! | `assignees` | **Supported and proven when `config.assignees: true`,** unsupported otherwise, which reads no issue's assignees and sends exactly the requests it sent before assignees existed. An issue's `assignees` by login, every one it holds, selected on the reads the source already makes; a write replaces them with one login or none as one more aliased field of the document the write already sends, and a login GitHub refuses, or answers leaving off the issue, is refused for that item naming it. See [`assignees`] for the login lookup. Proven by `onetaskgraph-github-projects-e2e`'s `assignees` journeys. |
159//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
160//! | `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. |
161//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
162//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
163//! | `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. |
164//! | `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. |
165//! | `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. |
166//! | `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. |
167//! | `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. |
168//! | `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. |
169//! | `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. |
170//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
171//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
172//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
173//!
174//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
175//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
176//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
177//! and the raw bytes as the request body. The numeric repository id is read at most once
178//! per repository per source instance. A loopback API endpoint also selects the loopback
179//! uploads host; production needs no assets repository, commits or extra configuration.
180//! Every returned JSON `url` is read with the same token before a body is written: only
181//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
182//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
183//! asset and HTTP status and says the token type may not be accepted by the upload
184//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
185//! fine-grained PATs or OAuth tokens.
186//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
187//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
188//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
189//! and verified afresh, in the repository the issue already lives in on an update.
190//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
191//! Uploads take the same mutation spacing as content writes. Both uploads and verification
192//! reads wait out classified rate limits within the configured per-call retry budget,
193//! on the supplied clock; verifying reads take no mutation slot.
194//!
195//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
196//! source has documents and that its tasks have comments, both of which hold — and the three
197//! facts behind the uniform `Native` on the
198//! predicates beside it are recorded below rather than re-derived, because a reader who
199//! takes `Native` to mean *the remote service filters* will read that uniformity as a
200//! lie.
201//!
202//! First, the plugin contract defines `Support::Native` as *the source applies this
203//! predicate itself*, and says nothing about where it applies it. What the declaration
204//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
205//! — so that the engine may push it down and apply nothing of its own.
206//!
207//! Second, this source can keep that promise for every predicate at no additional API
208//! cost, because whichever of the reads below answers a query has already read every
209//! candidate that query will return before it filters anything. Filtering those items is
210//! in-process work over data already in hand.
211//!
212//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
213//! applied in process over what that question returned. A project filter has a relationship — a
214//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
215//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
216//! a search for metadata values, is the board-scoped issue search carrying the text and each
217//! value as quoted phrases; an origin is the board's own field filter over its origin field
218//! beside the same search for the id. **The text search narrows, and that is this source's
219//! declared semantics:** GitHub matches whole words where the substring rule this source and
220//! the local Markdown source confirm with would match inside one, so an item holding the text
221//! only inside a longer word is never a candidate. Every item returned does contain the text.
222//! A project query's text, and a document query's scoped to no project, is that same search
223//! and narrows on the same terms, its candidates confirmed by their kind as well.
224//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
225//! so those three are applied in process over the candidates, and a query carrying none of
226//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
227//! engine compensate for work this source has already done, and declaring `projects` native
228//! while ignoring the filter (which this source once did) silently returns another project's
229//! tasks, because the engine trusts the declaration and applies nothing locally.
230//!
231//! # The three ways this source reaches an item, and what each costs
232//!
233//! A board read is charged for what its *nested* connections could return rather than for
234//! what was asked, so one whole-board read costs the same whether the question was about
235//! one project or about all of them. That is why a question about one project is never
236//! answered by reading the board:
237//!
238//! | The question | What is sent | What it costs |
239//! | --- | --- | --- |
240//! | 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 |
241//! | 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 |
242//! | one board page with each task's first comments, for `TaskSource::query_task_details` | [`graphql::BOARD_WITH_COMMENTS`] — the board's own `items`, without search completion | tasks and first comment pages in one request; board consistency, with comment-activity filtering refused |
243//! | 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 |
244//! | 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 |
245//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
246//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
247//! | 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 |
248//! | 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 |
249//! | 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 |
250//! | 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 |
251//! | 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 |
252//! | 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 |
253//!
254//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
255//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
256//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
257//! equal to declared points equal to the row. They include the origin lookup and the
258//! field/repository discovery a create needs. A bound re-copy changes status, priority,
259//! content and metadata; comment recount means a subsequent detail read. Each request here
260//! costs one declared point. A membership beyond the embedded page can additionally require
261//! the one-point membership recovery described above. A bound re-copy of a task filed under a
262//! project adds one read, the engine confirming that project's link by its own id once per
263//! command; and the same-source far ends a write newly names — those that do not already block
264//! the item, whose own read answered for them — are read together by their own ids,
265//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
266//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
267//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
268//!
269//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
270//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
271//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
272//! holds both halves.
273//!
274//! **An existing item is written body last.** A bound re-copy and a `task update` send its
275//! board fields first — the `Status` option and the `Priority` together, in one request — then
276//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
277//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
278//! undoing an earlier field when a later one fails, so that order is what makes a write
279//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
280//! the one piece of metadata written before the body, an origin a copy re-points, is put back
281//! when a later write is refused — and when putting it back is refused too, the write's own
282//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
283//! of those writes in turn, whole and as one aliased field failing after the one before it.
284//!
285//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
286//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
287//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
288//!
289//! - **A board is accepted at creation but its item is not answered, so a create still files
290//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
291//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
292//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
293//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
294//!   against a real board on 2026-10-01 with a create sending the board there and reading the
295//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
296//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
297//!   refused "Content already exists in this project" — GitHub had filed the issue after
298//!   answering, and refuses a second filing rather than answering with the item it holds. A
299//!   create therefore sends no `projectV2Ids` and files the issue with
300//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
301//!   real is the read before it: the board's fields and the repository's id together, in
302//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
303//! - **A comment still reads its target first, so a comment is 2 requests.**
304//!   `AddCommentInput.subjectId: ID!` is declared there with
305//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
306//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
307//!   project's issue, a document's issue, an issue on no board of this source and a pull
308//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
309//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
310//!   refuses those by name.
311//!
312//! | Verb | Requests / points | Documents |
313//! | --- | --- | --- |
314//! | 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 |
315//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
316//! | 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 |
317//! | 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 |
318//! | 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 |
319//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
320//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
321//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
322//! | 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 |
323//! | recount | 1 | ISSUE_DETAIL |
324//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
325//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
326//! | content | 2 | ISSUE, UPDATE_ISSUE |
327//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
328//! | 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 |
329//! | 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 |
330//! | record only | 1 | ISSUE |
331//!
332//! <!-- github-search-paging:start -->
333//! Board-scoped text, metadata, project-name and comment-activity searches send every
334//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
335//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
336//! needs rows. A page is never resized to the rows still needed: GitHub orders one
337//! search differently at different page sizes, so one fixed size makes a paged walk
338//! send exactly the requests one whole read sends, and the answer's order is the order
339//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
340//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
341//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
342//! returned and fetched pages: a limit is sliced from the pages it needs, and local
343//! confirmation can require more candidates than matching rows. Walking all pages
344//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
345//! cursor and how far into that page the last answer stopped, and resumes in the same
346//! process or a new one, without duplicates or gaps. It carries no rows: one process
347//! sends each page's search once, and a new process re-reads only the page it resumes
348//! in, then sends a further page once, never as a re-read, only when its limit still
349//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
350//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
351//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
352//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
353//! not required to include the original process's writes still omitted by the index.
354//! <!-- github-search-paging:end -->
355//!
356//! The board half of an issue — its board item's id, its `Status` option and this
357//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
358//! an item reached any of those ways resolves through the same
359//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
360//! same status, the same labels and the same qualified id. That connection comes back a
361//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
362//! on the page in hand and — only if that page reports more of the connection — in the
363//! last row's read of that one issue's memberships, resumed from the page's own cursor and
364//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
365//! report, which is what keeps an id naming another repository's issue from being answered
366//! as an item of this board; and because the page is where the search starts rather than
367//! where it ends, that answer is one about a connection read to exhaustion and never about
368//! an unread page. Nothing costs the extra read but an issue on more boards than a page
369//! holds: an issue this board really does not hold reports no next page, so its
370//! memberships are already exhausted where they arrived.
371//!
372//! **No document here selects the board's own `Labels` field, and nothing is lost by
373//! that.** An item's labels are read from its content alone, wherever that content is
374//! reached: the three documents above select `Issue.labels` on the fragment, and
375//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
376//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
377//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
378//! create one, and `ProjectV2FieldValue` — the whole of what
379//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
380//! it from the content, for every content type it exists on, and there is nothing it can
381//! hold that the content does not already say: for an `Issue` it *is* that issue's own
382//! labels, so selecting it beside them unions a set with itself.
383//!
384//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
385//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
386//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
387//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
388//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
389//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
390//! label set, and that set is the fixture issue's own, by
391//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
392//! item whose content is a draft reports an empty set, by
393//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
394//! selection is held over [`graphql::DOCUMENTS`] by
395//! `no_document_selects_the_boards_own_labels_field`.
396//!
397//! The whole-board row is still the board's own item connection, and deliberately: a
398//! **draft** board item is not an issue, so no search can list one, and the reads that have
399//! to answer for the whole board are the ones whose cost is the board's size anyway.
400//!
401//! **A question about one item this source already names by id never lists the board.**
402//! Whether that item is on this board, and what its board fields are, is answered by reading
403//! that item — its own `Issue.projectItems`, walked to exhaustion by
404//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
405//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
406//! holds. That covers a write's destination, the project a new item is filed under, a
407//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
408//! and the delete that takes back an item a copy made. What such a write needs of the board
409//! and the item does not carry — the board's id, the `Status` and origin field definitions —
410//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
411//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
412//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
413//! minutes. Scanning this host's 842-item board has refused a document copy and an update
414//! even though the items' own reads named that board. A scan there gives the wrong answer
415//! as well as paying for every page. So a `board.items` lookup does not belong on any of
416//! those paths.
417//!
418//! **What a read may return is capped too, and that cap is on the document rather than on
419//! the board.** GitHub limits the number of nodes **one query may return** to
420//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
421//! an error naming the connection the count crossed at, not a slow or a partial result.
422//! Every board this source reads is refused the same way, so no board is too big for these
423//! documents and none is small enough to save one that is over.
424//!
425//! The count is arithmetic over the document's own text: each connection contributes the
426//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
427//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
428//! restate them — `github-graphql-node-count` implements them, and
429//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
430//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
431//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
432//! same text on every run and fails naming any that reaches the limit — so a connection
433//! added to a shared fragment is caught there rather than by GitHub.
434//!
435//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
436//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
437//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
438//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
439//! effectively squared there, which is why it is the one the limit is most sensitive to.
440//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
441//! page of memberships misses is recovered by one further read rather than refused, so it
442//! buys a bound every read pays for at the price of a request only a multi-board issue
443//! pays.
444//!
445//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
446//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
447//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
448//! `cost` is rate-limit points, metered per hour across everything one credential does; it
449//! is what the two limiters [`Limiter`] tells apart meter, and a document under
450//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
451//! that second number, and `tests/point_cost.rs` pins every document in
452//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
453//! one under, the pin itself is the check. The credentialed lane reconciles both figures
454//! against GitHub's own, off a probe it already sends.
455//!
456//! **What is pinned that way is a per-document price and never a session's.** The record in
457//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
458//! **requests** and **worst-case nodes** — and neither is points. What one whole session
459//! consumes of the hourly point allowance is observable only from a credentialed run's own
460//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
461//! and what `tests/live.rs` prints at the end of every run.
462//!
463//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
464//!
465//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
466//! enumerations of a board can supply one alone.** Resolving a node id is strongly
467//! consistent, so a read by id and a project's own sub-issues are already current. The
468//! other two are not, and they are behind by different amounts and in different directions:
469//!
470//! - GitHub's **issue search** is an index and answers a write made moments ago with the
471//!   value from before it — usually for a second or two.
472//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
473//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
474//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
475//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
476//!
477//! That second one is a measurement rather than a caution. This repository's own
478//! credentialed journey writes a project and waits for the board to report it, then writes a
479//! task and waits for the same thing seconds later on the same board: the project wait is
480//! answered through the search and converged in two or three attempts in each of three runs,
481//! and the task wait is answered through `ProjectV2.items` and converged in none of them
482//! inside thirty. Separately, an item added to a second and larger board was read back by
483//! `Issue.projectItems` on that board's own id while every one of that connection's nine
484//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
485//! through the lagging one alone is what had a board read deny an issue that had certainly
486//! landed on it.
487//!
488//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
489//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
490//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
491//! search reports what the projection is behind on. What closes the last
492//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
493//! source answers is completed with what this process itself wrote, so an item created
494//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
495//! nothing is written down, and the record dies with the process. **A wait that has to
496//! observe GitHub's own data cannot be answered from that record** — which is why the
497//! credentialed journey asks through a source built afresh, and why the union above rather
498//! than a longer wait is what makes such a wait converge.
499//!
500//! **A narrowed read is the same bargain, stated for each of the three predicates it
501//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
502//! than walking the board, and every such answer is completed with what this process wrote —
503//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
504//! each filtered by the same predicates as the rest — so an item this command wrote a moment
505//! ago is returned by a query that matches it whether or not the index has caught up. An item
506//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
507//! consistent. What is left is stated rather than papered over:
508//!
509//! | Read | Finds | Behind by |
510//! | --- | --- | --- |
511//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
512//! | 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 |
513//! | 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 |
514//! | origin, third read | this process's own writes | nothing |
515//!
516//! So an origin carrier another process added within the last second or two, before either
517//! index has it, can be missing from an origin query, and one written by the release before
518//! this one — its origin in the field alone — can be missing for as long as the board's own
519//! item connection is behind on it. A copy that must not duplicate its own earlier write
520//! relies on the link it records, not on either index. **A board draft is not an issue**, so
521//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
522//! lists one, the origin lookup drops any the board's own field filter names, and one this
523//! process wrote is not added back either.
524//!
525//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
526//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
527//! body's metadata slot, so the issue search can find it in seconds. The field is
528//! authoritative: this source reads an item's origin from the field alone, so a slot that
529//! disagrees with it, or holds one where the field holds none, is never read as a second
530//! origin — and the release before this one reads the slot, drops that key's copy for the
531//! field's, and sees the same one origin.
532//!
533//! Filtering happens before paging, so a page of a filtered result is a page of the
534//! survivors rather than the survivors of a page. Label matching and the substring rule a
535//! text candidate is confirmed by answer the same question the same way the local Markdown
536//! source's do; which candidates a text search has to confirm is GitHub's word match, which
537//! is the one place the two sources can answer the same text differently.
538//!
539//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
540//! has one source, `capabilities`, and the note above is the reasoning behind it rather
541//! than a second copy of it: without the three facts recorded here a reader takes the
542//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
543//! crate's own capabilities test, which pins every field of it against a fully spelled-out
544//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
545//! fails to compile there rather than going unasserted. -->
546//! The fixture-server tests above run wherever this crate is selected; the credentialed
547//! lane runs in the same required check, beside them, and can fail it — it verifies the
548//! 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,
549//! one filed under neither, a label on one of the three and a closed status on another —
550//! because that shape is what tells an honoured predicate from an ignored one: a board
551//! holding a single project answers a project filter the same way whether or not this
552//! source applies it, which is exactly how the defect above went unseen.
553//!
554//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
555//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
556//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
557//! session or request, independently of the live demand. Fix that variable in the machine's
558//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
559//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
560//! policy crate; absent credentials or nominations otherwise skip.
561//!
562//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
563//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
564//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
565//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
566//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
567//! after its run reads back as `completed`, immediately before the listed-artifact batch.
568//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
569//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
570//! and the board's `onetaskgraph.origin` field untouched.
571//!
572//! # What a session of requests costs, and where the report is
573//!
574//! This source records **every** request it sends into [`accounting::Accounting`], at
575//! `send_once` — the one place a request leaves this crate, which is why a read path added
576//! later is counted without anybody remembering to count it. That is the whole of what this
577//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
578//! session's spend is arrived at, and what it deliberately does not know are set out.
579//!
580//! What one whole session of the live journey costs, counted that way against this crate's
581//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
582//! reduction it came out of, and with what it does and does not say about rate-limit points.
583//!
584//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
585//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
586//! code path — no environment variable, no feature, no build configuration — because an
587//! instrument nobody switches on measures nothing, and
588//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
589//! source's counts the whole session rather than this source's share. The credentialed lane
590//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
591//! passed or failed.
592//!
593//! **A live session refuses to start unless the account can afford it.** Before it does any
594//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
595//! GitHub documents as not counting against the REST rate limit and which answers both of
596//! its budgets at once — and starts only if, for each of them, what remains minus this
597//! session's estimated cost is still at least
598//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
599//! allowance. A session that cannot **declines**: it did not run, so it is
600//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
601//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
602//! waiting for the budget to come back. The estimate is derived offline from
603//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
604//! which is also where the published rule that model rests on is cited; the accounting
605//! above records the gate's own read like any other request, and
606//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
607//!
608//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
609//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
610//! text, which is what lets it run on every platform and on a pull request from a fork with
611//! no credential — and that is what actually stops a regression merging. But an offline
612//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
613//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
614//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
615//! number of nodes this query may return"* and whose `cost` is what that document would
616//! spend, both for a document **without executing it**, and the lane asks it for every query
617//! document this source sends, under the largest bindings this source sends, and fails when
618//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
619//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
620//! one; the offline pins still cover it. It records what those calls reported about the
621//! account's own allowance, because whether asking is free is a thing to observe rather than
622//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
623//! and `cost` is metered against an hourly allowance the accounting above reads off a
624//! credentialed run's own response headers.
625//!
626//! **GitHub has two rate limiters and this source is refused by both, so nothing here
627//! treats them as one thing.** The primary budget is the hourly allowance `gh api
628//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
629//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
630//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
631//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
632//! one part of.
633#![deny(missing_docs)]
634
635use std::collections::BTreeMap;
636use std::sync::{Arc, Mutex};
637use std::time::Duration;
638
639use chrono::{DateTime, Utc};
640use onetaskgraph_plugin_api::{
641    Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
642    ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
643    DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
644    Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
645    MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
646    ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
647    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
648    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
649    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
650    WaitCause, WriteJournal, WriteSupport, system_clock,
651};
652use reqwest::{Client, StatusCode, Url};
653use schemars::{Schema, schema_for};
654use secrecy::{ExposeSecret, SecretString};
655use serde::{Deserialize, Serialize};
656use serde_json::{Value, json};
657
658pub mod accounting;
659mod assets;
660mod assignees;
661mod batch;
662pub mod change_token;
663mod expected;
664pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
665mod pacing;
666mod projection;
667mod relations;
668mod visibility;
669
670use accounting::Accounting;
671
672/// The registry name for this plugin.
673pub const KIND: &str = "github-projects";
674/// GitHub's maximum connection page size.
675pub const MAX_PAGE_SIZE: u32 = 100;
676/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
677/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
678/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
679pub const SEARCH_PAGE_SIZE: u32 = 20;
680/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
681/// its comments: the largest batch the node-count model prices at one point.
682///
683/// Each aliased item is resolved once, and what GitHub charges for it is the connections
684/// under it — its labels, its page of board memberships, the field values of each of those
685/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
686/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
687/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
688/// one point and fails if one item more would still be priced at one.
689pub const DETAIL_BATCH: usize = 24;
690/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
691///
692/// Each reference asks GitHub for one connection — a page of its board memberships, each with
693/// its `Status` value read by name rather than through a connection of field values — so fifty
694/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
695/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
696/// issues per source. `tests/point_cost.rs` prices the document at one point.
697pub const REFERENCE_BATCH: usize = 50;
698/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
699/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
700/// point.
701///
702/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
703/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
704/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
705/// if one item more would still be priced at one.
706pub const COPY_DETAIL_BATCH: usize = 21;
707/// How many mutations one aliased request of a copy's creates, closes and updates carries:
708/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
709///
710/// Eleven is what GitHub was seen to accept in one request — the timing spike
711/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
712/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
713/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
714pub const MUTATION_BATCH: usize = 11;
715
716/// The most people one lookup ([`graphql::user_ids`]) asks for: GitHub's documented limit of
717/// [`NODE_COUNT_LIMIT`] nodes a request, each `user(login:)` field being one. Every person a
718/// call names that this process has not resolved is looked up in one request below it — so a
719/// call is split only past it.
720#[expect(
721    clippy::cast_possible_truncation,
722    reason = "500,000 fits every target's usize"
723)]
724pub const PEOPLE_PER_LOOKUP: usize = NODE_COUNT_LIMIT as usize;
725
726/// The most relation writes — `addSubIssue`, `removeSubIssue`, `addRelatesTo`,
727/// `removeRelatesTo` — one request carries: a 60-alias `addRelatesTo` answered HTTP 504 after
728/// landing 58 of its 60 (`spike-relations`), and 30 were accepted every time.
729pub const RELATION_BATCH: usize = 30;
730/// How many aliased writes one request of a copy's board field values and blocked-by edges
731/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
732/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
733///
734/// Nineteen is the most aliases the feasibility spike
735/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
736/// saw GitHub price at the one point every mutation request costs; no larger request was
737/// established, so a copy holding more is split.
738pub const WRITE_BATCH: usize = 19;
739/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
740/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
741/// of five succeeded (the feasibility spike's undo-batching run).
742pub const DELETE_BATCH: usize = 5;
743/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
744/// plan and the project's document, and no more, since each carries a page of the board's field
745/// filter and one of the issue search.
746pub const ORIGIN_BATCH: usize = 10;
747/// How many searches one [`graphql::SEARCH_BATCH`] request asks: what `task find` asks of a
748/// board in one call, each search an aliased `search` field of its own.
749pub const SEARCH_BATCH: usize = 20;
750/// How many origins one [`graphql::SEARCH_BATCH`] request looks up beside its searches, each by
751/// the board's own field filter in an aliased `items` field of its own and by one of the
752/// request's searches.
753pub const SEARCH_BATCH_ORIGINS: usize = 5;
754/// The rows each search of a [`graphql::SEARCH_BATCH`] request asks for on its first page.
755///
756/// A batched find asks narrowed questions that each expect a handful of answers, and every
757/// search slot is priced at its page size: three rows a slot keep a first round of
758/// [`SEARCH_BATCH`] searches at three points, where a page of [`SEARCH_PAGE_SIZE`] each would
759/// cost twenty. A search with more to answer is walked on at [`SEARCH_PAGE_SIZE`] rows a page.
760pub const SEARCH_BATCH_FIRST_PAGE: u32 = 3;
761
762/// The rows each resumed page of one [`graphql::SEARCH_BATCH`] round asks for, when `resumed`
763/// of its slots — searches and origin item listings alike — resume from a cursor.
764///
765/// Every resumed page of a round shares one [`SEARCH_PAGE_SIZE`] of rows, rather than each
766/// asking that many: a round in which all twenty searches walked on at twenty rows a slot cost
767/// 22 points in one request, while sharing holds every round to what its first pages cost and
768/// one page more. A search with more to answer is walked on over more rounds, each page still
769/// read, so what a walk answers is unchanged; a round of one resumed search asks the page a
770/// search alone asks.
771#[must_use]
772pub fn search_batch_resumed_page(resumed: usize) -> u32 {
773    let resumed = u32::try_from(resumed.max(1)).unwrap_or(u32::MAX);
774    (SEARCH_PAGE_SIZE / resumed).max(1)
775}
776
777/// The page sizes of the most expensive [`graphql::SEARCH_BATCH`] round this source sends:
778/// every slot on its first page, at [`SEARCH_BATCH_FIRST_PAGE`] rows, but one resumed slot
779/// asking all of [`search_batch_resumed_page`]'s shared rows.
780///
781/// `tests/point_cost.rs` holds every round shape [`search_batch_resumed_page`] allows to cost
782/// no more than this, and the credentialed lane reconciles GitHub's own price of the document
783/// under it. [`largest_page_sizes`] still binds every slot at [`SEARCH_PAGE_SIZE`], an upper
784/// bound on this, so the offline worst case covers more than any round sends.
785#[must_use]
786pub fn search_batch_largest_round() -> Variables {
787    let mut sizes = largest_page_sizes();
788    for slot in 0..SEARCH_BATCH {
789        sizes.insert(format!("first{slot}"), SEARCH_BATCH_FIRST_PAGE);
790    }
791    for slot in 0..SEARCH_BATCH_ORIGINS {
792        sizes.insert(format!("itemsFirst{slot}"), SEARCH_BATCH_FIRST_PAGE);
793    }
794    sizes.insert("first0".to_owned(), search_batch_resumed_page(1));
795    sizes
796}
797/// How many comments one [`graphql::ADD_COMMENTS`] request adds, and how many issue updates one
798/// [`graphql::WRITE_TASKS`] request carries: [`MUTATION_BATCH`], the most aliased mutations
799/// GitHub was seen to accept in one request.
800pub const COMMENT_BATCH: usize = MUTATION_BATCH;
801/// How many board field values one [`graphql::WRITE_TASKS`] request carries, beside
802/// [`MUTATION_BATCH`] field clears: [`WRITE_BATCH`].
803pub const TASK_FIELD_BATCH: usize = WRITE_BATCH;
804
805/// The most nodes any one document this source sends may be asked to return.
806///
807/// GitHub's own published per-query ceiling, taken from
808/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
809/// workspace cannot hold a stale copy of somebody else's number. A query above it is
810/// **refused before it is executed**, whoever is asking and whatever board they are
811/// asking about — so this is a bound on the documents rather than a budget that runs out.
812///
813/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
814/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
815/// everything the credential does — two numbers against two limits, and this constant
816/// bounds only the first. The second is computed offline too, per document:
817/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
818/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
819/// lane. There is no constant like this one to hold a price under, because points are an
820/// hourly allowance rather than a per-call bound.
821///
822/// Neither is a session's price. What `session-cost.md` records of a whole session is its
823/// **requests** and its **worst-case nodes**; what a whole session spends in points is
824/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
825/// [`accounting`]. The module section on the three ways this source reaches an item says how
826/// the count is arrived at, and which of the page sizes below decide it.
827pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
828
829/// Nested connection size for the connections that hang off one item.
830///
831/// It multiplies through every document that reaches an item under a page — the count
832/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
833/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
834/// every document under these constants and fails naming any that reaches the limit, so
835/// raising this is caught there rather than by GitHub.
836const NESTED_PAGE_SIZE: u32 = 50;
837/// How many of one issue's board memberships are read when an issue is reached directly.
838///
839/// An issue reached through a search or through its own node id carries its board half in
840/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
841/// under a page of issues, so every point of it multiplies through the whole document and
842/// is paid for whether or not any issue is on a second board — which is why it is
843/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
844///
845/// **Three, because what a page misses is now recovered rather than refused**, and the
846/// recovery is what the value is chosen against. An issue whose entry for this board sits
847/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
848/// that page's own cursor — so the value trades a bound every read pays for a request only
849/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
850/// boards would pay that request *per issue*, which is order N against the one page per
851/// hundred issues a read costs today. At three it is only reached by an issue on four or
852/// more boards at once, which keeps the recovery path exceptional rather than routine for
853/// a plausible deployment.
854const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
855/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
856/// its two connections for.
857///
858/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
859/// second is a duplicate a copy already takes the first of. Both connections are walked to
860/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
861/// never what the answer is. It is small because every point of it is paid on every lookup,
862/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
863/// worst-case nodes than the one whole-board read they replaced.
864const ORIGIN_PAGE_SIZE: u32 = 3;
865/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
866/// connections for: one, the one carrier an origin names.
867///
868/// A second carrier still shows: the page then says there is more, and that origin is left to
869/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
870/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
871/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
872/// origins at two points with a first page of three and at one with a first page of one.
873const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
874
875/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
876/// one point buys, since every row carries six connections GitHub charges for once per row.
877/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
878/// fails if one row more would still be priced at one.
879pub const MEMBER_PAGE_SIZE: u32 = 23;
880
881pub use github_graphql_node_count::{NodeCountError, Variables};
882
883/// The largest value this source can bind to each page-size variable its documents name.
884///
885/// Every `first:` in [`graphql`] reads one of these seven, and each is capped at the
886/// constant above it wherever a caller's own limit could reach it — `$first` at
887/// [`MAX_PAGE_SIZE`], `$commentFirst` at [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
888/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
889/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
890/// not one configuration of it, which is what makes a bound computed under it a bound on
891/// every read.
892pub fn largest_page_sizes() -> Variables {
893    let mut sizes = Variables::from([
894        ("first".to_owned(), MAX_PAGE_SIZE),
895        ("commentFirst".to_owned(), MAX_PAGE_SIZE),
896        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
897        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
898        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
899        ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
900        ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
901    ]);
902    // Each slot of the batched search pages at most what one search does.
903    for slot in 0..SEARCH_BATCH {
904        sizes.insert(format!("first{slot}"), SEARCH_PAGE_SIZE);
905    }
906    for slot in 0..SEARCH_BATCH_ORIGINS {
907        sizes.insert(format!("itemsFirst{slot}"), SEARCH_PAGE_SIZE);
908    }
909    sizes
910}
911
912/// The most nodes `document` could be asked to return, by GitHub's published rules.
913///
914/// Computed offline from the document's own text under [`largest_page_sizes`] — no
915/// network, no credential and no schema — by
916/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
917/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
918/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
919///
920/// # Errors
921///
922/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
923/// no single operation, or binds a page size this source does not name — each of which is
924/// a defect in the document rather than a number.
925pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
926    node_count(document, &largest_page_sizes())
927}
928
929/// The most rate-limit points one call of `document` could spend, by GitHub's published
930/// rules.
931///
932/// Computed offline from the document's own text under [`largest_page_sizes`] — no
933/// network, no credential and no schema — by
934/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
935/// This is `cost`, metered **per hour** against the allowance one credential shares across
936/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
937/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
938/// under, so what `tests/point_cost.rs` does with it is pin every document in
939/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
940/// figures against GitHub's own reported `cost`.
941///
942/// # Errors
943///
944/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
945/// no single operation, or binds a page size this source does not name — each of which is
946/// a defect in the document rather than a number.
947pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
948    github_graphql_node_count::point_cost(document, &largest_page_sizes())
949}
950
951/// The rate-limit points one call of `document` spends under `variables`, by GitHub's published
952/// rules: [`worst_case_point_cost`] is this under [`largest_page_sizes`], and a request this
953/// source sent is priced by it under the page sizes that request really bound — what GitHub
954/// charges the request, since it charges every connection at the page size asked for.
955///
956/// # Errors
957///
958/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
959/// no single operation, or binds a page size `variables` does not name.
960pub fn point_cost(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
961    github_graphql_node_count::point_cost(document, variables)
962}
963
964/// The most nodes `document` could be asked to return under `variables`.
965///
966/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
967/// [`accounting`] is this under the bindings one request really sent — one spelling of the
968/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
969/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
970///
971/// # Errors
972///
973/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
974/// no single operation, or binds a page size `variables` does not name.
975pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
976    github_graphql_node_count::node_count(document, variables)
977}
978
979/// `variables` with the page sizes and flags a token twin's inline issue selection binds,
980/// at a read's page sizes.
981fn token_variables(mut variables: Value) -> Value {
982    if let Some(object) = variables.as_object_mut() {
983        object.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
984        object.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
985        object.insert("duplicates".to_owned(), json!(true));
986    }
987    variables
988}
989
990/// The input members a mutation names the issue or board item it writes by.
991const WRITTEN_NAMES: [&str; 4] = ["id", "issueId", "subjectId", "itemId"];
992
993tokio::task_local! {
994    /// The source a targeted update is running on, by its address, while it runs: what has its
995    /// mutations sent as the twins selecting the whole issue, so the update answers the version
996    /// its own last response shows (see `GitHubProjectsSource::written_tokens`).
997    static SPELLING: usize;
998}
999
1000/// The issue-title prefix that makes a board issue a document.
1001///
1002/// A GitHub Projects board has no document type — it holds issues — so the discriminator
1003/// is the title, and this is the whole of it: an issue whose title begins with these bytes
1004/// is a document and every other issue is the task or project the sub-issue rule makes it.
1005///
1006/// It is spelled **once**, here, and read rather than restated everywhere else — including
1007/// by the shared journeys, which take it from this constant so a board fixture cannot
1008/// drift from what this source reads. `docs/metadata.md` records the two consequences that
1009/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
1010/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
1011/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
1012pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
1013
1014/// The most characters GitHub stores in one issue's body: a longer one is refused as
1015/// `body is too long (maximum is 65536 characters)`.
1016pub const BODY_LIMIT: u64 = 65_536;
1017
1018/// The address a new upload's URL is spelled at, as GitHub serves a user attachment: what the
1019/// size of a body is measured with before an asset of it is uploaded, a UUID long.
1020const PREDICTED_ATTACHMENT_URL: &str =
1021    "https://github.com/user-attachments/assets/00000000-0000-0000-0000-000000000000";
1022
1023/// Exact GraphQL query documents issued by this plugin.
1024///
1025/// Keeping the production documents here lets the pinned-schema test validate the same
1026/// bytes that are sent to GitHub, rather than a test-only copy which could drift
1027/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
1028/// field, and its guarded caller always supplies the complete existing option set with ids.
1029pub mod graphql {
1030    /// The board half of one item: the field values every document here reads it from.
1031    ///
1032    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
1033    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
1034    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
1035    /// *the same value*, because
1036    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
1037    /// one path. Three spellings of it is what would drift, so there is one.
1038    ///
1039    /// The `Status` option and this source's own origin text field are the whole of it. It
1040    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
1041    /// content, so it holds nothing the content's own `labels` do not already say, and it
1042    /// would sit a label connection two page sizes deep.
1043    macro_rules! board_item_values {
1044        () => {
1045            r#"fieldValues(first:$nestedFirst){nodes{
1046          ... on ProjectV2ItemFieldSingleSelectValue{name field{
1047            ... on ProjectV2SingleSelectField{id name options{id name}}
1048          }}
1049          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
1050        }pageInfo{hasNextPage}}"#
1051        };
1052    }
1053
1054    /// The variable every document reading an issue declares for the relationship events its
1055    /// change token counts, with its value as the default, so no caller binds it.
1056    ///
1057    /// A variable rather than a literal list because every argument a production document
1058    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
1059    /// its default to the pinned enum. Its default is the six event types GitHub records a
1060    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
1061    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
1062    /// project membership on the task, as its parent, which GitHub records there whichever
1063    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
1064    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
1065    /// decide. [`super::change_token`] says why the count is in the token.
1066    macro_rules! relationship_events_variable {
1067        () => {
1068            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
1069        };
1070    }
1071
1072    /// Everything this source reads about one issue, wherever it reaches that issue.
1073    ///
1074    /// A macro rather than a constant so the three documents below can `concat!` it: one
1075    /// spelling of these fields is what makes an issue read through the board-scoped
1076    /// search, through its own node id, and through its project's sub-issue relationship
1077    /// resolve to *the same* item, which is the whole of what
1078    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
1079    ///
1080    /// `projectItems` is what carries the board half of an issue: the board item's own id
1081    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
1082    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
1083    /// issue rather than on the board, which is what makes the cost of a read proportional
1084    /// to what was asked for instead of to the board's size.
1085    ///
1086    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
1087    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
1088    /// not on that page: a page here is where the search for the entry starts rather than
1089    /// where it ends.
1090    ///
1091    /// It does **not** select the board's `Labels` field value, and that is the whole of
1092    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
1093    /// a label connection there sits under `fieldValues` under `projectItems` under a page
1094    /// of issues, spending `$nestedFirst` twice down one path, and took
1095    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
1096    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
1097    /// above, and that connection is where every label this source reports comes from. No
1098    /// document in this module selects the board field any longer, [`BOARD`] included; the
1099    /// module documentation records why nothing it could have held is lost.
1100    macro_rules! board_issue {
1101        () => {
1102            concat!(
1103                r#" fragment BoardIssue on Issue{"#,
1104                board_issue_fields!(),
1105                "}"
1106            )
1107        };
1108    }
1109
1110    /// What [`board_issue!`] selects of an issue, written out where a document selects it
1111    /// inline rather than through the fragment.
1112    macro_rules! board_issue_fields {
1113        () => {
1114            concat!(
1115                r#"__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}
1116      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
1117      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1118        "#,
1119                board_item_values!(),
1120                r#"}pageInfo{hasNextPage endCursor}}"#
1121            )
1122        };
1123    }
1124
1125    /// One page of a repository's issues, newest first, each with when it was created and its
1126    /// body: what a retried copy reads to find the issue its earlier attempt created, by the
1127    /// create key in that body's metadata slot — never through search, whose index lags.
1128    pub const ISSUES_BY_CREATION: &str = "query($owner:String!,$name:String!,$first:Int!,$after:String){repository(owner:$owner,name:$name){issues(first:$first,after:$after,orderBy:{field:CREATED_AT,direction:DESC}){nodes{id createdAt body}pageInfo{hasNextPage endCursor}}}}";
1129    /// Every issue of one board, found by a search scoped to that board.
1130    ///
1131    /// This is how the projects a board holds are listed, and it selects no `items`
1132    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
1133    /// container walked page by page, so nothing nested inside a board item is paid for.
1134    /// Which of the issues it returns is a project is then read off `parent` — GitHub
1135    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
1136    /// discriminator has to be applied to the field, which is a scalar on the issue and
1137    /// costs nothing.
1138    pub const SEARCH_ISSUES: &str = concat!(
1139        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1140        relationship_events_variable!(),
1141        r#"){
1142      search(query:$search,type:$type,first:$first,after:$after){
1143        pageInfo{hasNextPage endCursor}
1144        nodes{__typename ...BoardIssue}
1145      }
1146    }"#,
1147        board_issue!()
1148    );
1149
1150    /// What a dependency read selects of each far end: enough to say which kind of item it
1151    /// is, its body included for the kind marker.
1152    macro_rules! related_issue {
1153        () => {
1154            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1155        };
1156    }
1157
1158    /// One issue by its own node id, which is what a qualified id names here — with what a
1159    /// write of it needs and the issue does not carry in `board_issue!`: the field
1160    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1161    ///
1162    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1163    /// answers a write made moments ago with the value from before it, and resolving a node
1164    /// id does not.
1165    ///
1166    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1167    /// reads it by its own id, and with them that one read answers everything the write
1168    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1169    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1170    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1171    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1172    /// they sit under one item, and this read is still one point.
1173    pub const ISSUE: &str = concat!(
1174        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1175        relationship_events_variable!(),
1176        r#"){
1177      node(id:$id){__typename ...BoardIssue ... on Issue{
1178        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1179          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1180          ... on ProjectV2Field{__typename id name dataType}
1181        }pageInfo{hasNextPage}}}}}
1182        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1183      }}
1184    }"#,
1185        board_issue!(),
1186        related_issue!()
1187    );
1188
1189    /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1190    /// under and that project's members beside it: the project issue whole, and a page of its
1191    /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1192    ///
1193    /// What a guarded write of a document's metadata reads, and what reading a document by its
1194    /// id is: a write to a board the boundary screens is held to who may read the document,
1195    /// which is the project's and every member's classification as much as its own, and here
1196    /// the one request that holds the version the write is compared against answers all of it.
1197    /// Its members carry no edges, which nothing classifying them reads.
1198    pub const DOCUMENT_IN_PROJECT: &str = concat!(
1199        r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1200        relationship_events_variable!(),
1201        r#"){
1202      node(id:$id){__typename ...BoardIssue ... on Issue{
1203        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1204          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1205          ... on ProjectV2Field{__typename id name dataType}
1206        }pageInfo{hasNextPage}}}}}
1207        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1208        project:parent{__typename ...BoardIssue
1209          subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1210        }
1211      }}
1212    }"#,
1213        board_issue!(),
1214        related_issue!()
1215    );
1216
1217    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1218    /// what blocks it.
1219    ///
1220    /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1221    /// one point buys — rather than at `$first`, because every row carries six connections and
1222    /// a hundred-row page cost six points to list a project of ten.
1223    ///
1224    /// The work this costs is the project's own size. Nothing about it grows as the board
1225    /// gains projects, or as those projects gain tasks.
1226    ///
1227    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1228    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1229    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1230    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1231    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1232    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1233    /// It adds a connection under each issue of the page — one rate-limit point per page,
1234    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1235    /// answer questions that never read an edge.
1236    ///
1237    /// **The project issue itself rides here too**, with the page of what blocks it, because
1238    /// the reader of a project's tasks reads the project next: a copy writing the project reads
1239    /// it and its edges before it writes them. Carried on the request that already names the
1240    /// project, the first page answers that read and the dependency read after it, at the
1241    /// state GitHub answered in this command, rather than spending one more read on an item
1242    /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1243    /// copy's read of a task this listing carried is answered by it rather than sent again.
1244    /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1245    pub const SUB_ISSUES: &str = concat!(
1246        r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1247        relationship_events_variable!(),
1248        r#"){
1249      node(id:$id){__typename ...BoardIssue
1250        ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1251          subIssues(first:$memberFirst,after:$after){
1252          pageInfo{hasNextPage endCursor}
1253          nodes{__typename ...BoardIssue ... on Issue{
1254            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1255          }}
1256        }}}
1257    }"#,
1258        board_issue!(),
1259        related_issue!()
1260    );
1261
1262    /// Everything this source reads about one issue comment, wherever it reaches one.
1263    ///
1264    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1265    /// and a comment just edited are handed to one mapper, so they are selected by one
1266    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1267    /// longer exists, and `login` is the one member every kind of actor carries; `__typename`
1268    /// says whether it is a `Bot`, whose `login` GitHub's own pages spell with `[bot]` after it.
1269    macro_rules! issue_comment {
1270        () => {
1271            "id author{__typename login} createdAt updatedAt body url"
1272        };
1273    }
1274
1275    /// What a read of the board's own `items` selects of each item's content.
1276    ///
1277    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1278    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1279    /// content by one spelling.
1280    macro_rules! board_item_content {
1281        () => { board_item_content!("") };
1282        ($comments:expr) => {
1283            concat!(r#" content{
1284        ... 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}}"#, $comments, r#"}
1285        ... on PullRequest{__typename id}
1286        ... on DraftIssue{__typename id title body createdAt updatedAt}
1287      }"#)
1288        };
1289    }
1290
1291    macro_rules! board_listing {
1292        ($variables:expr, $comments:expr) => { concat!(
1293        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1294        relationship_events_variable!(), $variables,
1295        r#"){
1296      owner:repositoryOwner(login:$owner){
1297        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1298      }
1299    } fragment Board on ProjectV2 { id title
1300      fields(first:$nestedFirst){nodes{
1301        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1302        ... on ProjectV2Field{__typename id name dataType}
1303      }pageInfo{hasNextPage}}
1304      items(first:$first,after:$after){nodes{id updatedAt "#,
1305        board_item_values!(),
1306        board_item_content!($comments),
1307        r#"} pageInfo{hasNextPage endCursor}}
1308    }"#
1309    ) };
1310    }
1311
1312    /// Reads the board's fields and one page of its items.
1313    pub const BOARD: &str = board_listing!("", "");
1314
1315    /// A board page carrying each issue's first comment page in the same request.
1316    pub const BOARD_WITH_COMMENTS: &str = concat!(
1317        board_listing!(
1318            ",$commentFirst:Int!",
1319            concat!(
1320                "comments(first:$commentFirst){nodes{",
1321                issue_comment!(),
1322                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}"
1323            )
1324        ),
1325        related_issue!()
1326    );
1327
1328    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1329    /// the board.
1330    ///
1331    /// **`originItems`** is the board's own items narrowed by its own field filter —
1332    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1333    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1334    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1335    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1336    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1337    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1338    /// fresh `addProjectV2ItemById` the way that connection does.
1339    ///
1340    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1341    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1342    /// indexes that comment, and the index catches up with a write in a second or two rather
1343    /// than in minutes, so it finds a carrier another process wrote that the first read is
1344    /// still behind on.
1345    ///
1346    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1347    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1348    /// from its last cursor, which answers an empty page. Every candidate either read returns
1349    /// is confirmed against its own origin field before it is reported, so a token match of
1350    /// the search or anything else the filter admits never is.
1351    ///
1352    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1353    /// own whole reads counts this one among them.
1354    pub const ORIGIN_LOOKUP: &str = concat!(
1355        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1356        relationship_events_variable!(),
1357        r#"){
1358      originItems:repositoryOwner(login:$owner){
1359        ... on ProjectV2Owner{projectV2(number:$number){
1360          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1361        board_item_values!(),
1362        board_item_content!(),
1363        r#"} pageInfo{hasNextPage endCursor}}
1364        }}
1365      }
1366      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1367        pageInfo{hasNextPage endCursor}
1368        nodes{__typename ...BoardIssue}
1369      }
1370    }"#,
1371        board_issue!()
1372    );
1373
1374    /// Every closed item of the board, by the board's own field filter — what
1375    /// `task status reconcile` reads, and nothing else of the board.
1376    ///
1377    /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1378    /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1379    /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1380    /// closed, with a reason and a card that contradict each other, from the item node it
1381    /// already carries, so a filter that also let open items through would cost reads and
1382    /// write nothing wrong.
1383    ///
1384    /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1385    /// option writes that follow need no read of the board. The root is aliased `closedItems`
1386    /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1387    /// among them.
1388    pub const CLOSED_ITEMS: &str = concat!(
1389        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1390        relationship_events_variable!(),
1391        r#"){
1392      closedItems:repositoryOwner(login:$owner){
1393        ... on ProjectV2Owner{projectV2(number:$number){id
1394          fields(first:$nestedFirst){nodes{
1395            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1396            ... on ProjectV2Field{__typename id name dataType}
1397          }pageInfo{hasNextPage}}
1398          items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1399        board_item_values!(),
1400        board_item_content!(),
1401        r#"} pageInfo{hasNextPage endCursor}}
1402        }}
1403      }
1404    }"#
1405    );
1406
1407    /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1408    macro_rules! reference_alias {
1409        ($n:literal) => {
1410            concat!(
1411                "\n      r",
1412                stringify!($n),
1413                ":repository(owner:$owner",
1414                stringify!($n),
1415                ",name:$name",
1416                stringify!($n),
1417                "){issue(number:$number",
1418                stringify!($n),
1419                "){...HeldIssue}}"
1420            )
1421        };
1422    }
1423
1424    /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1425    /// number — the one read `task status complete` makes of a source, and nothing else of
1426    /// the board: no `items`, no search and no project read.
1427    ///
1428    /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1429    /// references one point.** It selects no labels, and of each board membership only the
1430    /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1431    /// connection, its page of memberships, where the whole fragment asks for five. What a
1432    /// completion writes is a card and, at most, the issue's state, so the labels and the other
1433    /// field values it leaves out are never what it needs; an item read this way is held for
1434    /// that write alone and never answers another read. An issue whose entry for this board is
1435    /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1436    /// every other read of an issue recovers one.
1437    ///
1438    /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1439    /// fewer references binds the slots it has none for to its last one.
1440    pub const ISSUES_BY_REFERENCE: &str = concat!(
1441        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!"#,
1442        relationship_events_variable!(),
1443        r#"){"#,
1444        reference_alias!(0),
1445        reference_alias!(1),
1446        reference_alias!(2),
1447        reference_alias!(3),
1448        reference_alias!(4),
1449        reference_alias!(5),
1450        reference_alias!(6),
1451        reference_alias!(7),
1452        reference_alias!(8),
1453        reference_alias!(9),
1454        reference_alias!(10),
1455        reference_alias!(11),
1456        reference_alias!(12),
1457        reference_alias!(13),
1458        reference_alias!(14),
1459        reference_alias!(15),
1460        reference_alias!(16),
1461        reference_alias!(17),
1462        reference_alias!(18),
1463        reference_alias!(19),
1464        reference_alias!(20),
1465        reference_alias!(21),
1466        reference_alias!(22),
1467        reference_alias!(23),
1468        reference_alias!(24),
1469        reference_alias!(25),
1470        reference_alias!(26),
1471        reference_alias!(27),
1472        reference_alias!(28),
1473        reference_alias!(29),
1474        reference_alias!(30),
1475        reference_alias!(31),
1476        reference_alias!(32),
1477        reference_alias!(33),
1478        reference_alias!(34),
1479        reference_alias!(35),
1480        reference_alias!(36),
1481        reference_alias!(37),
1482        reference_alias!(38),
1483        reference_alias!(39),
1484        reference_alias!(40),
1485        reference_alias!(41),
1486        reference_alias!(42),
1487        reference_alias!(43),
1488        reference_alias!(44),
1489        reference_alias!(45),
1490        reference_alias!(46),
1491        reference_alias!(47),
1492        reference_alias!(48),
1493        reference_alias!(49),
1494        "\n    }",
1495        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}
1496      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1497        status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1498          ... on ProjectV2SingleSelectField{id name options{id name}}
1499        }}}
1500      }pageInfo{hasNextPage endCursor}}}"#
1501    );
1502
1503    /// The board's own id and field definitions, and not one of its items.
1504    ///
1505    /// What a write needs of the board when the item it writes does not say: the id a field
1506    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1507    /// origin fields. It selects no `items`, so what it costs is the board's field list
1508    /// however many items the board holds — and it decides nothing about which items those
1509    /// are, which is the question a read of one item by its own id answers instead.
1510    ///
1511    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1512    /// board's item reads by their root counts this one among them.
1513    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1514      boardFields:repositoryOwner(login:$owner){
1515        ... on ProjectV2Owner{projectV2(number:$number){id
1516          fields(first:$nestedFirst){nodes{
1517            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1518            ... on ProjectV2Field{__typename id name dataType}
1519          }pageInfo{hasNextPage}}
1520        }}
1521      }
1522    }"#;
1523
1524    /// One board draft by its own node id, with the board item it sits in.
1525    ///
1526    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1527    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1528    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1529    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1530    /// board listing hands it to, and nothing has to list the board to find one.
1531    pub const DRAFT: &str = concat!(
1532        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1533      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1534        projectV2Items(first:$boardItems){nodes{id project{id number}
1535        "#,
1536        board_item_values!(),
1537        r#"}pageInfo{hasNextPage endCursor}}}}
1538    }"#
1539    );
1540
1541    /// One issue's board memberships alone, walked past the page a read of it carried.
1542    ///
1543    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1544    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1545    /// boards than that page holds may have this board's entry past its end. This asks that
1546    /// one issue for its memberships and nothing else — the caller already holds the issue —
1547    /// so an answer of "this board does not hold it" is only ever given about a connection
1548    /// read to exhaustion.
1549    ///
1550    /// It selects the board item's id, its project number and the same
1551    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1552    /// very same resolver: an issue recovered this way reports the same title, the same
1553    /// status, the same labels and the same qualified id as one whose entry was on the
1554    /// page.
1555    ///
1556    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1557    /// multiplies through it and the membership connection can be walked at
1558    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1559    /// further request for any issue a person really keeps.
1560    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1561        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1562      node(id:$id){
1563        ... on Issue{projectItems(first:$first,after:$after){
1564          nodes{id updatedAt project{id number}
1565        "#,
1566        board_item_values!(),
1567        r#"}
1568          pageInfo{hasNextPage endCursor}}}
1569      }
1570    }"#
1571    );
1572    /// Whether the board's Project is public — half of what decides whether a write here can
1573    /// be read by anybody. Needs the `read:project` scope.
1574    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1575    /// Resolves the configured repository's node id, which creating an issue requires.
1576    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1577    /// What creating an issue needs and has not read yet: the board's own id and field
1578    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1579    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1580    ///
1581    /// Sent at the point a create knows which repository it is for, when neither half is
1582    /// already known to this process; a create needing only one of them sends that one's own
1583    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1584    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1585    /// status.
1586    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1587      boardFields:repositoryOwner(login:$owner){
1588        ... on ProjectV2Owner{projectV2(number:$number){id
1589          fields(first:$nestedFirst){nodes{
1590            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1591            ... on ProjectV2Field{__typename id name dataType}
1592          }pageInfo{hasNextPage}}
1593        }}
1594      }
1595      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1596    }"#;
1597    /// Reads both dependency directions for one issue, with each far end's own kind — and
1598    /// the issue's own body, which is where an edge to another source is recorded, so that
1599    /// half of a dependency read needs no second read of the issue or of the board.
1600    pub const ISSUE_DEPENDENCIES: &str = concat!(
1601        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1602      ... on Issue{body
1603        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1604        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1605      }}}"#,
1606        related_issue!()
1607    );
1608    /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1609    /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1610    /// along because its metadata slot is where an edge to another source is recorded — and a
1611    /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1612    /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1613    /// and only when that item is this board's: no draft read and no board read follow it.
1614    ///
1615    /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1616    /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1617    /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1618    /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1619    /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1620    /// one point; the credentialed lane's reconciliation is what holds the two to that.
1621    pub const ISSUE_DEPENDS_ON: &str = concat!(
1622        r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1623      node(id:$id){__typename
1624      ... on Issue{body
1625        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1626      }
1627      ... on DraftIssue{id body}}
1628      draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1629      board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1630        related_issue!()
1631    );
1632    /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1633    /// the reverse of a recorded edge is derived from the far end, never written down here.
1634    pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1635        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1636      ... on Issue{
1637        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1638      }}}"#,
1639        related_issue!()
1640    );
1641    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1642    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1643    /// `parentIssueId`, so this one request files it under both — see
1644    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1645    pub const CREATE_ISSUE: &str =
1646        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1647    /// Puts an existing issue on the configured board.
1648    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1649    /// Updates an issue's visible fields and its open or closed state in one call.
1650    pub const UPDATE_ISSUE: &str =
1651        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1652    /// Updates an existing draft's user-visible fields.
1653    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1654    /// Updates a text or single-select value on one project item.
1655    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}}}}}}}}"#;
1656    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1657    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1658    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1659    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}}}"#;
1660    /// Clears one project item's value of one field, which is what a `none` priority is.
1661    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}}}}}}}}"#;
1662    /// Creates one field: a single-select one with its options, or a text one for a projected
1663    /// metadata value. Only the guarded field setup may use this document, and only for a
1664    /// field the board lacks.
1665    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1666    /// Replaces a single-select field's options. Only the guarded field setup — the
1667    /// `status-options` and `fields` operations — may use this document, because GitHub
1668    /// treats the input as the complete option list.
1669    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1670    /// A fresh snapshot of the Status field and every board item's assignment.
1671    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}}}}}}"#;
1672    /// Files one issue under another as a sub-issue, which is what project membership is.
1673    pub const ADD_SUB_ISSUE: &str =
1674        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1675    /// Takes one issue back out of its parent.
1676    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1677    /// Adds GitHub's native issue blocked-by relationship.
1678    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1679    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1680    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1681    macro_rules! origin_lookup_alias {
1682        ($n:literal) => {
1683            concat!(
1684                "\n      o",
1685                stringify!($n),
1686                ":repositoryOwner(login:$owner) @include(if:$ask",
1687                stringify!($n),
1688                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1689                stringify!($n),
1690                "){nodes{id updatedAt ",
1691                board_item_values!(),
1692                board_item_content!(),
1693                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1694                stringify!($n),
1695                ":search(query:$search",
1696                stringify!($n),
1697                ",type:$type,first:$lookupFirst) @include(if:$ask",
1698                stringify!($n),
1699                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1700            )
1701        };
1702    }
1703
1704    /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1705    /// selected beside them: one spelling of the batched lookup for the documents that send it.
1706    macro_rules! origin_lookups {
1707        ($extra_variables:literal, $extra_fields:literal) => {
1708            concat!(
1709                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!"#,
1710                relationship_events_variable!(),
1711                $extra_variables,
1712                r#"){
1713      boardFields:repositoryOwner(login:$owner){
1714        ... on ProjectV2Owner{projectV2(number:$number){id
1715          fields(first:$nestedFirst){nodes{
1716            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1717            ... on ProjectV2Field{__typename id name dataType}
1718          }pageInfo{hasNextPage}}
1719        }}
1720      }
1721      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1722                $extra_fields,
1723                origin_lookup_alias!(0),
1724                origin_lookup_alias!(1),
1725                origin_lookup_alias!(2),
1726                origin_lookup_alias!(3),
1727                origin_lookup_alias!(4),
1728                origin_lookup_alias!(5),
1729                origin_lookup_alias!(6),
1730                origin_lookup_alias!(7),
1731                origin_lookup_alias!(8),
1732                origin_lookup_alias!(9),
1733                "\n    }",
1734                board_issue!()
1735            )
1736        };
1737    }
1738
1739    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1740    /// origins a copy is about to look for, and everything creating an item then reads —
1741    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1742    /// id and the numeric id of the repository an issue is created in — in one request.
1743    ///
1744    /// What a copy reads before it writes anything, so that finding none of its records on the
1745    /// board and creating every one costs this read and the writes. An origin whose page says
1746    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1747    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1748    pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1749
1750    /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1751    /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1752    /// and no connection under it.
1753    ///
1754    /// What a document copy sends where the public boundary screens its write: the document is
1755    /// held to who may read its project, which is its members' classification as much as the
1756    /// project's own, and this request carries both the project — among the origins it asks —
1757    /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1758    /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1759    pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1760        ",$project:ID!,$memberFirst:Int!",
1761        "\n      members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1762    );
1763
1764    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1765    /// whether it is sent.
1766    macro_rules! write_slot {
1767        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1768            concat!(
1769                " ",
1770                $prefix,
1771                stringify!($n),
1772                ":",
1773                $field,
1774                "(input:$",
1775                $prefix,
1776                stringify!($n),
1777                ") @include(if:$send",
1778                stringify!($n),
1779                "){",
1780                $selection,
1781                "}"
1782            )
1783        };
1784    }
1785
1786    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1787    /// and eleven `$send<n>`.
1788    macro_rules! write_variables {
1789        ($prefix:literal, $type:literal) => {
1790            concat!(
1791                "mutation(",
1792                "$",
1793                $prefix,
1794                "0:",
1795                $type,
1796                "!,$",
1797                $prefix,
1798                "1:",
1799                $type,
1800                "!,$",
1801                $prefix,
1802                "2:",
1803                $type,
1804                "!,$",
1805                $prefix,
1806                "3:",
1807                $type,
1808                "!,$",
1809                $prefix,
1810                "4:",
1811                $type,
1812                "!,$",
1813                $prefix,
1814                "5:",
1815                $type,
1816                "!,$",
1817                $prefix,
1818                "6:",
1819                $type,
1820                "!,$",
1821                $prefix,
1822                "7:",
1823                $type,
1824                "!,$",
1825                $prefix,
1826                "8:",
1827                $type,
1828                "!,$",
1829                $prefix,
1830                "9:",
1831                $type,
1832                "!,$",
1833                $prefix,
1834                "10:",
1835                $type,
1836                "!,",
1837                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1838                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1839                "$send10:Boolean!){"
1840            )
1841        };
1842    }
1843
1844    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1845    /// `$field`, each sent only while its `$send<n>` holds.
1846    macro_rules! write_batch {
1847        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1848            concat!(
1849                write_variables!($prefix, $type),
1850                write_slot!($prefix, $field, $selection, 0),
1851                write_slot!($prefix, $field, $selection, 1),
1852                write_slot!($prefix, $field, $selection, 2),
1853                write_slot!($prefix, $field, $selection, 3),
1854                write_slot!($prefix, $field, $selection, 4),
1855                write_slot!($prefix, $field, $selection, 5),
1856                write_slot!($prefix, $field, $selection, 6),
1857                write_slot!($prefix, $field, $selection, 7),
1858                write_slot!($prefix, $field, $selection, 8),
1859                write_slot!($prefix, $field, $selection, 9),
1860                write_slot!($prefix, $field, $selection, 10),
1861                "}"
1862            )
1863        };
1864    }
1865
1866    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1867    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1868    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1869    pub const CREATE_ISSUES: &str = write_batch!(
1870        "create",
1871        "CreateIssueInput",
1872        "createIssue",
1873        "issue{id number url}"
1874    );
1875
1876    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1877    /// the `Status` value of each, read only while `$read<n>` holds.
1878    macro_rules! created_item_alias {
1879        ($n:literal) => {
1880            concat!(
1881                "\n      i",
1882                stringify!($n),
1883                ":node(id:$id",
1884                stringify!($n),
1885                ") @include(if:$read",
1886                stringify!($n),
1887                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1888            )
1889        };
1890    }
1891
1892    /// The board item of each issue a copy created, up to
1893    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1894    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1895    /// answering the create, so its id comes from a read made once every create has been sent.
1896    pub const CREATED_BOARD_ITEMS: &str = concat!(
1897        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!){"#,
1898        created_item_alias!(0),
1899        created_item_alias!(1),
1900        created_item_alias!(2),
1901        created_item_alias!(3),
1902        created_item_alias!(4),
1903        created_item_alias!(5),
1904        created_item_alias!(6),
1905        created_item_alias!(7),
1906        created_item_alias!(8),
1907        created_item_alias!(9),
1908        created_item_alias!(10),
1909        created_item_alias!(11),
1910        created_item_alias!(12),
1911        created_item_alias!(13),
1912        created_item_alias!(14),
1913        created_item_alias!(15),
1914        created_item_alias!(16),
1915        created_item_alias!(17),
1916        created_item_alias!(18),
1917        created_item_alias!(19),
1918        created_item_alias!(20),
1919        "\n    }"
1920    );
1921
1922    /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1923    /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1924    macro_rules! wide_variables {
1925        ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1926            concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1927        };
1928    }
1929    macro_rules! wide_slots {
1930        ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1931            concat!($(
1932                " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1933                ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1934            )+)
1935        };
1936    }
1937
1938    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1939    /// created, each on its own item, in one ordered request.
1940    pub const WRITE_FIELD_VALUES: &str = concat!(
1941        "mutation(",
1942        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1943        "){",
1944        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),
1945        "}"
1946    );
1947
1948    /// A copy's board field values and its blocked-by edges together, up to
1949    /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1950    /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1951    /// What a copy sends when one request of its writes holds both kinds; one holding a single
1952    /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1953    pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1954        "mutation(",
1955        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1956        wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1957        "){",
1958        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),
1959        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),
1960        "}"
1961    );
1962
1963    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1964    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1965    pub const CLOSE_ISSUES: &str =
1966        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1967
1968    /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1969    /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1970    pub const UPDATE_ISSUES: &str =
1971        write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1972
1973    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1974    /// issues it created, in one request.
1975    pub const ADD_BLOCKED_BYS: &str = concat!(
1976        "mutation(",
1977        wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1978        "){",
1979        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),
1980        "}"
1981    );
1982
1983    /// Removes one native issue blocked-by relationship.
1984    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1985    /// Deletes one issue, which takes its board item with it.
1986    ///
1987    /// The engine sends this in one situation only: undoing a copy that could not finish,
1988    /// over the items that same copy created. Deleting the issue removes the board item
1989    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1990    pub const DELETE_ISSUE: &str =
1991        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1992    /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1993    /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1994    /// part way sends to take them back.
1995    pub const DELETE_ISSUES: &str = concat!(
1996        "mutation(",
1997        wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1998        "){",
1999        wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
2000        "}"
2001    );
2002
2003    /// One task's comments: a page of its issue's own `comments` connection.
2004    ///
2005    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
2006    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
2007    /// list every time somebody edited it; left unordered the connection answers in the order
2008    /// the comments were written, which is the order GitHub documents for the same collection
2009    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
2010    /// node count and the caller's own page size is pushed straight down.
2011    pub const ISSUE_COMMENTS: &str = concat!(
2012        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
2013        issue_comment!(),
2014        r#"}pageInfo{hasNextPage endCursor}}}}}"#
2015    );
2016    /// One issue by its own node id, with a page of its comments: what `task show` and a
2017    /// comment listing read, in one request.
2018    ///
2019    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
2020    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
2021    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
2022    /// connection there would multiply through both of those documents' price, and neither
2023    /// needs one.
2024    pub const ISSUE_DETAIL: &str = concat!(
2025        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2026        relationship_events_variable!(),
2027        r#"){
2028      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
2029        issue_comment!(),
2030        r#"}pageInfo{hasNextPage endCursor}}}}
2031    }"#,
2032        board_issue!()
2033    );
2034
2035    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
2036    /// page of its comments when `$comments` asks for them.
2037    macro_rules! issue_details_alias {
2038        ($n:literal) => {
2039            concat!(
2040                "\n      i",
2041                stringify!($n),
2042                ":node(id:$id",
2043                stringify!($n),
2044                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
2045                issue_comment!(),
2046                "}pageInfo{hasNextPage endCursor}}}}"
2047            )
2048        };
2049    }
2050
2051    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
2052    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
2053    ///
2054    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
2055    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
2056    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
2057    /// neither — so every connection under it would be priced at nothing and the pin in
2058    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
2059    /// one-item read the model already prices, so the batch costs what its aliases cost.
2060    ///
2061    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
2062    /// slots it has no item for to the last item it does, and reads that item again; the
2063    /// price is the document's, whatever its variables, so a short batch costs what a full
2064    /// one does and nothing more.
2065    pub const ISSUE_DETAILS: &str = concat!(
2066        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!"#,
2067        relationship_events_variable!(),
2068        r#"){"#,
2069        issue_details_alias!(0),
2070        issue_details_alias!(1),
2071        issue_details_alias!(2),
2072        issue_details_alias!(3),
2073        issue_details_alias!(4),
2074        issue_details_alias!(5),
2075        issue_details_alias!(6),
2076        issue_details_alias!(7),
2077        issue_details_alias!(8),
2078        issue_details_alias!(9),
2079        issue_details_alias!(10),
2080        issue_details_alias!(11),
2081        issue_details_alias!(12),
2082        issue_details_alias!(13),
2083        issue_details_alias!(14),
2084        issue_details_alias!(15),
2085        issue_details_alias!(16),
2086        issue_details_alias!(17),
2087        issue_details_alias!(18),
2088        issue_details_alias!(19),
2089        issue_details_alias!(20),
2090        issue_details_alias!(21),
2091        issue_details_alias!(22),
2092        issue_details_alias!(23),
2093        "\n    }",
2094        board_issue!()
2095    );
2096
2097    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
2098    /// page of its comments and the issues it is blocked by.
2099    macro_rules! issue_copy_details_alias {
2100        ($n:literal) => {
2101            concat!(
2102                "\n      i",
2103                stringify!($n),
2104                ":node(id:$id",
2105                stringify!($n),
2106                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
2107                issue_comment!(),
2108                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
2109            )
2110        };
2111    }
2112
2113    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
2114    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
2115    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
2116    /// at a time, so a copy of a changed task costs this read and nothing else.
2117    ///
2118    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
2119    /// what `task show-many` is priced by and which reads no edge.
2120    ///
2121    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
2122    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
2123    /// project copy, which reads its project and every task at once, knows from this same read
2124    /// which tasks the project holds at the destination, and its orphan check is answered by it
2125    /// rather than by a listing of the project.
2126    pub const ISSUE_COPY_DETAILS: &str = concat!(
2127        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!"#,
2128        relationship_events_variable!(),
2129        r#"){"#,
2130        issue_copy_details_alias!(0),
2131        issue_copy_details_alias!(1),
2132        issue_copy_details_alias!(2),
2133        issue_copy_details_alias!(3),
2134        issue_copy_details_alias!(4),
2135        issue_copy_details_alias!(5),
2136        issue_copy_details_alias!(6),
2137        issue_copy_details_alias!(7),
2138        issue_copy_details_alias!(8),
2139        issue_copy_details_alias!(9),
2140        issue_copy_details_alias!(10),
2141        issue_copy_details_alias!(11),
2142        issue_copy_details_alias!(12),
2143        issue_copy_details_alias!(13),
2144        issue_copy_details_alias!(14),
2145        issue_copy_details_alias!(15),
2146        issue_copy_details_alias!(16),
2147        issue_copy_details_alias!(17),
2148        issue_copy_details_alias!(18),
2149        issue_copy_details_alias!(19),
2150        issue_copy_details_alias!(20),
2151        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
2152        "\n    }",
2153        board_issue!(),
2154        related_issue!()
2155    );
2156
2157    /// Which issue one comment is on, read before that comment is edited or removed.
2158    ///
2159    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
2160    /// comment id given against the wrong task would change a comment on another issue.
2161    pub const COMMENT_ISSUE: &str =
2162        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
2163    /// Adds one comment to an issue, signed as the account the token belongs to.
2164    pub const ADD_COMMENT: &str = concat!(
2165        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
2166        issue_comment!(),
2167        r#"}}}}"#
2168    );
2169    /// Replaces the body of one issue comment.
2170    pub const UPDATE_COMMENT: &str = concat!(
2171        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2172        issue_comment!(),
2173        r#"}}}"#
2174    );
2175    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2176    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2177
2178    /// The variables of the search slots `<n>` of [`SEARCH_BATCH`]: its search, where it
2179    /// resumes, its page size and whether it is asked.
2180    macro_rules! search_batch_variables {
2181        ($($n:literal)+) => {
2182            concat!($(
2183                ",$search", stringify!($n), ":String!,$after", stringify!($n),
2184                ":String,$first", stringify!($n), ":Int!,$ask", stringify!($n), ":Boolean!",
2185            )+)
2186        };
2187    }
2188    /// The search slots `<n>` of [`SEARCH_BATCH`]: [`SEARCH_ISSUES`]'s one connection, aliased
2189    /// `s<n>`, sent only while `$ask<n>` holds.
2190    macro_rules! search_batch_slots {
2191        ($($n:literal)+) => {
2192            concat!($(
2193                "\n      s", stringify!($n), ":search(query:$search", stringify!($n),
2194                ",type:$type,first:$first", stringify!($n), ",after:$after", stringify!($n),
2195                ") @include(if:$ask", stringify!($n),
2196                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}",
2197            )+)
2198        };
2199    }
2200    /// The variables of the origin slots `<n>` of [`SEARCH_BATCH`]: the board's field filter
2201    /// naming the origin, where its items resume, their page size and whether it is asked.
2202    macro_rules! origin_batch_variables {
2203        ($($n:literal)+) => {
2204            concat!($(
2205                ",$filter", stringify!($n), ":String!,$itemsAfter", stringify!($n),
2206                ":String,$itemsFirst", stringify!($n), ":Int!,$origin", stringify!($n),
2207                ":Boolean!",
2208            )+)
2209        };
2210    }
2211    /// The origin slots `<n>` of [`SEARCH_BATCH`]: [`ORIGIN_LOOKUP`]'s board half, the board's
2212    /// own items through its field filter, aliased `o<n>`, sent only while `$origin<n>` holds.
2213    macro_rules! origin_batch_slots {
2214        ($($n:literal)+) => {
2215            concat!($(
2216                "\n      o", stringify!($n), ":repositoryOwner(login:$owner) @include(if:$origin",
2217                stringify!($n),
2218                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$itemsFirst",
2219                stringify!($n), ",after:$itemsAfter", stringify!($n), ",query:$filter",
2220                stringify!($n), "){nodes{id updatedAt ", board_item_values!(),
2221                board_item_content!(), "} pageInfo{hasNextPage endCursor}}}}}",
2222            )+)
2223        };
2224    }
2225
2226    /// Up to [`SEARCH_BATCH`](super::SEARCH_BATCH) board-scoped issue searches in one request,
2227    /// each [`SEARCH_ISSUES`]'s connection in an aliased `s<n>` field with its own query, page
2228    /// size and cursor, and beside them up to
2229    /// [`SEARCH_BATCH_ORIGINS`](super::SEARCH_BATCH_ORIGINS) origin lookups' board halves —
2230    /// [`ORIGIN_LOOKUP`]'s `items` filtered by the origin field — in aliased `o<n>` fields, each
2231    /// origin's issue search in a search slot of its own.
2232    ///
2233    /// What `task find` asks a board, every search a page at a time: one request a round,
2234    /// rather than one per search. A slot not asked is left out by `@include`, its page size
2235    /// bound to one; GitHub answers an error about one slot at that slot's path, and the other
2236    /// slots are answered beside it.
2237    pub const SEARCH_BATCH: &str = concat!(
2238        r#"query($owner:String!,$number:Int!,$type:SearchType!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2239        relationship_events_variable!(),
2240        search_batch_variables!(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19),
2241        origin_batch_variables!(0 1 2 3 4),
2242        "){",
2243        search_batch_slots!(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19),
2244        origin_batch_slots!(0 1 2 3 4),
2245        "\n    }",
2246        board_issue!()
2247    );
2248
2249    /// The comment slots `<n>` of [`ADD_COMMENTS`]: [`ADD_COMMENT`]'s mutation, aliased
2250    /// `comment<n>`, sent only while `$send<n>` holds.
2251    macro_rules! comment_slots {
2252        ($($n:literal)+) => {
2253            concat!($(
2254                " comment", stringify!($n), ":addComment(input:$comment", stringify!($n),
2255                ") @include(if:$send", stringify!($n), "){subject{id} commentEdge{node{",
2256                issue_comment!(), "}}}",
2257            )+)
2258        };
2259    }
2260
2261    /// Adds up to [`COMMENT_BATCH`](super::COMMENT_BATCH) comments in one request, each as
2262    /// [`ADD_COMMENT`] adds one, in order: what `task comment add-many` sends. GitHub runs each
2263    /// alias on its own and answers one it refused at its own path, beside the others.
2264    pub const ADD_COMMENTS: &str = concat!(
2265        "mutation(",
2266        wide_variables!("comment", "AddCommentInput", "send"; 0 1 2 3 4 5 6 7 8 9 10),
2267        "){",
2268        comment_slots!(0 1 2 3 4 5 6 7 8 9 10),
2269        "}"
2270    );
2271
2272    /// Many tasks' targeted updates, each as [`UPDATE_FIELDS`] and [`UPDATE_ISSUE`] write one:
2273    /// up to [`TASK_FIELD_BATCH`](super::TASK_FIELD_BATCH) board field values in `field<n>`,
2274    /// [`MUTATION_BATCH`](super::MUTATION_BATCH) field clears in `clear<n>`, and as many
2275    /// `updateIssue`s in `update<n>`, each sent only while its own `send…<n>` holds.
2276    ///
2277    /// What `task update-many` sends, in two requests at most: every item's field values and
2278    /// clears first, then every issue update of an item whose fields all landed — the order one
2279    /// item's update keeps, so a refusal part way leaves an item's body as it stood.
2280    ///
2281    /// Each `update<n>` selects the issue it updated as a read of it does, so its response spells
2282    /// the change token the task holds after its batch's writes — the version `update-many`
2283    /// answers — with no read after them. Its page sizes are bound by `$nestedFirst` and
2284    /// `$boardItems`, as every read's are.
2285    pub const WRITE_TASKS: &str = concat!(
2286        "mutation(",
2287        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
2288        wide_variables!("clear", "ClearProjectV2ItemFieldValueInput", "sendClear"; 0 1 2 3 4 5 6 7 8 9 10),
2289        wide_variables!("update", "UpdateIssueInput", "sendUpdate"; 0 1 2 3 4 5 6 7 8 9 10),
2290        r#"$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2291        relationship_events_variable!(),
2292        "){",
2293        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),
2294        wide_slots!("clear", "clearProjectV2ItemFieldValue", "projectV2Item{id}", "sendClear"; 0 1 2 3 4 5 6 7 8 9 10),
2295        wide_slots!("update", "updateIssue", "issue{id ...BoardIssue}", "sendUpdate"; 0 1 2 3 4 5 6 7 8 9 10),
2296        "}",
2297        board_issue!()
2298    );
2299
2300    /// One alias of [`ISSUE_RELATIONS`]: the item a batch's `$id<n>` names, as a read of it by
2301    /// its own id reads it, with a page of its sub-issues' ids.
2302    macro_rules! issue_relations_alias {
2303        ($n:literal) => {
2304            concat!(
2305                "\n      i",
2306                stringify!($n),
2307                ":node(id:$id",
2308                stringify!($n),
2309                "){__typename ...BoardIssue ... on Issue{subIssues(first:$first){nodes{id}pageInfo{hasNextPage}}}}"
2310            )
2311        };
2312    }
2313
2314    /// [`ISSUE_DETAILS`] with each issue's sub-issues in place of its comments: what `task
2315    /// relations` and a link write read of [`DETAIL_BATCH`](super::DETAIL_BATCH) tasks at once
2316    /// on a source reading sub-issues as children — each task whole, its parent and the ids of up
2317    /// to `$first` of its children.
2318    pub const ISSUE_RELATIONS: &str = concat!(
2319        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!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2320        relationship_events_variable!(),
2321        r#"){"#,
2322        issue_relations_alias!(0),
2323        issue_relations_alias!(1),
2324        issue_relations_alias!(2),
2325        issue_relations_alias!(3),
2326        issue_relations_alias!(4),
2327        issue_relations_alias!(5),
2328        issue_relations_alias!(6),
2329        issue_relations_alias!(7),
2330        issue_relations_alias!(8),
2331        issue_relations_alias!(9),
2332        issue_relations_alias!(10),
2333        issue_relations_alias!(11),
2334        issue_relations_alias!(12),
2335        issue_relations_alias!(13),
2336        issue_relations_alias!(14),
2337        issue_relations_alias!(15),
2338        issue_relations_alias!(16),
2339        issue_relations_alias!(17),
2340        issue_relations_alias!(18),
2341        issue_relations_alias!(19),
2342        issue_relations_alias!(20),
2343        issue_relations_alias!(21),
2344        issue_relations_alias!(22),
2345        issue_relations_alias!(23),
2346        "\n    }",
2347        board_issue!()
2348    );
2349
2350    /// Up to [`RELATION_BATCH`](super::RELATION_BATCH) parent writes in one request:
2351    /// `parent<n>` an `addSubIssue` (a re-parent carrying `replaceParent: true`) and `unparent<n>`
2352    /// a `removeSubIssue`, each sent only while its own `send…<n>` holds. GitHub runs each alias
2353    /// on its own and answers one it refused — a cycle, a depth or children limit — at its own
2354    /// path, beside the others.
2355    pub const SET_PARENTS: &str = concat!(
2356        "mutation(",
2357        wide_variables!("parent", "AddSubIssueInput", "sendParent"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2358        wide_variables!("unparent", "RemoveSubIssueInput", "sendUnparent"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2359        "){",
2360        wide_slots!("parent", "addSubIssue", "issue{id} subIssue{id}", "sendParent"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2361        wide_slots!("unparent", "removeSubIssue", "issue{id} subIssue{id}", "sendUnparent"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2362        "}"
2363    );
2364
2365    /// Up to [`RELATION_BATCH`](super::RELATION_BATCH) `relates-to` relations added in one
2366    /// request, each `relate<n>` sent only while `$send<n>` holds: what a link of a type a
2367    /// source mirrors natively writes beside its metadata.
2368    pub const ADD_RELATES_TO: &str = concat!(
2369        "mutation(",
2370        wide_variables!("relate", "AddRelatesToInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2371        "){",
2372        wide_slots!("relate", "addRelatesTo", "issue{id} relatedIssue{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2373        "}"
2374    );
2375
2376    /// [`ADD_RELATES_TO`]'s removal: up to [`RELATION_BATCH`](super::RELATION_BATCH)
2377    /// `relates-to` relations removed in one request.
2378    pub const REMOVE_RELATES_TO: &str = concat!(
2379        "mutation(",
2380        wide_variables!("unrelate", "RemoveRelatesToInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2381        "){",
2382        wide_slots!("unrelate", "removeRelatesTo", "issue{id} relatedIssue{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29),
2383        "}"
2384    );
2385
2386    /// One alias of [`RELATES_TO_READS`]: the issue `$id<n>` names and the ids of up to
2387    /// `$first` of the issues it relates to.
2388    macro_rules! relates_to_alias {
2389        ($n:literal) => {
2390            concat!(
2391                "\n      r",
2392                stringify!($n),
2393                ":node(id:$id",
2394                stringify!($n),
2395                "){... on Issue{id relatesTo(first:$first){nodes{id}pageInfo{hasNextPage}}}}"
2396            )
2397        };
2398    }
2399
2400    /// What [`DETAIL_BATCH`](super::DETAIL_BATCH) issues relate to, one alias each: read
2401    /// after a relation write whose answer never arrived, so each relation is answered from what
2402    /// GitHub holds rather than sent again blindly.
2403    pub const RELATES_TO_READS: &str = concat!(
2404        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!){"#,
2405        relates_to_alias!(0),
2406        relates_to_alias!(1),
2407        relates_to_alias!(2),
2408        relates_to_alias!(3),
2409        relates_to_alias!(4),
2410        relates_to_alias!(5),
2411        relates_to_alias!(6),
2412        relates_to_alias!(7),
2413        relates_to_alias!(8),
2414        relates_to_alias!(9),
2415        relates_to_alias!(10),
2416        relates_to_alias!(11),
2417        relates_to_alias!(12),
2418        relates_to_alias!(13),
2419        relates_to_alias!(14),
2420        relates_to_alias!(15),
2421        relates_to_alias!(16),
2422        relates_to_alias!(17),
2423        relates_to_alias!(18),
2424        relates_to_alias!(19),
2425        relates_to_alias!(20),
2426        relates_to_alias!(21),
2427        relates_to_alias!(22),
2428        relates_to_alias!(23),
2429        "\n    }"
2430    );
2431
2432    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2433    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2434    /// change token's every part and the first page of its comments.
2435    ///
2436    /// Sent **only** while that write runs, in place of the document named beside each
2437    /// below, so every other write's documents, prices and request counts are what they
2438    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2439    /// mutation's response is stamped seconds after the change it made, so against GitHub
2440    /// itself a response rarely determines a version; the cache is then left unserveable
2441    /// for the task, and its next read refreshes it.
2442    macro_rules! whole_issue {
2443        () => {
2444            concat!(
2445                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2446                issue_comment!(),
2447                r#"}pageInfo{hasNextPage endCursor}}}"#
2448            )
2449        };
2450    }
2451
2452    /// The variables every document of `whole_issue!` declares beside its own.
2453    macro_rules! whole_variables {
2454        () => {
2455            concat!(
2456                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2457                relationship_events_variable!()
2458            )
2459        };
2460    }
2461
2462    /// [`ADD_COMMENT`], selecting the whole issue commented on.
2463    pub const ADD_COMMENT_WHOLE: &str = concat!(
2464        r#"mutation($input:AddCommentInput!"#,
2465        whole_variables!(),
2466        r#"){addComment(input:$input){subject{id "#,
2467        whole_issue!(),
2468        r#"} commentEdge{node{"#,
2469        issue_comment!(),
2470        r#"}}}}"#,
2471        board_issue!()
2472    );
2473    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2474    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2475        r#"mutation($input:UpdateIssueCommentInput!"#,
2476        whole_variables!(),
2477        r#"){updateIssueComment(input:$input){issueComment{"#,
2478        issue_comment!(),
2479        r#" issue{"#,
2480        whole_issue!(),
2481        r#"}}}}"#,
2482        board_issue!()
2483    );
2484    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2485    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2486        r#"mutation($input:UpdateIssueInput!"#,
2487        whole_variables!(),
2488        r#"){updateIssue(input:$input){issue{id "#,
2489        whole_issue!(),
2490        r#"}}}"#,
2491        board_issue!()
2492    );
2493    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2494    pub const UPDATE_FIELD_WHOLE: &str = concat!(
2495        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2496        whole_variables!(),
2497        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2498        whole_issue!(),
2499        r#"}}}}"#,
2500        board_issue!()
2501    );
2502    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2503    pub const CLEAR_FIELD_WHOLE: &str = concat!(
2504        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2505        whole_variables!(),
2506        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2507        whole_issue!(),
2508        r#"}}}}"#,
2509        board_issue!()
2510    );
2511
2512    /// The variables every document selecting [`board_issue_fields!`] inline declares beside
2513    /// its own.
2514    macro_rules! token_variables {
2515        () => {
2516            concat!(
2517                r#",$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2518                relationship_events_variable!()
2519            )
2520        };
2521    }
2522
2523    /// [`UPDATE_ISSUE`], selecting what a read of the issue updated selects — every part of its
2524    /// change token, and no comment — inline, so its root fields are the plain document's.
2525    ///
2526    /// What a targeted update sends in the plain document's place, so the version it answers
2527    /// is the one its own last response spells (`GitHubProjectsSource::written_tokens`). It
2528    /// costs the one point the plain document does.
2529    pub const UPDATE_ISSUE_TOKEN: &str = concat!(
2530        r#"mutation($input:UpdateIssueInput!"#,
2531        token_variables!(),
2532        r#"){updateIssue(input:$input){issue{"#,
2533        board_issue_fields!(),
2534        r#"}}}"#
2535    );
2536    /// [`UPDATE_FIELD`], selecting the issue behind the board item written as
2537    /// [`UPDATE_ISSUE_TOKEN`] selects the issue it updates.
2538    pub const UPDATE_FIELD_TOKEN: &str = concat!(
2539        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2540        token_variables!(),
2541        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{... on Issue{"#,
2542        board_issue_fields!(),
2543        r#"}}}}}"#
2544    );
2545    /// [`CLEAR_FIELD`], selecting the issue behind the board item cleared as
2546    /// [`UPDATE_ISSUE_TOKEN`] selects the issue it updates.
2547    pub const CLEAR_FIELD_TOKEN: &str = concat!(
2548        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2549        token_variables!(),
2550        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{... on Issue{"#,
2551        board_issue_fields!(),
2552        r#"}}}}}"#
2553    );
2554
2555    /// The document a targeted update sends in `document`'s place, when it has one: the twin
2556    /// whose answer spells the change token of the issue written.
2557    #[must_use]
2558    pub fn token_twin(document: &str) -> Option<&'static str> {
2559        [
2560            (UPDATE_ISSUE, UPDATE_ISSUE_TOKEN),
2561            (UPDATE_FIELD, UPDATE_FIELD_TOKEN),
2562            (CLEAR_FIELD, CLEAR_FIELD_TOKEN),
2563        ]
2564        .into_iter()
2565        .find(|(plain, _)| *plain == document)
2566        .map(|(_, token)| token)
2567    }
2568
2569    /// The document sent in `document`'s place while a write the store's cache expects runs,
2570    /// when it has one.
2571    #[must_use]
2572    pub fn whole_twin(document: &str) -> Option<&'static str> {
2573        [
2574            (ADD_COMMENT, ADD_COMMENT_WHOLE),
2575            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2576            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2577            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2578            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2579        ]
2580        .into_iter()
2581        .find(|(plain, _)| *plain == document)
2582        .map(|(_, whole)| whole)
2583    }
2584
2585    /// The node id of one person by their login, as the aliased field `u0` answering `$login0`:
2586    /// what an assignee write names a person by, since `assigneeIds` takes node ids and a person
2587    /// is named by login.
2588    ///
2589    /// The inventoried form of the lookup. What is sent is this document sized to the call
2590    /// ([`user_ids`]): one `u<n>:user(login:$login<n>)` field per person the call names that
2591    /// this process has not resolved, so one request answers every one of them. GitHub answers a
2592    /// login it does not know with `null` at that alias beside a `NOT_FOUND` error naming it,
2593    /// and the others' ids beside them.
2594    pub const USER_IDS: &str = "query($login0:String!){ u0:user(login:$login0){id login}}";
2595
2596    /// [`USER_IDS`] for `people` logins — `$login0` to `$login<people - 1>`, each answered at
2597    /// its own alias — and [`USER_IDS`] itself for one.
2598    #[must_use]
2599    pub fn user_ids(people: usize) -> String {
2600        let people = people.max(1);
2601        let declared: Vec<String> = (0..people)
2602            .map(|slot| format!("$login{slot}:String!"))
2603            .collect();
2604        let fields: Vec<String> = (0..people)
2605            .map(|slot| format!("u{slot}:user(login:$login{slot}){{id login}}"))
2606            .collect();
2607        format!("query({}){{ {}}}", declared.join(","), fields.join(" "))
2608    }
2609
2610    /// The document a request sending `document` with `variables` really sends: [`USER_IDS`]
2611    /// sized to the logins `variables` binds, and every other document as it is.
2612    #[must_use]
2613    pub fn sized<'a>(
2614        document: &'a str,
2615        variables: &serde_json::Value,
2616    ) -> std::borrow::Cow<'a, str> {
2617        if document != USER_IDS {
2618            return std::borrow::Cow::Borrowed(document);
2619        }
2620        let people = (0..)
2621            .take_while(|slot| variables.get(format!("login{slot}")).is_some())
2622            .count();
2623        std::borrow::Cow::Owned(user_ids(people))
2624    }
2625
2626    /// The inventoried document `document` is an instance of: [`USER_IDS`] for any lookup
2627    /// [`user_ids`] sized, and `document` itself otherwise.
2628    #[must_use]
2629    pub fn inventoried(document: &str) -> &str {
2630        let people = document.matches(":user(login:$login").count();
2631        if people > 0 && user_ids(people) == document {
2632            USER_IDS
2633        } else {
2634            document
2635        }
2636    }
2637
2638    /// The selection of an issue's — or a draft's — assignees, as a source opted in to them
2639    /// (`config.assignees: true`) adds it to every document that reads one; see
2640    /// [`with_assignees`].
2641    const ASSIGNEES: &str = " assignees(first:$assigneeFirst){nodes{id login}}";
2642    /// The variable [`ASSIGNEES`] pages by, declared with its value as its default — the most
2643    /// people GitHub assigns one issue — so no caller binds it, and inserted first among a
2644    /// document's variables.
2645    const ASSIGNEE_VARIABLE: &str = "$assigneeFirst:Int!=10 ";
2646    /// [`ASSIGNEE_VARIABLE`] for a document declaring no variables of its own.
2647    const ASSIGNEE_VARIABLES: &str = "($assigneeFirst:Int!=10)";
2648    /// Where [`ASSIGNEES`] goes: straight after each of these, wherever an issue or a draft is
2649    /// read — what every read of an issue selects of it — and inside the answer of each
2650    /// `createIssue` and `updateIssue`, so a write's own answer says whom GitHub assigned.
2651    const ASSIGNEE_MARKERS: [&str; 2] = [
2652        "commentCount:comments{totalCount}",
2653        "title body createdAt updatedAt",
2654    ];
2655
2656    /// `document` as a source opted in to assignees sends it: [`ASSIGNEES`] selected wherever
2657    /// it reads an issue or a draft and in every `createIssue` and `updateIssue` answer, with the
2658    /// variable it pages by declared — or `None` when it reads and writes neither, and is sent
2659    /// as written.
2660    ///
2661    /// A rewrite of the one text sent rather than a second set of documents, so a source that
2662    /// has not opted in sends every document byte for byte as it did, and an opted-in one rides
2663    /// its assignees on the very reads and writes it already makes. [`without_assignees`] is its
2664    /// exact inverse, which the loopback board answers a document by.
2665    #[must_use]
2666    pub fn with_assignees(document: &str) -> Option<String> {
2667        let mut spelled = String::with_capacity(document.len() + 256);
2668        let mut inserted = false;
2669        let mut rest = document;
2670        while !rest.is_empty() {
2671            // The earliest place one is due, of every kind.
2672            let next = ASSIGNEE_MARKERS
2673                .iter()
2674                .filter_map(|marker| rest.find(marker).map(|at| (at + marker.len(), true)))
2675                .chain(["createIssue(", "updateIssue("].iter().filter_map(|write| {
2676                    let at = rest.find(write)?;
2677                    let open = at + rest[at..].find('{')?;
2678                    rest[open..]
2679                        .starts_with("{issue{id")
2680                        .then_some((open + "{issue{id".len(), true))
2681                        .or(Some((open, false)))
2682                }))
2683                .min_by_key(|(at, _)| *at);
2684            let Some((at, insert)) = next else {
2685                spelled.push_str(rest);
2686                break;
2687            };
2688            spelled.push_str(&rest[..at]);
2689            if insert {
2690                spelled.push_str(ASSIGNEES);
2691                inserted = true;
2692            }
2693            rest = &rest[at..];
2694            if !insert {
2695                // A write whose answer reads no issue's id first: past its opening brace.
2696                spelled.push('{');
2697                rest = &rest[1..];
2698            }
2699        }
2700        if !inserted {
2701            return None;
2702        }
2703        let open = spelled.find('(');
2704        let body = spelled.find('{');
2705        Some(match (open, body) {
2706            (Some(open), Some(body)) if open < body => {
2707                format!(
2708                    "{}{ASSIGNEE_VARIABLE}{}",
2709                    &spelled[..=open],
2710                    &spelled[open + 1..]
2711                )
2712            }
2713            (_, Some(body)) => format!(
2714                "{}{ASSIGNEE_VARIABLES}{}",
2715                &spelled[..body],
2716                &spelled[body..]
2717            ),
2718            _ => return None,
2719        })
2720    }
2721
2722    /// `document` as [`with_assignees`] spelled it, back as the document it was written as — or
2723    /// `None` for a document it did not rewrite.
2724    #[must_use]
2725    pub fn without_assignees(document: &str) -> Option<String> {
2726        if !document.contains(ASSIGNEES) {
2727            return None;
2728        }
2729        let plain = document.replace(ASSIGNEES, "");
2730        Some(if plain.contains(ASSIGNEE_VARIABLES) {
2731            plain.replacen(ASSIGNEE_VARIABLES, "", 1)
2732        } else {
2733            plain.replacen(ASSIGNEE_VARIABLE, "", 1)
2734        })
2735    }
2736
2737    /// Every document above, with what this source is doing when it sends one.
2738    ///
2739    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2740    /// name the call that was refused, and a `match` with a catch-all arm would answer a
2741    /// document added later with "talking to GitHub" and never say so.
2742    ///
2743    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2744    /// const` here that this list omits, so the two cannot part — which is the same guard
2745    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2746    pub const DOCUMENTS: [(&str, &str); 69] = [
2747        (USER_IDS, "looking up the people an assignee write names"),
2748        (
2749            ISSUE_RELATIONS,
2750            "reading several issues with their parents and sub-issues",
2751        ),
2752        (SET_PARENTS, "writing several issues' parents"),
2753        (ADD_RELATES_TO, "adding several relates-to relations"),
2754        (REMOVE_RELATES_TO, "removing several relates-to relations"),
2755        (
2756            RELATES_TO_READS,
2757            "reading what several issues relate to after a relation write went unanswered",
2758        ),
2759        (
2760            ISSUES_BY_CREATION,
2761            "listing a repository's issues newest first for a create key",
2762        ),
2763        (SEARCH_ISSUES, "searching this board's issues"),
2764        (ISSUE, "reading one issue"),
2765        (
2766            DOCUMENT_IN_PROJECT,
2767            "reading one document with its project and the project's members",
2768        ),
2769        (
2770            ISSUE_BOARD_ITEMS,
2771            "reading one issue's board memberships past the page it came with",
2772        ),
2773        (SUB_ISSUES, "reading a project's tasks"),
2774        (BOARD, "reading the board"),
2775        (
2776            BOARD_WITH_COMMENTS,
2777            "reading a board page with first comments",
2778        ),
2779        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2780        (CLOSED_ITEMS, "reading the board's closed items"),
2781        (
2782            ISSUES_BY_REFERENCE,
2783            "reading a batch of issues by their references",
2784        ),
2785        (BOARD_FIELDS, "reading the board's fields"),
2786        (DRAFT, "reading one draft"),
2787        (REPOSITORY, "reading the destination repository"),
2788        (
2789            PROJECT_VISIBILITY,
2790            "reading whether the board's project is public",
2791        ),
2792        (
2793            CREATION_CONTEXT,
2794            "reading the board's fields and the destination repository",
2795        ),
2796        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2797        (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2798        (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2799        (CREATE_ISSUE, "creating an issue"),
2800        (
2801            ORIGIN_LOOKUPS,
2802            "looking up the items copied from several origins, and what creating one reads",
2803        ),
2804        (
2805            ORIGIN_LOOKUPS_WITH_MEMBERS,
2806            "looking up the items copied from several origins, with a project's members",
2807        ),
2808        (CREATE_ISSUES, "creating several issues together"),
2809        (
2810            CREATED_BOARD_ITEMS,
2811            "reading the board items of the issues a copy created",
2812        ),
2813        (
2814            WRITE_FIELD_VALUES,
2815            "writing board fields of the items a copy created",
2816        ),
2817        (
2818            WRITE_FIELDS_AND_EDGES,
2819            "writing board fields and dependencies of the items a copy created",
2820        ),
2821        (CLOSE_ISSUES, "closing the issues a copy created"),
2822        (UPDATE_ISSUES, "updating several issues together"),
2823        (ADD_BLOCKED_BYS, "recording several dependencies together"),
2824        (ADD_TO_BOARD, "adding an issue to the board"),
2825        (UPDATE_ISSUE, "updating an issue"),
2826        (UPDATE_DRAFT, "updating a draft item"),
2827        (UPDATE_FIELD, "writing a board field"),
2828        (UPDATE_FIELDS, "writing board fields together"),
2829        (CLEAR_FIELD, "clearing a board field"),
2830        (
2831            CREATE_FIELD,
2832            "creating a board single-select field with its options",
2833        ),
2834        (
2835            STATUS_OPTIONS_SNAPSHOT,
2836            "snapshotting board Status options and assignments",
2837        ),
2838        (
2839            STATUS_OPTIONS_UPDATE,
2840            "safely replacing the board Status option list",
2841        ),
2842        (ADD_SUB_ISSUE, "filing an issue under its project"),
2843        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2844        (ADD_BLOCKED_BY, "recording a dependency"),
2845        (REMOVE_BLOCKED_BY, "removing a dependency"),
2846        (DELETE_ISSUE, "deleting an issue"),
2847        (DELETE_ISSUES, "deleting several issues together"),
2848        (ISSUE_COMMENTS, "reading a task's comments"),
2849        (ISSUE_DETAIL, "reading one issue with its comments"),
2850        (
2851            ISSUE_DETAILS,
2852            "reading a batch of issues with their comments",
2853        ),
2854        (
2855            ISSUE_COPY_DETAILS,
2856            "reading a batch of issues with their comments and dependencies for a copy",
2857        ),
2858        (COMMENT_ISSUE, "reading which issue a comment is on"),
2859        (ADD_COMMENT, "adding a comment"),
2860        (UPDATE_COMMENT, "editing a comment"),
2861        (DELETE_COMMENT, "deleting a comment"),
2862        (
2863            SEARCH_BATCH,
2864            "searching this board's issues several ways at once",
2865        ),
2866        (ADD_COMMENTS, "adding several comments together"),
2867        (WRITE_TASKS, "updating several tasks together"),
2868        (
2869            ADD_COMMENT_WHOLE,
2870            "adding a comment, answered with the whole issue",
2871        ),
2872        (
2873            UPDATE_COMMENT_WHOLE,
2874            "editing a comment, answered with the whole issue",
2875        ),
2876        (
2877            UPDATE_ISSUE_WHOLE,
2878            "updating an issue, answered with it whole",
2879        ),
2880        (
2881            UPDATE_FIELD_WHOLE,
2882            "writing a board field, answered with the whole issue",
2883        ),
2884        (
2885            CLEAR_FIELD_WHOLE,
2886            "clearing a board field, answered with the whole issue",
2887        ),
2888        (
2889            UPDATE_ISSUE_TOKEN,
2890            "updating an issue, answered with its version",
2891        ),
2892        (
2893            UPDATE_FIELD_TOKEN,
2894            "writing a board field, answered with the issue's version",
2895        ),
2896        (
2897            CLEAR_FIELD_TOKEN,
2898            "clearing a board field, answered with the issue's version",
2899        ),
2900    ];
2901}
2902
2903/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2904/// under, the variable carrying its input, and the boolean variable that includes it.
2905#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2906pub struct FieldSlot {
2907    /// The key of the answer's `data` this field's payload is under.
2908    pub alias: &'static str,
2909    /// The variable carrying this field's input.
2910    pub variable: &'static str,
2911    /// The boolean variable whose `true` sends this field.
2912    pub include: &'static str,
2913}
2914
2915/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2916pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2917    FieldSlot {
2918        alias: "updateProjectV2ItemFieldValue",
2919        variable: "input",
2920        include: "writeFirst",
2921    },
2922    FieldSlot {
2923        alias: "second",
2924        variable: "second",
2925        include: "writeSecond",
2926    },
2927    FieldSlot {
2928        alias: "third",
2929        variable: "third",
2930        include: "writeThird",
2931    },
2932    FieldSlot {
2933        alias: "fourth",
2934        variable: "fourth",
2935        include: "writeFourth",
2936    },
2937    FieldSlot {
2938        alias: "fifth",
2939        variable: "fifth",
2940        include: "writeFifth",
2941    },
2942    FieldSlot {
2943        alias: "sixth",
2944        variable: "sixth",
2945        include: "writeSixth",
2946    },
2947];
2948
2949/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2950pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2951    FieldSlot {
2952        alias: "cleared",
2953        variable: "clear",
2954        include: "writeClear",
2955    },
2956    FieldSlot {
2957        alias: "clearedSecond",
2958        variable: "clearSecond",
2959        include: "writeClearSecond",
2960    },
2961    FieldSlot {
2962        alias: "clearedThird",
2963        variable: "clearThird",
2964        include: "writeClearThird",
2965    },
2966];
2967
2968/// Which of GitHub's two rate limiters refused a request.
2969///
2970/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2971/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2972/// the whole reason this is carried rather than collapsed into "rate limited".
2973#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2974enum Limiter {
2975    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2976    Primary,
2977    /// The burst limiter over content-generating requests, which nothing reports.
2978    Secondary,
2979}
2980
2981/// The wordings GitHub answers a secondary rate limit with.
2982///
2983/// It sends them under a forbidden status, under a too-many-requests status, and inside
2984/// the `errors` of a *successful* response, which is why the text is what this matches on
2985/// rather than the status. `abuse detection` is the wording GitHub used before the
2986/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2987/// what a burst of content creation is refused with.
2988///
2989/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2990/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2991/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2992/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2993pub const SECONDARY_WORDINGS: [&str; 5] = [
2994    "secondary rate limit",
2995    "temporarily blocked from content creation",
2996    "abuse detection",
2997    "submitted too quickly",
2998    "exceeded a secondary",
2999];
3000
3001/// The wordings GitHub answers an exhausted primary budget with.
3002///
3003/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
3004/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
3005/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
3006/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
3007/// two phrases is a substring of it, so without it that answer read as a refusal that will
3008/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
3009/// one reason.
3010pub const PRIMARY_WORDINGS: [&str; 4] = [
3011    "api rate limit exceeded",
3012    "api rate limit already exceeded",
3013    "rate limit exceeded",
3014    "rate_limited",
3015];
3016
3017/// The wordings GitHub answers a write that collided with another change to the same
3018/// ordering with, in the `errors` of an HTTP 200: "Your attempt to move this item created a
3019/// temporary conflict. Please try again." The write did not take effect and GitHub asks for it
3020/// again, so it is retried on the same growing schedule a rate limit without a hint is, within
3021/// the same retry budget — except a `createIssue`, whose answer cannot say the issue was not
3022/// made, and which is therefore reported rather than replayed into a duplicate.
3023///
3024/// Observed rather than documented, so it is pinned exactly as [`SECONDARY_WORDINGS`] is:
3025/// `tests/fixtures/rate-limits.json` records where it was read under `conflict.observed`, and
3026/// the same drift gate reconciles the two both ways. Public for that gate alone.
3027pub const CONFLICT_WORDINGS: [&str; 1] = ["created a temporary conflict"];
3028
3029/// What a response *says about itself*, which is the only place a refusal can be read.
3030///
3031/// Deliberately not the whole response body. A board is a place people write about their
3032/// own work, and a task on it titled "the secondary rate limit" would, matched across the
3033/// raw text, turn a perfectly good answer into a refusal this source then waited out and
3034/// reported. So the item data is never read: what is read is GitHub's own REST-style
3035/// `message` envelope, which is what a forbidden status carries, and the `message` and
3036/// `type` of each GraphQL error, which is where a *successful* response says it.
3037///
3038/// A body that is not JSON at all has nothing structured to read, so only a failing
3039/// response's own text is taken — a successful response that is not JSON is malformed
3040/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
3041fn refusal_wording(status: StatusCode, body: &str) -> String {
3042    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
3043        return if status.is_success() {
3044            String::new()
3045        } else {
3046            body.to_owned()
3047        };
3048    };
3049    let mut said: Vec<&str> = parsed
3050        .get("message")
3051        .and_then(Value::as_str)
3052        .into_iter()
3053        .collect();
3054    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
3055        for error in errors {
3056            said.extend(
3057                ["message", "type"]
3058                    .into_iter()
3059                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
3060            );
3061        }
3062    }
3063    said.join("; ")
3064}
3065
3066impl Limiter {
3067    /// Which limiter refused this response, or `None` when none of them did.
3068    ///
3069    /// The wording is read first and the status only decides what carries none of it,
3070    /// because GitHub answers a secondary limit with a forbidden status far more often
3071    /// than with too-many-requests — while a forbidden status saying nothing about a limit
3072    /// really is a credential this token lacks.
3073    ///
3074    /// A response is a refusal because of its status or its own wording. A spent budget
3075    /// only ever explains one; it never turns an answer into a refusal.
3076    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
3077        let normalized = refusal_wording(status, body).to_ascii_lowercase();
3078        if SECONDARY_WORDINGS
3079            .iter()
3080            .any(|wording| normalized.contains(wording))
3081        {
3082            return Some(Self::Secondary);
3083        }
3084        if status == StatusCode::TOO_MANY_REQUESTS {
3085            return Some(Self::Primary);
3086        }
3087        // An exhausted budget *explains* a response that failed; it does not make one that
3088        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
3089        // request the budget allowed as well as on the ones it then refuses, so reading
3090        // the header alone threw away a good answer — and, once refusals were retried,
3091        // replayed a request that had already taken effect.
3092        if !status.is_success() && budget_exhausted {
3093            return Some(Self::Primary);
3094        }
3095        // A successful response saying it: GitHub reports a GraphQL rate limit in the
3096        // `errors` of an HTTP 200, where nothing about the status says so at all.
3097        if status.is_success()
3098            && PRIMARY_WORDINGS
3099                .iter()
3100                .any(|wording| normalized.contains(wording))
3101        {
3102            return Some(Self::Primary);
3103        }
3104        None
3105    }
3106
3107    /// What this limiter is called where an operator can look it up.
3108    const fn name(self) -> &'static str {
3109        match self {
3110            Self::Primary => "GitHub's primary API rate limit",
3111            Self::Secondary => "GitHub's secondary rate limit",
3112        }
3113    }
3114
3115    /// What the endpoint an operator would go and check says about this limiter.
3116    const fn where_to_look(self) -> &'static str {
3117        match self {
3118            Self::Primary => {
3119                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
3120                 comes back."
3121            }
3122            Self::Secondary => {
3123                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
3124                 primary budget and does not report this one, so budget showing there says \
3125                 nothing about this refusal, and every further attempt extends it."
3126            }
3127        }
3128    }
3129
3130    /// The next step this limiter actually calls for.
3131    const fn what_to_do(self) -> &'static str {
3132        match self {
3133            Self::Primary => {
3134                "wait for the reset `gh api rate_limit` reports, then run the command again."
3135            }
3136            Self::Secondary => {
3137                "leave this board alone for a few minutes, then run the command again — or \
3138                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
3139            }
3140        }
3141    }
3142}
3143
3144/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
3145#[derive(Debug, Clone, Copy)]
3146struct Limited {
3147    limiter: Limiter,
3148    hint: Option<u64>,
3149    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
3150    /// `x-ratelimit-reset`.
3151    hinted_by: Hint,
3152}
3153
3154/// Where a refusal's hint came from, which is what its wait is named after.
3155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3156enum Hint {
3157    /// `retry-after`, or no hint at all.
3158    RetryAfter,
3159    /// An exhausted primary budget's `x-ratelimit-reset`.
3160    PrimaryReset,
3161}
3162
3163impl Limited {
3164    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
3165    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
3166    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
3167    /// extends a secondary limit — or the schedule alone when there is none.
3168    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
3169        match (self.hint, self.hinted_by) {
3170            (Some(hint), Hint::RetryAfter) => (
3171                Duration::from_secs(hint).max(backoff),
3172                pacing::cause::RETRY_AFTER,
3173            ),
3174            (Some(hint), Hint::PrimaryReset) => (
3175                Duration::from_secs(hint).max(backoff),
3176                pacing::cause::PRIMARY_RESET,
3177            ),
3178            (None, _) => (backoff, pacing::cause::BACKOFF),
3179        }
3180    }
3181
3182    /// What the caller is told once this source has waited as long as it may.
3183    ///
3184    /// Both limiters report as [`SourceError::RateLimited`], because that is what
3185    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
3186    /// about *which* limiter it was makes it a different kind of failure. What differs is
3187    /// the operator's next step, and that is what the message carries — a secondary
3188    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
3189    /// budget looks fine, and then back to retry the very burst that was refused.
3190    fn exhausted(
3191        self,
3192        doing: &str,
3193        waits: u32,
3194        waited: Duration,
3195        needed: Duration,
3196        budget: Duration,
3197    ) -> SourceError {
3198        SourceError::RateLimited {
3199            retry_after_seconds: self.hint,
3200            message: Some(format!(
3201                "{} refused this source while {doing}; it waited {} out over {} and was refused \
3202                 again, and the next wait of {} would take it past the {} one call may spend \
3203                 waiting. {} next: {}",
3204                self.limiter.name(),
3205                plural(waits, "refusal"),
3206                seconds(waited),
3207                seconds(needed),
3208                seconds(budget),
3209                self.limiter.where_to_look(),
3210                self.limiter.what_to_do(),
3211            )),
3212        }
3213    }
3214}
3215
3216/// One HTTP attempt's result, with what its response said about the rate limit.
3217///
3218/// The two travel together so the record and the outcome are written from the same place:
3219/// what a response said about the budget is only readable while that response is in hand,
3220/// and what the attempt *meant* is only decidable once its body has been read.
3221struct Attempted {
3222    result: Result<Value, Attempt>,
3223    limits: accounting::RateLimit,
3224    /// GitHub's own reported cost for this call, for a document that asked for it.
3225    reported_cost: Option<u64>,
3226    response_outcome: accounting::ResponseOutcome,
3227}
3228
3229/// One attempt's outcome: an error to report, or a rate limit to wait out.
3230enum Attempt {
3231    Failed(SourceError),
3232    Limited(Limited),
3233    /// GitHub refused a write as a temporary conflict and asked for it again — see
3234    /// [`CONFLICT_WORDINGS`]. Carries what GitHub said, for the diagnostic once retrying ends.
3235    Conflicted(String),
3236}
3237
3238fn plural(count: u32, thing: &str) -> String {
3239    if count == 1 {
3240        format!("{count} {thing}")
3241    } else {
3242        format!("{count} {thing}s")
3243    }
3244}
3245
3246fn seconds(duration: Duration) -> String {
3247    format!("{:.1}s", duration.as_secs_f64())
3248}
3249
3250/// The member [`stamp_served`] writes onto every node of a response that says when it last
3251/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
3252const SERVED_AT: &str = "onetaskgraphServedAt";
3253
3254/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
3255/// node's time later knows the server's clock on the response that read it — which is what
3256/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
3257fn stamp_served(data: &mut Value, served: &str) {
3258    match data {
3259        Value::Object(members) => {
3260            if members.contains_key("updatedAt") {
3261                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
3262            }
3263            for member in members.values_mut() {
3264                stamp_served(member, served);
3265            }
3266        }
3267        Value::Array(items) => {
3268            for item in items {
3269                stamp_served(item, served);
3270            }
3271        }
3272        _ => {}
3273    }
3274}
3275
3276/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
3277///
3278/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
3279/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
3280/// header, and neither is what makes a response a refusal — so the whole cost of one this
3281/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
3282/// instead. Refusing the response over the header would turn a readable refusal into an
3283/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
3284fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
3285    value
3286        .and_then(|value| value.to_str().ok())
3287        .and_then(|value| value.trim().parse::<u64>().ok())
3288}
3289
3290/// A transport failure with every cause beneath it, outermost first.
3291///
3292/// The client's own text for a request it could not complete is `error sending request for
3293/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
3294/// port range, a connection closed before its answer — is only in the chain of sources below
3295/// it. Those differ in what to do next, so all of them are kept.
3296pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
3297    let mut text = error.to_string();
3298    let mut cause = error.source();
3299    while let Some(next) = cause {
3300        let said = next.to_string();
3301        if !text.ends_with(&said) {
3302            text = format!("{text}: {said}");
3303        }
3304        cause = next.source();
3305    }
3306    text
3307}
3308
3309/// Every mutation this source sends creates content — an issue, a board item, a field of
3310/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
3311/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
3312/// and what the keyword says are the same set. That is what makes the keyword a sound test
3313/// rather than a convenient one: counting an edit or a removal the limiter might not have
3314/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
3315pub(crate) fn is_mutation(query: &str) -> bool {
3316    query.trim_start().starts_with("mutation")
3317}
3318
3319/// What this source was doing, for a diagnostic that has to say so.
3320///
3321/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
3322/// a document added without a description is caught by that list's own gate instead of
3323/// falling through to the vague arm below.
3324fn operation_description(query: &str) -> &'static str {
3325    let query = graphql::inventoried(query);
3326    graphql::DOCUMENTS
3327        .iter()
3328        .find(|(document, _)| *document == query)
3329        .map_or("talking to GitHub", |(_, doing)| *doing)
3330}
3331
3332/// GitHub's published ceiling on content-generating requests in any one minute.
3333///
3334/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
3335/// GitHub's number rather than this source's. This source paces at it over its window: a
3336/// content-creating request goes out at once while the minute's creations leave room for it,
3337/// and waits until they do otherwise — see the `pacing` module.
3338pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
3339/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
3340/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
3341pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
3342/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
3343/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
3344/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
3345pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
3346/// The secondary points one GraphQL request carrying a mutation is charged.
3347pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
3348/// The secondary points one GraphQL request carrying no mutation is charged.
3349pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
3350/// The fastest *steady* spacing of content-creating mutations that cannot exceed
3351/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
3352///
3353/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
3354/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
3355/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
3356/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
3357/// value that holds a long sustained burst under the per-minute bound by spacing alone.
3358pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
3359/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
3360///
3361/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
3362/// own advice for a secondary limit — wait, and wait longer each time — without spending
3363/// the first minute of a transient refusal doing nothing.
3364pub const RETRY_BACKOFF_MS: u64 = 1_000;
3365/// Total time one call may spend waiting out rate limits before it reports a failure.
3366///
3367/// Two minutes is long enough to ride out the refusals a paced copy still collects and
3368/// short enough that a command an operator is watching returns. The bound is what makes
3369/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
3370/// the limiter, not in a process nobody can tell from a wedged one.
3371pub const RETRY_BUDGET_MS: u64 = 120_000;
3372
3373fn default_token_env() -> String {
3374    "GH_PROJECTS_TOKEN".to_owned()
3375}
3376fn default_endpoint() -> String {
3377    "https://api.github.com/graphql".to_owned()
3378}
3379
3380/// The name of a `Status` single-select option on the board.
3381///
3382/// Validated on the way in rather than checked later, so a blank option name — which
3383/// nothing on a board can be — is a state this type cannot hold.
3384#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3385#[serde(try_from = "String")]
3386#[schemars(extend("minLength" = 1))]
3387pub struct ColumnName(String);
3388
3389impl ColumnName {
3390    /// The option name, as the board spells it.
3391    fn as_str(&self) -> &str {
3392        &self.0
3393    }
3394}
3395
3396impl TryFrom<String> for ColumnName {
3397    type Error = String;
3398
3399    fn try_from(name: String) -> Result<Self, Self::Error> {
3400        if name.trim().is_empty() {
3401            return Err("a status_mapping option name cannot be blank".to_owned());
3402        }
3403        Ok(Self(name))
3404    }
3405}
3406
3407/// The two closed states this product can mean.
3408///
3409/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
3410/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
3411/// product never decides on a person's behalf that two issues are the same work.
3412#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
3413#[serde(rename_all = "kebab-case")]
3414pub enum ClosedState {
3415    /// `COMPLETED` — precisely done.
3416    Completed,
3417    /// `NOT_PLANNED` — precisely cancelled.
3418    NotPlanned,
3419}
3420
3421impl ClosedState {
3422    const fn reason(self) -> &'static str {
3423        match self {
3424            Self::Completed => "COMPLETED",
3425            Self::NotPlanned => "NOT_PLANNED",
3426        }
3427    }
3428}
3429
3430/// Configuration for one GitHub Projects v2 board.
3431///
3432/// It serializes to a document this same type reads back as itself, so a caller writing a
3433/// configuration — a consumer filing onto a board it configures — can build one here rather
3434/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
3435#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3436#[serde(default, deny_unknown_fields)]
3437pub struct GitHubProjectsConfig {
3438    /// Login of the user or organization which owns the board.
3439    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
3440    /// The project number shown in the board's GitHub URL.
3441    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
3442    // 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.
3443    /// `owner/name` of the repository this source creates an issue in when the item's own
3444    /// `repositories` field does not decide it.
3445    ///
3446    /// An item naming exactly one repository is created there — on a source declared
3447    /// `visibility: private`, only when that repository reads private, and otherwise as an
3448    /// item naming none is; a task or a document naming none or several is created in its
3449    /// parent project's repository; and a project, or a task or document with no parent,
3450    /// naming none or several is created here. A board
3451    /// has no repository of its own and `createIssue` requires one, so a write without
3452    /// this is refused naming the field. Reads never need it.
3453    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
3454    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
3455    /// GitHub logins whose repositories this source may create an issue in.
3456    ///
3457    /// Absent or empty, the default, restricts nothing. Otherwise a create whose issue the
3458    /// rule `repository` describes would file in a repository owned by anyone else is refused
3459    /// before anything of it is written — by `task copy`, `task copy --create`, a member
3460    /// create of a project copy and a dry run of each — with the error kind `creation-owner`,
3461    /// naming the repository and these owners. Logins compare case-insensitively, as GitHub
3462    /// compares them. An update of an issue that already exists is never refused by it.
3463    /// `task locate` reports such a refusal as `creates_in.refused` rather than failing.
3464    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3465    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
3466    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
3467    #[schemars(!skip_serializing_if)]
3468    pub creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates each entry against GitHub's login grammar, naming the one it refuses.
3469    /// Environment variable containing a fine-grained token with Projects and Issues
3470    /// read/write plus Pull requests read-only access for every repository represented on
3471    /// the board.
3472    #[serde(default = "default_token_env")]
3473    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
3474    /// GraphQL endpoint. GitHub Enterprise installations may override it.
3475    #[serde(default = "default_endpoint")]
3476    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
3477    /// Per-instance mapping from a status category to the option of the board's one
3478    /// `Status` field it lands on, for a task and for a project.
3479    ///
3480    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
3481    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
3482    /// where a kind left out leaves the category unmapped for that kind. A category this
3483    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
3484    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
3485    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
3486    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
3487    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
3488    /// either kind. No two categories may name one option for the same kind, ignoring case.
3489    /// `unknown` may name one existing option; every unknown word then lands on it and
3490    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
3491    /// each unknown word because it never creates board options.
3492    #[serde(default)]
3493    pub status_mapping: StatusMapping,
3494    /// Per-instance mapping from a task's priority to an option of this board's
3495    /// single-select field named `Priority`.
3496    ///
3497    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
3498    /// other priority is refused before it reaches this board. Present, each of `urgent`,
3499    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
3500    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
3501    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
3502    /// no two levels may name one option. Reads and writes never create the field or an
3503    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
3504    /// the board lacks is refused pointing there.
3505    #[serde(default)]
3506    pub priority_mapping: Option<PriorityMappingConfig>,
3507    /// Caller metadata values this source also writes to board text fields, so the board can
3508    /// filter on them.
3509    ///
3510    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
3511    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
3512    /// value. On every item write — create, update and copy, of a task, a project or a
3513    /// document — a string found there is written to the field, and nothing is sent when the
3514    /// field already holds it; nothing there, or `null`, clears the field when it holds a
3515    /// value; any other JSON type is refused before any mutation. The metadata comment in the
3516    /// issue body stays the one home of the value, and every read reports metadata from it.
3517    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
3518    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
3519    /// non-text field of that name, is refused pointing there.
3520    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3521    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
3522    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
3523    #[schemars(!skip_serializing_if)]
3524    pub metadata_fields: Vec<MetadataFieldConfig>,
3525    /// How fast this source writes, and how long it waits out a rate-limit refusal.
3526    ///
3527    /// Every field keeps its shipped default when it is absent, and the defaults are
3528    /// GitHub's own published limits rather than taste. See [`Pacing`].
3529    #[serde(default)]
3530    pub pacing: PacingConfig,
3531    /// What an issue's sub-issues mean on this board.
3532    ///
3533    /// `project`, the default, is this source's meaning since it was first written: an issue
3534    /// with sub-issues reads as a project and its sub-issues as that project's tasks. `parent`
3535    /// reads every issue as a task and a sub-issue as a child under the parent relation, which
3536    /// `task parent set-many` writes and `task relations` reads; a project write to such a
3537    /// source is refused naming this setting. The two meanings answer differently for the same
3538    /// issues, so a `parent` source needs a board of its own: two sources naming one board with
3539    /// different `sub_issues` are refused when the configuration is read.
3540    #[serde(default, skip_serializing_if = "SubIssues::is_project")]
3541    pub sub_issues: SubIssues,
3542    /// The link types this source also writes as GitHub's own `relates-to` relation, beside the
3543    /// store's metadata, so they show in the issue's Relationships panel: each type name mapped
3544    /// to `{"native": "relates-to"}`. A type not named here is kept in metadata alone. Any other
3545    /// word — `blocks`, `blocked-by`, `duplicate`, `similar` among them — is refused when the
3546    /// source is built.
3547    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
3548    // Kept in the schema as `"default": {}` although a serialized configuration leaves an empty
3549    // map out, so both SDKs model an absent map as an empty one rather than as `null`.
3550    #[schemars(!skip_serializing_if)]
3551    pub link_types: BTreeMap<String, onetaskgraph_plugin_api::LinkTypeConfig>,
3552    /// Whether this source's tasks carry the GitHub users each issue is assigned to, and
3553    /// take an assignee write. `false`, the default, reads no issue's assignees and writes
3554    /// none: every request this source sends is then exactly what it was before assignees
3555    /// existed. `true` reads each issue's assignees on the reads it already makes, and writes
3556    /// one person or none — by login — in the `createIssue` or `updateIssue` the write already
3557    /// sends, refusing a person GitHub does not know or leaves off the issue.
3558    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
3559    // Kept in the schema as `"default": false` although a serialized configuration leaves a
3560    // `false` out, so both SDKs model an absent setting as `false` rather than as `null`.
3561    #[schemars(!skip_serializing_if)]
3562    pub assignees: bool,
3563}
3564
3565/// What an issue's sub-issues mean on a GitHub Projects board: see
3566/// [`GitHubProjectsConfig::sub_issues`].
3567#[derive(
3568    Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema,
3569)]
3570#[serde(rename_all = "kebab-case")]
3571pub enum SubIssues {
3572    /// An issue with sub-issues is a project, and its sub-issues are its tasks.
3573    #[default]
3574    Project,
3575    /// Every issue is a task, and a sub-issue is a child under the parent relation.
3576    Parent,
3577}
3578
3579impl SubIssues {
3580    /// Whether this is the default meaning, which a serialized configuration leaves out.
3581    #[must_use]
3582    pub const fn is_project(&self) -> bool {
3583        matches!(self, Self::Project)
3584    }
3585}
3586
3587/// Which option of the board's `Priority` field each priority lands on.
3588///
3589/// One member per level rather than a map, so a key that is not a level is refused where
3590/// the configuration is read, naming the levels there are. `none` is not a member: it is no
3591/// value in the field, not an option of it.
3592#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3593#[serde(default, deny_unknown_fields)]
3594pub struct PriorityMappingConfig {
3595    /// The option `urgent` lands on; `Urgent` when absent.
3596    pub urgent: Option<PriorityOptionName>,
3597    /// The option `high` lands on; `High` when absent.
3598    pub high: Option<PriorityOptionName>,
3599    /// The option `medium` lands on; `Medium` when absent.
3600    pub medium: Option<PriorityOptionName>,
3601    /// The option `low` lands on; `Low` when absent.
3602    pub low: Option<PriorityOptionName>,
3603}
3604
3605/// The name of an option of the board's `Priority` single-select field.
3606///
3607/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
3608/// blank name.
3609#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3610#[serde(try_from = "String")]
3611#[schemars(extend("minLength" = 1))]
3612pub struct PriorityOptionName(String);
3613
3614impl PriorityOptionName {
3615    /// The option name, as the board spells it.
3616    fn as_str(&self) -> &str {
3617        &self.0
3618    }
3619}
3620
3621impl TryFrom<String> for PriorityOptionName {
3622    type Error = String;
3623
3624    fn try_from(name: String) -> Result<Self, Self::Error> {
3625        if name.trim().is_empty() {
3626            return Err("a priority_mapping option name cannot be blank".to_owned());
3627        }
3628        Ok(Self(name))
3629    }
3630}
3631
3632/// The name of the board field a priority is held in.
3633pub const PRIORITY_FIELD: &str = "Priority";
3634
3635/// The four priorities a board option can hold, in the order a new `Priority` field lists
3636/// them. `none` is not among them: it is the field holding no value.
3637///
3638/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
3639/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
3640/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
3641/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
3642pub const PRIORITY_LEVELS: [Priority; 4] = [
3643    Priority::Urgent,
3644    Priority::High,
3645    Priority::Medium,
3646    Priority::Low,
3647];
3648
3649/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
3650/// see that list for what this pins.
3651#[must_use]
3652pub const fn level_position(priority: Priority) -> Option<usize> {
3653    match priority {
3654        Priority::None => None,
3655        Priority::Urgent => Some(0),
3656        Priority::High => Some(1),
3657        Priority::Medium => Some(2),
3658        Priority::Low => Some(3),
3659    }
3660}
3661
3662/// This instance's complete priority-to-option mapping, read in both directions.
3663///
3664/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
3665/// two levels name one option.
3666#[derive(Debug, Clone)]
3667struct PriorityMapping {
3668    options: [PriorityOptionName; 4],
3669}
3670
3671impl PriorityMapping {
3672    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3673        let shipped = |name: &str| PriorityOptionName(name.to_owned());
3674        let mapping = Self {
3675            options: [
3676                config.urgent.unwrap_or_else(|| shipped("Urgent")),
3677                config.high.unwrap_or_else(|| shipped("High")),
3678                config.medium.unwrap_or_else(|| shipped("Medium")),
3679                config.low.unwrap_or_else(|| shipped("Low")),
3680            ],
3681        };
3682        for (index, option) in mapping.options.iter().enumerate() {
3683            if let Some(earlier) = mapping.options[..index]
3684                .iter()
3685                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
3686            {
3687                return Err(SourceError::Config {
3688                    message: format!(
3689                        "priority_mapping of source {instance} sends both {} and {} to the board \
3690                         option {:?}; one option cannot read back as two priorities",
3691                        PRIORITY_LEVELS[earlier],
3692                        PRIORITY_LEVELS[index],
3693                        option.as_str()
3694                    ),
3695                });
3696            }
3697        }
3698        Ok(mapping)
3699    }
3700
3701    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
3702    fn option(&self, priority: Priority) -> Option<&str> {
3703        level_position(priority).map(|index| self.options[index].as_str())
3704    }
3705
3706    /// The priority a board option name reports, or `None` when nothing maps to it.
3707    fn priority_of(&self, option: &str) -> Option<Priority> {
3708        self.options
3709            .iter()
3710            .position(|name| name.as_str().eq_ignore_ascii_case(option))
3711            .map(|index| PRIORITY_LEVELS[index])
3712    }
3713
3714    /// Every mapped option name, in the order a new `Priority` field lists them.
3715    fn names(&self) -> impl Iterator<Item = &str> {
3716        self.options.iter().map(PriorityOptionName::as_str)
3717    }
3718}
3719
3720/// One metadata value this source projects onto a board text field.
3721#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3722#[serde(deny_unknown_fields)]
3723pub struct MetadataFieldConfig {
3724    /// The name of the board text field the value is written to.
3725    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.
3726    /// The top-level metadata key the value is read from. A key under the reserved
3727    /// `onetaskgraph.` prefix is refused.
3728    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.
3729    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
3730    /// key's own value is the one projected. Left out of a serialized entry when empty.
3731    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3732    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
3733    #[schemars(!skip_serializing_if)]
3734    pub path: Vec<String>,
3735}
3736
3737/// The board fields no metadata value may be projected onto, compared ignoring case: the
3738/// three this source writes itself, and those GitHub owns on every board.
3739// 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.
3740const UNPROJECTABLE_FIELDS: [&str; 13] = [
3741    STATUS_FIELD,
3742    PRIORITY_FIELD,
3743    ORIGIN_FIELD,
3744    "Title",
3745    "Assignees",
3746    "Labels",
3747    "Linked pull requests",
3748    "Milestone",
3749    "Repository",
3750    "Reviewers",
3751    "Parent issue",
3752    "Sub-issues progress",
3753    "Type",
3754];
3755
3756/// One validated [`MetadataFieldConfig`] entry.
3757#[derive(Debug, Clone)]
3758struct MetadataField {
3759    field: ProjectedField,
3760    key: ProjectedKey,
3761    path: Vec<String>,
3762}
3763
3764/// The name of a board text field a metadata value is projected onto: never blank, never one
3765/// this source or GitHub writes, and never one another entry of the same source names.
3766#[derive(Debug, Clone, PartialEq, Eq)]
3767struct ProjectedField(String);
3768
3769/// The top-level metadata key a projected value is read from: never blank, and never under
3770/// the namespace this product reserves.
3771#[derive(Debug, Clone, PartialEq, Eq)]
3772struct ProjectedKey(String);
3773
3774impl std::ops::Deref for ProjectedField {
3775    type Target = str;
3776    fn deref(&self) -> &str {
3777        &self.0
3778    }
3779}
3780
3781impl std::ops::Deref for ProjectedKey {
3782    type Target = str;
3783    fn deref(&self) -> &str {
3784        &self.0
3785    }
3786}
3787
3788/// What one item's metadata says one projected field should hold.
3789#[derive(Debug, Clone, PartialEq, Eq)]
3790enum Projected {
3791    /// A string to hold.
3792    Text(String),
3793    /// Nothing: the key or a step of the path is absent, or the value is `null`.
3794    Nothing,
3795}
3796
3797impl MetadataField {
3798    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3799    /// cannot stand, naming the source and the entry.
3800    fn resolve(
3801        configured: Vec<MetadataFieldConfig>,
3802        instance: &SourceName,
3803    ) -> Result<Vec<Self>, SourceError> {
3804        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3805        for (index, entry) in configured.into_iter().enumerate() {
3806            let refuse = |why: String| SourceError::Config {
3807                message: format!(
3808                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3809                    entry.field, entry.key
3810                ),
3811            };
3812            if entry.field.trim().is_empty() {
3813                return Err(refuse("names a blank field".to_owned()));
3814            }
3815            if entry.key.trim().is_empty() {
3816                return Err(refuse("names a blank key".to_owned()));
3817            }
3818            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3819                return Err(refuse(format!(
3820                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
3821                     for its own keys; next: project a caller key instead",
3822                    MetadataKey::RESERVED_NAMESPACE
3823                )));
3824            }
3825            if let Some(owned) = UNPROJECTABLE_FIELDS
3826                .iter()
3827                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3828            {
3829                return Err(refuse(format!(
3830                    "names the board field {owned:?}, which this source or GitHub already \
3831                     writes; next: name a text field of your own"
3832                )));
3833            }
3834            if let Some(earlier) = resolved
3835                .iter()
3836                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3837            {
3838                return Err(refuse(format!(
3839                    "names the same board field as metadata_fields[{earlier}]; one field \
3840                     cannot hold two values"
3841                )));
3842            }
3843            resolved.push(Self {
3844                field: ProjectedField(entry.field),
3845                key: ProjectedKey(entry.key),
3846                path: entry.path,
3847            });
3848        }
3849        Ok(resolved)
3850    }
3851
3852    /// The path as a refusal spells it: `[]` for the key's own value.
3853    fn spelled_path(&self) -> String {
3854        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3855    }
3856
3857    /// What `metadata` says this field should hold, or the refusal for a value no text field
3858    /// can hold.
3859    fn projected(
3860        &self,
3861        metadata: &BTreeMap<String, Value>,
3862        instance: &SourceName,
3863    ) -> Result<Projected, SourceError> {
3864        let mut value = metadata.get(&*self.key);
3865        for step in &self.path {
3866            value = value.and_then(|held| held.get(step.as_str()));
3867        }
3868        let found = match value {
3869            None | Some(Value::Null) => return Ok(Projected::Nothing),
3870            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3871            Some(Value::Bool(_)) => "a boolean",
3872            Some(Value::Number(_)) => "a number",
3873            Some(Value::Array(_)) => "an array",
3874            Some(Value::Object(_)) => "an object",
3875        };
3876        Err(SourceError::Refused {
3877            message: format!(
3878                "source {instance} projects metadata key {:?} at path {} onto its board text \
3879                 field {:?}, and this item holds {found} there; only a string, null or nothing \
3880                 can be written to a text field; next: store a string there, or remove the \
3881                 value",
3882                &*self.key,
3883                self.spelled_path(),
3884                &*self.field
3885            ),
3886        })
3887    }
3888}
3889
3890/// What one item's `Priority` field says, read through this instance's mapping.
3891#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3892enum HeldPriority {
3893    /// A priority this source reports: an option the mapping names, or no value (`none`).
3894    Read(Priority),
3895    /// An option the mapping does not name, which is never read as a level or as `none`.
3896    Unmapped(String),
3897}
3898
3899/// How fast this source writes, and how long it waits out a rate-limit refusal.
3900///
3901/// Configurable because a GitHub Enterprise installation sets its own limits and an
3902/// operator who has already been refused may want to go slower still — not because the
3903/// defaults are guesses.
3904#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3905#[serde(default, deny_unknown_fields)]
3906pub struct PacingConfig {
3907    /// Shortest interval between two content-creating mutations, in milliseconds.
3908    ///
3909    /// Zero sends them as fast as they are asked for, which is what a fixture server on
3910    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3911    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.
3912    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3913    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3914    /// zero while there is a budget to spend, because a schedule of zero-length waits
3915    /// consumes none of it and so never ends.
3916    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.
3917    /// Total time one call may spend waiting out rate limits, in milliseconds.
3918    ///
3919    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3920    /// the bound is what makes this a wait rather than a hang.
3921    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.
3922}
3923
3924/// The largest any pacing setting may be, in milliseconds.
3925///
3926/// One hour. GitHub's own harshest published bound on content-generating requests works
3927/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3928/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3929/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3930/// and an interval beyond it is a command that never sends its second mutation. It also
3931/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3932/// what a `Duration` can hold on every platform.
3933pub const MAX_PACING_MS: u64 = 3_600_000;
3934
3935/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3936/// source holds.
3937///
3938/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3939/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3940/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3941/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3942/// is asked for, windows included.
3943#[derive(Debug, Clone, Copy)]
3944struct Pacing {
3945    /// Whether requests are held for GitHub's secondary windows: always, unless
3946    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3947    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3948    windows: bool,
3949    min_mutation_interval: Duration,
3950    retry_backoff: Duration,
3951    retry_budget: Duration,
3952}
3953
3954impl Pacing {
3955    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3956    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3957        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3958            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3959                message: format!(
3960                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3961                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3962                     GitHub's own harshest published limit"
3963                ),
3964            }),
3965            Some(value) => Ok(Duration::from_millis(value)),
3966            None => Ok(Duration::from_millis(default)),
3967        };
3968        let retry_backoff = bounded(
3969            config.retry_backoff_ms,
3970            RETRY_BACKOFF_MS,
3971            "retry_backoff_ms",
3972        )?;
3973        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3974        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3975            return Err(SourceError::Config {
3976                message: format!(
3977                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3978                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3979                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3980                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3981                     waiting at all",
3982                    retry_budget.as_millis()
3983                ),
3984            });
3985        }
3986        Ok(Self {
3987            windows: config.min_mutation_interval_ms != Some(0),
3988            min_mutation_interval: bounded(
3989                config.min_mutation_interval_ms,
3990                0,
3991                "min_mutation_interval_ms",
3992            )?,
3993            retry_backoff,
3994            retry_budget,
3995        })
3996    }
3997}
3998
3999/// Factory for [`GitHubProjectsSource`].
4000#[derive(Debug, Clone, Copy, Default)]
4001pub struct Plugin;
4002
4003impl SourcePlugin for Plugin {
4004    fn kind(&self) -> &'static str {
4005        KIND
4006    }
4007    /// The endpoint, the owner and the board's number: what names one board.
4008    fn remote_identity(&self, config: &Value) -> Option<String> {
4009        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
4010        Some(format!(
4011            "{}\n{}\n{}",
4012            config.endpoint, config.owner, config.project_number
4013        ))
4014    }
4015    fn config_schema(&self) -> Schema {
4016        schema_for!(GitHubProjectsConfig)
4017    }
4018    fn build(
4019        &self,
4020        name: &SourceName,
4021        config: &Value,
4022        secrets: &dyn SecretResolver,
4023    ) -> Result<Box<dyn TaskSource>, SourceError> {
4024        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
4025    }
4026
4027    fn build_with_clock(
4028        &self,
4029        name: &SourceName,
4030        config: &Value,
4031        secrets: &dyn SecretResolver,
4032        clock: SharedClock,
4033    ) -> Result<Box<dyn TaskSource>, SourceError> {
4034        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
4035    }
4036}
4037
4038/// The client `cell` holds, building it with `build` the first time it is asked for.
4039///
4040/// A failed build is the request's failure, with the message the source's construction gave it
4041/// before clients were deferred.
4042// 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`.
4043fn built(
4044    cell: &std::sync::OnceLock<Client>,
4045    build: impl FnOnce() -> Result<Client, SourceError>,
4046) -> Result<&Client, SourceError> {
4047    if let Some(client) = cell.get() {
4048        return Ok(client);
4049    }
4050    let client = build()?;
4051    Ok(cell.get_or_init(|| client))
4052}
4053
4054impl Plugin {
4055    /// Build a source recording every request it sends into an accounting the caller holds.
4056    ///
4057    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
4058    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
4059    /// session total rather than two — see [`accounting`] and
4060    /// [`GitHubProjectsSource::recording_into`].
4061    ///
4062    /// # Errors
4063    ///
4064    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
4065    /// [`SourceError::Config`] for configuration this plugin cannot use and
4066    /// [`SourceError::Auth`] for a credential it cannot find.
4067    pub fn build_recording_into(
4068        &self,
4069        name: &SourceName,
4070        config: &Value,
4071        secrets: &dyn SecretResolver,
4072        ledger: Arc<Accounting>,
4073    ) -> Result<Box<dyn TaskSource>, SourceError> {
4074        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
4075    }
4076
4077    fn build_recording_with_clock(
4078        &self,
4079        name: &SourceName,
4080        config: &Value,
4081        secrets: &dyn SecretResolver,
4082        ledger: Arc<Accounting>,
4083        clock: SharedClock,
4084    ) -> Result<Box<dyn TaskSource>, SourceError> {
4085        let config: GitHubProjectsConfig =
4086            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
4087                message: format!("source {name}: {e}"),
4088            })?;
4089        let prefix = format!("source {name}: ");
4090        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
4091            .map_err(|error| match error {
4092                // The shared `StatusMapping::distinct` names the source itself.
4093                SourceError::Config { message } if message.starts_with(&prefix) => {
4094                    SourceError::Config { message }
4095                }
4096                SourceError::Config { message } => SourceError::Config {
4097                    message: format!("{prefix}{message}"),
4098                },
4099                SourceError::Auth { message } => SourceError::Auth {
4100                    message: format!("source {name}: {message}"),
4101                },
4102                other => other,
4103            })?;
4104        source.clock = clock;
4105        Ok(Box::new(source))
4106    }
4107}
4108
4109/// Where a status category lands on this board, once configuration is resolved.
4110#[derive(Debug, Clone, PartialEq, Eq)]
4111enum StatusTarget {
4112    /// Not usable against this instance for this kind, and why.
4113    Disabled(UnmappedStatus),
4114    /// The board's `Status` option of this name.
4115    Column(ColumnName),
4116    /// A closed issue, with both its board option and the reason that says which closed it means.
4117    // 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.
4118    Terminal(ColumnName, ClosedState),
4119}
4120
4121/// Every status category, in the order the vocabulary declares them.
4122///
4123/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
4124/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
4125/// added to the shared vocabulary fails to compile until it is named there, and this
4126/// crate's suite reconciles this list against that enum's own derived schema, which is
4127/// generated from the variants rather than written beside them. The schema is what
4128/// catches a list left one short — a list checking only the positions it already holds
4129/// would pass while every mapping indexed by the new position panicked.
4130pub const CATEGORIES: [StatusCategory; 8] = [
4131    StatusCategory::Draft,
4132    StatusCategory::Backlog,
4133    StatusCategory::Todo,
4134    StatusCategory::Queued,
4135    StatusCategory::InProgress,
4136    StatusCategory::Done,
4137    StatusCategory::Cancelled,
4138    StatusCategory::Unknown,
4139];
4140
4141/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
4142#[must_use]
4143pub const fn category_position(category: StatusCategory) -> usize {
4144    match category {
4145        StatusCategory::Draft => 0,
4146        StatusCategory::Backlog => 1,
4147        StatusCategory::Todo => 2,
4148        StatusCategory::Queued => 3,
4149        StatusCategory::InProgress => 4,
4150        StatusCategory::Done => 5,
4151        StatusCategory::Cancelled => 6,
4152        StatusCategory::Unknown => 7,
4153    }
4154}
4155
4156/// The spelling a status category is configured and reported under.
4157fn category_name(category: StatusCategory) -> &'static str {
4158    match category {
4159        StatusCategory::Draft => "draft",
4160        StatusCategory::Backlog => "backlog",
4161        StatusCategory::Todo => "todo",
4162        StatusCategory::Queued => "queued",
4163        StatusCategory::InProgress => "in-progress",
4164        StatusCategory::Done => "done",
4165        StatusCategory::Cancelled => "cancelled",
4166        StatusCategory::Unknown => "unknown",
4167    }
4168}
4169
4170/// A shipped default's option name.
4171///
4172/// The literals below are this file's own and non-blank, and they are validated by the
4173/// one constructor a configured name goes through rather than beside it.
4174fn shipped_column(name: &'static str) -> ColumnName {
4175    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
4176}
4177
4178/// The shipped default for one category this instance's `status_mapping` does not mention,
4179/// for either kind.
4180fn shipped_default(category: StatusCategory) -> StatusTarget {
4181    match category {
4182        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
4183        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
4184        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
4185        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
4186        StatusCategory::Done => {
4187            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
4188        }
4189        StatusCategory::Cancelled => {
4190            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
4191        }
4192        StatusCategory::Draft | StatusCategory::Unknown => {
4193            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
4194        }
4195    }
4196}
4197
4198/// The two kinds a status is written and read for, each with its own half of the mapping.
4199const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
4200
4201/// This instance's complete category-to-target mapping for each kind, read in both
4202/// directions.
4203///
4204/// One target per category per kind, held at that category's own [`category_position`], so
4205/// a category missing from the mapping, named twice in it, or filed out of order is a state
4206/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
4207/// targets are options of the board's one `Status` field.
4208#[derive(Debug, Clone)]
4209struct BoardStatuses {
4210    tasks: [StatusTarget; CATEGORIES.len()],
4211    projects: [StatusTarget; CATEGORIES.len()],
4212}
4213
4214impl BoardStatuses {
4215    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
4216    /// would read back from one option.
4217    ///
4218    /// A category the mapping does not mention keeps its shipped default for both kinds; one
4219    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
4220    /// omits unmapped rather than defaulted.
4221    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
4222        let resolve_kind =
4223            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
4224                // `CATEGORIES[position] == category` for every category — the crate's suite
4225                // asserts it — so mapping the list in order fills each category's own slot.
4226                let mut targets = CATEGORIES.map(shipped_default);
4227                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
4228                    if !configured.mentions(category) {
4229                        continue;
4230                    }
4231                    *slot = match configured.name_for(category, kind) {
4232                        Err(why) => StatusTarget::Disabled(why),
4233                        Ok(name) => {
4234                            let option = ColumnName::try_from(name.as_str().to_owned())
4235                                .map_err(|message| SourceError::Config { message })?;
4236                            match category {
4237                                StatusCategory::Done => {
4238                                    StatusTarget::Terminal(option, ClosedState::Completed)
4239                                }
4240                                StatusCategory::Cancelled => {
4241                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
4242                                }
4243                                _ => StatusTarget::Column(option),
4244                            }
4245                        }
4246                    };
4247                }
4248                StatusMapping::distinct(
4249                    instance,
4250                    kind,
4251                    CATEGORIES
4252                        .iter()
4253                        .zip(&targets)
4254                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
4255                )?;
4256                Ok(targets)
4257            };
4258        Ok(Self {
4259            tasks: resolve_kind(ItemKind::Task)?,
4260            projects: resolve_kind(ItemKind::Project)?,
4261        })
4262    }
4263
4264    /// Every category's target for `kind`, in category order.
4265    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
4266        match kind {
4267            ItemKind::Task => &self.tasks,
4268            ItemKind::Project => &self.projects,
4269        }
4270    }
4271
4272    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
4273        &self.targets(kind)[category_position(category)]
4274    }
4275
4276    /// The category a board option name reports for `kind`, or `None` when nothing of that
4277    /// kind maps to it.
4278    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
4279        CATEGORIES.into_iter().find(|category| {
4280            self.target(kind, *category)
4281                .option()
4282                .is_some_and(|name| name.eq_ignore_ascii_case(option))
4283        })
4284    }
4285
4286    /// Every option name either kind maps a category to, each once ignoring case, in
4287    /// category order with a task's name before a project's — what the guarded setup asks
4288    /// the `Status` field to hold.
4289    fn wanted(&self) -> Vec<String> {
4290        let mut wanted: Vec<String> = Vec::new();
4291        for category in CATEGORIES {
4292            for kind in STATUS_KINDS {
4293                if let Some(name) = self.target(kind, category).option()
4294                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
4295                {
4296                    wanted.push(name.to_owned());
4297                }
4298            }
4299        }
4300        wanted
4301    }
4302
4303    /// The status an item of `kind` reports, from the three things a read of it says: its
4304    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
4305    ///
4306    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
4307    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
4308    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
4309    /// produce, so it is read permissively rather than refused — reads are faithful, and
4310    /// refusals belong on writes.
4311    ///
4312    /// **Its name follows the closure too, unless the option agrees with it.** The option's
4313    /// own name is kept when this kind's mapping reads that option as the same category, or
4314    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
4315    /// named `Shipped`. An option the mapping reads as another category contradicts the
4316    /// closure, and so does no option at all: the name is then the option the mapping gives
4317    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
4318    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
4319    /// it. An `unknown` closure keeps the option, or `Closed` without one.
4320    ///
4321    /// An open item's option reads through its own kind's mapping, and an option that mapping
4322    /// does not name reads as `Unknown` under its own name.
4323    ///
4324    /// One function of those three rather than of a response, so a narrow status write can
4325    /// answer what a re-read would report by applying it to the state it has just written.
4326    fn status(
4327        &self,
4328        kind: ItemKind,
4329        option: Option<&str>,
4330        closed: bool,
4331        reason: Option<&str>,
4332    ) -> Status {
4333        if closed {
4334            let category = closed_category(reason);
4335            let agrees = option.filter(|option| {
4336                category == StatusCategory::Unknown
4337                    || self
4338                        .category_of(kind, option)
4339                        .is_none_or(|read| read == category)
4340            });
4341            let name = agrees.or_else(|| self.target(kind, category).option());
4342            let fallback = match category {
4343                StatusCategory::Done => "Done",
4344                StatusCategory::Cancelled => "Cancelled",
4345                _ => "Closed",
4346            };
4347            return Status {
4348                category,
4349                name: name.or(option).unwrap_or(fallback).to_owned(),
4350            };
4351        }
4352        let name = option.unwrap_or("Open").to_owned();
4353        Status {
4354            category: self
4355                .category_of(kind, &name)
4356                .unwrap_or(StatusCategory::Unknown),
4357            name,
4358        }
4359    }
4360
4361    /// The option a closed item's `Status` has to hold for its card to agree with its
4362    /// closure, and the category that closure reads as — or `None` when the item is open,
4363    /// its closure reads `unknown`, or its option already agrees.
4364    ///
4365    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
4366    /// in line, each to the one option the mapping gives its category — an item with no
4367    /// option, or at one the mapping does not name, included.
4368    fn contradicted(
4369        &self,
4370        kind: ItemKind,
4371        option: Option<&str>,
4372        closed: bool,
4373        reason: Option<&str>,
4374    ) -> Option<(ClosedAs, &str)> {
4375        if !closed {
4376            return None;
4377        }
4378        let closed_as = match closed_category(reason) {
4379            StatusCategory::Done => ClosedAs::Done,
4380            StatusCategory::Cancelled => ClosedAs::Cancelled,
4381            _ => return None,
4382        };
4383        let wanted = self.target(kind, closed_as.category()).option()?;
4384        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
4385            return None;
4386        }
4387        Some((closed_as, wanted))
4388    }
4389}
4390
4391/// The category a closed issue reads as, from the reason it was closed with.
4392///
4393/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
4394/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
4395/// is one this source cannot read a category from.
4396fn closed_category(reason: Option<&str>) -> StatusCategory {
4397    match reason {
4398        None | Some("COMPLETED") => StatusCategory::Done,
4399        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
4400        Some(_) => StatusCategory::Unknown,
4401    }
4402}
4403
4404impl BoardStatuses {
4405    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
4406    /// case; a kind lacking none is left out.
4407    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
4408        STATUS_KINDS
4409            .into_iter()
4410            .filter_map(|kind| {
4411                let missing: Vec<String> = self
4412                    .targets(kind)
4413                    .iter()
4414                    .filter_map(StatusTarget::option)
4415                    .filter(|wanted| {
4416                        !existing
4417                            .iter()
4418                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4419                    })
4420                    .map(str::to_owned)
4421                    .collect();
4422                (!missing.is_empty()).then_some(KindMissing { kind, missing })
4423            })
4424            .collect()
4425    }
4426}
4427
4428impl StatusTarget {
4429    /// The board option this target selects, or `None` for an unmapped one.
4430    fn option(&self) -> Option<&str> {
4431        match self {
4432            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
4433            Self::Disabled(_) => None,
4434        }
4435    }
4436}
4437
4438// 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.
4439/// One repository this source can create an issue in, as `owner/name`.
4440///
4441/// Every `createIssue` this source sends names one of these: the item's own single
4442/// `repositories` entry — on a source declared private, only when it reads private — else
4443/// its parent project issue's repository, else the configured
4444/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
4445/// that choice and says what it refuses before `createIssue`.
4446// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
4447#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
4448#[serde(try_from = "String", into = "String")]
4449struct RepositoryTarget {
4450    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
4451    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
4452}
4453
4454impl RepositoryTarget {
4455    fn parse(value: &str) -> Result<Self, SourceError> {
4456        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
4457            message: format!(
4458                "repository must be spelled owner/name; {value:?} names no repository"
4459            ),
4460        })?;
4461        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
4462            return Err(SourceError::Config {
4463                message: format!(
4464                    "repository must be spelled owner/name with a GitHub login and one \
4465                     repository name; {value:?} is not"
4466                ),
4467            });
4468        }
4469        Ok(Self {
4470            owner: owner.to_owned(),
4471            name: name.to_owned(),
4472        })
4473    }
4474
4475    /// The one host whose repositories this source creates issues in, spelled once: it is
4476    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
4477    const HOST: &str = "github.com";
4478
4479    fn origin(&self) -> String {
4480        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
4481    }
4482
4483    /// The repository a normalized origin names, or why it is none this source can create
4484    /// an issue in: another host, or more or fewer than `owner/name` under this one.
4485    fn from_origin(origin: &Repository) -> Result<Self, String> {
4486        let not_here = || {
4487            format!(
4488                "{} is not a {}/owner/name repository",
4489                origin.as_str(),
4490                Self::HOST
4491            )
4492        };
4493        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
4494        if host != Self::HOST {
4495            return Err(not_here());
4496        }
4497        Self::parse(rest).map_err(|_| not_here())
4498    }
4499
4500    fn slug(&self) -> String {
4501        format!("{}/{}", self.owner, self.name)
4502    }
4503}
4504
4505/// Read back from a kept context as the `owner/name` it was written as, refused unless
4506/// [`RepositoryTarget::parse`] takes it.
4507impl TryFrom<String> for RepositoryTarget {
4508    type Error = String;
4509
4510    fn try_from(slug: String) -> Result<Self, Self::Error> {
4511        Self::parse(&slug).map_err(|error| error.to_string())
4512    }
4513}
4514
4515impl From<RepositoryTarget> for String {
4516    fn from(target: RepositoryTarget) -> Self {
4517        target.slug()
4518    }
4519}
4520
4521/// How long GitHub's issue search may go on missing a comment after it was made or edited,
4522/// which a read of the comments made since an instant sets its watermark back by: four times the
4523/// thirty seconds the credentialed lane has watched it lag.
4524pub const COMMENT_SEARCH_LAG: std::time::Duration = std::time::Duration::from_secs(120);
4525
4526/// A source which reads GitHub afresh for every operation.
4527pub struct GitHubProjectsSource {
4528    /// This source's configured name, used both to tell a far end naming this source
4529    /// from one naming a system it knows nothing about, and to name the instance a
4530    /// status refusal is about.
4531    name: SourceName,
4532    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
4533    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
4534    repository: Option<RepositoryTarget>,
4535    /// The logins whose repositories a create may file in; empty restricts nothing.
4536    creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after each entry passes GitHub's login grammar.
4537    endpoint: Url,
4538    token: SecretString,
4539    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
4540    statuses: BoardStatuses,
4541    /// Where each priority lands on this board, or `None` when this instance holds none.
4542    priorities: Option<PriorityMapping>,
4543    /// The metadata values this instance projects onto board text fields, in configured order.
4544    metadata_fields: Vec<MetadataField>,
4545    /// What this board's sub-issues mean.
4546    sub_issues: SubIssues,
4547    /// The link types this source mirrors as `relates-to`.
4548    link_types: onetaskgraph_plugin_api::LinkTypes,
4549    /// Whether this source's configuration opted in to assignees: every document it sends is
4550    /// then spelled by [`graphql::with_assignees`], and every task it reads carries them.
4551    assignees: bool,
4552    /// The GitHub node id of each person an assignee write of this process has named, by
4553    /// lowercased login — `None` for a login GitHub said it does not know — so a later call
4554    /// naming one sends no lookup. A write that fails, or is answered with other people, has the
4555    /// people it named forgotten when its call ends ([`Self::forgotten`]), so the next call
4556    /// naming them asks GitHub again while the rest of its own call does not.
4557    people: Mutex<BTreeMap<String, Option<String>>>,
4558    /// The node ids of the people a failed or mismatched assignee write of this call named,
4559    /// forgotten from [`Self::people`] when the call ends.
4560    forgotten: Mutex<Vec<String>>,
4561    /// GitHub's own spelling of each login this process looked up, by lowercased login: what a
4562    /// write that assigned a person reports them as, however the caller spelled them.
4563    spellings: Mutex<BTreeMap<String, String>>,
4564    /// Built on the first request rather than with the source, as [`Self::client`] says.
4565    client: std::sync::OnceLock<Client>,
4566    /// Built on the first attachment request, as [`Self::asset_client`] says.
4567    asset_client: std::sync::OnceLock<Client>,
4568    /// The markup the engine handed this source for its bodies' headings, applied to every
4569    /// visible body it composes and stripped from every one it reads back: see
4570    /// [`TaskSource::attach_body_markup`].
4571    body_markup: std::sync::OnceLock<onetaskgraph_plugin_api::SharedBodyMarkup>,
4572    /// Every item this source has created in this command, in the order it created them —
4573    /// dropped by [`TaskSource::end_command`].
4574    ///
4575    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
4576    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
4577    /// a copy resolving a dependency on an item it had just created refused it as not
4578    /// found. A board read is completed from this — an item remembered here and absent from
4579    /// the read is added back, because the board really does hold it and only the read is
4580    /// behind.
4581    ///
4582    /// It is not a cache of a user's work: nothing is remembered that this process did not
4583    /// itself just write, it lives and dies with the process, and it is never consulted for
4584    /// an item this source did not create.
4585    created: Mutex<Vec<Resolved>>,
4586    /// Every item that already existed and that this source has written in this command, as
4587    /// it wrote it — dropped by [`TaskSource::end_command`].
4588    ///
4589    /// The other half of [`Self::created`], held on the same terms and for the reason a
4590    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
4591    /// filter is an index behind a write this process made moments ago, so a query matching
4592    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
4593    /// is remembered that this process did not itself just write.
4594    updated: Mutex<Vec<Resolved>>,
4595    /// Every issue this source has added a comment to or edited a comment of in this command
4596    /// — dropped by [`TaskSource::end_command`].
4597    ///
4598    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
4599    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
4600    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
4601    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
4602    /// from before — until the index caught up. Each id here is a candidate of every such
4603    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
4604    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
4605    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
4606    /// process wrote is still found only once the index has it.
4607    commented: Mutex<Vec<NativeId>>,
4608    /// How fast this source writes, and how long it waits out a refusal.
4609    pacing: Pacing,
4610    /// When the last content-creating mutation finished, or the moment the furthest-out
4611    /// reserved slot releases the next one, whichever is later — so the one after it can be
4612    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
4613    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
4614    /// measured from.
4615    last_mutation: Mutex<Option<Duration>>,
4616    /// The `Date` GitHub stated on the last answer this source read: its own time, which the
4617    /// watermark of a comment read is set back from. See [`TaskSource::served_at`].
4618    last_served: Mutex<Option<DateTime<Utc>>>,
4619    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
4620    windows: Mutex<pacing::Windows>,
4621    /// The tasks a project holds, as a copy's batched read of the project and every one of them
4622    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
4623    /// a listing of that project's tasks is answered without a read of its own.
4624    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
4625    /// The web address of each issue this source created in the current command, as its
4626    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
4627    /// request.
4628    written: Mutex<BTreeMap<NativeId, String>>,
4629    /// What a copy has held back of the items it created, between
4630    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
4631    /// nothing is being held back, when every write lands whole as it is made.
4632    deferred: Mutex<Option<Deferred>>,
4633    /// Each existing issue's update held back while writes are, in the order it was made: sent
4634    /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
4635    /// this source sends or reads ever sees the board without them.
4636    held_updates: Mutex<Vec<HeldUpdate>>,
4637    /// The first page of comments a comment-activity read read of each candidate it confirmed,
4638    /// held for the one comment listing of that task that follows in this command — see
4639    /// `batch::hold_comment_pages`.
4640    held_comments: Mutex<BTreeMap<NativeId, Page<Comment>>>,
4641    /// The board field writes and issue updates a batch of targeted updates is holding back,
4642    /// while one runs — see `batch::TaskWrites`.
4643    task_writes: Mutex<Option<batch::TaskWrites>>,
4644    /// The change token each task a targeted update wrote in the current command holds after
4645    /// that write, by the task's id with its board item's id — spelled from the write's own
4646    /// last response, or from the read made right after it where no response showed the whole
4647    /// issue: what [`TaskSource::written_task`] answers as its `version`. Any later mutation
4648    /// naming the task or its board item drops its entry before it is sent, so an entry is
4649    /// never one write behind.
4650    /// A response that showed the issue whole but not every part of its token — a draft, or a
4651    /// page of labels or field values that did not end — records `None`, which a read would
4652    /// not improve on.
4653    written_tokens: Mutex<BTreeMap<NativeId, (String, Option<String>)>>,
4654    clock: SharedClock,
4655    /// Whether the configuration declares this source private, as
4656    /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
4657    /// named repository reads public is created — see [`Self::creation_rule`].
4658    declared_private: std::sync::atomic::AtomicBool,
4659    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
4660    /// The board as this process last read it, for the length of one command — dropped by
4661    /// [`TaskSource::end_command`].
4662    ///
4663    /// A copy of a project used to re-read the whole board, paged, before writing each of
4664    /// its items, which is by far the largest part of a copy's request count and none of
4665    /// its work. Nothing else changes this board while a command runs — this source's own
4666    /// writes are the only writer — so one read answers them all.
4667    ///
4668    /// It is not a store of a user's work and it is not the cache the no-persistence
4669    /// invariant forbids: it lives and dies with the process exactly as `created` does,
4670    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
4671    /// an item this command created and then depends on resolves whether or not GitHub's
4672    /// own eventually-consistent read has caught up. A write to an item already on the
4673    /// board updates the entry here too, so what this holds is the last read plus this
4674    /// process's own writes rather than a snapshot taken before them.
4675    board_cache: Mutex<Option<Board>>,
4676    /// Every issue this board's own search reported, for the length of one command — dropped
4677    /// by [`TaskSource::end_command`].
4678    ///
4679    /// The second half of a board read, and cached for the same reason and on the same
4680    /// terms as the first: it lives and dies with the process, nothing is written down, and
4681    /// a write this process makes updates the entry here exactly as it updates the one in
4682    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
4683    /// that lists this board's projects and its tasks pays for one search rather than two.
4684    search_cache: Mutex<Option<Vec<Resolved>>>,
4685    /// What each narrowed question GitHub was asked answered, keyed by that question, for
4686    /// the length of one command — dropped by [`TaskSource::end_command`].
4687    ///
4688    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
4689    /// and dies with the process, nothing is written down, a write this process makes
4690    /// updates the entry here as it updates the other two, and every answer is completed
4691    /// with this process's own writes each time it is given. A command that asks the same
4692    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
4693    /// write — pays for it once, which is what the whole-board read it replaced gave it.
4694    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
4695    search_next: Mutex<BTreeMap<String, Option<String>>>,
4696    /// Records already resolved in this command, reused by writes and for comment identity.
4697    /// Explicit item reads still reach GitHub. Nothing is persisted, and
4698    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
4699    /// its item as a person has since left it.
4700    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
4701    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
4702    /// answered as holding nothing — each taken by the next read of that item by id and by
4703    /// nothing else, dropped by any mutation naming it, and cleared by
4704    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
4705    /// read-before-write, made in the same request as the others' rather than on its own.
4706    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
4707    /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
4708    /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
4709    /// beside the members of the one project it names, when it names one — or for the next
4710    /// request this source makes, whichever comes first.
4711    pending_origins: Mutex<Vec<String>>,
4712    /// Each project's members as a lookup carrying them read them in this command, trimmed to
4713    /// their classification and repositories, taken by [`TaskSource::member_classifications`]
4714    /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
4715    member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
4716    /// Every item a listing of one project's members carried whole in this command — the
4717    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
4718    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
4719    /// it again. No other read is answered from it, so a read by id outside a copy's
4720    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
4721    /// item, and cleared by [`TaskSource::end_command`].
4722    carried: Mutex<BTreeMap<NativeId, Resolved>>,
4723    /// Items an issue read by reference reached, held for the one status write each was read
4724    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
4725    /// what such a write needs. Dropped by [`TaskSource::end_command`].
4726    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
4727    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
4728    /// asked for under, with the project that selector named — for the length of one command,
4729    /// dropped by [`TaskSource::end_command`].
4730    ///
4731    /// A caller pages through a project's tasks one engine page at a time, and every page
4732    /// is cut from the whole list of them, so without this each page walked every
4733    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
4734    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
4735    /// dies with the process, nothing is written down, a write this process makes updates or
4736    /// removes the entry here as it does there, and every answer is completed with this
4737    /// process's own writes each time it is given.
4738    children_cache: Mutex<ProjectChildren>,
4739    /// The board's own id and field definitions as this process last read them on their
4740    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
4741    ///
4742    /// What a write needs of the board and its item does not say, read once per command
4743    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
4744    /// lives and dies with the process and nothing is written down. It holds no item and so
4745    /// can answer no question about one — see [`Self::board_fields`].
4746    fields_cache: Mutex<Option<BoardFields>>,
4747    /// Each destination repository's node id, resolved once per repository
4748    /// rather than per issue created.
4749    ///
4750    /// A repository's node id does not change, and re-reading it for every issue of a copy
4751    /// spent one request per item on an answer this source already had. It is a map rather
4752    /// than one entry because a copy files each item in the repository its own
4753    /// `repositories` field names, so a plan across five repositories asks GitHub five
4754    /// times and not once per item.
4755    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
4756    /// What every request this source sends is recorded into.
4757    ///
4758    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
4759    /// a request leaves this crate, so nothing has to be switched on for a session to be
4760    /// counted. It is shared rather than owned so a caller accounting for a whole session —
4761    /// its own schema verification, board lookups, residue sweep and cleanup beside this
4762    /// source's reads and writes — adds up one accounting instead of two. See
4763    /// [`accounting`] for what a record carries and what a session's spend is and is not.
4764    ledger: Arc<Accounting>,
4765    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
4766    /// open, the issues primed from the cache, and the comments known on each.
4767    expected: expected::Held,
4768    /// What [`projection`] holds for a producer's projection: a kept placement awaiting its
4769    /// setup-context token, and where the last create was placed.
4770    projection: projection::Held,
4771}
4772
4773/// The board item a single field update's answer names, refused unless it is `item_id`.
4774fn field_update_item<'a>(data: &'a Value, item_id: &str) -> Result<&'a Value, SourceError> {
4775    let returned = data
4776        .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
4777        .ok_or_else(|| SourceError::Malformed {
4778            message: "GitHub field update returned no project item".into(),
4779        })?;
4780    if required_str(returned, "id")? != item_id {
4781        return Err(SourceError::Malformed {
4782            message: "GitHub field update returned the wrong project item".into(),
4783        });
4784    }
4785    Ok(returned)
4786}
4787
4788/// GitHub's closed single-select color vocabulary.
4789#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
4790#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
4791pub enum StatusOptionColor {
4792    /// Gray.
4793    Gray,
4794    /// Blue.
4795    Blue,
4796    /// Green.
4797    Green,
4798    /// Yellow.
4799    Yellow,
4800    /// Purple.
4801    Purple,
4802    /// Red.
4803    Red,
4804    /// Orange.
4805    Orange,
4806    /// Pink.
4807    Pink,
4808}
4809
4810/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
4811/// applies its additions.
4812#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4813pub enum SetupMode {
4814    /// Read without mutation.
4815    Plan,
4816    /// Apply and verify.
4817    Apply,
4818}
4819
4820/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
4821/// against it goes on compiling.
4822pub type StatusOptionsMode = SetupMode;
4823
4824/// The explicit result of the requested operation.
4825#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4826#[serde(rename_all = "kebab-case")]
4827pub enum StatusOptionsOutcome {
4828    /// A read-only plan.
4829    Planned,
4830    /// Apply found nothing missing.
4831    Unchanged,
4832    /// Additions were applied and verified.
4833    Applied,
4834}
4835
4836/// A GitHub single-select option's opaque GraphQL node identifier.
4837#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4838#[serde(transparent)]
4839pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4840
4841impl TryFrom<String> for StatusOptionId {
4842    type Error = String;
4843
4844    fn try_from(id: String) -> Result<Self, Self::Error> {
4845        if id.trim().is_empty() {
4846            return Err("a GitHub Status option id cannot be blank".to_owned());
4847        }
4848        Ok(Self(id))
4849    }
4850}
4851
4852/// One existing or proposed option in a guarded Status-field update.
4853#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4854pub struct StatusOption {
4855    /// GitHub's stable id.
4856    pub id: StatusOptionId,
4857    /// The visible option name.
4858    pub name: ColumnName,
4859    /// GitHub's single-select color token.
4860    pub color: StatusOptionColor,
4861    /// The option description, including an empty one.
4862    pub description: String,
4863}
4864
4865/// One board item's Status assignment, retained as recovery data.
4866#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4867pub struct StatusAssignment {
4868    /// The project item id whose assignment this is.
4869    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4870    // carried verbatim as operator recovery data; introducing a semantic type would claim
4871    // validation rules GitHub does not publish and no operation here interprets.
4872    pub item_id: String,
4873    /// The selected option, absent when the item has no status.
4874    #[serde(skip_serializing_if = "Option::is_none")]
4875    pub option: Option<AssignedStatusOption>,
4876}
4877
4878/// The inseparable id and name of an assigned option.
4879#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4880pub struct AssignedStatusOption {
4881    /// GitHub's stable id.
4882    pub id: StatusOptionId,
4883    /// The visible name.
4884    pub name: ColumnName,
4885}
4886
4887/// The plan and verified outcome of reconciling configured Status options.
4888#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4889pub struct StatusOptionsReport {
4890    /// The configured source name.
4891    pub source: SourceName,
4892    /// Configured option names absent before the operation.
4893    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4894    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4895    // serialized string here preserves the report's intentionally simple public contract.
4896    pub missing: Vec<String>,
4897    /// What the requested operation did.
4898    pub outcome: StatusOptionsOutcome,
4899    /// The complete option list observed before any mutation.
4900    pub existing: Vec<StatusOption>,
4901}
4902
4903#[derive(Debug, Clone, PartialEq, Eq)]
4904struct StatusSnapshot {
4905    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4906    // passed back as the mutation's project identity; a newtype could enforce no stronger
4907    // invariant because GitHub publishes no grammar for it.
4908    board_id: String,
4909    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4910    // passed back as the mutation's field identity; a newtype could enforce no stronger
4911    // invariant because GitHub publishes no grammar for it.
4912    field_id: String,
4913    options: Vec<StatusOption>,
4914    assignments: Vec<StatusAssignment>,
4915}
4916
4917/// The name of the board field a status is held in.
4918const STATUS_FIELD: &str = "Status";
4919
4920/// Every item's value of each field `report` names, as it stood before the setup wrote
4921/// anything — what a person puts back when the setup is refused part way.
4922fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4923    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4924        .fields
4925        .iter()
4926        .map(|field| (field.field.name(), before.assignments(field.field)))
4927        .collect();
4928    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4929        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4930    })
4931}
4932
4933/// One board field the guarded setup reads and writes — every one it reads, and the only
4934/// ones it writes.
4935#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4936pub enum BoardField {
4937    /// The single-select `Status` field every instance's `status_mapping` resolves into.
4938    Status,
4939    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4940    Priority,
4941}
4942
4943impl BoardField {
4944    /// The field's name on the board.
4945    #[must_use]
4946    pub const fn name(self) -> &'static str {
4947        match self {
4948            Self::Status => STATUS_FIELD,
4949            Self::Priority => PRIORITY_FIELD,
4950        }
4951    }
4952
4953    /// The field a board calls `name`, or `None` for one this setup does not own.
4954    fn named(name: &str) -> Option<Self> {
4955        [Self::Status, Self::Priority]
4956            .into_iter()
4957            .find(|field| field.name() == name)
4958    }
4959}
4960
4961/// What the guarded setup did to one field.
4962#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4963#[serde(rename_all = "kebab-case")]
4964pub enum FieldOutcome {
4965    /// A read-only plan.
4966    Planned,
4967    /// Apply found the field there with every configured option.
4968    Unchanged,
4969    /// Missing options were added to the field that was there, and verified.
4970    Applied,
4971    /// The field was not there; it was created holding the configured options, and verified.
4972    Created,
4973}
4974
4975/// One field's plan, or its verified outcome.
4976#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4977pub struct FieldReport {
4978    /// Which field.
4979    pub field: BoardField,
4980    /// Whether the board had the field before the operation.
4981    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4982    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4983    // "outcome", "existing"}` — so folding one into the other would change a published JSON
4984    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4985    // one constructor, and it derives `outcome` from `exists` in one match.
4986    pub exists: bool,
4987    /// Configured option names the field lacked before the operation — every one of them,
4988    /// in the order a new field lists them, when the field was not there at all.
4989    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4990    // mapping name and has therefore already passed its nonblank validation; the serialized
4991    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4992    pub missing: Vec<String>,
4993    /// For the `Status` field, which item kind each missing name is configured for: one
4994    /// entry per kind `status_mapping` names a missing option for, task before project, each
4995    /// listing that kind's missing names in category order. A name both kinds use is in
4996    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4997    /// `Priority`, which only a task holds.
4998    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4999    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
5000    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
5001    #[schemars(!skip_serializing_if)]
5002    pub kinds: Vec<KindMissing>,
5003    /// What the requested operation did.
5004    pub outcome: FieldOutcome,
5005    /// The field's complete option list observed before any mutation; empty when the field
5006    /// was not there.
5007    pub existing: Vec<StatusOption>,
5008}
5009
5010/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
5011#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
5012pub struct KindMissing {
5013    /// The kind these names are configured for.
5014    pub kind: ItemKind,
5015    /// The names that kind maps a category to and the field lacked, in category order.
5016    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
5017    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
5018    // intentionally simple public contract.
5019    pub missing: Vec<String>,
5020}
5021
5022/// The plan and verified outcome of setting up every field a source's configuration names.
5023#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
5024pub struct FieldsReport {
5025    /// The configured source name.
5026    pub source: SourceName,
5027    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
5028    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
5029    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
5030    // per field would change a published JSON shape. The states the list could hold and the
5031    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
5032    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
5033    pub fields: Vec<FieldReport>,
5034    /// Each board text field the source's `metadata_fields` projects a value onto, in
5035    /// configured order. Empty — and left out of the JSON — when it projects none.
5036    #[serde(default, skip_serializing_if = "Vec::is_empty")]
5037    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
5038    // both SDKs model an absent list as an empty one rather than as `null`.
5039    #[schemars(!skip_serializing_if)]
5040    pub metadata_fields: Vec<MetadataFieldReport>,
5041}
5042
5043/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
5044/// its GraphQL type when it has none — which is what a projected field's conflict reports.
5045///
5046/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
5047/// no conflict.
5048// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
5049// refuses exactly what `try_from` below does.
5050#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
5051#[serde(transparent)]
5052pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
5053
5054/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
5055/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
5056/// neither SDK's validator can be relied on to support.
5057const NON_TEXT_FIELD_TYPE_PATTERN: &str =
5058    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
5059
5060impl TryFrom<String> for NonTextFieldType {
5061    type Error = String;
5062
5063    fn try_from(kind: String) -> Result<Self, Self::Error> {
5064        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
5065            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
5066        }
5067        if kind == "TEXT" {
5068            return Err("a TEXT field is no conflict for a projected text field".to_owned());
5069        }
5070        Ok(Self(kind))
5071    }
5072}
5073
5074impl std::fmt::Display for NonTextFieldType {
5075    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5076        formatter.write_str(&self.0)
5077    }
5078}
5079
5080/// One projected metadata text field's plan, or its verified outcome.
5081///
5082/// The setup creates a missing one as a text field and never alters a field that is there:
5083/// a same-named field of another type is reported in `conflict` and left as it is.
5084#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
5085pub struct MetadataFieldReport {
5086    /// The board field's name.
5087    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.
5088    /// The metadata key the value is read from.
5089    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
5090    /// The object keys walked inside that key's value; empty for the key's own value.
5091    pub path: Vec<String>,
5092    /// Whether the board had a text field of that name before the operation.
5093    // 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.
5094    pub exists: bool,
5095    /// The type of a field of that name that is not a text field, which the setup leaves as
5096    /// it is; absent — and left out of the JSON — when there is none.
5097    #[serde(default, skip_serializing_if = "Option::is_none")]
5098    pub conflict: Option<NonTextFieldType>,
5099    /// What the requested operation did: `planned`; `created` for a field that was not
5100    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
5101    pub outcome: FieldOutcome,
5102}
5103
5104/// Which options one field is configured with, in the order a new field would list them.
5105struct FieldPlan {
5106    field: BoardField,
5107    wanted: Vec<String>,
5108}
5109
5110/// One single-select field as the guarded setup snapshots it.
5111#[derive(Debug, Clone, PartialEq, Eq)]
5112struct SnapshotField {
5113    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
5114    // passed back as the mutation's field identity; a newtype could enforce no stronger
5115    // invariant because GitHub publishes no grammar for it.
5116    field_id: String,
5117    options: Vec<StatusOption>,
5118}
5119
5120/// Every single-select field of a board and every item's value of each.
5121#[derive(Debug, Clone, PartialEq, Eq)]
5122struct BoardSnapshot {
5123    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
5124    // passed back as the mutation's project identity; a newtype could enforce no stronger
5125    // invariant because GitHub publishes no grammar for it.
5126    board_id: String,
5127    fields: BTreeMap<BoardField, SnapshotField>,
5128    /// Each board item's id, and its value of each field this setup owns that it holds one of.
5129    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
5130}
5131
5132impl BoardSnapshot {
5133    /// Every item's value of `field`, in board order — the recovery data a drift refusal
5134    /// carries.
5135    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
5136        self.items
5137            .iter()
5138            .map(|(item_id, values)| StatusAssignment {
5139                item_id: item_id.clone(),
5140                option: values.get(&field).cloned(),
5141            })
5142            .collect()
5143    }
5144}
5145
5146impl GitHubProjectsSource {
5147    /// `content` as this source sends it, with the markup the engine handed it applied.
5148    fn marked(&self, content: &str) -> String {
5149        match self.body_markup.get() {
5150            Some(markup) if !content.is_empty() => markup.written(content),
5151            _ => content.to_owned(),
5152        }
5153    }
5154
5155    /// The visible body and the metadata slot of `body` as [`metadata_body`] splits them,
5156    /// the visible body without the markup [`Self::marked`] applies.
5157    fn visible(
5158        &self,
5159        body: Option<String>,
5160    ) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
5161        let (visible, slot) = metadata_body(body)?;
5162        Ok((
5163            match (visible, self.body_markup.get()) {
5164                (Some(visible), Some(markup)) => {
5165                    Some(markup.read(&visible)).filter(|read| !read.is_empty())
5166                }
5167                (visible, _) => visible,
5168            },
5169            slot,
5170        ))
5171    }
5172
5173    /// Report missing configured Status options and, when `apply` is true, add them with
5174    /// a whole-list mutation that preserves every existing id and verifies the result.
5175    ///
5176    /// # Errors
5177    ///
5178    /// Refuses a board without a single-select `Status` field. A post-write difference in
5179    /// any pre-existing option id or item assignment is refused with the complete pre-write
5180    /// assignment snapshot in the diagnostic for recovery.
5181    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
5182    // successful mutation, both drift refusals, source selection, missing Status, casing,
5183    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
5184    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
5185    // responses from entering the defensive malformed-response branches below.
5186    pub async fn status_options(
5187        &self,
5188        mode: StatusOptionsMode,
5189    ) -> Result<StatusOptionsReport, SourceError> {
5190        let before = self.status_snapshot().await?;
5191        // A terminal category's option is as configured as an open one's: a terminal
5192        // write validates it before closing and refuses when the board lacks it. Both
5193        // kinds' names are options of the one field, so both are asked for.
5194        let missing = self
5195            .statuses
5196            .wanted()
5197            .into_iter()
5198            .filter(|wanted| {
5199                !before
5200                    .options
5201                    .iter()
5202                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
5203            })
5204            .collect::<Vec<_>>();
5205        let report = StatusOptionsReport {
5206            source: self.name.clone(),
5207            missing: missing.clone(),
5208            outcome: match (mode, missing.is_empty()) {
5209                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
5210                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
5211                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
5212            },
5213            existing: before.options.clone(),
5214        };
5215        if mode == StatusOptionsMode::Plan || missing.is_empty() {
5216            return Ok(report);
5217        }
5218        let mut options = before
5219            .options
5220            .iter()
5221            .map(|option| {
5222                json!({
5223                    "id": option.id, "name": option.name, "color": option.color,
5224                    "description": option.description,
5225                })
5226            })
5227            .collect::<Vec<_>>();
5228        options.extend(missing.iter().map(|name| {
5229            json!({
5230                "name": name, "color": "GRAY", "description": ""
5231            })
5232        }));
5233        self.graphql(
5234            graphql::STATUS_OPTIONS_UPDATE,
5235            json!({"input": {
5236                "fieldId": before.field_id,
5237                "singleSelectOptions": options,
5238            }}),
5239        )
5240        .await?;
5241        let after = self.status_snapshot().await?;
5242        let options_preserved = before
5243            .options
5244            .iter()
5245            .all(|old| after.options.iter().any(|new| new == old));
5246        let additions_present = missing.iter().all(|wanted| {
5247            after
5248                .options
5249                .iter()
5250                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
5251        });
5252        if !options_preserved || !additions_present || after.assignments != before.assignments {
5253            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
5254                SourceError::Malformed {
5255                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
5256                }
5257            })?;
5258            return Err(SourceError::Refused {
5259                message: format!(
5260                    "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}"
5261                ),
5262            });
5263        }
5264        Ok(report)
5265    }
5266
5267    /// A fresh snapshot of the Status field and every board item's assignment of it.
5268    ///
5269    /// # Errors
5270    ///
5271    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
5272    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
5273        // Status alone, as this operation has always read it: a `Priority` field is another
5274        // operation's, so nothing about it can refuse this one.
5275        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
5276        let field = board
5277            .fields
5278            .remove(&BoardField::Status)
5279            .ok_or_else(|| self.no_status_field())?;
5280        Ok(StatusSnapshot {
5281            assignments: board.assignments(BoardField::Status),
5282            board_id: board.board_id,
5283            field_id: field.field_id,
5284            options: field.options,
5285        })
5286    }
5287
5288    /// The refusal a board with no `Status` field is answered with by the guarded setup.
5289    fn no_status_field(&self) -> SourceError {
5290        SourceError::Refused {
5291            message: format!("source {} board has no Status field", self.name),
5292        }
5293    }
5294
5295    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
5296    // the real CLI loopback journey, including pagination. The individual malformed guards
5297    // are defensive validation of a schema-pinned third-party response, not separate user
5298    // journeys; drift and missing-field failures cover the operation's recovery behavior.
5299    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
5300    /// every board item's value of each, walked to the end of the board's items. A field not
5301    /// in `owned` is read past whatever it holds.
5302    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
5303        let mut after: Option<String> = None;
5304        let mut snapshot: Option<BoardSnapshot> = None;
5305        loop {
5306            let data = self
5307                .graphql(
5308                    graphql::STATUS_OPTIONS_SNAPSHOT,
5309                    json!({
5310                        "owner": self.owner, "number": self.project_number,
5311                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
5312                    }),
5313                )
5314                .await?;
5315            let board = data
5316                .pointer("/owner/projectV2")
5317                .filter(|board| board.is_object())
5318                .ok_or_else(|| SourceError::Refused {
5319                    message: format!(
5320                        "source {} has no accessible GitHub Projects board",
5321                        self.name
5322                    ),
5323                })?;
5324            if board
5325                .pointer("/fields/pageInfo/hasNextPage")
5326                .and_then(Value::as_bool)
5327                != Some(false)
5328            {
5329                return Err(SourceError::Malformed {
5330                    message:
5331                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
5332                            .into(),
5333                });
5334            }
5335            let mut fields = BTreeMap::new();
5336            // Only the fields this setup owns, by name: a node the single-select fragment did not
5337            // match carries no name, and a person's own single-select field — a `Size`, a
5338            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
5339            // `Status` or `Priority` field without its options is malformed, not absent.
5340            // 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.
5341            for (owned, field) in board
5342                .pointer("/fields/nodes")
5343                .and_then(Value::as_array)
5344                .ok_or_else(|| SourceError::Malformed {
5345                    message: "GitHub project fields.nodes is not an array".into(),
5346                })?
5347                .iter()
5348                .filter_map(|field| {
5349                    let named = BoardField::named(field.get("name")?.as_str()?)?;
5350                    owned.contains(&named).then_some((named, field))
5351                })
5352            {
5353                let options = field
5354                    .get("options")
5355                    .and_then(Value::as_array)
5356                    .ok_or_else(|| SourceError::Malformed {
5357                        message: "GitHub single-select field options is not an array".into(),
5358                    })?
5359                    .iter()
5360                    .map(|option| {
5361                        Ok(StatusOption {
5362                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
5363                                .map_err(|message| SourceError::Malformed { message })?,
5364                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
5365                                .map_err(|message| SourceError::Malformed {
5366                                    message: format!(
5367                                        "GitHub single-select option name is invalid: {message}"
5368                                    ),
5369                                })?,
5370                            color: serde_json::from_value(
5371                                option.get("color").cloned().unwrap_or(Value::Null),
5372                            )
5373                            .map_err(|error| {
5374                                SourceError::Malformed {
5375                                    message: format!(
5376                                        "GitHub single-select option color is invalid: {error}"
5377                                    ),
5378                                }
5379                            })?,
5380                            description: optional_str(option, "description")?
5381                                .unwrap_or_default()
5382                                .to_owned(),
5383                        })
5384                    })
5385                    .collect::<Result<Vec<_>, SourceError>>()?;
5386                let snapshot = SnapshotField {
5387                    field_id: required_nonblank_str(field, "id")?.to_owned(),
5388                    options,
5389                };
5390                // A board's field names are unique, so a second one is an answer that cannot
5391                // say which field the setup would act on — refused rather than one chosen.
5392                if fields.insert(owned, snapshot).is_some() {
5393                    return Err(SourceError::Malformed {
5394                        message: format!(
5395                            "GitHub answered two {} fields for this board",
5396                            owned.name()
5397                        ),
5398                    });
5399                }
5400            }
5401            let board_id = required_nonblank_str(board, "id")?.to_owned();
5402            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
5403                board_id,
5404                fields,
5405                items: Vec::new(),
5406            });
5407            let items = board
5408                .pointer("/items/nodes")
5409                .and_then(Value::as_array)
5410                .ok_or_else(|| SourceError::Malformed {
5411                    message: "GitHub project items.nodes is not an array".into(),
5412                })?;
5413            for item in items {
5414                let field_values =
5415                    item.get("fieldValues")
5416                        .ok_or_else(|| SourceError::Malformed {
5417                            message: "GitHub project item is missing fieldValues".into(),
5418                        })?;
5419                if field_values
5420                    .pointer("/pageInfo/hasNextPage")
5421                    .and_then(Value::as_bool)
5422                    != Some(false)
5423                {
5424                    return Err(SourceError::Malformed {
5425                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
5426                    });
5427                }
5428                let values = item
5429                    .pointer("/fieldValues/nodes")
5430                    .and_then(Value::as_array)
5431                    .ok_or_else(|| SourceError::Malformed {
5432                        message: "GitHub project item fieldValues.nodes is not an array".into(),
5433                    })?;
5434                let item_id = required_nonblank_str(item, "id")?;
5435                let mut assigned = BTreeMap::new();
5436                for value in values {
5437                    let Some(field) = value
5438                        .pointer("/field/name")
5439                        .and_then(Value::as_str)
5440                        .and_then(BoardField::named)
5441                        .filter(|field| owned.contains(field))
5442                    else {
5443                        continue;
5444                    };
5445                    let held = assigned.insert(
5446                        field,
5447                        AssignedStatusOption {
5448                            id: StatusOptionId::try_from(
5449                                required_str(value, "optionId")?.to_owned(),
5450                            )
5451                            .map_err(|message| SourceError::Malformed { message })?,
5452                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
5453                                .map_err(|message| SourceError::Malformed {
5454                                    message: format!(
5455                                        "GitHub assigned {} name is invalid: {message}",
5456                                        field.name()
5457                                    ),
5458                                })?,
5459                        },
5460                    );
5461                    // An item holds one value of a field, so a second one leaves no way to
5462                    // tell which it holds — and a verification or recovery built on either
5463                    // could restore the wrong one.
5464                    if held.is_some() {
5465                        return Err(SourceError::Malformed {
5466                            message: format!(
5467                                "GitHub answered two {} values for board item {item_id}",
5468                                field.name()
5469                            ),
5470                        });
5471                    }
5472                }
5473                current.items.push((item_id.to_owned(), assigned));
5474            }
5475            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
5476                message: "GitHub project is missing items".into(),
5477            })?;
5478            let has_next = page
5479                .pointer("/pageInfo/hasNextPage")
5480                .and_then(Value::as_bool)
5481                .ok_or_else(|| SourceError::Malformed {
5482                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
5483                })?;
5484            if !has_next {
5485                break;
5486            }
5487            let next =
5488                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
5489            validate_cursor_progress(after.as_deref(), next)?;
5490            after = Some(next.to_owned());
5491        }
5492        snapshot.ok_or_else(|| SourceError::Malformed {
5493            message: "GitHub returned no board field snapshot".into(),
5494        })
5495    }
5496    // llmlint: ignore-end[changed_behavior_has_e2e]
5497
5498    /// Report every board field this source's configuration names and, with
5499    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
5500    /// the `Priority` field when the board has none.
5501    ///
5502    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
5503    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
5504    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
5505    /// color and description: the whole option list goes back with every existing id, because
5506    /// a re-minted id clears every item's value.
5507    ///
5508    /// # Errors
5509    ///
5510    /// Refuses a board without a single-select `Status` field. After an apply the board is
5511    /// read again, and a pre-existing option or any item's value of either field that moved is
5512    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
5513    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
5514    // unchanged apply, a created field, an added option to each field, drift refusal, a board
5515    // with no Status field and a non-github-projects source through the compiled CLI against
5516    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
5517    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
5518        let owned: Vec<BoardField> = if self.priorities.is_some() {
5519            vec![BoardField::Status, BoardField::Priority]
5520        } else {
5521            vec![BoardField::Status]
5522        };
5523        let before = self.board_snapshot(&owned).await?;
5524        let mut plans = vec![FieldPlan {
5525            field: BoardField::Status,
5526            wanted: self.statuses.wanted(),
5527        }];
5528        if !before.fields.contains_key(&BoardField::Status) {
5529            return Err(self.no_status_field());
5530        }
5531        if let Some(mapping) = &self.priorities {
5532            plans.push(FieldPlan {
5533                field: BoardField::Priority,
5534                wanted: mapping.names().map(str::to_owned).collect(),
5535            });
5536        }
5537        // The snapshot reads single-select fields alone, so a field it did not find may still
5538        // be on the board under the name, of another type: creating one beside it would fail
5539        // part way, or leave two fields of one name. Asked of the board's own field list, and
5540        // only when a field is missing.
5541        if plans
5542            .iter()
5543            .any(|plan| !before.fields.contains_key(&plan.field))
5544        {
5545            let board = self.board_fields().await?;
5546            for plan in plans
5547                .iter()
5548                .filter(|plan| !before.fields.contains_key(&plan.field))
5549            {
5550                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
5551                    return Err(SourceError::Refused {
5552                        message: format!(
5553                            "source {}'s board has a {} field that is not a single-select field \
5554                             (it is a {}), so it cannot hold this source's options; next: rename \
5555                             or remove that field, then run this again",
5556                            self.name,
5557                            plan.field.name(),
5558                            optional_str(field, "__typename")?.unwrap_or("field of another type")
5559                        ),
5560                    });
5561                }
5562            }
5563        }
5564        let mut reports = Vec::new();
5565        for plan in &plans {
5566            let held = before.fields.get(&plan.field);
5567            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
5568            let mut missing: Vec<String> = Vec::new();
5569            for wanted in &plan.wanted {
5570                let present = existing
5571                    .iter()
5572                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
5573                    || missing
5574                        .iter()
5575                        .any(|named| named.eq_ignore_ascii_case(wanted));
5576                if !present {
5577                    missing.push(wanted.clone());
5578                }
5579            }
5580            let kinds = match plan.field {
5581                BoardField::Status => self.statuses.missing_by_kind(&existing),
5582                BoardField::Priority => Vec::new(),
5583            };
5584            reports.push(FieldReport {
5585                field: plan.field,
5586                exists: held.is_some(),
5587                kinds,
5588                outcome: match (mode, held.is_some(), missing.is_empty()) {
5589                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
5590                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
5591                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
5592                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
5593                },
5594                missing,
5595                existing,
5596            });
5597        }
5598        // A projected field is a text field, which the single-select snapshot cannot see, so
5599        // it is asked of the board's own field list — and only when one is configured.
5600        let mut metadata_fields = Vec::new();
5601        if !self.metadata_fields.is_empty() {
5602            let board = self.board_fields().await?;
5603            for projection in &self.metadata_fields {
5604                let conflict = match Board::field(&board.fields, &projection.field)? {
5605                    None => None,
5606                    Some(field) => {
5607                        let typename = optional_str(field, "__typename")?;
5608                        let data_type = optional_str(field, "dataType")?;
5609                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
5610                            None
5611                        } else {
5612                            let named = data_type
5613                                .filter(|kind| *kind != "TEXT")
5614                                .or(typename)
5615                                .unwrap_or("field of another type");
5616                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
5617                                |message| SourceError::Malformed {
5618                                    message: format!(
5619                                        "GitHub answered the board's {} field with a type this \
5620                                         setup cannot read: {message}",
5621                                        &*projection.field
5622                                    ),
5623                                },
5624                            )?)
5625                        }
5626                    }
5627                };
5628                let exists =
5629                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
5630                metadata_fields.push(MetadataFieldReport {
5631                    field: projection.field.to_string(),
5632                    key: projection.key.to_string(),
5633                    path: projection.path.clone(),
5634                    exists,
5635                    outcome: match (mode, exists || conflict.is_some()) {
5636                        (SetupMode::Plan, _) => FieldOutcome::Planned,
5637                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
5638                        (SetupMode::Apply, false) => FieldOutcome::Created,
5639                    },
5640                    conflict,
5641                });
5642            }
5643        }
5644        let report = FieldsReport {
5645            source: self.name.clone(),
5646            fields: reports,
5647            metadata_fields,
5648        };
5649        let writes: Vec<&FieldReport> = report
5650            .fields
5651            .iter()
5652            .filter(|field| !field.missing.is_empty() || !field.exists)
5653            .collect();
5654        let creates: Vec<&MetadataFieldReport> = report
5655            .metadata_fields
5656            .iter()
5657            .filter(|field| field.outcome == FieldOutcome::Created)
5658            .collect();
5659        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
5660            return Ok(report);
5661        }
5662        let mut landed: Vec<&str> = Vec::new();
5663        for field in &writes {
5664            let added = field
5665                .missing
5666                .iter()
5667                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
5668            let sent = match before.fields.get(&field.field) {
5669                Some(held) => {
5670                    let mut options = held
5671                        .options
5672                        .iter()
5673                        .map(|option| {
5674                            json!({
5675                                "id": option.id, "name": option.name, "color": option.color,
5676                                "description": option.description,
5677                            })
5678                        })
5679                        .collect::<Vec<_>>();
5680                    options.extend(added);
5681                    self.graphql(
5682                        graphql::STATUS_OPTIONS_UPDATE,
5683                        json!({"input": {
5684                            "fieldId": held.field_id,
5685                            "singleSelectOptions": options,
5686                        }}),
5687                    )
5688                    .await
5689                }
5690                None => {
5691                    self.graphql(
5692                        graphql::CREATE_FIELD,
5693                        json!({"input": {
5694                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
5695                            "name": field.field.name(),
5696                            "singleSelectOptions": added.collect::<Vec<_>>(),
5697                        }}),
5698                    )
5699                    .await
5700                }
5701            };
5702            // A mutation that failed does not establish that GitHub left its field as it was,
5703            // so every failure from here on carries the recovery data a drift refusal does.
5704            match sent {
5705                Ok(_) => landed.push(field.field.name()),
5706                Err(error) => {
5707                    let changed = if landed.is_empty() {
5708                        String::new()
5709                    } else {
5710                        format!("changed the {} field and then ", landed.join(" and "))
5711                    };
5712                    return Err(SourceError::Refused {
5713                        message: format!(
5714                            "the guarded field setup {changed}failed on the {} field, which it may \
5715                             have changed part way: {error}; the pre-write item assignments \
5716                             are:\n{}",
5717                            field.field.name(),
5718                            recovery(&report, &before)?
5719                        ),
5720                    });
5721                }
5722            }
5723        }
5724        for field in &creates {
5725            let sent = self
5726                .graphql(
5727                    graphql::CREATE_FIELD,
5728                    json!({"input": {
5729                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
5730                    }}),
5731                )
5732                .await;
5733            if let Err(error) = sent {
5734                let changed = if landed.is_empty() {
5735                    String::new()
5736                } else {
5737                    format!("changed the {} field and then ", landed.join(" and "))
5738                };
5739                return Err(SourceError::Refused {
5740                    message: format!(
5741                        "the guarded field setup {changed}failed creating the {} text field: \
5742                         {error}; run it again to create what is still missing",
5743                        field.field
5744                    ),
5745                });
5746            }
5747            landed.push(&field.field);
5748        }
5749        // The board has been written, so a verification read that fails leaves it unverified
5750        // rather than unchanged, and says what to put back.
5751        let after = match self.board_snapshot(&owned).await {
5752            Ok(after) => after,
5753            Err(error) => {
5754                return Err(SourceError::Refused {
5755                    message: format!(
5756                        "the guarded field setup changed the {} field and then could not read the \
5757                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
5758                        landed.join(" and "),
5759                        recovery(&report, &before)?
5760                    ),
5761                });
5762            }
5763        };
5764        let mut moved = Vec::new();
5765        for field in &report.fields {
5766            let name = field.field.name();
5767            let now = after
5768                .fields
5769                .get(&field.field)
5770                .map(|held| held.options.as_slice())
5771                .unwrap_or_default();
5772            if !field.existing.iter().all(|old| now.contains(old)) {
5773                moved.push(format!(
5774                    "a pre-existing {name} option id, name, color or description"
5775                ));
5776            }
5777            if !field.missing.iter().all(|wanted| {
5778                now.iter()
5779                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
5780            }) {
5781                moved.push(format!("an added {name} option"));
5782            }
5783            if after.assignments(field.field) != before.assignments(field.field) {
5784                moved.push(format!("an item's {name} value"));
5785            }
5786        }
5787        if !creates.is_empty() {
5788            // Read afresh: the view held for this command is the one from before the creates.
5789            *self.fields_cache()? = None;
5790            let board = match self.board_fields().await {
5791                Ok(board) => board,
5792                Err(error) => {
5793                    return Err(SourceError::Refused {
5794                        message: format!(
5795                            "the guarded field setup changed the {} field and then could not \
5796                             read the board back to verify it: {error}; run it again to verify \
5797                             it, which creates nothing a second time",
5798                            landed.join(" and ")
5799                        ),
5800                    });
5801                }
5802            };
5803            for field in &creates {
5804                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
5805                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
5806                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
5807                });
5808                if !text {
5809                    moved.push(format!("the created {} text field", field.field));
5810                }
5811            }
5812        }
5813        if !moved.is_empty() {
5814            return Err(SourceError::Refused {
5815                message: format!(
5816                    "GitHub changed {} after the guarded field setup; the pre-write item \
5817                     assignments are:\n{}",
5818                    moved.join(", "),
5819                    recovery(&report, &before)?
5820                ),
5821            });
5822        }
5823        Ok(report)
5824    }
5825
5826    /// Validate configuration and capture the named credential without exposing it.
5827    ///
5828    /// # Errors
5829    ///
5830    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
5831    /// [`SourceError::Auth`] when the named credential is missing or empty.
5832    pub fn new(
5833        name: &SourceName,
5834        config: GitHubProjectsConfig,
5835        secrets: &dyn SecretResolver,
5836    ) -> Result<Self, SourceError> {
5837        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
5838    }
5839
5840    /// The same, recording every request it sends into an accounting the caller holds too.
5841    ///
5842    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
5843    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
5844    /// up — passes the one it records those into, so the session total accounts for the
5845    /// whole session rather than for this source's share of it.
5846    ///
5847    /// # Errors
5848    ///
5849    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
5850    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
5851    pub fn recording_into(
5852        name: &SourceName,
5853        config: GitHubProjectsConfig,
5854        secrets: &dyn SecretResolver,
5855        ledger: Arc<Accounting>,
5856    ) -> Result<Self, SourceError> {
5857        if !valid_github_owner(&config.owner) {
5858            return Err(SourceError::Config {
5859                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
5860            });
5861        }
5862        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
5863            return Err(SourceError::Config {
5864                message: format!("project_number must be between 1 and {}", i32::MAX),
5865            });
5866        }
5867        if !valid_environment_name(&config.token_env) {
5868            return Err(SourceError::Config {
5869                message: "token_env must be a valid environment-variable name".into(),
5870            });
5871        }
5872        let repository = config
5873            .repository
5874            .as_deref()
5875            .map(RepositoryTarget::parse)
5876            .transpose()?;
5877        if let Some(entry) = config
5878            .creation_owners
5879            .iter()
5880            .find(|entry| !valid_github_owner(entry))
5881        {
5882            return Err(SourceError::Config {
5883                message: format!(
5884                    "creation_owners entry {entry:?} is not a GitHub login: a login is 1-39 ASCII \
5885                     letters, digits, or single hyphens, and cannot start or end with a hyphen"
5886                ),
5887            });
5888        }
5889        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5890            message: format!("endpoint is not a valid URL: {e}"),
5891        })?;
5892        if endpoint.scheme() != "https"
5893            && !(endpoint.scheme() == "http"
5894                && endpoint
5895                    .host_str()
5896                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5897        {
5898            return Err(SourceError::Config {
5899                message:
5900                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5901                        .into(),
5902            });
5903        }
5904        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5905            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),
5906        })?;
5907        Ok(Self {
5908            name: name.clone(),
5909            owner: config.owner,
5910            project_number: config.project_number,
5911            repository,
5912            creation_owners: config.creation_owners,
5913            asset_client: std::sync::OnceLock::new(),
5914            body_markup: std::sync::OnceLock::new(),
5915            endpoint,
5916            token,
5917            credential_name: config.token_env,
5918            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5919            priorities: config
5920                .priority_mapping
5921                .map(|mapping| PriorityMapping::resolve(mapping, name))
5922                .transpose()?,
5923            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5924            sub_issues: config.sub_issues,
5925            assignees: config.assignees,
5926            people: Mutex::new(BTreeMap::new()),
5927            forgotten: Mutex::new(Vec::new()),
5928            spellings: Mutex::new(BTreeMap::new()),
5929            link_types: onetaskgraph_plugin_api::LinkTypes::checked(
5930                KIND,
5931                &config.link_types,
5932                &[relations::RELATES_TO],
5933                relations::REFUSED_WORDS,
5934            )
5935            .map_err(|error| match error {
5936                SourceError::Config { message } => SourceError::Config {
5937                    message: format!("source {name}: {message}"),
5938                },
5939                other => other,
5940            })?,
5941            client: std::sync::OnceLock::new(),
5942            created: Mutex::new(Vec::new()),
5943            updated: Mutex::new(Vec::new()),
5944            commented: Mutex::new(Vec::new()),
5945            pacing: Pacing::resolve(config.pacing, name)?,
5946            last_mutation: Mutex::new(None),
5947            last_served: Mutex::new(None),
5948            windows: Mutex::new(pacing::Windows::default()),
5949            task_children: Mutex::new(BTreeMap::new()),
5950            written: Mutex::new(BTreeMap::new()),
5951            deferred: Mutex::new(None),
5952            held_updates: Mutex::new(Vec::new()),
5953            held_comments: Mutex::new(BTreeMap::new()),
5954            task_writes: Mutex::new(None),
5955            written_tokens: Mutex::new(BTreeMap::new()),
5956            clock: system_clock(),
5957            declared_private: std::sync::atomic::AtomicBool::new(false),
5958            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5959            board_cache: Mutex::new(None),
5960            search_cache: Mutex::new(None),
5961            narrowed_cache: Mutex::new(BTreeMap::new()),
5962            resolved_cache: Mutex::new(BTreeMap::new()),
5963            read_ahead: Mutex::new(BTreeMap::new()),
5964            pending_origins: Mutex::new(Vec::new()),
5965            member_classes: Mutex::new(BTreeMap::new()),
5966            carried: Mutex::new(BTreeMap::new()),
5967            held_cache: Mutex::new(BTreeMap::new()),
5968            children_cache: Mutex::new(BTreeMap::new()),
5969            search_next: Mutex::new(BTreeMap::new()),
5970            fields_cache: Mutex::new(None),
5971            repository_cache: Mutex::new(BTreeMap::new()),
5972            ledger,
5973            expected: expected::Held::default(),
5974            projection: projection::Held::default(),
5975        })
5976    }
5977
5978    /// The client every API request goes through, built on the first one.
5979    ///
5980    /// Not with the source: building a client loads the system's certificate store, which
5981    /// costs tens of milliseconds, and a command that configures this board but never sends to
5982    /// it — a read of another source — should not pay that in its process.
5983    fn client(&self) -> Result<&Client, SourceError> {
5984        built(&self.client, || {
5985            Client::builder()
5986                .user_agent("onetaskgraph")
5987                .build()
5988                .map_err(|e| SourceError::Config {
5989                    message: format!("cannot build HTTP client: {e}"),
5990                })
5991        })
5992    }
5993
5994    /// The client attachment requests go through, with its own redirect policy, built on the
5995    /// first one for the reason [`Self::client`] gives.
5996    fn asset_client(&self) -> Result<&Client, SourceError> {
5997        built(&self.asset_client, || assets::client(&self.endpoint))
5998    }
5999
6000    /// A snapshot of every request this source has sent, and what each cost.
6001    ///
6002    /// A value to hold and compare rather than a borrow of the accounting itself, so two
6003    /// of them can sit side by side. When this source was built with
6004    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
6005    /// point of building it that way.
6006    #[must_use]
6007    pub fn accounting(&self) -> accounting::Session {
6008        self.ledger.snapshot()
6009    }
6010
6011    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
6012    /// rate limit rather than handing it straight back as an error.
6013    ///
6014    /// Retrying is safe for every document here, including the mutations, and the reason
6015    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
6016    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
6017    /// this replays has already taken effect. An outcome this source cannot know — the
6018    /// send failed, or the body could not be read, so the mutation may well have landed —
6019    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
6020    /// attempt. A duplicate write would come from replaying one of those, and none is
6021    /// replayed.
6022    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
6023        // Updates held back go out before any other request, so whatever it reads or writes
6024        // sees the board as though each had been sent when it was made.
6025        Box::pin(self.send_held_updates()).await?;
6026        // Origins held back for a read ahead that never came are sent before anything else.
6027        if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
6028            Box::pin(self.send_pending_origins(None)).await?;
6029        }
6030        let journaling = self.expected.journaling();
6031        let spelling = self.spelling();
6032        if !journaling && !spelling {
6033            return self.graphql_sent(query, variables).await;
6034        }
6035        // While a write the store's cache expects runs, its mutations select the whole issue
6036        // they leave behind, so the response vouches for what the cache keeps — and every
6037        // request is recorded, so the write's parts are reported from their own responses.
6038        // A targeted update sends a twin of each selecting the issue's token parts inline, at
6039        // the same price, so the version it answers is the one its own last response spells.
6040        let (query, variables) = match (journaling, graphql::whole_twin(query)) {
6041            (true, Some(twin)) => (twin, expected::whole_variables(variables)),
6042            _ => match graphql::token_twin(query).filter(|_| spelling) {
6043                Some(twin) => (twin, token_variables(variables)),
6044                None => (query, variables),
6045            },
6046        };
6047        let sent = self.graphql_sent(query, variables.clone()).await;
6048        if spelling && let Ok(data) = &sent {
6049            self.record_written_token(query, &variables, data);
6050        }
6051        if journaling {
6052            self.journal(query, &variables, &sent).await;
6053        }
6054        sent
6055    }
6056
6057    /// Whether a targeted update of this source is running, whose mutations are each sent as
6058    /// the twin selecting the whole issue (see [`Self::written_tokens`]).
6059    fn spelling(&self) -> bool {
6060        SPELLING
6061            .try_with(|source| *source == std::ptr::from_ref(self) as usize)
6062            .unwrap_or(false)
6063    }
6064
6065    /// Record the change token the issue a mutation's response shows whole holds now, as
6066    /// [`TaskSource::written_task`] answers it; a response showing no whole issue records
6067    /// nothing.
6068    fn record_written_token(&self, document: &str, variables: &Value, data: &Value) {
6069        let (issue, _) = expected::read_back(document, variables, data);
6070        if let Some(issue) = issue {
6071            self.record_written_issue(issue);
6072        }
6073    }
6074
6075    /// Record the change token `issue`, as a write's own answer shows it whole, holds now — or,
6076    /// where that answer lacks a part of one, that it was shown and spells none.
6077    fn record_written_issue(&self, issue: &Value) {
6078        // An answer naming the issue by its id alone did not show it, so it says nothing.
6079        if issue.get("updatedAt").is_none() {
6080            return;
6081        }
6082        let Some(id) = issue.get("id").and_then(Value::as_str) else {
6083            return;
6084        };
6085        let (item, token) = match self.written_token_of(issue) {
6086            Some((item, token)) => (item, Some(token)),
6087            None => {
6088                // Answered whole, but without every part of a token: its board item's id, where
6089                // it says one, so a later write naming that item still drops the entry.
6090                let item = issue
6091                    .pointer("/projectItems/nodes")
6092                    .and_then(Value::as_array)
6093                    .and_then(|nodes| self.board_entry(nodes))
6094                    .and_then(|entry| entry.get("id"))
6095                    .and_then(Value::as_str)
6096                    .unwrap_or_default()
6097                    .to_owned();
6098                (item, None)
6099            }
6100        };
6101        if let Ok(mut tokens) = self.written_tokens.lock() {
6102            tokens.insert(NativeId(id.to_owned()), (item, token));
6103        }
6104    }
6105
6106    /// Record `token` as the change token the task `item` holds after this command's write.
6107    fn remember_written_token(&self, item: &Resolved, token: String) {
6108        if let Ok(mut tokens) = self.written_tokens.lock() {
6109            tokens.insert(item.id.clone(), (item.item_id.clone(), Some(token)));
6110        }
6111    }
6112
6113    /// The change token the task `id` holds after this command's latest write of it, when that
6114    /// write's own response — or the read made right after it — showed one.
6115    fn written_token(&self, id: &NativeId) -> Option<String> {
6116        self.written_answer(id).flatten()
6117    }
6118
6119    /// What this command's latest write of the task `id` showed of it: `Some` once a response
6120    /// showed the issue whole — its token, or `None` where that response could not spell one —
6121    /// and `None` where no response did.
6122    fn written_answer(&self, id: &NativeId) -> Option<Option<String>> {
6123        self.written_tokens
6124            .lock()
6125            .ok()?
6126            .get(id)
6127            .map(|(_, token)| token.clone())
6128    }
6129
6130    /// Drop the written token of every task `variables` names, by its id or its board item's,
6131    /// before a mutation that may move it is sent.
6132    fn forget_written_tokens(&self, variables: &Value) {
6133        let Ok(mut tokens) = self.written_tokens.lock() else {
6134            return;
6135        };
6136        if tokens.is_empty() {
6137            return;
6138        }
6139        let named: Vec<&str> = variables
6140            .as_object()
6141            .into_iter()
6142            .flat_map(|arguments| arguments.values())
6143            .flat_map(|input| {
6144                WRITTEN_NAMES
6145                    .iter()
6146                    .filter_map(|key| input.get(key).and_then(Value::as_str))
6147            })
6148            .collect();
6149        tokens.retain(|id, (item, _)| {
6150            !named
6151                .iter()
6152                .any(|name| *name == id.0 || *name == item.as_str())
6153        });
6154    }
6155
6156    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
6157    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
6158        if is_mutation(query) {
6159            self.forget_written_tokens(&variables);
6160        }
6161        if is_mutation(query)
6162            && ![
6163                graphql::ADD_COMMENT,
6164                graphql::UPDATE_COMMENT,
6165                graphql::DELETE_COMMENT,
6166                graphql::ADD_COMMENT_WHOLE,
6167                graphql::UPDATE_COMMENT_WHOLE,
6168            ]
6169            .contains(&query)
6170        {
6171            let mut cache = self.resolved_cache()?;
6172            let mut ahead = self.read_ahead()?;
6173            let mut carried = self.carried()?;
6174            for argument in ["input", "second", "third", "clear"] {
6175                if let Some(input) = variables.get(argument) {
6176                    let names = |id: &NativeId, item_id: Option<&str>| {
6177                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
6178                            input
6179                                .get(key)
6180                                .and_then(Value::as_str)
6181                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
6182                        })
6183                    };
6184                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
6185                    ahead.retain(|id, item| {
6186                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
6187                    });
6188                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
6189                }
6190            }
6191        }
6192        let doing = operation_description(query);
6193        let mutation = is_mutation(query);
6194        let charge = pacing::Charge::graphql(query, &variables);
6195        let mut waited = Duration::ZERO;
6196        let mut waits = 0_u32;
6197        let mut backoff = self.pacing.retry_backoff;
6198        loop {
6199            let ticket = self.pace(charge, mutation).await;
6200            // Every write but a create is asked for immediately before it is sent — a create's
6201            // fields, its relationships, its content and the delete that takes it back included
6202            // — so a copy that owns the create renews its hold first, and one that has lost it
6203            // sends nothing more. Asked again before a retry, which is sent later.
6204            if mutation
6205                && !query.contains("createIssue(")
6206                && let Err(refused) = <dyn onetaskgraph_plugin_api::WriteGuard>::check().await
6207            {
6208                self.paced(ticket, mutation);
6209                return Err(refused);
6210            }
6211            let attempt = self.send_once(query, &variables, waits + 1).await;
6212            self.paced(ticket, mutation);
6213            let remaining = self.pacing.retry_budget.saturating_sub(waited);
6214            let (wait, cause) = match attempt {
6215                Ok(data) => return Ok(data),
6216                Err(Attempt::Failed(error)) => return Err(error),
6217                Err(Attempt::Conflicted(said)) => {
6218                    // A create is never replayed: see `CONFLICT_WORDINGS`.
6219                    if query.contains("createIssue(") || backoff.is_zero() || backoff > remaining {
6220                        return Err(SourceError::Unavailable {
6221                            message: format!(
6222                                "GitHub refused this source while {doing} as a temporary \
6223                                 conflict ({said}); it retried {} over {} and the conflict \
6224                                 did not clear within the {} one call may spend waiting. \
6225                                 next: run the command again once nothing else is writing to \
6226                                 the same items",
6227                                plural(waits, "time"),
6228                                seconds(waited),
6229                                seconds(self.pacing.retry_budget),
6230                            ),
6231                        });
6232                    }
6233                    (backoff, pacing::cause::BACKOFF)
6234                }
6235                Err(Attempt::Limited(limited)) => {
6236                    let (wait, cause) = limited.wait(backoff);
6237                    // A wait of nothing spends none of the budget, so it is exhaustion rather
6238                    // than a retry. `Pacing::resolve` rules out every way of configuring one
6239                    // except a budget of zero, where reporting the first refusal is the ask.
6240                    if wait.is_zero() || wait > remaining {
6241                        return Err(limited.exhausted(
6242                            doing,
6243                            waits,
6244                            waited,
6245                            wait,
6246                            self.pacing.retry_budget,
6247                        ));
6248                    }
6249                    (wait, cause)
6250                }
6251            };
6252            self.clock.sleep_because(wait, cause).await;
6253            waited += wait;
6254            waits += 1;
6255            backoff = backoff.saturating_mul(2);
6256        }
6257    }
6258
6259    /// Hold one request charging `charge` until every secondary window it charges has room for
6260    /// the whole of it — and, for a mutation of an instance configured with
6261    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
6262    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
6263    ///
6264    /// The moment is decided and counted under the lock and waited out outside it, so two
6265    /// callers take two places rather than the same one, and no lock is held across an await.
6266    /// A request with room goes out at once: nothing here waits that a window does not ask for.
6267    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
6268        let (wait, cause, ticket) = {
6269            // A poisoned lock here costs pacing, not correctness, and refusing the write over
6270            // it would turn an earlier failure into a second one for no gain.
6271            let mut windows = self
6272                .windows
6273                .lock()
6274                .unwrap_or_else(std::sync::PoisonError::into_inner);
6275            let now = self.clock.now();
6276            let (mut at, mut cause) = if self.pacing.windows {
6277                windows.room(now, charge)
6278            } else {
6279                (now, None)
6280            };
6281            if mutation && !self.pacing.min_mutation_interval.is_zero() {
6282                let mut last = self
6283                    .last_mutation
6284                    .lock()
6285                    .unwrap_or_else(std::sync::PoisonError::into_inner);
6286                // `checked_add` rather than `+`: adding durations can panic on overflow, and
6287                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
6288                if let Some(earliest) = last
6289                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
6290                    && earliest > at
6291                {
6292                    at = earliest;
6293                    cause = Some(pacing::cause::MIN_INTERVAL);
6294                }
6295                *last = Some(at);
6296            }
6297            let ticket = windows.charge(at, charge);
6298            (at.saturating_sub(now), cause, ticket)
6299        };
6300        if let (false, Some(cause)) = (wait.is_zero(), cause) {
6301            self.clock.sleep_because(wait, cause).await;
6302        }
6303        ticket
6304    }
6305
6306    /// Record that the request `ticket` was charged for has finished, so each window — and
6307    /// the configured spacing — counts it from here rather than from the moment it left.
6308    ///
6309    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
6310    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
6311    /// departure would hand the limiter a window that looked roomier than it is, so a source
6312    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
6313    /// paced well inside a board's threshold was once refused by it on a slower machine while
6314    /// passing on a quick one. The previous request had arrived before its response came back,
6315    /// so counting from here is never earlier than the limiter does, whatever transit costs
6316    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
6317    /// reads for the next fifty minutes.
6318    ///
6319    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
6320    /// one that never left costs only room nobody needed.
6321    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
6322        let now = self.clock.now();
6323        self.windows
6324            .lock()
6325            .unwrap_or_else(std::sync::PoisonError::into_inner)
6326            .finish(ticket, now);
6327        if mutation && !self.pacing.min_mutation_interval.is_zero() {
6328            let mut last = self
6329                .last_mutation
6330                .lock()
6331                .unwrap_or_else(std::sync::PoisonError::into_inner);
6332            // `max` rather than an assignment: a concurrent caller may already have reserved a
6333            // slot further out, and completing this request must never pull that slot back in.
6334            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
6335        }
6336    }
6337
6338    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
6339    /// failure that waiting cannot help — and recorded, whichever of the three it was.
6340    ///
6341    /// This is the one place a request leaves this crate, which is why the accounting is
6342    /// here rather than at each of the callers: a read path added later is counted without
6343    /// anybody remembering to count it, and
6344    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
6345    /// when one is not.
6346    async fn send_once(
6347        &self,
6348        query: &str,
6349        variables: &Value,
6350        number: u32,
6351    ) -> Result<Value, Attempt> {
6352        // A lookup goes out sized to the people it names, and the record names and counts it as
6353        // what it is; everything that decides how it is retried or read compares `query`, the
6354        // constant it was written as.
6355        let sized = graphql::sized(query, variables);
6356        let executed = self.ledger.priced_document(sized.as_ref());
6357        let Attempted {
6358            result,
6359            limits,
6360            reported_cost,
6361            response_outcome,
6362        } = self.attempt(executed.as_ref(), variables).await;
6363        // No `otherwise` name: every document this source sends is one of its own, and the
6364        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
6365        let sending = accounting::Request::graphql(sized.as_ref(), variables, None, reported_cost);
6366        let outcome = match &result {
6367            Ok(_) => accounting::Outcome::Answered,
6368            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
6369            Err(Attempt::Failed(_) | Attempt::Conflicted(_)) => accounting::Outcome::Refused,
6370        };
6371        self.ledger
6372            .record(sending.finished(outcome, limits).with_execution(
6373                Some(executed.into_owned()),
6374                response_outcome,
6375                number,
6376            ));
6377        result
6378    }
6379
6380    /// The attempt itself, with what its response said about the rate limit alongside.
6381    ///
6382    /// The two are returned together rather than recorded here because every one of the
6383    /// early exits below is a different outcome, and a record written at each of them is a
6384    /// record one of them can be added without.
6385    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
6386        let mut limits = accounting::RateLimit::default();
6387        let mut reported_cost = None;
6388        let mut response_outcome = accounting::ResponseOutcome::TransportError;
6389        // Spelled here, at the one place a document leaves for GitHub, and nowhere earlier:
6390        // everything that decides how a document is retried or how its answer is read compares
6391        // it with the constant it was written as.
6392        let spelled = self
6393            .assignees
6394            .then(|| graphql::with_assignees(query))
6395            .flatten();
6396        let result = self
6397            .attempted(
6398                query,
6399                spelled.as_deref().unwrap_or(query),
6400                variables,
6401                &mut limits,
6402                &mut reported_cost,
6403                &mut response_outcome,
6404            )
6405            .await;
6406        Attempted {
6407            result,
6408            limits,
6409            reported_cost,
6410            response_outcome,
6411        }
6412    }
6413
6414    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
6415    async fn attempted(
6416        &self,
6417        query: &str,
6418        sent: &str,
6419        variables: &Value,
6420        limits: &mut accounting::RateLimit,
6421        reported_cost: &mut Option<u64>,
6422        response_outcome: &mut accounting::ResponseOutcome,
6423    ) -> Result<Value, Attempt> {
6424        let response = self
6425            .client()
6426            .map_err(Attempt::Failed)?
6427            .post(self.endpoint.clone())
6428            .bearer_auth(self.token.expose_secret())
6429            .json(&json!({"query": sent, "variables": variables}))
6430            .send()
6431            .await
6432            .map_err(|e| {
6433                Attempt::Failed(SourceError::Unavailable {
6434                    message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
6435                })
6436            })?;
6437        let status = response.status();
6438        if !status.is_success() {
6439            *response_outcome = accounting::ResponseOutcome::HttpError;
6440        }
6441        let header = |name: &str| whole_seconds(response.headers().get(name));
6442        let served = response
6443            .headers()
6444            .get(reqwest::header::DATE)
6445            .and_then(|value| value.to_str().ok())
6446            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
6447            .map(|at| at.with_timezone(&Utc));
6448        *limits = accounting::RateLimit::read(|name| {
6449            response
6450                .headers()
6451                .get(name)
6452                .and_then(|value| value.to_str().ok())
6453                .map(str::to_owned)
6454        });
6455        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
6456        // that are not text at all — is "not known to be exhausted". This never makes a
6457        // response a refusal on its own: it says which limiter a refusal is attributed to
6458        // and where its hint comes from, so a value this cannot read costs a hint rather
6459        // than an answer.
6460        let exhausted = response
6461            .headers()
6462            .get("x-ratelimit-remaining")
6463            .and_then(|value| value.to_str().ok())
6464            == Some("0");
6465        // `retry-after` is what GitHub asks for when it asks; when it does not and the
6466        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
6467        // which is the same question answered as an absolute time. Nothing else here is a
6468        // hint, and a schedule is what answers a refusal that carries none.
6469        let retry_after = header("retry-after");
6470        let hinted_by = if retry_after.is_some() || !exhausted {
6471            Hint::RetryAfter
6472        } else {
6473            Hint::PrimaryReset
6474        };
6475        let hint = retry_after.or_else(|| {
6476            exhausted
6477                .then(|| header("x-ratelimit-reset"))
6478                .flatten()
6479                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
6480        });
6481        // Read before it is parsed, because the evidence which tells a secondary rate
6482        // limit from a rejected credential is in the body of a response whose status says
6483        // only "forbidden" — and a non-success response was never parsed at all.
6484        let body = response.text().await.map_err(|e| {
6485            Attempt::Failed(SourceError::Unavailable {
6486                message: format!(
6487                    "GitHub GraphQL response could not be read: {}",
6488                    transport_error(&e)
6489                ),
6490            })
6491        })?;
6492        *reported_cost = serde_json::from_str::<Value>(&body)
6493            .ok()
6494            .as_ref()
6495            .and_then(|body| body.pointer("/data/rateLimit/cost"))
6496            .and_then(Value::as_u64);
6497        let parsed = serde_json::from_str::<Value>(&body).ok();
6498        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
6499        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
6500            return Err(Attempt::Limited(Limited {
6501                limiter,
6502                hint,
6503                hinted_by,
6504            }));
6505        }
6506        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
6507            return Err(Attempt::Failed(SourceError::Auth {
6508                message: format!(
6509                    "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"
6510                ),
6511            }));
6512        }
6513        if !status.is_success() {
6514            return Err(Attempt::Failed(SourceError::Unavailable {
6515                message: format!("GitHub GraphQL returned HTTP {status}"),
6516            }));
6517        }
6518        let said = refusal_wording(status, &body);
6519        let normalized = said.to_ascii_lowercase();
6520        if CONFLICT_WORDINGS
6521            .iter()
6522            .any(|wording| normalized.contains(wording))
6523        {
6524            return Err(Attempt::Conflicted(said));
6525        }
6526        // A lookup is read as the inventoried document it was sized from.
6527        let query = graphql::inventoried(query);
6528        let answered = if query == graphql::ISSUES_BY_REFERENCE {
6529            self.answer_unresolved(&body)
6530        } else if [
6531            graphql::CREATE_ISSUES,
6532            graphql::SEARCH_BATCH,
6533            graphql::USER_IDS,
6534            graphql::ADD_COMMENTS,
6535            graphql::WRITE_TASKS,
6536            graphql::SET_PARENTS,
6537            graphql::ADD_RELATES_TO,
6538            graphql::REMOVE_RELATES_TO,
6539        ]
6540        .contains(&query)
6541        {
6542            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
6543        } else {
6544            self.answer(&body)
6545        };
6546        let mut data = answered.map_err(Attempt::Failed)?;
6547        if let Some(served) = served {
6548            *self
6549                .last_served
6550                .lock()
6551                .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(served);
6552            stamp_served(&mut data, &served.to_rfc3339());
6553        }
6554        Ok(data)
6555    }
6556
6557    /// What one successful HTTP response says, once its GraphQL errors are read.
6558    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
6559    /// for this credential — a repository it cannot see, an issue number it does not hold —
6560    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
6561    /// that reference rather than a failure of the others. Any other error refuses the read,
6562    /// as there.
6563    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
6564        let parsed: Option<Value> = serde_json::from_str(body).ok();
6565        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
6566        // `issue`, and the answer holds `null` at exactly that path: anything else — another
6567        // kind of error, one naming no slot, or one beside a value it says is not there — is a
6568        // refusal of the whole read.
6569        let unresolved = |data: &Value, error: &Value| {
6570            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
6571                && error
6572                    .get("path")
6573                    .and_then(Value::as_array)
6574                    .is_some_and(|path| {
6575                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
6576                            alias
6577                                .strip_prefix('r')
6578                                .and_then(|slot| slot.parse::<usize>().ok())
6579                                .filter(|slot| *slot < REFERENCE_BATCH)
6580                                .map(|_| alias)
6581                        });
6582                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
6583                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
6584                            (Some(alias), Some("issue"), 2) => data
6585                                .get(alias)
6586                                .and_then(|repository| repository.get("issue"))
6587                                .is_some_and(Value::is_null),
6588                            _ => false,
6589                        }
6590                    })
6591        };
6592        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
6593            parsed.get("data").is_some_and(|data| {
6594                data.is_object()
6595                    && parsed
6596                        .get("errors")
6597                        .and_then(Value::as_array)
6598                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
6599            })
6600        });
6601        match parsed {
6602            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
6603            _ => self.answer(body),
6604        }
6605    }
6606
6607    fn answer(&self, body: &str) -> Result<Value, SourceError> {
6608        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
6609            message: format!("GitHub returned invalid JSON: {e}"),
6610        })?;
6611        let errors = body
6612            .get("errors")
6613            .map(|value| {
6614                value.as_array().ok_or_else(|| SourceError::Malformed {
6615                    message: "GitHub response errors is not an array".into(),
6616                })
6617            })
6618            .transpose()?;
6619        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
6620            let messages = errors
6621                .iter()
6622                .filter_map(|e| e.get("message").and_then(Value::as_str))
6623                .collect::<Vec<_>>()
6624                .join("; ");
6625            let message = if messages.is_empty() {
6626                "GitHub returned GraphQL errors".into()
6627            } else {
6628                messages
6629            };
6630            let normalized = message.to_ascii_lowercase();
6631            if normalized.contains("resource not accessible") || normalized.contains("scope") {
6632                return Err(SourceError::Auth {
6633                    message: format!(
6634                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
6635                        self.credential_name
6636                    ),
6637                });
6638            }
6639            return Err(SourceError::Refused { message });
6640        }
6641        body.get("data")
6642            .filter(|data| data.is_object())
6643            .cloned()
6644            .ok_or_else(|| SourceError::Malformed {
6645                message: "GitHub response has no data object".into(),
6646            })
6647    }
6648
6649    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
6650    // GraphQL cannot independently page them inside the outer item page. This source page is
6651    // deliberately bounded at that published maximum; the live drift journey exercises it.
6652    async fn board_page(
6653        &self,
6654        items_after: Option<&str>,
6655        items_first: u32,
6656    ) -> Result<Value, SourceError> {
6657        let data = self
6658            .graphql(
6659                graphql::BOARD,
6660                json!({"owner":self.owner,"number":self.project_number,
6661                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
6662                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
6663            )
6664            .await?;
6665        data.pointer("/owner/projectV2")
6666            .filter(|v| !v.is_null())
6667            .cloned()
6668            .ok_or_else(|| SourceError::Refused {
6669                message: format!(
6670                    "GitHub project {}/{} was not found or is not visible to the token",
6671                    self.owner, self.project_number
6672                ),
6673            })
6674    }
6675
6676    /// The search that finds the issues of this board, narrowed by `also` when it is
6677    /// given.
6678    ///
6679    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
6680    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
6681    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
6682    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
6683    /// from a task by the `parent` field each issue carries rather than by the search.
6684    fn board_search(&self, also: Option<&str>) -> String {
6685        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
6686        match also {
6687            Some(also) => format!("{scope} {also}"),
6688            None => scope,
6689        }
6690    }
6691
6692    /// One issue this source reached directly, as the board item a read of the board would
6693    /// have produced — or `None` when this board does not hold it.
6694    ///
6695    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
6696    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
6697    /// item's own id, that item's field values, and the issue as its content. One resolver
6698    /// for both routes is what makes an issue read through a search, through its own node
6699    /// id, or through its project's sub-issues report the same title, the same status, the
6700    /// same labels and the same qualified id.
6701    ///
6702    /// An issue with no entry for *this* board is not this source's to report, which is
6703    /// what keeps an id naming some other repository's issue from being answered as an item
6704    /// of this board. That answer is given about an **exhausted** connection and never
6705    /// about an unread page: the entry is looked for on the page in hand, and only if that
6706    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
6707    /// rest of it.
6708    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
6709        self.resolve_issue_as(issue, true).await
6710    }
6711
6712    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
6713    /// and writes only when `remember` says to.
6714    async fn resolve_issue_as(
6715        &self,
6716        issue: &Value,
6717        remember: bool,
6718    ) -> Result<Option<Resolved>, SourceError> {
6719        if optional_str(issue, "__typename")? != Some("Issue") {
6720            return Ok(None);
6721        }
6722        let memberships = issue
6723            .get("projectItems")
6724            .ok_or_else(|| SourceError::Malformed {
6725                message: "GitHub issue is missing projectItems".into(),
6726            })?;
6727        let nodes = memberships
6728            .get("nodes")
6729            .and_then(Value::as_array)
6730            .ok_or_else(|| SourceError::Malformed {
6731                message: "GitHub issue projectItems.nodes is not an array".into(),
6732            })?;
6733        let held = match self.board_entry(nodes) {
6734            Some(held) => held.clone(),
6735            None => {
6736                let info = memberships
6737                    .get("pageInfo")
6738                    .ok_or_else(|| SourceError::Malformed {
6739                        message: "GitHub issue projectItems has no pageInfo".into(),
6740                    })?;
6741                // The page held no entry for this board. Whether that means the issue is
6742                // not on it is a question about the rest of the connection, and only a
6743                // connection with no rest answers it here.
6744                if !required_bool(info, "hasNextPage")? {
6745                    return Ok(None);
6746                }
6747                let cursor = required_str(info, "endCursor")?;
6748                validate_cursor_progress(None, cursor)?;
6749                let issue_id = required_str(issue, "id")?;
6750                match self.board_membership(issue_id, cursor).await? {
6751                    Some(held) => held,
6752                    None => return Ok(None),
6753                }
6754            }
6755        };
6756        // The membership's own `updatedAt` rides along: it is the board half of the item's change
6757        // token, which a field change moves where the issue's own time does not.
6758        let item = json!({
6759            "id": required_str(&held, "id")?,
6760            "updatedAt": held.get("updatedAt"),
6761            "project": held.get("project"),
6762            "fieldValues": held.get("fieldValues"),
6763            "content": issue,
6764        });
6765        if remember {
6766            self.resolve(&item)
6767        } else {
6768            self.resolve_unremembered(&item)
6769        }
6770    }
6771
6772    /// This board's own entry among one page of an issue's `Issue.projectItems`.
6773    ///
6774    /// One spelling of *which membership is this board's*, so the page a read carries and
6775    /// the pages [`Self::board_membership`] walks are searched by the same rule.
6776    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
6777        nodes.iter().find(|node| {
6778            node.pointer("/project/number").and_then(Value::as_u64)
6779                == Some(u64::from(self.project_number))
6780        })
6781    }
6782
6783    /// The rest of one issue's board memberships, from `after`, for this board's entry.
6784    ///
6785    /// The recovery read: a page of memberships that holds no entry for this board says
6786    /// nothing about the memberships past it, so the connection is walked to exhaustion
6787    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
6788    /// positive answer — the whole connection was read and no entry named this board —
6789    /// rather than a failure, and the walk is held to
6790    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
6791    /// with a cursor that does not advance is refused instead of spun on.
6792    async fn board_membership(
6793        &self,
6794        issue: &str,
6795        after: &str,
6796    ) -> Result<Option<Value>, SourceError> {
6797        let mut after = after.to_owned();
6798        loop {
6799            let data = self
6800                .graphql(
6801                    graphql::ISSUE_BOARD_ITEMS,
6802                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
6803                           "nestedFirst":NESTED_PAGE_SIZE}),
6804                )
6805                .await?;
6806            let Some(connection) = data
6807                .pointer("/node/projectItems")
6808                .filter(|value| !value.is_null())
6809            else {
6810                // The id resolved to nothing, or to something with no memberships to walk —
6811                // which is the same answer as a connection holding no entry for this board.
6812                return Ok(None);
6813            };
6814            let nodes = connection
6815                .get("nodes")
6816                .and_then(Value::as_array)
6817                .ok_or_else(|| SourceError::Malformed {
6818                    message: "GitHub issue projectItems.nodes is not an array".into(),
6819                })?;
6820            if let Some(held) = self.board_entry(nodes) {
6821                return Ok(Some(held.clone()));
6822            }
6823            let info = connection
6824                .get("pageInfo")
6825                .ok_or_else(|| SourceError::Malformed {
6826                    message: "GitHub issue projectItems has no pageInfo".into(),
6827                })?;
6828            let next = required_bool(info, "hasNextPage")?
6829                .then(|| required_str(info, "endCursor"))
6830                .transpose()?;
6831            match next {
6832                Some(next) => {
6833                    validate_cursor_progress(Some(&after), next)?;
6834                    after = next.to_owned();
6835                }
6836                None => return Ok(None),
6837            }
6838        }
6839    }
6840
6841    /// One page of a board-scoped issue search, and where the next page resumes.
6842    async fn search_page(
6843        &self,
6844        search: &str,
6845        first: u32,
6846        after: Option<&str>,
6847    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
6848        let data = self
6849            .graphql(
6850                graphql::SEARCH_ISSUES,
6851                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
6852                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
6853                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6854            )
6855            .await?;
6856        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
6857            message: "GitHub search response has no search connection".into(),
6858        })?;
6859        let mut found = Vec::new();
6860        for node in connection
6861            .get("nodes")
6862            .and_then(Value::as_array)
6863            .ok_or_else(|| SourceError::Malformed {
6864                message: "GitHub search nodes is not an array".into(),
6865            })?
6866        {
6867            if let Some(resolved) = self.resolve_issue(node).await? {
6868                found.push(resolved);
6869            }
6870        }
6871        let info = connection
6872            .get("pageInfo")
6873            .ok_or_else(|| SourceError::Malformed {
6874                message: "GitHub search connection has no pageInfo".into(),
6875            })?;
6876        let next = required_bool(info, "hasNextPage")?
6877            .then(|| required_str(info, "endCursor"))
6878            .transpose()?
6879            .map(str::to_owned);
6880        if let Some(next) = &next {
6881            validate_cursor_progress(after, next)?;
6882        }
6883        Ok((found, next))
6884    }
6885
6886    /// Every issue this board holds, completed with what this run wrote.
6887    ///
6888    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
6889    /// is an index and is eventually consistent, so an issue this run created seconds ago
6890    /// can be absent from it, and a project listed straight after being written would
6891    /// otherwise be missing from its own board. What is added back is only what this
6892    /// process itself wrote, out of [`Self::created`], which lives and dies with the
6893    /// process.
6894    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
6895        let found = self.searched_issues().await?;
6896        self.completed_with_written(found, |_| true)
6897    }
6898
6899    /// Every issue this board's own search reports, walked to exhaustion, read once per
6900    /// source.
6901    ///
6902    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
6903    /// needs it too and the two would otherwise walk the same search twice in one command.
6904    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
6905    /// is.
6906    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
6907        let cached = self.search_cache()?.clone();
6908        if let Some(held) = cached {
6909            return Ok(held);
6910        }
6911        let mut after: Option<String> = None;
6912        let mut found = Vec::new();
6913        let search = self.board_search(None);
6914        loop {
6915            let (page, next) = self
6916                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
6917                .await?;
6918            found.extend(page);
6919            match next {
6920                Some(next) => after = Some(next),
6921                None => break,
6922            }
6923        }
6924        *self.search_cache()? = Some(found.clone());
6925        Ok(found)
6926    }
6927
6928    /// Every issue this board's own search reports with `also` beside its scope, walked to
6929    /// exhaustion — held for no later read, which may not be narrowed alike.
6930    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6931        let mut after: Option<String> = None;
6932        let mut found = Vec::new();
6933        let search = self.board_search(Some(also));
6934        loop {
6935            let (page, next) = self
6936                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
6937                .await?;
6938            found.extend(page);
6939            match next {
6940                Some(next) => after = Some(next),
6941                None => return Ok(found),
6942            }
6943        }
6944    }
6945
6946    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
6947    fn search_cache(
6948        &self,
6949    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
6950        self.search_cache
6951            .lock()
6952            .map_err(|_| SourceError::Unavailable {
6953                message: "this source's view of the board's issues was left inconsistent by an \
6954                      earlier failure; next: run the command again"
6955                    .into(),
6956            })
6957    }
6958
6959    /// `found`, with everything this run wrote that `keep` accepts and the read did not
6960    /// report.
6961    ///
6962    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
6963    /// at all: the search index is behind, and a node read of an item filed moments ago can
6964    /// be too.
6965    fn completed_with_written(
6966        &self,
6967        mut found: Vec<Resolved>,
6968        keep: impl Fn(&Resolved) -> bool,
6969    ) -> Result<Vec<Resolved>, SourceError> {
6970        for own in self.created()?.iter().filter(|own| keep(own)) {
6971            if !found.iter().any(|item| item.id == own.id) {
6972                found.push(own.clone());
6973            }
6974        }
6975        Ok(found)
6976    }
6977
6978    /// What resolving one node id reached.
6979    ///
6980    /// Three answers rather than an `Option`, because a board *draft* is none of the other
6981    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
6982    /// is completed by a read of the draft itself rather than reported as nothing.
6983    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
6984        let asked = self
6985            .graphql(
6986                graphql::ISSUE,
6987                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
6988                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6989            )
6990            .await;
6991        let data = match asked {
6992            Ok(data) => data,
6993            // A string that is not a node id at all is not a failure to report: it is an id
6994            // this board does not hold, which is what every read of one already answers.
6995            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
6996            Err(error) => return Err(error),
6997        };
6998        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6999            return Ok(Reached::Nothing);
7000        };
7001        if optional_str(node, "__typename")? == Some("DraftIssue") {
7002            return Ok(Reached::Draft);
7003        }
7004        Ok(match self.resolve_issue(node).await? {
7005            Some(item) => Reached::Held(Box::new(item)),
7006            None => Reached::Nothing,
7007        })
7008    }
7009
7010    /// One item of this board by its own id, whatever kind it is.
7011    ///
7012    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
7013    /// run wrote is read first, because a node read of an item created moments ago can
7014    /// still be behind the board field values written onto it — see [`Self::created`].
7015    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
7016        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
7017            return Ok(Some(own.clone()));
7018        }
7019        if let Some(held) = self.read_ahead()?.remove(id) {
7020            return Ok(held);
7021        }
7022        match self.reach(id).await? {
7023            Reached::Held(item) => Ok(Some(*item)),
7024            Reached::Nothing => Ok(None),
7025            Reached::Draft => self.draft_by_id(id).await,
7026        }
7027    }
7028
7029    /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
7030    /// answers the project it is filed under and that project's members beside it.
7031    ///
7032    /// Each is held for this command as a read of it would have been: the project for the
7033    /// next read of it by its id, and — when its page of members is the whole of them — the
7034    /// members for the next listing of that project. So a write held to who may read the
7035    /// document, which reads all three, is answered by this one request.
7036    async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
7037        if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
7038            return self.item_by_id(id).await;
7039        }
7040        let asked = self
7041            .graphql(
7042                graphql::DOCUMENT_IN_PROJECT,
7043                json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
7044                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7045                       "duplicates":true}),
7046            )
7047            .await;
7048        let data = match asked {
7049            Ok(data) => data,
7050            Err(error) if unresolvable_node(&error) => return Ok(None),
7051            Err(error) => return Err(error),
7052        };
7053        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
7054            return Ok(None);
7055        };
7056        if optional_str(node, "__typename")? == Some("DraftIssue") {
7057            return self.draft_by_id(id).await;
7058        }
7059        let Some(item) = self.resolve_issue(node).await? else {
7060            return Ok(None);
7061        };
7062        // An answer for another issue than the one asked about says nothing of that one.
7063        if item.id != *id {
7064            return Err(SourceError::Malformed {
7065                message: format!(
7066                    "GitHub answered issue {} when asked for document {}",
7067                    item.id.0, id.0
7068                ),
7069            });
7070        }
7071        if let Some(project) = node.get("project").filter(|value| !value.is_null())
7072            && let Some(parent) = self
7073                .resolve_issue(project)
7074                .await?
7075                .filter(|parent| Some(&parent.id) == item.parent.as_ref())
7076        {
7077            let members = project.get("subIssues").filter(|value| !value.is_null());
7078            let whole = members
7079                .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
7080                .and_then(Value::as_bool)
7081                == Some(false);
7082            if whole {
7083                let nodes = members
7084                    .and_then(|members| members.get("nodes"))
7085                    .and_then(Value::as_array)
7086                    .ok_or_else(|| SourceError::Malformed {
7087                        message: "GitHub subIssues.nodes is not an array".into(),
7088                    })?;
7089                let mut children = Vec::new();
7090                for member in nodes {
7091                    if let Some(resolved) = self.resolve_issue(member).await? {
7092                        children.push(resolved);
7093                    }
7094                }
7095                // Held as the project's members only when every one resolved and says it is
7096                // filed under it: a page with any other answers nothing of all the members.
7097                if children.len() == nodes.len()
7098                    && children
7099                        .iter()
7100                        .all(|child| child.parent.as_ref() == Some(&parent.id))
7101                {
7102                    self.children_cache()?
7103                        .insert(parent.id.clone(), (parent.id.clone(), children));
7104                }
7105            }
7106            self.read_ahead()?.insert(parent.id.clone(), Some(parent));
7107        }
7108        Ok(Some(item))
7109    }
7110
7111    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
7112    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
7113    /// than one request per id.
7114    ///
7115    /// What this run wrote answers first, as it does there, and only the rest is read. One id
7116    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
7117    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
7118    /// id at a time, so that id is answered as not held and the others as themselves; a draft
7119    /// is completed by a read of the draft, exactly as there.
7120    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
7121        let mut found: Vec<Option<Option<Resolved>>> = {
7122            let created = self.created()?;
7123            ids.iter()
7124                .map(|id| {
7125                    created
7126                        .iter()
7127                        .find(|own| own.id == *id)
7128                        .map(|own| Some(own.clone()))
7129                })
7130                .collect()
7131        };
7132        let unread: Vec<NativeId> = ids
7133            .iter()
7134            .zip(&found)
7135            .filter(|(_, found)| found.is_none())
7136            .map(|(id, _)| id.clone())
7137            .collect();
7138        let mut read = Vec::with_capacity(unread.len());
7139        if let [one] = unread.as_slice() {
7140            read.push(self.item_by_id(one).await?);
7141        } else {
7142            for batch in unread.chunks(DETAIL_BATCH) {
7143                let data = match self
7144                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
7145                    .await
7146                {
7147                    Ok(data) => data,
7148                    Err(error) if unresolvable_node(&error) => {
7149                        for id in batch {
7150                            read.push(self.item_by_id(id).await?);
7151                        }
7152                        continue;
7153                    }
7154                    Err(error) => return Err(error),
7155                };
7156                for (slot, id) in batch.iter().enumerate() {
7157                    let node =
7158                        data.get(format!("i{slot}"))
7159                            .ok_or_else(|| SourceError::Malformed {
7160                                message: format!(
7161                                    "GitHub answered a batch read with no item for {}",
7162                                    id.0
7163                                ),
7164                            })?;
7165                    read.push(if node.is_null() {
7166                        None
7167                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
7168                        self.draft_by_id(id).await?
7169                    } else {
7170                        if optional_str(node, "__typename")? == Some("Issue")
7171                            && required_str(node, "id")? != id.0
7172                        {
7173                            return Err(SourceError::Malformed {
7174                                message: format!(
7175                                    "GitHub answered the read of {} with issue {}",
7176                                    id.0,
7177                                    required_str(node, "id")?
7178                                ),
7179                            });
7180                        }
7181                        self.resolve_issue(node).await?
7182                    });
7183                }
7184            }
7185        }
7186        let mut read = read.into_iter();
7187        Ok(found
7188            .iter_mut()
7189            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
7190            .collect())
7191    }
7192
7193    fn read_ahead(
7194        &self,
7195    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
7196        self.read_ahead
7197            .lock()
7198            .map_err(|_| SourceError::Unavailable {
7199                message:
7200                    "items read ahead for a copy were left inconsistent; run the command again"
7201                        .into(),
7202            })
7203    }
7204
7205    fn resolved_cache(
7206        &self,
7207    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
7208        self.resolved_cache
7209            .lock()
7210            .map_err(|_| SourceError::Unavailable {
7211                message: "resolved item records were left inconsistent; run the command again"
7212                    .into(),
7213            })
7214    }
7215
7216    /// Reuse a record this invocation already resolved. The mutation sender invalidates
7217    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
7218    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
7219        let cached = self.resolved_cache()?.get(id).cloned();
7220        if let Some(item) = cached {
7221            return Ok(Some(item));
7222        }
7223        self.item_by_id(id).await
7224    }
7225
7226    /// Take out the item an issue read by reference reached for `id`, when one is held: the
7227    /// status write it was read for is the one caller, so no other read or write is ever
7228    /// answered from a read that selected only what that write needs.
7229    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
7230        Ok(self
7231            .held_cache
7232            .lock()
7233            .map_err(|_| SourceError::Unavailable {
7234                message: "items read by issue reference were left inconsistent; run the \
7235                          command again"
7236                    .into(),
7237            })?
7238            .remove(id))
7239    }
7240
7241    /// One board draft by its own id, with the board item it sits in — or `None` when no
7242    /// item of this board is that draft's.
7243    ///
7244    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
7245    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
7246    /// links a draft to one board item, so the page this read carries is the whole of that
7247    /// connection, and a page that reports more than it holds is refused rather than read
7248    /// as an answer about memberships nobody read.
7249    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
7250        let data = self
7251            .graphql(
7252                graphql::DRAFT,
7253                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
7254                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
7255            )
7256            .await?;
7257        // Gone between the two reads is an answer — the draft is no longer there. Anything
7258        // else than the draft [`Self::reach`] was just told this id is, is not one.
7259        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
7260            return Ok(None);
7261        };
7262        if optional_str(draft, "__typename")? != Some("DraftIssue") {
7263            return Err(SourceError::Malformed {
7264                message: format!(
7265                    "GitHub answered {} as a draft and then as something else",
7266                    id.0
7267                ),
7268            });
7269        }
7270        if required_str(draft, "id")? != id.0 {
7271            return Err(SourceError::Malformed {
7272                message: format!("GitHub answered a different draft for {}", id.0),
7273            });
7274        }
7275        let memberships = draft
7276            .get("projectV2Items")
7277            .ok_or_else(|| SourceError::Malformed {
7278                message: format!("GitHub draft {} is missing projectV2Items", id.0),
7279            })?;
7280        let nodes = memberships
7281            .get("nodes")
7282            .and_then(Value::as_array)
7283            .ok_or_else(|| SourceError::Malformed {
7284                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
7285            })?;
7286        let info = memberships
7287            .get("pageInfo")
7288            .ok_or_else(|| SourceError::Malformed {
7289                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
7290            })?;
7291        // Read whether or not this board's entry is on the page: a page claiming more than
7292        // the one item GitHub links a draft to is a malformed answer either way.
7293        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
7294            return Err(SourceError::Malformed {
7295                message: format!(
7296                    "GitHub draft {} reports more board items than the one GitHub links a draft \
7297                     to",
7298                    id.0
7299                ),
7300            });
7301        }
7302        if let Some(node) = nodes.first()
7303            && node
7304                .pointer("/project/number")
7305                .and_then(Value::as_u64)
7306                .is_none()
7307        {
7308            return Err(SourceError::Malformed {
7309                message: format!(
7310                    "GitHub draft {} board item has no numeric project number",
7311                    id.0
7312                ),
7313            });
7314        }
7315        let Some(held) = self.board_entry(nodes) else {
7316            return Ok(None);
7317        };
7318        if required_str(
7319            held.get("project").ok_or_else(|| SourceError::Malformed {
7320                message: format!("GitHub draft {} board item has no project", id.0),
7321            })?,
7322            "id",
7323        )? != self.board_fields().await?.id.as_str()
7324        {
7325            return Ok(None);
7326        }
7327        let item = json!({
7328            "id": required_str(held, "id")?,
7329            "project": held.get("project"),
7330            "fieldValues": held.get("fieldValues"),
7331            "content": draft,
7332        });
7333        self.resolve(&item)
7334    }
7335
7336    /// The board's own id and field definitions, for a write whose item does not carry
7337    /// them — never its items.
7338    ///
7339    /// A board this command has already listed supplies them, since it read them beside its
7340    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
7341    /// is consulted about which items the board holds: see the module documentation for
7342    /// why a question about one known item is answered by reading that item.
7343    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
7344        if let Some(board) = self.board_cache()?.as_ref() {
7345            return Ok(BoardFields {
7346                id: BoardId::parse(&board.id)?,
7347                fields: board.fields.clone(),
7348            });
7349        }
7350        if let Some(held) = self.fields_cache()?.clone() {
7351            return Ok(held);
7352        }
7353        let data = self
7354            .graphql(
7355                graphql::BOARD_FIELDS,
7356                json!({"owner":self.owner,"number":self.project_number,
7357                       "nestedFirst":NESTED_PAGE_SIZE}),
7358            )
7359            .await?;
7360        self.fields_read(&data)
7361    }
7362
7363    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
7364    /// the rest of this command.
7365    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
7366        let board = data
7367            .pointer("/boardFields/projectV2")
7368            .filter(|value| !value.is_null())
7369            .ok_or_else(|| SourceError::Refused {
7370                message: format!(
7371                    "GitHub project {}/{} was not found or is not visible to the token",
7372                    self.owner, self.project_number
7373                ),
7374            })?;
7375        let read = BoardFields {
7376            id: BoardId::parse(required_str(board, "id")?)?,
7377            fields: board.get("fields").cloned().unwrap_or(Value::Null),
7378        };
7379        *self.fields_cache()? = Some(read.clone());
7380        Ok(read)
7381    }
7382
7383    /// Read what creating an issue in `repository` needs and this command has not read yet —
7384    /// the board's fields and the repository's node id — in one request when it needs both.
7385    ///
7386    /// When either is already known this sends nothing, and the other is read by its own
7387    /// document where it is asked for, so no create reads anything twice.
7388    async fn creation_context(
7389        &self,
7390        repository: &RepositoryTarget,
7391        incoming: &Incoming<'_>,
7392    ) -> Result<(), SourceError> {
7393        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
7394        if fields_known || self.repository_cache()?.contains_key(repository) {
7395            return Ok(());
7396        }
7397        // A placement an earlier process kept is taken only while the project issue this create
7398        // has just read shows its setup-context token unchanged; see `projection`.
7399        if self.placed_by_kept(repository, incoming.parent) {
7400            return Ok(());
7401        }
7402        let data = self
7403            .graphql(
7404                graphql::CREATION_CONTEXT,
7405                json!({"owner":self.owner,"number":self.project_number,
7406                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
7407                       "repositoryName":repository.name}),
7408            )
7409            .await?;
7410        self.fields_read(&data)?;
7411        self.repository_read(&data, repository, incoming)?;
7412        Ok(())
7413    }
7414
7415    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
7416    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
7417        self.fields_cache
7418            .lock()
7419            .map_err(|_| SourceError::Unavailable {
7420                message: "this source's view of the board's fields was left inconsistent by an \
7421                      earlier failure; next: run the command again"
7422                    .into(),
7423            })
7424    }
7425
7426    /// What a write to `item` needs of the board, read off that item when it says enough and
7427    /// off [`Self::board_fields`] when it does not.
7428    ///
7429    /// A node read of an item names its board and carries the definition of every field it
7430    /// holds a value of — so an item naming its board, holding a value of the origin field
7431    /// when the write carries an origin, and, when the write carries a status, holding a
7432    /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
7433    /// guessed:** a field it holds no value of may still be on the board, and a view reading it
7434    /// as absent would refuse a write the board can take or skip a field write the board
7435    /// needs, so such an item — and a create, which has no item yet — takes the board's fields
7436    /// from their own read instead. A write carrying no origin to an item holding none — every
7437    /// write out of a source declared private — writes no origin whatever the board defines,
7438    /// so it needs no definition of that field.
7439    async fn fields_for(
7440        &self,
7441        item: Option<&Resolved>,
7442        writes_origin: bool,
7443        writes_status: bool,
7444        selects_priority: bool,
7445    ) -> Result<BoardFields, SourceError> {
7446        if let Some(board) = item.and_then(Resolved::carried_board) {
7447            return Ok(board);
7448        }
7449        if let Some(item) = item
7450            && let Some(board_id) = item.named_board()
7451            && (!writes_origin || item.defines(ORIGIN_FIELD))
7452            && (!writes_status || item.defines("Status"))
7453            && (!selects_priority || item.defines(PRIORITY_FIELD))
7454            && self
7455                .metadata_fields
7456                .iter()
7457                .all(|projection| item.defines(&projection.field))
7458        {
7459            return Ok(BoardFields {
7460                id: board_id,
7461                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7462            });
7463        }
7464        self.board_fields().await
7465    }
7466
7467    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
7468    /// when that id names nothing here with a sub-issue relationship to walk.
7469    ///
7470    /// `None` and an empty answer are different: `None` is *this is not an issue of this
7471    /// GitHub*, which is what sends a project selector on to be read as a name, and an
7472    /// empty vector is a project that holds nothing.
7473    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7474        let mut after: Option<String> = None;
7475        let mut children = Vec::new();
7476        // The project issue as the first page carried it, held once the walk has ended.
7477        let mut parent: Option<Resolved> = None;
7478        loop {
7479            let asked = self
7480                .graphql(
7481                    graphql::SUB_ISSUES,
7482                    json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
7483                           "nestedFirst":NESTED_PAGE_SIZE,
7484                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
7485                )
7486                .await;
7487            let data = match asked {
7488                Ok(data) => data,
7489                // A string that is not a node id at all is not a failure to report: it is
7490                // the ordinary answer to a selector naming a project by its name.
7491                Err(error) if unresolvable_node(&error) => return Ok(None),
7492                Err(error) => return Err(error),
7493            };
7494            let Some(connection) = data
7495                .pointer("/node/subIssues")
7496                .filter(|value| !value.is_null())
7497            else {
7498                // No such node, or one with no sub-issue relationship — a board draft is
7499                // the one this board can really hold.
7500                return Ok(None);
7501            };
7502            if after.is_none()
7503                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
7504            {
7505                parent = self
7506                    .resolve_issue(node)
7507                    .await?
7508                    .filter(|resolved| resolved.id == *id);
7509            }
7510            for node in connection
7511                .get("nodes")
7512                .and_then(Value::as_array)
7513                .ok_or_else(|| SourceError::Malformed {
7514                    message: "GitHub subIssues.nodes is not an array".into(),
7515                })?
7516            {
7517                if let Some(resolved) = self.resolve_issue(node).await? {
7518                    children.push(resolved);
7519                }
7520            }
7521            let info = connection
7522                .get("pageInfo")
7523                .ok_or_else(|| SourceError::Malformed {
7524                    message: "GitHub subIssues connection has no pageInfo".into(),
7525                })?;
7526            let next = required_bool(info, "hasNextPage")?
7527                .then(|| required_str(info, "endCursor"))
7528                .transpose()?;
7529            match next {
7530                Some(next) => {
7531                    validate_cursor_progress(after.as_deref(), next)?;
7532                    after = Some(next.to_owned());
7533                }
7534                None => {
7535                    // Held only once the walk has ended, so one failing part way holds
7536                    // nothing and every read it would have answered is made.
7537                    // The project issue the first page carried answers the next read of the
7538                    // project by its id in this command, as a read of it made now would: what
7539                    // classifying a project for a write reads straight after its members.
7540                    if let Some(parent) = &parent {
7541                        self.read_ahead()?
7542                            .entry(parent.id.clone())
7543                            .or_insert_with(|| Some(parent.clone()));
7544                    }
7545                    let mut carried = self.carried()?;
7546                    for item in parent.into_iter().chain(children.iter().cloned()) {
7547                        carried.insert(item.id.clone(), item);
7548                    }
7549                    return Ok(Some(children));
7550                }
7551            }
7552        }
7553    }
7554
7555    fn carried(
7556        &self,
7557    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
7558        self.carried.lock().map_err(|_| SourceError::Unavailable {
7559            message: "items a project's listing carried were left inconsistent; run the \
7560                      command again"
7561                .into(),
7562        })
7563    }
7564
7565    /// Which issue of this board a project *name* is, or `None` when none is.
7566    ///
7567    /// One bounded query which filters on that name at the server, rather than a walk of
7568    /// every issue the board holds. The name is compared again here: the qualifier narrows
7569    /// what GitHub sends, and this source decides what it names.
7570    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
7571        let search = self.board_search(Some(&title_qualifier(name)));
7572        let mut after = None;
7573        loop {
7574            let (candidates, next) = self
7575                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
7576                .await?;
7577            if let Some(item) = candidates.into_iter().find(|item| {
7578                item.kind == BoardKind::Work(ItemKind::Project)
7579                    && item.title.eq_ignore_ascii_case(name)
7580            }) {
7581                return Ok(Some(item.id));
7582            }
7583            match next {
7584                Some(next) => after = Some(next),
7585                None => return Ok(None),
7586            }
7587        }
7588    }
7589
7590    /// Everything filed under one project of this board: the sub-issues of the issue that
7591    /// project is.
7592    ///
7593    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
7594    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
7595    /// gains projects, or as another project gains tasks.
7596    ///
7597    /// A qualified id names the issue and is asked for its sub-issues directly: one
7598    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
7599    /// read as a project *name*, which costs the one bounded search
7600    /// [`Self::project_by_name`] makes.
7601    ///
7602    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
7603    /// — and each answer is still cut to the items whose parent is this project, so one this
7604    /// process has since filed elsewhere is not reported here, and completed with what this
7605    /// process filed under it.
7606    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
7607        let held = self.children_cache()?.get(selector).cloned();
7608        let (project, children) = match held {
7609            Some(held) => held,
7610            None => {
7611                let answered = match self.sub_issues(selector).await? {
7612                    Some(children) => (selector.clone(), children),
7613                    None => match self.project_by_name(&selector.0).await? {
7614                        Some(project) => {
7615                            let children = self.sub_issues(&project).await?.unwrap_or_default();
7616                            (project, children)
7617                        }
7618                        None => return Ok(Vec::new()),
7619                    },
7620                };
7621                self.children_cache()?
7622                    .insert(selector.clone(), answered.clone());
7623                answered
7624            }
7625        };
7626        let mut children: Vec<Resolved> = children
7627            .into_iter()
7628            .filter(|child| child.parent.as_ref() == Some(&project))
7629            .collect();
7630        for own in self.updated()?.iter() {
7631            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
7632                children.push(own.clone());
7633            }
7634        }
7635        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
7636    }
7637
7638    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
7639    /// completed with what this run wrote — the candidates a comment-activity read confirms.
7640    ///
7641    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
7642    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
7643    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
7644    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
7645    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
7646    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
7647    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
7648    /// rather than silently narrowing a caller's answer.
7649    ///
7650    /// The instant is written to the second, rounded down, which can only widen what the
7651    /// search returns; confirmation against each candidate's own comments is what makes the
7652    /// answer exact. The search is an index that lags a write by a second or two — the module
7653    /// documentation records it — so a caller that asks again from its last instant should
7654    /// overlap the two by more than that.
7655    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
7656        let found = self.searched(&updated_qualifier(since)).await?;
7657        self.completed_with_written(found, |_| true)
7658    }
7659
7660    /// Every issue of this board GitHub's issue search reports for the board-scoped search
7661    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
7662    ///
7663    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
7664    /// own record is the fresher of the two.
7665    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
7666        let search = self.board_search(Some(also));
7667        let mut after: Option<String> = None;
7668        let mut found = Vec::new();
7669        loop {
7670            let (page, next) = self
7671                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
7672                .await?;
7673            found.extend(page);
7674            match next {
7675                Some(next) => after = Some(next),
7676                None => return Ok(found),
7677            }
7678        }
7679    }
7680
7681    /// A bounded task answer; the versioned cursor carries the connection position, how
7682    /// many rows of the page starting there were already handed out, and the own-write ids
7683    /// already observed, including across a new source instance.
7684    ///
7685    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
7686    /// sliced from the pages it needs; why is the module documentation's paging contract.
7687    async fn search_tasks(
7688        &self,
7689        query: &TaskQuery,
7690        page: &PageRequest,
7691        also: &str,
7692    ) -> Result<Page<Task>, SourceError> {
7693        let mut position = match &page.cursor {
7694            None => SearchPosition::default(),
7695            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
7696                .ok()
7697                .filter(|position| {
7698                    position.version == SEARCH_CURSOR_VERSION
7699                        && position.connection.valid_resume(position.offset)
7700                })
7701                .ok_or_else(|| SourceError::Config {
7702                    message: "page cursor is invalid".into(),
7703                })?,
7704        };
7705        let search = self.board_search(Some(also));
7706        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
7707        let own = self.with_own_writes(Vec::new())?;
7708        // An issue this process commented on is a candidate of a comment-activity read
7709        // whether or not the search has caught up with the comment; see `Self::commented`.
7710        let commented = match query.commented_since {
7711            Some(_) => self.commented()?.clone(),
7712            None => Vec::new(),
7713        };
7714        for id in own.iter().map(|item| &item.id).chain(&commented) {
7715            if !position.own.contains(id) {
7716                position.own.push(id.clone());
7717            }
7718        }
7719        let mut tasks = Vec::new();
7720        while !position.connection.exhausted() && tasks.len() < limit {
7721            let first = SEARCH_PAGE_SIZE;
7722            // Page size is part of the key: a short cached answer cannot answer a wider ask.
7723            let key =
7724                serde_json::to_string(&("page", &search, &position.connection.after(), first))
7725                    .expect("search page key is serializable");
7726            let cached = if query.commented_since.is_none() {
7727                self.narrowed_cache()?.get(&key).cloned()
7728            } else {
7729                None
7730            };
7731            let (found, next) = match cached {
7732                Some(found) => {
7733                    let next = self
7734                        .search_next
7735                        .lock()
7736                        .map_err(|_| SourceError::Unavailable {
7737                            message:
7738                                "search pagination was left inconsistent; run the command again"
7739                                    .into(),
7740                        })?
7741                        .get(&key)
7742                        .cloned()
7743                        .flatten();
7744                    (found, next)
7745                }
7746                None => {
7747                    let (found, next) = self
7748                        .search_page(&search, first, position.connection.after())
7749                        .await?;
7750                    if query.commented_since.is_none() {
7751                        self.search_next
7752                            .lock()
7753                            .map_err(|_| SourceError::Unavailable {
7754                                message:
7755                                    "search pagination was left inconsistent; run the command again"
7756                                        .into(),
7757                            })?
7758                            .insert(key.clone(), next.clone());
7759                        self.narrowed_cache()?.insert(key, found.clone());
7760                    }
7761                    (found, next)
7762                }
7763            };
7764            let rows = found.len();
7765            // Every candidate of the page this read confirms by its comments is read together.
7766            let candidates =
7767                self.comment_candidates(found.iter().skip(position.offset), query, &query.project)?;
7768            self.hold_comment_pages(&candidates).await?;
7769            for mut item in found.into_iter().skip(position.offset) {
7770                if tasks.len() == limit {
7771                    break;
7772                }
7773                position.offset += 1;
7774                if position.own.contains(&item.id) {
7775                    if position.seen.contains(&item.id) {
7776                        continue;
7777                    }
7778                    position.seen.push(item.id.clone());
7779                    // The search's own copy of an issue this process only commented on is as
7780                    // good as a node read of it, since its comments are read either way.
7781                    let only_commented = commented.contains(&item.id)
7782                        && !own.iter().any(|written| written.id == item.id);
7783                    if !only_commented {
7784                        let updated_at = item.updated_at;
7785                        let Some(written) = self.search_written(&own, &item.id).await? else {
7786                            continue;
7787                        };
7788                        item = written;
7789                        item.updated_at = item.updated_at.max(updated_at);
7790                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
7791                    }
7792                }
7793                if item.kind == BoardKind::Work(ItemKind::Task) {
7794                    let task = item.task()?;
7795                    if task_matches(&task, query, &query.project)
7796                        && self.commented_since(&item, query.commented_since).await?
7797                    {
7798                        tasks.push(task);
7799                    }
7800                }
7801            }
7802            if position.offset < rows {
7803                continue;
7804            }
7805            position.offset = 0;
7806            position.connection = match next {
7807                Some(after) => SearchConnection::Continuing {
7808                    after: Cursor(after),
7809                },
7810                None => SearchConnection::Exhausted {},
7811            };
7812        }
7813        if position.connection.exhausted() {
7814            for id in position.own.clone() {
7815                if position.seen.contains(&id) {
7816                    continue;
7817                }
7818                if tasks.len() == limit {
7819                    break;
7820                }
7821                position.seen.push(id.clone());
7822                let Some(item) = self.search_written(&own, &id).await? else {
7823                    continue;
7824                };
7825                if item.kind == BoardKind::Work(ItemKind::Task) {
7826                    let task = item.task()?;
7827                    if task_matches(&task, query, &query.project)
7828                        && self.commented_since(&item, query.commented_since).await?
7829                    {
7830                        tasks.push(task);
7831                    }
7832                }
7833            }
7834        }
7835        let more = !position.connection.exhausted()
7836            || position.own.iter().any(|id| !position.seen.contains(id));
7837        Ok(Page {
7838            items: tasks,
7839            next: more.then(|| {
7840                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
7841            }),
7842        })
7843    }
7844
7845    /// A resumed process has the ids but no write records; resolve only a record the
7846    /// current page needs, by its uncached node read rather than the lagging search index.
7847    async fn search_written(
7848        &self,
7849        own: &[Resolved],
7850        id: &NativeId,
7851    ) -> Result<Option<Resolved>, SourceError> {
7852        match own.iter().find(|item| item.id == *id) {
7853            Some(item) => Ok(Some(item.clone())),
7854            None => self.item_by_id(id).await,
7855        }
7856    }
7857
7858    /// The candidates for a task query carrying a text, metadata or origin predicate, read
7859    /// without enumerating the board — or `None` for a query carrying none of the three, which
7860    /// keeps the reads it always had.
7861    ///
7862    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
7863    /// because it names at most a handful of items. Text and metadata are answered by one
7864    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
7865    /// further by `updated:>=` when the query also asks for comment activity, since both
7866    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
7867    /// process afterwards by the same predicates [`task_matches`] applies to every read.
7868    ///
7869    /// Completed with what this process wrote, its own record winning over the index's copy
7870    /// of the same item: see [`Self::with_own_writes`].
7871    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
7872        self.send_pending_origins(None).await?;
7873        let asked = match (&query.origin, narrowing_qualifiers(query)) {
7874            (Some(origin), _) => Narrowing::Origin(origin.clone()),
7875            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
7876                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
7877                None => qualifiers,
7878            }),
7879            (None, None) => return Ok(None),
7880        };
7881        // A question about comment activity is asked afresh every time, as it always was: it
7882        // is the one a caller polls from one source while waiting for the index, and an
7883        // answer held from the first poll would be the answer to every later one.
7884        let key = query.commented_since.is_none().then(|| asked.key());
7885        let cached = match &key {
7886            Some(key) => self.narrowed_cache()?.get(key).cloned(),
7887            None => None,
7888        };
7889        let found = match cached {
7890            Some(found) => found,
7891            None => {
7892                let found = match &asked {
7893                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
7894                    Narrowing::Search(also) => self.searched(also).await?,
7895                };
7896                if let Some(key) = key {
7897                    self.narrowed_cache()?.insert(key, found.clone());
7898                }
7899                found
7900            }
7901        };
7902        self.with_own_writes(found).map(Some)
7903    }
7904
7905    /// The candidates for a project or unscoped document query carrying a searchable text,
7906    /// read without enumerating the board — or `None` for a query with no text or a blank one,
7907    /// which keeps the read it always had.
7908    ///
7909    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
7910    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
7911    /// costs is the issues that match and never the board. Its answer is held for the command
7912    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
7913    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
7914    /// substring rule, exactly as an item of the wider read was, and is completed with what this
7915    /// process wrote: see [`Self::with_own_writes`].
7916    async fn text_searched(
7917        &self,
7918        text: Option<&TextQuery>,
7919    ) -> Result<Option<Vec<Resolved>>, SourceError> {
7920        let Some(also) = text_qualifiers(text) else {
7921            return Ok(None);
7922        };
7923        let key = Narrowing::Search(also.clone()).key();
7924        let cached = self.narrowed_cache()?.get(&key).cloned();
7925        let found = match cached {
7926            Some(found) => found,
7927            None => {
7928                let found = self.searched(&also).await?;
7929                self.narrowed_cache()?.insert(key, found.clone());
7930                found
7931            }
7932        };
7933        self.with_own_writes(found).map(Some)
7934    }
7935
7936    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
7937    /// [`Self::narrowed`] reads for a task query carrying that origin — one
7938    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
7939    /// what this process wrote — each confirmed against its own origin entry.
7940    ///
7941    /// What answers a project or document listing by origin, so finding every copy of one plan
7942    /// costs the items that carry its origin and never a page of the board.
7943    async fn copied_from(
7944        &self,
7945        origin: &str,
7946        kind: BoardKind,
7947    ) -> Result<Vec<Resolved>, SourceError> {
7948        let wanted = TaskQuery {
7949            origin: Some(origin.to_owned()),
7950            ..TaskQuery::default()
7951        };
7952        Ok(self
7953            .narrowed(&wanted)
7954            .await?
7955            .unwrap_or_default()
7956            .into_iter()
7957            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
7958            .collect())
7959    }
7960
7961    /// Every item of this board that may carry `origin` — a superset of those that do — found
7962    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
7963    ///
7964    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
7965    /// which reads the field every carrier holds, whichever release wrote it — and the
7966    /// board-scoped issue search for the same id as a phrase in the body, where this source
7967    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
7968    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
7969    /// query's, exactly.
7970    ///
7971    /// Both connections are walked to exhaustion, each from its own cursor. One that has
7972    /// already ended is sent its last cursor again, which answers an empty page, so the one
7973    /// document serves every page of either. What the two leave is stated in the module
7974    /// documentation: a carrier another process added within the last second or two, before
7975    /// either index has it.
7976    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
7977        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
7978        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
7979        let mut items_after: Option<String> = None;
7980        let mut search_after: Option<String> = None;
7981        let mut found: Vec<Resolved> = Vec::new();
7982        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
7983            if !found.iter().any(|held| held.id == resolved.id) {
7984                found.push(resolved);
7985            }
7986        };
7987        loop {
7988            let data = self
7989                .graphql(
7990                    graphql::ORIGIN_LOOKUP,
7991                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
7992                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
7993                           "itemsAfter":items_after,"searchAfter":search_after,
7994                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7995                           "duplicates":true}),
7996                )
7997                .await?;
7998            let items = data
7999                .pointer("/originItems/projectV2/items")
8000                .filter(|value| !value.is_null())
8001                .ok_or_else(|| SourceError::Refused {
8002                    message: format!(
8003                        "GitHub project {}/{} was not found or is not visible to the token",
8004                        self.owner, self.project_number
8005                    ),
8006                })?;
8007            for item in optional_nodes(Some(items), "project items")?
8008                .into_iter()
8009                .flatten()
8010            {
8011                // The board's own items list its drafts too, and a draft is not an issue: no
8012                // narrowed read answers with one, whatever its origin field holds.
8013                if let Some(resolved) = self.resolve(item)?
8014                    && resolved.content_kind == ContentKind::Issue
8015                {
8016                    keep(resolved, &mut found);
8017                }
8018            }
8019            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
8020                message: "GitHub search response has no search connection".into(),
8021            })?;
8022            for node in optional_nodes(Some(searched), "search")?
8023                .into_iter()
8024                .flatten()
8025            {
8026                if let Some(resolved) = self.resolve_issue(node).await? {
8027                    keep(resolved, &mut found);
8028                }
8029            }
8030            let items_next = resumed(items, items_after.as_deref())?;
8031            let search_next = resumed(searched, search_after.as_deref())?;
8032            if !items_next.has_more() && !search_next.has_more() {
8033                return Ok(found);
8034            }
8035            items_after = items_next.cursor();
8036            search_after = search_next.cursor();
8037        }
8038    }
8039
8040    /// `found`, with every item this process created or wrote in its place, and every one of
8041    /// them the read did not report added.
8042    ///
8043    /// This process's own record wins over the read's copy of the same item, because a read
8044    /// of an item written moments ago can still be behind what was written onto it — the
8045    /// origin field included, which is the one a narrowed read is confirmed against — and a
8046    /// read that still names an item under a predicate this process's write moved it out of
8047    /// must not return it. The one thing the read knows that the record cannot is when GitHub
8048    /// last saw the item change, which is what a comment-activity read rules a candidate out
8049    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
8050    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
8051    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
8052        // A board draft is not an issue, so no narrowed read returns one, and this process
8053        // having written one does not make it an answer either.
8054        let own: Vec<Resolved> = self
8055            .created()?
8056            .iter()
8057            .chain(self.updated()?.iter())
8058            .filter(|own| own.content_kind == ContentKind::Issue)
8059            .cloned()
8060            .collect();
8061        for mut own in own {
8062            self.resolved_cache()?.insert(own.id.clone(), own.clone());
8063            match found.iter_mut().find(|read| read.id == own.id) {
8064                Some(read) => {
8065                    own.updated_at = own.updated_at.max(read.updated_at);
8066                    *read = own;
8067                }
8068                None => found.push(own),
8069            }
8070        }
8071        Ok(found)
8072    }
8073
8074    /// Whether `item` has a comment created or last edited at or after `since` — always, when
8075    /// there is no instant to hold it to.
8076    ///
8077    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
8078    /// or after the instant moved it there: an issue not updated since holds no such comment,
8079    /// and its comments are never asked for — unless this process commented on it in this
8080    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
8081    /// only as far as the first that matches. A board draft is not an issue and has no
8082    /// comments, so it never matches.
8083    async fn commented_since(
8084        &self,
8085        item: &Resolved,
8086        since: Option<DateTime<Utc>>,
8087    ) -> Result<bool, SourceError> {
8088        let Some(since) = since else {
8089            return Ok(true);
8090        };
8091        if item.content_kind == ContentKind::DraftIssue {
8092            return Ok(false);
8093        }
8094        // An `updatedAt` this process's own record or a lagging index holds can predate a
8095        // comment this process wrote since, so only an issue it did not comment on is ruled
8096        // out by one.
8097        if item.updated_at.is_some_and(|updated| updated < since)
8098            && !self.commented()?.contains(&item.id)
8099        {
8100            return Ok(false);
8101        }
8102        let query = TaskQuery {
8103            commented_since: Some(since),
8104            ..TaskQuery::default()
8105        };
8106        let mut after: Option<String> = None;
8107        // A first page read beside its fellow candidates answers as this read's own would.
8108        let held = self.held_comments()?.get(&item.id).cloned();
8109        if let Some(first) = held {
8110            if query.comments_match(&first.items) {
8111                return Ok(true);
8112            }
8113            match first.next {
8114                Some(next) => after = Some(next.0),
8115                None => return Ok(false),
8116            }
8117        }
8118        loop {
8119            let data = self
8120                .graphql(
8121                    graphql::ISSUE_COMMENTS,
8122                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
8123                )
8124                .await?;
8125            let Some(connection) = data
8126                .get("node")
8127                .filter(|value| !value.is_null())
8128                .and_then(|node| node.get("comments"))
8129                .filter(|value| !value.is_null())
8130            else {
8131                // Removed since the search reported it: no longer an issue with comments.
8132                return Ok(false);
8133            };
8134            let comments = optional_nodes(Some(connection), "issue comments")?
8135                .into_iter()
8136                .flatten()
8137                .map(comment_from)
8138                .collect::<Result<Vec<_>, _>>()?;
8139            if query.comments_match(&comments) {
8140                return Ok(true);
8141            }
8142            match next_cursor(connection)? {
8143                Some(next) => {
8144                    validate_cursor_progress(after.as_deref(), &next.0)?;
8145                    after = Some(next.0);
8146                }
8147                None => return Ok(false),
8148            }
8149        }
8150    }
8151
8152    /// Every item on the board: the union of both enumerations GitHub offers of one.
8153    ///
8154    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
8155    /// board **draft** and reads the board's own fields beside its items, and only the search
8156    /// reports an item that connection is behind on. The module documentation is where the lag and the
8157    /// measurements behind it are written down.
8158    ///
8159    /// A search result is admitted on the same terms as any other issue this source reaches
8160    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
8161    /// names *this* board — so an issue the index still believes is here after it was taken
8162    /// off is refused rather than reported.
8163    ///
8164    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
8165    /// which is what the cache could otherwise have broken.
8166    async fn board(&self) -> Result<Board, SourceError> {
8167        self.board_completed_by(None).await
8168    }
8169
8170    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
8171    /// that half could add is then answered only if it holds.
8172    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
8173        let cached = self.board_cache()?.clone();
8174        let mut board = match cached {
8175            Some(board) => board,
8176            None => {
8177                let read = self.read_board().await?;
8178                *self.board_cache()? = Some(read.clone());
8179                read
8180            }
8181        };
8182        let searched = match also {
8183            None => self.searched_issues().await?,
8184            Some(also) => self.searched_narrowly(also).await?,
8185        };
8186        // A complete sweep asks for every page of this view. Reconcile ids once per
8187        // view, rather than scanning the board for every search result on every page.
8188        let mut ids: std::collections::HashSet<_> =
8189            board.items.iter().map(|item| item.id.clone()).collect();
8190        for held in searched {
8191            if ids.insert(held.id.clone()) {
8192                board.items.push(held);
8193            }
8194        }
8195        for own in self.created()?.iter() {
8196            if ids.insert(own.id.clone()) {
8197                board.items.push(own.clone());
8198            }
8199        }
8200        Ok(board)
8201    }
8202
8203    /// This process's own view of the board, or the refusal a poisoned lock is.
8204    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
8205        self.board_cache
8206            .lock()
8207            .map_err(|_| SourceError::Unavailable {
8208                message: "this source's view of the board was left inconsistent by an earlier \
8209                      failure; next: run the command again"
8210                    .into(),
8211            })
8212    }
8213
8214    /// Bring this process's own view of the board up to an item it has just written.
8215    ///
8216    /// A created item goes to `created`, which is what completes a board read GitHub's own
8217    /// eventual consistency has left behind. An item that was already there is replaced
8218    /// where it sits, so a second write of it in the same command reads its real parent
8219    /// rather than the one it had before the first write.
8220    ///
8221    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
8222    /// that wins: an item this same run created is held in `created` and not in the cached
8223    /// board, and `board` completes the cached board *from* `created`, so replacing only
8224    /// the cached copy of such an item replaces nothing and the read still reports the
8225    /// title it was created with. The search is the third, and it is the one an item the
8226    /// board's own projection is behind on sits in *alone* — which is exactly the item this
8227    /// source is least able to re-read, so leaving it out would put the stale title back on
8228    /// the only items the completion in [`Self::board`] exists for.
8229    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
8230        self.resolved_cache()?.insert(item.id.clone(), item.clone());
8231        if created {
8232            self.created()?.push(item);
8233            return Ok(());
8234        }
8235        {
8236            let mut own = self.created()?;
8237            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
8238                *held = item;
8239                return Ok(());
8240            }
8241        }
8242        {
8243            let mut own = self.updated()?;
8244            match own.iter_mut().find(|held| held.id == item.id) {
8245                Some(held) => *held = item.clone(),
8246                None => own.push(item.clone()),
8247            }
8248        }
8249        if let Some(board) = self.board_cache()?.as_mut()
8250            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
8251        {
8252            *held = item.clone();
8253        }
8254        if let Some(found) = self.search_cache()?.as_mut()
8255            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
8256        {
8257            *held = item.clone();
8258        }
8259        for found in self.narrowed_cache()?.values_mut() {
8260            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
8261                *held = item.clone();
8262            }
8263        }
8264        for (_, found) in self.children_cache()?.values_mut() {
8265            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
8266                *held = item.clone();
8267            }
8268        }
8269        Ok(())
8270    }
8271
8272    /// Forget one item this process has just deleted, from every half of its own view.
8273    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
8274        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
8275            created.retain(|held| held.content != *id);
8276        }
8277        self.resolved_cache()?.remove(id);
8278        self.read_ahead()?.remove(id);
8279        self.carried()?.remove(id);
8280        self.created()?.retain(|own| own.id != *id);
8281        self.updated()?.retain(|own| own.id != *id);
8282        self.commented()?.retain(|own| own != id);
8283        if let Some(board) = self.board_cache()?.as_mut() {
8284            board.items.retain(|item| item.id != *id);
8285        }
8286        if let Some(found) = self.search_cache()?.as_mut() {
8287            found.retain(|item| item.id != *id);
8288        }
8289        for found in self.narrowed_cache()?.values_mut() {
8290            found.retain(|item| item.id != *id);
8291        }
8292        for (_, found) in self.children_cache()?.values_mut() {
8293            found.retain(|item| item.id != *id);
8294        }
8295        Ok(())
8296    }
8297
8298    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
8299    fn narrowed_cache(
8300        &self,
8301    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
8302        self.narrowed_cache
8303            .lock()
8304            .map_err(|_| SourceError::Unavailable {
8305                message: "this source's view of a narrowed read was left inconsistent by an \
8306                      earlier failure; next: run the command again"
8307                    .into(),
8308            })
8309    }
8310
8311    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
8312    /// is.
8313    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
8314    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
8315        Ok(self.task_children()?.get(project).cloned())
8316    }
8317
8318    fn task_children(
8319        &self,
8320    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
8321        self.task_children
8322            .lock()
8323            .map_err(|_| SourceError::Unavailable {
8324                message: "this source's record of a project's tasks was left inconsistent by an \
8325                          earlier failure; next: run the command again"
8326                    .into(),
8327            })
8328    }
8329
8330    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
8331        self.children_cache
8332            .lock()
8333            .map_err(|_| SourceError::Unavailable {
8334                message: "this source's view of a project's tasks was left inconsistent by an \
8335                      earlier failure; next: run the command again"
8336                    .into(),
8337            })
8338    }
8339
8340    /// Every page of the board, read from GitHub.
8341    async fn read_board(&self) -> Result<Board, SourceError> {
8342        let mut after: Option<String> = None;
8343        let mut items = Vec::new();
8344        let mut board;
8345        loop {
8346            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
8347            for item in page
8348                .pointer("/items/nodes")
8349                .and_then(Value::as_array)
8350                .ok_or_else(|| SourceError::Malformed {
8351                    message: "GitHub project items.nodes is not an array".into(),
8352                })?
8353            {
8354                if let Some(resolved) = self.resolve(item)? {
8355                    items.push(resolved);
8356                }
8357            }
8358            let info = page
8359                .pointer("/items/pageInfo")
8360                .ok_or_else(|| SourceError::Malformed {
8361                    message: "GitHub project items have no pageInfo".into(),
8362                })?;
8363            let has_next = required_bool(info, "hasNextPage")?;
8364            let next = has_next
8365                .then(|| required_str(info, "endCursor"))
8366                .transpose()?;
8367            board = page.clone();
8368            match next {
8369                Some(next) => {
8370                    validate_cursor_progress(after.as_deref(), next)?;
8371                    after = Some(next.to_owned());
8372                }
8373                None => break,
8374            }
8375        }
8376        Ok(Board {
8377            id: required_str(&board, "id")?.to_owned(),
8378            fields: board.get("fields").cloned().unwrap_or(Value::Null),
8379            items,
8380        })
8381    }
8382
8383    /// The existing items this source has written, for completing a narrowed read that is
8384    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
8385    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
8386        self.updated.lock().map_err(|_| SourceError::Unavailable {
8387            message: "this source's record of what it wrote in this run was left inconsistent \
8388                      by an earlier failure; next: run the command again"
8389                .into(),
8390        })
8391    }
8392
8393    /// The issues this source has commented on in this command; see
8394    /// [`Self::commented`](GitHubProjectsSource::commented).
8395    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
8396        self.commented.lock().map_err(|_| SourceError::Unavailable {
8397            message: "this source's record of what it commented on in this run was left \
8398                      inconsistent by an earlier failure; next: run the command again"
8399                .into(),
8400        })
8401    }
8402
8403    /// Called only once GitHub has answered the comment write, so an issue whose comment
8404    /// failed is never made a candidate a later read would pay a node read for.
8405    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
8406        // A comment page held for this command is no longer what the issue holds.
8407        self.held_comments()?.remove(issue);
8408        let mut commented = self.commented()?;
8409        if !commented.contains(issue) {
8410            commented.push(issue.clone());
8411        }
8412        Ok(())
8413    }
8414
8415    /// The items this source has created, for completing a board read that is behind.
8416    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
8417        self.created.lock().map_err(|_| SourceError::Unavailable {
8418            message: "this source's record of what it created in this run was left \
8419                      inconsistent by an earlier failure; next: run the command again"
8420                .into(),
8421        })
8422    }
8423
8424    /// One board item as this source reports it, or `None` for content it ignores.
8425    ///
8426    /// A pull request is neither a project nor a task — it is somebody's change, not a
8427    /// unit of plan — and an item whose content the token cannot see has nothing to
8428    /// report at all.
8429    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
8430        let resolved = self.resolve_unremembered(item)?;
8431        if let Some(resolved) = &resolved {
8432            self.resolved_cache()?
8433                .insert(resolved.id.clone(), resolved.clone());
8434        }
8435        Ok(resolved)
8436    }
8437
8438    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
8439    /// write's own response needs, which says nothing this process should answer a later read
8440    /// with.
8441    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
8442        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
8443            message: "GitHub project item is missing content".into(),
8444        })?;
8445        if content.is_null() {
8446            return Ok(None);
8447        }
8448        let content_kind = match required_str(content, "__typename")? {
8449            "Issue" => ContentKind::Issue,
8450            "DraftIssue" => ContentKind::DraftIssue,
8451            _ => return Ok(None),
8452        };
8453        let field_values = item
8454            .get("fieldValues")
8455            .ok_or_else(|| SourceError::Malformed {
8456                message: "GitHub project item is missing fieldValues".into(),
8457            })?;
8458        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
8459        let nodes = field_values
8460            .get("nodes")
8461            .and_then(Value::as_array)
8462            .ok_or_else(|| SourceError::Malformed {
8463                message: "GitHub project item fieldValues.nodes is not an array".into(),
8464            })?;
8465        if let Some(labels) = content.get("labels") {
8466            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
8467        }
8468        let raw_body = optional_str(content, "body")?.map(str::to_owned);
8469        let (body, slot) = self.visible(raw_body.clone())?;
8470        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
8471            .map(|id| NativeId(id.to_owned()));
8472        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
8473        // to read one from; it is a task, and never a project.
8474        let sub_issues = match content_kind {
8475            ContentKind::Issue => sub_issue_total(content)?,
8476            ContentKind::DraftIssue => 0,
8477        };
8478        let content_id = required_str(content, "id")?;
8479        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
8480            message: format!("GitHub issue {content_id}: {message}"),
8481        })?;
8482        let raw_title = required_str(content, "title")?;
8483        // Read as children, a sub-issue is filed under no project and nothing is a project: the
8484        // GitHub parent is the parent relation instead.
8485        let (kind, parent, issue_parent) = match self.sub_issues {
8486            SubIssues::Project => (
8487                BoardKind::of(raw_title, parent.is_some(), sub_issues, marked),
8488                parent,
8489                None,
8490            ),
8491            SubIssues::Parent => (
8492                if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
8493                    BoardKind::Document
8494                } else {
8495                    BoardKind::Work(ItemKind::Task)
8496                },
8497                None,
8498                parent,
8499            ),
8500        };
8501        // The title a person wrote, which for a document is the one without the prefix —
8502        // the same way `content` above is the body without this source's metadata slot.
8503        let title = match kind {
8504            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
8505            BoardKind::Work(_) => raw_title.to_owned(),
8506        };
8507        let own_repository = content
8508            .pointer("/repository/nameWithOwner")
8509            .and_then(Value::as_str)
8510            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
8511            .transpose()
8512            .map_err(|message| SourceError::Malformed { message })?;
8513        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
8514            Repository::from_metadata(&slot)
8515                .map_err(|message| SourceError::Malformed { message })?
8516        } else {
8517            own_repository.clone().into_iter().collect()
8518        };
8519        let classification = Classification::from_metadata(&slot)
8520            .map_err(|message| SourceError::Malformed { message })?;
8521        let id = NativeId(content_id.to_owned());
8522        // Read only for a task, because only a task has either list: a project or a
8523        // document holding one of these keys holds nothing this source reports, and the
8524        // keys are left out of its caller-visible metadata all the same.
8525        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
8526            let listed = |key: &str| {
8527                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
8528                    .map_err(|message| SourceError::Malformed { message })
8529            };
8530            (
8531                listed(TaskRef::DELIVERS_KEY)?,
8532                listed(TaskRef::DELIVERED_BY_KEY)?,
8533            )
8534        } else {
8535            (Vec::new(), Vec::new())
8536        };
8537        let (option, closed, reason) = Self::status_parts(nodes, content)?;
8538        let priority = self.held_priority(nodes)?;
8539        let item_labels = labels(content)?;
8540        // Present when the item was reached through its own issue, whose board entry
8541        // names the board; a read of the board's own items has the board already. An
8542        // empty id names nothing a field write could address, so it is read as absent and
8543        // the write goes back to reading the board.
8544        let board_id = item
8545            .pointer("/project/id")
8546            .and_then(Value::as_str)
8547            .filter(|id| !id.is_empty());
8548        let resolved = Resolved {
8549            item_id: required_str(item, "id")?.to_owned(),
8550            id,
8551            content_kind,
8552            kind,
8553            title,
8554            body: body.filter(|value| !value.is_empty()),
8555            raw_body,
8556            status: self
8557                .statuses
8558                .status(kind.status_kind(), option, closed, reason),
8559            option: option.map(str::to_owned),
8560            priority,
8561            closed,
8562            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
8563            delivers,
8564            delivered_by,
8565            labels: item_labels.clone(),
8566            parent,
8567            issue_parent,
8568            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
8569            projected: self.held_projections(nodes)?,
8570            number: match content_kind {
8571                ContentKind::Issue => Some(issue_number(content)?),
8572                // A draft is filed in no repository, so nothing ever numbered it:
8573                // `DraftIssue` declares no `number` at all, exactly as it declares no
8574                // `subIssuesSummary` the branch above reads.
8575                ContentKind::DraftIssue => None,
8576            },
8577            url: optional_str(content, "url")?.map(str::to_owned),
8578            created_at: optional_time(content, "createdAt")?,
8579            updated_at: optional_time(content, "updatedAt")?,
8580            item_updated_at: optional_time(item, "updatedAt")?,
8581            comment_count: content
8582                .pointer("/commentCount/totalCount")
8583                .and_then(Value::as_u64),
8584            relationship_events: content
8585                .pointer("/relationshipEvents/totalCount")
8586                .and_then(Value::as_u64),
8587            // The issue node is stamped wherever it was read; a board item a read of the issue
8588            // put together from its memberships is not.
8589            served_at: match optional_time(content, SERVED_AT)? {
8590                Some(served) => Some(served),
8591                None => optional_time(item, SERVED_AT)?,
8592            },
8593            // Both connections were read to their end above — one reporting a further page is
8594            // refused — so these are every field value and every label the item holds. A
8595            // response that did not select the labels at all says nothing of them, and gives
8596            // no facets.
8597            facets: content.get("labels").is_some().then(|| {
8598                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
8599            }),
8600            own_repository,
8601            repositories,
8602            classification,
8603            slot,
8604            board_id: board_id.map(str::to_owned),
8605            fields: field_definitions(nodes),
8606            board_fields: Self::carried_board_fields(content, board_id)?,
8607            blocked_by: carried_blocked_by(content)?,
8608            assignees: if self.assignees {
8609                Some(assignees::assignee_logins(content)?)
8610            } else {
8611                None
8612            },
8613        };
8614        Ok(Some(resolved))
8615    }
8616
8617    /// The field definitions of the board `board_id` names — the project this issue's own
8618    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
8619    /// `None` when the read carried none, carried no entry for that board, or the board item
8620    /// named no board, which a write then answers by reading the board's fields itself.
8621    ///
8622    /// Matched by the board's node id and never by its number alone: a project number is
8623    /// unique only within its owner, so another owner's board numbered alike can sit on the
8624    /// same page, and its field and option ids address nothing on this one.
8625    fn carried_board_fields(
8626        content: &Value,
8627        board_id: Option<&str>,
8628    ) -> Result<Option<Value>, SourceError> {
8629        let (Some(nodes), Some(board_id)) = (
8630            content.pointer("/boards/nodes").and_then(Value::as_array),
8631            board_id,
8632        ) else {
8633            return Ok(None);
8634        };
8635        let Some(board) = nodes.iter().find_map(|node| {
8636            let project = node.get("project")?;
8637            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
8638        }) else {
8639            return Ok(None);
8640        };
8641        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
8642            return Ok(None);
8643        };
8644        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
8645        Ok(Some(fields.clone()))
8646    }
8647
8648    /// What one board item's `Priority` field says, through this instance's mapping.
8649    ///
8650    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
8651    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
8652    /// option the mapping does not name is kept as itself — never read as a level or as
8653    /// `none` — for a read of the task to report by name.
8654    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
8655        let Some(mapping) = &self.priorities else {
8656            return Ok(HeldPriority::Read(Priority::None));
8657        };
8658        // A value of the field that names no option — a text field someone called `Priority` —
8659        // is malformed rather than `none`: reading it as no priority would let the next copy
8660        // clear one a person set.
8661        let Some(option) = field_values
8662            .iter()
8663            .find(|value| {
8664                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
8665            })
8666            .map(|value| required_str(value, "name"))
8667            .transpose()?
8668        else {
8669            return Ok(HeldPriority::Read(Priority::None));
8670        };
8671        Ok(mapping.priority_of(option).map_or_else(
8672            || HeldPriority::Unmapped(option.to_owned()),
8673            HeldPriority::Read,
8674        ))
8675    }
8676
8677    /// What each projected metadata field holds for one board item, by field name — every text
8678    /// value GitHub answers, an empty one included.
8679    fn held_projections(
8680        &self,
8681        field_values: &[Value],
8682    ) -> Result<BTreeMap<String, String>, SourceError> {
8683        let mut held = BTreeMap::new();
8684        for projection in &self.metadata_fields {
8685            if let Some(text) = text_field(field_values, &projection.field)? {
8686                held.insert(projection.field.to_string(), text);
8687            }
8688        }
8689        Ok(held)
8690    }
8691
8692    /// What one board item's status is read from: its `Status` option, whether its issue
8693    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
8694    /// three into the status it reports.
8695    fn status_parts<'a>(
8696        field_values: &'a [Value],
8697        content: &'a Value,
8698    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
8699        let option = field_values
8700            .iter()
8701            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
8702            .map(|value| required_str(value, "name"))
8703            .transpose()?;
8704        let closed = optional_str(content, "state")? == Some("CLOSED");
8705        Ok((option, closed, optional_str(content, "stateReason")?))
8706    }
8707
8708    /// The board Status option this write selects, or the refusal that says why not.
8709    ///
8710    /// The mapped option is required for both open and terminal targets. A terminal write
8711    /// validates it before changing either representation, so it can never fall back to
8712    /// closing an issue whose board cannot display the matching status.
8713    ///
8714    /// Answers the field's id, the option's id, and the option's name as the board spells
8715    /// it — which is the name a read of the item reports once it sits there.
8716    fn column_for(
8717        &self,
8718        fields: &Value,
8719        kind: ItemKind,
8720        category: StatusCategory,
8721        target: &StatusTarget,
8722    ) -> Result<Option<(String, String, String)>, SourceError> {
8723        let Some(wanted) = target.option() else {
8724            return Ok(None);
8725        };
8726        let missing = |detail: &str| SourceError::Refused {
8727            message: format!(
8728                "{} status {} of source {} needs the board Status option {wanted:?}, and \
8729                 {detail}; next: add that option to the board, which `onetaskgraph sources \
8730                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
8731                 it has",
8732                kind.marker(),
8733                category_name(category),
8734                self.name,
8735                self.name,
8736                category_name(category),
8737                kind.marker()
8738            ),
8739        };
8740        let Some(field) = Board::field(fields, "Status")? else {
8741            return Err(missing("this board has no Status field"));
8742        };
8743        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
8744            return Err(missing(
8745                "this board's Status field is not a single-select field",
8746            ));
8747        }
8748        let option = field
8749            .get("options")
8750            .and_then(Value::as_array)
8751            .and_then(|options| {
8752                options.iter().find(|option| {
8753                    option
8754                        .get("name")
8755                        .and_then(Value::as_str)
8756                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8757                })
8758            });
8759        match option {
8760            None => Err(missing("this board does not have it")),
8761            Some(option) => Ok(Some((
8762                required_str(field, "id")?.to_owned(),
8763                required_str(option, "id")?.to_owned(),
8764                required_str(option, "name")?.to_owned(),
8765            ))),
8766        }
8767    }
8768
8769    /// The refusal a status that closes an issue is answered with over a board draft.
8770    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
8771        SourceError::Refused {
8772            message: format!(
8773                "status {} of source {} closes the item's issue, and GitHub draft items have \
8774                 no open or closed state",
8775                category_name(category),
8776                self.name
8777            ),
8778        }
8779    }
8780
8781    /// What a status write to one item needs of the board: the board's id and the
8782    /// definition of its `Status` field, read off the item when the item says both.
8783    ///
8784    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
8785    /// and its `Status` value carries that field's definition, options and all. An item that
8786    /// does not say — no board id, or no `Status` value to read the field off — takes them
8787    /// from [`Self::board_fields`], which reads no item.
8788    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
8789        if let Some(board) = item.carried_board() {
8790            return Ok(board);
8791        }
8792        if item.defines("Status")
8793            && let Some(board_id) = item.named_board()
8794        {
8795            return Ok(BoardFields {
8796                id: board_id,
8797                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8798            });
8799        }
8800        self.board_fields().await
8801    }
8802
8803    /// Set one task's or one project's status and nothing else; see
8804    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
8805    async fn set_status(
8806        &self,
8807        id: &NativeId,
8808        kind: ItemKind,
8809        category: StatusCategory,
8810    ) -> Result<Option<Status>, SourceError> {
8811        // Refused before anything is read, in the words a write of the same status is.
8812        let target = self.resolved_target(kind, category)?;
8813        // An item read by issue reference carries only what this write needs, so what it
8814        // becomes is never remembered for a later read to be answered from.
8815        let held = self.take_held(id)?;
8816        let by_reference = held.is_some();
8817        let bound = match held {
8818            Some(item) => Some(item),
8819            None => self.bound_item(id).await?,
8820        };
8821        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
8822            return Ok(None);
8823        };
8824        let board = self.status_board(&item).await?;
8825        let (field, option, name) = self
8826            .column_for(&board.fields, kind, category, &target)?
8827            .ok_or_else(|| SourceError::Malformed {
8828                message: format!(
8829                    "status {} of source {} names no board Status option",
8830                    category_name(category),
8831                    self.name
8832                ),
8833            })?;
8834        if item.status.category == category && item.option.as_deref() == Some(&name) {
8835            return Ok(Some(item.status));
8836        }
8837        match &target {
8838            StatusTarget::Terminal(_, reason) => {
8839                if item.content_kind == ContentKind::DraftIssue {
8840                    return Err(self.closes_a_draft(category));
8841                }
8842                // The option first, so a refused selection leaves an open issue open rather
8843                // than closed behind a card that still reads open.
8844                self.set_item_field(
8845                    board.id.as_str(),
8846                    &item.item_id,
8847                    &field,
8848                    json!({"singleSelectOptionId": option}),
8849                )
8850                .await?;
8851                // An issue already closed in a way that reads as this category keeps its state
8852                // and its reason: only its card was out of line, and closing it again would
8853                // overwrite a reason — a duplicate's — that says more than this write does.
8854                let already = item.closed && item.status.category == category;
8855                if !already {
8856                    self.update_content(
8857                        ContentKind::Issue,
8858                        &item.id,
8859                        json!({"stateInput": state_input(Some(&target))}),
8860                    )
8861                    .await?;
8862                }
8863                let reason = if already {
8864                    item.reason.clone()
8865                } else {
8866                    Some(reason.reason().to_owned())
8867                };
8868                item.closed = true;
8869                item.status = self
8870                    .statuses
8871                    .status(kind, Some(&name), true, reason.as_deref());
8872                item.option = Some(name);
8873                item.reason = reason;
8874            }
8875            StatusTarget::Column(_) => {
8876                // An option is what an open item's status is, so a closed issue is reopened
8877                // first — sitting closed in the column, it would read back as closed. A draft has
8878                // no state to reopen.
8879                if item.content_kind == ContentKind::Issue && item.closed {
8880                    self.update_content(
8881                        ContentKind::Issue,
8882                        &item.id,
8883                        json!({"stateInput": state_input(Some(&target))}),
8884                    )
8885                    .await?;
8886                    item.closed = false;
8887                }
8888                self.set_item_field(
8889                    board.id.as_str(),
8890                    &item.item_id,
8891                    &field,
8892                    json!({"singleSelectOptionId": option}),
8893                )
8894                .await?;
8895                item.status = self.statuses.status(kind, Some(&name), false, None);
8896                item.option = Some(name);
8897            }
8898            StatusTarget::Disabled(_) => {
8899                unreachable!("resolved_target refused a disabled status")
8900            }
8901        }
8902        let status = item.status.clone();
8903        if !by_reference {
8904            self.remember_written(item, false)?;
8905        }
8906        Ok(Some(status))
8907    }
8908
8909    /// Replace one task's `delivered_by` and nothing else; see
8910    /// [`TaskSource::set_delivered_by`].
8911    ///
8912    /// One update of the body, which differs from the body GitHub holds only inside the
8913    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
8914    async fn replace_delivered_by(
8915        &self,
8916        id: &NativeId,
8917        delivered_by: &[TaskRef],
8918    ) -> Result<Option<()>, SourceError> {
8919        let entries = TaskRef::listed(
8920            TaskRef::DELIVERED_BY_KEY,
8921            id,
8922            Some(&self.name),
8923            delivered_by.to_vec(),
8924        )
8925        .map_err(|message| SourceError::Refused { message })?;
8926        let Some(mut item) = self
8927            .bound_item(id)
8928            .await?
8929            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8930        else {
8931            return Ok(None);
8932        };
8933        let mut slot = item.slot.clone();
8934        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
8935        self.write_slot(&mut item, &slot).await?;
8936        item.delivered_by = entries;
8937        self.remember_written(item, false)?;
8938        Ok(Some(()))
8939    }
8940
8941    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
8942    /// see [`TaskSource::set_task_metadata`].
8943    ///
8944    /// `None` when this board holds no item by that id, or holds one of another kind. The
8945    /// answer is the item as this source now reads it, so what a caller is told the key
8946    /// holds is what the slot holds.
8947    ///
8948    /// A key already holding the value is answered without a write, compared as JSON rather
8949    /// than as the body's bytes: a slot a person spelled with other whitespace would
8950    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
8951    async fn set_slot_key(
8952        &self,
8953        id: &NativeId,
8954        kind: BoardKind,
8955        key: &MetadataKey,
8956        value: &Value,
8957    ) -> Result<Option<Resolved>, SourceError> {
8958        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8959            return Ok(None);
8960        };
8961        if item.slot.get(key.as_str()) == Some(value) {
8962            return Ok(Some(item));
8963        }
8964        let mut slot = item.slot.clone();
8965        slot.insert(key.as_str().to_owned(), value.clone());
8966        // A key a board text field projects moves that field first, so the body — the value's
8967        // one authoritative home — is written last, as every write of an existing item is.
8968        if self
8969            .metadata_fields
8970            .iter()
8971            .any(|projection| &*projection.key == key.as_str())
8972        {
8973            let board = self.projection_board(&item).await?;
8974            let (writes, projected) = self.projection_writes(
8975                &board.fields,
8976                Some(&item.projected),
8977                &slot,
8978                Some(&[key.as_str()]),
8979            )?;
8980            let (mut values, mut clears) = (Vec::new(), Vec::new());
8981            for write in &writes {
8982                write.join(&mut values, &mut clears);
8983            }
8984            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
8985                .await?;
8986            item.projected = projected;
8987        }
8988        self.write_slot(&mut item, &slot).await?;
8989        self.remember_written(item.clone(), false)?;
8990        Ok(Some(item))
8991    }
8992
8993    /// What a projection write to `item` needs of the board, read off the item when it says
8994    /// enough, as [`Self::fields_for`] reads it.
8995    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
8996        if let Some(board) = item.carried_board() {
8997            return Ok(board);
8998        }
8999        if let Some(board_id) = item.named_board()
9000            && self
9001                .metadata_fields
9002                .iter()
9003                .all(|projection| item.defines(&projection.field))
9004        {
9005            return Ok(BoardFields {
9006                id: board_id,
9007                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
9008            });
9009        }
9010        self.board_fields().await
9011    }
9012
9013    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
9014    /// `item` up to what that write left.
9015    ///
9016    /// The body sent differs from the body GitHub holds only inside the slot — see
9017    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
9018    /// the mutation the item's content takes, so a board draft's body is written with
9019    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
9020    async fn write_slot(
9021        &self,
9022        item: &mut Resolved,
9023        slot: &BTreeMap<String, Value>,
9024    ) -> Result<(), SourceError> {
9025        let held = item.raw_body.clone().unwrap_or_default();
9026        let body = with_slot(&held, slot)?;
9027        if body != held {
9028            self.update_content(item.content_kind, &item.id, json!({"body": body}))
9029                .await?;
9030        }
9031        let (visible, slot) = self.visible(Some(body.clone()))?;
9032        item.body = visible.filter(|value| !value.is_empty());
9033        item.raw_body = Some(body);
9034        item.slot = slot;
9035        Ok(())
9036    }
9037
9038    /// This instance's target for a category written to an item of `kind`, refusing one
9039    /// that kind has no option for — before anything is read or written.
9040    ///
9041    /// Nothing here mutates the board's option set to make room for a status. GitHub
9042    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
9043    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
9044    /// field and every item's status.
9045    fn resolved_target(
9046        &self,
9047        kind: ItemKind,
9048        category: StatusCategory,
9049    ) -> Result<StatusTarget, SourceError> {
9050        let target = self.statuses.target(kind, category).clone();
9051        let StatusTarget::Disabled(why) = target else {
9052            return Ok(target);
9053        };
9054        let refusal = why.refusal(&self.name, category, kind);
9055        // Why there is no shipped default, which is the question a person meeting this
9056        // refusal on a source that never mentioned the category asks.
9057        let shipped_none = match category {
9058            StatusCategory::Draft => Some(
9059                "draft has no shipped default because GitHub draft issues cannot have \
9060                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
9061            ),
9062            StatusCategory::Unknown => Some(
9063                "unknown has no shipped default because this board keeps no open-ended status \
9064                 word: every word classified unknown is written to the one board Status option \
9065                 status_mapping.unknown names",
9066            ),
9067            _ => None,
9068        };
9069        Err(match (refusal, shipped_none, why) {
9070            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
9071                SourceError::Refused {
9072                    message: format!("{message}; {note}"),
9073                }
9074            }
9075            (refusal, _, _) => refusal,
9076        })
9077    }
9078
9079    /// What writing `priority` does to one item's `Priority` field on this board, or the
9080    /// refusal naming what the board lacks.
9081    ///
9082    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
9083    /// none already, or of an item not created yet. Every other priority selects the option
9084    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
9085    /// without that option, is refused rather than given one: reads and writes never create
9086    /// a field or an option.
9087    fn priority_write(
9088        &self,
9089        fields: &Value,
9090        existing: Option<&Resolved>,
9091        priority: Priority,
9092    ) -> Result<Option<PriorityWrite>, SourceError> {
9093        let Some(mapping) = &self.priorities else {
9094            return Err(self.holds_no_priority());
9095        };
9096        let Some(wanted) = mapping.option(priority) else {
9097            if !existing.is_some_and(Resolved::holds_priority) {
9098                return Ok(None);
9099            }
9100            let field =
9101                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
9102                    message: format!(
9103                        "an item holding a {PRIORITY_FIELD} value was read without that field"
9104                    ),
9105                })?;
9106            return Ok(Some(PriorityWrite::Clear {
9107                field: required_str(field, "id")?.to_owned(),
9108            }));
9109        };
9110        let missing = |detail: &str| SourceError::Refused {
9111            message: format!(
9112                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
9113                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
9114                 it, or point priority_mapping.{priority} of this source at an option the board \
9115                 has",
9116                self.name, self.name
9117            ),
9118        };
9119        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
9120            return Err(missing(&format!(
9121                "this board has no {PRIORITY_FIELD} field"
9122            )));
9123        };
9124        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
9125            return Err(missing(&format!(
9126                "this board's {PRIORITY_FIELD} field is not a single-select field"
9127            )));
9128        }
9129        // An options list that is absent or not a list is an answer this source cannot read,
9130        // not a board lacking the option: `sources fields --apply` is no remedy for it.
9131        let option = field
9132            .get("options")
9133            .and_then(Value::as_array)
9134            .ok_or_else(|| SourceError::Malformed {
9135                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
9136            })?
9137            .iter()
9138            .find(|option| {
9139                option
9140                    .get("name")
9141                    .and_then(Value::as_str)
9142                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
9143            })
9144            .ok_or_else(|| missing("this board does not have it"))?;
9145        Ok(Some(PriorityWrite::Select {
9146            field: required_str(field, "id")?.to_owned(),
9147            option: required_str(option, "id")?.to_owned(),
9148        }))
9149    }
9150
9151    /// What writing `metadata` does to each projected text field of one item, and what those
9152    /// fields hold after it — or the refusal, before anything is sent.
9153    ///
9154    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
9155    /// narrows the work to the entries projecting one of those metadata keys, for a write that
9156    /// touches only them; `None` is every entry. A string the field already holds, and nothing
9157    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
9158    /// and so is a board without the field, or with a field of that name that is not a text
9159    /// field: reads and writes never create a field.
9160    fn projection_writes(
9161        &self,
9162        fields: &Value,
9163        held: Option<&BTreeMap<String, String>>,
9164        metadata: &BTreeMap<String, Value>,
9165        keys: Option<&[&str]>,
9166    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
9167        let mut projected = held.cloned().unwrap_or_default();
9168        let mut writes = Vec::new();
9169        for projection in self
9170            .metadata_fields
9171            .iter()
9172            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
9173        {
9174            let wanted = projection.projected(metadata, &self.name)?;
9175            let field = self.projection_field(fields, projection)?;
9176            let holding = projected.get(&*projection.field);
9177            match wanted {
9178                Projected::Text(text) if holding != Some(&text) => {
9179                    writes.push(ProjectionWrite::Set {
9180                        field,
9181                        text: text.clone(),
9182                    });
9183                    projected.insert(projection.field.to_string(), text);
9184                }
9185                Projected::Nothing if holding.is_some() => {
9186                    writes.push(ProjectionWrite::Clear { field });
9187                    projected.remove(&*projection.field);
9188                }
9189                Projected::Text(_) | Projected::Nothing => {}
9190            }
9191        }
9192        Ok((writes, projected))
9193    }
9194
9195    /// The id of the board text field one projection writes, or the refusal naming what the
9196    /// board has instead.
9197    fn projection_field(
9198        &self,
9199        fields: &Value,
9200        projection: &MetadataField,
9201    ) -> Result<String, SourceError> {
9202        let refuse = |detail: String| SourceError::Refused {
9203            message: format!(
9204                "source {} projects metadata key {:?} onto the board text field {:?}, and \
9205                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
9206                 text field, or name another field in metadata_fields",
9207                self.name, &*projection.key, &*projection.field, self.name
9208            ),
9209        };
9210        let Some(field) = Board::field(fields, &projection.field)? else {
9211            return Err(refuse("this board has no field of that name".to_owned()));
9212        };
9213        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
9214        let data_type = optional_str(field, "dataType")?;
9215        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
9216            return Err(refuse(format!(
9217                "this board's field of that name is not a text field (it is a {}), so it is \
9218                 left as it is; rename or remove that field first",
9219                data_type.unwrap_or(typename)
9220            )));
9221        }
9222        Ok(required_str(field, "id")?.to_owned())
9223    }
9224
9225    /// Apply one priority write to one board item.
9226    async fn write_priority(
9227        &self,
9228        board_id: &str,
9229        item_id: &str,
9230        write: &PriorityWrite,
9231    ) -> Result<(), SourceError> {
9232        match write {
9233            PriorityWrite::Select { field, option } => {
9234                self.set_item_field(
9235                    board_id,
9236                    item_id,
9237                    field,
9238                    json!({"singleSelectOptionId": option}),
9239                )
9240                .await
9241            }
9242            PriorityWrite::Clear { field } => {
9243                let data = self
9244                    .graphql(
9245                        graphql::CLEAR_FIELD,
9246                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
9247                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
9248                    )
9249                    .await?;
9250                let returned = data
9251                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
9252                    .ok_or_else(|| SourceError::Malformed {
9253                        message: "GitHub field clear returned no project item".into(),
9254                    })?;
9255                if required_str(returned, "id")? != item_id {
9256                    return Err(SourceError::Malformed {
9257                        message: "GitHub field clear returned the wrong project item".into(),
9258                    });
9259                }
9260                Ok(())
9261            }
9262        }
9263    }
9264
9265    /// The refusal a priority is answered with by an instance configured with no
9266    /// `priority_mapping`, which holds none.
9267    fn holds_no_priority(&self) -> SourceError {
9268        SourceError::Refused {
9269            message: format!(
9270                "source {} holds no task priority: its configuration sets no priority_mapping; \
9271                 next: set priority_mapping on this source, then run `onetaskgraph sources \
9272                 fields {} --apply` to set its board up",
9273                self.name, self.name
9274            ),
9275        }
9276    }
9277
9278    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
9279    ///
9280    /// One field write — a select, or a clear for `none` — and no title, body, label, state
9281    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
9282    async fn set_priority(
9283        &self,
9284        id: &NativeId,
9285        priority: Priority,
9286    ) -> Result<Option<Priority>, SourceError> {
9287        if self.priorities.is_none() {
9288            return Err(self.holds_no_priority());
9289        }
9290        let Some(mut item) = self
9291            .bound_item(id)
9292            .await?
9293            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
9294        else {
9295            return Ok(None);
9296        };
9297        if priority == Priority::None && !item.holds_priority() {
9298            return Ok(Some(priority));
9299        }
9300        // The item's own read carries the field's definition whenever it holds a value of
9301        // it, which a clear always does; a select onto an item holding none reads the board.
9302        let board = match (item.carried_board(), item.named_board()) {
9303            (Some(board), _) => board,
9304            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
9305                id,
9306                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
9307            },
9308            _ => self.board_fields().await?,
9309        };
9310        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
9311            return Ok(Some(priority));
9312        };
9313        let (document, root, input) = match write {
9314            PriorityWrite::Select { field, option } => (
9315                graphql::UPDATE_FIELD,
9316                "updateProjectV2ItemFieldValue",
9317                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
9318            ),
9319            PriorityWrite::Clear { field } => (
9320                graphql::CLEAR_FIELD,
9321                "clearProjectV2ItemFieldValue",
9322                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
9323            ),
9324        };
9325        let data = self
9326            .graphql(
9327                document,
9328                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
9329            )
9330            .await?;
9331        let returned = data
9332            .get(root)
9333            .and_then(|value| value.get("projectV2Item"))
9334            .ok_or_else(|| SourceError::Malformed {
9335                message: "GitHub priority write returned no project item".into(),
9336            })?;
9337        if required_str(returned, "id")? != item.item_id {
9338            return Err(SourceError::Malformed {
9339                message: "GitHub priority write returned the wrong project item".into(),
9340            });
9341        }
9342        let value = returned
9343            .get("fieldValueByName")
9344            .ok_or_else(|| SourceError::Malformed {
9345                message: "GitHub priority write returned no priority read-back".into(),
9346            })?;
9347        if !value.is_null()
9348            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
9349        {
9350            return Err(SourceError::Malformed {
9351                message: "GitHub priority read-back is not a Priority field value".into(),
9352            });
9353        }
9354        let values = if value.is_null() {
9355            Vec::new()
9356        } else {
9357            vec![value.clone()]
9358        };
9359        item.priority = self.held_priority(&values)?;
9360        let answer = item.task()?.priority;
9361        self.remember_written(item, false)?;
9362        Ok(Some(answer))
9363    }
9364
9365    /// Replace one task's visible body and nothing else; see
9366    /// [`TaskSource::set_task_content`].
9367    ///
9368    /// One update of the body, which differs from the body GitHub holds only outside the
9369    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
9370    /// this source keeps there reads back as it was. A body that would not change is not
9371    /// sent at all.
9372    async fn replace_content(
9373        &self,
9374        id: &NativeId,
9375        content: &str,
9376    ) -> Result<Option<()>, SourceError> {
9377        let Some(mut item) = self
9378            .bound_item(id)
9379            .await?
9380            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
9381        else {
9382            return Ok(None);
9383        };
9384        let held = item.raw_body.clone().unwrap_or_default();
9385        // The task's `## Links` section, re-rendered from the links it holds, stays with it.
9386        let content = &onetaskgraph_plugin_api::relinked(content, item.body.as_deref(), &item.slot);
9387        let body = with_content(&held, &self.marked(content))?;
9388        // Checked before anything is sent: content ending in what this source reads as its own
9389        // metadata slot would read back as metadata rather than as the content it was.
9390        let (visible, slot) = self.visible(Some(body.clone()))?;
9391        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
9392            return Err(SourceError::Refused {
9393                message: format!(
9394                    "this content ends in what source {} reads as its own metadata slot \
9395                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
9396                     as content; next: remove that trailing block from the content",
9397                    self.name
9398                ),
9399            });
9400        }
9401        if body != held {
9402            self.update_content(item.content_kind, &item.id, json!({"body": body}))
9403                .await?;
9404        }
9405        item.body = visible.filter(|value| !value.is_empty());
9406        item.raw_body = Some(body);
9407        item.slot = slot;
9408        self.remember_written(item, false)?;
9409        Ok(Some(()))
9410    }
9411
9412    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
9413    ///
9414    /// One read of the item — which carries the board's field definitions and the issue's
9415    /// `blockedBy`, so neither is read again — and then only what differs from it: the
9416    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
9417    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
9418    /// the title, the body — visible content and metadata slot together — and a state change.
9419    /// So an update naming any of title, body, metadata, status and priority is one read and
9420    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
9421    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
9422    /// as a whole write does; an open one selects its option and then reopens. The origin
9423    /// field is never written: an update is of an item that already exists, whose origin is
9424    /// what it is.
9425    ///
9426    /// The task answered is the item as those writes left it, built from the read and what was
9427    /// sent rather than read again — the same record a later read in this run answers from.
9428    ///
9429    /// Its `version` is the one the task holds after the writes: the change token the last
9430    /// mutation's own response spells — each sent as the twin selecting the whole issue, at
9431    /// the price of the plain document — or, where that mutation shows no whole issue (several
9432    /// board fields written together, or `blockedBy` alone), the token one read right after
9433    /// the writes spells. Never the token the item was read at before them. Against GitHub
9434    /// itself that token has not settled yet (`change_token::SETTLED`), so a read gives none
9435    /// until it has, and a write held to it before then is refused rather than let through;
9436    /// once settled, a read gives exactly this token unless the task changed again.
9437    async fn targeted_update(
9438        &self,
9439        id: &NativeId,
9440        update: &TaskUpdate,
9441    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
9442        Box::pin(SPELLING.scope(
9443            std::ptr::from_ref(self) as usize,
9444            self.composed_update(id, update),
9445        ))
9446        .await
9447    }
9448
9449    /// [`Self::targeted_update`], inside the scope that has its mutations answer whole.
9450    async fn composed_update(
9451        &self,
9452        id: &NativeId,
9453        update: &TaskUpdate,
9454    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
9455        // Everything this source can refuse without reading the item is refused first, in the
9456        // words a whole write of the same fields is refused with.
9457        update.consistent()?;
9458        if update.assignees.is_some() && !self.assignees {
9459            return Err(onetaskgraph_plugin_api::unwritable_field(KIND, "assignees"));
9460        }
9461        if update
9462            .title
9463            .as_deref()
9464            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
9465        {
9466            return Err(SourceError::Refused {
9467                message: format!(
9468                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9469                     spells a document, so it would read back as one rather than as a task; \
9470                     retitle it",
9471                    self.name
9472                ),
9473            });
9474        }
9475        if let Some(delivers) = &update.delivers {
9476            TaskRef::listed(
9477                TaskRef::DELIVERS_KEY,
9478                id,
9479                Some(&self.name),
9480                delivers.clone(),
9481            )
9482            .map_err(|message| SourceError::Refused { message })?;
9483        }
9484        if self.priorities.is_none()
9485            && update
9486                .priority
9487                .is_some_and(|priority| priority != Priority::None)
9488        {
9489            return Err(self.holds_no_priority());
9490        }
9491        let target = update
9492            .status
9493            .as_ref()
9494            .map(|status| self.resolved_target(ItemKind::Task, status.category))
9495            .transpose()?;
9496        let Some(mut item) = self
9497            .bound_item(id)
9498            .await?
9499            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
9500        else {
9501            return Ok(None);
9502        };
9503        let before = item.task()?;
9504        // The people it is to be assigned to, by node id, when they are not exactly whom it
9505        // holds: looked up — unless this process already has — and refused for a login GitHub
9506        // does not know, before anything is sent.
9507        let assignee_ids = match &update.assignees {
9508            Some(people)
9509                if item
9510                    .assignees
9511                    .as_deref()
9512                    .is_none_or(|held| !assignees::same_people(held, people)) =>
9513            {
9514                if item.content_kind == ContentKind::DraftIssue {
9515                    return Err(self.assigns_a_draft());
9516                }
9517                self.resolve_people(people).await?;
9518                Some(self.people_ids(people, &item.written_name())?)
9519            }
9520            _ => None,
9521        };
9522
9523        let mut status_move = None;
9524        if let (Some(status), Some(target)) = (&update.status, target) {
9525            let board = self.status_board(&item).await?;
9526            let (field, option, name) = self
9527                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
9528                .ok_or_else(|| SourceError::Malformed {
9529                    message: format!(
9530                        "status {} of source {} names no board Status option",
9531                        category_name(status.category),
9532                        self.name
9533                    ),
9534                })?;
9535            let terminal = matches!(target, StatusTarget::Terminal(_, _));
9536            if terminal && item.content_kind == ContentKind::DraftIssue {
9537                return Err(self.closes_a_draft(status.category));
9538            }
9539            let landed = match &target {
9540                StatusTarget::Terminal(_, reason) => {
9541                    self.statuses
9542                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
9543                }
9544                _ => self
9545                    .statuses
9546                    .status(ItemKind::Task, Some(&name), false, None),
9547            };
9548            let option_moves = item
9549                .option
9550                .as_deref()
9551                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
9552            let state_moves = item.content_kind == ContentKind::Issue
9553                && (item.closed != terminal || (terminal && item.status != landed));
9554            if let Some(moves) = Moves::of(option_moves, state_moves) {
9555                status_move = Some(StatusMove {
9556                    board: board.id,
9557                    field,
9558                    option,
9559                    name,
9560                    target,
9561                    landed,
9562                    moves,
9563                });
9564            }
9565        }
9566
9567        let mut priority_move = None;
9568        if let Some(priority) = update.priority
9569            && self.priorities.is_some()
9570            && item.priority != HeldPriority::Read(priority)
9571        {
9572            let board = match (item.carried_board(), item.named_board()) {
9573                (Some(board), _) => board,
9574                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
9575                    id: board,
9576                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
9577                },
9578                _ => self.board_fields().await?,
9579            };
9580            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
9581                priority_move = Some((board.id, write, priority));
9582            }
9583        }
9584
9585        // The metadata keys this update names that a board text field projects, and what
9586        // that does to those fields — refused, as a whole write refuses it, before anything
9587        // is sent.
9588        let touched: Vec<&str> = update
9589            .metadata_set
9590            .keys()
9591            .chain(update.metadata_remove.iter())
9592            .map(MetadataKey::as_str)
9593            .collect();
9594        let mut projection_move = None;
9595        if self
9596            .metadata_fields
9597            .iter()
9598            .any(|projection| touched.contains(&&*projection.key))
9599        {
9600            let mut metadata = item.slot.clone();
9601            for (key, value) in &update.metadata_set {
9602                metadata.insert(key.as_str().to_owned(), value.clone());
9603            }
9604            for key in &update.metadata_remove {
9605                metadata.remove(key.as_str());
9606            }
9607            let board = self.projection_board(&item).await?;
9608            let (writes, projected) = self.projection_writes(
9609                &board.fields,
9610                Some(&item.projected),
9611                &metadata,
9612                Some(&touched),
9613            )?;
9614            projection_move = Some((board.id, writes, projected));
9615        }
9616
9617        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
9618        // recorded in the slot, and the slot travels in the one body update below.
9619        let edges = match &update.depends_on {
9620            Some(edges) => Some(
9621                self.partition_edges(
9622                    BoardKind::Work(ItemKind::Task),
9623                    item.content_kind,
9624                    item.blocked_by.as_deref(),
9625                    edges,
9626                )
9627                .await?,
9628            ),
9629            None => None,
9630        };
9631
9632        // A record primed from what a producer wrote holds no body anybody read, so an update
9633        // changing the body reads it first: by the `Status` write's own answer when the update
9634        // moves the option — the mutation answering with the issue whole — and otherwise by
9635        // reading the issue. The body is then composed against what GitHub holds now.
9636        let mut option_selected = false;
9637        // The mark stays until a read lands, so an update that writes no body, or one whose
9638        // read fails, leaves the next update of it to read the body too.
9639        if (update.content.is_some()
9640            || !update.metadata_set.is_empty()
9641            || !update.metadata_remove.is_empty()
9642            || update.delivers.is_some()
9643            || update.repositories.is_some()
9644            || edges.is_some())
9645            && self.is_unread(id)
9646        {
9647            let live = match status_move.as_ref().filter(|moving| moving.moves.option()) {
9648                Some(moving) => {
9649                    option_selected = true;
9650                    Some(
9651                        self.select_reading_body(
9652                            moving.board.as_str(),
9653                            &item.item_id,
9654                            &moving.field,
9655                            &moving.option,
9656                        )
9657                        .await?,
9658                    )
9659                }
9660                None => {
9661                    self.forget(id)?;
9662                    let Some(read) = self.item_by_id(id).await? else {
9663                        return Ok(None);
9664                    };
9665                    read.raw_body
9666                }
9667            };
9668            let (visible, read_slot) = self.visible(live.clone())?;
9669            self.mark_read(id);
9670            item.raw_body = live;
9671            item.body = visible;
9672            item.slot = read_slot;
9673        }
9674
9675        let mut slot = item.slot.clone();
9676        for (key, value) in &update.metadata_set {
9677            slot.insert(key.as_str().to_owned(), value.clone());
9678        }
9679        for key in &update.metadata_remove {
9680            slot.remove(key.as_str());
9681        }
9682        if let Some(delivers) = &update.delivers {
9683            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
9684        }
9685        // The list a whole write records, by the same rule: none when it is exactly the issue's
9686        // own repository, or empty with no issue repository to derive, and otherwise the list.
9687        if let Some(repositories) = &update.repositories {
9688            let derivable = item
9689                .own_repository
9690                .as_ref()
9691                .map_or(repositories.is_empty(), |own| {
9692                    repositories.as_slice() == std::slice::from_ref(own)
9693                });
9694            if derivable {
9695                slot.remove(Repository::METADATA_KEY);
9696            } else {
9697                slot.insert(
9698                    Repository::METADATA_KEY.to_owned(),
9699                    Value::Array(
9700                        repositories
9701                            .iter()
9702                            .map(|repository| Value::String(repository.as_str().to_owned()))
9703                            .collect(),
9704                    ),
9705                );
9706            }
9707        }
9708        if let Some((_, recorded)) = &edges {
9709            record_edges(&mut slot, recorded);
9710        }
9711        let held = item.raw_body.clone().unwrap_or_default();
9712        // A content write keeps the task's `## Links` section, re-rendered from the links the
9713        // slot holds once written.
9714        let relinked = update
9715            .content
9716            .as_deref()
9717            .map(|content| onetaskgraph_plugin_api::relinked(content, item.body.as_deref(), &slot));
9718        let content = match &relinked {
9719            Some(content) => with_content(&held, &self.marked(content))?,
9720            None => held.clone(),
9721        };
9722        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
9723        // the body's bytes, as a metadata write compares it: a slot a person spelled with
9724        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
9725        let body = if slot == item.slot {
9726            content
9727        } else {
9728            with_slot(&content, &slot)?
9729        };
9730        // Checked before anything is sent, as a content write checks it: content ending in
9731        // what this source reads as its own slot would read back as metadata.
9732        let (visible, read) = self.visible(Some(body.clone()))?;
9733        let wanted = relinked.as_deref().or(item.body.as_deref());
9734        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
9735            return Err(SourceError::Refused {
9736                message: format!(
9737                    "this content ends in what source {} reads as its own metadata slot \
9738                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
9739                     as content; next: remove that trailing block from the content",
9740                    self.name
9741                ),
9742            });
9743        }
9744        let recorded_moves =
9745            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
9746
9747        // One `updateIssue` carries all three, because every mutation spends the secondary
9748        // limiter and the title, body and state are one mutation's inputs.
9749        let mut fields = serde_json::Map::new();
9750        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
9751            fields.insert("title".to_owned(), json!(title));
9752        }
9753        if body != held {
9754            fields.insert("body".to_owned(), json!(body));
9755        }
9756        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
9757            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
9758        }
9759        // The assignees ride the same mutation: replacing whoever the issue holds, in the one
9760        // `updateIssue` this update already ends with — or as the one it sends for them alone.
9761        if let Some(ids) = &assignee_ids {
9762            fields.insert("assigneeIds".to_owned(), json!(ids));
9763        }
9764        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
9765        // GitHub runs no two requests as one, and runs one document's mutation fields in order
9766        // without undoing an earlier field when a later one fails — so a body written before a
9767        // board field the board then refused would be left changed. Written after every other
9768        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
9769        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
9770        // first, together in one request — a terminal option selected before the issue
9771        // closes, as a whole write does — then the `blockedBy` difference, then the body.
9772        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
9773        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
9774        if let Some(moving) = status_move
9775            .as_ref()
9776            .filter(|moving| moving.moves.option() && !option_selected)
9777        {
9778            board_writes.push((
9779                &moving.board,
9780                (
9781                    moving.field.clone(),
9782                    json!({"singleSelectOptionId": moving.option}),
9783                ),
9784            ));
9785        }
9786        match &priority_move {
9787            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
9788                board,
9789                (field.clone(), json!({"singleSelectOptionId": option})),
9790            )),
9791            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
9792            None => {}
9793        }
9794        if let Some((board, writes, _)) = &projection_move {
9795            for write in writes {
9796                let (mut values, mut cleared) = (Vec::new(), Vec::new());
9797                write.join(&mut values, &mut cleared);
9798                board_writes.extend(values.into_iter().map(|value| (board, value)));
9799                clears.extend(cleared.into_iter().map(|field| (board, field)));
9800            }
9801        }
9802        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
9803        boards.extend(clears.iter().map(|(board, _)| *board));
9804        boards.dedup_by(|one, other| one.as_str() == other.as_str());
9805        for board in boards {
9806            let writes = board_writes
9807                .iter()
9808                .filter(|(on, _)| on.as_str() == board.as_str())
9809                .map(|(_, write)| write.clone())
9810                .collect::<Vec<_>>();
9811            let cleared = clears
9812                .iter()
9813                .filter(|(on, _)| on.as_str() == board.as_str())
9814                .map(|(_, field)| *field)
9815                .collect::<Vec<_>>();
9816            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
9817                .await?;
9818        }
9819        // Whether anything is sent: what the version answered has to come after.
9820        let sends =
9821            option_selected || !board_writes.is_empty() || !clears.is_empty() || !fields.is_empty();
9822        let mut blocked_by_moved = false;
9823        if let Some((native, _)) = &edges
9824            && item.content_kind == ContentKind::Issue
9825        {
9826            blocked_by_moved = self
9827                .reconcile_blocked_by(
9828                    &item.id,
9829                    native,
9830                    Issue::Existing(item.blocked_by.as_deref()),
9831                )
9832                .await?;
9833        }
9834        if !fields.is_empty() {
9835            self.update_content(item.content_kind, &item.id, Value::Object(fields))
9836                .await?;
9837        }
9838
9839        // The `blockedBy` this item was read with is what the write just changed, so it is no
9840        // answer any more: a later write in this command would reconcile against it — re-adding
9841        // what it kept and never removing what it dropped — and a forward-edge read would
9842        // answer with it. Left unknown, each asks GitHub instead.
9843        if blocked_by_moved {
9844            item.blocked_by = None;
9845        }
9846        if let Some(title) = &update.title {
9847            item.title.clone_from(title);
9848        }
9849        item.body = visible.filter(|value| !value.is_empty());
9850        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
9851        item.slot = slot;
9852        if let Some(delivers) = &update.delivers {
9853            item.delivers.clone_from(delivers);
9854        }
9855        if let Some(repositories) = &update.repositories {
9856            item.repositories.clone_from(repositories);
9857        }
9858        if let Some(moving) = status_move {
9859            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
9860                && item.content_kind == ContentKind::Issue;
9861            item.status = moving.landed;
9862            item.option = Some(moving.name);
9863        }
9864        if let Some((_, _, priority)) = priority_move {
9865            item.priority = HeldPriority::Read(priority);
9866        }
9867        if let Some((_, _, projected)) = projection_move {
9868            item.projected = projected;
9869        }
9870        // Only a write that assigned anyone moves whom the item holds, and they are who GitHub
9871        // says they are; one naming the people it already holds leaves its read as it was.
9872        if let (Some(people), Some(_)) = (&update.assignees, &assignee_ids) {
9873            item.assignees = Some(self.spelled(people));
9874        }
9875        let mut task = item.task()?;
9876        let mut written = update.changed(&before, &task);
9877        if blocked_by_moved || recorded_moves {
9878            written.insert(UpdatedField::DependsOn);
9879        }
9880        if sends || blocked_by_moved {
9881            // The token the item was read at is not the one it holds now. A batch of updates
9882            // holding this one's writes back reads every task it wrote once they are sent.
9883            task.version = if self.holding_writes_of(id) {
9884                None
9885            } else {
9886                self.version_after_write(id).await
9887            };
9888        }
9889        self.remember_written(item, false)?;
9890        Ok(Some(TaskUpdateOutcome {
9891            task,
9892            written,
9893            delivers_before: before.delivers,
9894        }))
9895    }
9896
9897    /// The change token the task `id` holds after the writes a targeted update just sent: the
9898    /// one the last mutation's own response showed, or, where none showed the whole issue, the
9899    /// one a read made now spells. `None` when that read cannot say — never the token the
9900    /// task was read at before the writes.
9901    async fn version_after_write(&self, id: &NativeId) -> Option<String> {
9902        // A response that showed the issue whole answers, a token or none: a read made now
9903        // would read the same parts.
9904        if let Some(answered) = self.written_answer(id) {
9905            return answered;
9906        }
9907        // An item this command created is answered from its own record, not read.
9908        if self.created().ok()?.iter().any(|own| own.id == *id) {
9909            return None;
9910        }
9911        let read = self.item_by_id(id).await.ok()??;
9912        let token = read.written_version()?;
9913        self.remember_written_token(&read, token.clone());
9914        Some(token)
9915    }
9916
9917    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
9918    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
9919    ///
9920    /// One update of the body: the content outside the slot, and inside it that one entry,
9921    /// every other entry kept as it was. This source keeps no template answers — an issue has
9922    /// no room beside itself that is not its body, and answers written there would duplicate
9923    /// what the content already says and count against GitHub's body limit — so `answers`
9924    /// reaches nothing here. A body that would not change is not sent at all.
9925    async fn replace_rendering(
9926        &self,
9927        id: &NativeId,
9928        kind: BoardKind,
9929        content: &str,
9930        provenance: &Value,
9931        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
9932    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
9933        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
9934            return Ok(None);
9935        };
9936        let held = item.raw_body.clone().unwrap_or_default();
9937        let mut slot = item.slot.clone();
9938        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
9939        let rewritten;
9940        let content = if let Some(assets) = assets {
9941            let uploads = self
9942                .upload_assets(item.own_repository.as_ref(), assets)
9943                .await?;
9944            rewritten =
9945                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
9946            rewritten.as_str()
9947        } else {
9948            content
9949        };
9950        let relinked = onetaskgraph_plugin_api::relinked(content, item.body.as_deref(), &slot);
9951        let content = relinked.as_str();
9952        let body = with_slot(&with_content(&held, &self.marked(content))?, &slot)?;
9953        // Checked before anything is sent, as a content write checks it.
9954        let (visible, read) = self.visible(Some(body.clone()))?;
9955        if visible.as_deref().unwrap_or_default() != content || read != slot {
9956            return Err(SourceError::Refused {
9957                message: format!(
9958                    "this content ends in what source {} reads as its own metadata slot \
9959                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
9960                     as content; next: remove that trailing block from the template",
9961                    self.name
9962                ),
9963            });
9964        }
9965        if body != held {
9966            self.update_content(item.content_kind, &item.id, json!({"body": body}))
9967                .await?;
9968        }
9969        item.body = visible.filter(|value| !value.is_empty());
9970        item.raw_body = Some(body);
9971        // The content as a read answers it: without the `## Links` section of a linked task.
9972        let answered = onetaskgraph_plugin_api::read_content(Some(content.to_owned()), &read);
9973        item.slot = read;
9974        self.remember_written(item, false)?;
9975        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
9976            id: id.clone(),
9977            content: answered,
9978        }))
9979    }
9980
9981    async fn set_item_field(
9982        &self,
9983        board_id: &str,
9984        item_id: &str,
9985        field_id: &str,
9986        value: Value,
9987    ) -> Result<(), SourceError> {
9988        let data = self
9989            .graphql(
9990                graphql::UPDATE_FIELD,
9991                json!({"input":{
9992                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
9993                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
9994            )
9995            .await?;
9996        field_update_item(&data, item_id).map(|_| ())
9997    }
9998
9999    /// GitHub accepts one value per field mutation; aliases combine those mutations in
10000    /// one request — writes in the order given, then clears. A lone write is
10001    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
10002    /// request's slots hold go in further requests of the same document, in order. Every
10003    /// returned item id is checked, including optional aliases.
10004    async fn set_item_fields(
10005        &self,
10006        board: &str,
10007        item: &str,
10008        fields: &[(String, Value)],
10009        clears: &[&str],
10010    ) -> Result<(), SourceError> {
10011        if !(fields.is_empty() && clears.is_empty())
10012            && self.hold_fields(board, item, fields, clears)?
10013        {
10014            return Ok(());
10015        }
10016        match (fields, clears) {
10017            ([], []) => return Ok(()),
10018            ([(field, value)], []) => {
10019                return self.set_item_field(board, item, field, value.clone()).await;
10020            }
10021            ([], [field]) => {
10022                return self
10023                    .write_priority(
10024                        board,
10025                        item,
10026                        &PriorityWrite::Clear {
10027                            field: (*field).to_owned(),
10028                        },
10029                    )
10030                    .await;
10031            }
10032            _ => {}
10033        }
10034        // Any field of the request stands in for an input a slot leaves out, which GitHub
10035        // still requires to be well formed although it never runs it.
10036        let filler = fields
10037            .first()
10038            .map_or_else(|| clears[0], |(field, _)| field.as_str());
10039        let mut writes = fields.iter();
10040        let mut cleared = clears.iter();
10041        loop {
10042            let batch: Vec<&(String, Value)> =
10043                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
10044            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
10045            if batch.is_empty() && batch_clears.is_empty() {
10046                return Ok(());
10047            }
10048            let mut variables = serde_json::Map::new();
10049            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
10050                let input = match batch.get(index) {
10051                    Some((field, value)) => {
10052                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
10053                    }
10054                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
10055                                   "value":{"text":""}}),
10056                };
10057                variables.insert(slot.variable.to_owned(), input);
10058                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
10059            }
10060            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
10061                let field = batch_clears.get(index).map_or(filler, |field| **field);
10062                variables.insert(
10063                    slot.variable.to_owned(),
10064                    json!({"projectId":board,"itemId":item,"fieldId":field}),
10065                );
10066                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
10067            }
10068            let data = self
10069                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
10070                .await?;
10071            let sent = FIELD_WRITE_SLOTS[..batch.len()]
10072                .iter()
10073                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
10074            for slot in sent {
10075                let alias = slot.alias;
10076                let returned = data
10077                    .get(alias)
10078                    .and_then(|value| value.get("projectV2Item"))
10079                    .ok_or_else(|| SourceError::Malformed {
10080                        message: format!("GitHub field update {alias} returned no project item"),
10081                    })?;
10082                if required_str(returned, "id")? != item {
10083                    return Err(SourceError::Malformed {
10084                        message: format!(
10085                            "GitHub field update {alias} returned the wrong project item"
10086                        ),
10087                    });
10088                }
10089            }
10090        }
10091    }
10092
10093    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
10094        let mut after: Option<String> = None;
10095        let mut ids = Vec::new();
10096        loop {
10097            let data = self
10098                .graphql(
10099                    graphql::ISSUE_DEPENDENCIES,
10100                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
10101                )
10102                .await?;
10103            let connection =
10104                data.pointer("/node/blockedBy")
10105                    .ok_or_else(|| SourceError::Malformed {
10106                        message: "GitHub dependency response has no blockedBy connection".into(),
10107                    })?;
10108            ids.extend(
10109                connection
10110                    .get("nodes")
10111                    .and_then(Value::as_array)
10112                    .ok_or_else(|| SourceError::Malformed {
10113                        message: "GitHub dependency response nodes is not an array".into(),
10114                    })?
10115                    .iter()
10116                    .map(|value| required_str(value, "id").map(str::to_owned))
10117                    .collect::<Result<Vec<_>, _>>()?,
10118            );
10119            let next = next_cursor(connection)?;
10120            if let Some(next) = &next {
10121                validate_cursor_progress(after.as_deref(), &next.0)?;
10122            }
10123            after = next.map(|cursor| cursor.0);
10124            if after.is_none() {
10125                return Ok(ids);
10126            }
10127        }
10128    }
10129
10130    async fn dependencies(
10131        &self,
10132        id: &NativeId,
10133        near_kind: ItemKind,
10134        direction: Direction,
10135        page: &PageRequest,
10136    ) -> Result<Page<DependencyEdge>, SourceError> {
10137        validate_page(page)?;
10138        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
10139        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
10140        let recorded = recorded_offset(cursor, direction)?;
10141        // What this issue is blocked by, when a read of it by its own id in this command
10142        // already carried the whole connection — a copy reads the item it writes before it
10143        // reads its edges — and the page asked for is the whole of it, or the recorded tail
10144        // after it. Answered from that read, in the shape the dependency read answers in;
10145        // anything else is asked of GitHub.
10146        let carried = match direction {
10147            Direction::DependsOn => self
10148                .resolved_cache()?
10149                .get(id)
10150                .filter(|item| item.content_kind == ContentKind::Issue)
10151                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
10152            Direction::DependedOnBy => None,
10153        }
10154        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
10155        // Asked for even in the recorded phase, whose page reads nothing from the
10156        // connection: `__typename` is what says whether this item has a native
10157        // relationship at all, and that is what decides which far ends the reserved key is
10158        // allowed to hold.
10159        let data = match carried {
10160            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
10161                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
10162            None => {
10163                let after = if recorded.is_some() { None } else { cursor };
10164                let first = page.limit.min(MAX_PAGE_SIZE);
10165                match direction {
10166                    Direction::DependsOn => {
10167                        self.graphql(
10168                            graphql::ISSUE_DEPENDS_ON,
10169                            json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
10170                                   "number":self.project_number,
10171                                   "boardItems":BOARD_ITEMS_PAGE_SIZE}),
10172                        )
10173                        .await?
10174                    }
10175                    Direction::DependedOnBy => {
10176                        self.graphql(
10177                            graphql::ISSUE_DEPENDED_ON_BY,
10178                            json!({"id":id.0,"first":first,"after":after}),
10179                        )
10180                        .await?
10181                    }
10182                }
10183            }
10184        };
10185        let draft_slot = draft_slot(&data, id, self.project_number)?;
10186        let node =
10187            data.get("node")
10188                .filter(|v| !v.is_null())
10189                .ok_or_else(|| SourceError::Refused {
10190                    message: format!(
10191                        "GitHub item {} was not found or does not support dependencies",
10192                        id.0
10193                    ),
10194                })?;
10195        let connection_name = match direction {
10196            Direction::DependsOn => "blockedBy",
10197            Direction::DependedOnBy => "blocking",
10198        };
10199        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
10200        // named natively and the reserved key may hold any far end. An issue's connections
10201        // hold issues, and this source reads them at the near item's own level.
10202        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
10203        if let Some(offset) = recorded {
10204            return Ok(recorded_page(
10205                self.recorded_edges(
10206                    id,
10207                    near_kind,
10208                    direction,
10209                    natively_names,
10210                    node,
10211                    draft_slot.as_ref(),
10212                )
10213                .await?,
10214                offset,
10215                limit,
10216            ));
10217        }
10218        if natively_names.is_none() {
10219            return Ok(recorded_page(
10220                self.recorded_edges(
10221                    id,
10222                    near_kind,
10223                    direction,
10224                    natively_names,
10225                    node,
10226                    draft_slot.as_ref(),
10227                )
10228                .await?,
10229                0,
10230                limit,
10231            ));
10232        }
10233        let connection = node
10234            .get(connection_name)
10235            .ok_or_else(|| SourceError::Malformed {
10236                message: "GitHub dependency response is missing its connection".into(),
10237            })?;
10238        let nodes = connection
10239            .get("nodes")
10240            .and_then(Value::as_array)
10241            .ok_or_else(|| SourceError::Malformed {
10242                message: "GitHub dependency response nodes is not an array".into(),
10243            })?;
10244        // `from` depends on `to`, always. GitHub spells the same relationship from either
10245        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
10246        // it — so the near item is `from` in one direction and `to` in the other.
10247        let items = nodes
10248            .iter()
10249            .map(|value| {
10250                let related = NativeId(required_str(value, "id")?.into());
10251                let related_kind = related_kind(value, self.sub_issues)?;
10252                let (from, to) = match direction {
10253                    Direction::DependsOn => (
10254                        DependencyEndpoint::from_native(id.clone(), near_kind),
10255                        DependencyEndpoint::from_native(related, related_kind),
10256                    ),
10257                    Direction::DependedOnBy => (
10258                        DependencyEndpoint::from_native(related, related_kind),
10259                        DependencyEndpoint::from_native(id.clone(), near_kind),
10260                    ),
10261                };
10262                Ok(DependencyEdge {
10263                    from,
10264                    to,
10265                    kind: DependencyKind::Blocks,
10266                })
10267            })
10268            .collect::<Result<Vec<_>, SourceError>>()?;
10269        let mut next = next_cursor(connection)?;
10270        if let Some(next) = &next {
10271            validate_cursor_progress(cursor, &next.0)?;
10272        }
10273        // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
10274        // body they are recorded in, as far as the page has room — so they cost no request of
10275        // their own — and only what does not fit is resumed by a cursor.
10276        let mut items = items;
10277        if next.is_none() {
10278            let recorded = self
10279                .recorded_edges(
10280                    id,
10281                    near_kind,
10282                    direction,
10283                    natively_names,
10284                    node,
10285                    draft_slot.as_ref(),
10286                )
10287                .await?;
10288            let room = limit.saturating_sub(items.len());
10289            let total = recorded.len();
10290            items.extend(recorded.into_iter().take(room));
10291            if total > room {
10292                next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
10293            }
10294        }
10295        Ok(Page { items, next })
10296    }
10297
10298    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
10299    /// a far end in another source has to live: no GitHub issue relationship can name one.
10300    ///
10301    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
10302    /// source never writes one down.
10303    ///
10304    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
10305    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
10306    /// request beyond the read already made, and reading the board for them would be a
10307    /// walk of every item for one field of one. A draft has no body in that answer, because
10308    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
10309    /// listing of the board, which can be behind on the very item asked about.
10310    async fn recorded_edges(
10311        &self,
10312        id: &NativeId,
10313        near_kind: ItemKind,
10314        direction: Direction,
10315        natively_names: Option<ItemKind>,
10316        node: &Value,
10317        draft_slot: Option<&Option<BTreeMap<String, Value>>>,
10318    ) -> Result<Vec<DependencyEdge>, SourceError> {
10319        if direction != Direction::DependsOn {
10320            return Ok(Vec::new());
10321        }
10322        let slot = match (node.get("body"), draft_slot) {
10323            (Some(body), _) if natively_names.is_some() => {
10324                metadata_body(body.as_str().map(str::to_owned))?.1
10325            }
10326            // A draft the dependency read itself answered: this board's, or none of its.
10327            (_, Some(Some(slot))) => slot.clone(),
10328            (_, Some(None)) => return Ok(Vec::new()),
10329            _ => {
10330                let Some(item) = self.bound_item(id).await? else {
10331                    return Ok(Vec::new());
10332                };
10333                item.slot
10334            }
10335        };
10336        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
10337            .map_err(|message| SourceError::Malformed { message })
10338    }
10339
10340    /// Why `creation_owners` refuses a create filed in `target`, or `None` when it allows it —
10341    /// every create when the list is empty.
10342    fn owner_refusal(&self, target: &RepositoryTarget) -> Option<String> {
10343        if self.creation_owners.is_empty()
10344            || self
10345                .creation_owners
10346                .iter()
10347                .any(|owner| owner.eq_ignore_ascii_case(&target.owner))
10348        {
10349            return None;
10350        }
10351        Some(format!(
10352            "source {} would create the issue in repository {}, owned by {}, and its \
10353             creation_owners allows only repositories of {}",
10354            self.name,
10355            target.slug(),
10356            target.owner,
10357            self.creation_owners.join(", ")
10358        ))
10359    }
10360
10361    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
10362        self.repository
10363            .as_ref()
10364            .ok_or_else(|| SourceError::Refused {
10365                message: format!(
10366                    "source {} has no repository configured, and a GitHub Projects board has no \
10367                 repository of its own to create an issue in; set repository: owner/name on \
10368                 this source",
10369                    self.name
10370                ),
10371            })
10372    }
10373
10374    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
10375    /// states.
10376    ///
10377    /// The fallback is demanded first, whichever arm answers: a write without a configured
10378    /// repository is refused naming the field exactly as it was before the rule existed,
10379    /// so a source that could not write before cannot write now, rather than writing for
10380    /// the one item whose own field happens to decide it.
10381    ///
10382    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
10383    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
10384    /// entry owned by someone other than the owner of the parent issue's repository —
10385    /// GitHub accepts a sub-issue from another repository of the same owner and from no
10386    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
10387    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
10388    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
10389    /// and is visible to the token is checked where its node id is resolved, still before
10390    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
10391    /// looked up in a listing of the board, which can be minutes behind an issue its own
10392    /// `projectItems` already places on it — and that read answers first from this process's
10393    /// own record, so a project created moments ago in this command answers though GitHub
10394    /// has not caught up.
10395    async fn creation_target(
10396        &self,
10397        incoming: &Incoming<'_>,
10398    ) -> Result<RepositoryTarget, SourceError> {
10399        let what = format!(
10400            "{} {:?}",
10401            incoming.written.kind().describes(),
10402            incoming.title
10403        );
10404        let target = self
10405            .creation_rule(incoming.repositories, incoming.parent, &what)
10406            .await?;
10407        // The engine asks `creation_refusal` before anything of a copy is written; this holds
10408        // every other create to the same restriction, before `createIssue`.
10409        match self.owner_refusal(&target) {
10410            Some(message) => Err(SourceError::Refused { message }),
10411            None => Ok(target),
10412        }
10413    }
10414
10415    /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
10416    /// and the project issue it is filed under — with `what` naming the item in a refusal: the
10417    /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
10418    /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
10419    /// differently.
10420    ///
10421    /// On a source declared private, an item's one named repository is read for its
10422    /// visibility first — once per engine hold, see the `visibility` module — and one that
10423    /// does not read private decides nothing: the issue is created in the parent's
10424    /// repository, else the configured one. A read that cannot be answered refuses.
10425    async fn creation_rule(
10426        &self,
10427        repositories: &[Repository],
10428        parent: Option<&NativeId>,
10429        what: &str,
10430    ) -> Result<RepositoryTarget, SourceError> {
10431        let parent = match parent {
10432            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
10433                SourceError::Refused {
10434                    message: format!(
10435                        "GitHub project issue {} was not found on the board of source {}, so {} \
10436                         cannot be filed under it",
10437                        parent.0, self.name, what
10438                    ),
10439                }
10440            })?),
10441            None => None,
10442        };
10443        let parents_repository = parent
10444            .as_ref()
10445            .map(|parent| {
10446                // A draft is on the board and so is found, but it has no repository to
10447                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
10448                // would refuse the task only once `createIssue` had made it.
10449                if parent.content_kind == ContentKind::DraftIssue {
10450                    return Err(SourceError::Refused {
10451                        message: format!(
10452                            "GitHub project item {} on the board of source {} is a draft, \
10453                             which cannot have sub-issues, so {} cannot be filed under it",
10454                            parent.id.0, self.name, what
10455                        ),
10456                    });
10457                }
10458                // An issue's repository is where a sub-issue is placed and whose owner it
10459                // is compared against, so a parent whose repository this source cannot
10460                // spell as `owner/name` — GitHub's login grammar is wider than this
10461                // source's floor — is one nothing can be filed under.
10462                parent
10463                    .own_repository
10464                    .as_ref()
10465                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
10466                    .ok_or_else(|| SourceError::Malformed {
10467                        message: format!(
10468                            "GitHub project issue {} on the board of source {} is in {}, which \
10469                             is not a {}/owner/name repository this source can place {} in",
10470                            parent.id.0,
10471                            self.name,
10472                            parent
10473                                .own_repository
10474                                .as_ref()
10475                                .map_or("no repository", Repository::as_str),
10476                            RepositoryTarget::HOST,
10477                            what
10478                        ),
10479                    })
10480            })
10481            .transpose()?;
10482        self.creation_rule_in_repository(repositories, parents_repository, what)
10483            .await
10484    }
10485
10486    async fn creation_rule_in_repository(
10487        &self,
10488        repositories: &[Repository],
10489        parents_repository: Option<RepositoryTarget>,
10490        what: &str,
10491    ) -> Result<RepositoryTarget, SourceError> {
10492        let fallback = self.configured_repository()?;
10493        match repositories {
10494            [named] => {
10495                let target =
10496                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
10497                        message: format!(
10498                            "{} names repository {}, which is not a {}/owner/name repository \
10499                             source {} can create an issue in; name one that is, or name none",
10500                            what,
10501                            named.as_str(),
10502                            RepositoryTarget::HOST,
10503                            self.name
10504                        ),
10505                    })?;
10506                // On a source declared private, an issue whose one named repository does not
10507                // read private is created where an item naming none is: the item still names
10508                // that repository in its metadata slot, and its issue stays private.
10509                if self.is_declared_private()
10510                    && self.repository_visibility(&target).await?
10511                        != onetaskgraph_plugin_api::Visibility::Private
10512                {
10513                    return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
10514                }
10515                if let Some(parents) = &parents_repository
10516                    && parents.owner != target.owner
10517                {
10518                    return Err(SourceError::Refused {
10519                        message: format!(
10520                            "{} names repository {}, owned by {}, but its project's issue is in \
10521                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
10522                             of the same owner as its parent issue; name a repository of {}, or \
10523                             name none",
10524                            what,
10525                            target.slug(),
10526                            target.owner,
10527                            parents.slug(),
10528                            parents.owner,
10529                            parents.owner
10530                        ),
10531                    });
10532                }
10533                Ok(target)
10534            }
10535            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
10536        }
10537    }
10538
10539    /// The node id of the repository `incoming` is being created in, or the refusal naming
10540    /// the item and the repository the token cannot see.
10541    ///
10542    /// Resolved once per command per repository; see [`Self::repository_cache`].
10543    async fn repository_id(
10544        &self,
10545        repository: &RepositoryTarget,
10546        incoming: &Incoming<'_>,
10547    ) -> Result<String, SourceError> {
10548        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
10549            return Ok(id);
10550        }
10551        let data = self
10552            .graphql(
10553                graphql::REPOSITORY,
10554                json!({"owner":repository.owner,"name":repository.name}),
10555            )
10556            .await?;
10557        self.repository_read(&data, repository, incoming)
10558    }
10559
10560    /// The repository's node id out of an answer carrying the `repository` root, held for
10561    /// the rest of this command, or the refusal naming the item that cannot be created in it.
10562    fn repository_read(
10563        &self,
10564        data: &Value,
10565        repository: &RepositoryTarget,
10566        incoming: &Incoming<'_>,
10567    ) -> Result<String, SourceError> {
10568        let node = data
10569            .get("repository")
10570            .filter(|value| !value.is_null())
10571            .ok_or_else(|| SourceError::Refused {
10572                message: format!(
10573                    "GitHub repository {} was not found or is not visible to the token, so {} \
10574                     {:?} cannot be created in it",
10575                    repository.slug(),
10576                    incoming.written.kind().describes(),
10577                    incoming.title
10578                ),
10579            })?;
10580        let id = required_str(node, "id")?.to_owned();
10581        self.repository_cache()?
10582            .insert(repository.clone(), id.clone());
10583        Ok(id)
10584    }
10585
10586    fn repository_cache(
10587        &self,
10588    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
10589        self.repository_cache
10590            .lock()
10591            .map_err(|_| SourceError::Unavailable {
10592                message: "this source's record of the destination repository was left \
10593                          inconsistent by an earlier failure; next: run the command again"
10594                    .into(),
10595            })
10596    }
10597
10598    /// Create or update one board item, whichever kind it is.
10599    async fn write_item(
10600        &self,
10601        incoming: &Incoming<'_>,
10602        target: Option<&NativeId>,
10603        depends_on: &[DependencyEdge],
10604    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10605        let prepared = self.prepare_write(incoming, target, depends_on).await?;
10606        let landed = match &prepared.destination {
10607            // Its content is written last, by `complete_write`, once everything else has landed.
10608            WriteDestination::Existing(item) => Landed {
10609                content_id: item.id.clone(),
10610                item_id: Some(item.item_id.clone()),
10611                url: item.url.clone(),
10612                number: item.number,
10613            },
10614            WriteDestination::New(target) => {
10615                if self.deferring()? {
10616                    self.create_filed_issue(
10617                        prepared.board.id.as_str(),
10618                        target,
10619                        incoming,
10620                        &prepared.body,
10621                    )
10622                    .await?
10623                } else {
10624                    self.create_and_file_issue(
10625                        prepared.board.id.as_str(),
10626                        target,
10627                        incoming,
10628                        &prepared.body,
10629                    )
10630                    .await?
10631                }
10632            }
10633        };
10634        self.complete_write(incoming, prepared, landed).await
10635    }
10636
10637    /// Everything one write of `incoming` reads and decides before it sends anything: what is
10638    /// there now, where a new item is created, the board's fields, and every value refused while
10639    /// nothing has been written — the item's body included.
10640    async fn prepare_write(
10641        &self,
10642        incoming: &Incoming<'_>,
10643        target: Option<&NativeId>,
10644        depends_on: &[DependencyEdge],
10645    ) -> Result<Prepared, SourceError> {
10646        // Refused before anything is read or written: a task or a project titled the way
10647        // this board spells a document would land as an issue this same source reads back
10648        // as a document, so the field this destination cannot carry is named rather than
10649        // written and silently reclassified.
10650        if let Written::Work(kind, _) = incoming.written
10651            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
10652        {
10653            return Err(SourceError::Refused {
10654                message: format!(
10655                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
10656                     spells a document, so it would read back as one rather than as a {}; \
10657                     retitle it, or copy it as a document",
10658                    kind.marker(),
10659                    self.name,
10660                    kind.marker()
10661                ),
10662            });
10663        }
10664        // The destination is read by its own id, and whether this board holds it is decided
10665        // by that read — its own `projectItems` — rather than by whether a listing of the
10666        // board happens to include it yet. See the module documentation.
10667        // An existing issue is never moved; a new one is created where the rule says — and
10668        // knowing where is what lets the board's fields and that repository's id be read
10669        // together, before anything below needs either.
10670        let destination = match target {
10671            Some(target) => {
10672                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
10673                    || SourceError::Refused {
10674                        message: format!("GitHub destination item {} was not found", target.0),
10675                    },
10676                )?))
10677            }
10678            None => {
10679                let target = self.creation_target(incoming).await?;
10680                self.creation_context(&target, incoming).await?;
10681                WriteDestination::New(target)
10682            }
10683        };
10684        let existing = destination.existing();
10685        let board = self
10686            .fields_for(
10687                existing,
10688                incoming.metadata.contains_key(ORIGIN_KEY),
10689                incoming.written.status().is_some(),
10690                incoming
10691                    .priority
10692                    .is_some_and(|priority| priority != Priority::None),
10693            )
10694            .await?;
10695        let status_target = incoming
10696            .written
10697            .work_status()
10698            .map(|(kind, status)| self.resolved_target(kind, status.category))
10699            .transpose()?;
10700        let column = match (incoming.written.work_status(), status_target.as_ref()) {
10701            (Some((kind, status)), Some(target)) => {
10702                self.column_for(&board.fields, kind, status.category, target)?
10703            }
10704            _ => None,
10705        };
10706        // Resolved before anything is created, for the reason the column above is: a
10707        // priority this board has no option for is refused while nothing has been written.
10708        let priority_write = match incoming.priority {
10709            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
10710            None => None,
10711        };
10712        // And for the reason the priority is: a value no projected field can hold, or a board
10713        // that cannot hold the field, is refused while nothing has been written.
10714        let (projections, projected) = self.projection_writes(
10715            &board.fields,
10716            existing.map(|item| &item.projected),
10717            incoming.metadata,
10718            None,
10719        )?;
10720        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
10721        if content_kind == ContentKind::DraftIssue {
10722            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
10723                (status_target.as_ref(), incoming.written.status())
10724            {
10725                return Err(self.closes_a_draft(status.category));
10726            }
10727            if incoming.parent.is_some() {
10728                return Err(SourceError::Refused {
10729                    message: "GitHub draft items cannot be a project's sub-issue".into(),
10730                });
10731            }
10732        }
10733        // The people it is to be assigned to are looked up — unless this process already has —
10734        // and a login GitHub does not know refused, while nothing has been written.
10735        if let Some(people) = incoming.assignees
10736            && existing.is_none_or(|item| {
10737                item.assignees
10738                    .as_deref()
10739                    .is_none_or(|held| !assignees::same_people(held, people))
10740            })
10741        {
10742            if content_kind == ContentKind::DraftIssue {
10743                return Err(self.assigns_a_draft());
10744            }
10745            self.resolve_people(people).await?;
10746            self.people_ids(
10747                people,
10748                &existing.map_or_else(
10749                    || format!("the new {}", incoming.title),
10750                    Resolved::written_name,
10751                ),
10752            )?;
10753        }
10754        match existing {
10755            Some(item) if content_kind == ContentKind::Issue => {
10756                if item.labels != incoming.labels {
10757                    return Err(SourceError::Refused {
10758                        message: "GitHub issue labels differ from the labels being written".into(),
10759                    });
10760                }
10761            }
10762            _ => {
10763                if !incoming.labels.is_empty() {
10764                    return Err(SourceError::Refused {
10765                        message: "GitHub items created by this destination carry no labels".into(),
10766                    });
10767                }
10768            }
10769        }
10770
10771        // The repository the issue really lives in is what the slot below is written against,
10772        // so a single entry that is where the issue is created travels as no key at all, and
10773        // the read side derives it back from the issue.
10774        let own_repository = match &destination {
10775            WriteDestination::Existing(item) => item.own_repository.clone(),
10776            WriteDestination::New(target) => Some(
10777                Repository::try_from(target.origin())
10778                    .map_err(|message| SourceError::Config { message })?,
10779            ),
10780        };
10781        let (native, fallback) = self
10782            .partition_edges(
10783                incoming.written.kind(),
10784                content_kind,
10785                existing.and_then(|item| item.blocked_by.as_deref()),
10786                depends_on,
10787            )
10788            .await?;
10789        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
10790        let content = match incoming.assets {
10791            Some(assets) => {
10792                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
10793                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
10794                    incoming.content.unwrap_or_default(),
10795                    &mut slot,
10796                    &uploads,
10797                );
10798                incoming.content.map(|_| rewritten)
10799            }
10800            None => incoming.content.map(str::to_owned),
10801        };
10802        // A write over an existing item keeps its `## Links` section, re-rendered from the
10803        // links it holds.
10804        let content = content.map(|content| {
10805            onetaskgraph_plugin_api::relinked(
10806                &content,
10807                existing.and_then(|item| item.body.as_deref()),
10808                incoming.metadata,
10809            )
10810        });
10811        let marked = content.as_deref().map(|content| self.marked(content));
10812        let body = compose_body(marked.as_deref(), &slot)?;
10813        // Read before anything is created, for the reason the field below is: a value
10814        // this destination cannot store has to refuse, and refusing after `createIssue`
10815        // would leave an issue behind that nothing asked for. The engine writes a
10816        // qualified id here; a caller handing this key anything else is told so rather
10817        // than having it silently stored as no origin at all.
10818        // 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.
10819        let origin = match incoming.metadata.get(ORIGIN_KEY) {
10820            None => "",
10821            Some(Value::String(origin)) => origin.as_str(),
10822            Some(other) => {
10823                return Err(SourceError::Refused {
10824                    message: format!(
10825                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
10826                         is {other}"
10827                    ),
10828                });
10829            }
10830        };
10831        // Resolved before anything is created: a board that cannot carry the copy origin
10832        // has to refuse the write, and refusing it after `createIssue` would leave an
10833        // issue behind that nothing asked for.
10834        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
10835            Some(field) => {
10836                if required_str(field, "__typename")? != "ProjectV2Field" {
10837                    return Err(SourceError::Refused {
10838                        message: format!(
10839                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
10840                        ),
10841                    });
10842                }
10843                Some(required_str(field, "id")?.to_owned())
10844            }
10845            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
10846                return Err(SourceError::Refused {
10847                    message: format!(
10848                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
10849                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
10850                         the board"
10851                    ),
10852                });
10853            }
10854            None => None,
10855        };
10856
10857        Ok(Prepared {
10858            origin: origin.to_owned(),
10859            destination,
10860            board,
10861            status_target,
10862            column,
10863            priority_write,
10864            projections,
10865            projected,
10866            content_kind,
10867            own_repository,
10868            native,
10869            slot,
10870            content,
10871            body,
10872            origin_field,
10873        })
10874    }
10875
10876    /// Everything one write does once the item it writes exists: its board fields, its
10877    /// relationships and — for an item that was already there — its content last, then what this
10878    /// process remembers of it.
10879    ///
10880    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
10881    /// item this command created has its fields, its closing and its edges held for
10882    /// [`TaskSource::finish_writes`] rather than sent here.
10883    async fn complete_write(
10884        &self,
10885        incoming: &Incoming<'_>,
10886        prepared: Prepared,
10887        landed: Landed,
10888    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10889        let Prepared {
10890            destination,
10891            board,
10892            status_target,
10893            column,
10894            priority_write,
10895            projections,
10896            projected,
10897            content_kind,
10898            own_repository,
10899            native,
10900            slot,
10901            content,
10902            body,
10903            origin,
10904            origin_field,
10905        } = prepared;
10906        let existing = destination.existing();
10907        let origin = origin.as_str();
10908        let Landed {
10909            content_id,
10910            item_id,
10911            url,
10912            number,
10913        } = landed;
10914
10915        // Only what differs from the item as this write read it is sent: a priority the item
10916        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
10917        // it since that read is left alone.
10918        let priority_write = priority_write.filter(|_| {
10919            !existing.is_some_and(|item| {
10920                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
10921            })
10922        });
10923        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
10924        let column = column
10925            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
10926            .map(|(field, option, _)| (field, option));
10927        // Creating an item here is several calls — `createIssue`, which files it on the
10928        // board, then its board fields, the parent and the dependencies — and GitHub can fail
10929        // at any of them. Everything this source can refuse *before* the first of those is
10930        // already checked above, so what is left is GitHub itself failing part way. When it
10931        // does over an item this call created, the issue is taken back: a write that
10932        // refused must not leave an item behind that nobody asked for, and one that does
10933        // makes the retry create a second.
10934        // Whether the board-field write carrying a moved origin was answered as landing whole.
10935        // When it was refused, GitHub does not say which of its fields ran before the one that
10936        // failed, so the origin may or may not have moved.
10937        let mut origin_landed = false;
10938        // An item this command created while writes are held back has every part that can land
10939        // later held for `finish_writes` — its own create filed it on the board and under its
10940        // project already — and so does a later write of it in the same command.
10941        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
10942        let landed = if holding {
10943            self.hold_back(
10944                &board.id,
10945                &content_id,
10946                existing,
10947                origin_field.as_deref(),
10948                origin,
10949                column,
10950                status_target.as_ref(),
10951                priority_write.as_ref(),
10952                &projections,
10953                &native,
10954            )
10955        } else if let Some(item_id) = item_id.as_deref() {
10956            self.finish_write(
10957                board.id.as_str(),
10958                incoming,
10959                &content_id,
10960                item_id,
10961                content_kind,
10962                existing,
10963                origin_field.as_deref(),
10964                origin,
10965                column,
10966                status_target.as_ref(),
10967                priority_write.as_ref(),
10968                &projections,
10969                &native,
10970                &mut origin_landed,
10971            )
10972            .await
10973        } else {
10974            // An issue a create filed on the board itself has no board item this process
10975            // knows until `finish_writes` reads it, so nothing but a held-back write may land
10976            // on it before then.
10977            Err(SourceError::Malformed {
10978                message: format!(
10979                    "issue {} was written before the board item GitHub filed it as was read",
10980                    content_id.0
10981                ),
10982            })
10983        };
10984        // An existing item's title, body and state go last, in one `updateIssue`, once its board
10985        // fields and its relationships have landed: a refusal of any of those then leaves its
10986        // body — and the metadata slot inside it — exactly as it stood.
10987        let landed = match (landed, existing) {
10988            (Ok(()), Some(item)) => {
10989                self.update_existing(item, incoming, &body, status_target.as_ref())
10990                    .await
10991            }
10992            (landed, _) => landed,
10993        };
10994        if let Err(error) = landed {
10995            match existing {
10996                // Best effort, and the write's own failure is what the caller is told: a
10997                // refusal naming the tidy-up would hide why the write failed at all.
10998                None => {
10999                    let _ = self.delete_issue(&content_id).await;
11000                }
11001                // The origin field is the one piece of an existing item's metadata written
11002                // before its body, so a write refused after it puts it back as it was. When
11003                // that is refused too, the write's own failure is still what the caller is
11004                // told — with what it left behind added, because the item's metadata is then
11005                // not as it stood and a caller retrying has to know which key moved.
11006                Some(item) => {
11007                    let before = item.origin.as_deref().unwrap_or("");
11008                    if let Some(field) = origin_field.as_deref()
11009                        && before != origin
11010                        && let Err(restore) = self
11011                            .set_item_field(
11012                                board.id.as_str(),
11013                                &item.item_id,
11014                                field,
11015                                json!({"text": before}),
11016                            )
11017                            .await
11018                    {
11019                        let left = if origin_landed {
11020                            format!(
11021                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
11022                                 not be put back to {before:?} ({restore}), so item {} still \
11023                                 holds {origin:?} there",
11024                                item.id.0
11025                            )
11026                        } else {
11027                            format!(
11028                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
11029                                 {origin:?}, GitHub does not say whether that part of it ran, \
11030                                 and putting it back to {before:?} was refused ({restore}), so \
11031                                 item {} holds {origin:?} or {before:?} there",
11032                                item.id.0
11033                            )
11034                        };
11035                        return Err(noting(
11036                            error,
11037                            &format!(
11038                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
11039                                 run the write again"
11040                            ),
11041                        ));
11042                    }
11043                }
11044            }
11045            return Err(error);
11046        }
11047
11048        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
11049            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
11050                self.statuses
11051                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
11052            }
11053            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
11054                self.statuses
11055                    .status(kind, written_option.as_deref(), false, None)
11056            }
11057            (Some((_, status)), _) => status.clone(),
11058            (None, _) => Status {
11059                category: StatusCategory::Unknown,
11060                name: "Open".to_owned(),
11061            },
11062        };
11063
11064        // So the rest of this command reads what it just did rather than what the board
11065        // said before it. See `remember_written` for which half takes it.
11066        let remembered = Resolved {
11067            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
11068            // and `learn_item` puts it here.
11069            // 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.
11070            item_id: item_id.unwrap_or_default(),
11071            id: content_id.clone(),
11072            content_kind,
11073            kind: incoming.written.kind(),
11074            title: incoming.title.to_owned(),
11075            // The visible half of the body this write composed, split back off it the
11076            // way a read splits it — so what this record reports is what a read of the
11077            // same issue reports, rather than the person's text with the metadata slot
11078            // still on the end of it.
11079            body: self.visible(body.clone())?.0,
11080            raw_body: body.clone(),
11081            // A document has no status of its own; what it reads back as is whatever
11082            // the issue's own state says, which is what a re-read reports.
11083            status: written_status,
11084            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
11085            priority: match incoming.priority {
11086                Some(priority) => HeldPriority::Read(priority),
11087                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
11088                    item.priority.clone()
11089                }),
11090            },
11091            // What `state_input` asked for: closed for a terminal target, open for any other
11092            // status, and the issue's own state left as it was by a document write.
11093            closed: content_kind == ContentKind::Issue
11094                && match status_target.as_ref() {
11095                    Some(StatusTarget::Terminal(_, _)) => true,
11096                    Some(_) => false,
11097                    None => existing.is_some_and(|item| item.closed),
11098                },
11099            reason: match status_target.as_ref() {
11100                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
11101                    Some(reason.reason().to_owned())
11102                }
11103                Some(_) => None,
11104                None => existing.and_then(|item| item.reason.clone()),
11105            },
11106            delivers: incoming.delivers.to_vec(),
11107            delivered_by: incoming.delivered_by.to_vec(),
11108            labels: incoming.labels.to_vec(),
11109            parent: incoming.parent.cloned(),
11110            issue_parent: None,
11111            origin: (!origin.is_empty()).then(|| origin.to_owned()),
11112            projected,
11113            number,
11114            // In the update path this is the item's own url, read off `existing` where the
11115            // record above was bound, so one expression serves both halves.
11116            url,
11117            created_at: existing.and_then(|item| item.created_at),
11118            updated_at: existing.and_then(|item| item.updated_at),
11119            // What this write moved is GitHub's to say, so a record of it gives no token until
11120            // it is read again.
11121            item_updated_at: None,
11122            comment_count: None,
11123            relationship_events: None,
11124            served_at: None,
11125            facets: None,
11126            own_repository,
11127            repositories: incoming.repositories.to_vec(),
11128            classification: incoming.classification,
11129            slot,
11130            board_id: Some(board.id.as_str().to_owned()),
11131            fields: board
11132                .fields
11133                .get("nodes")
11134                .and_then(Value::as_array)
11135                .cloned()
11136                .unwrap_or_default(),
11137            board_fields: Some(board.fields.clone()),
11138            // What this write left the relationship holding is known by id alone, and a
11139            // later read of its edges needs each far end's kind, so it reads them again.
11140            blocked_by: None,
11141            // The people it named, which its own answer was held to; none named keeps the
11142            // item's, and creates a task of an opted-in source unassigned.
11143            assignees: match incoming.assignees {
11144                Some(people) => Some(people.to_vec()),
11145                None => match existing {
11146                    Some(item) => item.assignees.clone(),
11147                    None => (self.assignees
11148                        && matches!(incoming.written, Written::Work(ItemKind::Task, _)))
11149                    .then(Vec::new),
11150                },
11151            },
11152        };
11153        self.remember_written(remembered, existing.is_none())?;
11154        Ok(onetaskgraph_plugin_api::AssetsWritten {
11155            id: content_id,
11156            content,
11157        })
11158    }
11159
11160    /// Everything a write does after the item exists: its board fields, its parent, and
11161    /// its dependencies.
11162    ///
11163    /// Split out of `write_item` so there is one place a failure past the point of no
11164    /// return is caught, rather than a tidy-up repeated at each `?` above.
11165    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
11166    // so there is one place a failure past the point of no return is caught, and its
11167    // arguments are exactly the values that tail already had in scope. Bundling them into a
11168    // struct would describe no concept — it would be "the arguments of this function" — and
11169    // would put the whole of `write_item`'s locals behind one more indirection.
11170    #[allow(clippy::too_many_arguments)]
11171    async fn finish_write(
11172        &self,
11173        board_id: &str,
11174        incoming: &Incoming<'_>,
11175        content_id: &NativeId,
11176        item_id: &str,
11177        content_kind: ContentKind,
11178        existing: Option<&Resolved>,
11179        origin_field: Option<&str>,
11180        origin: &str,
11181        column: Option<(String, String)>,
11182        status_target: Option<&StatusTarget>,
11183        priority: Option<&PriorityWrite>,
11184        projections: &[ProjectionWrite],
11185        native: &[String],
11186        origin_landed: &mut bool,
11187    ) -> Result<(), SourceError> {
11188        let (fields, clears) = field_writes(
11189            existing,
11190            origin_field,
11191            origin,
11192            column,
11193            priority,
11194            projections,
11195        );
11196        self.set_item_fields(board_id, item_id, &fields, &clears)
11197            .await?;
11198        *origin_landed = true;
11199
11200        // An existing issue closes in the `updateIssue` its write ends with; one created just
11201        // now closes here, once its option is selected.
11202        if existing.is_none()
11203            && content_kind == ContentKind::Issue
11204            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
11205        {
11206            self.update_content(
11207                ContentKind::Issue,
11208                content_id,
11209                json!({"stateInput":state_input(status_target)}),
11210            )
11211            .await?;
11212        }
11213
11214        if content_kind == ContentKind::Issue {
11215            self.reparent(
11216                existing.and_then(|item| item.parent.clone()),
11217                content_id,
11218                incoming.parent,
11219            )
11220            .await?;
11221            // A document takes part in no dependency graph, so writing one neither reads
11222            // nor changes the issue's own `blockedBy` relationships. Reconciling them
11223            // against the empty list a document write carries would *delete* whatever
11224            // relationships a person had made on that issue, which is a write nobody
11225            // asked for.
11226            if incoming.written.kind() != BoardKind::Document {
11227                let issue = match existing {
11228                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
11229                    None => Issue::Created,
11230                };
11231                self.reconcile_blocked_by(content_id, native, issue).await?;
11232            }
11233        }
11234        Ok(())
11235    }
11236
11237    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
11238    fn deferring(&self) -> Result<bool, SourceError> {
11239        Ok(self.deferred()?.is_some())
11240    }
11241
11242    /// Whether `content` is an item this command created whose parts are held back.
11243    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
11244        Ok(self
11245            .deferred()?
11246            .as_ref()
11247            .is_some_and(|deferred| deferred.holds(content)))
11248    }
11249
11250    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
11251        self.deferred.lock().map_err(|_| SourceError::Unavailable {
11252            message: "this source's record of the writes it is holding back was left \
11253                      inconsistent by an earlier failure; next: run the command again"
11254                .into(),
11255        })
11256    }
11257
11258    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
11259    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
11260    /// creates with: one request where adding it to the board and filing it under its project
11261    /// were two more. GitHub adds the board item after answering, so its id is not known here;
11262    /// [`TaskSource::finish_writes`] reads it.
11263    async fn create_filed_issue(
11264        &self,
11265        board_id: &str,
11266        repository: &RepositoryTarget,
11267        incoming: &Incoming<'_>,
11268        body: &Option<String>,
11269    ) -> Result<Landed, SourceError> {
11270        let repository_id = self.repository_id(repository, incoming).await?;
11271        let mut input = filed_input(&repository_id, board_id, incoming, body);
11272        self.assigning(&mut input, incoming)?;
11273        let data = match self
11274            .graphql(graphql::CREATE_ISSUE, json!({"input": &input}))
11275            .await
11276        {
11277            Ok(data) => data,
11278            Err(error) => return Err(self.refusing_people(&input, error)),
11279        };
11280        let created = data
11281            .pointer("/createIssue/issue")
11282            .filter(|value| !value.is_null())
11283            .ok_or_else(|| SourceError::Malformed {
11284                message: "GitHub issue creation returned no issue".into(),
11285            })?;
11286        let landed = self.created_landed(created).await?;
11287        // An issue GitHub created assigned otherwise than asked is refused and taken back; a
11288        // delete GitHub refuses too is answered beside the refusal, naming the issue left.
11289        if let Err(error) = self.assigned_as_asked(&input, Some(created)) {
11290            return Err(self.taken_back(&landed.content_id, created, error).await);
11291        }
11292        Ok(landed)
11293    }
11294
11295    /// Keep the address `createIssue` answered for the issue `content`, for
11296    /// [`TaskSource::written_location`].
11297    fn note_written(&self, content: &NativeId, url: Option<&str>) {
11298        if let Some(url) = url {
11299            self.written
11300                .lock()
11301                .unwrap_or_else(std::sync::PoisonError::into_inner)
11302                .insert(content.clone(), url.to_owned());
11303        }
11304    }
11305
11306    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
11307    /// read, the refusal, after taking the issue back.
11308    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
11309        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
11310        let url = optional_str(created, "url")?.map(str::to_owned);
11311        let number = match created_issue_number(created) {
11312            Ok(number) => number,
11313            Err(error) => {
11314                let _ = self.delete_issue(&content_id).await;
11315                return Err(error);
11316            }
11317        };
11318        self.note_written(&content_id, url.as_deref());
11319        Ok(Landed {
11320            content_id,
11321            item_id: None,
11322            url,
11323            number,
11324        })
11325    }
11326
11327    /// Hold back everything a write of the created item `content` would send after creating it,
11328    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
11329    /// `finish_write` sends, the parent excepted, which its create already named. A later write
11330    /// of the same item replaces what it changes.
11331    // 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.
11332    #[allow(clippy::too_many_arguments)]
11333    fn hold_back(
11334        &self,
11335        board_id: &BoardId,
11336        content: &NativeId,
11337        existing: Option<&Resolved>,
11338        origin_field: Option<&str>,
11339        origin: &str,
11340        column: Option<(String, String)>,
11341        status_target: Option<&StatusTarget>,
11342        priority: Option<&PriorityWrite>,
11343        projections: &[ProjectionWrite],
11344        native: &[String],
11345    ) -> Result<(), SourceError> {
11346        let status = column.map(|(field, option)| {
11347            let name = self
11348                .fields_cache()
11349                .ok()
11350                .and_then(|fields| {
11351                    fields.as_ref().and_then(|board| {
11352                        option_name(&board.fields, &field, &option).map(str::to_owned)
11353                    })
11354                })
11355                .unwrap_or_default();
11356            (field, option, name)
11357        });
11358        let (fields, clears) =
11359            field_writes(existing, origin_field, origin, None, priority, projections);
11360        let close = match status_target {
11361            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
11362            _ => None,
11363        };
11364        let mut deferred = self.deferred()?;
11365        let deferred = deferred.get_or_insert_with(Deferred::default);
11366        if let Deferred::Nothing = deferred {
11367            *deferred = Deferred::Held {
11368                board: board_id.clone(),
11369                created: Vec::new(),
11370            };
11371        }
11372        let Deferred::Held { created, .. } = deferred else {
11373            unreachable!("held back on the line above");
11374        };
11375        let held = match created.iter().position(|held| held.content == *content) {
11376            Some(at) => &mut created[at],
11377            None => {
11378                created.push(HeldBack {
11379                    content: content.clone(),
11380                    fields: Vec::new(),
11381                    status: None,
11382                    clears: Vec::new(),
11383                    close: None,
11384                    edges: Vec::new(),
11385                });
11386                created.last_mut().expect("just held")
11387            }
11388        };
11389        for (field, value) in fields {
11390            held.fields.retain(|(held, _)| *held != field);
11391            held.clears.retain(|held| *held != field);
11392            held.fields.push((field, value));
11393        }
11394        for field in clears {
11395            held.fields.retain(|(held, _)| held != field);
11396            if !held.clears.iter().any(|held| held == field) {
11397                held.clears.push(field.to_owned());
11398            }
11399        }
11400        if status.is_some() {
11401            held.status = status;
11402        }
11403        if close.is_some() {
11404            held.close = close;
11405        }
11406        // Nothing of an item created in this command has been sent but its create, so the edges
11407        // it is to have are exactly the ones its latest write names.
11408        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
11409        Ok(())
11410    }
11411
11412    /// Create every task of `writes`, together, filed on the board and under each one's
11413    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
11414    ///
11415    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
11416    /// before the first request — then created, then completed with its parts held back. A
11417    /// request refused part way leaves the issues it did create, which are taken back, with the
11418    /// rest of this call's, before the refusal is answered.
11419    async fn create_together(
11420        &self,
11421        writes: &[ItemWrite<Task>],
11422    ) -> Result<Vec<NativeId>, SourceError> {
11423        let incomings: Vec<Incoming<'_>> = writes
11424            .iter()
11425            .map(|write| self.task_incoming(write, None))
11426            .collect::<Result<_, _>>()?;
11427        // Every person these creates assign, looked up together rather than one write at a time.
11428        let people: Vec<String> = writes
11429            .iter()
11430            .filter_map(|write| write.item.assignees.clone())
11431            .flatten()
11432            .collect();
11433        self.resolve_people(&people).await?;
11434        let mut prepared = Vec::with_capacity(writes.len());
11435        for (incoming, write) in incomings.iter().zip(writes) {
11436            if write.target.is_some() {
11437                return Err(SourceError::Malformed {
11438                    message: "a write naming its target was handed to a batch of creates".into(),
11439                });
11440            }
11441            prepared.push(
11442                self.prepare_write(incoming, None, &write.depends_on)
11443                    .await?,
11444            );
11445        }
11446        let mut inputs = Vec::with_capacity(prepared.len());
11447        for (incoming, prepared) in incomings.iter().zip(&prepared) {
11448            let WriteDestination::New(target) = &prepared.destination else {
11449                return Err(SourceError::Malformed {
11450                    message: "an update was decided for a write handed to a batch of creates"
11451                        .into(),
11452                });
11453            };
11454            let repository_id = self.repository_id(target, incoming).await?;
11455            let mut input = filed_input(
11456                &repository_id,
11457                prepared.board.id.as_str(),
11458                incoming,
11459                &prepared.body,
11460            );
11461            self.assigning(&mut input, incoming)?;
11462            inputs.push(input);
11463        }
11464        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
11465        let refused = 'create: {
11466            for chunk in inputs.chunks(MUTATION_BATCH) {
11467                let mut variables = serde_json::Map::new();
11468                for slot in 0..MUTATION_BATCH {
11469                    variables.insert(
11470                        format!("create{slot}"),
11471                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
11472                    );
11473                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
11474                }
11475                let data = match self
11476                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
11477                    .await
11478                {
11479                    Ok(data) => data,
11480                    Err(error) => break 'create Some(error),
11481                };
11482                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
11483                    let said: Vec<&str> = errors
11484                        .as_array()
11485                        .into_iter()
11486                        .flatten()
11487                        .filter_map(|error| error.get("message").and_then(Value::as_str))
11488                        .collect();
11489                    SourceError::Refused {
11490                        message: if said.is_empty() {
11491                            "GitHub returned GraphQL errors".to_owned()
11492                        } else {
11493                            said.join("; ")
11494                        },
11495                    }
11496                });
11497                for (slot, input) in chunk.iter().enumerate() {
11498                    match data
11499                        .pointer(&format!("/create{slot}/issue"))
11500                        .filter(|value| !value.is_null())
11501                    {
11502                        Some(created) => match self.created_landed(created).await {
11503                            Ok(landed_one) => {
11504                                landed.push(landed_one);
11505                                // Taken back with the rest, as any refusal here is.
11506                                if let Err(error) = self.assigned_as_asked(input, Some(created)) {
11507                                    refusal.get_or_insert(error);
11508                                }
11509                            }
11510                            Err(error) => {
11511                                refusal.get_or_insert(error);
11512                            }
11513                        },
11514                        // An alias GitHub refused answers nothing, and the refusal says why.
11515                        None => {
11516                            refusal.get_or_insert(SourceError::Malformed {
11517                                message: "GitHub issue creation returned no issue".into(),
11518                            });
11519                        }
11520                    }
11521                }
11522                if let Some(error) = refusal {
11523                    break 'create Some(error);
11524                }
11525            }
11526            None
11527        };
11528        if let Some(error) = refused {
11529            // None of these ids reaches the caller, so an issue GitHub will not take back is
11530            // named here or by nobody.
11531            let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
11532            let left = self.delete_issues(&ids).await;
11533            let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
11534            return Err(left_behind(error, &left));
11535        }
11536        let created: Vec<NativeId> = landed
11537            .iter()
11538            .map(|landed| landed.content_id.clone())
11539            .collect();
11540        let mut ids = Vec::with_capacity(landed.len());
11541        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
11542            match self.complete_write(incoming, prepared, landed).await {
11543                Ok(written) => ids.push(written.id),
11544                // 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.
11545                Err(error) => {
11546                    let ids: Vec<&NativeId> = created.iter().collect();
11547                    let left = self.delete_issues(&ids).await;
11548                    let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
11549                    return Err(left_behind(error, &left));
11550                }
11551            }
11552        }
11553        Ok(ids)
11554    }
11555
11556    /// Hold back the update of the existing issue `issue` — `members`, what its own
11557    /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
11558    /// another go to GitHub as one request. Whatever this source held of the issue is dropped
11559    /// as the mutation itself would drop it. Answers whether it was held: never while a write
11560    /// the store's cache expects runs, which records each mutation's own answer, and never for
11561    /// an issue an update is already held for, which that one is sent ahead of.
11562    fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
11563        if self.expected.journaling() || self.deferred()?.is_none() {
11564            return Ok(false);
11565        }
11566        let mut held = self
11567            .held_updates
11568            .lock()
11569            .map_err(|_| SourceError::Unavailable {
11570                message: "issue updates held back were left inconsistent; run the command again"
11571                    .into(),
11572            })?;
11573        if held.iter().any(|earlier| earlier.issue == *issue) {
11574            return Ok(false);
11575        }
11576        let named = |value: &str| issue.0 == value;
11577        self.resolved_cache()?
11578            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
11579        self.read_ahead()?.retain(|id, item| {
11580            !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
11581        });
11582        self.carried()?
11583            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
11584        held.push(HeldUpdate::new(issue, members));
11585        Ok(true)
11586    }
11587
11588    /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
11589    /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
11590    /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
11591    async fn send_held_updates(&self) -> Result<(), SourceError> {
11592        let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
11593            SourceError::Unavailable {
11594                message: "issue updates held back were left inconsistent; run the command again"
11595                    .into(),
11596            }
11597        })?);
11598        if let [one] = held.as_slice() {
11599            return self
11600                .send_update(
11601                    graphql::UPDATE_ISSUE,
11602                    "/updateIssue/issue",
11603                    &one.issue,
11604                    one.input.clone(),
11605                )
11606                .await;
11607        }
11608        for chunk in held.chunks(MUTATION_BATCH) {
11609            let mut variables = serde_json::Map::new();
11610            for slot in 0..MUTATION_BATCH {
11611                variables.insert(
11612                    format!("update{slot}"),
11613                    chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
11614                );
11615                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
11616            }
11617            let data = match self
11618                .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
11619                .await
11620            {
11621                Ok(data) => data,
11622                Err(error) => {
11623                    for held in chunk {
11624                        self.forget_people(&held.input);
11625                    }
11626                    return Err(error);
11627                }
11628            };
11629            let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
11630            check_aliased_answers(
11631                graphql::UPDATE_ISSUES,
11632                &data,
11633                "update",
11634                &inputs,
11635                &[("issue", "id")],
11636            )?;
11637            for (slot, held) in chunk.iter().enumerate() {
11638                self.assigned_as_asked(&held.input, data.pointer(&format!("/update{slot}/issue")))?;
11639            }
11640        }
11641        Ok(())
11642    }
11643
11644    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
11645    /// created items' board items, then their field values and edges together in requests of
11646    /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
11647    /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
11648    async fn send_held_back(&self) -> Result<(), SourceError> {
11649        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
11650            return Ok(());
11651        };
11652        if created.is_empty() {
11653            return Ok(());
11654        }
11655        let sent = self.send_deferred(board.as_str(), &created).await;
11656        if let Err(error) = sent {
11657            // Best effort, and the failure is what the caller is told, as a write of one item
11658            // that fails part way takes back what it created: the copy that held these back
11659            // journalled every one, so one this cannot take back its undo tries again, and says
11660            // so when it cannot either.
11661            let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
11662            let _ = self.delete_issues(&ids).await;
11663            return Err(error);
11664        }
11665        Ok(())
11666    }
11667
11668    /// The requests [`Self::send_held_back`] makes.
11669    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
11670        let items = self.created_board_items(board, created).await?;
11671        let mut fields: Vec<Value> = Vec::new();
11672        let mut closes: Vec<Value> = Vec::new();
11673        let mut edges: Vec<Value> = Vec::new();
11674        for held in created {
11675            let (item, status) = &items[&held.content];
11676            let mut values = held.fields.clone();
11677            // A status the board already shows — set by a workflow a person turned on to file
11678            // new items at it — is not written again.
11679            if let Some((field, option, name)) = &held.status
11680                && status.as_deref() != Some(name.as_str())
11681            {
11682                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
11683            }
11684            for (field, value) in values {
11685                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
11686            }
11687            if let Some(reason) = held.close {
11688                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
11689            }
11690            for far in &held.edges {
11691                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
11692            }
11693            if !held.clears.is_empty() {
11694                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
11695                self.set_item_fields(board, item, &[], &clears).await?;
11696            }
11697        }
11698        // Each alias answers with what it wrote, which is what its input named: the board item a
11699        // field value is set on, the issue closed, the issue blocked and the one blocking it.
11700        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
11701        let close_answer: &[(&str, &str)] = &[("issue", "id")];
11702        let block_answer: &[(&str, &str)] =
11703            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
11704        // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
11705        // request: a request holding both kinds is the one document carrying both, so a plan's
11706        // values and edges cost one request where they fit in one.
11707        let writes: Vec<HeldWrite<'_>> = fields
11708            .iter()
11709            .map(HeldWrite::Field)
11710            .chain(edges.iter().map(HeldWrite::Edge))
11711            .collect();
11712        for chunk in writes.chunks(WRITE_BATCH) {
11713            let values: Vec<&Value> = chunk
11714                .iter()
11715                .filter_map(|write| match write {
11716                    HeldWrite::Field(input) => Some(*input),
11717                    HeldWrite::Edge(_) => None,
11718                })
11719                .collect();
11720            let blocks: Vec<&Value> = chunk
11721                .iter()
11722                .filter_map(|write| match write {
11723                    HeldWrite::Edge(input) => Some(*input),
11724                    HeldWrite::Field(_) => None,
11725                })
11726                .collect();
11727            let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
11728                (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
11729                (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
11730                _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
11731            };
11732            let mut variables = serde_json::Map::new();
11733            let kinds = [
11734                ("field", &values, sends[0], field_answer),
11735                ("block", &blocks, sends[1], block_answer),
11736            ];
11737            for (prefix, inputs, send, _) in &kinds {
11738                if inputs.is_empty() {
11739                    continue;
11740                }
11741                for slot in 0..WRITE_BATCH {
11742                    variables.insert(
11743                        format!("{prefix}{slot}"),
11744                        (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
11745                    );
11746                    variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
11747                }
11748            }
11749            let data = self.graphql(document, Value::Object(variables)).await?;
11750            for (prefix, inputs, _, answers) in kinds {
11751                check_aliased_answers(document, &data, prefix, inputs, answers)?;
11752            }
11753        }
11754        for chunk in closes.chunks(MUTATION_BATCH) {
11755            let mut variables = serde_json::Map::new();
11756            for slot in 0..MUTATION_BATCH {
11757                variables.insert(
11758                    format!("close{slot}"),
11759                    chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
11760                );
11761                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
11762            }
11763            let data = self
11764                .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
11765                .await?;
11766            let inputs: Vec<&Value> = chunk.iter().collect();
11767            check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
11768        }
11769        for held in created {
11770            self.learn_item(&held.content, &items[&held.content].0)?;
11771        }
11772        Ok(())
11773    }
11774
11775    /// The board item each of `created` is on this board, with its `Status` as the board set it:
11776    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
11777    /// eight and sixteen — for only those GitHub has not filed yet.
11778    ///
11779    /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
11780    /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
11781    /// create answer carrying the item — so only a read says it is there. One still absent once
11782    /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
11783    /// filing it meanwhile refuses that "Content already exists in this project", which says it
11784    /// is placed, and one more read finds the item rather than a second one being made.
11785    async fn created_board_items(
11786        &self,
11787        board: &str,
11788        created: &[HeldBack],
11789    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
11790        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
11791        let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
11792            created
11793                .iter()
11794                .map(|held| held.content.clone())
11795                .filter(|id| !found.contains_key(id))
11796                .collect()
11797        };
11798        let mut wait = Duration::from_secs(1);
11799        for attempt in 0..6 {
11800            let absent = unfiled(&found);
11801            if absent.is_empty() {
11802                return Ok(found);
11803            }
11804            if attempt > 0 {
11805                self.clock
11806                    .sleep_because(wait, pacing::cause::BOARD_FILING)
11807                    .await;
11808                wait = wait.saturating_mul(2);
11809            }
11810            self.read_created_items(board, &absent, &mut found).await?;
11811        }
11812        let mut placed_meanwhile = Vec::new();
11813        for id in unfiled(&found) {
11814            match self
11815                .graphql(
11816                    graphql::ADD_TO_BOARD,
11817                    json!({"input":{"projectId":board,"contentId":id.0}}),
11818                )
11819                .await
11820            {
11821                Ok(added) => {
11822                    let item = added
11823                        .pointer("/addProjectV2ItemById/item")
11824                        .filter(|value| !value.is_null())
11825                        .ok_or_else(|| SourceError::Malformed {
11826                            message: "GitHub board addition returned no project item".into(),
11827                        })?;
11828                    found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
11829                }
11830                Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
11831                Err(error) => return Err(error),
11832            }
11833        }
11834        if !placed_meanwhile.is_empty() {
11835            self.read_created_items(board, &placed_meanwhile, &mut found)
11836                .await?;
11837        }
11838        let absent = unfiled(&found);
11839        if absent.is_empty() {
11840            return Ok(found);
11841        }
11842        let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
11843        Err(SourceError::Unavailable {
11844            message: format!(
11845                "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
11846                 to it, still answers {} on no item of it: {}; next: run the copy again once \
11847                 GitHub has caught up",
11848                if absent.len() == 1 {
11849                    "an issue"
11850                } else {
11851                    "issues"
11852                },
11853                if absent.len() == 1 { "it" } else { "them" },
11854                if absent.len() == 1 { "it" } else { "them" },
11855                absent.join(", ")
11856            ),
11857        })
11858    }
11859
11860    /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
11861    /// request, adding to `found` the board item of every one GitHub has filed on `board`.
11862    async fn read_created_items(
11863        &self,
11864        board: &str,
11865        ids: &[NativeId],
11866        found: &mut BTreeMap<NativeId, (String, Option<String>)>,
11867    ) -> Result<(), SourceError> {
11868        for chunk in ids.chunks(COPY_DETAIL_BATCH) {
11869            let mut variables = serde_json::Map::new();
11870            for slot in 0..COPY_DETAIL_BATCH {
11871                variables.insert(
11872                    format!("id{slot}"),
11873                    json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
11874                );
11875                variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
11876            }
11877            variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
11878            variables.insert("statusName".into(), json!(STATUS_FIELD));
11879            let data = self
11880                .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
11881                .await?;
11882            for (slot, id) in chunk.iter().enumerate() {
11883                // An answer for another issue than the one asked about says nothing of
11884                // where that one is filed, and its board item is not this one's.
11885                let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
11886                let named = node.map(|node| node.get("id").and_then(Value::as_str));
11887                if let Some(named) = named
11888                    && named != Some(id.0.as_str())
11889                {
11890                    return Err(SourceError::Malformed {
11891                        message: format!(
11892                            "GitHub answered the board items of {} when asked for those of {}",
11893                            named.unwrap_or("an issue it did not name"),
11894                            id.0
11895                        ),
11896                    });
11897                }
11898                // An issue GitHub does not answer yet is not filed yet; one it answers is
11899                // read whole, its connection, each board item's board and its status, and
11900                // any of them unreadable is refused rather than read as not filed.
11901                let Some(node) = node else { continue };
11902                let malformed = |what: &str| SourceError::Malformed {
11903                    message: format!("GitHub answered the board items of {} with {what}", id.0),
11904                };
11905                let nodes = node
11906                    .pointer("/projectItems/nodes")
11907                    .and_then(Value::as_array)
11908                    .ok_or_else(|| malformed("no list of board items"))?;
11909                let mut item = None;
11910                for node in nodes {
11911                    let on = node
11912                        .pointer("/project/id")
11913                        .and_then(Value::as_str)
11914                        .ok_or_else(|| malformed("a board item naming no board"))?;
11915                    if on == board {
11916                        item = Some(node);
11917                    }
11918                }
11919                if let Some(item) = item {
11920                    let status = match item.get("status") {
11921                        None | Some(Value::Null) => None,
11922                        Some(status) => Some(
11923                            status
11924                                .get("name")
11925                                .and_then(Value::as_str)
11926                                .ok_or_else(|| malformed("a status that names no option"))?
11927                                .to_owned(),
11928                        ),
11929                    };
11930                    found.insert(
11931                        id.clone(),
11932                        (required_nonblank_str(item, "id")?.to_owned(), status),
11933                    );
11934                }
11935            }
11936        }
11937        Ok(())
11938    }
11939
11940    /// The board item id of `content`, which this process created and remembered without one,
11941    /// in every half of its own view.
11942    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
11943        if let Some(held) = self.resolved_cache()?.get_mut(content) {
11944            held.item_id = item.to_owned();
11945        }
11946        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
11947            held.item_id = item.to_owned();
11948        }
11949        Ok(())
11950    }
11951
11952    /// Ask together which items each of `origins` names, and what creating an item reads; see
11953    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
11954    /// page is left to its own lookup.
11955    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
11956        if self.repository.is_none() {
11957            return Ok(());
11958        }
11959        let unasked: Vec<&String> = {
11960            let held = self.narrowed_cache()?;
11961            let mut unasked: Vec<&String> = Vec::new();
11962            for origin in origins {
11963                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
11964                    && !unasked.contains(&origin)
11965                {
11966                    unasked.push(origin);
11967                }
11968            }
11969            unasked
11970        };
11971        if unasked.is_empty() {
11972            return Ok(());
11973        }
11974        // One batch is held back for the read ahead that follows, which may carry a project's
11975        // members beside it in the same request.
11976        if unasked.len() <= ORIGIN_BATCH {
11977            let mut pending =
11978                self.pending_origins
11979                    .lock()
11980                    .map_err(|_| SourceError::Unavailable {
11981                        message:
11982                            "origins held for a read ahead were left inconsistent; run the command \
11983                          again"
11984                                .into(),
11985                    })?;
11986            for origin in unasked {
11987                if !pending.contains(origin) {
11988                    pending.push(origin.clone());
11989                }
11990            }
11991            return Ok(());
11992        }
11993        let unasked: Vec<String> = unasked.into_iter().cloned().collect();
11994        self.send_origin_lookups(&unasked, None).await
11995    }
11996
11997    /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
11998    /// classification and repositories as the members page `data` carries them — when that page
11999    /// is every member, each one names `project` as its parent, and every record it holds is
12000    /// readable. Any other page holds nothing, so the members are read as they always were.
12001    fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
12002        let Some(page) = data
12003            .pointer("/members/subIssues")
12004            .filter(|page| !page.is_null())
12005        else {
12006            return Ok(());
12007        };
12008        if page
12009            .pointer("/pageInfo/hasNextPage")
12010            .and_then(Value::as_bool)
12011            != Some(false)
12012        {
12013            return Ok(());
12014        }
12015        let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
12016            return Ok(());
12017        };
12018        let mut members = Vec::with_capacity(nodes.len());
12019        for node in nodes {
12020            if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
12021                return Ok(());
12022            }
12023            let Some(body) = node.get("body").and_then(Value::as_str) else {
12024                return Ok(());
12025            };
12026            // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
12027            // tasks whatever its marker says, so each is a member the full read would classify.
12028            let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
12029                return Ok(());
12030            };
12031            let Ok(classification) = Classification::from_metadata(&slot) else {
12032                return Ok(());
12033            };
12034            let repositories = if slot.contains_key(Repository::METADATA_KEY) {
12035                let Ok(repositories) = Repository::from_metadata(&slot) else {
12036                    return Ok(());
12037                };
12038                repositories
12039            } else {
12040                let Some(origin) = node
12041                    .pointer("/repository/nameWithOwner")
12042                    .and_then(Value::as_str)
12043                else {
12044                    return Ok(());
12045                };
12046                let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
12047                else {
12048                    return Ok(());
12049                };
12050                vec![own]
12051            };
12052            members.push((classification, repositories));
12053        }
12054        self.member_classes
12055            .lock()
12056            .map_err(|_| SourceError::Unavailable {
12057                message: "a project's members held for its classification were left \
12058                          inconsistent; run the command again"
12059                    .into(),
12060            })?
12061            .insert(project.clone(), members);
12062        Ok(())
12063    }
12064
12065    /// Send every origin held back for the read ahead, beside the members of `members` when it
12066    /// names a project — or nothing, when none is held.
12067    async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
12068        let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
12069            SourceError::Unavailable {
12070                message: "origins held for a read ahead were left inconsistent; run the command \
12071                          again"
12072                    .into(),
12073            }
12074        })?);
12075        if pending.is_empty() {
12076            return Ok(());
12077        }
12078        self.send_origin_lookups(&pending, members).await
12079    }
12080
12081    /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
12082    /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
12083    /// a project — holding each origin's answer and, from a members page that is the whole of
12084    /// them, each member's classification.
12085    async fn send_origin_lookups(
12086        &self,
12087        unasked: &[String],
12088        members: Option<&NativeId>,
12089    ) -> Result<(), SourceError> {
12090        let Some(repository) = self.repository.clone() else {
12091            return Ok(());
12092        };
12093        let mut members = members;
12094        for chunk in unasked.chunks(ORIGIN_BATCH) {
12095            let mut variables = json!({"owner":self.owner,"number":self.project_number,
12096                "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
12097                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
12098                "repositoryOwner":repository.owner,"repositoryName":repository.name});
12099            for slot in 0..ORIGIN_BATCH {
12100                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
12101                variables[format!("filter{slot}")] =
12102                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
12103                variables[format!("search{slot}")] = json!(
12104                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
12105                );
12106                variables[format!("ask{slot}")] = json!(slot < chunk.len());
12107            }
12108            let project = members.take();
12109            let document = match project {
12110                Some(project) => {
12111                    variables["project"] = json!(project.0);
12112                    variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
12113                    graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
12114                }
12115                None => graphql::ORIGIN_LOOKUPS,
12116            };
12117            let data = self.graphql(document, variables).await?;
12118            self.fields_read(&data)?;
12119            if let Some(project) = project {
12120                self.hold_member_classes(project, &data)?;
12121            }
12122            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
12123                let id = required_nonblank_str(node, "id")?.to_owned();
12124                self.repository_cache()?.insert(repository.clone(), id);
12125                if let Some(numeric) = node
12126                    .get("databaseId")
12127                    .and_then(Value::as_u64)
12128                    .and_then(std::num::NonZeroU64::new)
12129                {
12130                    self.numeric_repositories
12131                        .lock()
12132                        .await
12133                        .insert(repository.clone(), numeric);
12134                }
12135            }
12136            for (slot, origin) in chunk.iter().enumerate() {
12137                let items = data
12138                    .pointer(&format!("/o{slot}/projectV2/items"))
12139                    .filter(|value| !value.is_null())
12140                    .ok_or_else(|| SourceError::Refused {
12141                        message: format!(
12142                            "GitHub project {}/{} was not found or is not visible to the token",
12143                            self.owner, self.project_number
12144                        ),
12145                    })?;
12146                let searched =
12147                    data.get(format!("s{slot}"))
12148                        .ok_or_else(|| SourceError::Malformed {
12149                            message: "GitHub search response has no search connection".into(),
12150                        })?;
12151                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
12152                    continue;
12153                }
12154                let mut found: Vec<Resolved> = Vec::new();
12155                for item in optional_nodes(Some(items), "project items")?
12156                    .into_iter()
12157                    .flatten()
12158                {
12159                    if let Some(resolved) = self.resolve(item)?
12160                        && resolved.content_kind == ContentKind::Issue
12161                        && !found.iter().any(|held| held.id == resolved.id)
12162                    {
12163                        found.push(resolved);
12164                    }
12165                }
12166                for node in optional_nodes(Some(searched), "search")?
12167                    .into_iter()
12168                    .flatten()
12169                {
12170                    if let Some(resolved) = self.resolve_issue(node).await?
12171                        && !found.iter().any(|held| held.id == resolved.id)
12172                    {
12173                        found.push(resolved);
12174                    }
12175                }
12176                self.narrowed_cache()?
12177                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
12178            }
12179        }
12180        Ok(())
12181    }
12182
12183    /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
12184    /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
12185    /// confirm, every one of a request refused whole included.
12186    async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
12187        let mut left = Vec::new();
12188        for chunk in ids.chunks(DELETE_BATCH) {
12189            let mut variables = serde_json::Map::new();
12190            for slot in 0..DELETE_BATCH {
12191                let id = chunk.get(slot).unwrap_or(&chunk[0]);
12192                variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
12193                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
12194            }
12195            let data = self
12196                .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
12197                .await
12198                .unwrap_or(Value::Null);
12199            for (slot, id) in chunk.iter().enumerate() {
12200                // Confirmed only by the repository the deleted issue was in, named by a
12201                // non-blank id: anything else says nothing of whether the issue is gone.
12202                let confirmed = data
12203                    .pointer(&format!("/delete{slot}/repository/id"))
12204                    .and_then(Value::as_str)
12205                    .is_some_and(|id| !id.trim().is_empty());
12206                if !confirmed {
12207                    left.push((*id).clone());
12208                }
12209            }
12210        }
12211        left
12212    }
12213
12214    /// Delete one issue, which takes its board item with it.
12215    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
12216        let data = self
12217            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
12218            .await?;
12219        data.pointer("/deleteIssue/repository")
12220            .filter(|value| !value.is_null())
12221            .ok_or_else(|| SourceError::Malformed {
12222                message: "GitHub issue deletion returned no repository".into(),
12223            })?;
12224        self.forget(id)?;
12225        Ok(())
12226    }
12227
12228    /// Remove one item this copy created, so a copy that could not finish leaves the board
12229    /// as it found it.
12230    ///
12231    /// Deleting the issue takes its board item with it, so there is no second mutation to
12232    /// keep in step. An id the board does not hold is not an error: the item is already
12233    /// gone, which is the state this asks for. Which that is, is decided by reading the item
12234    /// by its own id — a listing of the board can still be missing an item it holds, and
12235    /// reading that as *already gone* would leave behind the very item this was asked to
12236    /// take back.
12237    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
12238        let Some(item) = self.bound_item(id).await? else {
12239            return Ok(());
12240        };
12241        if item.content_kind == ContentKind::DraftIssue {
12242            return Err(SourceError::Refused {
12243                message: format!(
12244                    "GitHub item {} is a draft, and this source removes an item by deleting \
12245                     its issue; next: remove it from the board by hand",
12246                    id.0
12247                ),
12248            });
12249        }
12250        let data = self
12251            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
12252            .await?;
12253        data.pointer("/deleteIssue/repository")
12254            .filter(|value| !value.is_null())
12255            .ok_or_else(|| SourceError::Malformed {
12256                message: "GitHub issue deletion returned no repository".into(),
12257            })?;
12258        self.forget(id)?;
12259        Ok(())
12260    }
12261
12262    /// The issue a comment call on `task` is about, or `None` when this board holds no such
12263    /// task.
12264    ///
12265    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
12266    /// read of the task cannot disagree about which ids name one: a project or a document of
12267    /// this board is not a task here either.
12268    ///
12269    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
12270    /// issues and a draft is not one. It is refused rather than answered with an empty page,
12271    /// which would read as a task nobody has commented on yet.
12272    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
12273        let cached = self.resolved_cache()?.get(task).cloned();
12274        let Some(item) = (match cached {
12275            Some(item) => Some(item),
12276            None => self.item_by_id(task).await?,
12277        })
12278        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
12279            return Ok(None);
12280        };
12281        if item.content_kind == ContentKind::DraftIssue {
12282            return Err(self.draft_has_no_comments(task));
12283        }
12284        Ok(Some(item.id))
12285    }
12286
12287    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
12288    /// issues, and a draft is not one.
12289    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
12290        SourceError::Refused {
12291            message: format!(
12292                "task {} of source {} is a draft item on the board, and GitHub keeps \
12293                 comments on issues alone, so a draft has none to read or write; next: \
12294                 convert the draft to an issue on the board, then comment on the issue it \
12295                 becomes",
12296                task.0, self.name
12297            ),
12298        }
12299    }
12300
12301    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
12302    /// request — or `None` when this board holds no task by that id.
12303    ///
12304    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
12305    /// is answered with the draft and the refusal, at the price of the draft's own read.
12306    async fn issue_detail(
12307        &self,
12308        id: &NativeId,
12309        page: &PageRequest,
12310    ) -> Result<Option<TaskDetailRead>, SourceError> {
12311        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
12312        let asked = self
12313            .graphql(
12314                graphql::ISSUE_DETAIL,
12315                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
12316                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
12317                       "duplicates":true}),
12318            )
12319            .await;
12320        let data = match asked {
12321            Ok(data) => data,
12322            Err(error) if unresolvable_node(&error) => return Ok(None),
12323            Err(error) => return Err(error),
12324        };
12325        // `node` is null for an id that names nothing, and absent only from an answer this
12326        // source cannot read — never the same thing.
12327        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
12328            message: format!("GitHub answered the read of {} with no node", id.0),
12329        })?;
12330        self.detail_of(id, node, true, after).await
12331    }
12332
12333    /// Several tasks, each with the first page of its comments when `comments` is set, read
12334    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
12335    /// order.
12336    ///
12337    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
12338    /// one item at a time, so that id is answered as missing and the others as themselves; any
12339    /// other refusal is every id of that batch's answer.
12340    async fn issue_details(
12341        &self,
12342        ids: &[NativeId],
12343        comments: Option<&PageRequest>,
12344    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
12345        let mut read = Vec::with_capacity(ids.len());
12346        for batch in ids.chunks(DETAIL_BATCH) {
12347            match self
12348                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
12349                .await
12350            {
12351                Ok(data) => {
12352                    for (slot, id) in batch.iter().enumerate() {
12353                        // Every alias asked for is answered, null for an id naming nothing;
12354                        // one missing is an answer this source cannot read.
12355                        let read_one = match data.get(format!("i{slot}")) {
12356                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
12357                            None => Err(SourceError::Malformed {
12358                                message: format!(
12359                                    "GitHub answered a batch read with no item for {}",
12360                                    id.0
12361                                ),
12362                            }),
12363                        };
12364                        read.push(read_one);
12365                    }
12366                }
12367                Err(error) if unresolvable_node(&error) => {
12368                    for id in batch {
12369                        read.push(match comments {
12370                            Some(page) => self.issue_detail(id, page).await,
12371                            None => self.task_read(id).await,
12372                        });
12373                    }
12374                }
12375                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
12376            }
12377        }
12378        read
12379    }
12380
12381    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
12382    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
12383        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
12384            task,
12385            comments: None,
12386        }))
12387    }
12388
12389    /// What one node a detail read reached says: the task this board holds by `id`, with the
12390    /// page of comments the node carries when `commented` — or `None` for a node that is no
12391    /// task of this board.
12392    ///
12393    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
12394    /// and an item this process created answers from this process's own record, which a node
12395    /// read taken moments after the write can still be behind.
12396    async fn detail_of(
12397        &self,
12398        id: &NativeId,
12399        node: &Value,
12400        commented: bool,
12401        after: Option<&str>,
12402    ) -> Result<Option<TaskDetailRead>, SourceError> {
12403        if node.is_null() {
12404            return Ok(None);
12405        }
12406        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
12407        // An issue answered under one id is that id's, or the answer is not one this source
12408        // can report: reporting another issue's task and comments under the qualified id asked
12409        // for would be the one wrong answer here. A draft's own read checks the same.
12410        if !draft
12411            && optional_str(node, "__typename")? == Some("Issue")
12412            && required_str(node, "id")? != id.0
12413        {
12414            return Err(SourceError::Malformed {
12415                message: format!(
12416                    "GitHub answered the read of {} with issue {}",
12417                    id.0,
12418                    required_str(node, "id")?
12419                ),
12420            });
12421        }
12422        let item = if draft {
12423            self.draft_by_id(id).await?
12424        } else {
12425            self.resolve_issue(node).await?
12426        };
12427        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
12428        // for this command, so the dependency read the copy makes next is answered from it.
12429        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
12430            self.resolved_cache()?.insert(read.id.clone(), read.clone());
12431        }
12432        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
12433            return Ok(None);
12434        };
12435        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
12436        let task = own.unwrap_or(item).task()?;
12437        let comments = match (commented, draft) {
12438            (false, _) => None,
12439            (true, true) => Some(Err(self.draft_has_no_comments(id))),
12440            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
12441        };
12442        if commented && !draft && after.is_none() {
12443            self.expected.learn_comments(id, node);
12444        }
12445        Ok(Some(TaskDetailRead { task, comments }))
12446    }
12447
12448    /// Whether the comment `comment` is one of `issue`'s own.
12449    ///
12450    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
12451    /// comment's id and nothing else: a comment id given against the wrong task would
12452    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
12453    /// names something that is not an issue comment, is a comment this task does not have —
12454    /// which is what GitHub refusing to resolve it means too.
12455    async fn comment_is_on(
12456        &self,
12457        issue: &NativeId,
12458        comment: &NativeId,
12459    ) -> Result<bool, SourceError> {
12460        Ok(self.comment_on(issue, comment).await?.is_some())
12461    }
12462
12463    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
12464    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
12465    async fn comment_on(
12466        &self,
12467        issue: &NativeId,
12468        comment: &NativeId,
12469    ) -> Result<Option<String>, SourceError> {
12470        // A write the store's cache expects knows which comments its issue held, and their
12471        // bodies, at the version it expects: what this read would answer.
12472        if let Some(primed) = self.expected.primed_comment(issue, comment) {
12473            return Ok(primed);
12474        }
12475        let asked = self
12476            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
12477            .await;
12478        let data = match asked {
12479            Ok(data) => data,
12480            Err(error) if unresolvable_node(&error) => return Ok(None),
12481            Err(error) => return Err(error),
12482        };
12483        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
12484            return Ok(None);
12485        };
12486        if optional_str(node, "__typename")? != Some("IssueComment") {
12487            return Ok(None);
12488        }
12489        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
12490            message: format!("GitHub issue comment {} names no issue", comment.0),
12491        })?;
12492        if required_str(on, "id")? != issue.0 {
12493            return Ok(None);
12494        }
12495        Ok(Some(required_str(node, "body")?.to_owned()))
12496    }
12497
12498    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
12499    async fn partition_edges(
12500        &self,
12501        near_kind: BoardKind,
12502        near_content: ContentKind,
12503        carried: Option<&[Value]>,
12504        depends_on: &[DependencyEdge],
12505    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
12506        let mut native = Vec::new();
12507        let mut fallback = Vec::new();
12508        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
12509            .iter()
12510            .map(|edge| {
12511                // An opaque far end names another tracker, so it is never this board's own.
12512                let same_source = edge.to.designator().is_none()
12513                    && edge
12514                        .to
12515                        .source()
12516                        .is_none_or(|source| source == self.name.as_str());
12517                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
12518                // `DependencyEndpoint::source` both read it that way — and a native id may hold
12519                // colons of its own, so the far end is everything after that one separator.
12520                // Splitting at the last would truncate `work:urn:task:7` to `7`.
12521                let far_id = if edge.to.is_qualified() {
12522                    edge.to
12523                        .id()
12524                        .split_once(':')
12525                        .map_or(edge.to.id(), |(_, native)| native)
12526                } else {
12527                    edge.to.id()
12528                };
12529                // One that already blocks the near issue was answered by that issue's own
12530                // read, which carried each of its blockers' kinds — an issue every one — so it
12531                // is not read again.
12532                let blocking = carried.and_then(|nodes| {
12533                    nodes
12534                        .iter()
12535                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
12536                });
12537                (edge, far_id, same_source, blocking)
12538            })
12539            .collect();
12540        // Every other same-source far end is read by its own id, exactly as the item it is a
12541        // far end of is: whether this board holds it is that read's answer, never a listing's.
12542        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
12543        let mut unread: Vec<NativeId> = Vec::new();
12544        for (_, far_id, same_source, blocking) in &far_ends {
12545            let id = NativeId((*far_id).to_owned());
12546            if *same_source && blocking.is_none() && !unread.contains(&id) {
12547                unread.push(id);
12548            }
12549        }
12550        let read: BTreeMap<NativeId, Option<Resolved>> = unread
12551            .iter()
12552            .cloned()
12553            .zip(self.items_by_ids(&unread).await?)
12554            .collect();
12555        for (edge, far_id, same_source, blocking) in far_ends {
12556            let far = match (same_source, blocking) {
12557                (false, _) => None,
12558                (true, Some(node)) => Some(FarEnd {
12559                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
12560                        BoardKind::Document
12561                    } else {
12562                        BoardKind::Work(related_kind(node, self.sub_issues)?)
12563                    },
12564                    content_kind: ContentKind::Issue,
12565                }),
12566                (true, None) => {
12567                    let read = read
12568                        .get(&NativeId(far_id.to_owned()))
12569                        .cloned()
12570                        .flatten()
12571                        .ok_or_else(|| SourceError::Refused {
12572                            message: format!("GitHub dependency item {far_id} was not found"),
12573                        })?;
12574                    Some(FarEnd {
12575                        kind: read.kind,
12576                        content_kind: read.content_kind,
12577                    })
12578                }
12579            };
12580            let far = far.as_ref();
12581            // The caller says which kind the far end is, and this board holds the far end
12582            // itself, so a disagreement is settled here rather than stored: recorded, the
12583            // wrong kind would read back as a cross-level edge that never existed; written
12584            // natively, it would name a relationship of a different level than the caller
12585            // asked for.
12586            //
12587            // A far end this board holds as a *document* fails the same comparison and is
12588            // refused by the same sentence: `ItemKind` has no document variant because
12589            // nothing may point at one, so no caller can name it correctly and the refusal
12590            // is the only honest answer.
12591            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
12592                return Err(SourceError::Refused {
12593                    message: format!(
12594                        "GitHub dependency item {far_id} is a {} of this board, and this item \
12595                         names it as a {}; record the kind it is",
12596                        disagreeing.kind.describes(),
12597                        edge.to.kind.marker()
12598                    ),
12599                });
12600            }
12601            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
12602            // however the far end is spelled — and one classified native here would be
12603            // written nowhere at all, because a draft's native reconciliation never runs.
12604            let native_here = near_content == ContentKind::Issue
12605                && far.is_some_and(|far| {
12606                    far.content_kind == ContentKind::Issue
12607                        && BoardKind::Work(edge.to.kind) == near_kind
12608                });
12609            if native_here {
12610                native.push(far_id.to_owned());
12611            } else {
12612                fallback.push(edge.clone());
12613            }
12614        }
12615        Ok((native, fallback))
12616    }
12617
12618    async fn update_existing(
12619        &self,
12620        item: &Resolved,
12621        incoming: &Incoming<'_>,
12622        body: &Option<String>,
12623        status_target: Option<&StatusTarget>,
12624    ) -> Result<(), SourceError> {
12625        let title = incoming.written_title();
12626        // A terminal status closes the issue here, in the same mutation as its body: its board
12627        // option was selected before this, so a close never lands on an item whose board cannot
12628        // show it.
12629        let mut fields = match item.content_kind {
12630            ContentKind::DraftIssue => json!({"title":title,"body":body}),
12631            ContentKind::Issue => json!({"title":title,"body":body,
12632                                         "stateInput":state_input(status_target)}),
12633        };
12634        // The people it is to be assigned to, replacing whoever it holds — sent only when they
12635        // are not exactly those, and looked up before anything of this write was sent.
12636        if let Some(people) = incoming.assignees
12637            && item
12638                .assignees
12639                .as_deref()
12640                .is_none_or(|held| !assignees::same_people(held, people))
12641        {
12642            if item.content_kind == ContentKind::DraftIssue {
12643                return Err(self.assigns_a_draft());
12644            }
12645            fields["assigneeIds"] = json!(self.people_ids(people, &item.written_name())?);
12646        }
12647        // Only the members whose value differs from the item as this write read it are sent,
12648        // and nothing at all when none does — whether or not the store's cache expects a
12649        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
12650        // of it since that read is kept rather than overwritten with what it already held,
12651        // and a re-copy of an unchanged item costs no mutation.
12652        if let Some(members) = fields.as_object_mut() {
12653            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
12654                members.remove("title");
12655            }
12656            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
12657                (Some(Value::String(sent)), Some(held)) => sent == held,
12658                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
12659                _ => false,
12660            };
12661            if body_unchanged {
12662                members.remove("body");
12663            }
12664            // An issue already in the state the target asks for keeps it: an open one stays
12665            // open, and a closed one whose closure reads as the target's category keeps its
12666            // reason — a duplicate's says more than this write would.
12667            let unchanged_state = match status_target {
12668                Some(StatusTarget::Terminal(_, reason)) => {
12669                    item.closed
12670                        && closed_category(item.reason.as_deref())
12671                            == closed_category(Some(reason.reason()))
12672                }
12673                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
12674                None => true,
12675            };
12676            if unchanged_state {
12677                members.remove("stateInput");
12678            }
12679            if members.is_empty() {
12680                return Ok(());
12681            }
12682        }
12683        self.update_content(item.content_kind, &item.id, fields)
12684            .await
12685    }
12686
12687    /// Update one board item's content with exactly `fields` beside its id, through the
12688    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
12689    /// a draft.
12690    ///
12691    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
12692    /// is what lets a narrow write carry the one thing it changes and nothing else.
12693    async fn update_content(
12694        &self,
12695        kind: ContentKind,
12696        id: &NativeId,
12697        fields: Value,
12698    ) -> Result<(), SourceError> {
12699        let (operation, id_key, pointer) = match kind {
12700            ContentKind::DraftIssue => (
12701                graphql::UPDATE_DRAFT,
12702                "draftIssueId",
12703                "/updateProjectV2DraftIssue/draftIssue",
12704            ),
12705            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
12706        };
12707        if kind == ContentKind::Issue && self.hold_issue(&fields)? {
12708            return Ok(());
12709        }
12710        if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
12711            return Ok(());
12712        }
12713        let mut input = fields;
12714        input[id_key] = json!(id.0);
12715        self.send_update(operation, pointer, id, input).await
12716    }
12717
12718    /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
12719    /// to naming that item, `id`.
12720    async fn send_update(
12721        &self,
12722        operation: &str,
12723        pointer: &str,
12724        id: &NativeId,
12725        input: Value,
12726    ) -> Result<(), SourceError> {
12727        let data = match self.graphql(operation, json!({"input":&input})).await {
12728            Ok(data) => data,
12729            Err(error) => return Err(self.refusing_people(&input, error)),
12730        };
12731        let returned = data
12732            .pointer(pointer)
12733            .ok_or_else(|| SourceError::Malformed {
12734                message: "GitHub item update returned no item".into(),
12735            })?;
12736        if required_str(returned, "id")? != id.0 {
12737            return Err(SourceError::Malformed {
12738                message: "GitHub item update returned the wrong item".into(),
12739            });
12740        }
12741        self.assigned_as_asked(&input, Some(returned))
12742    }
12743
12744    /// Creates one issue, files it on the board, and reports what a read of it would say:
12745    /// its content id, its board item id, and the web address GitHub gave it.
12746    ///
12747    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
12748    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
12749    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
12750    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
12751    /// "Content already exists in this project". A terminal status is not written here:
12752    /// `finish_write` selects its option first and closes the issue after, so a close never
12753    /// lands on an item whose board cannot show it.
12754    ///
12755    /// The address and the number come back here because this is the only place either is
12756    /// known before GitHub's own board read catches up — an item this run created answers
12757    /// the reads that follow it out of the record below, and one remembered without them
12758    /// would report no location and no key for the rest of the run.
12759    async fn create_and_file_issue(
12760        &self,
12761        board_id: &str,
12762        repository: &RepositoryTarget,
12763        incoming: &Incoming<'_>,
12764        body: &Option<String>,
12765    ) -> Result<Landed, SourceError> {
12766        let repository_id = self.repository_id(repository, incoming).await?;
12767        let mut input = json!({
12768            "repositoryId":repository_id,"title":incoming.written_title(),"body":body
12769        });
12770        self.assigning(&mut input, incoming)?;
12771        let data = match self
12772            .graphql(graphql::CREATE_ISSUE, json!({"input": &input}))
12773            .await
12774        {
12775            Ok(data) => data,
12776            Err(error) => return Err(self.refusing_people(&input, error)),
12777        };
12778        let created = data
12779            .pointer("/createIssue/issue")
12780            .filter(|value| !value.is_null())
12781            .ok_or_else(|| SourceError::Malformed {
12782                message: "GitHub issue creation returned no issue".into(),
12783            })?;
12784        let content_id = NativeId(required_str(created, "id")?.to_owned());
12785        // Assigned otherwise than asked, the issue is refused and taken back as the filed
12786        // create's is, a refused delete answered beside the refusal.
12787        if let Err(error) = self.assigned_as_asked(&input, Some(created)) {
12788            return Err(self.taken_back(&content_id, created, error).await);
12789        }
12790        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
12791        // a response without it is not worth failing a landed write over — the item simply
12792        // reports no location until the board read catches up, which is what it did before.
12793        let url = optional_str(created, "url")?.map(str::to_owned);
12794        // The issue exists from here on, so an unreadable number and a refused board
12795        // filing below each try, best effort, to take it back: an issue in the repository
12796        // that is on no board is an item nobody asked for and nothing here would find again.
12797        //
12798        // Its number is optional on the same terms its address is — a landed write is not
12799        // worth failing over a member that came back missing, and such an item reports no
12800        // handle until a board read catches up. A number that is *present* and is not an
12801        // unsigned integer is still a response this source cannot read.
12802        let number = match created_issue_number(created) {
12803            Ok(number) => number,
12804            Err(error) => {
12805                let _ = self.delete_issue(&content_id).await;
12806                return Err(error);
12807            }
12808        };
12809        let added = match self
12810            .graphql(
12811                graphql::ADD_TO_BOARD,
12812                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
12813            )
12814            .await
12815        {
12816            Ok(added) => added,
12817            Err(error) => {
12818                let _ = self.delete_issue(&content_id).await;
12819                return Err(error);
12820            }
12821        };
12822        let item = added
12823            .pointer("/addProjectV2ItemById/item")
12824            .filter(|value| !value.is_null())
12825            .ok_or_else(|| SourceError::Malformed {
12826                message: "GitHub board addition returned no project item".into(),
12827            })?;
12828        self.note_written(&content_id, url.as_deref());
12829        Ok(Landed {
12830            content_id,
12831            item_id: Some(required_str(item, "id")?.to_owned()),
12832            url,
12833            number,
12834        })
12835    }
12836
12837    /// Move one issue under the project it now belongs to, or out of the one it left.
12838    async fn reparent(
12839        &self,
12840        held: Option<NativeId>,
12841        child: &NativeId,
12842        wanted: Option<&NativeId>,
12843    ) -> Result<(), SourceError> {
12844        if held.as_ref() == wanted {
12845            return Ok(());
12846        }
12847        if let Some(held) = &held {
12848            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
12849                .await?;
12850        }
12851        if let Some(wanted) = wanted {
12852            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
12853                .await?;
12854        }
12855        Ok(())
12856    }
12857
12858    async fn sub_issue(
12859        &self,
12860        operation: &str,
12861        parent: &NativeId,
12862        child: &NativeId,
12863        root: &str,
12864    ) -> Result<(), SourceError> {
12865        let data = self
12866            .graphql(
12867                operation,
12868                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
12869            )
12870            .await?;
12871        let issue =
12872            data.pointer(&format!("/{root}/issue"))
12873                .ok_or_else(|| SourceError::Malformed {
12874                    message: "GitHub sub-issue update returned no issue".into(),
12875                })?;
12876        let sub =
12877            data.pointer(&format!("/{root}/subIssue"))
12878                .ok_or_else(|| SourceError::Malformed {
12879                    message: "GitHub sub-issue update returned no sub-issue".into(),
12880                })?;
12881        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
12882            return Err(SourceError::Malformed {
12883                message: "GitHub sub-issue update returned the wrong issues".into(),
12884            });
12885        }
12886        Ok(())
12887    }
12888
12889    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
12890    /// whether there was one.
12891    ///
12892    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
12893    /// relationships are not read: there is nothing a read of them could find.
12894    async fn reconcile_blocked_by(
12895        &self,
12896        content_id: &NativeId,
12897        native: &[String],
12898        issue: Issue<'_>,
12899    ) -> Result<bool, SourceError> {
12900        let current = match issue {
12901            Issue::Created => Vec::new(),
12902            Issue::Existing(Some(held)) => held
12903                .iter()
12904                .map(|far| required_str(far, "id").map(str::to_owned))
12905                .collect::<Result<Vec<_>, _>>()?,
12906            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
12907        };
12908        let mut changed = false;
12909        for (operation, far_id) in current
12910            .iter()
12911            .filter(|id| !native.contains(id))
12912            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
12913            .chain(
12914                native
12915                    .iter()
12916                    .filter(|id| !current.contains(id))
12917                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
12918            )
12919        {
12920            let data = self
12921                .graphql(
12922                    operation,
12923                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
12924                )
12925                .await?;
12926            let root = if operation == graphql::ADD_BLOCKED_BY {
12927                "addBlockedBy"
12928            } else {
12929                "removeBlockedBy"
12930            };
12931            let issue =
12932                data.pointer(&format!("/{root}/issue"))
12933                    .ok_or_else(|| SourceError::Malformed {
12934                        message: "GitHub dependency update returned no issue".into(),
12935                    })?;
12936            let blocker = data
12937                .pointer(&format!("/{root}/blockingIssue"))
12938                .ok_or_else(|| SourceError::Malformed {
12939                    message: "GitHub dependency update returned no blocking issue".into(),
12940                })?;
12941            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
12942            {
12943                return Err(SourceError::Malformed {
12944                    message: "GitHub dependency update returned the wrong issues".into(),
12945                });
12946            }
12947            changed = true;
12948        }
12949        Ok(changed)
12950    }
12951}
12952
12953/// The board field values one write sends, in the order GitHub runs them, and the fields it
12954/// clears after them: the origin field when it moves, the `Status` option `column` names, the
12955/// priority, then each projected field.
12956fn field_writes<'a>(
12957    existing: Option<&Resolved>,
12958    origin_field: Option<&str>,
12959    origin: &str,
12960    column: Option<(String, String)>,
12961    priority: Option<&'a PriorityWrite>,
12962    projections: &'a [ProjectionWrite],
12963) -> (Vec<(String, Value)>, Vec<&'a str>) {
12964    let mut fields = Vec::new();
12965    if let Some(field_id) = origin_field
12966        && existing.map_or(!origin.is_empty(), |item| {
12967            item.origin.as_deref().unwrap_or("") != origin
12968        })
12969    {
12970        fields.push((field_id.to_owned(), json!({"text":origin})));
12971    }
12972    if let Some((field_id, option_id)) = column {
12973        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
12974    }
12975    let mut clears = Vec::new();
12976    match priority {
12977        Some(PriorityWrite::Select { field, option }) => {
12978            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
12979        }
12980        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
12981        None => {}
12982    }
12983    for projection in projections {
12984        projection.join(&mut fields, &mut clears);
12985    }
12986    (fields, clears)
12987}
12988
12989/// Everything one write decided before sending anything: see
12990/// [`GitHubProjectsSource::prepare_write`].
12991struct Prepared {
12992    /// What the write lands on.
12993    destination: WriteDestination,
12994    board: BoardFields,
12995    status_target: Option<StatusTarget>,
12996    /// The `Status` field, the option and the option's name the status lands on.
12997    // 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.
12998    column: Option<(String, String, String)>,
12999    priority_write: Option<PriorityWrite>,
13000    projections: Vec<ProjectionWrite>,
13001    projected: BTreeMap<String, String>,
13002    content_kind: ContentKind,
13003    own_repository: Option<Repository>,
13004    /// The far ends of the item's native blocked-by relationships.
13005    // 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.
13006    native: Vec<String>,
13007    slot: BTreeMap<String, Value>,
13008    content: Option<String>,
13009    body: Option<String>,
13010    /// The copy origin the item records, empty for none.
13011    // 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.
13012    origin: String,
13013    origin_field: Option<String>,
13014}
13015
13016/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
13017/// edge, each its input.
13018enum HeldWrite<'a> {
13019    Field(&'a Value),
13020    Edge(&'a Value),
13021}
13022
13023/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
13024/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
13025/// it, rather than answering with the item it holds.
13026fn already_on_the_board(error: &SourceError) -> bool {
13027    error
13028        .to_string()
13029        .contains("Content already exists in this project")
13030}
13031
13032/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
13033/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
13034/// item.
13035fn check_aliased_answers(
13036    document: &str,
13037    data: &Value,
13038    prefix: &str,
13039    inputs: &[&Value],
13040    answers: &[(&str, &str)],
13041) -> Result<(), SourceError> {
13042    for (slot, input) in inputs.iter().enumerate() {
13043        for (root, named) in answers {
13044            let answered = data
13045                .pointer(&format!("/{prefix}{slot}/{root}/id"))
13046                .and_then(Value::as_str)
13047                .ok_or_else(|| SourceError::Malformed {
13048                    message: format!(
13049                        "GitHub answered {} with nothing at {prefix}{slot}",
13050                        operation_description(document)
13051                    ),
13052                })?;
13053            let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
13054            if answered != asked {
13055                return Err(SourceError::Malformed {
13056                    message: format!(
13057                        "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
13058                         {asked}, the item it was asked to write",
13059                        operation_description(document)
13060                    ),
13061                });
13062            }
13063        }
13064    }
13065    Ok(())
13066}
13067
13068/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
13069/// back — each issue of `left` it created that is still in the repository, which its caller,
13070/// never handed their ids, could not name.
13071fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
13072    if left.is_empty() {
13073        return error;
13074    }
13075    SourceError::Refused {
13076        message: format!(
13077            "{error}; GitHub then refused to take back {}, which this write created and which \
13078             are still in the repository; next: delete {} before writing again",
13079            left.join(", "),
13080            if left.len() == 1 { "it" } else { "them" }
13081        ),
13082    }
13083}
13084
13085/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
13086enum WriteDestination {
13087    /// The item as it stood, for an update.
13088    Existing(Box<Resolved>),
13089    /// Where a new item is created.
13090    New(RepositoryTarget),
13091}
13092
13093impl WriteDestination {
13094    /// The item an update writes over, as it stood; `None` for a create.
13095    fn existing(&self) -> Option<&Resolved> {
13096        match self {
13097            Self::Existing(item) => Some(item),
13098            Self::New(_) => None,
13099        }
13100    }
13101}
13102
13103/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
13104/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
13105/// it writes beside that issue's id, which only [`Self::new`] sets.
13106struct HeldUpdate {
13107    issue: NativeId,
13108    // 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.
13109    input: Value,
13110}
13111
13112impl HeldUpdate {
13113    /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
13114    fn new(issue: &NativeId, members: &Value) -> Self {
13115        let mut input = members.clone();
13116        input["id"] = json!(issue.0);
13117        Self {
13118            issue: issue.clone(),
13119            input,
13120        }
13121    }
13122}
13123
13124/// What a copy has held back of the items it created; see
13125/// [`GitHubProjectsSource::hold_back`].
13126#[derive(Debug, Default)]
13127enum Deferred {
13128    /// Writes are being held back, and nothing has been held yet.
13129    #[default]
13130    Nothing,
13131    /// The items held back, every one of them on `board`.
13132    Held {
13133        /// The board every one of them is on.
13134        board: BoardId,
13135        /// Each created item, in the order it was created.
13136        created: Vec<HeldBack>,
13137    },
13138}
13139
13140impl Deferred {
13141    /// Whether `content` is one of the items held back.
13142    fn holds(&self, content: &NativeId) -> bool {
13143        match self {
13144            Self::Nothing => false,
13145            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
13146        }
13147    }
13148}
13149
13150/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
13151#[derive(Debug)]
13152struct HeldBack {
13153    /// The issue.
13154    content: NativeId,
13155    /// Its board field values other than its `Status`, in the order they are written.
13156    fields: Vec<(String, Value)>,
13157    /// The `Status` field, and the option and its name that the item's status lands on.
13158    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
13159    status: Option<(String, String, String)>,
13160    /// The fields it clears after those are written.
13161    clears: Vec<String>,
13162    /// The reason it is closed for, once its option is selected; `None` leaves it open.
13163    close: Option<ClosedState>,
13164    /// The issues it is blocked by.
13165    edges: Vec<NativeId>,
13166}
13167
13168/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
13169/// no GitHub type has a field of this name.
13170const ANSWER_ERRORS: &str = "onetaskgraphErrors";
13171
13172/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
13173/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
13174///
13175/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
13176/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
13177/// created named in the data beside it — the ids a copy needs to take them back.
13178fn answer_with_errors(body: &str) -> Option<Value> {
13179    let parsed: Value = serde_json::from_str(body).ok()?;
13180    let errors = parsed
13181        .get("errors")?
13182        .as_array()
13183        .filter(|errors| !errors.is_empty())?;
13184    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
13185    data[ANSWER_ERRORS] = Value::Array(errors.clone());
13186    Some(data)
13187}
13188
13189/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
13190/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
13191fn filed_input(
13192    repository: &str,
13193    board: &str,
13194    incoming: &Incoming<'_>,
13195    body: &Option<String>,
13196) -> Value {
13197    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
13198                           "body":body,"projectV2Ids":[board]});
13199    if let Some(parent) = incoming.parent {
13200        input["parentIssueId"] = json!(parent.0);
13201    }
13202    input
13203}
13204
13205/// The name of option `option` of field `field` among a board's `fields`.
13206fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
13207    fields
13208        .get("nodes")?
13209        .as_array()?
13210        .iter()
13211        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
13212        .get("options")?
13213        .as_array()?
13214        .iter()
13215        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
13216        .get("name")?
13217        .as_str()
13218}
13219
13220/// What a write needs to know of one far end it names: which kind of item it is, and whether
13221/// it is an issue a native relationship can name.
13222struct FarEnd {
13223    kind: BoardKind,
13224    content_kind: ContentKind,
13225}
13226
13227/// Whether the issue one write reconciles was created by that write or was already there.
13228#[derive(Clone, Copy, PartialEq, Eq)]
13229enum Issue<'a> {
13230    /// Created by this write, so it holds no relationships yet.
13231    Created,
13232    /// On the board before this write, holding whatever relationships it holds — the far
13233    /// ends of its whole `blockedBy`, when the read that reached it carried them.
13234    Existing(Option<&'a [Value]>),
13235}
13236
13237/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
13238enum Reached {
13239    /// An issue this board holds, resolved into everything this source reports about it.
13240    Held(Box<Resolved>),
13241    /// Nothing this board holds: no such node, or a node on some other board.
13242    Nothing,
13243    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
13244    /// again by [`GitHubProjectsSource::draft_by_id`].
13245    Draft,
13246}
13247
13248/// What GitHub says when a string is not a node id it can resolve.
13249///
13250/// Matched because it is the ordinary answer to a project selector naming a project by its
13251/// *name*, and reporting that as a failure would make naming one impossible. It is read
13252/// off the refusal GitHub sent, never guessed from the shape of the string: this source
13253/// does not define the syntax of a GitHub node id and would be wrong about it.
13254const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
13255
13256/// `error` with `note` added to the end of what it says, its kind and every other member
13257/// unchanged — so a caller still branches on the failure that happened, and reads beside it
13258/// what that failure left behind.
13259fn noting(error: SourceError, note: &str) -> SourceError {
13260    match error {
13261        SourceError::Config { message } => SourceError::Config {
13262            message: message + note,
13263        },
13264        SourceError::Auth { message } => SourceError::Auth {
13265            message: message + note,
13266        },
13267        SourceError::Refused { message } => SourceError::Refused {
13268            message: message + note,
13269        },
13270        SourceError::RateLimited {
13271            retry_after_seconds,
13272            message,
13273        } => SourceError::RateLimited {
13274            retry_after_seconds,
13275            message: Some(message.unwrap_or_default() + note),
13276        },
13277        SourceError::Unavailable { message } => SourceError::Unavailable {
13278            message: message + note,
13279        },
13280        SourceError::Malformed { message } => SourceError::Malformed {
13281            message: message + note,
13282        },
13283    }
13284}
13285
13286/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
13287/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
13288fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
13289    let mut variables = serde_json::Map::new();
13290    for slot in 0..COPY_DETAIL_BATCH {
13291        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
13292        variables.insert(format!("id{slot}"), json!(id));
13293    }
13294    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
13295    variables.insert("children".to_owned(), json!(children));
13296    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
13297    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
13298    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
13299    variables.insert("duplicates".to_owned(), json!(true));
13300    Value::Object(variables)
13301}
13302
13303/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
13304/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
13305/// for them.
13306///
13307/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
13308/// is read again at no added price.
13309fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
13310    let mut variables = serde_json::Map::new();
13311    for slot in 0..DETAIL_BATCH {
13312        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
13313        variables.insert(format!("id{slot}"), json!(id));
13314    }
13315    variables.insert(
13316        "first".to_owned(),
13317        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
13318    );
13319    variables.insert("comments".to_owned(), json!(comments.is_some()));
13320    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
13321    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
13322    variables.insert("duplicates".to_owned(), json!(true));
13323    Value::Object(variables)
13324}
13325
13326/// Whether this refusal is GitHub saying the id names no node at all.
13327fn unresolvable_node(error: &SourceError) -> bool {
13328    matches!(error, SourceError::Refused { message }
13329        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
13330}
13331
13332/// One project name, as a search qualifier which filters on it at the server.
13333///
13334/// Quoted so the whole title is one phrase rather than a bag of words, with the two
13335/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
13336/// the way it documents. A title matched here is still compared for equality afterwards:
13337/// the qualifier narrows what the server sends, and this source decides what it names.
13338fn title_qualifier(name: &str) -> String {
13339    format!("in:title {}", quoted(name))
13340}
13341
13342/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
13343/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
13344/// it documents — so a value holding a qualifier's spelling is searched for rather than
13345/// obeyed.
13346fn quoted(value: &str) -> String {
13347    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
13348    format!("\"{escaped}\"")
13349}
13350
13351/// The search qualifier for the issues updated at or after `since`.
13352///
13353/// Written to the second, rounded down, which can only widen what the search returns.
13354fn updated_qualifier(since: DateTime<Utc>) -> String {
13355    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
13356}
13357
13358/// The search terms that narrow a board-scoped issue search to a task query's text and
13359/// metadata predicates, or `None` when it carries neither.
13360///
13361/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
13362/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
13363/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
13364/// matches each in any field the `in:` qualifier names, so a query naming a title search and
13365/// a metadata value searches both fields for both — wider than asked, never narrower, and
13366/// every candidate is confirmed in process afterwards.
13367///
13368/// **This narrows a text search, and that is this source's declared semantics.** GitHub
13369/// matches whole tokens where a substring rule would match inside a word, so an item holding
13370/// the text only inside a longer word is not returned. A text of nothing but whitespace
13371/// matches every item, so it narrows nothing and is not sent.
13372fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
13373    let text = query
13374        .text
13375        .as_ref()
13376        .filter(|text| !text.terms.trim().is_empty());
13377    if text.is_none() && query.metadata.is_empty() {
13378        return None;
13379    }
13380    let (title, body) = match text.map(|text| text.fields) {
13381        None => (false, true),
13382        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
13383        Some(TextFields::Content) => (false, true),
13384        Some(TextFields::TitleOrContent) => (true, true),
13385    };
13386    let fields = match (title, body) {
13387        (true, true) => "in:title,body",
13388        (true, false) => "in:title",
13389        _ => "in:body",
13390    };
13391    let phrases = text
13392        .map(|text| text.terms.clone())
13393        .into_iter()
13394        .chain(
13395            query
13396                .metadata
13397                .iter()
13398                .map(|wanted| as_stored(wanted.value())),
13399        )
13400        .map(|phrase| quoted(&phrase))
13401        .collect::<Vec<_>>();
13402    Some(format!("{fields} {}", phrases.join(" ")))
13403}
13404
13405/// Whether every status category `query` keeps reads only off an open issue or a draft: a
13406/// closed issue reads `done`, `cancelled` or — closed for a reason that implies no category —
13407/// `unknown`, so a status filter naming none of those three can ask every issue search it sends
13408/// for open issues alone; each candidate's category is still confirmed from its own item.
13409fn open_only(query: &TaskQuery) -> bool {
13410    !query.statuses.is_empty()
13411        && query.statuses.iter().all(|category| {
13412            !matches!(
13413                category,
13414                StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13415            )
13416        })
13417}
13418
13419/// What a task query is narrowed by beside a board-scoped issue search's scope — its comment
13420/// activity and its text and metadata, with `is:open` when [`open_only`] — or `None` for a query
13421/// carrying none of those, which no narrowed search answers.
13422///
13423/// A narrowed search never answers a draft, which is no issue, so narrowing it to open issues
13424/// too leaves out nothing such a search could have answered.
13425fn search_qualifiers(query: &TaskQuery) -> Option<String> {
13426    let mut qualifiers: Vec<String> = [
13427        query.commented_since.map(updated_qualifier),
13428        narrowing_qualifiers(query),
13429    ]
13430    .into_iter()
13431    .flatten()
13432    .collect();
13433    if qualifiers.is_empty() {
13434        return None;
13435    }
13436    if open_only(query) {
13437        qualifiers.push(OPEN_QUALIFIER.to_owned());
13438    }
13439    Some(qualifiers.join(" "))
13440}
13441
13442/// The search terms that narrow a board-scoped issue search to a project or document query's
13443/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
13444/// carrying that text alone is sent as by [`narrowing_qualifiers`].
13445fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
13446    narrowing_qualifiers(&TaskQuery {
13447        text: text.cloned(),
13448        ..TaskQuery::default()
13449    })
13450}
13451
13452/// Refuses a project or document query's text GitHub's issue search cannot find, before
13453/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
13454/// query's.
13455fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
13456    refuse_unsearchable(&TaskQuery {
13457        text: text.cloned(),
13458        ..TaskQuery::default()
13459    })
13460}
13461
13462/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
13463/// before anything is asked of GitHub.
13464///
13465/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
13466/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
13467/// left out, the search is every issue of the board. So this source says it cannot answer
13468/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
13469/// nothing GitHub could search for, and keeps the board read it always had.
13470fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
13471    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
13472                       letter or digit with a bounded query";
13473    if let Some(text) = &query.text
13474        && !text.terms.trim().is_empty()
13475        && !has_words(&text.terms)
13476    {
13477        return Err(SourceError::Refused {
13478            message: format!(
13479                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
13480                text.terms
13481            ),
13482        });
13483    }
13484    if let Some(wanted) = query
13485        .metadata
13486        .iter()
13487        .find(|wanted| !has_words(wanted.value()))
13488    {
13489        return Err(SourceError::Refused {
13490            message: format!(
13491                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
13492                wanted.value(),
13493                std::iter::once(wanted.key())
13494                    .chain(wanted.path().iter().map(String::as_str))
13495                    .collect::<Vec<_>>()
13496                    .join("/"),
13497            ),
13498        });
13499    }
13500    Ok(())
13501}
13502
13503/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
13504fn has_words(phrase: &str) -> bool {
13505    phrase.chars().any(char::is_alphanumeric)
13506}
13507
13508/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
13509///
13510/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
13511/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
13512/// which GitHub's word match would read as different words.
13513fn as_stored(value: &str) -> String {
13514    let encoded = Value::String(value.to_owned()).to_string();
13515    encoded[1..encoded.len() - 1].to_owned()
13516}
13517
13518/// The one narrower question a task query carrying a text, metadata or origin predicate is
13519/// sent as.
13520enum Narrowing {
13521    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
13522    Origin(String),
13523    /// The board-scoped issue search narrowed by these qualifiers.
13524    Search(String),
13525}
13526
13527impl Narrowing {
13528    /// What this question is remembered under for the length of one command.
13529    fn key(&self) -> String {
13530        match self {
13531            Self::Origin(origin) => format!("origin {origin}"),
13532            Self::Search(also) => format!("search {also}"),
13533        }
13534    }
13535}
13536
13537/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
13538enum Resumed {
13539    /// It reported another page, which starts after this cursor.
13540    More(String),
13541    /// It has ended. Sending this cursor again — the page's own end when it had one, and
13542    /// otherwise the cursor it was reached from — answers an empty page, so the one document
13543    /// can go on walking the other connection.
13544    Ended(Option<String>),
13545}
13546
13547impl Resumed {
13548    /// Whether the connection has another page.
13549    const fn has_more(&self) -> bool {
13550        matches!(self, Self::More(_))
13551    }
13552
13553    /// The cursor to send this connection next.
13554    fn cursor(self) -> Option<String> {
13555        match self {
13556            Self::More(next) => Some(next),
13557            Self::Ended(last) => last,
13558        }
13559    }
13560}
13561
13562/// Where `connection`, reached from `after`, resumes — refused when it reports another page
13563/// with no cursor to it, or from a cursor that does not advance.
13564fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
13565    let info = connection
13566        .get("pageInfo")
13567        .ok_or_else(|| SourceError::Malformed {
13568            message: "GitHub connection has no pageInfo".into(),
13569        })?;
13570    let end = optional_str(info, "endCursor")?;
13571    if required_bool(info, "hasNextPage")? {
13572        let next = end.ok_or_else(|| SourceError::Malformed {
13573            message: "GitHub connection reports another page and no endCursor".into(),
13574        })?;
13575        validate_cursor_progress(after, next)?;
13576        return Ok(Resumed::More(next.to_owned()));
13577    }
13578    Ok(Resumed::Ended(
13579        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
13580    ))
13581}
13582
13583/// The board, and every item on it this source reports.
13584#[derive(Clone)]
13585struct Board {
13586    id: String,
13587    fields: Value,
13588    items: Vec<Resolved>,
13589}
13590
13591/// What a write needs of the board and nothing more: its node id and its field
13592/// definitions, in the shape a read of the board's own `fields` gives them.
13593///
13594/// Deliberately no items. A write decides which item it writes, which parent it files
13595/// under and which far ends it names by reading each of them by its own id; this is the
13596/// half of the board those reads cannot carry, and holding no item is what keeps it from
13597/// ever being asked whether an item is there.
13598#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
13599struct BoardFields {
13600    id: BoardId,
13601    fields: Value,
13602}
13603
13604/// A board's node id: what a field write and `addProjectV2ItemById` address.
13605///
13606/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
13607/// refused where it is read, and one an item names blank is read as not named at all.
13608#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
13609#[serde(try_from = "String")]
13610struct BoardId(String);
13611
13612impl TryFrom<String> for BoardId {
13613    type Error = String;
13614
13615    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
13616    fn try_from(id: String) -> Result<Self, Self::Error> {
13617        Self::parse(&id).map_err(|error| error.to_string())
13618    }
13619}
13620
13621/// Where one write left its item, for the record the rest of the command reads it out of.
13622///
13623/// A named record rather than a tuple because the update arm and the create arm each fill
13624/// all four, and two `Option`s of different meaning side by side in a tuple are two
13625/// positions a reader has to count.
13626struct Landed {
13627    /// The issue's own node id, which is the [`NativeId`] this source reports.
13628    content_id: NativeId,
13629    /// The board item's id, which is what a field write addresses — `None` for an issue its
13630    /// create filed on the board itself, whose board item GitHub adds after answering and
13631    /// [`TaskSource::finish_writes`] reads.
13632    // 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.
13633    item_id: Option<String>,
13634    /// The web address GitHub gave the issue, when it gave one.
13635    // 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.
13636    url: Option<String>,
13637    /// The issue's number on its repository, when GitHub reported one.
13638    number: Option<u64>,
13639}
13640
13641impl BoardId {
13642    fn parse(id: &str) -> Result<Self, SourceError> {
13643        if id.trim().is_empty() {
13644            return Err(SourceError::Malformed {
13645                message: "GitHub named a board with a blank node id".into(),
13646            });
13647        }
13648        Ok(Self(id.to_owned()))
13649    }
13650
13651    fn as_str(&self) -> &str {
13652        &self.0
13653    }
13654}
13655
13656impl Board {
13657    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
13658        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
13659        let nodes = fields
13660            .get("nodes")
13661            .and_then(Value::as_array)
13662            .ok_or_else(|| SourceError::Malformed {
13663                message: "GitHub project fields.nodes is not an array".into(),
13664            })?;
13665        Ok(nodes
13666            .iter()
13667            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
13668    }
13669}
13670
13671/// One board item, resolved into everything this source reports about it.
13672///
13673/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
13674/// [`TaskSource::prime_write`] takes back — see [`expected`].
13675#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
13676struct Resolved {
13677    item_id: String,
13678    id: NativeId,
13679    content_kind: ContentKind,
13680    kind: BoardKind,
13681    title: String,
13682    body: Option<String>,
13683    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
13684    /// that changes the slot alone has to keep byte for byte outside it.
13685    raw_body: Option<String>,
13686    status: Status,
13687    /// The name of the board `Status` option this item sits in, as the board spells it.
13688    option: Option<String>,
13689    /// What its `Priority` field says, read through this instance's mapping.
13690    priority: HeldPriority,
13691    /// Whether this item's issue is closed. A draft has no such state and is never closed.
13692    closed: bool,
13693    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
13694    /// issue, a draft, and an issue closed before GitHub recorded reasons.
13695    #[serde(default)]
13696    reason: Option<String>,
13697    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
13698    delivers: Vec<TaskRef>,
13699    /// Every task that delivers this one, read out of its slot. Empty for anything not a
13700    /// task.
13701    delivered_by: Vec<TaskRef>,
13702    labels: Vec<Label>,
13703    parent: Option<NativeId>,
13704    /// The issue's GitHub parent on a source reading sub-issues as children (`sub_issues:
13705    /// parent`), where [`Self::parent`] — the project it is filed under — is always `None`.
13706    #[serde(default)]
13707    issue_parent: Option<NativeId>,
13708    // 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.
13709    origin: Option<String>,
13710    /// What each board text field this instance projects metadata onto holds for this item,
13711    /// by field name — only the fields holding a value, an empty text included. What a write compares the
13712    /// item's metadata against, so a field already holding its value is not written again;
13713    /// never read back as metadata, which the slot alone reports.
13714    projected: BTreeMap<String, String>,
13715    /// The issue's own number on its repository, as GitHub reports it.
13716    ///
13717    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
13718    /// declares none, and a draft is not filed in a repository to be numbered by one — and
13719    /// an issue this run created whose creating mutation answered without one, which is a
13720    /// response GitHub's own schema says cannot happen and which a landed write is not
13721    /// worth failing over. An `Issue` read off the board always has one.
13722    number: Option<u64>,
13723    url: Option<String>,
13724    created_at: Option<DateTime<Utc>>,
13725    updated_at: Option<DateTime<Utc>>,
13726    /// When the board item itself last changed — what a board field write moves — when the
13727    /// read that reached it said.
13728    item_updated_at: Option<DateTime<Utc>>,
13729    /// How many comments the issue holds, when the read that reached it said.
13730    comment_count: Option<u64>,
13731    /// How many relationship events the issue's timeline holds — every dependency, as
13732    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
13733    /// removed — when the read that reached it said.
13734    relationship_events: Option<u64>,
13735    /// The server's clock on the response that read it — its `Date` header — when the
13736    /// response carried one.
13737    served_at: Option<DateTime<Utc>>,
13738    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
13739    /// its labels — when the read that reached it carried all three whole.
13740    #[serde(default)]
13741    facets: Option<change_token::FacetsDigest>,
13742    own_repository: Option<Repository>,
13743    repositories: Vec<Repository>,
13744    classification: Classification,
13745    slot: BTreeMap<String, Value>,
13746    /// The node id of the board this item sits on, when the read that reached it said.
13747    board_id: Option<String>,
13748    /// The definition of every board field this item holds a value of, in the shape a read
13749    /// of the board's own `fields` gives one.
13750    ///
13751    /// Only the fields this item has a value in: a field it holds nothing of is not here,
13752    /// which says nothing about whether the board has it.
13753    fields: Vec<Value>,
13754    /// Every field the board this item sits on defines, as its own read of the board's
13755    /// `fields` gives them — when the read that reached the item carried them, which a read
13756    /// of it by its own id does. What a write of it needs of the board, then, needs no read
13757    /// of the board.
13758    board_fields: Option<Value>,
13759    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
13760    /// selects one — when the read that reached it carried the connection to its end, which a
13761    /// read of it by its own id does for any issue blocked by no more than a page. What a
13762    /// write reconciles that relationship against, and what a read of its forward edges in
13763    /// the same command answers with.
13764    blocked_by: Option<Vec<Value>>,
13765    /// Every person the item is assigned to, by login in the order GitHub answered them — read
13766    /// only by a source opted in to assignees, and `None` for any other, and for a record no
13767    /// read or write of this process showed them on.
13768    #[serde(default)]
13769    assignees: Option<Vec<String>>,
13770}
13771
13772impl Resolved {
13773    /// The board this item's own read names it on, when that read named one this source can
13774    /// address.
13775    fn named_board(&self) -> Option<BoardId> {
13776        self.board_id
13777            .as_deref()
13778            .and_then(|id| BoardId::parse(id).ok())
13779    }
13780
13781    /// The board's id and every field it defines, when the read that reached this item
13782    /// carried both — which a read of it by its own id does.
13783    fn carried_board(&self) -> Option<BoardFields> {
13784        Some(BoardFields {
13785            id: self.named_board()?,
13786            fields: self.board_fields.clone()?,
13787        })
13788    }
13789
13790    /// Whether this item holds a value of the board field called `name`, and so carries
13791    /// that field's definition. `false` says nothing about whether the board has the field.
13792    fn defines(&self, name: &str) -> bool {
13793        self.fields
13794            .iter()
13795            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
13796    }
13797
13798    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
13799    /// in a field of its own, and none of the five keys that are only an encoding.
13800    ///
13801    /// The two delivery keys are left out for every kind, not only for a task: they are
13802    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
13803    /// document carrying one holds nothing a caller's own metadata could mean by it.
13804    fn metadata(&self) -> BTreeMap<String, Value> {
13805        let mut metadata = self.slot.clone();
13806        metadata.remove(Repository::METADATA_KEY);
13807        metadata.remove(DependencyEdge::RECORDED_KEY);
13808        metadata.remove(ItemKind::METADATA_KEY);
13809        metadata.remove(TaskRef::DELIVERS_KEY);
13810        metadata.remove(TaskRef::DELIVERED_BY_KEY);
13811        metadata.remove(Classification::METADATA_KEY);
13812        // The board field is the origin, and the body's copy of it is only a mirror for the
13813        // issue search to find: an item whose field holds none has none, whatever its body
13814        // says, so no reader ever sees two answers.
13815        metadata.remove(ORIGIN_KEY);
13816        if let Some(origin) = &self.origin {
13817            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
13818        }
13819        metadata
13820    }
13821
13822    /// Where this item is, as a link a reader can open.
13823    ///
13824    /// A board is a hosted place and every issue on it has a web address, so that address
13825    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
13826    /// of place it is, so a reader knows to open it rather than to read a file out. It
13827    /// does not replace or derive from `url`: the field goes on reporting exactly what it
13828    /// reported before, and this says what that address *is*.
13829    ///
13830    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
13831    /// rather than a third variant, which is the contract's "the source did not say". An
13832    /// issue this run created is not one of those: its address comes back from the
13833    /// creating mutation, so it is somewhere a reader can open from the moment it exists
13834    /// rather than from whenever the board read catches up.
13835    fn location(&self) -> Option<Location> {
13836        self.url.clone().map(Location::Url)
13837    }
13838
13839    /// The short handle this board's backend shows people for a task: the issue's number
13840    /// alone, as a decimal string.
13841    ///
13842    /// The number alone rather than `owner/repo#1043`, because that is the contract's
13843    /// value for this backend. A draft has no number and so no handle, which is the
13844    /// contract's *absent* rather than a handle of some other shape — and the native
13845    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
13846    /// derives from.
13847    fn key(&self) -> Option<String> {
13848        self.number.map(|number| number.to_string())
13849    }
13850
13851    /// This item's change token — see [`change_token`] — or `None` when the read that
13852    /// reached it did not carry all six of its parts, which a draft never does, did not say
13853    /// when GitHub served it, or found it changed too recently to have settled.
13854    fn version(&self) -> Option<String> {
13855        let parts = self.token_parts()?;
13856        change_token::settled(parts.issue, parts.item, self.served_at?)
13857            .then(|| change_token::spell(&parts))
13858    }
13859
13860    /// The change token this item, read just after a write of it, holds now — whether or not it
13861    /// has settled: what a written task's `version` answers (see
13862    /// [`GitHubProjectsSource::written_tokens`]), spelled from the parts a later read spells its
13863    /// token from.
13864    fn written_version(&self) -> Option<String> {
13865        self.token_parts().map(|parts| change_token::spell(&parts))
13866    }
13867
13868    /// Every part of this item's change token, or `None` when the read that reached it did not
13869    /// carry them all — a draft never does.
13870    fn token_parts(&self) -> Option<change_token::Parts> {
13871        match self.content_kind {
13872            ContentKind::Issue => Some(change_token::Parts {
13873                issue: self.updated_at?,
13874                item: self.item_updated_at?,
13875                comments: self.comment_count?,
13876                relationships: self.relationship_events?,
13877                facets: self.facets.clone()?,
13878                repository: self.own_repository.clone()?,
13879            }),
13880            ContentKind::DraftIssue => None,
13881        }
13882    }
13883
13884    /// Whether its `Priority` field holds a value at all, mapped or not.
13885    fn holds_priority(&self) -> bool {
13886        self.priority != HeldPriority::Read(Priority::None)
13887    }
13888
13889    /// The task this item is.
13890    ///
13891    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
13892    /// reading that as a level would be a guess, and reading it as `none` would let the next
13893    /// copy clear a priority a person set.
13894    fn task(&self) -> Result<Task, SourceError> {
13895        let priority = match &self.priority {
13896            HeldPriority::Read(priority) => *priority,
13897            HeldPriority::Unmapped(option) => {
13898                return Err(SourceError::Malformed {
13899                    message: format!(
13900                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
13901                         this source's priority_mapping does not name, so its priority cannot be \
13902                         read; next: name {option:?} under priority_mapping, or move the item to \
13903                         a mapped option",
13904                        self.id,
13905                        self.number
13906                            .map(|number| format!(" (#{number})"))
13907                            .unwrap_or_default()
13908                    ),
13909                });
13910            }
13911        };
13912        let metadata = self.metadata();
13913        Ok(Task {
13914            version: self.version(),
13915            id: self.id.clone(),
13916            key: self.key(),
13917            title: self.title.clone(),
13918            // The `## Links` section the engine rendered is stripped, so content round-trips.
13919            content: onetaskgraph_plugin_api::read_content(self.body.clone(), &metadata),
13920            status: self.status.clone(),
13921            priority,
13922            labels: self.labels.clone(),
13923            project: self.parent.clone(),
13924            url: self.url.clone(),
13925            location: self.location(),
13926            created_at: self.created_at,
13927            updated_at: self.updated_at,
13928            metadata,
13929            repositories: self.repositories.clone(),
13930            delivers: self.delivers.clone(),
13931            delivered_by: self.delivered_by.clone(),
13932            classification: self.classification,
13933            assignees: self.assignees.clone(),
13934        })
13935    }
13936
13937    fn project(&self) -> Project {
13938        Project {
13939            version: self.version(),
13940            id: self.id.clone(),
13941            title: self.title.clone(),
13942            content: self.body.clone(),
13943            status: self.status.clone(),
13944            labels: self.labels.clone(),
13945            url: self.url.clone(),
13946            location: self.location(),
13947            created_at: self.created_at,
13948            updated_at: self.updated_at,
13949            metadata: self.metadata(),
13950            repositories: self.repositories.clone(),
13951            classification: self.classification,
13952        }
13953    }
13954
13955    /// The same issue as a document: the project it is filed under, and no status and no
13956    /// dependencies, because a document is not work.
13957    fn document(&self) -> Document {
13958        Document {
13959            version: self.version(),
13960            id: self.id.clone(),
13961            title: self.title.clone(),
13962            content: self.body.clone(),
13963            project: self.parent.clone(),
13964            labels: self.labels.clone(),
13965            url: self.url.clone(),
13966            location: self.location(),
13967            created_at: self.created_at,
13968            updated_at: self.updated_at,
13969            metadata: self.metadata(),
13970            repositories: self.repositories.clone(),
13971            classification: self.classification,
13972        }
13973    }
13974}
13975
13976/// Where one targeted update moves an item's status, and which of its two halves move.
13977struct StatusMove {
13978    /// The board the item's `Status` field is on.
13979    board: BoardId,
13980    /// The `Status` field's id.
13981    field: String,
13982    /// The option's id.
13983    option: String,
13984    /// The option's name, as the board spells it.
13985    name: String,
13986    /// What the status asks of the issue's state.
13987    target: StatusTarget,
13988    /// The status the item reads as once it is there.
13989    landed: Status,
13990    /// Which of the status's two halves differ from what the item holds.
13991    moves: Moves,
13992}
13993
13994/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
13995/// closed state of its issue, or both. A status neither half of which differs is no move at all,
13996/// and is not a value of this type.
13997#[derive(Clone, Copy, PartialEq, Eq)]
13998enum Moves {
13999    /// The option alone.
14000    Option,
14001    /// The issue's state alone: open, closed, or closed with another reason.
14002    State,
14003    /// Both.
14004    Both,
14005}
14006
14007impl Moves {
14008    /// What differs, or `None` when nothing does.
14009    const fn of(option: bool, state: bool) -> Option<Self> {
14010        match (option, state) {
14011            (true, true) => Some(Self::Both),
14012            (true, false) => Some(Self::Option),
14013            (false, true) => Some(Self::State),
14014            (false, false) => None,
14015        }
14016    }
14017
14018    /// Whether the option moves.
14019    const fn option(self) -> bool {
14020        matches!(self, Self::Option | Self::Both)
14021    }
14022
14023    /// Whether the issue's state moves.
14024    const fn state(self) -> bool {
14025        matches!(self, Self::State | Self::Both)
14026    }
14027}
14028
14029/// What one write is, and the status that comes with being it.
14030///
14031/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
14032/// status and a task or a project always has one, so "a document carrying a status" and
14033/// "a task carrying none" are states a write cannot be in rather than states every use
14034/// site below has to defend against.
14035enum Written<'a> {
14036    /// A document, which is not work and so has no status at all.
14037    Document,
14038    /// A task or a project, and the status it is being written with.
14039    Work(ItemKind, &'a Status),
14040}
14041
14042impl Written<'_> {
14043    /// Which of the board's three kinds this write is.
14044    const fn kind(&self) -> BoardKind {
14045        match self {
14046            Self::Document => BoardKind::Document,
14047            Self::Work(kind, _) => BoardKind::Work(*kind),
14048        }
14049    }
14050
14051    /// The status this write carries. A document carries none, so a write of one says
14052    /// nothing about the issue's open or closed state and selects no board `Status`
14053    /// option.
14054    const fn status(&self) -> Option<&Status> {
14055        match self {
14056            Self::Document => None,
14057            Self::Work(_, status) => Some(status),
14058        }
14059    }
14060
14061    /// The status this write carries with the kind whose half of `status_mapping` it is
14062    /// written through.
14063    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
14064        match self {
14065            Self::Document => None,
14066            Self::Work(kind, status) => Some((*kind, status)),
14067        }
14068    }
14069}
14070
14071/// The item being written, in the one shape all three write methods reach.
14072struct Incoming<'a> {
14073    written: Written<'a>,
14074    /// The title a person wrote. A document's goes onto the issue with
14075    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
14076    title: &'a str,
14077    content: Option<&'a str>,
14078    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
14079    labels: &'a [Label],
14080    metadata: &'a BTreeMap<String, Value>,
14081    repositories: &'a [Repository],
14082    /// Recorded in the slot while private, and nowhere while public.
14083    classification: Classification,
14084    parent: Option<&'a NativeId>,
14085    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
14086    /// what keeps either key out of their slot.
14087    delivers: &'a [TaskRef],
14088    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
14089    delivered_by: &'a [TaskRef],
14090    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
14091    /// project, a document, and every write to an instance with no `priority_mapping` —
14092    /// which is what keeps such a write's requests exactly what they were before.
14093    priority: Option<Priority>,
14094    /// [`Task::assignees`] of a task written to a source opted in to them — the people it is
14095    /// to be assigned to, replacing whoever it holds — and `None` for everything else, which
14096    /// leaves an existing item's as they are and creates one unassigned.
14097    assignees: Option<&'a [String]>,
14098}
14099
14100/// What one write does to one projected metadata text field, by the field's id.
14101#[derive(Debug, Clone)]
14102enum ProjectionWrite {
14103    /// Write this text.
14104    Set {
14105        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.
14106        text: String,
14107    },
14108    /// Clear the field's value.
14109    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
14110}
14111
14112impl ProjectionWrite {
14113    /// Add this write to a field write's list of values and list of clears.
14114    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
14115        match self {
14116            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
14117            Self::Clear { field } => clears.push(field),
14118        }
14119    }
14120}
14121
14122/// What one write does to an item's `Priority` field.
14123enum PriorityWrite {
14124    /// Select this option of this field.
14125    Select {
14126        /// The `Priority` field's id.
14127        field: String,
14128        /// The mapped option's id.
14129        option: String,
14130    },
14131    /// Clear the field's value, which is what `none` is.
14132    Clear {
14133        /// The `Priority` field's id.
14134        field: String,
14135    },
14136}
14137
14138impl Incoming<'_> {
14139    /// The title this write puts on the issue.
14140    fn written_title(&self) -> String {
14141        match self.written {
14142            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
14143            Written::Work(..) => self.title.to_owned(),
14144        }
14145    }
14146}
14147
14148#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
14149enum ContentKind {
14150    DraftIssue,
14151    Issue,
14152}
14153
14154/// What one board issue is: a document, or the work an [`ItemKind`] names.
14155///
14156/// A type of this source's own rather than an `ItemKind` with a third variant, because
14157/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
14158/// document — the contract keeps a document out of that enum deliberately. Holding the
14159/// board's three answers in one value is what makes every place that asks "which is this?"
14160/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
14161/// two thirds of the board.
14162#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
14163enum BoardKind {
14164    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
14165    Document,
14166    /// Every other issue, and every draft.
14167    Work(ItemKind),
14168}
14169
14170impl BoardKind {
14171    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
14172    /// sub-issues and carrying the kind marker `marked` in its slot.
14173    ///
14174    /// The design prefix is read *first*, before either of the two rules that separate a
14175    /// project from a task. A document is not work whatever sub-issues it has and whatever
14176    /// marker it carries, and reading the prefix later would make a design issue with none of
14177    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
14178    /// overrides it: an issue filed under a project is that project's task even when it has
14179    /// sub-issues of its own.
14180    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
14181        if title.starts_with(DESIGN_TITLE_PREFIX) {
14182            Self::Document
14183        } else if filed {
14184            Self::Work(ItemKind::Task)
14185        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
14186            Self::Work(ItemKind::Project)
14187        } else {
14188            Self::Work(ItemKind::Task)
14189        }
14190    }
14191
14192    /// What the change token's facets call this kind.
14193    const fn facet(self) -> change_token::FacetKind {
14194        match self {
14195            Self::Document => change_token::FacetKind::Document,
14196            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
14197            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
14198        }
14199    }
14200
14201    /// Whose half of `status_mapping` an item of this kind reads its status through. A
14202    /// document has no status of its own, so the task half stands in for whatever the issue
14203    /// holds; nothing reports it.
14204    const fn status_kind(self) -> ItemKind {
14205        match self {
14206            Self::Document => ItemKind::Task,
14207            Self::Work(kind) => kind,
14208        }
14209    }
14210
14211    /// How a refusal names this kind to the person reading it.
14212    const fn describes(self) -> &'static str {
14213        match self {
14214            Self::Document => "document",
14215            Self::Work(kind) => kind.marker(),
14216        }
14217    }
14218}
14219
14220/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
14221///
14222/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
14223/// the shared cross-source journeys assert one answer to one question, so two sources
14224/// that disagree about what "carries the label bug" means fail them.
14225fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
14226    let holds = |name: &String| {
14227        labels
14228            .iter()
14229            .any(|label| label.name.eq_ignore_ascii_case(name))
14230    };
14231    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
14232        && filter.all_of.iter().all(holds)
14233        && !filter.none_of.iter().any(holds)
14234}
14235
14236/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
14237/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
14238fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
14239    statuses.is_empty() || statuses.contains(&category)
14240}
14241
14242/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
14243///
14244/// `content` is the item's own prose — the body with this source's trailing metadata
14245/// comment already taken off — so a search never matches an encoding the author of the
14246/// issue never wrote.
14247fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
14248    let terms = query.terms.to_lowercase();
14249    let in_title = title.to_lowercase().contains(&terms);
14250    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
14251    match query.fields {
14252        TextFields::Title => in_title,
14253        TextFields::Content => in_content,
14254        TextFields::TitleOrContent => in_title || in_content,
14255    }
14256}
14257
14258/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
14259///
14260/// The project predicate is passed separately because a read narrowed to one project has
14261/// already answered it by asking *that project* for its own items — and re-applying it
14262/// there would compare the caller's selector, which may be a project's **name**, against
14263/// the id of the project that name resolved to, and keep nothing. Every other read passes
14264/// `query.project` and applies it here, which is what keeps `projects` a predicate this
14265/// source really does apply.
14266fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
14267    labels_match(&task.labels, &query.labels)
14268        && status_matches(task.status.category, &query.statuses)
14269        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
14270        && match project {
14271            ProjectFilter::Any => true,
14272            ProjectFilter::Orphans => task.project.is_none(),
14273            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
14274        }
14275        && query
14276            .text
14277            .as_ref()
14278            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
14279        // Against the parsed metadata slot, and against the origin field, which is where
14280        // `Resolved::metadata` reads each of them from.
14281        && query.metadata_matches(&task.metadata)
14282        && query.origin_matches(&task.metadata)
14283}
14284
14285fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
14286    labels_match(&project.labels, &query.labels)
14287        && status_matches(project.status.category, &query.statuses)
14288        && query
14289            .text
14290            .as_ref()
14291            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
14292}
14293
14294/// The same three predicates a task query carries, minus the status filter.
14295///
14296/// A document is not work, so it has no status for one to compare against and the query
14297/// type carries none. The project predicate is the same one — a design issue filed under a
14298/// project issue is in that project, and one filed under nothing is in none — so it is
14299/// spelled the same way here rather than answered differently.
14300fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
14301    labels_match(&document.labels, &query.labels)
14302        && match project {
14303            ProjectFilter::Any => true,
14304            ProjectFilter::Orphans => document.project.is_none(),
14305            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
14306        }
14307        && query
14308            .text
14309            .as_ref()
14310            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
14311}
14312
14313/// Why a GitHub item's change token does not vouch for its forward edges.
14314///
14315/// A forward edge names its far end and that far end's kind, and the near item's token moves
14316/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
14317/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
14318/// nothing the near token reads, so an edge kept at the near version could name a far end that
14319/// is no longer what it said.
14320const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
14321     whether it became a document, or its deletion, so its forward edges are always read from \
14322     github";
14323
14324/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
14325/// categories an open issue reads as narrows a board-scoped search with.
14326const OPEN_QUALIFIER: &str = "is:open";
14327
14328/// The board filter `task status reconcile` reads the board through: closed items alone.
14329const CLOSED_FILTER: &str = "is:closed";
14330
14331/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
14332/// one reference, and a slot the batch has none for names its last one again.
14333fn reference_batch(batch: &[IssueRef]) -> Value {
14334    let mut variables = serde_json::Map::new();
14335    for slot in 0..REFERENCE_BATCH {
14336        let issue = batch.get(slot).or(batch.last());
14337        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
14338        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
14339        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
14340    }
14341    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
14342    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
14343    variables.insert("duplicates".to_owned(), json!(true));
14344    Value::Object(variables)
14345}
14346
14347/// One issue an issue read by reference answered, in the shape every other read of an issue
14348/// hands its resolver: each board membership's `Status` value, read by name, as the one field
14349/// value that membership carries.
14350///
14351/// A membership's `status` is `null` when its card sits at no option — or on a board with no
14352/// field of that name — and otherwise an option value naming its option; anything else is not
14353/// an answer to this read, and is refused rather than read as a card at no option.
14354fn held_issue(issue: &Value) -> Result<Value, SourceError> {
14355    let malformed = |what: &str| SourceError::Malformed {
14356        message: format!("GitHub answered an issue read by reference with {what}"),
14357    };
14358    let mut issue = issue.clone();
14359    let nodes = issue
14360        .pointer_mut("/projectItems/nodes")
14361        .and_then(Value::as_array_mut)
14362        .ok_or_else(|| malformed("no list of board memberships"))?;
14363    for node in nodes {
14364        let node = node
14365            .as_object_mut()
14366            .ok_or_else(|| malformed("a board membership that is not an object"))?;
14367        let status = match node.get("status") {
14368            None => return Err(malformed("a board membership with no status member")),
14369            Some(Value::Null) => None,
14370            Some(status) if status.get("name").is_some_and(Value::is_string) => {
14371                Some(status.clone())
14372            }
14373            // A single-select value of another field type answers `{}`; only an option value
14374            // carries a name.
14375            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
14376            Some(_) => return Err(malformed("a membership status that names no option")),
14377        };
14378        node.insert(
14379            "fieldValues".to_owned(),
14380            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
14381                   "pageInfo": {"hasNextPage": false}}),
14382        );
14383    }
14384    Ok(issue)
14385}
14386
14387/// The issue one slot of a read by reference answered, `None` when GitHub answered that
14388/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
14389/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
14390/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
14391/// than a reference nothing holds.
14392fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
14393    let alias = format!("r{slot}");
14394    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
14395        message: format!("GitHub answered a read by reference with no {alias}"),
14396    })?;
14397    if repository.is_null() {
14398        return Ok(None);
14399    }
14400    let issue = repository
14401        .get("issue")
14402        .ok_or_else(|| SourceError::Malformed {
14403            message: format!("GitHub answered {alias} of a read by reference with no issue"),
14404        })?;
14405    if issue.is_null() {
14406        return Ok(None);
14407    }
14408    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
14409    if optional_str(issue, "__typename")? != Some("Issue") {
14410        return Err(SourceError::Malformed {
14411            message: format!(
14412                "GitHub answered {alias} of a read by reference with an issue whose __typename \
14413                 is not Issue"
14414            ),
14415        });
14416    }
14417    Ok(Some(issue))
14418}
14419
14420/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
14421fn spoken_reason(reason: &str) -> String {
14422    reason.to_ascii_lowercase().replace('_', " ")
14423}
14424
14425impl GitHubProjectsSource {
14426    /// Every task and project of this board whose card contradicts its issue's closure, each
14427    /// judged against its own kind's half of `status_mapping`, read through the board's own
14428    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
14429    ///
14430    /// Each item the filter answers is confirmed from its own node: an open one, one whose
14431    /// closure reads `unknown`, and one whose card is already in line are left out whatever
14432    /// the filter let through. The board's fields ride along and are held for the option
14433    /// writes that follow, so those need no read of the board.
14434    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
14435        let mut after: Option<String> = None;
14436        let mut found = Vec::new();
14437        loop {
14438            let data = self
14439                .graphql(
14440                    graphql::CLOSED_ITEMS,
14441                    json!({"owner":self.owner,"number":self.project_number,
14442                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
14443                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
14444                )
14445                .await?;
14446            let board = data
14447                .pointer("/closedItems/projectV2")
14448                .filter(|value| !value.is_null())
14449                .ok_or_else(|| SourceError::Refused {
14450                    message: format!(
14451                        "GitHub project {}/{} was not found or is not visible to the token",
14452                        self.owner, self.project_number
14453                    ),
14454                })?;
14455            *self.fields_cache()? = Some(BoardFields {
14456                id: BoardId::parse(required_str(board, "id")?)?,
14457                fields: board.get("fields").cloned().unwrap_or(Value::Null),
14458            });
14459            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
14460                message: "GitHub project closed items are missing".into(),
14461            })?;
14462            for item in items
14463                .get("nodes")
14464                .and_then(Value::as_array)
14465                .ok_or_else(|| SourceError::Malformed {
14466                    message: "GitHub project items.nodes is not an array".into(),
14467                })?
14468            {
14469                let Some(resolved) = self.resolve(item)? else {
14470                    continue;
14471                };
14472                let BoardKind::Work(kind) = resolved.kind else {
14473                    continue;
14474                };
14475                let Some((closed_as, name)) = self.statuses.contradicted(
14476                    kind,
14477                    resolved.option.as_deref(),
14478                    resolved.closed,
14479                    resolved.reason.as_deref(),
14480                ) else {
14481                    continue;
14482                };
14483                found.push(StatusContradiction {
14484                    closed_as,
14485                    to: name.to_owned(),
14486                    option: resolved.option.clone(),
14487                    item: match kind {
14488                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
14489                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
14490                    },
14491                });
14492            }
14493            let info = items
14494                .get("pageInfo")
14495                .ok_or_else(|| SourceError::Malformed {
14496                    message: "GitHub project items have no pageInfo".into(),
14497                })?;
14498            if !required_bool(info, "hasNextPage")? {
14499                return Ok(found);
14500            }
14501            let next = required_str(info, "endCursor")?;
14502            validate_cursor_progress(after.as_deref(), next)?;
14503            after = Some(next.to_owned());
14504        }
14505    }
14506
14507    /// What this board holds for each issue in `issues`, once each in the order given, read
14508    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
14509    ///
14510    /// An issue GitHub cannot find for this source's credential, and one with no item on this
14511    /// board, are both absent. A closure that already says the work will not be done here —
14512    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
14513    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
14514    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
14515    /// tasks as sub-issues — is held under exactly these rules, judged against the project
14516    /// kind's half of `status_mapping`.
14517    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
14518        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
14519        for issue in issues {
14520            if !distinct.contains(issue) {
14521                distinct.push(issue.clone());
14522            }
14523        }
14524        let done = |kind: ItemKind| {
14525            self.statuses
14526                .target(kind, StatusCategory::Done)
14527                .option()
14528                .unwrap_or("Done")
14529                .to_owned()
14530        };
14531        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
14532        // its range names no issue any repository can hold: it is answered absent and never sent,
14533        // where binding it would have GitHub refuse the whole batch.
14534        let readable: Vec<IssueRef> = distinct
14535            .iter()
14536            .filter(|issue| i32::try_from(issue.number).is_ok())
14537            .cloned()
14538            .collect();
14539        let mut holdings = Vec::with_capacity(readable.len());
14540        for batch in readable.chunks(REFERENCE_BATCH) {
14541            let data = self
14542                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
14543                .await?;
14544            for (slot, issue) in batch.iter().enumerate() {
14545                let node = referenced_issue(&data, slot)?;
14546                let resolved = match node {
14547                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
14548                    None => None,
14549                };
14550                // A task and a project are both held; a document is not work, so nothing is.
14551                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
14552                    BoardKind::Work(kind) => Some((item, kind)),
14553                    BoardKind::Document => None,
14554                }) else {
14555                    holdings.push(IssueHolding {
14556                        issue: issue.clone(),
14557                        completion: Completion::Absent,
14558                    });
14559                    continue;
14560                };
14561                let task = match kind {
14562                    ItemKind::Task => {
14563                        let held = item.task()?;
14564                        HeldTask {
14565                            id: held.id,
14566                            kind,
14567                            option: item.option.clone(),
14568                            classification: held.classification,
14569                            repositories: held.repositories,
14570                            delivers: held.delivers,
14571                        }
14572                    }
14573                    // A project delivers no task, so what its write keeps in step is nothing.
14574                    ItemKind::Project => {
14575                        let held = item.project();
14576                        HeldTask {
14577                            id: held.id,
14578                            kind,
14579                            option: item.option.clone(),
14580                            classification: held.classification,
14581                            repositories: held.repositories,
14582                            delivers: Vec::new(),
14583                        }
14584                    }
14585                };
14586                let done = done(kind);
14587                let completion = match (item.closed, item.reason.as_deref()) {
14588                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
14589                        task,
14590                        reason: format!("closed as {}", spoken_reason(reason)),
14591                    },
14592                    (true, None | Some("COMPLETED"))
14593                        if item
14594                            .option
14595                            .as_deref()
14596                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
14597                    {
14598                        Completion::Unchanged { task }
14599                    }
14600                    _ => {
14601                        let kept = item.clone();
14602                        self.held_cache
14603                            .lock()
14604                            .map_err(|_| SourceError::Unavailable {
14605                                message: "items read by issue reference were left \
14606                                          inconsistent; run the command again"
14607                                    .into(),
14608                            })?
14609                            .insert(kept.id.clone(), kept);
14610                        Completion::Due {
14611                            task,
14612                            to: done.clone(),
14613                        }
14614                    }
14615                };
14616                holdings.push(IssueHolding {
14617                    issue: issue.clone(),
14618                    completion,
14619                });
14620            }
14621        }
14622        let mut read = holdings.into_iter();
14623        let mut answered = Vec::with_capacity(distinct.len());
14624        for issue in distinct {
14625            if i32::try_from(issue.number).is_ok() {
14626                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
14627                    message: format!("no holding was read for {issue}"),
14628                })?);
14629            } else {
14630                answered.push(IssueHolding {
14631                    issue,
14632                    completion: Completion::Absent,
14633                });
14634            }
14635        }
14636        Ok(answered)
14637    }
14638}
14639
14640#[async_trait::async_trait]
14641impl TaskSource for GitHubProjectsSource {
14642    fn kind(&self) -> &'static str {
14643        KIND
14644    }
14645    /// GitHub's issue search, which narrows a read of the comments made since an instant, is
14646    /// an index behind the writes it indexes — the credentialed lane has watched it miss a newly
14647    /// commented issue for thirty seconds — so a watermark is set back by
14648    /// [`COMMENT_SEARCH_LAG`], four times that.
14649    fn comment_consistency_window(&self) -> std::time::Duration {
14650        COMMENT_SEARCH_LAG
14651    }
14652    /// The `Date` of the last answer GitHub gave this source.
14653    fn served_at(&self) -> Option<DateTime<Utc>> {
14654        *self
14655            .last_served
14656            .lock()
14657            .unwrap_or_else(std::sync::PoisonError::into_inner)
14658    }
14659    fn attach_body_markup(&self, markup: onetaskgraph_plugin_api::SharedBodyMarkup) {
14660        let _ = self.body_markup.set(markup);
14661    }
14662    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
14663        change_token::order(a, b)
14664    }
14665    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
14666    /// member, the change each part of the token moves for. Not its forward edges — see
14667    /// [`FAR_ENDS_UNCOVERED`].
14668    fn version_coverage(&self) -> VersionCoverage {
14669        VersionCoverage {
14670            forward_edges: Coverage::Uncovered {
14671                reason: FAR_ENDS_UNCOVERED.to_owned(),
14672            },
14673            ..VersionCoverage::whole()
14674        }
14675    }
14676    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
14677    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
14678    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
14679    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
14680    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
14681    // 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.
14682    fn copy_version_coverage(&self) -> VersionCoverage {
14683        VersionCoverage::whole()
14684    }
14685    /// A project's fields, and not its forward edges, for the same reason a task's are not.
14686    fn project_version_coverage(&self) -> ProjectVersionCoverage {
14687        ProjectVersionCoverage {
14688            fields: Coverage::Covered,
14689            forward_edges: Coverage::Uncovered {
14690                reason: FAR_ENDS_UNCOVERED.to_owned(),
14691            },
14692        }
14693    }
14694    /// A document's fields and the manifest of its image assets, which its metadata slot
14695    /// records: both are the issue's body, whose every edit moves the token's issue time.
14696    fn document_version_coverage(&self) -> DocumentVersionCoverage {
14697        DocumentVersionCoverage::whole()
14698    }
14699    fn write_context(&self, id: &NativeId) -> Option<Value> {
14700        self.context_of(id)
14701    }
14702    fn board_context(&self) -> Option<Value> {
14703        self.board_context_held()
14704    }
14705    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
14706        self.prime(id, context, board)
14707    }
14708    fn unprime(&self, id: &NativeId) {
14709        self.unprime_held(id);
14710    }
14711    fn placement_context(&self) -> Option<Value> {
14712        self.placement_held()
14713    }
14714    fn prime_placement(&self, context: &Value) -> bool {
14715        self.prime_placement_held(context)
14716    }
14717    fn written_context(&self, id: &NativeId) -> Option<Value> {
14718        self.written_held(id)
14719    }
14720    fn prime_written(&self, id: &NativeId, context: &Value, task: &Task) -> bool {
14721        self.prime_written_held(id, context, task)
14722    }
14723    fn begin_journal(&self) {
14724        self.expected.begin();
14725    }
14726    fn end_journal(&self) -> Option<WriteJournal> {
14727        Some(self.expected.end())
14728    }
14729    fn capabilities(&self) -> Capabilities {
14730        Capabilities {
14731            projects: Support::Native,
14732            documents: Support::Native,
14733            comments: Support::Native,
14734            assets: Support::Native,
14735            priority: if self.priorities.is_some() {
14736                Support::Native
14737            } else {
14738                Support::Unsupported
14739            },
14740            filter_by_priority: Support::Native,
14741            filter_by_comment_activity: Support::Native,
14742            filter_by_metadata: Support::Native,
14743            filter_by_origin: Support::Native,
14744            reconcile_status: Support::Native,
14745            complete_by_issue: Support::Native,
14746            orphan_tasks: Support::Native,
14747            filter_by_label: Support::Native,
14748            filter_by_status: Support::Native,
14749            search_title: Support::Native,
14750            search_content: Support::Native,
14751            task_dependencies: DependencySupport::BothDirections,
14752            project_dependencies: DependencySupport::BothDirections,
14753            max_page_size: MAX_PAGE_SIZE,
14754            assignees: if self.assignees {
14755                Support::Native
14756            } else {
14757                Support::Unsupported
14758            },
14759        }
14760    }
14761    async fn visibility(
14762        &self,
14763        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
14764    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
14765        self.write_visibility(target).await
14766    }
14767    async fn visibility_with_project(
14768        &self,
14769        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
14770        project: &onetaskgraph_plugin_api::WriteTarget<'_>,
14771    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
14772        self.write_visibility_with_project(target, project).await
14773    }
14774    fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
14775        self.declared_private.store(
14776            declared == onetaskgraph_plugin_api::Visibility::Private,
14777            std::sync::atomic::Ordering::SeqCst,
14778        );
14779    }
14780    fn scope_visibility<'a>(
14781        &'a self,
14782        operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
14783    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
14784        self.scope_visibility_reads(operation)
14785    }
14786
14787    /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
14788    /// the create applies — reading the parent project's issue only when `target` names one.
14789    async fn creation_repository(
14790        &self,
14791        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
14792    ) -> Result<Option<Repository>, SourceError> {
14793        let onetaskgraph_plugin_api::WriteTarget::New {
14794            repositories,
14795            project,
14796        } = target
14797        else {
14798            return Ok(None);
14799        };
14800        let target = self
14801            .creation_rule(repositories, *project, "a new item")
14802            .await?;
14803        Repository::try_from(target.origin())
14804            .map(Some)
14805            .map_err(|message| SourceError::Malformed { message })
14806    }
14807    /// Every issue of the repository a create of `target` files in whose body carries `key` in
14808    /// its metadata slot, read off that repository's issues newest first back to `since` —
14809    /// never through search, whose index lags a fresh issue by seconds.
14810    async fn created_with_key(
14811        &self,
14812        record: onetaskgraph_plugin_api::MetadataRecord,
14813        key: &str,
14814        since: Option<DateTime<Utc>>,
14815        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
14816    ) -> Result<Vec<NativeId>, SourceError> {
14817        let _ = record;
14818        let onetaskgraph_plugin_api::WriteTarget::New {
14819            repositories,
14820            project,
14821        } = target
14822        else {
14823            return Ok(Vec::new());
14824        };
14825        let repository = self
14826            .creation_rule(repositories, *project, "a new item")
14827            .await?;
14828        let wanted = Value::String(key.to_owned());
14829        let mut found = Vec::new();
14830        let mut after: Option<String> = None;
14831        let mut followed = std::collections::HashSet::new();
14832        loop {
14833            let data = self
14834                .graphql(
14835                    graphql::ISSUES_BY_CREATION,
14836                    json!({"owner": repository.owner, "name": repository.name,
14837                           "first": MAX_PAGE_SIZE, "after": after}),
14838                )
14839                .await?;
14840            let issues =
14841                data.pointer("/repository/issues")
14842                    .ok_or_else(|| SourceError::Malformed {
14843                        message: format!(
14844                            "GitHub answered no issues for repository {}",
14845                            repository.slug()
14846                        ),
14847                    })?;
14848            let malformed = |what: &str| SourceError::Malformed {
14849                message: format!(
14850                    "GitHub answered a page of repository {}'s issues {what}",
14851                    repository.slug()
14852                ),
14853            };
14854            let nodes = issues
14855                .get("nodes")
14856                .and_then(Value::as_array)
14857                .ok_or_else(|| malformed("with no list of issues"))?;
14858            let mut older = false;
14859            for node in nodes {
14860                let id = node
14861                    .get("id")
14862                    .and_then(Value::as_str)
14863                    .filter(|id| !id.is_empty())
14864                    .ok_or_else(|| malformed("holding an issue with no id"))?;
14865                let created = optional_time(node, "createdAt")?
14866                    .ok_or_else(|| malformed("holding an issue with no creation time"))?;
14867                if since.is_some_and(|since| created < since) {
14868                    older = true;
14869                    break;
14870                }
14871                let body = match node.get("body") {
14872                    Some(Value::String(body)) => Some(body.clone()),
14873                    Some(Value::Null) => None,
14874                    _ => return Err(malformed("holding an issue with no body member")),
14875                };
14876                let (_, metadata) = metadata_body(body)?;
14877                if metadata.get(MetadataKey::CREATE_KEY) == Some(&wanted) {
14878                    found.push(NativeId(id.to_owned()));
14879                }
14880            }
14881            let next = issues
14882                .pointer("/pageInfo/hasNextPage")
14883                .and_then(Value::as_bool)
14884                .ok_or_else(|| malformed("that does not say whether another follows"))?;
14885            if older || !next {
14886                return Ok(found);
14887            }
14888            // A page that says another follows names where it starts, and never a page already
14889            // read: anything else would ask for the same pages again for ever.
14890            let cursor = issues
14891                .pointer("/pageInfo/endCursor")
14892                .and_then(Value::as_str)
14893                .filter(|cursor| !cursor.is_empty())
14894                .map(str::to_owned);
14895            if cursor
14896                .as_ref()
14897                .is_none_or(|cursor| !followed.insert(cursor.clone()))
14898            {
14899                return Err(SourceError::Malformed {
14900                    message: format!(
14901                        "GitHub answered a page of repository {}'s issues that says another \
14902                         follows without a new cursor to it",
14903                        repository.slug()
14904                    ),
14905                });
14906            }
14907            after = cursor;
14908        }
14909    }
14910
14911    /// `Repository.issues` lists an issue GitHub committed only once its listing catches up.
14912    fn create_key_lags(&self) -> bool {
14913        true
14914    }
14915
14916    /// The task as this process's own latest write of it left it, from the item it recorded
14917    /// that write as — never a request. Its `version` is always `None`: a board's change token
14918    /// is given only once an item has gone unchanged for [`change_token::SETTLED`], which an
14919    /// item this command has just written has not, so no write's own answer can vouch for one.
14920    async fn written_task(&self, id: &NativeId) -> Option<Task> {
14921        let held = self
14922            .created()
14923            .ok()?
14924            .iter()
14925            .find(|held| held.id == *id)
14926            .cloned()
14927            .or_else(|| {
14928                self.updated()
14929                    .ok()?
14930                    .iter()
14931                    .find(|held| held.id == *id)
14932                    .cloned()
14933            })?;
14934        let mut task = held.task().ok()?;
14935        task.version = self.written_token(id);
14936        Some(task)
14937    }
14938
14939    /// Why `creation_owners` refuses a create of `target`, by [`Self::creation_rule`] — read
14940    /// only when the list is not empty, so a source restricting nothing reads nothing.
14941    async fn creation_refusal(
14942        &self,
14943        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
14944    ) -> Result<Option<String>, SourceError> {
14945        let onetaskgraph_plugin_api::WriteTarget::New {
14946            repositories,
14947            project,
14948        } = target
14949        else {
14950            return Ok(None);
14951        };
14952        if self.creation_owners.is_empty() {
14953            return Ok(None);
14954        }
14955        let target = self
14956            .creation_rule(repositories, *project, "a new item")
14957            .await?;
14958        Ok(self.owner_refusal(&target))
14959    }
14960
14961    async fn health(&self) -> Result<Health, SourceError> {
14962        let board = self.board_page(None, 1).await?;
14963        Ok(Health {
14964            reachable: true,
14965            detail: Some(format!(
14966                "reading GitHub project {}/{} ({})",
14967                self.owner,
14968                self.project_number,
14969                required_str(&board, "title")?
14970            )),
14971        })
14972    }
14973    /// The board's own node id, digested: the one request reading its fields, or none when
14974    /// this command has read them already.
14975    async fn designator(&self) -> Result<Option<Designator>, SourceError> {
14976        let board = self.board_fields().await?;
14977        Designator::of_backend(KIND, board.id.as_str())
14978            .map(Some)
14979            .map_err(|message| SourceError::Malformed { message })
14980    }
14981    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
14982        self.item_by_id(id)
14983            .await?
14984            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
14985            .map(|item| item.task())
14986            .transpose()
14987    }
14988    async fn task_assets(
14989        &self,
14990        id: &NativeId,
14991    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
14992        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
14993    }
14994    async fn task_asset(
14995        &self,
14996        id: &NativeId,
14997        name: &onetaskgraph_plugin_api::AssetName,
14998    ) -> Result<Option<Vec<u8>>, SourceError> {
14999        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
15000            .await
15001    }
15002    async fn set_task_rendering_with_assets(
15003        &self,
15004        id: &NativeId,
15005        content: &str,
15006        provenance: &Value,
15007        _answers: &BTreeMap<String, Value>,
15008        assets: &onetaskgraph_plugin_api::AssetWrite,
15009    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
15010        self.replace_rendering(
15011            id,
15012            BoardKind::Work(ItemKind::Task),
15013            content,
15014            provenance,
15015            Some(assets),
15016        )
15017        .await
15018    }
15019    async fn document_assets(
15020        &self,
15021        id: &NativeId,
15022    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
15023        self.held_assets(id, BoardKind::Document).await
15024    }
15025    async fn document_asset(
15026        &self,
15027        id: &NativeId,
15028        name: &onetaskgraph_plugin_api::AssetName,
15029    ) -> Result<Option<Vec<u8>>, SourceError> {
15030        self.held_asset(id, BoardKind::Document, name).await
15031    }
15032    async fn set_document_rendering_with_assets(
15033        &self,
15034        id: &NativeId,
15035        content: &str,
15036        provenance: &Value,
15037        _answers: &BTreeMap<String, Value>,
15038        assets: &onetaskgraph_plugin_api::AssetWrite,
15039    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
15040        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
15041            .await
15042    }
15043    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
15044        Ok(self
15045            .item_by_id(id)
15046            .await?
15047            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
15048            .map(|item| item.project()))
15049    }
15050    /// Every narrowed search and origin lookup asked, a page at a time, as aliased fields of one
15051    /// [`graphql::SEARCH_BATCH`] request a round — a search's issue search in an `s<n>` field,
15052    /// an origin's board filter in an `o<n>` field beside its issue search — and every other
15053    /// query by [`Self::query_tasks`] on its own: one narrowed by a project or by comment
15054    /// activity, one carrying no narrowing, one resumed from a cursor that read handed out, and
15055    /// every query of a process that has written this board, whose own writes complete a
15056    /// narrowed answer. A lone search is its own read too, which costs a batch's point at a
15057    /// page of its own size.
15058    ///
15059    /// Each search's first page asks [`SEARCH_BATCH_FIRST_PAGE`] rows and each later page
15060    /// [`SEARCH_PAGE_SIZE`]. GitHub answers an error about one aliased field at that field's
15061    /// path, beside the others' answers, so a refused page fails that query's answer alone; a
15062    /// request it refuses whole is every query of that request's answer.
15063    async fn query_tasks_batch(
15064        &self,
15065        asks: &[(TaskQuery, PageRequest)],
15066    ) -> Vec<Result<Page<Task>, SourceError>> {
15067        self.search_batched(asks).await
15068    }
15069    async fn query_tasks(
15070        &self,
15071        query: &TaskQuery,
15072        page: &PageRequest,
15073    ) -> Result<Page<Task>, SourceError> {
15074        validate_page(page)?;
15075        refuse_unsearchable(query)?;
15076        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
15077        // category — `unknown`; every other category reads only off an open issue or a draft. So
15078        // a status filter naming none of those three asks every issue search it sends for open
15079        // issues alone; each candidate's category is still confirmed from its own item below.
15080        let open_only = open_only(query);
15081        if query.origin.is_none()
15082            && !matches!(query.project, ProjectFilter::Is(_))
15083            && let Some(also) = search_qualifiers(query)
15084        {
15085            return self.search_tasks(query, page, &also).await;
15086        }
15087
15088        // A read narrowed to one project asks that project for its own tasks, so nothing
15089        // about it costs what the rest of the board holds. A read carrying a text, metadata
15090        // or origin predicate asks GitHub the narrower question those predicates are, and a
15091        // read narrowed to comment activity alone asks the board's own issue search for the
15092        // issues updated since, which is every issue a comment could have been written or
15093        // edited on since. Every other task read is a question about the whole board and is
15094        // answered by reading it.
15095        let (held, membership) = match (&query.project, query.commented_since) {
15096            (ProjectFilter::Is(project), _) => (
15097                match self.held_task_children(project)? {
15098                    // Taken as `project_children` takes the project's own sub-issue read: with
15099                    // every item this process wrote under it since, and none it moved away.
15100                    Some(held) => self.completed_with_written(
15101                        held.into_iter()
15102                            .filter(|child| child.parent.as_ref() == Some(project))
15103                            .collect(),
15104                        |own| own.parent.as_ref() == Some(project),
15105                    )?,
15106                    None => self.project_children(project).await?,
15107                },
15108                // Answered by where these items came from; see `task_matches`.
15109                &ProjectFilter::Any,
15110            ),
15111            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
15112                match (self.narrowed(query).await?, since) {
15113                    (Some(narrowed), _) => (narrowed, &query.project),
15114                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
15115                    // The board's own items list every draft; only the search half, which
15116                    // completes them with issues they lag behind on, is narrowed — a closed
15117                    // issue that half leaves out is at no category the filter keeps.
15118                    (None, None) if open_only => (
15119                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
15120                        &query.project,
15121                    ),
15122                    (None, None) => (self.board().await?.items, &query.project),
15123                }
15124            }
15125        };
15126        // Every candidate this read confirms by its comments is read together.
15127        let candidates = self.comment_candidates(held.iter(), query, membership)?;
15128        self.hold_comment_pages(&candidates).await?;
15129        // Filtered before paged: a page of a filtered result is a page of the survivors,
15130        // never the survivors of a page.
15131        let mut tasks = Vec::new();
15132        for item in held
15133            .iter()
15134            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
15135        {
15136            let task = item.task()?;
15137            if task_matches(&task, query, membership)
15138                && self.commented_since(item, query.commented_since).await?
15139            {
15140                tasks.push(task);
15141            }
15142        }
15143        Ok(offset_page(
15144            tasks,
15145            numeric_cursor(page.cursor.as_ref())?,
15146            page.limit.min(MAX_PAGE_SIZE) as usize,
15147        ))
15148    }
15149    /// Read board pages directly, at the board connection's consistency. Unlike
15150    /// `query_tasks`, this does not complete a lagging board with issue search: a complete
15151    /// sweep needs one request per listing page, including the first comments.
15152    async fn query_task_details(
15153        &self,
15154        query: &TaskQuery,
15155        page: &PageRequest,
15156        comments: Option<&PageRequest>,
15157    ) -> Result<Page<TaskDetailRead>, SourceError> {
15158        let Some(comments) = comments else {
15159            let tasks = self.query_tasks(query, page).await?;
15160            return Ok(Page {
15161                items: tasks
15162                    .items
15163                    .into_iter()
15164                    .map(|task| TaskDetailRead {
15165                        task,
15166                        comments: None,
15167                    })
15168                    .collect(),
15169                next: tasks.next,
15170            });
15171        };
15172        validate_page(page)?;
15173        validate_page(comments)?;
15174        refuse_unsearchable(query)?;
15175        if comments.cursor.is_some() || query.commented_since.is_some() {
15176            return Err(SourceError::Refused {
15177                message: "a board listing with comments requires a first comment page and cannot filter by comment activity".into(),
15178            });
15179        }
15180        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
15181        let data = self
15182            .graphql(
15183                graphql::BOARD_WITH_COMMENTS,
15184                json!({
15185                    "owner":self.owner,"number":self.project_number,
15186                    "first":page.limit.min(MAX_PAGE_SIZE),"after":after,
15187                    "commentFirst":comments.limit.min(MAX_PAGE_SIZE),
15188                    "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true,
15189                }),
15190            )
15191            .await?;
15192        let connection = data
15193            .pointer("/owner/projectV2/items")
15194            .filter(|value| !value.is_null())
15195            .ok_or_else(|| SourceError::Refused {
15196                message: "GitHub project was not found or is not visible to the token".into(),
15197            })?;
15198        let mut items = Vec::new();
15199        for node in optional_nodes(Some(connection), "board items")?
15200            .into_iter()
15201            .flatten()
15202        {
15203            let Some(item) = self.resolve(node)? else {
15204                continue;
15205            };
15206            if item.kind != BoardKind::Work(ItemKind::Task) {
15207                continue;
15208            }
15209            let task = item.task()?;
15210            if !task_matches(&task, query, &query.project) {
15211                continue;
15212            }
15213            let read = if item.content_kind == ContentKind::DraftIssue {
15214                Err(self.draft_has_no_comments(&task.id))
15215            } else {
15216                self.expected.learn_comments(&task.id, &node["content"]);
15217                comment_page(&node["content"], &task.id.0, None).map(Some)
15218            };
15219            items.push(TaskDetailRead {
15220                task,
15221                comments: Some(read),
15222            });
15223        }
15224        let next = next_cursor(connection)?;
15225        if let Some(next) = &next {
15226            validate_cursor_progress(after, &next.0)?;
15227        }
15228        Ok(Page { items, next })
15229    }
15230
15231    async fn query_projects(
15232        &self,
15233        query: &ProjectQuery,
15234        page: &PageRequest,
15235    ) -> Result<Page<Project>, SourceError> {
15236        validate_page(page)?;
15237        refuse_unsearchable_text(query.text.as_ref())?;
15238        // The projects a board holds are found by an issue search scoped to that board,
15239        // never by walking the board's own item connection: what tells a project from a
15240        // task is the `parent` each issue carries, which costs nothing to read. A query
15241        // carrying a text asks that search for the text too, so it reads the issues that
15242        // hold it rather than every issue of the board.
15243        let held = match self.text_searched(query.text.as_ref()).await? {
15244            Some(searched) => searched,
15245            None => self.board_issues().await?,
15246        };
15247        let projects = held
15248            .iter()
15249            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
15250            .map(Resolved::project)
15251            .filter(|project| project_matches(project, query))
15252            .collect();
15253        Ok(offset_page(
15254            projects,
15255            numeric_cursor(page.cursor.as_ref())?,
15256            page.limit.min(MAX_PAGE_SIZE) as usize,
15257        ))
15258    }
15259    async fn query_projects_by_origin(
15260        &self,
15261        origin: &str,
15262        page: &PageRequest,
15263    ) -> Result<Page<Project>, SourceError> {
15264        validate_page(page)?;
15265        let projects = self
15266            .copied_from(origin, BoardKind::Work(ItemKind::Project))
15267            .await?
15268            .iter()
15269            .map(Resolved::project)
15270            .collect();
15271        Ok(offset_page(
15272            projects,
15273            numeric_cursor(page.cursor.as_ref())?,
15274            page.limit.min(MAX_PAGE_SIZE) as usize,
15275        ))
15276    }
15277    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
15278        let Some(item) = self
15279            .document_by_id(id)
15280            .await?
15281            .filter(|item| item.kind == BoardKind::Document)
15282        else {
15283            return Ok(None);
15284        };
15285        self.expected.read_document(&item);
15286        Ok(Some(item.document()))
15287    }
15288    async fn query_documents_by_origin(
15289        &self,
15290        origin: &str,
15291        page: &PageRequest,
15292    ) -> Result<Page<Document>, SourceError> {
15293        validate_page(page)?;
15294        let documents = self
15295            .copied_from(origin, BoardKind::Document)
15296            .await?
15297            .iter()
15298            .map(Resolved::document)
15299            .collect();
15300        Ok(offset_page(
15301            documents,
15302            numeric_cursor(page.cursor.as_ref())?,
15303            page.limit.min(MAX_PAGE_SIZE) as usize,
15304        ))
15305    }
15306    async fn query_documents(
15307        &self,
15308        query: &DocumentQuery,
15309        page: &PageRequest,
15310    ) -> Result<Page<Document>, SourceError> {
15311        validate_page(page)?;
15312        // Narrowed to one project, this is the same sub-issue read a task list scoped to
15313        // that project makes — a document filed under a project is a sub-issue of it too,
15314        // and which of them come back is the kind this caller asked for. Unscoped, a query
15315        // carrying a text asks the board-scoped issue search for it, as a task query does,
15316        // and only one carrying none reads the board.
15317        let (held, membership) = match &query.project {
15318            ProjectFilter::Is(project) => (
15319                self.project_children(project).await?,
15320                // Answered by where these items came from; see `task_matches`.
15321                &ProjectFilter::Any,
15322            ),
15323            ProjectFilter::Any | ProjectFilter::Orphans => {
15324                refuse_unsearchable_text(query.text.as_ref())?;
15325                match self.text_searched(query.text.as_ref()).await? {
15326                    Some(searched) => (searched, &query.project),
15327                    None => (self.board().await?.items, &query.project),
15328                }
15329            }
15330        };
15331        // Filtered before paged, exactly as a task read is: a page of a filtered result is
15332        // a page of the survivors, never the survivors of a page.
15333        let documents = held
15334            .iter()
15335            .filter(|item| item.kind == BoardKind::Document)
15336            .map(Resolved::document)
15337            .filter(|document| document_matches(document, query, membership))
15338            .collect();
15339        Ok(offset_page(
15340            documents,
15341            numeric_cursor(page.cursor.as_ref())?,
15342            page.limit.min(MAX_PAGE_SIZE) as usize,
15343        ))
15344    }
15345    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
15346        validate_page(page)?;
15347        let offset = numeric_cursor(page.cursor.as_ref())?;
15348        let mut labels = self
15349            .board()
15350            .await?
15351            .items
15352            .into_iter()
15353            .flat_map(|item| item.labels)
15354            .fold(Vec::new(), |mut all, label| {
15355                if !all.iter().any(|x: &Label| x.id == label.id) {
15356                    all.push(label);
15357                }
15358                all
15359            });
15360        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
15361        Ok(offset_page(
15362            labels,
15363            offset,
15364            page.limit.min(MAX_PAGE_SIZE) as usize,
15365        ))
15366    }
15367    async fn task_dependencies(
15368        &self,
15369        id: &NativeId,
15370        direction: Direction,
15371        page: &PageRequest,
15372    ) -> Result<Page<DependencyEdge>, SourceError> {
15373        self.dependencies(id, ItemKind::Task, direction, page).await
15374    }
15375    async fn project_dependencies(
15376        &self,
15377        id: &NativeId,
15378        direction: Direction,
15379        page: &PageRequest,
15380    ) -> Result<Page<DependencyEdge>, SourceError> {
15381        self.dependencies(id, ItemKind::Project, direction, page)
15382            .await
15383    }
15384
15385    fn writes(&self) -> WriteSupport {
15386        WriteSupport::Supported
15387    }
15388
15389    /// Create or update one task.
15390    ///
15391    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
15392    /// neither may name the task itself or name one task twice — and land in the body's
15393    /// metadata slot under their reserved keys, in place of any caller metadata of those
15394    /// names.
15395    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
15396        if write.item.project.is_some() {
15397            self.projectless()?;
15398        }
15399        self.write_task_assets(write, None)
15400            .await
15401            .map(|written| written.id)
15402    }
15403
15404    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
15405        self.origins_ahead(origins).await
15406    }
15407
15408    async fn member_classifications(
15409        &self,
15410        project: &NativeId,
15411    ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
15412        Ok(self
15413            .member_classes
15414            .lock()
15415            .map_err(|_| SourceError::Unavailable {
15416                message: "a project's members held for its classification were left \
15417                          inconsistent; run the command again"
15418                    .into(),
15419            })?
15420            .remove(project))
15421    }
15422
15423    /// The issue's own web address, as the `createIssue` that made it in this command answered
15424    /// it: a task, a project and a document are each an issue here.
15425    fn written_location(
15426        &self,
15427        _record: onetaskgraph_plugin_api::MetadataRecord,
15428        id: &NativeId,
15429    ) -> Option<Location> {
15430        self.written
15431            .lock()
15432            .unwrap_or_else(std::sync::PoisonError::into_inner)
15433            .get(id)
15434            .cloned()
15435            .map(Location::Url)
15436    }
15437
15438    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
15439    /// in one request, and a copy's tasks are created together only then, since their fields
15440    /// and edges are what is held back.
15441    fn create_batch_limit(&self) -> usize {
15442        if self.deferring().unwrap_or(false) {
15443            MUTATION_BATCH
15444        } else {
15445            1
15446        }
15447    }
15448
15449    // 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.
15450    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
15451        if writes.iter().any(|write| write.item.project.is_some()) {
15452            self.projectless()?;
15453        }
15454        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
15455            return Err(SourceError::Refused {
15456                message: format!(
15457                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
15458                     next: update it with write_task",
15459                    write.item.id,
15460                    write.target.as_ref().map_or("", |target| target.0.as_str())
15461                ),
15462            });
15463        }
15464        if !self.deferring()? {
15465            let mut created = Vec::with_capacity(writes.len());
15466            for write in writes {
15467                match self.write_task(write).await {
15468                    Ok(id) => created.push(id),
15469                    Err(error) => {
15470                        let mut left: Vec<&str> = Vec::new();
15471                        for id in &created {
15472                            if self.delete_issue(id).await.is_err() {
15473                                left.push(id.0.as_str());
15474                            }
15475                        }
15476                        return Err(left_behind(error, &left));
15477                    }
15478                }
15479            }
15480            return Ok(created);
15481        }
15482        self.create_together(writes).await
15483    }
15484
15485    fn begin_deferred_writes(&self) {
15486        if let Ok(mut deferred) = self.deferred.lock() {
15487            deferred.get_or_insert_with(Deferred::default);
15488        }
15489    }
15490
15491    async fn finish_writes(&self) -> Result<(), SourceError> {
15492        self.send_held_updates().await?;
15493        self.send_held_back().await
15494    }
15495
15496    async fn write_task_with_assets(
15497        &self,
15498        write: &ItemWrite<Task>,
15499        _answers: Option<&BTreeMap<String, Value>>,
15500        assets: &onetaskgraph_plugin_api::AssetWrite,
15501    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15502        self.write_task_assets(write, Some(assets)).await
15503    }
15504
15505    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
15506        self.projectless()?;
15507        self.write_item(
15508            &Incoming {
15509                written: Written::Work(ItemKind::Project, &write.item.status),
15510                title: &write.item.title,
15511                content: write.item.content.as_deref(),
15512                assets: None,
15513                labels: &write.item.labels,
15514                metadata: &write.item.metadata,
15515                repositories: &write.item.repositories,
15516                classification: write.item.classification,
15517                parent: None,
15518                delivers: &[],
15519                delivered_by: &[],
15520                priority: None,
15521                assignees: None,
15522            },
15523            write.target.as_ref(),
15524            &write.depends_on,
15525        )
15526        .await
15527        .map(|written| written.id)
15528    }
15529
15530    fn task_body_size(
15531        &self,
15532        write: &ItemWrite<Task>,
15533        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15534    ) -> Option<onetaskgraph_plugin_api::BodySize> {
15535        Some(self.body_size(&self.task_incoming(write, assets).ok()?))
15536    }
15537
15538    fn project_body_size(
15539        &self,
15540        write: &ItemWrite<Project>,
15541    ) -> Option<onetaskgraph_plugin_api::BodySize> {
15542        Some(self.body_size(&Incoming {
15543            written: Written::Work(ItemKind::Project, &write.item.status),
15544            title: &write.item.title,
15545            content: write.item.content.as_deref(),
15546            assets: None,
15547            labels: &write.item.labels,
15548            metadata: &write.item.metadata,
15549            repositories: &write.item.repositories,
15550            classification: write.item.classification,
15551            parent: None,
15552            delivers: &[],
15553            delivered_by: &[],
15554            priority: None,
15555            assignees: None,
15556        }))
15557    }
15558
15559    fn document_body_size(
15560        &self,
15561        write: &ItemWrite<Document>,
15562        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15563    ) -> Option<onetaskgraph_plugin_api::BodySize> {
15564        Some(self.body_size(&Incoming {
15565            written: Written::Document,
15566            title: &write.item.title,
15567            content: write.item.content.as_deref(),
15568            assets,
15569            labels: &write.item.labels,
15570            metadata: &write.item.metadata,
15571            repositories: &write.item.repositories,
15572            classification: write.item.classification,
15573            parent: write.item.project.as_ref(),
15574            delivers: &[],
15575            delivered_by: &[],
15576            priority: None,
15577            assignees: None,
15578        }))
15579    }
15580
15581    /// Create or update one document, which is one issue titled the way this board spells
15582    /// a document.
15583    ///
15584    /// Everything else is exactly a task write: caller metadata goes to the same canonical
15585    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
15586    /// or a field this board cannot carry is refused by name rather than dropped, a target
15587    /// naming an issue this board does not hold is refused rather than created, and an
15588    /// issue this call created is taken back when the rest of the write fails.
15589    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
15590        self.write_document_assets(write, None)
15591            .await
15592            .map(|written| written.id)
15593    }
15594
15595    async fn write_document_with_assets(
15596        &self,
15597        write: &ItemWrite<Document>,
15598        _answers: Option<&BTreeMap<String, Value>>,
15599        assets: &onetaskgraph_plugin_api::AssetWrite,
15600    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15601        self.write_document_assets(write, Some(assets)).await
15602    }
15603
15604    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
15605    /// which reads nothing; then the board's `Status` option. Over an existing item that is
15606    /// read off the item, as the write reads it, and the item is held among this command's
15607    /// resolved records so the write that follows reuses that read rather than repeating it;
15608    /// an item that does not carry the field takes the board's fields, which are held once
15609    /// read. A create is checked against the board's fields only when this command already
15610    /// holds them, because a create reads them together with its repository, in one request,
15611    /// and refuses a missing option before it writes anything.
15612    async fn check_status_write(
15613        &self,
15614        kind: ItemKind,
15615        category: StatusCategory,
15616        target: Option<&NativeId>,
15617    ) -> Result<(), SourceError> {
15618        let status = self.resolved_target(kind, category)?;
15619        if status.option().is_none() {
15620            return Ok(());
15621        }
15622        let fields = match target {
15623            Some(target) => {
15624                // A target this board does not hold is the write's own refusal to make.
15625                let Some(item) = self.bound_item(target).await? else {
15626                    return Ok(());
15627                };
15628                self.resolved_cache()?.insert(target.clone(), item.clone());
15629                // Only the `Status` field decides whether the write can land.
15630                self.fields_for(Some(&item), false, true, false)
15631                    .await?
15632                    .fields
15633            }
15634            None => {
15635                let held = self
15636                    .board_cache()?
15637                    .as_ref()
15638                    .map(|board| board.fields.clone());
15639                match held.or_else(|| {
15640                    self.fields_cache()
15641                        .ok()
15642                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
15643                }) {
15644                    Some(fields) => fields,
15645                    None => return Ok(()),
15646                }
15647            }
15648        };
15649        self.column_for(&fields, kind, category, &status)
15650            .map(|_| ())
15651    }
15652
15653    /// What a write of `status` reads back as: the `Status` option its category maps to for
15654    /// `kind`, and for a closing category the closure that option and its fixed reason read
15655    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
15656    /// this instance has no option for is answered as given, since writing it is refused.
15657    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
15658        match self.statuses.target(kind, status.category) {
15659            StatusTarget::Column(name) => {
15660                self.statuses.status(kind, Some(name.as_str()), false, None)
15661            }
15662            StatusTarget::Terminal(name, reason) => {
15663                self.statuses
15664                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
15665            }
15666            StatusTarget::Disabled(_) => status.clone(),
15667        }
15668    }
15669
15670    /// Set one task's status alone.
15671    ///
15672    /// An open target reopens a closed issue with an `updateIssue` carrying only its
15673    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
15674    /// terminal target selects its mapped option, then closes with its fixed reason. No
15675    /// request carries a title, a body or a label. The status
15676    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
15677    /// what a re-read reports.
15678    async fn set_task_status(
15679        &self,
15680        id: &NativeId,
15681        category: StatusCategory,
15682    ) -> Result<Option<Status>, SourceError> {
15683        self.set_status(id, ItemKind::Task, category).await
15684    }
15685
15686    /// Set one task's status and answer the task with it, reading the item once: the read the
15687    /// task is answered from is handed to the status write, which would otherwise read the
15688    /// same issue again — two requests on a fresh process, the read and the field write,
15689    /// where the default's `get_task` before `set_task_status` sent three.
15690    async fn set_task_status_reading(
15691        &self,
15692        id: &NativeId,
15693        category: StatusCategory,
15694    ) -> Result<Option<Task>, SourceError> {
15695        // Refused before anything is read, in the words the write itself refuses it.
15696        self.resolved_target(ItemKind::Task, category)?;
15697        let Some(item) = self
15698            .bound_item(id)
15699            .await?
15700            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
15701        else {
15702            return Ok(None);
15703        };
15704        let mut task = item.task()?;
15705        self.held_cache
15706            .lock()
15707            .map_err(|_| SourceError::Unavailable {
15708                message: "items read by issue reference were left inconsistent; run the \
15709                          command again"
15710                    .into(),
15711            })?
15712            .insert(id.clone(), item);
15713        let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
15714            return Ok(None);
15715        };
15716        task.status = status;
15717        Ok(Some(task))
15718    }
15719
15720    /// Set one project's status alone, by exactly the paired write a task's status takes, for
15721    /// the project kind: a terminal category selects the project kind's mapped option and then
15722    /// closes the issue with the category's fixed reason — an issue already closed with a
15723    /// reason that reads that category is left as it is — and an open category reopens a closed
15724    /// issue and then selects its option. A board draft is refused a terminal category as a
15725    /// task's is.
15726    async fn set_project_status(
15727        &self,
15728        id: &NativeId,
15729        category: StatusCategory,
15730    ) -> Result<Option<Status>, SourceError> {
15731        self.projectless()?;
15732        self.set_status(id, ItemKind::Project, category).await
15733    }
15734
15735    /// Every task and project whose card contradicts its issue's closure, read through the
15736    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
15737    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
15738        self.contradictions().await
15739    }
15740
15741    /// What this board holds for each issue reference, one request per fifty of them; see
15742    /// [`TaskSource::issue_holdings`].
15743    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
15744        self.holdings(issues).await
15745    }
15746
15747    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
15748    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
15749    /// for `none`. Refused by an instance with no `priority_mapping`.
15750    async fn set_task_priority(
15751        &self,
15752        id: &NativeId,
15753        priority: Priority,
15754    ) -> Result<Option<Priority>, SourceError> {
15755        self.set_priority(id, priority).await
15756    }
15757
15758    /// Replace one task's content with a single body update that keeps the metadata slot
15759    /// byte for byte.
15760    async fn set_task_content(
15761        &self,
15762        id: &NativeId,
15763        content: &str,
15764    ) -> Result<Option<()>, SourceError> {
15765        self.replace_content(id, content).await
15766    }
15767
15768    /// Replace one task issue's content and its provenance slot entry with a single body
15769    /// update. The answers are not kept: see `replace_rendering`.
15770    async fn set_task_rendering(
15771        &self,
15772        id: &NativeId,
15773        content: &str,
15774        provenance: &Value,
15775        _answers: &BTreeMap<String, Value>,
15776    ) -> Result<Option<()>, SourceError> {
15777        self.replace_rendering(
15778            id,
15779            BoardKind::Work(ItemKind::Task),
15780            content,
15781            provenance,
15782            None,
15783        )
15784        .await
15785        .map(|written| written.map(|_| ()))
15786    }
15787
15788    /// Replace one design-document issue's content and its provenance slot entry, on exactly
15789    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
15790    async fn set_document_rendering(
15791        &self,
15792        id: &NativeId,
15793        content: &str,
15794        provenance: &Value,
15795        _answers: &BTreeMap<String, Value>,
15796    ) -> Result<Option<()>, SourceError> {
15797        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
15798            .await
15799            .map(|written| written.map(|_| ()))
15800    }
15801
15802    /// Replace one project issue's content and its provenance slot entry, on exactly the
15803    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
15804    async fn set_project_rendering(
15805        &self,
15806        id: &NativeId,
15807        content: &str,
15808        provenance: &Value,
15809        _answers: &BTreeMap<String, Value>,
15810    ) -> Result<Option<()>, SourceError> {
15811        self.projectless()?;
15812        self.replace_rendering(
15813            id,
15814            BoardKind::Work(ItemKind::Project),
15815            content,
15816            provenance,
15817            None,
15818        )
15819        .await
15820        .map(|written| written.map(|_| ()))
15821    }
15822
15823    /// Apply a targeted update with one read of the item and a write only for what differs:
15824    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
15825    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
15826    async fn update_task(
15827        &self,
15828        id: &NativeId,
15829        update: &TaskUpdate,
15830    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
15831        self.targeted_update(id, update).await
15832    }
15833
15834    /// Each issue's sub-issue parent and children and its links, read in batches.
15835    async fn read_relations(
15836        &self,
15837        ids: &[NativeId],
15838    ) -> Vec<Result<Option<onetaskgraph_plugin_api::Relations>, SourceError>> {
15839        self.relations_of(ids).await
15840    }
15841
15842    async fn set_parents(
15843        &self,
15844        writes: &[onetaskgraph_plugin_api::ParentWrite],
15845    ) -> Vec<Result<onetaskgraph_plugin_api::ParentAnswer, SourceError>> {
15846        self.parents_set(writes).await
15847    }
15848
15849    fn link_types(&self) -> onetaskgraph_plugin_api::LinkTypes {
15850        self.link_types.clone()
15851    }
15852
15853    async fn add_native_links(
15854        &self,
15855        links: &[onetaskgraph_plugin_api::NativeLink],
15856    ) -> Vec<Result<(), SourceError>> {
15857        self.native_links(links, relations::NativeWrite::Add).await
15858    }
15859
15860    async fn remove_native_links(
15861        &self,
15862        links: &[onetaskgraph_plugin_api::NativeLink],
15863    ) -> Vec<Result<(), SourceError>> {
15864        self.native_links(links, relations::NativeWrite::Remove)
15865            .await
15866    }
15867
15868    /// Every task read together — [`DETAIL_BATCH`] to a [`graphql::ISSUE_DETAILS`] request —
15869    /// and each update's hold and guard decided on that read; then each update the read lets
15870    /// through composed exactly as [`Self::update_task`] composes one, its board field writes
15871    /// and its issue update held back and sent in [`graphql::WRITE_TASKS`] requests: every
15872    /// field write first, then the issue update of every task whose field writes all landed.
15873    ///
15874    /// **Where GitHub would refuse the batch as a whole, each item is read on its own:** a
15875    /// batched node read naming an id that resolves to nothing is refused whole, so each id of
15876    /// that batch is read by itself, the missing one answered missing and the rest as
15877    /// themselves. A batch of one, an update of a board draft, one naming dependencies and one
15878    /// of an item whose body this process has not read are each made on their own, as
15879    /// [`Self::update_task`] makes them, because each sends writes of its own in an order the
15880    /// batch does not hold. A field write GitHub refuses fails that task's update alone and
15881    /// sends nothing of its body.
15882    async fn update_tasks(
15883        &self,
15884        updates: &[onetaskgraph_plugin_api::BatchUpdate],
15885    ) -> Vec<Result<onetaskgraph_plugin_api::BatchAnswer<TaskUpdateOutcome>, SourceError>> {
15886        self.updates_batched(updates).await
15887    }
15888
15889    /// Replace one task's `delivered_by` with a single body update that changes the
15890    /// metadata slot and nothing outside it.
15891    async fn set_delivered_by(
15892        &self,
15893        id: &NativeId,
15894        delivered_by: &[TaskRef],
15895    ) -> Result<Option<()>, SourceError> {
15896        self.replace_delivered_by(id, delivered_by).await
15897    }
15898
15899    /// Set one key of one task issue's metadata with a single body update that changes the
15900    /// metadata slot and nothing outside it — no title, label, state or board field request —
15901    /// and sends nothing when the task already holds that value under the key.
15902    async fn set_task_metadata(
15903        &self,
15904        id: &NativeId,
15905        key: &MetadataKey,
15906        value: &Value,
15907    ) -> Result<Option<Task>, SourceError> {
15908        Ok(self
15909            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
15910            .await?
15911            .map(|item| item.task())
15912            .transpose()?)
15913    }
15914
15915    /// Set one key of one project issue's metadata, on exactly the terms of
15916    /// [`set_task_metadata`](TaskSource::set_task_metadata).
15917    async fn set_project_metadata(
15918        &self,
15919        id: &NativeId,
15920        key: &MetadataKey,
15921        value: &Value,
15922    ) -> Result<Option<Project>, SourceError> {
15923        self.projectless()?;
15924        Ok(self
15925            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
15926            .await?
15927            .map(|item| item.project()))
15928    }
15929
15930    /// Set one key of one design-document issue's metadata, on exactly the terms of
15931    /// [`set_task_metadata`](TaskSource::set_task_metadata).
15932    async fn set_document_metadata(
15933        &self,
15934        id: &NativeId,
15935        key: &MetadataKey,
15936        value: &Value,
15937    ) -> Result<Option<Document>, SourceError> {
15938        Ok(self
15939            .set_slot_key(id, BoardKind::Document, key, value)
15940            .await?
15941            .map(|item| item.document()))
15942    }
15943
15944    /// Take one key out of one issue's metadata slot with a single body update that changes the
15945    /// slot and nothing outside it, sending nothing when the slot does not hold the key.
15946    async fn remove_record_metadata(
15947        &self,
15948        record: onetaskgraph_plugin_api::MetadataRecord,
15949        id: &NativeId,
15950        key: &MetadataKey,
15951    ) -> Result<Option<Option<String>>, SourceError> {
15952        let kind = match record {
15953            onetaskgraph_plugin_api::MetadataRecord::Task => BoardKind::Work(ItemKind::Task),
15954            onetaskgraph_plugin_api::MetadataRecord::Project => BoardKind::Work(ItemKind::Project),
15955            onetaskgraph_plugin_api::MetadataRecord::Document => BoardKind::Document,
15956        };
15957        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
15958            return Ok(None);
15959        };
15960        if item.slot.contains_key(key.as_str()) {
15961            let mut slot = item.slot.clone();
15962            slot.remove(key.as_str());
15963            self.write_slot(&mut item, &slot).await?;
15964            self.remember_written(item.clone(), false)?;
15965        }
15966        Ok(Some(match record {
15967            onetaskgraph_plugin_api::MetadataRecord::Task => item.task()?.version,
15968            onetaskgraph_plugin_api::MetadataRecord::Project => item.project().version,
15969            onetaskgraph_plugin_api::MetadataRecord::Document => item.document().version,
15970        }))
15971    }
15972
15973    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
15974        self.delete_item(id).await
15975    }
15976
15977    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
15978        self.projectless()?;
15979        self.delete_item(id).await
15980    }
15981
15982    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
15983        self.delete_item(id).await
15984    }
15985
15986    /// One page of the task issue's own comments, walked by GitHub's own cursor.
15987    ///
15988    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
15989    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
15990    ///
15991    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
15992    /// board is the read of its comments. A draft this process already resolved is refused
15993    /// without one.
15994    async fn task_comments(
15995        &self,
15996        task: &NativeId,
15997        page: &PageRequest,
15998    ) -> Result<Option<Page<Comment>>, SourceError> {
15999        validate_page(page)?;
16000        let cached = self.resolved_cache()?.get(task).cloned();
16001        if let Some(item) = cached {
16002            if item.kind != BoardKind::Work(ItemKind::Task) {
16003                return Ok(None);
16004            }
16005            if item.content_kind == ContentKind::DraftIssue {
16006                return Err(self.draft_has_no_comments(task));
16007            }
16008        }
16009        // The first page a comment-activity read just read of this task, at the size asked, is
16010        // that read's answer: taken, so it answers once.
16011        if page.cursor.is_none()
16012            && page.limit >= MAX_PAGE_SIZE
16013            && let Some(held) = self.held_comments()?.remove(task)
16014        {
16015            return Ok(Some(held));
16016        }
16017        match self.issue_detail(task, page).await? {
16018            Some(TaskDetailRead {
16019                comments: Some(comments),
16020                ..
16021            }) => comments,
16022            _ => Ok(None),
16023        }
16024    }
16025
16026    /// Every id's task, with the first page of its comments when `comments` names it:
16027    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
16028    /// comments in one [`graphql::ISSUE_DETAIL`] request.
16029    async fn get_task_details(
16030        &self,
16031        ids: &[NativeId],
16032        comments: Option<&PageRequest>,
16033    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
16034        if let Some(page) = comments
16035            && let Err(error) = validate_page(page)
16036        {
16037            return ids.iter().map(|_| Err(error.clone())).collect();
16038        }
16039        match (ids, comments) {
16040            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
16041            ([id], None) => vec![self.task_read(id).await],
16042            _ => self.issue_details(ids, comments).await,
16043        }
16044    }
16045
16046    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
16047    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
16048    /// request, so the dependency read a copy makes of each next is answered from this one.
16049    async fn get_task_details_for_copy(
16050        &self,
16051        ids: &[NativeId],
16052        comments: &PageRequest,
16053    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
16054        // A page past the first is a page of one task's comments, which the detail read of it
16055        // alone answers.
16056        if comments.cursor.is_some() {
16057            return self.get_task_details(ids, Some(comments)).await;
16058        }
16059        if let Err(error) = validate_page(comments) {
16060            return ids.iter().map(|_| Err(error.clone())).collect();
16061        }
16062        let mut read = Vec::with_capacity(ids.len());
16063        for batch in ids.chunks(COPY_DETAIL_BATCH) {
16064            match self
16065                .graphql(
16066                    graphql::ISSUE_COPY_DETAILS,
16067                    copy_detail_batch(batch, comments, false),
16068                )
16069                .await
16070            {
16071                Ok(data) => {
16072                    for (slot, id) in batch.iter().enumerate() {
16073                        let read_one = match data.get(format!("i{slot}")) {
16074                            Some(node) => self.detail_of(id, node, true, None).await,
16075                            None => Err(SourceError::Malformed {
16076                                message: format!(
16077                                    "GitHub answered a batch read with no item for {}",
16078                                    id.0
16079                                ),
16080                            }),
16081                        };
16082                        read.push(read_one);
16083                    }
16084                }
16085                Err(error) if unresolvable_node(&error) => {
16086                    for id in batch {
16087                        read.push(self.issue_detail(id, comments).await);
16088                    }
16089                }
16090                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
16091            }
16092        }
16093        read
16094    }
16095
16096    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
16097    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
16098    /// and held for the next read of each by id; see `read_ahead`.
16099    ///
16100    /// What this run wrote is never read: its own record answers, as it does for a read by id.
16101    /// Nor is an item a listing of its project carried whole earlier in this command — see
16102    /// `carried` — which is held in place of reading it.
16103    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
16104    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
16105    /// answer naming another issue than the one asked for, are left to their own reads too.
16106    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
16107        // The origins asked just before go out now, beside the members of the one project this
16108        // names when it names one; a project those origins answered whole is held for its next
16109        // read by id rather than read again here.
16110        let hinted = match ids {
16111            [one] => Some(one),
16112            _ => None,
16113        };
16114        self.send_pending_origins(hinted).await?;
16115        if let Some(project) = hinted {
16116            let answered = self.resolved_cache()?.get(project).cloned();
16117            if let Some(answered) = answered
16118                && answered.kind == BoardKind::Work(ItemKind::Project)
16119            {
16120                self.read_ahead()?.insert(project.clone(), Some(answered));
16121                return Ok(());
16122            }
16123        }
16124        let mut unread: Vec<NativeId> = Vec::new();
16125        // Read whole by a listing of its project in this command, which a mutation naming it
16126        // would have dropped: held as a batch would have held it, once every batch is answered.
16127        let mut listed: Vec<NativeId> = Vec::new();
16128        {
16129            let created = self.created()?;
16130            let held = self.read_ahead()?;
16131            let carried = self.carried()?;
16132            for id in ids {
16133                if unread.contains(id)
16134                    || listed.contains(id)
16135                    || held.contains_key(id)
16136                    || created.iter().any(|own| own.id == *id)
16137                {
16138                    continue;
16139                }
16140                // An issue a listing carried without the whole of what blocks it is read in a
16141                // batch too, which carries that, rather than having its dependency read sent
16142                // on its own once the copy reconciles its edges.
16143                let whole = carried.get(id).is_some_and(|item| {
16144                    item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
16145                });
16146                if whole {
16147                    listed.push(id.clone());
16148                } else {
16149                    unread.push(id.clone());
16150                }
16151            }
16152        }
16153        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
16154        // what the item itself costs.
16155        let comments = PageRequest {
16156            cursor: None,
16157            limit: 1,
16158        };
16159        // Held only once every batch has been answered, so a read-ahead that fails part way
16160        // holds nothing — what a listing carried included, which stays carried — and every
16161        // item is read on its own, as the method promises.
16162        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
16163        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
16164        // answered them: what a project copy's orphan check asks, when that first id is its
16165        // project.
16166        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
16167        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
16168            let data = match self
16169                .graphql(
16170                    graphql::ISSUE_COPY_DETAILS,
16171                    copy_detail_batch(batch, &comments, index == 0),
16172                )
16173                .await
16174            {
16175                Ok(data) => data,
16176                Err(error) if unresolvable_node(&error) => continue,
16177                Err(error) => return Err(error),
16178            };
16179            for (slot, id) in batch.iter().enumerate() {
16180                let Some(node) = data.get(format!("i{slot}")) else {
16181                    continue;
16182                };
16183                let held = if node.is_null() {
16184                    None
16185                } else {
16186                    match optional_str(node, "__typename")? {
16187                        Some("DraftIssue") => continue,
16188                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
16189                        _ => {}
16190                    }
16191                    // Remembered below with the rest, so a later batch refused leaves no
16192                    // dependency read answered from this one either.
16193                    self.resolve_issue_as(node, false).await?
16194                };
16195                read.push((id.clone(), held));
16196            }
16197            if index == 0
16198                && let Some(node) = data.get("children").filter(|node| !node.is_null())
16199                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
16200                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
16201            {
16202                // A list GitHub did not answer is not an empty one: it would answer the project's
16203                // task listing with nothing, and the orphan check with no orphans.
16204                let listed = node
16205                    .pointer("/subIssues/nodes")
16206                    .and_then(Value::as_array)
16207                    .ok_or_else(|| SourceError::Malformed {
16208                        message: format!(
16209                            "GitHub answered the sub-issues of project {} with no list of them",
16210                            batch[0].0
16211                        ),
16212                    })?
16213                    .iter()
16214                    .map(|child| {
16215                        Ok((
16216                            required_str(child, "id")?.to_owned(),
16217                            required_str(child, "title")?.to_owned(),
16218                        ))
16219                    })
16220                    .collect::<Result<Vec<_>, SourceError>>()?;
16221                children = Some((batch[0].clone(), listed));
16222            }
16223        }
16224        // The project's tasks are known from this read alone when every child it named is held
16225        // from it, but for a document, which its title says is one and which a task listing
16226        // never answers: then a listing of the project's tasks is answered from them.
16227        if let Some((project, listed)) = children
16228            && read.iter().any(|(id, held)| {
16229                *id == project
16230                    && held
16231                        .as_ref()
16232                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
16233            })
16234        {
16235            let mut tasks = Vec::new();
16236            let mut whole = true;
16237            for (child, title) in &listed {
16238                match read.iter().find(|(id, _)| id.0 == *child) {
16239                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
16240                        if held.kind == BoardKind::Work(ItemKind::Task) {
16241                            tasks.push(held.clone());
16242                        }
16243                    }
16244                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
16245                    _ => whole = false,
16246                }
16247            }
16248            if whole {
16249                self.task_children()?.insert(project, tasks);
16250            }
16251        }
16252        let mut resolved = self.resolved_cache()?;
16253        let mut ahead = self.read_ahead()?;
16254        let mut carried = self.carried()?;
16255        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
16256            resolved.insert(item.id.clone(), item.clone());
16257        }
16258        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
16259        // and its next read is then its own.
16260        for id in &listed {
16261            let Some(item) = carried.remove(id) else {
16262                continue;
16263            };
16264            // The whole of what it is blocked by came with it, so the dependency read a copy
16265            // makes next is answered from this one, as a copy's detail read is.
16266            if item.blocked_by.is_some() {
16267                resolved.insert(item.id.clone(), item.clone());
16268            }
16269            read.push((id.clone(), Some(item)));
16270        }
16271        for (id, held) in read {
16272            ahead.insert(id, held);
16273        }
16274        Ok(())
16275    }
16276
16277    /// Add one comment to the task's issue, as the account the token belongs to.
16278    ///
16279    /// The author is refused before anything is sent — not even the task is read — because
16280    /// no answer GitHub could give would make posting under another name than the one asked
16281    /// for the right outcome.
16282    async fn add_comment(
16283        &self,
16284        task: &NativeId,
16285        comment: &NewComment,
16286    ) -> Result<Option<Comment>, SourceError> {
16287        if let Some(author) = &comment.author {
16288            return Err(SourceError::Refused {
16289                message: format!(
16290                    "source {} cannot post a comment as {author:?}: GitHub records the account \
16291                     the token signs in as the author of every comment; next: leave --author \
16292                     out, and the comment is posted as that account",
16293                    self.name
16294                ),
16295            });
16296        }
16297        let Some(issue) = self.commented_issue(task).await? else {
16298            return Ok(None);
16299        };
16300        let data = self
16301            .graphql(
16302                graphql::ADD_COMMENT,
16303                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
16304            )
16305            .await?;
16306        let subject = data
16307            .pointer("/addComment/subject")
16308            .filter(|value| !value.is_null())
16309            .ok_or_else(|| SourceError::Malformed {
16310                message: "GitHub comment addition returned no subject".into(),
16311            })?;
16312        if required_str(subject, "id")? != issue.0 {
16313            return Err(SourceError::Malformed {
16314                message: "GitHub comment addition answered about another issue".into(),
16315            });
16316        }
16317        let added = data
16318            .pointer("/addComment/commentEdge/node")
16319            .filter(|value| !value.is_null())
16320            .ok_or_else(|| SourceError::Malformed {
16321                message: "GitHub comment addition returned no comment".into(),
16322            })?;
16323        let added = comment_from(added)?;
16324        self.remember_commented(&issue)?;
16325        Ok(Some(added))
16326    }
16327
16328    /// Every task commented on read together — [`DETAIL_BATCH`] to a
16329    /// [`graphql::ISSUE_DETAILS`] request — and each comment's hold decided on that read; then
16330    /// every comment it lets through added by aliased `addComment`s of one
16331    /// [`graphql::ADD_COMMENTS`] request per [`COMMENT_BATCH`]. GitHub answers a refusal of one
16332    /// alias at that alias's path, so it fails that comment alone.
16333    ///
16334    /// **Where GitHub would refuse the batch as a whole, each item is read on its own:** a
16335    /// batched node read naming an id that resolves to nothing is refused whole, so each id of
16336    /// that batch is read by itself, the missing one answered missing. A batch of one is the
16337    /// comment alone, as [`Self::add_comment`] adds it.
16338    async fn add_comments(
16339        &self,
16340        comments: &[onetaskgraph_plugin_api::BatchComment],
16341    ) -> Vec<Result<onetaskgraph_plugin_api::BatchAnswer<Comment>, SourceError>> {
16342        self.comments_batched(comments).await
16343    }
16344
16345    async fn edit_comment(
16346        &self,
16347        task: &NativeId,
16348        comment: &NativeId,
16349        body: &CommentBody,
16350    ) -> Result<Option<Comment>, SourceError> {
16351        let Some(issue) = self.commented_issue(task).await? else {
16352            return Ok(None);
16353        };
16354        let Some(held) = self.comment_on(&issue, comment).await? else {
16355            return Ok(None);
16356        };
16357        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
16358        let body = CommentOrigin::kept(&held, body)?;
16359        let data = self
16360            .graphql(
16361                graphql::UPDATE_COMMENT,
16362                json!({"input":{"id":comment.0,"body":body.as_str()}}),
16363            )
16364            .await?;
16365        let edited = data
16366            .pointer("/updateIssueComment/issueComment")
16367            .filter(|value| !value.is_null())
16368            .ok_or_else(|| SourceError::Malformed {
16369                message: "GitHub comment update returned no comment".into(),
16370            })?;
16371        let edited = comment_from(edited)?;
16372        if edited.id != *comment {
16373            return Err(SourceError::Malformed {
16374                message: "GitHub comment update returned the wrong comment".into(),
16375            });
16376        }
16377        self.remember_commented(&issue)?;
16378        Ok(Some(edited))
16379    }
16380
16381    async fn delete_comment(
16382        &self,
16383        task: &NativeId,
16384        comment: &NativeId,
16385    ) -> Result<Option<NativeId>, SourceError> {
16386        let Some(issue) = self.commented_issue(task).await? else {
16387            return Ok(None);
16388        };
16389        if !self.comment_is_on(&issue, comment).await? {
16390            return Ok(None);
16391        }
16392        self.held_comments()?.remove(&issue);
16393        let data = self
16394            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
16395            .await?;
16396        // The payload says nothing about the comment it removed, so what is checked is that
16397        // GitHub answered the mutation at all rather than leaving it unanswered.
16398        data.get("deleteIssueComment")
16399            .filter(|value| !value.is_null())
16400            .ok_or_else(|| SourceError::Malformed {
16401                message: "GitHub comment deletion returned no payload".into(),
16402            })?;
16403        Ok(Some(comment.clone()))
16404    }
16405
16406    /// Every request this source has recorded, and what each of GitHub's two budgets was
16407    /// attributed — read off the same accounting the session report is rendered from, so
16408    /// the two cannot count one request two ways.
16409    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
16410        Ok(Some(self.ledger.snapshot().metering()))
16411    }
16412
16413    /// Drop every item, search answer and board read this source holds, so the next command
16414    /// reads the board as a person has since left it.
16415    ///
16416    /// Every one of those is held on the assumption that nothing but this source writes the
16417    /// board while a command runs, which stops being true the moment the command is over: a
16418    /// body a person edited would be overwritten from the record held here, and a card they
16419    /// moved would be read as still where this source left it. The board's own field
16420    /// definitions go too, because a person can add or delete a `Status` option and a write
16421    /// resolved against the held list would not re-read on a miss. What stays is what stays
16422    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
16423    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
16424    /// accounting [`metering`](TaskSource::metering) answers from.
16425    ///
16426    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
16427    /// refused, because clearing it is what puts it right.
16428    async fn end_command(&self) -> Result<(), SourceError> {
16429        fn clear<T: Default>(held: &Mutex<T>) {
16430            *held
16431                .lock()
16432                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
16433            held.clear_poison();
16434        }
16435        clear(&self.created);
16436        clear(&self.updated);
16437        clear(&self.commented);
16438        clear(&self.board_cache);
16439        clear(&self.search_cache);
16440        clear(&self.narrowed_cache);
16441        clear(&self.search_next);
16442        clear(&self.resolved_cache);
16443        clear(&self.read_ahead);
16444        clear(&self.pending_origins);
16445        clear(&self.member_classes);
16446        clear(&self.carried);
16447        clear(&self.held_cache);
16448        clear(&self.children_cache);
16449        clear(&self.task_children);
16450        clear(&self.written);
16451        clear(&self.fields_cache);
16452        clear(&self.held_comments);
16453        clear(&self.task_writes);
16454        clear(&self.written_tokens);
16455        self.forget_at_end_of_call();
16456        self.expected.end_command();
16457        Ok(())
16458    }
16459}
16460
16461/// One member of a project as a classification-only read answers it: its classification and
16462/// the repositories its work changes.
16463type MemberClass = (Classification, Vec<Repository>);
16464
16465/// Each project's sub-issues as one command read them, keyed by the selector they were asked
16466/// for under, beside the project that selector named; see `children_cache`.
16467type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
16468
16469/// One issue comment as the contract carries it.
16470///
16471/// `author` is absent both when GitHub answers `null` for an account that no longer exists
16472/// and when it answers an actor with no login, because either way the source did not say who
16473/// wrote it — which is what an absent author means, rather than an author called nothing.
16474///
16475/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
16476/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
16477/// and is read back with the marker taken out and reported as its origin.
16478fn comment_from(value: &Value) -> Result<Comment, SourceError> {
16479    let actor = value.get("author").unwrap_or(&Value::Null);
16480    // A bot's login is spelled as GitHub's own pages spell it, `<login>[bot]`, so a reader —
16481    // and every copy of the comment — can tell it from a person's.
16482    let bot = actor.get("__typename").and_then(Value::as_str) == Some("Bot");
16483    Ok(Comment {
16484        origin: None,
16485        id: NativeId(required_str(value, "id")?.to_owned()),
16486        author: optional_str(actor, "login")?.map(|login| {
16487            if bot && !login.ends_with("[bot]") {
16488                format!("{login}[bot]")
16489            } else {
16490                login.to_owned()
16491            }
16492        }),
16493        created_at: optional_time(value, "createdAt")?,
16494        updated_at: optional_time(value, "updatedAt")?,
16495        body: required_str(value, "body")?.to_owned(),
16496        url: optional_str(value, "url")?.map(str::to_owned),
16497    }
16498    .unmarked())
16499}
16500
16501/// The page of comments one issue node carries, resumed from `after`.
16502fn comment_page(
16503    node: &Value,
16504    issue: &str,
16505    after: Option<&str>,
16506) -> Result<Page<Comment>, SourceError> {
16507    let connection = node
16508        .get("comments")
16509        .filter(|value| !value.is_null())
16510        .ok_or_else(|| SourceError::Malformed {
16511            message: format!("GitHub issue {issue} answered with no comments connection"),
16512        })?;
16513    let items = optional_nodes(Some(connection), "issue comments")?
16514        .into_iter()
16515        .flatten()
16516        .map(comment_from)
16517        .collect::<Result<Vec<_>, _>>()?;
16518    let next = next_cursor(connection)?;
16519    if let Some(next) = &next {
16520        validate_cursor_progress(after, &next.0)?;
16521    }
16522    Ok(Page { items, next })
16523}
16524
16525/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
16526/// end — `None` when it carried none, or a page with more past it.
16527fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
16528    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
16529        return Ok(None);
16530    };
16531    if next_cursor(connection)?.is_some() {
16532        return Ok(None);
16533    }
16534    Ok(Some(
16535        optional_nodes(Some(connection), "blocked-by issues")?
16536            .into_iter()
16537            .flatten()
16538            .cloned()
16539            .collect(),
16540    ))
16541}
16542
16543/// Where the recorded tail of a dependency walk resumes; see
16544/// [`GitHubProjectsSource::recorded_edges`].
16545const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
16546
16547/// The board text field this source keeps a copy's origin in.
16548///
16549/// Named after the key it holds, and held to that name by the guard below rather than by
16550/// a reader noticing.
16551const ORIGIN_FIELD: &str = "onetaskgraph.origin";
16552
16553/// The metadata key that field holds.
16554///
16555/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
16556/// constructs or interprets the qualified id it carries. This source names it only to
16557/// route it — a short, typed value belongs in a typed field rather than in the body slot
16558/// a caller's own prose shares.
16559///
16560/// Restated rather than imported, because no plugin crate may depend on the engine. What
16561/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
16562/// target in `check`: it reads the engine's own literal and fails naming the file and the
16563/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
16564/// that creates a second item every run instead of finding the one it wrote — and that is
16565/// too late to learn it.
16566const ORIGIN_KEY: &str = "onetaskgraph.origin";
16567
16568/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
16569///
16570/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
16571/// is derived from the far end, never written down on the near item — so only a forward
16572/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
16573/// it did not come from, and it is told so rather than answered with an empty page that
16574/// reads as a walk which ended.
16575fn recorded_offset(
16576    cursor: Option<&str>,
16577    direction: Direction,
16578) -> Result<Option<usize>, SourceError> {
16579    cursor
16580        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
16581        .map(|offset| {
16582            if direction != Direction::DependsOn {
16583                return Err(SourceError::Config {
16584                    message: format!(
16585                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
16586                         reverse dependency read never issues; resume it in the direction \
16587                         that reported it"
16588                    ),
16589                });
16590            }
16591            offset.parse().map_err(|_| SourceError::Config {
16592                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
16593            })
16594        })
16595        .transpose()
16596}
16597
16598/// What a dependency read's own answer says of a board draft: `None` when the item is not a
16599/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
16600/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
16601/// draft's metadata, for one it does — its board item naming this board by its number and by
16602/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
16603fn draft_slot(
16604    data: &Value,
16605    id: &NativeId,
16606    project_number: u32,
16607) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
16608    let Some(draft) = data
16609        .get("node")
16610        .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
16611    else {
16612        return Ok(None);
16613    };
16614    let Some(items) = data.pointer("/draft/projectV2Items") else {
16615        return Ok(None);
16616    };
16617    if required_str(draft, "id")? != id.0 {
16618        return Err(SourceError::Malformed {
16619            message: format!("GitHub answered a different draft for {}", id.0),
16620        });
16621    }
16622    let nodes = items
16623        .get("nodes")
16624        .and_then(Value::as_array)
16625        .ok_or_else(|| SourceError::Malformed {
16626            message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
16627        })?;
16628    let info = items
16629        .get("pageInfo")
16630        .ok_or_else(|| SourceError::Malformed {
16631            message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
16632        })?;
16633    if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
16634        return Err(SourceError::Malformed {
16635            message: format!(
16636                "GitHub draft {} reports more board items than the one GitHub links a draft to",
16637                id.0
16638            ),
16639        });
16640    }
16641    let board = data
16642        .pointer("/board/projectV2/id")
16643        .and_then(Value::as_str)
16644        .ok_or_else(|| SourceError::Malformed {
16645            message: format!(
16646                "GitHub answered draft {} without this board's own id beside it",
16647                id.0
16648            ),
16649        })?;
16650    let mut held = false;
16651    for node in nodes {
16652        let project = node.get("project").ok_or_else(|| SourceError::Malformed {
16653            message: format!("GitHub draft {} board item has no project", id.0),
16654        })?;
16655        let number = project
16656            .get("number")
16657            .and_then(Value::as_u64)
16658            .ok_or_else(|| SourceError::Malformed {
16659                message: format!(
16660                    "GitHub draft {} board item has no numeric project number",
16661                    id.0
16662                ),
16663            })?;
16664        let project_id = required_str(project, "id")?;
16665        held |= number == u64::from(project_number) && project_id == board;
16666    }
16667    if !held {
16668        return Ok(Some(None));
16669    }
16670    Ok(Some(Some(
16671        metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
16672    )))
16673}
16674
16675fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
16676    let mut page = offset_page(edges, offset, limit.max(1));
16677    page.next = page
16678        .next
16679        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
16680    page
16681}
16682
16683/// The kind of one issue reached through a dependency connection.
16684///
16685/// The same questions the board scan asks, over the fields the dependency document
16686/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
16687/// then anything with sub-issues or the marker is a project.
16688///
16689/// # Errors
16690///
16691/// A far end this board holds as a document is refused rather than reported. The two
16692/// answers that are not refusals would both be wrong: reporting it as a task names an id
16693/// no task read of this source can find, and reporting it as a project names one no
16694/// project read can. There is no third value to return — `ItemKind` has no document
16695/// variant, because nothing may point at a document — so the relationship itself is what
16696/// the person is told about.
16697fn related_kind(value: &Value, meaning: SubIssues) -> Result<ItemKind, SourceError> {
16698    let id = required_str(value, "id")?;
16699    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
16700        return Err(SourceError::Refused {
16701            message: format!(
16702                "GitHub issue {id} is a document of this board — its title begins \
16703                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
16704                 on by one; next: remove that issue's blocking relationship on this board"
16705            ),
16706        });
16707    }
16708    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
16709    if parent.is_some() || meaning == SubIssues::Parent {
16710        return Ok(ItemKind::Task);
16711    }
16712    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
16713    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
16714        message: format!("GitHub issue {id}: {message}"),
16715    })?;
16716    let sub_issues = sub_issue_total(value)?;
16717    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
16718        ItemKind::Project
16719    } else {
16720        ItemKind::Task
16721    })
16722}
16723
16724/// The `stateInput` that closes an issue for `reason`.
16725fn closed_input(reason: ClosedState) -> Value {
16726    json!({"value":"CLOSED","stateReason":reason.reason()})
16727}
16728
16729/// The `IssueStateUpdateInput` one status target asks for.
16730///
16731/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
16732/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
16733/// a currently-closed issue: without that the item would read back `Unknown` and a copy
16734/// would report a change forever. A document has no status at all, and asks for neither.
16735fn state_input(target: Option<&StatusTarget>) -> Value {
16736    match target {
16737        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
16738        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
16739        // A document has no status, so a write of one says nothing about the issue's open
16740        // or closed state rather than forcing it open: `stateInput` is what carries that
16741        // instruction, and an explicit null asks for no change to it.
16742        None => Value::Null,
16743    }
16744}
16745
16746/// The metadata one write stores in the item's body slot.
16747///
16748/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
16749/// rather than carried: the kind marker so an empty project stays readable, the
16750/// repository list only when it is not exactly the issue's own repository, and the far
16751/// ends no relationship here can name.
16752///
16753/// The copy origin is the one typed field that is also mirrored here, and only as a
16754/// mirror: it lands in the board's origin field as well, which stays the one every reader
16755/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
16756/// and catches up with a write in seconds rather than minutes — can find the item by it.
16757/// A reader of the release before this one drops the slot's copy and reads the field, so an
16758/// item written here still reads with exactly one origin there.
16759fn slot_metadata(
16760    incoming: &Incoming<'_>,
16761    own_repository: Option<&Repository>,
16762    fallback: &[DependencyEdge],
16763) -> BTreeMap<String, Value> {
16764    let mut metadata = incoming.metadata.clone();
16765    match metadata.remove(ORIGIN_KEY) {
16766        Some(Value::String(origin)) if !origin.is_empty() => {
16767            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
16768        }
16769        _ => {}
16770    }
16771    match incoming.written.kind() {
16772        BoardKind::Work(kind) => metadata.insert(
16773            ItemKind::METADATA_KEY.to_owned(),
16774            Value::String(kind.marker().to_owned()),
16775        ),
16776        // A document is told by its title, so it carries no kind marker: that key names
16777        // what a dependency endpoint points at, and nothing may point at a document.
16778        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
16779    };
16780    incoming.classification.record(&mut metadata);
16781    let derivable = own_repository
16782        .map(|own| incoming.repositories == [own.clone()])
16783        .unwrap_or(incoming.repositories.is_empty());
16784    if derivable {
16785        metadata.remove(Repository::METADATA_KEY);
16786    } else {
16787        metadata.insert(
16788            Repository::METADATA_KEY.to_owned(),
16789            Value::Array(
16790                incoming
16791                    .repositories
16792                    .iter()
16793                    .map(|repository| Value::String(repository.as_str().to_owned()))
16794                    .collect(),
16795            ),
16796        );
16797    }
16798    // The typed lists are what land, whatever the caller's own metadata held under their
16799    // keys: a key of either name travelling beside the field would otherwise be a second
16800    // answer to the same question, and the field is the one the contract names.
16801    for (key, entries) in [
16802        (TaskRef::DELIVERS_KEY, incoming.delivers),
16803        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
16804    ] {
16805        set_task_list(&mut metadata, key, entries);
16806    }
16807    record_edges(&mut metadata, fallback);
16808    metadata
16809}
16810
16811/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
16812/// one slot's metadata, or no such key when there are none.
16813fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
16814    if fallback.is_empty() {
16815        metadata.remove(DependencyEdge::RECORDED_KEY);
16816    } else {
16817        metadata.insert(
16818            DependencyEdge::RECORDED_KEY.to_owned(),
16819            Value::Array(
16820                fallback
16821                    .iter()
16822                    .map(|edge| {
16823                        serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
16824                    })
16825                    .collect(),
16826            ),
16827        );
16828    }
16829}
16830
16831/// Every label one item carries, from its content's own connection and nowhere else.
16832///
16833/// There is no second place to read one from: no document this source sends selects the
16834/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
16835/// cannot carry one at all. The module documentation records the three schema facts that
16836/// settle it.
16837fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
16838    optional_nodes(content.get("labels"), "content labels")?
16839        .into_iter()
16840        .flatten()
16841        .map(|v| {
16842            Ok(Label {
16843                id: NativeId(required_str(v, "id")?.to_owned()),
16844                name: required_str(v, "name")?.to_owned(),
16845                color: optional_str(v, "color")?.map(str::to_owned),
16846            })
16847        })
16848        .collect()
16849}
16850
16851/// Each board field value one item holds, as the change token's facets name it: the field's
16852/// name, with the single-select option's name or the text. A value of a type no read here
16853/// selects, and one naming no field, says nothing a person can rename and is left out.
16854fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
16855    field_values
16856        .iter()
16857        .filter_map(|value| {
16858            let field = value.pointer("/field/name")?.as_str()?;
16859            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
16860            Some((field.to_owned(), held.to_owned()))
16861        })
16862        .collect()
16863}
16864
16865/// The digest of the facets of the issue `content`, whose board item's values are
16866/// `field_values` — or `None` when the response did not carry, whole, every member they are read
16867/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
16868/// label, and every field value.
16869///
16870/// What a write's own response spells its token from, by exactly the rules a read takes them by.
16871fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
16872    let ended = |connection: &Value| {
16873        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
16874    };
16875    let labels = content.get("labels").filter(|labels| ended(labels))?;
16876    if !ended(field_values) {
16877        return None;
16878    }
16879    let parent = content.get("parent")?;
16880    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
16881    let kind = BoardKind::of(
16882        content.get("title")?.as_str()?,
16883        !parent.is_null(),
16884        sub_issue_total(content).ok()?,
16885        ItemKind::from_metadata(&slot).ok()?,
16886    );
16887    let labels = self::labels(&json!({"labels": labels})).ok()?;
16888    let nodes = field_values.get("nodes")?.as_array()?;
16889    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
16890}
16891
16892/// The definition of each board field one item's values are values of, in the shape a read
16893/// of the board's own `fields` gives one.
16894///
16895/// A value names its field through a fragment on that field's own type, so the type is
16896/// known from which kind of value it is: a single-select value's field is a
16897/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
16898/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
16899fn field_definitions(field_values: &[Value]) -> Vec<Value> {
16900    field_values
16901        .iter()
16902        .filter_map(|value| {
16903            let field = value.get("field")?.as_object()?;
16904            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
16905            let typename = if value.get("text").is_some() {
16906                "ProjectV2Field"
16907            } else if value.get("name").is_some() {
16908                "ProjectV2SingleSelectField"
16909            } else {
16910                return None;
16911            };
16912            let mut defined = field.clone();
16913            defined.insert("__typename".to_owned(), json!(typename));
16914            // Only a text field holds a text value, so the value says the field's type too.
16915            if typename == "ProjectV2Field" {
16916                defined.insert("dataType".to_owned(), json!("TEXT"));
16917            }
16918            Some(Value::Object(defined))
16919        })
16920        .collect()
16921}
16922
16923fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
16924    let Some(node) = field_values
16925        .iter()
16926        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
16927    else {
16928        return Ok(None);
16929    };
16930    Ok(optional_str(node, "text")?.map(str::to_owned))
16931}
16932
16933fn valid_github_owner(owner: &str) -> bool {
16934    !owner.is_empty()
16935        && owner.len() <= 39
16936        && !owner.starts_with('-')
16937        && !owner.ends_with('-')
16938        && !owner.contains("--")
16939        && owner
16940            .bytes()
16941            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
16942}
16943
16944/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
16945/// neither of the two names a path segment already means.
16946fn valid_github_repository_name(name: &str) -> bool {
16947    !name.is_empty()
16948        && name.len() <= 100
16949        && name != "."
16950        && name != ".."
16951        && name
16952            .bytes()
16953            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
16954}
16955
16956fn valid_environment_name(name: &str) -> bool {
16957    let mut bytes = name.bytes();
16958    bytes
16959        .next()
16960        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
16961        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
16962}
16963
16964/// How many sub-issues one issue has.
16965///
16966/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
16967/// absent or non-integer one is a response this source cannot read — and reading it as
16968/// zero would classify a project as a task, which is exactly the mistake the marker
16969/// exists to keep from happening quietly.
16970fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
16971    let summary = issue
16972        .get("subIssuesSummary")
16973        .ok_or_else(|| SourceError::Malformed {
16974            message: "GitHub issue is missing subIssuesSummary".into(),
16975        })?;
16976    summary
16977        .get("total")
16978        .and_then(Value::as_u64)
16979        .ok_or_else(|| SourceError::Malformed {
16980            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
16981        })
16982}
16983
16984/// One issue's own `number`.
16985///
16986/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
16987/// an issue in this module asks for it. So a read of one that comes back without it, or
16988/// with something that is not an unsigned integer, is a response this source cannot read —
16989/// absence here is **not** "this issue has no number". A draft is the content that has
16990/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
16991/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
16992fn issue_number(issue: &Value) -> Result<u64, SourceError> {
16993    issue
16994        .get("number")
16995        .and_then(Value::as_u64)
16996        .ok_or_else(|| SourceError::Malformed {
16997            message: "GitHub issue number is missing or is not an unsigned integer".into(),
16998        })
16999}
17000
17001/// The `number` a creating mutation answered with, and `None` when it answered without one;
17002/// why a missing one is tolerated is at the call in `create_and_file_issue`.
17003fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
17004    match created.get("number") {
17005        None | Some(Value::Null) => Ok(None),
17006        Some(value) => value
17007            .as_u64()
17008            .map(Some)
17009            .ok_or_else(|| SourceError::Malformed {
17010                message: "GitHub created issue number is not an unsigned integer".into(),
17011            }),
17012    }
17013}
17014
17015fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
17016    value
17017        .get(field)
17018        .and_then(Value::as_str)
17019        .ok_or_else(|| SourceError::Malformed {
17020            message: format!("GitHub response is missing string field {field}"),
17021        })
17022}
17023
17024fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
17025    let found = required_str(value, field)?;
17026    if found.trim().is_empty() {
17027        return Err(SourceError::Malformed {
17028            message: format!("GitHub response has blank string field {field}"),
17029        });
17030    }
17031    Ok(found)
17032}
17033
17034/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
17035/// needs one — Linear spells them too, in its own description field.
17036///
17037/// Restated rather than shared, because a plugin crate depends on the contract crate and
17038/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
17039/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
17040/// source round-trips its own writes perfectly well under its own spelling.
17041const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
17042const METADATA_CLOSE: &str = "\n-->";
17043
17044/// What the composer puts between a non-empty visible body and the slot, and the one thing
17045/// the parser takes off the visible body when it takes the slot off — exactly once, so every
17046/// other trailing byte of the body comes back as it was written.
17047// 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.
17048const METADATA_SEPARATOR: &str = "\n\n";
17049
17050/// The visible body and the metadata slot at the end of it.
17051///
17052/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
17053/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
17054/// own content and is left alone. The visible body is everything before the slot less the
17055/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
17056fn metadata_body(
17057    body: Option<String>,
17058) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
17059    let Some(body) = body else {
17060        return Ok((None, BTreeMap::new()));
17061    };
17062    let Some(slot) = slot_span(&body)? else {
17063        return Ok((Some(body), BTreeMap::new()));
17064    };
17065    let metadata =
17066        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
17067            SourceError::Malformed {
17068                message: format!(
17069                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
17070                ),
17071            }
17072        })?;
17073    let before = &body[..slot.start];
17074    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
17075    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
17076}
17077
17078/// Where the metadata slot sits in one body, as byte offsets into it.
17079struct SlotSpan {
17080    /// Where [`METADATA_OPEN`] begins.
17081    start: usize,
17082    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
17083    encoded_start: usize,
17084    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
17085    encoded_end: usize,
17086    /// Just past [`METADATA_CLOSE`].
17087    end: usize,
17088}
17089
17090/// The slot at the very end of `body`, or `None` when it has none.
17091///
17092/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
17093/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
17094/// slot.
17095fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
17096    let Some(start) = body.rfind(METADATA_OPEN) else {
17097        return Ok(None);
17098    };
17099    let encoded_start = start + METADATA_OPEN.len();
17100    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
17101        return Err(SourceError::Malformed {
17102            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
17103        });
17104    };
17105    let encoded_end = encoded_start + relative_end;
17106    let end = encoded_end + METADATA_CLOSE.len();
17107    if !body[end..].trim().is_empty() {
17108        return Ok(None);
17109    }
17110    Ok(Some(SlotSpan {
17111        start,
17112        encoded_start,
17113        encoded_end,
17114        end,
17115    }))
17116}
17117
17118/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
17119/// slot as it was.
17120///
17121/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
17122/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
17123/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
17124/// or alone in an empty body — and a body with no slot that is given no metadata is
17125/// returned as it is.
17126fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
17127    let encoded = if metadata.is_empty() {
17128        None
17129    } else {
17130        Some(
17131            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
17132                message: error.to_string(),
17133            })?,
17134        )
17135    };
17136    Ok(match (slot_span(body)?, encoded) {
17137        (Some(slot), Some(encoded)) => format!(
17138            "{}{encoded}{}",
17139            &body[..slot.encoded_start],
17140            &body[slot.encoded_end..]
17141        ),
17142        (Some(slot), None) => {
17143            let before = &body[..slot.start];
17144            format!(
17145                "{}{}",
17146                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
17147                &body[slot.end..]
17148            )
17149        }
17150        (None, None) => body.to_owned(),
17151        (None, Some(encoded)) if body.is_empty() => {
17152            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
17153        }
17154        (None, Some(encoded)) => {
17155            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
17156        }
17157    })
17158}
17159
17160/// `body` with everything before its metadata slot replaced by `content`, and the slot
17161/// itself kept byte for byte.
17162///
17163/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
17164/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
17165/// `content` is empty — so a read of the result reports `content` as the visible body and
17166/// the slot's metadata exactly as it was.
17167fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
17168    let Some(slot) = slot_span(body)? else {
17169        return Ok(content.to_owned());
17170    };
17171    let kept = &body[slot.start..];
17172    Ok(if content.is_empty() {
17173        kept.to_owned()
17174    } else {
17175        format!("{content}{METADATA_SEPARATOR}{kept}")
17176    })
17177}
17178
17179/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
17180fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
17181    if entries.is_empty() {
17182        metadata.remove(key);
17183    } else {
17184        metadata.insert(
17185            key.to_owned(),
17186            Value::Array(
17187                entries
17188                    .iter()
17189                    .map(|entry| Value::String(entry.as_str().to_owned()))
17190                    .collect(),
17191            ),
17192        );
17193    }
17194}
17195
17196fn compose_body(
17197    content: Option<&str>,
17198    metadata: &BTreeMap<String, Value>,
17199) -> Result<Option<String>, SourceError> {
17200    let visible = content.unwrap_or_default();
17201    if metadata.is_empty() {
17202        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
17203    }
17204    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
17205        message: error.to_string(),
17206    })?;
17207    Ok(Some(if visible.is_empty() {
17208        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
17209    } else {
17210        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
17211    }))
17212}
17213
17214fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
17215    value
17216        .get(field)
17217        .and_then(Value::as_bool)
17218        .ok_or_else(|| SourceError::Malformed {
17219            message: format!("GitHub response is missing boolean field {field}"),
17220        })
17221}
17222fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
17223    match value.get(field) {
17224        None | Some(Value::Null) => Ok(None),
17225        Some(value) => value
17226            .as_str()
17227            .map(Some)
17228            .ok_or_else(|| SourceError::Malformed {
17229                message: format!("GitHub response field {field} is not a string or null"),
17230            }),
17231    }
17232}
17233fn optional_nodes<'a>(
17234    connection: Option<&'a Value>,
17235    name: &str,
17236) -> Result<Option<&'a Vec<Value>>, SourceError> {
17237    match connection {
17238        None | Some(Value::Null) => Ok(None),
17239        Some(value) => value
17240            .get("nodes")
17241            .and_then(Value::as_array)
17242            .map(Some)
17243            .ok_or_else(|| SourceError::Malformed {
17244                message: format!("GitHub {name}.nodes is not an array"),
17245            }),
17246    }
17247}
17248fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
17249    let page_info = connection
17250        .get("pageInfo")
17251        .ok_or_else(|| SourceError::Malformed {
17252            message: format!("GitHub {name} has no pageInfo"),
17253        })?;
17254    if required_bool(page_info, "hasNextPage")? {
17255        return Err(SourceError::Malformed {
17256            message: format!(
17257                "GitHub {name} exceeds the supported nested connection size of {size}"
17258            ),
17259        });
17260    }
17261    Ok(())
17262}
17263fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
17264    optional_str(value, field)?
17265        .map(|timestamp| {
17266            timestamp.parse().map_err(|error| SourceError::Malformed {
17267                message: format!("GitHub response field {field} is not a timestamp: {error}"),
17268            })
17269        })
17270        .transpose()
17271}
17272fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
17273    if page.limit == 0 {
17274        Err(SourceError::Config {
17275            message: "page limit must be at least 1".into(),
17276        })
17277    } else {
17278        Ok(())
17279    }
17280}
17281fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
17282    let page = connection
17283        .get("pageInfo")
17284        .filter(|value| value.is_object())
17285        .ok_or_else(|| SourceError::Malformed {
17286            message: "GitHub connection is missing pageInfo".into(),
17287        })?;
17288    if required_bool(page, "hasNextPage")? {
17289        let cursor = required_str(page, "endCursor")?;
17290        validate_cursor_progress(None, cursor)?;
17291        Ok(Some(Cursor(cursor.into())))
17292    } else {
17293        Ok(None)
17294    }
17295}
17296fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
17297    if next.is_empty() || previous == Some(next) {
17298        Err(SourceError::Malformed {
17299            message: "GitHub pagination cursor is empty or did not advance".into(),
17300        })
17301    } else {
17302        Ok(())
17303    }
17304}
17305/// The version of this plugin's opaque narrowing-search cursor.
17306pub const SEARCH_CURSOR_VERSION: u32 = 4;
17307
17308#[derive(Serialize, Deserialize)]
17309#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
17310enum SearchConnection {
17311    Initial {},
17312    Continuing { after: Cursor },
17313    Exhausted {},
17314}
17315impl SearchConnection {
17316    fn after(&self) -> Option<&str> {
17317        match self {
17318            Self::Continuing { after } => Some(&after.0),
17319            _ => None,
17320        }
17321    }
17322    fn exhausted(&self) -> bool {
17323        matches!(self, Self::Exhausted { .. })
17324    }
17325    /// Whether a cursor naming this position, `offset` rows into its page, is one this
17326    /// plugin could have handed out: a page is resumed only part of the way through it — an
17327    /// offset of a whole page or more would skip rows nobody was given — an initial page
17328    /// only once some of it was handed out, and an exhausted connection has no page to be
17329    /// part of the way through.
17330    fn valid_resume(&self, offset: usize) -> bool {
17331        let within = offset < SEARCH_PAGE_SIZE as usize;
17332        match self {
17333            Self::Initial { .. } => offset > 0 && within,
17334            Self::Continuing { after } => !after.0.is_empty() && within,
17335            Self::Exhausted { .. } => offset == 0,
17336        }
17337    }
17338}
17339
17340/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
17341#[derive(Serialize, Deserialize)]
17342#[serde(deny_unknown_fields)]
17343struct SearchPosition {
17344    version: u32,
17345    connection: SearchConnection,
17346    /// How many rows of the page `connection` starts were already handed out.
17347    #[serde(default, skip_serializing_if = "is_zero")]
17348    offset: usize,
17349    #[serde(default, skip_serializing_if = "Vec::is_empty")]
17350    seen: Vec<NativeId>,
17351    #[serde(default, skip_serializing_if = "Vec::is_empty")]
17352    own: Vec<NativeId>,
17353}
17354impl Default for SearchPosition {
17355    fn default() -> Self {
17356        Self {
17357            version: SEARCH_CURSOR_VERSION,
17358            connection: SearchConnection::Initial {},
17359            offset: 0,
17360            seen: Vec::new(),
17361            own: Vec::new(),
17362        }
17363    }
17364}
17365
17366fn is_zero(offset: &usize) -> bool {
17367    *offset == 0
17368}
17369
17370fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
17371    cursor.map_or(Ok(0), |c| {
17372        c.0.parse().map_err(|_| SourceError::Config {
17373            message: "page cursor is invalid".into(),
17374        })
17375    })
17376}
17377fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
17378    if offset > items.len() {
17379        return Page::last(vec![]);
17380    }
17381    let tail = items.split_off(offset);
17382    let mut selected = tail;
17383    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
17384    selected.truncate(limit);
17385    Page {
17386        items: selected,
17387        next,
17388    }
17389}
17390
17391impl GitHubProjectsSource {
17392    /// The body [`Self::write_item`] would compose for `incoming`, sized in the characters
17393    /// [`BODY_LIMIT`] counts, without a request: its slot as `slot_metadata` writes it, every
17394    /// asset reference rewritten to its recorded URL or — for one not uploaded yet — to an
17395    /// address as long as GitHub's, and the heading markup this board's bodies carry.
17396    ///
17397    /// What the write reads rather than knows is taken as it most often lands: the issue in
17398    /// the one repository the item names, else the configured one, and every edge on the
17399    /// board's own relationships rather than in the slot.
17400    fn body_size(&self, incoming: &Incoming<'_>) -> onetaskgraph_plugin_api::BodySize {
17401        let own = match incoming.repositories {
17402            [named] => Some(named.clone()),
17403            _ => self
17404                .repository
17405                .as_ref()
17406                .and_then(|target| Repository::try_from(target.origin()).ok()),
17407        };
17408        let mut slot = slot_metadata(incoming, own.as_ref(), &[]);
17409        let content = match incoming.assets {
17410            Some(assets) => {
17411                let mut uploads = onetaskgraph_plugin_api::AssetUploads::default();
17412                for asset in &assets.assets {
17413                    let url = assets
17414                        .recorded_assets
17415                        .as_ref()
17416                        .and_then(|recorded| recorded.reusable(&asset.name, &asset.sha256))
17417                        .unwrap_or(PREDICTED_ATTACHMENT_URL)
17418                        .to_owned();
17419                    uploads.0.insert(
17420                        asset.name.clone(),
17421                        onetaskgraph_plugin_api::AssetUpload {
17422                            sha256: asset.sha256.clone(),
17423                            url,
17424                        },
17425                    );
17426                }
17427                incoming.content.map(|content| {
17428                    onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads)
17429                })
17430            }
17431            None => incoming.content.map(str::to_owned),
17432        };
17433        let marked = content.as_deref().map(|content| self.marked(content));
17434        let size = compose_body(marked.as_deref(), &slot)
17435            .ok()
17436            .flatten()
17437            .map_or(0, |body| body.chars().count());
17438        onetaskgraph_plugin_api::BodySize {
17439            size: u64::try_from(size).unwrap_or(u64::MAX),
17440            limit: Some(BODY_LIMIT),
17441        }
17442    }
17443
17444    async fn write_task_assets(
17445        &self,
17446        write: &ItemWrite<Task>,
17447        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
17448    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
17449        self.write_item(
17450            &self.task_incoming(write, assets)?,
17451            write.target.as_ref(),
17452            &write.depends_on,
17453        )
17454        .await
17455    }
17456
17457    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
17458    /// are known to be ones this source can write.
17459    fn task_incoming<'a>(
17460        &self,
17461        write: &'a ItemWrite<Task>,
17462        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
17463    ) -> Result<Incoming<'a>, SourceError> {
17464        let near = write.target.as_ref().unwrap_or(&write.item.id);
17465        for (key, entries) in [
17466            (TaskRef::DELIVERS_KEY, &write.item.delivers),
17467            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
17468        ] {
17469            TaskRef::listed(key, near, Some(&self.name), entries.clone())
17470                .map_err(|message| SourceError::Refused { message })?;
17471        }
17472        if self.priorities.is_none() && write.item.priority != Priority::None {
17473            return Err(self.holds_no_priority());
17474        }
17475        if write.item.assignees.is_some() && !self.assignees {
17476            return Err(onetaskgraph_plugin_api::unwritable_field(KIND, "assignees"));
17477        }
17478        Ok(Incoming {
17479            written: Written::Work(ItemKind::Task, &write.item.status),
17480            title: &write.item.title,
17481            content: write.item.content.as_deref(),
17482            assets,
17483            labels: &write.item.labels,
17484            metadata: &write.item.metadata,
17485            repositories: &write.item.repositories,
17486            classification: write.item.classification,
17487            parent: write.item.project.as_ref(),
17488            delivers: &write.item.delivers,
17489            delivered_by: &write.item.delivered_by,
17490            priority: self.priorities.as_ref().map(|_| write.item.priority),
17491            assignees: write.item.assignees.as_deref(),
17492        })
17493    }
17494    async fn write_document_assets(
17495        &self,
17496        write: &ItemWrite<Document>,
17497        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
17498    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
17499        // A document takes part in no dependency graph, so there is no far end to write
17500        // natively and none to record: a caller naming one is told so rather than having it
17501        // stored under the reserved key, where a later read would report an edge the
17502        // contract says cannot exist.
17503        if !write.depends_on.is_empty() {
17504            return Err(SourceError::Refused {
17505                message: format!(
17506                    "this write names {} dependencies for a document, and a document takes \
17507                     part in no dependency graph; next: put the dependency on the task or \
17508                     project the document is about",
17509                    write.depends_on.len()
17510                ),
17511            });
17512        }
17513        self.write_item(
17514            &Incoming {
17515                written: Written::Document,
17516                title: &write.item.title,
17517                content: write.item.content.as_deref(),
17518                assets,
17519                labels: &write.item.labels,
17520                metadata: &write.item.metadata,
17521                repositories: &write.item.repositories,
17522                classification: write.item.classification,
17523                parent: write.item.project.as_ref(),
17524                delivers: &[],
17525                delivered_by: &[],
17526                priority: None,
17527                assignees: None,
17528            },
17529            write.target.as_ref(),
17530            &[],
17531        )
17532        .await
17533    }
17534}
17535
17536/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
17537/// itself, for the two things no journey can observe.
17538///
17539/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
17540/// with and without the call, that a settlement, a board listing and a metadata search each
17541/// read afresh after it — the resolved records, the written-item overlay, the board and its
17542/// search, and the narrowed searches. What they cannot reach is the held field definitions,
17543/// because a status write naming an option a person deleted is refused the same whether or
17544/// not the list is held, and a poisoned lock, because nothing outside the source can panic
17545/// while one of its locks is held. So these assert those directly, and every other holder
17546/// beside them so a holder added later without a clear in the call fails here.
17547#[cfg(test)]
17548mod end_command_tests {
17549    use super::*;
17550
17551    struct Token;
17552
17553    impl SecretResolver for Token {
17554        fn get(&self, var: &str) -> Option<SecretString> {
17555            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
17556        }
17557    }
17558
17559    fn source() -> GitHubProjectsSource {
17560        let config = serde_json::from_value(json!({
17561            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
17562            // Nothing here is sent: the source is only built and its state inspected.
17563            "endpoint": "http://127.0.0.1:9/graphql",
17564        }))
17565        .expect("a usable configuration");
17566        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
17567            .expect("the source builds")
17568    }
17569
17570    /// One issue as a board read answers it.
17571    fn resolved(source: &GitHubProjectsSource) -> Resolved {
17572        source
17573            .resolve(&json!({
17574                "id": "ITEM-1",
17575                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
17576                            "body": "what a person may since have edited", "state": "OPEN",
17577                            "stateReason": null, "url": null, "number": 1,
17578                            "subIssuesSummary": {"total": 0},
17579                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
17580                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
17581            }))
17582            .expect("the item reads")
17583            .expect("an issue")
17584    }
17585
17586    /// Hold something in every holder the call clears, and the repository id it keeps.
17587    fn fill(source: &GitHubProjectsSource) {
17588        let item = resolved(source);
17589        source.created.lock().unwrap().push(item.clone());
17590        source.updated.lock().unwrap().push(item.clone());
17591        *source.board_cache.lock().unwrap() = Some(Board {
17592            id: "PVT-board".into(),
17593            fields: json!({"nodes": []}),
17594            items: vec![item.clone()],
17595        });
17596        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
17597        source
17598            .narrowed_cache
17599            .lock()
17600            .unwrap()
17601            .insert("status:todo".into(), vec![item.clone()]);
17602        source.children_cache.lock().unwrap().insert(
17603            NativeId("P-1".into()),
17604            (NativeId("P-1".into()), vec![item.clone()]),
17605        );
17606        source
17607            .search_next
17608            .lock()
17609            .unwrap()
17610            .insert("status:todo".into(), Some("cursor".into()));
17611        source
17612            .resolved_cache
17613            .lock()
17614            .unwrap()
17615            .insert(item.id.clone(), item);
17616        *source.fields_cache.lock().unwrap() = Some(BoardFields {
17617            id: BoardId::parse("PVT-board").unwrap(),
17618            fields: json!({"nodes": []}),
17619        });
17620        source
17621            .repository_cache
17622            .lock()
17623            .unwrap()
17624            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
17625    }
17626
17627    fn assert_dropped(source: &GitHubProjectsSource) {
17628        assert!(source.created().unwrap().is_empty(), "created");
17629        assert!(source.updated().unwrap().is_empty(), "updated");
17630        assert!(source.board_cache().unwrap().is_none(), "board");
17631        assert!(source.search_cache.lock().unwrap().is_none(), "search");
17632        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
17633        assert!(
17634            source.children_cache.lock().unwrap().is_empty(),
17635            "project children"
17636        );
17637        assert!(
17638            source.search_next.lock().unwrap().is_empty(),
17639            "search paging"
17640        );
17641        assert!(
17642            source.resolved_cache().unwrap().is_empty(),
17643            "resolved records"
17644        );
17645        assert!(source.fields_cache().unwrap().is_none(), "board fields");
17646        assert_eq!(
17647            source.repository_cache().unwrap().len(),
17648            1,
17649            "a repository's node id stays valid and is kept"
17650        );
17651    }
17652
17653    fn end(source: &GitHubProjectsSource) {
17654        tokio::runtime::Builder::new_current_thread()
17655            .build()
17656            .unwrap()
17657            .block_on(source.end_command())
17658            .expect("the command ends");
17659    }
17660
17661    #[test]
17662    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
17663        let source = source();
17664        fill(&source);
17665        end(&source);
17666        assert_dropped(&source);
17667    }
17668
17669    #[test]
17670    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
17671        fn poison<T: Send>(held: &Mutex<T>) {
17672            std::thread::scope(|scope| {
17673                let _ = scope
17674                    .spawn(|| {
17675                        let _guard = held.lock().unwrap();
17676                        panic!("a failure while the lock is held");
17677                    })
17678                    .join();
17679            });
17680            assert!(held.is_poisoned());
17681        }
17682        let source = source();
17683        fill(&source);
17684        poison(&source.created);
17685        poison(&source.updated);
17686        poison(&source.board_cache);
17687        poison(&source.search_cache);
17688        poison(&source.narrowed_cache);
17689        poison(&source.search_next);
17690        poison(&source.resolved_cache);
17691        poison(&source.fields_cache);
17692        assert!(
17693            source.resolved_cache().is_err(),
17694            "a poisoned lock is refused before the call"
17695        );
17696        end(&source);
17697        assert_dropped(&source);
17698    }
17699}
17700
17701#[cfg(test)]
17702mod unprojectable_fields_tests {
17703    use super::*;
17704
17705    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
17706    fn documented() -> std::collections::BTreeSet<String> {
17707        let document = std::fs::read_to_string(
17708            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
17709        )
17710        .expect("docs/metadata.md is readable");
17711        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
17712        let lead = "a `field` equal, ignoring case, to ";
17713        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
17714        let sentence = &document[start..];
17715        let sentence = &sentence[..sentence
17716            .find("; and two entries naming one field")
17717            .expect("the refused-field sentence ends at the duplicate rule")];
17718        let (own, github) = sentence
17719            .split_once(" or a field GitHub owns on every board (")
17720            .expect("the sentence names GitHub's own fields");
17721        own.split('`')
17722            .skip(1)
17723            .step_by(2)
17724            .chain(
17725                github
17726                    .strip_suffix(')')
17727                    .expect("GitHub's own fields close the sentence")
17728                    .split(", "),
17729            )
17730            .map(str::to_owned)
17731            .collect()
17732    }
17733
17734    #[test]
17735    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
17736        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
17737            .iter()
17738            .map(|&name| name.to_owned())
17739            .collect();
17740        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
17741        let instance = SourceName::new("work").unwrap();
17742        for name in UNPROJECTABLE_FIELDS {
17743            for spelled in [name.to_owned(), name.to_uppercase()] {
17744                let entry = MetadataFieldConfig {
17745                    field: spelled.clone(),
17746                    key: "team.machine".to_owned(),
17747                    path: Vec::new(),
17748                };
17749                let refused = MetadataField::resolve(vec![entry], &instance)
17750                    .expect_err("an unprojectable field is refused");
17751                assert!(
17752                    refused.to_string().contains(&format!("{name:?}")),
17753                    "{spelled}: {refused}"
17754                );
17755            }
17756        }
17757    }
17758}