Skip to main content

onetaskgraph_github_projects/
lib.rs

1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
149//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
150//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `reconcile_status` | **Supported,** and proven by `onetaskgraph-github-projects-e2e`'s `status_reconcile.rs`: the board's own `ProjectV2.items(query:)` filter, `is:closed`, read page by page and never the unfiltered items, each candidate confirmed from its own item node — a closed task whose card is not at the option the mapping gives its closure's category. |
161//! | `complete_by_issue` | **Supported,** and proven by `status_complete.rs` there and in the engine's suite: each issue a reference names read in one aliased request per fifty — its repository, its number, its first page of board memberships with each card's `Status` read by name — and nothing else of the board. |
162//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
163//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
164//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
165//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
166//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
167//!
168//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
169//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
170//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
171//! and the raw bytes as the request body. The numeric repository id is read at most once
172//! per repository per source instance. A loopback API endpoint also selects the loopback
173//! uploads host; production needs no assets repository, commits or extra configuration.
174//! Every returned JSON `url` is read with the same token before a body is written: only
175//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
176//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
177//! asset and HTTP status and says the token type may not be accepted by the upload
178//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
179//! fine-grained PATs or OAuth tokens.
180//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
181//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
182//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
183//! and verified afresh, in the repository the issue already lives in on an update.
184//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
185//! Uploads take the same mutation spacing as content writes. Both uploads and verification
186//! reads wait out classified rate limits within the configured per-call retry budget,
187//! on the supplied clock; verifying reads take no mutation slot.
188//!
189//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
190//! source has documents and that its tasks have comments, both of which hold — and the three
191//! facts behind the uniform `Native` on the
192//! predicates beside it are recorded below rather than re-derived, because a reader who
193//! takes `Native` to mean *the remote service filters* will read that uniformity as a
194//! lie.
195//!
196//! First, the plugin contract defines `Support::Native` as *the source applies this
197//! predicate itself*, and says nothing about where it applies it. What the declaration
198//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
199//! — so that the engine may push it down and apply nothing of its own.
200//!
201//! Second, this source can keep that promise for every predicate at no additional API
202//! cost, because whichever of the reads below answers a query has already read every
203//! candidate that query will return before it filters anything. Filtering those items is
204//! in-process work over data already in hand.
205//!
206//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
207//! applied in process over what that question returned. A project filter has a relationship — a
208//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
209//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
210//! a search for metadata values, is the board-scoped issue search carrying the text and each
211//! value as quoted phrases; an origin is the board's own field filter over its origin field
212//! beside the same search for the id. **The text search narrows, and that is this source's
213//! declared semantics:** GitHub matches whole words where the substring rule this source and
214//! the local Markdown source confirm with would match inside one, so an item holding the text
215//! only inside a longer word is never a candidate. Every item returned does contain the text.
216//! A project query's text, and a document query's scoped to no project, is that same search
217//! and narrows on the same terms, its candidates confirmed by their kind as well.
218//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
219//! so those three are applied in process over the candidates, and a query carrying none of
220//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
221//! engine compensate for work this source has already done, and declaring `projects` native
222//! while ignoring the filter (which this source once did) silently returns another project's
223//! tasks, because the engine trusts the declaration and applies nothing locally.
224//!
225//! # The three ways this source reaches an item, and what each costs
226//!
227//! A board read is charged for what its *nested* connections could return rather than for
228//! what was asked, so one whole-board read costs the same whether the question was about
229//! one project or about all of them. That is why a question about one project is never
230//! answered by reading the board:
231//!
232//! | The question | What is sent | What it costs |
233//! | --- | --- | --- |
234//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
235//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
236//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
237//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
238//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
239//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
240//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
241//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
242//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
243//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
244//! | every task, every document, every label, when nothing above narrows the question | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
245//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
246//!
247//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
248//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
249//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
250//! equal to declared points equal to the row. They include the origin lookup and the
251//! field/repository discovery a create needs. A bound re-copy changes status, priority,
252//! content and metadata; comment recount means a subsequent detail read. Each request here
253//! costs one declared point. A membership beyond the embedded page can additionally require
254//! the one-point membership recovery described above. A bound re-copy of a task filed under a
255//! project adds one read, the engine confirming that project's link by its own id once per
256//! command; and the same-source far ends a write newly names — those that do not already block
257//! the item, whose own read answered for them — are read together by their own ids,
258//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
259//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
260//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
261//!
262//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
263//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
264//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
265//! holds both halves.
266//!
267//! **An existing item is written body last.** A bound re-copy and a `task update` send its
268//! board fields first — the `Status` option and the `Priority` together, in one request — then
269//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
270//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
271//! undoing an earlier field when a later one fails, so that order is what makes a write
272//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
273//! the one piece of metadata written before the body, an origin a copy re-points, is put back
274//! when a later write is refused — and when putting it back is refused too, the write's own
275//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
276//! of those writes in turn, whole and as one aliased field failing after the one before it.
277//!
278//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
279//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
280//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
281//!
282//! - **A board is accepted at creation but its item is not answered, so a create still files
283//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
284//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
285//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
286//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
287//!   against a real board on 2026-10-01 with a create sending the board there and reading the
288//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
289//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
290//!   refused "Content already exists in this project" — GitHub had filed the issue after
291//!   answering, and refuses a second filing rather than answering with the item it holds. A
292//!   create therefore sends no `projectV2Ids` and files the issue with
293//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
294//!   real is the read before it: the board's fields and the repository's id together, in
295//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
296//! - **A comment still reads its target first, so a comment is 2 requests.**
297//!   `AddCommentInput.subjectId: ID!` is declared there with
298//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
299//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
300//!   project's issue, a document's issue, an issue on no board of this source and a pull
301//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
302//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
303//!   refuses those by name.
304//!
305//! | Verb | Requests / points | Documents |
306//! | --- | --- | --- |
307//! | new copy | 4 | ORIGIN_LOOKUPS (the origin, the board's fields and the repository's node and numeric ids together, a page of one carrier for each origin), CREATE_ISSUE filing the issue on the board and under its project, CREATED_BOARD_ITEMS once GitHub has added it, WRITE_FIELD_VALUES |
308//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
309//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS when a board field differs, then UPDATE_ISSUE last carrying only the title, body or state that differs — none, when nothing does |
310//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
311//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
312//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
313//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
314//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
315//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
316//! | recount | 1 | ISSUE_DETAIL |
317//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
318//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
319//! | content | 2 | ISSUE, UPDATE_ISSUE |
320//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
321//! | document metadata, `--expect` a version | 2 | ISSUE, then UPDATE_ISSUE only when the issue is still at that version — the precondition GitHub's own update cannot take — primed with what the read answered |
322//! | update | 3 | `task update` naming any of title, body, metadata, status and priority — all five included: ISSUE, UPDATE_FIELDS (the status option and the priority together), UPDATE_ISSUE (title, body with its metadata slot, and state) last |
323//! | record only | 1 | ISSUE |
324//!
325//! <!-- github-search-paging:start -->
326//! Board-scoped text, metadata, project-name and comment-activity searches send every
327//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
328//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
329//! needs rows. A page is never resized to the rows still needed: GitHub orders one
330//! search differently at different page sizes, so one fixed size makes a paged walk
331//! send exactly the requests one whole read sends, and the answer's order is the order
332//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
333//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
334//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
335//! returned and fetched pages: a limit is sliced from the pages it needs, and local
336//! confirmation can require more candidates than matching rows. Walking all pages
337//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
338//! cursor and how far into that page the last answer stopped, and resumes in the same
339//! process or a new one, without duplicates or gaps. It carries no rows: one process
340//! sends each page's search once, and a new process re-reads only the page it resumes
341//! in, then sends a further page once, never as a re-read, only when its limit still
342//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
343//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
344//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
345//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
346//! not required to include the original process's writes still omitted by the index.
347//! <!-- github-search-paging:end -->
348//!
349//! The board half of an issue — its board item's id, its `Status` option and this
350//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
351//! an item reached any of those ways resolves through the same
352//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
353//! same status, the same labels and the same qualified id. That connection comes back a
354//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
355//! on the page in hand and — only if that page reports more of the connection — in the
356//! last row's read of that one issue's memberships, resumed from the page's own cursor and
357//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
358//! report, which is what keeps an id naming another repository's issue from being answered
359//! as an item of this board; and because the page is where the search starts rather than
360//! where it ends, that answer is one about a connection read to exhaustion and never about
361//! an unread page. Nothing costs the extra read but an issue on more boards than a page
362//! holds: an issue this board really does not hold reports no next page, so its
363//! memberships are already exhausted where they arrived.
364//!
365//! **No document here selects the board's own `Labels` field, and nothing is lost by
366//! that.** An item's labels are read from its content alone, wherever that content is
367//! reached: the three documents above select `Issue.labels` on the fragment, and
368//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
369//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
370//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
371//! create one, and `ProjectV2FieldValue` — the whole of what
372//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
373//! it from the content, for every content type it exists on, and there is nothing it can
374//! hold that the content does not already say: for an `Issue` it *is* that issue's own
375//! labels, so selecting it beside them unions a set with itself.
376//!
377//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
378//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
379//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
380//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
381//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
382//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
383//! label set, and that set is the fixture issue's own, by
384//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
385//! item whose content is a draft reports an empty set, by
386//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
387//! selection is held over [`graphql::DOCUMENTS`] by
388//! `no_document_selects_the_boards_own_labels_field`.
389//!
390//! The whole-board row is still the board's own item connection, and deliberately: a
391//! **draft** board item is not an issue, so no search can list one, and the reads that have
392//! to answer for the whole board are the ones whose cost is the board's size anyway.
393//!
394//! **A question about one item this source already names by id never lists the board.**
395//! Whether that item is on this board, and what its board fields are, is answered by reading
396//! that item — its own `Issue.projectItems`, walked to exhaustion by
397//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
398//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
399//! holds. That covers a write's destination, the project a new item is filed under, a
400//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
401//! and the delete that takes back an item a copy made. What such a write needs of the board
402//! and the item does not carry — the board's id, the `Status` and origin field definitions —
403//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
404//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
405//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
406//! minutes. Scanning this host's 842-item board has refused a document copy and an update
407//! even though the items' own reads named that board. A scan there gives the wrong answer
408//! as well as paying for every page. So a `board.items` lookup does not belong on any of
409//! those paths.
410//!
411//! **What a read may return is capped too, and that cap is on the document rather than on
412//! the board.** GitHub limits the number of nodes **one query may return** to
413//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
414//! an error naming the connection the count crossed at, not a slow or a partial result.
415//! Every board this source reads is refused the same way, so no board is too big for these
416//! documents and none is small enough to save one that is over.
417//!
418//! The count is arithmetic over the document's own text: each connection contributes the
419//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
420//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
421//! restate them — `github-graphql-node-count` implements them, and
422//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
423//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
424//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
425//! same text on every run and fails naming any that reaches the limit — so a connection
426//! added to a shared fragment is caught there rather than by GitHub.
427//!
428//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
429//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
430//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
431//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
432//! effectively squared there, which is why it is the one the limit is most sensitive to.
433//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
434//! page of memberships misses is recovered by one further read rather than refused, so it
435//! buys a bound every read pays for at the price of a request only a multi-board issue
436//! pays.
437//!
438//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
439//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
440//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
441//! `cost` is rate-limit points, metered per hour across everything one credential does; it
442//! is what the two limiters [`Limiter`] tells apart meter, and a document under
443//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
444//! that second number, and `tests/point_cost.rs` pins every document in
445//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
446//! one under, the pin itself is the check. The credentialed lane reconciles both figures
447//! against GitHub's own, off a probe it already sends.
448//!
449//! **What is pinned that way is a per-document price and never a session's.** The record in
450//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
451//! **requests** and **worst-case nodes** — and neither is points. What one whole session
452//! consumes of the hourly point allowance is observable only from a credentialed run's own
453//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
454//! and what `tests/live.rs` prints at the end of every run.
455//!
456//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
457//!
458//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
459//! enumerations of a board can supply one alone.** Resolving a node id is strongly
460//! consistent, so a read by id and a project's own sub-issues are already current. The
461//! other two are not, and they are behind by different amounts and in different directions:
462//!
463//! - GitHub's **issue search** is an index and answers a write made moments ago with the
464//!   value from before it — usually for a second or two.
465//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
466//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
467//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
468//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
469//!
470//! That second one is a measurement rather than a caution. This repository's own
471//! credentialed journey writes a project and waits for the board to report it, then writes a
472//! task and waits for the same thing seconds later on the same board: the project wait is
473//! answered through the search and converged in two or three attempts in each of three runs,
474//! and the task wait is answered through `ProjectV2.items` and converged in none of them
475//! inside thirty. Separately, an item added to a second and larger board was read back by
476//! `Issue.projectItems` on that board's own id while every one of that connection's nine
477//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
478//! through the lagging one alone is what had a board read deny an issue that had certainly
479//! landed on it.
480//!
481//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
482//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
483//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
484//! search reports what the projection is behind on. What closes the last
485//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
486//! source answers is completed with what this process itself wrote, so an item created
487//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
488//! nothing is written down, and the record dies with the process. **A wait that has to
489//! observe GitHub's own data cannot be answered from that record** — which is why the
490//! credentialed journey asks through a source built afresh, and why the union above rather
491//! than a longer wait is what makes such a wait converge.
492//!
493//! **A narrowed read is the same bargain, stated for each of the three predicates it
494//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
495//! than walking the board, and every such answer is completed with what this process wrote —
496//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
497//! each filtered by the same predicates as the rest — so an item this command wrote a moment
498//! ago is returned by a query that matches it whether or not the index has caught up. An item
499//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
500//! consistent. What is left is stated rather than papered over:
501//!
502//! | Read | Finds | Behind by |
503//! | --- | --- | --- |
504//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
505//! | origin, first read | the board's field filter over the origin field — every carrier, whichever release wrote it | what `ProjectV2.items` is behind on, which the measurements above put in minutes |
506//! | origin, second read | the issue search for the id in the body, where a write of this release mirrors it | a second or two, as any search |
507//! | origin, third read | this process's own writes | nothing |
508//!
509//! So an origin carrier another process added within the last second or two, before either
510//! index has it, can be missing from an origin query, and one written by the release before
511//! this one — its origin in the field alone — can be missing for as long as the board's own
512//! item connection is behind on it. A copy that must not duplicate its own earlier write
513//! relies on the link it records, not on either index. **A board draft is not an issue**, so
514//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
515//! lists one, the origin lookup drops any the board's own field filter names, and one this
516//! process wrote is not added back either.
517//!
518//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
519//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
520//! body's metadata slot, so the issue search can find it in seconds. The field is
521//! authoritative: this source reads an item's origin from the field alone, so a slot that
522//! disagrees with it, or holds one where the field holds none, is never read as a second
523//! origin — and the release before this one reads the slot, drops that key's copy for the
524//! field's, and sees the same one origin.
525//!
526//! Filtering happens before paging, so a page of a filtered result is a page of the
527//! survivors rather than the survivors of a page. Label matching and the substring rule a
528//! text candidate is confirmed by answer the same question the same way the local Markdown
529//! source's do; which candidates a text search has to confirm is GitHub's word match, which
530//! is the one place the two sources can answer the same text differently.
531//!
532//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
533//! has one source, `capabilities`, and the note above is the reasoning behind it rather
534//! than a second copy of it: without the three facts recorded here a reader takes the
535//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
536//! crate's own capabilities test, which pins every field of it against a fully spelled-out
537//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
538//! fails to compile there rather than going unasserted. -->
539//! The fixture-server tests above run wherever this crate is selected; the credentialed
540//! lane runs in the same required check, beside them, and can fail it — it verifies the
541//! current schema, then drives every field of the table above against the real board. It builds its own fixture there — two projects, one task filed under each,
542//! one filed under neither, a label on one of the three and a closed status on another —
543//! because that shape is what tells an honoured predicate from an ignored one: a board
544//! holding a single project answers a project filter the same way whether or not this
545//! source applies it, which is exactly how the defect above went unseen.
546//!
547//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
548//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
549//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
550//! session or request, independently of the live demand. Fix that variable in the machine's
551//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
552//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
553//! policy crate; absent credentials or nominations otherwise skip.
554//!
555//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
556//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
557//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
558//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
559//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
560//! after its run reads back as `completed`, immediately before the listed-artifact batch.
561//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
562//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
563//! and the board's `onetaskgraph.origin` field untouched.
564//!
565//! # What a session of requests costs, and where the report is
566//!
567//! This source records **every** request it sends into [`accounting::Accounting`], at
568//! `send_once` — the one place a request leaves this crate, which is why a read path added
569//! later is counted without anybody remembering to count it. That is the whole of what this
570//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
571//! session's spend is arrived at, and what it deliberately does not know are set out.
572//!
573//! What one whole session of the live journey costs, counted that way against this crate's
574//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
575//! reduction it came out of, and with what it does and does not say about rate-limit points.
576//!
577//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
578//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
579//! code path — no environment variable, no feature, no build configuration — because an
580//! instrument nobody switches on measures nothing, and
581//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
582//! source's counts the whole session rather than this source's share. The credentialed lane
583//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
584//! passed or failed.
585//!
586//! **A live session refuses to start unless the account can afford it.** Before it does any
587//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
588//! GitHub documents as not counting against the REST rate limit and which answers both of
589//! its budgets at once — and starts only if, for each of them, what remains minus this
590//! session's estimated cost is still at least
591//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
592//! allowance. A session that cannot **declines**: it did not run, so it is
593//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
594//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
595//! waiting for the budget to come back. The estimate is derived offline from
596//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
597//! which is also where the published rule that model rests on is cited; the accounting
598//! above records the gate's own read like any other request, and
599//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
600//!
601//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
602//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
603//! text, which is what lets it run on every platform and on a pull request from a fork with
604//! no credential — and that is what actually stops a regression merging. But an offline
605//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
606//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
607//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
608//! number of nodes this query may return"* and whose `cost` is what that document would
609//! spend, both for a document **without executing it**, and the lane asks it for every query
610//! document this source sends, under the largest bindings this source sends, and fails when
611//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
612//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
613//! one; the offline pins still cover it. It records what those calls reported about the
614//! account's own allowance, because whether asking is free is a thing to observe rather than
615//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
616//! and `cost` is metered against an hourly allowance the accounting above reads off a
617//! credentialed run's own response headers.
618//!
619//! **GitHub has two rate limiters and this source is refused by both, so nothing here
620//! treats them as one thing.** The primary budget is the hourly allowance `gh api
621//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
622//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
623//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
624//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
625//! one part of.
626#![deny(missing_docs)]
627
628use std::collections::BTreeMap;
629use std::sync::{Arc, Mutex};
630use std::time::Duration;
631
632use chrono::{DateTime, Utc};
633use onetaskgraph_plugin_api::{
634    Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
635    ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
636    DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
637    Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
638    MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
639    ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
640    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
641    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
642    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
643    WaitCause, WriteJournal, WriteSupport, system_clock,
644};
645use reqwest::{Client, StatusCode, Url};
646use schemars::{Schema, schema_for};
647use secrecy::{ExposeSecret, SecretString};
648use serde::{Deserialize, Serialize};
649use serde_json::{Value, json};
650
651pub mod accounting;
652mod assets;
653pub mod change_token;
654mod expected;
655pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
656mod pacing;
657mod projection;
658mod visibility;
659
660use accounting::Accounting;
661
662/// The registry name for this plugin.
663pub const KIND: &str = "github-projects";
664/// GitHub's maximum connection page size.
665pub const MAX_PAGE_SIZE: u32 = 100;
666/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
667/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
668/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
669pub const SEARCH_PAGE_SIZE: u32 = 20;
670/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
671/// its comments: the largest batch the node-count model prices at one point.
672///
673/// Each aliased item is resolved once, and what GitHub charges for it is the connections
674/// under it — its labels, its page of board memberships, the field values of each of those
675/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
676/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
677/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
678/// one point and fails if one item more would still be priced at one.
679pub const DETAIL_BATCH: usize = 24;
680/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
681///
682/// Each reference asks GitHub for one connection — a page of its board memberships, each with
683/// its `Status` value read by name rather than through a connection of field values — so fifty
684/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
685/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
686/// issues per source. `tests/point_cost.rs` prices the document at one point.
687pub const REFERENCE_BATCH: usize = 50;
688/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
689/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
690/// point.
691///
692/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
693/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
694/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
695/// if one item more would still be priced at one.
696pub const COPY_DETAIL_BATCH: usize = 21;
697/// How many mutations one aliased request of a copy's creates, closes and updates carries:
698/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
699///
700/// Eleven is what GitHub was seen to accept in one request — the timing spike
701/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
702/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
703/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
704pub const MUTATION_BATCH: usize = 11;
705/// How many aliased writes one request of a copy's board field values and blocked-by edges
706/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
707/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
708///
709/// Nineteen is the most aliases the feasibility spike
710/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
711/// saw GitHub price at the one point every mutation request costs; no larger request was
712/// established, so a copy holding more is split.
713pub const WRITE_BATCH: usize = 19;
714/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
715/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
716/// of five succeeded (the feasibility spike's undo-batching run).
717pub const DELETE_BATCH: usize = 5;
718/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
719/// plan and the project's document, and no more, since each carries a page of the board's field
720/// filter and one of the issue search.
721pub const ORIGIN_BATCH: usize = 10;
722
723/// The most nodes any one document this source sends may be asked to return.
724///
725/// GitHub's own published per-query ceiling, taken from
726/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
727/// workspace cannot hold a stale copy of somebody else's number. A query above it is
728/// **refused before it is executed**, whoever is asking and whatever board they are
729/// asking about — so this is a bound on the documents rather than a budget that runs out.
730///
731/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
732/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
733/// everything the credential does — two numbers against two limits, and this constant
734/// bounds only the first. The second is computed offline too, per document:
735/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
736/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
737/// lane. There is no constant like this one to hold a price under, because points are an
738/// hourly allowance rather than a per-call bound.
739///
740/// Neither is a session's price. What `session-cost.md` records of a whole session is its
741/// **requests** and its **worst-case nodes**; what a whole session spends in points is
742/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
743/// [`accounting`]. The module section on the three ways this source reaches an item says how
744/// the count is arrived at, and which of the page sizes below decide it.
745pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
746
747/// Nested connection size for the connections that hang off one item.
748///
749/// It multiplies through every document that reaches an item under a page — the count
750/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
751/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
752/// every document under these constants and fails naming any that reaches the limit, so
753/// raising this is caught there rather than by GitHub.
754const NESTED_PAGE_SIZE: u32 = 50;
755/// How many of one issue's board memberships are read when an issue is reached directly.
756///
757/// An issue reached through a search or through its own node id carries its board half in
758/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
759/// under a page of issues, so every point of it multiplies through the whole document and
760/// is paid for whether or not any issue is on a second board — which is why it is
761/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
762///
763/// **Three, because what a page misses is now recovered rather than refused**, and the
764/// recovery is what the value is chosen against. An issue whose entry for this board sits
765/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
766/// that page's own cursor — so the value trades a bound every read pays for a request only
767/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
768/// boards would pay that request *per issue*, which is order N against the one page per
769/// hundred issues a read costs today. At three it is only reached by an issue on four or
770/// more boards at once, which keeps the recovery path exceptional rather than routine for
771/// a plausible deployment.
772const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
773/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
774/// its two connections for.
775///
776/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
777/// second is a duplicate a copy already takes the first of. Both connections are walked to
778/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
779/// never what the answer is. It is small because every point of it is paid on every lookup,
780/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
781/// worst-case nodes than the one whole-board read they replaced.
782const ORIGIN_PAGE_SIZE: u32 = 3;
783/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
784/// connections for: one, the one carrier an origin names.
785///
786/// A second carrier still shows: the page then says there is more, and that origin is left to
787/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
788/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
789/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
790/// origins at two points with a first page of three and at one with a first page of one.
791const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
792
793/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
794/// one point buys, since every row carries six connections GitHub charges for once per row.
795/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
796/// fails if one row more would still be priced at one.
797pub const MEMBER_PAGE_SIZE: u32 = 23;
798
799pub use github_graphql_node_count::{NodeCountError, Variables};
800
801/// The largest value this source can bind to each page-size variable its documents name.
802///
803/// Every `first:` in [`graphql`] reads one of these six, and each is capped at the
804/// constant above it wherever a caller's own limit could reach it — `$first` at
805/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
806/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
807/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
808/// not one configuration of it, which is what makes a bound computed under it a bound on
809/// every read.
810pub fn largest_page_sizes() -> Variables {
811    Variables::from([
812        ("first".to_owned(), MAX_PAGE_SIZE),
813        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
814        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
815        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
816        ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
817        ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
818    ])
819}
820
821/// The most nodes `document` could be asked to return, by GitHub's published rules.
822///
823/// Computed offline from the document's own text under [`largest_page_sizes`] — no
824/// network, no credential and no schema — by
825/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
826/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
827/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
828///
829/// # Errors
830///
831/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
832/// no single operation, or binds a page size this source does not name — each of which is
833/// a defect in the document rather than a number.
834pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
835    node_count(document, &largest_page_sizes())
836}
837
838/// The most rate-limit points one call of `document` could spend, by GitHub's published
839/// rules.
840///
841/// Computed offline from the document's own text under [`largest_page_sizes`] — no
842/// network, no credential and no schema — by
843/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
844/// This is `cost`, metered **per hour** against the allowance one credential shares across
845/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
846/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
847/// under, so what `tests/point_cost.rs` does with it is pin every document in
848/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
849/// figures against GitHub's own reported `cost`.
850///
851/// # Errors
852///
853/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
854/// no single operation, or binds a page size this source does not name — each of which is
855/// a defect in the document rather than a number.
856pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
857    github_graphql_node_count::point_cost(document, &largest_page_sizes())
858}
859
860/// The most nodes `document` could be asked to return under `variables`.
861///
862/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
863/// [`accounting`] is this under the bindings one request really sent — one spelling of the
864/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
865/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
866///
867/// # Errors
868///
869/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
870/// no single operation, or binds a page size `variables` does not name.
871pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
872    github_graphql_node_count::node_count(document, variables)
873}
874
875/// The issue-title prefix that makes a board issue a document.
876///
877/// A GitHub Projects board has no document type — it holds issues — so the discriminator
878/// is the title, and this is the whole of it: an issue whose title begins with these bytes
879/// is a document and every other issue is the task or project the sub-issue rule makes it.
880///
881/// It is spelled **once**, here, and read rather than restated everywhere else — including
882/// by the shared journeys, which take it from this constant so a board fixture cannot
883/// drift from what this source reads. `docs/metadata.md` records the two consequences that
884/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
885/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
886/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
887pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
888
889/// Exact GraphQL query documents issued by this plugin.
890///
891/// Keeping the production documents here lets the pinned-schema test validate the same
892/// bytes that are sent to GitHub, rather than a test-only copy which could drift
893/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
894/// field, and its guarded caller always supplies the complete existing option set with ids.
895pub mod graphql {
896    /// The board half of one item: the field values every document here reads it from.
897    ///
898    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
899    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
900    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
901    /// *the same value*, because
902    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
903    /// one path. Three spellings of it is what would drift, so there is one.
904    ///
905    /// The `Status` option and this source's own origin text field are the whole of it. It
906    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
907    /// content, so it holds nothing the content's own `labels` do not already say, and it
908    /// would sit a label connection two page sizes deep.
909    macro_rules! board_item_values {
910        () => {
911            r#"fieldValues(first:$nestedFirst){nodes{
912          ... on ProjectV2ItemFieldSingleSelectValue{name field{
913            ... on ProjectV2SingleSelectField{id name options{id name}}
914          }}
915          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
916        }pageInfo{hasNextPage}}"#
917        };
918    }
919
920    /// The variable every document reading an issue declares for the relationship events its
921    /// change token counts, with its value as the default, so no caller binds it.
922    ///
923    /// A variable rather than a literal list because every argument a production document
924    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
925    /// its default to the pinned enum. Its default is the six event types GitHub records a
926    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
927    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
928    /// project membership on the task, as its parent, which GitHub records there whichever
929    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
930    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
931    /// decide. [`super::change_token`] says why the count is in the token.
932    macro_rules! relationship_events_variable {
933        () => {
934            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
935        };
936    }
937
938    /// Everything this source reads about one issue, wherever it reaches that issue.
939    ///
940    /// A macro rather than a constant so the three documents below can `concat!` it: one
941    /// spelling of these fields is what makes an issue read through the board-scoped
942    /// search, through its own node id, and through its project's sub-issue relationship
943    /// resolve to *the same* item, which is the whole of what
944    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
945    ///
946    /// `projectItems` is what carries the board half of an issue: the board item's own id
947    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
948    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
949    /// issue rather than on the board, which is what makes the cost of a read proportional
950    /// to what was asked for instead of to the board's size.
951    ///
952    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
953    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
954    /// not on that page: a page here is where the search for the entry starts rather than
955    /// where it ends.
956    ///
957    /// It does **not** select the board's `Labels` field value, and that is the whole of
958    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
959    /// a label connection there sits under `fieldValues` under `projectItems` under a page
960    /// of issues, spending `$nestedFirst` twice down one path, and took
961    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
962    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
963    /// above, and that connection is where every label this source reports comes from. No
964    /// document in this module selects the board field any longer, [`BOARD`] included; the
965    /// module documentation records why nothing it could have held is lost.
966    macro_rules! board_issue {
967        () => {
968            concat!(
969                r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
970      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
971      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
972        "#,
973                board_item_values!(),
974                r#"}pageInfo{hasNextPage endCursor}}}"#
975            )
976        };
977    }
978
979    /// One page of a repository's issues, newest first, each with when it was created and its
980    /// body: what a retried copy reads to find the issue its earlier attempt created, by the
981    /// create key in that body's metadata slot — never through search, whose index lags.
982    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}}}}";
983    /// Every issue of one board, found by a search scoped to that board.
984    ///
985    /// This is how the projects a board holds are listed, and it selects no `items`
986    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
987    /// container walked page by page, so nothing nested inside a board item is paid for.
988    /// Which of the issues it returns is a project is then read off `parent` — GitHub
989    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
990    /// discriminator has to be applied to the field, which is a scalar on the issue and
991    /// costs nothing.
992    pub const SEARCH_ISSUES: &str = concat!(
993        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
994        relationship_events_variable!(),
995        r#"){
996      search(query:$search,type:$type,first:$first,after:$after){
997        pageInfo{hasNextPage endCursor}
998        nodes{__typename ...BoardIssue}
999      }
1000    }"#,
1001        board_issue!()
1002    );
1003
1004    /// What a dependency read selects of each far end: enough to say which kind of item it
1005    /// is, its body included for the kind marker.
1006    macro_rules! related_issue {
1007        () => {
1008            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1009        };
1010    }
1011
1012    /// One issue by its own node id, which is what a qualified id names here — with what a
1013    /// write of it needs and the issue does not carry in `board_issue!`: the field
1014    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1015    ///
1016    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1017    /// answers a write made moments ago with the value from before it, and resolving a node
1018    /// id does not.
1019    ///
1020    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1021    /// reads it by its own id, and with them that one read answers everything the write
1022    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1023    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1024    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1025    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1026    /// they sit under one item, and this read is still one point.
1027    pub const ISSUE: &str = concat!(
1028        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1029        relationship_events_variable!(),
1030        r#"){
1031      node(id:$id){__typename ...BoardIssue ... on Issue{
1032        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1033          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1034          ... on ProjectV2Field{__typename id name dataType}
1035        }pageInfo{hasNextPage}}}}}
1036        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1037      }}
1038    }"#,
1039        board_issue!(),
1040        related_issue!()
1041    );
1042
1043    /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1044    /// under and that project's members beside it: the project issue whole, and a page of its
1045    /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1046    ///
1047    /// What a guarded write of a document's metadata reads, and what reading a document by its
1048    /// id is: a write to a board the boundary screens is held to who may read the document,
1049    /// which is the project's and every member's classification as much as its own, and here
1050    /// the one request that holds the version the write is compared against answers all of it.
1051    /// Its members carry no edges, which nothing classifying them reads.
1052    pub const DOCUMENT_IN_PROJECT: &str = concat!(
1053        r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1054        relationship_events_variable!(),
1055        r#"){
1056      node(id:$id){__typename ...BoardIssue ... on Issue{
1057        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1058          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1059          ... on ProjectV2Field{__typename id name dataType}
1060        }pageInfo{hasNextPage}}}}}
1061        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1062        project:parent{__typename ...BoardIssue
1063          subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1064        }
1065      }}
1066    }"#,
1067        board_issue!(),
1068        related_issue!()
1069    );
1070
1071    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1072    /// what blocks it.
1073    ///
1074    /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1075    /// one point buys — rather than at `$first`, because every row carries six connections and
1076    /// a hundred-row page cost six points to list a project of ten.
1077    ///
1078    /// The work this costs is the project's own size. Nothing about it grows as the board
1079    /// gains projects, or as those projects gain tasks.
1080    ///
1081    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1082    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1083    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1084    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1085    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1086    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1087    /// It adds a connection under each issue of the page — one rate-limit point per page,
1088    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1089    /// answer questions that never read an edge.
1090    ///
1091    /// **The project issue itself rides here too**, with the page of what blocks it, because
1092    /// the reader of a project's tasks reads the project next: a copy writing the project reads
1093    /// it and its edges before it writes them. Carried on the request that already names the
1094    /// project, the first page answers that read and the dependency read after it, at the
1095    /// state GitHub answered in this command, rather than spending one more read on an item
1096    /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1097    /// copy's read of a task this listing carried is answered by it rather than sent again.
1098    /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1099    pub const SUB_ISSUES: &str = concat!(
1100        r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1101        relationship_events_variable!(),
1102        r#"){
1103      node(id:$id){__typename ...BoardIssue
1104        ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1105          subIssues(first:$memberFirst,after:$after){
1106          pageInfo{hasNextPage endCursor}
1107          nodes{__typename ...BoardIssue ... on Issue{
1108            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1109          }}
1110        }}}
1111    }"#,
1112        board_issue!(),
1113        related_issue!()
1114    );
1115
1116    /// What a read of the board's own `items` selects of each item's content.
1117    ///
1118    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1119    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1120    /// content by one spelling.
1121    macro_rules! board_item_content {
1122        () => {
1123            r#" content{
1124        ... 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}}}
1125        ... on PullRequest{__typename id}
1126        ... on DraftIssue{__typename id title body createdAt updatedAt}
1127      }"#
1128        };
1129    }
1130
1131    /// Reads the board's fields and one page of its items.
1132    pub const BOARD: &str = concat!(
1133        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1134        relationship_events_variable!(),
1135        r#"){
1136      owner:repositoryOwner(login:$owner){
1137        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1138      }
1139    } fragment Board on ProjectV2 { id title
1140      fields(first:$nestedFirst){nodes{
1141        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1142        ... on ProjectV2Field{__typename id name dataType}
1143      }pageInfo{hasNextPage}}
1144      items(first:$first,after:$after){nodes{id updatedAt "#,
1145        board_item_values!(),
1146        board_item_content!(),
1147        r#"} pageInfo{hasNextPage endCursor}}
1148    }"#
1149    );
1150
1151    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1152    /// the board.
1153    ///
1154    /// **`originItems`** is the board's own items narrowed by its own field filter —
1155    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1156    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1157    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1158    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1159    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1160    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1161    /// fresh `addProjectV2ItemById` the way that connection does.
1162    ///
1163    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1164    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1165    /// indexes that comment, and the index catches up with a write in a second or two rather
1166    /// than in minutes, so it finds a carrier another process wrote that the first read is
1167    /// still behind on.
1168    ///
1169    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1170    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1171    /// from its last cursor, which answers an empty page. Every candidate either read returns
1172    /// is confirmed against its own origin field before it is reported, so a token match of
1173    /// the search or anything else the filter admits never is.
1174    ///
1175    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1176    /// own whole reads counts this one among them.
1177    pub const ORIGIN_LOOKUP: &str = concat!(
1178        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1179        relationship_events_variable!(),
1180        r#"){
1181      originItems:repositoryOwner(login:$owner){
1182        ... on ProjectV2Owner{projectV2(number:$number){
1183          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1184        board_item_values!(),
1185        board_item_content!(),
1186        r#"} pageInfo{hasNextPage endCursor}}
1187        }}
1188      }
1189      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1190        pageInfo{hasNextPage endCursor}
1191        nodes{__typename ...BoardIssue}
1192      }
1193    }"#,
1194        board_issue!()
1195    );
1196
1197    /// Every closed item of the board, by the board's own field filter — what
1198    /// `task status reconcile` reads, and nothing else of the board.
1199    ///
1200    /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1201    /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1202    /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1203    /// closed, with a reason and a card that contradict each other, from the item node it
1204    /// already carries, so a filter that also let open items through would cost reads and
1205    /// write nothing wrong.
1206    ///
1207    /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1208    /// option writes that follow need no read of the board. The root is aliased `closedItems`
1209    /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1210    /// among them.
1211    pub const CLOSED_ITEMS: &str = concat!(
1212        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1213        relationship_events_variable!(),
1214        r#"){
1215      closedItems:repositoryOwner(login:$owner){
1216        ... on ProjectV2Owner{projectV2(number:$number){id
1217          fields(first:$nestedFirst){nodes{
1218            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1219            ... on ProjectV2Field{__typename id name dataType}
1220          }pageInfo{hasNextPage}}
1221          items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1222        board_item_values!(),
1223        board_item_content!(),
1224        r#"} pageInfo{hasNextPage endCursor}}
1225        }}
1226      }
1227    }"#
1228    );
1229
1230    /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1231    macro_rules! reference_alias {
1232        ($n:literal) => {
1233            concat!(
1234                "\n      r",
1235                stringify!($n),
1236                ":repository(owner:$owner",
1237                stringify!($n),
1238                ",name:$name",
1239                stringify!($n),
1240                "){issue(number:$number",
1241                stringify!($n),
1242                "){...HeldIssue}}"
1243            )
1244        };
1245    }
1246
1247    /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1248    /// number — the one read `task status complete` makes of a source, and nothing else of
1249    /// the board: no `items`, no search and no project read.
1250    ///
1251    /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1252    /// references one point.** It selects no labels, and of each board membership only the
1253    /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1254    /// connection, its page of memberships, where the whole fragment asks for five. What a
1255    /// completion writes is a card and, at most, the issue's state, so the labels and the other
1256    /// field values it leaves out are never what it needs; an item read this way is held for
1257    /// that write alone and never answers another read. An issue whose entry for this board is
1258    /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1259    /// every other read of an issue recovers one.
1260    ///
1261    /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1262    /// fewer references binds the slots it has none for to its last one.
1263    pub const ISSUES_BY_REFERENCE: &str = concat!(
1264        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!"#,
1265        relationship_events_variable!(),
1266        r#"){"#,
1267        reference_alias!(0),
1268        reference_alias!(1),
1269        reference_alias!(2),
1270        reference_alias!(3),
1271        reference_alias!(4),
1272        reference_alias!(5),
1273        reference_alias!(6),
1274        reference_alias!(7),
1275        reference_alias!(8),
1276        reference_alias!(9),
1277        reference_alias!(10),
1278        reference_alias!(11),
1279        reference_alias!(12),
1280        reference_alias!(13),
1281        reference_alias!(14),
1282        reference_alias!(15),
1283        reference_alias!(16),
1284        reference_alias!(17),
1285        reference_alias!(18),
1286        reference_alias!(19),
1287        reference_alias!(20),
1288        reference_alias!(21),
1289        reference_alias!(22),
1290        reference_alias!(23),
1291        reference_alias!(24),
1292        reference_alias!(25),
1293        reference_alias!(26),
1294        reference_alias!(27),
1295        reference_alias!(28),
1296        reference_alias!(29),
1297        reference_alias!(30),
1298        reference_alias!(31),
1299        reference_alias!(32),
1300        reference_alias!(33),
1301        reference_alias!(34),
1302        reference_alias!(35),
1303        reference_alias!(36),
1304        reference_alias!(37),
1305        reference_alias!(38),
1306        reference_alias!(39),
1307        reference_alias!(40),
1308        reference_alias!(41),
1309        reference_alias!(42),
1310        reference_alias!(43),
1311        reference_alias!(44),
1312        reference_alias!(45),
1313        reference_alias!(46),
1314        reference_alias!(47),
1315        reference_alias!(48),
1316        reference_alias!(49),
1317        "\n    }",
1318        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}
1319      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1320        status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1321          ... on ProjectV2SingleSelectField{id name options{id name}}
1322        }}}
1323      }pageInfo{hasNextPage endCursor}}}"#
1324    );
1325
1326    /// The board's own id and field definitions, and not one of its items.
1327    ///
1328    /// What a write needs of the board when the item it writes does not say: the id a field
1329    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1330    /// origin fields. It selects no `items`, so what it costs is the board's field list
1331    /// however many items the board holds — and it decides nothing about which items those
1332    /// are, which is the question a read of one item by its own id answers instead.
1333    ///
1334    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1335    /// board's item reads by their root counts this one among them.
1336    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1337      boardFields:repositoryOwner(login:$owner){
1338        ... on ProjectV2Owner{projectV2(number:$number){id
1339          fields(first:$nestedFirst){nodes{
1340            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1341            ... on ProjectV2Field{__typename id name dataType}
1342          }pageInfo{hasNextPage}}
1343        }}
1344      }
1345    }"#;
1346
1347    /// One board draft by its own node id, with the board item it sits in.
1348    ///
1349    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1350    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1351    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1352    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1353    /// board listing hands it to, and nothing has to list the board to find one.
1354    pub const DRAFT: &str = concat!(
1355        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1356      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1357        projectV2Items(first:$boardItems){nodes{id project{id number}
1358        "#,
1359        board_item_values!(),
1360        r#"}pageInfo{hasNextPage endCursor}}}}
1361    }"#
1362    );
1363
1364    /// One issue's board memberships alone, walked past the page a read of it carried.
1365    ///
1366    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1367    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1368    /// boards than that page holds may have this board's entry past its end. This asks that
1369    /// one issue for its memberships and nothing else — the caller already holds the issue —
1370    /// so an answer of "this board does not hold it" is only ever given about a connection
1371    /// read to exhaustion.
1372    ///
1373    /// It selects the board item's id, its project number and the same
1374    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1375    /// very same resolver: an issue recovered this way reports the same title, the same
1376    /// status, the same labels and the same qualified id as one whose entry was on the
1377    /// page.
1378    ///
1379    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1380    /// multiplies through it and the membership connection can be walked at
1381    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1382    /// further request for any issue a person really keeps.
1383    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1384        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1385      node(id:$id){
1386        ... on Issue{projectItems(first:$first,after:$after){
1387          nodes{id updatedAt project{id number}
1388        "#,
1389        board_item_values!(),
1390        r#"}
1391          pageInfo{hasNextPage endCursor}}}
1392      }
1393    }"#
1394    );
1395    /// Whether the board's Project is public — half of what decides whether a write here can
1396    /// be read by anybody. Needs the `read:project` scope.
1397    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1398    /// Resolves the configured repository's node id, which creating an issue requires.
1399    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1400    /// What creating an issue needs and has not read yet: the board's own id and field
1401    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1402    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1403    ///
1404    /// Sent at the point a create knows which repository it is for, when neither half is
1405    /// already known to this process; a create needing only one of them sends that one's own
1406    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1407    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1408    /// status.
1409    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1410      boardFields:repositoryOwner(login:$owner){
1411        ... on ProjectV2Owner{projectV2(number:$number){id
1412          fields(first:$nestedFirst){nodes{
1413            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1414            ... on ProjectV2Field{__typename id name dataType}
1415          }pageInfo{hasNextPage}}
1416        }}
1417      }
1418      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1419    }"#;
1420    /// Reads both dependency directions for one issue, with each far end's own kind — and
1421    /// the issue's own body, which is where an edge to another source is recorded, so that
1422    /// half of a dependency read needs no second read of the issue or of the board.
1423    pub const ISSUE_DEPENDENCIES: &str = concat!(
1424        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1425      ... on Issue{body
1426        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1427        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1428      }}}"#,
1429        related_issue!()
1430    );
1431    /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1432    /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1433    /// along because its metadata slot is where an edge to another source is recorded — and a
1434    /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1435    /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1436    /// and only when that item is this board's: no draft read and no board read follow it.
1437    ///
1438    /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1439    /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1440    /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1441    /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1442    /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1443    /// one point; the credentialed lane's reconciliation is what holds the two to that.
1444    pub const ISSUE_DEPENDS_ON: &str = concat!(
1445        r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1446      node(id:$id){__typename
1447      ... on Issue{body
1448        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1449      }
1450      ... on DraftIssue{id body}}
1451      draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1452      board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1453        related_issue!()
1454    );
1455    /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1456    /// the reverse of a recorded edge is derived from the far end, never written down here.
1457    pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1458        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1459      ... on Issue{
1460        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1461      }}}"#,
1462        related_issue!()
1463    );
1464    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1465    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1466    /// `parentIssueId`, so this one request files it under both — see
1467    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1468    pub const CREATE_ISSUE: &str =
1469        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1470    /// Puts an existing issue on the configured board.
1471    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1472    /// Updates an issue's visible fields and its open or closed state in one call.
1473    pub const UPDATE_ISSUE: &str =
1474        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1475    /// Updates an existing draft's user-visible fields.
1476    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1477    /// Updates a text or single-select value on one project item.
1478    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}}}}}}}}"#;
1479    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1480    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1481    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1482    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}}}"#;
1483    /// Clears one project item's value of one field, which is what a `none` priority is.
1484    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}}}}}}}}"#;
1485    /// Creates one field: a single-select one with its options, or a text one for a projected
1486    /// metadata value. Only the guarded field setup may use this document, and only for a
1487    /// field the board lacks.
1488    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1489    /// Replaces a single-select field's options. Only the guarded field setup — the
1490    /// `status-options` and `fields` operations — may use this document, because GitHub
1491    /// treats the input as the complete option list.
1492    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1493    /// A fresh snapshot of the Status field and every board item's assignment.
1494    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}}}}}}"#;
1495    /// Files one issue under another as a sub-issue, which is what project membership is.
1496    pub const ADD_SUB_ISSUE: &str =
1497        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1498    /// Takes one issue back out of its parent.
1499    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1500    /// Adds GitHub's native issue blocked-by relationship.
1501    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1502    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1503    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1504    macro_rules! origin_lookup_alias {
1505        ($n:literal) => {
1506            concat!(
1507                "\n      o",
1508                stringify!($n),
1509                ":repositoryOwner(login:$owner) @include(if:$ask",
1510                stringify!($n),
1511                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1512                stringify!($n),
1513                "){nodes{id updatedAt ",
1514                board_item_values!(),
1515                board_item_content!(),
1516                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1517                stringify!($n),
1518                ":search(query:$search",
1519                stringify!($n),
1520                ",type:$type,first:$lookupFirst) @include(if:$ask",
1521                stringify!($n),
1522                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1523            )
1524        };
1525    }
1526
1527    /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1528    /// selected beside them: one spelling of the batched lookup for the documents that send it.
1529    macro_rules! origin_lookups {
1530        ($extra_variables:literal, $extra_fields:literal) => {
1531            concat!(
1532                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!"#,
1533                relationship_events_variable!(),
1534                $extra_variables,
1535                r#"){
1536      boardFields:repositoryOwner(login:$owner){
1537        ... on ProjectV2Owner{projectV2(number:$number){id
1538          fields(first:$nestedFirst){nodes{
1539            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1540            ... on ProjectV2Field{__typename id name dataType}
1541          }pageInfo{hasNextPage}}
1542        }}
1543      }
1544      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1545                $extra_fields,
1546                origin_lookup_alias!(0),
1547                origin_lookup_alias!(1),
1548                origin_lookup_alias!(2),
1549                origin_lookup_alias!(3),
1550                origin_lookup_alias!(4),
1551                origin_lookup_alias!(5),
1552                origin_lookup_alias!(6),
1553                origin_lookup_alias!(7),
1554                origin_lookup_alias!(8),
1555                origin_lookup_alias!(9),
1556                "\n    }",
1557                board_issue!()
1558            )
1559        };
1560    }
1561
1562    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1563    /// origins a copy is about to look for, and everything creating an item then reads —
1564    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1565    /// id and the numeric id of the repository an issue is created in — in one request.
1566    ///
1567    /// What a copy reads before it writes anything, so that finding none of its records on the
1568    /// board and creating every one costs this read and the writes. An origin whose page says
1569    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1570    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1571    pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1572
1573    /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1574    /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1575    /// and no connection under it.
1576    ///
1577    /// What a document copy sends where the public boundary screens its write: the document is
1578    /// held to who may read its project, which is its members' classification as much as the
1579    /// project's own, and this request carries both the project — among the origins it asks —
1580    /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1581    /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1582    pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1583        ",$project:ID!,$memberFirst:Int!",
1584        "\n      members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1585    );
1586
1587    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1588    /// whether it is sent.
1589    macro_rules! write_slot {
1590        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1591            concat!(
1592                " ",
1593                $prefix,
1594                stringify!($n),
1595                ":",
1596                $field,
1597                "(input:$",
1598                $prefix,
1599                stringify!($n),
1600                ") @include(if:$send",
1601                stringify!($n),
1602                "){",
1603                $selection,
1604                "}"
1605            )
1606        };
1607    }
1608
1609    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1610    /// and eleven `$send<n>`.
1611    macro_rules! write_variables {
1612        ($prefix:literal, $type:literal) => {
1613            concat!(
1614                "mutation(",
1615                "$",
1616                $prefix,
1617                "0:",
1618                $type,
1619                "!,$",
1620                $prefix,
1621                "1:",
1622                $type,
1623                "!,$",
1624                $prefix,
1625                "2:",
1626                $type,
1627                "!,$",
1628                $prefix,
1629                "3:",
1630                $type,
1631                "!,$",
1632                $prefix,
1633                "4:",
1634                $type,
1635                "!,$",
1636                $prefix,
1637                "5:",
1638                $type,
1639                "!,$",
1640                $prefix,
1641                "6:",
1642                $type,
1643                "!,$",
1644                $prefix,
1645                "7:",
1646                $type,
1647                "!,$",
1648                $prefix,
1649                "8:",
1650                $type,
1651                "!,$",
1652                $prefix,
1653                "9:",
1654                $type,
1655                "!,$",
1656                $prefix,
1657                "10:",
1658                $type,
1659                "!,",
1660                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1661                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1662                "$send10:Boolean!){"
1663            )
1664        };
1665    }
1666
1667    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1668    /// `$field`, each sent only while its `$send<n>` holds.
1669    macro_rules! write_batch {
1670        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1671            concat!(
1672                write_variables!($prefix, $type),
1673                write_slot!($prefix, $field, $selection, 0),
1674                write_slot!($prefix, $field, $selection, 1),
1675                write_slot!($prefix, $field, $selection, 2),
1676                write_slot!($prefix, $field, $selection, 3),
1677                write_slot!($prefix, $field, $selection, 4),
1678                write_slot!($prefix, $field, $selection, 5),
1679                write_slot!($prefix, $field, $selection, 6),
1680                write_slot!($prefix, $field, $selection, 7),
1681                write_slot!($prefix, $field, $selection, 8),
1682                write_slot!($prefix, $field, $selection, 9),
1683                write_slot!($prefix, $field, $selection, 10),
1684                "}"
1685            )
1686        };
1687    }
1688
1689    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1690    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1691    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1692    pub const CREATE_ISSUES: &str = write_batch!(
1693        "create",
1694        "CreateIssueInput",
1695        "createIssue",
1696        "issue{id number url}"
1697    );
1698
1699    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1700    /// the `Status` value of each, read only while `$read<n>` holds.
1701    macro_rules! created_item_alias {
1702        ($n:literal) => {
1703            concat!(
1704                "\n      i",
1705                stringify!($n),
1706                ":node(id:$id",
1707                stringify!($n),
1708                ") @include(if:$read",
1709                stringify!($n),
1710                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1711            )
1712        };
1713    }
1714
1715    /// The board item of each issue a copy created, up to
1716    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1717    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1718    /// answering the create, so its id comes from a read made once every create has been sent.
1719    pub const CREATED_BOARD_ITEMS: &str = concat!(
1720        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!){"#,
1721        created_item_alias!(0),
1722        created_item_alias!(1),
1723        created_item_alias!(2),
1724        created_item_alias!(3),
1725        created_item_alias!(4),
1726        created_item_alias!(5),
1727        created_item_alias!(6),
1728        created_item_alias!(7),
1729        created_item_alias!(8),
1730        created_item_alias!(9),
1731        created_item_alias!(10),
1732        created_item_alias!(11),
1733        created_item_alias!(12),
1734        created_item_alias!(13),
1735        created_item_alias!(14),
1736        created_item_alias!(15),
1737        created_item_alias!(16),
1738        created_item_alias!(17),
1739        created_item_alias!(18),
1740        created_item_alias!(19),
1741        created_item_alias!(20),
1742        "\n    }"
1743    );
1744
1745    /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1746    /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1747    macro_rules! wide_variables {
1748        ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1749            concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1750        };
1751    }
1752    macro_rules! wide_slots {
1753        ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1754            concat!($(
1755                " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1756                ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1757            )+)
1758        };
1759    }
1760
1761    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1762    /// created, each on its own item, in one ordered request.
1763    pub const WRITE_FIELD_VALUES: &str = concat!(
1764        "mutation(",
1765        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1766        "){",
1767        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),
1768        "}"
1769    );
1770
1771    /// A copy's board field values and its blocked-by edges together, up to
1772    /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1773    /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1774    /// What a copy sends when one request of its writes holds both kinds; one holding a single
1775    /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1776    pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1777        "mutation(",
1778        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1779        wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1780        "){",
1781        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),
1782        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),
1783        "}"
1784    );
1785
1786    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1787    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1788    pub const CLOSE_ISSUES: &str =
1789        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1790
1791    /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1792    /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1793    pub const UPDATE_ISSUES: &str =
1794        write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1795
1796    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1797    /// issues it created, in one request.
1798    pub const ADD_BLOCKED_BYS: &str = concat!(
1799        "mutation(",
1800        wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1801        "){",
1802        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),
1803        "}"
1804    );
1805
1806    /// Removes one native issue blocked-by relationship.
1807    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1808    /// Deletes one issue, which takes its board item with it.
1809    ///
1810    /// The engine sends this in one situation only: undoing a copy that could not finish,
1811    /// over the items that same copy created. Deleting the issue removes the board item
1812    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1813    pub const DELETE_ISSUE: &str =
1814        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1815    /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1816    /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1817    /// part way sends to take them back.
1818    pub const DELETE_ISSUES: &str = concat!(
1819        "mutation(",
1820        wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1821        "){",
1822        wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1823        "}"
1824    );
1825
1826    /// Everything this source reads about one issue comment, wherever it reaches one.
1827    ///
1828    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1829    /// and a comment just edited are handed to one mapper, so they are selected by one
1830    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1831    /// longer exists, and `login` is the one member every kind of actor carries.
1832    macro_rules! issue_comment {
1833        () => {
1834            "id author{login} createdAt updatedAt body url"
1835        };
1836    }
1837
1838    /// One task's comments: a page of its issue's own `comments` connection.
1839    ///
1840    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1841    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1842    /// list every time somebody edited it; left unordered the connection answers in the order
1843    /// the comments were written, which is the order GitHub documents for the same collection
1844    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1845    /// node count and the caller's own page size is pushed straight down.
1846    pub const ISSUE_COMMENTS: &str = concat!(
1847        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1848        issue_comment!(),
1849        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1850    );
1851    /// One issue by its own node id, with a page of its comments: what `task show` and a
1852    /// comment listing read, in one request.
1853    ///
1854    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1855    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1856    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1857    /// connection there would multiply through both of those documents' price, and neither
1858    /// needs one.
1859    pub const ISSUE_DETAIL: &str = concat!(
1860        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1861        relationship_events_variable!(),
1862        r#"){
1863      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1864        issue_comment!(),
1865        r#"}pageInfo{hasNextPage endCursor}}}}
1866    }"#,
1867        board_issue!()
1868    );
1869
1870    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1871    /// page of its comments when `$comments` asks for them.
1872    macro_rules! issue_details_alias {
1873        ($n:literal) => {
1874            concat!(
1875                "\n      i",
1876                stringify!($n),
1877                ":node(id:$id",
1878                stringify!($n),
1879                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1880                issue_comment!(),
1881                "}pageInfo{hasNextPage endCursor}}}}"
1882            )
1883        };
1884    }
1885
1886    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1887    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1888    ///
1889    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1890    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1891    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1892    /// neither — so every connection under it would be priced at nothing and the pin in
1893    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1894    /// one-item read the model already prices, so the batch costs what its aliases cost.
1895    ///
1896    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1897    /// slots it has no item for to the last item it does, and reads that item again; the
1898    /// price is the document's, whatever its variables, so a short batch costs what a full
1899    /// one does and nothing more.
1900    pub const ISSUE_DETAILS: &str = concat!(
1901        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!"#,
1902        relationship_events_variable!(),
1903        r#"){"#,
1904        issue_details_alias!(0),
1905        issue_details_alias!(1),
1906        issue_details_alias!(2),
1907        issue_details_alias!(3),
1908        issue_details_alias!(4),
1909        issue_details_alias!(5),
1910        issue_details_alias!(6),
1911        issue_details_alias!(7),
1912        issue_details_alias!(8),
1913        issue_details_alias!(9),
1914        issue_details_alias!(10),
1915        issue_details_alias!(11),
1916        issue_details_alias!(12),
1917        issue_details_alias!(13),
1918        issue_details_alias!(14),
1919        issue_details_alias!(15),
1920        issue_details_alias!(16),
1921        issue_details_alias!(17),
1922        issue_details_alias!(18),
1923        issue_details_alias!(19),
1924        issue_details_alias!(20),
1925        issue_details_alias!(21),
1926        issue_details_alias!(22),
1927        issue_details_alias!(23),
1928        "\n    }",
1929        board_issue!()
1930    );
1931
1932    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1933    /// page of its comments and the issues it is blocked by.
1934    macro_rules! issue_copy_details_alias {
1935        ($n:literal) => {
1936            concat!(
1937                "\n      i",
1938                stringify!($n),
1939                ":node(id:$id",
1940                stringify!($n),
1941                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1942                issue_comment!(),
1943                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1944            )
1945        };
1946    }
1947
1948    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1949    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1950    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1951    /// at a time, so a copy of a changed task costs this read and nothing else.
1952    ///
1953    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1954    /// what `task show-many` is priced by and which reads no edge.
1955    ///
1956    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1957    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1958    /// project copy, which reads its project and every task at once, knows from this same read
1959    /// which tasks the project holds at the destination, and its orphan check is answered by it
1960    /// rather than by a listing of the project.
1961    pub const ISSUE_COPY_DETAILS: &str = concat!(
1962        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!"#,
1963        relationship_events_variable!(),
1964        r#"){"#,
1965        issue_copy_details_alias!(0),
1966        issue_copy_details_alias!(1),
1967        issue_copy_details_alias!(2),
1968        issue_copy_details_alias!(3),
1969        issue_copy_details_alias!(4),
1970        issue_copy_details_alias!(5),
1971        issue_copy_details_alias!(6),
1972        issue_copy_details_alias!(7),
1973        issue_copy_details_alias!(8),
1974        issue_copy_details_alias!(9),
1975        issue_copy_details_alias!(10),
1976        issue_copy_details_alias!(11),
1977        issue_copy_details_alias!(12),
1978        issue_copy_details_alias!(13),
1979        issue_copy_details_alias!(14),
1980        issue_copy_details_alias!(15),
1981        issue_copy_details_alias!(16),
1982        issue_copy_details_alias!(17),
1983        issue_copy_details_alias!(18),
1984        issue_copy_details_alias!(19),
1985        issue_copy_details_alias!(20),
1986        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1987        "\n    }",
1988        board_issue!(),
1989        related_issue!()
1990    );
1991
1992    /// Which issue one comment is on, read before that comment is edited or removed.
1993    ///
1994    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1995    /// comment id given against the wrong task would change a comment on another issue.
1996    pub const COMMENT_ISSUE: &str =
1997        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1998    /// Adds one comment to an issue, signed as the account the token belongs to.
1999    pub const ADD_COMMENT: &str = concat!(
2000        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
2001        issue_comment!(),
2002        r#"}}}}"#
2003    );
2004    /// Replaces the body of one issue comment.
2005    pub const UPDATE_COMMENT: &str = concat!(
2006        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2007        issue_comment!(),
2008        r#"}}}"#
2009    );
2010    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2011    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2012
2013    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2014    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2015    /// change token's every part and the first page of its comments.
2016    ///
2017    /// Sent **only** while that write runs, in place of the document named beside each
2018    /// below, so every other write's documents, prices and request counts are what they
2019    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2020    /// mutation's response is stamped seconds after the change it made, so against GitHub
2021    /// itself a response rarely determines a version; the cache is then left unserveable
2022    /// for the task, and its next read refreshes it.
2023    macro_rules! whole_issue {
2024        () => {
2025            concat!(
2026                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2027                issue_comment!(),
2028                r#"}pageInfo{hasNextPage endCursor}}}"#
2029            )
2030        };
2031    }
2032
2033    /// The variables every document of `whole_issue!` declares beside its own.
2034    macro_rules! whole_variables {
2035        () => {
2036            concat!(
2037                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2038                relationship_events_variable!()
2039            )
2040        };
2041    }
2042
2043    /// [`ADD_COMMENT`], selecting the whole issue commented on.
2044    pub const ADD_COMMENT_WHOLE: &str = concat!(
2045        r#"mutation($input:AddCommentInput!"#,
2046        whole_variables!(),
2047        r#"){addComment(input:$input){subject{id "#,
2048        whole_issue!(),
2049        r#"} commentEdge{node{"#,
2050        issue_comment!(),
2051        r#"}}}}"#,
2052        board_issue!()
2053    );
2054    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2055    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2056        r#"mutation($input:UpdateIssueCommentInput!"#,
2057        whole_variables!(),
2058        r#"){updateIssueComment(input:$input){issueComment{"#,
2059        issue_comment!(),
2060        r#" issue{"#,
2061        whole_issue!(),
2062        r#"}}}}"#,
2063        board_issue!()
2064    );
2065    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2066    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2067        r#"mutation($input:UpdateIssueInput!"#,
2068        whole_variables!(),
2069        r#"){updateIssue(input:$input){issue{id "#,
2070        whole_issue!(),
2071        r#"}}}"#,
2072        board_issue!()
2073    );
2074    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2075    pub const UPDATE_FIELD_WHOLE: &str = concat!(
2076        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2077        whole_variables!(),
2078        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2079        whole_issue!(),
2080        r#"}}}}"#,
2081        board_issue!()
2082    );
2083    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2084    pub const CLEAR_FIELD_WHOLE: &str = concat!(
2085        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2086        whole_variables!(),
2087        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2088        whole_issue!(),
2089        r#"}}}}"#,
2090        board_issue!()
2091    );
2092
2093    /// The document sent in `document`'s place while a write the store's cache expects runs,
2094    /// when it has one.
2095    #[must_use]
2096    pub fn whole_twin(document: &str) -> Option<&'static str> {
2097        [
2098            (ADD_COMMENT, ADD_COMMENT_WHOLE),
2099            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2100            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2101            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2102            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2103        ]
2104        .into_iter()
2105        .find(|(plain, _)| *plain == document)
2106        .map(|(_, whole)| whole)
2107    }
2108
2109    /// Every document above, with what this source is doing when it sends one.
2110    ///
2111    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2112    /// name the call that was refused, and a `match` with a catch-all arm would answer a
2113    /// document added later with "talking to GitHub" and never say so.
2114    ///
2115    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2116    /// const` here that this list omits, so the two cannot part — which is the same guard
2117    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2118    pub const DOCUMENTS: [(&str, &str); 56] = [
2119        (
2120            ISSUES_BY_CREATION,
2121            "listing a repository's issues newest first for a create key",
2122        ),
2123        (SEARCH_ISSUES, "searching this board's issues"),
2124        (ISSUE, "reading one issue"),
2125        (
2126            DOCUMENT_IN_PROJECT,
2127            "reading one document with its project and the project's members",
2128        ),
2129        (
2130            ISSUE_BOARD_ITEMS,
2131            "reading one issue's board memberships past the page it came with",
2132        ),
2133        (SUB_ISSUES, "reading a project's tasks"),
2134        (BOARD, "reading the board"),
2135        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2136        (CLOSED_ITEMS, "reading the board's closed items"),
2137        (
2138            ISSUES_BY_REFERENCE,
2139            "reading a batch of issues by their references",
2140        ),
2141        (BOARD_FIELDS, "reading the board's fields"),
2142        (DRAFT, "reading one draft"),
2143        (REPOSITORY, "reading the destination repository"),
2144        (
2145            PROJECT_VISIBILITY,
2146            "reading whether the board's project is public",
2147        ),
2148        (
2149            CREATION_CONTEXT,
2150            "reading the board's fields and the destination repository",
2151        ),
2152        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2153        (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2154        (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2155        (CREATE_ISSUE, "creating an issue"),
2156        (
2157            ORIGIN_LOOKUPS,
2158            "looking up the items copied from several origins, and what creating one reads",
2159        ),
2160        (
2161            ORIGIN_LOOKUPS_WITH_MEMBERS,
2162            "looking up the items copied from several origins, with a project's members",
2163        ),
2164        (CREATE_ISSUES, "creating several issues together"),
2165        (
2166            CREATED_BOARD_ITEMS,
2167            "reading the board items of the issues a copy created",
2168        ),
2169        (
2170            WRITE_FIELD_VALUES,
2171            "writing board fields of the items a copy created",
2172        ),
2173        (
2174            WRITE_FIELDS_AND_EDGES,
2175            "writing board fields and dependencies of the items a copy created",
2176        ),
2177        (CLOSE_ISSUES, "closing the issues a copy created"),
2178        (UPDATE_ISSUES, "updating several issues together"),
2179        (ADD_BLOCKED_BYS, "recording several dependencies together"),
2180        (ADD_TO_BOARD, "adding an issue to the board"),
2181        (UPDATE_ISSUE, "updating an issue"),
2182        (UPDATE_DRAFT, "updating a draft item"),
2183        (UPDATE_FIELD, "writing a board field"),
2184        (UPDATE_FIELDS, "writing board fields together"),
2185        (CLEAR_FIELD, "clearing a board field"),
2186        (
2187            CREATE_FIELD,
2188            "creating a board single-select field with its options",
2189        ),
2190        (
2191            STATUS_OPTIONS_SNAPSHOT,
2192            "snapshotting board Status options and assignments",
2193        ),
2194        (
2195            STATUS_OPTIONS_UPDATE,
2196            "safely replacing the board Status option list",
2197        ),
2198        (ADD_SUB_ISSUE, "filing an issue under its project"),
2199        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2200        (ADD_BLOCKED_BY, "recording a dependency"),
2201        (REMOVE_BLOCKED_BY, "removing a dependency"),
2202        (DELETE_ISSUE, "deleting an issue"),
2203        (DELETE_ISSUES, "deleting several issues together"),
2204        (ISSUE_COMMENTS, "reading a task's comments"),
2205        (ISSUE_DETAIL, "reading one issue with its comments"),
2206        (
2207            ISSUE_DETAILS,
2208            "reading a batch of issues with their comments",
2209        ),
2210        (
2211            ISSUE_COPY_DETAILS,
2212            "reading a batch of issues with their comments and dependencies for a copy",
2213        ),
2214        (COMMENT_ISSUE, "reading which issue a comment is on"),
2215        (ADD_COMMENT, "adding a comment"),
2216        (UPDATE_COMMENT, "editing a comment"),
2217        (DELETE_COMMENT, "deleting a comment"),
2218        (
2219            ADD_COMMENT_WHOLE,
2220            "adding a comment, answered with the whole issue",
2221        ),
2222        (
2223            UPDATE_COMMENT_WHOLE,
2224            "editing a comment, answered with the whole issue",
2225        ),
2226        (
2227            UPDATE_ISSUE_WHOLE,
2228            "updating an issue, answered with it whole",
2229        ),
2230        (
2231            UPDATE_FIELD_WHOLE,
2232            "writing a board field, answered with the whole issue",
2233        ),
2234        (
2235            CLEAR_FIELD_WHOLE,
2236            "clearing a board field, answered with the whole issue",
2237        ),
2238    ];
2239}
2240
2241/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2242/// under, the variable carrying its input, and the boolean variable that includes it.
2243#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2244pub struct FieldSlot {
2245    /// The key of the answer's `data` this field's payload is under.
2246    pub alias: &'static str,
2247    /// The variable carrying this field's input.
2248    pub variable: &'static str,
2249    /// The boolean variable whose `true` sends this field.
2250    pub include: &'static str,
2251}
2252
2253/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2254pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2255    FieldSlot {
2256        alias: "updateProjectV2ItemFieldValue",
2257        variable: "input",
2258        include: "writeFirst",
2259    },
2260    FieldSlot {
2261        alias: "second",
2262        variable: "second",
2263        include: "writeSecond",
2264    },
2265    FieldSlot {
2266        alias: "third",
2267        variable: "third",
2268        include: "writeThird",
2269    },
2270    FieldSlot {
2271        alias: "fourth",
2272        variable: "fourth",
2273        include: "writeFourth",
2274    },
2275    FieldSlot {
2276        alias: "fifth",
2277        variable: "fifth",
2278        include: "writeFifth",
2279    },
2280    FieldSlot {
2281        alias: "sixth",
2282        variable: "sixth",
2283        include: "writeSixth",
2284    },
2285];
2286
2287/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2288pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2289    FieldSlot {
2290        alias: "cleared",
2291        variable: "clear",
2292        include: "writeClear",
2293    },
2294    FieldSlot {
2295        alias: "clearedSecond",
2296        variable: "clearSecond",
2297        include: "writeClearSecond",
2298    },
2299    FieldSlot {
2300        alias: "clearedThird",
2301        variable: "clearThird",
2302        include: "writeClearThird",
2303    },
2304];
2305
2306/// Which of GitHub's two rate limiters refused a request.
2307///
2308/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2309/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2310/// the whole reason this is carried rather than collapsed into "rate limited".
2311#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2312enum Limiter {
2313    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2314    Primary,
2315    /// The burst limiter over content-generating requests, which nothing reports.
2316    Secondary,
2317}
2318
2319/// The wordings GitHub answers a secondary rate limit with.
2320///
2321/// It sends them under a forbidden status, under a too-many-requests status, and inside
2322/// the `errors` of a *successful* response, which is why the text is what this matches on
2323/// rather than the status. `abuse detection` is the wording GitHub used before the
2324/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2325/// what a burst of content creation is refused with.
2326///
2327/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2328/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2329/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2330/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2331pub const SECONDARY_WORDINGS: [&str; 5] = [
2332    "secondary rate limit",
2333    "temporarily blocked from content creation",
2334    "abuse detection",
2335    "submitted too quickly",
2336    "exceeded a secondary",
2337];
2338
2339/// The wordings GitHub answers an exhausted primary budget with.
2340///
2341/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2342/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2343/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2344/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2345/// two phrases is a substring of it, so without it that answer read as a refusal that will
2346/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2347/// one reason.
2348pub const PRIMARY_WORDINGS: [&str; 4] = [
2349    "api rate limit exceeded",
2350    "api rate limit already exceeded",
2351    "rate limit exceeded",
2352    "rate_limited",
2353];
2354
2355/// The wordings GitHub answers a write that collided with another change to the same
2356/// ordering with, in the `errors` of an HTTP 200: "Your attempt to move this item created a
2357/// temporary conflict. Please try again." The write did not take effect and GitHub asks for it
2358/// again, so it is retried on the same growing schedule a rate limit without a hint is, within
2359/// the same retry budget — except a `createIssue`, whose answer cannot say the issue was not
2360/// made, and which is therefore reported rather than replayed into a duplicate.
2361///
2362/// Observed rather than documented, so it is pinned exactly as [`SECONDARY_WORDINGS`] is:
2363/// `tests/fixtures/rate-limits.json` records where it was read under `conflict.observed`, and
2364/// the same drift gate reconciles the two both ways. Public for that gate alone.
2365pub const CONFLICT_WORDINGS: [&str; 1] = ["created a temporary conflict"];
2366
2367/// What a response *says about itself*, which is the only place a refusal can be read.
2368///
2369/// Deliberately not the whole response body. A board is a place people write about their
2370/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2371/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2372/// reported. So the item data is never read: what is read is GitHub's own REST-style
2373/// `message` envelope, which is what a forbidden status carries, and the `message` and
2374/// `type` of each GraphQL error, which is where a *successful* response says it.
2375///
2376/// A body that is not JSON at all has nothing structured to read, so only a failing
2377/// response's own text is taken — a successful response that is not JSON is malformed
2378/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2379fn refusal_wording(status: StatusCode, body: &str) -> String {
2380    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2381        return if status.is_success() {
2382            String::new()
2383        } else {
2384            body.to_owned()
2385        };
2386    };
2387    let mut said: Vec<&str> = parsed
2388        .get("message")
2389        .and_then(Value::as_str)
2390        .into_iter()
2391        .collect();
2392    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2393        for error in errors {
2394            said.extend(
2395                ["message", "type"]
2396                    .into_iter()
2397                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
2398            );
2399        }
2400    }
2401    said.join("; ")
2402}
2403
2404impl Limiter {
2405    /// Which limiter refused this response, or `None` when none of them did.
2406    ///
2407    /// The wording is read first and the status only decides what carries none of it,
2408    /// because GitHub answers a secondary limit with a forbidden status far more often
2409    /// than with too-many-requests — while a forbidden status saying nothing about a limit
2410    /// really is a credential this token lacks.
2411    ///
2412    /// A response is a refusal because of its status or its own wording. A spent budget
2413    /// only ever explains one; it never turns an answer into a refusal.
2414    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2415        let normalized = refusal_wording(status, body).to_ascii_lowercase();
2416        if SECONDARY_WORDINGS
2417            .iter()
2418            .any(|wording| normalized.contains(wording))
2419        {
2420            return Some(Self::Secondary);
2421        }
2422        if status == StatusCode::TOO_MANY_REQUESTS {
2423            return Some(Self::Primary);
2424        }
2425        // An exhausted budget *explains* a response that failed; it does not make one that
2426        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2427        // request the budget allowed as well as on the ones it then refuses, so reading
2428        // the header alone threw away a good answer — and, once refusals were retried,
2429        // replayed a request that had already taken effect.
2430        if !status.is_success() && budget_exhausted {
2431            return Some(Self::Primary);
2432        }
2433        // A successful response saying it: GitHub reports a GraphQL rate limit in the
2434        // `errors` of an HTTP 200, where nothing about the status says so at all.
2435        if status.is_success()
2436            && PRIMARY_WORDINGS
2437                .iter()
2438                .any(|wording| normalized.contains(wording))
2439        {
2440            return Some(Self::Primary);
2441        }
2442        None
2443    }
2444
2445    /// What this limiter is called where an operator can look it up.
2446    const fn name(self) -> &'static str {
2447        match self {
2448            Self::Primary => "GitHub's primary API rate limit",
2449            Self::Secondary => "GitHub's secondary rate limit",
2450        }
2451    }
2452
2453    /// What the endpoint an operator would go and check says about this limiter.
2454    const fn where_to_look(self) -> &'static str {
2455        match self {
2456            Self::Primary => {
2457                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2458                 comes back."
2459            }
2460            Self::Secondary => {
2461                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2462                 primary budget and does not report this one, so budget showing there says \
2463                 nothing about this refusal, and every further attempt extends it."
2464            }
2465        }
2466    }
2467
2468    /// The next step this limiter actually calls for.
2469    const fn what_to_do(self) -> &'static str {
2470        match self {
2471            Self::Primary => {
2472                "wait for the reset `gh api rate_limit` reports, then run the command again."
2473            }
2474            Self::Secondary => {
2475                "leave this board alone for a few minutes, then run the command again — or \
2476                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2477            }
2478        }
2479    }
2480}
2481
2482/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2483#[derive(Debug, Clone, Copy)]
2484struct Limited {
2485    limiter: Limiter,
2486    hint: Option<u64>,
2487    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2488    /// `x-ratelimit-reset`.
2489    hinted_by: Hint,
2490}
2491
2492/// Where a refusal's hint came from, which is what its wait is named after.
2493#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2494enum Hint {
2495    /// `retry-after`, or no hint at all.
2496    RetryAfter,
2497    /// An exhausted primary budget's `x-ratelimit-reset`.
2498    PrimaryReset,
2499}
2500
2501impl Limited {
2502    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2503    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2504    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2505    /// extends a secondary limit — or the schedule alone when there is none.
2506    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2507        match (self.hint, self.hinted_by) {
2508            (Some(hint), Hint::RetryAfter) => (
2509                Duration::from_secs(hint).max(backoff),
2510                pacing::cause::RETRY_AFTER,
2511            ),
2512            (Some(hint), Hint::PrimaryReset) => (
2513                Duration::from_secs(hint).max(backoff),
2514                pacing::cause::PRIMARY_RESET,
2515            ),
2516            (None, _) => (backoff, pacing::cause::BACKOFF),
2517        }
2518    }
2519
2520    /// What the caller is told once this source has waited as long as it may.
2521    ///
2522    /// Both limiters report as [`SourceError::RateLimited`], because that is what
2523    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2524    /// about *which* limiter it was makes it a different kind of failure. What differs is
2525    /// the operator's next step, and that is what the message carries — a secondary
2526    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2527    /// budget looks fine, and then back to retry the very burst that was refused.
2528    fn exhausted(
2529        self,
2530        doing: &str,
2531        waits: u32,
2532        waited: Duration,
2533        needed: Duration,
2534        budget: Duration,
2535    ) -> SourceError {
2536        SourceError::RateLimited {
2537            retry_after_seconds: self.hint,
2538            message: Some(format!(
2539                "{} refused this source while {doing}; it waited {} out over {} and was refused \
2540                 again, and the next wait of {} would take it past the {} one call may spend \
2541                 waiting. {} next: {}",
2542                self.limiter.name(),
2543                plural(waits, "refusal"),
2544                seconds(waited),
2545                seconds(needed),
2546                seconds(budget),
2547                self.limiter.where_to_look(),
2548                self.limiter.what_to_do(),
2549            )),
2550        }
2551    }
2552}
2553
2554/// One HTTP attempt's result, with what its response said about the rate limit.
2555///
2556/// The two travel together so the record and the outcome are written from the same place:
2557/// what a response said about the budget is only readable while that response is in hand,
2558/// and what the attempt *meant* is only decidable once its body has been read.
2559struct Attempted {
2560    result: Result<Value, Attempt>,
2561    limits: accounting::RateLimit,
2562    /// GitHub's own reported cost for this call, for a document that asked for it.
2563    reported_cost: Option<u64>,
2564    response_outcome: accounting::ResponseOutcome,
2565}
2566
2567/// One attempt's outcome: an error to report, or a rate limit to wait out.
2568enum Attempt {
2569    Failed(SourceError),
2570    Limited(Limited),
2571    /// GitHub refused a write as a temporary conflict and asked for it again — see
2572    /// [`CONFLICT_WORDINGS`]. Carries what GitHub said, for the diagnostic once retrying ends.
2573    Conflicted(String),
2574}
2575
2576fn plural(count: u32, thing: &str) -> String {
2577    if count == 1 {
2578        format!("{count} {thing}")
2579    } else {
2580        format!("{count} {thing}s")
2581    }
2582}
2583
2584fn seconds(duration: Duration) -> String {
2585    format!("{:.1}s", duration.as_secs_f64())
2586}
2587
2588/// The member [`stamp_served`] writes onto every node of a response that says when it last
2589/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2590const SERVED_AT: &str = "onetaskgraphServedAt";
2591
2592/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2593/// node's time later knows the server's clock on the response that read it — which is what
2594/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2595fn stamp_served(data: &mut Value, served: &str) {
2596    match data {
2597        Value::Object(members) => {
2598            if members.contains_key("updatedAt") {
2599                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2600            }
2601            for member in members.values_mut() {
2602                stamp_served(member, served);
2603            }
2604        }
2605        Value::Array(items) => {
2606            for item in items {
2607                stamp_served(item, served);
2608            }
2609        }
2610        _ => {}
2611    }
2612}
2613
2614/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2615///
2616/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2617/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2618/// header, and neither is what makes a response a refusal — so the whole cost of one this
2619/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2620/// instead. Refusing the response over the header would turn a readable refusal into an
2621/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2622fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2623    value
2624        .and_then(|value| value.to_str().ok())
2625        .and_then(|value| value.trim().parse::<u64>().ok())
2626}
2627
2628/// A transport failure with every cause beneath it, outermost first.
2629///
2630/// The client's own text for a request it could not complete is `error sending request for
2631/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2632/// port range, a connection closed before its answer — is only in the chain of sources below
2633/// it. Those differ in what to do next, so all of them are kept.
2634pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2635    let mut text = error.to_string();
2636    let mut cause = error.source();
2637    while let Some(next) = cause {
2638        let said = next.to_string();
2639        if !text.ends_with(&said) {
2640            text = format!("{text}: {said}");
2641        }
2642        cause = next.source();
2643    }
2644    text
2645}
2646
2647/// Every mutation this source sends creates content — an issue, a board item, a field of
2648/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2649/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2650/// and what the keyword says are the same set. That is what makes the keyword a sound test
2651/// rather than a convenient one: counting an edit or a removal the limiter might not have
2652/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2653pub(crate) fn is_mutation(query: &str) -> bool {
2654    query.trim_start().starts_with("mutation")
2655}
2656
2657/// What this source was doing, for a diagnostic that has to say so.
2658///
2659/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2660/// a document added without a description is caught by that list's own gate instead of
2661/// falling through to the vague arm below.
2662fn operation_description(query: &str) -> &'static str {
2663    graphql::DOCUMENTS
2664        .iter()
2665        .find(|(document, _)| *document == query)
2666        .map_or("talking to GitHub", |(_, doing)| *doing)
2667}
2668
2669/// GitHub's published ceiling on content-generating requests in any one minute.
2670///
2671/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2672/// GitHub's number rather than this source's. This source paces at it over its window: a
2673/// content-creating request goes out at once while the minute's creations leave room for it,
2674/// and waits until they do otherwise — see the `pacing` module.
2675pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2676/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2677/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2678pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2679/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2680/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2681/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2682pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2683/// The secondary points one GraphQL request carrying a mutation is charged.
2684pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2685/// The secondary points one GraphQL request carrying no mutation is charged.
2686pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2687/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2688/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2689///
2690/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2691/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2692/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2693/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2694/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2695pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2696/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2697///
2698/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2699/// own advice for a secondary limit — wait, and wait longer each time — without spending
2700/// the first minute of a transient refusal doing nothing.
2701pub const RETRY_BACKOFF_MS: u64 = 1_000;
2702/// Total time one call may spend waiting out rate limits before it reports a failure.
2703///
2704/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2705/// short enough that a command an operator is watching returns. The bound is what makes
2706/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2707/// the limiter, not in a process nobody can tell from a wedged one.
2708pub const RETRY_BUDGET_MS: u64 = 120_000;
2709
2710fn default_token_env() -> String {
2711    "GH_PROJECTS_TOKEN".to_owned()
2712}
2713fn default_endpoint() -> String {
2714    "https://api.github.com/graphql".to_owned()
2715}
2716
2717/// The name of a `Status` single-select option on the board.
2718///
2719/// Validated on the way in rather than checked later, so a blank option name — which
2720/// nothing on a board can be — is a state this type cannot hold.
2721#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2722#[serde(try_from = "String")]
2723#[schemars(extend("minLength" = 1))]
2724pub struct ColumnName(String);
2725
2726impl ColumnName {
2727    /// The option name, as the board spells it.
2728    fn as_str(&self) -> &str {
2729        &self.0
2730    }
2731}
2732
2733impl TryFrom<String> for ColumnName {
2734    type Error = String;
2735
2736    fn try_from(name: String) -> Result<Self, Self::Error> {
2737        if name.trim().is_empty() {
2738            return Err("a status_mapping option name cannot be blank".to_owned());
2739        }
2740        Ok(Self(name))
2741    }
2742}
2743
2744/// The two closed states this product can mean.
2745///
2746/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2747/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2748/// product never decides on a person's behalf that two issues are the same work.
2749#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2750#[serde(rename_all = "kebab-case")]
2751pub enum ClosedState {
2752    /// `COMPLETED` — precisely done.
2753    Completed,
2754    /// `NOT_PLANNED` — precisely cancelled.
2755    NotPlanned,
2756}
2757
2758impl ClosedState {
2759    const fn reason(self) -> &'static str {
2760        match self {
2761            Self::Completed => "COMPLETED",
2762            Self::NotPlanned => "NOT_PLANNED",
2763        }
2764    }
2765}
2766
2767/// Configuration for one GitHub Projects v2 board.
2768///
2769/// It serializes to a document this same type reads back as itself, so a caller writing a
2770/// configuration — a consumer filing onto a board it configures — can build one here rather
2771/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2772#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2773#[serde(default, deny_unknown_fields)]
2774pub struct GitHubProjectsConfig {
2775    /// Login of the user or organization which owns the board.
2776    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2777    /// The project number shown in the board's GitHub URL.
2778    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2779    // 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.
2780    /// `owner/name` of the repository this source creates an issue in when the item's own
2781    /// `repositories` field does not decide it.
2782    ///
2783    /// An item naming exactly one repository is created there — on a source declared
2784    /// `visibility: private`, only when that repository reads private, and otherwise as an
2785    /// item naming none is; a task or a document naming none or several is created in its
2786    /// parent project's repository; and a project, or a task or document with no parent,
2787    /// naming none or several is created here. A board
2788    /// has no repository of its own and `createIssue` requires one, so a write without
2789    /// this is refused naming the field. Reads never need it.
2790    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2791    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2792    /// GitHub logins whose repositories this source may create an issue in.
2793    ///
2794    /// Absent or empty, the default, restricts nothing. Otherwise a create whose issue the
2795    /// rule `repository` describes would file in a repository owned by anyone else is refused
2796    /// before anything of it is written — by `task copy`, `task copy --create`, a member
2797    /// create of a project copy and a dry run of each — with the error kind `creation-owner`,
2798    /// naming the repository and these owners. Logins compare case-insensitively, as GitHub
2799    /// compares them. An update of an issue that already exists is never refused by it.
2800    /// `task locate` reports such a refusal as `creates_in.refused` rather than failing.
2801    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2802    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2803    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2804    #[schemars(!skip_serializing_if)]
2805    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.
2806    /// Environment variable containing a fine-grained token with Projects and Issues
2807    /// read/write plus Pull requests read-only access for every repository represented on
2808    /// the board.
2809    #[serde(default = "default_token_env")]
2810    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2811    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2812    #[serde(default = "default_endpoint")]
2813    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2814    /// Per-instance mapping from a status category to the option of the board's one
2815    /// `Status` field it lands on, for a task and for a project.
2816    ///
2817    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2818    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2819    /// where a kind left out leaves the category unmapped for that kind. A category this
2820    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2821    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2822    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2823    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2824    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2825    /// either kind. No two categories may name one option for the same kind, ignoring case.
2826    /// `unknown` may name one existing option; every unknown word then lands on it and
2827    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2828    /// each unknown word because it never creates board options.
2829    #[serde(default)]
2830    pub status_mapping: StatusMapping,
2831    /// Per-instance mapping from a task's priority to an option of this board's
2832    /// single-select field named `Priority`.
2833    ///
2834    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2835    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2836    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2837    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2838    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2839    /// no two levels may name one option. Reads and writes never create the field or an
2840    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2841    /// the board lacks is refused pointing there.
2842    #[serde(default)]
2843    pub priority_mapping: Option<PriorityMappingConfig>,
2844    /// Caller metadata values this source also writes to board text fields, so the board can
2845    /// filter on them.
2846    ///
2847    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2848    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2849    /// value. On every item write — create, update and copy, of a task, a project or a
2850    /// document — a string found there is written to the field, and nothing is sent when the
2851    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2852    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2853    /// issue body stays the one home of the value, and every read reports metadata from it.
2854    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2855    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2856    /// non-text field of that name, is refused pointing there.
2857    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2858    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2859    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2860    #[schemars(!skip_serializing_if)]
2861    pub metadata_fields: Vec<MetadataFieldConfig>,
2862    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2863    ///
2864    /// Every field keeps its shipped default when it is absent, and the defaults are
2865    /// GitHub's own published limits rather than taste. See [`Pacing`].
2866    #[serde(default)]
2867    pub pacing: PacingConfig,
2868}
2869
2870/// Which option of the board's `Priority` field each priority lands on.
2871///
2872/// One member per level rather than a map, so a key that is not a level is refused where
2873/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2874/// value in the field, not an option of it.
2875#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2876#[serde(default, deny_unknown_fields)]
2877pub struct PriorityMappingConfig {
2878    /// The option `urgent` lands on; `Urgent` when absent.
2879    pub urgent: Option<PriorityOptionName>,
2880    /// The option `high` lands on; `High` when absent.
2881    pub high: Option<PriorityOptionName>,
2882    /// The option `medium` lands on; `Medium` when absent.
2883    pub medium: Option<PriorityOptionName>,
2884    /// The option `low` lands on; `Low` when absent.
2885    pub low: Option<PriorityOptionName>,
2886}
2887
2888/// The name of an option of the board's `Priority` single-select field.
2889///
2890/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2891/// blank name.
2892#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2893#[serde(try_from = "String")]
2894#[schemars(extend("minLength" = 1))]
2895pub struct PriorityOptionName(String);
2896
2897impl PriorityOptionName {
2898    /// The option name, as the board spells it.
2899    fn as_str(&self) -> &str {
2900        &self.0
2901    }
2902}
2903
2904impl TryFrom<String> for PriorityOptionName {
2905    type Error = String;
2906
2907    fn try_from(name: String) -> Result<Self, Self::Error> {
2908        if name.trim().is_empty() {
2909            return Err("a priority_mapping option name cannot be blank".to_owned());
2910        }
2911        Ok(Self(name))
2912    }
2913}
2914
2915/// The name of the board field a priority is held in.
2916pub const PRIORITY_FIELD: &str = "Priority";
2917
2918/// The four priorities a board option can hold, in the order a new `Priority` field lists
2919/// them. `none` is not among them: it is the field holding no value.
2920///
2921/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2922/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2923/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2924/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2925pub const PRIORITY_LEVELS: [Priority; 4] = [
2926    Priority::Urgent,
2927    Priority::High,
2928    Priority::Medium,
2929    Priority::Low,
2930];
2931
2932/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2933/// see that list for what this pins.
2934#[must_use]
2935pub const fn level_position(priority: Priority) -> Option<usize> {
2936    match priority {
2937        Priority::None => None,
2938        Priority::Urgent => Some(0),
2939        Priority::High => Some(1),
2940        Priority::Medium => Some(2),
2941        Priority::Low => Some(3),
2942    }
2943}
2944
2945/// This instance's complete priority-to-option mapping, read in both directions.
2946///
2947/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2948/// two levels name one option.
2949#[derive(Debug, Clone)]
2950struct PriorityMapping {
2951    options: [PriorityOptionName; 4],
2952}
2953
2954impl PriorityMapping {
2955    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2956        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2957        let mapping = Self {
2958            options: [
2959                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2960                config.high.unwrap_or_else(|| shipped("High")),
2961                config.medium.unwrap_or_else(|| shipped("Medium")),
2962                config.low.unwrap_or_else(|| shipped("Low")),
2963            ],
2964        };
2965        for (index, option) in mapping.options.iter().enumerate() {
2966            if let Some(earlier) = mapping.options[..index]
2967                .iter()
2968                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2969            {
2970                return Err(SourceError::Config {
2971                    message: format!(
2972                        "priority_mapping of source {instance} sends both {} and {} to the board \
2973                         option {:?}; one option cannot read back as two priorities",
2974                        PRIORITY_LEVELS[earlier],
2975                        PRIORITY_LEVELS[index],
2976                        option.as_str()
2977                    ),
2978                });
2979            }
2980        }
2981        Ok(mapping)
2982    }
2983
2984    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2985    fn option(&self, priority: Priority) -> Option<&str> {
2986        level_position(priority).map(|index| self.options[index].as_str())
2987    }
2988
2989    /// The priority a board option name reports, or `None` when nothing maps to it.
2990    fn priority_of(&self, option: &str) -> Option<Priority> {
2991        self.options
2992            .iter()
2993            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2994            .map(|index| PRIORITY_LEVELS[index])
2995    }
2996
2997    /// Every mapped option name, in the order a new `Priority` field lists them.
2998    fn names(&self) -> impl Iterator<Item = &str> {
2999        self.options.iter().map(PriorityOptionName::as_str)
3000    }
3001}
3002
3003/// One metadata value this source projects onto a board text field.
3004#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3005#[serde(deny_unknown_fields)]
3006pub struct MetadataFieldConfig {
3007    /// The name of the board text field the value is written to.
3008    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.
3009    /// The top-level metadata key the value is read from. A key under the reserved
3010    /// `onetaskgraph.` prefix is refused.
3011    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.
3012    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
3013    /// key's own value is the one projected. Left out of a serialized entry when empty.
3014    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3015    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
3016    #[schemars(!skip_serializing_if)]
3017    pub path: Vec<String>,
3018}
3019
3020/// The board fields no metadata value may be projected onto, compared ignoring case: the
3021/// three this source writes itself, and those GitHub owns on every board.
3022// 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.
3023const UNPROJECTABLE_FIELDS: [&str; 13] = [
3024    STATUS_FIELD,
3025    PRIORITY_FIELD,
3026    ORIGIN_FIELD,
3027    "Title",
3028    "Assignees",
3029    "Labels",
3030    "Linked pull requests",
3031    "Milestone",
3032    "Repository",
3033    "Reviewers",
3034    "Parent issue",
3035    "Sub-issues progress",
3036    "Type",
3037];
3038
3039/// One validated [`MetadataFieldConfig`] entry.
3040#[derive(Debug, Clone)]
3041struct MetadataField {
3042    field: ProjectedField,
3043    key: ProjectedKey,
3044    path: Vec<String>,
3045}
3046
3047/// The name of a board text field a metadata value is projected onto: never blank, never one
3048/// this source or GitHub writes, and never one another entry of the same source names.
3049#[derive(Debug, Clone, PartialEq, Eq)]
3050struct ProjectedField(String);
3051
3052/// The top-level metadata key a projected value is read from: never blank, and never under
3053/// the namespace this product reserves.
3054#[derive(Debug, Clone, PartialEq, Eq)]
3055struct ProjectedKey(String);
3056
3057impl std::ops::Deref for ProjectedField {
3058    type Target = str;
3059    fn deref(&self) -> &str {
3060        &self.0
3061    }
3062}
3063
3064impl std::ops::Deref for ProjectedKey {
3065    type Target = str;
3066    fn deref(&self) -> &str {
3067        &self.0
3068    }
3069}
3070
3071/// What one item's metadata says one projected field should hold.
3072#[derive(Debug, Clone, PartialEq, Eq)]
3073enum Projected {
3074    /// A string to hold.
3075    Text(String),
3076    /// Nothing: the key or a step of the path is absent, or the value is `null`.
3077    Nothing,
3078}
3079
3080impl MetadataField {
3081    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3082    /// cannot stand, naming the source and the entry.
3083    fn resolve(
3084        configured: Vec<MetadataFieldConfig>,
3085        instance: &SourceName,
3086    ) -> Result<Vec<Self>, SourceError> {
3087        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3088        for (index, entry) in configured.into_iter().enumerate() {
3089            let refuse = |why: String| SourceError::Config {
3090                message: format!(
3091                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3092                    entry.field, entry.key
3093                ),
3094            };
3095            if entry.field.trim().is_empty() {
3096                return Err(refuse("names a blank field".to_owned()));
3097            }
3098            if entry.key.trim().is_empty() {
3099                return Err(refuse("names a blank key".to_owned()));
3100            }
3101            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3102                return Err(refuse(format!(
3103                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
3104                     for its own keys; next: project a caller key instead",
3105                    MetadataKey::RESERVED_NAMESPACE
3106                )));
3107            }
3108            if let Some(owned) = UNPROJECTABLE_FIELDS
3109                .iter()
3110                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3111            {
3112                return Err(refuse(format!(
3113                    "names the board field {owned:?}, which this source or GitHub already \
3114                     writes; next: name a text field of your own"
3115                )));
3116            }
3117            if let Some(earlier) = resolved
3118                .iter()
3119                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3120            {
3121                return Err(refuse(format!(
3122                    "names the same board field as metadata_fields[{earlier}]; one field \
3123                     cannot hold two values"
3124                )));
3125            }
3126            resolved.push(Self {
3127                field: ProjectedField(entry.field),
3128                key: ProjectedKey(entry.key),
3129                path: entry.path,
3130            });
3131        }
3132        Ok(resolved)
3133    }
3134
3135    /// The path as a refusal spells it: `[]` for the key's own value.
3136    fn spelled_path(&self) -> String {
3137        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3138    }
3139
3140    /// What `metadata` says this field should hold, or the refusal for a value no text field
3141    /// can hold.
3142    fn projected(
3143        &self,
3144        metadata: &BTreeMap<String, Value>,
3145        instance: &SourceName,
3146    ) -> Result<Projected, SourceError> {
3147        let mut value = metadata.get(&*self.key);
3148        for step in &self.path {
3149            value = value.and_then(|held| held.get(step.as_str()));
3150        }
3151        let found = match value {
3152            None | Some(Value::Null) => return Ok(Projected::Nothing),
3153            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3154            Some(Value::Bool(_)) => "a boolean",
3155            Some(Value::Number(_)) => "a number",
3156            Some(Value::Array(_)) => "an array",
3157            Some(Value::Object(_)) => "an object",
3158        };
3159        Err(SourceError::Refused {
3160            message: format!(
3161                "source {instance} projects metadata key {:?} at path {} onto its board text \
3162                 field {:?}, and this item holds {found} there; only a string, null or nothing \
3163                 can be written to a text field; next: store a string there, or remove the \
3164                 value",
3165                &*self.key,
3166                self.spelled_path(),
3167                &*self.field
3168            ),
3169        })
3170    }
3171}
3172
3173/// What one item's `Priority` field says, read through this instance's mapping.
3174#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3175enum HeldPriority {
3176    /// A priority this source reports: an option the mapping names, or no value (`none`).
3177    Read(Priority),
3178    /// An option the mapping does not name, which is never read as a level or as `none`.
3179    Unmapped(String),
3180}
3181
3182/// How fast this source writes, and how long it waits out a rate-limit refusal.
3183///
3184/// Configurable because a GitHub Enterprise installation sets its own limits and an
3185/// operator who has already been refused may want to go slower still — not because the
3186/// defaults are guesses.
3187#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3188#[serde(default, deny_unknown_fields)]
3189pub struct PacingConfig {
3190    /// Shortest interval between two content-creating mutations, in milliseconds.
3191    ///
3192    /// Zero sends them as fast as they are asked for, which is what a fixture server on
3193    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3194    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.
3195    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3196    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3197    /// zero while there is a budget to spend, because a schedule of zero-length waits
3198    /// consumes none of it and so never ends.
3199    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.
3200    /// Total time one call may spend waiting out rate limits, in milliseconds.
3201    ///
3202    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3203    /// the bound is what makes this a wait rather than a hang.
3204    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.
3205}
3206
3207/// The largest any pacing setting may be, in milliseconds.
3208///
3209/// One hour. GitHub's own harshest published bound on content-generating requests works
3210/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3211/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3212/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3213/// and an interval beyond it is a command that never sends its second mutation. It also
3214/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3215/// what a `Duration` can hold on every platform.
3216pub const MAX_PACING_MS: u64 = 3_600_000;
3217
3218/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3219/// source holds.
3220///
3221/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3222/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3223/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3224/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3225/// is asked for, windows included.
3226#[derive(Debug, Clone, Copy)]
3227struct Pacing {
3228    /// Whether requests are held for GitHub's secondary windows: always, unless
3229    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3230    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3231    windows: bool,
3232    min_mutation_interval: Duration,
3233    retry_backoff: Duration,
3234    retry_budget: Duration,
3235}
3236
3237impl Pacing {
3238    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3239    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3240        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3241            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3242                message: format!(
3243                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3244                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3245                     GitHub's own harshest published limit"
3246                ),
3247            }),
3248            Some(value) => Ok(Duration::from_millis(value)),
3249            None => Ok(Duration::from_millis(default)),
3250        };
3251        let retry_backoff = bounded(
3252            config.retry_backoff_ms,
3253            RETRY_BACKOFF_MS,
3254            "retry_backoff_ms",
3255        )?;
3256        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3257        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3258            return Err(SourceError::Config {
3259                message: format!(
3260                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3261                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3262                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3263                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3264                     waiting at all",
3265                    retry_budget.as_millis()
3266                ),
3267            });
3268        }
3269        Ok(Self {
3270            windows: config.min_mutation_interval_ms != Some(0),
3271            min_mutation_interval: bounded(
3272                config.min_mutation_interval_ms,
3273                0,
3274                "min_mutation_interval_ms",
3275            )?,
3276            retry_backoff,
3277            retry_budget,
3278        })
3279    }
3280}
3281
3282/// Factory for [`GitHubProjectsSource`].
3283#[derive(Debug, Clone, Copy, Default)]
3284pub struct Plugin;
3285
3286impl SourcePlugin for Plugin {
3287    fn kind(&self) -> &'static str {
3288        KIND
3289    }
3290    /// The endpoint, the owner and the board's number: what names one board.
3291    fn remote_identity(&self, config: &Value) -> Option<String> {
3292        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3293        Some(format!(
3294            "{}\n{}\n{}",
3295            config.endpoint, config.owner, config.project_number
3296        ))
3297    }
3298    fn config_schema(&self) -> Schema {
3299        schema_for!(GitHubProjectsConfig)
3300    }
3301    fn build(
3302        &self,
3303        name: &SourceName,
3304        config: &Value,
3305        secrets: &dyn SecretResolver,
3306    ) -> Result<Box<dyn TaskSource>, SourceError> {
3307        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3308    }
3309
3310    fn build_with_clock(
3311        &self,
3312        name: &SourceName,
3313        config: &Value,
3314        secrets: &dyn SecretResolver,
3315        clock: SharedClock,
3316    ) -> Result<Box<dyn TaskSource>, SourceError> {
3317        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3318    }
3319}
3320
3321/// The client `cell` holds, building it with `build` the first time it is asked for.
3322///
3323/// A failed build is the request's failure, with the message the source's construction gave it
3324/// before clients were deferred.
3325// 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`.
3326fn built(
3327    cell: &std::sync::OnceLock<Client>,
3328    build: impl FnOnce() -> Result<Client, SourceError>,
3329) -> Result<&Client, SourceError> {
3330    if let Some(client) = cell.get() {
3331        return Ok(client);
3332    }
3333    let client = build()?;
3334    Ok(cell.get_or_init(|| client))
3335}
3336
3337impl Plugin {
3338    /// Build a source recording every request it sends into an accounting the caller holds.
3339    ///
3340    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3341    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3342    /// session total rather than two — see [`accounting`] and
3343    /// [`GitHubProjectsSource::recording_into`].
3344    ///
3345    /// # Errors
3346    ///
3347    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3348    /// [`SourceError::Config`] for configuration this plugin cannot use and
3349    /// [`SourceError::Auth`] for a credential it cannot find.
3350    pub fn build_recording_into(
3351        &self,
3352        name: &SourceName,
3353        config: &Value,
3354        secrets: &dyn SecretResolver,
3355        ledger: Arc<Accounting>,
3356    ) -> Result<Box<dyn TaskSource>, SourceError> {
3357        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3358    }
3359
3360    fn build_recording_with_clock(
3361        &self,
3362        name: &SourceName,
3363        config: &Value,
3364        secrets: &dyn SecretResolver,
3365        ledger: Arc<Accounting>,
3366        clock: SharedClock,
3367    ) -> Result<Box<dyn TaskSource>, SourceError> {
3368        let config: GitHubProjectsConfig =
3369            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3370                message: format!("source {name}: {e}"),
3371            })?;
3372        let prefix = format!("source {name}: ");
3373        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3374            .map_err(|error| match error {
3375                // The shared `StatusMapping::distinct` names the source itself.
3376                SourceError::Config { message } if message.starts_with(&prefix) => {
3377                    SourceError::Config { message }
3378                }
3379                SourceError::Config { message } => SourceError::Config {
3380                    message: format!("{prefix}{message}"),
3381                },
3382                SourceError::Auth { message } => SourceError::Auth {
3383                    message: format!("source {name}: {message}"),
3384                },
3385                other => other,
3386            })?;
3387        source.clock = clock;
3388        Ok(Box::new(source))
3389    }
3390}
3391
3392/// Where a status category lands on this board, once configuration is resolved.
3393#[derive(Debug, Clone, PartialEq, Eq)]
3394enum StatusTarget {
3395    /// Not usable against this instance for this kind, and why.
3396    Disabled(UnmappedStatus),
3397    /// The board's `Status` option of this name.
3398    Column(ColumnName),
3399    /// A closed issue, with both its board option and the reason that says which closed it means.
3400    // 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.
3401    Terminal(ColumnName, ClosedState),
3402}
3403
3404/// Every status category, in the order the vocabulary declares them.
3405///
3406/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3407/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3408/// added to the shared vocabulary fails to compile until it is named there, and this
3409/// crate's suite reconciles this list against that enum's own derived schema, which is
3410/// generated from the variants rather than written beside them. The schema is what
3411/// catches a list left one short — a list checking only the positions it already holds
3412/// would pass while every mapping indexed by the new position panicked.
3413pub const CATEGORIES: [StatusCategory; 8] = [
3414    StatusCategory::Draft,
3415    StatusCategory::Backlog,
3416    StatusCategory::Todo,
3417    StatusCategory::Queued,
3418    StatusCategory::InProgress,
3419    StatusCategory::Done,
3420    StatusCategory::Cancelled,
3421    StatusCategory::Unknown,
3422];
3423
3424/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3425#[must_use]
3426pub const fn category_position(category: StatusCategory) -> usize {
3427    match category {
3428        StatusCategory::Draft => 0,
3429        StatusCategory::Backlog => 1,
3430        StatusCategory::Todo => 2,
3431        StatusCategory::Queued => 3,
3432        StatusCategory::InProgress => 4,
3433        StatusCategory::Done => 5,
3434        StatusCategory::Cancelled => 6,
3435        StatusCategory::Unknown => 7,
3436    }
3437}
3438
3439/// The spelling a status category is configured and reported under.
3440fn category_name(category: StatusCategory) -> &'static str {
3441    match category {
3442        StatusCategory::Draft => "draft",
3443        StatusCategory::Backlog => "backlog",
3444        StatusCategory::Todo => "todo",
3445        StatusCategory::Queued => "queued",
3446        StatusCategory::InProgress => "in-progress",
3447        StatusCategory::Done => "done",
3448        StatusCategory::Cancelled => "cancelled",
3449        StatusCategory::Unknown => "unknown",
3450    }
3451}
3452
3453/// A shipped default's option name.
3454///
3455/// The literals below are this file's own and non-blank, and they are validated by the
3456/// one constructor a configured name goes through rather than beside it.
3457fn shipped_column(name: &'static str) -> ColumnName {
3458    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3459}
3460
3461/// The shipped default for one category this instance's `status_mapping` does not mention,
3462/// for either kind.
3463fn shipped_default(category: StatusCategory) -> StatusTarget {
3464    match category {
3465        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3466        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3467        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3468        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3469        StatusCategory::Done => {
3470            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3471        }
3472        StatusCategory::Cancelled => {
3473            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3474        }
3475        StatusCategory::Draft | StatusCategory::Unknown => {
3476            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3477        }
3478    }
3479}
3480
3481/// The two kinds a status is written and read for, each with its own half of the mapping.
3482const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3483
3484/// This instance's complete category-to-target mapping for each kind, read in both
3485/// directions.
3486///
3487/// One target per category per kind, held at that category's own [`category_position`], so
3488/// a category missing from the mapping, named twice in it, or filed out of order is a state
3489/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3490/// targets are options of the board's one `Status` field.
3491#[derive(Debug, Clone)]
3492struct BoardStatuses {
3493    tasks: [StatusTarget; CATEGORIES.len()],
3494    projects: [StatusTarget; CATEGORIES.len()],
3495}
3496
3497impl BoardStatuses {
3498    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3499    /// would read back from one option.
3500    ///
3501    /// A category the mapping does not mention keeps its shipped default for both kinds; one
3502    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3503    /// omits unmapped rather than defaulted.
3504    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3505        let resolve_kind =
3506            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3507                // `CATEGORIES[position] == category` for every category — the crate's suite
3508                // asserts it — so mapping the list in order fills each category's own slot.
3509                let mut targets = CATEGORIES.map(shipped_default);
3510                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3511                    if !configured.mentions(category) {
3512                        continue;
3513                    }
3514                    *slot = match configured.name_for(category, kind) {
3515                        Err(why) => StatusTarget::Disabled(why),
3516                        Ok(name) => {
3517                            let option = ColumnName::try_from(name.as_str().to_owned())
3518                                .map_err(|message| SourceError::Config { message })?;
3519                            match category {
3520                                StatusCategory::Done => {
3521                                    StatusTarget::Terminal(option, ClosedState::Completed)
3522                                }
3523                                StatusCategory::Cancelled => {
3524                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
3525                                }
3526                                _ => StatusTarget::Column(option),
3527                            }
3528                        }
3529                    };
3530                }
3531                StatusMapping::distinct(
3532                    instance,
3533                    kind,
3534                    CATEGORIES
3535                        .iter()
3536                        .zip(&targets)
3537                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3538                )?;
3539                Ok(targets)
3540            };
3541        Ok(Self {
3542            tasks: resolve_kind(ItemKind::Task)?,
3543            projects: resolve_kind(ItemKind::Project)?,
3544        })
3545    }
3546
3547    /// Every category's target for `kind`, in category order.
3548    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3549        match kind {
3550            ItemKind::Task => &self.tasks,
3551            ItemKind::Project => &self.projects,
3552        }
3553    }
3554
3555    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3556        &self.targets(kind)[category_position(category)]
3557    }
3558
3559    /// The category a board option name reports for `kind`, or `None` when nothing of that
3560    /// kind maps to it.
3561    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3562        CATEGORIES.into_iter().find(|category| {
3563            self.target(kind, *category)
3564                .option()
3565                .is_some_and(|name| name.eq_ignore_ascii_case(option))
3566        })
3567    }
3568
3569    /// Every option name either kind maps a category to, each once ignoring case, in
3570    /// category order with a task's name before a project's — what the guarded setup asks
3571    /// the `Status` field to hold.
3572    fn wanted(&self) -> Vec<String> {
3573        let mut wanted: Vec<String> = Vec::new();
3574        for category in CATEGORIES {
3575            for kind in STATUS_KINDS {
3576                if let Some(name) = self.target(kind, category).option()
3577                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3578                {
3579                    wanted.push(name.to_owned());
3580                }
3581            }
3582        }
3583        wanted
3584    }
3585
3586    /// The status an item of `kind` reports, from the three things a read of it says: its
3587    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3588    ///
3589    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3590    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3591    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3592    /// produce, so it is read permissively rather than refused — reads are faithful, and
3593    /// refusals belong on writes.
3594    ///
3595    /// **Its name follows the closure too, unless the option agrees with it.** The option's
3596    /// own name is kept when this kind's mapping reads that option as the same category, or
3597    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3598    /// named `Shipped`. An option the mapping reads as another category contradicts the
3599    /// closure, and so does no option at all: the name is then the option the mapping gives
3600    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3601    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3602    /// it. An `unknown` closure keeps the option, or `Closed` without one.
3603    ///
3604    /// An open item's option reads through its own kind's mapping, and an option that mapping
3605    /// does not name reads as `Unknown` under its own name.
3606    ///
3607    /// One function of those three rather than of a response, so a narrow status write can
3608    /// answer what a re-read would report by applying it to the state it has just written.
3609    fn status(
3610        &self,
3611        kind: ItemKind,
3612        option: Option<&str>,
3613        closed: bool,
3614        reason: Option<&str>,
3615    ) -> Status {
3616        if closed {
3617            let category = closed_category(reason);
3618            let agrees = option.filter(|option| {
3619                category == StatusCategory::Unknown
3620                    || self
3621                        .category_of(kind, option)
3622                        .is_none_or(|read| read == category)
3623            });
3624            let name = agrees.or_else(|| self.target(kind, category).option());
3625            let fallback = match category {
3626                StatusCategory::Done => "Done",
3627                StatusCategory::Cancelled => "Cancelled",
3628                _ => "Closed",
3629            };
3630            return Status {
3631                category,
3632                name: name.or(option).unwrap_or(fallback).to_owned(),
3633            };
3634        }
3635        let name = option.unwrap_or("Open").to_owned();
3636        Status {
3637            category: self
3638                .category_of(kind, &name)
3639                .unwrap_or(StatusCategory::Unknown),
3640            name,
3641        }
3642    }
3643
3644    /// The option a closed item's `Status` has to hold for its card to agree with its
3645    /// closure, and the category that closure reads as — or `None` when the item is open,
3646    /// its closure reads `unknown`, or its option already agrees.
3647    ///
3648    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3649    /// in line, each to the one option the mapping gives its category — an item with no
3650    /// option, or at one the mapping does not name, included.
3651    fn contradicted(
3652        &self,
3653        kind: ItemKind,
3654        option: Option<&str>,
3655        closed: bool,
3656        reason: Option<&str>,
3657    ) -> Option<(ClosedAs, &str)> {
3658        if !closed {
3659            return None;
3660        }
3661        let closed_as = match closed_category(reason) {
3662            StatusCategory::Done => ClosedAs::Done,
3663            StatusCategory::Cancelled => ClosedAs::Cancelled,
3664            _ => return None,
3665        };
3666        let wanted = self.target(kind, closed_as.category()).option()?;
3667        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3668            return None;
3669        }
3670        Some((closed_as, wanted))
3671    }
3672}
3673
3674/// The category a closed issue reads as, from the reason it was closed with.
3675///
3676/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3677/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3678/// is one this source cannot read a category from.
3679fn closed_category(reason: Option<&str>) -> StatusCategory {
3680    match reason {
3681        None | Some("COMPLETED") => StatusCategory::Done,
3682        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3683        Some(_) => StatusCategory::Unknown,
3684    }
3685}
3686
3687impl BoardStatuses {
3688    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3689    /// case; a kind lacking none is left out.
3690    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3691        STATUS_KINDS
3692            .into_iter()
3693            .filter_map(|kind| {
3694                let missing: Vec<String> = self
3695                    .targets(kind)
3696                    .iter()
3697                    .filter_map(StatusTarget::option)
3698                    .filter(|wanted| {
3699                        !existing
3700                            .iter()
3701                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3702                    })
3703                    .map(str::to_owned)
3704                    .collect();
3705                (!missing.is_empty()).then_some(KindMissing { kind, missing })
3706            })
3707            .collect()
3708    }
3709}
3710
3711impl StatusTarget {
3712    /// The board option this target selects, or `None` for an unmapped one.
3713    fn option(&self) -> Option<&str> {
3714        match self {
3715            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3716            Self::Disabled(_) => None,
3717        }
3718    }
3719}
3720
3721// 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.
3722/// One repository this source can create an issue in, as `owner/name`.
3723///
3724/// Every `createIssue` this source sends names one of these: the item's own single
3725/// `repositories` entry — on a source declared private, only when it reads private — else
3726/// its parent project issue's repository, else the configured
3727/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3728/// that choice and says what it refuses before `createIssue`.
3729// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3730#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
3731#[serde(try_from = "String", into = "String")]
3732struct RepositoryTarget {
3733    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3734    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3735}
3736
3737impl RepositoryTarget {
3738    fn parse(value: &str) -> Result<Self, SourceError> {
3739        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3740            message: format!(
3741                "repository must be spelled owner/name; {value:?} names no repository"
3742            ),
3743        })?;
3744        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3745            return Err(SourceError::Config {
3746                message: format!(
3747                    "repository must be spelled owner/name with a GitHub login and one \
3748                     repository name; {value:?} is not"
3749                ),
3750            });
3751        }
3752        Ok(Self {
3753            owner: owner.to_owned(),
3754            name: name.to_owned(),
3755        })
3756    }
3757
3758    /// The one host whose repositories this source creates issues in, spelled once: it is
3759    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3760    const HOST: &str = "github.com";
3761
3762    fn origin(&self) -> String {
3763        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3764    }
3765
3766    /// The repository a normalized origin names, or why it is none this source can create
3767    /// an issue in: another host, or more or fewer than `owner/name` under this one.
3768    fn from_origin(origin: &Repository) -> Result<Self, String> {
3769        let not_here = || {
3770            format!(
3771                "{} is not a {}/owner/name repository",
3772                origin.as_str(),
3773                Self::HOST
3774            )
3775        };
3776        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3777        if host != Self::HOST {
3778            return Err(not_here());
3779        }
3780        Self::parse(rest).map_err(|_| not_here())
3781    }
3782
3783    fn slug(&self) -> String {
3784        format!("{}/{}", self.owner, self.name)
3785    }
3786}
3787
3788/// Read back from a kept context as the `owner/name` it was written as, refused unless
3789/// [`RepositoryTarget::parse`] takes it.
3790impl TryFrom<String> for RepositoryTarget {
3791    type Error = String;
3792
3793    fn try_from(slug: String) -> Result<Self, Self::Error> {
3794        Self::parse(&slug).map_err(|error| error.to_string())
3795    }
3796}
3797
3798impl From<RepositoryTarget> for String {
3799    fn from(target: RepositoryTarget) -> Self {
3800        target.slug()
3801    }
3802}
3803
3804/// A source which reads GitHub afresh for every operation.
3805pub struct GitHubProjectsSource {
3806    /// This source's configured name, used both to tell a far end naming this source
3807    /// from one naming a system it knows nothing about, and to name the instance a
3808    /// status refusal is about.
3809    name: SourceName,
3810    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3811    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3812    repository: Option<RepositoryTarget>,
3813    /// The logins whose repositories a create may file in; empty restricts nothing.
3814    creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after each entry passes GitHub's login grammar.
3815    endpoint: Url,
3816    token: SecretString,
3817    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3818    statuses: BoardStatuses,
3819    /// Where each priority lands on this board, or `None` when this instance holds none.
3820    priorities: Option<PriorityMapping>,
3821    /// The metadata values this instance projects onto board text fields, in configured order.
3822    metadata_fields: Vec<MetadataField>,
3823    /// Built on the first request rather than with the source, as [`Self::client`] says.
3824    client: std::sync::OnceLock<Client>,
3825    /// Built on the first attachment request, as [`Self::asset_client`] says.
3826    asset_client: std::sync::OnceLock<Client>,
3827    /// Every item this source has created in this command, in the order it created them —
3828    /// dropped by [`TaskSource::end_command`].
3829    ///
3830    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3831    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3832    /// a copy resolving a dependency on an item it had just created refused it as not
3833    /// found. A board read is completed from this — an item remembered here and absent from
3834    /// the read is added back, because the board really does hold it and only the read is
3835    /// behind.
3836    ///
3837    /// It is not a cache of a user's work: nothing is remembered that this process did not
3838    /// itself just write, it lives and dies with the process, and it is never consulted for
3839    /// an item this source did not create.
3840    created: Mutex<Vec<Resolved>>,
3841    /// Every item that already existed and that this source has written in this command, as
3842    /// it wrote it — dropped by [`TaskSource::end_command`].
3843    ///
3844    /// The other half of [`Self::created`], held on the same terms and for the reason a
3845    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3846    /// filter is an index behind a write this process made moments ago, so a query matching
3847    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3848    /// is remembered that this process did not itself just write.
3849    updated: Mutex<Vec<Resolved>>,
3850    /// Every issue this source has added a comment to or edited a comment of in this command
3851    /// — dropped by [`TaskSource::end_command`].
3852    ///
3853    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3854    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3855    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3856    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3857    /// from before — until the index caught up. Each id here is a candidate of every such
3858    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3859    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3860    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3861    /// process wrote is still found only once the index has it.
3862    commented: Mutex<Vec<NativeId>>,
3863    /// How fast this source writes, and how long it waits out a refusal.
3864    pacing: Pacing,
3865    /// When the last content-creating mutation finished, or the moment the furthest-out
3866    /// reserved slot releases the next one, whichever is later — so the one after it can be
3867    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3868    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3869    /// measured from.
3870    last_mutation: Mutex<Option<Duration>>,
3871    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3872    windows: Mutex<pacing::Windows>,
3873    /// The tasks a project holds, as a copy's batched read of the project and every one of them
3874    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3875    /// a listing of that project's tasks is answered without a read of its own.
3876    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3877    /// The web address of each issue this source created in the current command, as its
3878    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3879    /// request.
3880    written: Mutex<BTreeMap<NativeId, String>>,
3881    /// What a copy has held back of the items it created, between
3882    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3883    /// nothing is being held back, when every write lands whole as it is made.
3884    deferred: Mutex<Option<Deferred>>,
3885    /// Each existing issue's update held back while writes are, in the order it was made: sent
3886    /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3887    /// this source sends or reads ever sees the board without them.
3888    held_updates: Mutex<Vec<HeldUpdate>>,
3889    clock: SharedClock,
3890    /// Whether the configuration declares this source private, as
3891    /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
3892    /// named repository reads public is created — see [`Self::creation_rule`].
3893    declared_private: std::sync::atomic::AtomicBool,
3894    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3895    /// The board as this process last read it, for the length of one command — dropped by
3896    /// [`TaskSource::end_command`].
3897    ///
3898    /// A copy of a project used to re-read the whole board, paged, before writing each of
3899    /// its items, which is by far the largest part of a copy's request count and none of
3900    /// its work. Nothing else changes this board while a command runs — this source's own
3901    /// writes are the only writer — so one read answers them all.
3902    ///
3903    /// It is not a store of a user's work and it is not the cache the no-persistence
3904    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3905    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3906    /// an item this command created and then depends on resolves whether or not GitHub's
3907    /// own eventually-consistent read has caught up. A write to an item already on the
3908    /// board updates the entry here too, so what this holds is the last read plus this
3909    /// process's own writes rather than a snapshot taken before them.
3910    board_cache: Mutex<Option<Board>>,
3911    /// Every issue this board's own search reported, for the length of one command — dropped
3912    /// by [`TaskSource::end_command`].
3913    ///
3914    /// The second half of a board read, and cached for the same reason and on the same
3915    /// terms as the first: it lives and dies with the process, nothing is written down, and
3916    /// a write this process makes updates the entry here exactly as it updates the one in
3917    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3918    /// that lists this board's projects and its tasks pays for one search rather than two.
3919    search_cache: Mutex<Option<Vec<Resolved>>>,
3920    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3921    /// the length of one command — dropped by [`TaskSource::end_command`].
3922    ///
3923    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3924    /// and dies with the process, nothing is written down, a write this process makes
3925    /// updates the entry here as it updates the other two, and every answer is completed
3926    /// with this process's own writes each time it is given. A command that asks the same
3927    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3928    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3929    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3930    search_next: Mutex<BTreeMap<String, Option<String>>>,
3931    /// Records already resolved in this command, reused by writes and for comment identity.
3932    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3933    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3934    /// its item as a person has since left it.
3935    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3936    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3937    /// answered as holding nothing — each taken by the next read of that item by id and by
3938    /// nothing else, dropped by any mutation naming it, and cleared by
3939    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3940    /// read-before-write, made in the same request as the others' rather than on its own.
3941    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3942    /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3943    /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3944    /// beside the members of the one project it names, when it names one — or for the next
3945    /// request this source makes, whichever comes first.
3946    pending_origins: Mutex<Vec<String>>,
3947    /// Each project's members as a lookup carrying them read them in this command, trimmed to
3948    /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3949    /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3950    member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3951    /// Every item a listing of one project's members carried whole in this command — the
3952    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3953    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3954    /// it again. No other read is answered from it, so a read by id outside a copy's
3955    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3956    /// item, and cleared by [`TaskSource::end_command`].
3957    carried: Mutex<BTreeMap<NativeId, Resolved>>,
3958    /// Items an issue read by reference reached, held for the one status write each was read
3959    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3960    /// what such a write needs. Dropped by [`TaskSource::end_command`].
3961    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3962    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3963    /// asked for under, with the project that selector named — for the length of one command,
3964    /// dropped by [`TaskSource::end_command`].
3965    ///
3966    /// A caller pages through a project's tasks one engine page at a time, and every page
3967    /// is cut from the whole list of them, so without this each page walked every
3968    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3969    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3970    /// dies with the process, nothing is written down, a write this process makes updates or
3971    /// removes the entry here as it does there, and every answer is completed with this
3972    /// process's own writes each time it is given.
3973    children_cache: Mutex<ProjectChildren>,
3974    /// The board's own id and field definitions as this process last read them on their
3975    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3976    ///
3977    /// What a write needs of the board and its item does not say, read once per command
3978    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3979    /// lives and dies with the process and nothing is written down. It holds no item and so
3980    /// can answer no question about one — see [`Self::board_fields`].
3981    fields_cache: Mutex<Option<BoardFields>>,
3982    /// Each destination repository's node id, resolved once per repository
3983    /// rather than per issue created.
3984    ///
3985    /// A repository's node id does not change, and re-reading it for every issue of a copy
3986    /// spent one request per item on an answer this source already had. It is a map rather
3987    /// than one entry because a copy files each item in the repository its own
3988    /// `repositories` field names, so a plan across five repositories asks GitHub five
3989    /// times and not once per item.
3990    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3991    /// What every request this source sends is recorded into.
3992    ///
3993    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3994    /// a request leaves this crate, so nothing has to be switched on for a session to be
3995    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3996    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3997    /// source's reads and writes — adds up one accounting instead of two. See
3998    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3999    ledger: Arc<Accounting>,
4000    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
4001    /// open, the issues primed from the cache, and the comments known on each.
4002    expected: expected::Held,
4003    /// What [`projection`] holds for a producer's projection: a kept placement awaiting its
4004    /// setup-context token, and where the last create was placed.
4005    projection: projection::Held,
4006}
4007
4008/// The board item a single field update's answer names, refused unless it is `item_id`.
4009fn field_update_item<'a>(data: &'a Value, item_id: &str) -> Result<&'a Value, SourceError> {
4010    let returned = data
4011        .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
4012        .ok_or_else(|| SourceError::Malformed {
4013            message: "GitHub field update returned no project item".into(),
4014        })?;
4015    if required_str(returned, "id")? != item_id {
4016        return Err(SourceError::Malformed {
4017            message: "GitHub field update returned the wrong project item".into(),
4018        });
4019    }
4020    Ok(returned)
4021}
4022
4023/// GitHub's closed single-select color vocabulary.
4024#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
4025#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
4026pub enum StatusOptionColor {
4027    /// Gray.
4028    Gray,
4029    /// Blue.
4030    Blue,
4031    /// Green.
4032    Green,
4033    /// Yellow.
4034    Yellow,
4035    /// Purple.
4036    Purple,
4037    /// Red.
4038    Red,
4039    /// Orange.
4040    Orange,
4041    /// Pink.
4042    Pink,
4043}
4044
4045/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
4046/// applies its additions.
4047#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4048pub enum SetupMode {
4049    /// Read without mutation.
4050    Plan,
4051    /// Apply and verify.
4052    Apply,
4053}
4054
4055/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
4056/// against it goes on compiling.
4057pub type StatusOptionsMode = SetupMode;
4058
4059/// The explicit result of the requested operation.
4060#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4061#[serde(rename_all = "kebab-case")]
4062pub enum StatusOptionsOutcome {
4063    /// A read-only plan.
4064    Planned,
4065    /// Apply found nothing missing.
4066    Unchanged,
4067    /// Additions were applied and verified.
4068    Applied,
4069}
4070
4071/// A GitHub single-select option's opaque GraphQL node identifier.
4072#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4073#[serde(transparent)]
4074pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4075
4076impl TryFrom<String> for StatusOptionId {
4077    type Error = String;
4078
4079    fn try_from(id: String) -> Result<Self, Self::Error> {
4080        if id.trim().is_empty() {
4081            return Err("a GitHub Status option id cannot be blank".to_owned());
4082        }
4083        Ok(Self(id))
4084    }
4085}
4086
4087/// One existing or proposed option in a guarded Status-field update.
4088#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4089pub struct StatusOption {
4090    /// GitHub's stable id.
4091    pub id: StatusOptionId,
4092    /// The visible option name.
4093    pub name: ColumnName,
4094    /// GitHub's single-select color token.
4095    pub color: StatusOptionColor,
4096    /// The option description, including an empty one.
4097    pub description: String,
4098}
4099
4100/// One board item's Status assignment, retained as recovery data.
4101#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4102pub struct StatusAssignment {
4103    /// The project item id whose assignment this is.
4104    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4105    // carried verbatim as operator recovery data; introducing a semantic type would claim
4106    // validation rules GitHub does not publish and no operation here interprets.
4107    pub item_id: String,
4108    /// The selected option, absent when the item has no status.
4109    #[serde(skip_serializing_if = "Option::is_none")]
4110    pub option: Option<AssignedStatusOption>,
4111}
4112
4113/// The inseparable id and name of an assigned option.
4114#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4115pub struct AssignedStatusOption {
4116    /// GitHub's stable id.
4117    pub id: StatusOptionId,
4118    /// The visible name.
4119    pub name: ColumnName,
4120}
4121
4122/// The plan and verified outcome of reconciling configured Status options.
4123#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4124pub struct StatusOptionsReport {
4125    /// The configured source name.
4126    pub source: SourceName,
4127    /// Configured option names absent before the operation.
4128    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4129    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4130    // serialized string here preserves the report's intentionally simple public contract.
4131    pub missing: Vec<String>,
4132    /// What the requested operation did.
4133    pub outcome: StatusOptionsOutcome,
4134    /// The complete option list observed before any mutation.
4135    pub existing: Vec<StatusOption>,
4136}
4137
4138#[derive(Debug, Clone, PartialEq, Eq)]
4139struct StatusSnapshot {
4140    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4141    // passed back as the mutation's project identity; a newtype could enforce no stronger
4142    // invariant because GitHub publishes no grammar for it.
4143    board_id: String,
4144    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4145    // passed back as the mutation's field identity; a newtype could enforce no stronger
4146    // invariant because GitHub publishes no grammar for it.
4147    field_id: String,
4148    options: Vec<StatusOption>,
4149    assignments: Vec<StatusAssignment>,
4150}
4151
4152/// The name of the board field a status is held in.
4153const STATUS_FIELD: &str = "Status";
4154
4155/// Every item's value of each field `report` names, as it stood before the setup wrote
4156/// anything — what a person puts back when the setup is refused part way.
4157fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4158    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4159        .fields
4160        .iter()
4161        .map(|field| (field.field.name(), before.assignments(field.field)))
4162        .collect();
4163    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4164        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4165    })
4166}
4167
4168/// One board field the guarded setup reads and writes — every one it reads, and the only
4169/// ones it writes.
4170#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4171pub enum BoardField {
4172    /// The single-select `Status` field every instance's `status_mapping` resolves into.
4173    Status,
4174    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4175    Priority,
4176}
4177
4178impl BoardField {
4179    /// The field's name on the board.
4180    #[must_use]
4181    pub const fn name(self) -> &'static str {
4182        match self {
4183            Self::Status => STATUS_FIELD,
4184            Self::Priority => PRIORITY_FIELD,
4185        }
4186    }
4187
4188    /// The field a board calls `name`, or `None` for one this setup does not own.
4189    fn named(name: &str) -> Option<Self> {
4190        [Self::Status, Self::Priority]
4191            .into_iter()
4192            .find(|field| field.name() == name)
4193    }
4194}
4195
4196/// What the guarded setup did to one field.
4197#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4198#[serde(rename_all = "kebab-case")]
4199pub enum FieldOutcome {
4200    /// A read-only plan.
4201    Planned,
4202    /// Apply found the field there with every configured option.
4203    Unchanged,
4204    /// Missing options were added to the field that was there, and verified.
4205    Applied,
4206    /// The field was not there; it was created holding the configured options, and verified.
4207    Created,
4208}
4209
4210/// One field's plan, or its verified outcome.
4211#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4212pub struct FieldReport {
4213    /// Which field.
4214    pub field: BoardField,
4215    /// Whether the board had the field before the operation.
4216    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4217    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4218    // "outcome", "existing"}` — so folding one into the other would change a published JSON
4219    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4220    // one constructor, and it derives `outcome` from `exists` in one match.
4221    pub exists: bool,
4222    /// Configured option names the field lacked before the operation — every one of them,
4223    /// in the order a new field lists them, when the field was not there at all.
4224    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4225    // mapping name and has therefore already passed its nonblank validation; the serialized
4226    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4227    pub missing: Vec<String>,
4228    /// For the `Status` field, which item kind each missing name is configured for: one
4229    /// entry per kind `status_mapping` names a missing option for, task before project, each
4230    /// listing that kind's missing names in category order. A name both kinds use is in
4231    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4232    /// `Priority`, which only a task holds.
4233    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4234    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4235    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4236    #[schemars(!skip_serializing_if)]
4237    pub kinds: Vec<KindMissing>,
4238    /// What the requested operation did.
4239    pub outcome: FieldOutcome,
4240    /// The field's complete option list observed before any mutation; empty when the field
4241    /// was not there.
4242    pub existing: Vec<StatusOption>,
4243}
4244
4245/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4246#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4247pub struct KindMissing {
4248    /// The kind these names are configured for.
4249    pub kind: ItemKind,
4250    /// The names that kind maps a category to and the field lacked, in category order.
4251    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4252    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4253    // intentionally simple public contract.
4254    pub missing: Vec<String>,
4255}
4256
4257/// The plan and verified outcome of setting up every field a source's configuration names.
4258#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4259pub struct FieldsReport {
4260    /// The configured source name.
4261    pub source: SourceName,
4262    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4263    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4264    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4265    // per field would change a published JSON shape. The states the list could hold and the
4266    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4267    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4268    pub fields: Vec<FieldReport>,
4269    /// Each board text field the source's `metadata_fields` projects a value onto, in
4270    /// configured order. Empty — and left out of the JSON — when it projects none.
4271    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4272    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4273    // both SDKs model an absent list as an empty one rather than as `null`.
4274    #[schemars(!skip_serializing_if)]
4275    pub metadata_fields: Vec<MetadataFieldReport>,
4276}
4277
4278/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4279/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4280///
4281/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4282/// no conflict.
4283// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4284// refuses exactly what `try_from` below does.
4285#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4286#[serde(transparent)]
4287pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4288
4289/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4290/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4291/// neither SDK's validator can be relied on to support.
4292const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4293    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4294
4295impl TryFrom<String> for NonTextFieldType {
4296    type Error = String;
4297
4298    fn try_from(kind: String) -> Result<Self, Self::Error> {
4299        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4300            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4301        }
4302        if kind == "TEXT" {
4303            return Err("a TEXT field is no conflict for a projected text field".to_owned());
4304        }
4305        Ok(Self(kind))
4306    }
4307}
4308
4309impl std::fmt::Display for NonTextFieldType {
4310    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4311        formatter.write_str(&self.0)
4312    }
4313}
4314
4315/// One projected metadata text field's plan, or its verified outcome.
4316///
4317/// The setup creates a missing one as a text field and never alters a field that is there:
4318/// a same-named field of another type is reported in `conflict` and left as it is.
4319#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4320pub struct MetadataFieldReport {
4321    /// The board field's name.
4322    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.
4323    /// The metadata key the value is read from.
4324    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4325    /// The object keys walked inside that key's value; empty for the key's own value.
4326    pub path: Vec<String>,
4327    /// Whether the board had a text field of that name before the operation.
4328    // 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.
4329    pub exists: bool,
4330    /// The type of a field of that name that is not a text field, which the setup leaves as
4331    /// it is; absent — and left out of the JSON — when there is none.
4332    #[serde(default, skip_serializing_if = "Option::is_none")]
4333    pub conflict: Option<NonTextFieldType>,
4334    /// What the requested operation did: `planned`; `created` for a field that was not
4335    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4336    pub outcome: FieldOutcome,
4337}
4338
4339/// Which options one field is configured with, in the order a new field would list them.
4340struct FieldPlan {
4341    field: BoardField,
4342    wanted: Vec<String>,
4343}
4344
4345/// One single-select field as the guarded setup snapshots it.
4346#[derive(Debug, Clone, PartialEq, Eq)]
4347struct SnapshotField {
4348    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4349    // passed back as the mutation's field identity; a newtype could enforce no stronger
4350    // invariant because GitHub publishes no grammar for it.
4351    field_id: String,
4352    options: Vec<StatusOption>,
4353}
4354
4355/// Every single-select field of a board and every item's value of each.
4356#[derive(Debug, Clone, PartialEq, Eq)]
4357struct BoardSnapshot {
4358    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4359    // passed back as the mutation's project identity; a newtype could enforce no stronger
4360    // invariant because GitHub publishes no grammar for it.
4361    board_id: String,
4362    fields: BTreeMap<BoardField, SnapshotField>,
4363    /// Each board item's id, and its value of each field this setup owns that it holds one of.
4364    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4365}
4366
4367impl BoardSnapshot {
4368    /// Every item's value of `field`, in board order — the recovery data a drift refusal
4369    /// carries.
4370    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4371        self.items
4372            .iter()
4373            .map(|(item_id, values)| StatusAssignment {
4374                item_id: item_id.clone(),
4375                option: values.get(&field).cloned(),
4376            })
4377            .collect()
4378    }
4379}
4380
4381impl GitHubProjectsSource {
4382    /// Report missing configured Status options and, when `apply` is true, add them with
4383    /// a whole-list mutation that preserves every existing id and verifies the result.
4384    ///
4385    /// # Errors
4386    ///
4387    /// Refuses a board without a single-select `Status` field. A post-write difference in
4388    /// any pre-existing option id or item assignment is refused with the complete pre-write
4389    /// assignment snapshot in the diagnostic for recovery.
4390    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4391    // successful mutation, both drift refusals, source selection, missing Status, casing,
4392    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4393    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4394    // responses from entering the defensive malformed-response branches below.
4395    pub async fn status_options(
4396        &self,
4397        mode: StatusOptionsMode,
4398    ) -> Result<StatusOptionsReport, SourceError> {
4399        let before = self.status_snapshot().await?;
4400        // A terminal category's option is as configured as an open one's: a terminal
4401        // write validates it before closing and refuses when the board lacks it. Both
4402        // kinds' names are options of the one field, so both are asked for.
4403        let missing = self
4404            .statuses
4405            .wanted()
4406            .into_iter()
4407            .filter(|wanted| {
4408                !before
4409                    .options
4410                    .iter()
4411                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4412            })
4413            .collect::<Vec<_>>();
4414        let report = StatusOptionsReport {
4415            source: self.name.clone(),
4416            missing: missing.clone(),
4417            outcome: match (mode, missing.is_empty()) {
4418                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4419                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4420                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4421            },
4422            existing: before.options.clone(),
4423        };
4424        if mode == StatusOptionsMode::Plan || missing.is_empty() {
4425            return Ok(report);
4426        }
4427        let mut options = before
4428            .options
4429            .iter()
4430            .map(|option| {
4431                json!({
4432                    "id": option.id, "name": option.name, "color": option.color,
4433                    "description": option.description,
4434                })
4435            })
4436            .collect::<Vec<_>>();
4437        options.extend(missing.iter().map(|name| {
4438            json!({
4439                "name": name, "color": "GRAY", "description": ""
4440            })
4441        }));
4442        self.graphql(
4443            graphql::STATUS_OPTIONS_UPDATE,
4444            json!({"input": {
4445                "fieldId": before.field_id,
4446                "singleSelectOptions": options,
4447            }}),
4448        )
4449        .await?;
4450        let after = self.status_snapshot().await?;
4451        let options_preserved = before
4452            .options
4453            .iter()
4454            .all(|old| after.options.iter().any(|new| new == old));
4455        let additions_present = missing.iter().all(|wanted| {
4456            after
4457                .options
4458                .iter()
4459                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4460        });
4461        if !options_preserved || !additions_present || after.assignments != before.assignments {
4462            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4463                SourceError::Malformed {
4464                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4465                }
4466            })?;
4467            return Err(SourceError::Refused {
4468                message: format!(
4469                    "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}"
4470                ),
4471            });
4472        }
4473        Ok(report)
4474    }
4475
4476    /// A fresh snapshot of the Status field and every board item's assignment of it.
4477    ///
4478    /// # Errors
4479    ///
4480    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4481    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4482        // Status alone, as this operation has always read it: a `Priority` field is another
4483        // operation's, so nothing about it can refuse this one.
4484        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4485        let field = board
4486            .fields
4487            .remove(&BoardField::Status)
4488            .ok_or_else(|| self.no_status_field())?;
4489        Ok(StatusSnapshot {
4490            assignments: board.assignments(BoardField::Status),
4491            board_id: board.board_id,
4492            field_id: field.field_id,
4493            options: field.options,
4494        })
4495    }
4496
4497    /// The refusal a board with no `Status` field is answered with by the guarded setup.
4498    fn no_status_field(&self) -> SourceError {
4499        SourceError::Refused {
4500            message: format!("source {} board has no Status field", self.name),
4501        }
4502    }
4503
4504    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4505    // the real CLI loopback journey, including pagination. The individual malformed guards
4506    // are defensive validation of a schema-pinned third-party response, not separate user
4507    // journeys; drift and missing-field failures cover the operation's recovery behavior.
4508    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4509    /// every board item's value of each, walked to the end of the board's items. A field not
4510    /// in `owned` is read past whatever it holds.
4511    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4512        let mut after: Option<String> = None;
4513        let mut snapshot: Option<BoardSnapshot> = None;
4514        loop {
4515            let data = self
4516                .graphql(
4517                    graphql::STATUS_OPTIONS_SNAPSHOT,
4518                    json!({
4519                        "owner": self.owner, "number": self.project_number,
4520                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4521                    }),
4522                )
4523                .await?;
4524            let board = data
4525                .pointer("/owner/projectV2")
4526                .filter(|board| board.is_object())
4527                .ok_or_else(|| SourceError::Refused {
4528                    message: format!(
4529                        "source {} has no accessible GitHub Projects board",
4530                        self.name
4531                    ),
4532                })?;
4533            if board
4534                .pointer("/fields/pageInfo/hasNextPage")
4535                .and_then(Value::as_bool)
4536                != Some(false)
4537            {
4538                return Err(SourceError::Malformed {
4539                    message:
4540                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4541                            .into(),
4542                });
4543            }
4544            let mut fields = BTreeMap::new();
4545            // Only the fields this setup owns, by name: a node the single-select fragment did not
4546            // match carries no name, and a person's own single-select field — a `Size`, a
4547            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4548            // `Status` or `Priority` field without its options is malformed, not absent.
4549            // 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.
4550            for (owned, field) in board
4551                .pointer("/fields/nodes")
4552                .and_then(Value::as_array)
4553                .ok_or_else(|| SourceError::Malformed {
4554                    message: "GitHub project fields.nodes is not an array".into(),
4555                })?
4556                .iter()
4557                .filter_map(|field| {
4558                    let named = BoardField::named(field.get("name")?.as_str()?)?;
4559                    owned.contains(&named).then_some((named, field))
4560                })
4561            {
4562                let options = field
4563                    .get("options")
4564                    .and_then(Value::as_array)
4565                    .ok_or_else(|| SourceError::Malformed {
4566                        message: "GitHub single-select field options is not an array".into(),
4567                    })?
4568                    .iter()
4569                    .map(|option| {
4570                        Ok(StatusOption {
4571                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4572                                .map_err(|message| SourceError::Malformed { message })?,
4573                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4574                                .map_err(|message| SourceError::Malformed {
4575                                    message: format!(
4576                                        "GitHub single-select option name is invalid: {message}"
4577                                    ),
4578                                })?,
4579                            color: serde_json::from_value(
4580                                option.get("color").cloned().unwrap_or(Value::Null),
4581                            )
4582                            .map_err(|error| {
4583                                SourceError::Malformed {
4584                                    message: format!(
4585                                        "GitHub single-select option color is invalid: {error}"
4586                                    ),
4587                                }
4588                            })?,
4589                            description: optional_str(option, "description")?
4590                                .unwrap_or_default()
4591                                .to_owned(),
4592                        })
4593                    })
4594                    .collect::<Result<Vec<_>, SourceError>>()?;
4595                let snapshot = SnapshotField {
4596                    field_id: required_nonblank_str(field, "id")?.to_owned(),
4597                    options,
4598                };
4599                // A board's field names are unique, so a second one is an answer that cannot
4600                // say which field the setup would act on — refused rather than one chosen.
4601                if fields.insert(owned, snapshot).is_some() {
4602                    return Err(SourceError::Malformed {
4603                        message: format!(
4604                            "GitHub answered two {} fields for this board",
4605                            owned.name()
4606                        ),
4607                    });
4608                }
4609            }
4610            let board_id = required_nonblank_str(board, "id")?.to_owned();
4611            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4612                board_id,
4613                fields,
4614                items: Vec::new(),
4615            });
4616            let items = board
4617                .pointer("/items/nodes")
4618                .and_then(Value::as_array)
4619                .ok_or_else(|| SourceError::Malformed {
4620                    message: "GitHub project items.nodes is not an array".into(),
4621                })?;
4622            for item in items {
4623                let field_values =
4624                    item.get("fieldValues")
4625                        .ok_or_else(|| SourceError::Malformed {
4626                            message: "GitHub project item is missing fieldValues".into(),
4627                        })?;
4628                if field_values
4629                    .pointer("/pageInfo/hasNextPage")
4630                    .and_then(Value::as_bool)
4631                    != Some(false)
4632                {
4633                    return Err(SourceError::Malformed {
4634                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4635                    });
4636                }
4637                let values = item
4638                    .pointer("/fieldValues/nodes")
4639                    .and_then(Value::as_array)
4640                    .ok_or_else(|| SourceError::Malformed {
4641                        message: "GitHub project item fieldValues.nodes is not an array".into(),
4642                    })?;
4643                let item_id = required_nonblank_str(item, "id")?;
4644                let mut assigned = BTreeMap::new();
4645                for value in values {
4646                    let Some(field) = value
4647                        .pointer("/field/name")
4648                        .and_then(Value::as_str)
4649                        .and_then(BoardField::named)
4650                        .filter(|field| owned.contains(field))
4651                    else {
4652                        continue;
4653                    };
4654                    let held = assigned.insert(
4655                        field,
4656                        AssignedStatusOption {
4657                            id: StatusOptionId::try_from(
4658                                required_str(value, "optionId")?.to_owned(),
4659                            )
4660                            .map_err(|message| SourceError::Malformed { message })?,
4661                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4662                                .map_err(|message| SourceError::Malformed {
4663                                    message: format!(
4664                                        "GitHub assigned {} name is invalid: {message}",
4665                                        field.name()
4666                                    ),
4667                                })?,
4668                        },
4669                    );
4670                    // An item holds one value of a field, so a second one leaves no way to
4671                    // tell which it holds — and a verification or recovery built on either
4672                    // could restore the wrong one.
4673                    if held.is_some() {
4674                        return Err(SourceError::Malformed {
4675                            message: format!(
4676                                "GitHub answered two {} values for board item {item_id}",
4677                                field.name()
4678                            ),
4679                        });
4680                    }
4681                }
4682                current.items.push((item_id.to_owned(), assigned));
4683            }
4684            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4685                message: "GitHub project is missing items".into(),
4686            })?;
4687            let has_next = page
4688                .pointer("/pageInfo/hasNextPage")
4689                .and_then(Value::as_bool)
4690                .ok_or_else(|| SourceError::Malformed {
4691                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4692                })?;
4693            if !has_next {
4694                break;
4695            }
4696            let next =
4697                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4698            validate_cursor_progress(after.as_deref(), next)?;
4699            after = Some(next.to_owned());
4700        }
4701        snapshot.ok_or_else(|| SourceError::Malformed {
4702            message: "GitHub returned no board field snapshot".into(),
4703        })
4704    }
4705    // llmlint: ignore-end[changed_behavior_has_e2e]
4706
4707    /// Report every board field this source's configuration names and, with
4708    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4709    /// the `Priority` field when the board has none.
4710    ///
4711    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4712    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4713    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4714    /// color and description: the whole option list goes back with every existing id, because
4715    /// a re-minted id clears every item's value.
4716    ///
4717    /// # Errors
4718    ///
4719    /// Refuses a board without a single-select `Status` field. After an apply the board is
4720    /// read again, and a pre-existing option or any item's value of either field that moved is
4721    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4722    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4723    // unchanged apply, a created field, an added option to each field, drift refusal, a board
4724    // with no Status field and a non-github-projects source through the compiled CLI against
4725    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4726    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4727        let owned: Vec<BoardField> = if self.priorities.is_some() {
4728            vec![BoardField::Status, BoardField::Priority]
4729        } else {
4730            vec![BoardField::Status]
4731        };
4732        let before = self.board_snapshot(&owned).await?;
4733        let mut plans = vec![FieldPlan {
4734            field: BoardField::Status,
4735            wanted: self.statuses.wanted(),
4736        }];
4737        if !before.fields.contains_key(&BoardField::Status) {
4738            return Err(self.no_status_field());
4739        }
4740        if let Some(mapping) = &self.priorities {
4741            plans.push(FieldPlan {
4742                field: BoardField::Priority,
4743                wanted: mapping.names().map(str::to_owned).collect(),
4744            });
4745        }
4746        // The snapshot reads single-select fields alone, so a field it did not find may still
4747        // be on the board under the name, of another type: creating one beside it would fail
4748        // part way, or leave two fields of one name. Asked of the board's own field list, and
4749        // only when a field is missing.
4750        if plans
4751            .iter()
4752            .any(|plan| !before.fields.contains_key(&plan.field))
4753        {
4754            let board = self.board_fields().await?;
4755            for plan in plans
4756                .iter()
4757                .filter(|plan| !before.fields.contains_key(&plan.field))
4758            {
4759                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4760                    return Err(SourceError::Refused {
4761                        message: format!(
4762                            "source {}'s board has a {} field that is not a single-select field \
4763                             (it is a {}), so it cannot hold this source's options; next: rename \
4764                             or remove that field, then run this again",
4765                            self.name,
4766                            plan.field.name(),
4767                            optional_str(field, "__typename")?.unwrap_or("field of another type")
4768                        ),
4769                    });
4770                }
4771            }
4772        }
4773        let mut reports = Vec::new();
4774        for plan in &plans {
4775            let held = before.fields.get(&plan.field);
4776            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4777            let mut missing: Vec<String> = Vec::new();
4778            for wanted in &plan.wanted {
4779                let present = existing
4780                    .iter()
4781                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4782                    || missing
4783                        .iter()
4784                        .any(|named| named.eq_ignore_ascii_case(wanted));
4785                if !present {
4786                    missing.push(wanted.clone());
4787                }
4788            }
4789            let kinds = match plan.field {
4790                BoardField::Status => self.statuses.missing_by_kind(&existing),
4791                BoardField::Priority => Vec::new(),
4792            };
4793            reports.push(FieldReport {
4794                field: plan.field,
4795                exists: held.is_some(),
4796                kinds,
4797                outcome: match (mode, held.is_some(), missing.is_empty()) {
4798                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4799                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4800                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4801                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
4802                },
4803                missing,
4804                existing,
4805            });
4806        }
4807        // A projected field is a text field, which the single-select snapshot cannot see, so
4808        // it is asked of the board's own field list — and only when one is configured.
4809        let mut metadata_fields = Vec::new();
4810        if !self.metadata_fields.is_empty() {
4811            let board = self.board_fields().await?;
4812            for projection in &self.metadata_fields {
4813                let conflict = match Board::field(&board.fields, &projection.field)? {
4814                    None => None,
4815                    Some(field) => {
4816                        let typename = optional_str(field, "__typename")?;
4817                        let data_type = optional_str(field, "dataType")?;
4818                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4819                            None
4820                        } else {
4821                            let named = data_type
4822                                .filter(|kind| *kind != "TEXT")
4823                                .or(typename)
4824                                .unwrap_or("field of another type");
4825                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4826                                |message| SourceError::Malformed {
4827                                    message: format!(
4828                                        "GitHub answered the board's {} field with a type this \
4829                                         setup cannot read: {message}",
4830                                        &*projection.field
4831                                    ),
4832                                },
4833                            )?)
4834                        }
4835                    }
4836                };
4837                let exists =
4838                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4839                metadata_fields.push(MetadataFieldReport {
4840                    field: projection.field.to_string(),
4841                    key: projection.key.to_string(),
4842                    path: projection.path.clone(),
4843                    exists,
4844                    outcome: match (mode, exists || conflict.is_some()) {
4845                        (SetupMode::Plan, _) => FieldOutcome::Planned,
4846                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4847                        (SetupMode::Apply, false) => FieldOutcome::Created,
4848                    },
4849                    conflict,
4850                });
4851            }
4852        }
4853        let report = FieldsReport {
4854            source: self.name.clone(),
4855            fields: reports,
4856            metadata_fields,
4857        };
4858        let writes: Vec<&FieldReport> = report
4859            .fields
4860            .iter()
4861            .filter(|field| !field.missing.is_empty() || !field.exists)
4862            .collect();
4863        let creates: Vec<&MetadataFieldReport> = report
4864            .metadata_fields
4865            .iter()
4866            .filter(|field| field.outcome == FieldOutcome::Created)
4867            .collect();
4868        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4869            return Ok(report);
4870        }
4871        let mut landed: Vec<&str> = Vec::new();
4872        for field in &writes {
4873            let added = field
4874                .missing
4875                .iter()
4876                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4877            let sent = match before.fields.get(&field.field) {
4878                Some(held) => {
4879                    let mut options = held
4880                        .options
4881                        .iter()
4882                        .map(|option| {
4883                            json!({
4884                                "id": option.id, "name": option.name, "color": option.color,
4885                                "description": option.description,
4886                            })
4887                        })
4888                        .collect::<Vec<_>>();
4889                    options.extend(added);
4890                    self.graphql(
4891                        graphql::STATUS_OPTIONS_UPDATE,
4892                        json!({"input": {
4893                            "fieldId": held.field_id,
4894                            "singleSelectOptions": options,
4895                        }}),
4896                    )
4897                    .await
4898                }
4899                None => {
4900                    self.graphql(
4901                        graphql::CREATE_FIELD,
4902                        json!({"input": {
4903                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4904                            "name": field.field.name(),
4905                            "singleSelectOptions": added.collect::<Vec<_>>(),
4906                        }}),
4907                    )
4908                    .await
4909                }
4910            };
4911            // A mutation that failed does not establish that GitHub left its field as it was,
4912            // so every failure from here on carries the recovery data a drift refusal does.
4913            match sent {
4914                Ok(_) => landed.push(field.field.name()),
4915                Err(error) => {
4916                    let changed = if landed.is_empty() {
4917                        String::new()
4918                    } else {
4919                        format!("changed the {} field and then ", landed.join(" and "))
4920                    };
4921                    return Err(SourceError::Refused {
4922                        message: format!(
4923                            "the guarded field setup {changed}failed on the {} field, which it may \
4924                             have changed part way: {error}; the pre-write item assignments \
4925                             are:\n{}",
4926                            field.field.name(),
4927                            recovery(&report, &before)?
4928                        ),
4929                    });
4930                }
4931            }
4932        }
4933        for field in &creates {
4934            let sent = self
4935                .graphql(
4936                    graphql::CREATE_FIELD,
4937                    json!({"input": {
4938                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4939                    }}),
4940                )
4941                .await;
4942            if let Err(error) = sent {
4943                let changed = if landed.is_empty() {
4944                    String::new()
4945                } else {
4946                    format!("changed the {} field and then ", landed.join(" and "))
4947                };
4948                return Err(SourceError::Refused {
4949                    message: format!(
4950                        "the guarded field setup {changed}failed creating the {} text field: \
4951                         {error}; run it again to create what is still missing",
4952                        field.field
4953                    ),
4954                });
4955            }
4956            landed.push(&field.field);
4957        }
4958        // The board has been written, so a verification read that fails leaves it unverified
4959        // rather than unchanged, and says what to put back.
4960        let after = match self.board_snapshot(&owned).await {
4961            Ok(after) => after,
4962            Err(error) => {
4963                return Err(SourceError::Refused {
4964                    message: format!(
4965                        "the guarded field setup changed the {} field and then could not read the \
4966                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4967                        landed.join(" and "),
4968                        recovery(&report, &before)?
4969                    ),
4970                });
4971            }
4972        };
4973        let mut moved = Vec::new();
4974        for field in &report.fields {
4975            let name = field.field.name();
4976            let now = after
4977                .fields
4978                .get(&field.field)
4979                .map(|held| held.options.as_slice())
4980                .unwrap_or_default();
4981            if !field.existing.iter().all(|old| now.contains(old)) {
4982                moved.push(format!(
4983                    "a pre-existing {name} option id, name, color or description"
4984                ));
4985            }
4986            if !field.missing.iter().all(|wanted| {
4987                now.iter()
4988                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4989            }) {
4990                moved.push(format!("an added {name} option"));
4991            }
4992            if after.assignments(field.field) != before.assignments(field.field) {
4993                moved.push(format!("an item's {name} value"));
4994            }
4995        }
4996        if !creates.is_empty() {
4997            // Read afresh: the view held for this command is the one from before the creates.
4998            *self.fields_cache()? = None;
4999            let board = match self.board_fields().await {
5000                Ok(board) => board,
5001                Err(error) => {
5002                    return Err(SourceError::Refused {
5003                        message: format!(
5004                            "the guarded field setup changed the {} field and then could not \
5005                             read the board back to verify it: {error}; run it again to verify \
5006                             it, which creates nothing a second time",
5007                            landed.join(" and ")
5008                        ),
5009                    });
5010                }
5011            };
5012            for field in &creates {
5013                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
5014                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
5015                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
5016                });
5017                if !text {
5018                    moved.push(format!("the created {} text field", field.field));
5019                }
5020            }
5021        }
5022        if !moved.is_empty() {
5023            return Err(SourceError::Refused {
5024                message: format!(
5025                    "GitHub changed {} after the guarded field setup; the pre-write item \
5026                     assignments are:\n{}",
5027                    moved.join(", "),
5028                    recovery(&report, &before)?
5029                ),
5030            });
5031        }
5032        Ok(report)
5033    }
5034
5035    /// Validate configuration and capture the named credential without exposing it.
5036    ///
5037    /// # Errors
5038    ///
5039    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
5040    /// [`SourceError::Auth`] when the named credential is missing or empty.
5041    pub fn new(
5042        name: &SourceName,
5043        config: GitHubProjectsConfig,
5044        secrets: &dyn SecretResolver,
5045    ) -> Result<Self, SourceError> {
5046        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
5047    }
5048
5049    /// The same, recording every request it sends into an accounting the caller holds too.
5050    ///
5051    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
5052    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
5053    /// up — passes the one it records those into, so the session total accounts for the
5054    /// whole session rather than for this source's share of it.
5055    ///
5056    /// # Errors
5057    ///
5058    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
5059    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
5060    pub fn recording_into(
5061        name: &SourceName,
5062        config: GitHubProjectsConfig,
5063        secrets: &dyn SecretResolver,
5064        ledger: Arc<Accounting>,
5065    ) -> Result<Self, SourceError> {
5066        if !valid_github_owner(&config.owner) {
5067            return Err(SourceError::Config {
5068                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
5069            });
5070        }
5071        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
5072            return Err(SourceError::Config {
5073                message: format!("project_number must be between 1 and {}", i32::MAX),
5074            });
5075        }
5076        if !valid_environment_name(&config.token_env) {
5077            return Err(SourceError::Config {
5078                message: "token_env must be a valid environment-variable name".into(),
5079            });
5080        }
5081        let repository = config
5082            .repository
5083            .as_deref()
5084            .map(RepositoryTarget::parse)
5085            .transpose()?;
5086        if let Some(entry) = config
5087            .creation_owners
5088            .iter()
5089            .find(|entry| !valid_github_owner(entry))
5090        {
5091            return Err(SourceError::Config {
5092                message: format!(
5093                    "creation_owners entry {entry:?} is not a GitHub login: a login is 1-39 ASCII \
5094                     letters, digits, or single hyphens, and cannot start or end with a hyphen"
5095                ),
5096            });
5097        }
5098        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5099            message: format!("endpoint is not a valid URL: {e}"),
5100        })?;
5101        if endpoint.scheme() != "https"
5102            && !(endpoint.scheme() == "http"
5103                && endpoint
5104                    .host_str()
5105                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5106        {
5107            return Err(SourceError::Config {
5108                message:
5109                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5110                        .into(),
5111            });
5112        }
5113        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5114            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),
5115        })?;
5116        Ok(Self {
5117            name: name.clone(),
5118            owner: config.owner,
5119            project_number: config.project_number,
5120            repository,
5121            creation_owners: config.creation_owners,
5122            asset_client: std::sync::OnceLock::new(),
5123            endpoint,
5124            token,
5125            credential_name: config.token_env,
5126            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5127            priorities: config
5128                .priority_mapping
5129                .map(|mapping| PriorityMapping::resolve(mapping, name))
5130                .transpose()?,
5131            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5132            client: std::sync::OnceLock::new(),
5133            created: Mutex::new(Vec::new()),
5134            updated: Mutex::new(Vec::new()),
5135            commented: Mutex::new(Vec::new()),
5136            pacing: Pacing::resolve(config.pacing, name)?,
5137            last_mutation: Mutex::new(None),
5138            windows: Mutex::new(pacing::Windows::default()),
5139            task_children: Mutex::new(BTreeMap::new()),
5140            written: Mutex::new(BTreeMap::new()),
5141            deferred: Mutex::new(None),
5142            held_updates: Mutex::new(Vec::new()),
5143            clock: system_clock(),
5144            declared_private: std::sync::atomic::AtomicBool::new(false),
5145            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5146            board_cache: Mutex::new(None),
5147            search_cache: Mutex::new(None),
5148            narrowed_cache: Mutex::new(BTreeMap::new()),
5149            resolved_cache: Mutex::new(BTreeMap::new()),
5150            read_ahead: Mutex::new(BTreeMap::new()),
5151            pending_origins: Mutex::new(Vec::new()),
5152            member_classes: Mutex::new(BTreeMap::new()),
5153            carried: Mutex::new(BTreeMap::new()),
5154            held_cache: Mutex::new(BTreeMap::new()),
5155            children_cache: Mutex::new(BTreeMap::new()),
5156            search_next: Mutex::new(BTreeMap::new()),
5157            fields_cache: Mutex::new(None),
5158            repository_cache: Mutex::new(BTreeMap::new()),
5159            ledger,
5160            expected: expected::Held::default(),
5161            projection: projection::Held::default(),
5162        })
5163    }
5164
5165    /// The client every API request goes through, built on the first one.
5166    ///
5167    /// Not with the source: building a client loads the system's certificate store, which
5168    /// costs tens of milliseconds, and a command that configures this board but never sends to
5169    /// it — a read of another source — should not pay that in its process.
5170    fn client(&self) -> Result<&Client, SourceError> {
5171        built(&self.client, || {
5172            Client::builder()
5173                .user_agent("onetaskgraph")
5174                .build()
5175                .map_err(|e| SourceError::Config {
5176                    message: format!("cannot build HTTP client: {e}"),
5177                })
5178        })
5179    }
5180
5181    /// The client attachment requests go through, with its own redirect policy, built on the
5182    /// first one for the reason [`Self::client`] gives.
5183    fn asset_client(&self) -> Result<&Client, SourceError> {
5184        built(&self.asset_client, || assets::client(&self.endpoint))
5185    }
5186
5187    /// A snapshot of every request this source has sent, and what each cost.
5188    ///
5189    /// A value to hold and compare rather than a borrow of the accounting itself, so two
5190    /// of them can sit side by side. When this source was built with
5191    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5192    /// point of building it that way.
5193    #[must_use]
5194    pub fn accounting(&self) -> accounting::Session {
5195        self.ledger.snapshot()
5196    }
5197
5198    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5199    /// rate limit rather than handing it straight back as an error.
5200    ///
5201    /// Retrying is safe for every document here, including the mutations, and the reason
5202    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5203    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5204    /// this replays has already taken effect. An outcome this source cannot know — the
5205    /// send failed, or the body could not be read, so the mutation may well have landed —
5206    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5207    /// attempt. A duplicate write would come from replaying one of those, and none is
5208    /// replayed.
5209    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5210        // Updates held back go out before any other request, so whatever it reads or writes
5211        // sees the board as though each had been sent when it was made.
5212        Box::pin(self.send_held_updates()).await?;
5213        // Origins held back for a read ahead that never came are sent before anything else.
5214        if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5215            Box::pin(self.send_pending_origins(None)).await?;
5216        }
5217        if !self.expected.journaling() {
5218            return self.graphql_sent(query, variables).await;
5219        }
5220        // While a write the store's cache expects runs, its mutations select the whole issue
5221        // they leave behind, so the response vouches for what the cache keeps — and every
5222        // request is recorded, so the write's parts are reported from their own responses.
5223        let (query, variables) = match graphql::whole_twin(query) {
5224            Some(twin) => (twin, expected::whole_variables(variables)),
5225            None => (query, variables),
5226        };
5227        let sent = self.graphql_sent(query, variables.clone()).await;
5228        self.journal(query, &variables, &sent).await;
5229        sent
5230    }
5231
5232    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5233    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5234        if is_mutation(query)
5235            && ![
5236                graphql::ADD_COMMENT,
5237                graphql::UPDATE_COMMENT,
5238                graphql::DELETE_COMMENT,
5239                graphql::ADD_COMMENT_WHOLE,
5240                graphql::UPDATE_COMMENT_WHOLE,
5241            ]
5242            .contains(&query)
5243        {
5244            let mut cache = self.resolved_cache()?;
5245            let mut ahead = self.read_ahead()?;
5246            let mut carried = self.carried()?;
5247            for argument in ["input", "second", "third", "clear"] {
5248                if let Some(input) = variables.get(argument) {
5249                    let names = |id: &NativeId, item_id: Option<&str>| {
5250                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5251                            input
5252                                .get(key)
5253                                .and_then(Value::as_str)
5254                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
5255                        })
5256                    };
5257                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
5258                    ahead.retain(|id, item| {
5259                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5260                    });
5261                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
5262                }
5263            }
5264        }
5265        let doing = operation_description(query);
5266        let mutation = is_mutation(query);
5267        let charge = pacing::Charge::graphql(query, &variables);
5268        let mut waited = Duration::ZERO;
5269        let mut waits = 0_u32;
5270        let mut backoff = self.pacing.retry_backoff;
5271        loop {
5272            let ticket = self.pace(charge, mutation).await;
5273            // Every write but a create is asked for immediately before it is sent — a create's
5274            // fields, its relationships, its content and the delete that takes it back included
5275            // — so a copy that owns the create renews its hold first, and one that has lost it
5276            // sends nothing more. Asked again before a retry, which is sent later.
5277            if mutation
5278                && !query.contains("createIssue(")
5279                && let Err(refused) = <dyn onetaskgraph_plugin_api::WriteGuard>::check().await
5280            {
5281                self.paced(ticket, mutation);
5282                return Err(refused);
5283            }
5284            let attempt = self.send_once(query, &variables, waits + 1).await;
5285            self.paced(ticket, mutation);
5286            let remaining = self.pacing.retry_budget.saturating_sub(waited);
5287            let (wait, cause) = match attempt {
5288                Ok(data) => return Ok(data),
5289                Err(Attempt::Failed(error)) => return Err(error),
5290                Err(Attempt::Conflicted(said)) => {
5291                    // A create is never replayed: see `CONFLICT_WORDINGS`.
5292                    if query.contains("createIssue(") || backoff.is_zero() || backoff > remaining {
5293                        return Err(SourceError::Unavailable {
5294                            message: format!(
5295                                "GitHub refused this source while {doing} as a temporary \
5296                                 conflict ({said}); it retried {} over {} and the conflict \
5297                                 did not clear within the {} one call may spend waiting. \
5298                                 next: run the command again once nothing else is writing to \
5299                                 the same items",
5300                                plural(waits, "time"),
5301                                seconds(waited),
5302                                seconds(self.pacing.retry_budget),
5303                            ),
5304                        });
5305                    }
5306                    (backoff, pacing::cause::BACKOFF)
5307                }
5308                Err(Attempt::Limited(limited)) => {
5309                    let (wait, cause) = limited.wait(backoff);
5310                    // A wait of nothing spends none of the budget, so it is exhaustion rather
5311                    // than a retry. `Pacing::resolve` rules out every way of configuring one
5312                    // except a budget of zero, where reporting the first refusal is the ask.
5313                    if wait.is_zero() || wait > remaining {
5314                        return Err(limited.exhausted(
5315                            doing,
5316                            waits,
5317                            waited,
5318                            wait,
5319                            self.pacing.retry_budget,
5320                        ));
5321                    }
5322                    (wait, cause)
5323                }
5324            };
5325            self.clock.sleep_because(wait, cause).await;
5326            waited += wait;
5327            waits += 1;
5328            backoff = backoff.saturating_mul(2);
5329        }
5330    }
5331
5332    /// Hold one request charging `charge` until every secondary window it charges has room for
5333    /// the whole of it — and, for a mutation of an instance configured with
5334    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5335    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5336    ///
5337    /// The moment is decided and counted under the lock and waited out outside it, so two
5338    /// callers take two places rather than the same one, and no lock is held across an await.
5339    /// A request with room goes out at once: nothing here waits that a window does not ask for.
5340    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5341        let (wait, cause, ticket) = {
5342            // A poisoned lock here costs pacing, not correctness, and refusing the write over
5343            // it would turn an earlier failure into a second one for no gain.
5344            let mut windows = self
5345                .windows
5346                .lock()
5347                .unwrap_or_else(std::sync::PoisonError::into_inner);
5348            let now = self.clock.now();
5349            let (mut at, mut cause) = if self.pacing.windows {
5350                windows.room(now, charge)
5351            } else {
5352                (now, None)
5353            };
5354            if mutation && !self.pacing.min_mutation_interval.is_zero() {
5355                let mut last = self
5356                    .last_mutation
5357                    .lock()
5358                    .unwrap_or_else(std::sync::PoisonError::into_inner);
5359                // `checked_add` rather than `+`: adding durations can panic on overflow, and
5360                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5361                if let Some(earliest) = last
5362                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5363                    && earliest > at
5364                {
5365                    at = earliest;
5366                    cause = Some(pacing::cause::MIN_INTERVAL);
5367                }
5368                *last = Some(at);
5369            }
5370            let ticket = windows.charge(at, charge);
5371            (at.saturating_sub(now), cause, ticket)
5372        };
5373        if let (false, Some(cause)) = (wait.is_zero(), cause) {
5374            self.clock.sleep_because(wait, cause).await;
5375        }
5376        ticket
5377    }
5378
5379    /// Record that the request `ticket` was charged for has finished, so each window — and
5380    /// the configured spacing — counts it from here rather than from the moment it left.
5381    ///
5382    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5383    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5384    /// departure would hand the limiter a window that looked roomier than it is, so a source
5385    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5386    /// paced well inside a board's threshold was once refused by it on a slower machine while
5387    /// passing on a quick one. The previous request had arrived before its response came back,
5388    /// so counting from here is never earlier than the limiter does, whatever transit costs
5389    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5390    /// reads for the next fifty minutes.
5391    ///
5392    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5393    /// one that never left costs only room nobody needed.
5394    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5395        let now = self.clock.now();
5396        self.windows
5397            .lock()
5398            .unwrap_or_else(std::sync::PoisonError::into_inner)
5399            .finish(ticket, now);
5400        if mutation && !self.pacing.min_mutation_interval.is_zero() {
5401            let mut last = self
5402                .last_mutation
5403                .lock()
5404                .unwrap_or_else(std::sync::PoisonError::into_inner);
5405            // `max` rather than an assignment: a concurrent caller may already have reserved a
5406            // slot further out, and completing this request must never pull that slot back in.
5407            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5408        }
5409    }
5410
5411    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5412    /// failure that waiting cannot help — and recorded, whichever of the three it was.
5413    ///
5414    /// This is the one place a request leaves this crate, which is why the accounting is
5415    /// here rather than at each of the callers: a read path added later is counted without
5416    /// anybody remembering to count it, and
5417    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5418    /// when one is not.
5419    async fn send_once(
5420        &self,
5421        query: &str,
5422        variables: &Value,
5423        number: u32,
5424    ) -> Result<Value, Attempt> {
5425        let executed = self.ledger.priced_document(query);
5426        let Attempted {
5427            result,
5428            limits,
5429            reported_cost,
5430            response_outcome,
5431        } = self.attempt(executed.as_ref(), variables).await;
5432        // No `otherwise` name: every document this source sends is one of its own, and the
5433        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5434        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5435        let outcome = match &result {
5436            Ok(_) => accounting::Outcome::Answered,
5437            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5438            Err(Attempt::Failed(_) | Attempt::Conflicted(_)) => accounting::Outcome::Refused,
5439        };
5440        self.ledger
5441            .record(sending.finished(outcome, limits).with_execution(
5442                Some(executed.into_owned()),
5443                response_outcome,
5444                number,
5445            ));
5446        result
5447    }
5448
5449    /// The attempt itself, with what its response said about the rate limit alongside.
5450    ///
5451    /// The two are returned together rather than recorded here because every one of the
5452    /// early exits below is a different outcome, and a record written at each of them is a
5453    /// record one of them can be added without.
5454    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5455        let mut limits = accounting::RateLimit::default();
5456        let mut reported_cost = None;
5457        let mut response_outcome = accounting::ResponseOutcome::TransportError;
5458        let result = self
5459            .attempted(
5460                query,
5461                variables,
5462                &mut limits,
5463                &mut reported_cost,
5464                &mut response_outcome,
5465            )
5466            .await;
5467        Attempted {
5468            result,
5469            limits,
5470            reported_cost,
5471            response_outcome,
5472        }
5473    }
5474
5475    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5476    async fn attempted(
5477        &self,
5478        query: &str,
5479        variables: &Value,
5480        limits: &mut accounting::RateLimit,
5481        reported_cost: &mut Option<u64>,
5482        response_outcome: &mut accounting::ResponseOutcome,
5483    ) -> Result<Value, Attempt> {
5484        let response = self
5485            .client()
5486            .map_err(Attempt::Failed)?
5487            .post(self.endpoint.clone())
5488            .bearer_auth(self.token.expose_secret())
5489            .json(&json!({"query": query, "variables": variables}))
5490            .send()
5491            .await
5492            .map_err(|e| {
5493                Attempt::Failed(SourceError::Unavailable {
5494                    message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5495                })
5496            })?;
5497        let status = response.status();
5498        if !status.is_success() {
5499            *response_outcome = accounting::ResponseOutcome::HttpError;
5500        }
5501        let header = |name: &str| whole_seconds(response.headers().get(name));
5502        let served = response
5503            .headers()
5504            .get(reqwest::header::DATE)
5505            .and_then(|value| value.to_str().ok())
5506            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5507            .map(|at| at.with_timezone(&Utc));
5508        *limits = accounting::RateLimit::read(|name| {
5509            response
5510                .headers()
5511                .get(name)
5512                .and_then(|value| value.to_str().ok())
5513                .map(str::to_owned)
5514        });
5515        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5516        // that are not text at all — is "not known to be exhausted". This never makes a
5517        // response a refusal on its own: it says which limiter a refusal is attributed to
5518        // and where its hint comes from, so a value this cannot read costs a hint rather
5519        // than an answer.
5520        let exhausted = response
5521            .headers()
5522            .get("x-ratelimit-remaining")
5523            .and_then(|value| value.to_str().ok())
5524            == Some("0");
5525        // `retry-after` is what GitHub asks for when it asks; when it does not and the
5526        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5527        // which is the same question answered as an absolute time. Nothing else here is a
5528        // hint, and a schedule is what answers a refusal that carries none.
5529        let retry_after = header("retry-after");
5530        let hinted_by = if retry_after.is_some() || !exhausted {
5531            Hint::RetryAfter
5532        } else {
5533            Hint::PrimaryReset
5534        };
5535        let hint = retry_after.or_else(|| {
5536            exhausted
5537                .then(|| header("x-ratelimit-reset"))
5538                .flatten()
5539                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5540        });
5541        // Read before it is parsed, because the evidence which tells a secondary rate
5542        // limit from a rejected credential is in the body of a response whose status says
5543        // only "forbidden" — and a non-success response was never parsed at all.
5544        let body = response.text().await.map_err(|e| {
5545            Attempt::Failed(SourceError::Unavailable {
5546                message: format!(
5547                    "GitHub GraphQL response could not be read: {}",
5548                    transport_error(&e)
5549                ),
5550            })
5551        })?;
5552        *reported_cost = serde_json::from_str::<Value>(&body)
5553            .ok()
5554            .as_ref()
5555            .and_then(|body| body.pointer("/data/rateLimit/cost"))
5556            .and_then(Value::as_u64);
5557        let parsed = serde_json::from_str::<Value>(&body).ok();
5558        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5559        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5560            return Err(Attempt::Limited(Limited {
5561                limiter,
5562                hint,
5563                hinted_by,
5564            }));
5565        }
5566        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5567            return Err(Attempt::Failed(SourceError::Auth {
5568                message: format!(
5569                    "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"
5570                ),
5571            }));
5572        }
5573        if !status.is_success() {
5574            return Err(Attempt::Failed(SourceError::Unavailable {
5575                message: format!("GitHub GraphQL returned HTTP {status}"),
5576            }));
5577        }
5578        let said = refusal_wording(status, &body);
5579        let normalized = said.to_ascii_lowercase();
5580        if CONFLICT_WORDINGS
5581            .iter()
5582            .any(|wording| normalized.contains(wording))
5583        {
5584            return Err(Attempt::Conflicted(said));
5585        }
5586        let answered = if query == graphql::ISSUES_BY_REFERENCE {
5587            self.answer_unresolved(&body)
5588        } else if query == graphql::CREATE_ISSUES {
5589            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5590        } else {
5591            self.answer(&body)
5592        };
5593        let mut data = answered.map_err(Attempt::Failed)?;
5594        if let Some(served) = served {
5595            stamp_served(&mut data, &served.to_rfc3339());
5596        }
5597        Ok(data)
5598    }
5599
5600    /// What one successful HTTP response says, once its GraphQL errors are read.
5601    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5602    /// for this credential — a repository it cannot see, an issue number it does not hold —
5603    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5604    /// that reference rather than a failure of the others. Any other error refuses the read,
5605    /// as there.
5606    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5607        let parsed: Option<Value> = serde_json::from_str(body).ok();
5608        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5609        // `issue`, and the answer holds `null` at exactly that path: anything else — another
5610        // kind of error, one naming no slot, or one beside a value it says is not there — is a
5611        // refusal of the whole read.
5612        let unresolved = |data: &Value, error: &Value| {
5613            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5614                && error
5615                    .get("path")
5616                    .and_then(Value::as_array)
5617                    .is_some_and(|path| {
5618                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5619                            alias
5620                                .strip_prefix('r')
5621                                .and_then(|slot| slot.parse::<usize>().ok())
5622                                .filter(|slot| *slot < REFERENCE_BATCH)
5623                                .map(|_| alias)
5624                        });
5625                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5626                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5627                            (Some(alias), Some("issue"), 2) => data
5628                                .get(alias)
5629                                .and_then(|repository| repository.get("issue"))
5630                                .is_some_and(Value::is_null),
5631                            _ => false,
5632                        }
5633                    })
5634        };
5635        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5636            parsed.get("data").is_some_and(|data| {
5637                data.is_object()
5638                    && parsed
5639                        .get("errors")
5640                        .and_then(Value::as_array)
5641                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5642            })
5643        });
5644        match parsed {
5645            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5646            _ => self.answer(body),
5647        }
5648    }
5649
5650    fn answer(&self, body: &str) -> Result<Value, SourceError> {
5651        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5652            message: format!("GitHub returned invalid JSON: {e}"),
5653        })?;
5654        let errors = body
5655            .get("errors")
5656            .map(|value| {
5657                value.as_array().ok_or_else(|| SourceError::Malformed {
5658                    message: "GitHub response errors is not an array".into(),
5659                })
5660            })
5661            .transpose()?;
5662        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5663            let messages = errors
5664                .iter()
5665                .filter_map(|e| e.get("message").and_then(Value::as_str))
5666                .collect::<Vec<_>>()
5667                .join("; ");
5668            let message = if messages.is_empty() {
5669                "GitHub returned GraphQL errors".into()
5670            } else {
5671                messages
5672            };
5673            let normalized = message.to_ascii_lowercase();
5674            if normalized.contains("resource not accessible") || normalized.contains("scope") {
5675                return Err(SourceError::Auth {
5676                    message: format!(
5677                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5678                        self.credential_name
5679                    ),
5680                });
5681            }
5682            return Err(SourceError::Refused { message });
5683        }
5684        body.get("data")
5685            .filter(|data| data.is_object())
5686            .cloned()
5687            .ok_or_else(|| SourceError::Malformed {
5688                message: "GitHub response has no data object".into(),
5689            })
5690    }
5691
5692    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5693    // GraphQL cannot independently page them inside the outer item page. This source page is
5694    // deliberately bounded at that published maximum; the live drift journey exercises it.
5695    async fn board_page(
5696        &self,
5697        items_after: Option<&str>,
5698        items_first: u32,
5699    ) -> Result<Value, SourceError> {
5700        let data = self
5701            .graphql(
5702                graphql::BOARD,
5703                json!({"owner":self.owner,"number":self.project_number,
5704                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5705                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5706            )
5707            .await?;
5708        data.pointer("/owner/projectV2")
5709            .filter(|v| !v.is_null())
5710            .cloned()
5711            .ok_or_else(|| SourceError::Refused {
5712                message: format!(
5713                    "GitHub project {}/{} was not found or is not visible to the token",
5714                    self.owner, self.project_number
5715                ),
5716            })
5717    }
5718
5719    /// The search that finds the issues of this board, narrowed by `also` when it is
5720    /// given.
5721    ///
5722    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5723    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5724    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5725    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5726    /// from a task by the `parent` field each issue carries rather than by the search.
5727    fn board_search(&self, also: Option<&str>) -> String {
5728        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5729        match also {
5730            Some(also) => format!("{scope} {also}"),
5731            None => scope,
5732        }
5733    }
5734
5735    /// One issue this source reached directly, as the board item a read of the board would
5736    /// have produced — or `None` when this board does not hold it.
5737    ///
5738    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5739    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5740    /// item's own id, that item's field values, and the issue as its content. One resolver
5741    /// for both routes is what makes an issue read through a search, through its own node
5742    /// id, or through its project's sub-issues report the same title, the same status, the
5743    /// same labels and the same qualified id.
5744    ///
5745    /// An issue with no entry for *this* board is not this source's to report, which is
5746    /// what keeps an id naming some other repository's issue from being answered as an item
5747    /// of this board. That answer is given about an **exhausted** connection and never
5748    /// about an unread page: the entry is looked for on the page in hand, and only if that
5749    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5750    /// rest of it.
5751    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5752        self.resolve_issue_as(issue, true).await
5753    }
5754
5755    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5756    /// and writes only when `remember` says to.
5757    async fn resolve_issue_as(
5758        &self,
5759        issue: &Value,
5760        remember: bool,
5761    ) -> Result<Option<Resolved>, SourceError> {
5762        if optional_str(issue, "__typename")? != Some("Issue") {
5763            return Ok(None);
5764        }
5765        let memberships = issue
5766            .get("projectItems")
5767            .ok_or_else(|| SourceError::Malformed {
5768                message: "GitHub issue is missing projectItems".into(),
5769            })?;
5770        let nodes = memberships
5771            .get("nodes")
5772            .and_then(Value::as_array)
5773            .ok_or_else(|| SourceError::Malformed {
5774                message: "GitHub issue projectItems.nodes is not an array".into(),
5775            })?;
5776        let held = match self.board_entry(nodes) {
5777            Some(held) => held.clone(),
5778            None => {
5779                let info = memberships
5780                    .get("pageInfo")
5781                    .ok_or_else(|| SourceError::Malformed {
5782                        message: "GitHub issue projectItems has no pageInfo".into(),
5783                    })?;
5784                // The page held no entry for this board. Whether that means the issue is
5785                // not on it is a question about the rest of the connection, and only a
5786                // connection with no rest answers it here.
5787                if !required_bool(info, "hasNextPage")? {
5788                    return Ok(None);
5789                }
5790                let cursor = required_str(info, "endCursor")?;
5791                validate_cursor_progress(None, cursor)?;
5792                let issue_id = required_str(issue, "id")?;
5793                match self.board_membership(issue_id, cursor).await? {
5794                    Some(held) => held,
5795                    None => return Ok(None),
5796                }
5797            }
5798        };
5799        // The membership's own `updatedAt` rides along: it is the board half of the item's change
5800        // token, which a field change moves where the issue's own time does not.
5801        let item = json!({
5802            "id": required_str(&held, "id")?,
5803            "updatedAt": held.get("updatedAt"),
5804            "project": held.get("project"),
5805            "fieldValues": held.get("fieldValues"),
5806            "content": issue,
5807        });
5808        if remember {
5809            self.resolve(&item)
5810        } else {
5811            self.resolve_unremembered(&item)
5812        }
5813    }
5814
5815    /// This board's own entry among one page of an issue's `Issue.projectItems`.
5816    ///
5817    /// One spelling of *which membership is this board's*, so the page a read carries and
5818    /// the pages [`Self::board_membership`] walks are searched by the same rule.
5819    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5820        nodes.iter().find(|node| {
5821            node.pointer("/project/number").and_then(Value::as_u64)
5822                == Some(u64::from(self.project_number))
5823        })
5824    }
5825
5826    /// The rest of one issue's board memberships, from `after`, for this board's entry.
5827    ///
5828    /// The recovery read: a page of memberships that holds no entry for this board says
5829    /// nothing about the memberships past it, so the connection is walked to exhaustion
5830    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5831    /// positive answer — the whole connection was read and no entry named this board —
5832    /// rather than a failure, and the walk is held to
5833    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5834    /// with a cursor that does not advance is refused instead of spun on.
5835    async fn board_membership(
5836        &self,
5837        issue: &str,
5838        after: &str,
5839    ) -> Result<Option<Value>, SourceError> {
5840        let mut after = after.to_owned();
5841        loop {
5842            let data = self
5843                .graphql(
5844                    graphql::ISSUE_BOARD_ITEMS,
5845                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5846                           "nestedFirst":NESTED_PAGE_SIZE}),
5847                )
5848                .await?;
5849            let Some(connection) = data
5850                .pointer("/node/projectItems")
5851                .filter(|value| !value.is_null())
5852            else {
5853                // The id resolved to nothing, or to something with no memberships to walk —
5854                // which is the same answer as a connection holding no entry for this board.
5855                return Ok(None);
5856            };
5857            let nodes = connection
5858                .get("nodes")
5859                .and_then(Value::as_array)
5860                .ok_or_else(|| SourceError::Malformed {
5861                    message: "GitHub issue projectItems.nodes is not an array".into(),
5862                })?;
5863            if let Some(held) = self.board_entry(nodes) {
5864                return Ok(Some(held.clone()));
5865            }
5866            let info = connection
5867                .get("pageInfo")
5868                .ok_or_else(|| SourceError::Malformed {
5869                    message: "GitHub issue projectItems has no pageInfo".into(),
5870                })?;
5871            let next = required_bool(info, "hasNextPage")?
5872                .then(|| required_str(info, "endCursor"))
5873                .transpose()?;
5874            match next {
5875                Some(next) => {
5876                    validate_cursor_progress(Some(&after), next)?;
5877                    after = next.to_owned();
5878                }
5879                None => return Ok(None),
5880            }
5881        }
5882    }
5883
5884    /// One page of a board-scoped issue search, and where the next page resumes.
5885    async fn search_page(
5886        &self,
5887        search: &str,
5888        first: u32,
5889        after: Option<&str>,
5890    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5891        let data = self
5892            .graphql(
5893                graphql::SEARCH_ISSUES,
5894                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5895                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5896                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5897            )
5898            .await?;
5899        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5900            message: "GitHub search response has no search connection".into(),
5901        })?;
5902        let mut found = Vec::new();
5903        for node in connection
5904            .get("nodes")
5905            .and_then(Value::as_array)
5906            .ok_or_else(|| SourceError::Malformed {
5907                message: "GitHub search nodes is not an array".into(),
5908            })?
5909        {
5910            if let Some(resolved) = self.resolve_issue(node).await? {
5911                found.push(resolved);
5912            }
5913        }
5914        let info = connection
5915            .get("pageInfo")
5916            .ok_or_else(|| SourceError::Malformed {
5917                message: "GitHub search connection has no pageInfo".into(),
5918            })?;
5919        let next = required_bool(info, "hasNextPage")?
5920            .then(|| required_str(info, "endCursor"))
5921            .transpose()?
5922            .map(str::to_owned);
5923        if let Some(next) = &next {
5924            validate_cursor_progress(after, next)?;
5925        }
5926        Ok((found, next))
5927    }
5928
5929    /// Every issue this board holds, completed with what this run wrote.
5930    ///
5931    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5932    /// is an index and is eventually consistent, so an issue this run created seconds ago
5933    /// can be absent from it, and a project listed straight after being written would
5934    /// otherwise be missing from its own board. What is added back is only what this
5935    /// process itself wrote, out of [`Self::created`], which lives and dies with the
5936    /// process.
5937    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5938        let found = self.searched_issues().await?;
5939        self.completed_with_written(found, |_| true)
5940    }
5941
5942    /// Every issue this board's own search reports, walked to exhaustion, read once per
5943    /// source.
5944    ///
5945    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5946    /// needs it too and the two would otherwise walk the same search twice in one command.
5947    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5948    /// is.
5949    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5950        let cached = self.search_cache()?.clone();
5951        if let Some(held) = cached {
5952            return Ok(held);
5953        }
5954        let mut after: Option<String> = None;
5955        let mut found = Vec::new();
5956        let search = self.board_search(None);
5957        loop {
5958            let (page, next) = self
5959                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5960                .await?;
5961            found.extend(page);
5962            match next {
5963                Some(next) => after = Some(next),
5964                None => break,
5965            }
5966        }
5967        *self.search_cache()? = Some(found.clone());
5968        Ok(found)
5969    }
5970
5971    /// Every issue this board's own search reports with `also` beside its scope, walked to
5972    /// exhaustion — held for no later read, which may not be narrowed alike.
5973    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5974        let mut after: Option<String> = None;
5975        let mut found = Vec::new();
5976        let search = self.board_search(Some(also));
5977        loop {
5978            let (page, next) = self
5979                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5980                .await?;
5981            found.extend(page);
5982            match next {
5983                Some(next) => after = Some(next),
5984                None => return Ok(found),
5985            }
5986        }
5987    }
5988
5989    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5990    fn search_cache(
5991        &self,
5992    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5993        self.search_cache
5994            .lock()
5995            .map_err(|_| SourceError::Unavailable {
5996                message: "this source's view of the board's issues was left inconsistent by an \
5997                      earlier failure; next: run the command again"
5998                    .into(),
5999            })
6000    }
6001
6002    /// `found`, with everything this run wrote that `keep` accepts and the read did not
6003    /// report.
6004    ///
6005    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
6006    /// at all: the search index is behind, and a node read of an item filed moments ago can
6007    /// be too.
6008    fn completed_with_written(
6009        &self,
6010        mut found: Vec<Resolved>,
6011        keep: impl Fn(&Resolved) -> bool,
6012    ) -> Result<Vec<Resolved>, SourceError> {
6013        for own in self.created()?.iter().filter(|own| keep(own)) {
6014            if !found.iter().any(|item| item.id == own.id) {
6015                found.push(own.clone());
6016            }
6017        }
6018        Ok(found)
6019    }
6020
6021    /// What resolving one node id reached.
6022    ///
6023    /// Three answers rather than an `Option`, because a board *draft* is none of the other
6024    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
6025    /// is completed by a read of the draft itself rather than reported as nothing.
6026    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
6027        let asked = self
6028            .graphql(
6029                graphql::ISSUE,
6030                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
6031                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6032            )
6033            .await;
6034        let data = match asked {
6035            Ok(data) => data,
6036            // A string that is not a node id at all is not a failure to report: it is an id
6037            // this board does not hold, which is what every read of one already answers.
6038            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
6039            Err(error) => return Err(error),
6040        };
6041        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6042            return Ok(Reached::Nothing);
6043        };
6044        if optional_str(node, "__typename")? == Some("DraftIssue") {
6045            return Ok(Reached::Draft);
6046        }
6047        Ok(match self.resolve_issue(node).await? {
6048            Some(item) => Reached::Held(Box::new(item)),
6049            None => Reached::Nothing,
6050        })
6051    }
6052
6053    /// One item of this board by its own id, whatever kind it is.
6054    ///
6055    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
6056    /// run wrote is read first, because a node read of an item created moments ago can
6057    /// still be behind the board field values written onto it — see [`Self::created`].
6058    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6059        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
6060            return Ok(Some(own.clone()));
6061        }
6062        if let Some(held) = self.read_ahead()?.remove(id) {
6063            return Ok(held);
6064        }
6065        match self.reach(id).await? {
6066            Reached::Held(item) => Ok(Some(*item)),
6067            Reached::Nothing => Ok(None),
6068            Reached::Draft => self.draft_by_id(id).await,
6069        }
6070    }
6071
6072    /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
6073    /// answers the project it is filed under and that project's members beside it.
6074    ///
6075    /// Each is held for this command as a read of it would have been: the project for the
6076    /// next read of it by its id, and — when its page of members is the whole of them — the
6077    /// members for the next listing of that project. So a write held to who may read the
6078    /// document, which reads all three, is answered by this one request.
6079    async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6080        if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
6081            return self.item_by_id(id).await;
6082        }
6083        let asked = self
6084            .graphql(
6085                graphql::DOCUMENT_IN_PROJECT,
6086                json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
6087                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6088                       "duplicates":true}),
6089            )
6090            .await;
6091        let data = match asked {
6092            Ok(data) => data,
6093            Err(error) if unresolvable_node(&error) => return Ok(None),
6094            Err(error) => return Err(error),
6095        };
6096        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6097            return Ok(None);
6098        };
6099        if optional_str(node, "__typename")? == Some("DraftIssue") {
6100            return self.draft_by_id(id).await;
6101        }
6102        let Some(item) = self.resolve_issue(node).await? else {
6103            return Ok(None);
6104        };
6105        // An answer for another issue than the one asked about says nothing of that one.
6106        if item.id != *id {
6107            return Err(SourceError::Malformed {
6108                message: format!(
6109                    "GitHub answered issue {} when asked for document {}",
6110                    item.id.0, id.0
6111                ),
6112            });
6113        }
6114        if let Some(project) = node.get("project").filter(|value| !value.is_null())
6115            && let Some(parent) = self
6116                .resolve_issue(project)
6117                .await?
6118                .filter(|parent| Some(&parent.id) == item.parent.as_ref())
6119        {
6120            let members = project.get("subIssues").filter(|value| !value.is_null());
6121            let whole = members
6122                .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
6123                .and_then(Value::as_bool)
6124                == Some(false);
6125            if whole {
6126                let nodes = members
6127                    .and_then(|members| members.get("nodes"))
6128                    .and_then(Value::as_array)
6129                    .ok_or_else(|| SourceError::Malformed {
6130                        message: "GitHub subIssues.nodes is not an array".into(),
6131                    })?;
6132                let mut children = Vec::new();
6133                for member in nodes {
6134                    if let Some(resolved) = self.resolve_issue(member).await? {
6135                        children.push(resolved);
6136                    }
6137                }
6138                // Held as the project's members only when every one resolved and says it is
6139                // filed under it: a page with any other answers nothing of all the members.
6140                if children.len() == nodes.len()
6141                    && children
6142                        .iter()
6143                        .all(|child| child.parent.as_ref() == Some(&parent.id))
6144                {
6145                    self.children_cache()?
6146                        .insert(parent.id.clone(), (parent.id.clone(), children));
6147                }
6148            }
6149            self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6150        }
6151        Ok(Some(item))
6152    }
6153
6154    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6155    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6156    /// than one request per id.
6157    ///
6158    /// What this run wrote answers first, as it does there, and only the rest is read. One id
6159    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6160    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6161    /// id at a time, so that id is answered as not held and the others as themselves; a draft
6162    /// is completed by a read of the draft, exactly as there.
6163    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6164        let mut found: Vec<Option<Option<Resolved>>> = {
6165            let created = self.created()?;
6166            ids.iter()
6167                .map(|id| {
6168                    created
6169                        .iter()
6170                        .find(|own| own.id == *id)
6171                        .map(|own| Some(own.clone()))
6172                })
6173                .collect()
6174        };
6175        let unread: Vec<NativeId> = ids
6176            .iter()
6177            .zip(&found)
6178            .filter(|(_, found)| found.is_none())
6179            .map(|(id, _)| id.clone())
6180            .collect();
6181        let mut read = Vec::with_capacity(unread.len());
6182        if let [one] = unread.as_slice() {
6183            read.push(self.item_by_id(one).await?);
6184        } else {
6185            for batch in unread.chunks(DETAIL_BATCH) {
6186                let data = match self
6187                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6188                    .await
6189                {
6190                    Ok(data) => data,
6191                    Err(error) if unresolvable_node(&error) => {
6192                        for id in batch {
6193                            read.push(self.item_by_id(id).await?);
6194                        }
6195                        continue;
6196                    }
6197                    Err(error) => return Err(error),
6198                };
6199                for (slot, id) in batch.iter().enumerate() {
6200                    let node =
6201                        data.get(format!("i{slot}"))
6202                            .ok_or_else(|| SourceError::Malformed {
6203                                message: format!(
6204                                    "GitHub answered a batch read with no item for {}",
6205                                    id.0
6206                                ),
6207                            })?;
6208                    read.push(if node.is_null() {
6209                        None
6210                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6211                        self.draft_by_id(id).await?
6212                    } else {
6213                        if optional_str(node, "__typename")? == Some("Issue")
6214                            && required_str(node, "id")? != id.0
6215                        {
6216                            return Err(SourceError::Malformed {
6217                                message: format!(
6218                                    "GitHub answered the read of {} with issue {}",
6219                                    id.0,
6220                                    required_str(node, "id")?
6221                                ),
6222                            });
6223                        }
6224                        self.resolve_issue(node).await?
6225                    });
6226                }
6227            }
6228        }
6229        let mut read = read.into_iter();
6230        Ok(found
6231            .iter_mut()
6232            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6233            .collect())
6234    }
6235
6236    fn read_ahead(
6237        &self,
6238    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6239        self.read_ahead
6240            .lock()
6241            .map_err(|_| SourceError::Unavailable {
6242                message:
6243                    "items read ahead for a copy were left inconsistent; run the command again"
6244                        .into(),
6245            })
6246    }
6247
6248    fn resolved_cache(
6249        &self,
6250    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6251        self.resolved_cache
6252            .lock()
6253            .map_err(|_| SourceError::Unavailable {
6254                message: "resolved item records were left inconsistent; run the command again"
6255                    .into(),
6256            })
6257    }
6258
6259    /// Reuse a record this invocation already resolved. The mutation sender invalidates
6260    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6261    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6262        let cached = self.resolved_cache()?.get(id).cloned();
6263        if let Some(item) = cached {
6264            return Ok(Some(item));
6265        }
6266        self.item_by_id(id).await
6267    }
6268
6269    /// Take out the item an issue read by reference reached for `id`, when one is held: the
6270    /// status write it was read for is the one caller, so no other read or write is ever
6271    /// answered from a read that selected only what that write needs.
6272    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6273        Ok(self
6274            .held_cache
6275            .lock()
6276            .map_err(|_| SourceError::Unavailable {
6277                message: "items read by issue reference were left inconsistent; run the \
6278                          command again"
6279                    .into(),
6280            })?
6281            .remove(id))
6282    }
6283
6284    /// One board draft by its own id, with the board item it sits in — or `None` when no
6285    /// item of this board is that draft's.
6286    ///
6287    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6288    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6289    /// links a draft to one board item, so the page this read carries is the whole of that
6290    /// connection, and a page that reports more than it holds is refused rather than read
6291    /// as an answer about memberships nobody read.
6292    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6293        let data = self
6294            .graphql(
6295                graphql::DRAFT,
6296                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6297                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6298            )
6299            .await?;
6300        // Gone between the two reads is an answer — the draft is no longer there. Anything
6301        // else than the draft [`Self::reach`] was just told this id is, is not one.
6302        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6303            return Ok(None);
6304        };
6305        if optional_str(draft, "__typename")? != Some("DraftIssue") {
6306            return Err(SourceError::Malformed {
6307                message: format!(
6308                    "GitHub answered {} as a draft and then as something else",
6309                    id.0
6310                ),
6311            });
6312        }
6313        if required_str(draft, "id")? != id.0 {
6314            return Err(SourceError::Malformed {
6315                message: format!("GitHub answered a different draft for {}", id.0),
6316            });
6317        }
6318        let memberships = draft
6319            .get("projectV2Items")
6320            .ok_or_else(|| SourceError::Malformed {
6321                message: format!("GitHub draft {} is missing projectV2Items", id.0),
6322            })?;
6323        let nodes = memberships
6324            .get("nodes")
6325            .and_then(Value::as_array)
6326            .ok_or_else(|| SourceError::Malformed {
6327                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6328            })?;
6329        let info = memberships
6330            .get("pageInfo")
6331            .ok_or_else(|| SourceError::Malformed {
6332                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6333            })?;
6334        // Read whether or not this board's entry is on the page: a page claiming more than
6335        // the one item GitHub links a draft to is a malformed answer either way.
6336        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6337            return Err(SourceError::Malformed {
6338                message: format!(
6339                    "GitHub draft {} reports more board items than the one GitHub links a draft \
6340                     to",
6341                    id.0
6342                ),
6343            });
6344        }
6345        if let Some(node) = nodes.first()
6346            && node
6347                .pointer("/project/number")
6348                .and_then(Value::as_u64)
6349                .is_none()
6350        {
6351            return Err(SourceError::Malformed {
6352                message: format!(
6353                    "GitHub draft {} board item has no numeric project number",
6354                    id.0
6355                ),
6356            });
6357        }
6358        let Some(held) = self.board_entry(nodes) else {
6359            return Ok(None);
6360        };
6361        if required_str(
6362            held.get("project").ok_or_else(|| SourceError::Malformed {
6363                message: format!("GitHub draft {} board item has no project", id.0),
6364            })?,
6365            "id",
6366        )? != self.board_fields().await?.id.as_str()
6367        {
6368            return Ok(None);
6369        }
6370        let item = json!({
6371            "id": required_str(held, "id")?,
6372            "project": held.get("project"),
6373            "fieldValues": held.get("fieldValues"),
6374            "content": draft,
6375        });
6376        self.resolve(&item)
6377    }
6378
6379    /// The board's own id and field definitions, for a write whose item does not carry
6380    /// them — never its items.
6381    ///
6382    /// A board this command has already listed supplies them, since it read them beside its
6383    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6384    /// is consulted about which items the board holds: see the module documentation for
6385    /// why a question about one known item is answered by reading that item.
6386    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6387        if let Some(board) = self.board_cache()?.as_ref() {
6388            return Ok(BoardFields {
6389                id: BoardId::parse(&board.id)?,
6390                fields: board.fields.clone(),
6391            });
6392        }
6393        if let Some(held) = self.fields_cache()?.clone() {
6394            return Ok(held);
6395        }
6396        let data = self
6397            .graphql(
6398                graphql::BOARD_FIELDS,
6399                json!({"owner":self.owner,"number":self.project_number,
6400                       "nestedFirst":NESTED_PAGE_SIZE}),
6401            )
6402            .await?;
6403        self.fields_read(&data)
6404    }
6405
6406    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6407    /// the rest of this command.
6408    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6409        let board = data
6410            .pointer("/boardFields/projectV2")
6411            .filter(|value| !value.is_null())
6412            .ok_or_else(|| SourceError::Refused {
6413                message: format!(
6414                    "GitHub project {}/{} was not found or is not visible to the token",
6415                    self.owner, self.project_number
6416                ),
6417            })?;
6418        let read = BoardFields {
6419            id: BoardId::parse(required_str(board, "id")?)?,
6420            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6421        };
6422        *self.fields_cache()? = Some(read.clone());
6423        Ok(read)
6424    }
6425
6426    /// Read what creating an issue in `repository` needs and this command has not read yet —
6427    /// the board's fields and the repository's node id — in one request when it needs both.
6428    ///
6429    /// When either is already known this sends nothing, and the other is read by its own
6430    /// document where it is asked for, so no create reads anything twice.
6431    async fn creation_context(
6432        &self,
6433        repository: &RepositoryTarget,
6434        incoming: &Incoming<'_>,
6435    ) -> Result<(), SourceError> {
6436        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6437        if fields_known || self.repository_cache()?.contains_key(repository) {
6438            return Ok(());
6439        }
6440        // A placement an earlier process kept is taken only while the project issue this create
6441        // has just read shows its setup-context token unchanged; see `projection`.
6442        if self.placed_by_kept(repository, incoming.parent) {
6443            return Ok(());
6444        }
6445        let data = self
6446            .graphql(
6447                graphql::CREATION_CONTEXT,
6448                json!({"owner":self.owner,"number":self.project_number,
6449                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6450                       "repositoryName":repository.name}),
6451            )
6452            .await?;
6453        self.fields_read(&data)?;
6454        self.repository_read(&data, repository, incoming)?;
6455        Ok(())
6456    }
6457
6458    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6459    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6460        self.fields_cache
6461            .lock()
6462            .map_err(|_| SourceError::Unavailable {
6463                message: "this source's view of the board's fields was left inconsistent by an \
6464                      earlier failure; next: run the command again"
6465                    .into(),
6466            })
6467    }
6468
6469    /// What a write to `item` needs of the board, read off that item when it says enough and
6470    /// off [`Self::board_fields`] when it does not.
6471    ///
6472    /// A node read of an item names its board and carries the definition of every field it
6473    /// holds a value of — so an item naming its board, holding a value of the origin field
6474    /// when the write carries an origin, and, when the write carries a status, holding a
6475    /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6476    /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6477    /// as absent would refuse a write the board can take or skip a field write the board
6478    /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6479    /// from their own read instead. A write carrying no origin to an item holding none — every
6480    /// write out of a source declared private — writes no origin whatever the board defines,
6481    /// so it needs no definition of that field.
6482    async fn fields_for(
6483        &self,
6484        item: Option<&Resolved>,
6485        writes_origin: bool,
6486        writes_status: bool,
6487        selects_priority: bool,
6488    ) -> Result<BoardFields, SourceError> {
6489        if let Some(board) = item.and_then(Resolved::carried_board) {
6490            return Ok(board);
6491        }
6492        if let Some(item) = item
6493            && let Some(board_id) = item.named_board()
6494            && (!writes_origin || item.defines(ORIGIN_FIELD))
6495            && (!writes_status || item.defines("Status"))
6496            && (!selects_priority || item.defines(PRIORITY_FIELD))
6497            && self
6498                .metadata_fields
6499                .iter()
6500                .all(|projection| item.defines(&projection.field))
6501        {
6502            return Ok(BoardFields {
6503                id: board_id,
6504                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6505            });
6506        }
6507        self.board_fields().await
6508    }
6509
6510    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6511    /// when that id names nothing here with a sub-issue relationship to walk.
6512    ///
6513    /// `None` and an empty answer are different: `None` is *this is not an issue of this
6514    /// GitHub*, which is what sends a project selector on to be read as a name, and an
6515    /// empty vector is a project that holds nothing.
6516    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6517        let mut after: Option<String> = None;
6518        let mut children = Vec::new();
6519        // The project issue as the first page carried it, held once the walk has ended.
6520        let mut parent: Option<Resolved> = None;
6521        loop {
6522            let asked = self
6523                .graphql(
6524                    graphql::SUB_ISSUES,
6525                    json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6526                           "nestedFirst":NESTED_PAGE_SIZE,
6527                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6528                )
6529                .await;
6530            let data = match asked {
6531                Ok(data) => data,
6532                // A string that is not a node id at all is not a failure to report: it is
6533                // the ordinary answer to a selector naming a project by its name.
6534                Err(error) if unresolvable_node(&error) => return Ok(None),
6535                Err(error) => return Err(error),
6536            };
6537            let Some(connection) = data
6538                .pointer("/node/subIssues")
6539                .filter(|value| !value.is_null())
6540            else {
6541                // No such node, or one with no sub-issue relationship — a board draft is
6542                // the one this board can really hold.
6543                return Ok(None);
6544            };
6545            if after.is_none()
6546                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6547            {
6548                parent = self
6549                    .resolve_issue(node)
6550                    .await?
6551                    .filter(|resolved| resolved.id == *id);
6552            }
6553            for node in connection
6554                .get("nodes")
6555                .and_then(Value::as_array)
6556                .ok_or_else(|| SourceError::Malformed {
6557                    message: "GitHub subIssues.nodes is not an array".into(),
6558                })?
6559            {
6560                if let Some(resolved) = self.resolve_issue(node).await? {
6561                    children.push(resolved);
6562                }
6563            }
6564            let info = connection
6565                .get("pageInfo")
6566                .ok_or_else(|| SourceError::Malformed {
6567                    message: "GitHub subIssues connection has no pageInfo".into(),
6568                })?;
6569            let next = required_bool(info, "hasNextPage")?
6570                .then(|| required_str(info, "endCursor"))
6571                .transpose()?;
6572            match next {
6573                Some(next) => {
6574                    validate_cursor_progress(after.as_deref(), next)?;
6575                    after = Some(next.to_owned());
6576                }
6577                None => {
6578                    // Held only once the walk has ended, so one failing part way holds
6579                    // nothing and every read it would have answered is made.
6580                    // The project issue the first page carried answers the next read of the
6581                    // project by its id in this command, as a read of it made now would: what
6582                    // classifying a project for a write reads straight after its members.
6583                    if let Some(parent) = &parent {
6584                        self.read_ahead()?
6585                            .entry(parent.id.clone())
6586                            .or_insert_with(|| Some(parent.clone()));
6587                    }
6588                    let mut carried = self.carried()?;
6589                    for item in parent.into_iter().chain(children.iter().cloned()) {
6590                        carried.insert(item.id.clone(), item);
6591                    }
6592                    return Ok(Some(children));
6593                }
6594            }
6595        }
6596    }
6597
6598    fn carried(
6599        &self,
6600    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6601        self.carried.lock().map_err(|_| SourceError::Unavailable {
6602            message: "items a project's listing carried were left inconsistent; run the \
6603                      command again"
6604                .into(),
6605        })
6606    }
6607
6608    /// Which issue of this board a project *name* is, or `None` when none is.
6609    ///
6610    /// One bounded query which filters on that name at the server, rather than a walk of
6611    /// every issue the board holds. The name is compared again here: the qualifier narrows
6612    /// what GitHub sends, and this source decides what it names.
6613    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6614        let search = self.board_search(Some(&title_qualifier(name)));
6615        let mut after = None;
6616        loop {
6617            let (candidates, next) = self
6618                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6619                .await?;
6620            if let Some(item) = candidates.into_iter().find(|item| {
6621                item.kind == BoardKind::Work(ItemKind::Project)
6622                    && item.title.eq_ignore_ascii_case(name)
6623            }) {
6624                return Ok(Some(item.id));
6625            }
6626            match next {
6627                Some(next) => after = Some(next),
6628                None => return Ok(None),
6629            }
6630        }
6631    }
6632
6633    /// Everything filed under one project of this board: the sub-issues of the issue that
6634    /// project is.
6635    ///
6636    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6637    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6638    /// gains projects, or as another project gains tasks.
6639    ///
6640    /// A qualified id names the issue and is asked for its sub-issues directly: one
6641    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6642    /// read as a project *name*, which costs the one bounded search
6643    /// [`Self::project_by_name`] makes.
6644    ///
6645    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6646    /// — and each answer is still cut to the items whose parent is this project, so one this
6647    /// process has since filed elsewhere is not reported here, and completed with what this
6648    /// process filed under it.
6649    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6650        let held = self.children_cache()?.get(selector).cloned();
6651        let (project, children) = match held {
6652            Some(held) => held,
6653            None => {
6654                let answered = match self.sub_issues(selector).await? {
6655                    Some(children) => (selector.clone(), children),
6656                    None => match self.project_by_name(&selector.0).await? {
6657                        Some(project) => {
6658                            let children = self.sub_issues(&project).await?.unwrap_or_default();
6659                            (project, children)
6660                        }
6661                        None => return Ok(Vec::new()),
6662                    },
6663                };
6664                self.children_cache()?
6665                    .insert(selector.clone(), answered.clone());
6666                answered
6667            }
6668        };
6669        let mut children: Vec<Resolved> = children
6670            .into_iter()
6671            .filter(|child| child.parent.as_ref() == Some(&project))
6672            .collect();
6673        for own in self.updated()?.iter() {
6674            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6675                children.push(own.clone());
6676            }
6677        }
6678        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6679    }
6680
6681    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6682    /// completed with what this run wrote — the candidates a comment-activity read confirms.
6683    ///
6684    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6685    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6686    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6687    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6688    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6689    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6690    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6691    /// rather than silently narrowing a caller's answer.
6692    ///
6693    /// The instant is written to the second, rounded down, which can only widen what the
6694    /// search returns; confirmation against each candidate's own comments is what makes the
6695    /// answer exact. The search is an index that lags a write by a second or two — the module
6696    /// documentation records it — so a caller that asks again from its last instant should
6697    /// overlap the two by more than that.
6698    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6699        let found = self.searched(&updated_qualifier(since)).await?;
6700        self.completed_with_written(found, |_| true)
6701    }
6702
6703    /// Every issue of this board GitHub's issue search reports for the board-scoped search
6704    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6705    ///
6706    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6707    /// own record is the fresher of the two.
6708    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6709        let search = self.board_search(Some(also));
6710        let mut after: Option<String> = None;
6711        let mut found = Vec::new();
6712        loop {
6713            let (page, next) = self
6714                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6715                .await?;
6716            found.extend(page);
6717            match next {
6718                Some(next) => after = Some(next),
6719                None => return Ok(found),
6720            }
6721        }
6722    }
6723
6724    /// A bounded task answer; the versioned cursor carries the connection position, how
6725    /// many rows of the page starting there were already handed out, and the own-write ids
6726    /// already observed, including across a new source instance.
6727    ///
6728    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6729    /// sliced from the pages it needs; why is the module documentation's paging contract.
6730    async fn search_tasks(
6731        &self,
6732        query: &TaskQuery,
6733        page: &PageRequest,
6734        also: &str,
6735    ) -> Result<Page<Task>, SourceError> {
6736        let mut position = match &page.cursor {
6737            None => SearchPosition::default(),
6738            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6739                .ok()
6740                .filter(|position| {
6741                    position.version == SEARCH_CURSOR_VERSION
6742                        && position.connection.valid_resume(position.offset)
6743                })
6744                .ok_or_else(|| SourceError::Config {
6745                    message: "page cursor is invalid".into(),
6746                })?,
6747        };
6748        let search = self.board_search(Some(also));
6749        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6750        let own = self.with_own_writes(Vec::new())?;
6751        // An issue this process commented on is a candidate of a comment-activity read
6752        // whether or not the search has caught up with the comment; see `Self::commented`.
6753        let commented = match query.commented_since {
6754            Some(_) => self.commented()?.clone(),
6755            None => Vec::new(),
6756        };
6757        for id in own.iter().map(|item| &item.id).chain(&commented) {
6758            if !position.own.contains(id) {
6759                position.own.push(id.clone());
6760            }
6761        }
6762        let mut tasks = Vec::new();
6763        while !position.connection.exhausted() && tasks.len() < limit {
6764            let first = SEARCH_PAGE_SIZE;
6765            // Page size is part of the key: a short cached answer cannot answer a wider ask.
6766            let key =
6767                serde_json::to_string(&("page", &search, &position.connection.after(), first))
6768                    .expect("search page key is serializable");
6769            let cached = if query.commented_since.is_none() {
6770                self.narrowed_cache()?.get(&key).cloned()
6771            } else {
6772                None
6773            };
6774            let (found, next) = match cached {
6775                Some(found) => {
6776                    let next = self
6777                        .search_next
6778                        .lock()
6779                        .map_err(|_| SourceError::Unavailable {
6780                            message:
6781                                "search pagination was left inconsistent; run the command again"
6782                                    .into(),
6783                        })?
6784                        .get(&key)
6785                        .cloned()
6786                        .flatten();
6787                    (found, next)
6788                }
6789                None => {
6790                    let (found, next) = self
6791                        .search_page(&search, first, position.connection.after())
6792                        .await?;
6793                    if query.commented_since.is_none() {
6794                        self.search_next
6795                            .lock()
6796                            .map_err(|_| SourceError::Unavailable {
6797                                message:
6798                                    "search pagination was left inconsistent; run the command again"
6799                                        .into(),
6800                            })?
6801                            .insert(key.clone(), next.clone());
6802                        self.narrowed_cache()?.insert(key, found.clone());
6803                    }
6804                    (found, next)
6805                }
6806            };
6807            let rows = found.len();
6808            for mut item in found.into_iter().skip(position.offset) {
6809                if tasks.len() == limit {
6810                    break;
6811                }
6812                position.offset += 1;
6813                if position.own.contains(&item.id) {
6814                    if position.seen.contains(&item.id) {
6815                        continue;
6816                    }
6817                    position.seen.push(item.id.clone());
6818                    // The search's own copy of an issue this process only commented on is as
6819                    // good as a node read of it, since its comments are read either way.
6820                    let only_commented = commented.contains(&item.id)
6821                        && !own.iter().any(|written| written.id == item.id);
6822                    if !only_commented {
6823                        let updated_at = item.updated_at;
6824                        let Some(written) = self.search_written(&own, &item.id).await? else {
6825                            continue;
6826                        };
6827                        item = written;
6828                        item.updated_at = item.updated_at.max(updated_at);
6829                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6830                    }
6831                }
6832                if item.kind == BoardKind::Work(ItemKind::Task) {
6833                    let task = item.task()?;
6834                    if task_matches(&task, query, &query.project)
6835                        && self.commented_since(&item, query.commented_since).await?
6836                    {
6837                        tasks.push(task);
6838                    }
6839                }
6840            }
6841            if position.offset < rows {
6842                continue;
6843            }
6844            position.offset = 0;
6845            position.connection = match next {
6846                Some(after) => SearchConnection::Continuing {
6847                    after: Cursor(after),
6848                },
6849                None => SearchConnection::Exhausted {},
6850            };
6851        }
6852        if position.connection.exhausted() {
6853            for id in position.own.clone() {
6854                if position.seen.contains(&id) {
6855                    continue;
6856                }
6857                if tasks.len() == limit {
6858                    break;
6859                }
6860                position.seen.push(id.clone());
6861                let Some(item) = self.search_written(&own, &id).await? else {
6862                    continue;
6863                };
6864                if item.kind == BoardKind::Work(ItemKind::Task) {
6865                    let task = item.task()?;
6866                    if task_matches(&task, query, &query.project)
6867                        && self.commented_since(&item, query.commented_since).await?
6868                    {
6869                        tasks.push(task);
6870                    }
6871                }
6872            }
6873        }
6874        let more = !position.connection.exhausted()
6875            || position.own.iter().any(|id| !position.seen.contains(id));
6876        Ok(Page {
6877            items: tasks,
6878            next: more.then(|| {
6879                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6880            }),
6881        })
6882    }
6883
6884    /// A resumed process has the ids but no write records; resolve only a record the
6885    /// current page needs, by its uncached node read rather than the lagging search index.
6886    async fn search_written(
6887        &self,
6888        own: &[Resolved],
6889        id: &NativeId,
6890    ) -> Result<Option<Resolved>, SourceError> {
6891        match own.iter().find(|item| item.id == *id) {
6892            Some(item) => Ok(Some(item.clone())),
6893            None => self.item_by_id(id).await,
6894        }
6895    }
6896
6897    /// The candidates for a task query carrying a text, metadata or origin predicate, read
6898    /// without enumerating the board — or `None` for a query carrying none of the three, which
6899    /// keeps the reads it always had.
6900    ///
6901    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6902    /// because it names at most a handful of items. Text and metadata are answered by one
6903    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6904    /// further by `updated:>=` when the query also asks for comment activity, since both
6905    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6906    /// process afterwards by the same predicates [`task_matches`] applies to every read.
6907    ///
6908    /// Completed with what this process wrote, its own record winning over the index's copy
6909    /// of the same item: see [`Self::with_own_writes`].
6910    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6911        self.send_pending_origins(None).await?;
6912        let asked = match (&query.origin, narrowing_qualifiers(query)) {
6913            (Some(origin), _) => Narrowing::Origin(origin.clone()),
6914            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6915                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6916                None => qualifiers,
6917            }),
6918            (None, None) => return Ok(None),
6919        };
6920        // A question about comment activity is asked afresh every time, as it always was: it
6921        // is the one a caller polls from one source while waiting for the index, and an
6922        // answer held from the first poll would be the answer to every later one.
6923        let key = query.commented_since.is_none().then(|| asked.key());
6924        let cached = match &key {
6925            Some(key) => self.narrowed_cache()?.get(key).cloned(),
6926            None => None,
6927        };
6928        let found = match cached {
6929            Some(found) => found,
6930            None => {
6931                let found = match &asked {
6932                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6933                    Narrowing::Search(also) => self.searched(also).await?,
6934                };
6935                if let Some(key) = key {
6936                    self.narrowed_cache()?.insert(key, found.clone());
6937                }
6938                found
6939            }
6940        };
6941        self.with_own_writes(found).map(Some)
6942    }
6943
6944    /// The candidates for a project or unscoped document query carrying a searchable text,
6945    /// read without enumerating the board — or `None` for a query with no text or a blank one,
6946    /// which keeps the read it always had.
6947    ///
6948    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6949    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6950    /// costs is the issues that match and never the board. Its answer is held for the command
6951    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6952    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6953    /// substring rule, exactly as an item of the wider read was, and is completed with what this
6954    /// process wrote: see [`Self::with_own_writes`].
6955    async fn text_searched(
6956        &self,
6957        text: Option<&TextQuery>,
6958    ) -> Result<Option<Vec<Resolved>>, SourceError> {
6959        let Some(also) = text_qualifiers(text) else {
6960            return Ok(None);
6961        };
6962        let key = Narrowing::Search(also.clone()).key();
6963        let cached = self.narrowed_cache()?.get(&key).cloned();
6964        let found = match cached {
6965            Some(found) => found,
6966            None => {
6967                let found = self.searched(&also).await?;
6968                self.narrowed_cache()?.insert(key, found.clone());
6969                found
6970            }
6971        };
6972        self.with_own_writes(found).map(Some)
6973    }
6974
6975    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6976    /// [`Self::narrowed`] reads for a task query carrying that origin — one
6977    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6978    /// what this process wrote — each confirmed against its own origin entry.
6979    ///
6980    /// What answers a project or document listing by origin, so finding every copy of one plan
6981    /// costs the items that carry its origin and never a page of the board.
6982    async fn copied_from(
6983        &self,
6984        origin: &str,
6985        kind: BoardKind,
6986    ) -> Result<Vec<Resolved>, SourceError> {
6987        let wanted = TaskQuery {
6988            origin: Some(origin.to_owned()),
6989            ..TaskQuery::default()
6990        };
6991        Ok(self
6992            .narrowed(&wanted)
6993            .await?
6994            .unwrap_or_default()
6995            .into_iter()
6996            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6997            .collect())
6998    }
6999
7000    /// Every item of this board that may carry `origin` — a superset of those that do — found
7001    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
7002    ///
7003    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
7004    /// which reads the field every carrier holds, whichever release wrote it — and the
7005    /// board-scoped issue search for the same id as a phrase in the body, where this source
7006    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
7007    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
7008    /// query's, exactly.
7009    ///
7010    /// Both connections are walked to exhaustion, each from its own cursor. One that has
7011    /// already ended is sent its last cursor again, which answers an empty page, so the one
7012    /// document serves every page of either. What the two leave is stated in the module
7013    /// documentation: a carrier another process added within the last second or two, before
7014    /// either index has it.
7015    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
7016        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
7017        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
7018        let mut items_after: Option<String> = None;
7019        let mut search_after: Option<String> = None;
7020        let mut found: Vec<Resolved> = Vec::new();
7021        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
7022            if !found.iter().any(|held| held.id == resolved.id) {
7023                found.push(resolved);
7024            }
7025        };
7026        loop {
7027            let data = self
7028                .graphql(
7029                    graphql::ORIGIN_LOOKUP,
7030                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
7031                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
7032                           "itemsAfter":items_after,"searchAfter":search_after,
7033                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7034                           "duplicates":true}),
7035                )
7036                .await?;
7037            let items = data
7038                .pointer("/originItems/projectV2/items")
7039                .filter(|value| !value.is_null())
7040                .ok_or_else(|| SourceError::Refused {
7041                    message: format!(
7042                        "GitHub project {}/{} was not found or is not visible to the token",
7043                        self.owner, self.project_number
7044                    ),
7045                })?;
7046            for item in optional_nodes(Some(items), "project items")?
7047                .into_iter()
7048                .flatten()
7049            {
7050                // The board's own items list its drafts too, and a draft is not an issue: no
7051                // narrowed read answers with one, whatever its origin field holds.
7052                if let Some(resolved) = self.resolve(item)?
7053                    && resolved.content_kind == ContentKind::Issue
7054                {
7055                    keep(resolved, &mut found);
7056                }
7057            }
7058            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
7059                message: "GitHub search response has no search connection".into(),
7060            })?;
7061            for node in optional_nodes(Some(searched), "search")?
7062                .into_iter()
7063                .flatten()
7064            {
7065                if let Some(resolved) = self.resolve_issue(node).await? {
7066                    keep(resolved, &mut found);
7067                }
7068            }
7069            let items_next = resumed(items, items_after.as_deref())?;
7070            let search_next = resumed(searched, search_after.as_deref())?;
7071            if !items_next.has_more() && !search_next.has_more() {
7072                return Ok(found);
7073            }
7074            items_after = items_next.cursor();
7075            search_after = search_next.cursor();
7076        }
7077    }
7078
7079    /// `found`, with every item this process created or wrote in its place, and every one of
7080    /// them the read did not report added.
7081    ///
7082    /// This process's own record wins over the read's copy of the same item, because a read
7083    /// of an item written moments ago can still be behind what was written onto it — the
7084    /// origin field included, which is the one a narrowed read is confirmed against — and a
7085    /// read that still names an item under a predicate this process's write moved it out of
7086    /// must not return it. The one thing the read knows that the record cannot is when GitHub
7087    /// last saw the item change, which is what a comment-activity read rules a candidate out
7088    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
7089    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
7090    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
7091        // A board draft is not an issue, so no narrowed read returns one, and this process
7092        // having written one does not make it an answer either.
7093        let own: Vec<Resolved> = self
7094            .created()?
7095            .iter()
7096            .chain(self.updated()?.iter())
7097            .filter(|own| own.content_kind == ContentKind::Issue)
7098            .cloned()
7099            .collect();
7100        for mut own in own {
7101            self.resolved_cache()?.insert(own.id.clone(), own.clone());
7102            match found.iter_mut().find(|read| read.id == own.id) {
7103                Some(read) => {
7104                    own.updated_at = own.updated_at.max(read.updated_at);
7105                    *read = own;
7106                }
7107                None => found.push(own),
7108            }
7109        }
7110        Ok(found)
7111    }
7112
7113    /// Whether `item` has a comment created or last edited at or after `since` — always, when
7114    /// there is no instant to hold it to.
7115    ///
7116    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
7117    /// or after the instant moved it there: an issue not updated since holds no such comment,
7118    /// and its comments are never asked for — unless this process commented on it in this
7119    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
7120    /// only as far as the first that matches. A board draft is not an issue and has no
7121    /// comments, so it never matches.
7122    async fn commented_since(
7123        &self,
7124        item: &Resolved,
7125        since: Option<DateTime<Utc>>,
7126    ) -> Result<bool, SourceError> {
7127        let Some(since) = since else {
7128            return Ok(true);
7129        };
7130        if item.content_kind == ContentKind::DraftIssue {
7131            return Ok(false);
7132        }
7133        // An `updatedAt` this process's own record or a lagging index holds can predate a
7134        // comment this process wrote since, so only an issue it did not comment on is ruled
7135        // out by one.
7136        if item.updated_at.is_some_and(|updated| updated < since)
7137            && !self.commented()?.contains(&item.id)
7138        {
7139            return Ok(false);
7140        }
7141        let query = TaskQuery {
7142            commented_since: Some(since),
7143            ..TaskQuery::default()
7144        };
7145        let mut after: Option<String> = None;
7146        loop {
7147            let data = self
7148                .graphql(
7149                    graphql::ISSUE_COMMENTS,
7150                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7151                )
7152                .await?;
7153            let Some(connection) = data
7154                .get("node")
7155                .filter(|value| !value.is_null())
7156                .and_then(|node| node.get("comments"))
7157                .filter(|value| !value.is_null())
7158            else {
7159                // Removed since the search reported it: no longer an issue with comments.
7160                return Ok(false);
7161            };
7162            let comments = optional_nodes(Some(connection), "issue comments")?
7163                .into_iter()
7164                .flatten()
7165                .map(comment_from)
7166                .collect::<Result<Vec<_>, _>>()?;
7167            if query.comments_match(&comments) {
7168                return Ok(true);
7169            }
7170            match next_cursor(connection)? {
7171                Some(next) => {
7172                    validate_cursor_progress(after.as_deref(), &next.0)?;
7173                    after = Some(next.0);
7174                }
7175                None => return Ok(false),
7176            }
7177        }
7178    }
7179
7180    /// Every item on the board: the union of both enumerations GitHub offers of one.
7181    ///
7182    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7183    /// board **draft** and reads the board's own fields beside its items, and only the search
7184    /// reports an item that connection is behind on. The module documentation is where the lag and the
7185    /// measurements behind it are written down.
7186    ///
7187    /// A search result is admitted on the same terms as any other issue this source reaches
7188    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7189    /// names *this* board — so an issue the index still believes is here after it was taken
7190    /// off is refused rather than reported.
7191    ///
7192    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7193    /// which is what the cache could otherwise have broken.
7194    async fn board(&self) -> Result<Board, SourceError> {
7195        self.board_completed_by(None).await
7196    }
7197
7198    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7199    /// that half could add is then answered only if it holds.
7200    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7201        let cached = self.board_cache()?.clone();
7202        let mut board = match cached {
7203            Some(board) => board,
7204            None => {
7205                let read = self.read_board().await?;
7206                *self.board_cache()? = Some(read.clone());
7207                read
7208            }
7209        };
7210        let searched = match also {
7211            None => self.searched_issues().await?,
7212            Some(also) => self.searched_narrowly(also).await?,
7213        };
7214        for held in searched {
7215            if !board.items.iter().any(|item| item.id == held.id) {
7216                board.items.push(held);
7217            }
7218        }
7219        for own in self.created()?.iter() {
7220            if !board.items.iter().any(|item| item.id == own.id) {
7221                board.items.push(own.clone());
7222            }
7223        }
7224        Ok(board)
7225    }
7226
7227    /// This process's own view of the board, or the refusal a poisoned lock is.
7228    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7229        self.board_cache
7230            .lock()
7231            .map_err(|_| SourceError::Unavailable {
7232                message: "this source's view of the board was left inconsistent by an earlier \
7233                      failure; next: run the command again"
7234                    .into(),
7235            })
7236    }
7237
7238    /// Bring this process's own view of the board up to an item it has just written.
7239    ///
7240    /// A created item goes to `created`, which is what completes a board read GitHub's own
7241    /// eventual consistency has left behind. An item that was already there is replaced
7242    /// where it sits, so a second write of it in the same command reads its real parent
7243    /// rather than the one it had before the first write.
7244    ///
7245    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7246    /// that wins: an item this same run created is held in `created` and not in the cached
7247    /// board, and `board` completes the cached board *from* `created`, so replacing only
7248    /// the cached copy of such an item replaces nothing and the read still reports the
7249    /// title it was created with. The search is the third, and it is the one an item the
7250    /// board's own projection is behind on sits in *alone* — which is exactly the item this
7251    /// source is least able to re-read, so leaving it out would put the stale title back on
7252    /// the only items the completion in [`Self::board`] exists for.
7253    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7254        self.resolved_cache()?.insert(item.id.clone(), item.clone());
7255        if created {
7256            self.created()?.push(item);
7257            return Ok(());
7258        }
7259        {
7260            let mut own = self.created()?;
7261            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7262                *held = item;
7263                return Ok(());
7264            }
7265        }
7266        {
7267            let mut own = self.updated()?;
7268            match own.iter_mut().find(|held| held.id == item.id) {
7269                Some(held) => *held = item.clone(),
7270                None => own.push(item.clone()),
7271            }
7272        }
7273        if let Some(board) = self.board_cache()?.as_mut()
7274            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7275        {
7276            *held = item.clone();
7277        }
7278        if let Some(found) = self.search_cache()?.as_mut()
7279            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7280        {
7281            *held = item.clone();
7282        }
7283        for found in self.narrowed_cache()?.values_mut() {
7284            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7285                *held = item.clone();
7286            }
7287        }
7288        for (_, found) in self.children_cache()?.values_mut() {
7289            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7290                *held = item.clone();
7291            }
7292        }
7293        Ok(())
7294    }
7295
7296    /// Forget one item this process has just deleted, from every half of its own view.
7297    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7298        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7299            created.retain(|held| held.content != *id);
7300        }
7301        self.resolved_cache()?.remove(id);
7302        self.read_ahead()?.remove(id);
7303        self.carried()?.remove(id);
7304        self.created()?.retain(|own| own.id != *id);
7305        self.updated()?.retain(|own| own.id != *id);
7306        self.commented()?.retain(|own| own != id);
7307        if let Some(board) = self.board_cache()?.as_mut() {
7308            board.items.retain(|item| item.id != *id);
7309        }
7310        if let Some(found) = self.search_cache()?.as_mut() {
7311            found.retain(|item| item.id != *id);
7312        }
7313        for found in self.narrowed_cache()?.values_mut() {
7314            found.retain(|item| item.id != *id);
7315        }
7316        for (_, found) in self.children_cache()?.values_mut() {
7317            found.retain(|item| item.id != *id);
7318        }
7319        Ok(())
7320    }
7321
7322    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7323    fn narrowed_cache(
7324        &self,
7325    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7326        self.narrowed_cache
7327            .lock()
7328            .map_err(|_| SourceError::Unavailable {
7329                message: "this source's view of a narrowed read was left inconsistent by an \
7330                      earlier failure; next: run the command again"
7331                    .into(),
7332            })
7333    }
7334
7335    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7336    /// is.
7337    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7338    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7339        Ok(self.task_children()?.get(project).cloned())
7340    }
7341
7342    fn task_children(
7343        &self,
7344    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7345        self.task_children
7346            .lock()
7347            .map_err(|_| SourceError::Unavailable {
7348                message: "this source's record of a project's tasks was left inconsistent by an \
7349                          earlier failure; next: run the command again"
7350                    .into(),
7351            })
7352    }
7353
7354    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7355        self.children_cache
7356            .lock()
7357            .map_err(|_| SourceError::Unavailable {
7358                message: "this source's view of a project's tasks was left inconsistent by an \
7359                      earlier failure; next: run the command again"
7360                    .into(),
7361            })
7362    }
7363
7364    /// Every page of the board, read from GitHub.
7365    async fn read_board(&self) -> Result<Board, SourceError> {
7366        let mut after: Option<String> = None;
7367        let mut items = Vec::new();
7368        let mut board;
7369        loop {
7370            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7371            for item in page
7372                .pointer("/items/nodes")
7373                .and_then(Value::as_array)
7374                .ok_or_else(|| SourceError::Malformed {
7375                    message: "GitHub project items.nodes is not an array".into(),
7376                })?
7377            {
7378                if let Some(resolved) = self.resolve(item)? {
7379                    items.push(resolved);
7380                }
7381            }
7382            let info = page
7383                .pointer("/items/pageInfo")
7384                .ok_or_else(|| SourceError::Malformed {
7385                    message: "GitHub project items have no pageInfo".into(),
7386                })?;
7387            let has_next = required_bool(info, "hasNextPage")?;
7388            let next = has_next
7389                .then(|| required_str(info, "endCursor"))
7390                .transpose()?;
7391            board = page.clone();
7392            match next {
7393                Some(next) => {
7394                    validate_cursor_progress(after.as_deref(), next)?;
7395                    after = Some(next.to_owned());
7396                }
7397                None => break,
7398            }
7399        }
7400        Ok(Board {
7401            id: required_str(&board, "id")?.to_owned(),
7402            fields: board.get("fields").cloned().unwrap_or(Value::Null),
7403            items,
7404        })
7405    }
7406
7407    /// The existing items this source has written, for completing a narrowed read that is
7408    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7409    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7410        self.updated.lock().map_err(|_| SourceError::Unavailable {
7411            message: "this source's record of what it wrote in this run was left inconsistent \
7412                      by an earlier failure; next: run the command again"
7413                .into(),
7414        })
7415    }
7416
7417    /// The issues this source has commented on in this command; see
7418    /// [`Self::commented`](GitHubProjectsSource::commented).
7419    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7420        self.commented.lock().map_err(|_| SourceError::Unavailable {
7421            message: "this source's record of what it commented on in this run was left \
7422                      inconsistent by an earlier failure; next: run the command again"
7423                .into(),
7424        })
7425    }
7426
7427    /// Called only once GitHub has answered the comment write, so an issue whose comment
7428    /// failed is never made a candidate a later read would pay a node read for.
7429    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7430        let mut commented = self.commented()?;
7431        if !commented.contains(issue) {
7432            commented.push(issue.clone());
7433        }
7434        Ok(())
7435    }
7436
7437    /// The items this source has created, for completing a board read that is behind.
7438    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7439        self.created.lock().map_err(|_| SourceError::Unavailable {
7440            message: "this source's record of what it created in this run was left \
7441                      inconsistent by an earlier failure; next: run the command again"
7442                .into(),
7443        })
7444    }
7445
7446    /// One board item as this source reports it, or `None` for content it ignores.
7447    ///
7448    /// A pull request is neither a project nor a task — it is somebody's change, not a
7449    /// unit of plan — and an item whose content the token cannot see has nothing to
7450    /// report at all.
7451    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7452        let resolved = self.resolve_unremembered(item)?;
7453        if let Some(resolved) = &resolved {
7454            self.resolved_cache()?
7455                .insert(resolved.id.clone(), resolved.clone());
7456        }
7457        Ok(resolved)
7458    }
7459
7460    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7461    /// write's own response needs, which says nothing this process should answer a later read
7462    /// with.
7463    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7464        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7465            message: "GitHub project item is missing content".into(),
7466        })?;
7467        if content.is_null() {
7468            return Ok(None);
7469        }
7470        let content_kind = match required_str(content, "__typename")? {
7471            "Issue" => ContentKind::Issue,
7472            "DraftIssue" => ContentKind::DraftIssue,
7473            _ => return Ok(None),
7474        };
7475        let field_values = item
7476            .get("fieldValues")
7477            .ok_or_else(|| SourceError::Malformed {
7478                message: "GitHub project item is missing fieldValues".into(),
7479            })?;
7480        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7481        let nodes = field_values
7482            .get("nodes")
7483            .and_then(Value::as_array)
7484            .ok_or_else(|| SourceError::Malformed {
7485                message: "GitHub project item fieldValues.nodes is not an array".into(),
7486            })?;
7487        if let Some(labels) = content.get("labels") {
7488            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7489        }
7490        let raw_body = optional_str(content, "body")?.map(str::to_owned);
7491        let (body, slot) = metadata_body(raw_body.clone())?;
7492        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7493            .map(|id| NativeId(id.to_owned()));
7494        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7495        // to read one from; it is a task, and never a project.
7496        let sub_issues = match content_kind {
7497            ContentKind::Issue => sub_issue_total(content)?,
7498            ContentKind::DraftIssue => 0,
7499        };
7500        let content_id = required_str(content, "id")?;
7501        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7502            message: format!("GitHub issue {content_id}: {message}"),
7503        })?;
7504        let raw_title = required_str(content, "title")?;
7505        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7506        // The title a person wrote, which for a document is the one without the prefix —
7507        // the same way `content` above is the body without this source's metadata slot.
7508        let title = match kind {
7509            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7510            BoardKind::Work(_) => raw_title.to_owned(),
7511        };
7512        let own_repository = content
7513            .pointer("/repository/nameWithOwner")
7514            .and_then(Value::as_str)
7515            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7516            .transpose()
7517            .map_err(|message| SourceError::Malformed { message })?;
7518        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7519            Repository::from_metadata(&slot)
7520                .map_err(|message| SourceError::Malformed { message })?
7521        } else {
7522            own_repository.clone().into_iter().collect()
7523        };
7524        let classification = Classification::from_metadata(&slot)
7525            .map_err(|message| SourceError::Malformed { message })?;
7526        let id = NativeId(content_id.to_owned());
7527        // Read only for a task, because only a task has either list: a project or a
7528        // document holding one of these keys holds nothing this source reports, and the
7529        // keys are left out of its caller-visible metadata all the same.
7530        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7531            let listed = |key: &str| {
7532                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7533                    .map_err(|message| SourceError::Malformed { message })
7534            };
7535            (
7536                listed(TaskRef::DELIVERS_KEY)?,
7537                listed(TaskRef::DELIVERED_BY_KEY)?,
7538            )
7539        } else {
7540            (Vec::new(), Vec::new())
7541        };
7542        let (option, closed, reason) = Self::status_parts(nodes, content)?;
7543        let priority = self.held_priority(nodes)?;
7544        let item_labels = labels(content)?;
7545        // Present when the item was reached through its own issue, whose board entry
7546        // names the board; a read of the board's own items has the board already. An
7547        // empty id names nothing a field write could address, so it is read as absent and
7548        // the write goes back to reading the board.
7549        let board_id = item
7550            .pointer("/project/id")
7551            .and_then(Value::as_str)
7552            .filter(|id| !id.is_empty());
7553        let resolved = Resolved {
7554            item_id: required_str(item, "id")?.to_owned(),
7555            id,
7556            content_kind,
7557            kind,
7558            title,
7559            body: body.filter(|value| !value.is_empty()),
7560            raw_body,
7561            status: self
7562                .statuses
7563                .status(kind.status_kind(), option, closed, reason),
7564            option: option.map(str::to_owned),
7565            priority,
7566            closed,
7567            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7568            delivers,
7569            delivered_by,
7570            labels: item_labels.clone(),
7571            parent,
7572            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7573            projected: self.held_projections(nodes)?,
7574            number: match content_kind {
7575                ContentKind::Issue => Some(issue_number(content)?),
7576                // A draft is filed in no repository, so nothing ever numbered it:
7577                // `DraftIssue` declares no `number` at all, exactly as it declares no
7578                // `subIssuesSummary` the branch above reads.
7579                ContentKind::DraftIssue => None,
7580            },
7581            url: optional_str(content, "url")?.map(str::to_owned),
7582            created_at: optional_time(content, "createdAt")?,
7583            updated_at: optional_time(content, "updatedAt")?,
7584            item_updated_at: optional_time(item, "updatedAt")?,
7585            comment_count: content
7586                .pointer("/commentCount/totalCount")
7587                .and_then(Value::as_u64),
7588            relationship_events: content
7589                .pointer("/relationshipEvents/totalCount")
7590                .and_then(Value::as_u64),
7591            // The issue node is stamped wherever it was read; a board item a read of the issue
7592            // put together from its memberships is not.
7593            served_at: match optional_time(content, SERVED_AT)? {
7594                Some(served) => Some(served),
7595                None => optional_time(item, SERVED_AT)?,
7596            },
7597            // Both connections were read to their end above — one reporting a further page is
7598            // refused — so these are every field value and every label the item holds. A
7599            // response that did not select the labels at all says nothing of them, and gives
7600            // no facets.
7601            facets: content.get("labels").is_some().then(|| {
7602                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7603            }),
7604            own_repository,
7605            repositories,
7606            classification,
7607            slot,
7608            board_id: board_id.map(str::to_owned),
7609            fields: field_definitions(nodes),
7610            board_fields: Self::carried_board_fields(content, board_id)?,
7611            blocked_by: carried_blocked_by(content)?,
7612        };
7613        Ok(Some(resolved))
7614    }
7615
7616    /// The field definitions of the board `board_id` names — the project this issue's own
7617    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7618    /// `None` when the read carried none, carried no entry for that board, or the board item
7619    /// named no board, which a write then answers by reading the board's fields itself.
7620    ///
7621    /// Matched by the board's node id and never by its number alone: a project number is
7622    /// unique only within its owner, so another owner's board numbered alike can sit on the
7623    /// same page, and its field and option ids address nothing on this one.
7624    fn carried_board_fields(
7625        content: &Value,
7626        board_id: Option<&str>,
7627    ) -> Result<Option<Value>, SourceError> {
7628        let (Some(nodes), Some(board_id)) = (
7629            content.pointer("/boards/nodes").and_then(Value::as_array),
7630            board_id,
7631        ) else {
7632            return Ok(None);
7633        };
7634        let Some(board) = nodes.iter().find_map(|node| {
7635            let project = node.get("project")?;
7636            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7637        }) else {
7638            return Ok(None);
7639        };
7640        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7641            return Ok(None);
7642        };
7643        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7644        Ok(Some(fields.clone()))
7645    }
7646
7647    /// What one board item's `Priority` field says, through this instance's mapping.
7648    ///
7649    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7650    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7651    /// option the mapping does not name is kept as itself — never read as a level or as
7652    /// `none` — for a read of the task to report by name.
7653    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7654        let Some(mapping) = &self.priorities else {
7655            return Ok(HeldPriority::Read(Priority::None));
7656        };
7657        // A value of the field that names no option — a text field someone called `Priority` —
7658        // is malformed rather than `none`: reading it as no priority would let the next copy
7659        // clear one a person set.
7660        let Some(option) = field_values
7661            .iter()
7662            .find(|value| {
7663                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7664            })
7665            .map(|value| required_str(value, "name"))
7666            .transpose()?
7667        else {
7668            return Ok(HeldPriority::Read(Priority::None));
7669        };
7670        Ok(mapping.priority_of(option).map_or_else(
7671            || HeldPriority::Unmapped(option.to_owned()),
7672            HeldPriority::Read,
7673        ))
7674    }
7675
7676    /// What each projected metadata field holds for one board item, by field name — every text
7677    /// value GitHub answers, an empty one included.
7678    fn held_projections(
7679        &self,
7680        field_values: &[Value],
7681    ) -> Result<BTreeMap<String, String>, SourceError> {
7682        let mut held = BTreeMap::new();
7683        for projection in &self.metadata_fields {
7684            if let Some(text) = text_field(field_values, &projection.field)? {
7685                held.insert(projection.field.to_string(), text);
7686            }
7687        }
7688        Ok(held)
7689    }
7690
7691    /// What one board item's status is read from: its `Status` option, whether its issue
7692    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7693    /// three into the status it reports.
7694    fn status_parts<'a>(
7695        field_values: &'a [Value],
7696        content: &'a Value,
7697    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7698        let option = field_values
7699            .iter()
7700            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7701            .map(|value| required_str(value, "name"))
7702            .transpose()?;
7703        let closed = optional_str(content, "state")? == Some("CLOSED");
7704        Ok((option, closed, optional_str(content, "stateReason")?))
7705    }
7706
7707    /// The board Status option this write selects, or the refusal that says why not.
7708    ///
7709    /// The mapped option is required for both open and terminal targets. A terminal write
7710    /// validates it before changing either representation, so it can never fall back to
7711    /// closing an issue whose board cannot display the matching status.
7712    ///
7713    /// Answers the field's id, the option's id, and the option's name as the board spells
7714    /// it — which is the name a read of the item reports once it sits there.
7715    fn column_for(
7716        &self,
7717        fields: &Value,
7718        kind: ItemKind,
7719        category: StatusCategory,
7720        target: &StatusTarget,
7721    ) -> Result<Option<(String, String, String)>, SourceError> {
7722        let Some(wanted) = target.option() else {
7723            return Ok(None);
7724        };
7725        let missing = |detail: &str| SourceError::Refused {
7726            message: format!(
7727                "{} status {} of source {} needs the board Status option {wanted:?}, and \
7728                 {detail}; next: add that option to the board, which `onetaskgraph sources \
7729                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7730                 it has",
7731                kind.marker(),
7732                category_name(category),
7733                self.name,
7734                self.name,
7735                category_name(category),
7736                kind.marker()
7737            ),
7738        };
7739        let Some(field) = Board::field(fields, "Status")? else {
7740            return Err(missing("this board has no Status field"));
7741        };
7742        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7743            return Err(missing(
7744                "this board's Status field is not a single-select field",
7745            ));
7746        }
7747        let option = field
7748            .get("options")
7749            .and_then(Value::as_array)
7750            .and_then(|options| {
7751                options.iter().find(|option| {
7752                    option
7753                        .get("name")
7754                        .and_then(Value::as_str)
7755                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7756                })
7757            });
7758        match option {
7759            None => Err(missing("this board does not have it")),
7760            Some(option) => Ok(Some((
7761                required_str(field, "id")?.to_owned(),
7762                required_str(option, "id")?.to_owned(),
7763                required_str(option, "name")?.to_owned(),
7764            ))),
7765        }
7766    }
7767
7768    /// The refusal a status that closes an issue is answered with over a board draft.
7769    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7770        SourceError::Refused {
7771            message: format!(
7772                "status {} of source {} closes the item's issue, and GitHub draft items have \
7773                 no open or closed state",
7774                category_name(category),
7775                self.name
7776            ),
7777        }
7778    }
7779
7780    /// What a status write to one item needs of the board: the board's id and the
7781    /// definition of its `Status` field, read off the item when the item says both.
7782    ///
7783    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7784    /// and its `Status` value carries that field's definition, options and all. An item that
7785    /// does not say — no board id, or no `Status` value to read the field off — takes them
7786    /// from [`Self::board_fields`], which reads no item.
7787    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7788        if let Some(board) = item.carried_board() {
7789            return Ok(board);
7790        }
7791        if item.defines("Status")
7792            && let Some(board_id) = item.named_board()
7793        {
7794            return Ok(BoardFields {
7795                id: board_id,
7796                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7797            });
7798        }
7799        self.board_fields().await
7800    }
7801
7802    /// Set one task's or one project's status and nothing else; see
7803    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7804    async fn set_status(
7805        &self,
7806        id: &NativeId,
7807        kind: ItemKind,
7808        category: StatusCategory,
7809    ) -> Result<Option<Status>, SourceError> {
7810        // Refused before anything is read, in the words a write of the same status is.
7811        let target = self.resolved_target(kind, category)?;
7812        // An item read by issue reference carries only what this write needs, so what it
7813        // becomes is never remembered for a later read to be answered from.
7814        let held = self.take_held(id)?;
7815        let by_reference = held.is_some();
7816        let bound = match held {
7817            Some(item) => Some(item),
7818            None => self.bound_item(id).await?,
7819        };
7820        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7821            return Ok(None);
7822        };
7823        let board = self.status_board(&item).await?;
7824        let (field, option, name) = self
7825            .column_for(&board.fields, kind, category, &target)?
7826            .ok_or_else(|| SourceError::Malformed {
7827                message: format!(
7828                    "status {} of source {} names no board Status option",
7829                    category_name(category),
7830                    self.name
7831                ),
7832            })?;
7833        if item.status.category == category && item.option.as_deref() == Some(&name) {
7834            return Ok(Some(item.status));
7835        }
7836        match &target {
7837            StatusTarget::Terminal(_, reason) => {
7838                if item.content_kind == ContentKind::DraftIssue {
7839                    return Err(self.closes_a_draft(category));
7840                }
7841                // The option first, so a refused selection leaves an open issue open rather
7842                // than closed behind a card that still reads open.
7843                self.set_item_field(
7844                    board.id.as_str(),
7845                    &item.item_id,
7846                    &field,
7847                    json!({"singleSelectOptionId": option}),
7848                )
7849                .await?;
7850                // An issue already closed in a way that reads as this category keeps its state
7851                // and its reason: only its card was out of line, and closing it again would
7852                // overwrite a reason — a duplicate's — that says more than this write does.
7853                let already = item.closed && item.status.category == category;
7854                if !already {
7855                    self.update_content(
7856                        ContentKind::Issue,
7857                        &item.id,
7858                        json!({"stateInput": state_input(Some(&target))}),
7859                    )
7860                    .await?;
7861                }
7862                let reason = if already {
7863                    item.reason.clone()
7864                } else {
7865                    Some(reason.reason().to_owned())
7866                };
7867                item.closed = true;
7868                item.status = self
7869                    .statuses
7870                    .status(kind, Some(&name), true, reason.as_deref());
7871                item.option = Some(name);
7872                item.reason = reason;
7873            }
7874            StatusTarget::Column(_) => {
7875                // An option is what an open item's status is, so a closed issue is reopened
7876                // first — sitting closed in the column, it would read back as closed. A draft has
7877                // no state to reopen.
7878                if item.content_kind == ContentKind::Issue && item.closed {
7879                    self.update_content(
7880                        ContentKind::Issue,
7881                        &item.id,
7882                        json!({"stateInput": state_input(Some(&target))}),
7883                    )
7884                    .await?;
7885                    item.closed = false;
7886                }
7887                self.set_item_field(
7888                    board.id.as_str(),
7889                    &item.item_id,
7890                    &field,
7891                    json!({"singleSelectOptionId": option}),
7892                )
7893                .await?;
7894                item.status = self.statuses.status(kind, Some(&name), false, None);
7895                item.option = Some(name);
7896            }
7897            StatusTarget::Disabled(_) => {
7898                unreachable!("resolved_target refused a disabled status")
7899            }
7900        }
7901        let status = item.status.clone();
7902        if !by_reference {
7903            self.remember_written(item, false)?;
7904        }
7905        Ok(Some(status))
7906    }
7907
7908    /// Replace one task's `delivered_by` and nothing else; see
7909    /// [`TaskSource::set_delivered_by`].
7910    ///
7911    /// One update of the body, which differs from the body GitHub holds only inside the
7912    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7913    async fn replace_delivered_by(
7914        &self,
7915        id: &NativeId,
7916        delivered_by: &[TaskRef],
7917    ) -> Result<Option<()>, SourceError> {
7918        let entries = TaskRef::listed(
7919            TaskRef::DELIVERED_BY_KEY,
7920            id,
7921            Some(&self.name),
7922            delivered_by.to_vec(),
7923        )
7924        .map_err(|message| SourceError::Refused { message })?;
7925        let Some(mut item) = self
7926            .bound_item(id)
7927            .await?
7928            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7929        else {
7930            return Ok(None);
7931        };
7932        let mut slot = item.slot.clone();
7933        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7934        self.write_slot(&mut item, &slot).await?;
7935        item.delivered_by = entries;
7936        self.remember_written(item, false)?;
7937        Ok(Some(()))
7938    }
7939
7940    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7941    /// see [`TaskSource::set_task_metadata`].
7942    ///
7943    /// `None` when this board holds no item by that id, or holds one of another kind. The
7944    /// answer is the item as this source now reads it, so what a caller is told the key
7945    /// holds is what the slot holds.
7946    ///
7947    /// A key already holding the value is answered without a write, compared as JSON rather
7948    /// than as the body's bytes: a slot a person spelled with other whitespace would
7949    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7950    async fn set_slot_key(
7951        &self,
7952        id: &NativeId,
7953        kind: BoardKind,
7954        key: &MetadataKey,
7955        value: &Value,
7956    ) -> Result<Option<Resolved>, SourceError> {
7957        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7958            return Ok(None);
7959        };
7960        if item.slot.get(key.as_str()) == Some(value) {
7961            return Ok(Some(item));
7962        }
7963        let mut slot = item.slot.clone();
7964        slot.insert(key.as_str().to_owned(), value.clone());
7965        // A key a board text field projects moves that field first, so the body — the value's
7966        // one authoritative home — is written last, as every write of an existing item is.
7967        if self
7968            .metadata_fields
7969            .iter()
7970            .any(|projection| &*projection.key == key.as_str())
7971        {
7972            let board = self.projection_board(&item).await?;
7973            let (writes, projected) = self.projection_writes(
7974                &board.fields,
7975                Some(&item.projected),
7976                &slot,
7977                Some(&[key.as_str()]),
7978            )?;
7979            let (mut values, mut clears) = (Vec::new(), Vec::new());
7980            for write in &writes {
7981                write.join(&mut values, &mut clears);
7982            }
7983            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7984                .await?;
7985            item.projected = projected;
7986        }
7987        self.write_slot(&mut item, &slot).await?;
7988        self.remember_written(item.clone(), false)?;
7989        Ok(Some(item))
7990    }
7991
7992    /// What a projection write to `item` needs of the board, read off the item when it says
7993    /// enough, as [`Self::fields_for`] reads it.
7994    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7995        if let Some(board) = item.carried_board() {
7996            return Ok(board);
7997        }
7998        if let Some(board_id) = item.named_board()
7999            && self
8000                .metadata_fields
8001                .iter()
8002                .all(|projection| item.defines(&projection.field))
8003        {
8004            return Ok(BoardFields {
8005                id: board_id,
8006                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8007            });
8008        }
8009        self.board_fields().await
8010    }
8011
8012    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
8013    /// `item` up to what that write left.
8014    ///
8015    /// The body sent differs from the body GitHub holds only inside the slot — see
8016    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
8017    /// the mutation the item's content takes, so a board draft's body is written with
8018    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
8019    async fn write_slot(
8020        &self,
8021        item: &mut Resolved,
8022        slot: &BTreeMap<String, Value>,
8023    ) -> Result<(), SourceError> {
8024        let held = item.raw_body.clone().unwrap_or_default();
8025        let body = with_slot(&held, slot)?;
8026        if body != held {
8027            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8028                .await?;
8029        }
8030        let (visible, slot) = metadata_body(Some(body.clone()))?;
8031        item.body = visible.filter(|value| !value.is_empty());
8032        item.raw_body = Some(body);
8033        item.slot = slot;
8034        Ok(())
8035    }
8036
8037    /// This instance's target for a category written to an item of `kind`, refusing one
8038    /// that kind has no option for — before anything is read or written.
8039    ///
8040    /// Nothing here mutates the board's option set to make room for a status. GitHub
8041    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
8042    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
8043    /// field and every item's status.
8044    fn resolved_target(
8045        &self,
8046        kind: ItemKind,
8047        category: StatusCategory,
8048    ) -> Result<StatusTarget, SourceError> {
8049        let target = self.statuses.target(kind, category).clone();
8050        let StatusTarget::Disabled(why) = target else {
8051            return Ok(target);
8052        };
8053        let refusal = why.refusal(&self.name, category, kind);
8054        // Why there is no shipped default, which is the question a person meeting this
8055        // refusal on a source that never mentioned the category asks.
8056        let shipped_none = match category {
8057            StatusCategory::Draft => Some(
8058                "draft has no shipped default because GitHub draft issues cannot have \
8059                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
8060            ),
8061            StatusCategory::Unknown => Some(
8062                "unknown has no shipped default because this board keeps no open-ended status \
8063                 word: every word classified unknown is written to the one board Status option \
8064                 status_mapping.unknown names",
8065            ),
8066            _ => None,
8067        };
8068        Err(match (refusal, shipped_none, why) {
8069            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
8070                SourceError::Refused {
8071                    message: format!("{message}; {note}"),
8072                }
8073            }
8074            (refusal, _, _) => refusal,
8075        })
8076    }
8077
8078    /// What writing `priority` does to one item's `Priority` field on this board, or the
8079    /// refusal naming what the board lacks.
8080    ///
8081    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
8082    /// none already, or of an item not created yet. Every other priority selects the option
8083    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
8084    /// without that option, is refused rather than given one: reads and writes never create
8085    /// a field or an option.
8086    fn priority_write(
8087        &self,
8088        fields: &Value,
8089        existing: Option<&Resolved>,
8090        priority: Priority,
8091    ) -> Result<Option<PriorityWrite>, SourceError> {
8092        let Some(mapping) = &self.priorities else {
8093            return Err(self.holds_no_priority());
8094        };
8095        let Some(wanted) = mapping.option(priority) else {
8096            if !existing.is_some_and(Resolved::holds_priority) {
8097                return Ok(None);
8098            }
8099            let field =
8100                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
8101                    message: format!(
8102                        "an item holding a {PRIORITY_FIELD} value was read without that field"
8103                    ),
8104                })?;
8105            return Ok(Some(PriorityWrite::Clear {
8106                field: required_str(field, "id")?.to_owned(),
8107            }));
8108        };
8109        let missing = |detail: &str| SourceError::Refused {
8110            message: format!(
8111                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
8112                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
8113                 it, or point priority_mapping.{priority} of this source at an option the board \
8114                 has",
8115                self.name, self.name
8116            ),
8117        };
8118        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
8119            return Err(missing(&format!(
8120                "this board has no {PRIORITY_FIELD} field"
8121            )));
8122        };
8123        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
8124            return Err(missing(&format!(
8125                "this board's {PRIORITY_FIELD} field is not a single-select field"
8126            )));
8127        }
8128        // An options list that is absent or not a list is an answer this source cannot read,
8129        // not a board lacking the option: `sources fields --apply` is no remedy for it.
8130        let option = field
8131            .get("options")
8132            .and_then(Value::as_array)
8133            .ok_or_else(|| SourceError::Malformed {
8134                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
8135            })?
8136            .iter()
8137            .find(|option| {
8138                option
8139                    .get("name")
8140                    .and_then(Value::as_str)
8141                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8142            })
8143            .ok_or_else(|| missing("this board does not have it"))?;
8144        Ok(Some(PriorityWrite::Select {
8145            field: required_str(field, "id")?.to_owned(),
8146            option: required_str(option, "id")?.to_owned(),
8147        }))
8148    }
8149
8150    /// What writing `metadata` does to each projected text field of one item, and what those
8151    /// fields hold after it — or the refusal, before anything is sent.
8152    ///
8153    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8154    /// narrows the work to the entries projecting one of those metadata keys, for a write that
8155    /// touches only them; `None` is every entry. A string the field already holds, and nothing
8156    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8157    /// and so is a board without the field, or with a field of that name that is not a text
8158    /// field: reads and writes never create a field.
8159    fn projection_writes(
8160        &self,
8161        fields: &Value,
8162        held: Option<&BTreeMap<String, String>>,
8163        metadata: &BTreeMap<String, Value>,
8164        keys: Option<&[&str]>,
8165    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8166        let mut projected = held.cloned().unwrap_or_default();
8167        let mut writes = Vec::new();
8168        for projection in self
8169            .metadata_fields
8170            .iter()
8171            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8172        {
8173            let wanted = projection.projected(metadata, &self.name)?;
8174            let field = self.projection_field(fields, projection)?;
8175            let holding = projected.get(&*projection.field);
8176            match wanted {
8177                Projected::Text(text) if holding != Some(&text) => {
8178                    writes.push(ProjectionWrite::Set {
8179                        field,
8180                        text: text.clone(),
8181                    });
8182                    projected.insert(projection.field.to_string(), text);
8183                }
8184                Projected::Nothing if holding.is_some() => {
8185                    writes.push(ProjectionWrite::Clear { field });
8186                    projected.remove(&*projection.field);
8187                }
8188                Projected::Text(_) | Projected::Nothing => {}
8189            }
8190        }
8191        Ok((writes, projected))
8192    }
8193
8194    /// The id of the board text field one projection writes, or the refusal naming what the
8195    /// board has instead.
8196    fn projection_field(
8197        &self,
8198        fields: &Value,
8199        projection: &MetadataField,
8200    ) -> Result<String, SourceError> {
8201        let refuse = |detail: String| SourceError::Refused {
8202            message: format!(
8203                "source {} projects metadata key {:?} onto the board text field {:?}, and \
8204                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8205                 text field, or name another field in metadata_fields",
8206                self.name, &*projection.key, &*projection.field, self.name
8207            ),
8208        };
8209        let Some(field) = Board::field(fields, &projection.field)? else {
8210            return Err(refuse("this board has no field of that name".to_owned()));
8211        };
8212        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8213        let data_type = optional_str(field, "dataType")?;
8214        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8215            return Err(refuse(format!(
8216                "this board's field of that name is not a text field (it is a {}), so it is \
8217                 left as it is; rename or remove that field first",
8218                data_type.unwrap_or(typename)
8219            )));
8220        }
8221        Ok(required_str(field, "id")?.to_owned())
8222    }
8223
8224    /// Apply one priority write to one board item.
8225    async fn write_priority(
8226        &self,
8227        board_id: &str,
8228        item_id: &str,
8229        write: &PriorityWrite,
8230    ) -> Result<(), SourceError> {
8231        match write {
8232            PriorityWrite::Select { field, option } => {
8233                self.set_item_field(
8234                    board_id,
8235                    item_id,
8236                    field,
8237                    json!({"singleSelectOptionId": option}),
8238                )
8239                .await
8240            }
8241            PriorityWrite::Clear { field } => {
8242                let data = self
8243                    .graphql(
8244                        graphql::CLEAR_FIELD,
8245                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8246                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
8247                    )
8248                    .await?;
8249                let returned = data
8250                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8251                    .ok_or_else(|| SourceError::Malformed {
8252                        message: "GitHub field clear returned no project item".into(),
8253                    })?;
8254                if required_str(returned, "id")? != item_id {
8255                    return Err(SourceError::Malformed {
8256                        message: "GitHub field clear returned the wrong project item".into(),
8257                    });
8258                }
8259                Ok(())
8260            }
8261        }
8262    }
8263
8264    /// The refusal a priority is answered with by an instance configured with no
8265    /// `priority_mapping`, which holds none.
8266    fn holds_no_priority(&self) -> SourceError {
8267        SourceError::Refused {
8268            message: format!(
8269                "source {} holds no task priority: its configuration sets no priority_mapping; \
8270                 next: set priority_mapping on this source, then run `onetaskgraph sources \
8271                 fields {} --apply` to set its board up",
8272                self.name, self.name
8273            ),
8274        }
8275    }
8276
8277    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8278    ///
8279    /// One field write — a select, or a clear for `none` — and no title, body, label, state
8280    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8281    async fn set_priority(
8282        &self,
8283        id: &NativeId,
8284        priority: Priority,
8285    ) -> Result<Option<Priority>, SourceError> {
8286        if self.priorities.is_none() {
8287            return Err(self.holds_no_priority());
8288        }
8289        let Some(mut item) = self
8290            .bound_item(id)
8291            .await?
8292            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8293        else {
8294            return Ok(None);
8295        };
8296        if priority == Priority::None && !item.holds_priority() {
8297            return Ok(Some(priority));
8298        }
8299        // The item's own read carries the field's definition whenever it holds a value of
8300        // it, which a clear always does; a select onto an item holding none reads the board.
8301        let board = match (item.carried_board(), item.named_board()) {
8302            (Some(board), _) => board,
8303            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8304                id,
8305                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8306            },
8307            _ => self.board_fields().await?,
8308        };
8309        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8310            return Ok(Some(priority));
8311        };
8312        let (document, root, input) = match write {
8313            PriorityWrite::Select { field, option } => (
8314                graphql::UPDATE_FIELD,
8315                "updateProjectV2ItemFieldValue",
8316                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8317            ),
8318            PriorityWrite::Clear { field } => (
8319                graphql::CLEAR_FIELD,
8320                "clearProjectV2ItemFieldValue",
8321                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8322            ),
8323        };
8324        let data = self
8325            .graphql(
8326                document,
8327                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8328            )
8329            .await?;
8330        let returned = data
8331            .get(root)
8332            .and_then(|value| value.get("projectV2Item"))
8333            .ok_or_else(|| SourceError::Malformed {
8334                message: "GitHub priority write returned no project item".into(),
8335            })?;
8336        if required_str(returned, "id")? != item.item_id {
8337            return Err(SourceError::Malformed {
8338                message: "GitHub priority write returned the wrong project item".into(),
8339            });
8340        }
8341        let value = returned
8342            .get("fieldValueByName")
8343            .ok_or_else(|| SourceError::Malformed {
8344                message: "GitHub priority write returned no priority read-back".into(),
8345            })?;
8346        if !value.is_null()
8347            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8348        {
8349            return Err(SourceError::Malformed {
8350                message: "GitHub priority read-back is not a Priority field value".into(),
8351            });
8352        }
8353        let values = if value.is_null() {
8354            Vec::new()
8355        } else {
8356            vec![value.clone()]
8357        };
8358        item.priority = self.held_priority(&values)?;
8359        let answer = item.task()?.priority;
8360        self.remember_written(item, false)?;
8361        Ok(Some(answer))
8362    }
8363
8364    /// Replace one task's visible body and nothing else; see
8365    /// [`TaskSource::set_task_content`].
8366    ///
8367    /// One update of the body, which differs from the body GitHub holds only outside the
8368    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8369    /// this source keeps there reads back as it was. A body that would not change is not
8370    /// sent at all.
8371    async fn replace_content(
8372        &self,
8373        id: &NativeId,
8374        content: &str,
8375    ) -> Result<Option<()>, SourceError> {
8376        let Some(mut item) = self
8377            .bound_item(id)
8378            .await?
8379            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8380        else {
8381            return Ok(None);
8382        };
8383        let held = item.raw_body.clone().unwrap_or_default();
8384        let body = with_content(&held, content)?;
8385        // Checked before anything is sent: content ending in what this source reads as its own
8386        // metadata slot would read back as metadata rather than as the content it was.
8387        let (visible, slot) = metadata_body(Some(body.clone()))?;
8388        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8389            return Err(SourceError::Refused {
8390                message: format!(
8391                    "this content ends in what source {} reads as its own metadata slot \
8392                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8393                     as content; next: remove that trailing block from the content",
8394                    self.name
8395                ),
8396            });
8397        }
8398        if body != held {
8399            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8400                .await?;
8401        }
8402        item.body = visible.filter(|value| !value.is_empty());
8403        item.raw_body = Some(body);
8404        item.slot = slot;
8405        self.remember_written(item, false)?;
8406        Ok(Some(()))
8407    }
8408
8409    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8410    ///
8411    /// One read of the item — which carries the board's field definitions and the issue's
8412    /// `blockedBy`, so neither is read again — and then only what differs from it: the
8413    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8414    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8415    /// the title, the body — visible content and metadata slot together — and a state change.
8416    /// So an update naming any of title, body, metadata, status and priority is one read and
8417    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8418    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8419    /// as a whole write does; an open one selects its option and then reopens. The origin
8420    /// field is never written: an update is of an item that already exists, whose origin is
8421    /// what it is.
8422    ///
8423    /// The task answered is the item as those writes left it, built from the read and what was
8424    /// sent rather than read again — the same record a later read in this run answers from.
8425    async fn targeted_update(
8426        &self,
8427        id: &NativeId,
8428        update: &TaskUpdate,
8429    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8430        // Everything this source can refuse without reading the item is refused first, in the
8431        // words a whole write of the same fields is refused with.
8432        update.consistent()?;
8433        if update
8434            .title
8435            .as_deref()
8436            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8437        {
8438            return Err(SourceError::Refused {
8439                message: format!(
8440                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8441                     spells a document, so it would read back as one rather than as a task; \
8442                     retitle it",
8443                    self.name
8444                ),
8445            });
8446        }
8447        if let Some(delivers) = &update.delivers {
8448            TaskRef::listed(
8449                TaskRef::DELIVERS_KEY,
8450                id,
8451                Some(&self.name),
8452                delivers.clone(),
8453            )
8454            .map_err(|message| SourceError::Refused { message })?;
8455        }
8456        if self.priorities.is_none()
8457            && update
8458                .priority
8459                .is_some_and(|priority| priority != Priority::None)
8460        {
8461            return Err(self.holds_no_priority());
8462        }
8463        let target = update
8464            .status
8465            .as_ref()
8466            .map(|status| self.resolved_target(ItemKind::Task, status.category))
8467            .transpose()?;
8468        let Some(mut item) = self
8469            .bound_item(id)
8470            .await?
8471            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8472        else {
8473            return Ok(None);
8474        };
8475        let before = item.task()?;
8476
8477        let mut status_move = None;
8478        if let (Some(status), Some(target)) = (&update.status, target) {
8479            let board = self.status_board(&item).await?;
8480            let (field, option, name) = self
8481                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8482                .ok_or_else(|| SourceError::Malformed {
8483                    message: format!(
8484                        "status {} of source {} names no board Status option",
8485                        category_name(status.category),
8486                        self.name
8487                    ),
8488                })?;
8489            let terminal = matches!(target, StatusTarget::Terminal(_, _));
8490            if terminal && item.content_kind == ContentKind::DraftIssue {
8491                return Err(self.closes_a_draft(status.category));
8492            }
8493            let landed = match &target {
8494                StatusTarget::Terminal(_, reason) => {
8495                    self.statuses
8496                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8497                }
8498                _ => self
8499                    .statuses
8500                    .status(ItemKind::Task, Some(&name), false, None),
8501            };
8502            let option_moves = item
8503                .option
8504                .as_deref()
8505                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8506            let state_moves = item.content_kind == ContentKind::Issue
8507                && (item.closed != terminal || (terminal && item.status != landed));
8508            if let Some(moves) = Moves::of(option_moves, state_moves) {
8509                status_move = Some(StatusMove {
8510                    board: board.id,
8511                    field,
8512                    option,
8513                    name,
8514                    target,
8515                    landed,
8516                    moves,
8517                });
8518            }
8519        }
8520
8521        let mut priority_move = None;
8522        if let Some(priority) = update.priority
8523            && self.priorities.is_some()
8524            && item.priority != HeldPriority::Read(priority)
8525        {
8526            let board = match (item.carried_board(), item.named_board()) {
8527                (Some(board), _) => board,
8528                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8529                    id: board,
8530                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8531                },
8532                _ => self.board_fields().await?,
8533            };
8534            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8535                priority_move = Some((board.id, write, priority));
8536            }
8537        }
8538
8539        // The metadata keys this update names that a board text field projects, and what
8540        // that does to those fields — refused, as a whole write refuses it, before anything
8541        // is sent.
8542        let touched: Vec<&str> = update
8543            .metadata_set
8544            .keys()
8545            .chain(update.metadata_remove.iter())
8546            .map(MetadataKey::as_str)
8547            .collect();
8548        let mut projection_move = None;
8549        if self
8550            .metadata_fields
8551            .iter()
8552            .any(|projection| touched.contains(&&*projection.key))
8553        {
8554            let mut metadata = item.slot.clone();
8555            for (key, value) in &update.metadata_set {
8556                metadata.insert(key.as_str().to_owned(), value.clone());
8557            }
8558            for key in &update.metadata_remove {
8559                metadata.remove(key.as_str());
8560            }
8561            let board = self.projection_board(&item).await?;
8562            let (writes, projected) = self.projection_writes(
8563                &board.fields,
8564                Some(&item.projected),
8565                &metadata,
8566                Some(&touched),
8567            )?;
8568            projection_move = Some((board.id, writes, projected));
8569        }
8570
8571        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8572        // recorded in the slot, and the slot travels in the one body update below.
8573        let edges = match &update.depends_on {
8574            Some(edges) => Some(
8575                self.partition_edges(
8576                    BoardKind::Work(ItemKind::Task),
8577                    item.content_kind,
8578                    item.blocked_by.as_deref(),
8579                    edges,
8580                )
8581                .await?,
8582            ),
8583            None => None,
8584        };
8585
8586        // A record primed from what a producer wrote holds no body anybody read, so an update
8587        // changing the body reads it first: by the `Status` write's own answer when the update
8588        // moves the option — the mutation answering with the issue whole — and otherwise by
8589        // reading the issue. The body is then composed against what GitHub holds now.
8590        let mut option_selected = false;
8591        // The mark stays until a read lands, so an update that writes no body, or one whose
8592        // read fails, leaves the next update of it to read the body too.
8593        if (update.content.is_some()
8594            || !update.metadata_set.is_empty()
8595            || !update.metadata_remove.is_empty()
8596            || update.delivers.is_some()
8597            || edges.is_some())
8598            && self.is_unread(id)
8599        {
8600            let live = match status_move.as_ref().filter(|moving| moving.moves.option()) {
8601                Some(moving) => {
8602                    option_selected = true;
8603                    Some(
8604                        self.select_reading_body(
8605                            moving.board.as_str(),
8606                            &item.item_id,
8607                            &moving.field,
8608                            &moving.option,
8609                        )
8610                        .await?,
8611                    )
8612                }
8613                None => {
8614                    self.forget(id)?;
8615                    let Some(read) = self.item_by_id(id).await? else {
8616                        return Ok(None);
8617                    };
8618                    read.raw_body
8619                }
8620            };
8621            let (visible, read_slot) = metadata_body(live.clone())?;
8622            self.mark_read(id);
8623            item.raw_body = live;
8624            item.body = visible;
8625            item.slot = read_slot;
8626        }
8627
8628        let mut slot = item.slot.clone();
8629        for (key, value) in &update.metadata_set {
8630            slot.insert(key.as_str().to_owned(), value.clone());
8631        }
8632        for key in &update.metadata_remove {
8633            slot.remove(key.as_str());
8634        }
8635        if let Some(delivers) = &update.delivers {
8636            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8637        }
8638        if let Some((_, recorded)) = &edges {
8639            record_edges(&mut slot, recorded);
8640        }
8641        let held = item.raw_body.clone().unwrap_or_default();
8642        let content = match &update.content {
8643            Some(content) => with_content(&held, content)?,
8644            None => held.clone(),
8645        };
8646        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8647        // the body's bytes, as a metadata write compares it: a slot a person spelled with
8648        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8649        let body = if slot == item.slot {
8650            content
8651        } else {
8652            with_slot(&content, &slot)?
8653        };
8654        // Checked before anything is sent, as a content write checks it: content ending in
8655        // what this source reads as its own slot would read back as metadata.
8656        let (visible, read) = metadata_body(Some(body.clone()))?;
8657        let wanted = update.content.as_deref().or(item.body.as_deref());
8658        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8659            return Err(SourceError::Refused {
8660                message: format!(
8661                    "this content ends in what source {} reads as its own metadata slot \
8662                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8663                     as content; next: remove that trailing block from the content",
8664                    self.name
8665                ),
8666            });
8667        }
8668        let recorded_moves =
8669            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8670
8671        // One `updateIssue` carries all three, because every mutation spends the secondary
8672        // limiter and the title, body and state are one mutation's inputs.
8673        let mut fields = serde_json::Map::new();
8674        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8675            fields.insert("title".to_owned(), json!(title));
8676        }
8677        if body != held {
8678            fields.insert("body".to_owned(), json!(body));
8679        }
8680        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8681            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8682        }
8683        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8684        // GitHub runs no two requests as one, and runs one document's mutation fields in order
8685        // without undoing an earlier field when a later one fails — so a body written before a
8686        // board field the board then refused would be left changed. Written after every other
8687        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8688        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8689        // first, together in one request — a terminal option selected before the issue
8690        // closes, as a whole write does — then the `blockedBy` difference, then the body.
8691        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8692        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8693        if let Some(moving) = status_move
8694            .as_ref()
8695            .filter(|moving| moving.moves.option() && !option_selected)
8696        {
8697            board_writes.push((
8698                &moving.board,
8699                (
8700                    moving.field.clone(),
8701                    json!({"singleSelectOptionId": moving.option}),
8702                ),
8703            ));
8704        }
8705        match &priority_move {
8706            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8707                board,
8708                (field.clone(), json!({"singleSelectOptionId": option})),
8709            )),
8710            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8711            None => {}
8712        }
8713        if let Some((board, writes, _)) = &projection_move {
8714            for write in writes {
8715                let (mut values, mut cleared) = (Vec::new(), Vec::new());
8716                write.join(&mut values, &mut cleared);
8717                board_writes.extend(values.into_iter().map(|value| (board, value)));
8718                clears.extend(cleared.into_iter().map(|field| (board, field)));
8719            }
8720        }
8721        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8722        boards.extend(clears.iter().map(|(board, _)| *board));
8723        boards.dedup_by(|one, other| one.as_str() == other.as_str());
8724        for board in boards {
8725            let writes = board_writes
8726                .iter()
8727                .filter(|(on, _)| on.as_str() == board.as_str())
8728                .map(|(_, write)| write.clone())
8729                .collect::<Vec<_>>();
8730            let cleared = clears
8731                .iter()
8732                .filter(|(on, _)| on.as_str() == board.as_str())
8733                .map(|(_, field)| *field)
8734                .collect::<Vec<_>>();
8735            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8736                .await?;
8737        }
8738        let mut blocked_by_moved = false;
8739        if let Some((native, _)) = &edges
8740            && item.content_kind == ContentKind::Issue
8741        {
8742            blocked_by_moved = self
8743                .reconcile_blocked_by(
8744                    &item.id,
8745                    native,
8746                    Issue::Existing(item.blocked_by.as_deref()),
8747                )
8748                .await?;
8749        }
8750        if !fields.is_empty() {
8751            self.update_content(item.content_kind, &item.id, Value::Object(fields))
8752                .await?;
8753        }
8754
8755        // The `blockedBy` this item was read with is what the write just changed, so it is no
8756        // answer any more: a later write in this command would reconcile against it — re-adding
8757        // what it kept and never removing what it dropped — and a forward-edge read would
8758        // answer with it. Left unknown, each asks GitHub instead.
8759        if blocked_by_moved {
8760            item.blocked_by = None;
8761        }
8762        if let Some(title) = &update.title {
8763            item.title.clone_from(title);
8764        }
8765        item.body = visible.filter(|value| !value.is_empty());
8766        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8767        item.slot = slot;
8768        if let Some(delivers) = &update.delivers {
8769            item.delivers.clone_from(delivers);
8770        }
8771        if let Some(moving) = status_move {
8772            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8773                && item.content_kind == ContentKind::Issue;
8774            item.status = moving.landed;
8775            item.option = Some(moving.name);
8776        }
8777        if let Some((_, _, priority)) = priority_move {
8778            item.priority = HeldPriority::Read(priority);
8779        }
8780        if let Some((_, _, projected)) = projection_move {
8781            item.projected = projected;
8782        }
8783        let task = item.task()?;
8784        let mut written = update.changed(&before, &task);
8785        if blocked_by_moved || recorded_moves {
8786            written.insert(UpdatedField::DependsOn);
8787        }
8788        self.remember_written(item, false)?;
8789        Ok(Some(TaskUpdateOutcome {
8790            task,
8791            written,
8792            delivers_before: before.delivers,
8793        }))
8794    }
8795
8796    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8797    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8798    ///
8799    /// One update of the body: the content outside the slot, and inside it that one entry,
8800    /// every other entry kept as it was. This source keeps no template answers — an issue has
8801    /// no room beside itself that is not its body, and answers written there would duplicate
8802    /// what the content already says and count against GitHub's body limit — so `answers`
8803    /// reaches nothing here. A body that would not change is not sent at all.
8804    async fn replace_rendering(
8805        &self,
8806        id: &NativeId,
8807        kind: BoardKind,
8808        content: &str,
8809        provenance: &Value,
8810        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8811    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8812        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8813            return Ok(None);
8814        };
8815        let held = item.raw_body.clone().unwrap_or_default();
8816        let mut slot = item.slot.clone();
8817        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8818        let rewritten;
8819        let content = if let Some(assets) = assets {
8820            let uploads = self
8821                .upload_assets(item.own_repository.as_ref(), assets)
8822                .await?;
8823            rewritten =
8824                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8825            rewritten.as_str()
8826        } else {
8827            content
8828        };
8829        let body = with_slot(&with_content(&held, content)?, &slot)?;
8830        // Checked before anything is sent, as a content write checks it.
8831        let (visible, read) = metadata_body(Some(body.clone()))?;
8832        if visible.as_deref().unwrap_or_default() != content || read != slot {
8833            return Err(SourceError::Refused {
8834                message: format!(
8835                    "this content ends in what source {} reads as its own metadata slot \
8836                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8837                     as content; next: remove that trailing block from the template",
8838                    self.name
8839                ),
8840            });
8841        }
8842        if body != held {
8843            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8844                .await?;
8845        }
8846        item.body = visible.filter(|value| !value.is_empty());
8847        item.raw_body = Some(body);
8848        item.slot = read;
8849        self.remember_written(item, false)?;
8850        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8851            id: id.clone(),
8852            content: Some(content.to_owned()),
8853        }))
8854    }
8855
8856    async fn set_item_field(
8857        &self,
8858        board_id: &str,
8859        item_id: &str,
8860        field_id: &str,
8861        value: Value,
8862    ) -> Result<(), SourceError> {
8863        let data = self
8864            .graphql(
8865                graphql::UPDATE_FIELD,
8866                json!({"input":{
8867                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8868                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8869            )
8870            .await?;
8871        field_update_item(&data, item_id).map(|_| ())
8872    }
8873
8874    /// GitHub accepts one value per field mutation; aliases combine those mutations in
8875    /// one request — writes in the order given, then clears. A lone write is
8876    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8877    /// request's slots hold go in further requests of the same document, in order. Every
8878    /// returned item id is checked, including optional aliases.
8879    async fn set_item_fields(
8880        &self,
8881        board: &str,
8882        item: &str,
8883        fields: &[(String, Value)],
8884        clears: &[&str],
8885    ) -> Result<(), SourceError> {
8886        match (fields, clears) {
8887            ([], []) => return Ok(()),
8888            ([(field, value)], []) => {
8889                return self.set_item_field(board, item, field, value.clone()).await;
8890            }
8891            ([], [field]) => {
8892                return self
8893                    .write_priority(
8894                        board,
8895                        item,
8896                        &PriorityWrite::Clear {
8897                            field: (*field).to_owned(),
8898                        },
8899                    )
8900                    .await;
8901            }
8902            _ => {}
8903        }
8904        // Any field of the request stands in for an input a slot leaves out, which GitHub
8905        // still requires to be well formed although it never runs it.
8906        let filler = fields
8907            .first()
8908            .map_or_else(|| clears[0], |(field, _)| field.as_str());
8909        let mut writes = fields.iter();
8910        let mut cleared = clears.iter();
8911        loop {
8912            let batch: Vec<&(String, Value)> =
8913                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8914            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8915            if batch.is_empty() && batch_clears.is_empty() {
8916                return Ok(());
8917            }
8918            let mut variables = serde_json::Map::new();
8919            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8920                let input = match batch.get(index) {
8921                    Some((field, value)) => {
8922                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8923                    }
8924                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8925                                   "value":{"text":""}}),
8926                };
8927                variables.insert(slot.variable.to_owned(), input);
8928                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8929            }
8930            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8931                let field = batch_clears.get(index).map_or(filler, |field| **field);
8932                variables.insert(
8933                    slot.variable.to_owned(),
8934                    json!({"projectId":board,"itemId":item,"fieldId":field}),
8935                );
8936                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8937            }
8938            let data = self
8939                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8940                .await?;
8941            let sent = FIELD_WRITE_SLOTS[..batch.len()]
8942                .iter()
8943                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8944            for slot in sent {
8945                let alias = slot.alias;
8946                let returned = data
8947                    .get(alias)
8948                    .and_then(|value| value.get("projectV2Item"))
8949                    .ok_or_else(|| SourceError::Malformed {
8950                        message: format!("GitHub field update {alias} returned no project item"),
8951                    })?;
8952                if required_str(returned, "id")? != item {
8953                    return Err(SourceError::Malformed {
8954                        message: format!(
8955                            "GitHub field update {alias} returned the wrong project item"
8956                        ),
8957                    });
8958                }
8959            }
8960        }
8961    }
8962
8963    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8964        let mut after: Option<String> = None;
8965        let mut ids = Vec::new();
8966        loop {
8967            let data = self
8968                .graphql(
8969                    graphql::ISSUE_DEPENDENCIES,
8970                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8971                )
8972                .await?;
8973            let connection =
8974                data.pointer("/node/blockedBy")
8975                    .ok_or_else(|| SourceError::Malformed {
8976                        message: "GitHub dependency response has no blockedBy connection".into(),
8977                    })?;
8978            ids.extend(
8979                connection
8980                    .get("nodes")
8981                    .and_then(Value::as_array)
8982                    .ok_or_else(|| SourceError::Malformed {
8983                        message: "GitHub dependency response nodes is not an array".into(),
8984                    })?
8985                    .iter()
8986                    .map(|value| required_str(value, "id").map(str::to_owned))
8987                    .collect::<Result<Vec<_>, _>>()?,
8988            );
8989            let next = next_cursor(connection)?;
8990            if let Some(next) = &next {
8991                validate_cursor_progress(after.as_deref(), &next.0)?;
8992            }
8993            after = next.map(|cursor| cursor.0);
8994            if after.is_none() {
8995                return Ok(ids);
8996            }
8997        }
8998    }
8999
9000    async fn dependencies(
9001        &self,
9002        id: &NativeId,
9003        near_kind: ItemKind,
9004        direction: Direction,
9005        page: &PageRequest,
9006    ) -> Result<Page<DependencyEdge>, SourceError> {
9007        validate_page(page)?;
9008        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
9009        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
9010        let recorded = recorded_offset(cursor, direction)?;
9011        // What this issue is blocked by, when a read of it by its own id in this command
9012        // already carried the whole connection — a copy reads the item it writes before it
9013        // reads its edges — and the page asked for is the whole of it, or the recorded tail
9014        // after it. Answered from that read, in the shape the dependency read answers in;
9015        // anything else is asked of GitHub.
9016        let carried = match direction {
9017            Direction::DependsOn => self
9018                .resolved_cache()?
9019                .get(id)
9020                .filter(|item| item.content_kind == ContentKind::Issue)
9021                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
9022            Direction::DependedOnBy => None,
9023        }
9024        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
9025        // Asked for even in the recorded phase, whose page reads nothing from the
9026        // connection: `__typename` is what says whether this item has a native
9027        // relationship at all, and that is what decides which far ends the reserved key is
9028        // allowed to hold.
9029        let data = match carried {
9030            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
9031                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
9032            None => {
9033                let after = if recorded.is_some() { None } else { cursor };
9034                let first = page.limit.min(MAX_PAGE_SIZE);
9035                match direction {
9036                    Direction::DependsOn => {
9037                        self.graphql(
9038                            graphql::ISSUE_DEPENDS_ON,
9039                            json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
9040                                   "number":self.project_number,
9041                                   "boardItems":BOARD_ITEMS_PAGE_SIZE}),
9042                        )
9043                        .await?
9044                    }
9045                    Direction::DependedOnBy => {
9046                        self.graphql(
9047                            graphql::ISSUE_DEPENDED_ON_BY,
9048                            json!({"id":id.0,"first":first,"after":after}),
9049                        )
9050                        .await?
9051                    }
9052                }
9053            }
9054        };
9055        let draft_slot = draft_slot(&data, id, self.project_number)?;
9056        let node =
9057            data.get("node")
9058                .filter(|v| !v.is_null())
9059                .ok_or_else(|| SourceError::Refused {
9060                    message: format!(
9061                        "GitHub item {} was not found or does not support dependencies",
9062                        id.0
9063                    ),
9064                })?;
9065        let connection_name = match direction {
9066            Direction::DependsOn => "blockedBy",
9067            Direction::DependedOnBy => "blocking",
9068        };
9069        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
9070        // named natively and the reserved key may hold any far end. An issue's connections
9071        // hold issues, and this source reads them at the near item's own level.
9072        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
9073        if let Some(offset) = recorded {
9074            return Ok(recorded_page(
9075                self.recorded_edges(
9076                    id,
9077                    near_kind,
9078                    direction,
9079                    natively_names,
9080                    node,
9081                    draft_slot.as_ref(),
9082                )
9083                .await?,
9084                offset,
9085                limit,
9086            ));
9087        }
9088        if natively_names.is_none() {
9089            return Ok(recorded_page(
9090                self.recorded_edges(
9091                    id,
9092                    near_kind,
9093                    direction,
9094                    natively_names,
9095                    node,
9096                    draft_slot.as_ref(),
9097                )
9098                .await?,
9099                0,
9100                limit,
9101            ));
9102        }
9103        let connection = node
9104            .get(connection_name)
9105            .ok_or_else(|| SourceError::Malformed {
9106                message: "GitHub dependency response is missing its connection".into(),
9107            })?;
9108        let nodes = connection
9109            .get("nodes")
9110            .and_then(Value::as_array)
9111            .ok_or_else(|| SourceError::Malformed {
9112                message: "GitHub dependency response nodes is not an array".into(),
9113            })?;
9114        // `from` depends on `to`, always. GitHub spells the same relationship from either
9115        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
9116        // it — so the near item is `from` in one direction and `to` in the other.
9117        let items = nodes
9118            .iter()
9119            .map(|value| {
9120                let related = NativeId(required_str(value, "id")?.into());
9121                let related_kind = related_kind(value)?;
9122                let (from, to) = match direction {
9123                    Direction::DependsOn => (
9124                        DependencyEndpoint::from_native(id.clone(), near_kind),
9125                        DependencyEndpoint::from_native(related, related_kind),
9126                    ),
9127                    Direction::DependedOnBy => (
9128                        DependencyEndpoint::from_native(related, related_kind),
9129                        DependencyEndpoint::from_native(id.clone(), near_kind),
9130                    ),
9131                };
9132                Ok(DependencyEdge {
9133                    from,
9134                    to,
9135                    kind: DependencyKind::Blocks,
9136                })
9137            })
9138            .collect::<Result<Vec<_>, SourceError>>()?;
9139        let mut next = next_cursor(connection)?;
9140        if let Some(next) = &next {
9141            validate_cursor_progress(cursor, &next.0)?;
9142        }
9143        // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
9144        // body they are recorded in, as far as the page has room — so they cost no request of
9145        // their own — and only what does not fit is resumed by a cursor.
9146        let mut items = items;
9147        if next.is_none() {
9148            let recorded = self
9149                .recorded_edges(
9150                    id,
9151                    near_kind,
9152                    direction,
9153                    natively_names,
9154                    node,
9155                    draft_slot.as_ref(),
9156                )
9157                .await?;
9158            let room = limit.saturating_sub(items.len());
9159            let total = recorded.len();
9160            items.extend(recorded.into_iter().take(room));
9161            if total > room {
9162                next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
9163            }
9164        }
9165        Ok(Page { items, next })
9166    }
9167
9168    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
9169    /// a far end in another source has to live: no GitHub issue relationship can name one.
9170    ///
9171    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
9172    /// source never writes one down.
9173    ///
9174    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
9175    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9176    /// request beyond the read already made, and reading the board for them would be a
9177    /// walk of every item for one field of one. A draft has no body in that answer, because
9178    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9179    /// listing of the board, which can be behind on the very item asked about.
9180    async fn recorded_edges(
9181        &self,
9182        id: &NativeId,
9183        near_kind: ItemKind,
9184        direction: Direction,
9185        natively_names: Option<ItemKind>,
9186        node: &Value,
9187        draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9188    ) -> Result<Vec<DependencyEdge>, SourceError> {
9189        if direction != Direction::DependsOn {
9190            return Ok(Vec::new());
9191        }
9192        let slot = match (node.get("body"), draft_slot) {
9193            (Some(body), _) if natively_names.is_some() => {
9194                metadata_body(body.as_str().map(str::to_owned))?.1
9195            }
9196            // A draft the dependency read itself answered: this board's, or none of its.
9197            (_, Some(Some(slot))) => slot.clone(),
9198            (_, Some(None)) => return Ok(Vec::new()),
9199            _ => {
9200                let Some(item) = self.bound_item(id).await? else {
9201                    return Ok(Vec::new());
9202                };
9203                item.slot
9204            }
9205        };
9206        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9207            .map_err(|message| SourceError::Malformed { message })
9208    }
9209
9210    /// Why `creation_owners` refuses a create filed in `target`, or `None` when it allows it —
9211    /// every create when the list is empty.
9212    fn owner_refusal(&self, target: &RepositoryTarget) -> Option<String> {
9213        if self.creation_owners.is_empty()
9214            || self
9215                .creation_owners
9216                .iter()
9217                .any(|owner| owner.eq_ignore_ascii_case(&target.owner))
9218        {
9219            return None;
9220        }
9221        Some(format!(
9222            "source {} would create the issue in repository {}, owned by {}, and its \
9223             creation_owners allows only repositories of {}",
9224            self.name,
9225            target.slug(),
9226            target.owner,
9227            self.creation_owners.join(", ")
9228        ))
9229    }
9230
9231    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9232        self.repository
9233            .as_ref()
9234            .ok_or_else(|| SourceError::Refused {
9235                message: format!(
9236                    "source {} has no repository configured, and a GitHub Projects board has no \
9237                 repository of its own to create an issue in; set repository: owner/name on \
9238                 this source",
9239                    self.name
9240                ),
9241            })
9242    }
9243
9244    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9245    /// states.
9246    ///
9247    /// The fallback is demanded first, whichever arm answers: a write without a configured
9248    /// repository is refused naming the field exactly as it was before the rule existed,
9249    /// so a source that could not write before cannot write now, rather than writing for
9250    /// the one item whose own field happens to decide it.
9251    ///
9252    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9253    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9254    /// entry owned by someone other than the owner of the parent issue's repository —
9255    /// GitHub accepts a sub-issue from another repository of the same owner and from no
9256    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9257    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9258    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9259    /// and is visible to the token is checked where its node id is resolved, still before
9260    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9261    /// looked up in a listing of the board, which can be minutes behind an issue its own
9262    /// `projectItems` already places on it — and that read answers first from this process's
9263    /// own record, so a project created moments ago in this command answers though GitHub
9264    /// has not caught up.
9265    async fn creation_target(
9266        &self,
9267        incoming: &Incoming<'_>,
9268    ) -> Result<RepositoryTarget, SourceError> {
9269        let what = format!(
9270            "{} {:?}",
9271            incoming.written.kind().describes(),
9272            incoming.title
9273        );
9274        let target = self
9275            .creation_rule(incoming.repositories, incoming.parent, &what)
9276            .await?;
9277        // The engine asks `creation_refusal` before anything of a copy is written; this holds
9278        // every other create to the same restriction, before `createIssue`.
9279        match self.owner_refusal(&target) {
9280            Some(message) => Err(SourceError::Refused { message }),
9281            None => Ok(target),
9282        }
9283    }
9284
9285    /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9286    /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9287    /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
9288    /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
9289    /// differently.
9290    ///
9291    /// On a source declared private, an item's one named repository is read for its
9292    /// visibility first — once per engine hold, see the `visibility` module — and one that
9293    /// does not read private decides nothing: the issue is created in the parent's
9294    /// repository, else the configured one. A read that cannot be answered refuses.
9295    async fn creation_rule(
9296        &self,
9297        repositories: &[Repository],
9298        parent: Option<&NativeId>,
9299        what: &str,
9300    ) -> Result<RepositoryTarget, SourceError> {
9301        let parent = match parent {
9302            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9303                SourceError::Refused {
9304                    message: format!(
9305                        "GitHub project issue {} was not found on the board of source {}, so {} \
9306                         cannot be filed under it",
9307                        parent.0, self.name, what
9308                    ),
9309                }
9310            })?),
9311            None => None,
9312        };
9313        let parents_repository = parent
9314            .as_ref()
9315            .map(|parent| {
9316                // A draft is on the board and so is found, but it has no repository to
9317                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9318                // would refuse the task only once `createIssue` had made it.
9319                if parent.content_kind == ContentKind::DraftIssue {
9320                    return Err(SourceError::Refused {
9321                        message: format!(
9322                            "GitHub project item {} on the board of source {} is a draft, \
9323                             which cannot have sub-issues, so {} cannot be filed under it",
9324                            parent.id.0, self.name, what
9325                        ),
9326                    });
9327                }
9328                // An issue's repository is where a sub-issue is placed and whose owner it
9329                // is compared against, so a parent whose repository this source cannot
9330                // spell as `owner/name` — GitHub's login grammar is wider than this
9331                // source's floor — is one nothing can be filed under.
9332                parent
9333                    .own_repository
9334                    .as_ref()
9335                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9336                    .ok_or_else(|| SourceError::Malformed {
9337                        message: format!(
9338                            "GitHub project issue {} on the board of source {} is in {}, which \
9339                             is not a {}/owner/name repository this source can place {} in",
9340                            parent.id.0,
9341                            self.name,
9342                            parent
9343                                .own_repository
9344                                .as_ref()
9345                                .map_or("no repository", Repository::as_str),
9346                            RepositoryTarget::HOST,
9347                            what
9348                        ),
9349                    })
9350            })
9351            .transpose()?;
9352        self.creation_rule_in_repository(repositories, parents_repository, what)
9353            .await
9354    }
9355
9356    async fn creation_rule_in_repository(
9357        &self,
9358        repositories: &[Repository],
9359        parents_repository: Option<RepositoryTarget>,
9360        what: &str,
9361    ) -> Result<RepositoryTarget, SourceError> {
9362        let fallback = self.configured_repository()?;
9363        match repositories {
9364            [named] => {
9365                let target =
9366                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9367                        message: format!(
9368                            "{} names repository {}, which is not a {}/owner/name repository \
9369                             source {} can create an issue in; name one that is, or name none",
9370                            what,
9371                            named.as_str(),
9372                            RepositoryTarget::HOST,
9373                            self.name
9374                        ),
9375                    })?;
9376                // On a source declared private, an issue whose one named repository does not
9377                // read private is created where an item naming none is: the item still names
9378                // that repository in its metadata slot, and its issue stays private.
9379                if self.is_declared_private()
9380                    && self.repository_visibility(&target).await?
9381                        != onetaskgraph_plugin_api::Visibility::Private
9382                {
9383                    return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
9384                }
9385                if let Some(parents) = &parents_repository
9386                    && parents.owner != target.owner
9387                {
9388                    return Err(SourceError::Refused {
9389                        message: format!(
9390                            "{} names repository {}, owned by {}, but its project's issue is in \
9391                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
9392                             of the same owner as its parent issue; name a repository of {}, or \
9393                             name none",
9394                            what,
9395                            target.slug(),
9396                            target.owner,
9397                            parents.slug(),
9398                            parents.owner,
9399                            parents.owner
9400                        ),
9401                    });
9402                }
9403                Ok(target)
9404            }
9405            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9406        }
9407    }
9408
9409    /// The node id of the repository `incoming` is being created in, or the refusal naming
9410    /// the item and the repository the token cannot see.
9411    ///
9412    /// Resolved once per command per repository; see [`Self::repository_cache`].
9413    async fn repository_id(
9414        &self,
9415        repository: &RepositoryTarget,
9416        incoming: &Incoming<'_>,
9417    ) -> Result<String, SourceError> {
9418        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9419            return Ok(id);
9420        }
9421        let data = self
9422            .graphql(
9423                graphql::REPOSITORY,
9424                json!({"owner":repository.owner,"name":repository.name}),
9425            )
9426            .await?;
9427        self.repository_read(&data, repository, incoming)
9428    }
9429
9430    /// The repository's node id out of an answer carrying the `repository` root, held for
9431    /// the rest of this command, or the refusal naming the item that cannot be created in it.
9432    fn repository_read(
9433        &self,
9434        data: &Value,
9435        repository: &RepositoryTarget,
9436        incoming: &Incoming<'_>,
9437    ) -> Result<String, SourceError> {
9438        let node = data
9439            .get("repository")
9440            .filter(|value| !value.is_null())
9441            .ok_or_else(|| SourceError::Refused {
9442                message: format!(
9443                    "GitHub repository {} was not found or is not visible to the token, so {} \
9444                     {:?} cannot be created in it",
9445                    repository.slug(),
9446                    incoming.written.kind().describes(),
9447                    incoming.title
9448                ),
9449            })?;
9450        let id = required_str(node, "id")?.to_owned();
9451        self.repository_cache()?
9452            .insert(repository.clone(), id.clone());
9453        Ok(id)
9454    }
9455
9456    fn repository_cache(
9457        &self,
9458    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9459        self.repository_cache
9460            .lock()
9461            .map_err(|_| SourceError::Unavailable {
9462                message: "this source's record of the destination repository was left \
9463                          inconsistent by an earlier failure; next: run the command again"
9464                    .into(),
9465            })
9466    }
9467
9468    /// Create or update one board item, whichever kind it is.
9469    async fn write_item(
9470        &self,
9471        incoming: &Incoming<'_>,
9472        target: Option<&NativeId>,
9473        depends_on: &[DependencyEdge],
9474    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9475        let prepared = self.prepare_write(incoming, target, depends_on).await?;
9476        let landed = match &prepared.destination {
9477            // Its content is written last, by `complete_write`, once everything else has landed.
9478            WriteDestination::Existing(item) => Landed {
9479                content_id: item.id.clone(),
9480                item_id: Some(item.item_id.clone()),
9481                url: item.url.clone(),
9482                number: item.number,
9483            },
9484            WriteDestination::New(target) => {
9485                if self.deferring()? {
9486                    self.create_filed_issue(
9487                        prepared.board.id.as_str(),
9488                        target,
9489                        incoming,
9490                        &prepared.body,
9491                    )
9492                    .await?
9493                } else {
9494                    self.create_and_file_issue(
9495                        prepared.board.id.as_str(),
9496                        target,
9497                        incoming,
9498                        &prepared.body,
9499                    )
9500                    .await?
9501                }
9502            }
9503        };
9504        self.complete_write(incoming, prepared, landed).await
9505    }
9506
9507    /// Everything one write of `incoming` reads and decides before it sends anything: what is
9508    /// there now, where a new item is created, the board's fields, and every value refused while
9509    /// nothing has been written — the item's body included.
9510    async fn prepare_write(
9511        &self,
9512        incoming: &Incoming<'_>,
9513        target: Option<&NativeId>,
9514        depends_on: &[DependencyEdge],
9515    ) -> Result<Prepared, SourceError> {
9516        // Refused before anything is read or written: a task or a project titled the way
9517        // this board spells a document would land as an issue this same source reads back
9518        // as a document, so the field this destination cannot carry is named rather than
9519        // written and silently reclassified.
9520        if let Written::Work(kind, _) = incoming.written
9521            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9522        {
9523            return Err(SourceError::Refused {
9524                message: format!(
9525                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9526                     spells a document, so it would read back as one rather than as a {}; \
9527                     retitle it, or copy it as a document",
9528                    kind.marker(),
9529                    self.name,
9530                    kind.marker()
9531                ),
9532            });
9533        }
9534        // The destination is read by its own id, and whether this board holds it is decided
9535        // by that read — its own `projectItems` — rather than by whether a listing of the
9536        // board happens to include it yet. See the module documentation.
9537        // An existing issue is never moved; a new one is created where the rule says — and
9538        // knowing where is what lets the board's fields and that repository's id be read
9539        // together, before anything below needs either.
9540        let destination = match target {
9541            Some(target) => {
9542                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9543                    || SourceError::Refused {
9544                        message: format!("GitHub destination item {} was not found", target.0),
9545                    },
9546                )?))
9547            }
9548            None => {
9549                let target = self.creation_target(incoming).await?;
9550                self.creation_context(&target, incoming).await?;
9551                WriteDestination::New(target)
9552            }
9553        };
9554        let existing = destination.existing();
9555        let board = self
9556            .fields_for(
9557                existing,
9558                incoming.metadata.contains_key(ORIGIN_KEY),
9559                incoming.written.status().is_some(),
9560                incoming
9561                    .priority
9562                    .is_some_and(|priority| priority != Priority::None),
9563            )
9564            .await?;
9565        let status_target = incoming
9566            .written
9567            .work_status()
9568            .map(|(kind, status)| self.resolved_target(kind, status.category))
9569            .transpose()?;
9570        let column = match (incoming.written.work_status(), status_target.as_ref()) {
9571            (Some((kind, status)), Some(target)) => {
9572                self.column_for(&board.fields, kind, status.category, target)?
9573            }
9574            _ => None,
9575        };
9576        // Resolved before anything is created, for the reason the column above is: a
9577        // priority this board has no option for is refused while nothing has been written.
9578        let priority_write = match incoming.priority {
9579            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9580            None => None,
9581        };
9582        // And for the reason the priority is: a value no projected field can hold, or a board
9583        // that cannot hold the field, is refused while nothing has been written.
9584        let (projections, projected) = self.projection_writes(
9585            &board.fields,
9586            existing.map(|item| &item.projected),
9587            incoming.metadata,
9588            None,
9589        )?;
9590        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9591        if content_kind == ContentKind::DraftIssue {
9592            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9593                (status_target.as_ref(), incoming.written.status())
9594            {
9595                return Err(self.closes_a_draft(status.category));
9596            }
9597            if incoming.parent.is_some() {
9598                return Err(SourceError::Refused {
9599                    message: "GitHub draft items cannot be a project's sub-issue".into(),
9600                });
9601            }
9602        }
9603        match existing {
9604            Some(item) if content_kind == ContentKind::Issue => {
9605                if item.labels != incoming.labels {
9606                    return Err(SourceError::Refused {
9607                        message: "GitHub issue labels differ from the labels being written".into(),
9608                    });
9609                }
9610            }
9611            _ => {
9612                if !incoming.labels.is_empty() {
9613                    return Err(SourceError::Refused {
9614                        message: "GitHub items created by this destination carry no labels".into(),
9615                    });
9616                }
9617            }
9618        }
9619
9620        // The repository the issue really lives in is what the slot below is written against,
9621        // so a single entry that is where the issue is created travels as no key at all, and
9622        // the read side derives it back from the issue.
9623        let own_repository = match &destination {
9624            WriteDestination::Existing(item) => item.own_repository.clone(),
9625            WriteDestination::New(target) => Some(
9626                Repository::try_from(target.origin())
9627                    .map_err(|message| SourceError::Config { message })?,
9628            ),
9629        };
9630        let (native, fallback) = self
9631            .partition_edges(
9632                incoming.written.kind(),
9633                content_kind,
9634                existing.and_then(|item| item.blocked_by.as_deref()),
9635                depends_on,
9636            )
9637            .await?;
9638        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9639        let content = match incoming.assets {
9640            Some(assets) => {
9641                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9642                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9643                    incoming.content.unwrap_or_default(),
9644                    &mut slot,
9645                    &uploads,
9646                );
9647                incoming.content.map(|_| rewritten)
9648            }
9649            None => incoming.content.map(str::to_owned),
9650        };
9651        let body = compose_body(content.as_deref(), &slot)?;
9652        // Read before anything is created, for the reason the field below is: a value
9653        // this destination cannot store has to refuse, and refusing after `createIssue`
9654        // would leave an issue behind that nothing asked for. The engine writes a
9655        // qualified id here; a caller handing this key anything else is told so rather
9656        // than having it silently stored as no origin at all.
9657        // 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.
9658        let origin = match incoming.metadata.get(ORIGIN_KEY) {
9659            None => "",
9660            Some(Value::String(origin)) => origin.as_str(),
9661            Some(other) => {
9662                return Err(SourceError::Refused {
9663                    message: format!(
9664                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9665                         is {other}"
9666                    ),
9667                });
9668            }
9669        };
9670        // Resolved before anything is created: a board that cannot carry the copy origin
9671        // has to refuse the write, and refusing it after `createIssue` would leave an
9672        // issue behind that nothing asked for.
9673        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9674            Some(field) => {
9675                if required_str(field, "__typename")? != "ProjectV2Field" {
9676                    return Err(SourceError::Refused {
9677                        message: format!(
9678                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9679                        ),
9680                    });
9681                }
9682                Some(required_str(field, "id")?.to_owned())
9683            }
9684            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9685                return Err(SourceError::Refused {
9686                    message: format!(
9687                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9688                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9689                         the board"
9690                    ),
9691                });
9692            }
9693            None => None,
9694        };
9695
9696        Ok(Prepared {
9697            origin: origin.to_owned(),
9698            destination,
9699            board,
9700            status_target,
9701            column,
9702            priority_write,
9703            projections,
9704            projected,
9705            content_kind,
9706            own_repository,
9707            native,
9708            slot,
9709            content,
9710            body,
9711            origin_field,
9712        })
9713    }
9714
9715    /// Everything one write does once the item it writes exists: its board fields, its
9716    /// relationships and — for an item that was already there — its content last, then what this
9717    /// process remembers of it.
9718    ///
9719    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9720    /// item this command created has its fields, its closing and its edges held for
9721    /// [`TaskSource::finish_writes`] rather than sent here.
9722    async fn complete_write(
9723        &self,
9724        incoming: &Incoming<'_>,
9725        prepared: Prepared,
9726        landed: Landed,
9727    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9728        let Prepared {
9729            destination,
9730            board,
9731            status_target,
9732            column,
9733            priority_write,
9734            projections,
9735            projected,
9736            content_kind,
9737            own_repository,
9738            native,
9739            slot,
9740            content,
9741            body,
9742            origin,
9743            origin_field,
9744        } = prepared;
9745        let existing = destination.existing();
9746        let origin = origin.as_str();
9747        let Landed {
9748            content_id,
9749            item_id,
9750            url,
9751            number,
9752        } = landed;
9753
9754        // Only what differs from the item as this write read it is sent: a priority the item
9755        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9756        // it since that read is left alone.
9757        let priority_write = priority_write.filter(|_| {
9758            !existing.is_some_and(|item| {
9759                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9760            })
9761        });
9762        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9763        let column = column
9764            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9765            .map(|(field, option, _)| (field, option));
9766        // Creating an item here is several calls — `createIssue`, which files it on the
9767        // board, then its board fields, the parent and the dependencies — and GitHub can fail
9768        // at any of them. Everything this source can refuse *before* the first of those is
9769        // already checked above, so what is left is GitHub itself failing part way. When it
9770        // does over an item this call created, the issue is taken back: a write that
9771        // refused must not leave an item behind that nobody asked for, and one that does
9772        // makes the retry create a second.
9773        // Whether the board-field write carrying a moved origin was answered as landing whole.
9774        // When it was refused, GitHub does not say which of its fields ran before the one that
9775        // failed, so the origin may or may not have moved.
9776        let mut origin_landed = false;
9777        // An item this command created while writes are held back has every part that can land
9778        // later held for `finish_writes` — its own create filed it on the board and under its
9779        // project already — and so does a later write of it in the same command.
9780        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9781        let landed = if holding {
9782            self.hold_back(
9783                &board.id,
9784                &content_id,
9785                existing,
9786                origin_field.as_deref(),
9787                origin,
9788                column,
9789                status_target.as_ref(),
9790                priority_write.as_ref(),
9791                &projections,
9792                &native,
9793            )
9794        } else if let Some(item_id) = item_id.as_deref() {
9795            self.finish_write(
9796                board.id.as_str(),
9797                incoming,
9798                &content_id,
9799                item_id,
9800                content_kind,
9801                existing,
9802                origin_field.as_deref(),
9803                origin,
9804                column,
9805                status_target.as_ref(),
9806                priority_write.as_ref(),
9807                &projections,
9808                &native,
9809                &mut origin_landed,
9810            )
9811            .await
9812        } else {
9813            // An issue a create filed on the board itself has no board item this process
9814            // knows until `finish_writes` reads it, so nothing but a held-back write may land
9815            // on it before then.
9816            Err(SourceError::Malformed {
9817                message: format!(
9818                    "issue {} was written before the board item GitHub filed it as was read",
9819                    content_id.0
9820                ),
9821            })
9822        };
9823        // An existing item's title, body and state go last, in one `updateIssue`, once its board
9824        // fields and its relationships have landed: a refusal of any of those then leaves its
9825        // body — and the metadata slot inside it — exactly as it stood.
9826        let landed = match (landed, existing) {
9827            (Ok(()), Some(item)) => {
9828                self.update_existing(item, incoming, &body, status_target.as_ref())
9829                    .await
9830            }
9831            (landed, _) => landed,
9832        };
9833        if let Err(error) = landed {
9834            match existing {
9835                // Best effort, and the write's own failure is what the caller is told: a
9836                // refusal naming the tidy-up would hide why the write failed at all.
9837                None => {
9838                    let _ = self.delete_issue(&content_id).await;
9839                }
9840                // The origin field is the one piece of an existing item's metadata written
9841                // before its body, so a write refused after it puts it back as it was. When
9842                // that is refused too, the write's own failure is still what the caller is
9843                // told — with what it left behind added, because the item's metadata is then
9844                // not as it stood and a caller retrying has to know which key moved.
9845                Some(item) => {
9846                    let before = item.origin.as_deref().unwrap_or("");
9847                    if let Some(field) = origin_field.as_deref()
9848                        && before != origin
9849                        && let Err(restore) = self
9850                            .set_item_field(
9851                                board.id.as_str(),
9852                                &item.item_id,
9853                                field,
9854                                json!({"text": before}),
9855                            )
9856                            .await
9857                    {
9858                        let left = if origin_landed {
9859                            format!(
9860                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9861                                 not be put back to {before:?} ({restore}), so item {} still \
9862                                 holds {origin:?} there",
9863                                item.id.0
9864                            )
9865                        } else {
9866                            format!(
9867                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9868                                 {origin:?}, GitHub does not say whether that part of it ran, \
9869                                 and putting it back to {before:?} was refused ({restore}), so \
9870                                 item {} holds {origin:?} or {before:?} there",
9871                                item.id.0
9872                            )
9873                        };
9874                        return Err(noting(
9875                            error,
9876                            &format!(
9877                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9878                                 run the write again"
9879                            ),
9880                        ));
9881                    }
9882                }
9883            }
9884            return Err(error);
9885        }
9886
9887        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9888            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9889                self.statuses
9890                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9891            }
9892            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9893                self.statuses
9894                    .status(kind, written_option.as_deref(), false, None)
9895            }
9896            (Some((_, status)), _) => status.clone(),
9897            (None, _) => Status {
9898                category: StatusCategory::Unknown,
9899                name: "Open".to_owned(),
9900            },
9901        };
9902
9903        // So the rest of this command reads what it just did rather than what the board
9904        // said before it. See `remember_written` for which half takes it.
9905        let remembered = Resolved {
9906            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9907            // and `learn_item` puts it here.
9908            // 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.
9909            item_id: item_id.unwrap_or_default(),
9910            id: content_id.clone(),
9911            content_kind,
9912            kind: incoming.written.kind(),
9913            title: incoming.title.to_owned(),
9914            // The visible half of the body this write composed, split back off it the
9915            // way a read splits it — so what this record reports is what a read of the
9916            // same issue reports, rather than the person's text with the metadata slot
9917            // still on the end of it.
9918            body: metadata_body(body.clone())?.0,
9919            raw_body: body.clone(),
9920            // A document has no status of its own; what it reads back as is whatever
9921            // the issue's own state says, which is what a re-read reports.
9922            status: written_status,
9923            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9924            priority: match incoming.priority {
9925                Some(priority) => HeldPriority::Read(priority),
9926                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9927                    item.priority.clone()
9928                }),
9929            },
9930            // What `state_input` asked for: closed for a terminal target, open for any other
9931            // status, and the issue's own state left as it was by a document write.
9932            closed: content_kind == ContentKind::Issue
9933                && match status_target.as_ref() {
9934                    Some(StatusTarget::Terminal(_, _)) => true,
9935                    Some(_) => false,
9936                    None => existing.is_some_and(|item| item.closed),
9937                },
9938            reason: match status_target.as_ref() {
9939                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9940                    Some(reason.reason().to_owned())
9941                }
9942                Some(_) => None,
9943                None => existing.and_then(|item| item.reason.clone()),
9944            },
9945            delivers: incoming.delivers.to_vec(),
9946            delivered_by: incoming.delivered_by.to_vec(),
9947            labels: incoming.labels.to_vec(),
9948            parent: incoming.parent.cloned(),
9949            origin: (!origin.is_empty()).then(|| origin.to_owned()),
9950            projected,
9951            number,
9952            // In the update path this is the item's own url, read off `existing` where the
9953            // record above was bound, so one expression serves both halves.
9954            url,
9955            created_at: existing.and_then(|item| item.created_at),
9956            updated_at: existing.and_then(|item| item.updated_at),
9957            // What this write moved is GitHub's to say, so a record of it gives no token until
9958            // it is read again.
9959            item_updated_at: None,
9960            comment_count: None,
9961            relationship_events: None,
9962            served_at: None,
9963            facets: None,
9964            own_repository,
9965            repositories: incoming.repositories.to_vec(),
9966            classification: incoming.classification,
9967            slot,
9968            board_id: Some(board.id.as_str().to_owned()),
9969            fields: board
9970                .fields
9971                .get("nodes")
9972                .and_then(Value::as_array)
9973                .cloned()
9974                .unwrap_or_default(),
9975            board_fields: Some(board.fields.clone()),
9976            // What this write left the relationship holding is known by id alone, and a
9977            // later read of its edges needs each far end's kind, so it reads them again.
9978            blocked_by: None,
9979        };
9980        self.remember_written(remembered, existing.is_none())?;
9981        Ok(onetaskgraph_plugin_api::AssetsWritten {
9982            id: content_id,
9983            content,
9984        })
9985    }
9986
9987    /// Everything a write does after the item exists: its board fields, its parent, and
9988    /// its dependencies.
9989    ///
9990    /// Split out of `write_item` so there is one place a failure past the point of no
9991    /// return is caught, rather than a tidy-up repeated at each `?` above.
9992    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9993    // so there is one place a failure past the point of no return is caught, and its
9994    // arguments are exactly the values that tail already had in scope. Bundling them into a
9995    // struct would describe no concept — it would be "the arguments of this function" — and
9996    // would put the whole of `write_item`'s locals behind one more indirection.
9997    #[allow(clippy::too_many_arguments)]
9998    async fn finish_write(
9999        &self,
10000        board_id: &str,
10001        incoming: &Incoming<'_>,
10002        content_id: &NativeId,
10003        item_id: &str,
10004        content_kind: ContentKind,
10005        existing: Option<&Resolved>,
10006        origin_field: Option<&str>,
10007        origin: &str,
10008        column: Option<(String, String)>,
10009        status_target: Option<&StatusTarget>,
10010        priority: Option<&PriorityWrite>,
10011        projections: &[ProjectionWrite],
10012        native: &[String],
10013        origin_landed: &mut bool,
10014    ) -> Result<(), SourceError> {
10015        let (fields, clears) = field_writes(
10016            existing,
10017            origin_field,
10018            origin,
10019            column,
10020            priority,
10021            projections,
10022        );
10023        self.set_item_fields(board_id, item_id, &fields, &clears)
10024            .await?;
10025        *origin_landed = true;
10026
10027        // An existing issue closes in the `updateIssue` its write ends with; one created just
10028        // now closes here, once its option is selected.
10029        if existing.is_none()
10030            && content_kind == ContentKind::Issue
10031            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
10032        {
10033            self.update_content(
10034                ContentKind::Issue,
10035                content_id,
10036                json!({"stateInput":state_input(status_target)}),
10037            )
10038            .await?;
10039        }
10040
10041        if content_kind == ContentKind::Issue {
10042            self.reparent(
10043                existing.and_then(|item| item.parent.clone()),
10044                content_id,
10045                incoming.parent,
10046            )
10047            .await?;
10048            // A document takes part in no dependency graph, so writing one neither reads
10049            // nor changes the issue's own `blockedBy` relationships. Reconciling them
10050            // against the empty list a document write carries would *delete* whatever
10051            // relationships a person had made on that issue, which is a write nobody
10052            // asked for.
10053            if incoming.written.kind() != BoardKind::Document {
10054                let issue = match existing {
10055                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
10056                    None => Issue::Created,
10057                };
10058                self.reconcile_blocked_by(content_id, native, issue).await?;
10059            }
10060        }
10061        Ok(())
10062    }
10063
10064    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
10065    fn deferring(&self) -> Result<bool, SourceError> {
10066        Ok(self.deferred()?.is_some())
10067    }
10068
10069    /// Whether `content` is an item this command created whose parts are held back.
10070    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
10071        Ok(self
10072            .deferred()?
10073            .as_ref()
10074            .is_some_and(|deferred| deferred.holds(content)))
10075    }
10076
10077    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
10078        self.deferred.lock().map_err(|_| SourceError::Unavailable {
10079            message: "this source's record of the writes it is holding back was left \
10080                      inconsistent by an earlier failure; next: run the command again"
10081                .into(),
10082        })
10083    }
10084
10085    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
10086    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
10087    /// creates with: one request where adding it to the board and filing it under its project
10088    /// were two more. GitHub adds the board item after answering, so its id is not known here;
10089    /// [`TaskSource::finish_writes`] reads it.
10090    async fn create_filed_issue(
10091        &self,
10092        board_id: &str,
10093        repository: &RepositoryTarget,
10094        incoming: &Incoming<'_>,
10095        body: &Option<String>,
10096    ) -> Result<Landed, SourceError> {
10097        let repository_id = self.repository_id(repository, incoming).await?;
10098        let data = self
10099            .graphql(
10100                graphql::CREATE_ISSUE,
10101                json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
10102            )
10103            .await?;
10104        let created = data
10105            .pointer("/createIssue/issue")
10106            .filter(|value| !value.is_null())
10107            .ok_or_else(|| SourceError::Malformed {
10108                message: "GitHub issue creation returned no issue".into(),
10109            })?;
10110        self.created_landed(created).await
10111    }
10112
10113    /// Keep the address `createIssue` answered for the issue `content`, for
10114    /// [`TaskSource::written_location`].
10115    fn note_written(&self, content: &NativeId, url: Option<&str>) {
10116        if let Some(url) = url {
10117            self.written
10118                .lock()
10119                .unwrap_or_else(std::sync::PoisonError::into_inner)
10120                .insert(content.clone(), url.to_owned());
10121        }
10122    }
10123
10124    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
10125    /// read, the refusal, after taking the issue back.
10126    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
10127        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
10128        let url = optional_str(created, "url")?.map(str::to_owned);
10129        let number = match created_issue_number(created) {
10130            Ok(number) => number,
10131            Err(error) => {
10132                let _ = self.delete_issue(&content_id).await;
10133                return Err(error);
10134            }
10135        };
10136        self.note_written(&content_id, url.as_deref());
10137        Ok(Landed {
10138            content_id,
10139            item_id: None,
10140            url,
10141            number,
10142        })
10143    }
10144
10145    /// Hold back everything a write of the created item `content` would send after creating it,
10146    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
10147    /// `finish_write` sends, the parent excepted, which its create already named. A later write
10148    /// of the same item replaces what it changes.
10149    // 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.
10150    #[allow(clippy::too_many_arguments)]
10151    fn hold_back(
10152        &self,
10153        board_id: &BoardId,
10154        content: &NativeId,
10155        existing: Option<&Resolved>,
10156        origin_field: Option<&str>,
10157        origin: &str,
10158        column: Option<(String, String)>,
10159        status_target: Option<&StatusTarget>,
10160        priority: Option<&PriorityWrite>,
10161        projections: &[ProjectionWrite],
10162        native: &[String],
10163    ) -> Result<(), SourceError> {
10164        let status = column.map(|(field, option)| {
10165            let name = self
10166                .fields_cache()
10167                .ok()
10168                .and_then(|fields| {
10169                    fields.as_ref().and_then(|board| {
10170                        option_name(&board.fields, &field, &option).map(str::to_owned)
10171                    })
10172                })
10173                .unwrap_or_default();
10174            (field, option, name)
10175        });
10176        let (fields, clears) =
10177            field_writes(existing, origin_field, origin, None, priority, projections);
10178        let close = match status_target {
10179            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
10180            _ => None,
10181        };
10182        let mut deferred = self.deferred()?;
10183        let deferred = deferred.get_or_insert_with(Deferred::default);
10184        if let Deferred::Nothing = deferred {
10185            *deferred = Deferred::Held {
10186                board: board_id.clone(),
10187                created: Vec::new(),
10188            };
10189        }
10190        let Deferred::Held { created, .. } = deferred else {
10191            unreachable!("held back on the line above");
10192        };
10193        let held = match created.iter().position(|held| held.content == *content) {
10194            Some(at) => &mut created[at],
10195            None => {
10196                created.push(HeldBack {
10197                    content: content.clone(),
10198                    fields: Vec::new(),
10199                    status: None,
10200                    clears: Vec::new(),
10201                    close: None,
10202                    edges: Vec::new(),
10203                });
10204                created.last_mut().expect("just held")
10205            }
10206        };
10207        for (field, value) in fields {
10208            held.fields.retain(|(held, _)| *held != field);
10209            held.clears.retain(|held| *held != field);
10210            held.fields.push((field, value));
10211        }
10212        for field in clears {
10213            held.fields.retain(|(held, _)| held != field);
10214            if !held.clears.iter().any(|held| held == field) {
10215                held.clears.push(field.to_owned());
10216            }
10217        }
10218        if status.is_some() {
10219            held.status = status;
10220        }
10221        if close.is_some() {
10222            held.close = close;
10223        }
10224        // Nothing of an item created in this command has been sent but its create, so the edges
10225        // it is to have are exactly the ones its latest write names.
10226        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
10227        Ok(())
10228    }
10229
10230    /// Create every task of `writes`, together, filed on the board and under each one's
10231    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10232    ///
10233    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10234    /// before the first request — then created, then completed with its parts held back. A
10235    /// request refused part way leaves the issues it did create, which are taken back, with the
10236    /// rest of this call's, before the refusal is answered.
10237    async fn create_together(
10238        &self,
10239        writes: &[ItemWrite<Task>],
10240    ) -> Result<Vec<NativeId>, SourceError> {
10241        let incomings: Vec<Incoming<'_>> = writes
10242            .iter()
10243            .map(|write| self.task_incoming(write, None))
10244            .collect::<Result<_, _>>()?;
10245        let mut prepared = Vec::with_capacity(writes.len());
10246        for (incoming, write) in incomings.iter().zip(writes) {
10247            if write.target.is_some() {
10248                return Err(SourceError::Malformed {
10249                    message: "a write naming its target was handed to a batch of creates".into(),
10250                });
10251            }
10252            prepared.push(
10253                self.prepare_write(incoming, None, &write.depends_on)
10254                    .await?,
10255            );
10256        }
10257        let mut inputs = Vec::with_capacity(prepared.len());
10258        for (incoming, prepared) in incomings.iter().zip(&prepared) {
10259            let WriteDestination::New(target) = &prepared.destination else {
10260                return Err(SourceError::Malformed {
10261                    message: "an update was decided for a write handed to a batch of creates"
10262                        .into(),
10263                });
10264            };
10265            let repository_id = self.repository_id(target, incoming).await?;
10266            inputs.push(filed_input(
10267                &repository_id,
10268                prepared.board.id.as_str(),
10269                incoming,
10270                &prepared.body,
10271            ));
10272        }
10273        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10274        let refused = 'create: {
10275            for chunk in inputs.chunks(MUTATION_BATCH) {
10276                let mut variables = serde_json::Map::new();
10277                for slot in 0..MUTATION_BATCH {
10278                    variables.insert(
10279                        format!("create{slot}"),
10280                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10281                    );
10282                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10283                }
10284                let data = match self
10285                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10286                    .await
10287                {
10288                    Ok(data) => data,
10289                    Err(error) => break 'create Some(error),
10290                };
10291                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10292                    let said: Vec<&str> = errors
10293                        .as_array()
10294                        .into_iter()
10295                        .flatten()
10296                        .filter_map(|error| error.get("message").and_then(Value::as_str))
10297                        .collect();
10298                    SourceError::Refused {
10299                        message: if said.is_empty() {
10300                            "GitHub returned GraphQL errors".to_owned()
10301                        } else {
10302                            said.join("; ")
10303                        },
10304                    }
10305                });
10306                for slot in 0..chunk.len() {
10307                    match data
10308                        .pointer(&format!("/create{slot}/issue"))
10309                        .filter(|value| !value.is_null())
10310                    {
10311                        Some(created) => match self.created_landed(created).await {
10312                            Ok(created) => landed.push(created),
10313                            Err(error) => {
10314                                refusal.get_or_insert(error);
10315                            }
10316                        },
10317                        // An alias GitHub refused answers nothing, and the refusal says why.
10318                        None => {
10319                            refusal.get_or_insert(SourceError::Malformed {
10320                                message: "GitHub issue creation returned no issue".into(),
10321                            });
10322                        }
10323                    }
10324                }
10325                if let Some(error) = refusal {
10326                    break 'create Some(error);
10327                }
10328            }
10329            None
10330        };
10331        if let Some(error) = refused {
10332            // None of these ids reaches the caller, so an issue GitHub will not take back is
10333            // named here or by nobody.
10334            let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10335            let left = self.delete_issues(&ids).await;
10336            let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10337            return Err(left_behind(error, &left));
10338        }
10339        let created: Vec<NativeId> = landed
10340            .iter()
10341            .map(|landed| landed.content_id.clone())
10342            .collect();
10343        let mut ids = Vec::with_capacity(landed.len());
10344        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10345            match self.complete_write(incoming, prepared, landed).await {
10346                Ok(written) => ids.push(written.id),
10347                // 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.
10348                Err(error) => {
10349                    let ids: Vec<&NativeId> = created.iter().collect();
10350                    let left = self.delete_issues(&ids).await;
10351                    let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10352                    return Err(left_behind(error, &left));
10353                }
10354            }
10355        }
10356        Ok(ids)
10357    }
10358
10359    /// Hold back the update of the existing issue `issue` — `members`, what its own
10360    /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10361    /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10362    /// as the mutation itself would drop it. Answers whether it was held: never while a write
10363    /// the store's cache expects runs, which records each mutation's own answer, and never for
10364    /// an issue an update is already held for, which that one is sent ahead of.
10365    fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10366        if self.expected.journaling() || self.deferred()?.is_none() {
10367            return Ok(false);
10368        }
10369        let mut held = self
10370            .held_updates
10371            .lock()
10372            .map_err(|_| SourceError::Unavailable {
10373                message: "issue updates held back were left inconsistent; run the command again"
10374                    .into(),
10375            })?;
10376        if held.iter().any(|earlier| earlier.issue == *issue) {
10377            return Ok(false);
10378        }
10379        let named = |value: &str| issue.0 == value;
10380        self.resolved_cache()?
10381            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10382        self.read_ahead()?.retain(|id, item| {
10383            !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10384        });
10385        self.carried()?
10386            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10387        held.push(HeldUpdate::new(issue, members));
10388        Ok(true)
10389    }
10390
10391    /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10392    /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10393    /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10394    async fn send_held_updates(&self) -> Result<(), SourceError> {
10395        let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10396            SourceError::Unavailable {
10397                message: "issue updates held back were left inconsistent; run the command again"
10398                    .into(),
10399            }
10400        })?);
10401        if let [one] = held.as_slice() {
10402            return self
10403                .send_update(
10404                    graphql::UPDATE_ISSUE,
10405                    "/updateIssue/issue",
10406                    &one.issue,
10407                    one.input.clone(),
10408                )
10409                .await;
10410        }
10411        for chunk in held.chunks(MUTATION_BATCH) {
10412            let mut variables = serde_json::Map::new();
10413            for slot in 0..MUTATION_BATCH {
10414                variables.insert(
10415                    format!("update{slot}"),
10416                    chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10417                );
10418                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10419            }
10420            let data = self
10421                .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10422                .await?;
10423            let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10424            check_aliased_answers(
10425                graphql::UPDATE_ISSUES,
10426                &data,
10427                "update",
10428                &inputs,
10429                &[("issue", "id")],
10430            )?;
10431        }
10432        Ok(())
10433    }
10434
10435    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10436    /// created items' board items, then their field values and edges together in requests of
10437    /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10438    /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10439    async fn send_held_back(&self) -> Result<(), SourceError> {
10440        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10441            return Ok(());
10442        };
10443        if created.is_empty() {
10444            return Ok(());
10445        }
10446        let sent = self.send_deferred(board.as_str(), &created).await;
10447        if let Err(error) = sent {
10448            // Best effort, and the failure is what the caller is told, as a write of one item
10449            // that fails part way takes back what it created: the copy that held these back
10450            // journalled every one, so one this cannot take back its undo tries again, and says
10451            // so when it cannot either.
10452            let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10453            let _ = self.delete_issues(&ids).await;
10454            return Err(error);
10455        }
10456        Ok(())
10457    }
10458
10459    /// The requests [`Self::send_held_back`] makes.
10460    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10461        let items = self.created_board_items(board, created).await?;
10462        let mut fields: Vec<Value> = Vec::new();
10463        let mut closes: Vec<Value> = Vec::new();
10464        let mut edges: Vec<Value> = Vec::new();
10465        for held in created {
10466            let (item, status) = &items[&held.content];
10467            let mut values = held.fields.clone();
10468            // A status the board already shows — set by a workflow a person turned on to file
10469            // new items at it — is not written again.
10470            if let Some((field, option, name)) = &held.status
10471                && status.as_deref() != Some(name.as_str())
10472            {
10473                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10474            }
10475            for (field, value) in values {
10476                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10477            }
10478            if let Some(reason) = held.close {
10479                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10480            }
10481            for far in &held.edges {
10482                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10483            }
10484            if !held.clears.is_empty() {
10485                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10486                self.set_item_fields(board, item, &[], &clears).await?;
10487            }
10488        }
10489        // Each alias answers with what it wrote, which is what its input named: the board item a
10490        // field value is set on, the issue closed, the issue blocked and the one blocking it.
10491        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10492        let close_answer: &[(&str, &str)] = &[("issue", "id")];
10493        let block_answer: &[(&str, &str)] =
10494            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10495        // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10496        // request: a request holding both kinds is the one document carrying both, so a plan's
10497        // values and edges cost one request where they fit in one.
10498        let writes: Vec<HeldWrite<'_>> = fields
10499            .iter()
10500            .map(HeldWrite::Field)
10501            .chain(edges.iter().map(HeldWrite::Edge))
10502            .collect();
10503        for chunk in writes.chunks(WRITE_BATCH) {
10504            let values: Vec<&Value> = chunk
10505                .iter()
10506                .filter_map(|write| match write {
10507                    HeldWrite::Field(input) => Some(*input),
10508                    HeldWrite::Edge(_) => None,
10509                })
10510                .collect();
10511            let blocks: Vec<&Value> = chunk
10512                .iter()
10513                .filter_map(|write| match write {
10514                    HeldWrite::Edge(input) => Some(*input),
10515                    HeldWrite::Field(_) => None,
10516                })
10517                .collect();
10518            let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10519                (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10520                (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10521                _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10522            };
10523            let mut variables = serde_json::Map::new();
10524            let kinds = [
10525                ("field", &values, sends[0], field_answer),
10526                ("block", &blocks, sends[1], block_answer),
10527            ];
10528            for (prefix, inputs, send, _) in &kinds {
10529                if inputs.is_empty() {
10530                    continue;
10531                }
10532                for slot in 0..WRITE_BATCH {
10533                    variables.insert(
10534                        format!("{prefix}{slot}"),
10535                        (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10536                    );
10537                    variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10538                }
10539            }
10540            let data = self.graphql(document, Value::Object(variables)).await?;
10541            for (prefix, inputs, _, answers) in kinds {
10542                check_aliased_answers(document, &data, prefix, inputs, answers)?;
10543            }
10544        }
10545        for chunk in closes.chunks(MUTATION_BATCH) {
10546            let mut variables = serde_json::Map::new();
10547            for slot in 0..MUTATION_BATCH {
10548                variables.insert(
10549                    format!("close{slot}"),
10550                    chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10551                );
10552                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10553            }
10554            let data = self
10555                .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10556                .await?;
10557            let inputs: Vec<&Value> = chunk.iter().collect();
10558            check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10559        }
10560        for held in created {
10561            self.learn_item(&held.content, &items[&held.content].0)?;
10562        }
10563        Ok(())
10564    }
10565
10566    /// The board item each of `created` is on this board, with its `Status` as the board set it:
10567    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10568    /// eight and sixteen — for only those GitHub has not filed yet.
10569    ///
10570    /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10571    /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10572    /// create answer carrying the item — so only a read says it is there. One still absent once
10573    /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10574    /// filing it meanwhile refuses that "Content already exists in this project", which says it
10575    /// is placed, and one more read finds the item rather than a second one being made.
10576    async fn created_board_items(
10577        &self,
10578        board: &str,
10579        created: &[HeldBack],
10580    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10581        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10582        let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10583            created
10584                .iter()
10585                .map(|held| held.content.clone())
10586                .filter(|id| !found.contains_key(id))
10587                .collect()
10588        };
10589        let mut wait = Duration::from_secs(1);
10590        for attempt in 0..6 {
10591            let absent = unfiled(&found);
10592            if absent.is_empty() {
10593                return Ok(found);
10594            }
10595            if attempt > 0 {
10596                self.clock
10597                    .sleep_because(wait, pacing::cause::BOARD_FILING)
10598                    .await;
10599                wait = wait.saturating_mul(2);
10600            }
10601            self.read_created_items(board, &absent, &mut found).await?;
10602        }
10603        let mut placed_meanwhile = Vec::new();
10604        for id in unfiled(&found) {
10605            match self
10606                .graphql(
10607                    graphql::ADD_TO_BOARD,
10608                    json!({"input":{"projectId":board,"contentId":id.0}}),
10609                )
10610                .await
10611            {
10612                Ok(added) => {
10613                    let item = added
10614                        .pointer("/addProjectV2ItemById/item")
10615                        .filter(|value| !value.is_null())
10616                        .ok_or_else(|| SourceError::Malformed {
10617                            message: "GitHub board addition returned no project item".into(),
10618                        })?;
10619                    found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10620                }
10621                Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10622                Err(error) => return Err(error),
10623            }
10624        }
10625        if !placed_meanwhile.is_empty() {
10626            self.read_created_items(board, &placed_meanwhile, &mut found)
10627                .await?;
10628        }
10629        let absent = unfiled(&found);
10630        if absent.is_empty() {
10631            return Ok(found);
10632        }
10633        let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10634        Err(SourceError::Unavailable {
10635            message: format!(
10636                "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10637                 to it, still answers {} on no item of it: {}; next: run the copy again once \
10638                 GitHub has caught up",
10639                if absent.len() == 1 {
10640                    "an issue"
10641                } else {
10642                    "issues"
10643                },
10644                if absent.len() == 1 { "it" } else { "them" },
10645                if absent.len() == 1 { "it" } else { "them" },
10646                absent.join(", ")
10647            ),
10648        })
10649    }
10650
10651    /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10652    /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10653    async fn read_created_items(
10654        &self,
10655        board: &str,
10656        ids: &[NativeId],
10657        found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10658    ) -> Result<(), SourceError> {
10659        for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10660            let mut variables = serde_json::Map::new();
10661            for slot in 0..COPY_DETAIL_BATCH {
10662                variables.insert(
10663                    format!("id{slot}"),
10664                    json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10665                );
10666                variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10667            }
10668            variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10669            variables.insert("statusName".into(), json!(STATUS_FIELD));
10670            let data = self
10671                .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10672                .await?;
10673            for (slot, id) in chunk.iter().enumerate() {
10674                // An answer for another issue than the one asked about says nothing of
10675                // where that one is filed, and its board item is not this one's.
10676                let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10677                let named = node.map(|node| node.get("id").and_then(Value::as_str));
10678                if let Some(named) = named
10679                    && named != Some(id.0.as_str())
10680                {
10681                    return Err(SourceError::Malformed {
10682                        message: format!(
10683                            "GitHub answered the board items of {} when asked for those of {}",
10684                            named.unwrap_or("an issue it did not name"),
10685                            id.0
10686                        ),
10687                    });
10688                }
10689                // An issue GitHub does not answer yet is not filed yet; one it answers is
10690                // read whole, its connection, each board item's board and its status, and
10691                // any of them unreadable is refused rather than read as not filed.
10692                let Some(node) = node else { continue };
10693                let malformed = |what: &str| SourceError::Malformed {
10694                    message: format!("GitHub answered the board items of {} with {what}", id.0),
10695                };
10696                let nodes = node
10697                    .pointer("/projectItems/nodes")
10698                    .and_then(Value::as_array)
10699                    .ok_or_else(|| malformed("no list of board items"))?;
10700                let mut item = None;
10701                for node in nodes {
10702                    let on = node
10703                        .pointer("/project/id")
10704                        .and_then(Value::as_str)
10705                        .ok_or_else(|| malformed("a board item naming no board"))?;
10706                    if on == board {
10707                        item = Some(node);
10708                    }
10709                }
10710                if let Some(item) = item {
10711                    let status = match item.get("status") {
10712                        None | Some(Value::Null) => None,
10713                        Some(status) => Some(
10714                            status
10715                                .get("name")
10716                                .and_then(Value::as_str)
10717                                .ok_or_else(|| malformed("a status that names no option"))?
10718                                .to_owned(),
10719                        ),
10720                    };
10721                    found.insert(
10722                        id.clone(),
10723                        (required_nonblank_str(item, "id")?.to_owned(), status),
10724                    );
10725                }
10726            }
10727        }
10728        Ok(())
10729    }
10730
10731    /// The board item id of `content`, which this process created and remembered without one,
10732    /// in every half of its own view.
10733    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10734        if let Some(held) = self.resolved_cache()?.get_mut(content) {
10735            held.item_id = item.to_owned();
10736        }
10737        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10738            held.item_id = item.to_owned();
10739        }
10740        Ok(())
10741    }
10742
10743    /// Ask together which items each of `origins` names, and what creating an item reads; see
10744    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10745    /// page is left to its own lookup.
10746    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10747        if self.repository.is_none() {
10748            return Ok(());
10749        }
10750        let unasked: Vec<&String> = {
10751            let held = self.narrowed_cache()?;
10752            let mut unasked: Vec<&String> = Vec::new();
10753            for origin in origins {
10754                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10755                    && !unasked.contains(&origin)
10756                {
10757                    unasked.push(origin);
10758                }
10759            }
10760            unasked
10761        };
10762        if unasked.is_empty() {
10763            return Ok(());
10764        }
10765        // One batch is held back for the read ahead that follows, which may carry a project's
10766        // members beside it in the same request.
10767        if unasked.len() <= ORIGIN_BATCH {
10768            let mut pending =
10769                self.pending_origins
10770                    .lock()
10771                    .map_err(|_| SourceError::Unavailable {
10772                        message:
10773                            "origins held for a read ahead were left inconsistent; run the command \
10774                          again"
10775                                .into(),
10776                    })?;
10777            for origin in unasked {
10778                if !pending.contains(origin) {
10779                    pending.push(origin.clone());
10780                }
10781            }
10782            return Ok(());
10783        }
10784        let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10785        self.send_origin_lookups(&unasked, None).await
10786    }
10787
10788    /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10789    /// classification and repositories as the members page `data` carries them — when that page
10790    /// is every member, each one names `project` as its parent, and every record it holds is
10791    /// readable. Any other page holds nothing, so the members are read as they always were.
10792    fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10793        let Some(page) = data
10794            .pointer("/members/subIssues")
10795            .filter(|page| !page.is_null())
10796        else {
10797            return Ok(());
10798        };
10799        if page
10800            .pointer("/pageInfo/hasNextPage")
10801            .and_then(Value::as_bool)
10802            != Some(false)
10803        {
10804            return Ok(());
10805        }
10806        let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10807            return Ok(());
10808        };
10809        let mut members = Vec::with_capacity(nodes.len());
10810        for node in nodes {
10811            if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10812                return Ok(());
10813            }
10814            let Some(body) = node.get("body").and_then(Value::as_str) else {
10815                return Ok(());
10816            };
10817            // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10818            // tasks whatever its marker says, so each is a member the full read would classify.
10819            let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10820                return Ok(());
10821            };
10822            let Ok(classification) = Classification::from_metadata(&slot) else {
10823                return Ok(());
10824            };
10825            let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10826                let Ok(repositories) = Repository::from_metadata(&slot) else {
10827                    return Ok(());
10828                };
10829                repositories
10830            } else {
10831                let Some(origin) = node
10832                    .pointer("/repository/nameWithOwner")
10833                    .and_then(Value::as_str)
10834                else {
10835                    return Ok(());
10836                };
10837                let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10838                else {
10839                    return Ok(());
10840                };
10841                vec![own]
10842            };
10843            members.push((classification, repositories));
10844        }
10845        self.member_classes
10846            .lock()
10847            .map_err(|_| SourceError::Unavailable {
10848                message: "a project's members held for its classification were left \
10849                          inconsistent; run the command again"
10850                    .into(),
10851            })?
10852            .insert(project.clone(), members);
10853        Ok(())
10854    }
10855
10856    /// Send every origin held back for the read ahead, beside the members of `members` when it
10857    /// names a project — or nothing, when none is held.
10858    async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10859        let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10860            SourceError::Unavailable {
10861                message: "origins held for a read ahead were left inconsistent; run the command \
10862                          again"
10863                    .into(),
10864            }
10865        })?);
10866        if pending.is_empty() {
10867            return Ok(());
10868        }
10869        self.send_origin_lookups(&pending, members).await
10870    }
10871
10872    /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10873    /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10874    /// a project — holding each origin's answer and, from a members page that is the whole of
10875    /// them, each member's classification.
10876    async fn send_origin_lookups(
10877        &self,
10878        unasked: &[String],
10879        members: Option<&NativeId>,
10880    ) -> Result<(), SourceError> {
10881        let Some(repository) = self.repository.clone() else {
10882            return Ok(());
10883        };
10884        let mut members = members;
10885        for chunk in unasked.chunks(ORIGIN_BATCH) {
10886            let mut variables = json!({"owner":self.owner,"number":self.project_number,
10887                "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10888                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10889                "repositoryOwner":repository.owner,"repositoryName":repository.name});
10890            for slot in 0..ORIGIN_BATCH {
10891                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10892                variables[format!("filter{slot}")] =
10893                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10894                variables[format!("search{slot}")] = json!(
10895                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10896                );
10897                variables[format!("ask{slot}")] = json!(slot < chunk.len());
10898            }
10899            let project = members.take();
10900            let document = match project {
10901                Some(project) => {
10902                    variables["project"] = json!(project.0);
10903                    variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10904                    graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10905                }
10906                None => graphql::ORIGIN_LOOKUPS,
10907            };
10908            let data = self.graphql(document, variables).await?;
10909            self.fields_read(&data)?;
10910            if let Some(project) = project {
10911                self.hold_member_classes(project, &data)?;
10912            }
10913            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10914                let id = required_nonblank_str(node, "id")?.to_owned();
10915                self.repository_cache()?.insert(repository.clone(), id);
10916                if let Some(numeric) = node
10917                    .get("databaseId")
10918                    .and_then(Value::as_u64)
10919                    .and_then(std::num::NonZeroU64::new)
10920                {
10921                    self.numeric_repositories
10922                        .lock()
10923                        .await
10924                        .insert(repository.clone(), numeric);
10925                }
10926            }
10927            for (slot, origin) in chunk.iter().enumerate() {
10928                let items = data
10929                    .pointer(&format!("/o{slot}/projectV2/items"))
10930                    .filter(|value| !value.is_null())
10931                    .ok_or_else(|| SourceError::Refused {
10932                        message: format!(
10933                            "GitHub project {}/{} was not found or is not visible to the token",
10934                            self.owner, self.project_number
10935                        ),
10936                    })?;
10937                let searched =
10938                    data.get(format!("s{slot}"))
10939                        .ok_or_else(|| SourceError::Malformed {
10940                            message: "GitHub search response has no search connection".into(),
10941                        })?;
10942                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10943                    continue;
10944                }
10945                let mut found: Vec<Resolved> = Vec::new();
10946                for item in optional_nodes(Some(items), "project items")?
10947                    .into_iter()
10948                    .flatten()
10949                {
10950                    if let Some(resolved) = self.resolve(item)?
10951                        && resolved.content_kind == ContentKind::Issue
10952                        && !found.iter().any(|held| held.id == resolved.id)
10953                    {
10954                        found.push(resolved);
10955                    }
10956                }
10957                for node in optional_nodes(Some(searched), "search")?
10958                    .into_iter()
10959                    .flatten()
10960                {
10961                    if let Some(resolved) = self.resolve_issue(node).await?
10962                        && !found.iter().any(|held| held.id == resolved.id)
10963                    {
10964                        found.push(resolved);
10965                    }
10966                }
10967                self.narrowed_cache()?
10968                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
10969            }
10970        }
10971        Ok(())
10972    }
10973
10974    /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
10975    /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
10976    /// confirm, every one of a request refused whole included.
10977    async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
10978        let mut left = Vec::new();
10979        for chunk in ids.chunks(DELETE_BATCH) {
10980            let mut variables = serde_json::Map::new();
10981            for slot in 0..DELETE_BATCH {
10982                let id = chunk.get(slot).unwrap_or(&chunk[0]);
10983                variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
10984                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10985            }
10986            let data = self
10987                .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
10988                .await
10989                .unwrap_or(Value::Null);
10990            for (slot, id) in chunk.iter().enumerate() {
10991                // Confirmed only by the repository the deleted issue was in, named by a
10992                // non-blank id: anything else says nothing of whether the issue is gone.
10993                let confirmed = data
10994                    .pointer(&format!("/delete{slot}/repository/id"))
10995                    .and_then(Value::as_str)
10996                    .is_some_and(|id| !id.trim().is_empty());
10997                if !confirmed {
10998                    left.push((*id).clone());
10999                }
11000            }
11001        }
11002        left
11003    }
11004
11005    /// Delete one issue, which takes its board item with it.
11006    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
11007        let data = self
11008            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11009            .await?;
11010        data.pointer("/deleteIssue/repository")
11011            .filter(|value| !value.is_null())
11012            .ok_or_else(|| SourceError::Malformed {
11013                message: "GitHub issue deletion returned no repository".into(),
11014            })?;
11015        self.forget(id)?;
11016        Ok(())
11017    }
11018
11019    /// Remove one item this copy created, so a copy that could not finish leaves the board
11020    /// as it found it.
11021    ///
11022    /// Deleting the issue takes its board item with it, so there is no second mutation to
11023    /// keep in step. An id the board does not hold is not an error: the item is already
11024    /// gone, which is the state this asks for. Which that is, is decided by reading the item
11025    /// by its own id — a listing of the board can still be missing an item it holds, and
11026    /// reading that as *already gone* would leave behind the very item this was asked to
11027    /// take back.
11028    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
11029        let Some(item) = self.bound_item(id).await? else {
11030            return Ok(());
11031        };
11032        if item.content_kind == ContentKind::DraftIssue {
11033            return Err(SourceError::Refused {
11034                message: format!(
11035                    "GitHub item {} is a draft, and this source removes an item by deleting \
11036                     its issue; next: remove it from the board by hand",
11037                    id.0
11038                ),
11039            });
11040        }
11041        let data = self
11042            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11043            .await?;
11044        data.pointer("/deleteIssue/repository")
11045            .filter(|value| !value.is_null())
11046            .ok_or_else(|| SourceError::Malformed {
11047                message: "GitHub issue deletion returned no repository".into(),
11048            })?;
11049        self.forget(id)?;
11050        Ok(())
11051    }
11052
11053    /// The issue a comment call on `task` is about, or `None` when this board holds no such
11054    /// task.
11055    ///
11056    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
11057    /// read of the task cannot disagree about which ids name one: a project or a document of
11058    /// this board is not a task here either.
11059    ///
11060    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
11061    /// issues and a draft is not one. It is refused rather than answered with an empty page,
11062    /// which would read as a task nobody has commented on yet.
11063    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
11064        let cached = self.resolved_cache()?.get(task).cloned();
11065        let Some(item) = (match cached {
11066            Some(item) => Some(item),
11067            None => self.item_by_id(task).await?,
11068        })
11069        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11070            return Ok(None);
11071        };
11072        if item.content_kind == ContentKind::DraftIssue {
11073            return Err(self.draft_has_no_comments(task));
11074        }
11075        Ok(Some(item.id))
11076    }
11077
11078    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
11079    /// issues, and a draft is not one.
11080    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
11081        SourceError::Refused {
11082            message: format!(
11083                "task {} of source {} is a draft item on the board, and GitHub keeps \
11084                 comments on issues alone, so a draft has none to read or write; next: \
11085                 convert the draft to an issue on the board, then comment on the issue it \
11086                 becomes",
11087                task.0, self.name
11088            ),
11089        }
11090    }
11091
11092    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
11093    /// request — or `None` when this board holds no task by that id.
11094    ///
11095    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
11096    /// is answered with the draft and the refusal, at the price of the draft's own read.
11097    async fn issue_detail(
11098        &self,
11099        id: &NativeId,
11100        page: &PageRequest,
11101    ) -> Result<Option<TaskDetailRead>, SourceError> {
11102        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
11103        let asked = self
11104            .graphql(
11105                graphql::ISSUE_DETAIL,
11106                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
11107                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
11108                       "duplicates":true}),
11109            )
11110            .await;
11111        let data = match asked {
11112            Ok(data) => data,
11113            Err(error) if unresolvable_node(&error) => return Ok(None),
11114            Err(error) => return Err(error),
11115        };
11116        // `node` is null for an id that names nothing, and absent only from an answer this
11117        // source cannot read — never the same thing.
11118        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
11119            message: format!("GitHub answered the read of {} with no node", id.0),
11120        })?;
11121        self.detail_of(id, node, true, after).await
11122    }
11123
11124    /// Several tasks, each with the first page of its comments when `comments` is set, read
11125    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
11126    /// order.
11127    ///
11128    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
11129    /// one item at a time, so that id is answered as missing and the others as themselves; any
11130    /// other refusal is every id of that batch's answer.
11131    async fn issue_details(
11132        &self,
11133        ids: &[NativeId],
11134        comments: Option<&PageRequest>,
11135    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11136        let mut read = Vec::with_capacity(ids.len());
11137        for batch in ids.chunks(DETAIL_BATCH) {
11138            match self
11139                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
11140                .await
11141            {
11142                Ok(data) => {
11143                    for (slot, id) in batch.iter().enumerate() {
11144                        // Every alias asked for is answered, null for an id naming nothing;
11145                        // one missing is an answer this source cannot read.
11146                        let read_one = match data.get(format!("i{slot}")) {
11147                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
11148                            None => Err(SourceError::Malformed {
11149                                message: format!(
11150                                    "GitHub answered a batch read with no item for {}",
11151                                    id.0
11152                                ),
11153                            }),
11154                        };
11155                        read.push(read_one);
11156                    }
11157                }
11158                Err(error) if unresolvable_node(&error) => {
11159                    for id in batch {
11160                        read.push(match comments {
11161                            Some(page) => self.issue_detail(id, page).await,
11162                            None => self.task_read(id).await,
11163                        });
11164                    }
11165                }
11166                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
11167            }
11168        }
11169        read
11170    }
11171
11172    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
11173    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
11174        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
11175            task,
11176            comments: None,
11177        }))
11178    }
11179
11180    /// What one node a detail read reached says: the task this board holds by `id`, with the
11181    /// page of comments the node carries when `commented` — or `None` for a node that is no
11182    /// task of this board.
11183    ///
11184    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
11185    /// and an item this process created answers from this process's own record, which a node
11186    /// read taken moments after the write can still be behind.
11187    async fn detail_of(
11188        &self,
11189        id: &NativeId,
11190        node: &Value,
11191        commented: bool,
11192        after: Option<&str>,
11193    ) -> Result<Option<TaskDetailRead>, SourceError> {
11194        if node.is_null() {
11195            return Ok(None);
11196        }
11197        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
11198        // An issue answered under one id is that id's, or the answer is not one this source
11199        // can report: reporting another issue's task and comments under the qualified id asked
11200        // for would be the one wrong answer here. A draft's own read checks the same.
11201        if !draft
11202            && optional_str(node, "__typename")? == Some("Issue")
11203            && required_str(node, "id")? != id.0
11204        {
11205            return Err(SourceError::Malformed {
11206                message: format!(
11207                    "GitHub answered the read of {} with issue {}",
11208                    id.0,
11209                    required_str(node, "id")?
11210                ),
11211            });
11212        }
11213        let item = if draft {
11214            self.draft_by_id(id).await?
11215        } else {
11216            self.resolve_issue(node).await?
11217        };
11218        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
11219        // for this command, so the dependency read the copy makes next is answered from it.
11220        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
11221            self.resolved_cache()?.insert(read.id.clone(), read.clone());
11222        }
11223        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11224            return Ok(None);
11225        };
11226        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
11227        let task = own.unwrap_or(item).task()?;
11228        let comments = match (commented, draft) {
11229            (false, _) => None,
11230            (true, true) => Some(Err(self.draft_has_no_comments(id))),
11231            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11232        };
11233        if commented && !draft && after.is_none() {
11234            self.expected.learn_comments(id, node);
11235        }
11236        Ok(Some(TaskDetailRead { task, comments }))
11237    }
11238
11239    /// Whether the comment `comment` is one of `issue`'s own.
11240    ///
11241    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11242    /// comment's id and nothing else: a comment id given against the wrong task would
11243    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11244    /// names something that is not an issue comment, is a comment this task does not have —
11245    /// which is what GitHub refusing to resolve it means too.
11246    async fn comment_is_on(
11247        &self,
11248        issue: &NativeId,
11249        comment: &NativeId,
11250    ) -> Result<bool, SourceError> {
11251        Ok(self.comment_on(issue, comment).await?.is_some())
11252    }
11253
11254    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11255    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11256    async fn comment_on(
11257        &self,
11258        issue: &NativeId,
11259        comment: &NativeId,
11260    ) -> Result<Option<String>, SourceError> {
11261        // A write the store's cache expects knows which comments its issue held, and their
11262        // bodies, at the version it expects: what this read would answer.
11263        if let Some(primed) = self.expected.primed_comment(issue, comment) {
11264            return Ok(primed);
11265        }
11266        let asked = self
11267            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11268            .await;
11269        let data = match asked {
11270            Ok(data) => data,
11271            Err(error) if unresolvable_node(&error) => return Ok(None),
11272            Err(error) => return Err(error),
11273        };
11274        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11275            return Ok(None);
11276        };
11277        if optional_str(node, "__typename")? != Some("IssueComment") {
11278            return Ok(None);
11279        }
11280        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11281            message: format!("GitHub issue comment {} names no issue", comment.0),
11282        })?;
11283        if required_str(on, "id")? != issue.0 {
11284            return Ok(None);
11285        }
11286        Ok(Some(required_str(node, "body")?.to_owned()))
11287    }
11288
11289    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11290    async fn partition_edges(
11291        &self,
11292        near_kind: BoardKind,
11293        near_content: ContentKind,
11294        carried: Option<&[Value]>,
11295        depends_on: &[DependencyEdge],
11296    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11297        let mut native = Vec::new();
11298        let mut fallback = Vec::new();
11299        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11300            .iter()
11301            .map(|edge| {
11302                // An opaque far end names another tracker, so it is never this board's own.
11303                let same_source = edge.to.designator().is_none()
11304                    && edge
11305                        .to
11306                        .source()
11307                        .is_none_or(|source| source == self.name.as_str());
11308                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11309                // `DependencyEndpoint::source` both read it that way — and a native id may hold
11310                // colons of its own, so the far end is everything after that one separator.
11311                // Splitting at the last would truncate `work:urn:task:7` to `7`.
11312                let far_id = if edge.to.is_qualified() {
11313                    edge.to
11314                        .id()
11315                        .split_once(':')
11316                        .map_or(edge.to.id(), |(_, native)| native)
11317                } else {
11318                    edge.to.id()
11319                };
11320                // One that already blocks the near issue was answered by that issue's own
11321                // read, which carried each of its blockers' kinds — an issue every one — so it
11322                // is not read again.
11323                let blocking = carried.and_then(|nodes| {
11324                    nodes
11325                        .iter()
11326                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11327                });
11328                (edge, far_id, same_source, blocking)
11329            })
11330            .collect();
11331        // Every other same-source far end is read by its own id, exactly as the item it is a
11332        // far end of is: whether this board holds it is that read's answer, never a listing's.
11333        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11334        let mut unread: Vec<NativeId> = Vec::new();
11335        for (_, far_id, same_source, blocking) in &far_ends {
11336            let id = NativeId((*far_id).to_owned());
11337            if *same_source && blocking.is_none() && !unread.contains(&id) {
11338                unread.push(id);
11339            }
11340        }
11341        let read: BTreeMap<NativeId, Option<Resolved>> = unread
11342            .iter()
11343            .cloned()
11344            .zip(self.items_by_ids(&unread).await?)
11345            .collect();
11346        for (edge, far_id, same_source, blocking) in far_ends {
11347            let far = match (same_source, blocking) {
11348                (false, _) => None,
11349                (true, Some(node)) => Some(FarEnd {
11350                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11351                        BoardKind::Document
11352                    } else {
11353                        BoardKind::Work(related_kind(node)?)
11354                    },
11355                    content_kind: ContentKind::Issue,
11356                }),
11357                (true, None) => {
11358                    let read = read
11359                        .get(&NativeId(far_id.to_owned()))
11360                        .cloned()
11361                        .flatten()
11362                        .ok_or_else(|| SourceError::Refused {
11363                            message: format!("GitHub dependency item {far_id} was not found"),
11364                        })?;
11365                    Some(FarEnd {
11366                        kind: read.kind,
11367                        content_kind: read.content_kind,
11368                    })
11369                }
11370            };
11371            let far = far.as_ref();
11372            // The caller says which kind the far end is, and this board holds the far end
11373            // itself, so a disagreement is settled here rather than stored: recorded, the
11374            // wrong kind would read back as a cross-level edge that never existed; written
11375            // natively, it would name a relationship of a different level than the caller
11376            // asked for.
11377            //
11378            // A far end this board holds as a *document* fails the same comparison and is
11379            // refused by the same sentence: `ItemKind` has no document variant because
11380            // nothing may point at one, so no caller can name it correctly and the refusal
11381            // is the only honest answer.
11382            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11383                return Err(SourceError::Refused {
11384                    message: format!(
11385                        "GitHub dependency item {far_id} is a {} of this board, and this item \
11386                         names it as a {}; record the kind it is",
11387                        disagreeing.kind.describes(),
11388                        edge.to.kind.marker()
11389                    ),
11390                });
11391            }
11392            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11393            // however the far end is spelled — and one classified native here would be
11394            // written nowhere at all, because a draft's native reconciliation never runs.
11395            let native_here = near_content == ContentKind::Issue
11396                && far.is_some_and(|far| {
11397                    far.content_kind == ContentKind::Issue
11398                        && BoardKind::Work(edge.to.kind) == near_kind
11399                });
11400            if native_here {
11401                native.push(far_id.to_owned());
11402            } else {
11403                fallback.push(edge.clone());
11404            }
11405        }
11406        Ok((native, fallback))
11407    }
11408
11409    async fn update_existing(
11410        &self,
11411        item: &Resolved,
11412        incoming: &Incoming<'_>,
11413        body: &Option<String>,
11414        status_target: Option<&StatusTarget>,
11415    ) -> Result<(), SourceError> {
11416        let title = incoming.written_title();
11417        // A terminal status closes the issue here, in the same mutation as its body: its board
11418        // option was selected before this, so a close never lands on an item whose board cannot
11419        // show it.
11420        let mut fields = match item.content_kind {
11421            ContentKind::DraftIssue => json!({"title":title,"body":body}),
11422            ContentKind::Issue => json!({"title":title,"body":body,
11423                                         "stateInput":state_input(status_target)}),
11424        };
11425        // Only the members whose value differs from the item as this write read it are sent,
11426        // and nothing at all when none does — whether or not the store's cache expects a
11427        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11428        // of it since that read is kept rather than overwritten with what it already held,
11429        // and a re-copy of an unchanged item costs no mutation.
11430        if let Some(members) = fields.as_object_mut() {
11431            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11432                members.remove("title");
11433            }
11434            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11435                (Some(Value::String(sent)), Some(held)) => sent == held,
11436                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11437                _ => false,
11438            };
11439            if body_unchanged {
11440                members.remove("body");
11441            }
11442            // An issue already in the state the target asks for keeps it: an open one stays
11443            // open, and a closed one whose closure reads as the target's category keeps its
11444            // reason — a duplicate's says more than this write would.
11445            let unchanged_state = match status_target {
11446                Some(StatusTarget::Terminal(_, reason)) => {
11447                    item.closed
11448                        && closed_category(item.reason.as_deref())
11449                            == closed_category(Some(reason.reason()))
11450                }
11451                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11452                None => true,
11453            };
11454            if unchanged_state {
11455                members.remove("stateInput");
11456            }
11457            if members.is_empty() {
11458                return Ok(());
11459            }
11460        }
11461        self.update_content(item.content_kind, &item.id, fields)
11462            .await
11463    }
11464
11465    /// Update one board item's content with exactly `fields` beside its id, through the
11466    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11467    /// a draft.
11468    ///
11469    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11470    /// is what lets a narrow write carry the one thing it changes and nothing else.
11471    async fn update_content(
11472        &self,
11473        kind: ContentKind,
11474        id: &NativeId,
11475        fields: Value,
11476    ) -> Result<(), SourceError> {
11477        let (operation, id_key, pointer) = match kind {
11478            ContentKind::DraftIssue => (
11479                graphql::UPDATE_DRAFT,
11480                "draftIssueId",
11481                "/updateProjectV2DraftIssue/draftIssue",
11482            ),
11483            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11484        };
11485        if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11486            return Ok(());
11487        }
11488        let mut input = fields;
11489        input[id_key] = json!(id.0);
11490        self.send_update(operation, pointer, id, input).await
11491    }
11492
11493    /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11494    /// to naming that item, `id`.
11495    async fn send_update(
11496        &self,
11497        operation: &str,
11498        pointer: &str,
11499        id: &NativeId,
11500        input: Value,
11501    ) -> Result<(), SourceError> {
11502        let data = self.graphql(operation, json!({"input":input})).await?;
11503        let returned = data
11504            .pointer(pointer)
11505            .ok_or_else(|| SourceError::Malformed {
11506                message: "GitHub item update returned no item".into(),
11507            })?;
11508        if required_str(returned, "id")? != id.0 {
11509            return Err(SourceError::Malformed {
11510                message: "GitHub item update returned the wrong item".into(),
11511            });
11512        }
11513        Ok(())
11514    }
11515
11516    /// Creates one issue, files it on the board, and reports what a read of it would say:
11517    /// its content id, its board item id, and the web address GitHub gave it.
11518    ///
11519    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11520    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11521    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11522    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11523    /// "Content already exists in this project". A terminal status is not written here:
11524    /// `finish_write` selects its option first and closes the issue after, so a close never
11525    /// lands on an item whose board cannot show it.
11526    ///
11527    /// The address and the number come back here because this is the only place either is
11528    /// known before GitHub's own board read catches up — an item this run created answers
11529    /// the reads that follow it out of the record below, and one remembered without them
11530    /// would report no location and no key for the rest of the run.
11531    async fn create_and_file_issue(
11532        &self,
11533        board_id: &str,
11534        repository: &RepositoryTarget,
11535        incoming: &Incoming<'_>,
11536        body: &Option<String>,
11537    ) -> Result<Landed, SourceError> {
11538        let repository_id = self.repository_id(repository, incoming).await?;
11539        let data = self
11540            .graphql(
11541                graphql::CREATE_ISSUE,
11542                json!({"input":{
11543                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11544                }}),
11545            )
11546            .await?;
11547        let created = data
11548            .pointer("/createIssue/issue")
11549            .filter(|value| !value.is_null())
11550            .ok_or_else(|| SourceError::Malformed {
11551                message: "GitHub issue creation returned no issue".into(),
11552            })?;
11553        let content_id = NativeId(required_str(created, "id")?.to_owned());
11554        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11555        // a response without it is not worth failing a landed write over — the item simply
11556        // reports no location until the board read catches up, which is what it did before.
11557        let url = optional_str(created, "url")?.map(str::to_owned);
11558        // The issue exists from here on, so an unreadable number and a refused board
11559        // filing below each try, best effort, to take it back: an issue in the repository
11560        // that is on no board is an item nobody asked for and nothing here would find again.
11561        //
11562        // Its number is optional on the same terms its address is — a landed write is not
11563        // worth failing over a member that came back missing, and such an item reports no
11564        // handle until a board read catches up. A number that is *present* and is not an
11565        // unsigned integer is still a response this source cannot read.
11566        let number = match created_issue_number(created) {
11567            Ok(number) => number,
11568            Err(error) => {
11569                let _ = self.delete_issue(&content_id).await;
11570                return Err(error);
11571            }
11572        };
11573        let added = match self
11574            .graphql(
11575                graphql::ADD_TO_BOARD,
11576                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11577            )
11578            .await
11579        {
11580            Ok(added) => added,
11581            Err(error) => {
11582                let _ = self.delete_issue(&content_id).await;
11583                return Err(error);
11584            }
11585        };
11586        let item = added
11587            .pointer("/addProjectV2ItemById/item")
11588            .filter(|value| !value.is_null())
11589            .ok_or_else(|| SourceError::Malformed {
11590                message: "GitHub board addition returned no project item".into(),
11591            })?;
11592        self.note_written(&content_id, url.as_deref());
11593        Ok(Landed {
11594            content_id,
11595            item_id: Some(required_str(item, "id")?.to_owned()),
11596            url,
11597            number,
11598        })
11599    }
11600
11601    /// Move one issue under the project it now belongs to, or out of the one it left.
11602    async fn reparent(
11603        &self,
11604        held: Option<NativeId>,
11605        child: &NativeId,
11606        wanted: Option<&NativeId>,
11607    ) -> Result<(), SourceError> {
11608        if held.as_ref() == wanted {
11609            return Ok(());
11610        }
11611        if let Some(held) = &held {
11612            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11613                .await?;
11614        }
11615        if let Some(wanted) = wanted {
11616            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11617                .await?;
11618        }
11619        Ok(())
11620    }
11621
11622    async fn sub_issue(
11623        &self,
11624        operation: &str,
11625        parent: &NativeId,
11626        child: &NativeId,
11627        root: &str,
11628    ) -> Result<(), SourceError> {
11629        let data = self
11630            .graphql(
11631                operation,
11632                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11633            )
11634            .await?;
11635        let issue =
11636            data.pointer(&format!("/{root}/issue"))
11637                .ok_or_else(|| SourceError::Malformed {
11638                    message: "GitHub sub-issue update returned no issue".into(),
11639                })?;
11640        let sub =
11641            data.pointer(&format!("/{root}/subIssue"))
11642                .ok_or_else(|| SourceError::Malformed {
11643                    message: "GitHub sub-issue update returned no sub-issue".into(),
11644                })?;
11645        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11646            return Err(SourceError::Malformed {
11647                message: "GitHub sub-issue update returned the wrong issues".into(),
11648            });
11649        }
11650        Ok(())
11651    }
11652
11653    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11654    /// whether there was one.
11655    ///
11656    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11657    /// relationships are not read: there is nothing a read of them could find.
11658    async fn reconcile_blocked_by(
11659        &self,
11660        content_id: &NativeId,
11661        native: &[String],
11662        issue: Issue<'_>,
11663    ) -> Result<bool, SourceError> {
11664        let current = match issue {
11665            Issue::Created => Vec::new(),
11666            Issue::Existing(Some(held)) => held
11667                .iter()
11668                .map(|far| required_str(far, "id").map(str::to_owned))
11669                .collect::<Result<Vec<_>, _>>()?,
11670            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11671        };
11672        let mut changed = false;
11673        for (operation, far_id) in current
11674            .iter()
11675            .filter(|id| !native.contains(id))
11676            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11677            .chain(
11678                native
11679                    .iter()
11680                    .filter(|id| !current.contains(id))
11681                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11682            )
11683        {
11684            let data = self
11685                .graphql(
11686                    operation,
11687                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11688                )
11689                .await?;
11690            let root = if operation == graphql::ADD_BLOCKED_BY {
11691                "addBlockedBy"
11692            } else {
11693                "removeBlockedBy"
11694            };
11695            let issue =
11696                data.pointer(&format!("/{root}/issue"))
11697                    .ok_or_else(|| SourceError::Malformed {
11698                        message: "GitHub dependency update returned no issue".into(),
11699                    })?;
11700            let blocker = data
11701                .pointer(&format!("/{root}/blockingIssue"))
11702                .ok_or_else(|| SourceError::Malformed {
11703                    message: "GitHub dependency update returned no blocking issue".into(),
11704                })?;
11705            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11706            {
11707                return Err(SourceError::Malformed {
11708                    message: "GitHub dependency update returned the wrong issues".into(),
11709                });
11710            }
11711            changed = true;
11712        }
11713        Ok(changed)
11714    }
11715}
11716
11717/// The board field values one write sends, in the order GitHub runs them, and the fields it
11718/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11719/// priority, then each projected field.
11720fn field_writes<'a>(
11721    existing: Option<&Resolved>,
11722    origin_field: Option<&str>,
11723    origin: &str,
11724    column: Option<(String, String)>,
11725    priority: Option<&'a PriorityWrite>,
11726    projections: &'a [ProjectionWrite],
11727) -> (Vec<(String, Value)>, Vec<&'a str>) {
11728    let mut fields = Vec::new();
11729    if let Some(field_id) = origin_field
11730        && existing.map_or(!origin.is_empty(), |item| {
11731            item.origin.as_deref().unwrap_or("") != origin
11732        })
11733    {
11734        fields.push((field_id.to_owned(), json!({"text":origin})));
11735    }
11736    if let Some((field_id, option_id)) = column {
11737        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11738    }
11739    let mut clears = Vec::new();
11740    match priority {
11741        Some(PriorityWrite::Select { field, option }) => {
11742            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11743        }
11744        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11745        None => {}
11746    }
11747    for projection in projections {
11748        projection.join(&mut fields, &mut clears);
11749    }
11750    (fields, clears)
11751}
11752
11753/// Everything one write decided before sending anything: see
11754/// [`GitHubProjectsSource::prepare_write`].
11755struct Prepared {
11756    /// What the write lands on.
11757    destination: WriteDestination,
11758    board: BoardFields,
11759    status_target: Option<StatusTarget>,
11760    /// The `Status` field, the option and the option's name the status lands on.
11761    // 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.
11762    column: Option<(String, String, String)>,
11763    priority_write: Option<PriorityWrite>,
11764    projections: Vec<ProjectionWrite>,
11765    projected: BTreeMap<String, String>,
11766    content_kind: ContentKind,
11767    own_repository: Option<Repository>,
11768    /// The far ends of the item's native blocked-by relationships.
11769    // 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.
11770    native: Vec<String>,
11771    slot: BTreeMap<String, Value>,
11772    content: Option<String>,
11773    body: Option<String>,
11774    /// The copy origin the item records, empty for none.
11775    // 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.
11776    origin: String,
11777    origin_field: Option<String>,
11778}
11779
11780/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11781/// edge, each its input.
11782enum HeldWrite<'a> {
11783    Field(&'a Value),
11784    Edge(&'a Value),
11785}
11786
11787/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11788/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11789/// it, rather than answering with the item it holds.
11790fn already_on_the_board(error: &SourceError) -> bool {
11791    error
11792        .to_string()
11793        .contains("Content already exists in this project")
11794}
11795
11796/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11797/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11798/// item.
11799fn check_aliased_answers(
11800    document: &str,
11801    data: &Value,
11802    prefix: &str,
11803    inputs: &[&Value],
11804    answers: &[(&str, &str)],
11805) -> Result<(), SourceError> {
11806    for (slot, input) in inputs.iter().enumerate() {
11807        for (root, named) in answers {
11808            let answered = data
11809                .pointer(&format!("/{prefix}{slot}/{root}/id"))
11810                .and_then(Value::as_str)
11811                .ok_or_else(|| SourceError::Malformed {
11812                    message: format!(
11813                        "GitHub answered {} with nothing at {prefix}{slot}",
11814                        operation_description(document)
11815                    ),
11816                })?;
11817            let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11818            if answered != asked {
11819                return Err(SourceError::Malformed {
11820                    message: format!(
11821                        "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11822                         {asked}, the item it was asked to write",
11823                        operation_description(document)
11824                    ),
11825                });
11826            }
11827        }
11828    }
11829    Ok(())
11830}
11831
11832/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11833/// back — each issue of `left` it created that is still in the repository, which its caller,
11834/// never handed their ids, could not name.
11835fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11836    if left.is_empty() {
11837        return error;
11838    }
11839    SourceError::Refused {
11840        message: format!(
11841            "{error}; GitHub then refused to take back {}, which this write created and which \
11842             are still in the repository; next: delete {} before writing again",
11843            left.join(", "),
11844            if left.len() == 1 { "it" } else { "them" }
11845        ),
11846    }
11847}
11848
11849/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11850enum WriteDestination {
11851    /// The item as it stood, for an update.
11852    Existing(Box<Resolved>),
11853    /// Where a new item is created.
11854    New(RepositoryTarget),
11855}
11856
11857impl WriteDestination {
11858    /// The item an update writes over, as it stood; `None` for a create.
11859    fn existing(&self) -> Option<&Resolved> {
11860        match self {
11861            Self::Existing(item) => Some(item),
11862            Self::New(_) => None,
11863        }
11864    }
11865}
11866
11867/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11868/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11869/// it writes beside that issue's id, which only [`Self::new`] sets.
11870struct HeldUpdate {
11871    issue: NativeId,
11872    // 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.
11873    input: Value,
11874}
11875
11876impl HeldUpdate {
11877    /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11878    fn new(issue: &NativeId, members: &Value) -> Self {
11879        let mut input = members.clone();
11880        input["id"] = json!(issue.0);
11881        Self {
11882            issue: issue.clone(),
11883            input,
11884        }
11885    }
11886}
11887
11888/// What a copy has held back of the items it created; see
11889/// [`GitHubProjectsSource::hold_back`].
11890#[derive(Debug, Default)]
11891enum Deferred {
11892    /// Writes are being held back, and nothing has been held yet.
11893    #[default]
11894    Nothing,
11895    /// The items held back, every one of them on `board`.
11896    Held {
11897        /// The board every one of them is on.
11898        board: BoardId,
11899        /// Each created item, in the order it was created.
11900        created: Vec<HeldBack>,
11901    },
11902}
11903
11904impl Deferred {
11905    /// Whether `content` is one of the items held back.
11906    fn holds(&self, content: &NativeId) -> bool {
11907        match self {
11908            Self::Nothing => false,
11909            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11910        }
11911    }
11912}
11913
11914/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11915#[derive(Debug)]
11916struct HeldBack {
11917    /// The issue.
11918    content: NativeId,
11919    /// Its board field values other than its `Status`, in the order they are written.
11920    fields: Vec<(String, Value)>,
11921    /// The `Status` field, and the option and its name that the item's status lands on.
11922    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11923    status: Option<(String, String, String)>,
11924    /// The fields it clears after those are written.
11925    clears: Vec<String>,
11926    /// The reason it is closed for, once its option is selected; `None` leaves it open.
11927    close: Option<ClosedState>,
11928    /// The issues it is blocked by.
11929    edges: Vec<NativeId>,
11930}
11931
11932/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11933/// no GitHub type has a field of this name.
11934const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11935
11936/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11937/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11938///
11939/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11940/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11941/// created named in the data beside it — the ids a copy needs to take them back.
11942fn answer_with_errors(body: &str) -> Option<Value> {
11943    let parsed: Value = serde_json::from_str(body).ok()?;
11944    let errors = parsed
11945        .get("errors")?
11946        .as_array()
11947        .filter(|errors| !errors.is_empty())?;
11948    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11949    data[ANSWER_ERRORS] = Value::Array(errors.clone());
11950    Some(data)
11951}
11952
11953/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11954/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11955fn filed_input(
11956    repository: &str,
11957    board: &str,
11958    incoming: &Incoming<'_>,
11959    body: &Option<String>,
11960) -> Value {
11961    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11962                           "body":body,"projectV2Ids":[board]});
11963    if let Some(parent) = incoming.parent {
11964        input["parentIssueId"] = json!(parent.0);
11965    }
11966    input
11967}
11968
11969/// The name of option `option` of field `field` among a board's `fields`.
11970fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11971    fields
11972        .get("nodes")?
11973        .as_array()?
11974        .iter()
11975        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11976        .get("options")?
11977        .as_array()?
11978        .iter()
11979        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11980        .get("name")?
11981        .as_str()
11982}
11983
11984/// What a write needs to know of one far end it names: which kind of item it is, and whether
11985/// it is an issue a native relationship can name.
11986struct FarEnd {
11987    kind: BoardKind,
11988    content_kind: ContentKind,
11989}
11990
11991/// Whether the issue one write reconciles was created by that write or was already there.
11992#[derive(Clone, Copy, PartialEq, Eq)]
11993enum Issue<'a> {
11994    /// Created by this write, so it holds no relationships yet.
11995    Created,
11996    /// On the board before this write, holding whatever relationships it holds — the far
11997    /// ends of its whole `blockedBy`, when the read that reached it carried them.
11998    Existing(Option<&'a [Value]>),
11999}
12000
12001/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
12002enum Reached {
12003    /// An issue this board holds, resolved into everything this source reports about it.
12004    Held(Box<Resolved>),
12005    /// Nothing this board holds: no such node, or a node on some other board.
12006    Nothing,
12007    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
12008    /// again by [`GitHubProjectsSource::draft_by_id`].
12009    Draft,
12010}
12011
12012/// What GitHub says when a string is not a node id it can resolve.
12013///
12014/// Matched because it is the ordinary answer to a project selector naming a project by its
12015/// *name*, and reporting that as a failure would make naming one impossible. It is read
12016/// off the refusal GitHub sent, never guessed from the shape of the string: this source
12017/// does not define the syntax of a GitHub node id and would be wrong about it.
12018const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
12019
12020/// `error` with `note` added to the end of what it says, its kind and every other member
12021/// unchanged — so a caller still branches on the failure that happened, and reads beside it
12022/// what that failure left behind.
12023fn noting(error: SourceError, note: &str) -> SourceError {
12024    match error {
12025        SourceError::Config { message } => SourceError::Config {
12026            message: message + note,
12027        },
12028        SourceError::Auth { message } => SourceError::Auth {
12029            message: message + note,
12030        },
12031        SourceError::Refused { message } => SourceError::Refused {
12032            message: message + note,
12033        },
12034        SourceError::RateLimited {
12035            retry_after_seconds,
12036            message,
12037        } => SourceError::RateLimited {
12038            retry_after_seconds,
12039            message: Some(message.unwrap_or_default() + note),
12040        },
12041        SourceError::Unavailable { message } => SourceError::Unavailable {
12042            message: message + note,
12043        },
12044        SourceError::Malformed { message } => SourceError::Malformed {
12045            message: message + note,
12046        },
12047    }
12048}
12049
12050/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
12051/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
12052fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
12053    let mut variables = serde_json::Map::new();
12054    for slot in 0..COPY_DETAIL_BATCH {
12055        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12056        variables.insert(format!("id{slot}"), json!(id));
12057    }
12058    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
12059    variables.insert("children".to_owned(), json!(children));
12060    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
12061    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12062    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12063    variables.insert("duplicates".to_owned(), json!(true));
12064    Value::Object(variables)
12065}
12066
12067/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
12068/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
12069/// for them.
12070///
12071/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
12072/// is read again at no added price.
12073fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
12074    let mut variables = serde_json::Map::new();
12075    for slot in 0..DETAIL_BATCH {
12076        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12077        variables.insert(format!("id{slot}"), json!(id));
12078    }
12079    variables.insert(
12080        "first".to_owned(),
12081        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
12082    );
12083    variables.insert("comments".to_owned(), json!(comments.is_some()));
12084    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12085    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12086    variables.insert("duplicates".to_owned(), json!(true));
12087    Value::Object(variables)
12088}
12089
12090/// Whether this refusal is GitHub saying the id names no node at all.
12091fn unresolvable_node(error: &SourceError) -> bool {
12092    matches!(error, SourceError::Refused { message }
12093        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
12094}
12095
12096/// One project name, as a search qualifier which filters on it at the server.
12097///
12098/// Quoted so the whole title is one phrase rather than a bag of words, with the two
12099/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
12100/// the way it documents. A title matched here is still compared for equality afterwards:
12101/// the qualifier narrows what the server sends, and this source decides what it names.
12102fn title_qualifier(name: &str) -> String {
12103    format!("in:title {}", quoted(name))
12104}
12105
12106/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
12107/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
12108/// it documents — so a value holding a qualifier's spelling is searched for rather than
12109/// obeyed.
12110fn quoted(value: &str) -> String {
12111    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
12112    format!("\"{escaped}\"")
12113}
12114
12115/// The search qualifier for the issues updated at or after `since`.
12116///
12117/// Written to the second, rounded down, which can only widen what the search returns.
12118fn updated_qualifier(since: DateTime<Utc>) -> String {
12119    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
12120}
12121
12122/// The search terms that narrow a board-scoped issue search to a task query's text and
12123/// metadata predicates, or `None` when it carries neither.
12124///
12125/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
12126/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
12127/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
12128/// matches each in any field the `in:` qualifier names, so a query naming a title search and
12129/// a metadata value searches both fields for both — wider than asked, never narrower, and
12130/// every candidate is confirmed in process afterwards.
12131///
12132/// **This narrows a text search, and that is this source's declared semantics.** GitHub
12133/// matches whole tokens where a substring rule would match inside a word, so an item holding
12134/// the text only inside a longer word is not returned. A text of nothing but whitespace
12135/// matches every item, so it narrows nothing and is not sent.
12136fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
12137    let text = query
12138        .text
12139        .as_ref()
12140        .filter(|text| !text.terms.trim().is_empty());
12141    if text.is_none() && query.metadata.is_empty() {
12142        return None;
12143    }
12144    let (title, body) = match text.map(|text| text.fields) {
12145        None => (false, true),
12146        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
12147        Some(TextFields::Content) => (false, true),
12148        Some(TextFields::TitleOrContent) => (true, true),
12149    };
12150    let fields = match (title, body) {
12151        (true, true) => "in:title,body",
12152        (true, false) => "in:title",
12153        _ => "in:body",
12154    };
12155    let phrases = text
12156        .map(|text| text.terms.clone())
12157        .into_iter()
12158        .chain(
12159            query
12160                .metadata
12161                .iter()
12162                .map(|wanted| as_stored(wanted.value())),
12163        )
12164        .map(|phrase| quoted(&phrase))
12165        .collect::<Vec<_>>();
12166    Some(format!("{fields} {}", phrases.join(" ")))
12167}
12168
12169/// The search terms that narrow a board-scoped issue search to a project or document query's
12170/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
12171/// carrying that text alone is sent as by [`narrowing_qualifiers`].
12172fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
12173    narrowing_qualifiers(&TaskQuery {
12174        text: text.cloned(),
12175        ..TaskQuery::default()
12176    })
12177}
12178
12179/// Refuses a project or document query's text GitHub's issue search cannot find, before
12180/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
12181/// query's.
12182fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
12183    refuse_unsearchable(&TaskQuery {
12184        text: text.cloned(),
12185        ..TaskQuery::default()
12186    })
12187}
12188
12189/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
12190/// before anything is asked of GitHub.
12191///
12192/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
12193/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
12194/// left out, the search is every issue of the board. So this source says it cannot answer
12195/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
12196/// nothing GitHub could search for, and keeps the board read it always had.
12197fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
12198    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
12199                       letter or digit with a bounded query";
12200    if let Some(text) = &query.text
12201        && !text.terms.trim().is_empty()
12202        && !has_words(&text.terms)
12203    {
12204        return Err(SourceError::Refused {
12205            message: format!(
12206                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
12207                text.terms
12208            ),
12209        });
12210    }
12211    if let Some(wanted) = query
12212        .metadata
12213        .iter()
12214        .find(|wanted| !has_words(wanted.value()))
12215    {
12216        return Err(SourceError::Refused {
12217            message: format!(
12218                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
12219                wanted.value(),
12220                std::iter::once(wanted.key())
12221                    .chain(wanted.path().iter().map(String::as_str))
12222                    .collect::<Vec<_>>()
12223                    .join("/"),
12224            ),
12225        });
12226    }
12227    Ok(())
12228}
12229
12230/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12231fn has_words(phrase: &str) -> bool {
12232    phrase.chars().any(char::is_alphanumeric)
12233}
12234
12235/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12236///
12237/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12238/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12239/// which GitHub's word match would read as different words.
12240fn as_stored(value: &str) -> String {
12241    let encoded = Value::String(value.to_owned()).to_string();
12242    encoded[1..encoded.len() - 1].to_owned()
12243}
12244
12245/// The one narrower question a task query carrying a text, metadata or origin predicate is
12246/// sent as.
12247enum Narrowing {
12248    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12249    Origin(String),
12250    /// The board-scoped issue search narrowed by these qualifiers.
12251    Search(String),
12252}
12253
12254impl Narrowing {
12255    /// What this question is remembered under for the length of one command.
12256    fn key(&self) -> String {
12257        match self {
12258            Self::Origin(origin) => format!("origin {origin}"),
12259            Self::Search(also) => format!("search {also}"),
12260        }
12261    }
12262}
12263
12264/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12265enum Resumed {
12266    /// It reported another page, which starts after this cursor.
12267    More(String),
12268    /// It has ended. Sending this cursor again — the page's own end when it had one, and
12269    /// otherwise the cursor it was reached from — answers an empty page, so the one document
12270    /// can go on walking the other connection.
12271    Ended(Option<String>),
12272}
12273
12274impl Resumed {
12275    /// Whether the connection has another page.
12276    const fn has_more(&self) -> bool {
12277        matches!(self, Self::More(_))
12278    }
12279
12280    /// The cursor to send this connection next.
12281    fn cursor(self) -> Option<String> {
12282        match self {
12283            Self::More(next) => Some(next),
12284            Self::Ended(last) => last,
12285        }
12286    }
12287}
12288
12289/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12290/// with no cursor to it, or from a cursor that does not advance.
12291fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12292    let info = connection
12293        .get("pageInfo")
12294        .ok_or_else(|| SourceError::Malformed {
12295            message: "GitHub connection has no pageInfo".into(),
12296        })?;
12297    let end = optional_str(info, "endCursor")?;
12298    if required_bool(info, "hasNextPage")? {
12299        let next = end.ok_or_else(|| SourceError::Malformed {
12300            message: "GitHub connection reports another page and no endCursor".into(),
12301        })?;
12302        validate_cursor_progress(after, next)?;
12303        return Ok(Resumed::More(next.to_owned()));
12304    }
12305    Ok(Resumed::Ended(
12306        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12307    ))
12308}
12309
12310/// The board, and every item on it this source reports.
12311#[derive(Clone)]
12312struct Board {
12313    id: String,
12314    fields: Value,
12315    items: Vec<Resolved>,
12316}
12317
12318/// What a write needs of the board and nothing more: its node id and its field
12319/// definitions, in the shape a read of the board's own `fields` gives them.
12320///
12321/// Deliberately no items. A write decides which item it writes, which parent it files
12322/// under and which far ends it names by reading each of them by its own id; this is the
12323/// half of the board those reads cannot carry, and holding no item is what keeps it from
12324/// ever being asked whether an item is there.
12325#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12326struct BoardFields {
12327    id: BoardId,
12328    fields: Value,
12329}
12330
12331/// A board's node id: what a field write and `addProjectV2ItemById` address.
12332///
12333/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12334/// refused where it is read, and one an item names blank is read as not named at all.
12335#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12336#[serde(try_from = "String")]
12337struct BoardId(String);
12338
12339impl TryFrom<String> for BoardId {
12340    type Error = String;
12341
12342    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12343    fn try_from(id: String) -> Result<Self, Self::Error> {
12344        Self::parse(&id).map_err(|error| error.to_string())
12345    }
12346}
12347
12348/// Where one write left its item, for the record the rest of the command reads it out of.
12349///
12350/// A named record rather than a tuple because the update arm and the create arm each fill
12351/// all four, and two `Option`s of different meaning side by side in a tuple are two
12352/// positions a reader has to count.
12353struct Landed {
12354    /// The issue's own node id, which is the [`NativeId`] this source reports.
12355    content_id: NativeId,
12356    /// The board item's id, which is what a field write addresses — `None` for an issue its
12357    /// create filed on the board itself, whose board item GitHub adds after answering and
12358    /// [`TaskSource::finish_writes`] reads.
12359    // 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.
12360    item_id: Option<String>,
12361    /// The web address GitHub gave the issue, when it gave one.
12362    // 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.
12363    url: Option<String>,
12364    /// The issue's number on its repository, when GitHub reported one.
12365    number: Option<u64>,
12366}
12367
12368impl BoardId {
12369    fn parse(id: &str) -> Result<Self, SourceError> {
12370        if id.trim().is_empty() {
12371            return Err(SourceError::Malformed {
12372                message: "GitHub named a board with a blank node id".into(),
12373            });
12374        }
12375        Ok(Self(id.to_owned()))
12376    }
12377
12378    fn as_str(&self) -> &str {
12379        &self.0
12380    }
12381}
12382
12383impl Board {
12384    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12385        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12386        let nodes = fields
12387            .get("nodes")
12388            .and_then(Value::as_array)
12389            .ok_or_else(|| SourceError::Malformed {
12390                message: "GitHub project fields.nodes is not an array".into(),
12391            })?;
12392        Ok(nodes
12393            .iter()
12394            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12395    }
12396}
12397
12398/// One board item, resolved into everything this source reports about it.
12399///
12400/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12401/// [`TaskSource::prime_write`] takes back — see [`expected`].
12402#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12403struct Resolved {
12404    item_id: String,
12405    id: NativeId,
12406    content_kind: ContentKind,
12407    kind: BoardKind,
12408    title: String,
12409    body: Option<String>,
12410    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12411    /// that changes the slot alone has to keep byte for byte outside it.
12412    raw_body: Option<String>,
12413    status: Status,
12414    /// The name of the board `Status` option this item sits in, as the board spells it.
12415    option: Option<String>,
12416    /// What its `Priority` field says, read through this instance's mapping.
12417    priority: HeldPriority,
12418    /// Whether this item's issue is closed. A draft has no such state and is never closed.
12419    closed: bool,
12420    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12421    /// issue, a draft, and an issue closed before GitHub recorded reasons.
12422    #[serde(default)]
12423    reason: Option<String>,
12424    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12425    delivers: Vec<TaskRef>,
12426    /// Every task that delivers this one, read out of its slot. Empty for anything not a
12427    /// task.
12428    delivered_by: Vec<TaskRef>,
12429    labels: Vec<Label>,
12430    parent: Option<NativeId>,
12431    // 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.
12432    origin: Option<String>,
12433    /// What each board text field this instance projects metadata onto holds for this item,
12434    /// by field name — only the fields holding a value, an empty text included. What a write compares the
12435    /// item's metadata against, so a field already holding its value is not written again;
12436    /// never read back as metadata, which the slot alone reports.
12437    projected: BTreeMap<String, String>,
12438    /// The issue's own number on its repository, as GitHub reports it.
12439    ///
12440    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12441    /// declares none, and a draft is not filed in a repository to be numbered by one — and
12442    /// an issue this run created whose creating mutation answered without one, which is a
12443    /// response GitHub's own schema says cannot happen and which a landed write is not
12444    /// worth failing over. An `Issue` read off the board always has one.
12445    number: Option<u64>,
12446    url: Option<String>,
12447    created_at: Option<DateTime<Utc>>,
12448    updated_at: Option<DateTime<Utc>>,
12449    /// When the board item itself last changed — what a board field write moves — when the
12450    /// read that reached it said.
12451    item_updated_at: Option<DateTime<Utc>>,
12452    /// How many comments the issue holds, when the read that reached it said.
12453    comment_count: Option<u64>,
12454    /// How many relationship events the issue's timeline holds — every dependency, as
12455    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12456    /// removed — when the read that reached it said.
12457    relationship_events: Option<u64>,
12458    /// The server's clock on the response that read it — its `Date` header — when the
12459    /// response carried one.
12460    served_at: Option<DateTime<Utc>>,
12461    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12462    /// its labels — when the read that reached it carried all three whole.
12463    #[serde(default)]
12464    facets: Option<change_token::FacetsDigest>,
12465    own_repository: Option<Repository>,
12466    repositories: Vec<Repository>,
12467    classification: Classification,
12468    slot: BTreeMap<String, Value>,
12469    /// The node id of the board this item sits on, when the read that reached it said.
12470    board_id: Option<String>,
12471    /// The definition of every board field this item holds a value of, in the shape a read
12472    /// of the board's own `fields` gives one.
12473    ///
12474    /// Only the fields this item has a value in: a field it holds nothing of is not here,
12475    /// which says nothing about whether the board has it.
12476    fields: Vec<Value>,
12477    /// Every field the board this item sits on defines, as its own read of the board's
12478    /// `fields` gives them — when the read that reached the item carried them, which a read
12479    /// of it by its own id does. What a write of it needs of the board, then, needs no read
12480    /// of the board.
12481    board_fields: Option<Value>,
12482    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12483    /// selects one — when the read that reached it carried the connection to its end, which a
12484    /// read of it by its own id does for any issue blocked by no more than a page. What a
12485    /// write reconciles that relationship against, and what a read of its forward edges in
12486    /// the same command answers with.
12487    blocked_by: Option<Vec<Value>>,
12488}
12489
12490impl Resolved {
12491    /// The board this item's own read names it on, when that read named one this source can
12492    /// address.
12493    fn named_board(&self) -> Option<BoardId> {
12494        self.board_id
12495            .as_deref()
12496            .and_then(|id| BoardId::parse(id).ok())
12497    }
12498
12499    /// The board's id and every field it defines, when the read that reached this item
12500    /// carried both — which a read of it by its own id does.
12501    fn carried_board(&self) -> Option<BoardFields> {
12502        Some(BoardFields {
12503            id: self.named_board()?,
12504            fields: self.board_fields.clone()?,
12505        })
12506    }
12507
12508    /// Whether this item holds a value of the board field called `name`, and so carries
12509    /// that field's definition. `false` says nothing about whether the board has the field.
12510    fn defines(&self, name: &str) -> bool {
12511        self.fields
12512            .iter()
12513            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12514    }
12515
12516    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12517    /// in a field of its own, and none of the five keys that are only an encoding.
12518    ///
12519    /// The two delivery keys are left out for every kind, not only for a task: they are
12520    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12521    /// document carrying one holds nothing a caller's own metadata could mean by it.
12522    fn metadata(&self) -> BTreeMap<String, Value> {
12523        let mut metadata = self.slot.clone();
12524        metadata.remove(Repository::METADATA_KEY);
12525        metadata.remove(DependencyEdge::RECORDED_KEY);
12526        metadata.remove(ItemKind::METADATA_KEY);
12527        metadata.remove(TaskRef::DELIVERS_KEY);
12528        metadata.remove(TaskRef::DELIVERED_BY_KEY);
12529        metadata.remove(Classification::METADATA_KEY);
12530        // The board field is the origin, and the body's copy of it is only a mirror for the
12531        // issue search to find: an item whose field holds none has none, whatever its body
12532        // says, so no reader ever sees two answers.
12533        metadata.remove(ORIGIN_KEY);
12534        if let Some(origin) = &self.origin {
12535            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12536        }
12537        metadata
12538    }
12539
12540    /// Where this item is, as a link a reader can open.
12541    ///
12542    /// A board is a hosted place and every issue on it has a web address, so that address
12543    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12544    /// of place it is, so a reader knows to open it rather than to read a file out. It
12545    /// does not replace or derive from `url`: the field goes on reporting exactly what it
12546    /// reported before, and this says what that address *is*.
12547    ///
12548    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12549    /// rather than a third variant, which is the contract's "the source did not say". An
12550    /// issue this run created is not one of those: its address comes back from the
12551    /// creating mutation, so it is somewhere a reader can open from the moment it exists
12552    /// rather than from whenever the board read catches up.
12553    fn location(&self) -> Option<Location> {
12554        self.url.clone().map(Location::Url)
12555    }
12556
12557    /// The short handle this board's backend shows people for a task: the issue's number
12558    /// alone, as a decimal string.
12559    ///
12560    /// The number alone rather than `owner/repo#1043`, because that is the contract's
12561    /// value for this backend. A draft has no number and so no handle, which is the
12562    /// contract's *absent* rather than a handle of some other shape — and the native
12563    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12564    /// derives from.
12565    fn key(&self) -> Option<String> {
12566        self.number.map(|number| number.to_string())
12567    }
12568
12569    /// This item's change token — see [`change_token`] — or `None` when the read that
12570    /// reached it did not carry all six of its parts, which a draft never does, did not say
12571    /// when GitHub served it, or found it changed too recently to have settled.
12572    fn version(&self) -> Option<String> {
12573        match self.content_kind {
12574            ContentKind::Issue => {
12575                let (issue, item) = (self.updated_at?, self.item_updated_at?);
12576                let parts = change_token::Parts {
12577                    issue,
12578                    item,
12579                    comments: self.comment_count?,
12580                    relationships: self.relationship_events?,
12581                    facets: self.facets.clone()?,
12582                    repository: self.own_repository.clone()?,
12583                };
12584                change_token::settled(issue, item, self.served_at?)
12585                    .then(|| change_token::spell(&parts))
12586            }
12587            ContentKind::DraftIssue => None,
12588        }
12589    }
12590
12591    /// Whether its `Priority` field holds a value at all, mapped or not.
12592    fn holds_priority(&self) -> bool {
12593        self.priority != HeldPriority::Read(Priority::None)
12594    }
12595
12596    /// The task this item is.
12597    ///
12598    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12599    /// reading that as a level would be a guess, and reading it as `none` would let the next
12600    /// copy clear a priority a person set.
12601    fn task(&self) -> Result<Task, SourceError> {
12602        let priority = match &self.priority {
12603            HeldPriority::Read(priority) => *priority,
12604            HeldPriority::Unmapped(option) => {
12605                return Err(SourceError::Malformed {
12606                    message: format!(
12607                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12608                         this source's priority_mapping does not name, so its priority cannot be \
12609                         read; next: name {option:?} under priority_mapping, or move the item to \
12610                         a mapped option",
12611                        self.id,
12612                        self.number
12613                            .map(|number| format!(" (#{number})"))
12614                            .unwrap_or_default()
12615                    ),
12616                });
12617            }
12618        };
12619        Ok(Task {
12620            version: self.version(),
12621            id: self.id.clone(),
12622            key: self.key(),
12623            title: self.title.clone(),
12624            content: self.body.clone(),
12625            status: self.status.clone(),
12626            priority,
12627            labels: self.labels.clone(),
12628            project: self.parent.clone(),
12629            url: self.url.clone(),
12630            location: self.location(),
12631            created_at: self.created_at,
12632            updated_at: self.updated_at,
12633            metadata: self.metadata(),
12634            repositories: self.repositories.clone(),
12635            delivers: self.delivers.clone(),
12636            delivered_by: self.delivered_by.clone(),
12637            classification: self.classification,
12638        })
12639    }
12640
12641    fn project(&self) -> Project {
12642        Project {
12643            version: self.version(),
12644            id: self.id.clone(),
12645            title: self.title.clone(),
12646            content: self.body.clone(),
12647            status: self.status.clone(),
12648            labels: self.labels.clone(),
12649            url: self.url.clone(),
12650            location: self.location(),
12651            created_at: self.created_at,
12652            updated_at: self.updated_at,
12653            metadata: self.metadata(),
12654            repositories: self.repositories.clone(),
12655            classification: self.classification,
12656        }
12657    }
12658
12659    /// The same issue as a document: the project it is filed under, and no status and no
12660    /// dependencies, because a document is not work.
12661    fn document(&self) -> Document {
12662        Document {
12663            version: self.version(),
12664            id: self.id.clone(),
12665            title: self.title.clone(),
12666            content: self.body.clone(),
12667            project: self.parent.clone(),
12668            labels: self.labels.clone(),
12669            url: self.url.clone(),
12670            location: self.location(),
12671            created_at: self.created_at,
12672            updated_at: self.updated_at,
12673            metadata: self.metadata(),
12674            repositories: self.repositories.clone(),
12675            classification: self.classification,
12676        }
12677    }
12678}
12679
12680/// Where one targeted update moves an item's status, and which of its two halves move.
12681struct StatusMove {
12682    /// The board the item's `Status` field is on.
12683    board: BoardId,
12684    /// The `Status` field's id.
12685    field: String,
12686    /// The option's id.
12687    option: String,
12688    /// The option's name, as the board spells it.
12689    name: String,
12690    /// What the status asks of the issue's state.
12691    target: StatusTarget,
12692    /// The status the item reads as once it is there.
12693    landed: Status,
12694    /// Which of the status's two halves differ from what the item holds.
12695    moves: Moves,
12696}
12697
12698/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12699/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12700/// and is not a value of this type.
12701#[derive(Clone, Copy, PartialEq, Eq)]
12702enum Moves {
12703    /// The option alone.
12704    Option,
12705    /// The issue's state alone: open, closed, or closed with another reason.
12706    State,
12707    /// Both.
12708    Both,
12709}
12710
12711impl Moves {
12712    /// What differs, or `None` when nothing does.
12713    const fn of(option: bool, state: bool) -> Option<Self> {
12714        match (option, state) {
12715            (true, true) => Some(Self::Both),
12716            (true, false) => Some(Self::Option),
12717            (false, true) => Some(Self::State),
12718            (false, false) => None,
12719        }
12720    }
12721
12722    /// Whether the option moves.
12723    const fn option(self) -> bool {
12724        matches!(self, Self::Option | Self::Both)
12725    }
12726
12727    /// Whether the issue's state moves.
12728    const fn state(self) -> bool {
12729        matches!(self, Self::State | Self::Both)
12730    }
12731}
12732
12733/// What one write is, and the status that comes with being it.
12734///
12735/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12736/// status and a task or a project always has one, so "a document carrying a status" and
12737/// "a task carrying none" are states a write cannot be in rather than states every use
12738/// site below has to defend against.
12739enum Written<'a> {
12740    /// A document, which is not work and so has no status at all.
12741    Document,
12742    /// A task or a project, and the status it is being written with.
12743    Work(ItemKind, &'a Status),
12744}
12745
12746impl Written<'_> {
12747    /// Which of the board's three kinds this write is.
12748    const fn kind(&self) -> BoardKind {
12749        match self {
12750            Self::Document => BoardKind::Document,
12751            Self::Work(kind, _) => BoardKind::Work(*kind),
12752        }
12753    }
12754
12755    /// The status this write carries. A document carries none, so a write of one says
12756    /// nothing about the issue's open or closed state and selects no board `Status`
12757    /// option.
12758    const fn status(&self) -> Option<&Status> {
12759        match self {
12760            Self::Document => None,
12761            Self::Work(_, status) => Some(status),
12762        }
12763    }
12764
12765    /// The status this write carries with the kind whose half of `status_mapping` it is
12766    /// written through.
12767    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12768        match self {
12769            Self::Document => None,
12770            Self::Work(kind, status) => Some((*kind, status)),
12771        }
12772    }
12773}
12774
12775/// The item being written, in the one shape all three write methods reach.
12776struct Incoming<'a> {
12777    written: Written<'a>,
12778    /// The title a person wrote. A document's goes onto the issue with
12779    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12780    title: &'a str,
12781    content: Option<&'a str>,
12782    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12783    labels: &'a [Label],
12784    metadata: &'a BTreeMap<String, Value>,
12785    repositories: &'a [Repository],
12786    /// Recorded in the slot while private, and nowhere while public.
12787    classification: Classification,
12788    parent: Option<&'a NativeId>,
12789    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12790    /// what keeps either key out of their slot.
12791    delivers: &'a [TaskRef],
12792    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12793    delivered_by: &'a [TaskRef],
12794    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12795    /// project, a document, and every write to an instance with no `priority_mapping` —
12796    /// which is what keeps such a write's requests exactly what they were before.
12797    priority: Option<Priority>,
12798}
12799
12800/// What one write does to one projected metadata text field, by the field's id.
12801#[derive(Debug, Clone)]
12802enum ProjectionWrite {
12803    /// Write this text.
12804    Set {
12805        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.
12806        text: String,
12807    },
12808    /// Clear the field's value.
12809    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12810}
12811
12812impl ProjectionWrite {
12813    /// Add this write to a field write's list of values and list of clears.
12814    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12815        match self {
12816            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12817            Self::Clear { field } => clears.push(field),
12818        }
12819    }
12820}
12821
12822/// What one write does to an item's `Priority` field.
12823enum PriorityWrite {
12824    /// Select this option of this field.
12825    Select {
12826        /// The `Priority` field's id.
12827        field: String,
12828        /// The mapped option's id.
12829        option: String,
12830    },
12831    /// Clear the field's value, which is what `none` is.
12832    Clear {
12833        /// The `Priority` field's id.
12834        field: String,
12835    },
12836}
12837
12838impl Incoming<'_> {
12839    /// The title this write puts on the issue.
12840    fn written_title(&self) -> String {
12841        match self.written {
12842            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12843            Written::Work(..) => self.title.to_owned(),
12844        }
12845    }
12846}
12847
12848#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12849enum ContentKind {
12850    DraftIssue,
12851    Issue,
12852}
12853
12854/// What one board issue is: a document, or the work an [`ItemKind`] names.
12855///
12856/// A type of this source's own rather than an `ItemKind` with a third variant, because
12857/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12858/// document — the contract keeps a document out of that enum deliberately. Holding the
12859/// board's three answers in one value is what makes every place that asks "which is this?"
12860/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12861/// two thirds of the board.
12862#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12863enum BoardKind {
12864    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12865    Document,
12866    /// Every other issue, and every draft.
12867    Work(ItemKind),
12868}
12869
12870impl BoardKind {
12871    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12872    /// sub-issues and carrying the kind marker `marked` in its slot.
12873    ///
12874    /// The design prefix is read *first*, before either of the two rules that separate a
12875    /// project from a task. A document is not work whatever sub-issues it has and whatever
12876    /// marker it carries, and reading the prefix later would make a design issue with none of
12877    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12878    /// overrides it: an issue filed under a project is that project's task even when it has
12879    /// sub-issues of its own.
12880    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12881        if title.starts_with(DESIGN_TITLE_PREFIX) {
12882            Self::Document
12883        } else if filed {
12884            Self::Work(ItemKind::Task)
12885        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12886            Self::Work(ItemKind::Project)
12887        } else {
12888            Self::Work(ItemKind::Task)
12889        }
12890    }
12891
12892    /// What the change token's facets call this kind.
12893    const fn facet(self) -> change_token::FacetKind {
12894        match self {
12895            Self::Document => change_token::FacetKind::Document,
12896            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12897            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12898        }
12899    }
12900
12901    /// Whose half of `status_mapping` an item of this kind reads its status through. A
12902    /// document has no status of its own, so the task half stands in for whatever the issue
12903    /// holds; nothing reports it.
12904    const fn status_kind(self) -> ItemKind {
12905        match self {
12906            Self::Document => ItemKind::Task,
12907            Self::Work(kind) => kind,
12908        }
12909    }
12910
12911    /// How a refusal names this kind to the person reading it.
12912    const fn describes(self) -> &'static str {
12913        match self {
12914            Self::Document => "document",
12915            Self::Work(kind) => kind.marker(),
12916        }
12917    }
12918}
12919
12920/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12921///
12922/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12923/// the shared cross-source journeys assert one answer to one question, so two sources
12924/// that disagree about what "carries the label bug" means fail them.
12925fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12926    let holds = |name: &String| {
12927        labels
12928            .iter()
12929            .any(|label| label.name.eq_ignore_ascii_case(name))
12930    };
12931    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12932        && filter.all_of.iter().all(holds)
12933        && !filter.none_of.iter().any(holds)
12934}
12935
12936/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12937/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12938fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12939    statuses.is_empty() || statuses.contains(&category)
12940}
12941
12942/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12943///
12944/// `content` is the item's own prose — the body with this source's trailing metadata
12945/// comment already taken off — so a search never matches an encoding the author of the
12946/// issue never wrote.
12947fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12948    let terms = query.terms.to_lowercase();
12949    let in_title = title.to_lowercase().contains(&terms);
12950    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12951    match query.fields {
12952        TextFields::Title => in_title,
12953        TextFields::Content => in_content,
12954        TextFields::TitleOrContent => in_title || in_content,
12955    }
12956}
12957
12958/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12959///
12960/// The project predicate is passed separately because a read narrowed to one project has
12961/// already answered it by asking *that project* for its own items — and re-applying it
12962/// there would compare the caller's selector, which may be a project's **name**, against
12963/// the id of the project that name resolved to, and keep nothing. Every other read passes
12964/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12965/// source really does apply.
12966fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12967    labels_match(&task.labels, &query.labels)
12968        && status_matches(task.status.category, &query.statuses)
12969        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12970        && match project {
12971            ProjectFilter::Any => true,
12972            ProjectFilter::Orphans => task.project.is_none(),
12973            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12974        }
12975        && query
12976            .text
12977            .as_ref()
12978            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12979        // Against the parsed metadata slot, and against the origin field, which is where
12980        // `Resolved::metadata` reads each of them from.
12981        && query.metadata_matches(&task.metadata)
12982        && query.origin_matches(&task.metadata)
12983}
12984
12985fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12986    labels_match(&project.labels, &query.labels)
12987        && status_matches(project.status.category, &query.statuses)
12988        && query
12989            .text
12990            .as_ref()
12991            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12992}
12993
12994/// The same three predicates a task query carries, minus the status filter.
12995///
12996/// A document is not work, so it has no status for one to compare against and the query
12997/// type carries none. The project predicate is the same one — a design issue filed under a
12998/// project issue is in that project, and one filed under nothing is in none — so it is
12999/// spelled the same way here rather than answered differently.
13000fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
13001    labels_match(&document.labels, &query.labels)
13002        && match project {
13003            ProjectFilter::Any => true,
13004            ProjectFilter::Orphans => document.project.is_none(),
13005            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
13006        }
13007        && query
13008            .text
13009            .as_ref()
13010            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
13011}
13012
13013/// Why a GitHub item's change token does not vouch for its forward edges.
13014///
13015/// A forward edge names its far end and that far end's kind, and the near item's token moves
13016/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
13017/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
13018/// nothing the near token reads, so an edge kept at the near version could name a far end that
13019/// is no longer what it said.
13020const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
13021     whether it became a document, or its deletion, so its forward edges are always read from \
13022     github";
13023
13024/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
13025/// categories an open issue reads as narrows a board-scoped search with.
13026const OPEN_QUALIFIER: &str = "is:open";
13027
13028/// The board filter `task status reconcile` reads the board through: closed items alone.
13029const CLOSED_FILTER: &str = "is:closed";
13030
13031/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
13032/// one reference, and a slot the batch has none for names its last one again.
13033fn reference_batch(batch: &[IssueRef]) -> Value {
13034    let mut variables = serde_json::Map::new();
13035    for slot in 0..REFERENCE_BATCH {
13036        let issue = batch.get(slot).or(batch.last());
13037        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
13038        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
13039        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
13040    }
13041    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
13042    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
13043    variables.insert("duplicates".to_owned(), json!(true));
13044    Value::Object(variables)
13045}
13046
13047/// One issue an issue read by reference answered, in the shape every other read of an issue
13048/// hands its resolver: each board membership's `Status` value, read by name, as the one field
13049/// value that membership carries.
13050///
13051/// A membership's `status` is `null` when its card sits at no option — or on a board with no
13052/// field of that name — and otherwise an option value naming its option; anything else is not
13053/// an answer to this read, and is refused rather than read as a card at no option.
13054fn held_issue(issue: &Value) -> Result<Value, SourceError> {
13055    let malformed = |what: &str| SourceError::Malformed {
13056        message: format!("GitHub answered an issue read by reference with {what}"),
13057    };
13058    let mut issue = issue.clone();
13059    let nodes = issue
13060        .pointer_mut("/projectItems/nodes")
13061        .and_then(Value::as_array_mut)
13062        .ok_or_else(|| malformed("no list of board memberships"))?;
13063    for node in nodes {
13064        let node = node
13065            .as_object_mut()
13066            .ok_or_else(|| malformed("a board membership that is not an object"))?;
13067        let status = match node.get("status") {
13068            None => return Err(malformed("a board membership with no status member")),
13069            Some(Value::Null) => None,
13070            Some(status) if status.get("name").is_some_and(Value::is_string) => {
13071                Some(status.clone())
13072            }
13073            // A single-select value of another field type answers `{}`; only an option value
13074            // carries a name.
13075            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
13076            Some(_) => return Err(malformed("a membership status that names no option")),
13077        };
13078        node.insert(
13079            "fieldValues".to_owned(),
13080            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
13081                   "pageInfo": {"hasNextPage": false}}),
13082        );
13083    }
13084    Ok(issue)
13085}
13086
13087/// The issue one slot of a read by reference answered, `None` when GitHub answered that
13088/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
13089/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
13090/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
13091/// than a reference nothing holds.
13092fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
13093    let alias = format!("r{slot}");
13094    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
13095        message: format!("GitHub answered a read by reference with no {alias}"),
13096    })?;
13097    if repository.is_null() {
13098        return Ok(None);
13099    }
13100    let issue = repository
13101        .get("issue")
13102        .ok_or_else(|| SourceError::Malformed {
13103            message: format!("GitHub answered {alias} of a read by reference with no issue"),
13104        })?;
13105    if issue.is_null() {
13106        return Ok(None);
13107    }
13108    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
13109    if optional_str(issue, "__typename")? != Some("Issue") {
13110        return Err(SourceError::Malformed {
13111            message: format!(
13112                "GitHub answered {alias} of a read by reference with an issue whose __typename \
13113                 is not Issue"
13114            ),
13115        });
13116    }
13117    Ok(Some(issue))
13118}
13119
13120/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
13121fn spoken_reason(reason: &str) -> String {
13122    reason.to_ascii_lowercase().replace('_', " ")
13123}
13124
13125impl GitHubProjectsSource {
13126    /// Every task and project of this board whose card contradicts its issue's closure, each
13127    /// judged against its own kind's half of `status_mapping`, read through the board's own
13128    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
13129    ///
13130    /// Each item the filter answers is confirmed from its own node: an open one, one whose
13131    /// closure reads `unknown`, and one whose card is already in line are left out whatever
13132    /// the filter let through. The board's fields ride along and are held for the option
13133    /// writes that follow, so those need no read of the board.
13134    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13135        let mut after: Option<String> = None;
13136        let mut found = Vec::new();
13137        loop {
13138            let data = self
13139                .graphql(
13140                    graphql::CLOSED_ITEMS,
13141                    json!({"owner":self.owner,"number":self.project_number,
13142                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
13143                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
13144                )
13145                .await?;
13146            let board = data
13147                .pointer("/closedItems/projectV2")
13148                .filter(|value| !value.is_null())
13149                .ok_or_else(|| SourceError::Refused {
13150                    message: format!(
13151                        "GitHub project {}/{} was not found or is not visible to the token",
13152                        self.owner, self.project_number
13153                    ),
13154                })?;
13155            *self.fields_cache()? = Some(BoardFields {
13156                id: BoardId::parse(required_str(board, "id")?)?,
13157                fields: board.get("fields").cloned().unwrap_or(Value::Null),
13158            });
13159            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
13160                message: "GitHub project closed items are missing".into(),
13161            })?;
13162            for item in items
13163                .get("nodes")
13164                .and_then(Value::as_array)
13165                .ok_or_else(|| SourceError::Malformed {
13166                    message: "GitHub project items.nodes is not an array".into(),
13167                })?
13168            {
13169                let Some(resolved) = self.resolve(item)? else {
13170                    continue;
13171                };
13172                let BoardKind::Work(kind) = resolved.kind else {
13173                    continue;
13174                };
13175                let Some((closed_as, name)) = self.statuses.contradicted(
13176                    kind,
13177                    resolved.option.as_deref(),
13178                    resolved.closed,
13179                    resolved.reason.as_deref(),
13180                ) else {
13181                    continue;
13182                };
13183                found.push(StatusContradiction {
13184                    closed_as,
13185                    to: name.to_owned(),
13186                    option: resolved.option.clone(),
13187                    item: match kind {
13188                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
13189                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
13190                    },
13191                });
13192            }
13193            let info = items
13194                .get("pageInfo")
13195                .ok_or_else(|| SourceError::Malformed {
13196                    message: "GitHub project items have no pageInfo".into(),
13197                })?;
13198            if !required_bool(info, "hasNextPage")? {
13199                return Ok(found);
13200            }
13201            let next = required_str(info, "endCursor")?;
13202            validate_cursor_progress(after.as_deref(), next)?;
13203            after = Some(next.to_owned());
13204        }
13205    }
13206
13207    /// What this board holds for each issue in `issues`, once each in the order given, read
13208    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
13209    ///
13210    /// An issue GitHub cannot find for this source's credential, and one with no item on this
13211    /// board, are both absent. A closure that already says the work will not be done here —
13212    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
13213    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
13214    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
13215    /// tasks as sub-issues — is held under exactly these rules, judged against the project
13216    /// kind's half of `status_mapping`.
13217    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13218        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
13219        for issue in issues {
13220            if !distinct.contains(issue) {
13221                distinct.push(issue.clone());
13222            }
13223        }
13224        let done = |kind: ItemKind| {
13225            self.statuses
13226                .target(kind, StatusCategory::Done)
13227                .option()
13228                .unwrap_or("Done")
13229                .to_owned()
13230        };
13231        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13232        // its range names no issue any repository can hold: it is answered absent and never sent,
13233        // where binding it would have GitHub refuse the whole batch.
13234        let readable: Vec<IssueRef> = distinct
13235            .iter()
13236            .filter(|issue| i32::try_from(issue.number).is_ok())
13237            .cloned()
13238            .collect();
13239        let mut holdings = Vec::with_capacity(readable.len());
13240        for batch in readable.chunks(REFERENCE_BATCH) {
13241            let data = self
13242                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13243                .await?;
13244            for (slot, issue) in batch.iter().enumerate() {
13245                let node = referenced_issue(&data, slot)?;
13246                let resolved = match node {
13247                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13248                    None => None,
13249                };
13250                // A task and a project are both held; a document is not work, so nothing is.
13251                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13252                    BoardKind::Work(kind) => Some((item, kind)),
13253                    BoardKind::Document => None,
13254                }) else {
13255                    holdings.push(IssueHolding {
13256                        issue: issue.clone(),
13257                        completion: Completion::Absent,
13258                    });
13259                    continue;
13260                };
13261                let task = match kind {
13262                    ItemKind::Task => {
13263                        let held = item.task()?;
13264                        HeldTask {
13265                            id: held.id,
13266                            kind,
13267                            option: item.option.clone(),
13268                            classification: held.classification,
13269                            repositories: held.repositories,
13270                            delivers: held.delivers,
13271                        }
13272                    }
13273                    // A project delivers no task, so what its write keeps in step is nothing.
13274                    ItemKind::Project => {
13275                        let held = item.project();
13276                        HeldTask {
13277                            id: held.id,
13278                            kind,
13279                            option: item.option.clone(),
13280                            classification: held.classification,
13281                            repositories: held.repositories,
13282                            delivers: Vec::new(),
13283                        }
13284                    }
13285                };
13286                let done = done(kind);
13287                let completion = match (item.closed, item.reason.as_deref()) {
13288                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13289                        task,
13290                        reason: format!("closed as {}", spoken_reason(reason)),
13291                    },
13292                    (true, None | Some("COMPLETED"))
13293                        if item
13294                            .option
13295                            .as_deref()
13296                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13297                    {
13298                        Completion::Unchanged { task }
13299                    }
13300                    _ => {
13301                        let kept = item.clone();
13302                        self.held_cache
13303                            .lock()
13304                            .map_err(|_| SourceError::Unavailable {
13305                                message: "items read by issue reference were left \
13306                                          inconsistent; run the command again"
13307                                    .into(),
13308                            })?
13309                            .insert(kept.id.clone(), kept);
13310                        Completion::Due {
13311                            task,
13312                            to: done.clone(),
13313                        }
13314                    }
13315                };
13316                holdings.push(IssueHolding {
13317                    issue: issue.clone(),
13318                    completion,
13319                });
13320            }
13321        }
13322        let mut read = holdings.into_iter();
13323        let mut answered = Vec::with_capacity(distinct.len());
13324        for issue in distinct {
13325            if i32::try_from(issue.number).is_ok() {
13326                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13327                    message: format!("no holding was read for {issue}"),
13328                })?);
13329            } else {
13330                answered.push(IssueHolding {
13331                    issue,
13332                    completion: Completion::Absent,
13333                });
13334            }
13335        }
13336        Ok(answered)
13337    }
13338}
13339
13340#[async_trait::async_trait]
13341impl TaskSource for GitHubProjectsSource {
13342    fn kind(&self) -> &'static str {
13343        KIND
13344    }
13345    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13346        change_token::order(a, b)
13347    }
13348    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13349    /// member, the change each part of the token moves for. Not its forward edges — see
13350    /// [`FAR_ENDS_UNCOVERED`].
13351    fn version_coverage(&self) -> VersionCoverage {
13352        VersionCoverage {
13353            forward_edges: Coverage::Uncovered {
13354                reason: FAR_ENDS_UNCOVERED.to_owned(),
13355            },
13356            ..VersionCoverage::whole()
13357        }
13358    }
13359    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13360    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13361    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13362    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13363    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13364    // 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.
13365    fn copy_version_coverage(&self) -> VersionCoverage {
13366        VersionCoverage::whole()
13367    }
13368    /// A project's fields, and not its forward edges, for the same reason a task's are not.
13369    fn project_version_coverage(&self) -> ProjectVersionCoverage {
13370        ProjectVersionCoverage {
13371            fields: Coverage::Covered,
13372            forward_edges: Coverage::Uncovered {
13373                reason: FAR_ENDS_UNCOVERED.to_owned(),
13374            },
13375        }
13376    }
13377    /// A document's fields and the manifest of its image assets, which its metadata slot
13378    /// records: both are the issue's body, whose every edit moves the token's issue time.
13379    fn document_version_coverage(&self) -> DocumentVersionCoverage {
13380        DocumentVersionCoverage::whole()
13381    }
13382    fn write_context(&self, id: &NativeId) -> Option<Value> {
13383        self.context_of(id)
13384    }
13385    fn board_context(&self) -> Option<Value> {
13386        self.board_context_held()
13387    }
13388    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13389        self.prime(id, context, board)
13390    }
13391    fn unprime(&self, id: &NativeId) {
13392        self.unprime_held(id);
13393    }
13394    fn placement_context(&self) -> Option<Value> {
13395        self.placement_held()
13396    }
13397    fn prime_placement(&self, context: &Value) -> bool {
13398        self.prime_placement_held(context)
13399    }
13400    fn written_context(&self, id: &NativeId) -> Option<Value> {
13401        self.written_held(id)
13402    }
13403    fn prime_written(&self, id: &NativeId, context: &Value, task: &Task) -> bool {
13404        self.prime_written_held(id, context, task)
13405    }
13406    fn begin_journal(&self) {
13407        self.expected.begin();
13408    }
13409    fn end_journal(&self) -> Option<WriteJournal> {
13410        Some(self.expected.end())
13411    }
13412    fn capabilities(&self) -> Capabilities {
13413        Capabilities {
13414            projects: Support::Native,
13415            documents: Support::Native,
13416            comments: Support::Native,
13417            assets: Support::Native,
13418            priority: if self.priorities.is_some() {
13419                Support::Native
13420            } else {
13421                Support::Unsupported
13422            },
13423            filter_by_priority: Support::Native,
13424            filter_by_comment_activity: Support::Native,
13425            filter_by_metadata: Support::Native,
13426            filter_by_origin: Support::Native,
13427            reconcile_status: Support::Native,
13428            complete_by_issue: Support::Native,
13429            orphan_tasks: Support::Native,
13430            filter_by_label: Support::Native,
13431            filter_by_status: Support::Native,
13432            search_title: Support::Native,
13433            search_content: Support::Native,
13434            task_dependencies: DependencySupport::BothDirections,
13435            project_dependencies: DependencySupport::BothDirections,
13436            max_page_size: MAX_PAGE_SIZE,
13437        }
13438    }
13439    async fn visibility(
13440        &self,
13441        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13442    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13443        self.write_visibility(target).await
13444    }
13445    async fn visibility_with_project(
13446        &self,
13447        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13448        project: &onetaskgraph_plugin_api::WriteTarget<'_>,
13449    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13450        self.write_visibility_with_project(target, project).await
13451    }
13452    fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
13453        self.declared_private.store(
13454            declared == onetaskgraph_plugin_api::Visibility::Private,
13455            std::sync::atomic::Ordering::SeqCst,
13456        );
13457    }
13458    fn scope_visibility<'a>(
13459        &'a self,
13460        operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
13461    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
13462        self.scope_visibility_reads(operation)
13463    }
13464
13465    /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13466    /// the create applies — reading the parent project's issue only when `target` names one.
13467    async fn creation_repository(
13468        &self,
13469        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13470    ) -> Result<Option<Repository>, SourceError> {
13471        let onetaskgraph_plugin_api::WriteTarget::New {
13472            repositories,
13473            project,
13474        } = target
13475        else {
13476            return Ok(None);
13477        };
13478        let target = self
13479            .creation_rule(repositories, *project, "a new item")
13480            .await?;
13481        Repository::try_from(target.origin())
13482            .map(Some)
13483            .map_err(|message| SourceError::Malformed { message })
13484    }
13485    /// Every issue of the repository a create of `target` files in whose body carries `key` in
13486    /// its metadata slot, read off that repository's issues newest first back to `since` —
13487    /// never through search, whose index lags a fresh issue by seconds.
13488    async fn created_with_key(
13489        &self,
13490        record: onetaskgraph_plugin_api::MetadataRecord,
13491        key: &str,
13492        since: Option<DateTime<Utc>>,
13493        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13494    ) -> Result<Vec<NativeId>, SourceError> {
13495        let _ = record;
13496        let onetaskgraph_plugin_api::WriteTarget::New {
13497            repositories,
13498            project,
13499        } = target
13500        else {
13501            return Ok(Vec::new());
13502        };
13503        let repository = self
13504            .creation_rule(repositories, *project, "a new item")
13505            .await?;
13506        let wanted = Value::String(key.to_owned());
13507        let mut found = Vec::new();
13508        let mut after: Option<String> = None;
13509        let mut followed = std::collections::HashSet::new();
13510        loop {
13511            let data = self
13512                .graphql(
13513                    graphql::ISSUES_BY_CREATION,
13514                    json!({"owner": repository.owner, "name": repository.name,
13515                           "first": MAX_PAGE_SIZE, "after": after}),
13516                )
13517                .await?;
13518            let issues =
13519                data.pointer("/repository/issues")
13520                    .ok_or_else(|| SourceError::Malformed {
13521                        message: format!(
13522                            "GitHub answered no issues for repository {}",
13523                            repository.slug()
13524                        ),
13525                    })?;
13526            let malformed = |what: &str| SourceError::Malformed {
13527                message: format!(
13528                    "GitHub answered a page of repository {}'s issues {what}",
13529                    repository.slug()
13530                ),
13531            };
13532            let nodes = issues
13533                .get("nodes")
13534                .and_then(Value::as_array)
13535                .ok_or_else(|| malformed("with no list of issues"))?;
13536            let mut older = false;
13537            for node in nodes {
13538                let id = node
13539                    .get("id")
13540                    .and_then(Value::as_str)
13541                    .filter(|id| !id.is_empty())
13542                    .ok_or_else(|| malformed("holding an issue with no id"))?;
13543                let created = optional_time(node, "createdAt")?
13544                    .ok_or_else(|| malformed("holding an issue with no creation time"))?;
13545                if since.is_some_and(|since| created < since) {
13546                    older = true;
13547                    break;
13548                }
13549                let body = match node.get("body") {
13550                    Some(Value::String(body)) => Some(body.clone()),
13551                    Some(Value::Null) => None,
13552                    _ => return Err(malformed("holding an issue with no body member")),
13553                };
13554                let (_, metadata) = metadata_body(body)?;
13555                if metadata.get(MetadataKey::CREATE_KEY) == Some(&wanted) {
13556                    found.push(NativeId(id.to_owned()));
13557                }
13558            }
13559            let next = issues
13560                .pointer("/pageInfo/hasNextPage")
13561                .and_then(Value::as_bool)
13562                .ok_or_else(|| malformed("that does not say whether another follows"))?;
13563            if older || !next {
13564                return Ok(found);
13565            }
13566            // A page that says another follows names where it starts, and never a page already
13567            // read: anything else would ask for the same pages again for ever.
13568            let cursor = issues
13569                .pointer("/pageInfo/endCursor")
13570                .and_then(Value::as_str)
13571                .filter(|cursor| !cursor.is_empty())
13572                .map(str::to_owned);
13573            if cursor
13574                .as_ref()
13575                .is_none_or(|cursor| !followed.insert(cursor.clone()))
13576            {
13577                return Err(SourceError::Malformed {
13578                    message: format!(
13579                        "GitHub answered a page of repository {}'s issues that says another \
13580                         follows without a new cursor to it",
13581                        repository.slug()
13582                    ),
13583                });
13584            }
13585            after = cursor;
13586        }
13587    }
13588
13589    /// `Repository.issues` lists an issue GitHub committed only once its listing catches up.
13590    fn create_key_lags(&self) -> bool {
13591        true
13592    }
13593
13594    /// The task as this process's own latest write of it left it, from the item it recorded
13595    /// that write as — never a request. Its `version` is always `None`: a board's change token
13596    /// is given only once an item has gone unchanged for [`change_token::SETTLED`], which an
13597    /// item this command has just written has not, so no write's own answer can vouch for one.
13598    async fn written_task(&self, id: &NativeId) -> Option<Task> {
13599        let held = self
13600            .created()
13601            .ok()?
13602            .iter()
13603            .find(|held| held.id == *id)
13604            .cloned()
13605            .or_else(|| {
13606                self.updated()
13607                    .ok()?
13608                    .iter()
13609                    .find(|held| held.id == *id)
13610                    .cloned()
13611            })?;
13612        let mut task = held.task().ok()?;
13613        task.version = None;
13614        Some(task)
13615    }
13616
13617    /// Why `creation_owners` refuses a create of `target`, by [`Self::creation_rule`] — read
13618    /// only when the list is not empty, so a source restricting nothing reads nothing.
13619    async fn creation_refusal(
13620        &self,
13621        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13622    ) -> Result<Option<String>, SourceError> {
13623        let onetaskgraph_plugin_api::WriteTarget::New {
13624            repositories,
13625            project,
13626        } = target
13627        else {
13628            return Ok(None);
13629        };
13630        if self.creation_owners.is_empty() {
13631            return Ok(None);
13632        }
13633        let target = self
13634            .creation_rule(repositories, *project, "a new item")
13635            .await?;
13636        Ok(self.owner_refusal(&target))
13637    }
13638
13639    async fn health(&self) -> Result<Health, SourceError> {
13640        let board = self.board_page(None, 1).await?;
13641        Ok(Health {
13642            reachable: true,
13643            detail: Some(format!(
13644                "reading GitHub project {}/{} ({})",
13645                self.owner,
13646                self.project_number,
13647                required_str(&board, "title")?
13648            )),
13649        })
13650    }
13651    /// The board's own node id, digested: the one request reading its fields, or none when
13652    /// this command has read them already.
13653    async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13654        let board = self.board_fields().await?;
13655        Designator::of_backend(KIND, board.id.as_str())
13656            .map(Some)
13657            .map_err(|message| SourceError::Malformed { message })
13658    }
13659    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13660        self.item_by_id(id)
13661            .await?
13662            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13663            .map(|item| item.task())
13664            .transpose()
13665    }
13666    async fn task_assets(
13667        &self,
13668        id: &NativeId,
13669    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13670        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13671    }
13672    async fn task_asset(
13673        &self,
13674        id: &NativeId,
13675        name: &onetaskgraph_plugin_api::AssetName,
13676    ) -> Result<Option<Vec<u8>>, SourceError> {
13677        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13678            .await
13679    }
13680    async fn set_task_rendering_with_assets(
13681        &self,
13682        id: &NativeId,
13683        content: &str,
13684        provenance: &Value,
13685        _answers: &BTreeMap<String, Value>,
13686        assets: &onetaskgraph_plugin_api::AssetWrite,
13687    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13688        self.replace_rendering(
13689            id,
13690            BoardKind::Work(ItemKind::Task),
13691            content,
13692            provenance,
13693            Some(assets),
13694        )
13695        .await
13696    }
13697    async fn document_assets(
13698        &self,
13699        id: &NativeId,
13700    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13701        self.held_assets(id, BoardKind::Document).await
13702    }
13703    async fn document_asset(
13704        &self,
13705        id: &NativeId,
13706        name: &onetaskgraph_plugin_api::AssetName,
13707    ) -> Result<Option<Vec<u8>>, SourceError> {
13708        self.held_asset(id, BoardKind::Document, name).await
13709    }
13710    async fn set_document_rendering_with_assets(
13711        &self,
13712        id: &NativeId,
13713        content: &str,
13714        provenance: &Value,
13715        _answers: &BTreeMap<String, Value>,
13716        assets: &onetaskgraph_plugin_api::AssetWrite,
13717    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13718        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13719            .await
13720    }
13721    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13722        Ok(self
13723            .item_by_id(id)
13724            .await?
13725            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13726            .map(|item| item.project()))
13727    }
13728    async fn query_tasks(
13729        &self,
13730        query: &TaskQuery,
13731        page: &PageRequest,
13732    ) -> Result<Page<Task>, SourceError> {
13733        validate_page(page)?;
13734        refuse_unsearchable(query)?;
13735        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13736        // category — `unknown`; every other category reads only off an open issue or a draft. So
13737        // a status filter naming none of those three asks every issue search it sends for open
13738        // issues alone; each candidate's category is still confirmed from its own item below.
13739        let open_only = !query.statuses.is_empty()
13740            && query.statuses.iter().all(|category| {
13741                !matches!(
13742                    category,
13743                    StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13744                )
13745            });
13746        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13747            let mut qualifiers: Vec<String> = [
13748                query.commented_since.map(updated_qualifier),
13749                narrowing_qualifiers(query),
13750            ]
13751            .into_iter()
13752            .flatten()
13753            .collect();
13754            // A narrowed search never answers a draft, which is no issue, so narrowing it to
13755            // open issues too leaves out nothing such a search could have answered.
13756            if !qualifiers.is_empty() {
13757                if open_only {
13758                    qualifiers.push(OPEN_QUALIFIER.to_owned());
13759                }
13760                return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13761            }
13762        }
13763
13764        // A read narrowed to one project asks that project for its own tasks, so nothing
13765        // about it costs what the rest of the board holds. A read carrying a text, metadata
13766        // or origin predicate asks GitHub the narrower question those predicates are, and a
13767        // read narrowed to comment activity alone asks the board's own issue search for the
13768        // issues updated since, which is every issue a comment could have been written or
13769        // edited on since. Every other task read is a question about the whole board and is
13770        // answered by reading it.
13771        let (held, membership) = match (&query.project, query.commented_since) {
13772            (ProjectFilter::Is(project), _) => (
13773                match self.held_task_children(project)? {
13774                    // Taken as `project_children` takes the project's own sub-issue read: with
13775                    // every item this process wrote under it since, and none it moved away.
13776                    Some(held) => self.completed_with_written(
13777                        held.into_iter()
13778                            .filter(|child| child.parent.as_ref() == Some(project))
13779                            .collect(),
13780                        |own| own.parent.as_ref() == Some(project),
13781                    )?,
13782                    None => self.project_children(project).await?,
13783                },
13784                // Answered by where these items came from; see `task_matches`.
13785                &ProjectFilter::Any,
13786            ),
13787            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13788                match (self.narrowed(query).await?, since) {
13789                    (Some(narrowed), _) => (narrowed, &query.project),
13790                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13791                    // The board's own items list every draft; only the search half, which
13792                    // completes them with issues they lag behind on, is narrowed — a closed
13793                    // issue that half leaves out is at no category the filter keeps.
13794                    (None, None) if open_only => (
13795                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13796                        &query.project,
13797                    ),
13798                    (None, None) => (self.board().await?.items, &query.project),
13799                }
13800            }
13801        };
13802        // Filtered before paged: a page of a filtered result is a page of the survivors,
13803        // never the survivors of a page.
13804        let mut tasks = Vec::new();
13805        for item in held
13806            .iter()
13807            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13808        {
13809            let task = item.task()?;
13810            if task_matches(&task, query, membership)
13811                && self.commented_since(item, query.commented_since).await?
13812            {
13813                tasks.push(task);
13814            }
13815        }
13816        Ok(offset_page(
13817            tasks,
13818            numeric_cursor(page.cursor.as_ref())?,
13819            page.limit.min(MAX_PAGE_SIZE) as usize,
13820        ))
13821    }
13822    async fn query_projects(
13823        &self,
13824        query: &ProjectQuery,
13825        page: &PageRequest,
13826    ) -> Result<Page<Project>, SourceError> {
13827        validate_page(page)?;
13828        refuse_unsearchable_text(query.text.as_ref())?;
13829        // The projects a board holds are found by an issue search scoped to that board,
13830        // never by walking the board's own item connection: what tells a project from a
13831        // task is the `parent` each issue carries, which costs nothing to read. A query
13832        // carrying a text asks that search for the text too, so it reads the issues that
13833        // hold it rather than every issue of the board.
13834        let held = match self.text_searched(query.text.as_ref()).await? {
13835            Some(searched) => searched,
13836            None => self.board_issues().await?,
13837        };
13838        let projects = held
13839            .iter()
13840            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13841            .map(Resolved::project)
13842            .filter(|project| project_matches(project, query))
13843            .collect();
13844        Ok(offset_page(
13845            projects,
13846            numeric_cursor(page.cursor.as_ref())?,
13847            page.limit.min(MAX_PAGE_SIZE) as usize,
13848        ))
13849    }
13850    async fn query_projects_by_origin(
13851        &self,
13852        origin: &str,
13853        page: &PageRequest,
13854    ) -> Result<Page<Project>, SourceError> {
13855        validate_page(page)?;
13856        let projects = self
13857            .copied_from(origin, BoardKind::Work(ItemKind::Project))
13858            .await?
13859            .iter()
13860            .map(Resolved::project)
13861            .collect();
13862        Ok(offset_page(
13863            projects,
13864            numeric_cursor(page.cursor.as_ref())?,
13865            page.limit.min(MAX_PAGE_SIZE) as usize,
13866        ))
13867    }
13868    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13869        let Some(item) = self
13870            .document_by_id(id)
13871            .await?
13872            .filter(|item| item.kind == BoardKind::Document)
13873        else {
13874            return Ok(None);
13875        };
13876        self.expected.read_document(&item);
13877        Ok(Some(item.document()))
13878    }
13879    async fn query_documents_by_origin(
13880        &self,
13881        origin: &str,
13882        page: &PageRequest,
13883    ) -> Result<Page<Document>, SourceError> {
13884        validate_page(page)?;
13885        let documents = self
13886            .copied_from(origin, BoardKind::Document)
13887            .await?
13888            .iter()
13889            .map(Resolved::document)
13890            .collect();
13891        Ok(offset_page(
13892            documents,
13893            numeric_cursor(page.cursor.as_ref())?,
13894            page.limit.min(MAX_PAGE_SIZE) as usize,
13895        ))
13896    }
13897    async fn query_documents(
13898        &self,
13899        query: &DocumentQuery,
13900        page: &PageRequest,
13901    ) -> Result<Page<Document>, SourceError> {
13902        validate_page(page)?;
13903        // Narrowed to one project, this is the same sub-issue read a task list scoped to
13904        // that project makes — a document filed under a project is a sub-issue of it too,
13905        // and which of them come back is the kind this caller asked for. Unscoped, a query
13906        // carrying a text asks the board-scoped issue search for it, as a task query does,
13907        // and only one carrying none reads the board.
13908        let (held, membership) = match &query.project {
13909            ProjectFilter::Is(project) => (
13910                self.project_children(project).await?,
13911                // Answered by where these items came from; see `task_matches`.
13912                &ProjectFilter::Any,
13913            ),
13914            ProjectFilter::Any | ProjectFilter::Orphans => {
13915                refuse_unsearchable_text(query.text.as_ref())?;
13916                match self.text_searched(query.text.as_ref()).await? {
13917                    Some(searched) => (searched, &query.project),
13918                    None => (self.board().await?.items, &query.project),
13919                }
13920            }
13921        };
13922        // Filtered before paged, exactly as a task read is: a page of a filtered result is
13923        // a page of the survivors, never the survivors of a page.
13924        let documents = held
13925            .iter()
13926            .filter(|item| item.kind == BoardKind::Document)
13927            .map(Resolved::document)
13928            .filter(|document| document_matches(document, query, membership))
13929            .collect();
13930        Ok(offset_page(
13931            documents,
13932            numeric_cursor(page.cursor.as_ref())?,
13933            page.limit.min(MAX_PAGE_SIZE) as usize,
13934        ))
13935    }
13936    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13937        validate_page(page)?;
13938        let offset = numeric_cursor(page.cursor.as_ref())?;
13939        let mut labels = self
13940            .board()
13941            .await?
13942            .items
13943            .into_iter()
13944            .flat_map(|item| item.labels)
13945            .fold(Vec::new(), |mut all, label| {
13946                if !all.iter().any(|x: &Label| x.id == label.id) {
13947                    all.push(label);
13948                }
13949                all
13950            });
13951        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13952        Ok(offset_page(
13953            labels,
13954            offset,
13955            page.limit.min(MAX_PAGE_SIZE) as usize,
13956        ))
13957    }
13958    async fn task_dependencies(
13959        &self,
13960        id: &NativeId,
13961        direction: Direction,
13962        page: &PageRequest,
13963    ) -> Result<Page<DependencyEdge>, SourceError> {
13964        self.dependencies(id, ItemKind::Task, direction, page).await
13965    }
13966    async fn project_dependencies(
13967        &self,
13968        id: &NativeId,
13969        direction: Direction,
13970        page: &PageRequest,
13971    ) -> Result<Page<DependencyEdge>, SourceError> {
13972        self.dependencies(id, ItemKind::Project, direction, page)
13973            .await
13974    }
13975
13976    fn writes(&self) -> WriteSupport {
13977        WriteSupport::Supported
13978    }
13979
13980    /// Create or update one task.
13981    ///
13982    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
13983    /// neither may name the task itself or name one task twice — and land in the body's
13984    /// metadata slot under their reserved keys, in place of any caller metadata of those
13985    /// names.
13986    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
13987        self.write_task_assets(write, None)
13988            .await
13989            .map(|written| written.id)
13990    }
13991
13992    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
13993        self.origins_ahead(origins).await
13994    }
13995
13996    async fn member_classifications(
13997        &self,
13998        project: &NativeId,
13999    ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
14000        Ok(self
14001            .member_classes
14002            .lock()
14003            .map_err(|_| SourceError::Unavailable {
14004                message: "a project's members held for its classification were left \
14005                          inconsistent; run the command again"
14006                    .into(),
14007            })?
14008            .remove(project))
14009    }
14010
14011    /// The issue's own web address, as the `createIssue` that made it in this command answered
14012    /// it: a task, a project and a document are each an issue here.
14013    fn written_location(
14014        &self,
14015        _record: onetaskgraph_plugin_api::MetadataRecord,
14016        id: &NativeId,
14017    ) -> Option<Location> {
14018        self.written
14019            .lock()
14020            .unwrap_or_else(std::sync::PoisonError::into_inner)
14021            .get(id)
14022            .cloned()
14023            .map(Location::Url)
14024    }
14025
14026    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
14027    /// in one request, and a copy's tasks are created together only then, since their fields
14028    /// and edges are what is held back.
14029    fn create_batch_limit(&self) -> usize {
14030        if self.deferring().unwrap_or(false) {
14031            MUTATION_BATCH
14032        } else {
14033            1
14034        }
14035    }
14036
14037    // 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.
14038    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
14039        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
14040            return Err(SourceError::Refused {
14041                message: format!(
14042                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
14043                     next: update it with write_task",
14044                    write.item.id,
14045                    write.target.as_ref().map_or("", |target| target.0.as_str())
14046                ),
14047            });
14048        }
14049        if !self.deferring()? {
14050            let mut created = Vec::with_capacity(writes.len());
14051            for write in writes {
14052                match self.write_task(write).await {
14053                    Ok(id) => created.push(id),
14054                    Err(error) => {
14055                        let mut left: Vec<&str> = Vec::new();
14056                        for id in &created {
14057                            if self.delete_issue(id).await.is_err() {
14058                                left.push(id.0.as_str());
14059                            }
14060                        }
14061                        return Err(left_behind(error, &left));
14062                    }
14063                }
14064            }
14065            return Ok(created);
14066        }
14067        self.create_together(writes).await
14068    }
14069
14070    fn begin_deferred_writes(&self) {
14071        if let Ok(mut deferred) = self.deferred.lock() {
14072            deferred.get_or_insert_with(Deferred::default);
14073        }
14074    }
14075
14076    async fn finish_writes(&self) -> Result<(), SourceError> {
14077        self.send_held_updates().await?;
14078        self.send_held_back().await
14079    }
14080
14081    async fn write_task_with_assets(
14082        &self,
14083        write: &ItemWrite<Task>,
14084        _answers: Option<&BTreeMap<String, Value>>,
14085        assets: &onetaskgraph_plugin_api::AssetWrite,
14086    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14087        self.write_task_assets(write, Some(assets)).await
14088    }
14089
14090    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
14091        self.write_item(
14092            &Incoming {
14093                written: Written::Work(ItemKind::Project, &write.item.status),
14094                title: &write.item.title,
14095                content: write.item.content.as_deref(),
14096                assets: None,
14097                labels: &write.item.labels,
14098                metadata: &write.item.metadata,
14099                repositories: &write.item.repositories,
14100                classification: write.item.classification,
14101                parent: None,
14102                delivers: &[],
14103                delivered_by: &[],
14104                priority: None,
14105            },
14106            write.target.as_ref(),
14107            &write.depends_on,
14108        )
14109        .await
14110        .map(|written| written.id)
14111    }
14112
14113    /// Create or update one document, which is one issue titled the way this board spells
14114    /// a document.
14115    ///
14116    /// Everything else is exactly a task write: caller metadata goes to the same canonical
14117    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
14118    /// or a field this board cannot carry is refused by name rather than dropped, a target
14119    /// naming an issue this board does not hold is refused rather than created, and an
14120    /// issue this call created is taken back when the rest of the write fails.
14121    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
14122        self.write_document_assets(write, None)
14123            .await
14124            .map(|written| written.id)
14125    }
14126
14127    async fn write_document_with_assets(
14128        &self,
14129        write: &ItemWrite<Document>,
14130        _answers: Option<&BTreeMap<String, Value>>,
14131        assets: &onetaskgraph_plugin_api::AssetWrite,
14132    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14133        self.write_document_assets(write, Some(assets)).await
14134    }
14135
14136    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
14137    /// which reads nothing; then the board's `Status` option. Over an existing item that is
14138    /// read off the item, as the write reads it, and the item is held among this command's
14139    /// resolved records so the write that follows reuses that read rather than repeating it;
14140    /// an item that does not carry the field takes the board's fields, which are held once
14141    /// read. A create is checked against the board's fields only when this command already
14142    /// holds them, because a create reads them together with its repository, in one request,
14143    /// and refuses a missing option before it writes anything.
14144    async fn check_status_write(
14145        &self,
14146        kind: ItemKind,
14147        category: StatusCategory,
14148        target: Option<&NativeId>,
14149    ) -> Result<(), SourceError> {
14150        let status = self.resolved_target(kind, category)?;
14151        if status.option().is_none() {
14152            return Ok(());
14153        }
14154        let fields = match target {
14155            Some(target) => {
14156                // A target this board does not hold is the write's own refusal to make.
14157                let Some(item) = self.bound_item(target).await? else {
14158                    return Ok(());
14159                };
14160                self.resolved_cache()?.insert(target.clone(), item.clone());
14161                // Only the `Status` field decides whether the write can land.
14162                self.fields_for(Some(&item), false, true, false)
14163                    .await?
14164                    .fields
14165            }
14166            None => {
14167                let held = self
14168                    .board_cache()?
14169                    .as_ref()
14170                    .map(|board| board.fields.clone());
14171                match held.or_else(|| {
14172                    self.fields_cache()
14173                        .ok()
14174                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
14175                }) {
14176                    Some(fields) => fields,
14177                    None => return Ok(()),
14178                }
14179            }
14180        };
14181        self.column_for(&fields, kind, category, &status)
14182            .map(|_| ())
14183    }
14184
14185    /// What a write of `status` reads back as: the `Status` option its category maps to for
14186    /// `kind`, and for a closing category the closure that option and its fixed reason read
14187    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
14188    /// this instance has no option for is answered as given, since writing it is refused.
14189    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
14190        match self.statuses.target(kind, status.category) {
14191            StatusTarget::Column(name) => {
14192                self.statuses.status(kind, Some(name.as_str()), false, None)
14193            }
14194            StatusTarget::Terminal(name, reason) => {
14195                self.statuses
14196                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
14197            }
14198            StatusTarget::Disabled(_) => status.clone(),
14199        }
14200    }
14201
14202    /// Set one task's status alone.
14203    ///
14204    /// An open target reopens a closed issue with an `updateIssue` carrying only its
14205    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
14206    /// terminal target selects its mapped option, then closes with its fixed reason. No
14207    /// request carries a title, a body or a label. The status
14208    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
14209    /// what a re-read reports.
14210    async fn set_task_status(
14211        &self,
14212        id: &NativeId,
14213        category: StatusCategory,
14214    ) -> Result<Option<Status>, SourceError> {
14215        self.set_status(id, ItemKind::Task, category).await
14216    }
14217
14218    /// Set one task's status and answer the task with it, reading the item once: the read the
14219    /// task is answered from is handed to the status write, which would otherwise read the
14220    /// same issue again — two requests on a fresh process, the read and the field write,
14221    /// where the default's `get_task` before `set_task_status` sent three.
14222    async fn set_task_status_reading(
14223        &self,
14224        id: &NativeId,
14225        category: StatusCategory,
14226    ) -> Result<Option<Task>, SourceError> {
14227        // Refused before anything is read, in the words the write itself refuses it.
14228        self.resolved_target(ItemKind::Task, category)?;
14229        let Some(item) = self
14230            .bound_item(id)
14231            .await?
14232            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
14233        else {
14234            return Ok(None);
14235        };
14236        let mut task = item.task()?;
14237        self.held_cache
14238            .lock()
14239            .map_err(|_| SourceError::Unavailable {
14240                message: "items read by issue reference were left inconsistent; run the \
14241                          command again"
14242                    .into(),
14243            })?
14244            .insert(id.clone(), item);
14245        let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
14246            return Ok(None);
14247        };
14248        task.status = status;
14249        Ok(Some(task))
14250    }
14251
14252    /// Set one project's status alone, by exactly the paired write a task's status takes, for
14253    /// the project kind: a terminal category selects the project kind's mapped option and then
14254    /// closes the issue with the category's fixed reason — an issue already closed with a
14255    /// reason that reads that category is left as it is — and an open category reopens a closed
14256    /// issue and then selects its option. A board draft is refused a terminal category as a
14257    /// task's is.
14258    async fn set_project_status(
14259        &self,
14260        id: &NativeId,
14261        category: StatusCategory,
14262    ) -> Result<Option<Status>, SourceError> {
14263        self.set_status(id, ItemKind::Project, category).await
14264    }
14265
14266    /// Every task and project whose card contradicts its issue's closure, read through the
14267    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
14268    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
14269        self.contradictions().await
14270    }
14271
14272    /// What this board holds for each issue reference, one request per fifty of them; see
14273    /// [`TaskSource::issue_holdings`].
14274    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
14275        self.holdings(issues).await
14276    }
14277
14278    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
14279    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
14280    /// for `none`. Refused by an instance with no `priority_mapping`.
14281    async fn set_task_priority(
14282        &self,
14283        id: &NativeId,
14284        priority: Priority,
14285    ) -> Result<Option<Priority>, SourceError> {
14286        self.set_priority(id, priority).await
14287    }
14288
14289    /// Replace one task's content with a single body update that keeps the metadata slot
14290    /// byte for byte.
14291    async fn set_task_content(
14292        &self,
14293        id: &NativeId,
14294        content: &str,
14295    ) -> Result<Option<()>, SourceError> {
14296        self.replace_content(id, content).await
14297    }
14298
14299    /// Replace one task issue's content and its provenance slot entry with a single body
14300    /// update. The answers are not kept: see `replace_rendering`.
14301    async fn set_task_rendering(
14302        &self,
14303        id: &NativeId,
14304        content: &str,
14305        provenance: &Value,
14306        _answers: &BTreeMap<String, Value>,
14307    ) -> Result<Option<()>, SourceError> {
14308        self.replace_rendering(
14309            id,
14310            BoardKind::Work(ItemKind::Task),
14311            content,
14312            provenance,
14313            None,
14314        )
14315        .await
14316        .map(|written| written.map(|_| ()))
14317    }
14318
14319    /// Replace one design-document issue's content and its provenance slot entry, on exactly
14320    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14321    async fn set_document_rendering(
14322        &self,
14323        id: &NativeId,
14324        content: &str,
14325        provenance: &Value,
14326        _answers: &BTreeMap<String, Value>,
14327    ) -> Result<Option<()>, SourceError> {
14328        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
14329            .await
14330            .map(|written| written.map(|_| ()))
14331    }
14332
14333    /// Replace one project issue's content and its provenance slot entry, on exactly the
14334    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14335    async fn set_project_rendering(
14336        &self,
14337        id: &NativeId,
14338        content: &str,
14339        provenance: &Value,
14340        _answers: &BTreeMap<String, Value>,
14341    ) -> Result<Option<()>, SourceError> {
14342        self.replace_rendering(
14343            id,
14344            BoardKind::Work(ItemKind::Project),
14345            content,
14346            provenance,
14347            None,
14348        )
14349        .await
14350        .map(|written| written.map(|_| ()))
14351    }
14352
14353    /// Apply a targeted update with one read of the item and a write only for what differs:
14354    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
14355    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
14356    async fn update_task(
14357        &self,
14358        id: &NativeId,
14359        update: &TaskUpdate,
14360    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
14361        self.targeted_update(id, update).await
14362    }
14363
14364    /// Replace one task's `delivered_by` with a single body update that changes the
14365    /// metadata slot and nothing outside it.
14366    async fn set_delivered_by(
14367        &self,
14368        id: &NativeId,
14369        delivered_by: &[TaskRef],
14370    ) -> Result<Option<()>, SourceError> {
14371        self.replace_delivered_by(id, delivered_by).await
14372    }
14373
14374    /// Set one key of one task issue's metadata with a single body update that changes the
14375    /// metadata slot and nothing outside it — no title, label, state or board field request —
14376    /// and sends nothing when the task already holds that value under the key.
14377    async fn set_task_metadata(
14378        &self,
14379        id: &NativeId,
14380        key: &MetadataKey,
14381        value: &Value,
14382    ) -> Result<Option<Task>, SourceError> {
14383        Ok(self
14384            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
14385            .await?
14386            .map(|item| item.task())
14387            .transpose()?)
14388    }
14389
14390    /// Set one key of one project issue's metadata, on exactly the terms of
14391    /// [`set_task_metadata`](TaskSource::set_task_metadata).
14392    async fn set_project_metadata(
14393        &self,
14394        id: &NativeId,
14395        key: &MetadataKey,
14396        value: &Value,
14397    ) -> Result<Option<Project>, SourceError> {
14398        Ok(self
14399            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
14400            .await?
14401            .map(|item| item.project()))
14402    }
14403
14404    /// Set one key of one design-document issue's metadata, on exactly the terms of
14405    /// [`set_task_metadata`](TaskSource::set_task_metadata).
14406    async fn set_document_metadata(
14407        &self,
14408        id: &NativeId,
14409        key: &MetadataKey,
14410        value: &Value,
14411    ) -> Result<Option<Document>, SourceError> {
14412        Ok(self
14413            .set_slot_key(id, BoardKind::Document, key, value)
14414            .await?
14415            .map(|item| item.document()))
14416    }
14417
14418    /// Take one key out of one issue's metadata slot with a single body update that changes the
14419    /// slot and nothing outside it, sending nothing when the slot does not hold the key.
14420    async fn remove_record_metadata(
14421        &self,
14422        record: onetaskgraph_plugin_api::MetadataRecord,
14423        id: &NativeId,
14424        key: &MetadataKey,
14425    ) -> Result<Option<Option<String>>, SourceError> {
14426        let kind = match record {
14427            onetaskgraph_plugin_api::MetadataRecord::Task => BoardKind::Work(ItemKind::Task),
14428            onetaskgraph_plugin_api::MetadataRecord::Project => BoardKind::Work(ItemKind::Project),
14429            onetaskgraph_plugin_api::MetadataRecord::Document => BoardKind::Document,
14430        };
14431        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
14432            return Ok(None);
14433        };
14434        if item.slot.contains_key(key.as_str()) {
14435            let mut slot = item.slot.clone();
14436            slot.remove(key.as_str());
14437            self.write_slot(&mut item, &slot).await?;
14438            self.remember_written(item.clone(), false)?;
14439        }
14440        Ok(Some(match record {
14441            onetaskgraph_plugin_api::MetadataRecord::Task => item.task()?.version,
14442            onetaskgraph_plugin_api::MetadataRecord::Project => item.project().version,
14443            onetaskgraph_plugin_api::MetadataRecord::Document => item.document().version,
14444        }))
14445    }
14446
14447    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14448        self.delete_item(id).await
14449    }
14450
14451    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14452        self.delete_item(id).await
14453    }
14454
14455    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14456        self.delete_item(id).await
14457    }
14458
14459    /// One page of the task issue's own comments, walked by GitHub's own cursor.
14460    ///
14461    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14462    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14463    ///
14464    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14465    /// board is the read of its comments. A draft this process already resolved is refused
14466    /// without one.
14467    async fn task_comments(
14468        &self,
14469        task: &NativeId,
14470        page: &PageRequest,
14471    ) -> Result<Option<Page<Comment>>, SourceError> {
14472        validate_page(page)?;
14473        let cached = self.resolved_cache()?.get(task).cloned();
14474        if let Some(item) = cached {
14475            if item.kind != BoardKind::Work(ItemKind::Task) {
14476                return Ok(None);
14477            }
14478            if item.content_kind == ContentKind::DraftIssue {
14479                return Err(self.draft_has_no_comments(task));
14480            }
14481        }
14482        match self.issue_detail(task, page).await? {
14483            Some(TaskDetailRead {
14484                comments: Some(comments),
14485                ..
14486            }) => comments,
14487            _ => Ok(None),
14488        }
14489    }
14490
14491    /// Every id's task, with the first page of its comments when `comments` names it:
14492    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14493    /// comments in one [`graphql::ISSUE_DETAIL`] request.
14494    async fn get_task_details(
14495        &self,
14496        ids: &[NativeId],
14497        comments: Option<&PageRequest>,
14498    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14499        if let Some(page) = comments
14500            && let Err(error) = validate_page(page)
14501        {
14502            return ids.iter().map(|_| Err(error.clone())).collect();
14503        }
14504        match (ids, comments) {
14505            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14506            ([id], None) => vec![self.task_read(id).await],
14507            _ => self.issue_details(ids, comments).await,
14508        }
14509    }
14510
14511    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14512    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14513    /// request, so the dependency read a copy makes of each next is answered from this one.
14514    async fn get_task_details_for_copy(
14515        &self,
14516        ids: &[NativeId],
14517        comments: &PageRequest,
14518    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14519        // A page past the first is a page of one task's comments, which the detail read of it
14520        // alone answers.
14521        if comments.cursor.is_some() {
14522            return self.get_task_details(ids, Some(comments)).await;
14523        }
14524        if let Err(error) = validate_page(comments) {
14525            return ids.iter().map(|_| Err(error.clone())).collect();
14526        }
14527        let mut read = Vec::with_capacity(ids.len());
14528        for batch in ids.chunks(COPY_DETAIL_BATCH) {
14529            match self
14530                .graphql(
14531                    graphql::ISSUE_COPY_DETAILS,
14532                    copy_detail_batch(batch, comments, false),
14533                )
14534                .await
14535            {
14536                Ok(data) => {
14537                    for (slot, id) in batch.iter().enumerate() {
14538                        let read_one = match data.get(format!("i{slot}")) {
14539                            Some(node) => self.detail_of(id, node, true, None).await,
14540                            None => Err(SourceError::Malformed {
14541                                message: format!(
14542                                    "GitHub answered a batch read with no item for {}",
14543                                    id.0
14544                                ),
14545                            }),
14546                        };
14547                        read.push(read_one);
14548                    }
14549                }
14550                Err(error) if unresolvable_node(&error) => {
14551                    for id in batch {
14552                        read.push(self.issue_detail(id, comments).await);
14553                    }
14554                }
14555                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14556            }
14557        }
14558        read
14559    }
14560
14561    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14562    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14563    /// and held for the next read of each by id; see `read_ahead`.
14564    ///
14565    /// What this run wrote is never read: its own record answers, as it does for a read by id.
14566    /// Nor is an item a listing of its project carried whole earlier in this command — see
14567    /// `carried` — which is held in place of reading it.
14568    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14569    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14570    /// answer naming another issue than the one asked for, are left to their own reads too.
14571    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14572        // The origins asked just before go out now, beside the members of the one project this
14573        // names when it names one; a project those origins answered whole is held for its next
14574        // read by id rather than read again here.
14575        let hinted = match ids {
14576            [one] => Some(one),
14577            _ => None,
14578        };
14579        self.send_pending_origins(hinted).await?;
14580        if let Some(project) = hinted {
14581            let answered = self.resolved_cache()?.get(project).cloned();
14582            if let Some(answered) = answered
14583                && answered.kind == BoardKind::Work(ItemKind::Project)
14584            {
14585                self.read_ahead()?.insert(project.clone(), Some(answered));
14586                return Ok(());
14587            }
14588        }
14589        let mut unread: Vec<NativeId> = Vec::new();
14590        // Read whole by a listing of its project in this command, which a mutation naming it
14591        // would have dropped: held as a batch would have held it, once every batch is answered.
14592        let mut listed: Vec<NativeId> = Vec::new();
14593        {
14594            let created = self.created()?;
14595            let held = self.read_ahead()?;
14596            let carried = self.carried()?;
14597            for id in ids {
14598                if unread.contains(id)
14599                    || listed.contains(id)
14600                    || held.contains_key(id)
14601                    || created.iter().any(|own| own.id == *id)
14602                {
14603                    continue;
14604                }
14605                // An issue a listing carried without the whole of what blocks it is read in a
14606                // batch too, which carries that, rather than having its dependency read sent
14607                // on its own once the copy reconciles its edges.
14608                let whole = carried.get(id).is_some_and(|item| {
14609                    item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14610                });
14611                if whole {
14612                    listed.push(id.clone());
14613                } else {
14614                    unread.push(id.clone());
14615                }
14616            }
14617        }
14618        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14619        // what the item itself costs.
14620        let comments = PageRequest {
14621            cursor: None,
14622            limit: 1,
14623        };
14624        // Held only once every batch has been answered, so a read-ahead that fails part way
14625        // holds nothing — what a listing carried included, which stays carried — and every
14626        // item is read on its own, as the method promises.
14627        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14628        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14629        // answered them: what a project copy's orphan check asks, when that first id is its
14630        // project.
14631        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14632        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14633            let data = match self
14634                .graphql(
14635                    graphql::ISSUE_COPY_DETAILS,
14636                    copy_detail_batch(batch, &comments, index == 0),
14637                )
14638                .await
14639            {
14640                Ok(data) => data,
14641                Err(error) if unresolvable_node(&error) => continue,
14642                Err(error) => return Err(error),
14643            };
14644            for (slot, id) in batch.iter().enumerate() {
14645                let Some(node) = data.get(format!("i{slot}")) else {
14646                    continue;
14647                };
14648                let held = if node.is_null() {
14649                    None
14650                } else {
14651                    match optional_str(node, "__typename")? {
14652                        Some("DraftIssue") => continue,
14653                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
14654                        _ => {}
14655                    }
14656                    // Remembered below with the rest, so a later batch refused leaves no
14657                    // dependency read answered from this one either.
14658                    self.resolve_issue_as(node, false).await?
14659                };
14660                read.push((id.clone(), held));
14661            }
14662            if index == 0
14663                && let Some(node) = data.get("children").filter(|node| !node.is_null())
14664                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14665                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14666            {
14667                // A list GitHub did not answer is not an empty one: it would answer the project's
14668                // task listing with nothing, and the orphan check with no orphans.
14669                let listed = node
14670                    .pointer("/subIssues/nodes")
14671                    .and_then(Value::as_array)
14672                    .ok_or_else(|| SourceError::Malformed {
14673                        message: format!(
14674                            "GitHub answered the sub-issues of project {} with no list of them",
14675                            batch[0].0
14676                        ),
14677                    })?
14678                    .iter()
14679                    .map(|child| {
14680                        Ok((
14681                            required_str(child, "id")?.to_owned(),
14682                            required_str(child, "title")?.to_owned(),
14683                        ))
14684                    })
14685                    .collect::<Result<Vec<_>, SourceError>>()?;
14686                children = Some((batch[0].clone(), listed));
14687            }
14688        }
14689        // The project's tasks are known from this read alone when every child it named is held
14690        // from it, but for a document, which its title says is one and which a task listing
14691        // never answers: then a listing of the project's tasks is answered from them.
14692        if let Some((project, listed)) = children
14693            && read.iter().any(|(id, held)| {
14694                *id == project
14695                    && held
14696                        .as_ref()
14697                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14698            })
14699        {
14700            let mut tasks = Vec::new();
14701            let mut whole = true;
14702            for (child, title) in &listed {
14703                match read.iter().find(|(id, _)| id.0 == *child) {
14704                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14705                        if held.kind == BoardKind::Work(ItemKind::Task) {
14706                            tasks.push(held.clone());
14707                        }
14708                    }
14709                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14710                    _ => whole = false,
14711                }
14712            }
14713            if whole {
14714                self.task_children()?.insert(project, tasks);
14715            }
14716        }
14717        let mut resolved = self.resolved_cache()?;
14718        let mut ahead = self.read_ahead()?;
14719        let mut carried = self.carried()?;
14720        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14721            resolved.insert(item.id.clone(), item.clone());
14722        }
14723        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14724        // and its next read is then its own.
14725        for id in &listed {
14726            let Some(item) = carried.remove(id) else {
14727                continue;
14728            };
14729            // The whole of what it is blocked by came with it, so the dependency read a copy
14730            // makes next is answered from this one, as a copy's detail read is.
14731            if item.blocked_by.is_some() {
14732                resolved.insert(item.id.clone(), item.clone());
14733            }
14734            read.push((id.clone(), Some(item)));
14735        }
14736        for (id, held) in read {
14737            ahead.insert(id, held);
14738        }
14739        Ok(())
14740    }
14741
14742    /// Add one comment to the task's issue, as the account the token belongs to.
14743    ///
14744    /// The author is refused before anything is sent — not even the task is read — because
14745    /// no answer GitHub could give would make posting under another name than the one asked
14746    /// for the right outcome.
14747    async fn add_comment(
14748        &self,
14749        task: &NativeId,
14750        comment: &NewComment,
14751    ) -> Result<Option<Comment>, SourceError> {
14752        if let Some(author) = &comment.author {
14753            return Err(SourceError::Refused {
14754                message: format!(
14755                    "source {} cannot post a comment as {author:?}: GitHub records the account \
14756                     the token signs in as the author of every comment; next: leave --author \
14757                     out, and the comment is posted as that account",
14758                    self.name
14759                ),
14760            });
14761        }
14762        let Some(issue) = self.commented_issue(task).await? else {
14763            return Ok(None);
14764        };
14765        let data = self
14766            .graphql(
14767                graphql::ADD_COMMENT,
14768                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14769            )
14770            .await?;
14771        let subject = data
14772            .pointer("/addComment/subject")
14773            .filter(|value| !value.is_null())
14774            .ok_or_else(|| SourceError::Malformed {
14775                message: "GitHub comment addition returned no subject".into(),
14776            })?;
14777        if required_str(subject, "id")? != issue.0 {
14778            return Err(SourceError::Malformed {
14779                message: "GitHub comment addition answered about another issue".into(),
14780            });
14781        }
14782        let added = data
14783            .pointer("/addComment/commentEdge/node")
14784            .filter(|value| !value.is_null())
14785            .ok_or_else(|| SourceError::Malformed {
14786                message: "GitHub comment addition returned no comment".into(),
14787            })?;
14788        let added = comment_from(added)?;
14789        self.remember_commented(&issue)?;
14790        Ok(Some(added))
14791    }
14792
14793    async fn edit_comment(
14794        &self,
14795        task: &NativeId,
14796        comment: &NativeId,
14797        body: &CommentBody,
14798    ) -> Result<Option<Comment>, SourceError> {
14799        let Some(issue) = self.commented_issue(task).await? else {
14800            return Ok(None);
14801        };
14802        let Some(held) = self.comment_on(&issue, comment).await? else {
14803            return Ok(None);
14804        };
14805        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14806        let body = CommentOrigin::kept(&held, body)?;
14807        let data = self
14808            .graphql(
14809                graphql::UPDATE_COMMENT,
14810                json!({"input":{"id":comment.0,"body":body.as_str()}}),
14811            )
14812            .await?;
14813        let edited = data
14814            .pointer("/updateIssueComment/issueComment")
14815            .filter(|value| !value.is_null())
14816            .ok_or_else(|| SourceError::Malformed {
14817                message: "GitHub comment update returned no comment".into(),
14818            })?;
14819        let edited = comment_from(edited)?;
14820        if edited.id != *comment {
14821            return Err(SourceError::Malformed {
14822                message: "GitHub comment update returned the wrong comment".into(),
14823            });
14824        }
14825        self.remember_commented(&issue)?;
14826        Ok(Some(edited))
14827    }
14828
14829    async fn delete_comment(
14830        &self,
14831        task: &NativeId,
14832        comment: &NativeId,
14833    ) -> Result<Option<NativeId>, SourceError> {
14834        let Some(issue) = self.commented_issue(task).await? else {
14835            return Ok(None);
14836        };
14837        if !self.comment_is_on(&issue, comment).await? {
14838            return Ok(None);
14839        }
14840        let data = self
14841            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14842            .await?;
14843        // The payload says nothing about the comment it removed, so what is checked is that
14844        // GitHub answered the mutation at all rather than leaving it unanswered.
14845        data.get("deleteIssueComment")
14846            .filter(|value| !value.is_null())
14847            .ok_or_else(|| SourceError::Malformed {
14848                message: "GitHub comment deletion returned no payload".into(),
14849            })?;
14850        Ok(Some(comment.clone()))
14851    }
14852
14853    /// Every request this source has recorded, and what each of GitHub's two budgets was
14854    /// attributed — read off the same accounting the session report is rendered from, so
14855    /// the two cannot count one request two ways.
14856    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14857        Ok(Some(self.ledger.snapshot().metering()))
14858    }
14859
14860    /// Drop every item, search answer and board read this source holds, so the next command
14861    /// reads the board as a person has since left it.
14862    ///
14863    /// Every one of those is held on the assumption that nothing but this source writes the
14864    /// board while a command runs, which stops being true the moment the command is over: a
14865    /// body a person edited would be overwritten from the record held here, and a card they
14866    /// moved would be read as still where this source left it. The board's own field
14867    /// definitions go too, because a person can add or delete a `Status` option and a write
14868    /// resolved against the held list would not re-read on a miss. What stays is what stays
14869    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14870    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14871    /// accounting [`metering`](TaskSource::metering) answers from.
14872    ///
14873    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14874    /// refused, because clearing it is what puts it right.
14875    async fn end_command(&self) -> Result<(), SourceError> {
14876        fn clear<T: Default>(held: &Mutex<T>) {
14877            *held
14878                .lock()
14879                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14880            held.clear_poison();
14881        }
14882        clear(&self.created);
14883        clear(&self.updated);
14884        clear(&self.commented);
14885        clear(&self.board_cache);
14886        clear(&self.search_cache);
14887        clear(&self.narrowed_cache);
14888        clear(&self.search_next);
14889        clear(&self.resolved_cache);
14890        clear(&self.read_ahead);
14891        clear(&self.pending_origins);
14892        clear(&self.member_classes);
14893        clear(&self.carried);
14894        clear(&self.held_cache);
14895        clear(&self.children_cache);
14896        clear(&self.task_children);
14897        clear(&self.written);
14898        clear(&self.fields_cache);
14899        self.expected.end_command();
14900        Ok(())
14901    }
14902}
14903
14904/// One member of a project as a classification-only read answers it: its classification and
14905/// the repositories its work changes.
14906type MemberClass = (Classification, Vec<Repository>);
14907
14908/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14909/// for under, beside the project that selector named; see `children_cache`.
14910type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14911
14912/// One issue comment as the contract carries it.
14913///
14914/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14915/// and when it answers an actor with no login, because either way the source did not say who
14916/// wrote it — which is what an absent author means, rather than an author called nothing.
14917///
14918/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14919/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14920/// and is read back with the marker taken out and reported as its origin.
14921fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14922    Ok(Comment {
14923        origin: None,
14924        id: NativeId(required_str(value, "id")?.to_owned()),
14925        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14926            .map(str::to_owned),
14927        created_at: optional_time(value, "createdAt")?,
14928        updated_at: optional_time(value, "updatedAt")?,
14929        body: required_str(value, "body")?.to_owned(),
14930        url: optional_str(value, "url")?.map(str::to_owned),
14931    }
14932    .unmarked())
14933}
14934
14935/// The page of comments one issue node carries, resumed from `after`.
14936fn comment_page(
14937    node: &Value,
14938    issue: &str,
14939    after: Option<&str>,
14940) -> Result<Page<Comment>, SourceError> {
14941    let connection = node
14942        .get("comments")
14943        .filter(|value| !value.is_null())
14944        .ok_or_else(|| SourceError::Malformed {
14945            message: format!("GitHub issue {issue} answered with no comments connection"),
14946        })?;
14947    let items = optional_nodes(Some(connection), "issue comments")?
14948        .into_iter()
14949        .flatten()
14950        .map(comment_from)
14951        .collect::<Result<Vec<_>, _>>()?;
14952    let next = next_cursor(connection)?;
14953    if let Some(next) = &next {
14954        validate_cursor_progress(after, &next.0)?;
14955    }
14956    Ok(Page { items, next })
14957}
14958
14959/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14960/// end — `None` when it carried none, or a page with more past it.
14961fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14962    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14963        return Ok(None);
14964    };
14965    if next_cursor(connection)?.is_some() {
14966        return Ok(None);
14967    }
14968    Ok(Some(
14969        optional_nodes(Some(connection), "blocked-by issues")?
14970            .into_iter()
14971            .flatten()
14972            .cloned()
14973            .collect(),
14974    ))
14975}
14976
14977/// Where the recorded tail of a dependency walk resumes; see
14978/// [`GitHubProjectsSource::recorded_edges`].
14979const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
14980
14981/// The board text field this source keeps a copy's origin in.
14982///
14983/// Named after the key it holds, and held to that name by the guard below rather than by
14984/// a reader noticing.
14985const ORIGIN_FIELD: &str = "onetaskgraph.origin";
14986
14987/// The metadata key that field holds.
14988///
14989/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
14990/// constructs or interprets the qualified id it carries. This source names it only to
14991/// route it — a short, typed value belongs in a typed field rather than in the body slot
14992/// a caller's own prose shares.
14993///
14994/// Restated rather than imported, because no plugin crate may depend on the engine. What
14995/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
14996/// target in `check`: it reads the engine's own literal and fails naming the file and the
14997/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
14998/// that creates a second item every run instead of finding the one it wrote — and that is
14999/// too late to learn it.
15000const ORIGIN_KEY: &str = "onetaskgraph.origin";
15001
15002/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
15003///
15004/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
15005/// is derived from the far end, never written down on the near item — so only a forward
15006/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
15007/// it did not come from, and it is told so rather than answered with an empty page that
15008/// reads as a walk which ended.
15009fn recorded_offset(
15010    cursor: Option<&str>,
15011    direction: Direction,
15012) -> Result<Option<usize>, SourceError> {
15013    cursor
15014        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
15015        .map(|offset| {
15016            if direction != Direction::DependsOn {
15017                return Err(SourceError::Config {
15018                    message: format!(
15019                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
15020                         reverse dependency read never issues; resume it in the direction \
15021                         that reported it"
15022                    ),
15023                });
15024            }
15025            offset.parse().map_err(|_| SourceError::Config {
15026                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
15027            })
15028        })
15029        .transpose()
15030}
15031
15032/// What a dependency read's own answer says of a board draft: `None` when the item is not a
15033/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
15034/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
15035/// draft's metadata, for one it does — its board item naming this board by its number and by
15036/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
15037fn draft_slot(
15038    data: &Value,
15039    id: &NativeId,
15040    project_number: u32,
15041) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
15042    let Some(draft) = data
15043        .get("node")
15044        .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
15045    else {
15046        return Ok(None);
15047    };
15048    let Some(items) = data.pointer("/draft/projectV2Items") else {
15049        return Ok(None);
15050    };
15051    if required_str(draft, "id")? != id.0 {
15052        return Err(SourceError::Malformed {
15053            message: format!("GitHub answered a different draft for {}", id.0),
15054        });
15055    }
15056    let nodes = items
15057        .get("nodes")
15058        .and_then(Value::as_array)
15059        .ok_or_else(|| SourceError::Malformed {
15060            message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
15061        })?;
15062    let info = items
15063        .get("pageInfo")
15064        .ok_or_else(|| SourceError::Malformed {
15065            message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
15066        })?;
15067    if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
15068        return Err(SourceError::Malformed {
15069            message: format!(
15070                "GitHub draft {} reports more board items than the one GitHub links a draft to",
15071                id.0
15072            ),
15073        });
15074    }
15075    let board = data
15076        .pointer("/board/projectV2/id")
15077        .and_then(Value::as_str)
15078        .ok_or_else(|| SourceError::Malformed {
15079            message: format!(
15080                "GitHub answered draft {} without this board's own id beside it",
15081                id.0
15082            ),
15083        })?;
15084    let mut held = false;
15085    for node in nodes {
15086        let project = node.get("project").ok_or_else(|| SourceError::Malformed {
15087            message: format!("GitHub draft {} board item has no project", id.0),
15088        })?;
15089        let number = project
15090            .get("number")
15091            .and_then(Value::as_u64)
15092            .ok_or_else(|| SourceError::Malformed {
15093                message: format!(
15094                    "GitHub draft {} board item has no numeric project number",
15095                    id.0
15096                ),
15097            })?;
15098        let project_id = required_str(project, "id")?;
15099        held |= number == u64::from(project_number) && project_id == board;
15100    }
15101    if !held {
15102        return Ok(Some(None));
15103    }
15104    Ok(Some(Some(
15105        metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
15106    )))
15107}
15108
15109fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
15110    let mut page = offset_page(edges, offset, limit.max(1));
15111    page.next = page
15112        .next
15113        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
15114    page
15115}
15116
15117/// The kind of one issue reached through a dependency connection.
15118///
15119/// The same questions the board scan asks, over the fields the dependency document
15120/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
15121/// then anything with sub-issues or the marker is a project.
15122///
15123/// # Errors
15124///
15125/// A far end this board holds as a document is refused rather than reported. The two
15126/// answers that are not refusals would both be wrong: reporting it as a task names an id
15127/// no task read of this source can find, and reporting it as a project names one no
15128/// project read can. There is no third value to return — `ItemKind` has no document
15129/// variant, because nothing may point at a document — so the relationship itself is what
15130/// the person is told about.
15131fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
15132    let id = required_str(value, "id")?;
15133    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
15134        return Err(SourceError::Refused {
15135            message: format!(
15136                "GitHub issue {id} is a document of this board — its title begins \
15137                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
15138                 on by one; next: remove that issue's blocking relationship on this board"
15139            ),
15140        });
15141    }
15142    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
15143    if parent.is_some() {
15144        return Ok(ItemKind::Task);
15145    }
15146    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
15147    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
15148        message: format!("GitHub issue {id}: {message}"),
15149    })?;
15150    let sub_issues = sub_issue_total(value)?;
15151    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
15152        ItemKind::Project
15153    } else {
15154        ItemKind::Task
15155    })
15156}
15157
15158/// The `stateInput` that closes an issue for `reason`.
15159fn closed_input(reason: ClosedState) -> Value {
15160    json!({"value":"CLOSED","stateReason":reason.reason()})
15161}
15162
15163/// The `IssueStateUpdateInput` one status target asks for.
15164///
15165/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
15166/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
15167/// a currently-closed issue: without that the item would read back `Unknown` and a copy
15168/// would report a change forever. A document has no status at all, and asks for neither.
15169fn state_input(target: Option<&StatusTarget>) -> Value {
15170    match target {
15171        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
15172        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
15173        // A document has no status, so a write of one says nothing about the issue's open
15174        // or closed state rather than forcing it open: `stateInput` is what carries that
15175        // instruction, and an explicit null asks for no change to it.
15176        None => Value::Null,
15177    }
15178}
15179
15180/// The metadata one write stores in the item's body slot.
15181///
15182/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
15183/// rather than carried: the kind marker so an empty project stays readable, the
15184/// repository list only when it is not exactly the issue's own repository, and the far
15185/// ends no relationship here can name.
15186///
15187/// The copy origin is the one typed field that is also mirrored here, and only as a
15188/// mirror: it lands in the board's origin field as well, which stays the one every reader
15189/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
15190/// and catches up with a write in seconds rather than minutes — can find the item by it.
15191/// A reader of the release before this one drops the slot's copy and reads the field, so an
15192/// item written here still reads with exactly one origin there.
15193fn slot_metadata(
15194    incoming: &Incoming<'_>,
15195    own_repository: Option<&Repository>,
15196    fallback: &[DependencyEdge],
15197) -> BTreeMap<String, Value> {
15198    let mut metadata = incoming.metadata.clone();
15199    match metadata.remove(ORIGIN_KEY) {
15200        Some(Value::String(origin)) if !origin.is_empty() => {
15201            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
15202        }
15203        _ => {}
15204    }
15205    match incoming.written.kind() {
15206        BoardKind::Work(kind) => metadata.insert(
15207            ItemKind::METADATA_KEY.to_owned(),
15208            Value::String(kind.marker().to_owned()),
15209        ),
15210        // A document is told by its title, so it carries no kind marker: that key names
15211        // what a dependency endpoint points at, and nothing may point at a document.
15212        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
15213    };
15214    incoming.classification.record(&mut metadata);
15215    let derivable = own_repository
15216        .map(|own| incoming.repositories == [own.clone()])
15217        .unwrap_or(incoming.repositories.is_empty());
15218    if derivable {
15219        metadata.remove(Repository::METADATA_KEY);
15220    } else {
15221        metadata.insert(
15222            Repository::METADATA_KEY.to_owned(),
15223            Value::Array(
15224                incoming
15225                    .repositories
15226                    .iter()
15227                    .map(|repository| Value::String(repository.as_str().to_owned()))
15228                    .collect(),
15229            ),
15230        );
15231    }
15232    // The typed lists are what land, whatever the caller's own metadata held under their
15233    // keys: a key of either name travelling beside the field would otherwise be a second
15234    // answer to the same question, and the field is the one the contract names.
15235    for (key, entries) in [
15236        (TaskRef::DELIVERS_KEY, incoming.delivers),
15237        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
15238    ] {
15239        set_task_list(&mut metadata, key, entries);
15240    }
15241    record_edges(&mut metadata, fallback);
15242    metadata
15243}
15244
15245/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
15246/// one slot's metadata, or no such key when there are none.
15247fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
15248    if fallback.is_empty() {
15249        metadata.remove(DependencyEdge::RECORDED_KEY);
15250    } else {
15251        metadata.insert(
15252            DependencyEdge::RECORDED_KEY.to_owned(),
15253            Value::Array(
15254                fallback
15255                    .iter()
15256                    .map(|edge| {
15257                        serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
15258                    })
15259                    .collect(),
15260            ),
15261        );
15262    }
15263}
15264
15265/// Every label one item carries, from its content's own connection and nowhere else.
15266///
15267/// There is no second place to read one from: no document this source sends selects the
15268/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
15269/// cannot carry one at all. The module documentation records the three schema facts that
15270/// settle it.
15271fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
15272    optional_nodes(content.get("labels"), "content labels")?
15273        .into_iter()
15274        .flatten()
15275        .map(|v| {
15276            Ok(Label {
15277                id: NativeId(required_str(v, "id")?.to_owned()),
15278                name: required_str(v, "name")?.to_owned(),
15279                color: optional_str(v, "color")?.map(str::to_owned),
15280            })
15281        })
15282        .collect()
15283}
15284
15285/// Each board field value one item holds, as the change token's facets name it: the field's
15286/// name, with the single-select option's name or the text. A value of a type no read here
15287/// selects, and one naming no field, says nothing a person can rename and is left out.
15288fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
15289    field_values
15290        .iter()
15291        .filter_map(|value| {
15292            let field = value.pointer("/field/name")?.as_str()?;
15293            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
15294            Some((field.to_owned(), held.to_owned()))
15295        })
15296        .collect()
15297}
15298
15299/// The digest of the facets of the issue `content`, whose board item's values are
15300/// `field_values` — or `None` when the response did not carry, whole, every member they are read
15301/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
15302/// label, and every field value.
15303///
15304/// What a write's own response spells its token from, by exactly the rules a read takes them by.
15305fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
15306    let ended = |connection: &Value| {
15307        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
15308    };
15309    let labels = content.get("labels").filter(|labels| ended(labels))?;
15310    if !ended(field_values) {
15311        return None;
15312    }
15313    let parent = content.get("parent")?;
15314    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
15315    let kind = BoardKind::of(
15316        content.get("title")?.as_str()?,
15317        !parent.is_null(),
15318        sub_issue_total(content).ok()?,
15319        ItemKind::from_metadata(&slot).ok()?,
15320    );
15321    let labels = self::labels(&json!({"labels": labels})).ok()?;
15322    let nodes = field_values.get("nodes")?.as_array()?;
15323    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
15324}
15325
15326/// The definition of each board field one item's values are values of, in the shape a read
15327/// of the board's own `fields` gives one.
15328///
15329/// A value names its field through a fragment on that field's own type, so the type is
15330/// known from which kind of value it is: a single-select value's field is a
15331/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
15332/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
15333fn field_definitions(field_values: &[Value]) -> Vec<Value> {
15334    field_values
15335        .iter()
15336        .filter_map(|value| {
15337            let field = value.get("field")?.as_object()?;
15338            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
15339            let typename = if value.get("text").is_some() {
15340                "ProjectV2Field"
15341            } else if value.get("name").is_some() {
15342                "ProjectV2SingleSelectField"
15343            } else {
15344                return None;
15345            };
15346            let mut defined = field.clone();
15347            defined.insert("__typename".to_owned(), json!(typename));
15348            // Only a text field holds a text value, so the value says the field's type too.
15349            if typename == "ProjectV2Field" {
15350                defined.insert("dataType".to_owned(), json!("TEXT"));
15351            }
15352            Some(Value::Object(defined))
15353        })
15354        .collect()
15355}
15356
15357fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
15358    let Some(node) = field_values
15359        .iter()
15360        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
15361    else {
15362        return Ok(None);
15363    };
15364    Ok(optional_str(node, "text")?.map(str::to_owned))
15365}
15366
15367fn valid_github_owner(owner: &str) -> bool {
15368    !owner.is_empty()
15369        && owner.len() <= 39
15370        && !owner.starts_with('-')
15371        && !owner.ends_with('-')
15372        && !owner.contains("--")
15373        && owner
15374            .bytes()
15375            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
15376}
15377
15378/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
15379/// neither of the two names a path segment already means.
15380fn valid_github_repository_name(name: &str) -> bool {
15381    !name.is_empty()
15382        && name.len() <= 100
15383        && name != "."
15384        && name != ".."
15385        && name
15386            .bytes()
15387            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
15388}
15389
15390fn valid_environment_name(name: &str) -> bool {
15391    let mut bytes = name.bytes();
15392    bytes
15393        .next()
15394        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
15395        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
15396}
15397
15398/// How many sub-issues one issue has.
15399///
15400/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
15401/// absent or non-integer one is a response this source cannot read — and reading it as
15402/// zero would classify a project as a task, which is exactly the mistake the marker
15403/// exists to keep from happening quietly.
15404fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
15405    let summary = issue
15406        .get("subIssuesSummary")
15407        .ok_or_else(|| SourceError::Malformed {
15408            message: "GitHub issue is missing subIssuesSummary".into(),
15409        })?;
15410    summary
15411        .get("total")
15412        .and_then(Value::as_u64)
15413        .ok_or_else(|| SourceError::Malformed {
15414            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
15415        })
15416}
15417
15418/// One issue's own `number`.
15419///
15420/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
15421/// an issue in this module asks for it. So a read of one that comes back without it, or
15422/// with something that is not an unsigned integer, is a response this source cannot read —
15423/// absence here is **not** "this issue has no number". A draft is the content that has
15424/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
15425/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
15426fn issue_number(issue: &Value) -> Result<u64, SourceError> {
15427    issue
15428        .get("number")
15429        .and_then(Value::as_u64)
15430        .ok_or_else(|| SourceError::Malformed {
15431            message: "GitHub issue number is missing or is not an unsigned integer".into(),
15432        })
15433}
15434
15435/// The `number` a creating mutation answered with, and `None` when it answered without one;
15436/// why a missing one is tolerated is at the call in `create_and_file_issue`.
15437fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
15438    match created.get("number") {
15439        None | Some(Value::Null) => Ok(None),
15440        Some(value) => value
15441            .as_u64()
15442            .map(Some)
15443            .ok_or_else(|| SourceError::Malformed {
15444                message: "GitHub created issue number is not an unsigned integer".into(),
15445            }),
15446    }
15447}
15448
15449fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15450    value
15451        .get(field)
15452        .and_then(Value::as_str)
15453        .ok_or_else(|| SourceError::Malformed {
15454            message: format!("GitHub response is missing string field {field}"),
15455        })
15456}
15457
15458fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15459    let found = required_str(value, field)?;
15460    if found.trim().is_empty() {
15461        return Err(SourceError::Malformed {
15462            message: format!("GitHub response has blank string field {field}"),
15463        });
15464    }
15465    Ok(found)
15466}
15467
15468/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15469/// needs one — Linear spells them too, in its own description field.
15470///
15471/// Restated rather than shared, because a plugin crate depends on the contract crate and
15472/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15473/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15474/// source round-trips its own writes perfectly well under its own spelling.
15475const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15476const METADATA_CLOSE: &str = "\n-->";
15477
15478/// What the composer puts between a non-empty visible body and the slot, and the one thing
15479/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15480/// other trailing byte of the body comes back as it was written.
15481// 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.
15482const METADATA_SEPARATOR: &str = "\n\n";
15483
15484/// The visible body and the metadata slot at the end of it.
15485///
15486/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15487/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15488/// own content and is left alone. The visible body is everything before the slot less the
15489/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15490fn metadata_body(
15491    body: Option<String>,
15492) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15493    let Some(body) = body else {
15494        return Ok((None, BTreeMap::new()));
15495    };
15496    let Some(slot) = slot_span(&body)? else {
15497        return Ok((Some(body), BTreeMap::new()));
15498    };
15499    let metadata =
15500        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15501            SourceError::Malformed {
15502                message: format!(
15503                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15504                ),
15505            }
15506        })?;
15507    let before = &body[..slot.start];
15508    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15509    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15510}
15511
15512/// Where the metadata slot sits in one body, as byte offsets into it.
15513struct SlotSpan {
15514    /// Where [`METADATA_OPEN`] begins.
15515    start: usize,
15516    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15517    encoded_start: usize,
15518    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15519    encoded_end: usize,
15520    /// Just past [`METADATA_CLOSE`].
15521    end: usize,
15522}
15523
15524/// The slot at the very end of `body`, or `None` when it has none.
15525///
15526/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15527/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15528/// slot.
15529fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15530    let Some(start) = body.rfind(METADATA_OPEN) else {
15531        return Ok(None);
15532    };
15533    let encoded_start = start + METADATA_OPEN.len();
15534    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15535        return Err(SourceError::Malformed {
15536            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15537        });
15538    };
15539    let encoded_end = encoded_start + relative_end;
15540    let end = encoded_end + METADATA_CLOSE.len();
15541    if !body[end..].trim().is_empty() {
15542        return Ok(None);
15543    }
15544    Ok(Some(SlotSpan {
15545        start,
15546        encoded_start,
15547        encoded_end,
15548        end,
15549    }))
15550}
15551
15552/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15553/// slot as it was.
15554///
15555/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15556/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15557/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15558/// or alone in an empty body — and a body with no slot that is given no metadata is
15559/// returned as it is.
15560fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15561    let encoded = if metadata.is_empty() {
15562        None
15563    } else {
15564        Some(
15565            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15566                message: error.to_string(),
15567            })?,
15568        )
15569    };
15570    Ok(match (slot_span(body)?, encoded) {
15571        (Some(slot), Some(encoded)) => format!(
15572            "{}{encoded}{}",
15573            &body[..slot.encoded_start],
15574            &body[slot.encoded_end..]
15575        ),
15576        (Some(slot), None) => {
15577            let before = &body[..slot.start];
15578            format!(
15579                "{}{}",
15580                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15581                &body[slot.end..]
15582            )
15583        }
15584        (None, None) => body.to_owned(),
15585        (None, Some(encoded)) if body.is_empty() => {
15586            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15587        }
15588        (None, Some(encoded)) => {
15589            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15590        }
15591    })
15592}
15593
15594/// `body` with everything before its metadata slot replaced by `content`, and the slot
15595/// itself kept byte for byte.
15596///
15597/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15598/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15599/// `content` is empty — so a read of the result reports `content` as the visible body and
15600/// the slot's metadata exactly as it was.
15601fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15602    let Some(slot) = slot_span(body)? else {
15603        return Ok(content.to_owned());
15604    };
15605    let kept = &body[slot.start..];
15606    Ok(if content.is_empty() {
15607        kept.to_owned()
15608    } else {
15609        format!("{content}{METADATA_SEPARATOR}{kept}")
15610    })
15611}
15612
15613/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15614fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15615    if entries.is_empty() {
15616        metadata.remove(key);
15617    } else {
15618        metadata.insert(
15619            key.to_owned(),
15620            Value::Array(
15621                entries
15622                    .iter()
15623                    .map(|entry| Value::String(entry.as_str().to_owned()))
15624                    .collect(),
15625            ),
15626        );
15627    }
15628}
15629
15630fn compose_body(
15631    content: Option<&str>,
15632    metadata: &BTreeMap<String, Value>,
15633) -> Result<Option<String>, SourceError> {
15634    let visible = content.unwrap_or_default();
15635    if metadata.is_empty() {
15636        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15637    }
15638    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15639        message: error.to_string(),
15640    })?;
15641    Ok(Some(if visible.is_empty() {
15642        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15643    } else {
15644        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15645    }))
15646}
15647
15648fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15649    value
15650        .get(field)
15651        .and_then(Value::as_bool)
15652        .ok_or_else(|| SourceError::Malformed {
15653            message: format!("GitHub response is missing boolean field {field}"),
15654        })
15655}
15656fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15657    match value.get(field) {
15658        None | Some(Value::Null) => Ok(None),
15659        Some(value) => value
15660            .as_str()
15661            .map(Some)
15662            .ok_or_else(|| SourceError::Malformed {
15663                message: format!("GitHub response field {field} is not a string or null"),
15664            }),
15665    }
15666}
15667fn optional_nodes<'a>(
15668    connection: Option<&'a Value>,
15669    name: &str,
15670) -> Result<Option<&'a Vec<Value>>, SourceError> {
15671    match connection {
15672        None | Some(Value::Null) => Ok(None),
15673        Some(value) => value
15674            .get("nodes")
15675            .and_then(Value::as_array)
15676            .map(Some)
15677            .ok_or_else(|| SourceError::Malformed {
15678                message: format!("GitHub {name}.nodes is not an array"),
15679            }),
15680    }
15681}
15682fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15683    let page_info = connection
15684        .get("pageInfo")
15685        .ok_or_else(|| SourceError::Malformed {
15686            message: format!("GitHub {name} has no pageInfo"),
15687        })?;
15688    if required_bool(page_info, "hasNextPage")? {
15689        return Err(SourceError::Malformed {
15690            message: format!(
15691                "GitHub {name} exceeds the supported nested connection size of {size}"
15692            ),
15693        });
15694    }
15695    Ok(())
15696}
15697fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15698    optional_str(value, field)?
15699        .map(|timestamp| {
15700            timestamp.parse().map_err(|error| SourceError::Malformed {
15701                message: format!("GitHub response field {field} is not a timestamp: {error}"),
15702            })
15703        })
15704        .transpose()
15705}
15706fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15707    if page.limit == 0 {
15708        Err(SourceError::Config {
15709            message: "page limit must be at least 1".into(),
15710        })
15711    } else {
15712        Ok(())
15713    }
15714}
15715fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15716    let page = connection
15717        .get("pageInfo")
15718        .filter(|value| value.is_object())
15719        .ok_or_else(|| SourceError::Malformed {
15720            message: "GitHub connection is missing pageInfo".into(),
15721        })?;
15722    if required_bool(page, "hasNextPage")? {
15723        let cursor = required_str(page, "endCursor")?;
15724        validate_cursor_progress(None, cursor)?;
15725        Ok(Some(Cursor(cursor.into())))
15726    } else {
15727        Ok(None)
15728    }
15729}
15730fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15731    if next.is_empty() || previous == Some(next) {
15732        Err(SourceError::Malformed {
15733            message: "GitHub pagination cursor is empty or did not advance".into(),
15734        })
15735    } else {
15736        Ok(())
15737    }
15738}
15739/// The version of this plugin's opaque narrowing-search cursor.
15740pub const SEARCH_CURSOR_VERSION: u32 = 4;
15741
15742#[derive(Serialize, Deserialize)]
15743#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15744enum SearchConnection {
15745    Initial {},
15746    Continuing { after: Cursor },
15747    Exhausted {},
15748}
15749impl SearchConnection {
15750    fn after(&self) -> Option<&str> {
15751        match self {
15752            Self::Continuing { after } => Some(&after.0),
15753            _ => None,
15754        }
15755    }
15756    fn exhausted(&self) -> bool {
15757        matches!(self, Self::Exhausted { .. })
15758    }
15759    /// Whether a cursor naming this position, `offset` rows into its page, is one this
15760    /// plugin could have handed out: a page is resumed only part of the way through it — an
15761    /// offset of a whole page or more would skip rows nobody was given — an initial page
15762    /// only once some of it was handed out, and an exhausted connection has no page to be
15763    /// part of the way through.
15764    fn valid_resume(&self, offset: usize) -> bool {
15765        let within = offset < SEARCH_PAGE_SIZE as usize;
15766        match self {
15767            Self::Initial { .. } => offset > 0 && within,
15768            Self::Continuing { after } => !after.0.is_empty() && within,
15769            Self::Exhausted { .. } => offset == 0,
15770        }
15771    }
15772}
15773
15774/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15775#[derive(Serialize, Deserialize)]
15776#[serde(deny_unknown_fields)]
15777struct SearchPosition {
15778    version: u32,
15779    connection: SearchConnection,
15780    /// How many rows of the page `connection` starts were already handed out.
15781    #[serde(default, skip_serializing_if = "is_zero")]
15782    offset: usize,
15783    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15784    seen: Vec<NativeId>,
15785    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15786    own: Vec<NativeId>,
15787}
15788impl Default for SearchPosition {
15789    fn default() -> Self {
15790        Self {
15791            version: SEARCH_CURSOR_VERSION,
15792            connection: SearchConnection::Initial {},
15793            offset: 0,
15794            seen: Vec::new(),
15795            own: Vec::new(),
15796        }
15797    }
15798}
15799
15800fn is_zero(offset: &usize) -> bool {
15801    *offset == 0
15802}
15803
15804fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15805    cursor.map_or(Ok(0), |c| {
15806        c.0.parse().map_err(|_| SourceError::Config {
15807            message: "page cursor is invalid".into(),
15808        })
15809    })
15810}
15811fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15812    if offset > items.len() {
15813        return Page::last(vec![]);
15814    }
15815    let tail = items.split_off(offset);
15816    let mut selected = tail;
15817    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15818    selected.truncate(limit);
15819    Page {
15820        items: selected,
15821        next,
15822    }
15823}
15824
15825impl GitHubProjectsSource {
15826    async fn write_task_assets(
15827        &self,
15828        write: &ItemWrite<Task>,
15829        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15830    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15831        self.write_item(
15832            &self.task_incoming(write, assets)?,
15833            write.target.as_ref(),
15834            &write.depends_on,
15835        )
15836        .await
15837    }
15838
15839    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15840    /// are known to be ones this source can write.
15841    fn task_incoming<'a>(
15842        &self,
15843        write: &'a ItemWrite<Task>,
15844        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15845    ) -> Result<Incoming<'a>, SourceError> {
15846        let near = write.target.as_ref().unwrap_or(&write.item.id);
15847        for (key, entries) in [
15848            (TaskRef::DELIVERS_KEY, &write.item.delivers),
15849            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15850        ] {
15851            TaskRef::listed(key, near, Some(&self.name), entries.clone())
15852                .map_err(|message| SourceError::Refused { message })?;
15853        }
15854        if self.priorities.is_none() && write.item.priority != Priority::None {
15855            return Err(self.holds_no_priority());
15856        }
15857        Ok(Incoming {
15858            written: Written::Work(ItemKind::Task, &write.item.status),
15859            title: &write.item.title,
15860            content: write.item.content.as_deref(),
15861            assets,
15862            labels: &write.item.labels,
15863            metadata: &write.item.metadata,
15864            repositories: &write.item.repositories,
15865            classification: write.item.classification,
15866            parent: write.item.project.as_ref(),
15867            delivers: &write.item.delivers,
15868            delivered_by: &write.item.delivered_by,
15869            priority: self.priorities.as_ref().map(|_| write.item.priority),
15870        })
15871    }
15872    async fn write_document_assets(
15873        &self,
15874        write: &ItemWrite<Document>,
15875        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15876    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15877        // A document takes part in no dependency graph, so there is no far end to write
15878        // natively and none to record: a caller naming one is told so rather than having it
15879        // stored under the reserved key, where a later read would report an edge the
15880        // contract says cannot exist.
15881        if !write.depends_on.is_empty() {
15882            return Err(SourceError::Refused {
15883                message: format!(
15884                    "this write names {} dependencies for a document, and a document takes \
15885                     part in no dependency graph; next: put the dependency on the task or \
15886                     project the document is about",
15887                    write.depends_on.len()
15888                ),
15889            });
15890        }
15891        self.write_item(
15892            &Incoming {
15893                written: Written::Document,
15894                title: &write.item.title,
15895                content: write.item.content.as_deref(),
15896                assets,
15897                labels: &write.item.labels,
15898                metadata: &write.item.metadata,
15899                repositories: &write.item.repositories,
15900                classification: write.item.classification,
15901                parent: write.item.project.as_ref(),
15902                delivers: &[],
15903                delivered_by: &[],
15904                priority: None,
15905            },
15906            write.target.as_ref(),
15907            &[],
15908        )
15909        .await
15910    }
15911}
15912
15913/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15914/// itself, for the two things no journey can observe.
15915///
15916/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15917/// with and without the call, that a settlement, a board listing and a metadata search each
15918/// read afresh after it — the resolved records, the written-item overlay, the board and its
15919/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15920/// because a status write naming an option a person deleted is refused the same whether or
15921/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15922/// while one of its locks is held. So these assert those directly, and every other holder
15923/// beside them so a holder added later without a clear in the call fails here.
15924#[cfg(test)]
15925mod end_command_tests {
15926    use super::*;
15927
15928    struct Token;
15929
15930    impl SecretResolver for Token {
15931        fn get(&self, var: &str) -> Option<SecretString> {
15932            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15933        }
15934    }
15935
15936    fn source() -> GitHubProjectsSource {
15937        let config = serde_json::from_value(json!({
15938            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15939            // Nothing here is sent: the source is only built and its state inspected.
15940            "endpoint": "http://127.0.0.1:9/graphql",
15941        }))
15942        .expect("a usable configuration");
15943        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15944            .expect("the source builds")
15945    }
15946
15947    /// One issue as a board read answers it.
15948    fn resolved(source: &GitHubProjectsSource) -> Resolved {
15949        source
15950            .resolve(&json!({
15951                "id": "ITEM-1",
15952                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15953                            "body": "what a person may since have edited", "state": "OPEN",
15954                            "stateReason": null, "url": null, "number": 1,
15955                            "subIssuesSummary": {"total": 0},
15956                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15957                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15958            }))
15959            .expect("the item reads")
15960            .expect("an issue")
15961    }
15962
15963    /// Hold something in every holder the call clears, and the repository id it keeps.
15964    fn fill(source: &GitHubProjectsSource) {
15965        let item = resolved(source);
15966        source.created.lock().unwrap().push(item.clone());
15967        source.updated.lock().unwrap().push(item.clone());
15968        *source.board_cache.lock().unwrap() = Some(Board {
15969            id: "PVT-board".into(),
15970            fields: json!({"nodes": []}),
15971            items: vec![item.clone()],
15972        });
15973        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
15974        source
15975            .narrowed_cache
15976            .lock()
15977            .unwrap()
15978            .insert("status:todo".into(), vec![item.clone()]);
15979        source.children_cache.lock().unwrap().insert(
15980            NativeId("P-1".into()),
15981            (NativeId("P-1".into()), vec![item.clone()]),
15982        );
15983        source
15984            .search_next
15985            .lock()
15986            .unwrap()
15987            .insert("status:todo".into(), Some("cursor".into()));
15988        source
15989            .resolved_cache
15990            .lock()
15991            .unwrap()
15992            .insert(item.id.clone(), item);
15993        *source.fields_cache.lock().unwrap() = Some(BoardFields {
15994            id: BoardId::parse("PVT-board").unwrap(),
15995            fields: json!({"nodes": []}),
15996        });
15997        source
15998            .repository_cache
15999            .lock()
16000            .unwrap()
16001            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
16002    }
16003
16004    fn assert_dropped(source: &GitHubProjectsSource) {
16005        assert!(source.created().unwrap().is_empty(), "created");
16006        assert!(source.updated().unwrap().is_empty(), "updated");
16007        assert!(source.board_cache().unwrap().is_none(), "board");
16008        assert!(source.search_cache.lock().unwrap().is_none(), "search");
16009        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
16010        assert!(
16011            source.children_cache.lock().unwrap().is_empty(),
16012            "project children"
16013        );
16014        assert!(
16015            source.search_next.lock().unwrap().is_empty(),
16016            "search paging"
16017        );
16018        assert!(
16019            source.resolved_cache().unwrap().is_empty(),
16020            "resolved records"
16021        );
16022        assert!(source.fields_cache().unwrap().is_none(), "board fields");
16023        assert_eq!(
16024            source.repository_cache().unwrap().len(),
16025            1,
16026            "a repository's node id stays valid and is kept"
16027        );
16028    }
16029
16030    fn end(source: &GitHubProjectsSource) {
16031        tokio::runtime::Builder::new_current_thread()
16032            .build()
16033            .unwrap()
16034            .block_on(source.end_command())
16035            .expect("the command ends");
16036    }
16037
16038    #[test]
16039    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
16040        let source = source();
16041        fill(&source);
16042        end(&source);
16043        assert_dropped(&source);
16044    }
16045
16046    #[test]
16047    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
16048        fn poison<T: Send>(held: &Mutex<T>) {
16049            std::thread::scope(|scope| {
16050                let _ = scope
16051                    .spawn(|| {
16052                        let _guard = held.lock().unwrap();
16053                        panic!("a failure while the lock is held");
16054                    })
16055                    .join();
16056            });
16057            assert!(held.is_poisoned());
16058        }
16059        let source = source();
16060        fill(&source);
16061        poison(&source.created);
16062        poison(&source.updated);
16063        poison(&source.board_cache);
16064        poison(&source.search_cache);
16065        poison(&source.narrowed_cache);
16066        poison(&source.search_next);
16067        poison(&source.resolved_cache);
16068        poison(&source.fields_cache);
16069        assert!(
16070            source.resolved_cache().is_err(),
16071            "a poisoned lock is refused before the call"
16072        );
16073        end(&source);
16074        assert_dropped(&source);
16075    }
16076}
16077
16078#[cfg(test)]
16079mod unprojectable_fields_tests {
16080    use super::*;
16081
16082    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
16083    fn documented() -> std::collections::BTreeSet<String> {
16084        let document = std::fs::read_to_string(
16085            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
16086        )
16087        .expect("docs/metadata.md is readable");
16088        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
16089        let lead = "a `field` equal, ignoring case, to ";
16090        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
16091        let sentence = &document[start..];
16092        let sentence = &sentence[..sentence
16093            .find("; and two entries naming one field")
16094            .expect("the refused-field sentence ends at the duplicate rule")];
16095        let (own, github) = sentence
16096            .split_once(" or a field GitHub owns on every board (")
16097            .expect("the sentence names GitHub's own fields");
16098        own.split('`')
16099            .skip(1)
16100            .step_by(2)
16101            .chain(
16102                github
16103                    .strip_suffix(')')
16104                    .expect("GitHub's own fields close the sentence")
16105                    .split(", "),
16106            )
16107            .map(str::to_owned)
16108            .collect()
16109    }
16110
16111    #[test]
16112    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
16113        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
16114            .iter()
16115            .map(|&name| name.to_owned())
16116            .collect();
16117        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
16118        let instance = SourceName::new("work").unwrap();
16119        for name in UNPROJECTABLE_FIELDS {
16120            for spelled in [name.to_owned(), name.to_uppercase()] {
16121                let entry = MetadataFieldConfig {
16122                    field: spelled.clone(),
16123                    key: "team.machine".to_owned(),
16124                    path: Vec::new(),
16125                };
16126                let refused = MetadataField::resolve(vec![entry], &instance)
16127                    .expect_err("an unprojectable field is refused");
16128                assert!(
16129                    refused.to_string().contains(&format!("{name:?}")),
16130                    "{spelled}: {refused}"
16131                );
16132            }
16133        }
16134    }
16135}